UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

163 lines (162 loc) • 5.87 kB
import { o as resolveRequiredOsHomeDir } from "./home-dir-EKmgmdPG.mjs"; import { i as isMissingPathError } from "./errno-DFcoqIT9.mjs"; import { r as isPathInside } from "./path-guards-TIjxtj6H.mjs"; import { n as LEGACY_IMPLICIT_AGENT_ID } from "./session-key-CqAyZeVk.mjs"; import { i as listAgentIds } from "./agent-roster-t1ooUdXp.mjs"; import { O as tryResolveAmbientOwnerAgentId, j as tryResolveLegacyDataOwnerAgentId, v as resolveEffectiveAgentDir } from "./agent-scope-config-DmXgFGKP.mjs"; import { r as isUpdateRehearsalReadOnlyPath } from "./update-rehearsal-paths-Dr1MbUA_.mjs"; import { r as cloneEnvWithPlatformSemantics } from "./config-env-vars-ByeGN-9N.mjs"; import { t as inspectOpenClawAgentDatabaseOwner } from "./openclaw-agent-db-owner-read-BF5eClGm.mjs"; import { t as resolveLegacyProfileMigrationExceptions } from "./state-migrations.profile-scope-Da_ly_JW.mjs"; import fs from "node:fs"; import path from "node:path"; import os from "node:os"; import { replaceFileAtomicSync } from "@openclaw/fs-safe/atomic"; //#region src/infra/state-migrations.agent-dir-receipt.ts const LEGACY_AGENT_DIR_RECEIPT = ".legacy-agent-dir-migration.json"; function receiptContent(e, t) { return `${JSON.stringify({ version: 1, source: e, target: t })}\n`; } function hasCompletedLegacyAgentDirMigration(e, t) { try { return fs.lstatSync(path.join(t, ".legacy-agent-dir-migration.json")).isFile() && fs.readFileSync(path.join(t, ".legacy-agent-dir-migration.json"), "utf8") === receiptContent(fs.realpathSync(e), fs.realpathSync(t)); } catch (e) { if (isMissingPathError(e)) return false; throw e; } } function recordCompletedLegacyAgentDirMigration(e, t) { const n = path.join(t, LEGACY_AGENT_DIR_RECEIPT); const r = receiptContent(e, t); if (fs.lstatSync(n, { throwIfNoEntry: false })?.isFile() && fs.readFileSync(n, "utf8") === r) return; const requireMissingReceipt = () => { if (fs.lstatSync(n, { throwIfNoEntry: false })) throw new Error(`Preserved unrecognized migration receipt at ${n}; inspect it before moving it aside`); }; requireMissingReceipt(); replaceFileAtomicSync({ filePath: n, content: r, mode: 384, dirMode: fs.statSync(t).mode & 4095, tempPrefix: ".legacy-agent-dir-migration", beforeRename: requireMissingReceipt, syncTempFile: true, syncParentDir: true }); } function legacyAgentQuarantineNotices(e, t, n = Date.now()) { let r; try { r = fs.realpathSync(e); } catch { return []; } return [r, path.join(r, "agents", t)].flatMap((e) => { try { const t = fs.realpathSync(e); if (t !== r && !isPathInside(r, t)) return []; const i = fs.readdirSync(t, { withFileTypes: true }).filter((e) => { const t = /^agent\.legacy-(\d+)(?:-|$)/.exec(e.name)?.[1]; return e.isDirectory() && t && n - Number(t) > 2592e6; }); return i.length > 0 ? [`${i.length} legacy agent quarantine(s) older than 30 days in ${t}; inspect agent.legacy-* and remove only copies you no longer need.`] : []; } catch { return []; } }); } //#endregion //#region src/agents/install-agent-dir.ts function resolveInstallAgentDir(e, t) { const n = cloneEnvWithPlatformSemantics(t?.env ?? process.env); const r = t?.homedir ?? os.homedir; let i; const read = () => i ??= typeof e === "function" ? e(n) : { config: e, env: n }; const overrideDir = () => t?.agentDir ?? ((i?.env ?? n).OPENCLAW_AGENT_DIR?.replace(/^~(?=\/|$)/, () => resolveRequiredOsHomeDir(i?.env ?? n, r)) || void 0); const agentId = () => { const { config: e } = read(); const t = tryResolveAmbientOwnerAgentId(e); return t && listAgentIds(e).includes(t) ? t : void 0; }; const targetDir = (e) => { const t = overrideDir(); if (t !== void 0) return t; const { config: n, env: i } = read(); const a = e ?? agentId(); return overrideDir() || (a ? resolveEffectiveAgentDir(n, a, { env: i, homedir: r }) : void 0); }; const select = (e, n) => { let r; const readOwner = () => { const r = path.join(e, "openclaw-agent.sqlite"); if (fs.existsSync(r)) { const e = inspectOpenClawAgentDatabaseOwner(r); if (e.status !== "owned") throw new Error(`Cannot read the agent database owner at ${r}. Run openclaw doctor --fix.`); return e.agentId; } return n === "legacy" || t?.agentDir !== void 0 ? LEGACY_IMPLICIT_AGENT_ID : agentId(); }; return { dir: e, migrationState: n, get owner() { return (r ??= { owner: readOwner() }).owner; } }; }; let a; const resolveDirectory = () => { const e = targetDir(); const t = overrideDir(); if (t !== void 0) return select(t, "explicit"); const { standaloneAgent: n } = resolveLegacyProfileMigrationExceptions({ env: read().env, homedir: r, configuredAgentDir: e }); try { if (n && !isUpdateRehearsalReadOnlyPath(n, read().env) && fs.readdirSync(n).length > 0 && (!e || !hasCompletedLegacyAgentDirMigration(n, e))) return select(n, "legacy"); } catch (e) { if (!isMissingPathError(e)) throw e; } return e ? select(e, "current") : void 0; }; return { get config() { return read().config; }, get env() { return read().env; }, get migrationTarget() { const { config: e } = read(); const t = tryResolveAmbientOwnerAgentId(e) ?? tryResolveLegacyDataOwnerAgentId(e); const n = t && listAgentIds(e).includes(t) ? t : void 0; const r = targetDir(n); return r === void 0 ? void 0 : { dir: r, owner: n }; }, get optionalDirectory() { return a ??= resolveDirectory(); }, get directory() { const e = a ??= resolveDirectory(); if (!e) throw new Error("Select an agent owner or set OPENCLAW_AGENT_DIR before resolving the install directory."); return e; } }; } //#endregion export { recordCompletedLegacyAgentDirMigration as i, LEGACY_AGENT_DIR_RECEIPT as n, legacyAgentQuarantineNotices as r, resolveInstallAgentDir as t };