openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
146 lines (145 loc) • 5.8 kB
JavaScript
import { o as writeRuntimeJson } from "./runtime-B-SLYP-m.mjs";
import { r as terminalText } from "./safe-text-C80HAQ2e.mjs";
import { r as resolveStateDir } from "./state-dir-DiG0gXzD.mjs";
import { p as shortenHomePath } from "./utils-C8YECn2Q.mjs";
import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs";
import { n as normalizeAgentId } from "./agent-id-CQjtp2ft.mjs";
import "./session-key-CqAyZeVk.mjs";
import { h as resolveConfiguredAgentId } from "./agent-scope-config-DmXgFGKP.mjs";
import { a as isSqliteCorruptionError } from "./sqlite-error-diagnostics-6_hREUnq.mjs";
import { s as resolveOpenClawStateSqlitePath } from "./openclaw-state-db.paths-R3ArRCcz.mjs";
import { t as readBackupEngine } from "./physical-store-BRXG56Jw.mjs";
import { t as captureRuntimeConfigAsyncReader } from "./io.runtime-DsJ2dLCQ.mjs";
import "./config-CMA-J084.mjs";
import { t as assertNotUpdateCapturePath } from "./update-capture-paths-B2WcdTCG.mjs";
import "./database-backup-B6k1k0NC.mjs";
import { a as recordBackupOutcomeBestEffort, o as resolveBackupAgentRoot, u as resolveRequiredBackupPath } from "./backup-shared-BmR-itzA.mjs";
import { n as createLocalSqliteSnapshotProvider } from "./local-repository-C3N4pLx_.mjs";
import path from "node:path";
import fs from "node:fs/promises";
//#region src/commands/backup-sqlite.ts
const OPENCLAW_SNAPSHOT_READ_OPTIONS = { allowedDatabaseRoles: ["global", "agent"] };
async function backupSqliteCreateCommand(e, t) {
const n = resolveRequiredBackupPath(t.repository, "--repository");
const r = t.global === true ? resolveOpenClawStateSqlitePath() : void 0;
if (r) assertNotUpdateCapturePath(r, resolveStateDir());
const i = await captureRuntimeConfigAsyncReader({ skipPluginValidation: true })();
try {
const a = await resolveSnapshotDatabase(t, i, r);
let o = false;
try {
o = readBackupEngine(a.path).engine === "postgres";
} catch (e) {
if (!isSqliteCorruptionError(e)) throw e;
}
if (o) throw new Error("backup sqlite cannot copy a PostgreSQL anchor. Use openclaw backup create; stop through the service owner, then rerun.");
const s = await createLocalSqliteSnapshotProvider({ repositoryPath: n }).create(a);
const c = {
ok: true,
snapshotPath: s.ref.path,
manifest: s.manifest
};
await recordBackupOutcomeBestEffort(e, {
kind: "sqlite-snapshot",
archivePath: c.snapshotPath,
status: "ok"
});
if (t.json) writeRuntimeJson(e, c);
else e.log([
terminalText`SQLite snapshot created: ${shortenHomePath(c.snapshotPath)}`,
terminalText`Database: ${formatDatabaseIdentity(c.manifest.database)}`,
terminalText`Size: ${c.manifest.artifact.sizeBytes} bytes`
].join("\n"));
return c;
} catch (t) {
await recordBackupOutcomeBestEffort(e, {
kind: "sqlite-snapshot",
archivePath: n,
status: "failed",
error: formatErrorMessage(t)
});
throw t;
}
}
async function backupSqliteListCommand(e, t) {
const n = resolveRequiredBackupPath(t.repository, "--repository");
const r = await createLocalSqliteSnapshotProvider({
repositoryPath: n,
...OPENCLAW_SNAPSHOT_READ_OPTIONS
}).list();
const i = {
ok: true,
repositoryPath: n,
snapshots: r
};
if (t.json) writeRuntimeJson(e, i);
else if (r.length === 0) e.log(terminalText`No SQLite snapshots in ${shortenHomePath(n)}.`);
else e.log(r.map((e) => terminalText`${e.manifest.createdAt} ${formatDatabaseIdentity(e.manifest.database)} ${e.manifest.artifact.sizeBytes} bytes ${shortenHomePath(e.ref.path)}`).join("\n"));
return i;
}
async function backupSqliteVerifyCommand(e, t, n) {
const r = resolveSnapshot(t, n.scratch);
const i = await r.provider.verify(r.ref);
const a = {
ok: true,
snapshotPath: r.ref.path,
manifest: i.manifest
};
if (n.json) writeRuntimeJson(e, a);
else e.log(terminalText`SQLite snapshot verified: ${shortenHomePath(a.snapshotPath)} (${formatDatabaseIdentity(a.manifest.database)})`);
return a;
}
async function backupSqliteRestoreCommand(e, t, n) {
const r = resolveSnapshot(t);
const i = resolveRequiredBackupPath(n.target, "--target");
const a = await r.provider.restoreFresh(r.ref, i);
const o = {
ok: true,
snapshotPath: r.ref.path,
targetPath: i,
manifest: a.manifest
};
if (n.json) writeRuntimeJson(e, o);
else e.log(terminalText`SQLite snapshot restored: ${shortenHomePath(o.targetPath)} (${formatDatabaseIdentity(o.manifest.database)})`);
return o;
}
async function resolveSnapshotDatabase(e, t, n) {
const r = e.agent?.trim();
if (e.agent !== void 0 && !r) throw new Error("--agent must not be blank");
if (e.global === true && r) throw new Error("Choose exactly one SQLite snapshot source: --global or --agent <id>.");
if (e.global !== true && !r) throw new Error("Choose a SQLite snapshot source: --global or --agent <id>.");
if (n) return {
path: await fs.realpath(n),
identity: { role: "global" }
};
const i = resolveConfiguredAgentId(t, normalizeAgentId(r));
const a = await resolveBackupAgentRoot(t, i);
assertNotUpdateCapturePath(a.databasePath, resolveStateDir());
return {
path: await fs.realpath(a.databasePath),
identity: {
role: "agent",
agentId: i
}
};
}
function resolveSnapshot(e, t) {
const n = resolveRequiredBackupPath(e, "<snapshot>");
const r = path.dirname(n);
const i = t ? resolveRequiredBackupPath(t, "--scratch") : path.dirname(r);
return {
provider: createLocalSqliteSnapshotProvider({
repositoryPath: r,
validationRootPath: i,
...OPENCLAW_SNAPSHOT_READ_OPTIONS
}),
ref: { path: n }
};
}
function formatDatabaseIdentity(e) {
if (e.role === "global") return "global";
if (e.role === "agent") return `agent:${e.agentId}`;
return e.id;
}
//#endregion
export { backupSqliteCreateCommand, backupSqliteListCommand, backupSqliteRestoreCommand, backupSqliteVerifyCommand };