openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
4 lines • 7.26 kB
JavaScript
import{l as normalizeOptionalString,o as normalizeLowercaseStringOrEmpty}from"./string-coerce-CpTFcSwY.mjs";import{o as writeRuntimeJson,r as defaultRuntime}from"./runtime-DIcLIHcp.mjs";import{n as sanitizeTerminalText}from"./safe-text-B_QTgn0s.mjs";import{t as formatErrorMessage}from"./errors-D37BJTfI.mjs";import{t as formatCliCommand}from"./command-format-D7KhNLP0.mjs";import{n as awaitWithinDeadline,t as ABSOLUTE_DEADLINE_EXPIRED}from"./absolute-deadline-CE14TWdo.mjs";import{t as captureRuntimeConfigAsyncReader}from"./io.runtime-BvSeVFIY.mjs";import{r as resolveChannelAccount}from"./account-resolution-BMEKSThh.mjs";import"./config-DkZ8y8Gv.mjs";import{r as theme}from"./theme-DS2CtI9X.mjs";import{t as danger}from"./globals-l9I53ZKA.mjs";import{t as ExpectedCliError}from"./expected-cli-error-oDVmXYil.mjs";import{n as listReadOnlyChannelPluginsForConfigAsync}from"./read-only-B-Uv2t8Q.mjs";import{i as resolveChannelDefaultAccountId}from"./helpers-1A2j6LGz.mjs";import{a as formatUnknownChannelMessage}from"./error-format-D9Q6t3gZ.mjs";import{a as getChannelsCommandSecretTargetIds}from"./command-secret-targets-X-olXSJu.mjs";import{l as resolveMessageActionDiscoveryForPluginAsync,t as createMessageActionDiscoveryContext}from"./message-action-discovery-rhv-hZtK.mjs";import{n as parseTimeoutMsWithFallback}from"./parse-timeout-euv6DhlU.mjs";import{t as resolveCommandConfigWithSecrets}from"./command-config-resolution-D_5kK7Ri.mjs";import{t as resolveInstallableChannelPlugin}from"./channel-plugin-resolution-Cc16Hm1A.mjs";import{n as requireValidConfigFileSnapshot,r as requireValidConfigForWrite}from"./config-validation-BexrhW6U.mjs";import{t as persistChannelPluginConfig}from"./plugin-config-persistence-C297sR0X.mjs";import{s as formatChannelAccountLabel}from"./shared-DqVdi65N.mjs";
//!src/commands/channels/capabilities.ts
const CHANNEL_CAPABILITIES_TIMEOUT_MAX_MS=3e4;async function runChannelCapabilitiesCheck(e){try{const t=await awaitWithinDeadline(async()=>e.run(),performance.now()+e.timeoutMs,()=>performance.now());return t===ABSOLUTE_DEADLINE_EXPIRED?e.failure(void 0,true):t}catch(t){return e.failure(t,false)}}function formatSupport(e){if(!e)return`unknown`;const t=[];if(e.chatTypes?.length)t.push(`chatTypes=${e.chatTypes.join(`,`)}`);for(const n of[`polls`,`reactions`,`edit`,`unsend`,`reply`,`effects`,`groupManagement`,`threads`,`media`,`nativeCommands`,`blockStreaming`])if(e[n])t.push(n);return t.length?t.join(` `):`none`}function formatGenericProbeLines(e){if(!e||typeof e!==`object`)return[];const t=e;const n=typeof t.ok===`boolean`?t.ok:void 0;if(n===true)return[{text:`Check: ok`}];if(n===false)return[{text:`Check: failed${typeof t.error===`string`&&t.error?` (${t.error})`:``}`,tone:`error`}];return[]}function renderDisplayLine(e){const t=sanitizeTerminalText(e.text);switch(e.tone){case`muted`:case`success`:case`warn`:case`error`:return theme[e.tone](t);default:return t}}async function resolveCapabilitiesRuntimeConfig(e,t){return(await resolveCommandConfigWithSecrets({config:e,commandName:`channels`,targetIds:getChannelsCommandSecretTargetIds(),runtime:t})).effectiveConfig}async function channelsCapabilitiesCommand(e,t=defaultRuntime){const n=normalizeLowercaseStringOrEmpty(e.channel);const r=Boolean(n&&n!==`all`);const i=r?await requireValidConfigForWrite(t):null;const a=r?i?.snapshot:await requireValidConfigFileSnapshot(t);if(!a)return;let o=await resolveCapabilitiesRuntimeConfig(a.config,t);const s=Math.min(parseTimeoutMsWithFallback(e.timeout,1e4,{invalidType:`error`}),CHANNEL_CAPABILITIES_TIMEOUT_MAX_MS);const c=normalizeOptionalString(e.target)??``;if((!n||n===`all`)&&(e.account||c)){const t=`${e.account?`--account`:`--target`} requires a specific --channel. Run ${formatCliCommand(`openclaw channels list`)} to choose one.`;throw new ExpectedCliError({message:t,humanOutput:danger(t),machineOutput:t})}let l=await listReadOnlyChannelPluginsForConfigAsync(o,{includeSetupFallbackPlugins:true});if(r){const r=await resolveInstallableChannelPlugin({cfg:a.sourceConfig,runtime:t,agentId:e.agent,rawChannel:n,allowInstall:true});if(r.configChanged){await persistChannelPluginConfig({cfg:r.cfg,pluginInstalled:r.pluginInstalled,baseHash:a.hash,writeOptions:i?.writeOptions,runtime:t});o=await resolveCapabilitiesRuntimeConfig(await captureRuntimeConfigAsyncReader()(),t)}l=r.plugin?[r.plugin]:[]}if(l.length===0){if(!r){if(e.json){writeRuntimeJson(t,{channels:[]});return}t.log(theme.muted(`No configured channel capabilities found. Run ${formatCliCommand(`openclaw channels list --all`)} to see available channels.`));return}const i=formatUnknownChannelMessage({channel:n});throw new ExpectedCliError({message:i,humanOutput:danger(i),machineOutput:i})}const u=[];for(const t of l){const n=normalizeOptionalString(e.account);const r=c||void 0;const i=n?[n]:(()=>{const e=t.config.listAccountIds(o);return e.length>0?e:[resolveChannelDefaultAccountId({plugin:t,cfg:o,accountIds:e})]})();for(const e of i){const n=await resolveChannelAccount({plugin:t,cfg:o,accountId:e});const i=t.config.isConfigured?await t.config.isConfigured(n,o):Boolean(n);const a=t.config.isEnabled?t.config.isEnabled(n,o):n.enabled!==false;let c;if(i&&a&&t.status?.probeAccount)c=await runChannelCapabilitiesCheck({timeoutMs:s,failure:(e,t)=>t?{ok:false,timedOut:true,error:`check timed out after ${s}ms`}:{ok:false,error:formatErrorMessage(e)},run:()=>t.status?.probeAccount?.({account:n,timeoutMs:s,cfg:o})});const l=i&&a&&t.status?.buildCapabilitiesDiagnostics?await runChannelCapabilitiesCheck({timeoutMs:s,failure:(e,t)=>({lines:[{text:t?`Diagnostics: timed out after ${s}ms`:`Diagnostics: failed (${formatErrorMessage(e)})`,tone:`error`}],...t?{details:{timedOut:true}}:{}}),run:()=>t.status?.buildCapabilitiesDiagnostics?.({account:n,timeoutMs:s,cfg:o,probe:c,target:r})}):void 0;const d=(await resolveMessageActionDiscoveryForPluginAsync({pluginId:t.id,actions:t.actions,context:createMessageActionDiscoveryContext({cfg:o,accountId:e}),includeActions:true})).actions;const f=Array.from(/* @__PURE__ */ new Set([`send`,`broadcast`,...d]));u.push({plugin:t,channel:t.id,accountId:e,accountName:typeof n.name===`string`?normalizeOptionalString(n.name):void 0,configured:i,enabled:a,support:t.capabilities,probe:c,actions:f,diagnostics:l})}}if(e.json){writeRuntimeJson(t,{channels:u});return}const d=[];for(const e of u){const t=formatChannelAccountLabel({channel:e.channel,accountId:e.accountId,name:e.accountName,channelLabel:e.plugin.meta.label??e.channel,channelStyle:theme.accent,accountStyle:theme.heading});d.push(theme.heading(t));d.push(`Support: ${formatSupport(e.support)}`);d.push(`Actions: ${e.actions.join(`, `)}`);if(e.configured===false||e.enabled===false){const t=e.configured===false?`not configured`:`configured`;const n=e.enabled===false?`disabled`:`enabled`;d.push(`Status: ${t}, ${n}`)}const n=e.plugin.status?.formatCapabilitiesProbe?.({probe:e.probe});const r=n?.length?n:formatGenericProbeLines(e.probe);if(r.length>0)d.push(...r.map(renderDisplayLine));else if(e.configured&&e.enabled)d.push(theme.muted(`Check: unavailable`));if(e.diagnostics?.lines?.length)d.push(...e.diagnostics.lines.map(renderDisplayLine));d.push(``)}t.log(d.join(`
`).trimEnd())}export{channelsCapabilitiesCommand};