> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-HK/llms.txt # Run.timeTravelStream() `.timeTravelStream()` 方法會從任何指定步驟開始重新執行 Workflow,並以串流傳送事件。進行時間旅行執行時,你可以即時接收最新資訊,同時完整掌握每個步驟的進度。 ## 使用範例 ```typescript const run = await workflow.createRun() const output = run.timeTravelStream({ step: 'step2', inputData: { value: 10 }, }) // Process events as they arrive for await (const event of output.fullStream) { console.log(event.type, event.payload) } // Get the final result const result = await output.result ``` ## 參數 所有參數都與 [`Run.timeTravel()`](https://mastra.zisheng.pro/zh-HK/reference/workflows/run-methods/timeTravel) 相同。詳細的參數文件請參閱 [timeTravel 參考](https://mastra.zisheng.pro/zh-HK/reference/workflows/run-methods/timeTravel)。 ## 傳回值 **output** (`WorkflowRunOutput>`): 同時包含串流和結果 promise 的物件 **output.fullStream** (`ReadableStream`): 隨執行進度發出 Workflow 事件的可讀串流 **output.result** (`Promise>`): 解析為最終 Workflow 執行結果的 promise **output.traceId** (`string`): 啟用 Tracing 時,與此次執行相關聯的 Trace ID ## 串流事件 串流會在執行期間發出 Workflow 事件: - `workflow-step-start`:步驟開始執行時發出 - `workflow-step-finish`:步驟成功完成時發出 - `workflow-step-error`:步驟遇到錯誤時發出 - `workflow-step-suspended`:步驟暫停時發出 - 視乎步驟類型而定的其他事件(Agent、Tool 等) ## 延伸使用範例 ### 在時間旅行期間處理事件 ```typescript const run = await workflow.createRun() const output = run.timeTravelStream({ step: 'step2', inputData: { value: 10 }, }) for await (const event of output.fullStream) { switch (event.type) { case 'workflow-step-start': console.log(`Starting step: ${event.payload.stepName}`) break case 'workflow-step-finish': console.log(`Completed step: ${event.payload.stepName}`) break case 'workflow-step-error': console.error(`Error in step: ${event.payload.stepName}`, event.payload.error) break } } const result = await output.result console.log('Time travel completed:', result) ``` ### 帶有 context 的時間旅行串流 ```typescript const output = run.timeTravelStream({ step: 'step2', context: { step1: { status: 'success', payload: { value: 0 }, output: { step1Result: 2 }, startedAt: Date.now(), endedAt: Date.now(), }, }, }) for await (const event of output.fullStream) { // Handle events console.log(event) } const result = await output.result ``` ### 帶有巢狀 Workflow 的時間旅行串流 ```typescript const output = run.timeTravelStream({ step: ['nestedWorkflow', 'step3'], inputData: { value: 10 }, nestedStepsContext: { nestedWorkflow: { step2: { status: 'success', payload: { step1Result: 2 }, output: { step2Result: 3 }, startedAt: Date.now(), endedAt: Date.now(), }, }, }, }) for await (const event of output.fullStream) { console.log(event.type, event.payload) } const result = await output.result ``` ## 注意事項 - 時間旅行執行完成或遇到錯誤時,串流會自動關閉 - Workflow 仍在執行時,你可以處理串流中的事件 - `result` promise 只會在所有步驟完成後解析 - 串流事件的格式與一般 Workflow 串流相同 - 時間旅行串流依賴持久保存的 Workflow 快照,因此必須先配置儲存空間 ## 相關內容 - [Run.timeTravel()](https://mastra.zisheng.pro/zh-HK/reference/workflows/run-methods/timeTravel) - [時間旅行](https://mastra.zisheng.pro/zh-HK/docs/workflows/time-travel) - [Workflow 串流](https://mastra.zisheng.pro/zh-HK/docs/workflows/overview) - [Run.stream()](https://mastra.zisheng.pro/zh-HK/reference/streaming/workflows/stream) - [Run.resumeStream()](https://mastra.zisheng.pro/zh-HK/reference/streaming/workflows/resumeStream)