openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 10 kB
JavaScript
import{n as e}from"./openclaw-state-read-rolldown-runtime-C7TU3oOp.mjs";import{Tt as t,zt as n}from"./openclaw-state-read-subsystem-Dwoo5bcz.mjs";import{It as r,Mt as i,a}from"./openclaw-state-read-sqlite-database-admission-CrkztOzk.mjs";import{O as o}from"./openclaw-state-read-openclaw-state-db-cache-DyomEqgV.mjs";import{h as s,p as c}from"./openclaw-state-read-sqlite-private-snapshot-fork-B2lPOtW3.mjs";import{Bt as l,J as u,Pt as d,Y as f}from"./openclaw-state-read-sqlite-readonly-worker-D6FdUovN.mjs";import{s as p}from"./openclaw-state-read-openclaw-state-db-contract-vqwNsqc6.mjs";import{n as m,t as h}from"./openclaw-state-read-gateway-supervision-CAmdOi7r.mjs";import{a as g}from"./openclaw-state-read-sqlite-busy-timeout-B0rAL9T0.mjs";import{h as _,u as v}from"./openclaw-state-read-sqlite-schema-facts-Dtolu7-X.mjs";import{n as y,r as b}from"./openclaw-state-read-node-sqlite-B_Qn1gsS.mjs";import{n as x}from"./openclaw-state-read-agent-id-DmscQLD6.mjs";import"./openclaw-state-read-session-key-H2_GWFw8.mjs";import{S,T as C,g as w,w as T}from"./openclaw-state-read-openclaw-state-db-readonly-Bv32Owin.mjs";import{b as E,n as D,p as O,t as k}from"./openclaw-state-read-openclaw-agent-db-engine-CmBIEYKS.mjs";import{a as A,i as j}from"./openclaw-state-read-openclaw-agent-db-registry-listing-BSp1z3ZT.mjs";import M,{lstatSync as N,readdirSync as P}from"node:fs";import F from"node:path";import{isMainThread as I}from"node:worker_threads";function resolveSessionStoreCompatibilityAgentId(e){let t=e.agents?.defaults?.sessionStore?.agentId?.trim();return t?x(t):w(e)??`main`}function inspectOpenClawAgentDatabaseOwner(e,t){if(I&&k()){let n=a(e,c,()=>void 0);if(!t?.revalidateSchema&&n?.role===`agent`&&n.agentId)return{status:`owned`,agentId:x(n.agentId)};throw Error(`PostgreSQL agent ownership requires worker preparation before synchronous inspection; use the asynchronous agent database preparation owner.`)}let n,r;try{let i=F.resolve(e),a=E.databases.get(i);if(!t?.revalidateSchema&&a?.db.isOpen&&!E.failures.has(i))return _(a.db,()=>O(a.db,e)),E.idleTimers.get(a.db)?.refresh(),{status:`owned`,agentId:a.agentId};n=k()?y(b(e),{timeout:p}):m(e,{readOnly:!0}),g(n,p),k()?r=D(n,void 0,e,!0):t?.revalidateSchema&&v(n),O(r??n,e);let o=s(r??n);return o?o.role!==`agent`||!o.agentId?{status:`unreadable`}:{status:`owned`,agentId:x(o.agentId)}:{status:`unowned`}}catch{return{status:`unreadable`}}finally{try{r?.close()}finally{n?.close()}}}function resolveCapturedSessionStorePath(e,t,n,r,i=`configured`){if(!r)return S(i==="default"?void 0:e,{agentId:t,env:n});let a=r.get(t);if(!a)throw Error(`Session store path was not captured for ${t}`);return a[i]}function captureSessionStoreReadCandidate(e,n){let r=F.resolve(e);return{path:r,physicalPath:n?F.join(t(F.dirname(r)),F.basename(r)):t(r),...n?{scope:n}:{}}}function isSessionStoreReadCandidateCurrent(e){let t=captureSessionStoreReadCandidate(e.path,e.scope).physicalPath;return matchesSessionStoreReadCandidatePhysicalPath(e,t)}function matchesSessionStoreReadCandidatePhysicalPath(e,t){let n=resolveCapturedSessionStoreReadCandidatePhysicalPath(e);return t===n?!0:e.scope||!isSymlinkFreeWindowsShortPath(e.path)&&!isSymlinkFreeWindowsShortPath(e.physicalPath)?!1:matchesWindowsFileAlias(t,n)}function resolveCapturedSessionStoreReadCandidatePhysicalPath(e){return e.scope||!isSymlinkFreeWindowsShortPath(e.physicalPath)?e.physicalPath:t(e.physicalPath)}function isSymlinkFreeWindowsShortPath(e){return process.platform!==`win32`||!/(?:^|[\\/])[^\\/]*~\d+(?=[\\/]|$)/iu.test(e)?!1:isSymlinkFreePath(e)}function isSymlinkFreePath(e){let t=F.resolve(e),n=F.parse(t),r=n.root;for(let e of t.slice(n.root.length).split(F.sep)){r=F.join(r,e);let t=M.lstatSync(r,{throwIfNoEntry:!1});if(!t||t.isSymbolicLink())return!1}return!0}function matchesWindowsFileAlias(e,t){let n=isSymlinkFreeWindowsShortPath(e)?e:isSymlinkFreeWindowsShortPath(t)?t:void 0;if(process.platform!==`win32`||!n||!isSymlinkFreePath(e)||!isSymlinkFreePath(t))return!1;let i=M.statSync(F.dirname(e),{bigint:!0}),a=M.statSync(F.dirname(t),{bigint:!0});if(i.dev!==a.dev||i.ino!==a.ino)return!1;let o=r(e),s=r(t);return o?.key.startsWith(`file:`)===!0&&o.key===s?.key&&o.birthtime===s.birthtime}function assertSessionStoreReadCandidate(e,n){if(I&&l()){let t=n.find(t=>t.identity&&(t.path===F.resolve(e)||t.physicalPath===F.resolve(e)));if(!t)throw Error(`Session inspection requires a worker-prepared physical candidate`);return t.physicalPath}let r=t(e);for(let t of n){let n=!t.scope&&r===resolveCapturedSessionStoreReadCandidatePhysicalPath(t);if((d(t,e)||n||!t.scope&&matchesWindowsFileAlias(t.path,e))&&(!t.scope||d({...t,path:resolveCapturedSessionStoreReadCandidatePhysicalPath(t)},r))&&(!t.scope&&t.path===F.resolve(e)?matchesSessionStoreReadCandidatePhysicalPath(t,r):isSessionStoreReadCandidateCurrent(t)))return t.identity&&i(t.path,t.identity),r}throw Error(`Session database target changed outside captured discovery custody: ${e}`)}const L=/* @__PURE__ */ new Set([`OPENCLAW_STATE_DIR`,`OPENCLAW_PROFILE`,`OPENCLAW_SUPERVISOR_MODE`,`OPENCLAW_HOME`,`HOME`,`USERPROFILE`,`PREFIX`,`ANDROID_DATA`,`OPENCLAW_TEST_FAST`,`VITEST`,`VITEST_POOL_ID`,`VITEST_WORKER_ID`,`NODE_ENV`]);function captureSessionTranscriptStorageEnvironment(e){let t=o(Object.fromEntries(Object.keys(e).filter(e=>L.has(e.toUpperCase())).map(t=>[t,e[t]])));return{OPENCLAW_STATE_DIR:n(t),...h(t)?{OPENCLAW_SUPERVISOR_MODE:`external`}:{}}}var R=/* @__PURE__ */ e({SessionStoreRegistryReadRequired:()=>SessionStoreRegistryReadRequired,prepareSqliteTargetFromSessionStorePath:()=>prepareSqliteTargetFromSessionStorePath,readSessionStoreRegistryRows:()=>readSessionStoreRegistryRows,resolveSqliteTargetFromSessionStorePath:()=>resolveSqliteTargetFromSessionStorePath}),SessionStoreRegistryReadRequired=class extends Error{};function readSessionStoreRegistryRows(e,t,n){if(e&&`status`in e){if(e.status===`deferred`)throw new SessionStoreRegistryReadRequired(`Session target discovery requires registry rows`);let t=/* @__PURE__ */ Error(`Session target registry is unavailable`);throw n?.(t),t}return e??j({env:t})}async function prepareSqliteTargetFromSessionStorePath(e,t={},n){n?.throwIfAborted();let r=C(e);if(r.agentId)return r;let i=captureSessionTranscriptStorageEnvironment(t.env??process.env),a=A({env:i}),o={storePath:F.resolve(e.trim()),agentId:t.agentId,defaultAgentId:t.defaultAgentId,env:i},{resolveSessionSqliteTargetInWorker:s}=await import(`./openclaw-state-read-session-transcript-read-worker-runtime-B6S8M_cR.mjs`),c=await a.read(n),l=readSessionStoreRegistryRows(c.result.status===`available`?c.result.entries:c.result);return n?.throwIfAborted(),s({...o,registeredDatabases:l},n)}function resolvePersistedOwners(e,t,n,r){let i=[...new Set(t.filter(t=>n(t.path,e)).map(e=>x(e.agentId)))],a;return i.length===1?hasFilesystemEntry(e,r.onReadError):a=resolveDatabaseOwner(e,r.readCandidates,r.onReadError),{registeredOwners:i,databaseOwner:a}}function resolveDatabaseOwner(e,t,n){if(!hasFilesystemEntry(e,n))return;let r=inspectOpenClawAgentDatabaseOwner(t?assertSessionStoreReadCandidate(e,t):e);return r.status===`owned`?x(r.agentId):void 0}function hasFilesystemEntry(e,t){try{return N(e),!0}catch(e){if(e.code===`ENOENT`)return!1;throw t?.(e),e}}function resolveCustomStoreSqlitePath(e){let t=F.resolve(e.unsuffixedPath),n=F.basename(t,`.sqlite`),r=F.dirname(t),i=x(e.options.defaultAgentId??`main`),a=x(e.options.agentId??i),o=readSessionStoreRegistryRows(e.options.registeredDatabases,e.options.env,e.options.onReadError),s=e.options.isSameDatabasePath??u(),resolvePersistedOwner=t=>{let{registeredOwners:n,databaseOwner:r}=resolvePersistedOwners(t,o,s,e.options);return{effectiveOwner:n.length===1?n[0]:n.length===0?r:void 0,registeredOwners:n}},{registeredOwners:c,effectiveOwner:l}=resolvePersistedOwner(t),resolveSuffixedTarget=t=>{let i=`${n}.${t}`,a=/* @__PURE__ */ new Set;for(let e of o){if(!s(F.dirname(e.path),r))continue;let t=T(F.basename(e.path),i);t!==void 0&&a.add(t)}try{for(let e of P(r)){let t=T(e,i);t!==void 0&&a.add(t)}}catch(t){if(t.code!==`ENOENT`)throw e.options.onReadError?.(t),t}let candidatePathAt=e=>F.join(r,e===1?`${i}.sqlite`:`${i}.${e}.sqlite`),c=[...a].toSorted((e,t)=>e-t);for(let e of c){let n=candidatePathAt(e);if(resolvePersistedOwner(n).effectiveOwner===t)return{owned:!0,path:n}}let l=1;for(let e of c)if(e===l)l+=1;else if(e>l)break;for(let n=l;;n+=1){let r=candidatePathAt(n),i=resolvePersistedOwner(r);if(i.effectiveOwner===t)return{owned:!0,path:r};if(i.registeredOwners.length===0&&!hasFilesystemEntry(r,e.options.onReadError))return{owned:!1,path:r}}},d=resolveSuffixedTarget(i),f=a===i?d:resolveSuffixedTarget(a),p=d.owned,m=f.owned,h=c.length===0&&!hasFilesystemEntry(t,e.options.onReadError),g=l||p||!h?void 0:i,_=l??g,v=a===l||!m&&a===g,y=l?c.length===1?`database-registry`:`database-path`:p?`registered-suffixed`:c.length>1?`ambiguous-registry`:h?`configured-default`:`occupied-unsuffixed`;return{agentId:a,path:v?t:f.path,ownerSource:y,..._?{unsuffixedOwnerAgentId:_}:{}}}function resolveSqliteTargetFromSessionStorePath(e,t={}){let n=C(e),r=t.agentId?x(t.agentId):void 0;if(r&&f(n.path,{agentId:r,env:t.env}))return{agentId:r,path:n.path};if(n.agentId)return n;if(n.shared){let e=readSessionStoreRegistryRows(t.registeredDatabases,t.env,t.onReadError),{registeredOwners:r,databaseOwner:i}=resolvePersistedOwners(n.path,e,t.isSameDatabasePath??u(),t),a=x(t.defaultAgentId??`main`);return{agentId:(r.length===1?r[0]:void 0)??i??a,path:n.path,shared:!0,ownerSource:r.length===1?`database-registry`:i?`database-path`:r.length>1?`ambiguous-registry`:`configured-default`}}return resolveCustomStoreSqlitePath({unsuffixedPath:n.path,options:t})}export{captureSessionStoreReadCandidate as a,resolveSessionStoreCompatibilityAgentId as c,captureSessionTranscriptStorageEnvironment as i,resolveSqliteTargetFromSessionStorePath as n,resolveCapturedSessionStorePath as o,R as r,inspectOpenClawAgentDatabaseOwner as s,readSessionStoreRegistryRows as t};