UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

161 lines (160 loc) • 4.76 kB
import { t as isPluginJsonValue } from "./host-hook-json-BdnpRWog.mjs"; import { n as validateJsonSchemaValue } from "./schema-validator-NvoOmaWr.mjs"; import { n as jsonResult } from "./tool-results-CcV0sxnb.mjs"; import "./common-njQNtpnW.mjs"; import { t as definePluginEntry } from "./plugin-entry-CiJdaHtx.mjs"; import { i as toolPluginMetadataSymbol } from "./tool-plugin-B-9NRGYk.mjs"; //#region src/plugin-sdk/feature-plugin.ts var FeatureValidationError = class extends Error { constructor(e, t) { super(t); this.code = e; this.name = "FeatureValidationError"; } }; function validateFeatureValue(e, t, n, r) { const i = r === "input" ? "INVALID_INPUT" : "INVALID_OUTPUT"; if (!isPluginJsonValue(t)) throw new FeatureValidationError(i, `Feature ${r} must be bounded JSON`); const a = validateJsonSchemaValue({ schema: e, cacheKey: n, value: t }); if (!a.ok) { const e = a.errors.map((e) => e.text).join("; ").slice(0, 512); throw new FeatureValidationError(i, `Feature ${r} does not match its schema: ${e}`); } return t; } /** One implementation serves each declared surface without relaying through privileged RPC. */ function defineFeaturePlugin(e) { const { contract: t } = e; const n = Object.entries(t.operations).toSorted(([e], [t]) => e.localeCompare(t)); const r = definePluginEntry({ id: t.pluginId, name: e.name, description: e.description, register(r) { if (r.id !== t.pluginId) throw new Error("Feature contract must belong to the registering plugin"); let i; const a = { emit(e, n) { const r = t.events[e]; if (!r || !i) throw new Error("Feature event emitter is unavailable until its Gateway service starts"); const a = validateFeatureValue(r, n, `feature:${t.pluginId}:event:${e}`, "output"); i.emit(e, a, { scope: "operator.read" }); } }; if (Object.keys(t.events).length > 0) r.registerService({ id: `${t.pluginId}:feature-events`, start(e) { i = e.gatewayEvents; }, stop() { i = void 0; } }); const o = e.setup(r, a); for (const [i, a] of n) { const n = i; const s = o[n]; if (typeof s !== "function") throw new Error(`Feature operation ${i} is missing its handler`); const invoke = async (e, n) => { const r = n.source === "tool" ? n.signal : void 0; r?.throwIfAborted(); const o = validateFeatureValue(a.input, e, `feature:${t.pluginId}:${i}:input`, "input"); const c = await s(o, n); r?.throwIfAborted(); return validateFeatureValue(a.output, c, `feature:${t.pluginId}:${i}:output`, "output"); }; const c = a.kind === "query" ? "operator.read" : "operator.write"; r.session.controls.registerSessionAction({ id: i, description: a.description, schema: a.input, requiredScopes: [c], async handler(e) { try { return { ok: true, result: await invoke(e.payload, { source: "session-action", action: e, api: r }) }; } catch (e) { if (e instanceof FeatureValidationError) return { ok: false, error: e.message, code: e.code }; throw e; } } }); if (a.tool) { const e = a.tool; r.registerTool((t) => ({ name: e.name, label: e.label ?? e.name, description: a.description, parameters: a.input, outputSchema: a.output, execute: async (e, n, i, a) => jsonResult(await invoke(n, { source: "tool", tool: t, toolCallId: e, signal: i, onUpdate: a, api: r })) }), { name: e.name, ...e.optional ? { optional: true } : {} }); } const l = e.commands?.[n]; if (l) r.registerCommand({ name: l.name, description: l.description ?? a.description, acceptsArgs: true, requireAuth: true, requiredScopes: [c], async handler(e) { const t = await invoke(l.parse(e), { source: "command", command: e, api: r }); if (l.format) return l.format(t, e); return { text: JSON.stringify(t, null, 2) }; } }); } } }); const i = { id: t.pluginId, name: e.name, description: e.description, activation: { onStartup: true }, configSchema: r.configSchema.jsonSchema ?? { type: "object", additionalProperties: false }, tools: n.flatMap(([, e]) => e.tool ? [{ name: e.tool.name, label: e.tool.label ?? e.tool.name, description: e.description, parameters: e.input, outputSchema: e.output, ...e.tool.optional ? { optional: true } : {} }] : []) }; Object.defineProperty(r, toolPluginMetadataSymbol, { value: i, enumerable: false }); return r; } //#endregion export { defineFeaturePlugin as t };