Skip to main content

步骤类

🌐 Step Class

Step 类定义了工作流中的各个工作单元,封装了执行逻辑、数据验证以及输入/输出处理。它可以接受工具或代理作为参数,从而自动创建步骤。

🌐 The Step class defines individual units of work within a workflow, encapsulating execution logic, data validation, and input/output handling. It can take either a tool or an agent as a parameter to automatically create a step from them.

使用示例
Direct link to 使用示例

🌐 Usage example

src/mastra/workflows/test-workflow.ts
import { createWorkflow, createStep } from "@mastra/core/workflows";
import { z } from "zod";

const step1 = createStep({
id: "step-1",
description: "passes value from input to output",
inputSchema: z.object({
value: z.number(),
}),
outputSchema: z.object({
value: z.number(),
}),
execute: async ({ inputData }) => {
const { value } = inputData;
return {
value,
};
},
});

从代理创建步骤
Direct link to 从代理创建步骤

🌐 Creating steps from agents

你可以直接从一个代理创建一个步骤。该步骤将使用代理的名称作为其ID。

🌐 You can create a step directly from an agent. The step will use the agent's name as its ID.

基本代理步骤
Direct link to 基本代理步骤

🌐 Basic agent step

src/mastra/workflows/test-workflow.ts
import { testAgent } from "../agents/test-agent";

const agentStep = createStep(testAgent);
// inputSchema: { prompt: string }
// outputSchema: { text: string }

具有结构化输出的代理步骤
Direct link to 具有结构化输出的代理步骤

🌐 Agent step with structured output

传递 structuredOutput 以让代理返回类型化的结构化数据:

🌐 Pass structuredOutput to have the agent return typed structured data:

src/mastra/workflows/test-workflow.ts
const articleSchema = z.object({
title: z.string(),
summary: z.string(),
tags: z.array(z.string()),
});

const agentStep = createStep(testAgent, {
structuredOutput: { schema: articleSchema },
});
// inputSchema: { prompt: string }
// outputSchema: { title: string, summary: string, tags: string[] }

代理步骤选项
Direct link to 代理步骤选项

🌐 Agent step options

structuredOutput:

{ schema: z.ZodType<any> }
When provided, the agent returns structured data matching this schema instead of plain text. The step's outputSchema is set to the provided schema.

onFinish:

(result: AgentResult) => void
Callback invoked when the agent completes generation.

构造函数参数
Direct link to 构造函数参数

🌐 Constructor Parameters

id:

string
Unique identifier for the step

description:

string
Optional description of what the step does

inputSchema:

z.ZodType<any>
Zod schema defining the input structure

outputSchema:

z.ZodType<any>
Zod schema defining the output structure

resumeSchema:

z.ZodType<any>
Optional Zod schema for resuming the step

suspendSchema:

z.ZodType<any>
Optional Zod schema for suspending the step

stateSchema:

z.ZodObject<any>
Optional Zod schema for the step state. Automatically injected when using Mastra's state system. The stateSchema must be a subset of the workflow's stateSchema. If not specified, type is 'any'.

execute:

(params: ExecuteParams) => Promise<any>
Async function containing step logic

ExecuteParams
Direct link to ExecuteParams

inputData:

z.infer<TStepInput>
The input data matching the inputSchema

resumeData:

z.infer<TResumeSchema>
The resume data matching the resumeSchema, when resuming the step from a suspended state. Only exists if the step is being resumed.

suspendData:

z.infer<TSuspendSchema>
The suspend data that was originally passed to suspend() when the step was suspended. Only exists if the step is being resumed and was previously suspended with data.

mastra:

Mastra
Access to Mastra services (agents, tools, etc.)

getStepResult:

(step: Step | string) => any
Function to access results from other steps

getInitData:

() => any
Function to access the initial input data of the workflow in any step

suspend:

(suspendPayload: any, suspendOptions?: { resumeLabel?: string }) => Promise<void>
Function to pause workflow execution

state:

z.infer<TState>
The current workflow state. Contains shared values that persist across all steps and suspend/resume cycles. The structure is defined by the step's stateSchema.

setState:

(state: z.infer<TState>) => void
Function to set the state of the workflow. Inject via reducer-like pattern, such as 'setState({ ...state, ...newState })'

runId:

string
Current run id

requestContext?:

RequestContext
Request Context for dependency injection and contextual information.

retryCount?:

number
The retry count for this specific step, it automatically increases each time the step is retried

🌐 Related