UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

5 lines • 4.29 kB
import{o as resolveGlobalSingleton}from"./global-singleton-DolL9xGt.mjs";import{o as getPluginRuntimeGatewayRequestScope}from"./gateway-request-scope-7deHmsb_.mjs";import{t as createSubsystemLogger}from"./subsystem-DLCDFvCH.mjs";import{x as isPluginRegistryRetired}from"./registry-lifecycle-DGwblXFd.mjs";import{l as getActivePluginRegistry}from"./runtime-Cl6PvJHC.mjs";import{t as getPluginRuntimeGenerationRegistry}from"./generation-state-Bed3DvwS.mjs";import"./generation-scope-CxHnQlew.mjs";import{t as createHookRunner}from"./hooks-Bu2gwS_g.mjs"; //!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??[]}}}function getGlobalHookRunnerRegistry(){return resolveHookRegistry(hookRunnerGlobalState)?createLiveHookRegistryFacade():null} //!src/plugins/hook-runner-global.ts function initializeGlobalHookRunner(e){hookRunnerGlobalState.registry=e;ensureGlobalHookRunner()}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}function getGlobalHookRunner(){return hookRunnerGlobalState.hookRunner}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)}`)}}function resetGlobalHookRunner(){hookRunnerGlobalState.hookRunner=null;hookRunnerGlobalState.registry=null}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};