UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

88 lines (87 loc) • 3.97 kB
import { c as hasErrnoCode } from "./paths-0xDagtub.mjs"; import { a as resolveStateDir, i as normalizeWindowsPathPreservingCase, n as isPathInside, o as LruCache } from "./path-guards-quCEQx1i.mjs"; import { v as resolveIdentityPathViaExistingAncestorSync } from "./redact-BRF0hr6G.mjs"; import { c as normalizeDatabasePath } from "./sqlite-worker-identity-CiWdPbPl.mjs"; import { statSync } from "node:fs"; import path from "node:path"; //#region src/state/openclaw-state-db.paths.ts const stateSqlitePaths = new LruCache(64); function resolveDatabasePath(e = {}) { return e.path === void 0 ? resolveOpenClawStateSqlitePath(e.env ?? process.env) : normalizeDatabasePath(path.resolve(e.path)); } function existingPathOrUndefined(e) { try { statSync(e); return e; } catch (e) { if (hasErrnoCode(e, "ENOENT")) return; throw e; } } function resolveOpenClawStateSqliteDir(e = process.env) { return path.dirname(resolveOpenClawStateSqlitePath(e)); } function resolveOpenClawStateSqlitePath(e = process.env) { const t = resolveStateDir(e); const n = stateSqlitePaths.peek(t); if (n !== void 0) return n; const r = normalizeDatabasePath(path.join(t, "state", "openclaw.sqlite")); stateSqlitePaths.set(t, r); return r; } /** Resolve the state owner directory for a canonical or explicit shared database path. */ function resolveOpenClawStateDirForDatabasePath(e) { const t = path.dirname(path.resolve(e)); return path.basename(t) === "state" ? path.dirname(t) : t; } /** Resolve the integrity/quarantine store that survives loss of the primary state database. */ function resolveQuarantineStorePath(e) { return path.join(resolveOpenClawStateSqliteDir(e), "openclaw-quarantine.sqlite"); } /** Resolve the durable registry form for one agent database path. */ function resolveOpenClawAgentDatabaseStoredPath(e, t) { const n = process.platform === "win32"; const r = resolveOpenClawStateDirForDatabasePath(e); let i = n ? normalizeWindowsPathPreservingCase(r) : r; const a = n && path.isAbsolute(t) ? t : path.resolve(t); const o = n ? normalizeWindowsPathPreservingCase(a) : a; if (!path.isAbsolute(i) || !path.isAbsolute(o)) return a; let s = path.relative(i, o); const c = s.startsWith("..") || path.isAbsolute(s) || n && !isPathInside(r, a); if (c) { const e = resolveIdentityPathViaExistingAncestorSync(i); if (n && !isPathInside(e, a)) return a; i = n ? normalizeWindowsPathPreservingCase(e) : e; if (!path.isAbsolute(i)) return a; s = path.relative(i, o); } if (s.startsWith("..") || path.isAbsolute(s)) return a; if (c && !n) return s; const l = [i, path.toNamespacedPath(i)].map((e) => `${e}${e.endsWith(path.sep) ? "" : path.sep}`).find((e) => t.startsWith(e)); return path.isAbsolute(t) && l !== void 0 ? t.slice(l.length) : s; } function describeAgentPathMigration(e) { const { relativized: t, reanchored: n, deleted: r } = e; if (t === 0 && n.length === 0 && r.length === 0) return []; const i = n.length + r.length; const a = [ `${t} relativized`, n.length > 0 && `${n.length} re-anchored`, r.length > 0 && `${r.length} removed` ].filter(Boolean); return [ `Migrated agent database registry paths to state-relative storage${i > 0 ? ` (${a.join(", ")})` : ""}`, ...n.map((e) => `Re-anchored agent database registry path ${e} to the current state directory`), ...r.map((e) => `Removed duplicate agent database registry path ${e}`) ]; } function warnAgentPathMigration(e, t, n) { if (t.reanchored.length === 0 && t.deleted.length === 0) return; e.warn("agent database registry rows re-anchored or removed during v9 migration", { reanchored: t.reanchored, deleted: t.deleted, path: n }); } //#endregion export { resolveOpenClawStateDirForDatabasePath as a, warnAgentPathMigration as c, resolveOpenClawAgentDatabaseStoredPath as i, existingPathOrUndefined as n, resolveOpenClawStateSqlitePath as o, resolveDatabasePath as r, resolveQuarantineStorePath as s, describeAgentPathMigration as t };