@mastra/core
Version:
Mastra is a framework for building AI-powered applications and agents with a modern TypeScript stack.
93 lines (65 loc) • 2.63 kB
Markdown
# Workflow\.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.
## Usage example
```typescript
workflow.map(async ({ inputData }) => `${inputData.value} - map`)
```
## Parameters
**mappingFunction** (`(params: { inputData: any }) => any`): Function that transforms input data and returns the mapped result
## Returns
**workflow** (`Workflow`): The workflow instance for method chaining
## Using `inputData`
Use `inputData` to access the full output of the previous step.
```typescript
.then(step1)
.map(({ inputData }) => {
console.log(inputData);
})
```
## Using `getStepResult()`
Use `getStepResult()` to access the full output of a specific step by referencing the step's instance.
```typescript
.then(step1)
.map(async ({ getStepResult }) => {
console.log(getStepResult(step1));
})
```
## Using `getInitData()`
Use `getInitData<typeof workflow>()` to access the initial input data provided to the workflow.
```typescript
.then(step1)
.map(async ({ getInitData }) => {
console.log(getInitData<any>());
})
```
## Using `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:
```typescript
import { mapVariable } from '@mastra/core/workflows'
```
### Extracting fields from step outputs
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`:
```typescript
.then(step1)
.map({
details: mapVariable({
step: step1,
path: "value"
})
})
```
### Extracting fields from workflow input
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`:
```typescript
export const testWorkflow = createWorkflow({...});
testWorkflow
.then(step1)
.map({
details: mapVariable({
initData: testWorkflow,
path: "value"
})
})
```
## Related
- [Input Data Mapping](https://mastra.ai/docs/workflows/control-flow)