> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-HK/llms.txt # Run.resume() `.resume()` 方法會使用新資料,從指定步驟繼續執行已暫停的 Workflow。 ## 使用範例 ```typescript const run = await workflow.createRun() const result = await run.start({ inputData: { value: 'initial data' } }) if (result.status === 'suspended') { const resumedResults = await run.resume({ resumeData: { value: 'resume data' }, }) } ``` ## 參數 **resumeData** (`z.infer`): 用於繼續執行已暫停步驟的資料 **step** (`Step | [...Step[], Step] | string | string[]`): 要從哪個或哪些步驟繼續執行。可以是 Step 實例、Step 陣列、步驟 ID 字串或步驟 ID 字串陣列 **requestContext** (`RequestContext`): 繼續執行時使用的 Request Context 資料 **retryCount** (`number`): 執行巢狀 Workflow 時的可選重試次數 **forEachIndex** (`number`): 指定已暫停 .foreach() 步驟的特定迭代。傳入要繼續執行之迭代的零起始索引;其他迭代會維持暫停。省略此欄位,即會使用相同的 resumeData 繼續執行該步驟所有已暫停的迭代。 **tracingContext** (`TracingContext`): 用於建立子 span 及加入 metadata 的 Tracing context。使用 Mastra 的 tracing 系統時會自動注入。 **tracingContext.currentSpan** (`Span`): 用於建立子 span 及加入 metadata 的目前 span。可用於建立自訂子 span,或在執行期間更新 span 屬性。 **tracingOptions** (`TracingOptions`): Tracing 設定選項。 **tracingOptions.metadata** (`Record`): 加入根 trace span 的 metadata。適合用於加入自訂屬性,例如用戶 ID、session ID 或 feature flag。 **tracingOptions.requestContextKeys** (`string[]`): 額外擷取為此 trace metadata 的 RequestContext key。支援以點號標記法指定巢狀值(例如 'user.id')。 **tracingOptions.traceId** (`string`): 此執行使用的 Trace ID(1 至 32 個十六進制字元)。如有提供,此 trace 將會成為指定 trace 的一部分。 **tracingOptions.parentSpanId** (`string`): 此執行使用的父 span ID(1 至 16 個十六進制字元)。如有提供,根 span 將會建立為此 span 的子項。 **tracingOptions.tags** (`string[]`): 套用至此 trace 的標籤。這些字串標籤可用於分類及篩選 trace。 **outputOptions** (`OutputOptions`): 輸出設定選項。 **outputOptions.includeState** (`boolean`): 是否在結果中包含 Workflow 執行狀態。 ## 傳回值 **result** (`Promise>`): 解析為 Workflow 執行結果的 promise,當中包含步驟輸出及狀態 **traceId** (`string`): 啟用 Tracing 時與此執行相關的 trace ID。可用於關聯日誌及偵錯執行流程。 **spanId** (`string`): 啟用 Tracing 時與此執行相關的根 span ID。可用於 span 層級的查找及關聯。 ## 延伸使用範例 ```typescript if (result.status === 'suspended') { const resumedResults = await run.resume({ step: result.suspended[0], resumeData: { value: 'resume data' }, }) } ``` > **備註:** 當只有一個步驟暫停時,可以省略 `step` 參數,Workflow 便會自動繼續執行該步驟。若 Workflow 有多個已暫停步驟,則必須明確指定要繼續執行的步驟。 ### 繼續執行單次 [`.foreach()`](https://mastra.zisheng.pro/zh-HK/reference/workflows/workflow-methods/foreach) 迭代 當 [`.foreach()`](https://mastra.zisheng.pro/zh-HK/reference/workflows/workflow-methods/foreach) 步驟在多次迭代中暫停時,可使用 `forEachIndex`(零起始)配合不同的 `resumeData`,逐次繼續執行個別迭代。未由 `forEachIndex` 指定的迭代會維持暫停,直至繼續執行為止。 ```typescript // Resume only the second iteration (index 1) with its own data await run.resume({ step: 'approve', resumeData: { ok: true }, forEachIndex: 1, }) // Later, resume the first iteration with different data await run.resume({ step: 'approve', resumeData: { ok: false }, forEachIndex: 0, }) ``` 如省略 `forEachIndex`,該步驟每個已暫停的迭代都會使用相同的 `resumeData` 繼續執行。 ## 相關內容 - [Workflows 概覽](https://mastra.zisheng.pro/zh-HK/docs/workflows/overview) - [Workflow.createRun()](https://mastra.zisheng.pro/zh-HK/reference/workflows/workflow-methods/create-run) - [暫停及繼續執行](https://mastra.zisheng.pro/zh-HK/docs/workflows/suspend-and-resume) - [Human in the loop](https://mastra.zisheng.pro/zh-HK/docs/workflows/human-in-the-loop)