UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

110 lines (109 loc) • 5.91 kB
import { r as terminalText } from "./safe-text-C80HAQ2e.mjs"; import { p as shortenHomePath } from "./utils-C8YECn2Q.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 { t as parseDurationMs } from "./parse-duration-D6UJm52I.mjs"; import { t as captureRuntimeConfigAsyncReader } from "./io.runtime-DsJ2dLCQ.mjs"; import "./config-CMA-J084.mjs"; import { a as executeGitCommand } from "./git-exec-DHBL622T.mjs"; import { a as isImplicitLocalGatewayTargetFromCli, n as callGatewayFromCli } from "./gateway-rpc-CxUN2f1a.mjs"; import { u as resolveRequiredBackupPath } from "./backup-shared-BmR-itzA.mjs"; import { r as listCronJobsFromGateway } from "./list-jobs-B1tD7qNd.mjs"; import { i as resolveBackupNamespace, n as normalizeBackupRetention } from "./backup-retention-D-I6jsEu.mjs"; import { t as GIT_BACKUP_PUSH_CREDENTIAL_WARNING } from "./backup-git-DdgJ_qwo.mjs"; import { n as buildBackupScheduleJob, t as backupScheduleModeForDeclaration } from "./backup-command-BiUB26T8.mjs"; //#region src/commands/backup-schedule.ts const LOCAL_GATEWAY_REQUIRED_ERROR = "backup enable manages backups on the Gateway host and currently requires a local Gateway. Create the cron job manually with openclaw cron add for remote Gateways."; /** * Unattended pushed schedules make credential retention durable in remote * history, so they redact by default; --include-secrets is the explicit * full-fidelity override. Local (non-push) schedules keep full fidelity for * complete restores. */ function resolveScheduledRedaction(e) { if (e.excludeSecrets && e.includeSecrets) throw new Error("Use either --exclude-secrets or --include-secrets, not both."); if (!e.push) return e.excludeSecrets === true; return e.includeSecrets !== true; } async function resolveScheduleSpec(e, t) { if (e.to !== void 0) { if (e.repository !== void 0 || e.push || e.excludeSecrets || e.includeSecrets || e.globalOnly || e.agent !== void 0) throw new Error("--to cannot be combined with Git backup options (--repository, --push, --exclude-secrets, --include-secrets, --global-only, --agent)."); const n = e.to.trim(); if (!n) throw new Error("--to must name a configured storage location."); if (!(await captureRuntimeConfigAsyncReader({ skipPluginValidation: true })()).storage?.locations?.[n]) throw new Error(`Storage location "${n}" is not configured. Run openclaw storage list.`); return { mode: "offsite", everyMs: t, location: n, namespace: resolveBackupNamespace(e.namespace), claimNamespace: e.claimNamespace === true, includeWorkspace: e.includeWorkspace !== false, ...normalizeBackupRetention(e) }; } if (e.namespace !== void 0 || e.claimNamespace || e.includeWorkspace === false || e.keepDaily !== void 0 || e.keepWeekly !== void 0 || e.keepMonthly !== void 0) throw new Error("--namespace, --claim-namespace, --no-include-workspace, and --keep-* require --to <location>."); const n = resolveRequiredBackupPath(e.repository, "--repository"); const r = e.agent?.trim(); if (e.agent !== void 0 && !r) throw new Error("--agent must not be blank"); if (e.globalOnly && r) throw new Error("Use either --global-only or --agent <id>, not both."); const i = r ? resolveConfiguredAgentId(await captureRuntimeConfigAsyncReader({ skipPluginValidation: true })(), normalizeAgentId(r)) : void 0; return { mode: "git", everyMs: t, repository: n, scope: e.globalOnly ? { kind: "global" } : i ? { kind: "agent", agentId: i } : { kind: "all" }, push: e.push === true, excludeSecrets: resolveScheduledRedaction(e) }; } async function assertLocalGatewayScheduleTarget(e) { if (!await isImplicitLocalGatewayTargetFromCli(e)) throw new Error(LOCAL_GATEWAY_REQUIRED_ERROR); } async function backupEnableCommand(e, t) { await assertLocalGatewayScheduleTarget(t); const n = t.every?.trim() ?? "24h"; const r = parseDurationMs(n, { defaultUnit: "ms" }); if (!Number.isSafeInteger(r) || r <= 0) throw new Error("--every must be a positive duration such as 6h or 24h."); const i = await resolveScheduleSpec(t, r); if (i.mode === "git" && i.push) { if ((await executeGitCommand(i.repository, [ "remote", "get-url", "origin" ])).code !== 0) throw new Error(terminalText`--push requires an origin remote. Run: openclaw backup git init --repository ${shortenHomePath(i.repository)} --remote <url>`); if (!i.excludeSecrets) e.error(GIT_BACKUP_PUSH_CREDENTIAL_WARNING); } const a = await callGatewayFromCli("cron.add", t, buildBackupScheduleJob(i)); const o = a.job?.id; if (!o) throw new Error("cron.add returned no scheduled backup job id."); const s = a.created === false; e.log(terminalText`Scheduled ${i.mode === "git" ? "Git" : "offsite"} backups ${s ? "updated" : "enabled"}: every ${n} to ${i.mode === "git" ? shortenHomePath(i.repository) : i.location}`); return { id: o, updated: s }; } async function backupDisableCommand(e, t) { await assertLocalGatewayScheduleTarget(t); if (t.git && t.offsite) throw new Error("Use either --git or --offsite, or omit both to disable all backup schedules."); const { jobs: n } = await listCronJobsFromGateway(t, { includeDisabled: true }); const r = t.git ? "git" : t.offsite ? "offsite" : void 0; const i = n.filter((e) => { const t = backupScheduleModeForDeclaration(e.declarationKey); return t !== void 0 && (r === void 0 || t === r); }); const a = r === "git" ? "Scheduled Git backups" : r === "offsite" ? "Scheduled offsite backups" : "Scheduled backups"; if (i.length === 0) { e.log(`${a} are already disabled.`); return { removed: false }; } for (const e of i) await callGatewayFromCli("cron.remove", t, { id: e.id }); e.log(`${a} disabled.`); return { removed: true }; } //#endregion export { backupDisableCommand, backupEnableCommand };