输入数据映射
🌐 Input Data Mapping
输入数据映射允许对下一步骤的输入值进行显式映射。这些值可以来自多个来源:
🌐 Input data mapping allows explicit mapping of values for the inputs of the next step. These values can come from a number of sources:
- 前一步的输出
- 请求上下文
- 常量
- 工作流的初始输入
使用 .map() 进行映射Direct link to mapping-with-map
🌐 Mapping with .map()
在这个例子中,来自 step1 的 output 被转换以匹配 step2 所需的 inputSchema。可以使用 .map 函数的 inputData 参数来获取 step1 的值。
🌐 In this example the output from step1 is transformed to match the inputSchema required for the step2. The value from step1 is available using the inputData parameter of the .map function.

const step1 = createStep({...});
const step2 = createStep({...});
export const testWorkflow = createWorkflow({...})
.then(step1)
.map(async ({ inputData }) => {
const { value } = inputData;
return {
output: `new ${value}`
};
})
.then(step2)
.commit();
使用 inputDataDirect link to using-inputdata
🌐 Using inputData
使用 inputData 访问上一步的完整输出:
🌐 Use inputData to access the full output of the previous step:
.then(step1)
.map(({ inputData }) => {
console.log(inputData);
})
使用 getStepResult()Direct link to using-getstepresult
🌐 Using getStepResult()
使用 getStepResult 通过引用步骤的实例来访问特定步骤的完整输出:
🌐 Use getStepResult to access the full output of a specific step by referencing the step's instance:
.then(step1)
.map(async ({ getStepResult }) => {
console.log(getStepResult(step1));
})
使用 getInitData<any>()Direct link to using-getinitdataany
🌐 Using getInitData<any>()
使用 getInitData<any> 来访问提供给工作流的初始输入数据:
🌐 Use getInitData<any> to access the initial input data provided to the workflow:
.then(step1)
.map(async ({ getInitData }) => {
console.log(getInitData<any>());
})
使用 mapVariable()Direct link to using-mapvariable
🌐 Using mapVariable()
要使用 mapVariable,请从 workflows 模块导入必要的函数:
🌐 To use mapVariable import the necessary function from the workflows module:
import { mapVariable } from "@mastra/core/workflows";
使用 mapVariable() 的重命名步骤Direct link to renaming-step-with-mapvariable
🌐 Renaming step with mapVariable()
你可以在 .map() 中使用对象语法重命名步骤输出。在下面的示例中,step1 的 value 输出被重命名为 details:
🌐 You can rename step outputs using the object syntax in .map(). In the example below, the value output from step1 is renamed to details:
.then(step1)
.map({
details: mapVariable({
step: step,
path: "value"
})
})
使用 mapVariable() 重命名工作流Direct link to renaming-workflows-with-mapvariable
🌐 Renaming workflows with mapVariable()
你可以使用引用组合来重命名工作流输出。这涉及将工作流实例作为 initData 传递。
🌐 You can rename workflow outputs by using referential composition. This involves passing the workflow instance as the initData.
export const testWorkflow = createWorkflow({...});
testWorkflow
.then(step1)
.map({
details: mapVariable({
initData: testWorkflow,
path: "value"
})
})