fix(chat): gate stream resume on auth and a real chat id

Merged
SW
3mo ago
+105-3

Implements item F4a of #1949 — the cold-load error toast.

The bug

useVercelChat passed resume: true unconditionally to useChat, so every mount issued GET /api/chat/{chatId}/stream?startIndex=0. On a cold load of / that request fails two different ways:

CauseResponse
401The GET beats Privy's session resolution, so it carries no Authorization headerExactly one of x-api-key or Authorization must be provided
404NewChatBootstrap mounts <Chat> with a client-generated placeholder UUID (NewChatBootstrap.tsx L34) that has no row behind it until provisioning resolvesChat not found

Either one hits onError, which raises toast.error("An error occurred, please try again!"). Reproduced 2/2 in #1949 on an account unrelated to the reporter, so it is global. To a non-technical customer it reads as "the home page is broken and I can't start a chat" — which is exactly how it was reported.

The fix

Gate resume on both preconditions via a new shouldResumeStream helper:

  • Auth resolved (authenticated from Privy), so the header is present.
  • A chat id that actually exists — the route chatId param on /sessions/[sessionId]/chats/[chatId], or workflowChatId once bootstrap resolves. Never the placeholder.

This delays the resume, it does not skip it. useChat re-runs its resume effect when the flag changes (useEffect(() => { if (resume) chatRef.current.resumeStream() }, [resume, chatRef]) in @ai-sdk/react@3.0.243), verified against the installed package rather than assumed. So returning to a chat mid-turn on the canonical route still re-attaches once auth lands.

Why a separate file

One exported function per file (repo principles). It also makes the four-way condition directly testable without mounting the whole chat tree.

Tests

RED before GREEN. lib/chat/__tests__/shouldResumeStream.test.ts covers all five states:

ScenarioResume?
Cold home load, no auth, no chat✗
Auth resolved, still a placeholder chat✗
Real chat route, auth not yet resolved✗
Real chat route, auth resolved✓
Bootstrap minted a real chat id✓
Test Files  93 passed (93)
     Tests  397 passed (397)

tsc --noEmit clean for the changed files. ESLint on hooks/useVercelChat.ts: 4 warnings before, 3 after — this removes one pre-existing warning and adds none. 0 errors.

Not in scope

  • F4b (/api/accounts/undefined/credits → 400) is the other half of the same cold load and ships as its own PR — different file, different race, no dependency between them.
  • The console.error at the onError site violates the repo's no-production-logging rule but predates this change.

Verification status

🔄 Preview verification pending — Done-when for F4a is a cold load of / produces zero 4xx responses and no error toast, which needs the deployed preview and a DevTools network trace. Results to follow as a comment on this PR.


Summary by cubic

Fixes the cold-load error toast by gating stream resume on a resolved session and an existing route chat id. Implements F4a of chat#1949 and stops 401/404s and post-bootstrap resume loops, while keeping mid-turn re-attach working.

  • Bug Fixes
    • Added shouldResumeStream and wired it into useVercelChat to resume only when authenticated and a real routeChatId exists (never on a bootstrapped chat).
    • Prevents early GET /api/chat/{id}/stream calls without Authorization, against placeholder ids, and the 204 resume loop after bootstrap.
    • Added unit tests covering five key states.