UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

93 lines (92 loc) • 3.81 kB
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs"; import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs"; import { t as createSubsystemLogger } from "./subsystem-BHJiy01V.mjs"; import { l as getActivePluginRegistry, p as getPluginRegistrationContext, v as requireActivePluginRegistry } from "./runtime-ezCEpumg.mjs"; //#region 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; } /** Register for a family (e.g. "command") or an exact action (e.g. "command:new"). */ 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; } /** Dispatch family handlers before exact-action handlers, in registration order; isolate errors. */ 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: omitHookContextOperatorSources(r), timestamp: /* @__PURE__ */ new Date(), messages: [] }; } /** Hook payloads must not disclose the private source used to recover a person. */ function omitHookContextOperatorSources(e) { if (!("sessionEntry" in e) && !("previousSessionEntry" in e)) return e; const t = { ...e }; for (const e of ["sessionEntry", "previousSessionEntry"]) { const n = t[e]; if (isRecord(n) && Object.hasOwn(n, "operatorSources")) { const { operatorSources: r, ...i } = n; t[e] = i; } } 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"); } //#endregion export { isGatewayStartupEvent as a, triggerInternalHook as c, isAgentBootstrapEvent as i, unregisterInternalHook as l, createInternalHookEvent as n, registerInternalHook as o, hasInternalHookListeners as r, setInternalHooksEnabled as s, clearInternalHooks as t };