Fix duplicate assistant messages by updating existing message
- Check if there's already an assistant message with only reasoning content - Update existing assistant message with final content instead of creating new one - Add duplicate detection to prevent multiple messages with same content - Ensures only one assistant response appears after tool calls
This commit is contained in:
parent
0ed5dbaa67
commit
55d54bce60
1 changed files with 24 additions and 5 deletions
|
|
@ -489,11 +489,30 @@ export const useStreamResponse = ({
|
||||||
const updated = [...prev]
|
const updated = [...prev]
|
||||||
const conv = updated.find((c) => c.id === conversationId)
|
const conv = updated.find((c) => c.id === conversationId)
|
||||||
if (conv) {
|
if (conv) {
|
||||||
conv.messages.push({
|
// Check if there's already an assistant message with only reasoning content
|
||||||
role: 'assistant',
|
const existingAssistantMsg = assistantMessageIndex >= 0 ? conv.messages[assistantMessageIndex] : null
|
||||||
content: finalContent
|
if (existingAssistantMsg &&
|
||||||
})
|
existingAssistantMsg.role === 'assistant' &&
|
||||||
debugLog(`[useStreamResponse] Added final assistant response:`, finalContent)
|
(!existingAssistantMsg.content || existingAssistantMsg.content.trim() === '') &&
|
||||||
|
existingAssistantMsg.reasoning) {
|
||||||
|
// Update the existing assistant message with final content
|
||||||
|
existingAssistantMsg.content = finalContent
|
||||||
|
debugLog(`[useStreamResponse] Updated existing assistant message with final content:`, finalContent)
|
||||||
|
} else {
|
||||||
|
// Check if we already have a message with this content to avoid duplicates
|
||||||
|
const isDuplicate = conv.messages.some(msg =>
|
||||||
|
msg.role === 'assistant' && msg.content === finalContent
|
||||||
|
)
|
||||||
|
if (!isDuplicate) {
|
||||||
|
conv.messages.push({
|
||||||
|
role: 'assistant',
|
||||||
|
content: finalContent
|
||||||
|
})
|
||||||
|
debugLog(`[useStreamResponse] Added new final assistant response:`, finalContent)
|
||||||
|
} else {
|
||||||
|
debugLog(`[useStreamResponse] Skipping duplicate assistant response:`, finalContent)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return updated
|
return updated
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue