@nylorun/core 0.4.0-beta → 0.6.0-beta

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 (44) hide show
  1. package/CHANGELOG.md +57 -0
  2. package/README.md +1 -1
  3. package/dist/compatibility.d.ts +54 -1
  4. package/dist/compatibility.js +191 -1
  5. package/dist/contracts.d.ts +554 -19
  6. package/dist/contracts.js +463 -29
  7. package/dist/define.d.ts +4 -0
  8. package/dist/define.js +2 -0
  9. package/dist/definition/sandbox-tools.js +1 -1
  10. package/dist/definition/variant.d.ts +7 -0
  11. package/dist/definition/variant.js +87 -0
  12. package/dist/definition/workflow/build.d.ts +38 -0
  13. package/dist/definition/workflow/build.js +62 -0
  14. package/dist/definition/workflow/chain.d.ts +49 -0
  15. package/dist/definition/workflow/chain.js +41 -0
  16. package/dist/definition/workflow/diagnostics.d.ts +14 -0
  17. package/dist/definition/workflow/diagnostics.js +38 -0
  18. package/dist/definition/workflow/index.d.ts +18 -0
  19. package/dist/definition/workflow/index.js +10 -0
  20. package/dist/definition/workflow/loop.d.ts +54 -0
  21. package/dist/definition/workflow/loop.js +102 -0
  22. package/dist/definition/workflow/map.d.ts +17 -0
  23. package/dist/definition/workflow/map.js +46 -0
  24. package/dist/definition/workflow/parallel.d.ts +19 -0
  25. package/dist/definition/workflow/parallel.js +37 -0
  26. package/dist/definition/workflow/patch.d.ts +11 -0
  27. package/dist/definition/workflow/patch.js +45 -0
  28. package/dist/definition/workflow/runnable.d.ts +40 -0
  29. package/dist/definition/workflow/runnable.js +251 -0
  30. package/dist/definition/workflow/slot.d.ts +23 -0
  31. package/dist/definition/workflow/slot.js +16 -0
  32. package/dist/definition/workflow/switch.d.ts +26 -0
  33. package/dist/definition/workflow/switch.js +66 -0
  34. package/dist/definition/workflow/types.d.ts +25 -0
  35. package/dist/definition/workflow/types.js +7 -0
  36. package/dist/definition/workflow/verdict.d.ts +20 -0
  37. package/dist/definition/workflow/verdict.js +25 -0
  38. package/dist/index.d.ts +1 -0
  39. package/dist/types/observe.d.ts +64 -0
  40. package/dist/types/workflow.d.ts +106 -0
  41. package/dist/types/workflow.js +1 -0
  42. package/dist/utils/hash.d.ts +3 -2
  43. package/dist/utils/hash.js +4 -4
  44. package/package.json +2 -2
