fix(frontend): prevent stale 'new' thread ID from triggering 422 history requests (#1960)

After history.replaceState updates the URL from /chats/new to
/chats/{UUID}, Next.js useParams does not update because replaceState
bypasses the router. The useEffect in useThreadChat would then set
threadIdFromPath ('new') as the threadId, causing the LangGraph SDK
to call POST /threads/new/history which returns HTTP 422 (Invalid
thread ID: must be a UUID).

This fix adds a guard to skip the threadId update when
threadIdFromPath is the literal string 'new', preserving the
already-correct UUID that was set when the thread was created.
This commit is contained in:
siwuai 2026-04-08 10:03:07 +08:00 committed by GitHub
parent 722a9c4753
commit 24805200f0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -24,6 +24,14 @@ export function useThreadChat() {
setThreadId(uuid());
return;
}
// Guard: after history.replaceState updates the URL from /chats/new to
// /chats/{UUID}, Next.js useParams may still return the stale "new" value
// because replaceState does not trigger router updates. Avoid propagating
// this invalid thread ID to downstream hooks (e.g. useStream), which would
// cause a 422 from LangGraph Server.
if (threadIdFromPath === "new") {
return;
}
setIsNewThread(false);
setThreadId(threadIdFromPath);
}, [pathname, threadIdFromPath]);