UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 10.9 kB
import{n as e}from"./openclaw-state-read-rolldown-runtime-C7TU3oOp.mjs";import{Cn as t,gt as n,t as r}from"./openclaw-state-read-subsystem-Dwoo5bcz.mjs";import{tt as i}from"./openclaw-state-read-sqlite-database-admission-CrkztOzk.mjs";import{r as a}from"./openclaw-state-read-src-RL5xtYG2.mjs";import{H as o,V as s,a as c,c as l,d as u,l as d,o as f,t as p,u as m}from"./openclaw-state-read-openclaw-state-db-cache-DyomEqgV.mjs";import{f as h}from"./openclaw-state-read-sqlite-post-commit-CsXPYVOo.mjs";import{rt as g,tt as _}from"./openclaw-state-read-sqlite-readonly-worker-D6FdUovN.mjs";import{r as v,t as y}from"./openclaw-state-read-runtime-worker-generation-DIZIqsAo.mjs";import{s as b}from"./openclaw-state-read-sqlite-worker-request-class-DQkpMMiB.mjs";import{i as x}from"./openclaw-state-read-sqlite-worker-operation-admission-DdAi_0af.mjs";import{D as S,p as C}from"./openclaw-state-read-sqlite-snapshot-staging-owner-DKlm1W_1.mjs";import{t as w}from"./openclaw-state-read-sqlite-handle-lifecycle-uqyIC6ii.mjs";import{n as T}from"./openclaw-state-read-acquire-with-wait-DzhiyV2X.mjs";import{c as E,i as D,n as O,o as k,r as A,s as j,t as M}from"./openclaw-state-read-sqlite-worker-store-IQvL052n.mjs";import{AsyncLocalStorage as N}from"node:async_hooks";import{isDeepStrictEqual as P}from"node:util";function assertOpenClawStateWorkerActorPath(e){if(f.getKnownOpenClawStateDatabaseIdentity(e.databasePath)?.key!==e.key)throw new v(`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 E(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(o(e.admission.databasePath),s()!==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=N.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:x((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 F=r(`state/worker`);function createSharedStateWorkerOwner(){let e=b(`sharedStateStore`),t=/* @__PURE__ */ new Set,r=/* @__PURE__ */ new Set,o=/* @__PURE__ */ new Map,s=_(()=>t.size>0||o.size>0||r.size>0,retireIdleWorkers),matches=(e,t)=>t===void 0||e.context.admission.identity.key===t.key,hasActiveActorOperations=e=>[...r].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),t.delete(e);let n=o.get(e);if(n||(n={},o.set(e,n)),s(),n.pending)return n.pending;let r=e.store?e.store.close():e.opening.then(e=>e?.close(),()=>e.cleanup?e.cleanup.close():e.context.maintenanceScope?void 0:M(e.context.admission.databasePath));n.pending=r;let settled=()=>{n.pending=void 0,hasPendingCleanup(e)||o.delete(e),s()};return r.then(settled,settled),r},scheduleIdleRetirement=e=>{if(e.activeOperations!==0||e.idleTimer||e.context.maintenanceScope||!e.store||!t.has(e))return;e.idleSince=performance.now();let r=n(()=>setTimeout(()=>{e.idleTimer===r&&e.activeOperations===0&&t.has(e)&&(e.idleTimer=void 0,retire(e).catch(t=>{F.warn(`Idle shared-state worker retirement failed`,{path:e.context.admission.databasePath,error:t})}))},w));e.idleTimer=r,r.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,n)=>{let i=[...t].find(t=>t.store===e);if(!i)throw Error(`Shared-state worker operation lost its actor owner`);clearIdleRetirement(i),i.activeOperations+=1,r.add(i),n&&(i.nativeLostListeners.add(n),D(e)||n());let a=!1;return()=>{if(!a){if(a=!0,n&&i.nativeLostListeners.delete(n),--i.activeOperations,i.activeOperations===0&&r.delete(i),s(),t.has(i)&&!D(e)&&!hasActiveActorOperations(i)){let e=i.actor?retireActor(i.actor):retire(i);return e.catch(e=>{F.warn(`Shared-state worker retirement failed`,{path:i.context.admission.databasePath,error:e})}),e}scheduleIdleRetirement(i)}}},retireActor=e=>{let n=j(e);for(let n of/* @__PURE__ */ new Set([...t,...o.keys()]))n.actor===e&&retire(n);return n};async function close(e){for(let n of t.values())matches(n,e)&&retire(n);let n=(await Promise.allSettled([...o.keys()].filter(t=>matches(t,e)).map(retire))).flatMap(e=>e.status===`rejected`?[e.reason]:[]);if(s(),n.length)throw AggregateError(n,`Failed to close shared-state SQLite workers`)}m({phase:`after-resources`,close}),u(e=>{if(e.kind!==`opened`){if(!e.identity)return;close(e.identity).catch(t=>{F.warn(`Shared-state worker retirement failed`,{path:e.path,error:t})})}});function retireIdleWorkers(e){for(let r of t)g(r.idleSince,e)&&n(()=>retireIdle(r))?.catch(e=>{F.warn(`Idle shared-state worker retirement failed`,{path:r.context.admission.databasePath,error:e})})}let retireIdle=e=>e.idleTimer&&e.store&&t.has(e)&&!e.context.maintenanceScope&&!hasActiveActorOperations(e)?retire(e):void 0;return{close,retainOperation,async openCleanup(n,a,s,c,l){let u={...c};if(s(),!u.key.startsWith(`file:`))return;let assertCurrent=()=>{s();for(let e of r)if(matches(e,u)&&e.store&&!D(e.store))throw new i(`Shared-state cleanup is waiting for accepted operations to settle`,`unavailable`)};assertCurrent();for(let e of/* @__PURE__ */ new Set([...t,...o.keys()]))(t.has(e)||o.has(e))&&matches(e,u)&&e.store&&!D(e.store)&&(assertCurrent(),await(e.actor?retireActor(e.actor):retire(e)),assertCurrent());return k({...y(e),databasePath:n,existingOnly:!0,admission:{identity:u.key,birthtime:u.birthtime,assertCurrent}},a,assertCurrent,{cleanupAccess:l&&{assertDatabaseAccess:l.assertCleanupDatabaseAccess}})},async open(r,i={}){let{existingOnly:c=!1,assertCurrent:u,signal:d}=i,{admission:f}=r,wait=e=>a(e,d),p=y(e),assertAdmission=()=>{d?.throwIfAborted(),f.assertCurrent(),u?.()};assertAdmission(),assertOpenClawStateWorkerSchemaContext(r);for(let e of/* @__PURE__ */ new Set([...t,...o.keys()]))if(matches(e,f.identity)&&t.has(e)&&(e.context.existingSchemaPath!==r.existingSchemaPath||!P(e.context.gatewayServiceInstallation,r.gatewayServiceInstallation)||e.source.moduleUrl.href!==p.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 o)if(matches(e,f.identity)&&e.context.maintenanceScope===r.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)){o.delete(e),s();continue}throw Error(`Shared-state SQLite cleanup is pending; close the database before reopening`)}try{await wait(t.pending)}catch(n){if(o.get(e)===t)throw n}assertAdmission()}assertAdmission();let m=[...t].find(e=>matches(e,f.identity)&&e.context.maintenanceScope===r.maintenanceScope&&e.context.existingSchemaPath===r.existingSchemaPath&&P(e.context.gatewayServiceInstallation,r.gatewayServiceInstallation)),g=!m;if(!m){let e=captureOpenClawStateWorkerOpeningGuard(r),i=/* @__PURE__ */ new Set,open=()=>e.run(()=>k({...p,databasePath:f.databasePath,existingOnly:c},r,e.assertCurrent,{maintenanceScope:r.maintenanceScope,...c&&f.identity.key.startsWith(`file:`)?{createAdmission:e.createAdmission}:{},retireIdle:retireIdle.bind(void 0,a),retainCleanup:e=>{a.cleanup=e},onNativeLost:h.bind(void 0,i,void 0,void 0)})),a={source:p,context:r,existingOnly:c,nativeLostListeners:i,activeOperations:0,opening:Promise.resolve(void 0)};a.opening=r.maintenanceScope?open():n(open),m=a,a.opening=a.opening.then(e=>(e?(a.store=e,a.actor=O(e),scheduleIdleRetirement(a)):(t.delete(a),s()),e)),t.add(m),s(),r.maintenanceScope?.own(m,`shared-resources`,()=>retire(a)),m.opening.catch(()=>{t.delete(a),hasPendingCleanup(a)&&!o.has(a)&&o.set(a,{}),s()})}let _=await(g?m.opening:wait(m.opening));try{f.assertCurrent()}catch(e){return retireAfterFailure(m,e,`admission`)}if(d?.throwIfAborted(),u?.(),!_)return!c&&m.existingOnly?this.open(r,{...i,existingOnly:!1}):void 0;if(!t.has(m))return this.open(r,i);if(clearIdleRetirement(m),!D(_)&&!hasActiveActorOperations(m))return await wait(m.actor?retireActor(m.actor):retire(m)),this.open(r,i);try{m.bound||(l(m.context.admission,O(_).identity),m.bound=!0)}catch(e){return retireAfterFailure(m,e,`binding`)}return m.actor&&assertOpenClawStateWorkerActorPath(m.actor),t.delete(m),t.add(m),_}}}function getOpenClawStateWorkerOwner(){return p(),t(Symbol.for(`openclaw.sharedStateWorkerOwner`),createSharedStateWorkerOwner)}var I=/* @__PURE__ */ e({executeOpenClawStateWorker:()=>executeOpenClawStateWorker,openOpenClawStateWorkerCleanupStore:()=>openOpenClawStateWorkerCleanupStore,runOpenClawStateWorkerOperation:()=>runOpenClawStateWorkerOperation});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 T(()=>runWithCapturedWorkerContext(e,async()=>{try{n?.signal?.throwIfAborted(),e.admission.assertCurrent(),n?.assertCurrent?.();let r=await c(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=C(t),r=S(n,e.admission.databasePath);if(r&&!f.getOpenClawStateDatabaseRecordedFailure(e.admission.databasePath)){try{e.admission.assertCurrent()}catch{throw n}d(e.admission.databasePath,r)}throw n}}))}export{I as n,openOpenClawStateWorkerCleanupStore as t};