制御フロー
ワークフローを構築する際は、通常、処理を再利用可能な小さなタスクに分解してつなげます。ステップは、入力・出力・実行ロジックを定義し、これらのタスクを体系的に管理するための枠組みを提供します。
- スキーマが一致する場合、各ステップの
outputSchemaは自動的に次のステップのinputSchemaに渡されます。 - スキーマが一致しない場合は、Input data mapping を使用して、
outputSchemaを想定されるinputSchemaに変換します。
.then() を使ったステップのチェーン
.then() を使ってステップをチェーンし、順番に実行します:

import { createWorkflow, createStep } from "@mastra/core/workflows";
import { z } from "zod";
const step1 = createStep({...});
const step2 = createStep({...});
export const testWorkflow = createWorkflow({...})
.then(step1)
.then(step2)
.commit();これは想定どおりに動作します。step1 を実行し、続いて step2 を実行します。
.parallel() を使った並列ステップ
.parallel() を使ってステップを並列実行します:

import { createWorkflow, createStep } from "@mastra/core/workflows";
import { z } from "zod";
const step1 = createStep({...});
const step2 = createStep({...});
const step3 = createStep({...});
export const testWorkflow = createWorkflow({...})
.parallel([step1, step2])
.then(step3)
.commit();step1 と step2 を並行して実行し、両方の完了後に step3 に進みます。
詳しくは Parallel Execution with Steps をご覧ください。
📹 視聴: ステップを並列実行して Mastra ワークフローを最適化する方法 → YouTube(3分)
.branch() を使った条件分岐ロジック
.branch() を使って条件付きでステップを実行します:

import { createWorkflow, createStep } from "@mastra/core/workflows";
import { z } from "zod";
const lessThanStep = createStep({...});
const greaterThanStep = createStep({...});
export const testWorkflow = createWorkflow({...})
.branch([
[async ({ inputData: { value } }) => value <= 10, lessThanStep],
[async ({ inputData: { value } }) => value > 10, greaterThanStep]
])
.commit();分岐条件は順に評価されますが、条件に一致したステップは並行して実行されます。
詳細は Workflow with Conditional Branching を参照してください。
ループ処理のステップ
Workflows は2種類のループをサポートします。ステップやネストされたワークフローなど、ステップ互換の構成要素をループさせる場合、初期の inputData は前のステップの出力から供給されます。
互換性を確保するため、ループの初期入力は前のステップの出力の構造と一致しているか、map 関数を使って明示的に変換されている必要があります。
- 前のステップの出力の構造と一致させる、または
map関数を使って明示的に変換する。
.dowhile() で繰り返す
条件が真のあいだ、ステップを繰り返し実行します。

import { createWorkflow, createStep } from "@mastra/core/workflows";
import { z } from "zod";
const counterStep = createStep({...});
export const testWorkflow = createWorkflow({...})
.dowhile(counterStep, async ({ inputData: { number } }) => number < 10)
.commit();.dountil() を使った繰り返し
条件が真になるまでステップを繰り返し実行します。

import { createWorkflow, createStep } from "@mastra/core/workflows";
import { z } from "zod";
const counterStep = createStep({...});
export const testWorkflow = createWorkflow({...})
.dountil(counterStep, async ({ inputData: { number } }) => number > 10)
.commit();.foreach() による繰り返し
inputSchema の各アイテムに対して、同じステップを順に実行します。

import { createWorkflow, createStep } from "@mastra/core/workflows";
import { z } from "zod";
const mapStep = createStep({...});
export const testWorkflow = createWorkflow({...})
.foreach(mapStep)
.commit();同時実行の上限を設定する
concurrency を使うと、同時に実行する数の上限を指定しつつ、ステップを並列で実行できます。
import { createWorkflow, createStep } from "@mastra/core/workflows";
import { z } from "zod";
const mapStep = createStep({...})
export const testWorkflow = createWorkflow({...})
.foreach(mapStep, { concurrency: 2 })
.commit();ネストされたワークフローを使う
.then() に渡して、ステップとしてネストされたワークフローを使用します。これにより、その各ステップが親ワークフローの一部として順番に実行されます。
import { createWorkflow, createStep } from "@mastra/core/workflows";
import { z } from "zod";
export const nestedWorkflow = createWorkflow({...})
export const testWorkflow = createWorkflow({...})
.then(nestedWorkflow)
.commit();ワークフローのクローン作成
既存のワークフローを複製するには cloneWorkflow を使用します。これにより、id などのパラメータを上書きしながら、その構造を再利用できます。
import { createWorkflow, createStep, cloneWorkflow } from "@mastra/core/workflows";
import { z } from "zod";
const step1 = createStep({...});
const parentWorkflow = createWorkflow({...})
const clonedWorkflow = cloneWorkflow(parentWorkflow, { id: "cloned-workflow" });
export const testWorkflow = createWorkflow({...})
.then(step1)
.then(clonedWorkflow)
.commit();実行インスタンスの例
次の例は、複数の入力で実行を開始する方法を示します。各入力は mapStep を順番に通過します。
import { mastra } from "./mastra";
const run = await mastra.getWorkflow("testWorkflow").createRunAsync();
const result = await run.start({
inputData: [{ number: 10 }, { number: 100 }, { number: 200 }]
});この実行をターミナルから実行するには:
npx tsx src/test-workflow.ts