@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,13 +1,15 @@
1
1
  import { bindTool } from "./bind-tool.js";
2
2
  import { HarnessError } from "../errors.js";
3
3
  import { bindOutputContract } from "./output-contract.js";
4
- import { hashManifest } from "../utils/hash.js";
5
4
  export function bindAgent(middleware, manifest, options = {}, dynamics = new Map()) {
6
5
  const seen = new Set();
7
6
  const keys = new Set();
8
7
  const tools = [];
9
8
  for (const declaration of middleware)
10
- for (const tool of declaration.tools ?? []) {
9
+ for (const tool of [
10
+ ...(declaration.tools ?? []),
11
+ ...(declaration.sessionTools ?? []),
12
+ ]) {
11
13
  const key = JSON.stringify([declaration.id, tool.name]);
12
14
  if (seen.has(tool) || keys.has(key))
13
15
  throw new HarnessError("tool.invalid", `Duplicate registered tool '${tool.name}' in '${declaration.id}'`);
@@ -32,7 +34,6 @@ export function bindAgent(middleware, manifest, options = {}, dynamics = new Map
32
34
  id: manifest.id,
33
35
  name: manifest.name,
34
36
  manifest,
35
- hash: hashManifest(manifest),
36
37
  toJSON: () => manifest,
37
38
  };
38
39
  Object.defineProperty(agent, "getBinding", {
@@ -52,8 +53,8 @@ function buildImplementations(middleware, dynamics, snapshots) {
52
53
  ...(Object.keys(tools).length
53
54
  ? { tools: Object.freeze({ ...tools }) }
54
55
  : {}),
55
- ...(dyn?.beforeModelCall ? { beforeModelCall: dyn.beforeModelCall } : {}),
56
- ...(dyn?.afterModelCall ? { afterModelCall: dyn.afterModelCall } : {}),
56
+ ...(dyn?.before ? { before: Object.freeze({ ...dyn.before }) } : {}),
57
+ ...(dyn?.after ? { after: Object.freeze({ ...dyn.after }) } : {}),
57
58
  ...(dyn?.middleware ? { middleware: dyn.middleware } : {}),
58
59
  });
59
60
  }
@@ -1,3 +1,7 @@
1
+ import type { JsonObject } from "../types/shared.js";
2
+ import type { McpServerManifest, SandboxManifest, SkillManifest } from "../types/manifest.js";
3
+ import type { SkillRecord } from "../types/middleware.js";
4
+ import type { HookManifest } from "../types/dynamics.js";
1
5
  import type { MiddlewareContributions, StepMiddleware } from "../types/middleware.js";
2
6
  import type { ToolApproval, ToolDefinition, ToolEffects, ToolExecutionContext, ToolInputSchema, ToolOutputSchema, ToolOwner, ToolRunResult, ToolSchema } from "../types/tool.js";
3
7
  export interface BoundToolDefinition<Info = unknown> extends Omit<ToolDefinition<ToolInputSchema, Info, ToolOutputSchema | undefined>, "inputSchema" | "outputSchema" | "execute" | "run" | "input" | "output"> {
@@ -11,10 +15,22 @@ export interface BoundToolDefinition<Info = unknown> extends Omit<ToolDefinition
11
15
  }
12
16
  export interface BoundMiddleware {
13
17
  readonly id: string;
18
+ readonly name?: string;
19
+ readonly description?: string;
14
20
  readonly handle: StepMiddleware;
15
21
  readonly hasMiddleware: boolean;
16
22
  readonly tools?: readonly ToolDefinition<any, any, any>[];
23
+ /** Execution-only tools. Advertised for this run and omitted from the manifest. */
24
+ readonly sessionTools?: readonly ToolDefinition<any, any, any>[];
17
25
  readonly contributions?: MiddlewareContributions;
18
- readonly beforeModelCall?: boolean;
19
- readonly afterModelCall?: boolean;
26
+ /** Registered hook points, in canonical order. */
27
+ readonly hooks?: readonly HookManifest[];
28
+ readonly manifestType?: "agent" | "agent-plugin";
29
+ readonly metadata?: JsonObject;
30
+ readonly skills?: Readonly<Record<string, SkillManifest>>;
31
+ readonly skillRecords?: Readonly<Record<string, SkillRecord>>;
32
+ readonly mcpServers?: Readonly<Record<string, McpServerManifest>>;
33
+ readonly sandbox?: SandboxManifest;
34
+ /** Package directory for a plugin capability. Not a manifest field. */
35
+ readonly pluginRoot?: string;
20
36
  }
@@ -1,29 +1,33 @@
1
1
  import type { BoundMiddleware } from "./bound.js";
2
2
  import type { CapabilityDeclaration, StepMiddleware } from "../types/middleware.js";
3
- import type { BuildDiagnostic } from "../types/shared.js";
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
6
  import type { BuiltAgent } from "../types/agent.js";
7
7
  import type { AgentManifest } from "../types/manifest.js";
8
- import type { AfterModelCallFn, BeforeModelCallFn } from "../types/dynamics.js";
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> {
12
12
  readonly outputSchema?: Schema;
13
13
  readonly id: string;
14
- readonly name: string;
14
+ readonly name?: string;
15
+ readonly description?: string;
16
+ readonly metadata?: JsonObject;
15
17
  readonly instructions?: string | readonly string[];
16
18
  /** Top-level tools (H2). Composed as capability id `"agent"`. */
17
19
  readonly tools?: readonly ToolDefinition<any, any, any>[];
18
20
  }
19
21
  interface BuilderSnapshot {
20
22
  readonly id: string;
21
- readonly name: string;
23
+ readonly name?: string;
24
+ readonly description?: string;
25
+ readonly metadata?: JsonObject;
22
26
  readonly outputSchema?: ToolSchemaSource;
23
27
  readonly entries: readonly BoundMiddleware[];
24
28
  readonly dynamics: ReadonlyMap<string, CapabilityDynamics>;
25
- readonly agentBefore?: BeforeModelCallFn;
26
- readonly agentAfter?: AfterModelCallFn;
29
+ /** Builder-level problems, reported with the assembly diagnostics. */
30
+ readonly diagnostics?: readonly BuildDiagnostic[];
27
31
  }
28
32
  export declare class AgentBuildError extends HarnessError {
29
33
  readonly diagnostics: readonly BuildDiagnostic[];
@@ -47,15 +51,22 @@ export declare class AgentBuilder<Info = unknown, Schema extends ToolSchemaSourc
47
51
  private static withSnapshot;
48
52
  /** Compose-time identity — available before `.build()`. */
49
53
  get id(): string;
50
- get name(): string;
54
+ get name(): string | undefined;
51
55
  get manifest(): AgentManifest;
52
- get hash(): string;
53
56
  toJSON(): AgentManifest;
54
57
  use(middleware: StepMiddleware<Info>): AgentBuilder<Info, Schema>;
55
58
  use(id: string, middleware: StepMiddleware<Info>): AgentBuilder<Info, Schema>;
56
59
  use(declaration: CapabilityDeclaration<Info>): AgentBuilder<Info, Schema>;
57
- beforeModelCall(fn: BeforeModelCallFn<Info>): AgentBuilder<Info, Schema>;
58
- afterModelCall(fn: AfterModelCallFn<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;
59
70
  /** Optional no-op: returns the assembled agent facade. */
60
71
  build(): BuiltAgent<Info, Schema extends ToolSchemaSource ? SchemaOutput<Schema> : string>;
61
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) {
@@ -55,9 +56,6 @@ export class AgentBuilder {
55
56
  get manifest() {
56
57
  return this.ensure().manifest;
57
58
  }
58
- get hash() {
59
- return this.ensure().hash;
60
- }
61
59
  toJSON() {
62
60
  return this.ensure().toJSON();
63
61
  }
@@ -88,12 +86,8 @@ export class AgentBuilder {
88
86
  }
89
87
  const dynamics = new Map(this.#snapshot.dynamics);
90
88
  dynamics.set(compiled.bound.id, {
91
- ...(compiled.beforeModelCall
92
- ? { beforeModelCall: compiled.beforeModelCall }
93
- : {}),
94
- ...(compiled.afterModelCall
95
- ? { afterModelCall: compiled.afterModelCall }
96
- : {}),
89
+ ...(compiled.before ? { before: compiled.before } : {}),
90
+ ...(compiled.after ? { after: compiled.after } : {}),
97
91
  ...(compiled.middleware ? { middleware: compiled.middleware } : {}),
98
92
  });
99
93
  return AgentBuilder.withSnapshot({
@@ -102,34 +96,47 @@ export class AgentBuilder {
102
96
  dynamics,
103
97
  });
104
98
  }
105
- beforeModelCall(fn) {
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"`);
106
117
  const dynamics = new Map(this.#snapshot.dynamics);
107
118
  const existing = dynamics.get("agent") ?? {};
108
- dynamics.set("agent", {
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 = {
109
132
  ...existing,
110
- beforeModelCall: fn,
111
- });
112
- const entries = ensureAgentCapabilityFlag(this.#snapshot.entries, "beforeModelCall");
133
+ [at]: Object.freeze({ ...current, [scope]: fn }),
134
+ };
135
+ dynamics.set("agent", next);
113
136
  return AgentBuilder.withSnapshot({
114
137
  ...this.#snapshot,
115
- entries,
138
+ entries: withAgentHooks(this.#snapshot.entries, next),
116
139
  dynamics,
117
- agentBefore: fn,
118
- });
119
- }
120
- afterModelCall(fn) {
121
- const dynamics = new Map(this.#snapshot.dynamics);
122
- const existing = dynamics.get("agent") ?? {};
123
- dynamics.set("agent", {
124
- ...existing,
125
- afterModelCall: fn,
126
- });
127
- const entries = ensureAgentCapabilityFlag(this.#snapshot.entries, "afterModelCall");
128
- return AgentBuilder.withSnapshot({
129
- ...this.#snapshot,
130
- entries,
131
- dynamics,
132
- agentAfter: fn,
133
140
  });
134
141
  }
135
142
  /** Optional no-op: returns the assembled agent facade. */
@@ -141,9 +148,15 @@ export class AgentBuilder {
141
148
  return this.#agent;
142
149
  if (this.#error)
143
150
  throw this.#error;
151
+ if (this.#snapshot.diagnostics?.length) {
152
+ this.#error = new AgentBuildError(this.#snapshot.diagnostics);
153
+ throw this.#error;
154
+ }
144
155
  const result = assembleAgent(this.#snapshot.entries, {
145
156
  id: this.#snapshot.id,
146
157
  name: this.#snapshot.name,
158
+ description: this.#snapshot.description,
159
+ metadata: this.#snapshot.metadata,
147
160
  outputSchema: this.#snapshot.outputSchema,
148
161
  }, this.#snapshot.dynamics);
149
162
  if (!result.ok) {
@@ -173,7 +186,11 @@ function createSnapshot(options) {
173
186
  }
174
187
  return {
175
188
  id: options.id,
176
- name: options.name,
189
+ ...(options.name === undefined ? {} : { name: options.name }),
190
+ ...(options.description === undefined
191
+ ? {}
192
+ : { description: options.description }),
193
+ ...(options.metadata === undefined ? {} : { metadata: options.metadata }),
177
194
  outputSchema: options.outputSchema,
178
195
  entries: Object.freeze(entries),
179
196
  dynamics,
@@ -189,13 +206,12 @@ function nextMiddlewareId(entries) {
189
206
  } while (taken.has(id));
190
207
  return id;
191
208
  }
192
- function ensureAgentCapabilityFlag(entries, flag) {
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);
193
212
  const index = entries.findIndex((item) => item.id === "agent");
194
213
  if (index >= 0) {
195
- const current = entries[index];
196
- if (current[flag])
197
- return entries;
198
- const next = Object.freeze({ ...current, [flag]: true });
214
+ const next = Object.freeze({ ...entries[index], hooks });
199
215
  return Object.freeze([
200
216
  ...entries.slice(0, index),
201
217
  next,
@@ -209,7 +225,7 @@ function ensureAgentCapabilityFlag(entries, flag) {
209
225
  handle: async (_request, next) => next(),
210
226
  hasMiddleware: false,
211
227
  contributions: Object.freeze({}),
212
- [flag]: true,
228
+ hooks,
213
229
  }),
214
230
  ]);
215
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 { AfterModelCallFn, BeforeModelCallFn } from "../types/dynamics.js";
3
+ import type { AfterHooks, BeforeHooks } from "../types/dynamics.js";
4
4
  export interface CompiledCapability {
5
5
  readonly bound: BoundMiddleware;
6
- readonly beforeModelCall?: BeforeModelCallFn<any>;
7
- readonly afterModelCall?: AfterModelCallFn<any>;
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)
@@ -17,19 +19,40 @@ export function compileDeclaration(declaration) {
17
19
  return {
18
20
  bound: {
19
21
  id: declaration.id,
22
+ ...(declaration.name === undefined ? {} : { name: declaration.name }),
23
+ ...(declaration.description === undefined
24
+ ? {}
25
+ : { description: declaration.description }),
20
26
  handle,
21
27
  hasMiddleware: declaration.middleware !== undefined,
22
28
  tools: tools?.items,
23
29
  ...(contributions === undefined ? {} : { contributions }),
24
- ...(declaration.beforeModelCall ? { beforeModelCall: true } : {}),
25
- ...(declaration.afterModelCall ? { afterModelCall: true } : {}),
30
+ ...(hooks === undefined ? {} : { hooks }),
31
+ ...(declaration.type === undefined ? {} : { manifestType: declaration.type }),
32
+ ...(declaration.metadata === undefined
33
+ ? {}
34
+ : { metadata: declaration.metadata }),
35
+ ...(declaration.skills === undefined ||
36
+ Object.keys(declaration.skills).length === 0
37
+ ? {}
38
+ : { skills: declaration.skills }),
39
+ ...(declaration.skillRecords === undefined ||
40
+ Object.keys(declaration.skillRecords).length === 0
41
+ ? {}
42
+ : { skillRecords: declaration.skillRecords }),
43
+ ...(declaration.mcpServers === undefined ||
44
+ Object.keys(declaration.mcpServers).length === 0
45
+ ? {}
46
+ : { mcpServers: declaration.mcpServers }),
47
+ ...(declaration.sandbox === undefined
48
+ ? {}
49
+ : { sandbox: declaration.sandbox }),
50
+ ...(declaration.pluginRoot === undefined
51
+ ? {}
52
+ : { pluginRoot: declaration.pluginRoot }),
26
53
  },
27
- ...(declaration.beforeModelCall
28
- ? { beforeModelCall: declaration.beforeModelCall }
29
- : {}),
30
- ...(declaration.afterModelCall
31
- ? { afterModelCall: declaration.afterModelCall }
32
- : {}),
54
+ ...(declaration.before === undefined ? {} : { before: declaration.before }),
55
+ ...(declaration.after === undefined ? {} : { after: declaration.after }),
33
56
  ...(declaration.middleware ? { middleware: declaration.middleware } : {}),
34
57
  };
35
58
  }
@@ -2,5 +2,12 @@ import type { AgentManifest } from "../types/manifest.js";
2
2
  import type { BuiltAgent } from "../types/agent.js";
3
3
  import type { JsonObject } from "../types/shared.js";
4
4
  import type { Implementations } from "./implementations.js";
5
+ /** A tool that exists for one execution and is not part of the hashed manifest. */
6
+ export interface SessionToolRef {
7
+ readonly capabilityId: string;
8
+ readonly name: string;
9
+ }
5
10
  /** Rebuild an agent from manifest JSON + in-process implementations (T28). */
6
- export declare function agentFrom<Info = unknown>(json: AgentManifest | JsonObject, implementations: Implementations<Info>): BuiltAgent<Info>;
11
+ export declare function agentFrom<Info = unknown>(json: AgentManifest | JsonObject, implementations: Implementations<Info>, options?: {
12
+ readonly sessionTools?: readonly SessionToolRef[];
13
+ }): BuiltAgent<Info>;