diff --git a/frontend/src/features/chat/composables/useChat.ts b/frontend/src/features/chat/composables/useChat.ts index 2c38d5bd..5a4415a0 100644 --- a/frontend/src/features/chat/composables/useChat.ts +++ b/frontend/src/features/chat/composables/useChat.ts @@ -210,20 +210,19 @@ export function useChat() { activeRunId.value = run.runId; runMessages.push({ role: 'user', content: query }); - let botMsgRef: ChatMessage | null = null; + let botMsgIndex: number | null = null; let hasAssistantContent = false; let hasTerminalEvent = false; const ensureBotMessage = () => { - if (!botMsgRef) { - botMsgRef = { + if (botMsgIndex === null) { + botMsgIndex = runMessages.push({ role: 'assistant', content: '', retriever_resources: [], attribution_parts: [], - }; - runMessages.push(botMsgRef); + }) - 1; } - return botMsgRef; + return runMessages[botMsgIndex] || null; }; isThinking.value = true; @@ -299,8 +298,9 @@ export function useChat() { botMsg.content += normalizeStreamText(data.answer); hasAssistantContent = true; } - if (botMsgRef) { - applyEventMetadata(botMsgRef, data); + if (botMsgIndex !== null) { + const currentBotMsg = runMessages[botMsgIndex]; + if (currentBotMsg) applyEventMetadata(currentBotMsg, data); } } else if (eventName === 'message_end' || eventName === 'workflow_finished') { hasTerminalEvent = true; @@ -324,8 +324,9 @@ export function useChat() { botMsg.content += normalizedAnswer; } } - if (botMsgRef) { - applyEventMetadata(botMsgRef, data); + if (botMsgIndex !== null) { + const currentBotMsg = runMessages[botMsgIndex]; + if (currentBotMsg) applyEventMetadata(currentBotMsg, data); } } else if (eventName === 'error') { const botMsg = ensureBotMessage();