UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 4.72 kB
import{a as asOptionalRecord}from"./record-coerce-BTbqdJ3-.mjs";import{r as defaultRuntime}from"./runtime-DIcLIHcp.mjs";import{t as formatErrorMessage}from"./errors-D37BJTfI.mjs";import{t as createDedupeCache}from"./dedupe-CsfmSvUH.mjs";import{n as normalizeMessageChannel}from"./message-channel-core-D_hPOadj.mjs";import{t as isDeliverableMessageChannel}from"./message-channel-normalize-hDAO6_JQ.mjs";import"./message-channel-514k57tV.mjs";import{r as resolveChannelAccount}from"./account-resolution-BMEKSThh.mjs";import{n as getRuntimeVisibleChannelPlugin,r as listRuntimeVisibleChannelPlugins}from"./runtime-visible-channels-Cx_NfvRz.mjs";import{n as resolveMissingOfficialExternalChannelPluginRepairHint,r as resolveMissingOfficialExternalChannelPluginRepairHints}from"./official-external-plugin-repair-hints-CcJDRzw1.mjs";import{a as formatUnknownChannelMessage}from"./error-format-D9Q6t3gZ.mjs";import{r as resolveOutboundChannelPlugin}from"./channel-resolution-DC4uO43j.mjs";import{t as isAccountEnabled}from"./account-enabled-YsTp1izZ.mjs"; //!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.`)}export{listConfiguredMessageChannels as n,resolveMessageChannelSelection as r,isConfiguredChannel as t};