From 9a825d5afc1bab9f735def6244270a0021a37715 Mon Sep 17 00:00:00 2001 From: Glen Maddern Date: Tue, 24 Jun 2025 17:28:08 +1000 Subject: [PATCH] fix: Remove thinking XML tags from main message content - Add regex-based cleaning to remove ... tags from display text - Handle both closed and unclosed think tags in streaming content - Apply cleaning only for reasoning-capable models to avoid unnecessary processing - Update conversation content with cleaned response instead of raw response This ensures thinking tags are extracted for reasoning display but don't appear in the main message content, providing a clean user experience. --- examples/chat-ui/src/hooks/useStreamResponse.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/examples/chat-ui/src/hooks/useStreamResponse.ts b/examples/chat-ui/src/hooks/useStreamResponse.ts index 10d7a85..b4004a0 100644 --- a/examples/chat-ui/src/hooks/useStreamResponse.ts +++ b/examples/chat-ui/src/hooks/useStreamResponse.ts @@ -268,15 +268,23 @@ export const useStreamResponse = ({ aiResponse = aiResponse.replace(/.*?<\/chat-title>/, '').trim() } + // Remove thinking tags from the display text (they should be extracted by middleware) + let cleanedResponse = aiResponse + if (supportsReasoning(selectedModel)) { + cleanedResponse = aiResponse.replace(/[\s\S]*?<\/think>/g, '').trim() + // Also handle unclosed think tags + cleanedResponse = cleanedResponse.replace(/[\s\S]*$/g, '').trim() + } + setConversations((prev) => { const updated = [...prev] const conv = updated.find((c) => c.id === conversationId) if (conv) { - debugLog(`[useStreamResponse] Updating conversation message content (length: ${aiResponse.length})`) + debugLog(`[useStreamResponse] Updating conversation message content (length: ${cleanedResponse.length})`) // Update the specific assistant message we created, not just the last message const assistantMessage = conv.messages[assistantMessageIndex] if (assistantMessage && hasContent(assistantMessage) && assistantMessage.role === 'assistant') { - assistantMessage.content = aiResponse + assistantMessage.content = cleanedResponse debugLog(`[useStreamResponse] Updated assistant message at index ${assistantMessageIndex}`) } else { debugLog(`[useStreamResponse] Could not find assistant message at index ${assistantMessageIndex}`, assistantMessage)