openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
79 lines (78 loc) • 3.6 kB
JavaScript
import { l as isRecord, r as asNullableRecord } from "./record-coerce-CVPuDqcc.mjs";
import { t as formatCliCommand } from "./command-format-CqdwUgPk.mjs";
import { n as normalizeAgentId, t as isValidAgentId } from "./agent-id-CQjtp2ft.mjs";
import { i as listAgentIds } from "./agent-roster-t1ooUdXp.mjs";
import { H as isSameAuthoredSessionStoreConfig } from "./agent-scope-config-DmXgFGKP.mjs";
import "./backup-rotation-DIugSdzV.mjs";
import { r as sanitizeDoctorNote } from "./emit-notes-llcc2fq-.mjs";
import { n as containsAuthoredInclude, r as readDoctorConfigBackup } from "./include-migration-ownership-CWJZ0wk-.mjs";
import JSON5 from "json5";
//#region src/commands/doctor-session-store-owner.ts
const OWNER_KEY = "agents.defaults.sessionStore.agentId";
/** Offers historical ownership without guessing whether its removal was intentional. */
async function prepareSessionStoreOwnerRecovery(e) {
const t = {
config: e.config,
changes: [],
warnings: []
};
if (!e.snapshot.valid || e.config.agents?.defaults?.sessionStore?.agentId !== void 0 || containsAuthoredInclude(e.snapshot.parsed)) return t;
const n = asNullableRecord(asNullableRecord(e.snapshot.parsed)?.session)?.store;
if (n !== void 0 && typeof n !== "string") return t;
const r = new Set(listAgentIds(e.config).map(normalizeAgentId));
for (let i = 0; i < 5; i++) {
const a = `${e.snapshot.path}.bak${i === 0 ? "" : `.${i}`}`;
let o;
let s;
try {
o = readDoctorConfigBackup(a);
s = o === void 0 ? void 0 : JSON5.parse(o);
} catch {
t.warnings.push(`Could not inspect ${a} for ${OWNER_KEY} recovery. Review that backup or re-author ${OWNER_KEY}; Doctor did not choose an owner.`);
break;
}
if (o === void 0) continue;
if (!isRecord(s) || containsAuthoredInclude(s)) break;
if (s.session !== void 0 && !isRecord(s.session)) break;
const c = asNullableRecord(s.session)?.store;
if (c !== void 0 && typeof c !== "string" || !isSameAuthoredSessionStoreConfig(c, n)) break;
const l = asNullableRecord(asNullableRecord(asNullableRecord(s.agents)?.defaults)?.sessionStore)?.agentId;
if (l === void 0) continue;
if (typeof l !== "string" || !isValidAgentId(l) || !r.has(normalizeAgentId(l))) {
t.warnings.push(`${OWNER_KEY} is missing, but its last backed-up value in ${a} does not name a configured agent. Re-author ${OWNER_KEY} with the intended owner; Doctor did not choose one.`);
break;
}
const u = formatCliCommand(`openclaw config set ${OWNER_KEY} ${l.trim()}`);
if (!await e.prompter?.confirmRuntimeRepair({
message: sanitizeDoctorNote(`Restore ${OWNER_KEY}=${JSON.stringify(l)} from ${a}? Its removal may have been intentional.`),
initialValue: false,
requiresInteractiveConfirmation: true
})) {
t.warnings.push(`${OWNER_KEY} is missing; ${a} retains ${JSON.stringify(l)} for the unchanged session.store. The removal may have been intentional, so Doctor left it unchanged. To restore this value, run "${u}".`);
break;
}
let d;
try {
d = readDoctorConfigBackup(a);
} catch {}
if (d !== o) {
t.warnings.push(`${a} changed or became unreadable while confirming ${OWNER_KEY}; nothing was restored. Rerun Doctor to inspect the current backup.`);
break;
}
const f = structuredClone(e.config);
f.agents ??= {};
f.agents.defaults ??= {};
f.agents.defaults.sessionStore = {
...f.agents.defaults.sessionStore,
agentId: l
};
return {
config: f,
changes: [`Restored ${OWNER_KEY}=${JSON.stringify(l)} from ${a}.`],
warnings: []
};
}
return t;
}
//#endregion
export { prepareSessionStoreOwnerRecovery };