From 59022dc5ddc3c8bb242a4dd42fb154a89ef725d2 Mon Sep 17 00:00:00 2001 From: lik Date: Thu, 19 Nov 2026 19:47:58 +0800 Subject: [PATCH] =?UTF-8?q?fix(agent):=20=E6=94=BE=E5=AE=BD=E5=87=BA?= =?UTF-8?q?=E8=AF=8A=E6=B8=85=E5=8D=95=E8=BE=93=E5=87=BA=E4=B8=8E=E5=8E=86?= =?UTF-8?q?=E5=8F=B2=E5=B7=A5=E5=85=B7=E7=BB=93=E6=9E=9C=E4=BF=9D=E7=95=99?= =?UTF-8?q?=E9=95=BF=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 输出上限支持按端配置并放宽到 escort 3000 / admin 5000;医院与网页类工具结果入历史保留 3000 字,数据库类维持 300 字,避免出诊信息被截断或追问时丢失。 --- agent/bots/bot-escort-admin/agent.js | 8 +++++--- agent/bots/bot-escort/agent.js | 8 +++++--- agent/infra/guard.js | 21 +++++++++++++++++++-- agent/infra/history.js | 18 +++++++++++++++++- conf.json | 4 ++++ test/guard.test.js | 15 ++++++++++++--- test/logic.test.js | 24 ++++++++++++++++++++++++ 7 files changed, 86 insertions(+), 12 deletions(-) diff --git a/agent/bots/bot-escort-admin/agent.js b/agent/bots/bot-escort-admin/agent.js index b2d95b5..25bc1c4 100644 --- a/agent/bots/bot-escort-admin/agent.js +++ b/agent/bots/bot-escort-admin/agent.js @@ -8,7 +8,7 @@ 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"; +import { OutputGuard, OUTPUT_MAX_CHARS, resolveOutputMaxChars } from "../../infra/guard.js"; // 密钥由 conf.json 统一提供;百度地图 token 由 http_get 按域名在服务端注入 process.env.TAVILY_API_KEY ??= config.agent.tavily?.apiKey; @@ -37,8 +37,10 @@ export default class EscortAdminAgent { this.temperature = options.temperature ?? 0.0; // 历史消息字符数上限(管理端多步工具调用更易堆积上下文) this.maxHistoryChars = options.maxHistoryChars || config.agent.maxHistoryChars || 40000; - // 单条回复字符上限:管理端要列记录,比咨询侧留更大余量 - this.maxOutputChars = options.maxOutputChars || config.agent.maxOutputChars || OUTPUT_MAX_CHARS.admin; + // 单条回复字符上限:管理端要转述记录与医院清单,默认值需能完整输出一次排班列表; + // 可在 conf.json agent.maxOutputChars 调整 + this.maxOutputChars = options.maxOutputChars + || resolveOutputMaxChars(config.agent.maxOutputChars, "admin", OUTPUT_MAX_CHARS.admin); } clearMessages(userInfo) { diff --git a/agent/bots/bot-escort/agent.js b/agent/bots/bot-escort/agent.js index d9334ad..77a1aa5 100644 --- a/agent/bots/bot-escort/agent.js +++ b/agent/bots/bot-escort/agent.js @@ -16,7 +16,7 @@ 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 { OutputGuard, OUTPUT_MAX_CHARS, resolveOutputMaxChars } from "../../infra/guard.js"; import { webFetchTool, webSearchTool, getCalendarInfoTool, getYearHolidaysTool, httpGetTool, createEscortRecordQueryTool, createHealthProfileQueryTool, @@ -35,8 +35,10 @@ export default class EscortAgent { this.maxHistoryChars = options.maxHistoryChars || config.agent.maxHistoryChars || 40000; // 咨询场景需要一点表达自由度:温度为 0 时共情回复会退化为固定句式 this.temperature = options.temperature ?? 0.3; - // 单条回复字符上限:提示词要求 1-3 句,超长时由输出护栏兜底截断 - this.maxOutputChars = options.maxOutputChars || config.agent.maxOutputChars || OUTPUT_MAX_CHARS.escort; + // 单条回复字符上限:提示词要求 1-3 句,护栏只兜底异常长文, + // 默认值需能完整输出一次出诊/排班清单;可在 conf.json agent.maxOutputChars 调整 + this.maxOutputChars = options.maxOutputChars + || resolveOutputMaxChars(config.agent.maxOutputChars, "escort", OUTPUT_MAX_CHARS.escort); } clearMessages() { diff --git a/agent/infra/guard.js b/agent/infra/guard.js index f8925a6..81e727c 100644 --- a/agent/infra/guard.js +++ b/agent/infra/guard.js @@ -11,8 +11,25 @@ import logger from "../../utils/logger.js"; * 篇幅与文风仍由提示词负责,这里不放宽口径。 */ -// 单条回复的字符上限:用户侧咨询应为 1-3 句;管理侧要转述医院信息,留更大余量 -export const OUTPUT_MAX_CHARS = { escort: 400, admin: 1800 }; +// 单条回复的字符上限:需完整容纳一次出诊/排班清单,日常对话的篇幅仍由提示词控制在 1-3 句。 +// 这里只是防止模型输出异常长文的兜底,不应把正常的清单类回答截断。 +export const OUTPUT_MAX_CHARS = { escort: 3000, admin: 5000 }; + +/** + * 解析单条回复上限:配置可写成数字(两端共用)或按端对象 { escort, admin }, + * 非法值回落到默认上限。 + * @param {number|Object|undefined} configured conf.json 中的 agent.maxOutputChars + * @param {"escort"|"admin"} role 当前 agent 角色 + * @param {number} fallback 默认上限 + * @returns {number} + */ +export function resolveOutputMaxChars(configured, role, fallback) { + if (typeof configured === "number" && Number.isFinite(configured) && configured > 0) { + return configured; + } + const value = configured?.[role]; + return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : fallback; +} // 身份泄露与空泛客套:两个 agent 都不该出现 const IDENTITY = [ diff --git a/agent/infra/history.js b/agent/infra/history.js index 6e974b5..5ada3ca 100644 --- a/agent/infra/history.js +++ b/agent/infra/history.js @@ -13,6 +13,16 @@ import logger from "../../utils/logger.js"; // tool 结果保留的字符数,超出部分以标记代替 export const TOOL_RESULT_MAX_CHARS = 300; +// 网页正文/医院档案类结果:下一轮追问常要引用其中的排班、流程明细,保留更长的开头 +export const LARGE_TOOL_RESULT_MAX_CHARS = 3000; +// 这些工具的输出属于“资料型”内容,数据库类查询仍用上面的小上限,避免记录列表挤占对话 +export const LARGE_TOOL_RESULT_TOOLS = new Set([ + "hospital_knowledge_read", + "hospital_knowledge_list", + "web_fetch", + "web_search", + "http_get", +]); // 子代理(task 工具)的完整回答:主 agent 需要据此转述,上限放宽,仍避免整篇塞进历史 export const SUBAGENT_RESULT_MAX_CHARS = 4000; @@ -30,7 +40,13 @@ export function appendToolMessage(messages, msg, max) { messages.push(msg); return msg; } - const limit = max ?? (msg.name === "task" ? SUBAGENT_RESULT_MAX_CHARS : TOOL_RESULT_MAX_CHARS); + const limit = max ?? ( + msg.name === "task" + ? SUBAGENT_RESULT_MAX_CHARS + : LARGE_TOOL_RESULT_TOOLS.has(msg.name) + ? LARGE_TOOL_RESULT_MAX_CHARS + : TOOL_RESULT_MAX_CHARS + ); const stored = Object.assign(Object.create(Object.getPrototypeOf(msg)), msg); if (typeof stored.content === "string") { stored.content = truncateToolResult(stored.content, limit); diff --git a/conf.json b/conf.json index 482fd9d..eba523f 100644 --- a/conf.json +++ b/conf.json @@ -18,6 +18,10 @@ }, "agent": { "maxHistoryChars": 40000, + "maxOutputChars": { + "escort": 3000, + "admin": 5000 + }, "dataDir": "./data", "model": "deepseek", "media": { diff --git a/test/guard.test.js b/test/guard.test.js index 2e14bf9..953ef4b 100644 --- a/test/guard.test.js +++ b/test/guard.test.js @@ -1,7 +1,7 @@ import { test } from "node:test"; import assert from "node:assert/strict"; -import { OutputGuard, OUTPUT_MAX_CHARS } from "../agent/infra/guard.js"; +import { OutputGuard, OUTPUT_MAX_CHARS, resolveOutputMaxChars } from "../agent/infra/guard.js"; // 按服务端聚合方式跑完一整条流,返回最终下发给客户端的文本 function runStream(chunks, options) { @@ -58,6 +58,15 @@ test("没有句末可切时按上限硬切", () => { }); test("两类 agent 的默认上限符合预期", () => { - assert.equal(OUTPUT_MAX_CHARS.escort, 400); - assert.equal(OUTPUT_MAX_CHARS.admin, 1800); + assert.equal(OUTPUT_MAX_CHARS.escort, 3000); + assert.equal(OUTPUT_MAX_CHARS.admin, 5000); +}); + +test("输出上限支持数字与按端配置,非法值回落到默认", () => { + assert.equal(resolveOutputMaxChars({ escort: 1200, admin: 2400 }, "escort", 3000), 1200); + assert.equal(resolveOutputMaxChars({ escort: 1200, admin: 2400 }, "admin", 5000), 2400); + assert.equal(resolveOutputMaxChars(2000, "admin", 5000), 2000); + assert.equal(resolveOutputMaxChars(undefined, "escort", 3000), 3000); + assert.equal(resolveOutputMaxChars({ escort: 0 }, "escort", 3000), 3000); + assert.equal(resolveOutputMaxChars({ admin: "bad" }, "admin", 5000), 5000); }); diff --git a/test/logic.test.js b/test/logic.test.js index c5d57f9..56d3388 100644 --- a/test/logic.test.js +++ b/test/logic.test.js @@ -13,6 +13,11 @@ import { applyBaiduAuthorization, buildRequestUrl, } from "../agent/infra/tools/web/http_get.js"; +import { + appendToolMessage, + TOOL_RESULT_MAX_CHARS, + LARGE_TOOL_RESULT_MAX_CHARS, +} from "../agent/infra/history.js"; test("订单状态机只允许正向流转", () => { assert.equal(canTransition("pending", "confirmed"), true); @@ -162,3 +167,22 @@ test("状态机表覆盖全部订单状态", () => { ["cancelled", "none", "requested"] ); }); + +test("历史中医院/网页结果保留更长开头,数据库结果维持短截断", () => { + // 2000 字:数据库类仍截到 300,医院/网页类完整保留(原上限 300 会丢失出诊清单后半段) + const medium = "医".repeat(2000); + const dbStored = appendToolMessage([], { name: "escort_record_query", content: medium }); + assert.equal(dbStored.content.slice(0, TOOL_RESULT_MAX_CHARS), medium.slice(0, TOOL_RESULT_MAX_CHARS)); + assert.ok(dbStored.content.includes("已截断")); + + for (const name of ["hospital_knowledge_read", "web_fetch"]) { + const stored = appendToolMessage([], { name, content: medium }); + assert.equal(stored.content, medium); + } + + // 超长内容:医院/网页类截到 3000,而不是 300 + const long = "医".repeat(5000); + const largeStored = appendToolMessage([], { name: "hospital_knowledge_read", content: long }); + assert.equal(largeStored.content.slice(0, LARGE_TOOL_RESULT_MAX_CHARS), long.slice(0, LARGE_TOOL_RESULT_MAX_CHARS)); + assert.ok(largeStored.content.includes("已截断")); +});