@barnum/barnum 0.0.0-main-68fd94ca → 0.0.0-main-ccc32185

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.ts +1 -1
  2. package/package.json +1 -1
  3. package/run.ts +51 -41
package/index.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export * from "./barnum-config-schema.zod.js";
2
2
  export * from "./barnum-cli-schema.zod.js";
3
- export { BarnumConfig, type RunOptions } from "./run.js";
3
+ export { BarnumConfig, type RunOptions, type ConfigInput, type StepInput } from "./run.js";
4
4
  export { Handler, createHandler, isHandler } from "./types.js";
5
5
  export type {
6
6
  HandlerDefinition,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barnum/barnum",
3
- "version": "0.0.0-main-68fd94ca",
3
+ "version": "0.0.0-main-ccc32185",
4
4
  "type": "module",
5
5
  "description": "Barnum CLI - workflow engine for agents.",
6
6
  "main": "index.ts",
package/run.ts CHANGED
@@ -4,6 +4,7 @@ import { createRequire } from "node:module";
4
4
  import { dirname, resolve } from "node:path";
5
5
  import { fileURLToPath } from "node:url";
6
6
  import { configSchema } from "./barnum-config-schema.zod.js";
7
+ import { Handler, isHandler } from "./types.js";
7
8
  import { z } from "zod";
8
9
  import { zodToJsonSchema } from "zod-to-json-schema";
9
10
 
@@ -100,63 +101,72 @@ function assertSerializableZod(schema: z.ZodType, stepName: string): void {
100
101
  if (def.valueType) assertSerializableZod(def.valueType, stepName);
101
102
  }
102
103
 
103
- // ==================== BarnumConfig ====================
104
-
105
- export class BarnumConfig {
106
- private readonly config: z.output<typeof configSchema>;
107
-
108
- private constructor(config: z.output<typeof configSchema>) {
109
- this.config = config;
110
- }
104
+ // ==================== Config input types ====================
111
105
 
112
- static fromConfig(config: z.input<typeof configSchema>): BarnumConfig {
113
- return new BarnumConfig(configSchema.parse(config));
114
- }
106
+ type ZodConfigInput = z.input<typeof configSchema>;
107
+ type ZodStepInput = ZodConfigInput["steps"][number];
115
108
 
116
- private async resolveConfig(): Promise<z.output<typeof configSchema>> {
117
- const config = structuredClone(this.config);
109
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
110
+ export type StepInput = Omit<ZodStepInput, "action"> & {
111
+ action: { kind: "Bash"; script: string } | Handler<any, any>;
112
+ stepConfig?: unknown;
113
+ };
118
114
 
119
- for (const step of config.steps) {
120
- if (step.action.kind !== "TypeScript") continue;
121
- const action = step.action;
115
+ export type ConfigInput = Omit<ZodConfigInput, "steps"> & {
116
+ steps: StepInput[];
117
+ };
122
118
 
123
- // Import the handler module
124
- const mod = await import(action.path);
125
- const handler = mod[action.exportedAs ?? "default"];
119
+ function resolveHandlers(config: ConfigInput): ZodConfigInput {
120
+ return {
121
+ ...config,
122
+ steps: config.steps.map((step) => {
123
+ const { stepConfig, action, ...stepRest } = step;
126
124
 
127
- if (!handler?.stepConfigValidator || !handler?.getStepValueValidator) {
128
- throw new Error(
129
- `Step "${step.name}": handler at "${action.path}" is missing required ` +
130
- `"stepConfigValidator" or "getStepValueValidator". ` +
131
- `See HandlerDefinition interface.`,
132
- );
125
+ if (!isHandler(action)) {
126
+ return { ...stepRest, action } as ZodStepInput;
133
127
  }
134
128
 
135
- // Validate step config
136
- const parsedStepConfig = handler.stepConfigValidator.parse(
137
- action.stepConfig ?? {},
138
- );
129
+ const def = action.__definition;
130
+ const parsedStepConfig = def.stepConfigValidator.parse(stepConfig ?? null);
139
131
 
140
- // Get value validator
141
- const valueValidator = handler.getStepValueValidator(parsedStepConfig);
142
-
143
- // Reject non-serializable Zod features
132
+ const valueValidator = def.getStepValueValidator(parsedStepConfig);
144
133
  assertSerializableZod(valueValidator, step.name);
145
-
146
- // Convert Zod → JSON Schema and embed in config
147
- action.valueSchema = zodToJsonSchema(valueValidator, {
134
+ const valueSchema = zodToJsonSchema(valueValidator, {
148
135
  target: "jsonSchema7",
149
136
  });
150
- }
151
137
 
152
- return config;
138
+ return {
139
+ ...stepRest,
140
+ action: {
141
+ kind: "TypeScript" as const,
142
+ path: action.__filePath,
143
+ exportedAs: "default",
144
+ stepConfig: parsedStepConfig,
145
+ valueSchema,
146
+ },
147
+ };
148
+ }),
149
+ };
150
+ }
151
+
152
+ // ==================== BarnumConfig ====================
153
+
154
+ export class BarnumConfig {
155
+ private readonly config: z.output<typeof configSchema>;
156
+
157
+ private constructor(config: z.output<typeof configSchema>) {
158
+ this.config = config;
159
+ }
160
+
161
+ static fromConfig(config: ConfigInput): BarnumConfig {
162
+ const resolved = resolveHandlers(config);
163
+ return new BarnumConfig(configSchema.parse(resolved));
153
164
  }
154
165
 
155
- async run(opts?: RunOptions): Promise<ChildProcess> {
156
- const config = await this.resolveConfig();
166
+ run(opts?: RunOptions): ChildProcess {
157
167
  const args = opts?.resumeFrom
158
168
  ? ["run", "--resume-from", opts.resumeFrom]
159
- : ["run", "--config", JSON.stringify(config)];
169
+ : ["run", "--config", JSON.stringify(this.config)];
160
170
  if (opts?.entrypointValue != null)
161
171
  args.push("--entrypoint-value", JSON.stringify(opts.entrypointValue));
162
172
  if (opts?.logLevel) args.push("--log-level", opts.logLevel);