openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
194 lines (193 loc) • 6.86 kB
JavaScript
import { c as normalizeOptionalLowercaseString } from "./string-coerce-CCIiB4H7.mjs";
import { a as asOptionalRecord } from "./record-coerce-CVPuDqcc.mjs";
import { u as isUnresolvedSecretInputError } from "./types.secrets-BmNLLnHp.mjs";
import { n as findUninspectedPluginDiagnostic } from "./discovery-availability-DtlJ7XvT.mjs";
import { t as applyPluginDoctorCompatibilitySequence } from "./doctor-compatibility-migration-Be-t21B8.mjs";
import { s as loadManifestMetadataSnapshot } from "./manifest-contract-eligibility-DoGGge-2.mjs";
import { i as getBundledChannelSetupPlugin, n as getBundledChannelPlugin } from "./bundled-BnH1gY8j.mjs";
import { n as getLoadedChannelPlugin } from "./registry-ByqVE5S3.mjs";
import { t as listDoctorConfiguredChannelIds } from "./configured-channel-ids-4FVS0H72.mjs";
import { r as resolveReadOnlyChannelPluginsForConfig } from "./read-only-idsFCBWl.mjs";
//#region src/commands/doctor/shared/channel-doctor.ts
function isChannelDoctorBlockedByConfig(e, t) {
if (t.plugins?.enabled === false) return true;
const n = normalizeOptionalLowercaseString(e) ?? e;
if (t.plugins?.entries?.[n]?.enabled === false) return true;
return asOptionalRecord(t.channels?.[n])?.enabled === false;
}
function safelyResolveChannelPlugin(e, t) {
try {
return t(e);
} catch {
return;
}
}
function listReadOnlyChannelDoctorsById(e) {
try {
return new Map(resolveReadOnlyChannelPluginsForConfig(e.cfg, {
...e.env ? { env: e.env } : {},
includePersistedAuthState: false,
includeSetupFallbackPlugins: true
}).plugins.map(({ id: e, doctor: t }) => [e, t]));
} catch {
return /* @__PURE__ */ new Map();
}
}
function mergeDoctorAdapters(e) {
const t = {};
for (const n of e) {
if (!n) continue;
for (const [e, r] of Object.entries(n)) if (r !== void 0 && !(e in t)) Object.assign(t, { [e]: r });
}
return Object.keys(t).length > 0 ? t : void 0;
}
function listChannelDoctorEntries(e, t = listDoctorConfiguredChannelIds(e.cfg, {
configEntryPolicy: "enabled",
skipWhenPluginsDisabled: true,
excludeExplicitlyDisabled: true,
sort: "codepoint"
}), n) {
const r = new Set(t.filter((t) => !isChannelDoctorBlockedByConfig(t, e.cfg)));
if (r.size === 0) return [];
const i = n ?? listReadOnlyChannelDoctorsById(e);
const a = [];
for (const e of r) {
const t = mergeDoctorAdapters([
i.get(e),
safelyResolveChannelPlugin(e, getLoadedChannelPlugin)?.doctor,
safelyResolveChannelPlugin(e, getBundledChannelSetupPlugin)?.doctor,
safelyResolveChannelPlugin(e, getBundledChannelPlugin)?.doctor
]);
if (!t) continue;
a.push({
id: e,
doctor: t
});
}
return a;
}
function preserveUnavailableChannelConfig(e) {
if (!e.cfg.plugins?.load?.paths?.length) return;
const t = findUninspectedPluginDiagnostic(loadManifestMetadataSnapshot({
config: e.cfg,
env: e.env
}).diagnostics);
return t ? {
config: e.cfg,
changes: [],
warnings: [t.message]
} : void 0;
}
async function collectChannelDoctorMutations(e, t, n) {
const r = preserveUnavailableChannelConfig(e);
if (r) return [r];
const i = [];
let a = e.cfg;
for (const { doctor: r } of listChannelDoctorEntries(e, n)) {
const e = await t(r, a);
if (e?.changes.length) {
i.push(e);
a = e.config;
} else if (e?.warnings?.length) i.push({
config: a,
changes: [],
warnings: e.warnings
});
}
return i;
}
/** Build cached empty-allowlist hooks backed by channel doctor adapters. */
function createChannelDoctorEmptyAllowlistPolicyHooks(e) {
const t = listReadOnlyChannelDoctorsById(e);
const n = /* @__PURE__ */ new Map();
const entriesForChannel = (r) => {
const i = n.get(r);
if (i) return i;
const a = listChannelDoctorEntries(e, [r], t);
n.set(r, a);
return a;
};
return {
extraWarningsForAccount: (e) => entriesForChannel(e.channelName).flatMap((t) => t.doctor.collectEmptyAllowlistExtraWarnings?.(e) ?? []),
shouldSkipDefaultEmptyGroupAllowlistWarning: (e) => entriesForChannel(e.channelName).some((t) => t.doctor.shouldSkipDefaultEmptyGroupAllowlistWarning?.(e) === true)
};
}
/** Run interactive/non-interactive channel setup repair sequences and collect notes. */
async function runChannelDoctorConfigSequences(e) {
const t = preserveUnavailableChannelConfig(e);
if (t) return {
changeNotes: [],
warningNotes: t.warnings ?? []
};
const n = [];
const r = [];
const i = [];
for (const t of listChannelDoctorEntries(e)) {
const a = await t.doctor.runConfigSequence?.(e);
if (!a) continue;
n.push(...a.changeNotes);
r.push(...a.infoNotes ?? []);
i.push(...a.warningNotes);
}
return {
changeNotes: n,
warningNotes: i,
...r.length > 0 ? { infoNotes: r } : {}
};
}
/** Collect compatibility migrations from configured channel doctor adapters in order. */
function collectChannelDoctorCompatibilityMutations(e, t = {}) {
const n = preserveUnavailableChannelConfig({
cfg: e,
env: t.env
});
if (n) return [n];
const r = applyPluginDoctorCompatibilitySequence(e, listChannelDoctorEntries({
cfg: e,
env: t.env
}).map(({ id: e, doctor: t }) => ({
pluginId: e,
normalizeCompatibilityConfig: t.normalizeCompatibilityConfig?.bind(t)
})));
return r.changes.length || r.warnings?.length ? [r] : [];
}
async function collectChannelDoctorStaleConfigMutations(e, t = {}) {
return collectChannelDoctorMutations({
cfg: e,
env: t.env
}, (e, t) => e.cleanStaleConfig?.({ cfg: t }), t.channelIds);
}
async function collectChannelDoctorPreviewWarnings(e) {
const t = [];
for (const n of listChannelDoctorEntries(e)) {
let r;
try {
r = await n.doctor.collectPreviewWarnings?.(e);
} catch (e) {
if (!isUnresolvedSecretInputError(e)) throw e;
t.push(`- channels.${n.id}: configured SecretRef at ${e.path} is unavailable in doctor preview; skipping secret-backed channel preview checks.`);
continue;
}
if (r?.length) t.push(...r);
}
return t;
}
/** Collect warnings for mutable channel allowlists that doctor cannot safely edit. */
async function collectChannelDoctorMutableAllowlistWarnings(e) {
const t = [];
for (const n of listChannelDoctorEntries(e)) {
const r = await n.doctor.collectMutableAllowlistWarnings?.(e);
if (r?.length) t.push(...r);
}
return t;
}
/** Collect channel repair mutations and warning-only repair results from doctor adapters. */
async function collectChannelDoctorRepairMutations(e) {
return collectChannelDoctorMutations(e, (t, n) => t.repairConfig?.({
cfg: n,
doctorFixCommand: e.doctorFixCommand,
...e.env ? { env: e.env } : {}
}));
}
//#endregion
export { collectChannelDoctorStaleConfigMutations as a, collectChannelDoctorRepairMutations as i, collectChannelDoctorMutableAllowlistWarnings as n, createChannelDoctorEmptyAllowlistPolicyHooks as o, collectChannelDoctorPreviewWarnings as r, runChannelDoctorConfigSequences as s, collectChannelDoctorCompatibilityMutations as t };