import type { AgentRunSnapshot } from "../canvas-agent-runtime/protocol"; /** Only ordinary assistant text belongs in chat; never use reasoning/tool events. */ export function publicAssistantText(text: string) { return text.replace(/[\s\S]*?<\/think>/gi, "") .replace(/\bZCode\b/gi, "Agent") .replace(/(?:sk-[\w-]{12,}|Bearer\s+[\w.-]+)/gi, "[已隐藏]"); } export function canvasAgentReplyText( snapshot: Pick & { verification?: AgentRunSnapshot["verification"]; }, ) { const final = publicAssistantText(snapshot.message || "").trim(); if (snapshot.verification && snapshot.verification.passed === false && final) { return final; } const text = snapshot.assistantText || ""; return !final || text.trimEnd().endsWith(final) ? text : text ? `${text.trimEnd()}\n\n${final}` : final; }