Commit 93255ad
feat(workflow): export WorkflowChatTransport with stream reconnection support (#14340)
## Background
The `WorkflowChatTransport` class was implemented in `@ai-sdk/workflow`
but not exported, making it unavailable to consumers. This transport is
needed for `useChat` to enable automatic stream reconnection in
workflow-based chat apps — handling network failures, page refreshes,
and function timeouts by reconnecting to the workflow's stream endpoint.
Reference:
[DurableChatTransport](https://useworkflow.dev/docs/api-reference/workflow-ai/workflow-chat-transport)
## Summary
- Export `WorkflowChatTransport` class and related types
(`WorkflowChatTransportOptions`, `SendMessagesOptions`,
`ReconnectToStreamOptions`) from `@ai-sdk/workflow`
- Add `initialStartIndex` option for resuming streams from the tail
(negative values like `-50` fetch only the last 50 chunks, useful for
page refresh recovery without replaying the full conversation)
- Implement `x-workflow-stream-tail-index` header resolution to compute
absolute chunk positions from negative start indices, with graceful
fallback to replay-from-start when the header is missing
- Fix positive `startIndex` reconnection: set `chunkIndex` to match the
explicit start position so retries after disconnection resume correctly
- Add `startIndex` per-call override on `ReconnectToStreamOptions`
- Extract `getErrorMessage` utility for proper error formatting in
reconnection failures (avoids `[object Object]` in error messages)
- Update `examples/next-workflow` main page to use
`WorkflowChatTransport` with `useChat`
- Add `examples/next-workflow/test` page with mock API routes that
simulate stream interruption and verify reconnection recovery end-to-end
## Documentation
- **API reference**: New `WorkflowChatTransport` reference page at
`docs/reference/ai-sdk-workflow/workflow-chat-transport` with
constructor parameters, methods, reconnection flow, server requirements,
and examples
- **Workflow agent guide**: New "Resumable Streaming" section with
client and server endpoint examples
- **Transport guide**: New "Workflow Transport" section linking to the
reference
- **Workflow reference index**: Added `WorkflowChatTransport` card
## Manual Verification
1. Started `examples/next-workflow` dev server (`pnpm next dev`)
2. **Happy path** (`/`): Sent "What is the weather in San Francisco?" —
WorkflowAgent called `getWeather` tool, responded with "86°F and windy".
Sent "What is 42 * 17?" — called `calculate` tool, responded "714". Both
messages used `WorkflowChatTransport`.
3. **Stream interruption + reconnection** (`/test`): The test page uses
mock API routes where the POST endpoint sends only 2 of 6 SSE chunks (no
`finish` event), simulating a function timeout. The transport detected
the missing `finish` chunk, automatically reconnected via GET to
`/api/test-chat/{runId}/stream?startIndex=2`, received the remaining
chunks, and displayed the complete message. The transport log panel
confirmed the full lifecycle:
- `POST response received` (`onChatSendMessage` callback fired)
- `Status: streaming` (partial stream consumed)
- Auto-reconnect via GET (transparent to the user)
- `Chat ended: total chunks=6` (`onChatEnd` callback fired)
- `Status: ready`
## Checklist
- [x] Tests have been added / updated (for bug fixes / features)
- [x] Documentation has been added / updated (for bug fixes / features)
- [x] A _patch_ changeset for relevant packages has been added (for bug
fixes / features - run `pnpm changeset` in the project root)
- [x] I have reviewed this pull request (self-review)
## Related Issues
Follow-up to #12165
---------
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>1 parent cb1bdd2 commit 93255ad
File tree
13 files changed
+1548
-3
lines changed- .changeset
- content/docs
- 03-agents
- 04-ai-sdk-ui
- 07-reference/04-ai-sdk-workflow
- examples/next-workflow/app
- api/test-chat
- [runId]/stream
- test
- packages/workflow/src
13 files changed
+1548
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
194 | 194 | | |
195 | 195 | | |
196 | 196 | | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
197 | 278 | | |
198 | 279 | | |
199 | 280 | | |
| |||
385 | 466 | | |
386 | 467 | | |
387 | 468 | | |
| 469 | + | |
388 | 470 | | |
389 | 471 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
156 | 156 | | |
157 | 157 | | |
158 | 158 | | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
159 | 197 | | |
160 | 198 | | |
161 | 199 | | |
| |||
0 commit comments