From a7800604b6a526d0c06defeb07c34b019259d79d Mon Sep 17 00:00:00 2001 From: viettranx Date: Sat, 28 Mar 2026 18:14:08 +0700 Subject: [PATCH] fix(web): streaming broken on first chat send due to expectingRunRef race MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When sending the first message (no session key), handleSend calls navigate() then send(). The navigation triggers a session-change effect that resets expectingRunRef=false before run.started arrives, causing all streaming events to be silently dropped. Only reset expectingRunRef when switching between existing sessions, not on the "" → new-key transition that is part of the send flow. --- ui/web/src/pages/chat/hooks/use-chat-messages.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ui/web/src/pages/chat/hooks/use-chat-messages.ts b/ui/web/src/pages/chat/hooks/use-chat-messages.ts index cc671e0c..fdf21bd9 100644 --- a/ui/web/src/pages/chat/hooks/use-chat-messages.ts +++ b/ui/web/src/pages/chat/hooks/use-chat-messages.ts @@ -47,6 +47,7 @@ export function useChatMessages(sessionKey: string, agentId: string) { const prevKeyRef = useRef(sessionKey); useEffect(() => { if (sessionKey === prevKeyRef.current) return; + const wasEmpty = !prevKeyRef.current; prevKeyRef.current = sessionKey; setStreamText(null); setThinkingText(null); @@ -56,7 +57,11 @@ export function useChatMessages(sessionKey: string, agentId: string) { setBlockReplies([]); setTeamTasks([]); runIdRef.current = null; - expectingRunRef.current = false; + // Only reset when switching between existing sessions, not on "" → new key + // (the "" → key transition is part of the send flow for new sessions). + if (!wasEmpty) { + expectingRunRef.current = false; + } streamRef.current = ""; thinkingRef.current = ""; toolStreamRef.current = [];