/** * 去除模型推理与调试包装,供 Markdown 展示与出货模块使用。 * 仅处理明确的推理标签、纯调试 JSON 和开头技术诊断行,不改写正常业务回答。 */ export const stripReasoningPrelude = (text: string) => { if (!text) return ''; let cleaned = text .replace(//gi, '') .replace(//gi, '') .trim(); if (/^(?:\{|\[)/.test(cleaned)) { try { const parsed = JSON.parse(cleaned) as unknown; if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) { const keys = Object.keys(parsed as Record); const technicalKeys = new Set([ 'workflow_run_id', 'node_id', 'node_execution_id', 'status', 'error', 'outputs', 'inputs', 'metadata', 'traceback', 'tool_output', 'tool_response', ]); if (keys.length > 0 && keys.every((key) => technicalKeys.has(key))) return ''; } } catch { // 非完整 JSON 继续按普通回答展示。 } } const lines = cleaned.split('\n'); while ( lines.length > 1 && /^\s*(?:workflow(?:_run)?_id|node(?:_execution)?_id|tool(?:_output|_response)?|traceback|status|error)(?:[_\s-]*(?:code|message))?\s*[:=]/i.test(lines[0] || '') ) { lines.shift(); } cleaned = lines.join('\n').trim(); const reasoningMarkers = [ '让我分析', '根据应答生成规则', '分析知识库返回的内容', '我需要直接给出答案', '思考过程', '推理过程', ]; const hasReasoningMarker = reasoningMarkers.some((marker) => cleaned.includes(marker)); if (!hasReasoningMarker) return cleaned; const headingMatch = cleaned.match(/(^|\n)\s{0,3}#{1,6}\s+/); if (headingMatch && typeof headingMatch.index === 'number') { const start = headingMatch.index + (headingMatch[1] ? headingMatch[1].length : 0); cleaned = cleaned.slice(start).trim(); } return cleaned; };