UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

104 lines (103 loc) 4.97 kB
import { i as resolveGlobalSingleton } from "./global-singleton-Dc_stLtU.js"; import { AsyncLocalStorage } from "node:async_hooks"; //#region src/plugins/runtime/gateway-request-scope.ts const PLUGIN_RUNTIME_GATEWAY_REQUEST_SCOPE_KEY = Symbol.for("openclaw.pluginRuntimeGatewayRequestScope"); const GATEWAY_CONTEXT_RESOLVERS_KEY = Symbol.for("openclaw.gatewayContextResolvers"); const pluginRuntimeGatewayRequestScope = resolveGlobalSingleton(PLUGIN_RUNTIME_GATEWAY_REQUEST_SCOPE_KEY, () => new AsyncLocalStorage()); const gatewayContextResolvers = resolveGlobalSingleton(GATEWAY_CONTEXT_RESOLVERS_KEY, () => /* @__PURE__ */ new WeakMap()); function bindGatewayContextResolver(owner, resolver) { if (resolver) gatewayContextResolvers.set(owner, resolver); } const getGatewayContextResolver = (owner) => gatewayContextResolvers.get(owner); /** Match the host owner without invoking a possibly retired execution resolver. */ function hasGatewayContextOwner(owner, gatewayOwner) { const resolver = gatewayContextResolvers.get(owner); return resolver !== void 0 && (gatewayContextResolvers.get(resolver) ?? resolver) === gatewayOwner; } const clearGatewayContextResolver = (owner) => gatewayContextResolvers.delete(owner); /** Carry only closure-bound node authorities into a nested request scope. */ function getPluginRuntimeGatewayNodeAuthorities() { const scope = pluginRuntimeGatewayRequestScope.getStore(); return { invokeWithSessionNodeAuthority: scope?.invokeWithSessionNodeAuthority, nodePlacementGrantAuthority: scope?.nodePlacementGrantAuthority }; } function getSharedGatewayContextResolver(owners) { const resolvers = owners.map(getGatewayContextResolver); if (resolvers.every((resolve) => !resolve)) return; return () => { const contexts = resolvers.map((resolve) => { try { return resolve?.(); } catch { return; } }); if (resolvers.some((resolve) => !resolve)) throw new Error("incompatible Gateway bindings: bound and unbound owners"); if (contexts.some((context) => !context)) return; if (contexts.some((context) => context !== contexts[0])) throw new Error("incompatible Gateway instances"); return contexts[0]; }; } /** * Runs plugin gateway handlers with request-scoped context that runtime helpers can read. */ function withPluginRuntimeGatewayRequestScope(scope, run) { return pluginRuntimeGatewayRequestScope.run(scope, run); } /** Runs detached work with its captured Gateway binding, including an explicitly unbound owner. */ function withPluginRuntimeGatewayContextResolver(resolveGatewayContext, run, options) { const current = options?.inheritRequestScope === false ? void 0 : pluginRuntimeGatewayRequestScope.getStore(); const scoped = { ...current, isWebchatConnect: current?.isWebchatConnect ?? (() => false), resolveGatewayContext }; delete scoped.context; return pluginRuntimeGatewayRequestScope.run(scoped, run); } /** Runs work against an owned registry handle while preserving any gateway request facts. */ function withPluginRuntimeRegistryScope(registry, run) { if (!registry) return run(); const current = pluginRuntimeGatewayRequestScope.getStore(); return pluginRuntimeGatewayRequestScope.run({ isWebchatConnect: () => false, ...current, pluginRegistry: registry }, run); } /** * Runs work under the current gateway request scope while attaching plugin identity. */ function withPluginRuntimePluginScope(scope, run) { const current = pluginRuntimeGatewayRequestScope.getStore(); const scoped = current ? { ...current, pluginId: scope.pluginId } : { pluginId: scope.pluginId, isWebchatConnect: () => false }; if (scope.pluginSource !== void 0) scoped.pluginSource = scope.pluginSource; else delete scoped.pluginSource; if (scope.pluginOrigin !== void 0) scoped.pluginOrigin = scope.pluginOrigin; else delete scoped.pluginOrigin; if (scope.pluginTrustedOfficialInstall !== void 0) scoped.pluginTrustedOfficialInstall = scope.pluginTrustedOfficialInstall; else delete scoped.pluginTrustedOfficialInstall; return pluginRuntimeGatewayRequestScope.run(scoped, run); } /** * Runs work under the current gateway request scope while attaching plugin identity. */ function withPluginRuntimePluginIdScope(pluginId, run) { return withPluginRuntimePluginScope({ pluginId }, run); } /** * Returns the current plugin gateway request scope when called from a plugin request handler. */ function getPluginRuntimeGatewayRequestScope() { return pluginRuntimeGatewayRequestScope.getStore(); } //#endregion export { getPluginRuntimeGatewayRequestScope as a, withPluginRuntimeGatewayContextResolver as c, withPluginRuntimePluginScope as d, withPluginRuntimeRegistryScope as f, getPluginRuntimeGatewayNodeAuthorities as i, withPluginRuntimeGatewayRequestScope as l, clearGatewayContextResolver as n, getSharedGatewayContextResolver as o, getGatewayContextResolver as r, hasGatewayContextOwner as s, bindGatewayContextResolver as t, withPluginRuntimePluginIdScope as u };