feat(chat): resume in-flight workflow stream on chat mount

AR
6mo ago
+160-32

Reconnects to a still-running workflow stream when a chat is reopened mid-generation, instead of showing a frozen partial turn.

The GET chat snapshot already reports isStreaming. We now surface it and, when true, reconnect through the AI SDK resume path to GET /api/chat/{chatId}/stream (recoupable/api#596). The transport's headers carry the Privy bearer on the reconnect GET automatically.

  • getChatSnapshot replaces getChatMessages, returning { messages, isStreaming }
  • useMessageLoader fires onActiveStream once when the loaded chat is streaming
  • workflow transport gains prepareReconnectToStreamRequest → GET stream route
  • useVercelChat resumes via useChat's resumeStream() on that signal

Test plan

  • Start a long generation, reload the page mid-stream → chat reattaches and continues streaming to completion.
  • Reopen a chat whose run already finished → snapshot isStreaming: false, no reconnect; persisted messages render.
  • New/empty chat → no resume attempt.
  • Unit: lib/messages/__tests__/getChatSnapshot.test.ts.

Depends on recoupable/api#596 being live.


Summary by cubic

Reattaches to an in-flight workflow stream when a chat mounts, so reopening mid-generation resumes live instead of showing a frozen partial turn. Also updates getChatSnapshot to throw on failed fetches, so errors surface instead of rendering a blank chat.

  • New Features

    • Replaced getChatMessages with getChatSnapshot, returning { messages, isStreaming }.
    • useMessageLoader signals onActiveStream when isStreaming is true; useVercelChat resumes via resumeStream().
    • Transport adds prepareReconnectToStreamRequest to GET /api/chat/{chatId}/stream (bearer forwarded via headers).
  • Dependencies

    • Requires recoupable/api#596 (GET stream route) to be live.