UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

9 lines • 3.14 kB
import{n as normalizeLowercaseStringOrEmpty}from"./string-coerce-BBqv4GXj.mjs";import{n as resolveHomeRelativePath,r as resolveRequiredHomeDir}from"./home-dir-C2A7IM0a.mjs";import{hasNodeErrorCode,isNotFoundPathError,isPathInside as isPathInside$1,normalizeWindowsPathForComparison as normalizeWindowsPathForComparison$1}from"@openclaw/fs-safe/path";import path from"node:path";import os from"node:os"; //!src/infra/path-guards.ts function normalizeWindowsPathPreservingCase(e){const t=path.win32.normalize(e);if(!t.startsWith(`\\\\?\\`))return t;const n=t.slice(4);return n.toUpperCase().startsWith(`UNC\\`)?`\\\\${n.slice(4)}`:n} //!src/cli/profile-utils.ts const PROFILE_NAME_RE=/^[a-z0-9][a-z0-9_-]{0,63}$/i;function isValidProfileName(e){return PROFILE_NAME_RE.test(e)}function normalizeProfileName(e){const t=e?.trim();if(!t)return null;if(normalizeLowercaseStringOrEmpty(t)==="default")return null;if(!isValidProfileName(t))return null;return t}function resolveProfileGatewayPort(e){if(e===`dev`)return 19001;let t=2166136261;for(const n of Buffer.from(e,`utf8`))t=Math.imul(t^n,16777619)>>>0;return 2e4+t%4e4}function resolveProfileStateDir(e,t,n){const r=e.trim();if(!isValidProfileName(r))throw new Error(`Invalid profile name: ${JSON.stringify(e)}`);const i=normalizeLowercaseStringOrEmpty(r)==="default"?``:`-${r}`;return path.join(resolveRequiredHomeDir(t,n),`.openclaw${i}`)} //!src/infra/lru-cache.ts var LruCache=class{#e;#t;#n;#r=/* @__PURE__ */ new Map;#i=0;constructor(e,t){this.#e=Number.isFinite(e)&&e>0?Math.max(1,Math.floor(e)):1;this.#t=t?.maxBytes??Number.POSITIVE_INFINITY;this.#n=t?.sizeOf}get size(){return this.#r.size}clear(){this.#r.clear();this.#i=0}keys(){return this.#r.keys()}delete(e){if(!this.#r.has(e))return false;this.#i-=this.#n?.(this.#r.get(e))??0;return this.#r.delete(e)}deleteValue(e){for(const[t,n]of this.#r)if(n===e)this.delete(t)}peek(e){return this.#r.get(e)}get(e){if(!this.#r.has(e))return;const t=this.#r.get(e);this.#r.delete(e);this.#r.set(e,t);return t}set(e,t){this.delete(e);this.#r.set(e,t);this.#i+=this.#n?.(t)??0;for(const e of this.#r.keys()){if(this.#r.size<=this.#e&&this.#i<=this.#t)break;this.delete(e)}}}; //!src/config/state-dir.ts const absoluteStateDirs=new LruCache(64);function resolveNewStateDir(e=resolveRequiredHomeDir){return path.join(e(),`.openclaw`)}function resolveStateDir(e=process.env,t=os.homedir){const n=e.OPENCLAW_STATE_DIR?.trim();if(n&&process.platform!==`win32`&&path.isAbsolute(n)){const e=absoluteStateDirs.peek(n);if(e!==void 0)return e;const t=path.resolve(n);absoluteStateDirs.set(n,t);return t}if(n)return resolveHomeRelativePath(n,{env:e,homedir:t});return resolveStateDirFromHome(e,()=>resolveRequiredHomeDir(e,t))}function resolveStateDirFromHome(e,t){const n=normalizeProfileName(e.OPENCLAW_PROFILE);return n?resolveProfileStateDir(n,e,t):resolveNewStateDir(t)}export{resolveProfileStateDir as a,isPathInside$1 as c,resolveProfileGatewayPort as i,normalizeWindowsPathForComparison$1 as l,LruCache as n,hasNodeErrorCode as o,normalizeProfileName as r,isNotFoundPathError as s,resolveStateDir as t,normalizeWindowsPathPreservingCase as u};