import logger from "../../utils/logger.js"; /** * 输出护栏(主 agent 与管理 agent 共用) * * 流式输出无法回撤:字符一旦发给客户端,就会被追加进气泡。 * 因此这里只做两件在流式下可做对的兜底: * 1. 禁用词拦截:命中即整段剔除;尾部若可能是禁用词的前缀就先不发送, * 避免「作为A」+「I」这种跨 chunk 的命中被漏过; * 2. 超长截断:总输出超过上限时在最近句末/换行处截断并补省略号,之后不再输出。 * 篇幅与文风仍由提示词负责,这里不放宽口径。 */ // 单条回复的字符上限:用户侧咨询应为 1-3 句,管理侧要列记录,留更大余量 export const OUTPUT_MAX_CHARS = { escort: 400, admin: 600 }; // 身份泄露与空泛客套:两个 agent 都不该出现 const IDENTITY = [ "作为AI", "作为一个AI", "作为人工智能", "作为一个人工智能", "作为语言模型", "作为大模型", "我是一个AI", "我是AI", "AI助手", // 与上面重叠的完整自我描述:按长度优先匹配,避免只删掉前半截留下「助手」 "作为AI助手", "作为一个AI助手", "作为一个人工智能助手", "我是一个AI助手", "我是AI助手", "我是您的AI助手", ]; const COURTESY = [ "很高兴为您服务", "很高兴为您解答", "希望对您有帮助", "希望这对您有帮助", "您还有什么问题吗", "还有其他问题吗", "祝您早日康复", ]; // 越界承诺:医疗结果与服务可得性都不能打包票 const OVERPROMISE = [ "一定能治好", "一定可以治好", "保证治好", "保证治愈", "包治百病", "百分之百安全", "绝对安全", ]; export const OUTPUT_FORBIDDEN = [...IDENTITY, ...COURTESY, ...OVERPROMISE]; // 句末候选:中英文标点 + 换行,列表项按换行断开更自然 const SENTENCE_ENDS = ["。", "!", "?", ";", "\n", "!", "?", ";"]; // 在限额内尽量切在句末;找不到时硬切,并去掉尾部悬挂的标点/空白 function pickCut(text, room) { // 留一个字符给省略号 const window = text.slice(0, Math.max(room - 1, 1)); const minIndex = Math.floor(window.length * 0.4); let cutAt = -1; for (const ch of SENTENCE_ENDS) { const index = window.lastIndexOf(ch); if (index >= minIndex && index > cutAt) { cutAt = index; } } const cut = cutAt >= 0 ? window.slice(0, cutAt + 1) : window; return cut.replace(/[\s,、,]+$/u, ""); } export class OutputGuard { constructor({ label = "agent", maxChars = 400, forbidden = OUTPUT_FORBIDDEN } = {}) { this.label = label; this.maxChars = maxChars; // 长的先匹配:避免「作为AI助手」被「作为AI」先删成半截 this.forbidden = forbidden.filter(Boolean).sort((a, b) => b.length - a.length); this.buffer = ""; this.emitted = 0; this.done = false; this.hits = []; // 禁用词被整段剔除后,下一段开头可能残留逗号/句号,需要一并吞掉 this._stripLeading = false; } /** 输入一段模型输出,返回可以发送给客户端的文本(可能为空串) */ feed(chunk) { if (this.done || !chunk) return ""; if (this.emitted >= this.maxChars) { this.done = true; logger.warn(`[OutputGuard:${this.label}] 输出超过上限 ${this.maxChars} 字,后续内容已丢弃`); return ""; } const text = this.buffer + chunk; this.buffer = ""; if (this.emitted + text.length > this.maxChars) { const cut = pickCut(text, this.maxChars - this.emitted); // 截断点若落在禁用词中间,退到词首,避免把半截词发出去 const hold = this._holdback(cut); const kept = hold > 0 ? cut.slice(0, cut.length - hold) : cut; // 已经输出过内容时,若剩余额度放不下一句完整的话,直接以省略号收尾, // 避免把下一段的前几个字拼成半句(如「。后面还有…」) const endsAtSentence = SENTENCE_ENDS.includes(kept.slice(-1)); const fragment = this.emitted > 0 && kept && !endsAtSentence ? "" : kept; this.done = true; const out = `${this._sanitize(fragment)}…`; this.emitted += out.length; logger.warn(`[OutputGuard:${this.label}] 输出超过上限 ${this.maxChars} 字,已在句末截断`, { emitted: this.emitted, dropped: text.length - fragment.length, }); return out; } const hold = this._holdback(text); const ready = hold > 0 ? text.slice(0, text.length - hold) : text; if (hold > 0) { this.buffer = text.slice(text.length - hold); } const out = this._sanitize(ready); this.emitted += out.length; return out; } /** 流结束时补发 holdback 里的残余文本 */ finish() { if (this.done) return ""; this.done = true; const out = this._sanitize(this.buffer); this.buffer = ""; this.emitted += out.length; return out; } // 尾部若可能是某个禁用词的前缀,先留在 buffer 等下一段拼起来再判断; // 完整命中也要留(作为它自身的前缀),避免「作为」刚发出就在下一段补上「AI」 _holdback(text) { let hold = 0; for (const word of this.forbidden) { const max = Math.min(word.length, text.length); for (let len = max; len > hold; len--) { if (word.startsWith(text.slice(text.length - len))) { hold = len; break; } } } return hold; } _sanitize(text) { if (!text) return ""; let out = text; if (this._stripLeading) { out = out.replace(/^[,、,;;::。.!!??\s]+/u, ""); // 整段都是标点时继续保持标记,等下一段真正有正文再清除 this._stripLeading = out === ""; } if (!this.forbidden.length) return out; let hit = false; for (const word of this.forbidden) { if (!out.includes(word)) continue; const removed = out.split(word).length - 1; out = out.split(word).join(""); if (!this.hits.includes(word)) this.hits.push(word); hit = true; logger.warn(`[OutputGuard:${this.label}] 拦截禁用词「${word}」`, { removed }); } if (!hit) return out; // 剔除后留下的重复标点做最小修补;整段被剔除时空结果不宜直接发送 out = out .replace(/([,、,])[,、,]+/gu, "$1") .replace(/([。!?!?])[。!?!?]+/gu, "$1") .replace(/[,、,]\s*([。!?!?])/gu, "$1") .replace(/^[,、,;;::。.!!??\s]+/u, ""); this._stripLeading = out === ""; return out; } } export default OutputGuard;