fix: Improve thinking tag removal with more aggressive cleaning

- Add multiple regex passes to handle various think tag scenarios
- Remove standalone <think> and </think> tags that might remain
- Clean leading whitespace after tag removal
- Add debug logging to track text cleaning process
- Handle streaming cases where tags appear before closing tags arrive

This provides more robust cleaning to ensure no think tags appear
in the final message content during streaming scenarios.
This commit is contained in:
Glen Maddern 2025-06-24 17:35:14 +10:00
commit 03dd988f35
2 changed files with 11 additions and 2 deletions

View file

@ -34,7 +34,7 @@ const ChatMessage: React.FC<ChatMessageProps> = ({
className={`
${
message.role === "user"
? "max-w-[80%] rounded-2xl px-3 py-2 border border-zinc-200/10 bg-zinc-100 text-black"
? "max-w-[80%] rounded-2xl px-3 py-2 border border-zinc-200/10 bg-white text-black"
: " text-zinc-900 w-full"
}`}
>

View file

@ -271,9 +271,18 @@ export const useStreamResponse = ({
// Remove thinking tags from the display text (they should be extracted by middleware)
let cleanedResponse = aiResponse
if (supportsReasoning(selectedModel)) {
debugLog(`[useStreamResponse] Original response:`, aiResponse.substring(0, 100))
// Remove complete thinking blocks
cleanedResponse = aiResponse.replace(/<think>[\s\S]*?<\/think>/g, '').trim()
// Also handle unclosed think tags
// Remove unclosed think tags (everything from <think> to end)
cleanedResponse = cleanedResponse.replace(/<think>[\s\S]*$/g, '').trim()
// Remove any remaining opening think tags
cleanedResponse = cleanedResponse.replace(/<think>/g, '').trim()
// Remove any remaining closing think tags
cleanedResponse = cleanedResponse.replace(/<\/think>/g, '').trim()
// Clean up any remaining whitespace or newlines at the start
cleanedResponse = cleanedResponse.replace(/^\s+/, '')
debugLog(`[useStreamResponse] Cleaned response:`, cleanedResponse.substring(0, 100))
}
setConversations((prev) => {