UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 11.1 kB
import{M as e}from"./transcript-chat-message-content-Cx-VPFHv.mjs";import{i as t}from"./transcript-global-singleton-DIJgobpU.mjs";import{s as n}from"./transcript-openclaw-agent-search-schema-BQwqsKhe.mjs";import"./transcript-plugin-instance-invocation-7pP9ZY8b.mjs";import{t as r}from"./transcript-detached-async-context-BKXaSw-L.mjs";import{m as i}from"./transcript-sqlite-database-admission-Cro6ZHgY.mjs";import{x as a}from"./transcript-sqlite-error-diagnostics-Bzt01wuM.mjs";import{t as o}from"./transcript-subsystem-er2pCebD.mjs";import{L as s}from"./transcript-sqlite-table-layout-migration-6FYoXQ_h.mjs";import{r as c}from"./transcript-src-DFL7yLCI.mjs";import{_ as l,n as u}from"./transcript-openclaw-state-worker-error-DM4pmO7c.mjs";import{l as d}from"./transcript-sqlite-post-commit-BDZjIt2B.mjs";import{t as f}from"./transcript-runtime-process-url-BahjIwf7.mjs";import{F as p}from"./transcript-openclaw-agent-db-schema-read-0j86l7zG.mjs";import{G as m,K as h,a as g,c as _,d as v,l as y,o as b,t as x,u as S}from"./transcript-openclaw-state-db-cache-DY0tZ0IC.mjs";import{C,l as w,m as T}from"./transcript-worker-task-pool-xHbAfEur.mjs";import{n as E}from"./transcript-acquire-with-wait-18yu6N4Q.mjs";import{c as D,i as O,l as k,n as A,o as j,r as M,t as N}from"./transcript-sqlite-worker-store-EaynLmvH.mjs";import{AsyncLocalStorage as P}from"node:async_hooks";import{isDeepStrictEqual as F}from"node:util";function assertOpenClawStateWorkerActorPath(e){if(b.getKnownOpenClawStateDatabaseIdentity(e.databasePath)?.key!==e.key)throw new p(`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=A(e);return k(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(h(e.admission.databasePath),m()!==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=P.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:T((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 I=o(`state/worker`);function createSharedStateWorkerOwner(){let e=f(`sharedStateStore`),t=/* @__PURE__ */ new Set,o=/* @__PURE__ */ new Set,l=/* @__PURE__ */ new Map,u=s(()=>t.size>0||l.size>0||o.size>0,retireIdleWorkers),matches=(e,t)=>t===void 0||e.context.admission.identity.key===t.key,hasActiveActorOperations=e=>[...o].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&&M(e.context.admission.databasePath)),retire=e=>{clearIdleRetirement(e),t.delete(e);let n=l.get(e);if(n||(n={},l.set(e,n)),u(),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:N(e.context.admission.databasePath));n.pending=r;let settled=()=>{n.pending=void 0,hasPendingCleanup(e)||l.delete(e),u()};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 i=r(()=>setTimeout(()=>{e.idleTimer===i&&e.activeOperations===0&&t.has(e)&&(e.idleTimer=void 0,retire(e).catch(t=>{I.warn(`Idle shared-state worker retirement failed`,{path:e.context.admission.databasePath,error:t})}))},n));e.idleTimer=i,i.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 r=[...t].find(t=>t.store===e);if(!r)throw Error(`Shared-state worker operation lost its actor owner`);clearIdleRetirement(r),r.activeOperations+=1,o.add(r),n&&(r.nativeLostListeners.add(n),O(e)||n());let i=!1;return()=>{if(!i){if(i=!0,n&&r.nativeLostListeners.delete(n),--r.activeOperations,r.activeOperations===0&&o.delete(r),u(),t.has(r)&&!O(e)&&!hasActiveActorOperations(r)){let e=r.actor?retireActor(r.actor):retire(r);return e.catch(e=>{I.warn(`Shared-state worker retirement failed`,{path:r.context.admission.databasePath,error:e})}),e}scheduleIdleRetirement(r)}}},retireActor=e=>{let n=D(e);for(let n of/* @__PURE__ */ new Set([...t,...l.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([...l.keys()].filter(t=>matches(t,e)).map(retire))).flatMap(e=>e.status===`rejected`?[e.reason]:[]);if(u(),n.length)throw AggregateError(n,`Failed to close shared-state SQLite workers`)}S({phase:`after-resources`,close}),v(e=>{if(e.kind!==`opened`){if(!e.identity)return;close(e.identity).catch(t=>{I.warn(`Shared-state worker retirement failed`,{path:e.path,error:t})})}});function retireIdleWorkers(e){for(let n of t)C(n.idleSince,e)&&r(()=>retireIdle(n))?.catch(e=>{I.warn(`Idle shared-state worker retirement failed`,{path:n.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,r,i,s,c){let u={...s};if(i(),!u.key.startsWith(`file:`))return;let assertCurrent=()=>{i();for(let e of o)if(matches(e,u)&&e.store&&!O(e.store))throw new a(`Shared-state cleanup is waiting for accepted operations to settle`,`unavailable`)};assertCurrent();for(let e of/* @__PURE__ */ new Set([...t,...l.keys()]))(t.has(e)||l.has(e))&&matches(e,u)&&e.store&&!O(e.store)&&(assertCurrent(),await(e.actor?retireActor(e.actor):retire(e)),assertCurrent());return j({...w(e),databasePath:n,existingOnly:!0,admission:{identity:u.key,birthtime:u.birthtime,assertCurrent}},r,assertCurrent,{cleanupAccess:c&&{assertDatabaseAccess:c.assertCleanupDatabaseAccess}})},async open(n,a={}){let{existingOnly:o=!1,assertCurrent:s,signal:f}=a,{admission:p}=n,wait=e=>c(e,f),m=w(e),assertAdmission=()=>{f?.throwIfAborted(),p.assertCurrent(),s?.()};assertAdmission(),assertOpenClawStateWorkerSchemaContext(n);for(let e of/* @__PURE__ */ new Set([...t,...l.keys()]))if(matches(e,p.identity)&&t.has(e)&&(e.context.existingSchemaPath!==n.existingSchemaPath||!F(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),u();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=[...t].find(e=>matches(e,p.identity)&&e.context.maintenanceScope===n.maintenanceScope&&e.context.existingSchemaPath===n.existingSchemaPath&&F(e.context.gatewayServiceInstallation,n.gatewayServiceInstallation)),g=!h;if(!h){let e=captureOpenClawStateWorkerOpeningGuard(n),i=/* @__PURE__ */ new Set,open=()=>e.run(()=>j({...m,databasePath:p.databasePath,existingOnly:o},n,e.assertCurrent,{maintenanceScope:n.maintenanceScope,...o&&p.identity.key.startsWith(`file:`)?{createAdmission:e.createAdmission}:{},retireIdle:retireIdle.bind(void 0,a),retainCleanup:e=>{a.cleanup=e},onNativeLost:d.bind(void 0,i,void 0,void 0)})),a={source:m,context:n,existingOnly:o,nativeLostListeners:i,activeOperations:0,opening:Promise.resolve(void 0)};a.opening=n.maintenanceScope?open():r(open),h=a,a.opening=a.opening.then(e=>(e?(a.store=e,a.actor=A(e),scheduleIdleRetirement(a)):(t.delete(a),u()),e)),t.add(h),u(),n.maintenanceScope?.own(h,`shared-resources`,()=>retire(a)),h.opening.catch(()=>{t.delete(a),hasPendingCleanup(a)&&!l.has(a)&&l.set(a,{}),u()})}let v=await(g?h.opening:wait(h.opening));try{p.assertCurrent()}catch(e){return retireAfterFailure(h,e,`admission`)}if(f?.throwIfAborted(),s?.(),!v)return!o&&h.existingOnly?this.open(n,{...a,existingOnly:!1}):void 0;if(!t.has(h))return this.open(n,a);if(clearIdleRetirement(h),!O(v)&&!hasActiveActorOperations(h))return await wait(h.actor?retireActor(h.actor):retire(h)),this.open(n,a);try{h.bound||(_(h.context.admission,A(v).identity),h.bound=!0),p!==h.context.admission&&i(p.databasePath,p.identity)}catch(e){return retireAfterFailure(h,e,`binding`)}return h.actor&&assertOpenClawStateWorkerActorPath(h.actor),t.delete(h),t.add(h),v}}}function getOpenClawStateWorkerOwner(){return x(),t(Symbol.for(`openclaw.sharedStateWorkerOwner`),createSharedStateWorkerOwner)}var L=/* @__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 E(()=>runWithCapturedWorkerContext(e,async()=>{try{n?.signal?.throwIfAborted(),e.admission.assertCurrent(),n?.assertCurrent?.();let r=await g(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=u(t),r=l(n,e.admission.databasePath);if(r&&!b.getOpenClawStateDatabaseRecordedFailure(e.admission.databasePath)){try{e.admission.assertCurrent()}catch{throw n}y(e.admission.databasePath,r)}throw n}}))}export{L as n,openOpenClawStateWorkerCleanupStore as t};