openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
292 lines (291 loc) • 7.93 kB
JavaScript
import { r as asNullableRecord } from "./record-coerce-CVPuDqcc.mjs";
import { t as mergeDeep } from "./deep-merge-Wuxftp-p.mjs";
import { i as resolveChannelAccountKey } from "./account-lookup-FhQZVFfo.mjs";
import { isDeepStrictEqual } from "node:util";
//#region src/config/channel-config-normalization.ts
/** Applies one channel-specific doctor migration to every object-shaped account. */
function normalizeChannelAccounts(e) {
const t = asNullableRecord(e.entry.accounts);
if (!t) return {
entry: e.entry,
changed: false
};
let n = false;
const r = { ...t };
for (const [i, a] of Object.entries(t)) {
const t = asNullableRecord(a);
if (!t) continue;
const o = e.normalizeAccount({
account: t,
accountId: i,
pathPrefix: `${e.pathPrefix}.accounts.${i}`,
changes: e.changes
});
if (o.changed) {
r[i] = o.entry;
n = true;
}
}
return n ? {
entry: {
...e.entry,
accounts: r
},
changed: true
} : {
entry: e.entry,
changed: false
};
}
/** Applies the same channel-specific doctor migration at root and account scope. */
function normalizeChannelConfigEntries(e) {
const t = e.changes ?? [];
const n = e.cfg.channels;
const r = asNullableRecord(n?.[e.channelId]);
if (!r) return {
config: e.cfg,
changes: t
};
const i = `channels.${e.channelId}`;
const a = e.normalizeEntry({
entry: r,
pathPrefix: i,
changes: t
});
const o = normalizeChannelAccounts({
entry: a.entry,
pathPrefix: i,
changes: t,
normalizeAccount: (t) => e.normalizeEntry({
entry: t.account,
accountId: t.accountId,
pathPrefix: t.pathPrefix,
changes: t.changes
})
});
if (!a.changed && !o.changed) return {
config: e.cfg,
changes: t
};
return {
config: {
...e.cfg,
channels: {
...n,
[e.channelId]: o.entry
}
},
changes: t
};
}
//#endregion
//#region src/config/channel-doctor-helpers.ts
function stripRetiredKeys(e) {
if (e.recursive && Array.isArray(e.value)) {
let t = false;
const n = e.value.map((n, r) => {
const i = stripRetiredKeys({
...e,
value: n,
pathPrefix: `${e.pathPrefix}[${r}]`
});
t = t || i.changed;
return i.value;
});
return {
value: t ? n : e.value,
changed: t
};
}
const t = asNullableRecord(e.value);
if (!t) return {
value: e.value,
changed: false
};
let n = false;
const r = {};
for (const [i, a] of Object.entries(t)) {
if (e.keys.has(i)) {
e.onRemove?.({
key: i,
pathPrefix: e.pathPrefix
});
n = true;
continue;
}
if (!e.recursive) {
r[i] = a;
continue;
}
const t = stripRetiredKeys({
...e,
value: a,
pathPrefix: `${e.pathPrefix}.${i}`
});
n = n || t.changed;
r[i] = t.value;
}
return {
value: n ? r : e.value,
changed: n
};
}
/** Removes retired keys recursively or from a channel root and its accounts. */
function stripRetiredChannelKeys(e) {
const t = e.cfg.channels;
const n = asNullableRecord(t?.[e.channelId]);
if (!n) return {
config: e.cfg,
changed: false
};
const r = `channels.${e.channelId}`;
if (e.scope === "recursive") {
const i = stripRetiredKeys({
value: n,
keys: e.keys,
pathPrefix: r,
recursive: true,
onRemove: e.onRemove
});
return i.changed ? {
config: {
...e.cfg,
channels: {
...t,
[e.channelId]: i.value
}
},
changed: true
} : {
config: e.cfg,
changed: false
};
}
const i = normalizeChannelConfigEntries({
cfg: e.cfg,
channelId: e.channelId,
normalizeEntry: (t) => {
const n = stripRetiredKeys({
value: t.entry,
keys: e.keys,
pathPrefix: t.pathPrefix,
recursive: false,
onRemove: e.onRemove
});
return {
entry: n.value,
changed: n.changed
};
}
});
return {
config: i.config,
changed: i.config !== e.cfg
};
}
/** Materializes root/default-account inheritance after aliases create streaming. */
function materializeInheritedAccountStreaming(e) {
const t = e.cfg.channels;
const n = asNullableRecord(t?.[e.channelId]);
const r = asNullableRecord(n?.accounts);
if (!n || !r) return e.cfg;
const i = asNullableRecord(n.streaming);
const a = resolveChannelAccountKey(r, "default", e.channelId, (e) => e.trim().toLowerCase());
let o = false;
const s = { ...r };
const c = Object.keys(r).toSorted((e, t) => e === a ? -1 : t === a ? 1 : e.localeCompare(t));
for (const t of c) {
if (asNullableRecord(e.accountsBefore?.[t])?.streaming !== void 0) continue;
const n = asNullableRecord(s[t]);
const r = asNullableRecord(n?.streaming);
if (!n || !r) continue;
const c = a ? asNullableRecord(asNullableRecord(s[a])?.streaming) : null;
const l = t === a ? i : c ?? i;
if (!l) continue;
const u = asNullableRecord(mergeDeep(l, r));
if (!u || isDeepStrictEqual(u, r)) continue;
s[t] = {
...n,
streaming: u
};
o = true;
const d = t !== a && a && c ? `channels.${e.channelId}.accounts.${a}.streaming` : `channels.${e.channelId}.streaming`;
e.changes.push(`Copied ${d} into channels.${e.channelId}.accounts.${t}.streaming to keep inherited settings while migrating flat streaming keys.`);
}
return o ? {
...e.cfg,
channels: {
...t,
[e.channelId]: {
...n,
accounts: s
}
}
} : e.cfg;
}
//#endregion
//#region src/config/legacy-private-network-migration.ts
/** Detects the retired flat `allowPrivateNetwork` key before doctor migration. */
function hasLegacyFlatAllowPrivateNetworkAlias(e) {
const t = asNullableRecord(e);
return Boolean(t && Object.hasOwn(t, "allowPrivateNetwork"));
}
/** Moves flat private-network config into `network.dangerouslyAllowPrivateNetwork`. */
function migrateLegacyFlatAllowPrivateNetworkAlias(e) {
if (!hasLegacyFlatAllowPrivateNetworkAlias(e.entry)) return {
entry: e.entry,
changed: false
};
const t = e.entry.allowPrivateNetwork;
const n = asNullableRecord(e.entry.network);
const r = n ? { ...n } : {};
const i = r.dangerouslyAllowPrivateNetwork;
const a = typeof i !== "boolean" && (typeof t === "boolean" || i === void 0) ? t : i;
delete r.dangerouslyAllowPrivateNetwork;
if (a !== void 0) r.dangerouslyAllowPrivateNetwork = a;
const o = { ...e.entry };
delete o.allowPrivateNetwork;
if (Object.keys(r).length > 0) o.network = r;
else delete o.network;
e.changes.push(`Moved ${e.pathPrefix}.allowPrivateNetwork → ${e.pathPrefix}.network.dangerouslyAllowPrivateNetwork (${String(a)}).`);
return {
entry: o,
changed: true
};
}
function hasLegacyAllowPrivateNetworkInAccounts(e) {
const t = asNullableRecord(e);
return Boolean(t && Object.values(t).some(hasLegacyFlatAllowPrivateNetworkAlias));
}
/** Build doctor rules that migrate legacy private-network aliases for one channel config. */
function createLegacyPrivateNetworkDoctorContract(e) {
const t = `channels.${e.channelKey}`;
return {
legacyConfigRules: [{
path: ["channels", e.channelKey],
message: `${t}.allowPrivateNetwork is legacy; use ${t}.network.dangerouslyAllowPrivateNetwork instead. Run "openclaw doctor --fix".`,
match: hasLegacyFlatAllowPrivateNetworkAlias
}, {
path: [
"channels",
e.channelKey,
"accounts"
],
message: `${t}.accounts.<id>.allowPrivateNetwork is legacy; use ${t}.accounts.<id>.network.dangerouslyAllowPrivateNetwork instead. Run "openclaw doctor --fix".`,
match: hasLegacyAllowPrivateNetworkInAccounts
}],
normalizeCompatibilityConfig: ({ cfg: t }) => {
if (!asNullableRecord(t.channels)) return {
config: t,
changes: []
};
return normalizeChannelConfigEntries({
cfg: t,
channelId: e.channelKey,
normalizeEntry: migrateLegacyFlatAllowPrivateNetworkAlias
});
}
};
}
//#endregion
export { stripRetiredChannelKeys as a, materializeInheritedAccountStreaming as i, hasLegacyFlatAllowPrivateNetworkAlias as n, normalizeChannelAccounts as o, migrateLegacyFlatAllowPrivateNetworkAlias as r, normalizeChannelConfigEntries as s, createLegacyPrivateNetworkDoctorContract as t };