UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

148 lines (147 loc) • 5.71 kB
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs"; import { o as getPluginRuntimeGatewayRequestScope } from "./gateway-request-scope-BvvDhs5D.mjs"; import { t as createSubsystemLogger } from "./subsystem-BHJiy01V.mjs"; import { x as isPluginRegistryRetired } from "./registry-lifecycle-CJjWnl-y.mjs"; import { l as getActivePluginRegistry } from "./runtime-ezCEpumg.mjs"; import { t as getPluginRuntimeGenerationRegistry } from "./generation-state-CbpjEoxA.mjs"; import "./generation-scope-Cq7RLFex.mjs"; import { t as createHookRunner } from "./hooks-BiZpB_10.mjs"; //#region src/plugins/hook-runner-global-state.ts const hookRunnerGlobalStateKey = Symbol.for("openclaw.plugins.hook-runner-global-state"); const hookRunnerGlobalState = resolveGlobalSingleton(hookRunnerGlobalStateKey, () => ({ hookRunner: null, registry: null }), (e) => { e.registry = null; }, "plugin-registry"); function resolveRootHookRegistry(e) { const t = getActivePluginRegistry(); const n = e.registry && !isPluginRegistryRetired(e.registry) ? e.registry : null; if (!n || n === t) return t ?? n; return overlayHookRegistries(t, n); } function overlayHookRegistries(e, t) { if (!t || t === e) return e; if (!e) return t; const n = new Set(t.plugins.map((e) => e.id)); const r = /* @__PURE__ */ new Map(); for (const e of t.hooks) { if (!Array.isArray(e.events)) continue; const t = r.get(e.pluginId) ?? /* @__PURE__ */ new Set(); for (const n of e.events) t.add(n); r.set(e.pluginId, t); } const i = new Set(t.typedHooks.map((e) => `${e.pluginId}\0${e.hookName}`)); const a = new Set((t.trustedToolPolicies ?? []).map((e) => `${e.pluginId}\0${e.policy.id}`)); const o = [...(e.trustedToolPolicies ?? []).filter((e) => !a.has(`${e.pluginId}\0${e.policy.id}`)), ...t.trustedToolPolicies ?? []].toSorted((e, t) => { return (e.origin === "bundled" ? 0 : 1) - (t.origin === "bundled" ? 0 : 1); }); return { hooks: [...e.hooks.flatMap((e) => { const t = r.get(e.pluginId); if (!t || !Array.isArray(e.events)) return e; const n = e.events.filter((e) => !t.has(e)); return n.length === 0 ? [] : [{ ...e, events: n }]; }), ...t.hooks], typedHooks: [...e.typedHooks.filter((e) => !i.has(`${e.pluginId}\0${e.hookName}`)), ...t.typedHooks], plugins: [...e.plugins.filter((e) => !n.has(e.id)), ...t.plugins], trustedToolPolicies: o }; } function resolveHookRegistry(e) { const t = getPluginRuntimeGenerationRegistry(); if (t && !isPluginRegistryRetired(t)) return t; const n = getPluginRuntimeGatewayRequestScope()?.pluginRegistry ?? null; return overlayHookRegistries(resolveRootHookRegistry(e), n && !isPluginRegistryRetired(n) ? n : null); } function createLiveHookRegistryFacade() { return { get hooks() { return resolveHookRegistry(hookRunnerGlobalState)?.hooks ?? []; }, get typedHooks() { return resolveHookRegistry(hookRunnerGlobalState)?.typedHooks ?? []; }, get plugins() { return resolveHookRegistry(hookRunnerGlobalState)?.plugins ?? []; }, get trustedToolPolicies() { return resolveHookRegistry(hookRunnerGlobalState)?.trustedToolPolicies ?? []; } }; } /** Get the registry view that backs global hook dispatch. */ function getGlobalHookRunnerRegistry() { return resolveHookRegistry(hookRunnerGlobalState) ? createLiveHookRegistryFacade() : null; } //#endregion //#region src/plugins/hook-runner-global.ts /** * Initialize the global hook runner with a plugin registry. * Called on every plugin registry activation and by SDK consumers. The runner * instance stays stable so references captured mid-run keep seeing current hooks. */ function initializeGlobalHookRunner(e) { hookRunnerGlobalState.registry = e; ensureGlobalHookRunner(); } /** * Create the global hook runner without claiming a registry. Its live facade reads * the scoped registry of the current run, so a host that never activated a process * root (a headless `agent exec` run) still dispatches that run's hooks. */ function ensureGlobalHookRunner() { if (!hookRunnerGlobalState.hookRunner) hookRunnerGlobalState.hookRunner = createHookRunner(createLiveHookRegistryFacade(), { logger: createSubsystemLogger("plugins"), catchErrors: true, failurePolicyByHook: { before_agent_run: "fail-closed", before_install: "fail-closed", before_tool_call: "fail-closed" } }); return hookRunnerGlobalState.hookRunner; } /** * Get the global hook runner. * Returns null if plugins haven't been loaded yet. */ function getGlobalHookRunner() { return hookRunnerGlobalState.hookRunner; } /** * Get the registry from the most recent activation or explicit initialization. * Returns null if plugins haven't been loaded yet. */ function getGlobalPluginRegistry() { return hookRunnerGlobalState.registry; } function hasGlobalHooks(e, t) { return hookRunnerGlobalState.hookRunner?.hasHooks(e, t) ?? false; } async function runGlobalGatewayStopSafely(e) { const t = createSubsystemLogger("plugins"); const n = e.registry ? createHookRunner(e.registry, { logger: t }) : getGlobalHookRunner(); if (!n?.hasHooks("gateway_stop")) return; try { await n.runGatewayStop(e.event, e.ctx); } catch (n) { if (e.onError) { e.onError(n); return; } t.warn(`gateway_stop hook failed: ${String(n)}`); } } /** * Reset the global hook runner (for testing). */ function resetGlobalHookRunner() { hookRunnerGlobalState.hookRunner = null; hookRunnerGlobalState.registry = null; } //#endregion export { initializeGlobalHookRunner as a, getGlobalHookRunnerRegistry as c, hasGlobalHooks as i, getGlobalHookRunner as n, resetGlobalHookRunner as o, getGlobalPluginRegistry as r, runGlobalGatewayStopSafely as s, ensureGlobalHookRunner as t };