UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

129 lines (128 loc) • 4.78 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { r as normalizeOptionalAccountId } from "./account-id-BqKT3GuY.mjs"; import { r as assertSecretOwnerAvailable } from "./runtime-degraded-state-BSnwk5PQ.mjs"; import { t as getChannelPlugin } from "./registry-ByqVE5S3.mjs"; import "./plugins-BoMiRtiY.mjs"; import { r as listRuntimeVisibleChannelPlugins } from "./runtime-visible-channels-CE896D4p.mjs"; import { r as resolveChannelAccount, t as channelHasConfiguredState } from "./account-resolution-D7u1J1M0.mjs"; import { t as MessageActionDeniedError } from "./message-action-denial-Csh3qR94.mjs"; import { i as resolveChannelDefaultAccountId } from "./helpers-ELZNDAnv.mjs"; import { t as isChannelAccountExplicitlyDisabled } from "./account-config-enabled-Cg2pOk9t.mjs"; import { s as resolveChannelAccountEnabled } from "./account-summary-RVgW5Dlx.mjs"; import { r as resolveOutboundChannelPlugin } from "./channel-resolution-DjtSMYUG.mjs"; import { t as isAccountEnabled } from "./account-enabled-CulIMLY0.mjs"; import { t as isConfiguredChannel } from "./channel-selection-DBeuWpKq.mjs"; //#region src/infra/outbound/message-account-selection.ts /** * Binds a caller-supplied message account to one listed channel account. * Host-derived defaults and binding accounts bypass this helper by design. */ async function validateExplicitMessageAccountSelection(e) { const t = normalizeOptionalString(e.accountId); if (!t) return; const n = normalizeOptionalAccountId(t); if (!n) throw new MessageActionDeniedError(`Invalid account ID "${t}".`, "message_account_invalid", "message-account:valid"); const r = normalizeOptionalString(e.channel); if (!r) return n; const i = e.plugin ?? resolveOutboundChannelPlugin({ channel: r, cfg: e.cfg }) ?? getChannelPlugin(r); if (!i) return n; let a = i.config.listAccountIds(e.cfg).find((e) => normalizeOptionalAccountId(e) === n); if (!a) { const t = resolveChannelDefaultAccountId({ plugin: i, cfg: e.cfg }); if (normalizeOptionalAccountId(t) === n) a = t; } if (!a) throw new MessageActionDeniedError(`Unknown account "${t}" for channel ${r}.`, "message_account_unknown", "message-account:known"); const assertAccountEnabled = (t) => { if (isChannelAccountExplicitlyDisabled({ cfg: e.cfg, channel: i.id, accountId: a }) || t && !t()) throw new MessageActionDeniedError(`Account "${a}" for channel ${r} is disabled.`, "message_account_disabled", "message-account:enabled"); }; assertAccountEnabled(); if (e.checkResolvedAccount !== false) { assertSecretOwnerAvailable("account", `${i.id}:${n}`); const t = await resolveChannelAccount({ plugin: i, cfg: e.cfg, accountId: n }); assertSecretOwnerAvailable("account", `${i.id}:${n}`); assertAccountEnabled(() => resolveChannelAccountEnabled({ plugin: i, account: t, cfg: e.cfg })); } return n; } /** Checks configured and enabled state after channel availability is resolved. */ async function isPotentialConfiguredMessageChannel(e) { const t = e.cfg.channels?.[e.plugin.id]; if (!Array.isArray(t) && !isAccountEnabled(t)) return false; if (isConfiguredChannel(e.cfg, e.plugin.id)) return true; try { return await channelHasConfiguredState({ plugin: e.plugin, cfg: e.cfg, env: process.env }) === true; } catch { return false; } } /** * Plans an unscoped broadcast before SecretRefs are resolved. Rejected routes * stay in candidateChannels for per-channel errors but cannot expose secrets. * Host-derived binding/default accounts do not use this explicit-account plan. */ async function resolveMessageBroadcastAccountPlan(e) { const t = await validateExplicitMessageAccountSelection({ cfg: e.cfg, accountId: e.accountId, checkResolvedAccount: false }); if (!t) return; const n = []; for (const t of listRuntimeVisibleChannelPlugins()) if (resolveOutboundChannelPlugin({ channel: t.id, cfg: e.cfg }) && await isPotentialConfiguredMessageChannel({ cfg: e.cfg, plugin: t })) n.push(t); const r = []; for (const i of n) try { await validateExplicitMessageAccountSelection({ cfg: e.cfg, channel: i.id, accountId: t, plugin: i, checkResolvedAccount: false }); const n = i.config.inspectAccount?.(e.cfg, t); const a = n ?? await resolveChannelAccount({ plugin: i, cfg: e.cfg, accountId: t }); if (a !== void 0 && (n != null ? isAccountEnabled(n) : resolveChannelAccountEnabled({ plugin: i, account: a, cfg: e.cfg }))) r.push(i.id); } catch {} return { accountId: t, candidateChannels: n.map((e) => e.id), secretChannels: r }; } //#endregion export { resolveMessageBroadcastAccountPlan as n, validateExplicitMessageAccountSelection as r, isPotentialConfiguredMessageChannel as t };