跳到主要内容
非官方简体中文翻译,内容版权归 Mastra 所有,遵循 Apache-2.0 许可。

Mastra.getWorkflow()

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

Usage example
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 */},
})

Type inference
Type inference的直接链接

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

  • Input data schemas
  • Output data schemas
  • Step results

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 won't be as precise.

Parameters
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.

Returns
Returns的直接链接

workflow:

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