@mastra/core
Version:
53 lines (35 loc) • 2.38 kB
Markdown
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
Converts a `WorkflowState` object (as returned by `workflow.getWorkflowRunById()`) into a `ReadableStream` of AI SDK UIMessage data parts. The stream contains the same `WorkflowDataPart` and `WorkflowStepDataPart` chunks that the live [`workflowRoute()`](https://mastra.ai/reference/ai-sdk/workflow-route) produces.
Use this when you want to display a completed or suspended workflow run using the same UI components that render live workflow streams.
Next.js App Router endpoint that replays a past workflow run as a stream:
```typescript
import { workflowSnapshotToStream } from '@mastra/ai-sdk'
import { createUIMessageStreamResponse } from 'ai'
import { mastra } from '@/src/mastra'
export async function GET(req: Request) {
const { searchParams } = new URL(req.url)
const runId = searchParams.get('runId')!
const workflowRun = await mastra.getWorkflow('myWorkflow').getWorkflowRunById(runId)
if (!workflowRun) {
return new Response('Not found', { status: 404 })
}
const stream = workflowSnapshotToStream(workflowRun)
return createUIMessageStreamResponse({ stream })
}
```
**workflowRun** (`WorkflowState`): The workflow run state object, as returned by getWorkflowRunById() or the workflow runs API. Contains runId, workflowName, status, and steps.
## Returns
`ReadableStream` — A stream of AI SDK UIMessage data parts containing:
- A `start` marker
- A `WorkflowDataPart` with the overall workflow status and all step summaries
- A `WorkflowStepDataPart` for each step with its full output
- A `finish` marker
Pass this stream to [`createUIMessageStreamResponse()`](https://ai-sdk.dev/docs/reference/ai-sdk-ui/create-ui-message-stream-response) or `createUIMessageStream()` from the AI SDK.
## Related
- [`workflowRoute()`](https://mastra.ai/reference/ai-sdk/workflow-route): Stream live workflow runs
- [`handleWorkflowStream()`](https://mastra.ai/reference/ai-sdk/handle-workflow-stream): Framework-agnostic handler for live workflow streaming
- [`toAISdkStream()`](https://mastra.ai/reference/ai-sdk/to-ai-sdk-stream): Convert live Mastra streams to AI SDK format
- [`toAISdkMessages()`](https://mastra.ai/reference/ai-sdk/to-ai-sdk-messages): Convert stored agent messages to AI SDK format