repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by bugabinga
opencode/plugins/chef/pipeline/builder.ts
RawFile outline
- RetryPolicy line 3
- maxAttempts line 4
- backoffMs line 5
- retryOn line 6
- StepDefinition line 9
- name line 12
- type line 13
- agent line 14
- prompt line 15
- output line 16
- timeout line 17
- retry line 18
- GateDefinition line 21
- name line 22
- type line 23
- agent line 24
- expect line 25
- onFail line 26
- ParallelDefinition line 29
- name line 32
- type line 33
- steps line 34
- WhenDefinition line 37
- name line 40
- type line 41
- condition line 42
- loopTarget line 43
- PhaseDefinition line 46
- PipelineDefinition line 54
- name line 55
- phases line 56
- FragmentDefinition line 59
- name line 62
- steps line 63
- PipelineBuilder line 66
- phases line 69
- constructor line 71
- step line 73
- gate line 88
- parallel line 101
- when line 110
- loop line 119
- use line 127
- validate line 134
- build line 202
- pipeline line 210
- step line 218
- fragment line 227
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 };
}