From 68ecdebbc0c965d9d7e7caf85c5c7dfa9e4a2c51 Mon Sep 17 00:00:00 2001 From: Glen Maddern Date: Tue, 24 Jun 2025 18:15:57 +1000 Subject: [PATCH] fix: Clean think tags from reasoning content before display - Add cleaning logic to remove and tags from reasoning - Trim whitespace after tag removal for clean display - Add debug logging to show before/after cleaning - The middleware extracts reasoning but includes the original tags This ensures the reasoning blocks show clean thinking content without the XML tags that were being captured along with the reasoning text. --- examples/chat-ui/src/hooks/useStreamResponse.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/examples/chat-ui/src/hooks/useStreamResponse.ts b/examples/chat-ui/src/hooks/useStreamResponse.ts index 0f6daa4..abf6a4f 100644 --- a/examples/chat-ui/src/hooks/useStreamResponse.ts +++ b/examples/chat-ui/src/hooks/useStreamResponse.ts @@ -323,14 +323,24 @@ export const useStreamResponse = ({ // Add reasoning to the final message if we collected any if (reasoning && assistantMessageCreated) { debugLog(`[useStreamResponse] Adding collected reasoning:`, reasoning.substring(0, 100)) + + // Clean the reasoning content by removing think tags + let cleanedReasoning = reasoning + // Remove opening and closing think tags + cleanedReasoning = cleanedReasoning.replace(//g, '').replace(/<\/think>/g, '') + // Trim any leading/trailing whitespace + cleanedReasoning = cleanedReasoning.trim() + + debugLog(`[useStreamResponse] Cleaned reasoning:`, cleanedReasoning.substring(0, 100)) + setConversations((prev) => { const updated = [...prev] const conv = updated.find((c) => c.id === conversationId) if (conv && assistantMessageIndex >= 0) { const assistantMessage = conv.messages[assistantMessageIndex] if (assistantMessage && hasContent(assistantMessage) && assistantMessage.role === 'assistant') { - assistantMessage.reasoning = reasoning - debugLog(`[useStreamResponse] Added reasoning to assistant message at index ${assistantMessageIndex}`) + assistantMessage.reasoning = cleanedReasoning + debugLog(`[useStreamResponse] Added cleaned reasoning to assistant message at index ${assistantMessageIndex}`) } } return updated