UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

150 lines (149 loc) • 6.03 kB
import { n as resolvePreferredOpenClawTmpDir } from "../../tmp-openclaw-dir-Bf0oU1Cn.mjs"; import { o as sdkOpenNodeSqliteDatabase } from "../../sqlite-runtime-lcoEVOL7.mjs"; import { n as withTempWorkspace } from "../../fs-safe-compat-Cbx7s6au.mjs"; import "../../temp-path-CW7QMeae.mjs"; import { i as applyMigrationManualItem, l as markMigrationItemConflict, r as applyMigrationConfigPatchItem, u as markMigrationItemError, v as summarizeMigrationItems } from "../../migration-C73lp9ea.mjs"; import { a as resolvePlannedMigrationTargets, i as copyMigrationFileItem, o as withCachedMigrationConfigRuntime, r as copyMemoryMigrationFileItem, s as writeMigrationReport, t as archiveMigrationItem } from "../../migration-runtime-2xeVeXDu.mjs"; import { t as appendItem } from "../../helpers-BrAeOFdd.mjs"; import { c as HERMES_REASON_MODEL_PROVIDER_CONFLICT, f as findHermesModelProviderDependency, p as readHermesModelDetails } from "../../items-CtQPQmTr.mjs"; import { t as applyAuthItem } from "../../auth-B3rTz2kG.mjs"; import { t as applyModelItem } from "../../model-kcVAWoy7.mjs"; import { t as applySecretItem } from "../../secrets-cWl7KOyf.mjs"; import { t as buildHermesPlan } from "../../plan-Cp7L0grH.mjs"; import path from "node:path"; import fs from "node:fs/promises"; //#region extensions/migrate-hermes/apply.ts const HERMES_SQLITE_SNAPSHOT_PREFIX = "openclaw-migrate-hermes-sqlite-"; function isHermesMemoryOnlyCopyItem(e) { return e.kind === "memory" && e.action === "copy" && e.details?.sourceType === "hermes-memory" && e.details?.collectionId === "hermes"; } function assertConsistentMemoryPlan(e) { const t = e.items.some(isHermesMemoryOnlyCopyItem); const n = e.items.some((e) => e.kind === "memory" && e.action === "append"); if (t && n) throw new Error("Hermes migration plan mixes memory-only copy and append items"); } async function archiveHermesItem(e, t) { if (!e.source || path.extname(e.source) !== ".db") return await archiveMigrationItem(e, t); const n = e.source; let r; try { r = await fs.lstat(n); } catch { return await archiveMigrationItem(e, t); } if (!r.isFile()) return await archiveMigrationItem(e, t); try { return await withTempWorkspace({ rootDir: resolvePreferredOpenClawTmpDir(), prefix: HERMES_SQLITE_SNAPSHOT_PREFIX }, async ({ dir: r }) => { const i = path.join(r, path.basename(n)); const a = sdkOpenNodeSqliteDatabase(n, { readOnly: true }); try { a.exec("PRAGMA busy_timeout = 30000;"); a.prepare("VACUUM INTO ?").run(i); } finally { a.close(); } await fs.chmod(i, 384); return { ...await archiveMigrationItem({ ...e, source: i }, t), source: n }; }); } catch (r) { const i = r instanceof Error ? r.message : String(r); let a; try { a = await withTempWorkspace({ rootDir: resolvePreferredOpenClawTmpDir(), prefix: HERMES_SQLITE_SNAPSHOT_PREFIX }, async ({ dir: r }) => { const i = path.join(r, `${path.basename(n)}-recovery`); await fs.mkdir(i, { recursive: true }); for (const e of [ n, `${n}-wal`, `${n}-shm` ]) { if (!(await fs.lstat(e).catch(() => void 0))?.isFile()) continue; try { await fs.copyFile(e, path.join(i, path.basename(e))); } catch (t) { const r = t.code; if (e !== n && r === "ENOENT") continue; throw t; } } return await archiveMigrationItem({ ...e, source: i }, t); }); } catch (t) { const n = t instanceof Error ? t.message : String(t); return markMigrationItemError(e, `SQLite snapshot failed: ${i}; recovery archive failed: ${n}`); } if (a.status === "migrated") return markMigrationItemError({ ...a, source: n }, `SQLite snapshot failed; database recovery files preserved for manual review: ${i}`); return markMigrationItemError({ ...a, source: n }, `SQLite snapshot failed: ${i}; recovery archive failed: ${a.reason ?? a.status}`); } } async function applyHermesPlan(e) { const t = e.plan ?? await buildHermesPlan(e.ctx); assertConsistentMemoryPlan(t); const n = e.ctx.reportDir ?? path.join(e.ctx.stateDir, "migration", "hermes"); const r = resolvePlannedMigrationTargets(e.ctx); const i = /* @__PURE__ */ new Map(); const a = withCachedMigrationConfigRuntime(e.ctx.runtime ?? e.runtime, e.ctx.config); const o = { ...e.ctx, runtime: a }; const s = [...t.items.filter((e) => e.id.startsWith("config:model-provider:")), ...t.items.filter((e) => !e.id.startsWith("config:model-provider:"))]; for (const a of s) { if (a.status !== "planned") { i.set(a, a); continue; } let s; if (a.id === "config:default-model") { const e = readHermesModelDetails(a)?.model; const n = e ? findHermesModelProviderDependency(t.items, e) : void 0; const r = n ? i.get(n) : void 0; if (r && r.status !== "migrated") s = r.status === "conflict" ? markMigrationItemConflict(a, HERMES_REASON_MODEL_PROVIDER_CONFLICT) : markMigrationItemError(a, `model provider config failed: ${r.reason ?? r.status}`); else s = await applyModelItem(o, a); } else if (a.kind === "config") s = await applyMigrationConfigPatchItem(o, a); else if (a.kind === "manual") s = applyMigrationManualItem(a); else if (a.action === "archive") s = await archiveHermesItem(a, n); else if (a.kind === "auth") s = await applyAuthItem(o, a, r); else if (a.kind === "secret") s = await applySecretItem(o, a, r); else if (isHermesMemoryOnlyCopyItem(a)) s = await copyMemoryMigrationFileItem(a, n, { workspaceDir: r.workspaceDir, overwrite: e.ctx.overwrite }); else if (a.action === "append") s = await appendItem(a); else s = await copyMigrationFileItem(a, n, { overwrite: e.ctx.overwrite }); i.set(a, s); } const c = t.items.map((e) => i.get(e) ?? e); const l = { ...t, items: c, summary: summarizeMigrationItems(c), backupPath: e.ctx.backupPath, reportDir: n }; await writeMigrationReport(l, { title: "Hermes Migration Report" }); return l; } //#endregion export { applyHermesPlan };