import { z } from "zod"; export interface RetryPolicy { maxAttempts: number; backoffMs: number; retryOn: string[] | undefined; } export interface StepDefinition< TContext extends Record = Record, > { name: string; type: "step"; agent: string; prompt: string | ((ctx: TContext) => string); output: z.ZodType; timeout?: number; retry?: RetryPolicy; } export interface GateDefinition { name: string; type: "gate"; agent?: string; expect: z.ZodType; onFail?: string; } export interface ParallelDefinition< TContext extends Record = Record, > { name: string; type: "parallel"; steps: StepDefinition[]; } export interface WhenDefinition< TContext extends Record = Record, > { name: string; type: "when"; condition: (ctx: TContext) => boolean; loopTarget?: string; } export type PhaseDefinition< TContext extends Record = Record, > = | StepDefinition | GateDefinition | ParallelDefinition | WhenDefinition; export interface PipelineDefinition { name: string; phases: PhaseDefinition[]; } export interface FragmentDefinition< TContext extends Record = Record, > { name: string; steps: Omit, "type">[]; } export class PipelineBuilder< TContext extends Record = Record, > { private phases: PhaseDefinition[] = []; constructor(private name: string) {} step(name: string, config: { agent: string; prompt: string | ((ctx: TContext) => string); output: z.ZodType; timeout?: number; retry?: RetryPolicy; }): this { this.phases.push({ name, type: "step", ...config, }); return this; } gate(name: string, config: { agent?: string; expect: z.ZodType; onFail?: string; }): this { this.phases.push({ name, type: "gate", ...config, }); return this; } parallel(steps: Omit, "type">[]): this { this.phases.push({ name: `parallel_${this.phases.length}`, type: "parallel", steps: steps.map((s) => ({ ...s, type: "step" as const })), }); return this; } when(condition: (ctx: TContext) => boolean): this { this.phases.push({ name: `when_${this.phases.length}`, type: "when", condition, }); return this; } loop(targetStepName: string): this { const lastPhase = this.phases[this.phases.length - 1]; if (lastPhase && lastPhase.type === "when") { lastPhase.loopTarget = targetStepName; } return this; } use(fragment: FragmentDefinition): this { this.phases.push( ...fragment.steps.map((s) => ({ ...s, type: "step" as const })), ); return this; } validate(): { valid: boolean; errors: string[] } { const errors: string[] = []; const stepNames = new Set(); for (const phase of this.phases) { // Collect step names stepNames.add(phase.name); if (phase.type === "parallel") { for (const s of phase.steps) { stepNames.add(s.name); } } // Validate each phase if (phase.type === "step") { if (!phase.output || typeof phase.output.parse !== "function") { errors.push( `Step "${phase.name}" has invalid output schema (expected Zod type)`, ); } if (!phase.agent || typeof phase.agent !== "string") { errors.push( `Step "${phase.name}" has invalid agent (expected string)`, ); } } if (phase.type === "gate") { if (!phase.expect || typeof phase.expect.parse !== "function") { errors.push( `Gate "${phase.name}" has invalid expect schema (expected Zod type)`, ); } if (phase.onFail && !stepNames.has(phase.onFail)) { errors.push( `Gate "${phase.name}" onFail target "${phase.onFail}" not found in pipeline`, ); } } if (phase.type === "when") { if (phase.loopTarget && !stepNames.has(phase.loopTarget)) { errors.push( `Loop target "${phase.loopTarget}" not found in pipeline`, ); } if (typeof phase.condition !== "function") { errors.push( `When "${phase.name}" has invalid condition (expected function)`, ); } } if (phase.type === "parallel") { for (const s of phase.steps) { if (!s.output || typeof s.output.parse !== "function") { errors.push(`Parallel step "${s.name}" has invalid output schema`); } if (!s.agent || typeof s.agent !== "string") { errors.push(`Parallel step "${s.name}" has invalid agent`); } } } } return { valid: errors.length === 0, errors }; } build(): PipelineDefinition { return { name: this.name, phases: this.phases as PhaseDefinition[], }; } } export function pipeline< TContext extends Record = Record, >( name: string, ): PipelineBuilder { return new PipelineBuilder(name); } export function step< TContext extends Record = Record, >( name: string, config: Omit, "name" | "type">, ): Omit, "type"> { return { name, ...config }; } export function fragment< TContext extends Record = Record, >( name: string, steps: Omit, "type">[], ): FragmentDefinition { return { name, steps }; }