@@ -0,0 +1,251 @@
1
+ import { bindingFromAgent } from "../binding.js";
2
+ import { normalizeToolDefinition, normalizeSchema } from "../schema.js";
3
+ import { HarnessError } from "../../errors.js";
4
+ import { isBuiltWorkflow } from "./types.js";
5
+ import { failOne } from "./diagnostics.js";
6
+ import { isSlot } from "./slot.js";
7
+ function isToolDefinition(value) {
8
+ if (!value || typeof value !== "object")
9
+ return false;
10
+ if (!("name" in value) || typeof value.name !== "string")
11
+ return false;
12
+ const v = value;
13
+ return (typeof v.execute === "function" ||
14
+ typeof v.run === "function" ||
15
+ v.inputSchema !== undefined ||
16
+ v.input !== undefined);
17
+ }
18
+ function isAgentLike(value) {
19
+ if (!value || typeof value !== "object")
20
+ return false;
21
+ if (isBuiltWorkflow(value))
22
+ return false;
23
+ return ("id" in value &&
24
+ typeof value.id === "string" &&
25
+ "manifest" in value &&
26
+ typeof value.getBinding === "function");
27
+ }
28
+ function builtAgentOf(run) {
29
+ if (run &&
30
+ typeof run === "object" &&
31
+ "getBinding" in run &&
32
+ typeof run.getBinding === "function" &&
33
+ "manifest" in run &&
34
+ !isBuiltWorkflow(run))
35
+ return run;
36
+ if (run && typeof run === "object" && "build" in run && typeof run.build === "function")
37
+ return run.build();
38
+ failOne("workflow.invalid-runnable", "Expected a built agent or Agent builder");
39
+ }
40
+ /** Collect sandbox specs declared on an agent (and its delegates, one level). */
41
+ export function sandboxSpecsOf(agent) {
42
+ const specs = [];
43
+ const binding = bindingFromAgent(agent);
44
+ for (const cap of binding.manifest.capabilities) {
45
+ if (cap.sandbox)
46
+ specs.push(cap.sandbox);
47
+ }
48
+ for (const tool of binding.tools) {
49
+ const child = tool.delegate?.agent;
50
+ if (!child)
51
+ continue;
52
+ for (const cap of child.manifest.capabilities) {
53
+ if (cap.sandbox)
54
+ specs.push(cap.sandbox);
55
+ }
56
+ }
57
+ return specs;
58
+ }
59
+ /**
60
+ * Project a tool for use as a workflow node.
61
+ * Unlike agent tools, workflow tool nodes may take any JSON Schema root type (workflows.md §9).
62
+ * Built without `bindTool` so we skip the agent-only object-input rule in `normalizeSchema`.
63
+ */
64
+ function bindToolNode(tool) {
65
+ const normalized = normalizeToolDefinition(tool);
66
+ if (!normalized.name)
67
+ throw new HarnessError("tool.invalid-name", "Tool name must not be empty");
68
+ const execute = normalized.execute ?? normalized.run;
69
+ if (typeof execute !== "function")
70
+ throw new HarnessError("tool.invalid", `Tool '${normalized.name}' must provide run() or execute()`);
71
+ const rawInput = (normalized.inputSchema ?? normalized.input);
72
+ if (!rawInput)
73
+ throw new HarnessError("tool.invalid-schema", `Tool '${normalized.name}' must provide input or inputSchema`);
74
+ // role "output" skips the object-root check used for agent tool inputs.
75
+ const inputSchema = schemaForNode(rawInput, "input");
76
+ const rawOutput = (normalized.outputSchema ?? normalized.output);
77
+ const outputSchema = rawOutput ? schemaForNode(rawOutput, "output") : undefined;
78
+ return Object.freeze({
79
+ source: tool,
80
+ name: normalized.name,
81
+ ...(normalized.description ? { description: normalized.description } : {}),
82
+ inputSchema,
83
+ ...(outputSchema === undefined ? {} : { outputSchema }),
84
+ execute: execute.bind(normalized),
85
+ ...(normalized.approval === undefined ? {} : { approval: normalized.approval }),
86
+ ...(normalized.effects === undefined ? {} : { effects: normalized.effects }),
87
+ owner: Object.freeze({ middlewareId: "workflow", slot: "tool-node" }),
88
+ });
89
+ }
90
+ /** Normalize a schema for a workflow tool node (any JSON Schema root type allowed). */
91
+ function schemaForNode(source, role) {
92
+ if (source &&
93
+ typeof source === "object" &&
94
+ "jsonSchema" in source &&
95
+ typeof source.validate === "function")
96
+ return source;
97
+ // Pass role "output" so normalizeSchema does not require an object root.
98
+ return normalizeSchema(source, role === "input" ? "output" : role);
99
+ }
100
+ function toolManifestNode(bound) {
101
+ return {
102
+ tool: {
103
+ name: bound.name,
104
+ ...(bound.description === undefined ? {} : { description: bound.description }),
105
+ inputSchema: bound.inputSchema.jsonSchema,
106
+ ...(bound.outputSchema === undefined
107
+ ? {}
108
+ : { outputSchema: bound.outputSchema.jsonSchema }),
109
+ },
110
+ };
111
+ }
112
+ function agentNode(agent) {
113
+ const binding = bindingFromAgent(agent);
114
+ return {
115
+ node: { agent: agent.id },
116
+ agents: { [agent.id]: binding },
117
+ sandboxSpecs: sandboxSpecsOf(agent),
118
+ };
119
+ }
120
+ function resolveRunnable(run, rename) {
121
+ if (isBuiltWorkflow(run)) {
122
+ const binding = run.getBinding();
123
+ const id = rename ?? run.id;
124
+ return {
125
+ id,
126
+ node: binding.manifest.root,
127
+ agents: { ...binding.agents },
128
+ nodes: { ...binding.nodes },
129
+ sandboxSpecs: sandboxSpecsFromAgents(binding.agents),
130
+ isReshapingSlot: false,
131
+ };
132
+ }
133
+ if (isToolDefinition(run)) {
134
+ const bound = bindToolNode(run);
135
+ const id = rename ?? bound.name;
136
+ return {
137
+ id,
138
+ node: toolManifestNode(bound),
139
+ agents: {},
140
+ nodes: { [id]: { kind: "tool", tool: bound } },
141
+ tool: bound,
142
+ sandboxSpecs: [],
143
+ isReshapingSlot: false,
144
+ };
145
+ }
146
+ if (isAgentLike(run) ||
147
+ (run && typeof run === "object" && "build" in run && typeof run.build === "function")) {
148
+ const agent = builtAgentOf(run);
149
+ const resolved = agentNode(agent);
150
+ const id = rename ?? agent.id;
151
+ return {
152
+ id,
153
+ node: resolved.node,
154
+ agents: resolved.agents,
155
+ nodes: {},
156
+ sandboxSpecs: resolved.sandboxSpecs,
157
+ isReshapingSlot: false,
158
+ };
159
+ }
160
+ failOne("workflow.invalid-runnable", "Unsupported workflow runnable");
161
+ }
162
+ function sandboxSpecsFromAgents(agents) {
163
+ const specs = [];
164
+ for (const binding of Object.values(agents)) {
165
+ for (const cap of binding.manifest.capabilities) {
166
+ if (cap.sandbox)
167
+ specs.push(cap.sandbox);
168
+ }
169
+ }
170
+ return specs;
171
+ }
172
+ /**
173
+ * Resolve a runnable or slot into a manifest node plus local bindings.
174
+ * When the slot has `input`, the node is wrapped as `{ slot: { id?, input, run } }`.
175
+ */
176
+ export function resolveChild(child, defaultRename) {
177
+ if (isSlot(child)) {
178
+ const slot = child;
179
+ const resolved = resolveRunnable(slot.run, slot.id ?? defaultRename);
180
+ const id = slot.id ?? resolved.id;
181
+ const hasInput = typeof slot.input === "function";
182
+ if (!hasInput && slot.id === undefined) {
183
+ return { ...resolved, id, isReshapingSlot: false };
184
+ }
185
+ const node = {
186
+ slot: {
187
+ ...(slot.id !== undefined ? { id: slot.id } : {}),
188
+ ...(hasInput ? { input: { fn: true } } : {}),
189
+ run: resolved.node,
190
+ },
191
+ };
192
+ const remapped = remapNodeKeys(resolved.nodes, resolved.id, id);
193
+ if (hasInput) {
194
+ remapped[`${id}/input`] = {
195
+ kind: "fn",
196
+ fn: slot.input,
197
+ };
198
+ }
199
+ return {
200
+ id,
201
+ node,
202
+ agents: resolved.agents,
203
+ nodes: remapped,
204
+ tool: resolved.tool,
205
+ sandboxSpecs: resolved.sandboxSpecs,
206
+ isReshapingSlot: hasInput,
207
+ };
208
+ }
209
+ return resolveRunnable(child, defaultRename);
210
+ }
211
+ /**
212
+ * Rewrite node keys that are rooted at `fromRoot` so they sit at `toPath`.
213
+ * Used when nesting a child under a parent path (workflows.md §6).
214
+ *
215
+ * Example: Loop nodes `{ code, code/decide }` nested at `ship/code` become
216
+ * `{ ship/code, ship/code/decide }`.
217
+ */
218
+ export function remapNodeKeys(nodes, fromRoot, toPath) {
219
+ if (fromRoot === toPath)
220
+ return nodes;
221
+ const out = {};
222
+ for (const [key, impl] of Object.entries(nodes)) {
223
+ if (key === fromRoot)
224
+ out[toPath] = impl;
225
+ else if (key.startsWith(`${fromRoot}/`))
226
+ out[`${toPath}${key.slice(fromRoot.length)}`] = impl;
227
+ else
228
+ out[`${toPath}/${key}`] = impl;
229
+ }
230
+ return out;
231
+ }
232
+ /** @deprecated Prefer remapNodeKeys — kept for call-site clarity during migration. */
233
+ export function prefixNodeKeys(nodes, parentPath) {
234
+ return remapNodeKeys(nodes, "", parentPath);
235
+ }
236
+ export function runnableId(child) {
237
+ if (isSlot(child)) {
238
+ if (child.id !== undefined)
239
+ return child.id;
240
+ return runnableId(child.run);
241
+ }
242
+ if (isBuiltWorkflow(child))
243
+ return child.id;
244
+ if (isToolDefinition(child))
245
+ return child.name;
246
+ if (isAgentLike(child))
247
+ return child.id;
248
+ if (child && typeof child === "object" && "build" in child && typeof child.build === "function")
249
+ return child.build().id;
250
+ failOne("workflow.invalid-runnable", "Cannot determine runnable id");
251
+ }
@@ -0,0 +1,23 @@
1
+ import type { JsonValue } from "../../types/shared.js";
2
+ import type { WorkflowRunnable } from "./runnable.js";
3
+ /**
4
+ * Arguments for a slot `input` reshape function (workflows.md §4).
5
+ * `results` comes from the nearest enclosing Chain; `index` only in Map `each`.
6
+ */
7
+ export type SlotInputArgs<Value = JsonValue, Input = JsonValue, Results extends Readonly<Record<string, JsonValue>> = Readonly<Record<string, JsonValue>>> = {
8
+ readonly value: Value;
9
+ readonly input: Input;
10
+ readonly results: Results;
11
+ readonly index?: number;
12
+ };
13
+ /**
14
+ * Plain-object rename/reshape wrapper. There is no helper constructor —
15
+ * write `{ run, id?, input? }` (workflows.md §4, decision 5).
16
+ */
17
+ export type Slot<Run extends WorkflowRunnable = WorkflowRunnable, Value = JsonValue, Input = JsonValue, Results extends Readonly<Record<string, JsonValue>> = Readonly<Record<string, JsonValue>>> = {
18
+ readonly run: Run;
19
+ readonly id?: string;
20
+ readonly input?: (args: SlotInputArgs<Value, Input, Results>) => JsonValue;
21
+ };
22
+ export type ChildRef<Run extends WorkflowRunnable = WorkflowRunnable> = Run | Slot<Run>;
23
+ export declare function isSlot(value: unknown): value is Slot;
@@ -0,0 +1,16 @@
1
+ export function isSlot(value) {
2
+ if (!value || typeof value !== "object")
3
+ return false;
4
+ if (!("run" in value))
5
+ return false;
6
+ // Agents / workflows expose manifest + getBinding; tools expose name + execute/run.
7
+ if ("manifest" in value || "getBinding" in value)
8
+ return false;
9
+ if ("name" in value &&
10
+ (typeof value.execute === "function" ||
11
+ typeof value.run === "function" ||
12
+ "inputSchema" in value ||
13
+ "input" in value))
14
+ return false;
15
+ return true;
16
+ }
@@ -0,0 +1,26 @@
1
+ import type { JsonValue } from "../../types/shared.js";
2
+ import type { ToolSchemaSource } from "../../types/tool.js";
3
+ import { type WorkflowRunnable } from "./runnable.js";
4
+ import type { ChildRef } from "./slot.js";
5
+ import type { BuiltWorkflow, OutputOf } from "./types.js";
6
+ export type SwitchCases = Readonly<Record<string, ChildRef>>;
7
+ export type SwitchOn<In, Cases extends SwitchCases, HasDefault extends boolean> = HasDefault extends true ? (input: In) => string : (input: In) => Extract<keyof Cases, string>;
8
+ export type SwitchOutput<Cases extends SwitchCases, Default extends ChildRef | undefined> = OutputOf<Cases[keyof Cases]> | (Default extends ChildRef ? OutputOf<Default> : never);
9
+ export interface SwitchOptions<Cases extends SwitchCases = SwitchCases, In = JsonValue, Default extends ChildRef | undefined = undefined> {
10
+ readonly id: string;
11
+ readonly on: SwitchOn<In, Cases, Default extends ChildRef ? true : false>;
12
+ readonly cases: Cases;
13
+ readonly default?: Default;
14
+ readonly inputSchema?: ToolSchemaSource;
15
+ readonly outputSchema?: ToolSchemaSource;
16
+ }
17
+ /**
18
+ * Pick exactly one case by a pure `on` key (switch.md).
19
+ * `default` is reserved and cannot be a case key.
20
+ */
21
+ export declare function Switch<const Cases extends SwitchCases, In = JsonValue, Default extends ChildRef | undefined = undefined>(options: SwitchOptions<Cases, In, Default> & {
22
+ readonly cases: Cases;
23
+ readonly default?: Default;
24
+ }): BuiltWorkflow<In, SwitchOutput<Cases, Default>>;
25
+ /** @deprecated Prefer Switch — kept for documentation symmetry with decision tables. */
26
+ export type SwitchRunnable = WorkflowRunnable;
@@ -0,0 +1,66 @@
1
+ import { assertValidPathPart, diagnostic, fail, } from "./diagnostics.js";
2
+ import { emptyAccumulator, finishWorkflow, mergeChild } from "./build.js";
3
+ import { remapNodeKeys, resolveChild } from "./runnable.js";
4
+ /**
5
+ * Pick exactly one case by a pure `on` key (switch.md).
6
+ * `default` is reserved and cannot be a case key.
7
+ */
8
+ export function Switch(options) {
9
+ const { id, on, cases } = options;
10
+ if (!id)
11
+ fail([diagnostic("switch.missing-id", "Switch requires id")]);
12
+ assertValidPathPart(id, "Switch id");
13
+ if (typeof on !== "function")
14
+ fail([diagnostic("switch.missing-on", "Switch requires on")]);
15
+ if (!cases || Object.keys(cases).length === 0)
16
+ fail([diagnostic("switch.empty-cases", "Switch cases must have at least one entry")]);
17
+ const caseKeys = Object.keys(cases);
18
+ for (const key of caseKeys) {
19
+ if (key === "default")
20
+ fail([
21
+ diagnostic("switch.reserved-default", "default is reserved and cannot be a case key"),
22
+ ]);
23
+ assertValidPathPart(key, "Switch case key");
24
+ }
25
+ const acc = emptyAccumulator();
26
+ const caseNodes = {};
27
+ for (const key of caseKeys) {
28
+ const resolved = resolveChild(cases[key]);
29
+ const casePath = `${id}/${key}`;
30
+ mergeChild(acc, {
31
+ agents: resolved.agents,
32
+ nodes: remapNodeKeys(resolved.nodes, resolved.id, casePath),
33
+ sandboxSpecs: resolved.sandboxSpecs,
34
+ });
35
+ caseNodes[key] = resolved.node;
36
+ }
37
+ let defaultNode;
38
+ if (options.default !== undefined) {
39
+ const resolved = resolveChild(options.default);
40
+ const defaultPath = `${id}/default`;
41
+ mergeChild(acc, {
42
+ agents: resolved.agents,
43
+ nodes: remapNodeKeys(resolved.nodes, resolved.id, defaultPath),
44
+ sandboxSpecs: resolved.sandboxSpecs,
45
+ });
46
+ defaultNode = resolved.node;
47
+ }
48
+ acc.nodes[`${id}/on`] = {
49
+ kind: "fn",
50
+ fn: on,
51
+ };
52
+ return finishWorkflow({
53
+ id,
54
+ root: {
55
+ switch: {
56
+ id,
57
+ on: { fn: true },
58
+ cases: caseNodes,
59
+ ...(defaultNode === undefined ? {} : { default: defaultNode }),
60
+ },
61
+ },
62
+ acc,
63
+ inputSchema: options.inputSchema,
64
+ outputSchema: options.outputSchema,
65
+ });
66
+ }
@@ -0,0 +1,25 @@
1
+ import type { WorkflowBinding, WorkflowManifest } from "../../types/workflow.js";
2
+ import type { JsonValue } from "../../types/shared.js";
3
+ import type { ToolSchemaSource } from "../../types/tool.js";
4
+ /** A workflow built with Chain / Switch / Parallel / Map / Loop. */
5
+ export interface BuiltWorkflow<In = JsonValue, Out = JsonValue> {
6
+ readonly id: string;
7
+ readonly manifest: WorkflowManifest;
8
+ readonly inputSchema?: ToolSchemaSource;
9
+ readonly outputSchema?: ToolSchemaSource;
10
+ toJSON(): WorkflowManifest;
11
+ getBinding(): WorkflowBinding;
12
+ /** Phantom type carriers for inference (never present at runtime). */
13
+ readonly __input?: In;
14
+ readonly __output?: Out;
15
+ }
16
+ export declare function isBuiltWorkflow(value: unknown): value is BuiltWorkflow;
17
+ /** Infer a runnable's output type when known; otherwise JsonValue. */
18
+ export type OutputOf<R> = R extends BuiltWorkflow<any, infer O> ? O : R extends {
19
+ readonly __output?: infer O;
20
+ } ? O : R extends {
21
+ build(): BuiltWorkflow<any, infer O>;
22
+ } ? O : JsonValue;
23
+ export type InputOf<R> = R extends BuiltWorkflow<infer I, any> ? I : R extends {
24
+ readonly __input?: infer I;
25
+ } ? I : JsonValue;
@@ -0,0 +1,7 @@
1
+ export function isBuiltWorkflow(value) {
2
+ return (!!value &&
3
+ typeof value === "object" &&
4
+ "manifest" in value &&
5
+ value.manifest?.kind === "workflow" &&
6
+ typeof value.getBinding === "function");
7
+ }
@@ -0,0 +1,20 @@
1
+ import { z } from "zod";
2
+ import type { Verdict as VerdictValue } from "../../types/workflow.js";
3
+ /**
4
+ * Zod schema for Loop verify outcomes (loops.md §3.2).
5
+ * Extensible via `.extend({ data: ... })` as in the design examples.
6
+ * Feedback is required on failure — enforced by `isVerdict` / `VerdictSchema`.
7
+ */
8
+ export declare const Verdict: z.ZodObject<{
9
+ pass: z.ZodBoolean;
10
+ feedback: z.ZodOptional<z.ZodString>;
11
+ data: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
12
+ }, z.core.$strip>;
13
+ /** Full Verdict validation including required feedback on failure. */
14
+ export declare const VerdictSchema: z.ZodObject<{
15
+ pass: z.ZodBoolean;
16
+ feedback: z.ZodOptional<z.ZodString>;
17
+ data: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
18
+ }, z.core.$strip>;
19
+ /** True when a value matches the Verdict wire shape. */
20
+ export declare function isVerdict(value: unknown): value is VerdictValue;
@@ -0,0 +1,25 @@
1
+ import { z } from "zod";
2
+ /**
3
+ * Zod schema for Loop verify outcomes (loops.md §3.2).
4
+ * Extensible via `.extend({ data: ... })` as in the design examples.
5
+ * Feedback is required on failure — enforced by `isVerdict` / `VerdictSchema`.
6
+ */
7
+ export const Verdict = z.object({
8
+ pass: z.boolean(),
9
+ feedback: z.string().optional(),
10
+ data: z.record(z.string(), z.any()).optional(),
11
+ });
12
+ /** Full Verdict validation including required feedback on failure. */
13
+ export const VerdictSchema = Verdict.superRefine((value, ctx) => {
14
+ if (value.pass === false && (value.feedback === undefined || value.feedback === "")) {
15
+ ctx.addIssue({
16
+ code: "custom",
17
+ message: "Verdict feedback is required when pass is false",
18
+ path: ["feedback"],
19
+ });
20
+ }
21
+ });
22
+ /** True when a value matches the Verdict wire shape. */
23
+ export function isVerdict(value) {
24
+ return VerdictSchema.safeParse(value).success;
25
+ }
package/dist/index.d.ts CHANGED
@@ -8,3 +8,4 @@ export type * from "./types/model.js";
8
8
  export type * from "./types/shared.js";
