UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 10.7 kB
import{i as e}from"./transcript-global-singleton-DIJgobpU.mjs";import"./transcript-plugin-instance-invocation-7y9lfaeO.mjs";import{t}from"./transcript-detached-async-context-BKXaSw-L.mjs";import{x as n}from"./transcript-sqlite-error-diagnostics-CLL4Z7ly.mjs";import{t as r}from"./transcript-subsystem-C0UTtuvS.mjs";import{r as i}from"./transcript-src-gRxwdYZM.mjs";import{C as a,rt as o,x as s}from"./transcript-sqlite-readonly-worker-cXU3LbpW.mjs";import{h as c,n as l}from"./transcript-openclaw-state-worker-error-CXQGmrMf.mjs";import{f as u}from"./transcript-sqlite-post-commit-B_pko32i.mjs";import{t as d}from"./transcript-runtime-process-url-DysJEE9m.mjs";import{h as f}from"./transcript-openclaw-agent-db-resources-W6NYYBYU.mjs";import{X as p,Z as m,f as h,g,h as _,m as v,p as y,t as b,u as x}from"./transcript-openclaw-state-db-cache-kzfc9rd-.mjs";import{u as S}from"./transcript-sqlite-snapshot-staging-owner-B-Am-HqY.mjs";import{t as C}from"./transcript-sqlite-handle-lifecycle-fOjKc_YY.mjs";import{n as w}from"./transcript-acquire-with-wait-18yu6N4Q.mjs";import{c as T,i as E,l as D,n as O,o as k,r as A,t as j}from"./transcript-sqlite-worker-store-y0VcAwYV.mjs";import{AsyncLocalStorage as M}from"node:async_hooks";import{isDeepStrictEqual as N}from"node:util";function assertOpenClawStateWorkerActorPath(e){if(h.getKnownOpenClawStateDatabaseIdentity(e.databasePath)?.key!==e.key)throw new f(`Shared-state worker opening path changed`)}function runWithCapturedWorkerContext(e,t){let n=e.maintenanceScope,run=()=>n?n.run(()=>n.track(t())):t();return e.runInCapturedSchemaScope?e.runInCapturedSchemaScope(run):run()}function runWithOpenClawStateWorkerStore(e,t,n,r,i,a){let{admission:o}=t,s=O(e);return D(e,e=>n(a?{execute:(t,n)=>e.execute(t,{...n,signal:n?.signal?AbortSignal.any([a,n.signal]):a})}:e),t,e=>{o.assertCurrent(),assertOpenClawStateWorkerActorPath(s),r?.(e)},i)}function assertOpenClawStateWorkerSchemaContext(e){if(m(e.admission.databasePath),p()!==e.existingSchemaPath)throw Error(`Shared-state worker schema context does not match its caller`)}function captureOpenClawStateWorkerOpeningGuard(e){let t=e.admission,n=t.identity,r=M.bind(()=>t.assertCurrent()),assertCurrent=()=>{if(!r)throw Error(`Shared-state worker opening admission is closed`);r()};return{assertCurrent,createAdmission:()=>{let e=!1;return{nativeLocations:[t.databasePath],databaseIdentity:{databasePath:t.databasePath,key:n.key,birthtime:n.birthtime},admission:S((t,n)=>{if(e||t.stage!==`open`)throw Error(`Shared-state worker open admission requested out of order`);if(assertCurrent(),!n())throw Error(`Shared-state worker open admission expired`);e=!0})}},async run(e){try{return await e()}finally{r=void 0}}}}const P=r(`state/worker`);function createSharedStateWorkerOwner(){let e=d(`sharedStateStore`),r=/* @__PURE__ */ new Set,c=/* @__PURE__ */ new Set,l=/* @__PURE__ */ new Map,f=s(()=>r.size>0||l.size>0||c.size>0,retireIdleWorkers),matches=(e,t)=>t===void 0||e.context.admission.identity.key===t.key,hasActiveActorOperations=e=>[...c].some(t=>e.actor?t.actor===e.actor:t===e),clearIdleRetirement=e=>{e.idleSince=void 0,clearTimeout(e.idleTimer),e.idleTimer=void 0},hasPendingCleanup=e=>e.cleanup?.pending??(!e.context.maintenanceScope&&A(e.context.admission.databasePath)),retire=e=>{clearIdleRetirement(e),r.delete(e);let t=l.get(e);if(t||(t={},l.set(e,t)),f(),t.pending)return t.pending;let n=e.store?e.store.close():e.opening.then(e=>e?.close(),()=>e.cleanup?e.cleanup.close():e.context.maintenanceScope?void 0:j(e.context.admission.databasePath));t.pending=n;let settled=()=>{t.pending=void 0,hasPendingCleanup(e)||l.delete(e),f()};return n.then(settled,settled),n},scheduleIdleRetirement=e=>{if(e.activeOperations!==0||e.idleTimer||e.context.maintenanceScope||!e.store||!r.has(e))return;e.idleSince=performance.now();let n=t(()=>setTimeout(()=>{e.idleTimer===n&&e.activeOperations===0&&r.has(e)&&(e.idleTimer=void 0,retire(e).catch(t=>{P.warn(`Idle shared-state worker retirement failed`,{path:e.context.admission.databasePath,error:t})}))},C));e.idleTimer=n,n.unref?.()},retireAfterFailure=async(e,t,n)=>{try{await retire(e)}catch(e){throw AggregateError([t,e],`Shared-state worker ${n} and cleanup failed`,{cause:e})}throw t},retainOperation=(e,t)=>{let n=[...r].find(t=>t.store===e);if(!n)throw Error(`Shared-state worker operation lost its actor owner`);clearIdleRetirement(n),n.activeOperations+=1,c.add(n),t&&(n.nativeLostListeners.add(t),E(e)||t());let i=!1;return()=>{if(!i){if(i=!0,t&&n.nativeLostListeners.delete(t),--n.activeOperations,n.activeOperations===0&&c.delete(n),f(),r.has(n)&&!E(e)&&!hasActiveActorOperations(n)){let e=n.actor?retireActor(n.actor):retire(n);return e.catch(e=>{P.warn(`Shared-state worker retirement failed`,{path:n.context.admission.databasePath,error:e})}),e}scheduleIdleRetirement(n)}}},retireActor=e=>{let t=T(e);for(let t of/* @__PURE__ */ new Set([...r,...l.keys()]))t.actor===e&&retire(t);return t};async function close(e){for(let t of r.values())matches(t,e)&&retire(t);let t=(await Promise.allSettled([...l.keys()].filter(t=>matches(t,e)).map(retire))).flatMap(e=>e.status===`rejected`?[e.reason]:[]);if(f(),t.length)throw AggregateError(t,`Failed to close shared-state SQLite workers`)}_({phase:`after-resources`,close}),g(e=>{if(e.kind!==`opened`){if(!e.identity)return;close(e.identity).catch(t=>{P.warn(`Shared-state worker retirement failed`,{path:e.path,error:t})})}});function retireIdleWorkers(e){for(let n of r)a(n.idleSince,e)&&t(()=>retireIdle(n))?.catch(e=>{P.warn(`Idle shared-state worker retirement failed`,{path:n.context.admission.databasePath,error:e})})}let retireIdle=e=>e.idleTimer&&e.store&&r.has(e)&&!e.context.maintenanceScope&&!hasActiveActorOperations(e)?retire(e):void 0;return{close,retainOperation,async openCleanup(t,i,a,s,u){let d={...s};if(a(),!d.key.startsWith(`file:`))return;let assertCurrent=()=>{a();for(let e of c)if(matches(e,d)&&e.store&&!E(e.store))throw new n(`Shared-state cleanup is waiting for accepted operations to settle`,`unavailable`)};assertCurrent();for(let e of/* @__PURE__ */ new Set([...r,...l.keys()]))(r.has(e)||l.has(e))&&matches(e,d)&&e.store&&!E(e.store)&&(assertCurrent(),await(e.actor?retireActor(e.actor):retire(e)),assertCurrent());return k({...o(e),databasePath:t,existingOnly:!0,admission:{identity:d.key,birthtime:d.birthtime,assertCurrent}},i,assertCurrent,{cleanupAccess:u&&{assertDatabaseAccess:u.assertCleanupDatabaseAccess}})},async open(n,a={}){let{existingOnly:s=!1,assertCurrent:c,signal:d}=a,{admission:p}=n,wait=e=>i(e,d),m=o(e),assertAdmission=()=>{d?.throwIfAborted(),p.assertCurrent(),c?.()};assertAdmission(),assertOpenClawStateWorkerSchemaContext(n);for(let e of/* @__PURE__ */ new Set([...r,...l.keys()]))if(matches(e,p.identity)&&r.has(e)&&(e.context.existingSchemaPath!==n.existingSchemaPath||!N(e.context.gatewayServiceInstallation,n.gatewayServiceInstallation)||e.source.moduleUrl.href!==m.moduleUrl.href)){if(hasActiveActorOperations(e))throw Error(`Shared-state worker policy changed while its actor is in use; close its leases first`);await wait(e.actor?retireActor(e.actor):retire(e)),assertAdmission()}for(let[e,t]of l)if(matches(e,p.identity)&&e.context.maintenanceScope===n.maintenanceScope){if(hasActiveActorOperations(e))throw Error(`Shared-state worker is retiring while its actor is in use; close its leases first`);if(!t.pending){if(!hasPendingCleanup(e)){l.delete(e),f();continue}throw Error(`Shared-state SQLite cleanup is pending; close the database before reopening`)}try{await wait(t.pending)}catch(n){if(l.get(e)===t)throw n}assertAdmission()}assertAdmission();let h=[...r].find(e=>matches(e,p.identity)&&e.context.maintenanceScope===n.maintenanceScope&&e.context.existingSchemaPath===n.existingSchemaPath&&N(e.context.gatewayServiceInstallation,n.gatewayServiceInstallation)),g=!h;if(!h){let e=captureOpenClawStateWorkerOpeningGuard(n),i=/* @__PURE__ */ new Set,open=()=>e.run(()=>k({...m,databasePath:p.databasePath,existingOnly:s},n,e.assertCurrent,{maintenanceScope:n.maintenanceScope,...s&&p.identity.key.startsWith(`file:`)?{createAdmission:e.createAdmission}:{},retireIdle:retireIdle.bind(void 0,a),retainCleanup:e=>{a.cleanup=e},onNativeLost:u.bind(void 0,i,void 0,void 0)})),a={source:m,context:n,existingOnly:s,nativeLostListeners:i,activeOperations:0,opening:Promise.resolve(void 0)};a.opening=n.maintenanceScope?open():t(open),h=a,a.opening=a.opening.then(e=>(e?(a.store=e,a.actor=O(e),scheduleIdleRetirement(a)):(r.delete(a),f()),e)),r.add(h),f(),n.maintenanceScope?.own(h,`shared-resources`,()=>retire(a)),h.opening.catch(()=>{r.delete(a),hasPendingCleanup(a)&&!l.has(a)&&l.set(a,{}),f()})}let _=await(g?h.opening:wait(h.opening));try{p.assertCurrent()}catch(e){return retireAfterFailure(h,e,`admission`)}if(d?.throwIfAborted(),c?.(),!_)return!s&&h.existingOnly?this.open(n,{...a,existingOnly:!1}):void 0;if(!r.has(h))return this.open(n,a);if(clearIdleRetirement(h),!E(_)&&!hasActiveActorOperations(h))return await wait(h.actor?retireActor(h.actor):retire(h)),this.open(n,a);try{h.bound||(y(h.context.admission,O(_).identity),h.bound=!0)}catch(e){return retireAfterFailure(h,e,`binding`)}return h.actor&&assertOpenClawStateWorkerActorPath(h.actor),r.delete(h),r.add(h),_}}}function getOpenClawStateWorkerOwner(){return b(),e(Symbol.for(`openclaw.sharedStateWorkerOwner`),createSharedStateWorkerOwner)}function openOpenClawStateWorkerCleanupStore(e,t,n,r,i){return getOpenClawStateWorkerOwner().openCleanup(e,t,n,r,i)}async function executeOpenClawStateWorker(e,t){let n=await runOpenClawStateWorkerOperation(e,e=>e.execute(t));return e.admission.assertCurrent(),n}async function runOpenClawStateWorkerOperation(e,t,n){return w(()=>runWithCapturedWorkerContext(e,async()=>{try{n?.signal?.throwIfAborted(),e.admission.assertCurrent(),n?.assertCurrent?.();let r=await x(e,n?.signal);if(r)throw r;e.admission.assertCurrent(),n?.assertCurrent?.();let i=await getOpenClawStateWorkerOwner().open(e,n);if(!i){if(n?.signal?.throwIfAborted(),e.admission.assertCurrent(),n?.existingOnly)return;throw Error(`Canonical shared-state worker did not open its database`)}let a=getOpenClawStateWorkerOwner().retainOperation(i);try{return n?.signal?.throwIfAborted(),e.admission.assertCurrent(),n?.assertCurrent?.(),await runWithOpenClawStateWorkerStore(i,e,t,n?.assertCurrent,n?.createAdmission,n?.signal)}finally{a()}}catch(t){let n=l(t),r=c(n,e.admission.databasePath);if(r&&!h.getOpenClawStateDatabaseRecordedFailure(e.admission.databasePath)){try{e.admission.assertCurrent()}catch{throw n}v(e.admission.databasePath,r)}throw n}}))}export{openOpenClawStateWorkerCleanupStore as n,runOpenClawStateWorkerOperation as r,executeOpenClawStateWorker as t};