openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
70 lines (69 loc) • 3.19 kB
JavaScript
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { i as normalizeChatChannelId } from "./ids-BRSWrLzE.mjs";
import "./backup-rotation-DIugSdzV.mjs";
import { n as containsAuthoredInclude, r as readDoctorConfigBackup } from "./include-migration-ownership-CWJZ0wk-.mjs";
import { t as createCommandOwnerChannelResolver } from "./doctor-command-owner-Dul2IrrD.mjs";
import { i as resolveLegacyHeartbeatAgents, s as resolveLegacyHeartbeatIntervalMs } from "./doctor-heartbeat-legacy-DmzlheVU.mjs";
import JSON5 from "json5";
//#region src/commands/doctor-command-owner-recovery.ts
function withoutLegacyOwnerKind(e) {
const t = typeof e === "string" ? /^([^:]+):user:([^:\s*]+)$/i.exec(e.trim()) : null;
const n = t && normalizeChatChannelId(t[1]);
return n && t ? `${n}:${t[2]}` : e;
}
/** Recover only a recorded direct-user kind for an owner whose identity still matches. */
function recoverCommandOwnerTargetKinds(e) {
const { config: t, snapshot: n } = e;
const r = {
config: t,
changes: [],
warnings: []
};
const i = t.commands?.ownerAllowFrom;
if (!i?.length || !resolveLegacyHeartbeatAgents(t).some(({ heartbeat: e }) => (e?.target === void 0 || e.target === "owner") && resolveLegacyHeartbeatIntervalMs(t, void 0, e))) return r;
const a = /* @__PURE__ */ new Map();
const o = createCommandOwnerChannelResolver(t);
for (const [e, t] of i.entries()) {
const n = typeof t === "string" ? /^([^:]+):([^:\s*]+)$/.exec(t.trim()) : null;
const r = n && normalizeChatChannelId(n[1]);
const i = n?.[2];
if (!r || !i) continue;
const s = o(r)?.messaging;
if (s?.directTargetStyle === "user-prefixed" && s.inferTargetChatType?.({ to: i }) !== "direct" && s.inferTargetChatType?.({ to: `user:${i}` }) === "direct") a.set(e, `${r}:user:${i}`);
}
if (a.size === 0) return r;
const s = [...i];
if (!containsAuthoredInclude(n.parsed)) for (let e = 0; e < 5; e++) {
const o = `${n.path}.bak${e === 0 ? "" : `.${e}`}`;
let c;
try {
const e = readDoctorConfigBackup(o);
if (e === void 0) continue;
c = JSON5.parse(e);
} catch {
r.warnings.push(`Could not inspect ${o} for command-owner target recovery.`);
break;
}
const l = isRecord(c) && isRecord(c.commands) ? c.commands.ownerAllowFrom : void 0;
if (containsAuthoredInclude(c) || !Array.isArray(l) || l.length !== i.length || l.some((e, t) => e !== i[t] && withoutLegacyOwnerKind(e) !== i[t])) break;
for (const [e, t] of a) if (l[e] !== i[e]) {
s[e] = t;
a.delete(e);
r.changes.push(`Restored commands.ownerAllowFrom[${e}] target kind from ${o}.`);
}
if (r.changes.length) {
r.config = {
...t,
commands: {
...t.commands,
ownerAllowFrom: s
}
};
if (a.size === 0) break;
}
}
for (const [e, t] of a) r.warnings.push(`commands.ownerAllowFrom[${e}]=${JSON.stringify(i[e])} cannot resolve a direct heartbeat owner route; heartbeats can skip with reason="no-route". No matching config backup proves the user target kind. After confirming this is your user ID, set commands.ownerAllowFrom[${e}] to ${JSON.stringify(t)}. Doctor left this owner unchanged.`);
return r;
}
//#endregion
export { recoverCommandOwnerTargetKinds };