@nylorun/core 0.2.0-beta → 0.3.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 +20 -0
- package/dist/contracts.d.ts +168 -14
- package/dist/contracts.js +160 -10
- package/dist/define.d.ts +6 -2
- package/dist/define.js +3 -0
- package/dist/definition/assemble.d.ts +3 -3
- package/dist/definition/assemble.js +16 -2
- package/dist/definition/bind-agent.js +2 -2
- package/dist/definition/bound.d.ts +5 -3
- package/dist/definition/builder.d.ts +13 -5
- package/dist/definition/builder.js +47 -34
- package/dist/definition/declaration.d.ts +3 -3
- package/dist/definition/declaration.js +8 -8
- package/dist/definition/from.js +41 -17
- package/dist/definition/helpers.d.ts +7 -5
- package/dist/definition/helpers.js +4 -8
- 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 +7 -3
- package/dist/definition/sandbox-tools.d.ts +5 -0
- package/dist/definition/sandbox-tools.js +109 -0
- package/dist/types/dynamics.d.ts +62 -14
- package/dist/types/manifest.d.ts +29 -3
- package/dist/types/middleware.d.ts +9 -5
- package/dist/types/tool.d.ts +1 -1
- package/dist/utils/sandbox.d.ts +17 -0
- package/dist/utils/sandbox.js +64 -0
- package/package.json +1 -1
|
@@ -5,7 +5,7 @@ import { HarnessError } from "../errors.js";
|
|
|
5
5
|
import type { ToolDefinition, ToolSchemaSource, SchemaOutput } from "../types/tool.js";
|
|
6
6
|
import type { 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> {
|
|
@@ -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[];
|
|
@@ -57,8 +57,16 @@ export declare class AgentBuilder<Info = unknown, Schema extends ToolSchemaSourc
|
|
|
57
57
|
use(middleware: StepMiddleware<Info>): AgentBuilder<Info, Schema>;
|
|
58
58
|
use(id: string, middleware: StepMiddleware<Info>): AgentBuilder<Info, Schema>;
|
|
59
59
|
use(declaration: CapabilityDeclaration<Info>): AgentBuilder<Info, Schema>;
|
|
60
|
-
|
|
61
|
-
|
|
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
2
|
import type { CapabilityDeclaration, StepMiddleware } from "../types/middleware.js";
|
|
3
|
-
import type {
|
|
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
10
|
export declare function compileDeclaration<State>(declaration: CapabilityDeclaration<State>): CompiledCapability;
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import { hooksFrom } from "./hooks.js";
|
|
1
2
|
export function compileDeclaration(declaration) {
|
|
2
3
|
const tools = copyItems(declaration.tools, declaration.id);
|
|
3
4
|
const instructions = copyItems(declaration.instructions, declaration.id);
|
|
4
5
|
const model = declaration.model;
|
|
6
|
+
const hooks = hooksFrom(declaration.before, declaration.after);
|
|
5
7
|
const contributions = snapshotContributions(instructions?.items, tools?.items, model);
|
|
6
8
|
const handle = async (request, next) => {
|
|
7
9
|
if (tools)
|
|
@@ -25,8 +27,7 @@ export function compileDeclaration(declaration) {
|
|
|
25
27
|
hasMiddleware: declaration.middleware !== undefined,
|
|
26
28
|
tools: tools?.items,
|
|
27
29
|
...(contributions === undefined ? {} : { contributions }),
|
|
28
|
-
...(
|
|
29
|
-
...(declaration.afterModelCall ? { afterModelCall: true } : {}),
|
|
30
|
+
...(hooks === undefined ? {} : { hooks }),
|
|
30
31
|
...(declaration.type === undefined ? {} : { manifestType: declaration.type }),
|
|
31
32
|
...(declaration.metadata === undefined
|
|
32
33
|
? {}
|
|
@@ -43,16 +44,15 @@ export function compileDeclaration(declaration) {
|
|
|
43
44
|
Object.keys(declaration.mcpServers).length === 0
|
|
44
45
|
? {}
|
|
45
46
|
: { mcpServers: declaration.mcpServers }),
|
|
47
|
+
...(declaration.sandbox === undefined
|
|
48
|
+
? {}
|
|
49
|
+
: { sandbox: declaration.sandbox }),
|
|
46
50
|
...(declaration.pluginRoot === undefined
|
|
47
51
|
? {}
|
|
48
52
|
: { pluginRoot: declaration.pluginRoot }),
|
|
49
53
|
},
|
|
50
|
-
...(declaration.
|
|
51
|
-
|
|
52
|
-
: {}),
|
|
53
|
-
...(declaration.afterModelCall
|
|
54
|
-
? { afterModelCall: declaration.afterModelCall }
|
|
55
|
-
: {}),
|
|
54
|
+
...(declaration.before === undefined ? {} : { before: declaration.before }),
|
|
55
|
+
...(declaration.after === undefined ? {} : { after: declaration.after }),
|
|
56
56
|
...(declaration.middleware ? { middleware: declaration.middleware } : {}),
|
|
57
57
|
};
|
|
58
58
|
}
|
package/dist/definition/from.js
CHANGED
|
@@ -2,6 +2,7 @@ import { HarnessError } from "../errors.js";
|
|
|
2
2
|
import { assembleAgent } from "./assemble.js";
|
|
3
3
|
import { schemaFromJSON } from "./schema-json.js";
|
|
4
4
|
import { copyJsonObject, deepFreeze } from "../utils/immutable.js";
|
|
5
|
+
import { HOOK_POINTS, hasHook, hookListIssue } from "./hooks.js";
|
|
5
6
|
/** Rebuild an agent from manifest JSON + in-process implementations (T28). */
|
|
6
7
|
export function agentFrom(json, implementations, options) {
|
|
7
8
|
const manifest = normalizeManifest(json);
|
|
@@ -20,10 +21,7 @@ export function agentFrom(json, implementations, options) {
|
|
|
20
21
|
throw new HarnessError("agent.build-failed", `Missing session tool implementation '${item.name}' for capability '${capability.id}'`);
|
|
21
22
|
return live;
|
|
22
23
|
});
|
|
23
|
-
|
|
24
|
-
throw new HarnessError("agent.build-failed", `Missing beforeModelCall implementation for capability '${capability.id}'`);
|
|
25
|
-
if (capability.afterModelCall && !impl.afterModelCall)
|
|
26
|
-
throw new HarnessError("agent.build-failed", `Missing afterModelCall implementation for capability '${capability.id}'`);
|
|
24
|
+
assertHookImplementations(capability, impl);
|
|
27
25
|
const handle = impl.middleware ??
|
|
28
26
|
(async (request, next) => {
|
|
29
27
|
const advertised = [...tools, ...session];
|
|
@@ -65,8 +63,7 @@ export function agentFrom(json, implementations, options) {
|
|
|
65
63
|
}
|
|
66
64
|
: {}),
|
|
67
65
|
}),
|
|
68
|
-
...(capability.
|
|
69
|
-
...(capability.afterModelCall ? { afterModelCall: true } : {}),
|
|
66
|
+
...(capability.hooks === undefined ? {} : { hooks: capability.hooks }),
|
|
70
67
|
manifestType: capability.type,
|
|
71
68
|
...(capability.metadata === undefined
|
|
72
69
|
? {}
|
|
@@ -74,17 +71,16 @@ export function agentFrom(json, implementations, options) {
|
|
|
74
71
|
...(capability.mcpServers === undefined
|
|
75
72
|
? {}
|
|
76
73
|
: { mcpServers: capability.mcpServers }),
|
|
74
|
+
...(capability.sandbox === undefined
|
|
75
|
+
? {}
|
|
76
|
+
: { sandbox: capability.sandbox }),
|
|
77
77
|
...(capability.skills === undefined
|
|
78
78
|
? {}
|
|
79
79
|
: { skills: capability.skills }),
|
|
80
80
|
}));
|
|
81
81
|
dynamics.set(capability.id, {
|
|
82
|
-
...(impl.
|
|
83
|
-
|
|
84
|
-
: {}),
|
|
85
|
-
...(impl.afterModelCall
|
|
86
|
-
? { afterModelCall: impl.afterModelCall }
|
|
87
|
-
: {}),
|
|
82
|
+
...(impl.before ? { before: impl.before } : {}),
|
|
83
|
+
...(impl.after ? { after: impl.after } : {}),
|
|
88
84
|
...(impl.middleware ? { middleware: impl.middleware } : {}),
|
|
89
85
|
});
|
|
90
86
|
}
|
|
@@ -130,6 +126,16 @@ function resolveTools(capability, tools) {
|
|
|
130
126
|
throw new HarnessError("agent.build-failed", `Missing tool implementation '${declared.name}' for capability '${capability.id}'`);
|
|
131
127
|
});
|
|
132
128
|
}
|
|
129
|
+
function assertHookImplementations(capability, impl) {
|
|
130
|
+
for (const point of HOOK_POINTS) {
|
|
131
|
+
const declared = hasHook(capability.hooks, point.at, point.scope);
|
|
132
|
+
const live = impl[point.at]?.[point.scope] !== undefined;
|
|
133
|
+
if (declared && !live)
|
|
134
|
+
throw new HarnessError("agent.build-failed", `Missing ${point.at}("${point.scope}") implementation for capability '${capability.id}'`);
|
|
135
|
+
if (live && !declared)
|
|
136
|
+
throw new HarnessError("agent.build-failed", `Capability '${capability.id}' implements ${point.at}("${point.scope}") but the manifest does not declare it`);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
133
139
|
function normalizeManifest(json) {
|
|
134
140
|
if (!json || typeof json !== "object" || Array.isArray(json))
|
|
135
141
|
throw new HarnessError("agent.build-failed", "Agent.from requires a manifest object");
|
|
@@ -144,8 +150,10 @@ function normalizeManifest(json) {
|
|
|
144
150
|
throw new HarnessError("agent.build-failed", "Manifest capabilities must be an array");
|
|
145
151
|
if ("schemaVersion" in value && value.schemaVersion !== undefined)
|
|
146
152
|
throw new HarnessError("agent.build-failed", "Manifest field schemaVersion was renamed to manifestSchemaVersion");
|
|
147
|
-
if (value.manifestSchemaVersion !==
|
|
148
|
-
throw new HarnessError("agent.build-failed",
|
|
153
|
+
if (value.manifestSchemaVersion !== 4)
|
|
154
|
+
throw new HarnessError("agent.build-failed", value.manifestSchemaVersion === 3
|
|
155
|
+
? "manifestSchemaVersion 3 is no longer supported; rebuild the agent with before/after hooks (see MIGRATION.md)"
|
|
156
|
+
: `Unsupported manifestSchemaVersion ${String(value.manifestSchemaVersion)}`);
|
|
149
157
|
// Reject top-level model (Runtime-owned).
|
|
150
158
|
if ("model" in value && value.model !== undefined)
|
|
151
159
|
throw new HarnessError("agent.build-failed", "Manifest must not include top-level model; Runtime owns model resolution");
|
|
@@ -166,7 +174,7 @@ function normalizeManifest(json) {
|
|
|
166
174
|
servers.add(name);
|
|
167
175
|
}
|
|
168
176
|
return deepFreeze({
|
|
169
|
-
manifestSchemaVersion:
|
|
177
|
+
manifestSchemaVersion: 4,
|
|
170
178
|
id: value.id,
|
|
171
179
|
...(typeof value.name === "string" && value.name ? { name: value.name } : {}),
|
|
172
180
|
...(typeof value.description === "string"
|
|
@@ -204,6 +212,14 @@ function normalizeCapability(capability) {
|
|
|
204
212
|
throw new HarnessError("agent.build-failed", `Capability '${capability.id}' must not include model; Runtime owns model resolution`);
|
|
205
213
|
if (capability.type !== "agent" && capability.type !== "agent-plugin")
|
|
206
214
|
throw new HarnessError("agent.build-failed", `Capability '${capability.id}' type must be agent or agent-plugin`);
|
|
215
|
+
for (const legacy of ["beforeModelCall", "afterModelCall"])
|
|
216
|
+
if (legacy in raw)
|
|
217
|
+
throw new HarnessError("agent.build-failed", `Capability '${capability.id}' field ${legacy} was replaced by hooks (see MIGRATION.md)`);
|
|
218
|
+
if (capability.hooks !== undefined) {
|
|
219
|
+
const issue = hookListIssue(capability.hooks);
|
|
220
|
+
if (issue)
|
|
221
|
+
throw new HarnessError("agent.build-failed", `Capability '${capability.id}' ${issue}`);
|
|
222
|
+
}
|
|
207
223
|
const metadata = capability.metadata === undefined
|
|
208
224
|
? undefined
|
|
209
225
|
: copyJsonObject(capability.metadata, "metadata");
|
|
@@ -225,8 +241,16 @@ function normalizeCapability(capability) {
|
|
|
225
241
|
: {}),
|
|
226
242
|
...(skills === undefined ? {} : { skills }),
|
|
227
243
|
...(mcpServers === undefined ? {} : { mcpServers }),
|
|
228
|
-
...(capability.
|
|
229
|
-
|
|
244
|
+
...(capability.sandbox === undefined
|
|
245
|
+
? {}
|
|
246
|
+
: {
|
|
247
|
+
sandbox: deepFreeze(copyJsonObject(capability.sandbox, "sandbox")),
|
|
248
|
+
}),
|
|
249
|
+
...(capability.hooks === undefined
|
|
250
|
+
? {}
|
|
251
|
+
: {
|
|
252
|
+
hooks: Object.freeze(capability.hooks.map((hook) => Object.freeze({ at: hook.at, scope: hook.scope }))),
|
|
253
|
+
}),
|
|
230
254
|
});
|
|
231
255
|
}
|
|
232
256
|
function normalizeSkills(capabilityId, skills) {
|
|
@@ -4,18 +4,20 @@ import type { ToolDefinition, ToolInputSchema, ToolOutputSchema, ToolSchemaSourc
|
|
|
4
4
|
export declare const tool: <InputSchema extends ToolInputSchema, Info = unknown, OutputSchema extends ToolOutputSchema | undefined = undefined>(value: ToolDefinition<InputSchema, Info, OutputSchema>) => ToolDefinition<InputSchema, Info, OutputSchema>;
|
|
5
5
|
/** @deprecated Prefer Runtime model resolution. Capability-disguised `.use({ model })` is not the taught path. */
|
|
6
6
|
export declare const model: <T extends ModelAdapter>(value: T) => T;
|
|
7
|
-
/** @deprecated Prefer `
|
|
7
|
+
/** @deprecated Prefer `before` / `after` hooks. Kept through 1.0. */
|
|
8
8
|
export declare const middleware: <T extends StepMiddleware>(value: T) => T;
|
|
9
|
-
/** Compose a reusable capability bundle (tools, instructions, optional
|
|
9
|
+
/** Compose a reusable capability bundle (tools, instructions, optional hooks). */
|
|
10
10
|
export declare function capability<Info = unknown>(declaration: {
|
|
11
11
|
readonly id: string;
|
|
12
12
|
readonly name?: string;
|
|
13
13
|
readonly description?: string;
|
|
14
14
|
readonly tools?: readonly ToolDefinition<any, Info, any>[];
|
|
15
15
|
readonly instructions?: string | readonly string[];
|
|
16
|
-
|
|
17
|
-
readonly
|
|
18
|
-
/**
|
|
16
|
+
/** Run before each turn or before every model call (step). */
|
|
17
|
+
readonly before?: import("../types/dynamics.js").BeforeHooks<Info>;
|
|
18
|
+
/** Run after every model call (step) or after the turn's final answer. */
|
|
19
|
+
readonly after?: import("../types/dynamics.js").AfterHooks<Info>;
|
|
20
|
+
/** @deprecated Prefer before / after hooks. */
|
|
19
21
|
readonly middleware?: StepMiddleware<Info>;
|
|
20
22
|
/** @deprecated Model resolution is Runtime-owned; not projected into the manifest. */
|
|
21
23
|
readonly model?: import("../types/model.js").ModelDirective;
|
|
@@ -2,9 +2,9 @@ import { prepareTool } from "./schema.js";
|
|
|
2
2
|
export const tool = (value) => prepareTool(value);
|
|
3
3
|
/** @deprecated Prefer Runtime model resolution. Capability-disguised `.use({ model })` is not the taught path. */
|
|
4
4
|
export const model = (value) => value;
|
|
5
|
-
/** @deprecated Prefer `
|
|
5
|
+
/** @deprecated Prefer `before` / `after` hooks. Kept through 1.0. */
|
|
6
6
|
export const middleware = (value) => value;
|
|
7
|
-
/** Compose a reusable capability bundle (tools, instructions, optional
|
|
7
|
+
/** Compose a reusable capability bundle (tools, instructions, optional hooks). */
|
|
8
8
|
export function capability(declaration) {
|
|
9
9
|
const instructions = declaration.instructions === undefined
|
|
10
10
|
? undefined
|
|
@@ -19,12 +19,8 @@ export function capability(declaration) {
|
|
|
19
19
|
: { description: declaration.description }),
|
|
20
20
|
...(declaration.tools === undefined ? {} : { tools: declaration.tools }),
|
|
21
21
|
...(instructions === undefined ? {} : { instructions }),
|
|
22
|
-
...(declaration.
|
|
23
|
-
|
|
24
|
-
: { beforeModelCall: declaration.beforeModelCall }),
|
|
25
|
-
...(declaration.afterModelCall === undefined
|
|
26
|
-
? {}
|
|
27
|
-
: { afterModelCall: declaration.afterModelCall }),
|
|
22
|
+
...(declaration.before === undefined ? {} : { before: declaration.before }),
|
|
23
|
+
...(declaration.after === undefined ? {} : { after: declaration.after }),
|
|
28
24
|
...(declaration.middleware === undefined
|
|
29
25
|
? {}
|
|
30
26
|
: { middleware: declaration.middleware }),
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { AfterHooks, BeforeHooks, HookAt, HookManifest, HookScope } from "../types/dynamics.js";
|
|
2
|
+
import type { Implementations } from "./implementations.js";
|
|
3
|
+
/** Every hook point, in the order the loop reaches them and the manifest lists them. */
|
|
4
|
+
export declare const HOOK_POINTS: readonly HookManifest[];
|
|
5
|
+
/** Canonical manifest list for a capability's hooks, or undefined when it has none. */
|
|
6
|
+
export declare function hooksFrom(before: BeforeHooks<any> | undefined, after: AfterHooks<any> | undefined): readonly HookManifest[] | undefined;
|
|
7
|
+
export declare function hasHook(hooks: readonly HookManifest[] | undefined, at: HookAt, scope: HookScope): boolean;
|
|
8
|
+
/** Why a manifest `hooks` value is invalid, or undefined when it is valid. */
|
|
9
|
+
export declare function hookListIssue(value: unknown): string | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* Run one hook point for the listed capabilities, concurrently.
|
|
12
|
+
* Never throws: a missing or failing hook blocks with its message, so a
|
|
13
|
+
* re-delivered hook cannot fail the same way forever.
|
|
14
|
+
*/
|
|
15
|
+
export declare function runHookPoint(implementations: Implementations<any>, hook: {
|
|
16
|
+
readonly at: HookAt;
|
|
17
|
+
readonly scope: HookScope;
|
|
18
|
+
readonly capabilityIds: readonly string[];
|
|
19
|
+
}, args: unknown): Promise<Readonly<Record<string, unknown>>>;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/** Every hook point, in the order the loop reaches them and the manifest lists them. */
|
|
2
|
+
export const HOOK_POINTS = Object.freeze([
|
|
3
|
+
Object.freeze({ at: "before", scope: "turn" }),
|
|
4
|
+
Object.freeze({ at: "before", scope: "step" }),
|
|
5
|
+
Object.freeze({ at: "after", scope: "step" }),
|
|
6
|
+
Object.freeze({ at: "after", scope: "turn" }),
|
|
7
|
+
]);
|
|
8
|
+
const pointIndex = (point) => HOOK_POINTS.findIndex((item) => item.at === point.at && item.scope === point.scope);
|
|
9
|
+
/** Canonical manifest list for a capability's hooks, or undefined when it has none. */
|
|
10
|
+
export function hooksFrom(before, after) {
|
|
11
|
+
const hooks = HOOK_POINTS.filter((point) => typeof (point.at === "before" ? before : after)?.[point.scope] ===
|
|
12
|
+
"function");
|
|
13
|
+
return hooks.length ? Object.freeze(hooks) : undefined;
|
|
14
|
+
}
|
|
15
|
+
export function hasHook(hooks, at, scope) {
|
|
16
|
+
return hooks?.some((hook) => hook.at === at && hook.scope === scope) ?? false;
|
|
17
|
+
}
|
|
18
|
+
/** Why a manifest `hooks` value is invalid, or undefined when it is valid. */
|
|
19
|
+
export function hookListIssue(value) {
|
|
20
|
+
if (!Array.isArray(value))
|
|
21
|
+
return "hooks must be an array";
|
|
22
|
+
if (value.length === 0)
|
|
23
|
+
return "hooks must be omitted when empty";
|
|
24
|
+
let previous = -1;
|
|
25
|
+
for (const item of value) {
|
|
26
|
+
if (item === null ||
|
|
27
|
+
typeof item !== "object" ||
|
|
28
|
+
Object.keys(item).some((key) => key !== "at" && key !== "scope"))
|
|
29
|
+
return "each hook must be { at, scope }";
|
|
30
|
+
const index = pointIndex(item);
|
|
31
|
+
if (index < 0)
|
|
32
|
+
return `unknown hook point '${String(item.at)} ${String(item.scope)}'`;
|
|
33
|
+
if (index === previous)
|
|
34
|
+
return `duplicate hook '${item.at} ${item.scope}'`;
|
|
35
|
+
if (index < previous)
|
|
36
|
+
return "hooks must be listed as before turn, before step, after step, after turn";
|
|
37
|
+
previous = index;
|
|
38
|
+
}
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Run one hook point for the listed capabilities, concurrently.
|
|
43
|
+
* Never throws: a missing or failing hook blocks with its message, so a
|
|
44
|
+
* re-delivered hook cannot fail the same way forever.
|
|
45
|
+
*/
|
|
46
|
+
export async function runHookPoint(implementations, hook, args) {
|
|
47
|
+
const entries = await Promise.all(hook.capabilityIds.map(async (id) => {
|
|
48
|
+
const fn = implementations[id]?.[hook.at]?.[hook.scope];
|
|
49
|
+
if (typeof fn !== "function")
|
|
50
|
+
return [
|
|
51
|
+
id,
|
|
52
|
+
{
|
|
53
|
+
block: `Missing ${hook.at}("${hook.scope}") implementation for capability '${id}'`,
|
|
54
|
+
},
|
|
55
|
+
];
|
|
56
|
+
try {
|
|
57
|
+
return [id, (await fn(args)) ?? {}];
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
return [
|
|
61
|
+
id,
|
|
62
|
+
{ block: error instanceof Error ? error.message : String(error) },
|
|
63
|
+
];
|
|
64
|
+
}
|
|
65
|
+
}));
|
|
66
|
+
return Object.freeze(Object.fromEntries(entries));
|
|
67
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { AfterHooks, BeforeHooks } from "../types/dynamics.js";
|
|
2
2
|
import type { ToolDefinition } from "../types/tool.js";
|
|
3
3
|
import type { StepMiddleware } from "../types/middleware.js";
|
|
4
4
|
/** In-process implementations table keyed by capability id (never serialized). */
|
|
@@ -7,8 +7,8 @@ export interface Implementations<Info = unknown> {
|
|
|
7
7
|
readonly tools?: {
|
|
8
8
|
readonly [toolName: string]: ToolDefinition<any, Info, any>;
|
|
9
9
|
};
|
|
10
|
-
readonly
|
|
11
|
-
readonly
|
|
10
|
+
readonly before?: BeforeHooks<Info>;
|
|
11
|
+
readonly after?: AfterHooks<Info>;
|
|
12
12
|
/** @deprecated Local engine only through 1.0. */
|
|
13
13
|
readonly middleware?: StepMiddleware<Info>;
|
|
14
14
|
};
|
|
@@ -4,7 +4,7 @@ import { copyJsonObject, deepFreeze } from "../utils/immutable.js";
|
|
|
4
4
|
export function createManifest(input) {
|
|
5
5
|
const capabilities = input.middleware.map((item) => projectCapability(item));
|
|
6
6
|
return deepFreeze({
|
|
7
|
-
manifestSchemaVersion:
|
|
7
|
+
manifestSchemaVersion: 4,
|
|
8
8
|
id: input.id,
|
|
9
9
|
...(input.name === undefined ? {} : { name: input.name }),
|
|
10
10
|
...(input.description === undefined
|
|
@@ -41,8 +41,12 @@ function projectCapability(item) {
|
|
|
41
41
|
: { tools: item.tools.map((tool) => projectTool(tool)) }),
|
|
42
42
|
...(item.skills === undefined ? {} : { skills: item.skills }),
|
|
43
43
|
...(item.mcpServers === undefined ? {} : { mcpServers: item.mcpServers }),
|
|
44
|
-
...(item.
|
|
45
|
-
|
|
44
|
+
...(item.sandbox === undefined
|
|
45
|
+
? {}
|
|
46
|
+
: { sandbox: copyJsonObject(item.sandbox, "sandbox") }),
|
|
47
|
+
...(item.hooks === undefined
|
|
48
|
+
? {}
|
|
49
|
+
: { hooks: item.hooks.map((hook) => ({ at: hook.at, scope: hook.scope })) }),
|
|
46
50
|
};
|
|
47
51
|
}
|
|
48
52
|
function projectTool(tool) {
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { ToolDefinition } from "../types/tool.js";
|
|
2
|
+
/** Instructions attached to every sandbox capability. */
|
|
3
|
+
export declare const SANDBOX_INSTRUCTIONS: string;
|
|
4
|
+
/** The six built-in sandbox tools. Schemas are part of the hashed manifest; change them deliberately. */
|
|
5
|
+
export declare function createSandboxTools(): readonly ToolDefinition[];
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { tool } from "./helpers.js";
|
|
3
|
+
import { ToolError } from "./tool-error.js";
|
|
4
|
+
import { SANDBOX_WORKSPACE } from "../utils/sandbox.js";
|
|
5
|
+
const PATH_NOTE = `Relative paths resolve under ${SANDBOX_WORKSPACE}.`;
|
|
6
|
+
/** Instructions attached to every sandbox capability. */
|
|
7
|
+
export const SANDBOX_INSTRUCTIONS = `You have an isolated Linux sandbox with a persistent working directory at ${SANDBOX_WORKSPACE}. ` +
|
|
8
|
+
"Use bash to run commands, read/write/edit to work with files, and grep/glob to search. " +
|
|
9
|
+
"Network egress is limited by policy; a failed download usually means the host is not allowed.";
|
|
10
|
+
/**
|
|
11
|
+
* Stub implementation for the developer process. The Runtime executes sandbox tools;
|
|
12
|
+
* reaching this means the agent ran without a Nylorun Runtime.
|
|
13
|
+
*/
|
|
14
|
+
async function runtimeOnly() {
|
|
15
|
+
throw new ToolError("sandbox.runtime-only", "Sandbox tools run in the Nylorun Runtime. Run the agent with `nylorun dev` or `nylorun serve` to use them.");
|
|
16
|
+
}
|
|
17
|
+
/** The six built-in sandbox tools. Schemas are part of the hashed manifest; change them deliberately. */
|
|
18
|
+
export function createSandboxTools() {
|
|
19
|
+
return Object.freeze([
|
|
20
|
+
tool({
|
|
21
|
+
name: "bash",
|
|
22
|
+
description: `Run a bash command in the sandbox. The working directory is ${SANDBOX_WORKSPACE}. ` +
|
|
23
|
+
"Returns exitCode, stdout and stderr; a non-zero exit code is a normal result, not an error. " +
|
|
24
|
+
"Long output is truncated in the middle.",
|
|
25
|
+
inputSchema: z.object({
|
|
26
|
+
command: z.string().min(1).describe("The command line to run with bash."),
|
|
27
|
+
timeout: z
|
|
28
|
+
.number()
|
|
29
|
+
.int()
|
|
30
|
+
.min(1)
|
|
31
|
+
.max(600)
|
|
32
|
+
.optional()
|
|
33
|
+
.describe("Seconds before the command is killed. Default 120, maximum 600."),
|
|
34
|
+
}),
|
|
35
|
+
effects: "write",
|
|
36
|
+
execute: runtimeOnly,
|
|
37
|
+
}),
|
|
38
|
+
tool({
|
|
39
|
+
name: "read",
|
|
40
|
+
description: `Read a text file from the sandbox. ${PATH_NOTE} Returns numbered lines. ` +
|
|
41
|
+
"Use offset and limit to page through large files.",
|
|
42
|
+
inputSchema: z.object({
|
|
43
|
+
path: z.string().min(1),
|
|
44
|
+
offset: z
|
|
45
|
+
.number()
|
|
46
|
+
.int()
|
|
47
|
+
.min(1)
|
|
48
|
+
.optional()
|
|
49
|
+
.describe("First line to return, 1-based."),
|
|
50
|
+
limit: z
|
|
51
|
+
.number()
|
|
52
|
+
.int()
|
|
53
|
+
.min(1)
|
|
54
|
+
.max(5000)
|
|
55
|
+
.optional()
|
|
56
|
+
.describe("Maximum number of lines. Default 2000."),
|
|
57
|
+
}),
|
|
58
|
+
effects: "read",
|
|
59
|
+
execute: runtimeOnly,
|
|
60
|
+
}),
|
|
61
|
+
tool({
|
|
62
|
+
name: "write",
|
|
63
|
+
description: `Create or overwrite a text file in the sandbox. ${PATH_NOTE} Parent directories are created.`,
|
|
64
|
+
inputSchema: z.object({
|
|
65
|
+
path: z.string().min(1),
|
|
66
|
+
content: z.string(),
|
|
67
|
+
}),
|
|
68
|
+
effects: "write",
|
|
69
|
+
execute: runtimeOnly,
|
|
70
|
+
}),
|
|
71
|
+
tool({
|
|
72
|
+
name: "edit",
|
|
73
|
+
description: `Replace text in a sandbox file. ${PATH_NOTE} old_string must match exactly and be unique ` +
|
|
74
|
+
"unless replace_all is true.",
|
|
75
|
+
inputSchema: z.object({
|
|
76
|
+
path: z.string().min(1),
|
|
77
|
+
old_string: z.string().min(1),
|
|
78
|
+
new_string: z.string(),
|
|
79
|
+
replace_all: z.boolean().optional(),
|
|
80
|
+
}),
|
|
81
|
+
effects: "write",
|
|
82
|
+
execute: runtimeOnly,
|
|
83
|
+
}),
|
|
84
|
+
tool({
|
|
85
|
+
name: "grep",
|
|
86
|
+
description: `Search file contents in the sandbox with an extended regular expression. ${PATH_NOTE} ` +
|
|
87
|
+
"Returns matching lines as path:line:text.",
|
|
88
|
+
inputSchema: z.object({
|
|
89
|
+
pattern: z.string().min(1),
|
|
90
|
+
path: z.string().min(1).optional().describe(`Directory or file to search. Default ${SANDBOX_WORKSPACE}.`),
|
|
91
|
+
glob: z.string().min(1).optional().describe("Only search files whose name matches, e.g. \"*.py\"."),
|
|
92
|
+
ignore_case: z.boolean().optional(),
|
|
93
|
+
max_results: z.number().int().min(1).max(1000).optional().describe("Default 200."),
|
|
94
|
+
}),
|
|
95
|
+
effects: "read",
|
|
96
|
+
execute: runtimeOnly,
|
|
97
|
+
}),
|
|
98
|
+
tool({
|
|
99
|
+
name: "glob",
|
|
100
|
+
description: `Find files in the sandbox by glob pattern, e.g. "**/*.csv". ${PATH_NOTE} Returns matching paths.`,
|
|
101
|
+
inputSchema: z.object({
|
|
102
|
+
pattern: z.string().min(1),
|
|
103
|
+
path: z.string().min(1).optional().describe(`Directory to search. Default ${SANDBOX_WORKSPACE}.`),
|
|
104
|
+
}),
|
|
105
|
+
effects: "read",
|
|
106
|
+
execute: runtimeOnly,
|
|
107
|
+
}),
|
|
108
|
+
]);
|
|
109
|
+
}
|