fix(web): streaming broken on first chat send due to expectingRunRef race

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.
This commit is contained in:
viettranx committed 2026-03-28 18:14:08 +07:00
1 parent 2d344e4f35
commit a7800604b6
1 file changed
+6 -1
@@ -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 = [];