@nylorun/core 0.2.0-beta → 0.4.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 +28 -0
- package/dist/contracts.d.ts +194 -14
- package/dist/contracts.js +203 -10
- package/dist/define.d.ts +11 -5
- package/dist/define.js +4 -0
- package/dist/definition/assemble.d.ts +3 -3
- package/dist/definition/assemble.js +51 -2
- package/dist/definition/bind-agent.js +2 -2
- package/dist/definition/bind-tool.js +3 -0
- package/dist/definition/bound.d.ts +8 -3
- package/dist/definition/builder.d.ts +18 -10
- package/dist/definition/builder.js +47 -34
- package/dist/definition/declaration.d.ts +5 -5
- package/dist/definition/declaration.js +21 -9
- package/dist/definition/delegate.d.ts +35 -0
- package/dist/definition/delegate.js +72 -0
- package/dist/definition/from.js +56 -19
- package/dist/definition/helpers.d.ts +8 -6
- package/dist/definition/helpers.js +12 -9
- package/dist/definition/hooks.d.ts +19 -0
- package/dist/definition/hooks.js +67 -0
- package/dist/definition/implementations.d.ts +3 -3
- package/dist/definition/manifest.js +10 -3
- package/dist/definition/sandbox-tools.d.ts +5 -0
- package/dist/definition/sandbox-tools.js +109 -0
- package/dist/types/agent.d.ts +5 -0
- package/dist/types/dynamics.d.ts +62 -14
- package/dist/types/manifest.d.ts +34 -3
- package/dist/types/middleware.d.ts +14 -5
- package/dist/types/observe.d.ts +23 -1
- package/dist/types/tool.d.ts +12 -1
- package/dist/utils/sandbox.d.ts +17 -0
- package/dist/utils/sandbox.js +64 -0
- package/package.json +1 -1
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { bindAgent } from "./bind-agent.js";
|
|
2
2
|
import { createManifest } from "./manifest.js";
|
|
3
3
|
import { createSkillTools } from "./skill-tools.js";
|
|
4
|
+
import { DELEGATE_NAME_PATTERN, delegateOf, delegatesOf } from "./delegate.js";
|
|
5
|
+
import { canonical } from "../utils/canonical.js";
|
|
4
6
|
const diagnostic = (code, message, extra = {}) => Object.freeze({ code, message, ...extra });
|
|
5
7
|
export function assembleAgent(middleware, identity, dynamics = new Map()) {
|
|
6
8
|
const diagnostics = [];
|
|
@@ -36,8 +38,7 @@ export function assembleAgent(middleware, identity, dynamics = new Map()) {
|
|
|
36
38
|
...(item.contributions === undefined
|
|
37
39
|
? {}
|
|
38
40
|
: { contributions: item.contributions }),
|
|
39
|
-
...(item.
|
|
40
|
-
...(item.afterModelCall ? { afterModelCall: true } : {}),
|
|
41
|
+
...(item.hooks === undefined ? {} : { hooks: item.hooks }),
|
|
41
42
|
...(item.manifestType === undefined
|
|
42
43
|
? {}
|
|
43
44
|
: { manifestType: item.manifestType }),
|
|
@@ -45,6 +46,7 @@ export function assembleAgent(middleware, identity, dynamics = new Map()) {
|
|
|
45
46
|
...(item.mcpServers === undefined
|
|
46
47
|
? {}
|
|
47
48
|
: { mcpServers: item.mcpServers }),
|
|
49
|
+
...(item.sandbox === undefined ? {} : { sandbox: item.sandbox }),
|
|
48
50
|
...(item.skills === undefined ? {} : { skills: item.skills }),
|
|
49
51
|
...(item.skillRecords === undefined
|
|
50
52
|
? {}
|
|
@@ -58,6 +60,21 @@ export function assembleAgent(middleware, identity, dynamics = new Map()) {
|
|
|
58
60
|
}
|
|
59
61
|
const skilled = attachSkillTools(frozen);
|
|
60
62
|
diagnostics.push(...skilled.diagnostics);
|
|
63
|
+
// The wire schema rejects tool names shared across capabilities; report them at build time
|
|
64
|
+
// with both owners. Duplicates within one capability keep their registration-time error.
|
|
65
|
+
const toolOwners = new Map();
|
|
66
|
+
for (const item of skilled.items)
|
|
67
|
+
for (const entry of item.tools ?? []) {
|
|
68
|
+
const owner = toolOwners.get(entry.name);
|
|
69
|
+
if (owner !== undefined && owner !== item.id)
|
|
70
|
+
diagnostics.push(diagnostic("tool.duplicate-name", `Tool '${entry.name}' is declared by capabilities '${owner}' and '${item.id}'` +
|
|
71
|
+
(owner === "sandbox" || item.id === "sandbox"
|
|
72
|
+
? "; sandbox() provides built-in bash, read, write, edit, grep and glob tools, so rename yours"
|
|
73
|
+
: "")));
|
|
74
|
+
else
|
|
75
|
+
toolOwners.set(entry.name, item.id);
|
|
76
|
+
}
|
|
77
|
+
diagnostics.push(...delegationDiagnostics(skilled.items));
|
|
61
78
|
if (diagnostics.length)
|
|
62
79
|
return Object.freeze({
|
|
63
80
|
ok: false,
|
|
@@ -76,6 +93,38 @@ export function assembleAgent(middleware, identity, dynamics = new Map()) {
|
|
|
76
93
|
const agent = bindAgent(frozenMiddleware, manifest, identity, dynamics);
|
|
77
94
|
return Object.freeze({ ok: true, agent, manifest });
|
|
78
95
|
}
|
|
96
|
+
/** v1 delegation is one level deep and non-interactive; anything else fails the build by name. */
|
|
97
|
+
function delegationDiagnostics(items) {
|
|
98
|
+
const found = [];
|
|
99
|
+
const sandboxes = new Set();
|
|
100
|
+
for (const item of items)
|
|
101
|
+
if (item.sandbox)
|
|
102
|
+
sandboxes.add(canonical(item.sandbox));
|
|
103
|
+
for (const item of items)
|
|
104
|
+
for (const tool of item.tools ?? []) {
|
|
105
|
+
const delegate = delegateOf(tool);
|
|
106
|
+
if (!delegate)
|
|
107
|
+
continue;
|
|
108
|
+
const child = delegate.manifest;
|
|
109
|
+
const extra = { toolName: tool.name };
|
|
110
|
+
if (!DELEGATE_NAME_PATTERN.test(child.id))
|
|
111
|
+
found.push(diagnostic("delegation.invalid-name", `Agent '${child.id}' is used as a tool, so its id must be 1-64 letters, digits, '_' or '-'`, extra));
|
|
112
|
+
if (!child.description?.trim())
|
|
113
|
+
found.push(diagnostic("delegation.description-required", `Agent '${child.id}' is used as a tool by capability '${item.id}' and needs a description: the parent's model reads it to decide when to delegate`, extra));
|
|
114
|
+
const nested = delegatesOf(child)[0];
|
|
115
|
+
if (nested)
|
|
116
|
+
found.push(diagnostic("delegation.nested", `'${child.id}' delegates to '${nested.manifest.id}'; nested delegation is not supported yet`, extra));
|
|
117
|
+
for (const childTool of delegate.agent?.getBinding().tools ?? [])
|
|
118
|
+
if (childTool.approval)
|
|
119
|
+
found.push(diagnostic("delegation.approval-unsupported", `'${childTool.name}' in '${child.id}' needs approval; agents used as tools can't ask for it yet, so keep it on the parent`, extra));
|
|
120
|
+
for (const capability of child.capabilities)
|
|
121
|
+
if (capability.sandbox)
|
|
122
|
+
sandboxes.add(canonical(capability.sandbox));
|
|
123
|
+
}
|
|
124
|
+
if (sandboxes.size > 1)
|
|
125
|
+
found.push(diagnostic("sandbox.mismatch", "An agent and the agents it uses as tools share one sandbox, so every sandbox they declare must be identical"));
|
|
126
|
+
return found;
|
|
127
|
+
}
|
|
79
128
|
const SKILL_TOOL_NAMES = new Set(["load_skill", "read_skill_resource"]);
|
|
80
129
|
function attachSkillTools(items) {
|
|
81
130
|
const skills = new Map();
|
|
@@ -53,8 +53,8 @@ function buildImplementations(middleware, dynamics, snapshots) {
|
|
|
53
53
|
...(Object.keys(tools).length
|
|
54
54
|
? { tools: Object.freeze({ ...tools }) }
|
|
55
55
|
: {}),
|
|
56
|
-
...(dyn?.
|
|
57
|
-
...(dyn?.
|
|
56
|
+
...(dyn?.before ? { before: Object.freeze({ ...dyn.before }) } : {}),
|
|
57
|
+
...(dyn?.after ? { after: Object.freeze({ ...dyn.after }) } : {}),
|
|
58
58
|
...(dyn?.middleware ? { middleware: dyn.middleware } : {}),
|
|
59
59
|
});
|
|
60
60
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { HarnessError } from "../errors.js";
|
|
2
2
|
import { normalizeToolDefinition, normalizedSchemasFor } from "./schema.js";
|
|
3
|
+
import { delegateOf } from "./delegate.js";
|
|
3
4
|
/** Prepares an executable Tool and records its middleware slot provenance. */
|
|
4
5
|
export function bindTool(item, owner) {
|
|
5
6
|
const prepared = normalizeToolDefinition(item);
|
|
@@ -8,6 +9,7 @@ export function bindTool(item, owner) {
|
|
|
8
9
|
if (typeof prepared.execute !== "function")
|
|
9
10
|
throw new HarnessError("tool.invalid", `Tool '${prepared.name}' must provide execute()`);
|
|
10
11
|
const schemas = normalizedSchemasFor(prepared);
|
|
12
|
+
const delegate = delegateOf(item);
|
|
11
13
|
return Object.freeze({
|
|
12
14
|
// Keep the original object identity so ToolRegistry WeakMap lookups succeed.
|
|
13
15
|
source: item,
|
|
@@ -21,5 +23,6 @@ export function bindTool(item, owner) {
|
|
|
21
23
|
...(prepared.approval === undefined ? {} : { approval: prepared.approval }),
|
|
22
24
|
...(prepared.effects === undefined ? {} : { effects: prepared.effects }),
|
|
23
25
|
owner: Object.freeze({ ...owner }),
|
|
26
|
+
...(delegate === undefined ? {} : { delegate }),
|
|
24
27
|
});
|
|
25
28
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { JsonObject } from "../types/shared.js";
|
|
2
|
-
import type { McpServerManifest, SkillManifest } from "../types/manifest.js";
|
|
2
|
+
import type { McpServerManifest, SandboxManifest, SkillManifest } from "../types/manifest.js";
|
|
3
3
|
import type { SkillRecord } from "../types/middleware.js";
|
|
4
|
+
import type { HookManifest } from "../types/dynamics.js";
|
|
5
|
+
import type { Delegate } from "./delegate.js";
|
|
4
6
|
import type { MiddlewareContributions, StepMiddleware } from "../types/middleware.js";
|
|
5
7
|
import type { ToolApproval, ToolDefinition, ToolEffects, ToolExecutionContext, ToolInputSchema, ToolOutputSchema, ToolOwner, ToolRunResult, ToolSchema } from "../types/tool.js";
|
|
6
8
|
export interface BoundToolDefinition<Info = unknown> extends Omit<ToolDefinition<ToolInputSchema, Info, ToolOutputSchema | undefined>, "inputSchema" | "outputSchema" | "execute" | "run" | "input" | "output"> {
|
|
@@ -11,6 +13,8 @@ export interface BoundToolDefinition<Info = unknown> extends Omit<ToolDefinition
|
|
|
11
13
|
readonly effects?: ToolEffects;
|
|
12
14
|
readonly owner: ToolOwner;
|
|
13
15
|
readonly source: ToolDefinition<any, any, any>;
|
|
16
|
+
/** Set when this tool is an agent used as a tool; the engine runs it. */
|
|
17
|
+
readonly delegate?: Delegate;
|
|
14
18
|
}
|
|
15
19
|
export interface BoundMiddleware {
|
|
16
20
|
readonly id: string;
|
|
@@ -22,13 +26,14 @@ export interface BoundMiddleware {
|
|
|
22
26
|
/** Execution-only tools. Advertised for this run and omitted from the manifest. */
|
|
23
27
|
readonly sessionTools?: readonly ToolDefinition<any, any, any>[];
|
|
24
28
|
readonly contributions?: MiddlewareContributions;
|
|
25
|
-
|
|
26
|
-
readonly
|
|
29
|
+
/** Registered hook points, in canonical order. */
|
|
30
|
+
readonly hooks?: readonly HookManifest[];
|
|
27
31
|
readonly manifestType?: "agent" | "agent-plugin";
|
|
28
32
|
readonly metadata?: JsonObject;
|
|
29
33
|
readonly skills?: Readonly<Record<string, SkillManifest>>;
|
|
30
34
|
readonly skillRecords?: Readonly<Record<string, SkillRecord>>;
|
|
31
35
|
readonly mcpServers?: Readonly<Record<string, McpServerManifest>>;
|
|
36
|
+
readonly sandbox?: SandboxManifest;
|
|
32
37
|
/** Package directory for a plugin capability. Not a manifest field. */
|
|
33
38
|
readonly pluginRoot?: string;
|
|
34
39
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type { BoundMiddleware } from "./bound.js";
|
|
2
|
-
import type {
|
|
2
|
+
import type { CapabilityInput, StepMiddleware } from "../types/middleware.js";
|
|
3
3
|
import type { BuildDiagnostic, JsonObject } from "../types/shared.js";
|
|
4
4
|
import { HarnessError } from "../errors.js";
|
|
5
5
|
import type { ToolDefinition, ToolSchemaSource, SchemaOutput } from "../types/tool.js";
|
|
6
|
-
import type { BuiltAgent } from "../types/agent.js";
|
|
6
|
+
import type { AgentTool, BuiltAgent } from "../types/agent.js";
|
|
7
7
|
import type { AgentManifest } from "../types/manifest.js";
|
|
8
|
-
import type {
|
|
8
|
+
import type { AfterHook, BeforeHook, HookScope } from "../types/dynamics.js";
|
|
9
9
|
import type { Implementations } from "./implementations.js";
|
|
10
10
|
import { type CapabilityDynamics } from "./assemble.js";
|
|
11
11
|
export interface AgentOptions<Schema extends ToolSchemaSource | undefined = undefined> {
|
|
@@ -15,8 +15,8 @@ export interface AgentOptions<Schema extends ToolSchemaSource | undefined = unde
|
|
|
15
15
|
readonly description?: string;
|
|
16
16
|
readonly metadata?: JsonObject;
|
|
17
17
|
readonly instructions?: string | readonly string[];
|
|
18
|
-
/** Top-level tools (H2). Composed as capability id `"agent"`. */
|
|
19
|
-
readonly tools?: readonly ToolDefinition<any, any, any>[];
|
|
18
|
+
/** Top-level tools (H2), and agents used as tools. Composed as capability id `"agent"`. */
|
|
19
|
+
readonly tools?: readonly (ToolDefinition<any, any, any> | AgentTool)[];
|
|
20
20
|
}
|
|
21
21
|
interface BuilderSnapshot {
|
|
22
22
|
readonly id: string;
|
|
@@ -26,8 +26,8 @@ interface BuilderSnapshot {
|
|
|
26
26
|
readonly outputSchema?: ToolSchemaSource;
|
|
27
27
|
readonly entries: readonly BoundMiddleware[];
|
|
28
28
|
readonly dynamics: ReadonlyMap<string, CapabilityDynamics>;
|
|
29
|
-
|
|
30
|
-
readonly
|
|
29
|
+
/** Builder-level problems, reported with the assembly diagnostics. */
|
|
30
|
+
readonly diagnostics?: readonly BuildDiagnostic[];
|
|
31
31
|
}
|
|
32
32
|
export declare class AgentBuildError extends HarnessError {
|
|
33
33
|
readonly diagnostics: readonly BuildDiagnostic[];
|
|
@@ -56,9 +56,17 @@ export declare class AgentBuilder<Info = unknown, Schema extends ToolSchemaSourc
|
|
|
56
56
|
toJSON(): AgentManifest;
|
|
57
57
|
use(middleware: StepMiddleware<Info>): AgentBuilder<Info, Schema>;
|
|
58
58
|
use(id: string, middleware: StepMiddleware<Info>): AgentBuilder<Info, Schema>;
|
|
59
|
-
use(declaration:
|
|
60
|
-
|
|
61
|
-
|
|
59
|
+
use(declaration: CapabilityInput<Info>): AgentBuilder<Info, Schema>;
|
|
60
|
+
/**
|
|
61
|
+
* Run `fn` before each turn (`"turn"`) or before every model call (`"step"`).
|
|
62
|
+
* The returned Patch applies to the whole turn or to that one model call.
|
|
63
|
+
*/
|
|
64
|
+
before<S extends HookScope>(scope: S, fn: BeforeHook<S, Info>): AgentBuilder<Info, Schema>;
|
|
65
|
+
/**
|
|
66
|
+
* Run `fn` after every model call (`"step"`) or after the turn's final answer (`"turn"`).
|
|
67
|
+
*/
|
|
68
|
+
after<S extends HookScope>(scope: S, fn: AfterHook<S, Info>): AgentBuilder<Info, Schema>;
|
|
69
|
+
private addAgentHook;
|
|
62
70
|
/** Optional no-op: returns the assembled agent facade. */
|
|
63
71
|
build(): BuiltAgent<Info, Schema extends ToolSchemaSource ? SchemaOutput<Schema> : string>;
|
|
64
72
|
private ensure;
|
|
@@ -2,6 +2,7 @@ import { HarnessError } from "../errors.js";
|
|
|
2
2
|
import { assembleAgent } from "./assemble.js";
|
|
3
3
|
import { compileDeclaration } from "./declaration.js";
|
|
4
4
|
import { agentFrom } from "./from.js";
|
|
5
|
+
import { hooksFrom } from "./hooks.js";
|
|
5
6
|
export class AgentBuildError extends HarnessError {
|
|
6
7
|
diagnostics;
|
|
7
8
|
constructor(diagnostics) {
|
|
@@ -85,12 +86,8 @@ export class AgentBuilder {
|
|
|
85
86
|
}
|
|
86
87
|
const dynamics = new Map(this.#snapshot.dynamics);
|
|
87
88
|
dynamics.set(compiled.bound.id, {
|
|
88
|
-
...(compiled.
|
|
89
|
-
|
|
90
|
-
: {}),
|
|
91
|
-
...(compiled.afterModelCall
|
|
92
|
-
? { afterModelCall: compiled.afterModelCall }
|
|
93
|
-
: {}),
|
|
89
|
+
...(compiled.before ? { before: compiled.before } : {}),
|
|
90
|
+
...(compiled.after ? { after: compiled.after } : {}),
|
|
94
91
|
...(compiled.middleware ? { middleware: compiled.middleware } : {}),
|
|
95
92
|
});
|
|
96
93
|
return AgentBuilder.withSnapshot({
|
|
@@ -99,34 +96,47 @@ export class AgentBuilder {
|
|
|
99
96
|
dynamics,
|
|
100
97
|
});
|
|
101
98
|
}
|
|
102
|
-
|
|
99
|
+
/**
|
|
100
|
+
* Run `fn` before each turn (`"turn"`) or before every model call (`"step"`).
|
|
101
|
+
* The returned Patch applies to the whole turn or to that one model call.
|
|
102
|
+
*/
|
|
103
|
+
before(scope, fn) {
|
|
104
|
+
return this.addAgentHook("before", scope, fn);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Run `fn` after every model call (`"step"`) or after the turn's final answer (`"turn"`).
|
|
108
|
+
*/
|
|
109
|
+
after(scope, fn) {
|
|
110
|
+
return this.addAgentHook("after", scope, fn);
|
|
111
|
+
}
|
|
112
|
+
addAgentHook(at, scope, fn) {
|
|
113
|
+
if (typeof fn !== "function")
|
|
114
|
+
throw new HarnessError("configuration.invalid", `${at}("${scope}") requires a function`);
|
|
115
|
+
if (scope !== "turn" && scope !== "step")
|
|
116
|
+
throw new HarnessError("configuration.invalid", `Unknown hook scope '${String(scope)}'; use "turn" or "step"`);
|
|
103
117
|
const dynamics = new Map(this.#snapshot.dynamics);
|
|
104
118
|
const existing = dynamics.get("agent") ?? {};
|
|
105
|
-
|
|
119
|
+
const current = existing[at];
|
|
120
|
+
if (current?.[scope] !== undefined)
|
|
121
|
+
return AgentBuilder.withSnapshot({
|
|
122
|
+
...this.#snapshot,
|
|
123
|
+
diagnostics: Object.freeze([
|
|
124
|
+
...(this.#snapshot.diagnostics ?? []),
|
|
125
|
+
Object.freeze({
|
|
126
|
+
code: "hook.duplicate",
|
|
127
|
+
message: `Agent hook ${at}("${scope}") is registered more than once`,
|
|
128
|
+
}),
|
|
129
|
+
]),
|
|
130
|
+
});
|
|
131
|
+
const next = {
|
|
106
132
|
...existing,
|
|
107
|
-
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
return AgentBuilder.withSnapshot({
|
|
111
|
-
...this.#snapshot,
|
|
112
|
-
entries,
|
|
113
|
-
dynamics,
|
|
114
|
-
agentBefore: fn,
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
afterModelCall(fn) {
|
|
118
|
-
const dynamics = new Map(this.#snapshot.dynamics);
|
|
119
|
-
const existing = dynamics.get("agent") ?? {};
|
|
120
|
-
dynamics.set("agent", {
|
|
121
|
-
...existing,
|
|
122
|
-
afterModelCall: fn,
|
|
123
|
-
});
|
|
124
|
-
const entries = ensureAgentCapabilityFlag(this.#snapshot.entries, "afterModelCall");
|
|
133
|
+
[at]: Object.freeze({ ...current, [scope]: fn }),
|
|
134
|
+
};
|
|
135
|
+
dynamics.set("agent", next);
|
|
125
136
|
return AgentBuilder.withSnapshot({
|
|
126
137
|
...this.#snapshot,
|
|
127
|
-
entries,
|
|
138
|
+
entries: withAgentHooks(this.#snapshot.entries, next),
|
|
128
139
|
dynamics,
|
|
129
|
-
agentAfter: fn,
|
|
130
140
|
});
|
|
131
141
|
}
|
|
132
142
|
/** Optional no-op: returns the assembled agent facade. */
|
|
@@ -138,6 +148,10 @@ export class AgentBuilder {
|
|
|
138
148
|
return this.#agent;
|
|
139
149
|
if (this.#error)
|
|
140
150
|
throw this.#error;
|
|
151
|
+
if (this.#snapshot.diagnostics?.length) {
|
|
152
|
+
this.#error = new AgentBuildError(this.#snapshot.diagnostics);
|
|
153
|
+
throw this.#error;
|
|
154
|
+
}
|
|
141
155
|
const result = assembleAgent(this.#snapshot.entries, {
|
|
142
156
|
id: this.#snapshot.id,
|
|
143
157
|
name: this.#snapshot.name,
|
|
@@ -192,13 +206,12 @@ function nextMiddlewareId(entries) {
|
|
|
192
206
|
} while (taken.has(id));
|
|
193
207
|
return id;
|
|
194
208
|
}
|
|
195
|
-
|
|
209
|
+
/** Record the agent-level hooks on the synthetic `"agent"` capability, creating it if needed. */
|
|
210
|
+
function withAgentHooks(entries, dynamics) {
|
|
211
|
+
const hooks = hooksFrom(dynamics.before, dynamics.after);
|
|
196
212
|
const index = entries.findIndex((item) => item.id === "agent");
|
|
197
213
|
if (index >= 0) {
|
|
198
|
-
const
|
|
199
|
-
if (current[flag])
|
|
200
|
-
return entries;
|
|
201
|
-
const next = Object.freeze({ ...current, [flag]: true });
|
|
214
|
+
const next = Object.freeze({ ...entries[index], hooks });
|
|
202
215
|
return Object.freeze([
|
|
203
216
|
...entries.slice(0, index),
|
|
204
217
|
next,
|
|
@@ -212,7 +225,7 @@ function ensureAgentCapabilityFlag(entries, flag) {
|
|
|
212
225
|
handle: async (_request, next) => next(),
|
|
213
226
|
hasMiddleware: false,
|
|
214
227
|
contributions: Object.freeze({}),
|
|
215
|
-
|
|
228
|
+
hooks,
|
|
216
229
|
}),
|
|
217
230
|
]);
|
|
218
231
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type { BoundMiddleware } from "./bound.js";
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
2
|
+
import type { CapabilityInput, StepMiddleware } from "../types/middleware.js";
|
|
3
|
+
import type { AfterHooks, BeforeHooks } from "../types/dynamics.js";
|
|
4
4
|
export interface CompiledCapability {
|
|
5
5
|
readonly bound: BoundMiddleware;
|
|
6
|
-
readonly
|
|
7
|
-
readonly
|
|
6
|
+
readonly before?: BeforeHooks<any>;
|
|
7
|
+
readonly after?: AfterHooks<any>;
|
|
8
8
|
readonly middleware?: StepMiddleware<any>;
|
|
9
9
|
}
|
|
10
|
-
export declare function compileDeclaration<State>(declaration:
|
|
10
|
+
export declare function compileDeclaration<State>(declaration: CapabilityInput<State>): CompiledCapability;
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
+
import { hooksFrom } from "./hooks.js";
|
|
2
|
+
import { delegateTool, isAgentItem } from "./delegate.js";
|
|
1
3
|
export function compileDeclaration(declaration) {
|
|
2
|
-
const tools = copyItems(declaration.tools, declaration.id);
|
|
4
|
+
const tools = asTools(copyItems(declaration.tools, declaration.id));
|
|
3
5
|
const instructions = copyItems(declaration.instructions, declaration.id);
|
|
4
6
|
const model = declaration.model;
|
|
7
|
+
const hooks = hooksFrom(declaration.before, declaration.after);
|
|
5
8
|
const contributions = snapshotContributions(instructions?.items, tools?.items, model);
|
|
6
9
|
const handle = async (request, next) => {
|
|
7
10
|
if (tools)
|
|
@@ -25,8 +28,7 @@ export function compileDeclaration(declaration) {
|
|
|
25
28
|
hasMiddleware: declaration.middleware !== undefined,
|
|
26
29
|
tools: tools?.items,
|
|
27
30
|
...(contributions === undefined ? {} : { contributions }),
|
|
28
|
-
...(
|
|
29
|
-
...(declaration.afterModelCall ? { afterModelCall: true } : {}),
|
|
31
|
+
...(hooks === undefined ? {} : { hooks }),
|
|
30
32
|
...(declaration.type === undefined ? {} : { manifestType: declaration.type }),
|
|
31
33
|
...(declaration.metadata === undefined
|
|
32
34
|
? {}
|
|
@@ -43,16 +45,15 @@ export function compileDeclaration(declaration) {
|
|
|
43
45
|
Object.keys(declaration.mcpServers).length === 0
|
|
44
46
|
? {}
|
|
45
47
|
: { mcpServers: declaration.mcpServers }),
|
|
48
|
+
...(declaration.sandbox === undefined
|
|
49
|
+
? {}
|
|
50
|
+
: { sandbox: declaration.sandbox }),
|
|
46
51
|
...(declaration.pluginRoot === undefined
|
|
47
52
|
? {}
|
|
48
53
|
: { pluginRoot: declaration.pluginRoot }),
|
|
49
54
|
},
|
|
50
|
-
...(declaration.
|
|
51
|
-
|
|
52
|
-
: {}),
|
|
53
|
-
...(declaration.afterModelCall
|
|
54
|
-
? { afterModelCall: declaration.afterModelCall }
|
|
55
|
-
: {}),
|
|
55
|
+
...(declaration.before === undefined ? {} : { before: declaration.before }),
|
|
56
|
+
...(declaration.after === undefined ? {} : { after: declaration.after }),
|
|
56
57
|
...(declaration.middleware ? { middleware: declaration.middleware } : {}),
|
|
57
58
|
};
|
|
58
59
|
}
|
|
@@ -91,3 +92,14 @@ function copyItems(value, defaultSlot) {
|
|
|
91
92
|
items: Object.freeze([...value.items]),
|
|
92
93
|
});
|
|
93
94
|
}
|
|
95
|
+
/** Agents placed in `tools` become the tools that delegate to them. */
|
|
96
|
+
function asTools(value) {
|
|
97
|
+
if (value === undefined)
|
|
98
|
+
return undefined;
|
|
99
|
+
if (!value.items.some(isAgentItem))
|
|
100
|
+
return value;
|
|
101
|
+
return Object.freeze({
|
|
102
|
+
slot: value.slot,
|
|
103
|
+
items: Object.freeze(value.items.map((item) => (isAgentItem(item) ? delegateTool(item) : item))),
|
|
104
|
+
});
|
|
105
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { BuiltAgent } from "../types/agent.js";
|
|
2
|
+
import type { AgentManifest } from "../types/manifest.js";
|
|
3
|
+
import type { JsonObject } from "../types/shared.js";
|
|
4
|
+
import type { ToolDefinition } from "../types/tool.js";
|
|
5
|
+
/** An agent used as another agent's tool. The engine runs it; customer code never does. */
|
|
6
|
+
export interface Delegate {
|
|
7
|
+
/** The child's manifest, inlined into the parent's tool. */
|
|
8
|
+
readonly manifest: AgentManifest;
|
|
9
|
+
/** The authored child, when available locally. Absent when rebuilt from a manifest. */
|
|
10
|
+
readonly agent?: BuiltAgent;
|
|
11
|
+
}
|
|
12
|
+
/** Model-facing tool names: what every supported provider accepts. */
|
|
13
|
+
export declare const DELEGATE_NAME_PATTERN: RegExp;
|
|
14
|
+
/** The input every agent used as a tool accepts. Part of the hashed manifest; change deliberately. */
|
|
15
|
+
export declare const DELEGATE_INPUT_SCHEMA: JsonObject;
|
|
16
|
+
/** A built agent or an agent builder; both expose getBinding() and manifest. */
|
|
17
|
+
export declare function isAgentItem(value: unknown): value is Pick<BuiltAgent, "id" | "manifest" | "getBinding"> & {
|
|
18
|
+
build?: () => BuiltAgent;
|
|
19
|
+
};
|
|
20
|
+
/** Turn an agent placed in `tools` into the tool that delegates to it. */
|
|
21
|
+
export declare function delegateTool(item: unknown): ToolDefinition;
|
|
22
|
+
/** Rebuild the delegating tool from a manifest's `agent` body. */
|
|
23
|
+
export declare function delegateFromManifest(manifest: AgentManifest, tool: {
|
|
24
|
+
readonly description?: string;
|
|
25
|
+
readonly inputSchema: JsonObject;
|
|
26
|
+
}): ToolDefinition;
|
|
27
|
+
/** The delegate behind a tool definition, or behind the bound snapshot of one. */
|
|
28
|
+
export declare function delegateOf(tool: unknown): Delegate | undefined;
|
|
29
|
+
/** The agents a manifest delegates to, with the capability and tool that declare each. */
|
|
30
|
+
export declare function delegatesOf(manifest: AgentManifest): readonly {
|
|
31
|
+
readonly capabilityId: string;
|
|
32
|
+
readonly manifest: AgentManifest;
|
|
33
|
+
}[];
|
|
34
|
+
/** The manifest of the delegated agent `id`, or undefined. One level deep by construction. */
|
|
35
|
+
export declare function delegateManifest(manifest: AgentManifest, id: string): AgentManifest | undefined;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { ToolError } from "./tool-error.js";
|
|
3
|
+
import { normalizeSchema } from "./schema.js";
|
|
4
|
+
import { schemaFromJSON } from "./schema-json.js";
|
|
5
|
+
/** Survives duplicate installed copies of core, like the error brands. */
|
|
6
|
+
const DELEGATE = Symbol.for("@nylorun/core/delegate");
|
|
7
|
+
/** Model-facing tool names: what every supported provider accepts. */
|
|
8
|
+
export const DELEGATE_NAME_PATTERN = /^[A-Za-z0-9_-]{1,64}$/;
|
|
9
|
+
const taskSchema = z
|
|
10
|
+
.object({
|
|
11
|
+
task: z
|
|
12
|
+
.string()
|
|
13
|
+
.min(1)
|
|
14
|
+
.describe("A complete, self-contained task. The agent sees nothing else."),
|
|
15
|
+
})
|
|
16
|
+
.strict();
|
|
17
|
+
/** The input every agent used as a tool accepts. Part of the hashed manifest; change deliberately. */
|
|
18
|
+
export const DELEGATE_INPUT_SCHEMA = normalizeSchema(taskSchema, "input").jsonSchema;
|
|
19
|
+
async function engineOnly() {
|
|
20
|
+
throw new ToolError("delegation.engine-only", "Agents used as tools are run by the Nylorun engine, not by an executor");
|
|
21
|
+
}
|
|
22
|
+
/** A built agent or an agent builder; both expose getBinding() and manifest. */
|
|
23
|
+
export function isAgentItem(value) {
|
|
24
|
+
if (value === null || typeof value !== "object")
|
|
25
|
+
return false;
|
|
26
|
+
const item = value;
|
|
27
|
+
return (typeof item.getBinding === "function" &&
|
|
28
|
+
typeof item.execute !== "function" &&
|
|
29
|
+
typeof item.run !== "function");
|
|
30
|
+
}
|
|
31
|
+
/** Turn an agent placed in `tools` into the tool that delegates to it. */
|
|
32
|
+
export function delegateTool(item) {
|
|
33
|
+
const candidate = item;
|
|
34
|
+
const agent = typeof candidate.build === "function" ? candidate.build() : candidate;
|
|
35
|
+
return withDelegate(agent.manifest, agent);
|
|
36
|
+
}
|
|
37
|
+
/** Rebuild the delegating tool from a manifest's `agent` body. */
|
|
38
|
+
export function delegateFromManifest(manifest, tool) {
|
|
39
|
+
return withDelegate(manifest, undefined, tool);
|
|
40
|
+
}
|
|
41
|
+
function withDelegate(manifest, agent, declared) {
|
|
42
|
+
const description = declared ? declared.description : manifest.description;
|
|
43
|
+
const definition = {
|
|
44
|
+
name: manifest.id,
|
|
45
|
+
...(description === undefined ? {} : { description }),
|
|
46
|
+
inputSchema: declared ? schemaFromJSON(declared.inputSchema) : taskSchema,
|
|
47
|
+
execute: engineOnly,
|
|
48
|
+
};
|
|
49
|
+
const delegate = Object.freeze({ manifest, ...(agent ? { agent } : {}) });
|
|
50
|
+
Object.defineProperty(definition, DELEGATE, { value: delegate, enumerable: false });
|
|
51
|
+
return Object.freeze(definition);
|
|
52
|
+
}
|
|
53
|
+
/** The delegate behind a tool definition, or behind the bound snapshot of one. */
|
|
54
|
+
export function delegateOf(tool) {
|
|
55
|
+
if (tool === null || typeof tool !== "object")
|
|
56
|
+
return undefined;
|
|
57
|
+
const value = tool;
|
|
58
|
+
return value[DELEGATE] ?? (value.source === tool ? undefined : delegateOf(value.source));
|
|
59
|
+
}
|
|
60
|
+
/** The agents a manifest delegates to, with the capability and tool that declare each. */
|
|
61
|
+
export function delegatesOf(manifest) {
|
|
62
|
+
const found = [];
|
|
63
|
+
for (const capability of manifest.capabilities)
|
|
64
|
+
for (const tool of capability.tools ?? [])
|
|
65
|
+
if (tool.agent)
|
|
66
|
+
found.push({ capabilityId: capability.id, manifest: tool.agent });
|
|
67
|
+
return found;
|
|
68
|
+
}
|
|
69
|
+
/** The manifest of the delegated agent `id`, or undefined. One level deep by construction. */
|
|
70
|
+
export function delegateManifest(manifest, id) {
|
|
71
|
+
return delegatesOf(manifest).find((item) => item.manifest.id === id)?.manifest;
|
|
72
|
+
}
|