Skip to content

feat(workflow): export WorkflowChatTransport with stream reconnection support#14340

Merged
gr2m merged 4 commits intogr2m/durable-agentfrom
gr2m/workflow-chat-transport
Apr 11, 2026
Merged

feat(workflow): export WorkflowChatTransport with stream reconnection support#14340
gr2m merged 4 commits intogr2m/durable-agentfrom
gr2m/workflow-chat-transport

Conversation

@gr2m
Copy link
Copy Markdown
Collaborator

@gr2m gr2m commented Apr 10, 2026

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

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

  • Tests have been added / updated (for bug fixes / features)
  • Documentation has been added / updated (for bug fixes / features)
  • A patch changeset for relevant packages has been added (for bug fixes / features - run pnpm changeset in the project root)
  • I have reviewed this pull request (self-review)

Related Issues

Follow-up to #12165

…upport

Export WorkflowChatTransport as a public API from @ai-sdk/workflow. Add
initialStartIndex option for resuming streams from the tail (negative
values), with x-workflow-stream-tail-index header resolution and graceful
fallback. Extract getErrorMessage utility for proper error formatting in
reconnection failures. Update next-workflow example to use the transport.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@tigent tigent bot added ai/workflow related to WorkflowAgent or Vercel Workflow DevKit in general (useworkflow.dev) feature New feature or request labels Apr 10, 2026
When a positive startIndex is used, set chunkIndex to match so that
retries after disconnection resume from the correct position instead
of restarting from 0.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Add /test page and mock /api/test-chat endpoints that simulate stream
interruption. The POST route sends a partial stream (no finish event),
forcing the transport to reconnect via the GET stream endpoint. The
transport log panel shows the full lifecycle: POST -> streaming ->
reconnect -> Chat ended.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Add API reference page for WorkflowChatTransport with constructor
  parameters, methods, reconnection flow, server requirements, and
  usage examples
- Add WorkflowChatTransport card to workflow reference index
- Add "Resumable Streaming" section to WorkflowAgent guide with
  client and server endpoint examples
- Add "Workflow Transport" section to the transport guide page
- Add link to WorkflowChatTransport reference in Next Steps

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@gr2m gr2m merged commit 93255ad into gr2m/durable-agent Apr 11, 2026
3 checks passed
@gr2m gr2m deleted the gr2m/workflow-chat-transport branch April 11, 2026 00:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai/workflow related to WorkflowAgent or Vercel Workflow DevKit in general (useworkflow.dev) feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant