UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 2.68 kB
import{i as e}from"./transcript-global-singleton-DIJgobpU.mjs";import{j as t}from"./transcript-plugin-instance-invocation-7pP9ZY8b.mjs";import{n}from"./transcript-error-graph-internal-pLXvNI1L.mjs";import"./transcript-detached-async-context-BKXaSw-L.mjs";import{i as r}from"./transcript-src-DFL7yLCI.mjs";import{t as i}from"./transcript-deferred-B75K7ut7.mjs";import{AsyncLocalStorage as a}from"node:async_hooks";async function settlesWithin(e,t){return await r(e.then(()=>!0),t,()=>!1,{ref:!1})}const o=e(Symbol.for(`openclaw.asyncWorkScope`),()=>new a),s=e(Symbol.for(`openclaw.asyncWorkScopeAncestry`),()=>new a),c=e(Symbol.for(`openclaw.asyncWorkScopeClosedError`),()=>class extends Error{constructor(){super(`Async work scope is closed`),this.name=`AsyncWorkScopeClosedError`}});var l=class AsyncWorkScope{constructor(e){this.failures=e,this.pending=/* @__PURE__ */ new Set,this.controller=new AbortController,this.phase=`open`}get signal(){return this.controller.signal}get hasPendingWork(){return this.pending.size>0}get isClosing(){return this.phase!==`open`}enter(e){let n=o.getStore();if(n===this)return e();let r=s.getStore(),i=n?r?.scope===n?r:{scope:n}:void 0;return t(o,this,()=>(s.enterWith({scope:this,parent:i}),e()))}run(e){if(this.phase===`closed`)throw new c;if(this.pending.size>0)return this.enter(e);let t=i();this.pending.add(t.promise);try{return this.enter(e)}finally{t.resolve(),this.pending.delete(t.promise)}}track(e){if(this.phase===`closed`)return Promise.reject(new c);let t=i();this.pending.add(t.promise),t.promise.then(()=>this.pending.delete(t.promise),e=>{this.pending.delete(t.promise),this.failures&&(n(e),this.failures.add(e))});try{t.resolve(this.enter(e))}catch(e){t.reject(e)}return t.promise}beginClose(e){this.phase===`open`&&(this.phase=`closing`,this.controller.abort(e),n(this.controller.signal.reason))}runWhenIdle(e){return AsyncWorkScope.runWhenAllIdle(()=>[this],()=>this.track(e))}static async runWhenAllIdle(e,t){let n=e();for(;n.some(e=>e.pending.size>0);)await Promise.allSettled(n.flatMap(e=>Array.from(e.pending))),n=e();return t()}async drain(){for(this.beginClose();this.pending.size>0;)await Promise.allSettled(this.pending);this.phase=`closed`}};e(Symbol.for(`openclaw.gatewayRetainedWorkTimeoutError`),()=>class extends Error{constructor(){super(`Retained Gateway work did not settle after abort; resources remain retained`)}});function runOutsideAsyncWorkScope(e){return o.getStore()===void 0?s.getStore()===void 0?e():s.exit(e):t(o,void 0,()=>(s.enterWith(void 0),e()))}function getAsyncWorkSignal(){return o.getStore()?.signal}export{settlesWithin as i,getAsyncWorkSignal as n,runOutsideAsyncWorkScope as r,l as t};