9
9
  export type * from "./types/manifest.js";
10
10
  export type * from "./types/transcript.js";
11
+ export type * from "./types/workflow.js";
@@ -2,6 +2,70 @@ import type { ContextSnapshot, ModelCandidate, ModelCall, ModelConfigurationSnap
2
2
  import type { JsonObject, JsonValue } from "./shared.js";
3
3
  import type { InputEvent, TranscriptEntry } from "./transcript.js";
4
4
  import type { AgentRef, RequiredInteraction, ToolDescriptor, ToolResult } from "./tool.js";
5
+ /** Curated failure carried on workflow `node.failed` and `turn.failed`. */
6
+ export interface WorkflowErrorPayload {
7
+ readonly code: string;
8
+ readonly message: string;
9
+ readonly path?: string;
10
+ }
11
+ /** LiveEvent payloads for workflow sessions (`workflows.md` §11, `loops.md` §4.6). */
12
+ export type WorkflowLiveEventPayload = {
13
+ readonly type: "node.started";
14
+ readonly path: string;
15
+ readonly kind: string;
16
+ readonly key: string;
17
+ readonly iterations?: string;
18
+ } | {
19
+ readonly type: "node.completed";
20
+ readonly path: string;
21
+ readonly iterations?: string;
22
+ } | {
23
+ readonly type: "node.failed";
24
+ readonly path: string;
25
+ readonly iterations?: string;
26
+ readonly error: WorkflowErrorPayload;
27
+ } | {
28
+ readonly type: "node.agent";
29
+ readonly path: string;
30
+ readonly iterations?: string;
31
+ readonly sessionId: string;
32
+ readonly turnId: string;
33
+ } | {
34
+ readonly type: "switch.selected";
35
+ readonly path: string;
36
+ readonly case: string;
37
+ } | {
38
+ readonly type: "map.items";
39
+ readonly path: string;
40
+ readonly count: number;
41
+ } | {
42
+ readonly type: "loop.iteration";
43
+ readonly path: string;
44
+ readonly n: number;
45
+ readonly sessionId?: string;
46
+ readonly turnId?: string;
47
+ readonly manifestHash?: string;
48
+ } | {
49
+ readonly type: "loop.waiting";
50
+ readonly path: string;
51
+ readonly n: number;
52
+ readonly sessionId: string;
53
+ readonly interactions: readonly RequiredInteraction[];
54
+ } | {
55
+ readonly type: "loop.verified";
56
+ readonly path: string;
57
+ readonly n: number;
58
+ readonly pass: boolean;
59
+ readonly feedback?: string;
60
+ readonly data?: JsonValue;
61
+ readonly verifierSessionId?: string;
62
+ } | {
63
+ readonly type: "loop.decided";
64
+ readonly path: string;
65
+ readonly n: number;
66
+ readonly next: "input" | "output";
67
+ readonly patched: boolean;
68
+ };
5
69
  export type ObserveToolSnapshot = ToolDescriptor;
6
70
  /** JSON-only model-configuration view suitable for an observation stream. */
7
71
  export interface ObserveModelConfigurationSnapshot extends Omit<ModelConfigurationSnapshot, "tools"> {
@@ -0,0 +1,106 @@
1
+ import type { AgentBinding } from "../definition/binding.js";
2
+ import type { BoundToolDefinition } from "../definition/bound.js";
3
+ import type { JsonObject, JsonValue } from "./shared.js";
4
+ import type { SandboxManifest } from "./manifest.js";
5
+ /** Loop verify outcome: pass, or fail with required feedback. */
6
+ export type Verdict = {
7
+ readonly pass: true;
8
+ readonly data?: JsonValue;
9
+ } | {
10
+ readonly pass: false;
11
+ readonly feedback: string;
12
+ readonly data?: JsonValue;
13
+ };
14
+ /** Local function marker in a workflow manifest (code never crosses the wire). */
15
+ export interface WorkflowFnRef {
16
+ readonly fn: true;
17
+ }
18
+ /** Agent leaf: referenced by definition id. */
19
+ export interface WorkflowAgentNode {
20
+ readonly agent: string;
21
+ }
22
+ /** Tool leaf: name and schemas only; implementation stays in the binding. */
23
+ export interface WorkflowToolNode {
24
+ readonly tool: {
25
+ readonly name: string;
26
+ readonly description?: string;
27
+ readonly inputSchema?: JsonObject;
28
+ readonly outputSchema?: JsonObject;
29
+ };
30
+ }
31
+ export interface WorkflowChainNode {
32
+ readonly chain: {
33
+ readonly id: string;
34
+ readonly steps: readonly WorkflowNode[];
35
+ };
36
+ }
37
+ export interface WorkflowSwitchNode {
38
+ readonly switch: {
39
+ readonly id: string;
40
+ readonly on: WorkflowFnRef;
41
+ readonly cases: Readonly<Record<string, WorkflowNode>>;
42
+ readonly default?: WorkflowNode;
43
+ };
44
+ }
45
+ export interface WorkflowParallelNode {
46
+ readonly parallel: {
47
+ readonly id: string;
48
+ readonly branches: Readonly<Record<string, WorkflowNode>>;
49
+ };
50
+ }
51
+ export interface WorkflowMapNode {
52
+ readonly map: {
53
+ readonly id: string;
54
+ readonly over: WorkflowFnRef;
55
+ readonly each: WorkflowNode;
56
+ };
57
+ }
58
+ export interface WorkflowLoopNode {
59
+ readonly loop: {
60
+ readonly id: string;
61
+ readonly run: WorkflowNode;
62
+ readonly verify: WorkflowFnRef | WorkflowAgentNode | WorkflowSlotNode;
63
+ readonly decide: WorkflowFnRef;
64
+ };
65
+ }
66
+ /** Rename and/or reshape a child: `{ slot: { id?, input?, run } }`. */
67
+ export interface WorkflowSlotNode {
68
+ readonly slot: {
69
+ readonly id?: string;
70
+ readonly input?: WorkflowFnRef;
71
+ readonly run: WorkflowNode;
72
+ };
73
+ }
74
+ export type WorkflowNode = WorkflowAgentNode | WorkflowToolNode | WorkflowChainNode | WorkflowSwitchNode | WorkflowParallelNode | WorkflowMapNode | WorkflowLoopNode | WorkflowSlotNode;
75
+ /** Wire document for a workflow definition. Missing `kind` is never a workflow. */
76
+ export interface WorkflowManifest {
77
+ readonly kind: "workflow";
78
+ readonly workflowSchemaVersion: 1;
79
+ readonly id: string;
80
+ readonly root: WorkflowNode;
81
+ readonly sandbox?: SandboxManifest;
82
+ }
83
+ /**
84
+ * Local implementation for a node key: a tool node, a pure `fn`, or a Loop `verify` function.
85
+ * Functions are never serialized.
86
+ */
87
+ export type WorkflowNodeImplementation = {
88
+ readonly kind: "tool";
89
+ readonly tool: BoundToolDefinition;
90
+ } | {
91
+ readonly kind: "fn";
92
+ readonly fn: (...args: never[]) => unknown;
93
+ } | {
94
+ readonly kind: "verify";
95
+ readonly fn: (...args: never[]) => unknown;
96
+ };
97
+ /**
98
+ * Built-workflow binding: manifest plus node-key → implementation, and referenced agent bindings.
99
+ * Not a wire format; `toJSON()` stays manifest-only.
100
+ */
101
+ export interface WorkflowBinding {
102
+ readonly manifest: WorkflowManifest;
103
+ /** Node key (path without Map indices) → local code. */
104
+ readonly nodes: Readonly<Record<string, WorkflowNodeImplementation>>;
105
+ readonly agents: Readonly<Record<string, AgentBinding>>;
106
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -1,3 +1,4 @@
1
1
  import type { AgentManifest } from "../types/manifest.js";
2
- /** Canonical SHA-256 hex digest of a manifest (agent identity). */
3
- export declare function hashManifest(manifest: AgentManifest): string;
2
+ import type { WorkflowManifest } from "../types/workflow.js";
3
+ /** Canonical SHA-256 hex digest of an agent or workflow definition document. */
4
+ export declare function hashManifest(manifest: AgentManifest | WorkflowManifest): string;
@@ -1,7 +1,7 @@
1
- import { sha256 } from "@noble/hashes/sha256";
2
- import { bytesToHex } from "@noble/hashes/utils";
1
+ import { sha256 } from "@noble/hashes/sha2.js";
2
+ import { bytesToHex, utf8ToBytes } from "@noble/hashes/utils.js";
3
3
  import { canonical } from "./canonical.js";
4
- /** Canonical SHA-256 hex digest of a manifest (agent identity). */
4
+ /** Canonical SHA-256 hex digest of an agent or workflow definition document. */
5
5
  export function hashManifest(manifest) {
6
- return bytesToHex(sha256(canonical(manifest)));
6
+ return bytesToHex(sha256(utf8ToBytes(canonical(manifest))));
7
7
  }