Skip to main content

Workflow.map()

.map() 方法将前一步骤的输出数据映射到后续步骤的输入,从而允许你在步骤之间转换数据。

🌐 The .map() method maps output data from a previous step to the input of a subsequent step, allowing you to transform data between steps.

使用示例
Direct link to 使用示例

🌐 Usage example

workflow.map(async ({ inputData }) => `${inputData.value} - map`

参数
Direct link to 参数

🌐 Parameters

mappingFunction:

(params: { inputData: any }) => any
Function that transforms input data and returns the mapped result

返回
Direct link to 返回

🌐 Returns

workflow:

Workflow
The workflow instance for method chaining

使用 inputData
Direct 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()
Direct link to using-getinitdata

🌐 Using getInitData()

使用 getInitData<typeof workflow>() 来访问提供给工作流的初始输入数据。

  .then(step1)
.map(async ({ getInitData }) => {
console.log(getInitData());
})

使用 mapVariable()
Direct link to using-mapvariable

🌐 Using mapVariable()

.map() 的对象形式提供了一种用于映射字段的替代声明性语法。你无需编写函数,而是定义一个对象,其中每个键都是新的字段名,每个值使用 mapVariable() 从前面的步骤或工作流输入中提取数据。

从 workflows 模块中导入 mapVariable()

🌐 The object form of .map() provides an alternative declarative syntax for mapping fields. Instead of writing a function, you define an object where each key is a new field name and each value uses mapVariable() to extract data from previous steps or workflow input. Import mapVariable() from the workflows module:

import { mapVariable } from "@mastra/core/workflows";

从步骤输出中提取字段
Direct link to 从步骤输出中提取字段

🌐 Extracting fields from step outputs

使用 mapVariable()step 从步骤的输出中提取特定字段,并将其映射到新的字段名称。path 参数指定要提取的字段。 在此示例中,从 step1 的输出中提取 value 字段,并映射到名为 details 的新字段:

🌐 Use mapVariable() with step to extract a specific field from a step's output and map it to a new field name. The path parameter specifies which field to extract. In this example, the value field from step1's output is extracted and mapped to a new field called details:

  .then(step1)
.map({
details: mapVariable({
step: step1,
path: "value"
})
})

从工作流输入中提取字段
Direct link to 从工作流输入中提取字段

🌐 Extracting fields from workflow input

使用 mapVariable()initData 从工作流的初始输入数据中提取特定字段。当你需要将原始工作流输入传递给后续步骤时,这非常有用。 在此示例中,从工作流输入中提取 value 字段,并映射到一个名为 details 的字段:

🌐 Use mapVariable() with initData to extract a specific field from the workflow's initial input data. This is useful when you need to pass the original workflow input to a later step. In this example, the value field from the workflow's input is extracted and mapped to a field called details:

export const testWorkflow = createWorkflow({...});

testWorkflow
.then(step1)
.map({
details: mapVariable({
initData: testWorkflow,
path: "value"
})
})

🌐 Related