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 };
|
||||
}
|
||||
Reference in New Issue
Block a user