import { defineWorkflow } from '@polos/sdk';
import { z } from 'zod';
const parentStateSchema = z.object({
tasksStarted: z.number().default(0),
});
type ParentState = z.infer<typeof parentStateSchema>;
const childStateSchema = z.object({
parentId: z.string().default(''),
config: z.record(z.unknown()).default({}),
});
type ChildState = z.infer<typeof childStateSchema>;
const parentWorkflow = defineWorkflow<ParentInput, ParentState, Result>(
{ id: 'parent_workflow', stateSchema: parentStateSchema },
async (ctx, input) => {
ctx.state.tasksStarted += 1;
// Invoke child workflow with some initial values for its state
const result = await ctx.step.invokeAndWait(
'call_child',
childWorkflow,
{
payload: { data: input.data },
initialState: {
parentId: ctx.executionId,
config: { mode: 'production' },
},
},
);
return result;
},
);
const childWorkflow = defineWorkflow<ChildInput, ChildState, { processed: boolean }>(
{ id: 'child_workflow', stateSchema: childStateSchema },
async (ctx, input) => {
// Access initial state
console.log(`Parent ID: ${ctx.state.parentId}`);
console.log(`Config: ${JSON.stringify(ctx.state.config)}`);
await ctx.step.run('process', () => processData(input.data));
return { processed: true };
},
);