From 16f40ceb54595c4e74bd5678cc40f58b2f014739 Mon Sep 17 00:00:00 2001 From: Manmohan Sharma Date: Thu, 16 Apr 2026 15:15:53 -0700 Subject: [PATCH] fix(frontend): pass assistantMsgId directly to fix stale closure bug --- services/frontend/components/chat/ChatWindow.tsx | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/services/frontend/components/chat/ChatWindow.tsx b/services/frontend/components/chat/ChatWindow.tsx index f9bc48bc..cc87a530 100644 --- a/services/frontend/components/chat/ChatWindow.tsx +++ b/services/frontend/components/chat/ChatWindow.tsx @@ -60,7 +60,7 @@ export default function ChatWindow() { setIsStreaming(false); }, []); - const streamFromApi = useCallback(async (convId: string, content: string, temp?: number, topk?: number) => { + const streamFromApi = useCallback(async (convId: string, assistantMsgId: string, content: string, temp?: number, topk?: number) => { stop(); const ac = new AbortController(); abortRef.current = ac; @@ -110,9 +110,7 @@ export default function ChatWindow() { } if (typeof data.token === 'string') { streamingBufferRef.current += data.token; - if (streamingMsgId && convId) { - updateMessage(convId, streamingMsgId!, streamingBufferRef.current); - } + updateMessage(convId, assistantMsgId, streamingBufferRef.current); } } catch { /* skip malformed */ } } @@ -123,8 +121,8 @@ export default function ChatWindow() { } catch (err) { if ((err as Error).name !== 'AbortError') { console.error('[chat] stream error:', err); - if (streamingMsgId && currentConversationId) { - updateMessage(currentConversationId, streamingMsgId!, `Error: ${(err as Error).message}`); + if (assistantMsgId && convId) { + updateMessage(convId, assistantMsgId, `Error: ${(err as Error).message}`); } } setStreamingMsgId(null); @@ -133,7 +131,7 @@ export default function ChatWindow() { setIsStreaming(false); abortRef.current = null; } - }, [stop, streamingMsgId, currentConversationId, updateMessage]); + }, [stop, updateMessage]); const ensureConversation = useCallback(async () => { if (currentConversationId) return currentConversationId; @@ -174,7 +172,7 @@ export default function ChatWindow() { setStreamingMsgId(assistantId); streamingBufferRef.current = ''; - await streamFromApi(convId, text, temperature, topK); + await streamFromApi(convId, assistantId, text, temperature, topK); }, [ draft,