WorkflowChatTransport helper provides a drop-in transport for the AI SDK that handles client-side resumption logic automatically.
Implementing Stream Resumption
Let’s add stream resumption to a basic durable agent:Return the Run ID from Your API
Modify your chat endpoint to include the workflow run ID in a response header:app/api/chat/route.ts
Add a Stream Reconnection Endpoint
Create a new API route that returns the stream for an existing run:app/api/chat/[id]/stream/route.ts
startIndex parameter ensures the client can resume from exactly where it left off.Use WorkflowChatTransport in the Client
Replace the default transport in useChat with WorkflowChatTransport:app/page.tsx
How It Works
- Initial request: When the user sends a message,
WorkflowChatTransportmakes a POST to/api/chat - Run ID storage: The API starts a workflow and returns the run ID in the
x-workflow-run-idheader - Callback execution:
onChatSendMessagestores this run ID in localStorage - Automatic reconnection: If the stream is interrupted before receiving a “finish” chunk, the transport automatically reconnects
- URL construction:
prepareReconnectToStreamRequestbuilds the reconnection URL using the stored run ID - Resume from index: The reconnection endpoint returns the stream from the last known position
- Cleanup: When the stream completes,
onChatEndclears the stored run ID
Advanced Configuration
Custom Run ID Storage
Instead of localStorage, store the run ID in a database:lineNumbers
Error Handling
Handle reconnection errors gracefully:lineNumbers
Authentication
Add authentication headers to reconnection requests:lineNumbers
Manual Reconnection
Trigger reconnection manually:lineNumbers
Testing Resumability
Simulate Network Interruption
Test stream resumption by artificially interrupting the connection:lineNumbers
Simulate Function Timeout
Test resumption after serverless function timeout:app/api/chat/route.ts
Related Documentation
WorkflowChatTransportAPI Reference - Full configuration options- Streaming - Understanding workflow streams
getRun()API Reference - Retrieving existing runs- Chat Session Modeling - Managing conversation state