openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 5.98 kB
JavaScript
import{I as e,N as t,p as n}from"./disk-budget.worker-paths-DAFiPrD7.mjs";import{realpathSync as r,statSync as i}from"node:fs";import a from"node:path";import{isDeepStrictEqual as o}from"node:util";import{probePathSuffixAliasesSync as s,resolvePathPrefixSync as c}from"@openclaw/fs-safe/advanced";function ok(e){return{ok:!0,value:e}}function err(e){return{ok:!1,error:e}}const l=/^[a-z0-9][a-z0-9_-]{0,63}$/i,u=/[^a-z0-9_-]+/g,d=/^-+/,f=/-+$/;function normalizeAgentId(e){let t=normalizeAgentIdStrict(e);return t.ok?t.value:`main`}function normalizeAgentIdStrict(e){let t=(e??``).trim(),n=t.toLowerCase();if(l.test(t))return ok(n);let r=n.replace(u,`-`).replace(d,``).replace(f,``).slice(0,64);return r?ok(r):err(`unrepresentable`)}function isValidAgentId(e){let t=(e??``).trim();return!!t&&l.test(t)}const p=`incognito-openclaw-agent.sqlite`,m=/* @__PURE__ */ new Map,h=m.keys();function resolveOpenClawAgentSqlitePath(e){if(e.path!=null)return a.resolve(e.path);let t=normalizeAgentId(e.agentId),r=n(e.env??process.env),i=`${t}:${r}`,o=m.get(i);if(o!==void 0)return o;let s=a.resolve(r,`agents`,t,`agent`,`openclaw-agent.sqlite`);if(m.set(i,s),m.size>256){let e=h.next();e.done||m.delete(e.value)}return s}function resolveIncognitoOpenClawAgentSqlitePath(e){return a.join(a.dirname(resolveOpenClawAgentSqlitePath(e)),p)}function isIncognitoOpenClawAgentSqlitePath(e,t){let n=a.resolve(e);return a.basename(n)===`incognito-openclaw-agent.sqlite`&&n===resolveIncognitoOpenClawAgentSqlitePath(t)}const g=/* @__PURE__ */ new Map;function shouldProbeUnicodeCaseVariants(e,t){let hasNonAscii=e=>e.split(``).some(e=>e.charCodeAt(0)>127);if(!hasNonAscii(e)&&!hasNonAscii(t))return!1;let n=e.toLowerCase()===t.toLowerCase();return e.toUpperCase()===t.toUpperCase()||n&&Array.from(e).length===Array.from(t).length}function areMissingSuffixAliases(e){if(e.left===e.right)return!0;let t=e.left.split(a.sep),n=e.right.split(a.sep);if(t.length!==n.length||[...t,...n].some(e=>!e||e===`.`||e===`..`))return!1;let r=[e.left,e.right].toSorted(),i=JSON.stringify([e.parentDevice,e.parentInode,e.parentRealPath,...r]),o=g.get(i);if(o!==void 0)return o;let c,l;try{c=s({directory:e.parentRealPath,left:e.left,right:e.right,maxDepth:t.length,shouldProbeCaseVariants:shouldProbeUnicodeCaseVariants})}catch(e){l=e}if(c===void 0)throw Error(`Cannot determine whether database paths alias under ${JSON.stringify(e.parentRealPath)}: ${JSON.stringify(e.left)} and ${JSON.stringify(e.right)}. Check directory access and retry.`,{cause:l});return g.set(i,c),c}function anchorDatabasePathWithoutNormalizing(e){let t=a.sep===`\\`?e.replaceAll(`/`,`\\`):e;if(a.isAbsolute(t))return t;if(a.sep===`\\`){let e=/^([A-Za-z]:)(.*)$/u.exec(t);if(e){let t=a.resolve(`${e[1]}.`);return e[2]?`${t}${t.endsWith(a.sep)?``:a.sep}${e[2]}`:t}}let n=process.cwd();return`${n}${n.endsWith(a.sep)?``:a.sep}${t}`}function resolveAgentDatabasePathIdentity(t){let n=anchorDatabasePathWithoutNormalizing(t);try{let e=r.native(n),t=i(e,{bigint:!0});return{lexicalPath:n,realPath:e,device:t.dev.toString(),inode:t.ino.toString()}}catch(t){if(!e(t,`ENOENT`))throw t;let r=a.parse(n).root,o=c(r+n.slice(r.length).split(a.sep).filter(Boolean).join(a.sep)),s=o.existingPath,l=i(s,{bigint:!0});return{lexicalPath:n,parentDevice:l.dev.toString(),parentInode:l.ino.toString(),parentRealPath:s,unresolvedSuffix:o.unresolvedSegments.join(a.sep)}}}function areSameAgentDatabasePathIdentities(e,t){return e.lexicalPath===t.lexicalPath?!0:`realPath`in e?`realPath`in t&&(e.realPath===t.realPath||e.device===t.device&&e.inode===t.inode):!(`realPath`in t)&&e.parentDevice===t.parentDevice&&e.parentInode===t.parentInode&&areMissingSuffixAliases({left:e.unresolvedSuffix,right:t.unresolvedSuffix,parentDevice:e.parentDevice,parentInode:e.parentInode,parentRealPath:e.parentRealPath})}function createOpenClawAgentDatabasePathMatcher(e=[]){let t=new Map(e.map(e=>[e.lexicalPath,e])),resolveIdentity=e=>{let n=anchorDatabasePathWithoutNormalizing(e),r=t.get(n);if(r)return r;let i=resolveAgentDatabasePathIdentity(n);return t.set(n,i),i};return Object.assign((e,t)=>areSameAgentDatabasePathIdentities(resolveIdentity(e),resolveIdentity(t)),{capture(e){resolveIdentity(e)},observations:()=>[...t.values()],createIndex(){let e=/* @__PURE__ */ new Map,t=[],n=/* @__PURE__ */ new Map,r=/* @__PURE__ */ new Map,i=/* @__PURE__ */ new Map;return a=>{let o=resolveIdentity(a),s=e.get(o.lexicalPath);if(s!==void 0)return s;let c;if(`realPath`in o){let e=`${o.device}:${o.inode}`,i=n.get(e),a=r.get(o.realPath);c=i===void 0?a:a===void 0?i:Math.min(i,a),c===void 0&&(c=t.push(o)-1,n.set(e,c),r.set(o.realPath,c))}else{let e=`${o.parentDevice}:${o.parentInode}`,n=i.get(e)??[];c=n.find(e=>areSameAgentDatabasePathIdentities(t[e],o)),c===void 0&&(c=t.push(o)-1,n.push(c),i.set(e,n))}let l=t[c].lexicalPath;return e.set(o.lexicalPath,l),l}},isCurrent(){for(let e of t.values()){let t=resolveAgentDatabasePathIdentity(e.lexicalPath);if(!o(e,t))return!1}return!0}})}function isSameOpenClawAgentDatabasePath(e,t){return areSameAgentDatabasePathIdentities(resolveAgentDatabasePathIdentity(e),resolveAgentDatabasePathIdentity(t))}function canonicalPathForRegistryBoundary(e){let t=resolveAgentDatabasePathIdentity(e);if(`realPath`in t)return t.realPath;let n=t.unresolvedSuffix.split(a.sep);return n.includes(`..`)?t.parentRealPath:a.join(t.parentRealPath,...n)}function isPersistentOpenClawAgentDatabasePath(e,r=process.env){let i=a.resolve(e),o=a.join(a.resolve(n(r)),`imports`);if(t(o,i))return!1;let s=canonicalPathForRegistryBoundary(e),c=canonicalPathForRegistryBoundary(n(r)),l=canonicalPathForRegistryBoundary(a.join(c,`imports`));return!t(l,s)}export{isSameOpenClawAgentDatabasePath as a,isValidAgentId as c,ok as d,isPersistentOpenClawAgentDatabasePath as i,normalizeAgentId as l,createOpenClawAgentDatabasePathMatcher as n,resolveIncognitoOpenClawAgentSqlitePath as o,isIncognitoOpenClawAgentSqlitePath as r,resolveOpenClawAgentSqlitePath as s,p as t,err as u};