UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

106 lines (105 loc) • 4.57 kB
import { c as normalizeOptionalLowercaseString } from "./string-coerce-CCIiB4H7.mjs"; import { r as resolveGlobalDedupeCache } from "./dedupe-DxT0h2gR.mjs"; import { _ as wrapCurrentPluginInstance } from "./plugin-instance-scope-oezVdCr2.mjs"; import { _ as requireActivePluginChannelRegistry, b as resolveDirectPluginRegistrationOwner, p as getPluginRegistrationContext } from "./runtime-ezCEpumg.mjs"; //#region src/plugins/interactive-state.ts const PLUGIN_INTERACTIVE_STATE_KEY = Symbol.for("openclaw.pluginInteractiveState"); const PLUGIN_INTERACTIVE_CALLBACK_DEDUPE_KEY = Symbol.for("openclaw.pluginInteractiveCallbackDedupe"); function getState() { const e = globalThis; const t = e[PLUGIN_INTERACTIVE_STATE_KEY]; const n = typeof t === "object" && t !== null ? t : void 0; const r = { callbackDedupe: resolveGlobalDedupeCache(PLUGIN_INTERACTIVE_CALLBACK_DEDUPE_KEY, { ttlMs: 3e5, maxSize: 4096 }), inflightCallbackDedupe: n?.inflightCallbackDedupe instanceof Set ? n.inflightCallbackDedupe : /* @__PURE__ */ new Set() }; e[PLUGIN_INTERACTIVE_STATE_KEY] = r; return r; } /** Claims an interactive callback dedupe key while the callback is in flight. */ function claimPluginInteractiveCallbackDedupe(e, t = Date.now()) { if (!e) return true; const n = getState(); if (n.inflightCallbackDedupe.has(e) || n.callbackDedupe.peek(e, t)) return false; n.inflightCallbackDedupe.add(e); return true; } /** Commits an interactive callback dedupe key after successful handling. */ function commitPluginInteractiveCallbackDedupe(e, t = Date.now()) { if (!e) return; const n = getState(); n.inflightCallbackDedupe.delete(e); n.callbackDedupe.check(e, t); } /** Releases an in-flight interactive callback dedupe claim without committing it. */ function releasePluginInteractiveCallbackDedupe(e) { if (!e) return; getState().inflightCallbackDedupe.delete(e); } /** Clears plugin interactive handlers and callback dedupe state. */ function clearPluginInteractiveHandlersState() { const e = getState(); e.callbackDedupe.clear(); e.inflightCallbackDedupe.clear(); } //#endregion //#region src/plugins/interactive-registry.ts function toPluginInteractiveRegistryKey(e, t) { return `${normalizeOptionalLowercaseString(e) ?? ""}:${t.trim()}`; } /** Resolves a handler from registry-owned registrations without changing global state. */ function resolvePluginInteractiveRegistrationsMatch(e, t, n) { const r = n.trim(); if (!r) return null; const i = r.indexOf(":"); const a = i >= 0 ? r.slice(0, i) : r; const o = toPluginInteractiveRegistryKey(t, a); const s = e.find((e) => toPluginInteractiveRegistryKey(e.channel, e.namespace) === o); return s ? { registration: s, namespace: a, payload: i >= 0 ? r.slice(i + 1) : "" } : null; } /** Registers one handler whose lifetime follows its owning plugin registry. */ function registerPluginInteractiveHandlerInRegistry(e, t, n, r) { const i = e.interactiveHandlers; const a = n.namespace.trim(); if (!a) return { ok: false, error: "Interactive handler namespace cannot be empty" }; if (!/^[A-Za-z0-9._-]+$/.test(a)) return { ok: false, error: "Interactive handler namespace must contain only letters, numbers, dots, underscores, and hyphens" }; const o = toPluginInteractiveRegistryKey(n.channel, a); const s = i.find((e) => toPluginInteractiveRegistryKey(e.channel, e.namespace) === o); if (s) return { ok: false, error: `Interactive handler namespace "${a}" already registered by plugin "${s.pluginId}"` }; i.push({ ...wrapCurrentPluginInstance(n), namespace: a, channel: normalizeOptionalLowercaseString(n.channel) ?? "", pluginId: t, pluginName: r?.pluginName, pluginRoot: r?.pluginRoot }); return { ok: true }; } /** Registers one process-global interactive handler. */ function registerPluginInteractiveHandler(e, t, n) { return registerPluginInteractiveHandlerInRegistry(getPluginRegistrationContext()?.registry ?? requireActivePluginChannelRegistry(), resolveDirectPluginRegistrationOwner(e) ?? e, t, n); } /** Clears all active plugin interactive handlers. */ function clearPluginInteractiveHandlers() { requireActivePluginChannelRegistry().interactiveHandlers.length = 0; clearPluginInteractiveHandlersState(); } //#endregion export { claimPluginInteractiveCallbackDedupe as a, resolvePluginInteractiveRegistrationsMatch as i, registerPluginInteractiveHandler as n, commitPluginInteractiveCallbackDedupe as o, registerPluginInteractiveHandlerInRegistry as r, releasePluginInteractiveCallbackDedupe as s, clearPluginInteractiveHandlers as t };