UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

252 lines (251 loc) • 8.74 kB
import { l as normalizeOptionalString, o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CCIiB4H7.mjs"; import { o as writeRuntimeJson, r as defaultRuntime } from "./runtime-B-SLYP-m.mjs"; import { n as sanitizeTerminalText } from "./safe-text-C80HAQ2e.mjs"; import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs"; import { t as formatCliCommand } from "./command-format-CqdwUgPk.mjs"; import { n as awaitWithinDeadline, t as ABSOLUTE_DEADLINE_EXPIRED } from "./absolute-deadline-kK4DcdPs.mjs"; import { t as captureRuntimeConfigAsyncReader } from "./io.runtime-DsJ2dLCQ.mjs"; import "./config-CMA-J084.mjs"; import { r as theme } from "./theme-BOI0xyRZ.mjs"; import { t as danger } from "./globals-CgepmiqE.mjs"; import { r as resolveChannelAccount } from "./account-resolution-D7u1J1M0.mjs"; import { t as ExpectedCliError } from "./failure-output-DVVDqwuu.mjs"; import { t as listReadOnlyChannelPluginsForConfig } from "./read-only-idsFCBWl.mjs"; import { i as resolveChannelDefaultAccountId } from "./helpers-ELZNDAnv.mjs"; import { a as formatUnknownChannelMessage } from "./error-format-O7EzXfiI.mjs"; import { a as getChannelsCommandSecretTargetIds } from "./command-secret-targets-CH2XIeV_.mjs"; import { l as resolveMessageActionDiscoveryForPluginAsync, t as createMessageActionDiscoveryContext } from "./message-action-discovery-D58BOJ5F.mjs"; import { n as parseTimeoutMsWithFallback } from "./parse-timeout-qqSdJSy9.mjs"; import { t as resolveCommandConfigWithSecrets } from "./command-config-resolution-Bv3V6f3-.mjs"; import { t as resolveInstallableChannelPlugin } from "./channel-plugin-resolution-Umf-XLaZ.mjs"; import { n as requireValidConfigFileSnapshot, r as requireValidConfigForWrite } from "./config-validation-Cy6dkTWs.mjs"; import { t as persistChannelPluginConfig } from "./plugin-config-persistence-B-x9ZZc_.mjs"; import { s as formatChannelAccountLabel } from "./shared-yNot1pgX.mjs"; //#region 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; } /** Print or serialize configured channel capabilities, actions, and optional health probe details. */ 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 = listReadOnlyChannelPluginsForConfig(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("\n").trimEnd()); } //#endregion export { channelsCapabilitiesCommand };