- guard.js:流式输出护栏,跨 chunk 禁用词拦截并在句末截断超长回复 - history.js:tool 结果入历史前截断、历史按完整轮次裁剪,避免断链与上下文挤占 - reference.js:服务项目/协议改为可读文本渲染,替代裸 JSON 误导模型 - 临时脚本 test_restricted_backend.mjs 转为 test/restricted_fs_backend.test.js
64 lines
2.3 KiB
JavaScript
64 lines
2.3 KiB
JavaScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
|
|
import { OutputGuard, OUTPUT_MAX_CHARS } from "../agent/infra/guard.js";
|
|
|
|
// 按服务端聚合方式跑完一整条流,返回最终下发给客户端的文本
|
|
function runStream(chunks, options) {
|
|
const guard = new OutputGuard(options);
|
|
const parts = [];
|
|
for (const chunk of chunks) {
|
|
const text = guard.feed(chunk);
|
|
if (text) parts.push(text);
|
|
}
|
|
const tail = guard.finish();
|
|
if (tail) parts.push(tail);
|
|
return { text: parts.join(""), guard };
|
|
}
|
|
|
|
test("跨 chunk 的禁用词不会漏过,也不留下孤立标点", () => {
|
|
const { text, guard } = runStream(["作为", "AI", ",我不能给出诊断。"]);
|
|
assert.equal(text, "我不能给出诊断。");
|
|
assert.deepEqual(guard.hits, ["作为AI"]);
|
|
});
|
|
|
|
test("禁用词在句中时修补重复标点", () => {
|
|
const { text } = runStream(["嗯,作为AI。我认为你说得对。"]);
|
|
assert.equal(text, "嗯。我认为你说得对。");
|
|
});
|
|
|
|
test("整条回复只有禁用词时输出为空", () => {
|
|
const { text } = runStream(["作为", "AI", "助手"]);
|
|
assert.equal(text, "");
|
|
});
|
|
|
|
test("禁用词落在流末尾时在 finish 阶段拦截", () => {
|
|
const { text } = runStream(["回答仅供参考,", "希望对您有帮助"]);
|
|
assert.equal(text, "回答仅供参考,");
|
|
});
|
|
|
|
test("普通文本不吞字,尾部 holdback 在 finish 阶段补发", () => {
|
|
const { text } = runStream(["您好", ",陪诊流程是:先预约,再确认。"]);
|
|
assert.equal(text, "您好,陪诊流程是:先预约,再确认。");
|
|
});
|
|
|
|
test("超长回复在句末截断并补省略号,之后不再输出", () => {
|
|
const sentence = "陪诊服务分为预约、确认、陪同就诊三步。";
|
|
const { text, guard } = runStream([sentence.repeat(10), "后面还有很长的内容。"], { maxChars: 120 });
|
|
|
|
assert.ok(text.length <= 120);
|
|
assert.ok(text.endsWith("。…"));
|
|
assert.equal(guard.done, true);
|
|
});
|
|
|
|
test("没有句末可切时按上限硬切", () => {
|
|
const { text } = runStream(["这是一段没有句号的很长文本".repeat(20)], { maxChars: 50 });
|
|
assert.ok(text.length <= 50);
|
|
assert.ok(text.endsWith("…"));
|
|
});
|
|
|
|
test("两类 agent 的默认上限符合预期", () => {
|
|
assert.equal(OUTPUT_MAX_CHARS.escort, 400);
|
|
assert.equal(OUTPUT_MAX_CHARS.admin, 600);
|
|
});
|