Fix message ordering - create final response at end instead of updating existing

- Remove logic that updates existing assistant message
- Always create new final response message at end of conversation
- This ensures proper order: thinking → tool call → tool result → final response
- Still prevents duplicates by checking existing content
This commit is contained in:
Glen Maddern 2025-06-25 08:25:03 +10:00
commit 8159950ffd

View file

@ -489,16 +489,7 @@ 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) {
// Check if there's already an assistant message with only reasoning content // Always create a NEW message at the end (after tool calls) for final response
const existingAssistantMsg = assistantMessageIndex >= 0 ? conv.messages[assistantMessageIndex] : null
if (existingAssistantMsg &&
existingAssistantMsg.role === 'assistant' &&
(!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 // Check if we already have a message with this content to avoid duplicates
const isDuplicate = conv.messages.some(msg => const isDuplicate = conv.messages.some(msg =>
msg.role === 'assistant' && msg.content === finalContent msg.role === 'assistant' && msg.content === finalContent
@ -508,12 +499,11 @@ export const useStreamResponse = ({
role: 'assistant', role: 'assistant',
content: finalContent content: finalContent
}) })
debugLog(`[useStreamResponse] Added new final assistant response:`, finalContent) debugLog(`[useStreamResponse] Added final assistant response at end:`, finalContent)
} else { } else {
debugLog(`[useStreamResponse] Skipping duplicate assistant response:`, finalContent) debugLog(`[useStreamResponse] Skipping duplicate assistant response:`, finalContent)
} }
} }
}
return updated return updated
}) })
scrollToBottom(true) scrollToBottom(true)