From 24805200f0c8ba8db4a3d3317b2835f7739cf9a7 Mon Sep 17 00:00:00 2001 From: siwuai <458372151@qq.com> Date: Wed, 8 Apr 2026 10:03:07 +0800 Subject: [PATCH] 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. --- .../src/components/workspace/chats/use-thread-chat.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/frontend/src/components/workspace/chats/use-thread-chat.ts b/frontend/src/components/workspace/chats/use-thread-chat.ts index 85e7db1a8..6913e3b76 100644 --- a/frontend/src/components/workspace/chats/use-thread-chat.ts +++ b/frontend/src/components/workspace/chats/use-thread-chat.ts @@ -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]);