跳到主要内容
非官方简体中文翻译,内容版权归 Mastra 所有,遵循 Apache-2.0 许可。

workflowRoute()

Creates a workflow route handler for streaming workflow execution in AI SDK format. The function registers an HTTP POST endpoint that accepts input data and executes a workflow before streaming the response to the client in AI SDK-compatible format. Use it inside a custom API route.

Use handleWorkflowStream() if you need a framework-agnostic handler.

workflowRoute() keeps the existing AI SDK v5/default behavior. If your app is typed against AI SDK v6, pass version: 'v6'.

Agent streaming in workflows

When a workflow step pipes an agent's stream to the workflow writer (e.g., await response.fullStream.pipeTo(writer)), the agent's text chunks and tool calls are forwarded to the UI stream in real time, even when the agent runs inside workflow steps.

See Workflow Streaming for more details.

Usage example
Usage example的直接链接

This example shows how to set up a workflow route at the /workflow endpoint that uses a workflow with the ID weatherWorkflow.

src/mastra/index.ts
import { Mastra } from '@mastra/core'
import { workflowRoute } from '@mastra/ai-sdk'

export const mastra = new Mastra({
server: {
apiRoutes: [
workflowRoute({
path: '/workflow',
workflow: 'weatherWorkflow',
}),
],
},
})

You can also use runtime-defined workflow routing based on a workflowId. The URL /workflow/weatherWorkflow will resolve to the workflow with the ID weatherWorkflow.

src/mastra/index.ts
import { Mastra } from '@mastra/core'
import { workflowRoute } from '@mastra/ai-sdk'

export const mastra = new Mastra({
server: {
apiRoutes: [
workflowRoute({
path: '/workflow/:workflowId',
}),
],
},
})

Parameters
Parameters的直接链接

version?:

'v5' | 'v6' | 'v7'
= 'v5'
Selects the AI SDK stream contract to emit. Omit it or pass 'v5' for the existing default behavior. Pass 'v6' when your app is typed against AI SDK v6 response helpers. Pass 'v7' when your app is typed against AI SDK v7.

path?:

string
= '/api/workflows/:workflowId/stream'
The route path (e.g., /workflow or /workflow/:workflowId). Include :workflowId for dynamic workflow routing.

workflow?:

string
= undefined
Fixed workflow ID when not using dynamic routing.

includeTextStreamParts?:

boolean
= true
Whether to include text stream parts in the output.

Additional configuration
Additional configuration的直接链接

You can use prepareSendMessagesRequest to customize the request sent to the workflow route, for example to pass additional configuration to the workflow:

const { error, status, sendMessage, messages, regenerate, stop } = useChat({
transport: new DefaultChatTransport({
api: 'http://localhost:4111/workflow',
prepareSendMessagesRequest({ messages }) {
return {
body: {
inputData: {
city: messages[messages.length - 1].parts[0].text,
},
// Or resumeData for resuming a suspended workflow
resumeData: {
confirmation: messages[messages.length - 1].parts[0].text,
},
},
}
},
}),
})
On this page