openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 5.03 kB
JavaScript
import{o as resolveGlobalSingleton}from"./global-singleton-DolL9xGt.mjs";import{t as createDeferredCore}from"./deferred-QsZCoVH-.mjs";import{o as getAsyncWorkSignal,t as AsyncWorkScope}from"./async-work-scope-DBcxYU4O.mjs";import{a as pluginInstanceInvocation}from"./plugin-instance-invocation-na_CYyUz.mjs";import{h as getCanonicalGatewayContextResolver,o as getPluginRuntimeGatewayRequestScope,x as resolvePluginReturnPromise}from"./gateway-request-scope-7deHmsb_.mjs";import{t as bindPluginSharedStateAdmission}from"./plugin-shared-state-admission-Bu1GRtk3.mjs";import{i as hasRetainedPluginRuntimeCloseError}from"./runtime-close-error-Ct99vNsO.mjs";import{AsyncLocalStorage}from"node:async_hooks";
//!src/plugins/legacy-sdk-resource-host.ts
var LegacyPluginSdkResourceHost=class{constructor(e){this.replyDispatch=e;this.work=new AsyncWorkScope;this.claims=/* @__PURE__ */ new Map;this.providerProjections=/* @__PURE__ */ new WeakMap;this.pending=/* @__PURE__ */ new Set;this.failures=[]}invokeReplyDispatch(e){return this.invoke(()=>this.trackSharedStateWork(()=>{if(!this.replyDispatch)throw new Error(`Plugin SDK resource host has no reply dispatch runtime`);return e(this.replyDispatch)}))}get scheduler(){this.assertOpen();if(!this.gatewayScheduler)throw new Error(`Plugin SDK resource host has no Gateway scheduler`);this.gatewayScheduler.signal.throwIfAborted();return this.gatewayScheduler}bindScheduler(e){this.assertOpen();e.signal.throwIfAborted();if(this.gatewayScheduler&&this.gatewayScheduler!==e)throw new Error(`Plugin SDK resource host already belongs to another Gateway scheduler`);this.gatewayScheduler=e}assertOpen(){if(this.closing||this.work.isClosing)throw new Error(`Plugin SDK resource host is closed`)}run(e){return hostContext.run(this,e)}track(e){this.assertOpen();return this.work.track(()=>this.run(e))}assertSharedStateAdmission(){const e=sharedStateWorkContext.getStore();if(e?.host===this&&e.active)return;if(this.closing||this.work.isClosing)throw Object.assign(/* @__PURE__ */ new Error(`Gateway is closing; plugin shared-state admission is closed`),{code:`STATE_DATABASE_READ_ADMISSION_INVALIDATED`})}trackSharedStateWork(e){this.assertSharedStateAdmission();const t={host:this,active:true};return this.work.track(async()=>{try{return await sharedStateWorkContext.run(t,e)}finally{t.active=false}})}invoke(e){if(getAsyncWorkSignal()!==this.work.signal)this.assertOpen();return this.work.run(()=>this.run(()=>{const t=e();const n=resolvePluginReturnPromise(t);return n?this.work.track(()=>n):t}))}adopt(e,t){this.assertOpen();if(this.claims.has(e))this.releaseClaim(t);else this.claims.set(e,t)}getProviderProjection(e,t){this.assertOpen();let n=this.providerProjections.get(e);if(!n){n=t();this.providerProjections.set(e,n)}return n}forgetProviderProjection(e,t){if(this.providerProjections.get(e)===t)this.providerProjections.delete(e)}releaseClaim(e){const t=createDeferredCore();const n=t.promise.catch(e=>{this.failures.push(e)}).finally(()=>{this.pending.delete(n)});this.pending.add(n);t.resolve(AsyncWorkScope.runWhenAllIdle(()=>[this.work],()=>e.release()))}async drainWork(){await this.work.drain();await this.drainPendingReleases();if(this.failures.some(hasRetainedPluginRuntimeCloseError))throw new AggregateError(this.failures,`Plugin SDK resources could not all be disposed`)}async drainPendingReleases(){while(this.pending.size>0)await Promise.all(this.pending)}close(){if(!this.closing)this.closing=Promise.resolve().then(async()=>{await this.drainWork();const e=[...this.claims.values()];this.claims.clear();for(const t of e)this.releaseClaim(t);await this.drainPendingReleases();if(this.failures.length>0)throw new AggregateError(this.failures,`Plugin SDK resources could not all be disposed`)});return this.closing}};const{hostContext,gatewayHosts,sharedStateWorkContext}=resolveGlobalSingleton(Symbol.for(`openclaw.legacyPluginSdkResourceHosts`),()=>({hostContext:new AsyncLocalStorage,sharedStateWorkContext:new AsyncLocalStorage,gatewayHosts:/* @__PURE__ */ new WeakMap}));const STANDALONE_HOST_KEY=Symbol.for(`openclaw.legacyPluginSdkStandaloneResourceHost`);function bindLegacyPluginSdkResourceHost(e,t){gatewayHosts.set(e,t)}function getBoundLegacyPluginSdkResourceHost(){const e=getPluginRuntimeGatewayRequestScope();const t=e?.resolveGatewayContext??e?.context?.resolveGatewayContext;if(t){const e=getCanonicalGatewayContextResolver(t);const n=e?gatewayHosts.get(e):void 0;if(!n)throw new Error(`Gateway SDK resource host is not bound`);return n}const n=hostContext.getStore();return n===globalThis[STANDALONE_HOST_KEY]?void 0:n}function getLegacyPluginSdkResourceHost(){return getBoundLegacyPluginSdkResourceHost()??resolveGlobalSingleton(STANDALONE_HOST_KEY,()=>new LegacyPluginSdkResourceHost)}bindPluginSharedStateAdmission(()=>pluginInstanceInvocation.getStore()?getBoundLegacyPluginSdkResourceHost():void 0);export{getLegacyPluginSdkResourceHost as i,bindLegacyPluginSdkResourceHost as n,getBoundLegacyPluginSdkResourceHost as r,LegacyPluginSdkResourceHost as t};