openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 3.25 kB
JavaScript
import{o as resolveGlobalSingleton}from"./global-singleton-DolL9xGt.mjs";import{l as isRecord}from"./record-coerce-BTbqdJ3-.mjs";import{t as formatErrorMessage}from"./errors-D37BJTfI.mjs";import{t as createSubsystemLogger}from"./subsystem-DLCDFvCH.mjs";import{l as getActivePluginRegistry,p as getPluginRegistrationContext,v as requireActivePluginRegistry}from"./runtime-Cl6PvJHC.mjs";
//!src/hooks/internal-hooks.ts
const handlers=resolveGlobalSingleton(Symbol.for(`openclaw.internalHookHandlers`),()=>/* @__PURE__ */ new Map);const internalHooksEnabledState=resolveGlobalSingleton(Symbol.for(`openclaw.internalHooksEnabled`),()=>({enabled:true}));const log=createSubsystemLogger(`internal-hooks`);function listActiveRegistrations(){return getActivePluginRegistry()?.legacyInternalHooks??[]}function listLegacyPluginInternalHooks(e){return listActiveRegistrations().filter(t=>t.event===e).map(e=>e.handler)}function clearLegacyPluginInternalHooks(){const e=getPluginRegistrationContext();(e?.registry??requireActivePluginRegistry()).legacyInternalHooks.length=0}function registerInternalHook(e,t){if(!handlers.has(e))handlers.set(e,[]);handlers.get(e).push(t)}function unregisterInternalHook(e,t){const n=handlers.get(e);if(!n)return;const r=n.indexOf(t);if(r!==-1)n.splice(r,1);if(n.length===0)handlers.delete(e)}function clearInternalHooks(){handlers.clear();clearLegacyPluginInternalHooks()}function setInternalHooksEnabled(e){internalHooksEnabledState.enabled=e}function hasInternalHookListeners(e,t){return(handlers.get(e)?.length??0)+listLegacyPluginInternalHooks(e).length>0||(handlers.get(`${e}:${t}`)?.length??0)+listLegacyPluginInternalHooks(`${e}:${t}`).length>0}async function triggerInternalHook(e){if(!internalHooksEnabledState.enabled)return;const t=`${e.type}:${e.action}`;const n=[...handlers.get(e.type)??[],...listLegacyPluginInternalHooks(e.type),...handlers.get(t)??[],...listLegacyPluginInternalHooks(t)];for(const t of n)try{await t(e)}catch(t){const n=formatErrorMessage(t);log.error(`Hook error [${e.type}:${e.action}]: ${n}`)}}function createInternalHookEvent(e,t,n,r={}){return{type:e,action:t,sessionKey:n,context:`sessionEntry`in r||`previousSessionEntry`in r?omitHookContextOperatorSources(r):r,timestamp:/* @__PURE__ */ new Date,messages:[]}}function omitHookSessionOperatorSources(e){if(!isRecord(e)||!Object.hasOwn(e,`operatorSources`))return e;const{operatorSources:t,...n}=e;return n}function omitHookContextOperatorSources(e){const t={...e};for(const e of[`sessionEntry`,`previousSessionEntry`])if(e in t)t[e]=omitHookSessionOperatorSources(t[e]);return t}function hasHookEventContext(e,t,n){return e.type===t&&e.action===n&&e.context!==null&&typeof e.context===`object`}function isAgentBootstrapEvent(e){return hasHookEventContext(e,`agent`,`bootstrap`)&&typeof e.context.workspaceDir===`string`&&Array.isArray(e.context.bootstrapFiles)}function isGatewayStartupEvent(e){return hasHookEventContext(e,`gateway`,`startup`)}export{isGatewayStartupEvent as a,setInternalHooksEnabled as c,isAgentBootstrapEvent as i,triggerInternalHook as l,createInternalHookEvent as n,omitHookSessionOperatorSources as o,hasInternalHookListeners as r,registerInternalHook as s,clearInternalHooks as t,unregisterInternalHook as u};