From 8159950ffdab86aa260e5b8eca92cfedfc2d183e Mon Sep 17 00:00:00 2001 From: Glen Maddern Date: Wed, 25 Jun 2025 08:25:03 +1000 Subject: [PATCH] Fix message ordering - create final response at end instead of updating existing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- .../chat-ui/src/hooks/useStreamResponse.ts | 34 +++++++------------ 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/examples/chat-ui/src/hooks/useStreamResponse.ts b/examples/chat-ui/src/hooks/useStreamResponse.ts index 93a98f1..080aba3 100644 --- a/examples/chat-ui/src/hooks/useStreamResponse.ts +++ b/examples/chat-ui/src/hooks/useStreamResponse.ts @@ -489,29 +489,19 @@ export const useStreamResponse = ({ const updated = [...prev] const conv = updated.find((c) => c.id === conversationId) if (conv) { - // Check if there's already an assistant message with only reasoning content - 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) + // Always create a NEW message at the end (after tool calls) for final response + // 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 final assistant response at end:`, 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) - } + debugLog(`[useStreamResponse] Skipping duplicate assistant response:`, finalContent) } } return updated