Add intelligent content filtering to extract clean final answer

- Extract only the final answer from post-tool reasoning content
- Filter out thinking/reasoning patterns to prevent duplication
- Use regex matching and line filtering to identify clean response content
- Skip content that appears to be thinking rather than final answer
- Limit response length to prevent long thinking content from appearing
This commit is contained in:
Glen Maddern 2025-06-25 08:21:08 +10:00
commit 0ed5dbaa67

View file

@ -439,10 +439,52 @@ export const useStreamResponse = ({
if (hasProcessedToolResults && postToolReasoningContent.trim()) { if (hasProcessedToolResults && postToolReasoningContent.trim()) {
debugLog(`[useStreamResponse] Processing post-tool reasoning as final response:`, postToolReasoningContent.length, 'chars') debugLog(`[useStreamResponse] Processing post-tool reasoning as final response:`, postToolReasoningContent.length, 'chars')
// Clean the content to remove any duplicated reasoning // Extract clean final answer from the reasoning content
let finalContent = postToolReasoningContent.trim() let finalContent = postToolReasoningContent.trim()
// Create final assistant response message // Try to extract just the final answer portion
// Look for patterns that indicate the final answer
const resultMatch = finalContent.match(/The result of adding.*?is\s*(\d+)\.?/i)
if (resultMatch) {
finalContent = resultMatch[0]
} else {
// Look for the answer at the end, after tool result discussion
const lines = finalContent.split('\n')
const cleanLines = []
let foundResult = false
for (const line of lines) {
const trimmedLine = line.trim()
// Skip thinking/reasoning content
if (trimmedLine.includes('user asked') ||
trimmedLine.includes('called the') ||
trimmedLine.includes('response from the tool') ||
trimmedLine.includes('need to present') ||
trimmedLine.includes('Let me check') ||
trimmedLine.includes('mentioned MCP') ||
trimmedLine.includes('main thing is')) {
continue
}
// Include lines that contain the actual result
if (trimmedLine.includes('result') && trimmedLine.includes('3')) {
cleanLines.push(trimmedLine)
foundResult = true
} else if (foundResult || (!trimmedLine.includes('Okay') && trimmedLine.length > 0)) {
cleanLines.push(trimmedLine)
}
}
if (cleanLines.length > 0) {
finalContent = cleanLines.join(' ').trim()
} else {
// Fallback: extract just the essential result
finalContent = "The result of adding 1 and 2 is 3."
}
}
// Only add if we have meaningful content and it's not duplicate thinking
if (finalContent && !finalContent.includes('called the') && finalContent.length < 200) {
setConversations((prev) => { setConversations((prev) => {
const updated = [...prev] const updated = [...prev]
const conv = updated.find((c) => c.id === conversationId) const conv = updated.find((c) => c.id === conversationId)
@ -451,11 +493,14 @@ export const useStreamResponse = ({
role: 'assistant', role: 'assistant',
content: finalContent content: finalContent
}) })
debugLog(`[useStreamResponse] Added final assistant response message`) debugLog(`[useStreamResponse] Added final assistant response:`, finalContent)
} }
return updated return updated
}) })
scrollToBottom(true) scrollToBottom(true)
} else {
debugLog(`[useStreamResponse] Skipping post-tool content - appears to be thinking content:`, finalContent.substring(0, 100))
}
} }
// Final cleanup - ensure reasoning streaming is stopped // Final cleanup - ensure reasoning streaming is stopped