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:
- The outputs of a previous step
- The runtime context
- A constant value
- The initial input of the workflow
Map
In this example the output
from cityCoordinatesStep
is transformed to match the inputSchema
required for the locationDetailsStep
. The values from the cityCoordinatesStep
are available using the inputData
parameter of the .map
function.
const cityCoordinatesStep = createStep({
id: "city-step",
description: "Gets details about a city",
inputSchema: z.object({
city: z.string()
}),
outputSchema: z.object({
city_name: z.string(),
country_name: z.string(),
country_timezone: z.string()
}),
execute: async ({ inputData }) => {
const { city } = inputData;
const geocodingResponse = await fetch(`https://geocoding-api.open-meteo.com/v1/search?name=${city}`);
const geocodingData = await geocodingResponse.json();
const { name, country, timezone } = geocodingData.results[0];
return {
city_name: name,
country_name: country,
country_timezone: timezone
};
}
});
const locationDetailsStep = createStep({
id: "location-step",
description: "Display location details",
inputSchema: z.object({
details: z.string()
}),
outputSchema: z.object({
outcome: z.string()
}),
execute: async ({ inputData }) => {
const { details } = inputData;
return {
outcome: details
};
}
});
export const testWorkflow = createWorkflow({
id: "test-workflow",
description: 'Test workflow',
inputSchema: z.object({
city: z.string()
}),
outputSchema: z.object({
outcome: z.string()
})
})
.then(cityCoordinatesStep)
.map(({ inputData }) => {
const { city_name, country_name, country_timezone } = inputData;
return {
details: `${city_name}, ${country_name}, ${country_timezone}`
};
})
.then(locationDetailsStep)
.commit();
inputData
Use inputData
to access the full output of the previous step:
.map(({ inputData }) => {
const { city_name, country_name, country_timezone } = inputData;
...
})
getStepResult
Use getStepResult
to access the full output of a specific step by referencing the step’s instance:
.then(cityCoordinatesStep)
.map(({ getStepResult }) => {
console.log(getStepResult(cityCoordinatesStep));
...
})
The above log would produce an output similar to the below:
{
city_name: 'London',
country_name: 'United Kingdom',
country_timezone: 'Europe/London'
}
getInitData
Use getInitData
to access the initial input data provided to the workflow:
.then(cityCoordinatesStep)
.map(({ getInitData }) => {
console.log(getInitData());
...
})
The above log would produce an output similar to the below:
{ city: 'London' }
Renaming Outputs
Step Outputs
You can rename step outputs using the object syntax in .map()
. In the example below, the city_name
output from cityCoordinatesStep
is renamed to details
:
.then(cityCoordinatesStep)
.map({
details: {
step: cityCoordinatesStep,
path: "city_name"
}
})
The output would be similar to the below:
{ details: 'London' }
Workflow Outputs
You can rename workflow outputs by using referential composition. This involves passing the workflow instance as the initData
.
export const testWorkflow = createWorkflow({
id: "test-workflow",
description: 'Test workflow',
inputSchema: z.object({
city: z.string()
}),
outputSchema: z.object({
outcome: z.string()
})
});
testWorkflow
.then(cityCoordinatesStep)
.map({
details: {
initData: testWorkflow,
path: "city"
}
})
The output would be similar to the below:
{ details: 'London' }