UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

135 lines (134 loc) • 4.31 kB
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs"; import { n as prepareGatewayContextBindingOwner, t as getGatewayContextBindingSlot } from "./gateway-context-binding-owner-BGZ2bJF4.mjs"; //#region src/plugins/runtime/gateway-context-binding.ts const { bindGatewayContextResolver, getGatewayContextLifetime, getGatewayContextResolver, clearGatewayContextResolver } = resolveGlobalSingleton(Symbol.for("openclaw.gatewayContextBindings"), () => { const e = Symbol("gatewayContextBinding"); const t = Symbol("gatewayContextLifetime"); class GatewayContextBinding { #e; #t; constructor(e) { this.#e = e; Object.setPrototypeOf(this, null); } static read(e, t) { return typeof e === "object" && e !== null && #e in e && e.#e === t ? e : void 0; } static get(e) { return e.#t; } static set(e, t) { e.#t = t; } } class GatewayContextLifetime { #e; #t = new AbortController(); constructor(e) { this.#e = e; Object.setPrototypeOf(this, null); } static read(e, t) { return typeof e === "object" && e !== null && #e in e && e.#e === t ? e.#t : void 0; } } function getGatewayContextBinding(t) { const n = getGatewayContextBindingSlot(t); const r = n && Object.getOwnPropertyDescriptor(n, e)?.value; return GatewayContextBinding.read(r, t); } function getLifetime(e) { const n = Object.getOwnPropertyDescriptor(e, t)?.value; const r = GatewayContextLifetime.read(n, e); if (r) return r; const i = new GatewayContextLifetime(e); Object.defineProperty(e, t, { value: i }); return GatewayContextLifetime.read(i, e); } function bindResolver(t, n) { if (n) { prepareGatewayContextBindingOwner(t); let r = getGatewayContextBinding(t); if (!r) { const n = getGatewayContextBindingSlot(t); r = new GatewayContextBinding(t); Object.defineProperty(n, e, { value: r }); } GatewayContextBinding.set(r, n); } } function readResolver(e) { const t = getGatewayContextBinding(e); return t ? GatewayContextBinding.get(t) : void 0; } function clearResolver(e) { const t = getGatewayContextBinding(e); const n = t !== void 0 && GatewayContextBinding.get(t) !== void 0; if (t) GatewayContextBinding.set(t, void 0); return n; } return { bindGatewayContextResolver: bindResolver, getGatewayContextLifetime: getLifetime, getGatewayContextResolver: readResolver, clearGatewayContextResolver: clearResolver }; }); /** Follows explicit wrapper ownership without invoking any execution resolver. */ function getCanonicalGatewayContextResolver(e) { const t = /* @__PURE__ */ new Set(); let n = e; while (!t.has(n)) { t.add(n); const e = getGatewayContextResolver(n); if (!e) return n; n = e; } } /** Match the host owner without invoking a possibly retired execution resolver. */ function hasGatewayContextOwner(e, t) { const n = getGatewayContextResolver(e); return n !== void 0 && getCanonicalGatewayContextResolver(n) === t; } function getSharedGatewayContextResolver(e) { const t = e.map(getGatewayContextResolver); if (t.every((e) => !e)) return; const shared = () => { const e = t.map((e) => { try { return e?.(); } catch { return; } }); if (t.some((e) => !e)) throw new Error("incompatible Gateway bindings: bound and unbound owners"); if (e.some((e) => !e)) return; if (e.some((t) => t !== e[0])) throw new Error("incompatible Gateway instances"); return e[0]; }; const n = t.map((e) => e ? getCanonicalGatewayContextResolver(e) : void 0); const r = n[0]; if (r && n.every((e) => e === r)) bindGatewayContextResolver(shared, r); return shared; } function captureGatewayContextResolver(e) { if (!e) return; let t; try { t = e(); } catch { return () => void 0; } if (!t) return () => void 0; const resolveAdmittedContext = () => { try { return e() === t ? t : void 0; } catch { return; } }; bindGatewayContextResolver(resolveAdmittedContext, t.resolveGatewayContext); return resolveAdmittedContext; } //#endregion export { getGatewayContextLifetime as a, hasGatewayContextOwner as c, getCanonicalGatewayContextResolver as i, captureGatewayContextResolver as n, getGatewayContextResolver as o, clearGatewayContextResolver as r, getSharedGatewayContextResolver as s, bindGatewayContextResolver as t };