diff --git a/frontend/src/lib/components/assistant/AssistantChatPanel.svelte b/frontend/src/lib/components/assistant/AssistantChatPanel.svelte index 7cca1e2..2c6c380 100644 --- a/frontend/src/lib/components/assistant/AssistantChatPanel.svelte +++ b/frontend/src/lib/components/assistant/AssistantChatPanel.svelte @@ -56,6 +56,7 @@ let archivedConversationsTotal = 0; let historyPage = 1; let historyHasNext = false; + let historyLoadVersion = 0; let conversationsPage = 1; let conversationsHasNext = false; let historyViewport = null; @@ -71,18 +72,22 @@ * @POST: messages are populated from persisted history and conversation id is synchronized. * @SIDE_EFFECT: Performs API call to assistant history endpoint. */ - async function loadHistory() { + async function loadHistory(targetConversationId = conversationId) { if (loadingHistory || !isOpen) return; + const requestVersion = ++historyLoadVersion; loadingHistory = true; try { - const history = await getAssistantHistory(1, HISTORY_PAGE_SIZE, conversationId, true); + const history = await getAssistantHistory(1, HISTORY_PAGE_SIZE, targetConversationId, true); + if (requestVersion !== historyLoadVersion) { + return; + } messages = (history.items || []).map((msg) => ({ ...msg, actions: msg.actions || msg.metadata?.actions || [], })); historyPage = 1; historyHasNext = Boolean(history.has_next); - if (history.conversation_id && history.conversation_id !== conversationId) { + if (!targetConversationId && history.conversation_id && history.conversation_id !== conversationId) { setAssistantConversationId(history.conversation_id); } initialized = true; @@ -286,9 +291,13 @@ async function selectConversation(conversation) { if (!conversation?.conversation_id) return; if (conversation.conversation_id === conversationId) return; + // Invalidate any in-flight history request to avoid stale conversation overwrite. + historyLoadVersion += 1; setAssistantConversationId(conversation.conversation_id); - initialized = false; - await loadHistory(); + messages = []; + historyPage = 1; + historyHasNext = false; + await loadHistory(conversation.conversation_id); } // [/DEF:selectConversation:Function]