順次ステップによるワークフロー
ワークフローは、特定の順序で次々に実行するように連鎖させることができます。
制御フローダイアグラム
この例では、then
メソッドを使用してワークフローステップを連鎖させ、順次ステップ間でデータを渡し、それらを順番に実行する方法を示しています。
こちらが制御フローダイアグラムです:

ステップの作成
ステップを作成し、ワークフローを初期化しましょう。
import { Step, Workflow } from "@mastra/core/workflows";
import { z } from "zod";
const stepOne = new Step({
id: "stepOne",
execute: async ({ context }) => ({
doubledValue: context.triggerData.inputValue * 2,
}),
});
const stepTwo = new Step({
id: "stepTwo",
execute: async ({ context }) => {
if (context.steps.stepOne.status !== "success") {
return { incrementedValue: 0 }
}
return { incrementedValue: context.steps.stepOne.output.doubledValue + 1 }
},
});
const stepThree = new Step({
id: "stepThree",
execute: async ({ context }) => {
if (context.steps.stepTwo.status !== "success") {
return { tripledValue: 0 }
}
return { tripledValue: context.steps.stepTwo.output.incrementedValue * 3 }
},
});
// Build the workflow
const myWorkflow = new Workflow({
name: "my-workflow",
triggerSchema: z.object({
inputValue: z.number(),
}),
});
ステップを連鎖してワークフローを実行する
では、ステップを連鎖させましょう。
// sequential steps
myWorkflow.step(stepOne).then(stepTwo).then(stepThree);
myWorkflow.commit();
const { start } = myWorkflow.createRun();
const res = await start({ triggerData: { inputValue: 90 } });
GitHubで例を見る