M
Mastra•2w ago
arpitBhalla

How to use state when using `foreach`

if inputData is [1,2] and in subWorkflow1, I do setState it should be passed to subWorkflow2. (RequestContext works fine.)
workflows.forEach(subWorkflow => subWorkflow.branch([input === 1,subWorkflow1],[input === 2,subWorkflow2]]))
workflows.forEach(subWorkflow => subWorkflow.branch([input === 1,subWorkflow1],[input === 2,subWorkflow2]]))
(šŸ‘‡ actual code)
const subWorkflow1 = createWorkflow({
id: 's1',
inputSchema: z.any(),
outputSchema: z.any(),
})
.then(
createStep({
id: 's1s',
inputSchema: z.any(),
outputSchema: z.any(),
execute: async ctx => {
console.log('s1', ctx.state);
return;
},
})
)
.commit();

const subWorkflow2 = createWorkflow({
id: 's2',
inputSchema: z.any(),
outputSchema: z.any(),
})
.then(
createStep({
id: 's2s',
inputSchema: z.any(),
outputSchema: z.any(),
execute: async ctx => {
console.log('s2', ctx.state);
ctx.setState({...ctx.state, output: 2});
return;
},
})
)
.commit();

const routing = createWorkflow({
id: 'routing',
inputSchema: z.any(),
outputSchema: z.any(),
})
.branch([
[async s => s.inputData === 1, subWorkflow1],
[async s => s.inputData === 2, subWorkflow2],
])
.commit();

const workflows = createWorkflow({
id: 'root',
inputSchema: z.array(z.any()),
outputSchema: z.any(),
})
.foreach(routing)
.commit();

async function run() {
const run = await workflows.createRun();
run.start({inputData: [2, 1]});
}
run();
const subWorkflow1 = createWorkflow({
id: 's1',
inputSchema: z.any(),
outputSchema: z.any(),
})
.then(
createStep({
id: 's1s',
inputSchema: z.any(),
outputSchema: z.any(),
execute: async ctx => {
console.log('s1', ctx.state);
return;
},
})
)
.commit();

const subWorkflow2 = createWorkflow({
id: 's2',
inputSchema: z.any(),
outputSchema: z.any(),
})
.then(
createStep({
id: 's2s',
inputSchema: z.any(),
outputSchema: z.any(),
execute: async ctx => {
console.log('s2', ctx.state);
ctx.setState({...ctx.state, output: 2});
return;
},
})
)
.commit();

const routing = createWorkflow({
id: 'routing',
inputSchema: z.any(),
outputSchema: z.any(),
})
.branch([
[async s => s.inputData === 1, subWorkflow1],
[async s => s.inputData === 2, subWorkflow2],
])
.commit();

const workflows = createWorkflow({
id: 'root',
inputSchema: z.array(z.any()),
outputSchema: z.any(),
})
.foreach(routing)
.commit();

async function run() {
const run = await workflows.createRun();
run.start({inputData: [2, 1]});
}
run();
Output ->
s2 {}
s1 {}
s2 {}
s1 {}
2 Replies
Mastra Triager
Mastra Triager•2w ago
šŸ“ Created GitHub issue: https://github.com/mastra-ai/mastra/issues/10472 šŸ” If you're experiencing an error, please provide a minimal reproducible example whenever possible to help us resolve it quickly. šŸ™ Thank you for helping us improve Mastra! šŸ“ Created GitHub issue: https://github.com/mastra-ai/mastra/issues/10474 šŸ” If you're experiencing an error, please provide a minimal reproducible example whenever possible to help us resolve it quickly. šŸ™ Thank you for helping us improve Mastra!
Taofeeq
Taofeeq•5d ago
Hi @arpitBhalla , thanks for bringing this to our attention It is now fixed and will be in the next 1.0.0-beta and 0.x release

Did you find this page helpful?