import { RegistryChangeKind } from '@matatbread/matbot-plugin-api'; import type { Tool, ToolRegistry, Notifier, ToolContext, ToolEvent, ToolInputError } from './types.js'; /** Judges one call: every `validate` hook. */ export type ToolCallJudge = (tool: string, parameters: unknown, signal: AbortSignal) => Promise; /** Cap on the rendered value. Long enough to identify what was sent, short enough that a base64 * attachment or a large document cannot turn one rejected field into the bulk of a turn's context. */ const VALUE_CHARS = 160; /** * `.x: expected string, actual value \`{"x":8}\`` * * The value is wrapped in its own field name, so what is shown is a fragment the caller can compare * against what it sent rather than a bare literal it has to locate. An array element or the root has no * name to wrap it in, so those render the value alone — the path already says which one it was. */ function formatError(e: ToolInputError): string { if (e.value === undefined) return `${e.path}: ${e.message}`; const named = /\.([A-Za-z_$][\w$]*)$/.exec(e.path); let shown: string; try { // Validation now runs on internal calls too, so this is not always JSON.parse output: a caller can // reach here with a bigint or a cycle, and a throw while REPORTING an error would replace the // diagnosis with a stack trace. shown = JSON.stringify(named ? { [named[1]!]: e.value } : e.value) ?? String(e.value); } catch { return `${e.path}: ${e.message}`; } if (shown.length > VALUE_CHARS) shown = `${shown.slice(0, VALUE_CHARS)}…`; return `${e.path}: ${e.message}, actual value \`${shown}\``; } /** * The `code` on the `error` event a rejected call yields. 422 rather than 400 because the body parsed * fine and its *shape* is wrong, and — the reason a number is safe here at all — a process exit code * cannot exceed 255, so this can never be mistaken for one (`bash` puts its exit status in the same * field). A transport may map a 4xx code onto its own status; nothing is obliged to. */ export const TOOL_INPUT_INVALID = 422; /** * Validation belongs at the executor, not in a `toolcall` hook, because that hook is a RUNNER channel: * it guards the model's path and nothing else. `POST /tools/:name` and `invokeTool` both call * `tool.executor.execute` directly and fire no runner hooks, so a runner hook leaves two doors open and * invites the per-door duplication that then drifts. Wrapping here is the one place every door already * passes through — which is why the `validate` hook channel is dispatched from here, not by the runner. * * Wrapped at REGISTRATION rather than at `resolve`, so there is exactly one wrapper per tool and * `resolve(x) === resolve(x)` still holds. * * Validators are consulted per CALL, not captured: they are registered long after the tools are, and * may be unloaded. None with an opinion ⇒ the call passes straight through, so core mandates no * validation — it only honours what is registered. The cost when a validator IS * present is one generator frame plus a structural check (measured in the tens of nanoseconds against * tool work measured in milliseconds); the deliberate trade is that an INTERNAL call is validated too, * though a statically-typed call site should already be sound. That is not pure waste: `invokeTool` * with a dynamic name, a trigger's `invoke.params` (typed `object`) and a compiled skill's payload are * all internal callers whose input no compiler has checked. */ function validatingTool(tool: Tool, judge: ToolCallJudge): Tool { const inner = tool.executor; return copyTool(tool, { executor: { execute(input: unknown, ctx: ToolContext): AsyncIterable { return (async function* () { const errs = await judge(tool.name, input, ctx.signal); if (errs !== undefined && errs.length > 0) { const detail = errs.map(formatError).join('; '); yield { type: 'error', message: `Invalid input for tool "${tool.name}": ${detail}`, code: TOOL_INPUT_INVALID, failure: 'invalid' }; return; } // `yield*` delegates `return`/`throw` as well as values, so an abort still reaches the tool. yield* inner.execute(input, ctx); })(); }, }, }); } /** * `tool` with `fields` replaced, keeping its accessors as accessors. A spread reads a getter once and * stores the value, and the host copies every tool it registers (to stamp its owner, to wrap its * executor), so a description built when it is read — the `provider` tool's lists the adapters loaded * NOW — would be frozen at registration, and so would anything a shadow captured of it. */ export function copyTool(tool: Tool, fields: Partial): Tool { const descriptors: PropertyDescriptorMap = Object.getOwnPropertyDescriptors(tool); for (const [key, value] of Object.entries(fields)) { descriptors[key] = { value, writable: true, enumerable: true, configurable: true }; } return Object.defineProperties({}, descriptors) as Tool; } /** * One tool per name, and the host's own registration of each name kept beside it. A plugin's tool that * displaced the host's is withdrawn back to it (by `remove` or the plugin's unload), so a plugin can shadow * a built-in — `provider`, `plugin`, a core tool — and unloading it gives the built-in back, where the * name used to be left empty until a restart. A plugin's tool that displaced ANOTHER plugin's is not: * that one is gone, not suspended, because plugins come and go independently — Y is installed over X, * then X is removed — and a stack would reinstate what nobody has loaded. Only what the host put there * stays for as long as the machine does, which is the same line the swap-members draw (see CLAUDE.md, * "a swap-member reverts to the host's captured boot default"). * * "The host's" is a registration with no `pluginName`: a plugin's are always stamped with its name. */ export class ToolRegistryImpl implements ToolRegistry { private readonly tools = new Map(); private readonly builtins = new Map(); // The host's notifier proxy, injected at boot (both hosts build it before this registry). Held as the // proxy, not a resolved impl, so a later register('Notifier', …) takes effect here too. Optional: a // registry built without one (tests) simply announces nothing. private readonly notifier: Notifier | undefined; // Host-injected, and consulted per call: validators are registered by plugins long after the builtin // tools are seeded, and may be unloaded again. private readonly judge: ToolCallJudge | undefined; constructor(initial?: Iterable, notifier?: Notifier, judge?: ToolCallJudge) { this.judge = judge; if (initial !== undefined) for (const tool of initial) this.set(this.guard(tool)); this.notifier = notifier; } private guard(tool: Tool): Tool { return this.judge === undefined ? tool : validatingTool(tool, this.judge); } private set(tool: Tool): void { this.tools.set(tool.name, tool); if (tool.pluginName === undefined) this.builtins.set(tool.name, tool); } async validate(name: string, parameters: unknown): Promise { return this.judge?.(name, parameters, new AbortController().signal); } private announce(name: string, operation: 'added' | 'removed', pluginName?: string): void { this.notifier?.notify({ kind: RegistryChangeKind, source: 'tools', registry: 'tools', name, operation, ...(pluginName !== undefined ? { detail: { pluginName } } : {}), }); } register(tool: Tool): void { this.set(this.guard(tool)); this.announce(tool.name, 'added', tool.pluginName); } /** A plugin's tool goes back to the host's of that name, if there is one; the host's own goes. */ remove(name: string): void { const tool = this.tools.get(name); if (tool !== undefined) this.withdraw(tool); } removeByPlugin(pluginName: string): void { for (const tool of [...this.tools.values()]) if (tool.pluginName === pluginName) this.withdraw(tool); } // A reinstated built-in reads as `added`, as a remount does on the mount table's `services` arm: the // name is present, and provided by the host again. private withdraw(tool: Tool): void { const builtin = tool.pluginName === undefined ? undefined : this.builtins.get(tool.name); if (builtin === undefined) { this.tools.delete(tool.name); this.builtins.delete(tool.name); this.announce(tool.name, 'removed'); } else { this.tools.set(tool.name, builtin); this.announce(tool.name, 'added'); } } resolve(name: string): Tool | null { return this.tools.get(name) ?? null; } list(): Tool[] { return [...this.tools.values()]; } }