openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 4.88 kB
JavaScript
import{n as e}from"./transcript-agent-id-DmscQLD6.mjs";import{n as t,r as n}from"./transcript-openclaw-agent-db.paths-DbFl2s5M.mjs";import"./transcript-session-key-MDDhuEG9.mjs";import{i as r,n as i,t as a}from"./transcript-session-sqlite-target-paths-C_ECqUX6.mjs";import{a as o,i as s}from"./transcript-openclaw-agent-db-registry-listing-hSVmRkvc.mjs";import{t as c}from"./transcript-openclaw-agent-db-owner-read-0uCmT78p.mjs";import{t as l}from"./transcript-session-store-read-candidates-rRmKiW_Z.mjs";import{t as u}from"./transcript-transcript-target-binding-5Rp9WohZ.mjs";import d from"node:path";import{lstatSync as f,readdirSync as p}from"node:fs";var 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??s({env:t})}async function prepareSqliteTargetFromSessionStorePath(e,t={},n){n?.throwIfAborted();let i=r(e);if(i.agentId)return i;let a=u(t.env??process.env),s=o({env:a}),c={storePath:d.resolve(e.trim()),agentId:t.agentId,defaultAgentId:t.defaultAgentId,env:a},{resolveSessionSqliteTargetInWorker:l}=await import(`./transcript-session-transcript-read-worker-runtime-BLpZejcF.mjs`),f=await s.read(n),p=readSessionStoreRegistryRows(f.result.status===`available`?f.result.entries:f.result);return n?.throwIfAborted(),l({...c,registeredDatabases:p},n)}function resolvePersistedOwners(t,n,r,i){let a=[...new Set(n.filter(e=>r(e.path,t)).map(t=>e(t.agentId)))],o;return a.length===1?hasFilesystemEntry(t,i.onReadError):o=resolveDatabaseOwner(t,i.readCandidates,i.onReadError),{registeredOwners:a,databaseOwner:o}}function resolveDatabaseOwner(t,n,r){if(!hasFilesystemEntry(t,r))return;let i=n?l(t,n):t,a=c(i);return a.status===`owned`?e(a.agentId):void 0}function hasFilesystemEntry(e,t){try{return f(e),!0}catch(e){if(e.code===`ENOENT`)return!1;throw t?.(e),e}}function resolveCustomStoreSqlitePath(n){let r=d.resolve(n.unsuffixedPath),a=d.basename(r,`.sqlite`),o=d.dirname(r),s=e(n.options.defaultAgentId??`main`),c=e(n.options.agentId??s),l=readSessionStoreRegistryRows(n.options.registeredDatabases,n.options.env,n.options.onReadError),u=n.options.isSameDatabasePath??t(),resolvePersistedOwner=e=>{let{registeredOwners:t,databaseOwner:r}=resolvePersistedOwners(e,l,u,n.options);return{effectiveOwner:t.length===1?t[0]:t.length===0?r:void 0,registeredOwners:t}},{registeredOwners:f,effectiveOwner:m}=resolvePersistedOwner(r),resolveSuffixedTarget=e=>{let t=`${a}.${e}`,r=/* @__PURE__ */ new Set;for(let e of l){if(!u(d.dirname(e.path),o))continue;let n=i(d.basename(e.path),t);n!==void 0&&r.add(n)}try{for(let e of p(o)){let n=i(e,t);n!==void 0&&r.add(n)}}catch(e){if(e.code!==`ENOENT`)throw n.options.onReadError?.(e),e}let candidatePathAt=e=>d.join(o,e===1?`${t}.sqlite`:`${t}.${e}.sqlite`),s=[...r].toSorted((e,t)=>e-t);for(let t of s){let n=candidatePathAt(t);if(resolvePersistedOwner(n).effectiveOwner===e)return{owned:!0,path:n}}let c=1;for(let e of s)if(e===c)c+=1;else if(e>c)break;for(let t=c;;t+=1){let r=candidatePathAt(t),i=resolvePersistedOwner(r);if(i.effectiveOwner===e)return{owned:!0,path:r};if(i.registeredOwners.length===0&&!hasFilesystemEntry(r,n.options.onReadError))return{owned:!1,path:r}}},h=resolveSuffixedTarget(s),g=c===s?h:resolveSuffixedTarget(c),_=h.owned,v=g.owned,y=f.length===0&&!hasFilesystemEntry(r,n.options.onReadError),b=m||_||!y?void 0:s,x=m??b,S=c===m||!v&&c===b,C=m?f.length===1?`database-registry`:`database-path`:_?`registered-suffixed`:f.length>1?`ambiguous-registry`:y?`configured-default`:`occupied-unsuffixed`;return{agentId:c,path:S?r:g.path,ownerSource:C,...x?{unsuffixedOwnerAgentId:x}:{}}}function resolveSqliteTargetFromSessionStorePath(i,a={}){let o=r(i),s=a.agentId?e(a.agentId):void 0;if(s&&n(o.path,{agentId:s,env:a.env}))return{agentId:s,path:o.path};if(o.agentId)return o;if(o.shared){let n=readSessionStoreRegistryRows(a.registeredDatabases,a.env,a.onReadError),{registeredOwners:r,databaseOwner:i}=resolvePersistedOwners(o.path,n,a.isSameDatabasePath??t(),a),s=e(a.defaultAgentId??`main`);return{agentId:(r.length===1?r[0]:void 0)??i??s,path:o.path,shared:!0,ownerSource:r.length===1?`database-registry`:i?`database-path`:r.length>1?`ambiguous-registry`:`configured-default`}}return resolveCustomStoreSqlitePath({unsuffixedPath:o.path,options:a})}function listDurableSqliteTargetOwnersForSessionStorePath(e){let t=/* @__PURE__ */ new Set;for(let n of a(e)){let e=resolveDatabaseOwner(n);e&&t.add(e)}return[...t]}export{resolveSqliteTargetFromSessionStorePath as a,readSessionStoreRegistryRows as i,listDurableSqliteTargetOwnersForSessionStorePath as n,prepareSqliteTargetFromSessionStorePath as r,SessionStoreRegistryReadRequired as t};