Files
dukang/server/dukang-api/src/integrations/wecom/wecom-bot-reply.util.ts
T
2026-08-04 21:38:49 +08:00

25 lines
985 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/** 去掉 LLM 误输出的 JSON / API 占位,避免展示给用户 */
export function sanitizeWecomUserReply(text: string): string {
let s = text.trim();
s = s.replace(/```(?:json)?\s*[\s\S]*?```/gi, '').trim();
s = s.replace(/\{\s*"api"\s*:[\s\S]*?\}/gi, '').trim();
s = s.replace(/请稍等[,]?系统正在检索[^\n]*/gi, '').trim();
s = s.replace(/^我来帮您[^\n]*\n+/i, '').trim();
if (!s) {
return '未能生成有效回复。请使用「帮助」中的指令,或换一种问法。';
}
return s;
}
/** 解析 LLM 工具行:TOOL support_tickets_open 或 TOOL order_lookup DK123 */
export function parseWecomToolLine(raw: string): { name: string; args: string } | null {
const line = raw
.split('\n')
.map((l) => l.trim())
.find((l) => /^TOOL\s+\S+/i.test(l));
if (!line) return null;
const m = line.match(/^TOOL\s+(\S+)(?:\s+(.*))?$/i);
if (!m) return null;
return { name: m[1].toLowerCase(), args: (m[2] ?? '').trim() };
}