feat: Agent 工具日志结构化描述
工具结果与 custom 流事件改用 describeToolResult/describeStreamPayload 输出摘要,替代截断 200 字符的原始内容;thread_id 取 userInfo._id
This commit is contained in:
@@ -7,6 +7,7 @@ import config from '../../../config.js';
|
||||
import logger from '../../../utils/logger.js';
|
||||
import { createRestrictedBackend } from "../../infra/restricted_fs_backend.js";
|
||||
import { appendToolMessage, trimHistory } from "../../infra/history.js";
|
||||
import { describeToolResult, describeStreamPayload } from "../../infra/tool_log.js";
|
||||
import { OutputGuard, OUTPUT_MAX_CHARS } from "../../infra/guard.js";
|
||||
|
||||
// 密钥由 conf.json 统一提供;百度地图 token 由 http_get 按域名在服务端注入
|
||||
@@ -130,7 +131,7 @@ export default class EscortAdminAgent {
|
||||
for (const msg of data_.messages ?? []) {
|
||||
if (msg.type === "tool") {
|
||||
appendToolMessage(session.messages, msg);
|
||||
logger.info(`Tool complete: ${msg.name}, Result: ${String(msg.content).slice(0, 200)}`);
|
||||
logger.info(`Tool complete: ${msg.name}`, describeToolResult(msg.content));
|
||||
}
|
||||
}
|
||||
} else if (nodeName === "model_request") {
|
||||
@@ -165,7 +166,7 @@ export default class EscortAdminAgent {
|
||||
callback(source, "tool", message.text, message.id);
|
||||
}
|
||||
} else if (mode === "custom") {
|
||||
logger.info("custom: ", data);
|
||||
logger.info("custom event", describeStreamPayload(data));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ process.env.BAIDU_MAP_AUTH_TOKEN ??= config.agent.baiduMap?.authToken;
|
||||
import Prompts from "./prompts.js";
|
||||
import { createRestrictedBackend } from "../../infra/restricted_fs_backend.js";
|
||||
import { appendToolMessage, trimHistory } from "../../infra/history.js";
|
||||
import { describeToolResult, describeStreamPayload } from "../../infra/tool_log.js";
|
||||
import { OutputGuard, OUTPUT_MAX_CHARS } from "../../infra/guard.js";
|
||||
import {
|
||||
webFetchTool, webSearchTool, getCalendarInfoTool, getYearHolidaysTool,
|
||||
@@ -103,7 +104,7 @@ export default class EscortAgent {
|
||||
recursion_limit: 50,
|
||||
streamMode: ["updates", "messages", "custom"], subgraphs: true,
|
||||
configurable: {
|
||||
thread_id: msgs[0].userId
|
||||
thread_id: String(userInfo?._id ?? "")
|
||||
}
|
||||
})) {
|
||||
const isSubagent = namespace.some(s => s.startsWith("tools:"));
|
||||
@@ -119,7 +120,7 @@ export default class EscortAgent {
|
||||
for (const msg of data_.messages ?? []) {
|
||||
if (msg.type === "tool") {
|
||||
appendToolMessage(this.messages, msg);
|
||||
logger.info(`Tool complete: ${msg.name}, Result: ${String(msg.content).slice(0, 200)}`);
|
||||
logger.info(`Tool complete: ${msg.name}`, describeToolResult(msg.content));
|
||||
}
|
||||
}
|
||||
} else if (nodeName === "model_request") {
|
||||
@@ -157,7 +158,7 @@ export default class EscortAgent {
|
||||
callback(source, "tool", message.text, message.id);
|
||||
}
|
||||
} else if (mode === "custom") {
|
||||
logger.info("custom: ", data);
|
||||
logger.info("custom event", describeStreamPayload(data));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* 工具结果 / 流事件的安全日志摘要
|
||||
*
|
||||
* 工具返回里可能包含患者姓名、手机号、健康档案、订单地址等健康数据,
|
||||
* 直接记录正文(即使只截前 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 };
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
import { describeToolResult, describeStreamPayload } from "../agent/infra/tool_log.js";
|
||||
|
||||
test("工具结果日志只保留长度与 success,不落患者信息", () => {
|
||||
const content = JSON.stringify({
|
||||
success: true,
|
||||
data: {
|
||||
patient: { name: "张三", mobile: "13800138000" },
|
||||
address: "北京市东城区某小区 1 号楼",
|
||||
},
|
||||
});
|
||||
|
||||
const summary = describeToolResult(content);
|
||||
assert.deepEqual(summary, { chars: content.length, success: true });
|
||||
|
||||
const logged = JSON.stringify(summary);
|
||||
assert.equal(logged.includes("张三"), false);
|
||||
assert.equal(logged.includes("13800138000"), false);
|
||||
assert.equal(logged.includes("北京市"), false);
|
||||
});
|
||||
|
||||
test("工具失败只记录 errorCode,不记录可能带姓名的错误文案", () => {
|
||||
const content = JSON.stringify({
|
||||
success: false,
|
||||
error: "未找到患者张三的订单",
|
||||
errorCode: "NOT_FOUND",
|
||||
});
|
||||
|
||||
const summary = describeToolResult(content);
|
||||
assert.deepEqual(summary, {
|
||||
chars: content.length,
|
||||
success: false,
|
||||
errorCode: "NOT_FOUND",
|
||||
});
|
||||
assert.equal(JSON.stringify(summary).includes("张三"), false);
|
||||
});
|
||||
|
||||
test("纯文本或截断后的工具结果只记录长度", () => {
|
||||
assert.deepEqual(describeToolResult("患者张三,手机号 13800138000"), {
|
||||
chars: 20,
|
||||
});
|
||||
assert.deepEqual(describeToolResult(""), { chars: 0 });
|
||||
});
|
||||
|
||||
test("custom 流事件只记录类型/字段名,不记录字段值", () => {
|
||||
const summary = describeStreamPayload({
|
||||
type: "progress",
|
||||
content: "正在读取张三的健康档案",
|
||||
});
|
||||
|
||||
assert.deepEqual(summary, {
|
||||
type: "object",
|
||||
keys: ["type", "content"],
|
||||
});
|
||||
assert.equal(JSON.stringify(summary).includes("张三"), false);
|
||||
|
||||
assert.deepEqual(describeStreamPayload("张三的订单"), { type: "string", chars: 5 });
|
||||
assert.deepEqual(describeStreamPayload([1, 2, 3]), { type: "array", items: 3 });
|
||||
});
|
||||
Reference in New Issue
Block a user