@nylorun/core 0.0.0-bootstrap → 0.2.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 +21 -0
- package/LICENSE +192 -0
- package/README.md +16 -0
- package/dist/compatibility.d.ts +3 -0
- package/dist/compatibility.js +3 -0
- package/dist/contracts.d.ts +330 -0
- package/dist/contracts.js +366 -0
- package/dist/define.d.ts +32 -0
- package/dist/define.js +15 -0
- package/dist/definition/assemble.d.ts +29 -0
- package/dist/definition/assemble.js +120 -0
- package/dist/definition/bind-agent.d.ts +8 -0
- package/dist/definition/bind-agent.js +62 -0
- package/dist/definition/bind-tool.d.ts +4 -0
- package/dist/definition/bind-tool.js +25 -0
- package/dist/definition/binding.d.ts +15 -0
- package/dist/definition/binding.js +9 -0
- package/dist/definition/bound.d.ts +34 -0
- package/dist/definition/bound.js +1 -0
- package/dist/definition/builder.d.ts +66 -0
- package/dist/definition/builder.js +218 -0
- package/dist/definition/declaration.d.ts +10 -0
- package/dist/definition/declaration.js +93 -0
- package/dist/definition/from.d.ts +13 -0
- package/dist/definition/from.js +294 -0
- package/dist/definition/helpers.d.ts +23 -0
- package/dist/definition/helpers.js +33 -0
- package/dist/definition/implementations.d.ts +16 -0
- package/dist/definition/implementations.js +3 -0
- package/dist/definition/manifest.d.ts +13 -0
- package/dist/definition/manifest.js +60 -0
- package/dist/definition/output-contract.d.ts +6 -0
- package/dist/definition/output-contract.js +12 -0
- package/dist/definition/schema-json.d.ts +3 -0
- package/dist/definition/schema-json.js +22 -0
- package/dist/definition/schema.d.ts +16 -0
- package/dist/definition/schema.js +284 -0
- package/dist/definition/skill-tools.d.ts +3 -0
- package/dist/definition/skill-tools.js +56 -0
- package/dist/definition/tool-error.d.ts +9 -0
- package/dist/definition/tool-error.js +20 -0
- package/dist/errors.d.ts +17 -0
- package/dist/errors.js +22 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +1 -0
- package/dist/types/agent.d.ts +10 -0
- package/dist/types/agent.js +1 -0
- package/dist/types/dynamics.d.ts +53 -0
- package/dist/types/dynamics.js +1 -0
- package/dist/types/manifest.d.ts +57 -0
- package/dist/types/manifest.js +1 -0
- package/dist/types/middleware.d.ts +92 -0
- package/dist/types/middleware.js +1 -0
- package/dist/types/model.d.ts +184 -0
- package/dist/types/model.js +1 -0
- package/dist/types/observe.d.ts +180 -0
- package/dist/types/observe.js +1 -0
- package/dist/types/shared.d.ts +25 -0
- package/dist/types/shared.js +1 -0
- package/dist/types/tool.d.ts +194 -0
- package/dist/types/tool.js +1 -0
- package/dist/types/transcript.d.ts +65 -0
- package/dist/types/transcript.js +1 -0
- package/dist/utils/canonical.d.ts +1 -0
- package/dist/utils/canonical.js +10 -0
- package/dist/utils/hash.d.ts +3 -0
- package/dist/utils/hash.js +7 -0
- package/dist/utils/immutable.d.ts +6 -0
- package/dist/utils/immutable.js +76 -0
- package/package.json +57 -7
- package/index.js +0 -1
- package/publish.out +0 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { AgentManifest } from "../types/manifest.js";
|
|
2
|
+
import type { BuiltAgent } from "../types/agent.js";
|
|
3
|
+
import type { ToolSchemaSource } from "../types/tool.js";
|
|
4
|
+
import type { BoundMiddleware, BoundToolDefinition } from "./bound.js";
|
|
5
|
+
import type { Implementations } from "./implementations.js";
|
|
6
|
+
/** Local code binding. Only manifest is serializable; functions stay in the application. */
|
|
7
|
+
export interface AgentBinding {
|
|
8
|
+
readonly manifest: AgentManifest;
|
|
9
|
+
readonly tools: readonly BoundToolDefinition[];
|
|
10
|
+
readonly declarations: readonly BoundMiddleware[];
|
|
11
|
+
readonly implementations: Implementations;
|
|
12
|
+
readonly outputSchema?: ToolSchemaSource;
|
|
13
|
+
}
|
|
14
|
+
export declare function bindingFromAgent(agent: BuiltAgent): AgentBinding;
|
|
15
|
+
export declare function implementationsFor(agent: BuiltAgent): Implementations;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { HarnessError } from "../errors.js";
|
|
2
|
+
export function bindingFromAgent(agent) {
|
|
3
|
+
if (!agent || typeof agent.getBinding !== "function")
|
|
4
|
+
throw new HarnessError("execution.invalid-input", "An agent with getBinding() is required");
|
|
5
|
+
return agent.getBinding();
|
|
6
|
+
}
|
|
7
|
+
export function implementationsFor(agent) {
|
|
8
|
+
return bindingFromAgent(agent).implementations;
|
|
9
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { JsonObject } from "../types/shared.js";
|
|
2
|
+
import type { McpServerManifest, SkillManifest } from "../types/manifest.js";
|
|
3
|
+
import type { SkillRecord } from "../types/middleware.js";
|
|
4
|
+
import type { MiddlewareContributions, StepMiddleware } from "../types/middleware.js";
|
|
5
|
+
import type { ToolApproval, ToolDefinition, ToolEffects, ToolExecutionContext, ToolInputSchema, ToolOutputSchema, ToolOwner, ToolRunResult, ToolSchema } from "../types/tool.js";
|
|
6
|
+
export interface BoundToolDefinition<Info = unknown> extends Omit<ToolDefinition<ToolInputSchema, Info, ToolOutputSchema | undefined>, "inputSchema" | "outputSchema" | "execute" | "run" | "input" | "output"> {
|
|
7
|
+
readonly inputSchema: ToolSchema<unknown>;
|
|
8
|
+
readonly outputSchema?: ToolSchema<unknown>;
|
|
9
|
+
readonly execute: (args: unknown, context: ToolExecutionContext<Info>) => Promise<ToolRunResult>;
|
|
10
|
+
readonly approval?: ToolApproval<ToolInputSchema>;
|
|
11
|
+
readonly effects?: ToolEffects;
|
|
12
|
+
readonly owner: ToolOwner;
|
|
13
|
+
readonly source: ToolDefinition<any, any, any>;
|
|
14
|
+
}
|
|
15
|
+
export interface BoundMiddleware {
|
|
16
|
+
readonly id: string;
|
|
17
|
+
readonly name?: string;
|
|
18
|
+
readonly description?: string;
|
|
19
|
+
readonly handle: StepMiddleware;
|
|
20
|
+
readonly hasMiddleware: boolean;
|
|
21
|
+
readonly tools?: readonly ToolDefinition<any, any, any>[];
|
|
22
|
+
/** Execution-only tools. Advertised for this run and omitted from the manifest. */
|
|
23
|
+
readonly sessionTools?: readonly ToolDefinition<any, any, any>[];
|
|
24
|
+
readonly contributions?: MiddlewareContributions;
|
|
25
|
+
readonly beforeModelCall?: boolean;
|
|
26
|
+
readonly afterModelCall?: boolean;
|
|
27
|
+
readonly manifestType?: "agent" | "agent-plugin";
|
|
28
|
+
readonly metadata?: JsonObject;
|
|
29
|
+
readonly skills?: Readonly<Record<string, SkillManifest>>;
|
|
30
|
+
readonly skillRecords?: Readonly<Record<string, SkillRecord>>;
|
|
31
|
+
readonly mcpServers?: Readonly<Record<string, McpServerManifest>>;
|
|
32
|
+
/** Package directory for a plugin capability. Not a manifest field. */
|
|
33
|
+
readonly pluginRoot?: string;
|
|
34
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { BoundMiddleware } from "./bound.js";
|
|
2
|
+
import type { CapabilityDeclaration, StepMiddleware } from "../types/middleware.js";
|
|
3
|
+
import type { BuildDiagnostic, JsonObject } from "../types/shared.js";
|
|
4
|
+
import { HarnessError } from "../errors.js";
|
|
5
|
+
import type { ToolDefinition, ToolSchemaSource, SchemaOutput } from "../types/tool.js";
|
|
6
|
+
import type { BuiltAgent } from "../types/agent.js";
|
|
7
|
+
import type { AgentManifest } from "../types/manifest.js";
|
|
8
|
+
import type { AfterModelCallFn, BeforeModelCallFn } from "../types/dynamics.js";
|
|
9
|
+
import type { Implementations } from "./implementations.js";
|
|
10
|
+
import { type CapabilityDynamics } from "./assemble.js";
|
|
11
|
+
export interface AgentOptions<Schema extends ToolSchemaSource | undefined = undefined> {
|
|
12
|
+
readonly outputSchema?: Schema;
|
|
13
|
+
readonly id: string;
|
|
14
|
+
readonly name?: string;
|
|
15
|
+
readonly description?: string;
|
|
16
|
+
readonly metadata?: JsonObject;
|
|
17
|
+
readonly instructions?: string | readonly string[];
|
|
18
|
+
/** Top-level tools (H2). Composed as capability id `"agent"`. */
|
|
19
|
+
readonly tools?: readonly ToolDefinition<any, any, any>[];
|
|
20
|
+
}
|
|
21
|
+
interface BuilderSnapshot {
|
|
22
|
+
readonly id: string;
|
|
23
|
+
readonly name?: string;
|
|
24
|
+
readonly description?: string;
|
|
25
|
+
readonly metadata?: JsonObject;
|
|
26
|
+
readonly outputSchema?: ToolSchemaSource;
|
|
27
|
+
readonly entries: readonly BoundMiddleware[];
|
|
28
|
+
readonly dynamics: ReadonlyMap<string, CapabilityDynamics>;
|
|
29
|
+
readonly agentBefore?: BeforeModelCallFn;
|
|
30
|
+
readonly agentAfter?: AfterModelCallFn;
|
|
31
|
+
}
|
|
32
|
+
export declare class AgentBuildError extends HarnessError {
|
|
33
|
+
readonly diagnostics: readonly BuildDiagnostic[];
|
|
34
|
+
constructor(diagnostics: readonly BuildDiagnostic[]);
|
|
35
|
+
}
|
|
36
|
+
export declare class AgentLifecycleError extends HarnessError {
|
|
37
|
+
constructor(message: string);
|
|
38
|
+
}
|
|
39
|
+
export declare function Agent<Info = unknown, Schema extends ToolSchemaSource | undefined = undefined>(options: AgentOptions<Schema>): AgentBuilder<Info, Schema>;
|
|
40
|
+
export declare namespace Agent {
|
|
41
|
+
function from<Info = unknown>(json: AgentManifest | import("../types/shared.js").JsonObject, implementations: Implementations<Info>): BuiltAgent<Info>;
|
|
42
|
+
}
|
|
43
|
+
declare const SNAPSHOT: unique symbol;
|
|
44
|
+
export declare class AgentBuilder<Info = unknown, Schema extends ToolSchemaSource | undefined = undefined> {
|
|
45
|
+
#private;
|
|
46
|
+
getBinding(): import("./binding.js").AgentBinding;
|
|
47
|
+
constructor(options: AgentOptions<Schema>);
|
|
48
|
+
/** @internal */
|
|
49
|
+
constructor(snapshot: BuilderSnapshot, brand: typeof SNAPSHOT);
|
|
50
|
+
static create<Info, Schema extends ToolSchemaSource | undefined>(options: AgentOptions<Schema>): AgentBuilder<Info, Schema>;
|
|
51
|
+
private static withSnapshot;
|
|
52
|
+
/** Compose-time identity — available before `.build()`. */
|
|
53
|
+
get id(): string;
|
|
54
|
+
get name(): string | undefined;
|
|
55
|
+
get manifest(): AgentManifest;
|
|
56
|
+
toJSON(): AgentManifest;
|
|
57
|
+
use(middleware: StepMiddleware<Info>): AgentBuilder<Info, Schema>;
|
|
58
|
+
use(id: string, middleware: StepMiddleware<Info>): AgentBuilder<Info, Schema>;
|
|
59
|
+
use(declaration: CapabilityDeclaration<Info>): AgentBuilder<Info, Schema>;
|
|
60
|
+
beforeModelCall(fn: BeforeModelCallFn<Info>): AgentBuilder<Info, Schema>;
|
|
61
|
+
afterModelCall(fn: AfterModelCallFn<Info>): AgentBuilder<Info, Schema>;
|
|
62
|
+
/** Optional no-op: returns the assembled agent facade. */
|
|
63
|
+
build(): BuiltAgent<Info, Schema extends ToolSchemaSource ? SchemaOutput<Schema> : string>;
|
|
64
|
+
private ensure;
|
|
65
|
+
}
|
|
66
|
+
export type { Implementations };
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { HarnessError } from "../errors.js";
|
|
2
|
+
import { assembleAgent } from "./assemble.js";
|
|
3
|
+
import { compileDeclaration } from "./declaration.js";
|
|
4
|
+
import { agentFrom } from "./from.js";
|
|
5
|
+
export class AgentBuildError extends HarnessError {
|
|
6
|
+
diagnostics;
|
|
7
|
+
constructor(diagnostics) {
|
|
8
|
+
super("agent.build-failed", diagnostics.map((item) => item.message).join("; ") || "Agent build failed");
|
|
9
|
+
this.diagnostics = diagnostics;
|
|
10
|
+
this.name = "AgentBuildError";
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export class AgentLifecycleError extends HarnessError {
|
|
14
|
+
constructor(message) {
|
|
15
|
+
super("agent.lifecycle-sealed", message);
|
|
16
|
+
this.name = "AgentLifecycleError";
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export function Agent(options) {
|
|
20
|
+
return AgentBuilder.create(options);
|
|
21
|
+
}
|
|
22
|
+
(function (Agent) {
|
|
23
|
+
function from(json, implementations) {
|
|
24
|
+
return agentFrom(json, implementations);
|
|
25
|
+
}
|
|
26
|
+
Agent.from = from;
|
|
27
|
+
})(Agent || (Agent = {}));
|
|
28
|
+
const SNAPSHOT = Symbol("AgentBuilder.snapshot");
|
|
29
|
+
export class AgentBuilder {
|
|
30
|
+
#snapshot;
|
|
31
|
+
#agent;
|
|
32
|
+
#error;
|
|
33
|
+
getBinding() {
|
|
34
|
+
return this.build().getBinding();
|
|
35
|
+
}
|
|
36
|
+
constructor(optionsOrSnapshot, brand) {
|
|
37
|
+
this.#snapshot =
|
|
38
|
+
brand === SNAPSHOT
|
|
39
|
+
? optionsOrSnapshot
|
|
40
|
+
: createSnapshot(optionsOrSnapshot);
|
|
41
|
+
}
|
|
42
|
+
static create(options) {
|
|
43
|
+
return new AgentBuilder(options);
|
|
44
|
+
}
|
|
45
|
+
static withSnapshot(snapshot) {
|
|
46
|
+
return new AgentBuilder(snapshot, SNAPSHOT);
|
|
47
|
+
}
|
|
48
|
+
/** Compose-time identity — available before `.build()`. */
|
|
49
|
+
get id() {
|
|
50
|
+
return this.#snapshot.id;
|
|
51
|
+
}
|
|
52
|
+
get name() {
|
|
53
|
+
return this.#snapshot.name;
|
|
54
|
+
}
|
|
55
|
+
get manifest() {
|
|
56
|
+
return this.ensure().manifest;
|
|
57
|
+
}
|
|
58
|
+
toJSON() {
|
|
59
|
+
return this.ensure().toJSON();
|
|
60
|
+
}
|
|
61
|
+
use(idOrMiddleware, middleware) {
|
|
62
|
+
let compiled;
|
|
63
|
+
if (typeof idOrMiddleware === "function") {
|
|
64
|
+
compiled = {
|
|
65
|
+
bound: {
|
|
66
|
+
id: nextMiddlewareId(this.#snapshot.entries),
|
|
67
|
+
handle: idOrMiddleware,
|
|
68
|
+
hasMiddleware: true,
|
|
69
|
+
},
|
|
70
|
+
middleware: idOrMiddleware,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
else if (typeof idOrMiddleware === "object") {
|
|
74
|
+
compiled = compileDeclaration(idOrMiddleware);
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
compiled = {
|
|
78
|
+
bound: {
|
|
79
|
+
id: idOrMiddleware,
|
|
80
|
+
handle: middleware,
|
|
81
|
+
hasMiddleware: true,
|
|
82
|
+
},
|
|
83
|
+
middleware: middleware,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
const dynamics = new Map(this.#snapshot.dynamics);
|
|
87
|
+
dynamics.set(compiled.bound.id, {
|
|
88
|
+
...(compiled.beforeModelCall
|
|
89
|
+
? { beforeModelCall: compiled.beforeModelCall }
|
|
90
|
+
: {}),
|
|
91
|
+
...(compiled.afterModelCall
|
|
92
|
+
? { afterModelCall: compiled.afterModelCall }
|
|
93
|
+
: {}),
|
|
94
|
+
...(compiled.middleware ? { middleware: compiled.middleware } : {}),
|
|
95
|
+
});
|
|
96
|
+
return AgentBuilder.withSnapshot({
|
|
97
|
+
...this.#snapshot,
|
|
98
|
+
entries: Object.freeze([...this.#snapshot.entries, compiled.bound]),
|
|
99
|
+
dynamics,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
beforeModelCall(fn) {
|
|
103
|
+
const dynamics = new Map(this.#snapshot.dynamics);
|
|
104
|
+
const existing = dynamics.get("agent") ?? {};
|
|
105
|
+
dynamics.set("agent", {
|
|
106
|
+
...existing,
|
|
107
|
+
beforeModelCall: fn,
|
|
108
|
+
});
|
|
109
|
+
const entries = ensureAgentCapabilityFlag(this.#snapshot.entries, "beforeModelCall");
|
|
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");
|
|
125
|
+
return AgentBuilder.withSnapshot({
|
|
126
|
+
...this.#snapshot,
|
|
127
|
+
entries,
|
|
128
|
+
dynamics,
|
|
129
|
+
agentAfter: fn,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
/** Optional no-op: returns the assembled agent facade. */
|
|
133
|
+
build() {
|
|
134
|
+
return this.ensure();
|
|
135
|
+
}
|
|
136
|
+
ensure() {
|
|
137
|
+
if (this.#agent)
|
|
138
|
+
return this.#agent;
|
|
139
|
+
if (this.#error)
|
|
140
|
+
throw this.#error;
|
|
141
|
+
const result = assembleAgent(this.#snapshot.entries, {
|
|
142
|
+
id: this.#snapshot.id,
|
|
143
|
+
name: this.#snapshot.name,
|
|
144
|
+
description: this.#snapshot.description,
|
|
145
|
+
metadata: this.#snapshot.metadata,
|
|
146
|
+
outputSchema: this.#snapshot.outputSchema,
|
|
147
|
+
}, this.#snapshot.dynamics);
|
|
148
|
+
if (!result.ok) {
|
|
149
|
+
this.#error = new AgentBuildError(result.diagnostics);
|
|
150
|
+
throw this.#error;
|
|
151
|
+
}
|
|
152
|
+
this.#agent = result.agent;
|
|
153
|
+
return this.#agent;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
function createSnapshot(options) {
|
|
157
|
+
const entries = [];
|
|
158
|
+
const dynamics = new Map();
|
|
159
|
+
const instructions = options.instructions === undefined
|
|
160
|
+
? undefined
|
|
161
|
+
: typeof options.instructions === "string"
|
|
162
|
+
? [options.instructions]
|
|
163
|
+
: options.instructions;
|
|
164
|
+
if (instructions !== undefined || options.tools !== undefined) {
|
|
165
|
+
const compiled = compileDeclaration({
|
|
166
|
+
id: "agent",
|
|
167
|
+
...(instructions === undefined ? {} : { instructions }),
|
|
168
|
+
...(options.tools === undefined ? {} : { tools: options.tools }),
|
|
169
|
+
});
|
|
170
|
+
entries.push(compiled.bound);
|
|
171
|
+
dynamics.set("agent", {});
|
|
172
|
+
}
|
|
173
|
+
return {
|
|
174
|
+
id: options.id,
|
|
175
|
+
...(options.name === undefined ? {} : { name: options.name }),
|
|
176
|
+
...(options.description === undefined
|
|
177
|
+
? {}
|
|
178
|
+
: { description: options.description }),
|
|
179
|
+
...(options.metadata === undefined ? {} : { metadata: options.metadata }),
|
|
180
|
+
outputSchema: options.outputSchema,
|
|
181
|
+
entries: Object.freeze(entries),
|
|
182
|
+
dynamics,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
function nextMiddlewareId(entries) {
|
|
186
|
+
const taken = new Set(entries.map((item) => item.id));
|
|
187
|
+
let seq = 0;
|
|
188
|
+
let id;
|
|
189
|
+
do {
|
|
190
|
+
seq += 1;
|
|
191
|
+
id = `middleware-${seq}`;
|
|
192
|
+
} while (taken.has(id));
|
|
193
|
+
return id;
|
|
194
|
+
}
|
|
195
|
+
function ensureAgentCapabilityFlag(entries, flag) {
|
|
196
|
+
const index = entries.findIndex((item) => item.id === "agent");
|
|
197
|
+
if (index >= 0) {
|
|
198
|
+
const current = entries[index];
|
|
199
|
+
if (current[flag])
|
|
200
|
+
return entries;
|
|
201
|
+
const next = Object.freeze({ ...current, [flag]: true });
|
|
202
|
+
return Object.freeze([
|
|
203
|
+
...entries.slice(0, index),
|
|
204
|
+
next,
|
|
205
|
+
...entries.slice(index + 1),
|
|
206
|
+
]);
|
|
207
|
+
}
|
|
208
|
+
return Object.freeze([
|
|
209
|
+
...entries,
|
|
210
|
+
Object.freeze({
|
|
211
|
+
id: "agent",
|
|
212
|
+
handle: async (_request, next) => next(),
|
|
213
|
+
hasMiddleware: false,
|
|
214
|
+
contributions: Object.freeze({}),
|
|
215
|
+
[flag]: true,
|
|
216
|
+
}),
|
|
217
|
+
]);
|
|
218
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { BoundMiddleware } from "./bound.js";
|
|
2
|
+
import type { CapabilityDeclaration, StepMiddleware } from "../types/middleware.js";
|
|
3
|
+
import type { AfterModelCallFn, BeforeModelCallFn } from "../types/dynamics.js";
|
|
4
|
+
export interface CompiledCapability {
|
|
5
|
+
readonly bound: BoundMiddleware;
|
|
6
|
+
readonly beforeModelCall?: BeforeModelCallFn<any>;
|
|
7
|
+
readonly afterModelCall?: AfterModelCallFn<any>;
|
|
8
|
+
readonly middleware?: StepMiddleware<any>;
|
|
9
|
+
}
|
|
10
|
+
export declare function compileDeclaration<State>(declaration: CapabilityDeclaration<State>): CompiledCapability;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
export function compileDeclaration(declaration) {
|
|
2
|
+
const tools = copyItems(declaration.tools, declaration.id);
|
|
3
|
+
const instructions = copyItems(declaration.instructions, declaration.id);
|
|
4
|
+
const model = declaration.model;
|
|
5
|
+
const contributions = snapshotContributions(instructions?.items, tools?.items, model);
|
|
6
|
+
const handle = async (request, next) => {
|
|
7
|
+
if (tools)
|
|
8
|
+
request.configuration.tools.set(tools.slot, tools.items);
|
|
9
|
+
if (instructions)
|
|
10
|
+
request.configuration.instructions.set(instructions.slot, instructions.items);
|
|
11
|
+
if (model)
|
|
12
|
+
request.configuration.model.select(model);
|
|
13
|
+
return declaration.middleware
|
|
14
|
+
? declaration.middleware(request, next)
|
|
15
|
+
: next();
|
|
16
|
+
};
|
|
17
|
+
return {
|
|
18
|
+
bound: {
|
|
19
|
+
id: declaration.id,
|
|
20
|
+
...(declaration.name === undefined ? {} : { name: declaration.name }),
|
|
21
|
+
...(declaration.description === undefined
|
|
22
|
+
? {}
|
|
23
|
+
: { description: declaration.description }),
|
|
24
|
+
handle,
|
|
25
|
+
hasMiddleware: declaration.middleware !== undefined,
|
|
26
|
+
tools: tools?.items,
|
|
27
|
+
...(contributions === undefined ? {} : { contributions }),
|
|
28
|
+
...(declaration.beforeModelCall ? { beforeModelCall: true } : {}),
|
|
29
|
+
...(declaration.afterModelCall ? { afterModelCall: true } : {}),
|
|
30
|
+
...(declaration.type === undefined ? {} : { manifestType: declaration.type }),
|
|
31
|
+
...(declaration.metadata === undefined
|
|
32
|
+
? {}
|
|
33
|
+
: { metadata: declaration.metadata }),
|
|
34
|
+
...(declaration.skills === undefined ||
|
|
35
|
+
Object.keys(declaration.skills).length === 0
|
|
36
|
+
? {}
|
|
37
|
+
: { skills: declaration.skills }),
|
|
38
|
+
...(declaration.skillRecords === undefined ||
|
|
39
|
+
Object.keys(declaration.skillRecords).length === 0
|
|
40
|
+
? {}
|
|
41
|
+
: { skillRecords: declaration.skillRecords }),
|
|
42
|
+
...(declaration.mcpServers === undefined ||
|
|
43
|
+
Object.keys(declaration.mcpServers).length === 0
|
|
44
|
+
? {}
|
|
45
|
+
: { mcpServers: declaration.mcpServers }),
|
|
46
|
+
...(declaration.pluginRoot === undefined
|
|
47
|
+
? {}
|
|
48
|
+
: { pluginRoot: declaration.pluginRoot }),
|
|
49
|
+
},
|
|
50
|
+
...(declaration.beforeModelCall
|
|
51
|
+
? { beforeModelCall: declaration.beforeModelCall }
|
|
52
|
+
: {}),
|
|
53
|
+
...(declaration.afterModelCall
|
|
54
|
+
? { afterModelCall: declaration.afterModelCall }
|
|
55
|
+
: {}),
|
|
56
|
+
...(declaration.middleware ? { middleware: declaration.middleware } : {}),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
function snapshotContributions(instructions, tools, model) {
|
|
60
|
+
const snapInstructions = instructions === undefined ? undefined : Object.freeze([...instructions]);
|
|
61
|
+
const snapTools = tools === undefined
|
|
62
|
+
? undefined
|
|
63
|
+
: Object.freeze(tools.map((tool) => Object.freeze({
|
|
64
|
+
name: tool.name,
|
|
65
|
+
...(tool.description === undefined
|
|
66
|
+
? {}
|
|
67
|
+
: { description: tool.description }),
|
|
68
|
+
})));
|
|
69
|
+
// Model is Runtime-owned for the published manifest; still allow local middleware select().
|
|
70
|
+
void model;
|
|
71
|
+
if (snapInstructions === undefined && snapTools === undefined) {
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
return Object.freeze({
|
|
75
|
+
...(snapInstructions === undefined
|
|
76
|
+
? {}
|
|
77
|
+
: { instructions: snapInstructions }),
|
|
78
|
+
...(snapTools === undefined ? {} : { tools: snapTools }),
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
function copyItems(value, defaultSlot) {
|
|
82
|
+
if (value === undefined)
|
|
83
|
+
return undefined;
|
|
84
|
+
if (!("items" in value))
|
|
85
|
+
return Object.freeze({
|
|
86
|
+
slot: defaultSlot,
|
|
87
|
+
items: Object.freeze([...value]),
|
|
88
|
+
});
|
|
89
|
+
return Object.freeze({
|
|
90
|
+
slot: value.slot,
|
|
91
|
+
items: Object.freeze([...value.items]),
|
|
92
|
+
});
|
|
93
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { AgentManifest } from "../types/manifest.js";
|
|
2
|
+
import type { BuiltAgent } from "../types/agent.js";
|
|
3
|
+
import type { JsonObject } from "../types/shared.js";
|
|
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
|
+
}
|
|
10
|
+
/** Rebuild an agent from manifest JSON + in-process implementations (T28). */
|
|
11
|
+
export declare function agentFrom<Info = unknown>(json: AgentManifest | JsonObject, implementations: Implementations<Info>, options?: {
|
|
12
|
+
readonly sessionTools?: readonly SessionToolRef[];
|
|
13
|
+
}): BuiltAgent<Info>;
|