@nylorun/core 0.3.0-beta → 0.5.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.
- package/CHANGELOG.md +32 -0
- package/dist/compatibility.d.ts +48 -1
- package/dist/compatibility.js +192 -1
- package/dist/contracts.d.ts +562 -19
- package/dist/contracts.js +442 -29
- package/dist/define.d.ts +9 -3
- package/dist/define.js +3 -0
- package/dist/definition/assemble.js +35 -0
- package/dist/definition/bind-tool.js +3 -0
- package/dist/definition/bound.d.ts +3 -0
- package/dist/definition/builder.d.ts +5 -5
- package/dist/definition/declaration.d.ts +2 -2
- package/dist/definition/declaration.js +13 -1
- package/dist/definition/delegate.d.ts +35 -0
- package/dist/definition/delegate.js +72 -0
- package/dist/definition/from.js +15 -2
- package/dist/definition/helpers.d.ts +1 -1
- package/dist/definition/helpers.js +8 -1
- package/dist/definition/manifest.js +3 -0
- package/dist/definition/variant.d.ts +7 -0
- package/dist/definition/variant.js +87 -0
- package/dist/definition/workflow/build.d.ts +38 -0
- package/dist/definition/workflow/build.js +62 -0
- package/dist/definition/workflow/chain.d.ts +49 -0
- package/dist/definition/workflow/chain.js +41 -0
- package/dist/definition/workflow/diagnostics.d.ts +14 -0
- package/dist/definition/workflow/diagnostics.js +38 -0
- package/dist/definition/workflow/index.d.ts +18 -0
- package/dist/definition/workflow/index.js +10 -0
- package/dist/definition/workflow/loop.d.ts +54 -0
- package/dist/definition/workflow/loop.js +102 -0
- package/dist/definition/workflow/map.d.ts +17 -0
- package/dist/definition/workflow/map.js +46 -0
- package/dist/definition/workflow/parallel.d.ts +19 -0
- package/dist/definition/workflow/parallel.js +37 -0
- package/dist/definition/workflow/patch.d.ts +11 -0
- package/dist/definition/workflow/patch.js +45 -0
- package/dist/definition/workflow/runnable.d.ts +40 -0
- package/dist/definition/workflow/runnable.js +251 -0
- package/dist/definition/workflow/slot.d.ts +23 -0
- package/dist/definition/workflow/slot.js +16 -0
- package/dist/definition/workflow/switch.d.ts +26 -0
- package/dist/definition/workflow/switch.js +66 -0
- package/dist/definition/workflow/types.d.ts +25 -0
- package/dist/definition/workflow/types.js +7 -0
- package/dist/definition/workflow/verdict.d.ts +20 -0
- package/dist/definition/workflow/verdict.js +25 -0
- package/dist/index.d.ts +1 -0
- package/dist/types/agent.d.ts +5 -0
- package/dist/types/manifest.d.ts +5 -0
- package/dist/types/middleware.d.ts +5 -0
- package/dist/types/observe.d.ts +87 -1
- package/dist/types/tool.d.ts +11 -0
- package/dist/types/workflow.d.ts +106 -0
- package/dist/types/workflow.js +1 -0
- package/dist/utils/hash.d.ts +3 -2
- package/dist/utils/hash.js +1 -1
- package/package.json +1 -1
|
@@ -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,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
package/dist/types/agent.d.ts
CHANGED
|
@@ -8,3 +8,8 @@ export interface BuiltAgent<Info = unknown, Output = string> {
|
|
|
8
8
|
toJSON(): AgentManifest;
|
|
9
9
|
getBinding(): AgentBinding;
|
|
10
10
|
}
|
|
11
|
+
/**
|
|
12
|
+
* An agent placed in another agent's `tools`: a built agent or an `Agent(...)` builder.
|
|
13
|
+
* It becomes a tool named after the agent's id that takes `{ task: string }`.
|
|
14
|
+
*/
|
|
15
|
+
export type AgentTool = Pick<BuiltAgent<any, any>, "id" | "manifest" | "getBinding">;
|
package/dist/types/manifest.d.ts
CHANGED
|
@@ -7,6 +7,11 @@ export interface ToolManifest {
|
|
|
7
7
|
readonly description?: string;
|
|
8
8
|
readonly inputSchema: JsonObject;
|
|
9
9
|
readonly outputSchema?: JsonObject;
|
|
10
|
+
/**
|
|
11
|
+
* Present when this tool is another agent. The engine runs it with a fresh context and
|
|
12
|
+
* returns its final output; its input is always `{ task: string }`. One level deep.
|
|
13
|
+
*/
|
|
14
|
+
readonly agent?: AgentManifest;
|
|
10
15
|
}
|
|
11
16
|
export interface SkillManifest {
|
|
12
17
|
readonly name: string;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ContextMutationOptions, ModelCandidate, ModelDirective, ModelToolCall, ModelConfigurationMutationOptions } from "./model.js";
|
|
2
2
|
import type { InputEvent, TranscriptEntry } from "./transcript.js";
|
|
3
|
+
import type { AgentTool } from "./agent.js";
|
|
3
4
|
import type { McpServerManifest, SandboxManifest, SkillManifest } from "./manifest.js";
|
|
4
5
|
/** Skill files held on the developer binding. Not a manifest field. */
|
|
5
6
|
export interface SkillRecord {
|
|
@@ -85,6 +86,10 @@ export interface CapabilityDeclaration<Info = unknown> {
|
|
|
85
86
|
/** Run after each model call (step) or after the turn's final answer. */
|
|
86
87
|
readonly after?: AfterHooks<Info>;
|
|
87
88
|
}
|
|
89
|
+
/** What `.use()` accepts: a capability whose `tools` may also hold agents used as tools. */
|
|
90
|
+
export type CapabilityInput<Info = unknown> = Omit<CapabilityDeclaration<Info>, "tools"> & {
|
|
91
|
+
readonly tools?: CapabilityItems<ToolDefinition<any, Info, any> | AgentTool>;
|
|
92
|
+
};
|
|
88
93
|
export interface MiddlewareContributions {
|
|
89
94
|
readonly instructions?: readonly string[];
|
|
90
95
|
readonly tools?: readonly {
|
package/dist/types/observe.d.ts
CHANGED
|
@@ -1,7 +1,71 @@
|
|
|
1
1
|
import type { ContextSnapshot, ModelCandidate, ModelCall, ModelConfigurationSnapshot } from "./model.js";
|
|
2
2
|
import type { JsonObject, JsonValue } from "./shared.js";
|
|
3
3
|
import type { InputEvent, TranscriptEntry } from "./transcript.js";
|
|
4
|
-
import type { RequiredInteraction, ToolDescriptor, ToolResult } from "./tool.js";
|
|
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"> {
|
|
@@ -144,6 +208,28 @@ export type ObserveEvent = {
|
|
|
144
208
|
readonly attributes: {
|
|
145
209
|
readonly token?: JsonValue;
|
|
146
210
|
};
|
|
211
|
+
} | {
|
|
212
|
+
readonly type: "delegation.started";
|
|
213
|
+
readonly turnId: string;
|
|
214
|
+
readonly stepId: string;
|
|
215
|
+
readonly callId: string;
|
|
216
|
+
readonly invocationId: string;
|
|
217
|
+
readonly toolName: string;
|
|
218
|
+
readonly agent: AgentRef;
|
|
219
|
+
readonly attributes: {
|
|
220
|
+
readonly task: string;
|
|
221
|
+
};
|
|
222
|
+
} | {
|
|
223
|
+
readonly type: "delegation.completed";
|
|
224
|
+
readonly turnId: string;
|
|
225
|
+
readonly stepId: string;
|
|
226
|
+
readonly callId: string;
|
|
227
|
+
readonly invocationId: string;
|
|
228
|
+
readonly toolName: string;
|
|
229
|
+
readonly agent: AgentRef;
|
|
230
|
+
/** The agent's own end state: completed, failed, cancelled or paused. */
|
|
231
|
+
readonly status: string;
|
|
232
|
+
readonly attributes: ToolResult;
|
|
147
233
|
} | {
|
|
148
234
|
readonly type: "turn.completed";
|
|
149
235
|
readonly turnId: string;
|
package/dist/types/tool.d.ts
CHANGED
|
@@ -102,6 +102,15 @@ export interface ToolExecutionResume {
|
|
|
102
102
|
readonly value?: JsonValue;
|
|
103
103
|
readonly token?: JsonValue;
|
|
104
104
|
}
|
|
105
|
+
/**
|
|
106
|
+
* The agent a call belongs to. `path` is `"<root>"` or `"<root>/<child>"`; `delegationId`
|
|
107
|
+
* identifies one run of an agent used as a tool and is absent for the root agent.
|
|
108
|
+
*/
|
|
109
|
+
export interface AgentRef {
|
|
110
|
+
readonly id: string;
|
|
111
|
+
readonly path: string;
|
|
112
|
+
readonly delegationId?: string;
|
|
113
|
+
}
|
|
105
114
|
interface ToolExecutionContextBase {
|
|
106
115
|
readonly executionId: string;
|
|
107
116
|
readonly turnId: string;
|
|
@@ -122,6 +131,8 @@ interface ToolExecutionContextBase {
|
|
|
122
131
|
readonly session: {
|
|
123
132
|
readonly id: string;
|
|
124
133
|
};
|
|
134
|
+
/** The agent this call belongs to: the root agent, or an agent used as a tool. */
|
|
135
|
+
readonly agent?: AgentRef;
|
|
125
136
|
/** Live progress stub — emits an observation when a listener is present. */
|
|
126
137
|
progress(message: string, data?: JsonObject): void;
|
|
127
138
|
/** Durable wait: park for a human/app response. */
|