@nylorun/core 0.1.1-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.
@@ -1,31 +1,44 @@
1
1
  import { HarnessError } from "../errors.js";
2
2
  import { assembleAgent } from "./assemble.js";
3
3
  import { schemaFromJSON } from "./schema-json.js";
4
- import { deepFreeze } from "../utils/immutable.js";
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
- export function agentFrom(json, implementations) {
7
+ export function agentFrom(json, implementations, options) {
7
8
  const manifest = normalizeManifest(json);
9
+ const sessionTools = options?.sessionTools ?? [];
10
+ assertSessionTools(manifest, sessionTools);
8
11
  const entries = [];
9
12
  const dynamics = new Map();
10
13
  for (const capability of manifest.capabilities) {
11
14
  const impl = implementations[capability.id] ?? {};
12
15
  const tools = resolveTools(capability, impl.tools);
13
- if (capability.beforeModelCall && !impl.beforeModelCall)
14
- throw new HarnessError("agent.build-failed", `Missing beforeModelCall implementation for capability '${capability.id}'`);
15
- if (capability.afterModelCall && !impl.afterModelCall)
16
- throw new HarnessError("agent.build-failed", `Missing afterModelCall implementation for capability '${capability.id}'`);
16
+ const session = sessionTools
17
+ .filter((item) => item.capabilityId === capability.id)
18
+ .map((item) => {
19
+ const live = impl.tools?.[item.name];
20
+ if (!live)
21
+ throw new HarnessError("agent.build-failed", `Missing session tool implementation '${item.name}' for capability '${capability.id}'`);
22
+ return live;
23
+ });
24
+ assertHookImplementations(capability, impl);
17
25
  const handle = impl.middleware ??
18
26
  (async (request, next) => {
19
- if (tools.length)
20
- request.configuration.tools.set(capability.id, tools.map((tool) => implementations[capability.id]?.tools?.[tool.name] ?? tool));
27
+ const advertised = [...tools, ...session];
28
+ if (advertised.length)
29
+ request.configuration.tools.set(capability.id, advertised.map((tool) => implementations[capability.id]?.tools?.[tool.name] ?? tool));
21
30
  if (capability.instructions)
22
31
  request.configuration.instructions.set(capability.id, capability.instructions);
23
32
  return next();
24
33
  });
25
34
  entries.push(Object.freeze({
26
35
  id: capability.id,
36
+ ...(capability.name === undefined ? {} : { name: capability.name }),
37
+ ...(capability.description === undefined
38
+ ? {}
39
+ : { description: capability.description }),
27
40
  handle,
28
- hasMiddleware: impl.middleware !== undefined || capability.hasMiddleware,
41
+ hasMiddleware: impl.middleware !== undefined,
29
42
  tools: capability.tools !== undefined
30
43
  ? tools.map((tool) => {
31
44
  const live = impl.tools?.[tool.name];
@@ -34,6 +47,7 @@ export function agentFrom(json, implementations) {
34
47
  return live;
35
48
  })
36
49
  : undefined,
50
+ ...(session.length === 0 ? {} : { sessionTools: session }),
37
51
  contributions: Object.freeze({
38
52
  ...(capability.instructions
39
53
  ? { instructions: capability.instructions }
@@ -49,78 +63,247 @@ export function agentFrom(json, implementations) {
49
63
  }
50
64
  : {}),
51
65
  }),
52
- ...(capability.beforeModelCall ? { beforeModelCall: true } : {}),
53
- ...(capability.afterModelCall ? { afterModelCall: true } : {}),
66
+ ...(capability.hooks === undefined ? {} : { hooks: capability.hooks }),
67
+ manifestType: capability.type,
68
+ ...(capability.metadata === undefined
69
+ ? {}
70
+ : { metadata: capability.metadata }),
71
+ ...(capability.mcpServers === undefined
72
+ ? {}
73
+ : { mcpServers: capability.mcpServers }),
74
+ ...(capability.sandbox === undefined
75
+ ? {}
76
+ : { sandbox: capability.sandbox }),
77
+ ...(capability.skills === undefined
78
+ ? {}
79
+ : { skills: capability.skills }),
54
80
  }));
55
81
  dynamics.set(capability.id, {
56
- ...(impl.beforeModelCall
57
- ? { beforeModelCall: impl.beforeModelCall }
58
- : {}),
59
- ...(impl.afterModelCall
60
- ? { afterModelCall: impl.afterModelCall }
61
- : {}),
82
+ ...(impl.before ? { before: impl.before } : {}),
83
+ ...(impl.after ? { after: impl.after } : {}),
62
84
  ...(impl.middleware ? { middleware: impl.middleware } : {}),
63
85
  });
64
86
  }
65
87
  const outputSchema = manifest.outputSchema
66
88
  ? schemaFromJSON(manifest.outputSchema)
67
89
  : undefined;
68
- const result = assembleAgent(entries, { id: manifest.id, name: manifest.name, outputSchema }, dynamics);
90
+ const result = assembleAgent(entries, {
91
+ id: manifest.id,
92
+ name: manifest.name,
93
+ description: manifest.description,
94
+ metadata: manifest.metadata,
95
+ outputSchema,
96
+ runtime: manifest.runtime,
97
+ }, dynamics);
69
98
  if (!result.ok)
70
99
  throw new HarnessError("agent.build-failed", result.diagnostics.map((item) => item.message).join("; "));
71
100
  return result.agent;
72
101
  }
102
+ function assertSessionTools(manifest, sessionTools) {
103
+ const seen = new Set();
104
+ for (const item of sessionTools) {
105
+ if (seen.has(item.name))
106
+ throw new HarnessError("agent.build-failed", `Duplicate session tool '${item.name}'`);
107
+ seen.add(item.name);
108
+ const capability = manifest.capabilities.find((entry) => entry.id === item.capabilityId);
109
+ if (!capability)
110
+ throw new HarnessError("agent.build-failed", `Session tool '${item.name}' references unknown capability '${item.capabilityId}'`);
111
+ if (capability.tools?.some((tool) => tool.name === item.name))
112
+ throw new HarnessError("agent.build-failed", `Session tool '${item.name}' collides with a declared tool`);
113
+ }
114
+ }
73
115
  function resolveTools(capability, tools) {
74
116
  if (!capability.tools?.length)
75
117
  return [];
76
- return capability.tools.map((tool) => {
77
- const live = tools?.[tool.name];
78
- if (!live)
79
- throw new HarnessError("agent.build-failed", `Missing tool implementation '${tool.name}' for capability '${capability.id}'`);
80
- return live;
118
+ return capability.tools.flatMap((declared) => {
119
+ const live = tools?.[declared.name];
120
+ if (live)
121
+ return [live];
122
+ if ((declared.name === "load_skill" || declared.name === "read_skill_resource") &&
123
+ capability.skills &&
124
+ Object.keys(capability.skills).length > 0)
125
+ return [];
126
+ throw new HarnessError("agent.build-failed", `Missing tool implementation '${declared.name}' for capability '${capability.id}'`);
81
127
  });
82
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
+ }
83
139
  function normalizeManifest(json) {
84
140
  if (!json || typeof json !== "object" || Array.isArray(json))
85
141
  throw new HarnessError("agent.build-failed", "Agent.from requires a manifest object");
86
142
  const value = json;
87
143
  if (typeof value.id !== "string" || !value.id)
88
144
  throw new HarnessError("agent.build-failed", "Manifest id must be a non-empty string");
89
- if (typeof value.name !== "string" || !value.name)
90
- throw new HarnessError("agent.build-failed", "Manifest name must be a non-empty string");
145
+ if ("name" in value && value.name !== undefined) {
146
+ if (typeof value.name !== "string" || !value.name)
147
+ throw new HarnessError("agent.build-failed", "Manifest name must be a non-empty string");
148
+ }
91
149
  if (!Array.isArray(value.capabilities))
92
150
  throw new HarnessError("agent.build-failed", "Manifest capabilities must be an array");
93
- const schemaVersion = value.schemaVersion === undefined ? 2 : value.schemaVersion;
94
- if (schemaVersion !== 2)
95
- throw new HarnessError("agent.build-failed", `Unsupported manifest schemaVersion ${schemaVersion}`);
151
+ if ("schemaVersion" in value && value.schemaVersion !== undefined)
152
+ throw new HarnessError("agent.build-failed", "Manifest field schemaVersion was renamed to manifestSchemaVersion");
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)}`);
96
157
  // Reject top-level model (Runtime-owned).
97
158
  if ("model" in value && value.model !== undefined)
98
159
  throw new HarnessError("agent.build-failed", "Manifest must not include top-level model; Runtime owns model resolution");
160
+ if ("description" in value && value.description !== undefined) {
161
+ if (typeof value.description !== "string")
162
+ throw new HarnessError("agent.build-failed", "Manifest description must be a string");
163
+ }
164
+ const metadata = "metadata" in value && value.metadata !== undefined
165
+ ? copyJsonObject(value.metadata, "metadata")
166
+ : undefined;
167
+ const runtime = normalizeRuntime(value.runtime);
168
+ const capabilities = value.capabilities.map(normalizeCapability);
169
+ const servers = new Set();
170
+ for (const capability of capabilities)
171
+ for (const name of Object.keys(capability.mcpServers ?? {})) {
172
+ if (servers.has(name))
173
+ throw new HarnessError("agent.build-failed", `Duplicate MCP server '${name}'`);
174
+ servers.add(name);
175
+ }
99
176
  return deepFreeze({
100
- schemaVersion: 2,
177
+ manifestSchemaVersion: 4,
101
178
  id: value.id,
102
- name: value.name,
179
+ ...(typeof value.name === "string" && value.name ? { name: value.name } : {}),
180
+ ...(typeof value.description === "string"
181
+ ? { description: value.description }
182
+ : {}),
183
+ ...(metadata === undefined ? {} : { metadata }),
103
184
  ...(value.outputSchema && typeof value.outputSchema === "object"
104
185
  ? { outputSchema: value.outputSchema }
105
186
  : {}),
106
- capabilities: value.capabilities.map(normalizeCapability),
187
+ capabilities,
188
+ ...(runtime === undefined ? {} : { runtime }),
107
189
  });
108
190
  }
191
+ function normalizeRuntime(value) {
192
+ if (value === undefined)
193
+ return undefined;
194
+ if (!value ||
195
+ typeof value !== "object" ||
196
+ Array.isArray(value) ||
197
+ Object.keys(value).length > 0)
198
+ throw new HarnessError("agent.build-failed", "runtime must be an empty object when present");
199
+ return {};
200
+ }
109
201
  function normalizeCapability(capability) {
202
+ const raw = capability;
203
+ if ("name" in raw && raw.name !== undefined) {
204
+ if (typeof raw.name !== "string" || !raw.name)
205
+ throw new HarnessError("agent.build-failed", `Capability '${capability.id}' name must be a non-empty string`);
206
+ }
207
+ if ("description" in raw && raw.description !== undefined) {
208
+ if (typeof raw.description !== "string")
209
+ throw new HarnessError("agent.build-failed", `Capability '${capability.id}' description must be a string`);
210
+ }
211
+ if ("model" in raw && raw.model !== undefined)
212
+ throw new HarnessError("agent.build-failed", `Capability '${capability.id}' must not include model; Runtime owns model resolution`);
213
+ if (capability.type !== "agent" && capability.type !== "agent-plugin")
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
+ }
223
+ const metadata = capability.metadata === undefined
224
+ ? undefined
225
+ : copyJsonObject(capability.metadata, "metadata");
226
+ const mcpServers = normalizeMcpServers(capability.id, capability.mcpServers);
227
+ const skills = normalizeSkills(capability.id, capability.skills);
110
228
  return Object.freeze({
111
229
  id: capability.id,
112
- kind: capability.kind ?? "capability",
113
- hasMiddleware: capability.hasMiddleware ?? false,
230
+ type: capability.type,
231
+ ...(typeof raw.name === "string" && raw.name ? { name: raw.name } : {}),
232
+ ...(typeof raw.description === "string"
233
+ ? { description: raw.description }
234
+ : {}),
235
+ ...(metadata === undefined ? {} : { metadata }),
114
236
  ...(capability.instructions
115
237
  ? { instructions: Object.freeze([...capability.instructions]) }
116
238
  : {}),
117
239
  ...(capability.tools
118
240
  ? { tools: Object.freeze(capability.tools.map(normalizeTool)) }
119
241
  : {}),
120
- ...(capability.beforeModelCall ? { beforeModelCall: true } : {}),
121
- ...(capability.afterModelCall ? { afterModelCall: true } : {}),
242
+ ...(skills === undefined ? {} : { skills }),
243
+ ...(mcpServers === undefined ? {} : { mcpServers }),
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
+ }),
122
254
  });
123
255
  }
256
+ function normalizeSkills(capabilityId, skills) {
257
+ if (skills === undefined)
258
+ return undefined;
259
+ if (!skills ||
260
+ typeof skills !== "object" ||
261
+ Array.isArray(skills) ||
262
+ Object.keys(skills).length === 0)
263
+ throw new HarnessError("agent.build-failed", `Capability '${capabilityId}' skills must be a non-empty map or omitted`);
264
+ const normalized = {};
265
+ for (const [key, skill] of Object.entries(skills)) {
266
+ if (!skill ||
267
+ typeof skill !== "object" ||
268
+ skill.name !== key ||
269
+ typeof skill.description !== "string" ||
270
+ skill.description.length === 0)
271
+ throw new HarnessError("agent.build-failed", `Capability '${capabilityId}' skills key '${key}' must equal the skill name`);
272
+ normalized[key] = Object.freeze({
273
+ name: skill.name,
274
+ description: skill.description,
275
+ });
276
+ }
277
+ return Object.freeze(normalized);
278
+ }
279
+ function normalizeMcpServers(capabilityId, servers) {
280
+ if (servers === undefined)
281
+ return undefined;
282
+ if (!servers ||
283
+ typeof servers !== "object" ||
284
+ Array.isArray(servers) ||
285
+ Object.keys(servers).length === 0)
286
+ throw new HarnessError("agent.build-failed", `Capability '${capabilityId}' mcpServers must be a non-empty map or omitted`);
287
+ const normalized = {};
288
+ for (const [key, server] of Object.entries(servers)) {
289
+ if (!isMcpServer(server) || server.name !== key)
290
+ throw new HarnessError("agent.build-failed", `Capability '${capabilityId}' mcpServers key '${key}' must equal the server name`);
291
+ normalized[key] = server;
292
+ }
293
+ return Object.freeze(normalized);
294
+ }
295
+ function isMcpServer(value) {
296
+ if (!value || typeof value !== "object" || Array.isArray(value))
297
+ return false;
298
+ const server = value;
299
+ if (typeof server.name !== "string" || !server.name)
300
+ return false;
301
+ if (server.type === "stdio")
302
+ return typeof server.command === "string" && server.command.length > 0;
303
+ if (server.type === "streamable-http" || server.type === "sse")
304
+ return typeof server.url === "string" && server.url.length > 0;
305
+ return false;
306
+ }
124
307
  function normalizeTool(tool) {
125
308
  return Object.freeze({
126
309
  name: tool.name,
@@ -4,16 +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 `beforeModelCall` / `afterModelCall`. Kept through 1.0. */
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 dynamics). */
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
+ readonly name?: string;
13
+ readonly description?: string;
12
14
  readonly tools?: readonly ToolDefinition<any, Info, any>[];
13
15
  readonly instructions?: string | readonly string[];
14
- readonly beforeModelCall?: import("../types/dynamics.js").BeforeModelCallFn<Info>;
15
- readonly afterModelCall?: import("../types/dynamics.js").AfterModelCallFn<Info>;
16
- /** @deprecated Prefer beforeModelCall / afterModelCall. */
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. */
17
21
  readonly middleware?: StepMiddleware<Info>;
18
22
  /** @deprecated Model resolution is Runtime-owned; not projected into the manifest. */
19
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 `beforeModelCall` / `afterModelCall`. Kept through 1.0. */
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 dynamics). */
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
@@ -13,14 +13,14 @@ export function capability(declaration) {
13
13
  : declaration.instructions;
14
14
  return {
15
15
  id: declaration.id,
16
+ ...(declaration.name === undefined ? {} : { name: declaration.name }),
17
+ ...(declaration.description === undefined
18
+ ? {}
19
+ : { description: declaration.description }),
16
20
  ...(declaration.tools === undefined ? {} : { tools: declaration.tools }),
17
21
  ...(instructions === undefined ? {} : { instructions }),
18
- ...(declaration.beforeModelCall === undefined
19
- ? {}
20
- : { beforeModelCall: declaration.beforeModelCall }),
21
- ...(declaration.afterModelCall === undefined
22
- ? {}
23
- : { afterModelCall: declaration.afterModelCall }),
22
+ ...(declaration.before === undefined ? {} : { before: declaration.before }),
23
+ ...(declaration.after === undefined ? {} : { after: declaration.after }),
24
24
  ...(declaration.middleware === undefined
25
25
  ? {}
26
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 { AfterModelCallFn, BeforeModelCallFn } from "../types/dynamics.js";
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 beforeModelCall?: BeforeModelCallFn<Info>;
11
- readonly afterModelCall?: AfterModelCallFn<Info>;
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
  };
@@ -1,9 +1,13 @@
1
1
  import type { BoundMiddleware } from "./bound.js";
2
- import type { AgentManifest } from "../types/manifest.js";
2
+ import type { AgentManifest, RuntimeManifest } from "../types/manifest.js";
3
+ import type { JsonObject } from "../types/shared.js";
3
4
  import type { ToolSchemaSource } from "../types/tool.js";
4
5
  export declare function createManifest(input: {
5
6
  id: string;
6
- name: string;
7
+ name?: string;
8
+ description?: string;
9
+ metadata?: JsonObject;
7
10
  outputSchema?: ToolSchemaSource;
11
+ runtime?: RuntimeManifest;
8
12
  middleware: readonly BoundMiddleware[];
9
13
  }): AgentManifest;
@@ -1,12 +1,18 @@
1
1
  import { bindOutputContract } from "./output-contract.js";
2
2
  import { normalizedSchemasFor } from "./schema.js";
3
- import { deepFreeze } from "../utils/immutable.js";
3
+ 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
- schemaVersion: 2,
7
+ manifestSchemaVersion: 4,
8
8
  id: input.id,
9
- name: input.name,
9
+ ...(input.name === undefined ? {} : { name: input.name }),
10
+ ...(input.description === undefined
11
+ ? {}
12
+ : { description: input.description }),
13
+ ...(input.metadata === undefined
14
+ ? {}
15
+ : { metadata: copyJsonObject(input.metadata, "metadata") }),
10
16
  ...(input.outputSchema === undefined
11
17
  ? {}
12
18
  : {
@@ -14,34 +20,35 @@ export function createManifest(input) {
14
20
  .jsonSchema,
15
21
  }),
16
22
  capabilities,
23
+ ...(input.runtime === undefined ? {} : { runtime: input.runtime }),
17
24
  });
18
25
  }
19
26
  function projectCapability(item) {
20
27
  const contributions = item.contributions;
21
28
  return {
22
29
  id: item.id,
23
- kind: capabilityKind(item),
24
- hasMiddleware: item.hasMiddleware,
30
+ type: item.manifestType ?? "agent",
31
+ ...(item.name === undefined ? {} : { name: item.name }),
32
+ ...(item.description === undefined ? {} : { description: item.description }),
33
+ ...(item.metadata === undefined
34
+ ? {}
35
+ : { metadata: copyJsonObject(item.metadata, "metadata") }),
25
36
  ...(contributions?.instructions === undefined
26
37
  ? {}
27
38
  : { instructions: contributions.instructions }),
28
39
  ...(item.tools === undefined
29
40
  ? {}
30
41
  : { tools: item.tools.map((tool) => projectTool(tool)) }),
31
- ...(item.beforeModelCall ? { beforeModelCall: true } : {}),
32
- ...(item.afterModelCall ? { afterModelCall: true } : {}),
33
- // Deliberately omit capability.model from new manifests (Runtime-owned).
42
+ ...(item.skills === undefined ? {} : { skills: item.skills }),
43
+ ...(item.mcpServers === undefined ? {} : { mcpServers: item.mcpServers }),
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 })) }),
34
50
  };
35
51
  }
36
- function capabilityKind(item) {
37
- if (item.id === "agent")
38
- return "agent";
39
- if (item.contributions !== undefined ||
40
- item.beforeModelCall ||
41
- item.afterModelCall)
42
- return "capability";
43
- return "middleware";
44
- }
45
52
  function projectTool(tool) {
46
53
  const schemas = normalizedSchemasFor(tool);
47
54
  return {
@@ -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[];