openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 6.41 kB
JavaScript
import{s as hasErrnoCode}from"./global-singleton-CTnqrmaI.js";import{g as normalizeAgentId}from"./runtime-doctor-migrations-D8alsOFs.js";import{c as resolveStateDir,i as isPathInside}from"./account-core-34NfXidI.js";import"./boundary-file-read-D9ria5A8.js";import path from"node:path";import{realpathSync,statSync}from"node:fs";import{isDeepStrictEqual}from"node:util";import{probePathSuffixAliasesSync,resolvePathPrefixSync}from"@openclaw/fs-safe/advanced";
//!src/state/openclaw-agent-db.paths.ts
const INCOGNITO_AGENT_SQLITE_BASENAME=`incognito-openclaw-agent.sqlite`;const agentSqlitePaths=/* @__PURE__ */ new Map;const agentSqlitePathKeys=agentSqlitePaths.keys();function resolveOpenClawAgentSqlitePath(e){if(e.path!=null)return path.resolve(e.path);const t=normalizeAgentId(e.agentId);const n=resolveStateDir(e.env??process.env);const r=`${t}:${n}`;const i=agentSqlitePaths.get(r);if(i!==void 0)return i;const a=path.resolve(n,`agents`,t,`agent`,`openclaw-agent.sqlite`);agentSqlitePaths.set(r,a);if(agentSqlitePaths.size>256){const e=agentSqlitePathKeys.next();if(!e.done)agentSqlitePaths.delete(e.value)}return a}function resolveIncognitoOpenClawAgentSqlitePath(e){return path.join(path.dirname(resolveOpenClawAgentSqlitePath(e)),INCOGNITO_AGENT_SQLITE_BASENAME)}function isIncognitoOpenClawAgentSqlitePath(e,t){const n=path.resolve(e);return path.basename(n)===`incognito-openclaw-agent.sqlite`&&n===resolveIncognitoOpenClawAgentSqlitePath(t)}const missingSuffixAliasCache=/* @__PURE__ */ new Map;function shouldProbeUnicodeCaseVariants(e,t){const hasNonAscii=e=>e.split(``).some(e=>e.charCodeAt(0)>127);if(!hasNonAscii(e)&&!hasNonAscii(t))return false;const 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 true;const t=e.left.split(path.sep);const n=e.right.split(path.sep);if(t.length!==n.length||[...t,...n].some(e=>!e||e===`.`||e===`..`))return false;const r=[e.left,e.right].toSorted();const i=JSON.stringify([e.parentDevice,e.parentInode,e.parentRealPath,...r]);const a=missingSuffixAliasCache.get(i);if(a!==void 0)return a;let o;let s;try{o=probePathSuffixAliasesSync({directory:e.parentRealPath,left:e.left,right:e.right,maxDepth:t.length,shouldProbeCaseVariants:shouldProbeUnicodeCaseVariants})}catch(e){s=e}if(o===void 0)throw new 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:s});missingSuffixAliasCache.set(i,o);return o}function anchorDatabasePathWithoutNormalizing(e){const t=path.sep===`\\`?e.replaceAll(`/`,`\\`):e;if(path.isAbsolute(t))return t;if(path.sep===`\\`){const e=/^([A-Za-z]:)(.*)$/u.exec(t);if(e){const t=path.resolve(`${e[1]}.`);return e[2]?`${t}${t.endsWith(path.sep)?``:path.sep}${e[2]}`:t}}const n=process.cwd();return`${n}${n.endsWith(path.sep)?``:path.sep}${t}`}function resolveAgentDatabasePathIdentity(e){const t=anchorDatabasePathWithoutNormalizing(e);try{const e=realpathSync.native(t);const n=statSync(e,{bigint:true});return{lexicalPath:t,realPath:e,device:n.dev.toString(),inode:n.ino.toString()}}catch(e){if(!hasErrnoCode(e,`ENOENT`))throw e;const n=path.parse(t).root;const r=resolvePathPrefixSync(n+t.slice(n.length).split(path.sep).filter(Boolean).join(path.sep));const i=r.existingPath;const a=statSync(i,{bigint:true});return{lexicalPath:t,parentDevice:a.dev.toString(),parentInode:a.ino.toString(),parentRealPath:i,unresolvedSuffix:r.unresolvedSegments.join(path.sep)}}}function areSameAgentDatabasePathIdentities(e,t){if(e.lexicalPath===t.lexicalPath)return true;if(`realPath`in e)return`realPath`in t&&(e.realPath===t.realPath||e.device===t.device&&e.inode===t.inode);return!(`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=[]){const t=new Map(e.map(e=>[e.lexicalPath,e]));const resolveIdentity=e=>{const n=anchorDatabasePathWithoutNormalizing(e);const r=t.get(n);if(r)return r;const i=resolveAgentDatabasePathIdentity(n);t.set(n,i);return i};return Object.assign((e,t)=>areSameAgentDatabasePathIdentities(resolveIdentity(e),resolveIdentity(t)),{capture(e){resolveIdentity(e)},observations:()=>[...t.values()],createIndex(){const e=/* @__PURE__ */ new Map;const t=[];const n=/* @__PURE__ */ new Map;const r=/* @__PURE__ */ new Map;const i=/* @__PURE__ */ new Map;return a=>{const o=resolveIdentity(a);const s=e.get(o.lexicalPath);if(s!==void 0)return s;let c;if(`realPath`in o){const e=`${o.device}:${o.inode}`;const i=n.get(e);const a=r.get(o.realPath);c=i===void 0?a:a===void 0?i:Math.min(i,a);if(c===void 0){c=t.push(o)-1;n.set(e,c);r.set(o.realPath,c)}}else{const e=`${o.parentDevice}:${o.parentInode}`;const n=i.get(e)??[];c=n.find(e=>areSameAgentDatabasePathIdentities(t[e],o));if(c===void 0){c=t.push(o)-1;n.push(c);i.set(e,n)}}const l=t[c].lexicalPath;e.set(o.lexicalPath,l);return l}},isCurrent(){for(const e of t.values()){const t=resolveAgentDatabasePathIdentity(e.lexicalPath);if(!isDeepStrictEqual(e,t))return false}return true}})}function isSameOpenClawAgentDatabasePath(e,t){return areSameAgentDatabasePathIdentities(resolveAgentDatabasePathIdentity(e),resolveAgentDatabasePathIdentity(t))}function canonicalPathForRegistryBoundary(e){const t=resolveAgentDatabasePathIdentity(e);if(`realPath`in t)return t.realPath;const n=t.unresolvedSuffix.split(path.sep);return n.includes(`..`)?t.parentRealPath:path.join(t.parentRealPath,...n)}function isPersistentOpenClawAgentDatabasePath(e,t=process.env){const n=path.resolve(e);const r=path.join(path.resolve(resolveStateDir(t)),`imports`);if(isPathInside(r,n))return false;const i=canonicalPathForRegistryBoundary(e);const a=canonicalPathForRegistryBoundary(resolveStateDir(t));const o=canonicalPathForRegistryBoundary(path.join(a,`imports`));return!isPathInside(o,i)}export{isSameOpenClawAgentDatabasePath as a,isPersistentOpenClawAgentDatabasePath as i,createOpenClawAgentDatabasePathMatcher as n,resolveIncognitoOpenClawAgentSqlitePath as o,isIncognitoOpenClawAgentSqlitePath as r,resolveOpenClawAgentSqlitePath as s,INCOGNITO_AGENT_SQLITE_BASENAME as t};