UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 3.15 kB
import{l as isRecord,r as asNullableRecord}from"./record-coerce-BTbqdJ3-.mjs";import{n as normalizeAgentId,t as isValidAgentId}from"./agent-id-cOfqmfln.mjs";import{t as formatCliCommand}from"./command-format-D7KhNLP0.mjs";import{i as listAgentIds}from"./agent-roster-4XrgUgPK.mjs";import{n as isSameAuthoredSessionStoreConfig}from"./session-store-config-Ck41Fpqm.mjs";import"./agent-scope-config-CG9CFpGX.mjs";import"./backup-rotation-BmNg1h15.mjs";import{r as sanitizeDoctorNote}from"./emit-notes-DCY5wFZF.mjs";import{n as containsAuthoredInclude,r as readDoctorConfigBackup}from"./include-migration-ownership-KASn2yVx.mjs";import JSON5 from"json5"; //!src/commands/doctor-session-store-owner.ts const OWNER_KEY=`agents.defaults.sessionStore.agentId`;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}export{prepareSessionStoreOwnerRecovery};