@nylorun/core 0.4.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 +24 -0
- package/dist/compatibility.d.ts +48 -1
- package/dist/compatibility.js +192 -1
- package/dist/contracts.d.ts +536 -19
- package/dist/contracts.js +399 -29
- package/dist/define.d.ts +4 -0
- package/dist/define.js +2 -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/observe.d.ts +64 -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,62 @@
|
|
|
1
|
+
import { canonical } from "../../utils/canonical.js";
|
|
2
|
+
import { diagnostic, fail } from "./diagnostics.js";
|
|
3
|
+
export function emptyAccumulator() {
|
|
4
|
+
return { agents: {}, nodes: {}, sandboxSpecs: [] };
|
|
5
|
+
}
|
|
6
|
+
export function mergeChild(acc, child) {
|
|
7
|
+
Object.assign(acc.agents, child.agents);
|
|
8
|
+
Object.assign(acc.nodes, child.nodes);
|
|
9
|
+
for (const spec of child.sandboxSpecs)
|
|
10
|
+
acc.sandboxSpecs.push(spec);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Every agent in the tree that declares a sandbox must declare an identical spec
|
|
14
|
+
* (workflows.md §8). Returns the common spec, or undefined when none declare one.
|
|
15
|
+
*/
|
|
16
|
+
export function resolveWorkflowSandbox(specs) {
|
|
17
|
+
if (specs.length === 0)
|
|
18
|
+
return { diagnostics: [] };
|
|
19
|
+
const keys = new Set(specs.map((s) => canonical(s)));
|
|
20
|
+
if (keys.size > 1)
|
|
21
|
+
return {
|
|
22
|
+
diagnostics: [
|
|
23
|
+
diagnostic("workflow.sandbox-mismatch", "Every agent in the tree that declares a sandbox must declare an identical spec"),
|
|
24
|
+
],
|
|
25
|
+
};
|
|
26
|
+
return { sandbox: specs[0], diagnostics: [] };
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Assemble a BuiltWorkflow: non-enumerable getBinding, manifest-only toJSON,
|
|
30
|
+
* optional document-level sandbox (workflows.md §18).
|
|
31
|
+
*/
|
|
32
|
+
export function finishWorkflow(options) {
|
|
33
|
+
const diagnostics = [...(options.diagnostics ?? [])];
|
|
34
|
+
const { sandbox, diagnostics: sandboxDiagnostics } = resolveWorkflowSandbox(options.acc.sandboxSpecs);
|
|
35
|
+
diagnostics.push(...sandboxDiagnostics);
|
|
36
|
+
if (diagnostics.length)
|
|
37
|
+
fail(diagnostics);
|
|
38
|
+
const manifest = {
|
|
39
|
+
kind: "workflow",
|
|
40
|
+
workflowSchemaVersion: 1,
|
|
41
|
+
id: options.id,
|
|
42
|
+
root: options.root,
|
|
43
|
+
...(sandbox === undefined ? {} : { sandbox: sandbox }),
|
|
44
|
+
};
|
|
45
|
+
const binding = Object.freeze({
|
|
46
|
+
manifest,
|
|
47
|
+
nodes: Object.freeze({ ...options.acc.nodes }),
|
|
48
|
+
agents: Object.freeze({ ...options.acc.agents }),
|
|
49
|
+
});
|
|
50
|
+
const built = {
|
|
51
|
+
id: options.id,
|
|
52
|
+
manifest,
|
|
53
|
+
toJSON: () => manifest,
|
|
54
|
+
...(options.inputSchema === undefined ? {} : { inputSchema: options.inputSchema }),
|
|
55
|
+
...(options.outputSchema === undefined ? {} : { outputSchema: options.outputSchema }),
|
|
56
|
+
};
|
|
57
|
+
Object.defineProperty(built, "getBinding", {
|
|
58
|
+
value: () => binding,
|
|
59
|
+
enumerable: false,
|
|
60
|
+
});
|
|
61
|
+
return built;
|
|
62
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
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, Slot, SlotInputArgs } from "./slot.js";
|
|
5
|
+
import type { BuiltWorkflow, OutputOf } from "./types.js";
|
|
6
|
+
export type ChainStep = ChildRef;
|
|
7
|
+
type StepId<S> = S extends Slot<infer R, any, any, any> ? S extends {
|
|
8
|
+
readonly id: infer I extends string;
|
|
9
|
+
} ? I : RunnableId<R> : RunnableId<S>;
|
|
10
|
+
type RunnableId<R> = R extends {
|
|
11
|
+
readonly id: infer I extends string;
|
|
12
|
+
} ? I : R extends {
|
|
13
|
+
readonly name: infer N extends string;
|
|
14
|
+
} ? N : string;
|
|
15
|
+
type StepOutput<S> = S extends Slot<infer R, any, any, any> ? OutputOf<R> : OutputOf<S>;
|
|
16
|
+
/** Results object available to step K (earlier step outputs only). */
|
|
17
|
+
export type ResultsBefore<Steps extends readonly ChainStep[], K extends number> = {
|
|
18
|
+
readonly [I in keyof Steps as I extends `${infer N extends number}` ? N extends K ? never : `${N}` extends `${infer M extends number}` ? M extends number ? M extends K ? never : Compare<M, K> extends true ? StepId<Steps[M] & ChainStep> : never : never : never : never]: StepOutput<Steps[I & keyof Steps] & ChainStep>;
|
|
19
|
+
};
|
|
20
|
+
/** `true` when A < B (both number literals). Simplified: use a recursive counter for small tuples. */
|
|
21
|
+
type Compare<A extends number, B extends number> = A extends B ? false : LessThan<[...TupleOf<A>], [...TupleOf<B>]>;
|
|
22
|
+
type TupleOf<N extends number, Acc extends unknown[] = []> = Acc["length"] extends N ? Acc : TupleOf<N, [...Acc, unknown]>;
|
|
23
|
+
type LessThan<A extends unknown[], B extends unknown[]> = A["length"] extends B["length"] ? false : A extends [...B, ...unknown[]] ? false : B extends [...A, ...unknown[]] ? true : false;
|
|
24
|
+
/** Typed results accumulating through a steps tuple. */
|
|
25
|
+
export type ChainResults<Steps extends readonly ChainStep[]> = {
|
|
26
|
+
readonly [I in keyof Steps as StepId<Steps[I & keyof Steps] & ChainStep>]: StepOutput<Steps[I & keyof Steps] & ChainStep>;
|
|
27
|
+
};
|
|
28
|
+
export type LastOutput<Steps extends readonly ChainStep[]> = Steps extends readonly [
|
|
29
|
+
...infer _Rest,
|
|
30
|
+
infer Last
|
|
31
|
+
] ? StepOutput<Last & ChainStep> : JsonValue;
|
|
32
|
+
export interface ChainOptions<Steps extends readonly [ChainStep, ...ChainStep[]] = readonly [ChainStep, ...ChainStep[]], In = JsonValue, Out = LastOutput<Steps>> {
|
|
33
|
+
readonly id: string;
|
|
34
|
+
readonly steps: Steps;
|
|
35
|
+
readonly inputSchema?: ToolSchemaSource;
|
|
36
|
+
readonly outputSchema?: ToolSchemaSource;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Run steps in order. Each step's output is the next step's input unless a slot
|
|
40
|
+
* reshapes it. `results` exposes earlier step outputs to later slots (chain.md).
|
|
41
|
+
*/
|
|
42
|
+
export declare function Chain<const Steps extends readonly [ChainStep, ...ChainStep[]], In = JsonValue>(options: ChainOptions<Steps, In>): BuiltWorkflow<In, LastOutput<Steps>>;
|
|
43
|
+
/** Helper type: a slot whose `input` may only read known earlier result keys. */
|
|
44
|
+
export type ChainSlot<Run extends WorkflowRunnable, PrevResults extends Readonly<Record<string, JsonValue>>, Value = JsonValue, Input = JsonValue> = {
|
|
45
|
+
readonly run: Run;
|
|
46
|
+
readonly id?: string;
|
|
47
|
+
readonly input?: (args: SlotInputArgs<Value, Input, PrevResults>) => JsonValue;
|
|
48
|
+
};
|
|
49
|
+
export {};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { assertValidPathPart, diagnostic, fail, siblingUniquenessDiagnostics, } from "./diagnostics.js";
|
|
2
|
+
import { emptyAccumulator, finishWorkflow, mergeChild } from "./build.js";
|
|
3
|
+
import { remapNodeKeys, resolveChild, runnableId, } from "./runnable.js";
|
|
4
|
+
import { isSlot } from "./slot.js";
|
|
5
|
+
/**
|
|
6
|
+
* Run steps in order. Each step's output is the next step's input unless a slot
|
|
7
|
+
* reshapes it. `results` exposes earlier step outputs to later slots (chain.md).
|
|
8
|
+
*/
|
|
9
|
+
export function Chain(options) {
|
|
10
|
+
const { id, steps } = options;
|
|
11
|
+
if (!id)
|
|
12
|
+
fail([diagnostic("chain.missing-id", "Chain requires id")]);
|
|
13
|
+
assertValidPathPart(id, "Chain id");
|
|
14
|
+
if (!steps || steps.length === 0)
|
|
15
|
+
fail([diagnostic("chain.empty-steps", "Chain steps must be non-empty")]);
|
|
16
|
+
const ids = steps.map((step) => runnableId(step));
|
|
17
|
+
const dupes = siblingUniquenessDiagnostics(ids, "Chain step");
|
|
18
|
+
if (dupes.length)
|
|
19
|
+
fail(dupes);
|
|
20
|
+
const acc = emptyAccumulator();
|
|
21
|
+
const stepNodes = [];
|
|
22
|
+
for (let i = 0; i < steps.length; i++) {
|
|
23
|
+
const step = steps[i];
|
|
24
|
+
const resolved = resolveChild(step);
|
|
25
|
+
assertValidPathPart(resolved.id, "Chain step id");
|
|
26
|
+
const stepPath = `${id}/${resolved.id}`;
|
|
27
|
+
mergeChild(acc, {
|
|
28
|
+
agents: resolved.agents,
|
|
29
|
+
nodes: remapNodeKeys(resolved.nodes, resolved.id, stepPath),
|
|
30
|
+
sandboxSpecs: resolved.sandboxSpecs,
|
|
31
|
+
});
|
|
32
|
+
stepNodes.push(resolved.node);
|
|
33
|
+
}
|
|
34
|
+
return finishWorkflow({
|
|
35
|
+
id,
|
|
36
|
+
root: { chain: { id, steps: stepNodes } },
|
|
37
|
+
acc,
|
|
38
|
+
inputSchema: options.inputSchema,
|
|
39
|
+
outputSchema: options.outputSchema,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { BuildDiagnostic } from "../../types/shared.js";
|
|
2
|
+
/** Thrown when a workflow primitive fails its build-time checks. */
|
|
3
|
+
export declare class WorkflowBuildError extends Error {
|
|
4
|
+
readonly diagnostics: readonly BuildDiagnostic[];
|
|
5
|
+
constructor(diagnostics: readonly BuildDiagnostic[]);
|
|
6
|
+
}
|
|
7
|
+
export declare function diagnostic(code: string, message: string, extra?: Partial<BuildDiagnostic>): BuildDiagnostic;
|
|
8
|
+
export declare function fail(diagnostics: readonly BuildDiagnostic[]): never;
|
|
9
|
+
export declare function failOne(code: string, message: string, extra?: Partial<BuildDiagnostic>): never;
|
|
10
|
+
/** Path parts: non-empty, with no `/`, `[` or `]`. */
|
|
11
|
+
export declare function isValidPathPart(part: string): boolean;
|
|
12
|
+
export declare function assertValidPathPart(part: string, label: string): void;
|
|
13
|
+
/** Record duplicate sibling path parts; returns diagnostics (does not throw). */
|
|
14
|
+
export declare function siblingUniquenessDiagnostics(parts: readonly string[], label: string): BuildDiagnostic[];
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/** Thrown when a workflow primitive fails its build-time checks. */
|
|
2
|
+
export class WorkflowBuildError extends Error {
|
|
3
|
+
diagnostics;
|
|
4
|
+
constructor(diagnostics) {
|
|
5
|
+
super(diagnostics.map((item) => item.message).join("; ") || "Workflow build failed");
|
|
6
|
+
this.diagnostics = diagnostics;
|
|
7
|
+
this.name = "WorkflowBuildError";
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export function diagnostic(code, message, extra = {}) {
|
|
11
|
+
return Object.freeze({ code, message, ...extra });
|
|
12
|
+
}
|
|
13
|
+
export function fail(diagnostics) {
|
|
14
|
+
throw new WorkflowBuildError(Object.freeze([...diagnostics]));
|
|
15
|
+
}
|
|
16
|
+
export function failOne(code, message, extra) {
|
|
17
|
+
fail([diagnostic(code, message, extra)]);
|
|
18
|
+
}
|
|
19
|
+
/** Path parts: non-empty, with no `/`, `[` or `]`. */
|
|
20
|
+
export function isValidPathPart(part) {
|
|
21
|
+
return part.length > 0 && !/[\/\[\]]/.test(part);
|
|
22
|
+
}
|
|
23
|
+
export function assertValidPathPart(part, label) {
|
|
24
|
+
if (!isValidPathPart(part))
|
|
25
|
+
failOne("workflow.invalid-path-part", `${label} '${part}' must be a non-empty path part with no '/', '[' or ']'`);
|
|
26
|
+
}
|
|
27
|
+
/** Record duplicate sibling path parts; returns diagnostics (does not throw). */
|
|
28
|
+
export function siblingUniquenessDiagnostics(parts, label) {
|
|
29
|
+
const seen = new Map();
|
|
30
|
+
const found = [];
|
|
31
|
+
for (const part of parts) {
|
|
32
|
+
const count = (seen.get(part) ?? 0) + 1;
|
|
33
|
+
seen.set(part, count);
|
|
34
|
+
if (count === 2)
|
|
35
|
+
found.push(diagnostic("workflow.duplicate-sibling", `Duplicate ${label} id '${part}'; path parts must be unique among siblings`, { details: { id: part } }));
|
|
36
|
+
}
|
|
37
|
+
return found;
|
|
38
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export { Loop } from "./loop.js";
|
|
2
|
+
export type { LoopOptions, LoopRunnable, LoopVerify, LoopVerifyFn, LoopDecideFn, LoopVerifyArgs, LoopDecideArgs, LoopDecision, } from "./loop.js";
|
|
3
|
+
export { Chain } from "./chain.js";
|
|
4
|
+
export type { ChainOptions, ChainStep, ChainResults, ChainSlot, LastOutput, } from "./chain.js";
|
|
5
|
+
export { Switch } from "./switch.js";
|
|
6
|
+
export type { SwitchOptions, SwitchCases, SwitchOn, SwitchOutput, } from "./switch.js";
|
|
7
|
+
export { Parallel } from "./parallel.js";
|
|
8
|
+
export type { ParallelOptions, ParallelBranches, ParallelOutput } from "./parallel.js";
|
|
9
|
+
export { Map } from "./map.js";
|
|
10
|
+
export type { MapOptions, MapOver } from "./map.js";
|
|
11
|
+
export { isSlot } from "./slot.js";
|
|
12
|
+
export type { Slot, SlotInputArgs, ChildRef } from "./slot.js";
|
|
13
|
+
export { Verdict, VerdictSchema, isVerdict } from "./verdict.js";
|
|
14
|
+
export { withInstructions, withoutTools } from "./patch.js";
|
|
15
|
+
export { WorkflowBuildError, isValidPathPart } from "./diagnostics.js";
|
|
16
|
+
export { isBuiltWorkflow } from "./types.js";
|
|
17
|
+
export type { BuiltWorkflow, OutputOf, InputOf } from "./types.js";
|
|
18
|
+
export type { WorkflowRunnable } from "./runnable.js";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { Loop } from "./loop.js";
|
|
2
|
+
export { Chain } from "./chain.js";
|
|
3
|
+
export { Switch } from "./switch.js";
|
|
4
|
+
export { Parallel } from "./parallel.js";
|
|
5
|
+
export { Map } from "./map.js";
|
|
6
|
+
export { isSlot } from "./slot.js";
|
|
7
|
+
export { Verdict, VerdictSchema, isVerdict } from "./verdict.js";
|
|
8
|
+
export { withInstructions, withoutTools } from "./patch.js";
|
|
9
|
+
export { WorkflowBuildError, isValidPathPart } from "./diagnostics.js";
|
|
10
|
+
export { isBuiltWorkflow } from "./types.js";
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { AgentTool, BuiltAgent } from "../../types/agent.js";
|
|
2
|
+
import type { JsonValue } from "../../types/shared.js";
|
|
3
|
+
import type { Verdict } from "../../types/workflow.js";
|
|
4
|
+
import type { ToolSchemaSource } from "../../types/tool.js";
|
|
5
|
+
import { type WorkflowRunnable } from "./runnable.js";
|
|
6
|
+
import { type ChildRef, type Slot } from "./slot.js";
|
|
7
|
+
import type { BuiltWorkflow } from "./types.js";
|
|
8
|
+
/** Arguments passed to a Loop verify function. */
|
|
9
|
+
export type LoopVerifyArgs = {
|
|
10
|
+
readonly input: JsonValue;
|
|
11
|
+
readonly output: JsonValue;
|
|
12
|
+
readonly iteration: number;
|
|
13
|
+
};
|
|
14
|
+
/** Arguments passed to a Loop decide function. */
|
|
15
|
+
export type LoopDecideArgs = {
|
|
16
|
+
readonly agent?: import("../../types/manifest.js").AgentManifest;
|
|
17
|
+
readonly output: JsonValue;
|
|
18
|
+
readonly verdict: Verdict;
|
|
19
|
+
readonly iteration: number;
|
|
20
|
+
readonly input: JsonValue;
|
|
21
|
+
readonly history: readonly {
|
|
22
|
+
readonly iteration: number;
|
|
23
|
+
readonly output: JsonValue;
|
|
24
|
+
readonly verdict: Verdict;
|
|
25
|
+
}[];
|
|
26
|
+
};
|
|
27
|
+
export type LoopDecision = {
|
|
28
|
+
readonly input: JsonValue;
|
|
29
|
+
readonly agent?: import("../../types/manifest.js").AgentManifest;
|
|
30
|
+
} | {
|
|
31
|
+
readonly output: JsonValue;
|
|
32
|
+
};
|
|
33
|
+
export type LoopVerifyFn = (args: LoopVerifyArgs, ctx?: unknown) => Verdict | Promise<Verdict>;
|
|
34
|
+
export type LoopDecideFn = (args: LoopDecideArgs) => LoopDecision;
|
|
35
|
+
/** @deprecated Prefer WorkflowRunnable — tracer alias. */
|
|
36
|
+
export type LoopRunnable = WorkflowRunnable;
|
|
37
|
+
export type LoopVerify = LoopVerifyFn | BuiltAgent | AgentTool | {
|
|
38
|
+
build(): BuiltAgent;
|
|
39
|
+
} | Slot<BuiltAgent | AgentTool | {
|
|
40
|
+
build(): BuiltAgent;
|
|
41
|
+
}>;
|
|
42
|
+
export interface LoopOptions<In = JsonValue, Out = JsonValue> {
|
|
43
|
+
readonly id: string;
|
|
44
|
+
readonly run: ChildRef;
|
|
45
|
+
readonly verify: LoopVerify;
|
|
46
|
+
readonly decide: LoopDecideFn;
|
|
47
|
+
readonly inputSchema?: ToolSchemaSource;
|
|
48
|
+
readonly outputSchema?: ToolSchemaSource;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Build a Loop workflow: run → verify → decide, repeating until decide returns output.
|
|
52
|
+
* `run` accepts any runnable or slot; `verify` is a function or verifier agent (loops.md).
|
|
53
|
+
*/
|
|
54
|
+
export declare function Loop<In = JsonValue, Out = JsonValue>(options: LoopOptions<In, Out>): BuiltWorkflow<In, Out>;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { normalizeSchema } from "../schema.js";
|
|
2
|
+
import { assertValidPathPart, diagnostic, fail, } from "./diagnostics.js";
|
|
3
|
+
import { emptyAccumulator, finishWorkflow, mergeChild } from "./build.js";
|
|
4
|
+
import { remapNodeKeys, resolveChild, } from "./runnable.js";
|
|
5
|
+
import { isSlot } from "./slot.js";
|
|
6
|
+
function isVerifyFn(value) {
|
|
7
|
+
return typeof value === "function";
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Build-time check: a verifier agent's outputSchema must accept Verdict (loops.md §5).
|
|
11
|
+
*/
|
|
12
|
+
function assertVerifierAgent(agent) {
|
|
13
|
+
const schema = agent.getBinding().outputSchema;
|
|
14
|
+
if (!schema) {
|
|
15
|
+
fail([
|
|
16
|
+
diagnostic("loop.verify-schema", `Verifier agent '${agent.id}' must declare an outputSchema that extends Verdict`),
|
|
17
|
+
]);
|
|
18
|
+
}
|
|
19
|
+
const normalized = normalizeSchema(schema, "output");
|
|
20
|
+
const pass = normalized.validate({ pass: true });
|
|
21
|
+
const failVerdict = normalized.validate({ pass: false, feedback: "x" });
|
|
22
|
+
if (!pass.ok || !failVerdict.ok) {
|
|
23
|
+
fail([
|
|
24
|
+
diagnostic("loop.verify-schema", `Verifier agent '${agent.id}' outputSchema must accept Verdict ({ pass: true } | { pass: false, feedback })`),
|
|
25
|
+
]);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Build a Loop workflow: run → verify → decide, repeating until decide returns output.
|
|
30
|
+
* `run` accepts any runnable or slot; `verify` is a function or verifier agent (loops.md).
|
|
31
|
+
*/
|
|
32
|
+
export function Loop(options) {
|
|
33
|
+
const { id, verify, decide } = options;
|
|
34
|
+
if (!id)
|
|
35
|
+
fail([diagnostic("loop.missing-id", "Loop requires id")]);
|
|
36
|
+
assertValidPathPart(id, "Loop id");
|
|
37
|
+
if (!options.run)
|
|
38
|
+
fail([diagnostic("loop.missing-run", "Loop requires run")]);
|
|
39
|
+
if (!verify)
|
|
40
|
+
fail([diagnostic("loop.missing-verify", "Loop requires verify")]);
|
|
41
|
+
if (!decide || typeof decide !== "function")
|
|
42
|
+
fail([diagnostic("loop.missing-decide", "Loop requires decide")]);
|
|
43
|
+
const acc = emptyAccumulator();
|
|
44
|
+
const runResolved = resolveChild(options.run);
|
|
45
|
+
const runPath = `${id}/${runResolved.id}`;
|
|
46
|
+
mergeChild(acc, {
|
|
47
|
+
agents: runResolved.agents,
|
|
48
|
+
nodes: remapNodeKeys(runResolved.nodes, runResolved.id, runPath),
|
|
49
|
+
sandboxSpecs: runResolved.sandboxSpecs,
|
|
50
|
+
});
|
|
51
|
+
let verifyField;
|
|
52
|
+
if (isVerifyFn(verify)) {
|
|
53
|
+
verifyField = { fn: true };
|
|
54
|
+
acc.nodes[id] = { kind: "verify", fn: verify };
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
const verifyChild = isSlot(verify)
|
|
58
|
+
? resolveChild(verify)
|
|
59
|
+
: resolveChild(verify);
|
|
60
|
+
const agentEntry = Object.values(verifyChild.agents)[0];
|
|
61
|
+
if (!agentEntry) {
|
|
62
|
+
fail([
|
|
63
|
+
diagnostic("loop.invalid-verify", "Loop.verify must be a verify function or a verifier agent"),
|
|
64
|
+
]);
|
|
65
|
+
}
|
|
66
|
+
assertVerifierAgent({
|
|
67
|
+
id: agentEntry.manifest.id,
|
|
68
|
+
getBinding: () => agentEntry,
|
|
69
|
+
});
|
|
70
|
+
// Verifier agent sessions are derived separately; local fn keys (slot input) sit under verify.
|
|
71
|
+
mergeChild(acc, {
|
|
72
|
+
agents: verifyChild.agents,
|
|
73
|
+
nodes: remapNodeKeys(verifyChild.nodes, verifyChild.id, `${id}/verify`),
|
|
74
|
+
sandboxSpecs: verifyChild.sandboxSpecs,
|
|
75
|
+
});
|
|
76
|
+
const node = verifyChild.node;
|
|
77
|
+
if (!("agent" in node) && !("slot" in node)) {
|
|
78
|
+
fail([
|
|
79
|
+
diagnostic("loop.invalid-verify", "Loop.verify must be a verify function or a verifier agent"),
|
|
80
|
+
]);
|
|
81
|
+
}
|
|
82
|
+
verifyField = node;
|
|
83
|
+
}
|
|
84
|
+
acc.nodes[`${id}/decide`] = {
|
|
85
|
+
kind: "fn",
|
|
86
|
+
fn: decide,
|
|
87
|
+
};
|
|
88
|
+
return finishWorkflow({
|
|
89
|
+
id,
|
|
90
|
+
root: {
|
|
91
|
+
loop: {
|
|
92
|
+
id,
|
|
93
|
+
run: runResolved.node,
|
|
94
|
+
verify: verifyField,
|
|
95
|
+
decide: { fn: true },
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
acc,
|
|
99
|
+
inputSchema: options.inputSchema,
|
|
100
|
+
outputSchema: options.outputSchema,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { JsonValue } from "../../types/shared.js";
|
|
2
|
+
import type { ToolSchemaSource } from "../../types/tool.js";
|
|
3
|
+
import type { ChildRef } from "./slot.js";
|
|
4
|
+
import type { BuiltWorkflow, OutputOf } from "./types.js";
|
|
5
|
+
export type MapOver<In, Item> = (input: In) => readonly Item[];
|
|
6
|
+
export interface MapOptions<Each extends ChildRef = ChildRef, In = JsonValue, Item = JsonValue> {
|
|
7
|
+
readonly id: string;
|
|
8
|
+
readonly over: MapOver<In, Item>;
|
|
9
|
+
readonly each: Each;
|
|
10
|
+
readonly inputSchema?: ToolSchemaSource;
|
|
11
|
+
readonly outputSchema?: ToolSchemaSource;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Run `each` once per item from pure `over(input)` (map.md).
|
|
15
|
+
* Output is an array of each item's output, in item order. Empty list → [].
|
|
16
|
+
*/
|
|
17
|
+
export declare function Map<Each extends ChildRef, In = JsonValue, Item = JsonValue>(options: MapOptions<Each, In, Item>): BuiltWorkflow<In, OutputOf<Each>[]>;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { assertValidPathPart, diagnostic, fail, } from "./diagnostics.js";
|
|
2
|
+
import { emptyAccumulator, finishWorkflow, mergeChild } from "./build.js";
|
|
3
|
+
import { remapNodeKeys, resolveChild, runnableId } from "./runnable.js";
|
|
4
|
+
/**
|
|
5
|
+
* Run `each` once per item from pure `over(input)` (map.md).
|
|
6
|
+
* Output is an array of each item's output, in item order. Empty list → [].
|
|
7
|
+
*/
|
|
8
|
+
export function Map(options) {
|
|
9
|
+
const { id, over, each } = options;
|
|
10
|
+
if (!id)
|
|
11
|
+
fail([diagnostic("map.missing-id", "Map requires id")]);
|
|
12
|
+
assertValidPathPart(id, "Map id");
|
|
13
|
+
if (typeof over !== "function")
|
|
14
|
+
fail([diagnostic("map.missing-over", "Map requires over")]);
|
|
15
|
+
if (each === undefined || each === null)
|
|
16
|
+
fail([diagnostic("map.missing-each", "Map requires each")]);
|
|
17
|
+
const resolved = resolveChild(each);
|
|
18
|
+
assertValidPathPart(resolved.id, "Map each id");
|
|
19
|
+
const acc = emptyAccumulator();
|
|
20
|
+
// Node key drops Map indices: implement/code, not implement[i]/code.
|
|
21
|
+
const eachPath = `${id}/${resolved.id}`;
|
|
22
|
+
mergeChild(acc, {
|
|
23
|
+
agents: resolved.agents,
|
|
24
|
+
nodes: remapNodeKeys(resolved.nodes, resolved.id, eachPath),
|
|
25
|
+
sandboxSpecs: resolved.sandboxSpecs,
|
|
26
|
+
});
|
|
27
|
+
acc.nodes[`${id}/over`] = {
|
|
28
|
+
kind: "fn",
|
|
29
|
+
fn: over,
|
|
30
|
+
};
|
|
31
|
+
// Preserve each child's id for path construction (map.md §3.2).
|
|
32
|
+
void runnableId(each);
|
|
33
|
+
return finishWorkflow({
|
|
34
|
+
id,
|
|
35
|
+
root: {
|
|
36
|
+
map: {
|
|
37
|
+
id,
|
|
38
|
+
over: { fn: true },
|
|
39
|
+
each: resolved.node,
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
acc,
|
|
43
|
+
inputSchema: options.inputSchema,
|
|
44
|
+
outputSchema: options.outputSchema,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { JsonValue } from "../../types/shared.js";
|
|
2
|
+
import type { ToolSchemaSource } from "../../types/tool.js";
|
|
3
|
+
import type { ChildRef } from "./slot.js";
|
|
4
|
+
import type { BuiltWorkflow, OutputOf } from "./types.js";
|
|
5
|
+
export type ParallelBranches = Readonly<Record<string, ChildRef>>;
|
|
6
|
+
export type ParallelOutput<Branches extends ParallelBranches> = {
|
|
7
|
+
readonly [K in keyof Branches]: OutputOf<Branches[K]>;
|
|
8
|
+
};
|
|
9
|
+
export interface ParallelOptions<Branches extends ParallelBranches = ParallelBranches, In = JsonValue> {
|
|
10
|
+
readonly id: string;
|
|
11
|
+
readonly branches: Branches;
|
|
12
|
+
readonly inputSchema?: ToolSchemaSource;
|
|
13
|
+
readonly outputSchema?: ToolSchemaSource;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Run a fixed set of named branches concurrently on the same input (parallel.md).
|
|
17
|
+
* Output is an object keyed by branch name in declaration order.
|
|
18
|
+
*/
|
|
19
|
+
export declare function Parallel<const Branches extends ParallelBranches, In = JsonValue>(options: ParallelOptions<Branches, In>): BuiltWorkflow<In, ParallelOutput<Branches>>;
|
|
@@ -0,0 +1,37 @@
|
|
|
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
|
+
* Run a fixed set of named branches concurrently on the same input (parallel.md).
|
|
6
|
+
* Output is an object keyed by branch name in declaration order.
|
|
7
|
+
*/
|
|
8
|
+
export function Parallel(options) {
|
|
9
|
+
const { id, branches } = options;
|
|
10
|
+
if (!id)
|
|
11
|
+
fail([diagnostic("parallel.missing-id", "Parallel requires id")]);
|
|
12
|
+
assertValidPathPart(id, "Parallel id");
|
|
13
|
+
if (!branches || Object.keys(branches).length === 0)
|
|
14
|
+
fail([
|
|
15
|
+
diagnostic("parallel.empty-branches", "Parallel branches must have at least one entry"),
|
|
16
|
+
]);
|
|
17
|
+
const acc = emptyAccumulator();
|
|
18
|
+
const branchNodes = {};
|
|
19
|
+
for (const name of Object.keys(branches)) {
|
|
20
|
+
assertValidPathPart(name, "Parallel branch name");
|
|
21
|
+
const resolved = resolveChild(branches[name]);
|
|
22
|
+
const branchPath = `${id}/${name}`;
|
|
23
|
+
mergeChild(acc, {
|
|
24
|
+
agents: resolved.agents,
|
|
25
|
+
nodes: remapNodeKeys(resolved.nodes, resolved.id, branchPath),
|
|
26
|
+
sandboxSpecs: resolved.sandboxSpecs,
|
|
27
|
+
});
|
|
28
|
+
branchNodes[name] = resolved.node;
|
|
29
|
+
}
|
|
30
|
+
return finishWorkflow({
|
|
31
|
+
id,
|
|
32
|
+
root: { parallel: { id, branches: branchNodes } },
|
|
33
|
+
acc,
|
|
34
|
+
inputSchema: options.inputSchema,
|
|
35
|
+
outputSchema: options.outputSchema,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { AgentManifest } from "../../types/manifest.js";
|
|
2
|
+
/**
|
|
3
|
+
* Append instruction text to the agent's primary capability (loops.md §3.3).
|
|
4
|
+
* Pure: returns a new manifest; never mutates.
|
|
5
|
+
*/
|
|
6
|
+
export declare function withInstructions(manifest: AgentManifest, text: string): AgentManifest;
|
|
7
|
+
/**
|
|
8
|
+
* Remove named tools from every capability (loops.md §3.3). Pure.
|
|
9
|
+
* Tools may be removed, not added — matches Loop variant rules.
|
|
10
|
+
*/
|
|
11
|
+
export declare function withoutTools(manifest: AgentManifest, names: readonly string[]): AgentManifest;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { deepFreeze } from "../../utils/immutable.js";
|
|
2
|
+
function primaryCapabilityIndex(manifest) {
|
|
3
|
+
const agentIdx = manifest.capabilities.findIndex((c) => c.id === "agent");
|
|
4
|
+
return agentIdx >= 0 ? agentIdx : 0;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Append instruction text to the agent's primary capability (loops.md §3.3).
|
|
8
|
+
* Pure: returns a new manifest; never mutates.
|
|
9
|
+
*/
|
|
10
|
+
export function withInstructions(manifest, text) {
|
|
11
|
+
if (typeof text !== "string" || text.length === 0)
|
|
12
|
+
throw new TypeError("withInstructions requires a non-empty string");
|
|
13
|
+
if (manifest.capabilities.length === 0)
|
|
14
|
+
throw new TypeError("withInstructions requires a manifest with at least one capability");
|
|
15
|
+
const index = primaryCapabilityIndex(manifest);
|
|
16
|
+
const capabilities = manifest.capabilities.map((cap, i) => {
|
|
17
|
+
if (i !== index)
|
|
18
|
+
return cap;
|
|
19
|
+
return {
|
|
20
|
+
...cap,
|
|
21
|
+
instructions: Object.freeze([...(cap.instructions ?? []), text]),
|
|
22
|
+
};
|
|
23
|
+
});
|
|
24
|
+
return deepFreeze({ ...manifest, capabilities });
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Remove named tools from every capability (loops.md §3.3). Pure.
|
|
28
|
+
* Tools may be removed, not added — matches Loop variant rules.
|
|
29
|
+
*/
|
|
30
|
+
export function withoutTools(manifest, names) {
|
|
31
|
+
const remove = new Set(names);
|
|
32
|
+
const capabilities = manifest.capabilities.map((cap) => {
|
|
33
|
+
if (!cap.tools?.length)
|
|
34
|
+
return cap;
|
|
35
|
+
const tools = cap.tools.filter((tool) => !remove.has(tool.name));
|
|
36
|
+
if (tools.length === cap.tools.length)
|
|
37
|
+
return cap;
|
|
38
|
+
if (tools.length === 0) {
|
|
39
|
+
const { tools: _dropped, ...rest } = cap;
|
|
40
|
+
return rest;
|
|
41
|
+
}
|
|
42
|
+
return { ...cap, tools: Object.freeze(tools) };
|
|
43
|
+
});
|
|
44
|
+
return deepFreeze({ ...manifest, capabilities });
|
|
45
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { AgentTool, BuiltAgent } from "../../types/agent.js";
|
|
2
|
+
import type { JsonObject } from "../../types/shared.js";
|
|
3
|
+
import type { ToolDefinition } from "../../types/tool.js";
|
|
4
|
+
import type { WorkflowNode } from "../../types/workflow.js";
|
|
5
|
+
import type { AgentBinding } from "../binding.js";
|
|
6
|
+
import type { BoundToolDefinition } from "../bound.js";
|
|
7
|
+
import { type BuiltWorkflow } from "./types.js";
|
|
8
|
+
import { type ChildRef } from "./slot.js";
|
|
9
|
+
/** Anything a primitive accepts as a child (or a slot wrapping one). */
|
|
10
|
+
export type WorkflowRunnable = BuiltAgent | AgentTool | {
|
|
11
|
+
build(): BuiltAgent;
|
|
12
|
+
} | ToolDefinition<any, any, any> | BuiltWorkflow;
|
|
13
|
+
export type ResolvedChild = {
|
|
14
|
+
readonly id: string;
|
|
15
|
+
readonly node: WorkflowNode;
|
|
16
|
+
readonly agents: Record<string, AgentBinding>;
|
|
17
|
+
/** Nested workflow implementations, keyed relative to the child root id. */
|
|
18
|
+
readonly nodes: Record<string, import("../../types/workflow.js").WorkflowNodeImplementation>;
|
|
19
|
+
readonly tool?: BoundToolDefinition;
|
|
20
|
+
readonly sandboxSpecs: readonly JsonObject[];
|
|
21
|
+
readonly isReshapingSlot: boolean;
|
|
22
|
+
};
|
|
23
|
+
/** Collect sandbox specs declared on an agent (and its delegates, one level). */
|
|
24
|
+
export declare function sandboxSpecsOf(agent: BuiltAgent | AgentTool): JsonObject[];
|
|
25
|
+
/**
|
|
26
|
+
* Resolve a runnable or slot into a manifest node plus local bindings.
|
|
27
|
+
* When the slot has `input`, the node is wrapped as `{ slot: { id?, input, run } }`.
|
|
28
|
+
*/
|
|
29
|
+
export declare function resolveChild(child: ChildRef, defaultRename?: string): ResolvedChild;
|
|
30
|
+
/**
|
|
31
|
+
* Rewrite node keys that are rooted at `fromRoot` so they sit at `toPath`.
|
|
32
|
+
* Used when nesting a child under a parent path (workflows.md §6).
|
|
33
|
+
*
|
|
34
|
+
* Example: Loop nodes `{ code, code/decide }` nested at `ship/code` become
|
|
35
|
+
* `{ ship/code, ship/code/decide }`.
|
|
36
|
+
*/
|
|
37
|
+
export declare function remapNodeKeys(nodes: Record<string, import("../../types/workflow.js").WorkflowNodeImplementation>, fromRoot: string, toPath: string): Record<string, import("../../types/workflow.js").WorkflowNodeImplementation>;
|
|
38
|
+
/** @deprecated Prefer remapNodeKeys — kept for call-site clarity during migration. */
|
|
39
|
+
export declare function prefixNodeKeys(nodes: Record<string, import("../../types/workflow.js").WorkflowNodeImplementation>, parentPath: string): Record<string, import("../../types/workflow.js").WorkflowNodeImplementation>;
|
|
40
|
+
export declare function runnableId(child: ChildRef): string;
|