25 lines
985 B
TypeScript
25 lines
985 B
TypeScript
/** 去掉 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() };
|
||
}
|