工具结果与 custom 流事件改用 describeToolResult/describeStreamPayload 输出摘要,替代截断 200 字符的原始内容;thread_id 取 userInfo._id
48 lines
1.7 KiB
JavaScript
48 lines
1.7 KiB
JavaScript
/**
|
|
* 工具结果 / 流事件的安全日志摘要
|
|
*
|
|
* 工具返回里可能包含患者姓名、手机号、健康档案、订单地址等健康数据,
|
|
* 直接记录正文(即使只截前 200 字)也会把这些信息落盘。
|
|
* 这里只提取排障需要的元信息:长度、success 标记、字段名。
|
|
*/
|
|
|
|
/**
|
|
* 工具结果摘要
|
|
* @param {*} content 工具返回内容(通常是 JSON 字符串)
|
|
* @returns {{chars: number, success?: boolean, errorCode?: string}}
|
|
*/
|
|
export function describeToolResult(content) {
|
|
const text = typeof content === "string" ? content : "";
|
|
const info = { chars: text.length };
|
|
if (!text) return info;
|
|
|
|
try {
|
|
const parsed = JSON.parse(text);
|
|
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
if (typeof parsed.success === "boolean") info.success = parsed.success;
|
|
// 只接受短错误码;错误文案可能包含用户输入的姓名/手机号,不记录
|
|
if (typeof parsed.errorCode === "string" && parsed.errorCode.length <= 64) {
|
|
info.errorCode = parsed.errorCode;
|
|
}
|
|
}
|
|
} catch {
|
|
// 纯文本或截断后的 JSON 不解析,也不记录正文
|
|
}
|
|
|
|
return info;
|
|
}
|
|
|
|
/**
|
|
* 流事件摘要:只记录数据类型/字段名,不记录字段值
|
|
* @param {*} data custom 流事件数据
|
|
* @returns {{type?: string, chars?: number, items?: number, keys?: string[]}}
|
|
*/
|
|
export function describeStreamPayload(data) {
|
|
if (typeof data === "string") return { type: "string", chars: data.length };
|
|
if (Array.isArray(data)) return { type: "array", items: data.length };
|
|
if (data && typeof data === "object") {
|
|
return { type: "object", keys: Object.keys(data).slice(0, 10) };
|
|
}
|
|
return { type: typeof data };
|
|
}
|