refactor(agent): 抽出 guard/history/reference 共享模块并补测试

- guard.js:流式输出护栏,跨 chunk 禁用词拦截并在句末截断超长回复

- history.js:tool 结果入历史前截断、历史按完整轮次裁剪,避免断链与上下文挤占

- reference.js:服务项目/协议改为可读文本渲染,替代裸 JSON 误导模型

- 临时脚本 test_restricted_backend.mjs 转为 test/restricted_fs_backend.test.js
This commit is contained in:
lik
2026-11-01 11:42:11 +08:00
parent 13981c047b
commit 9825ac376f
6 changed files with 468 additions and 58 deletions
+56
View File
@@ -0,0 +1,56 @@
/**
* 参考信息的文本化渲染(主 agent 与管理 agent 共用)
*
* 直接 JSON.stringify 的问题:
* 1. services 里的 booking / image / icon / tag 是前端配置,模型会把
* booking.sessions(如 08:00—17:00)误读成「可与用户约定的可约时段」,造成错误许诺;
* 2. 裸 JSON 更耗 token,且模型解析散文优于 JSON。
* 因此只渲染问答真正需要的字段,公示文案(subtitle / notices)保持逐字原文。
*/
export function formatServices(services) {
const areas = services?.[0]?.areas;
const lines = [];
if (areas?.length) {
lines.push(`服务区域:${areas.join('、')}`);
}
lines.push("(以下价格为公示价,单位:元)");
for (const s of services ?? []) {
lines.push("");
lines.push(`${s.title}|${s.price} 元|${s.subtitle ?? ""}`.trim());
if (s.description) {
lines.push(`适用:${s.description}`);
}
if (s.flow?.length) {
lines.push(`流程:${s.flow.map((step) => step.title).join(" → ")}`);
}
for (const notice of s.notices ?? []) {
lines.push(`· ${notice}`);
}
}
return lines.join("\n");
}
export function formatAgreement(agreement) {
const lines = [`${agreement.title}(原文摘要)`];
for (const section of agreement.sections ?? []) {
lines.push("");
lines.push(`【${section.number}. ${section.title}】`);
for (const item of section.items ?? []) {
lines.push(`- ${item}`);
}
if (section.content) {
lines.push(section.content);
}
if (section.table?.rows?.length) {
const { headers = [], rows } = section.table;
lines.push(headers.join(" / "));
for (const row of rows) {
lines.push(row.join(" / "));
}
}
}
return lines.join("\n");
}