Skip to main content

Mastra.getWorkflow()

.getWorkflow() 方法用于通过其注册密钥检索工作流。该方法为工作流的输入和输出模式提供完整的 TypeScript 类型推断。

🌐 The .getWorkflow() method is used to retrieve a workflow by its registration key. This method provides full TypeScript type inference for workflow input and output schemas.

使用示例
Direct link to 使用示例

🌐 Usage example

// Retrieve workflow by its registration key (recommended for type inference)
const workflow = mastra.getWorkflow("testWorkflow");
const run = await workflow.createRun();

// TypeScript will properly infer the input schema type
await run.startAsync({
inputData: { /* properly typed */ }
});

类型推断
Direct link to 类型推断

🌐 Type inference

为了获得最佳的 TypeScript 支持,请使用工作流的 注册密钥(在将工作流添加到 Mastra 时使用的密钥)与 getWorkflow()。这可以为以下内容提供完整的类型推断:

🌐 For best TypeScript support, use getWorkflow() with the workflow's registration key (the key used when adding the workflow to Mastra). This provides full type inference for:

  • 输入数据模式
  • 输出数据模式
  • 步骤结果

如果你需要通过 id 属性而不是注册密钥来检索工作流,可以使用 getWorkflowById(),但请注意类型推断不会那么精确。

🌐 If you need to retrieve a workflow by its id property instead of its registration key, you can use getWorkflowById(), but note that type inference will not be as precise.

参数
Direct link to 参数

🌐 Parameters

id:

TWorkflowId extends keyof TWorkflows
The ID of the workflow to retrieve. Must be a valid workflow ID that exists in the Mastra configuration.

options:

{ serialized?: boolean }
Optional configuration object. When `serialized` is true, returns only the workflow name instead of the full workflow instance.

返回
Direct link to 返回

🌐 Returns

workflow:

TWorkflows[TWorkflowId]
The workflow instance with the specified ID. Throws an error if the workflow is not found.

🌐 Related