> Discover all available pages from the documentation index: https://mastra.zisheng.pro/ja/llms.txt # Run.stream() `.stream()` メソッドは、Workflow からのレスポンスをリアルタイムでストリーミングします。イベントの `ReadableStream` を直接返します。 ## 使用例 ```typescript const run = await workflow.createRun() const stream = await run.stream({ inputData: { value: 'initial data', }, }) for await (const chunk of stream) { console.log(chunk) } ``` ## パラメーター **inputData** (`z.infer`): Workflow の入力スキーマに一致する入力データ **requestContext** (`RequestContext`): Workflow の実行中に使用する Request Context データ **tracingContext** (`TracingContext`): 子 span の作成とメタデータの追加に使用する Tracing コンテキスト。 **tracingContext.currentSpan** (`Span`): 子 span の作成とメタデータの追加に使用する現在の span。 **tracingOptions** (`TracingOptions`): Tracing 設定のオプション。 **tracingOptions.metadata** (`Record`): ルート Trace span に追加するメタデータ。 **tracingOptions.requestContextKeys** (`string[]`): この Trace のメタデータとして抽出する追加の RequestContext キー。ネストされた値にはドット記法(例: 'user.id')を使用できます。 **tracingOptions.traceId** (`string`): この実行で使用する Trace ID(1〜32文字の16進数)。指定すると、この Trace は指定された Trace の一部になります。 **tracingOptions.parentSpanId** (`string`): この実行で使用する親 span ID(1〜16文字の16進数)。指定すると、ルート span はこの span の子として作成されます。 **tracingOptions.tags** (`string[]`): この Trace に適用するタグ。Trace の分類とフィルタリングに使用する文字列ラベルです。 **closeOnSuspend** (`boolean`): Workflow が中断されたときにストリームを閉じるか、Workflow が成功またはエラーで終了するまでストリームを開いたままにするかを指定します。デフォルト値は true です。 ## 戻り値 非同期イテラブルインターフェースを実装した `WorkflowRunOutput` オブジェクトを返します。`for await...of` ループで直接使用でき、ストリームと Workflow の実行結果にアクセスできます。 **fullStream** (`ReadableStream`): リアルタイムで進捗を追跡できる、反復可能な Workflow イベントの ReadableStream。WorkflowRunOutput オブジェクトを直接反復することもできます。 **result** (`Promise>`): Workflow の最終結果に解決される Promise **status** (`WorkflowRunStatus`): 現在の Workflow 実行ステータス('running'、'suspended'、'success'、'failed'、'canceled'、または 'tripwire') **usage** (`Promise<{ inputTokens: number; outputTokens: number; totalTokens: number, reasoningTokens?: number, cachedInputTokens?: number }>`): トークン使用量の統計に解決される Promise ## 詳細な使用例 ```typescript const run = await workflow.createRun() const stream = run.stream({ inputData: { value: 'initial data', }, }) // Iterate over stream events (you can iterate over stream directly or use stream.fullStream) for await (const chunk of stream) { console.log(chunk) } // Access the final result const result = await stream.result console.log('Final result:', result) // Access token usage const usage = await stream.usage console.log('Token usage:', usage) // Check current status console.log('Status:', stream.status) ``` ## ストリームイベント ストリームは、Workflow の実行中にイベントを発行します。各イベントには `type` フィールドと、関連データを含む `payload` があります。 - **`workflow-start`**: Workflow の実行が開始されます - **`workflow-step-start`**: ステップの実行が開始されます - **`workflow-step-output`**: ステップからカスタム出力が生成されます - **`workflow-step-progress`**: foreach ステップが反復ごとの進捗を報告します(`completedCount`、`totalCount`、`currentIndex`、`iterationStatus`、および省略可能な `iterationOutput` を含みます) - **`workflow-step-result`**: ステップが結果とともに完了します - **`workflow-finish`**: Workflow の実行が使用量の統計とともに完了します。実行が成功した場合、`payload.finalWorkflowResult` に Workflow の最終結果が格納されるため、ストリームの利用側で追加の取得処理は不要です ## 関連項目 - [Workflow の概要](https://mastra.zisheng.pro/ja/docs/workflows/overview) - [Workflow.createRun()](https://mastra.zisheng.pro/ja/reference/workflows/workflow-methods/create-run) - [Run.resumeStream()](https://mastra.zisheng.pro/ja/reference/streaming/workflows/resumeStream)