@nylorun/core 0.13.0-beta → 0.16.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 +131 -0
- package/dist/compatibility.d.ts +68 -25
- package/dist/compatibility.js +67 -27
- package/dist/contracts.d.ts +325 -959
- package/dist/contracts.js +281 -516
- package/dist/define.d.ts +13 -10
- package/dist/define.js +7 -4
- package/dist/definition/assemble.d.ts +0 -3
- package/dist/definition/assemble.js +13 -19
- package/dist/definition/bind-agent.js +0 -2
- package/dist/definition/bind-tool.js +0 -1
- package/dist/definition/bound.d.ts +3 -10
- package/dist/definition/builder.d.ts +19 -55
- package/dist/definition/builder.js +19 -130
- package/dist/definition/capability.d.ts +3 -15
- package/dist/definition/capability.js +3 -25
- package/dist/definition/code-tools.d.ts +22 -0
- package/dist/definition/code-tools.js +46 -0
- package/dist/definition/declaration.d.ts +0 -3
- package/dist/definition/declaration.js +8 -11
- package/dist/definition/delegate.d.ts +7 -7
- package/dist/definition/flow/compile.d.ts +4 -5
- package/dist/definition/flow/compile.js +120 -72
- package/dist/definition/flow/from.d.ts +2 -2
- package/dist/definition/flow/from.js +14 -33
- package/dist/definition/flow/index.d.ts +2 -2
- package/dist/definition/flow/index.js +2 -2
- package/dist/definition/flow/paths.d.ts +32 -17
- package/dist/definition/flow/paths.js +35 -14
- package/dist/definition/flow/spec.d.ts +6 -9
- package/dist/definition/flow/spec.js +49 -31
- package/dist/definition/flow/types.d.ts +40 -116
- package/dist/definition/from.js +36 -41
- package/dist/definition/helpers.d.ts +1 -1
- package/dist/definition/helpers.js +1 -1
- package/dist/definition/http-tool.d.ts +66 -0
- package/dist/definition/http-tool.js +131 -0
- package/dist/definition/implementations.d.ts +0 -3
- package/dist/definition/manifest.js +5 -4
- package/dist/definition/mcp.d.ts +4 -2
- package/dist/definition/mcp.js +14 -18
- package/dist/definition/removed.d.ts +10 -0
- package/dist/definition/removed.js +26 -0
- package/dist/definition/sandbox-capability.d.ts +2 -0
- package/dist/definition/sandbox-capability.js +11 -1
- package/dist/definition/schema.js +0 -3
- package/dist/definition/skill-tools.d.ts +16 -2
- package/dist/definition/skill-tools.js +38 -31
- package/dist/definition/variant.d.ts +1 -1
- package/dist/definition/variant.js +3 -14
- package/dist/definition/workflow/diagnostics.d.ts +1 -7
- package/dist/definition/workflow/diagnostics.js +1 -24
- package/dist/definition/workflow/index.d.ts +3 -17
- package/dist/definition/workflow/index.js +2 -9
- package/dist/definition/workflow/runnable.d.ts +2 -34
- package/dist/definition/workflow/runnable.js +8 -142
- package/dist/definition/workflow/types.d.ts +1 -10
- package/dist/harness-api/messages.d.ts +24 -27
- package/dist/harness-api/messages.js +1 -1
- package/dist/harness-api/schema.d.ts +0 -16
- package/dist/harness-api/schema.js +5 -12
- package/dist/index.d.ts +1 -1
- package/dist/types/manifest.d.ts +41 -14
- package/dist/types/middleware.d.ts +11 -16
- package/dist/types/observe.d.ts +0 -6
- package/dist/types/tool.d.ts +0 -6
- package/dist/types/workflow.d.ts +54 -110
- package/dist/utils/definition-files.d.ts +31 -0
- package/dist/utils/definition-files.js +84 -0
- package/package.json +1 -1
- package/dist/definition/hooks.d.ts +0 -19
- package/dist/definition/hooks.js +0 -67
- package/dist/definition/workflow/build.d.ts +0 -28
- package/dist/definition/workflow/build.js +0 -40
- package/dist/definition/workflow/chain.d.ts +0 -49
- package/dist/definition/workflow/chain.js +0 -40
- package/dist/definition/workflow/loop.d.ts +0 -54
- package/dist/definition/workflow/loop.js +0 -100
- package/dist/definition/workflow/map.d.ts +0 -17
- package/dist/definition/workflow/map.js +0 -45
- package/dist/definition/workflow/parallel.d.ts +0 -19
- package/dist/definition/workflow/parallel.js +0 -36
- package/dist/definition/workflow/patch.d.ts +0 -11
- package/dist/definition/workflow/patch.js +0 -45
- package/dist/definition/workflow/slot.d.ts +0 -23
- package/dist/definition/workflow/slot.js +0 -16
- package/dist/definition/workflow/switch.d.ts +0 -26
- package/dist/definition/workflow/switch.js +0 -64
- package/dist/types/dynamics.d.ts +0 -101
- package/dist/types/dynamics.js +0 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Rebuild a flow agent from its workflow manifest
|
|
2
|
+
* Rebuild a flow agent from its workflow manifest v3 plus the tools its tool nodes name,
|
|
3
3
|
* the way `Agent.from` rebuilds a ReAct agent from its manifest and implementations.
|
|
4
4
|
*/
|
|
5
5
|
import { HarnessError } from "../../errors.js";
|
|
@@ -7,39 +7,30 @@ import { WorkflowManifestSchema } from "../../contracts.js";
|
|
|
7
7
|
import { agentFrom } from "../from.js";
|
|
8
8
|
import { deepFreeze } from "../../utils/immutable.js";
|
|
9
9
|
import { bindToolNode } from "../workflow/runnable.js";
|
|
10
|
-
import { forEachFlowNode,
|
|
10
|
+
import { forEachFlowNode, isWorkflowManifest } from "./paths.js";
|
|
11
11
|
export function flowFrom(json, implementations = {}) {
|
|
12
12
|
const manifest = deepFreeze(JSON.parse(JSON.stringify(WorkflowManifestSchema.parse(json))));
|
|
13
|
-
if (!isWorkflowManifestV2(manifest))
|
|
14
|
-
throw new HarnessError("agent.build-failed", "Agent.from reads workflow manifest v2; rebuild a v1 workflow with Chain, Switch, Parallel, Map or Loop");
|
|
15
13
|
const needs = [];
|
|
16
14
|
const agents = {};
|
|
17
15
|
collect(manifest, "", needs, agents);
|
|
18
16
|
const nodes = {};
|
|
19
17
|
const given = implementations.nodes ?? {};
|
|
20
18
|
const missing = [];
|
|
21
|
-
for (const
|
|
22
|
-
const impl = given[
|
|
19
|
+
for (const key of needs) {
|
|
20
|
+
const impl = given[key];
|
|
23
21
|
if (impl === undefined) {
|
|
24
|
-
missing.push(
|
|
22
|
+
missing.push(key);
|
|
25
23
|
continue;
|
|
26
24
|
}
|
|
27
|
-
if (
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
nodes[need.key] = { kind: "tool", tool: bindToolNode(impl) };
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
if (typeof impl !== "function")
|
|
34
|
-
throw new HarnessError("agent.build-failed", `'${need.key}' is a flow function: pass a function`);
|
|
35
|
-
nodes[need.key] = { kind: need.kind, fn: impl };
|
|
36
|
-
}
|
|
25
|
+
if (typeof impl === "function")
|
|
26
|
+
throw new HarnessError("agent.build-failed", `'${key}' is a tool node: pass a tool, not a function`);
|
|
27
|
+
nodes[key] = { kind: "tool", tool: bindToolNode(impl) };
|
|
37
28
|
}
|
|
38
29
|
if (missing.length)
|
|
39
30
|
throw new HarnessError("agent.build-failed", `Missing flow implementation${missing.length === 1 ? "" : "s"}: ${missing.join(", ")}`);
|
|
40
|
-
const unknown = Object.keys(given).filter((key) => !needs.
|
|
31
|
+
const unknown = Object.keys(given).filter((key) => !needs.includes(key));
|
|
41
32
|
if (unknown.length)
|
|
42
|
-
throw new HarnessError("agent.build-failed", `Flow agent '${manifest.id}' has no
|
|
33
|
+
throw new HarnessError("agent.build-failed", `Flow agent '${manifest.id}' has no tool node${unknown.length === 1 ? "" : "s"} ${unknown.join(", ")}`);
|
|
43
34
|
const bindings = {};
|
|
44
35
|
for (const [id, agent] of Object.entries(agents))
|
|
45
36
|
bindings[id] = agentFrom(agent, implementations.agents?.[id] ?? {}).getBinding();
|
|
@@ -52,24 +43,14 @@ export function flowFrom(json, implementations = {}) {
|
|
|
52
43
|
Object.defineProperty(built, "getBinding", { value: () => binding, enumerable: false });
|
|
53
44
|
return built;
|
|
54
45
|
}
|
|
55
|
-
/** Every stage key the
|
|
46
|
+
/** Every tool node's stage key but an HTTP stage's (the Runtime runs it), and every embedded ReAct agent. */
|
|
56
47
|
function collect(flow, prefix, needs, agents) {
|
|
57
48
|
forEachFlowNode(flow.root, ({ node, key }) => {
|
|
58
|
-
if (node.
|
|
59
|
-
needs.push(
|
|
60
|
-
if ("tool" in node)
|
|
61
|
-
needs.push({ key, kind: "tool" });
|
|
62
|
-
if ("switch" in node)
|
|
63
|
-
needs.push({ key: functionKey(key, "on"), kind: "fn" });
|
|
64
|
-
if ("loop" in node) {
|
|
65
|
-
if ("fn" in node.loop.verify)
|
|
66
|
-
needs.push({ key: functionKey(key, "verify"), kind: "verify" });
|
|
67
|
-
if (node.loop.decide)
|
|
68
|
-
needs.push({ key: functionKey(key, "decide"), kind: "fn" });
|
|
69
|
-
}
|
|
49
|
+
if ("tool" in node && !node.tool.http)
|
|
50
|
+
needs.push(key);
|
|
70
51
|
if ("agent" in node) {
|
|
71
52
|
const target = flow.agents[node.agent];
|
|
72
|
-
if (
|
|
53
|
+
if (isWorkflowManifest(target))
|
|
73
54
|
collect(target, key, needs, agents);
|
|
74
55
|
else
|
|
75
56
|
agents[node.agent] = target;
|
|
@@ -2,10 +2,10 @@ import type { Flow } from "./types.js";
|
|
|
2
2
|
export { FlowBuilder, isFlowBuilder } from "./spec.js";
|
|
3
3
|
export { compileAgentFlow } from "./compile.js";
|
|
4
4
|
export type { FlowImplementations } from "./from.js";
|
|
5
|
-
export { ROOT_POSITION, childPosition, embeddedAgent,
|
|
5
|
+
export { ROOT_POSITION, childPosition, embeddedAgent, flowHttpTarget, forEachFlowNode, indexSuffix, isLeafNode, isWorkflowManifest, leafPart, leafPath, stageKey, stripIndices, type FlowHttpTarget, type FlowNodeVisit, type FlowVisitNode, } from "./paths.js";
|
|
6
6
|
export type * from "./types.js";
|
|
7
7
|
/**
|
|
8
8
|
* A sequence with no id, for a switch case, parallel branch, map item or loop body
|
|
9
|
-
* that is more than one step: `flow().
|
|
9
|
+
* that is more than one step: `flow().pipe(planner).map(implementer)`.
|
|
10
10
|
*/
|
|
11
11
|
export declare function flow<In = any>(): Flow<In>;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { FlowBuilder } from "./spec.js";
|
|
2
2
|
export { FlowBuilder, isFlowBuilder } from "./spec.js";
|
|
3
3
|
export { compileAgentFlow } from "./compile.js";
|
|
4
|
-
export { ROOT_POSITION, childPosition, embeddedAgent,
|
|
4
|
+
export { ROOT_POSITION, childPosition, embeddedAgent, flowHttpTarget, forEachFlowNode, indexSuffix, isLeafNode, isWorkflowManifest, leafPart, leafPath, stageKey, stripIndices, } from "./paths.js";
|
|
5
5
|
/**
|
|
6
6
|
* A sequence with no id, for a switch case, parallel branch, map item or loop body
|
|
7
|
-
* that is more than one step: `flow().
|
|
7
|
+
* that is more than one step: `flow().pipe(planner).map(implementer)`.
|
|
8
8
|
*/
|
|
9
9
|
export function flow() {
|
|
10
10
|
return new FlowBuilder();
|
|
@@ -1,43 +1,45 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Paths and keys for workflow manifest
|
|
2
|
+
* Paths and keys for workflow manifest v3 (flow agents). Shared by the compiler, the
|
|
3
3
|
* flow engine, the executor and Studio, so every package names a node the same way.
|
|
4
4
|
*
|
|
5
5
|
* - A **leaf path** names an agent or tool session: the leaf's id (its `id`, or the
|
|
6
6
|
* agent's id or tool name), with `[i]` for each Map item it runs in, under the ids of
|
|
7
7
|
* any nested flow agents. Control stages and `flow()` add nothing, so wrapping a step
|
|
8
8
|
* in a Loop or moving it out of a Switch keeps its session.
|
|
9
|
-
* - A **stage key** names a node
|
|
10
|
-
*
|
|
11
|
-
*
|
|
9
|
+
* - A **stage key** names a node: a leaf's id, a control stage's `id`, or else its
|
|
10
|
+
* position from the flow root (`@1.default.1`). A tool node's code is bound under its key, and
|
|
11
|
+
* the Runtime finds an HTTP stage or an HTTP verifier by it.
|
|
12
12
|
*/
|
|
13
|
-
import type {
|
|
14
|
-
import type { AgentManifest } from "../../types/manifest.js";
|
|
13
|
+
import type { WorkflowAgentNode, WorkflowHttpVerify, WorkflowManifest, WorkflowNode, WorkflowToolNode } from "../../types/workflow.js";
|
|
14
|
+
import type { AgentManifest, HttpToolTarget } from "../../types/manifest.js";
|
|
15
|
+
import type { JsonObject } from "../../types/shared.js";
|
|
16
|
+
/** A node the walk visits: a stage, or a Loop's HTTP verifier. */
|
|
17
|
+
export type FlowVisitNode = WorkflowNode | WorkflowHttpVerify;
|
|
15
18
|
/** Position of a flow's root node. */
|
|
16
19
|
export declare const ROOT_POSITION = "@";
|
|
17
|
-
|
|
18
|
-
export declare function
|
|
20
|
+
/** True for a workflow manifest v3: a flow agent, or a nested one among `agents`. */
|
|
21
|
+
export declare function isWorkflowManifest(manifest: {
|
|
19
22
|
readonly kind?: unknown;
|
|
20
23
|
readonly workflowSchemaVersion?: unknown;
|
|
21
|
-
} | undefined): manifest is
|
|
22
|
-
export declare function isLeafNode(node:
|
|
24
|
+
} | undefined): manifest is WorkflowManifest;
|
|
25
|
+
export declare function isLeafNode(node: WorkflowNode): node is WorkflowAgentNode | WorkflowToolNode;
|
|
23
26
|
/** A leaf's path part: its `id`, else the agent id or tool name. Undefined for control nodes. */
|
|
24
|
-
export declare function leafPart(node:
|
|
27
|
+
export declare function leafPart(node: FlowVisitNode): string | undefined;
|
|
25
28
|
/** The position of a child: `@` + index for the root's children, else `parent.segment`. */
|
|
26
29
|
export declare function childPosition(parent: string, segment: string | number): string;
|
|
27
30
|
/**
|
|
28
31
|
* A node's stage key: a leaf's path part, a control node's `id`, or its position.
|
|
29
32
|
* `prefix` is the key of the nested flow agent the node runs in, if any.
|
|
30
33
|
*/
|
|
31
|
-
export declare function stageKey(node:
|
|
32
|
-
export declare function functionKey(key: string, role: FlowFunctionRole): string;
|
|
34
|
+
export declare function stageKey(node: FlowVisitNode, position: string, prefix?: string): string;
|
|
33
35
|
/** `[0][2]` for Map item indices, outermost first. */
|
|
34
36
|
export declare function indexSuffix(indices: readonly number[]): string;
|
|
35
37
|
/** A leaf's session path: `prefix/part[i]…`. */
|
|
36
38
|
export declare function leafPath(prefix: string, part: string, indices?: readonly number[]): string;
|
|
37
|
-
/** Drop Map indices from a path: the key a
|
|
39
|
+
/** Drop Map indices from a path: the key a tool node's code is bound under. */
|
|
38
40
|
export declare function stripIndices(path: string): string;
|
|
39
41
|
export interface FlowNodeVisit {
|
|
40
|
-
readonly node:
|
|
42
|
+
readonly node: FlowVisitNode;
|
|
41
43
|
readonly position: string;
|
|
42
44
|
/** Stage key, including the nested flow agent prefix. */
|
|
43
45
|
readonly key: string;
|
|
@@ -50,11 +52,24 @@ export interface FlowNodeVisit {
|
|
|
50
52
|
* Visit every node of one flow, parents first. Nested flow agents are not entered:
|
|
51
53
|
* their nodes live in their own manifest under `agents`.
|
|
52
54
|
*/
|
|
53
|
-
export declare function forEachFlowNode(root:
|
|
55
|
+
export declare function forEachFlowNode(root: WorkflowNode, visit: (entry: FlowNodeVisit) => void, options?: {
|
|
54
56
|
readonly prefix?: string;
|
|
55
57
|
}): void;
|
|
58
|
+
/** What the Runtime needs of an HTTP stage or an HTTP verifier. */
|
|
59
|
+
export interface FlowHttpTarget {
|
|
60
|
+
readonly http: HttpToolTarget;
|
|
61
|
+
/** An HTTP stage's output schema; a verifier's answer is checked as a verdict instead. */
|
|
62
|
+
readonly outputSchema?: JsonObject;
|
|
63
|
+
/** True for a Loop's HTTP verifier. */
|
|
64
|
+
readonly verify: boolean;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* The HTTP stage or HTTP verifier of `manifest` at stage key `key`, looking through nested
|
|
68
|
+
* flow agents (`outer/inner`), or undefined.
|
|
69
|
+
*/
|
|
70
|
+
export declare function flowHttpTarget(manifest: WorkflowManifest, key: string): FlowHttpTarget | undefined;
|
|
56
71
|
/**
|
|
57
72
|
* The embedded agent an agent node names, looked up through the nested flow agents
|
|
58
73
|
* (`flow`, outermost first) the node runs in.
|
|
59
74
|
*/
|
|
60
|
-
export declare function embeddedAgent(manifest:
|
|
75
|
+
export declare function embeddedAgent(manifest: WorkflowManifest, flow: readonly string[], agentId: string): AgentManifest | WorkflowManifest | undefined;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/** Position of a flow's root node. */
|
|
2
2
|
export const ROOT_POSITION = "@";
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
/** True for a workflow manifest v3: a flow agent, or a nested one among `agents`. */
|
|
4
|
+
export function isWorkflowManifest(manifest) {
|
|
5
|
+
return manifest?.kind === "workflow" && manifest.workflowSchemaVersion === 3;
|
|
5
6
|
}
|
|
6
7
|
export function isLeafNode(node) {
|
|
7
8
|
return "agent" in node || "tool" in node;
|
|
@@ -23,12 +24,9 @@ export function childPosition(parent, segment) {
|
|
|
23
24
|
* `prefix` is the key of the nested flow agent the node runs in, if any.
|
|
24
25
|
*/
|
|
25
26
|
export function stageKey(node, position, prefix = "") {
|
|
26
|
-
const own = leafPart(node) ?? node.id ?? position;
|
|
27
|
+
const own = leafPart(node) ?? ("id" in node ? node.id : undefined) ?? position;
|
|
27
28
|
return prefix ? `${prefix}/${own}` : own;
|
|
28
29
|
}
|
|
29
|
-
export function functionKey(key, role) {
|
|
30
|
-
return `${key}:${role}`;
|
|
31
|
-
}
|
|
32
30
|
/** `[0][2]` for Map item indices, outermost first. */
|
|
33
31
|
export function indexSuffix(indices) {
|
|
34
32
|
return indices.map((index) => `[${index}]`).join("");
|
|
@@ -38,7 +36,7 @@ export function leafPath(prefix, part, indices = []) {
|
|
|
38
36
|
const own = `${part}${indexSuffix(indices)}`;
|
|
39
37
|
return prefix ? `${prefix}/${own}` : own;
|
|
40
38
|
}
|
|
41
|
-
/** Drop Map indices from a path: the key a
|
|
39
|
+
/** Drop Map indices from a path: the key a tool node's code is bound under. */
|
|
42
40
|
export function stripIndices(path) {
|
|
43
41
|
return path.replace(/\[\d+]/g, "");
|
|
44
42
|
}
|
|
@@ -66,12 +64,37 @@ export function forEachFlowNode(root, visit, options = {}) {
|
|
|
66
64
|
walk(node.map.each, childPosition(position, "each"), "each", true);
|
|
67
65
|
else if ("loop" in node) {
|
|
68
66
|
walk(node.loop.run, childPosition(position, "run"), "run", inMap);
|
|
69
|
-
|
|
70
|
-
walk(node.loop.verify, childPosition(position, "verify"), "verify", inMap);
|
|
67
|
+
walk(node.loop.verify, childPosition(position, "verify"), "verify", inMap);
|
|
71
68
|
}
|
|
72
69
|
};
|
|
73
70
|
walk(root, ROOT_POSITION, "root", false);
|
|
74
71
|
}
|
|
72
|
+
/**
|
|
73
|
+
* The HTTP stage or HTTP verifier of `manifest` at stage key `key`, looking through nested
|
|
74
|
+
* flow agents (`outer/inner`), or undefined.
|
|
75
|
+
*/
|
|
76
|
+
export function flowHttpTarget(manifest, key) {
|
|
77
|
+
let found;
|
|
78
|
+
const search = (flow, prefix) => forEachFlowNode(flow.root, ({ node, key: at, role }) => {
|
|
79
|
+
if (found)
|
|
80
|
+
return;
|
|
81
|
+
if (at === key && "http" in node)
|
|
82
|
+
found = { http: node.http, verify: role === "verify" };
|
|
83
|
+
else if (at === key && "tool" in node && node.tool.http)
|
|
84
|
+
found = {
|
|
85
|
+
http: node.tool.http,
|
|
86
|
+
...(node.tool.outputSchema === undefined ? {} : { outputSchema: node.tool.outputSchema }),
|
|
87
|
+
verify: false,
|
|
88
|
+
};
|
|
89
|
+
else if ("agent" in node && key.startsWith(`${at}/`)) {
|
|
90
|
+
const nested = flow.agents[node.agent];
|
|
91
|
+
if (isWorkflowManifest(nested))
|
|
92
|
+
search(nested, at);
|
|
93
|
+
}
|
|
94
|
+
}, { prefix });
|
|
95
|
+
search(manifest, "");
|
|
96
|
+
return found;
|
|
97
|
+
}
|
|
75
98
|
/**
|
|
76
99
|
* The embedded agent an agent node names, looked up through the nested flow agents
|
|
77
100
|
* (`flow`, outermost first) the node runs in.
|
|
@@ -79,12 +102,10 @@ export function forEachFlowNode(root, visit, options = {}) {
|
|
|
79
102
|
export function embeddedAgent(manifest, flow, agentId) {
|
|
80
103
|
let scope = manifest;
|
|
81
104
|
for (const id of flow) {
|
|
82
|
-
const next =
|
|
83
|
-
|
|
84
|
-
: undefined;
|
|
85
|
-
if (!next || !isWorkflowManifestV2(next))
|
|
105
|
+
const next = scope.agents[id];
|
|
106
|
+
if (!isWorkflowManifest(next))
|
|
86
107
|
return undefined;
|
|
87
108
|
scope = next;
|
|
88
109
|
}
|
|
89
|
-
return
|
|
110
|
+
return scope.agents[agentId];
|
|
90
111
|
}
|
|
@@ -1,45 +1,40 @@
|
|
|
1
|
-
/** A function in a flow stage: `input`, `on`, `verify` or `decide`. */
|
|
2
|
-
export type FlowFn = (args: any) => unknown;
|
|
3
1
|
/** A child given a new step id with `.withId()`. */
|
|
4
2
|
export interface NamedChild {
|
|
5
3
|
readonly [NAMED]: true;
|
|
6
4
|
readonly run: unknown;
|
|
7
5
|
readonly id: string;
|
|
8
6
|
}
|
|
7
|
+
/** One stage of a flow. Stages hold agents, tools and flows: never functions. */
|
|
9
8
|
export type FlowStage = {
|
|
10
9
|
readonly kind: "step";
|
|
11
10
|
readonly child: unknown;
|
|
12
11
|
readonly id?: string;
|
|
13
|
-
readonly input?: FlowFn;
|
|
14
12
|
} | {
|
|
15
13
|
readonly kind: "switch";
|
|
16
14
|
readonly cases: Readonly<Record<string, unknown>>;
|
|
17
|
-
readonly on: FlowFn;
|
|
18
15
|
readonly id?: string;
|
|
19
|
-
readonly input?: FlowFn;
|
|
20
16
|
} | {
|
|
21
17
|
readonly kind: "parallel";
|
|
22
18
|
readonly branches: Readonly<Record<string, unknown>>;
|
|
23
19
|
readonly id?: string;
|
|
24
|
-
readonly input?: FlowFn;
|
|
25
20
|
} | {
|
|
26
21
|
readonly kind: "map";
|
|
27
22
|
readonly each: unknown;
|
|
28
23
|
readonly id?: string;
|
|
29
|
-
readonly input?: FlowFn;
|
|
30
24
|
} | {
|
|
31
25
|
readonly kind: "loop";
|
|
32
26
|
readonly body: unknown;
|
|
33
27
|
readonly verify: unknown;
|
|
34
28
|
readonly max?: number;
|
|
35
|
-
readonly decide?: FlowFn;
|
|
36
29
|
readonly id?: string;
|
|
37
|
-
readonly input?: FlowFn;
|
|
38
30
|
};
|
|
39
31
|
export declare const NAMED: unique symbol;
|
|
40
32
|
export declare function named(run: unknown, id: string): NamedChild;
|
|
41
33
|
export declare function isNamedChild(value: unknown): value is NamedChild;
|
|
42
34
|
type Options = Readonly<Record<string, unknown>> | undefined;
|
|
35
|
+
/** `.pipe(a, b, c)`: one stage per child, in order. */
|
|
36
|
+
export declare function pipeStages(children: readonly unknown[]): FlowStage[];
|
|
37
|
+
/** `.step(child, { id })`: deprecated, the same as `.pipe(child.withId(id))`. */
|
|
43
38
|
export declare function stepStage(child: unknown, options?: Options): FlowStage;
|
|
44
39
|
export declare function switchStage(cases: unknown, options?: Options): FlowStage;
|
|
45
40
|
export declare function parallelStage(branches: unknown, options?: Options): FlowStage;
|
|
@@ -54,6 +49,8 @@ export declare class FlowBuilder {
|
|
|
54
49
|
constructor(stages?: readonly FlowStage[]);
|
|
55
50
|
/** @internal The stages, in order. */
|
|
56
51
|
get stages(): readonly FlowStage[];
|
|
52
|
+
pipe(...children: unknown[]): FlowBuilder;
|
|
53
|
+
/** @deprecated Use `.pipe()`. */
|
|
57
54
|
step(child: unknown, options?: Options): FlowBuilder;
|
|
58
55
|
switch(cases: unknown, options?: Options): FlowBuilder;
|
|
59
56
|
parallel(branches: unknown, options?: Options): FlowBuilder;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { HarnessError } from "../../errors.js";
|
|
2
|
+
import { deprecate } from "../../utils/deprecate.js";
|
|
2
3
|
export const NAMED = Symbol.for("nylorun.flow.named");
|
|
3
4
|
const FLOW = Symbol.for("nylorun.flow");
|
|
4
5
|
export function named(run, id) {
|
|
@@ -10,11 +11,17 @@ export function isNamedChild(value) {
|
|
|
10
11
|
return !!value && typeof value === "object" && value[NAMED] === true;
|
|
11
12
|
}
|
|
12
13
|
const HINTS = {
|
|
13
|
-
over: "A
|
|
14
|
-
from: "
|
|
14
|
+
over: "A map runs over the previous output: an array, or its `items` field.",
|
|
15
|
+
from: "Each stage gets the previous stage's output.",
|
|
15
16
|
as: "Use { id } to name a step.",
|
|
16
17
|
name: "Use { id } to name a stage.",
|
|
17
|
-
default: "Put default among the cases: .switch({ ...cases, default }
|
|
18
|
+
default: "Put default among the cases: .switch({ ...cases, default }).",
|
|
19
|
+
};
|
|
20
|
+
/** Function options that flows no longer take, and what replaces each (see MIGRATION.md). */
|
|
21
|
+
const REMOVED = {
|
|
22
|
+
input: "stages get the previous output; return what the next stage needs from the previous agent's output schema",
|
|
23
|
+
on: "switch reads the previous output: a string or its `route` field",
|
|
24
|
+
decide: "removed; the loop retries with the verifier's feedback until max",
|
|
18
25
|
};
|
|
19
26
|
function checkOptions(method, options, allowed) {
|
|
20
27
|
if (options === undefined)
|
|
@@ -24,70 +31,77 @@ function checkOptions(method, options, allowed) {
|
|
|
24
31
|
for (const key of Object.keys(options)) {
|
|
25
32
|
if (allowed.includes(key))
|
|
26
33
|
continue;
|
|
34
|
+
if (REMOVED[key])
|
|
35
|
+
throw new HarnessError("configuration.invalid", `${method} no longer takes '${key}': ${REMOVED[key]} (see MIGRATION.md).`);
|
|
27
36
|
const hint = HINTS[key] ? ` ${HINTS[key]}` : "";
|
|
28
37
|
throw new HarnessError("configuration.invalid", `Unknown option '${key}' for ${method}. Allowed: ${allowed.join(", ")}.${hint}`);
|
|
29
38
|
}
|
|
30
39
|
if (options.id !== undefined && (typeof options.id !== "string" || options.id.length === 0))
|
|
31
40
|
throw new HarnessError("configuration.invalid", `${method} id must be a non-empty string`);
|
|
32
|
-
if (options.input !== undefined && typeof options.input !== "function")
|
|
33
|
-
throw new HarnessError("configuration.invalid", `${method} input must be a function`);
|
|
34
41
|
}
|
|
35
42
|
function requireChild(method, child) {
|
|
36
43
|
if (child === undefined || child === null)
|
|
37
44
|
throw new HarnessError("configuration.invalid", `${method} requires an agent, tool or flow()`);
|
|
45
|
+
if (typeof child === "function")
|
|
46
|
+
throw new HarnessError("configuration.invalid", `${method} takes an agent, tool or flow(), not a function: flows run no code (see MIGRATION.md)`);
|
|
38
47
|
}
|
|
39
48
|
function requireRecord(method, label, value) {
|
|
40
49
|
if (!value || typeof value !== "object" || Array.isArray(value) || Object.keys(value).length === 0)
|
|
41
50
|
throw new HarnessError("configuration.invalid", `${method} requires a non-empty object of ${label}`);
|
|
51
|
+
for (const [name, child] of Object.entries(value))
|
|
52
|
+
requireChild(`${method} '${name}'`, child);
|
|
42
53
|
return value;
|
|
43
54
|
}
|
|
44
|
-
function
|
|
45
|
-
return {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
55
|
+
function idOf(options) {
|
|
56
|
+
return options?.id === undefined ? {} : { id: options.id };
|
|
57
|
+
}
|
|
58
|
+
/** `.pipe(a, b, c)`: one stage per child, in order. */
|
|
59
|
+
export function pipeStages(children) {
|
|
60
|
+
if (children.length === 0)
|
|
61
|
+
throw new HarnessError("configuration.invalid", ".pipe() requires at least one agent, tool or flow()");
|
|
62
|
+
return children.map((child) => {
|
|
63
|
+
requireChild(".pipe()", child);
|
|
64
|
+
return Object.freeze({ kind: "step", child });
|
|
65
|
+
});
|
|
49
66
|
}
|
|
67
|
+
/** `.step(child, { id })`: deprecated, the same as `.pipe(child.withId(id))`. */
|
|
50
68
|
export function stepStage(child, options) {
|
|
51
|
-
|
|
69
|
+
deprecate("NYLORUN_DEP_STEP", ".step() is deprecated. Use .pipe().");
|
|
70
|
+
checkOptions(".step()", options, ["id"]);
|
|
52
71
|
requireChild(".step()", child);
|
|
53
|
-
return Object.freeze({ kind: "step", child, ...
|
|
72
|
+
return Object.freeze({ kind: "step", child, ...idOf(options) });
|
|
54
73
|
}
|
|
55
74
|
export function switchStage(cases, options) {
|
|
56
|
-
checkOptions(".switch()", options, ["
|
|
75
|
+
checkOptions(".switch()", options, ["id"]);
|
|
57
76
|
const record = requireRecord(".switch()", "cases", cases);
|
|
58
|
-
|
|
59
|
-
throw new HarnessError("configuration.invalid", ".switch() requires { on: ({ input }) => caseName }");
|
|
60
|
-
return Object.freeze({
|
|
61
|
-
kind: "switch",
|
|
62
|
-
cases: Object.freeze({ ...record }),
|
|
63
|
-
on: options.on,
|
|
64
|
-
...common(options),
|
|
65
|
-
});
|
|
77
|
+
return Object.freeze({ kind: "switch", cases: Object.freeze({ ...record }), ...idOf(options) });
|
|
66
78
|
}
|
|
67
79
|
export function parallelStage(branches, options) {
|
|
68
|
-
checkOptions(".parallel()", options, ["id"
|
|
80
|
+
checkOptions(".parallel()", options, ["id"]);
|
|
69
81
|
const record = requireRecord(".parallel()", "branches", branches);
|
|
70
|
-
return Object.freeze({ kind: "parallel", branches: Object.freeze({ ...record }), ...
|
|
82
|
+
return Object.freeze({ kind: "parallel", branches: Object.freeze({ ...record }), ...idOf(options) });
|
|
71
83
|
}
|
|
72
84
|
export function mapStage(each, options) {
|
|
73
|
-
checkOptions(".map()", options, ["id"
|
|
85
|
+
checkOptions(".map()", options, ["id"]);
|
|
74
86
|
requireChild(".map()", each);
|
|
75
|
-
return Object.freeze({ kind: "map", each, ...
|
|
87
|
+
return Object.freeze({ kind: "map", each, ...idOf(options) });
|
|
76
88
|
}
|
|
77
89
|
export function loopStage(body, options) {
|
|
78
|
-
checkOptions(".loop()", options, ["verify", "max", "
|
|
90
|
+
checkOptions(".loop()", options, ["verify", "max", "id"]);
|
|
79
91
|
requireChild(".loop()", body);
|
|
80
92
|
if (options?.verify === undefined)
|
|
81
|
-
throw new HarnessError("configuration.invalid", ".loop() requires { verify }: a
|
|
82
|
-
if (
|
|
83
|
-
throw new HarnessError("configuration.invalid", ".loop()
|
|
93
|
+
throw new HarnessError("configuration.invalid", ".loop() requires { verify }: a verifier agent, or an HTTP verifier http({ url })");
|
|
94
|
+
if (typeof options.verify === "function")
|
|
95
|
+
throw new HarnessError("configuration.invalid", ".loop() no longer takes a verify function: use a verifier agent or an HTTP verifier, http({ url }) (see MIGRATION.md).");
|
|
96
|
+
const verify = options.verify;
|
|
97
|
+
if (verify && typeof verify === "object" && ("fn" in verify || "command" in verify))
|
|
98
|
+
throw new HarnessError("configuration.invalid", ".loop() verify: Functions are not available yet. Use a verifier agent or an HTTP verifier, http({ url }).");
|
|
84
99
|
return Object.freeze({
|
|
85
100
|
kind: "loop",
|
|
86
101
|
body,
|
|
87
102
|
verify: options.verify,
|
|
88
103
|
...(options.max === undefined ? {} : { max: options.max }),
|
|
89
|
-
...(options
|
|
90
|
-
...common(options),
|
|
104
|
+
...idOf(options),
|
|
91
105
|
});
|
|
92
106
|
}
|
|
93
107
|
// ── flow(): a sequence with no id ────────────────────────────────────────────
|
|
@@ -105,6 +119,10 @@ export class FlowBuilder {
|
|
|
105
119
|
get stages() {
|
|
106
120
|
return this.#stages;
|
|
107
121
|
}
|
|
122
|
+
pipe(...children) {
|
|
123
|
+
return new FlowBuilder([...this.#stages, ...pipeStages(children)]);
|
|
124
|
+
}
|
|
125
|
+
/** @deprecated Use `.pipe()`. */
|
|
108
126
|
step(child, options) {
|
|
109
127
|
return new FlowBuilder([...this.#stages, stepStage(child, options)]);
|
|
110
128
|
}
|