Skip to Content

run.watch()

.watch() 関数は mastra run の状態変化を購読し、実行の進行状況を監視したり、状態の更新に反応したりすることができます。

使用例

import { Workflow } from "@mastra/core/workflows"; const workflow = new Workflow({ name: "document-processor", }); const run = workflow.createRun(); // Subscribe to state changes const unsubscribe = run.watch(({ results, activePaths }) => { console.log("Results:", results); console.log("Active paths:", activePaths); }); // Run the workflow await run.start({ input: { text: "Process this document" }, }); // Stop watching unsubscribe();

パラメーター

callback:

(state: WorkflowState) => void
ワークフローの状態が変化するたびに呼び出される関数

WorkflowState のプロパティ

results:

Record<string, any>
完了したワークフローステップからの出力

activePaths:

Map<string, { status: string; suspendPayload?: any; stepPath: string[] }>
各ステップの現在のステータス

runId:

string
ワークフロー実行のID

timestamp:

number
ワークフロー実行のタイムスタンプ

戻り値

unsubscribe:

() => void
ワークフローの状態変更の監視を停止する関数

追加の例

特定のステップの完了を監視する:

run.watch(({ results, activePaths }) => { if (activePaths.get("processDocument")?.status === "completed") { console.log( "Document processing output:", results["processDocument"].output, ); } });

エラー処理:

run.watch(({ results, activePaths }) => { if (activePaths.get("processDocument")?.status === "failed") { console.error( "Document processing failed:", results["processDocument"].error, ); // Implement error recovery logic } });

関連項目