openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
128 lines (127 loc) • 5.35 kB
JavaScript
import { a as asOptionalRecord } from "./record-coerce-CVPuDqcc.mjs";
import { r as defaultRuntime } from "./runtime-B-SLYP-m.mjs";
import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs";
import { t as createDedupeCache } from "./dedupe-DxT0h2gR.mjs";
import { n as normalizeMessageChannel } from "./message-channel-core-BbYZT1a-.mjs";
import { t as isDeliverableMessageChannel } from "./message-channel-normalize-CFHSufeo.mjs";
import "./message-channel-DTYVLJT1.mjs";
import { n as getRuntimeVisibleChannelPlugin, r as listRuntimeVisibleChannelPlugins } from "./runtime-visible-channels-CE896D4p.mjs";
import { n as resolveMissingOfficialExternalChannelPluginRepairHint, r as resolveMissingOfficialExternalChannelPluginRepairHints } from "./official-external-plugin-repair-hints-CLGaUsqQ.mjs";
import { r as resolveChannelAccount } from "./account-resolution-D7u1J1M0.mjs";
import { a as formatUnknownChannelMessage } from "./error-format-O7EzXfiI.mjs";
import { r as resolveOutboundChannelPlugin } from "./channel-resolution-DjtSMYUG.mjs";
import { t as isAccountEnabled } from "./account-enabled-CulIMLY0.mjs";
//#region src/infra/outbound/channel-selection.ts
function isConfiguredChannel(e, t) {
const n = asOptionalRecord(asOptionalRecord(e.channels)?.[t]);
return n !== void 0 && n.enabled !== false;
}
function listConfiguredOfficialExternalRepairHints(e) {
const t = asOptionalRecord(e.channels);
if (!t) return [];
return resolveMissingOfficialExternalChannelPluginRepairHints({
config: e,
channelIds: Object.keys(t).filter((t) => isConfiguredChannel(e, t))
});
}
function formatMissingOfficialExternalChannelsMessage(e) {
const [t] = e;
if (e.length === 1 && t) return `Configured official external channel ${t.label} is missing its plugin. ${t.repairHint}`;
return `Configured official external channels ${e.map((e) => e.label).join(", ")} are missing their plugins. Run: openclaw doctor --fix, or install individually: ${e.map((e) => e.installCommand).join("; ")}.`;
}
const loggedChannelSelectionErrors = createDedupeCache({
ttlMs: 0,
maxSize: 1024
});
function logChannelSelectionError(e, t, n, r) {
const i = formatErrorMessage(r);
const a = `${e}:${t}:${n}:${i}`;
if (loggedChannelSelectionErrors.check(a)) return;
defaultRuntime.error?.(`[channel-selection] ${e}(${t}) ${n} failed: ${i}`);
}
async function isPluginConfigured(e, t, n) {
const r = e.config.listAccountIds(t);
for (const i of r) {
let r = "inspectAccount";
let a;
try {
if (n === "read_only") {
const n = asOptionalRecord(await e.config.inspectAccount?.(t, i));
if (n) {
if (isAccountEnabled(n) && n.configured === true) return true;
continue;
}
}
r = "resolveAccount";
a = await resolveChannelAccount({
plugin: e,
cfg: t,
accountId: i
});
} catch (t) {
logChannelSelectionError(e.id, i, r, t);
continue;
}
if (!(e.config.isEnabled ? e.config.isEnabled(a, t) : isAccountEnabled(a))) continue;
try {
if (await e.config.isConfigured?.(a, t) ?? true) return true;
} catch (t) {
logChannelSelectionError(e.id, i, "isConfigured", t);
}
}
return false;
}
async function listConfiguredMessageChannelPlugins(e, t = "strict") {
const n = [];
for (const r of listRuntimeVisibleChannelPlugins()) if (resolveOutboundChannelPlugin({
channel: r.id,
cfg: e
}) && await isPluginConfigured(r, e, t)) n.push(r);
return n;
}
async function listConfiguredMessageChannels(e) {
return (await listConfiguredMessageChannelPlugins(e)).map((e) => e.id);
}
async function resolveMessageChannelSelection(e) {
const t = normalizeMessageChannel(e.channel);
for (const n of ["channel", "fallbackChannel"]) {
const r = e.cfg;
const i = n === "channel" ? t : normalizeMessageChannel(e[n]);
const a = e.agentId;
if (!i) continue;
const o = resolveOutboundChannelPlugin({
channel: i,
cfg: r,
agentId: a,
allowBootstrap: true
});
if (o) return {
channel: o.id,
plugin: o
};
}
if (t) {
if (!isDeliverableMessageChannel(t) && !getRuntimeVisibleChannelPlugin(t)) throw new Error(formatUnknownChannelMessage({ channel: t }));
const n = isConfiguredChannel(e.cfg, t) ? resolveMissingOfficialExternalChannelPluginRepairHint({
config: e.cfg,
channelId: t
}) : null;
if (n?.channelId === t) throw new Error(`Channel is unavailable: ${t}. ${n.repairHint}`);
throw new Error(`Channel is unavailable: ${t}`);
}
const n = await listConfiguredMessageChannelPlugins(e.cfg, e.accountResolution);
const r = n.map((e) => e.id);
const [i] = n;
if (n.length === 1 && i) return {
channel: i.id,
plugin: i
};
if (r.length === 0) {
const t = listConfiguredOfficialExternalRepairHints(e.cfg);
if (t.length > 0) throw new Error(`Channel is required (no available channels detected). ${formatMissingOfficialExternalChannelsMessage(t)}`);
throw new Error("Channel is required (no configured channels detected). Run openclaw channels add to configure one, or pass --channel <channel> after enabling a channel. Use openclaw channels list --all to see available channel ids.");
}
throw new Error(`Channel is required when multiple channels are configured: ${r.join(", ")}. Pass --channel <channel> to choose one.`);
}
//#endregion
export { listConfiguredMessageChannels as n, resolveMessageChannelSelection as r, isConfiguredChannel as t };