工作流类
🌐 Workflow Class
Workflow 类使你能够为复杂操作序列创建状态机,并支持条件分支和数据验证。
🌐 The Workflow class enables you to create state machines for complex sequences of operations with conditional branching and data validation.
使用示例Direct link to 使用示例
🌐 Usage example
import { createWorkflow } from "@mastra/core/workflows";
import { z } from "zod";
export const workflow = createWorkflow({
id: "test-workflow",
inputSchema: z.object({
value: z.string(),
}),
outputSchema: z.object({
value: z.string(),
}),
});
构造函数参数Direct link to 构造函数参数
🌐 Constructor parameters
id:
inputSchema:
outputSchema:
stateSchema?:
options?:
WorkflowOptionsDirect link to WorkflowOptions
tracingPolicy?:
validateInputs?:
shouldPersistSnapshot?:
onFinish?:
onError?:
WorkflowFinishCallbackResultDirect link to WorkflowFinishCallbackResult
传递给 onFinish 回调的结果对象。
🌐 The result object passed to onFinish callbacks.
status:
result?:
error?:
steps:
tripwire?:
runId:
workflowId:
resourceId?:
getInitData:
mastra?:
requestContext:
logger:
state:
WorkflowErrorCallbackInfoDirect link to WorkflowErrorCallbackInfo
传递给 onError 回调的错误信息对象。
🌐 The error info object passed to onError callbacks.
status:
error?:
steps:
tripwire?:
runId:
workflowId:
resourceId?:
getInitData:
mastra?:
requestContext:
logger:
state:
以初始状态运行Direct link to 以初始状态运行
🌐 Running with initial state
在启动工作流运行时,你可以传递 initialState 来设置工作流状态的初始值:
🌐 When starting a workflow run, you can pass initialState to set the starting values for the workflow's state:
const run = await workflow.createRun();
const result = await run.start({
inputData: { value: "hello" },
initialState: {
counter: 0,
items: [],
},
});
initialState 对象应与工作流的 stateSchema 中定义的结构相匹配。更多详细信息,请参见 工作流状态。
🌐 The initialState object should match the structure defined in the workflow's stateSchema. See Workflow State for more details.
工作流程状态Direct link to 工作流程状态
🌐 Workflow status
工作流的 status 表示其当前的执行状态。可能的取值为:
🌐 A workflow's status indicates its current execution state. The possible values are:
success:
failed:
suspended:
tripwire:
处理绊线状态Direct link to 处理绊线状态
🌐 Handling tripwire status
当工作流包含一个触发警报的代理步骤时,工作流将返回 status: 'tripwire' 并包含警报详细信息:
🌐 When a workflow contains an agent step that triggers a tripwire, the workflow returns with status: 'tripwire' and includes tripwire details:
const run = await workflow.createRun();
const result = await run.start({ inputData: { message: "Hello" } });
if (result.status === "tripwire") {
console.log("Workflow terminated by tripwire:", result.tripwire?.reason);
console.log("Processor ID:", result.tripwire?.processorId);
console.log("Retry requested:", result.tripwire?.retry);
}
这与 status: 'failed' 不同,后者表示意外错误。触发器状态意味着处理器故意停止了执行(例如,用于内容审核)。
🌐 This is distinct from status: 'failed' which indicates an unexpected error. A tripwire status means a processor intentionally stopped execution (e.g., for content moderation).
相关Direct link to 相关
🌐 Related