openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
7 lines • 10.1 kB
JavaScript
import{o as resolveGlobalSingleton}from"./global-singleton-DolL9xGt.mjs";import{i as getPluginExecutionFrame,s as runWithPluginExecutionFrame}from"./plugin-instance-invocation-na_CYyUz.mjs";import{a as withAgentDatabasePreparationContext}from"./agent-database-preparation-context-CPut921o.mjs";import{n as getPluginRegistryState}from"./runtime-state-BX08OLue.mjs";import{n as getPluginRuntimeExecutionFrame,t as PluginRuntimeExecutionFrame}from"./execution-frame-Dr3M9DJS.mjs";import{n as getPluginRuntimeLoadContextState}from"./load-context-state-30rh4Jdb.mjs";import{n as prepareGatewayContextBindingOwner,t as getGatewayContextBindingSlot}from"./gateway-context-binding-owner-D1XU3_jb.mjs";import{types}from"node:util";
//!src/plugins/plugin-return-value.ts
const promiseThen=Object.getOwnPropertyDescriptor(Promise.prototype,`then`);const promiseSpecies=Object.getOwnPropertyDescriptor(Promise,Symbol.species);function mapPluginReturnPromise(e,t,n){const r=e.then;const i=types.isPromise(e)&&Object.getPrototypeOf(e)===Promise.prototype?Object.getOwnPropertyDescriptor(e,`constructor`)??Object.getOwnPropertyDescriptor(Promise.prototype,`constructor`):void 0;const a=r===promiseThen?.value&&i?.value===Promise&&Object.getOwnPropertyDescriptor(Promise,Symbol.species)?.get===promiseSpecies?.get;return{value:Reflect.apply(r,e,n?[t,n]:[t]),host:a}}function resolvePluginReturnPromise(e){if(types.isPromise(e))return Promise.resolve(e);if(e===null||typeof e!==`object`&&typeof e!==`function`)return;let t;try{t=Reflect.get(e,`then`)}catch(e){return Promise.reject(e)}if(typeof t!==`function`)return;return Promise.resolve({then(n,r){Reflect.apply(t,e,[n,r])}})}
//!src/plugins/runtime/gateway-context-binding.ts
const{gatewayContextBindingKey,gatewayContextLifetimeKey,GatewayContextBinding,GatewayContextLifetime}=resolveGlobalSingleton(Symbol.for(`openclaw.gatewayContextBindings`),()=>{class Binding{#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 Lifetime{#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}}return{gatewayContextBindingKey:Symbol(`gatewayContextBinding`),gatewayContextLifetimeKey:Symbol(`gatewayContextLifetime`),GatewayContextBinding:Binding,GatewayContextLifetime:Lifetime}});function getGatewayContextBinding(e){const t=getGatewayContextBindingSlot(e);const n=t&&Object.getOwnPropertyDescriptor(t,gatewayContextBindingKey)?.value;return GatewayContextBinding.read(n,e)}function getGatewayContextLifetime(e){const t=Object.getOwnPropertyDescriptor(e,gatewayContextLifetimeKey)?.value;const n=GatewayContextLifetime.read(t,e);if(n)return n;const r=new GatewayContextLifetime(e);Object.defineProperty(e,gatewayContextLifetimeKey,{value:r});return GatewayContextLifetime.read(r,e)}function bindGatewayContextResolver(e,t){if(t){prepareGatewayContextBindingOwner(e);let n=getGatewayContextBinding(e);if(!n){const t=getGatewayContextBindingSlot(e);n=new GatewayContextBinding(e);Object.defineProperty(t,gatewayContextBindingKey,{value:n})}GatewayContextBinding.set(n,t)}}function getGatewayContextResolver(e){const t=getGatewayContextBinding(e);return t?GatewayContextBinding.get(t):void 0}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}}function hasGatewayContextOwner(e,t){const n=getGatewayContextResolver(e);return n!==void 0&&getCanonicalGatewayContextResolver(n)===t}function clearGatewayContextResolver(e){const t=getGatewayContextBinding(e);const n=t!==void 0&&GatewayContextBinding.get(t)!==void 0;if(t)GatewayContextBinding.set(t,void 0);return n}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}
//!src/plugins/runtime/gateway-request-scope.ts
function runWithPluginGatewayScope(e,t,n){const r=getPluginExecutionFrame();const i=n??r?.invocation;const a=getPluginRuntimeExecutionFrame(r);if(a?.gatewayScope===e&&a.invocation===i)return t();return runWithPluginExecutionFrame(new PluginRuntimeExecutionFrame(r??{},e,a?.generationRegistry,i),t)}const isNotWebchatConnect=()=>false;function settleRegistryScope(e,t){return mapPluginReturnPromise(e,e=>{t.length=0;return e},e=>{t.length=0;throw e}).value}const ExpiredPluginRegistryScopeError=resolveGlobalSingleton(Symbol.for(`openclaw.expiredPluginRegistryScopeError`),()=>class extends Error{constructor(){super(`Plugin registry scope is no longer available`)}});const borrowedRegistryProperty={enumerable:true,configurable:true};function bindBorrowedRegistry(e,t,n=false){let r=n;let i=t&&new WeakRef(t);borrowedRegistryProperty.get=()=>{const e=i?.deref();if(r||i&&!e)throw new ExpiredPluginRegistryScopeError;return e};borrowedRegistryProperty.set=e=>{i=e&&new WeakRef(e);r=false};Object.defineProperty(e,"pluginRegistry",borrowedRegistryProperty)}function copyGatewayScope(e,t=true){const n={isWebchatConnect:isNotWebchatConnect};if(!e)return n;for(const t of Object.keys(e))if(t!==`pluginRegistry`)Reflect.set(n,t,Reflect.get(e,t));for(const t of Object.getOwnPropertySymbols(e))if(Object.prototype.propertyIsEnumerable.call(e,t))Reflect.set(n,t,Reflect.get(e,t));if(t&&Object.prototype.propertyIsEnumerable.call(e,`pluginRegistry`))try{bindBorrowedRegistry(n,e.pluginRegistry)}catch(e){if(!(e instanceof ExpiredPluginRegistryScopeError))throw e;bindBorrowedRegistry(n,void 0,true)}return n}function getPluginRuntimeGatewayNodeAuthorities(){const e=getPluginRuntimeGatewayRequestScope();return{invokeWithSessionNodeAuthority:e?.invokeWithSessionNodeAuthority,nodePlacementGrantAuthority:e?.nodePlacementGrantAuthority}}function withPluginRuntimeGatewayRequestScope(e,t){return runWithPluginGatewayScope(e,t)}function withPluginRuntimeGatewayContextResolver(e,t,n){const r=n?.inheritRequestScope===false?void 0:getPluginRuntimeGatewayRequestScope();const i=copyGatewayScope(r);i.isWebchatConnect=r?.isWebchatConnect??isNotWebchatConnect;i.resolveGatewayContext=e;delete i.context;return withAgentDatabasePreparationContext(resolveAgentDatabasePreparation(e),()=>runWithPluginGatewayScope(i,t))}const resolveUnboundAgentDatabasePreparation=()=>void 0;const agentDatabasePreparationResolvers=/* @__PURE__ */ new WeakMap;function resolveAgentDatabasePreparation(e){if(!e)return resolveUnboundAgentDatabasePreparation;let resolve=agentDatabasePreparationResolvers.get(e);if(!resolve){resolve=()=>e()?.agentDatabaseStartup;agentDatabasePreparationResolvers.set(e,resolve)}return resolve}function withPluginRuntimeRegistryScope(e,t,n){if(!e)return t();return runWithPluginGatewayScope(createRegistryScope(e,getPluginRuntimeGatewayRequestScope(),n),()=>{const n=t();const r=resolvePluginReturnPromise(n);return r?settleRegistryScope(r,[e]):n})}function createRegistryScope(e,t,n){const r=copyGatewayScope(t,false);let i;if(n===void 0&&t)try{if(t.pluginRegistry===e)i=t.declaredProviderOwners}catch(e){if(!(e instanceof ExpiredPluginRegistryScopeError))throw e}bindBorrowedRegistry(r,e);r.declaredProviderOwners=n??i??getPluginRuntimeLoadContextState(e)?.declaredProviderOwners;return r}function applyPluginScope(e,t){e.pluginId=t.pluginId;if(t.pluginSource!==void 0)e.pluginSource=t.pluginSource;else delete e.pluginSource;if(t.pluginOrigin!==void 0)e.pluginOrigin=t.pluginOrigin;else delete e.pluginOrigin;if(t.pluginTrustedOfficialInstall!==void 0)e.pluginTrustedOfficialInstall=t.pluginTrustedOfficialInstall;else delete e.pluginTrustedOfficialInstall}function withPluginRuntimePluginScope(e,t,n,r){const i=getPluginRuntimeGatewayRequestScope();const a=n?createRegistryScope(n,i):copyGatewayScope(i);applyPluginScope(a,e);return runWithPluginGatewayScope(a,t,r)}function runOutsidePluginRuntimeRegistryScope(e){const t=getPluginRuntimeGatewayRequestScope();if(!t)return e();const n=copyGatewayScope(t,false);n.pluginRegistry=void 0;n.declaredProviderOwners=void 0;return runWithPluginGatewayScope(n,e)}function getPluginRuntimeGatewayRequestScope(){return getPluginRuntimeExecutionFrame()?.gatewayScope}function getPluginRegistryForContext(){const e=getPluginRegistryState();return e?.registrationContext?.registry??getPluginRuntimeGatewayRequestScope()?.pluginRegistry??e?.activeRegistry??null}function getInProcessGatewayRequestContext(e){if(e)return e();const t=getPluginRuntimeGatewayRequestScope();return t?.resolveGatewayContext?t.resolveGatewayContext():t?.context}export{getGatewayContextResolver as _,getPluginRuntimeGatewayNodeAuthorities as a,mapPluginReturnPromise as b,withPluginRuntimeGatewayContextResolver as c,withPluginRuntimeRegistryScope as d,bindGatewayContextResolver as f,getGatewayContextLifetime as g,getCanonicalGatewayContextResolver as h,getPluginRegistryForContext as i,withPluginRuntimeGatewayRequestScope as l,clearGatewayContextResolver as m,createRegistryScope as n,getPluginRuntimeGatewayRequestScope as o,captureGatewayContextResolver as p,getInProcessGatewayRequestContext as r,runOutsidePluginRuntimeRegistryScope as s,ExpiredPluginRegistryScopeError as t,withPluginRuntimePluginScope as u,getSharedGatewayContextResolver as v,resolvePluginReturnPromise as x,hasGatewayContextOwner as y};