feat(chat): reconnect via WorkflowChatTransport instead of hand-rolled recovery
Row 1 of the chat#1923 plan — the fix for the UI stall.
Replaces our client-side stream-recovery machinery with @workflow/ai's WorkflowChatTransport, which owns reconnection natively.
Why the transport, and not one more fix
The transport is a single generator:
for await (const chunk of postStream) { chunkIndex++; yield chunk; if (chunk.type === 'finish') gotFinish = true; }
if (!gotFinish) yield* this.reconnectToStreamIterator(options, runId, chunkIndex);
// …which loops: while (!gotFinish) { GET …/stream?startIndex=chunkIndex; … }
One continuous stream reaches useChat, so the turn settles exactly when finish arrives.
Our version tracked that same state across React refs — an in-flight flag, an edge-detected trigger, a probe schedule, a terminal-state branch — and every fix exposed another unverified state:
| Attempt | Failure |
|---|---|
trigger on useChat status | fired once, never again |
flag held across await resumeStream() | blocked recovery for the whole ~120 s connection |
| final stream end | fired nothing at all — 2 of 3 preview runs passed, the third hung after 9 recoveries |
There is no trigger to miss here, and no ref to hold.
Deleted — 7 files
createChunkCountingFetch, buildStreamReconnectUrl, shouldRecoverStalledStream, useStreamRecovery, their tests, and the hook's wiring in useVercelChat. startIndex is no longer ours to compute.
Kept, because the SDK cannot know it
- auth +
sessionId/chatIdviaprepareSendMessagesRequest - the reconnect URL repointed at
/api/chat/{chatId}/stream— the SDK defaults to keying by run id - a 204 override: our api answers "nothing to resume" with a bodyless 204, which the SDK surfaces as
Failed to fetch chat: 204and counts towardmaxConsecutiveErrors. Mirrors upstream open-agents' wrapper.
Version choice
@workflow/ai@4.2.0 is stable and carries newer transport code than the 5.x beta upstream pins — I diffed them, and 4.2.0 has the mid-part-resume orphan filter (createOrphanFilter) that 5.0.0-beta.4 lacks. That matters here because we resume at arbitrary chunk indices.
Lockfile resolved with default resolution: 9 version changes, viem and everything else unmoved. --config.resolution-mode=lowest-direct was tried and rejected — it downgraded viem 2.52.0 → 2.23.2 and broke the coinbase types. "Lowest" is not "least change".
Checks
356 tests pass, tsc --noEmit clean, next build compiles.
Preview verification to follow in a comment. The gate is the one that has never held: reconnects chain through every stream end, and the turn ends coherently — across 3 runs.
Summary by cubic
Switches chat reconnection to @workflow/ai’s WorkflowChatTransport to stop UI stalls on long runs by resuming streams until the finish chunk. Removes custom recovery code and aligns reconnects with our API.
- Refactors
- Added
createWorkflowChatTransportand switcheduseChatTransport; removeduseStreamRecoveryand related helpers/tests. - Reconnects call
/api/chat/{chatId}/streamwith auth;startIndexis computed by the transport and it streams untilfinish; treat204as “nothing to resume”. - Injected
sessionId,chatId, andrecoupAccessTokenviaprepareSendMessagesRequest. - Added
@workflow/ai@4.2.0; lockfile updated.
- Added
Summary by CodeRabbit
-
New Features
- Improved chat connection handling with automatic stream resumption.
- Added secure session and access-token handling for chat requests and reconnects.
- Added a single retry for reconnects caused by expired authentication.
- Gracefully handles completed streams during reconnection.
-
Bug Fixes
- Improved recovery from interrupted or incomplete chat responses.
- Prevents unnecessary reconnect attempts when a stream has ended normally.