repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

opencode/plugins/chef/pipeline/builder.ts

Raw
File outline
import { z } from "zod";

export interface RetryPolicy {
  maxAttempts: number;
  backoffMs: number;
  retryOn: string[] | undefined;
}

export interface StepDefinition<
  TContext extends Record<string, any> = Record<string, any>,
> {
  name: string;
  type: "step";
  agent: string;
  prompt: string | ((ctx: TContext) => string);
  output: z.ZodType<any>;
  timeout?: number;
  retry?: RetryPolicy;
}

export interface GateDefinition {
  name: string;
  type: "gate";
  agent?: string;
  expect: z.ZodType<any>;
  onFail?: string;
}

export interface ParallelDefinition<
  TContext extends Record<string, any> = Record<string, any>,
> {
  name: string;
  type: "parallel";
  steps: StepDefinition<TContext>[];
}

export interface WhenDefinition<
  TContext extends Record<string, any> = Record<string, any>,
> {
  name: string;
  type: "when";
  condition: (ctx: TContext) => boolean;
  loopTarget?: string;
}

export type PhaseDefinition<
  TContext extends Record<string, any> = Record<string, any>,
> =
  | StepDefinition<TContext>
  | GateDefinition
  | ParallelDefinition<TContext>
  | WhenDefinition<TContext>;

export interface PipelineDefinition {
  name: string;
  phases: PhaseDefinition[];
}

export interface FragmentDefinition<
  TContext extends Record<string, any> = Record<string, any>,
> {
  name: string;
  steps: Omit<StepDefinition<TContext>, "type">[];
}

export class PipelineBuilder<
  TContext extends Record<string, any> = Record<string, any>,
> {
  private phases: PhaseDefinition<TContext>[] = [];

  constructor(private name: string) {}

  step(name: string, config: {
    agent: string;
    prompt: string | ((ctx: TContext) => string);
    output: z.ZodType<any>;
    timeout?: number;
    retry?: RetryPolicy;
  }): this {
    this.phases.push({
      name,
      type: "step",
      ...config,
    });
    return this;
  }

  gate(name: string, config: {
    agent?: string;
    expect: z.ZodType<any>;
    onFail?: string;
  }): this {
    this.phases.push({
      name,
      type: "gate",
      ...config,
    });
    return this;
  }

  parallel(steps: Omit<StepDefinition<TContext>, "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<TContext>): 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<string>();

    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<string, any> = Record<string, any>,
>(
  name: string,
): PipelineBuilder<TContext> {
  return new PipelineBuilder<TContext>(name);
}

export function step<
  TContext extends Record<string, any> = Record<string, any>,
>(
  name: string,
  config: Omit<StepDefinition<TContext>, "name" | "type">,
): Omit<StepDefinition<TContext>, "type"> {
  return { name, ...config };
}

export function fragment<
  TContext extends Record<string, any> = Record<string, any>,
>(
  name: string,
  steps: Omit<StepDefinition<TContext>, "type">[],
): FragmentDefinition<TContext> {
  return { name, steps };
}