@psimona/darkf 0.8.0 → 0.9.0

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.
@@ -1,6 +1,7 @@
1
1
  import { expect, test } from "bun:test";
2
+ import { okOutputType } from "../test/ok-output.ts";
2
3
  import { buildRegistry, validateWorkflow } from "./graph.ts";
3
- import { DONE, EXIT, FAILED, type WorkflowDef } from "./types.ts";
4
+ import { DONE, EXIT, FAILED, type OutputSchema, type WorkflowDef } from "./types.ts";
4
5
 
5
6
  function registryFor(workflow: WorkflowDef) {
6
7
  return buildRegistry(workflow);
@@ -11,7 +12,13 @@ test("reports no errors for a well-formed workflow", () => {
11
12
  initial: "A",
12
13
  states: {
13
14
  A: { type: "script", file: "a.ts", next: "B" },
14
- B: { type: "agent", agent: { model: "m" }, prompt: "b", on: { ok: DONE, bad: FAILED } },
15
+ B: {
16
+ type: "agent",
17
+ outputType: okOutputType,
18
+ agent: { model: "m" },
19
+ prompt: "b",
20
+ on: { ok: DONE, bad: FAILED },
21
+ },
15
22
  },
16
23
  };
17
24
 
@@ -177,11 +184,24 @@ const runnerWorkflow: WorkflowDef = {
177
184
  Group: {
178
185
  initial: "Named",
179
186
  states: {
180
- Named: { type: "agent", agent: { model: "m" }, prompt: "p", runner: "other", next: "Bare" },
187
+ Named: {
188
+ type: "agent",
189
+ outputType: okOutputType,
190
+ agent: { model: "m" },
191
+ prompt: "p",
192
+ runner: "other",
193
+ next: "Bare",
194
+ },
181
195
  },
182
196
  next: DONE,
183
197
  },
184
- Bare: { type: "agent", agent: { model: "m" }, prompt: "p", next: "Script" },
198
+ Bare: {
199
+ type: "agent",
200
+ outputType: okOutputType,
201
+ agent: { model: "m" },
202
+ prompt: "p",
203
+ next: "Script",
204
+ },
185
205
  Script: { type: "script", file: "a.ts", next: DONE },
186
206
  },
187
207
  };
@@ -208,7 +228,14 @@ test("needs no default when every agent state names a registered runner", () =>
208
228
  const workflow: WorkflowDef = {
209
229
  initial: "A",
210
230
  states: {
211
- A: { type: "agent", agent: { model: "m" }, prompt: "p", runner: "other", next: DONE },
231
+ A: {
232
+ type: "agent",
233
+ outputType: okOutputType,
234
+ agent: { model: "m" },
235
+ prompt: "p",
236
+ runner: "other",
237
+ next: DONE,
238
+ },
212
239
  },
213
240
  };
214
241
 
@@ -219,7 +246,14 @@ test("reports an inherited object key such as toString as unknown", () => {
219
246
  const workflow: WorkflowDef = {
220
247
  initial: "A",
221
248
  states: {
222
- A: { type: "agent", agent: { model: "m" }, prompt: "p", runner: "toString", next: DONE },
249
+ A: {
250
+ type: "agent",
251
+ outputType: okOutputType,
252
+ agent: { model: "m" },
253
+ prompt: "p",
254
+ runner: "toString",
255
+ next: DONE,
256
+ },
223
257
  },
224
258
  };
225
259
 
@@ -242,12 +276,30 @@ test("flags every agent state without a non-empty string model, ignoring scripts
242
276
  states: {
243
277
  Group: {
244
278
  initial: "Bare",
245
- states: { Bare: { type: "agent", prompt: "p", next: "Empty" } },
279
+ states: { Bare: { type: "agent", outputType: okOutputType, prompt: "p", next: "Empty" } },
246
280
  next: DONE,
247
281
  },
248
- Empty: { type: "agent", prompt: "p", agent: { model: "" }, next: "NotString" },
249
- NotString: { type: "agent", prompt: "p", agent: { model: 7 }, next: "Pinned" },
250
- Pinned: { type: "agent", prompt: "p", agent: { model: "m" }, next: "Script" },
282
+ Empty: {
283
+ type: "agent",
284
+ outputType: okOutputType,
285
+ prompt: "p",
286
+ agent: { model: "" },
287
+ next: "NotString",
288
+ },
289
+ NotString: {
290
+ type: "agent",
291
+ outputType: okOutputType,
292
+ prompt: "p",
293
+ agent: { model: 7 },
294
+ next: "Pinned",
295
+ },
296
+ Pinned: {
297
+ type: "agent",
298
+ outputType: okOutputType,
299
+ prompt: "p",
300
+ agent: { model: "m" },
301
+ next: "Script",
302
+ },
251
303
  Script: { type: "script", file: "a.ts", next: DONE },
252
304
  },
253
305
  };
@@ -264,9 +316,21 @@ test("takes the model from the default bag for default-runner states only", () =
264
316
  initial: "Inherit",
265
317
  agent: { model: "m" },
266
318
  states: {
267
- Inherit: { type: "agent", prompt: "p", agent: { auto: true }, next: "Cleared" },
268
- Cleared: { type: "agent", prompt: "p", agent: { model: undefined }, next: "Named" },
269
- Named: { type: "agent", prompt: "p", runner: "other", next: DONE },
319
+ Inherit: {
320
+ type: "agent",
321
+ outputType: okOutputType,
322
+ prompt: "p",
323
+ agent: { auto: true },
324
+ next: "Cleared",
325
+ },
326
+ Cleared: {
327
+ type: "agent",
328
+ outputType: okOutputType,
329
+ prompt: "p",
330
+ agent: { model: undefined },
331
+ next: "Named",
332
+ },
333
+ Named: { type: "agent", outputType: okOutputType, prompt: "p", runner: "other", next: DONE },
270
334
  },
271
335
  };
272
336
 
@@ -275,3 +339,80 @@ test("takes the model from the default bag for default-runner states only", () =
275
339
  { kind: "missing-model", state: "Named" },
276
340
  ]);
277
341
  });
342
+
343
+ // --- mandatory outputType on agent leaves (docs/specs/mandatory-agent-output-types/) ---
344
+
345
+ function outputTypeErrors(workflow: WorkflowDef) {
346
+ return validateWorkflow(registryFor(workflow), workflow);
347
+ }
348
+
349
+ test("flags an agent leaf without an outputType, ignoring scripts", () => {
350
+ const workflow: WorkflowDef = {
351
+ initial: "Script",
352
+ states: {
353
+ Script: { type: "script", file: "a.ts", next: "Bare" },
354
+ Bare: { type: "agent", agent: { model: "m" }, prompt: "p", next: DONE },
355
+ },
356
+ };
357
+
358
+ expect(outputTypeErrors(workflow)).toEqual([{ kind: "missing-output-type", state: "Bare" }]);
359
+ });
360
+
361
+ test("flags an outputType that is not an object with a safeParse function", () => {
362
+ const malformed = (value: unknown) => value as OutputSchema;
363
+ const workflow: WorkflowDef = {
364
+ initial: "NotAnObject",
365
+ states: {
366
+ NotAnObject: {
367
+ type: "agent",
368
+ agent: { model: "m" },
369
+ prompt: "p",
370
+ outputType: malformed("schema"),
371
+ next: "NoSafeParse",
372
+ },
373
+ NoSafeParse: {
374
+ type: "agent",
375
+ agent: { model: "m" },
376
+ prompt: "p",
377
+ outputType: malformed({}),
378
+ next: DONE,
379
+ },
380
+ },
381
+ };
382
+
383
+ expect(outputTypeErrors(workflow)).toEqual([
384
+ { kind: "missing-output-type", state: "NotAnObject" },
385
+ { kind: "missing-output-type", state: "NoSafeParse" },
386
+ ]);
387
+ });
388
+
389
+ test("flags an agent leaf nested in a composite", () => {
390
+ const workflow: WorkflowDef = {
391
+ initial: "Outer",
392
+ states: {
393
+ Outer: {
394
+ initial: "Bare",
395
+ states: { Bare: { type: "agent", agent: { model: "m" }, prompt: "p", next: DONE } },
396
+ next: DONE,
397
+ },
398
+ },
399
+ };
400
+
401
+ expect(outputTypeErrors(workflow)).toEqual([{ kind: "missing-output-type", state: "Bare" }]);
402
+ });
403
+
404
+ test("collects every agent leaf without an outputType in one pass", () => {
405
+ const workflow: WorkflowDef = {
406
+ initial: "A",
407
+ states: {
408
+ A: { type: "agent", agent: { model: "m" }, prompt: "p", next: "B" },
409
+ B: { type: "agent", agent: { model: "m" }, prompt: "p", next: "Script" },
410
+ Script: { type: "script", file: "a.ts", next: DONE },
411
+ },
412
+ };
413
+
414
+ expect(outputTypeErrors(workflow)).toEqual([
415
+ { kind: "missing-output-type", state: "A" },
416
+ { kind: "missing-output-type", state: "B" },
417
+ ]);
418
+ });
package/src/core/graph.ts CHANGED
@@ -65,7 +65,8 @@ export type WorkflowValidationError =
65
65
  | { kind: "dead-end-leaf"; state: string }
66
66
  | { kind: "unknown-runner"; state: string; runner: string; known: string[] }
67
67
  | { kind: "missing-runner"; state: string }
68
- | { kind: "missing-model"; state: string };
68
+ | { kind: "missing-model"; state: string }
69
+ | { kind: "missing-output-type"; state: string };
69
70
 
70
71
  /** The runners a workflow has available, as names only — `core` never sees the objects. */
71
72
  export interface AvailableRunners {
@@ -93,8 +94,9 @@ export function effectiveAgent(
93
94
  * second, cheap pass over the same flat `Map` `buildRegistry` produced, not
94
95
  * a second tree-walk. Collects every problem found; never stops at the
95
96
  * first. `DONE`/`FAILED` are valid targets only for a leaf's `on`/`next`,
96
- * never for any `initial` (composite or top-level). Without `runners`, the
97
- * runner checks are skipped.
97
+ * never for any `initial` (composite or top-level). Every agent leaf must
98
+ * declare an `outputType` (docs/specs/mandatory-agent-output-types/). Without
99
+ * `runners`, the runner checks are skipped.
98
100
  */
99
101
  export function validateWorkflow(
100
102
  registry: Map<string, RegistryEntry>,
@@ -134,6 +136,14 @@ export function validateWorkflow(
134
136
  if (typeof model !== "string" || model === "") {
135
137
  errors.push({ kind: "missing-model", state: id });
136
138
  }
139
+ const { outputType } = def;
140
+ if (
141
+ typeof outputType !== "object" ||
142
+ outputType === null ||
143
+ typeof outputType.safeParse !== "function"
144
+ ) {
145
+ errors.push({ kind: "missing-output-type", state: id });
146
+ }
137
147
  }
138
148
 
139
149
  if (runners && def.type === "agent") {