diff --git a/langgraph/src/main.tsx b/langgraph/src/main.tsx index 7a0d5d7..ff05bb9 100644 --- a/langgraph/src/main.tsx +++ b/langgraph/src/main.tsx @@ -153,26 +153,12 @@ function App() { useEffect(() => { autoResizeTextarea(textareaRef); }, [input]); // ── Active messages ────────────────────────────────────────────────────── - const lastMessagesRef = useRef([]); - const lastThreadIdRef = useRef(null); - // Detect thread switch — when threadId changes, thread.messages still holds stale data - // for the current render cycle (useStream is async). Track this to suppress stale data. - const threadJustSwitched = currentThreadId !== lastThreadIdRef.current; - if (threadJustSwitched) { - lastMessagesRef.current = []; - lastThreadIdRef.current = currentThreadId; - } - // Only trust thread.messages when useStream has caught up to the current threadId - const threadMessagesValid = !threadJustSwitched && thread.messages.length > 0; - if (threadMessagesValid) { - lastMessagesRef.current = thread.messages; - } + // thread.switchThread() is called in useEffect whenever currentThreadId changes, + // which clears thread.messages. So thread.messages is always safe to trust: + // - empty = thread just switched or genuinely empty → fall back to historicalMessages + // - non-empty = useStream has loaded messages for the current thread const activeMessages: Message[] = deduplicateMessages( - threadMessagesValid - ? thread.messages - : lastMessagesRef.current.length > 0 - ? lastMessagesRef.current - : historicalMessages + thread.messages.length > 0 ? thread.messages : historicalMessages ); // Completed / failed tool IDs