> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-TW/llms.txt # Mastra.addDynamicWorkflow() > **Beta:** 動態 Workflow 目前處於 beta 階段。在 API 穩定之前,可能會出現不伴隨主版本號升級的破壞性變更。 `.addDynamicWorkflow()` 方法會驗證動態 Workflow 定義,並將其作為即時 Workflow 註冊到執行個體上,同時透過 `workflowDefinitions` Storage domain 將其持久儲存。註冊後,該 Workflow 可透過 [`getWorkflow()`](https://mastra.zisheng.pro/zh-TW/reference/core/getWorkflow) 像其他 Workflow 一樣執行。 完整設定範例請參閱[動態 Workflow](https://mastra.zisheng.pro/zh-TW/docs/workflows/dynamic-workflows),接受的欄位和 graph 項目請參閱[動態 Workflow 定義參考](https://mastra.zisheng.pro/zh-TW/reference/workflows/dynamic-workflow-definition)。 ## 使用範例 ```typescript await mastra.addDynamicWorkflow({ id: 'greeting-workflow', description: 'Returns a greeting for the supplied name', inputSchema: { type: 'object', properties: { name: { type: 'string' } }, required: ['name'], }, outputSchema: { type: 'object', properties: { message: { type: 'string' } }, required: ['message'], }, graph: [ { type: 'mapping', id: 'create-greeting', mapConfig: JSON.stringify({ message: { template: 'Hello, ${initData.name}!' }, }), }, ], }) const run = await mastra.getWorkflow('greeting-workflow').createRun() const result = await run.start({ inputData: { name: 'Ada' } }) ``` ## 參數 **def** (`DynamicWorkflowGraph`): Workflow 定義:id、選用的 description 和 metadata、JSON Schema 輸入/輸出 schema、選用的 state 和 request-context schema,以及步驟 graph。 ## 傳回值 定義完成驗證、註冊和持久儲存後,此 Promise 便會 resolve。 ## 行為 - 在變更任何內容之前,會完整驗證定義(結構、參照、schema 流程)。graph 參照的 Agent、Tool 和 Workflow 必須已在執行個體上註冊。 - 新增具有現有 ID 的定義會同時替換已儲存的定義和即時註冊。執行中的 run 會保留啟動時使用的 graph。 - 如果沒有支援 `workflowDefinitions` domain 的 Storage adapter,Workflow 仍會經過驗證並註冊到記憶體中,但重新啟動後定義會遺失。 - 要同時新增根 Workflow 及其巢狀的輔助 Workflow,請使用 [`addDynamicWorkflows()`](https://mastra.zisheng.pro/zh-TW/reference/core/addDynamicWorkflows)。 ## 相關內容 - [Mastra.addDynamicWorkflows()](https://mastra.zisheng.pro/zh-TW/reference/core/addDynamicWorkflows): 新增一組依相依關係排序的定義 - [Mastra.getWorkflow()](https://mastra.zisheng.pro/zh-TW/reference/core/getWorkflow): 取得已註冊的 Workflow - [動態 Workflow](https://mastra.zisheng.pro/zh-TW/docs/workflows/dynamic-workflows): 設定和使用動態 Workflow - [動態 Workflow 定義](https://mastra.zisheng.pro/zh-TW/reference/workflows/dynamic-workflow-definition): 定義欄位和 graph 項目