Skip to main content

handleWorkflowStream()

用于 AI SDK 兼容格式中流式工作流执行的框架无关处理器。当你需要在 Hono 或 Mastra 自身的 apiRoutes 功能之外处理工作流流式传输时,可以直接使用此函数。

🌐 Framework-agnostic handler for streaming workflow execution in AI SDK-compatible format. Use this function directly when you need to handle workflow streaming outside Hono or Mastra's own apiRoutes feature.

handleWorkflowStream() 返回一个 ReadableStream,你可以用 createUIMessageStreamResponse() 封装它。

如果你想在 Mastra 服务器中创建工作流程路线,请使用 workflowRoute()

🌐 Use workflowRoute() if you want to create a workflow route inside a Mastra server.

工作流中的代理流

当工作流步骤将代理的流传输给工作流写入器(例如 await response.fullStream.pipeTo(writer))时,代理的文本块和工具调用会实时转发到 UI 流,即使代理在工作流步骤内运行也是如此。

🌐 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.

使用示例
Direct link to 使用示例

🌐 Usage example

Next.js 应用路由示例:

🌐 Next.js App Router example:

app/api/workflow/route.ts
import { handleWorkflowStream } from '@mastra/ai-sdk';
import { createUIMessageStreamResponse } from 'ai';
import { mastra } from '@/src/mastra';

export async function POST(req: Request) {
const params = await req.json();
const stream = await handleWorkflowStream({
mastra,
workflowId: 'weatherWorkflow',
params,
});
return createUIMessageStreamResponse({ stream });
}

参数
Direct link to 参数

🌐 Parameters

mastra:

Mastra
The Mastra instance containing registered workflows.

workflowId:

string
The ID of the workflow to execute.

params:

WorkflowStreamHandlerParams
Parameters for the workflow stream.

params.runId?:

string
Optional run ID for the workflow execution.

params.resourceId?:

string
Optional resource ID for the workflow run.

params.inputData?:

Record<string, any>
Input data for starting a new workflow execution.

params.resumeData?:

Record<string, any>
Data for resuming a suspended workflow execution.

params.requestContext?:

RequestContext
Request context to pass to the workflow execution.

params.tracingOptions?:

TracingOptions
Options for tracing and observability.

params.step?:

string
Specific step to target in the workflow.

includeTextStreamParts?:

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