forked from WrongStack/WrongStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.ts
More file actions
424 lines (401 loc) · 15.7 KB
/
Copy pathapi.ts
File metadata and controls
424 lines (401 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
import { ExtensionRegistry } from '../extension/registry.js';
import { KERNEL_API_VERSION } from './loader.js';
import type { HookRegistry } from '../hooks/registry.js';
import type { Container } from '../kernel/container.js';
import type { EventBus, EventName, Listener } from '../kernel/events.js';
import type { Pipeline } from '../kernel/pipeline.js';
import type { ProviderRegistry } from '../registry/provider-registry.js';
import type { SlashCommandRegistry } from '../registry/slash-command-registry.js';
import type { ToolRegistry } from '../registry/tool-registry.js';
import type { ToolWrapper } from '../registry/tool-registry.js';
import type { Config } from '../types/config.js';
import type { Logger } from '../types/logger.js';
import type { ModelsRegistry } from '../types/models-registry.js';
import type { Mailbox } from '../coordination/mailbox-types.js';
import type {
MCPRegistryView,
MetricsSinkView,
PluginAPI,
PluginCapabilities,
PluginDependency,
PluginPipelines,
ProviderFactory,
ProviderRegistryView,
SessionWriterView,
SlashCommandRegistryView,
ToolRegistryView,
} from '../types/plugin.js';
import type { HookEvent, HookMatcher, InProcessHook } from '../types/hooks.js';
import type { SystemPromptContributor } from '../types/system-prompt-contributor.js';
import type { JSONSchema, Tool } from '../types/tool.js';
export interface PluginAPIInit {
ownerName: string;
container: Container;
events: EventBus;
/**
* The agent's concrete pipelines. `DefaultPluginAPI` converts each to a
* `ReadonlyPipeline` before exposing them to the plugin — plugins can
* inspect and invoke pipelines but cannot mutate them.
*/
pipelines: PluginPipelines;
toolRegistry: ToolRegistry;
providerRegistry: ProviderRegistry;
slashCommandRegistry?: SlashCommandRegistry | undefined;
mcpRegistry?: MCPRegistryView | undefined;
/**
* The agent's extension registry. Plugins register AgentExtension
* instances here to hook into agent lifecycle events.
*/
extensions?: ExtensionRegistry | undefined;
/**
* The host's lifecycle hook registry. When provided, `api.registerHook`
* adds in-process hooks here. When absent, `registerHook` is a noop.
*/
hookRegistry?: HookRegistry | undefined;
/**
* The active session writer. Plugins append custom events here.
* When not provided, a noop writer is used.
*/
sessionWriter?: SessionWriterView | undefined;
/**
* The host's metrics sink. When set, the plugin gets a scoped view
* that auto-prefixes metric names with `plugin.<pluginName>.`.
* When not provided, a noop sink is used.
*/
metricsSink?: MetricsSinkView | undefined;
/**
* The host's models registry (models.dev-backed catalog of providers,
* models, and per-token pricing). When provided, plugins that need
* model metadata (cost-tracker, billing reports) can query it instead
* of relying on bundled tables. Optional — minimal hosts/tests may
* omit it.
*/
modelsRegistry?: ModelsRegistry | undefined;
/**
* The host's project-level mailbox. When provided, plugins that publish
* to other agents (todo-listener, session-recap) can call `api.mailbox.send`.
* When not provided, those plugins should gracefully no-op.
*/
mailbox?: Mailbox | undefined;
config: Config;
/**
* The host's ConfigStore. Used to wire `api.onConfigChange()`.
* When not provided, `onConfigChange` is a noop.
*/
configStore?: { watch(cb: (next: unknown, prev: unknown) => void): () => void };
log: Logger;
/**
* Whether this plugin is first-party ("official"). Set by the host based on
* the plugin's load source (e.g. shipped in the built-in factory list), NOT
* self-declared by the plugin. Official plugins may register bare slash
* command names and override built-ins; external plugins are namespaced.
* Defaults to false.
*/
official?: boolean | undefined;
/**
* Declared capabilities of the plugin. Used for capability-based
* authorization checks (e.g. tool mutation permissions).
*/
capabilities?: PluginCapabilities | undefined;
}
export class DefaultPluginAPI implements PluginAPI {
readonly container: Container;
readonly events: EventBus;
readonly pipelines: PluginPipelines;
readonly tools: ToolRegistryView;
readonly providers: ProviderRegistryView;
readonly mcp: MCPRegistryView;
readonly slashCommands: SlashCommandRegistryView;
readonly extensions: ExtensionRegistry;
readonly session: SessionWriterView;
readonly metrics: MetricsSinkView;
readonly config: Config;
readonly log: Logger;
readonly modelsRegistry: ModelsRegistry | undefined;
readonly mailbox: Mailbox | undefined;
private readonly configStore:
| { watch(cb: (next: unknown, prev: unknown) => void): () => void }
| undefined;
private readonly hookRegistry: HookRegistry | undefined;
private readonly ownerName: string;
private readonly pluginCleanupFns: Array<() => void> = [];
constructor(init: PluginAPIInit) {
const owner = init.ownerName;
this.ownerName = owner;
this.hookRegistry = init.hookRegistry;
this.container = init.container;
this.events = init.events;
this.config = init.config;
this.configStore = init.configStore;
this.log = init.log.child({ plugin: owner });
this.extensions = init.extensions ?? new ExtensionRegistry();
this.session = init.sessionWriter ?? noopSession;
this.metrics = init.metricsSink ? scopedMetrics(init.metricsSink, owner) : noopMetrics;
this.modelsRegistry = init.modelsRegistry;
this.mailbox = init.mailbox;
// Convert concrete pipelines to read-only views before passing to plugins.
const pipelines = init.pipelines as never as Record<string, Pipeline<unknown>>;
const readonlyPipelines: PluginPipelines = {} as PluginPipelines;
for (const [key, pipeline] of Object.entries(pipelines)) {
readonlyPipelines[key] = pipeline.asReadonly() as PluginPipelines[typeof key];
}
this.pipelines = readonlyPipelines;
const tr = init.toolRegistry;
const isOfficial = init.official === true;
const capabilities = init.capabilities;
// Trust tiers for the tool registry, mirroring the slash-command registry:
// only first-party ("official") plugins (and core) may mutate a tool they
// do not own. An external plugin can register, wrap, and unregister its OWN
// tools, but must not silently downgrade a built-in's `permission` via
// `wrap`, nor `unregister` another owner's safeguard. Officiality is set by
// the host from the load source — never self-declared. (`register` is
// already collision-safe: it throws on a duplicate name, so an external
// plugin cannot shadow a built-in by re-registering it.)
const assertCanMutateTool = (name: string, op: string): void => {
if (isOfficial) return;
const currentOwner = tr.ownerOf(name);
// undefined: tool doesn't exist — let the downstream call no-op/throw.
if (currentOwner === undefined) return;
// `wrap` records a chain owner like "core+plugin"; a tool this plugin
// solely registered and (re)wrapped is "plugin" or "plugin+plugin".
// The plugin owns the tool only if EVERY segment is itself — so a core
// tool ("core") or one another plugin touched ("core+plugin") is denied.
const ownedSolelyByMe = currentOwner.split('+').every((seg) => seg === owner);
if (ownedSolelyByMe) return;
// 2026-06-13: Capability-based mutation check for non-official plugins
// that don't own the tool. If the plugin declares toolMutateCapabilities
// matching the tool's capabilities, allow the mutation.
const toolCaps = tr.get(name)?.capabilities ?? [];
const pluginMutateCaps = capabilities?.toolMutateCapabilities ?? [];
const hasRequiredCap = toolCaps.some((c) => pluginMutateCaps.includes(c));
if (!hasRequiredCap) {
throw new Error(
`Plugin "${owner}" may not ${op} tool "${name}" — it is owned by "${currentOwner}". ` +
`Tool capabilities: [${toolCaps.join(', ') || 'none'}]. ` +
`Plugin toolMutateCapabilities: [${pluginMutateCaps.join(', ') || 'none'}]. ` +
`Missing required capability to mutate this tool.`,
);
}
};
this.tools = {
register: (t: Tool) => tr.register(t, owner),
unregister: (name: string) => {
assertCanMutateTool(name, 'unregister');
return tr.unregister(name);
},
wrap: (name: string, wrapper: ToolWrapper) => {
assertCanMutateTool(name, 'wrap');
tr.wrap(name, wrapper, owner);
},
get: (name: string) => tr.get(name),
list: () => tr.list(),
};
const pr = init.providerRegistry;
this.providers = {
register: (f: ProviderFactory) => pr.register(f),
unregister: (type: string) => pr.unregister(type),
create: (cfg) => pr.create(cfg as { type: string }),
list: () => pr.list(),
};
this.mcp = init.mcpRegistry ?? noopMcp;
const scr = init.slashCommandRegistry;
const official = init.official === true;
this.slashCommands = scr
? {
register: (cmd) => scr.register(cmd, owner, { official }),
unregister: (name) => scr.unregister(name),
get: (name) => scr.get(name),
list: () => scr.list(),
}
: noopSlashCommands;
}
onEvent<K extends EventName>(event: K, handler: Listener<K>): () => void {
const off = this.events.on(event, handler);
this.pluginCleanupFns.push(off);
return off;
}
onPattern(pattern: string, handler: (event: string, payload: unknown) => void): () => void {
const off = this.events.onPattern(pattern, handler);
this.pluginCleanupFns.push(off);
return off;
}
emitCustom(event: string, payload: unknown): void {
this.events.emitCustom(event, payload);
}
onConfigChange(handler: (next: Readonly<Config>, prev: Readonly<Config>) => void): () => void {
if (!this.configStore) return () => {};
return this.configStore.watch(handler as (next: unknown, prev: unknown) => void);
}
/** Called by the plugin loader when uninstalling the plugin. */
drainCleanup(): void {
for (const fn of this.pluginCleanupFns.splice(0)) {
try {
fn();
} catch {
/* best-effort */
}
}
// Belt-and-braces: drain any hooks this plugin still owns. If `setup()`
// threw partway through, the unsubscribe functions above may not cover
// every registered hook (the push happens *after* registerInProcess
// returns). Sweeping by owner guarantees no closure outlives its plugin.
if (this.hookRegistry) this.hookRegistry.drainByOwner(this.ownerName);
}
registerSystemPromptContributor(c: SystemPromptContributor): () => void {
return this.extensions.registerSystemPromptContributor(c);
}
registerHook(
event: HookEvent,
matcher: HookMatcher | undefined,
hook: InProcessHook,
): () => void {
if (!this.hookRegistry) return () => {};
const off = this.hookRegistry.registerInProcess(event, matcher, hook, this.ownerName);
this.pluginCleanupFns.push(off);
return off;
}
}
const noopMcp: MCPRegistryView = {
start: async () => undefined,
stop: async () => undefined,
restart: async () => undefined,
list: () => [],
};
const noopSlashCommands: SlashCommandRegistryView = {
register() {
/* noop */
},
unregister() {
return false;
},
get() {
return undefined;
},
list() {
return [];
},
};
const noopSession: SessionWriterView = {
append: async () => {
/* noop */
},
};
const noopMetrics: MetricsSinkView = {
counter() {},
histogram() {},
gauge() {},
};
/**
* Wrap a MetricsSinkView so every metric name is prefixed with
* `plugin.<pluginName>.`. This prevents metric name collisions
* between plugins and keeps the Prometheus output organized.
*/
function scopedMetrics(sink: MetricsSinkView, pluginName: string): MetricsSinkView {
const prefix = `plugin.${pluginName}.`;
return {
counter(name, value, labels) {
sink.counter(`${prefix}${name}`, value, labels);
},
histogram(name, value, labels) {
sink.histogram(`${prefix}${name}`, value, labels);
},
gauge(name, value, labels) {
sink.gauge(`${prefix}${name}`, value, labels);
},
};
}
/**
* Define a plugin with automatic `apiVersion` injection and optional
* TypeScript type inference for the plugin options schema.
*
* The `options` generic is inferred from the `factory` function's second
* parameter, so annotating it gives you fully-typed options throughout:
*
* @example
* ```ts
* import { definePlugin } from '@wrongstack/core';
*
* interface MyOptions {
* threshold: number;
* enabled: boolean;
* }
*
* const myPlugin = definePlugin(
* {
* name: 'my-plugin',
* version: '0.1.0',
* description: 'My example plugin',
* capabilities: { tools: true },
* configSchema: {
* type: 'object',
* properties: {
* threshold: { type: 'number', default: 100 },
* enabled: { type: 'boolean', default: true },
* },
* },
* defaultConfig: { threshold: 100, enabled: true },
* },
* (api, options) => {
* // options.threshold is `number` here, not `unknown`
* api.tools.register({
* name: 'my-tool',
* description: `Threshold is ${options.threshold}`,
* // ...
* });
* },
* );
*
* export default myPlugin;
* ```
*
* Plugins that don't need typed options can omit the interface and let
* TypeScript infer from `defaultConfig`, or omit `defaultConfig` entirely
* (options will be `undefined` at runtime, accessible via `api.config`):
*
* @example
* ```ts
* const simplePlugin = definePlugin(
* { name: 'simple' },
* (api) => {
* api.tools.register({ name: 'ping', description: 'Ping the service' });
* },
* );
* ```
*
* The `apiVersion` is automatically set to the current `KERNEL_API_VERSION`
* so plugins are always compatible with the kernel that loaded them.
* Plugins that need to pin to a specific kernel version can override it
* in the returned object before exporting.
*/
export function definePlugin<const TOptions extends Record<string, unknown> | undefined>(
metadata: {
name: string;
version?: string | undefined;
description?: string | undefined;
capabilities?: PluginCapabilities | undefined;
configSchema?: JSONSchema | undefined;
defaultConfig?: TOptions | undefined;
dependsOn?: (string | PluginDependency)[] | undefined;
optionalDeps?: (string | PluginDependency)[] | undefined;
conflictsWith?: string[] | undefined;
},
factory: (api: PluginAPI, options: TOptions) => void | Promise<void>,
): { name: string; version?: string | undefined; description?: string | undefined; apiVersion: string; capabilities?: PluginCapabilities | undefined; configSchema?: JSONSchema | undefined; defaultConfig?: TOptions | undefined; dependsOn?: (string | PluginDependency)[] | undefined; optionalDeps?: (string | PluginDependency)[] | undefined; conflictsWith?: string[] | undefined; setup: (api: PluginAPI) => void | Promise<void> } {
return {
name: metadata.name,
version: metadata.version,
description: metadata.description,
capabilities: metadata.capabilities,
configSchema: metadata.configSchema,
defaultConfig: metadata.defaultConfig,
dependsOn: metadata.dependsOn,
optionalDeps: metadata.optionalDeps,
conflictsWith: metadata.conflictsWith,
apiVersion: KERNEL_API_VERSION,
// Cast the factory to match the Plugin.setup signature.
// The opts parameter ({ signal }) is accepted by Plugin.setup but the
// definePlugin factory doesn't need to declare it — the host always passes
// it at the call site; the factory just doesn't have to care about it.
setup: (api: PluginAPI) => factory(api, metadata.defaultConfig as TOptions),
};
}