UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

5 lines • 5.24 kB
import{o as writeRuntimeJson}from"./runtime-DIcLIHcp.mjs";import{r as terminalText}from"./safe-text-B_QTgn0s.mjs";import{r as resolveStateDir}from"./state-dir-BwIClCab.mjs";import{f as shortenHomePath}from"./utils-Dlk0IYAa.mjs";import{t as formatErrorMessage}from"./errors-D37BJTfI.mjs";import{n as normalizeAgentId}from"./agent-id-cOfqmfln.mjs";import"./session-key-BPgKfU2m.mjs";import{h as resolveConfiguredAgentId}from"./agent-scope-config-CG9CFpGX.mjs";import{a as isSqliteCorruptionError}from"./sqlite-error-diagnostics-ASl1MXPN.mjs";import{s as resolveOpenClawStateSqlitePath}from"./openclaw-state-db.paths-CbgXfEg1.mjs";import{t as readBackupEngine}from"./physical-store-Dt80uUi6.mjs";import{t as captureRuntimeConfigAsyncReader}from"./io.runtime-BvSeVFIY.mjs";import"./config-DkZ8y8Gv.mjs";import{n as throwExpectedCliError}from"./expected-cli-error-oDVmXYil.mjs";import{t as assertNotUpdateCapturePath}from"./update-capture-paths-C5GOuESg.mjs";import"./database-backup-yFN_P503.mjs";import{a as recordBackupOutcomeBestEffort,o as resolveBackupAgentRoot,u as resolveRequiredBackupPath}from"./backup-shared-CVcoDGTC.mjs";import{n as createLocalSqliteSnapshotProvider}from"./local-repository-Cak9Fbcn.mjs";import path from"node:path";import fs from"node:fs/promises"; //!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(` `));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(` `));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)throwExpectedCliError(`--agent must not be blank`);if(e.global===true&&r)throwExpectedCliError(`Choose exactly one SQLite snapshot source: --global or --agent <id>.`);if(e.global!==true&&!r)throwExpectedCliError(`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}export{backupSqliteCreateCommand,backupSqliteListCommand,backupSqliteRestoreCommand,backupSqliteVerifyCommand};