UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 2.85 kB
import{o as resolveGlobalSingleton}from"./global-singleton-DolL9xGt.mjs";import{t as captureEffectAuthority}from"./effect-authority-CpQjcEYQ.mjs";import{AsyncLocalStorage}from"node:async_hooks"; //!src/shared/channel-read-authority.ts const completionKey=Symbol.for(`openclaw.channelReadAuthority.completion`);const authorityScope=resolveGlobalSingleton(Symbol.for(`openclaw.channelReadAuthority`),()=>new AsyncLocalStorage);function captureChannelReadAuthority(){return authorityScope.getStore()}function captureChannelReadScope(){return authorityScope.getStore()?.[completionKey]}async function settleChannelReadResource(e,t){try{await e.settle(t)}catch(e){try{const[{createSubsystemLogger:t},{redactToolPayloadText:n}]=await Promise.all([import(`./subsystem-DyP22TCO.mjs`),import(`./redact-DwQ-ukUh.mjs`)]);const r=t(`channels`);const i=e instanceof AggregateError?e.errors:[e];for(const e of i)r.error(`Channel read output cleanup failed: ${n(String(e))}`)}catch{console.error(`Channel read output cleanup failed; diagnostic logger unavailable.`)}}}async function settleReadResources(e,t){await Promise.all(Array.from(e,e=>settleChannelReadResource(e,t)));e.clear()}async function withChannelReadAuthority(e,t,n,r){if(!e){const e=await t();r?.(e);return e}const i=captureEffectAuthority();const a=authorityScope.getStore();const o=a?.[completionKey];const s=[o?.signal,n].filter(e=>Boolean(e));const c=s.length>1?AbortSignal.any(s):s[0];const l=/* @__PURE__ */ new Set;let u=true;const assertAuthority=()=>{a?.();if(!u)throw new Error(`Channel read authority is no longer active.`);c?.throwIfAborted();e();for(const e of l)e.assertCurrent?.()};const d=Object.assign(assertAuthority,{[completionKey]:{assertCurrent:assertAuthority,signal:c,registerResource:t=>{assertAuthority();l.add({key:t.key,settle:t.settle,onDelegated:t.onDelegated,assertCurrent:()=>{c?.throwIfAborted();e();t.assertCurrent?.()}})},delegateResource:(e,t,n)=>{assertAuthority();const r=Array.from(l).find(t=>t.key===e);if(!r)return o?.delegateResource(e,t,n)??false;const i=r.settle;const a=r.assertCurrent;r.onDelegated?.(n);r.settle=e=>t(e,i);r.assertCurrent=()=>{a?.();n?.()};return true},discardResource:async e=>{const t=Array.from(l).find(t=>t.key===e);if(!t)return await o?.discardResource(e)??false;await settleChannelReadResource(t,false);l.delete(t);return true}}});assertAuthority();try{let e;try{e=await authorityScope.run(d,t)}catch(e){await i.initiate(assertAuthority);throw e}await i.initiate(()=>{assertAuthority();if(o){for(const e of l)o.registerResource(e);l.clear()}r?.(e);u=false});if(l.size>0)await settleReadResources(l,true);return e}catch(e){u=false;if(l.size>0)await settleReadResources(l,false);throw e}finally{u=false}}export{withChannelReadAuthority as i,captureChannelReadScope as n,settleChannelReadResource as r,captureChannelReadAuthority as t};