| 1 | export function isWorkflowToolOutputText(text: string): boolean { |
| 2 | const value = text.trim() |
| 3 | return /^context\.md updated for stage \w+\./.test(value) || value === 'thinking.md updated' |
| 4 | } |
| 5 | |
| 6 | export function normalizeThinkingAssistantReply(text: string): string { |
| 7 | let next = text.trim() |
| 8 | if (!next || isWorkflowToolOutputText(next)) return '' |
| 9 | |
| 10 | // Some providers stream tool-call JSON arguments as text before the actual reply. |
| 11 | // Keep the human-facing tail instead of persisting the internal argument payload. |
| 12 | if (/^"?topic"?\s*:/.test(next) && next.includes('"userIntent"')) { |
| 13 | const end = next.indexOf('}\n\n') |
| 14 | if (end >= 0) { |
| 15 | next = next.slice(end + 3).trim() |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | return isWorkflowToolOutputText(next) ? '' : next |
| 20 | } |
| 21 | |
| 22 | export function normalizeThinkingMessages<T extends { role: string; content: string }>( |
| 23 | messages: T[] |
| 24 | ): T[] { |
| 25 | return messages.flatMap((message) => { |
| 26 | if (message.role !== 'assistant') return message.content.trim() ? [message] : [] |
| 27 | const content = normalizeThinkingAssistantReply(message.content) |
| 28 | return content ? [{ ...message, content }] : [] |
| 29 | }) |
| 30 | } |
| 31 |