メインコンテンツへ移動

Run.stream()

.stream() メソッドは、Workflow からのレスポンスをリアルタイムでストリーミングします。イベントの ReadableStream を直接返します。

使用例
使用例への直接リンク

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<TInput>
Workflow の入力スキーマに一致する入力データ

requestContext?:

RequestContext
Workflow の実行中に使用する Request Context データ

tracingContext?:

TracingContext
子 span の作成とメタデータの追加に使用する Tracing コンテキスト。

currentSpan?:

Span
子 span の作成とメタデータの追加に使用する現在の span。

tracingOptions?:

TracingOptions
Tracing 設定のオプション。

metadata?:

Record<string, any>
ルート Trace span に追加するメタデータ。

requestContextKeys?:

string[]
この Trace のメタデータとして抽出する追加の RequestContext キー。ネストされた値にはドット記法(例: 'user.id')を使用できます。

traceId?:

string
この実行で使用する Trace ID(1〜32文字の16進数)。指定すると、この Trace は指定された Trace の一部になります。

parentSpanId?:

string
この実行で使用する親 span ID(1〜16文字の16進数)。指定すると、ルート span はこの span の子として作成されます。

tags?:

string[]
この Trace に適用するタグ。Trace の分類とフィルタリングに使用する文字列ラベルです。

closeOnSuspend?:

boolean
Workflow が中断されたときにストリームを閉じるか、Workflow が成功またはエラーで終了するまでストリームを開いたままにするかを指定します。デフォルト値は true です。

戻り値
戻り値への直接リンク

非同期イテラブルインターフェースを実装した WorkflowRunOutput オブジェクトを返します。for await...of ループで直接使用でき、ストリームと Workflow の実行結果にアクセスできます。

fullStream:

ReadableStream<WorkflowStreamEvent>
リアルタイムで進捗を追跡できる、反復可能な Workflow イベントの ReadableStream。WorkflowRunOutput オブジェクトを直接反復することもできます。

result:

Promise<WorkflowResult<TState, TInput, TOutput, TSteps>>
Workflow の最終結果に解決される Promise

status:

WorkflowRunStatus
現在の Workflow 実行ステータス('running'、'suspended'、'success'、'failed'、'canceled'、または 'tripwire')

usage:

Promise<{ inputTokens: number; outputTokens: number; totalTokens: number, reasoningTokens?: number, cachedInputTokens?: number }>
トークン使用量の統計に解決される Promise

詳細な使用例
詳細な使用例への直接リンク

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 ステップが反復ごとの進捗を報告します(completedCounttotalCountcurrentIndexiterationStatus、および省略可能な iterationOutput を含みます)
  • workflow-step-result: ステップが結果とともに完了します
  • workflow-finish: Workflow の実行が使用量の統計とともに完了します。実行が成功した場合、payload.finalWorkflowResult に Workflow の最終結果が格納されるため、ストリームの利用側で追加の取得処理は不要です