openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
149 lines (148 loc) • 4.36 kB
JavaScript
import { i as resolveBackupNamespace, n as normalizeBackupRetention } from "./backup-retention-D-I6jsEu.mjs";
import { parseArgs } from "node:util";
//#region src/cron/backup-command.ts
const BACKUP_SCHEDULE_DECLARATION_KEYS = {
git: "openclaw-backup-scheduled",
offsite: "openclaw-backup-offsite-scheduled"
};
const BACKUP_COMMANDS = {
git: [
"openclaw",
"backup",
"git",
"create"
],
offsite: [
"openclaw",
"backup",
"create"
]
};
function backupScheduleModeForDeclaration(e) {
if (e === BACKUP_SCHEDULE_DECLARATION_KEYS.git) return "git";
if (e === BACKUP_SCHEDULE_DECLARATION_KEYS.offsite) return "offsite";
}
/** Identifies managed command execution even when an operator edits its arguments. */
function isScheduledBackupCommand(e) {
const t = backupScheduleModeForDeclaration(e.declarationKey);
const n = e.payload;
return t !== void 0 && n.kind === "command" && BACKUP_COMMANDS[t].every((e, t) => n.argv[t] === e);
}
function buildBackupScheduleJob(e) {
const t = [...BACKUP_COMMANDS[e.mode]];
if (e.mode === "git") {
t.push("--repository", e.repository);
t.push(...e.scope.kind === "agent" ? ["--agent", e.scope.agentId] : [`--${e.scope.kind}`]);
if (e.push) t.push("--push");
if (e.excludeSecrets) t.push("--exclude-secrets");
} else {
t.push("--to", e.location, "--namespace", e.namespace);
if (e.claimNamespace) t.push("--claim-namespace");
if (!e.includeWorkspace) t.push("--no-include-workspace");
for (const [n, r] of [
["--keep-daily", e.keepDaily],
["--keep-weekly", e.keepWeekly],
["--keep-monthly", e.keepMonthly]
]) if (r !== void 0) t.push(n, String(r));
}
const n = BACKUP_SCHEDULE_DECLARATION_KEYS[e.mode];
return {
declarationKey: n,
name: n,
enabled: true,
schedule: {
kind: "every",
everyMs: e.everyMs
},
sessionTarget: "isolated",
wakeMode: "now",
payload: {
kind: "command",
argv: t,
timeoutSeconds: 86400
},
delivery: { mode: "none" }
};
}
/** Decode the persisted argv contract for status without relying on display names. */
function parseBackupScheduleJob(e) {
const t = backupScheduleModeForDeclaration(e.declarationKey);
if (!t || !isScheduledBackupCommand(e) || e.payload.kind !== "command" || e.schedule.kind !== "every") return;
const n = e.schedule.everyMs;
try {
const r = e.payload.argv.slice(BACKUP_COMMANDS[t].length);
if (t === "git") {
const { values: e } = parseArgs({
args: r,
options: {
repository: { type: "string" },
all: { type: "boolean" },
global: { type: "boolean" },
agent: { type: "string" },
push: { type: "boolean" },
"exclude-secrets": { type: "boolean" }
}
});
if (!e.repository || [
e.all,
e.global,
e.agent
].filter(Boolean).length > 1) return;
return {
mode: t,
everyMs: n,
repository: e.repository,
scope: e.agent ? {
kind: "agent",
agentId: e.agent
} : { kind: e.global ? "global" : "all" },
push: e.push === true,
excludeSecrets: e["exclude-secrets"] === true
};
}
const { values: i } = parseArgs({
args: r,
options: {
to: { type: "string" },
namespace: { type: "string" },
"claim-namespace": { type: "boolean" },
"no-include-workspace": { type: "boolean" },
"keep-daily": { type: "string" },
"keep-weekly": { type: "string" },
"keep-monthly": { type: "string" }
}
});
if (!i.to) return;
return {
mode: t,
everyMs: n,
location: i.to,
namespace: resolveBackupNamespace(i.namespace),
claimNamespace: i["claim-namespace"] === true,
includeWorkspace: i["no-include-workspace"] !== true,
...normalizeBackupRetention({
keepDaily: i["keep-daily"],
keepWeekly: i["keep-weekly"],
keepMonthly: i["keep-monthly"]
})
};
} catch {
return;
}
}
function summarizeBackupSchedules(e) {
return e.flatMap((e) => {
const t = parseBackupScheduleJob(e);
return t ? [{
id: e.id,
mode: t.mode,
target: t.mode === "git" ? t.repository : t.location,
...t.mode === "offsite" ? { namespace: t.namespace } : {},
enabled: e.enabled,
everyMs: t.everyMs,
...e.state.nextRunAtMs === void 0 ? {} : { nextRunAtMs: e.state.nextRunAtMs }
}] : [];
});
}
//#endregion
export { summarizeBackupSchedules as i, buildBackupScheduleJob as n, isScheduledBackupCommand as r, backupScheduleModeForDeclaration as t };