fix(agent): 放宽出诊清单输出与历史工具结果保留长度
输出上限支持按端配置并放宽到 escort 3000 / admin 5000;医院与网页类工具结果入历史保留 3000 字,数据库类维持 300 字,避免出诊信息被截断或追问时丢失。
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
+19
-2
@@ -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 = [
|
||||
|
||||
+17
-1
@@ -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);
|
||||
|
||||
@@ -18,6 +18,10 @@
|
||||
},
|
||||
"agent": {
|
||||
"maxHistoryChars": 40000,
|
||||
"maxOutputChars": {
|
||||
"escort": 3000,
|
||||
"admin": 5000
|
||||
},
|
||||
"dataDir": "./data",
|
||||
"model": "deepseek",
|
||||
"media": {
|
||||
|
||||
+12
-3
@@ -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);
|
||||
});
|
||||
|
||||
@@ -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("已截断"));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user