From 64df787448fbd48fa10c6907f5c085536432850e Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 14 Feb 2026 15:18:19 +0000 Subject: [PATCH] refactor(channels): share account summary helpers --- src/channels/account-summary.ts | 36 ++++++++++++++++++++++++ src/commands/status-all/channels.ts | 41 ++++----------------------- src/infra/channel-summary.ts | 43 ++++------------------------- 3 files changed, 48 insertions(+), 72 deletions(-) create mode 100644 src/channels/account-summary.ts diff --git a/src/channels/account-summary.ts b/src/channels/account-summary.ts new file mode 100644 index 0000000000..f4ff677a1c --- /dev/null +++ b/src/channels/account-summary.ts @@ -0,0 +1,36 @@ +import type { OpenClawConfig } from "../config/config.js"; +import type { ChannelAccountSnapshot } from "./plugins/types.core.js"; +import type { ChannelPlugin } from "./plugins/types.plugin.js"; + +export function buildChannelAccountSnapshot(params: { + plugin: ChannelPlugin; + account: unknown; + cfg: OpenClawConfig; + accountId: string; + enabled: boolean; + configured: boolean; +}): ChannelAccountSnapshot { + const described = params.plugin.config.describeAccount?.(params.account, params.cfg); + return { + enabled: params.enabled, + configured: params.configured, + ...described, + accountId: params.accountId, + }; +} + +export function formatChannelAllowFrom(params: { + plugin: ChannelPlugin; + cfg: OpenClawConfig; + accountId?: string | null; + allowFrom: Array; +}): string[] { + if (params.plugin.config.formatAllowFrom) { + return params.plugin.config.formatAllowFrom({ + cfg: params.cfg, + accountId: params.accountId, + allowFrom: params.allowFrom, + }); + } + return params.allowFrom.map((entry) => String(entry).trim()).filter(Boolean); +} diff --git a/src/commands/status-all/channels.ts b/src/commands/status-all/channels.ts index bb17e3c2e7..fe14e79354 100644 --- a/src/commands/status-all/channels.ts +++ b/src/commands/status-all/channels.ts @@ -5,6 +5,10 @@ import type { ChannelPlugin, } from "../../channels/plugins/types.js"; import type { OpenClawConfig } from "../../config/config.js"; +import { + buildChannelAccountSnapshot, + formatChannelAllowFrom, +} from "../../channels/account-summary.js"; import { resolveChannelDefaultAccountId } from "../../channels/plugins/helpers.js"; import { listChannelPlugins } from "../../channels/plugins/index.js"; import { sha256HexPrefix } from "../../logging/redact-identifier.js"; @@ -105,39 +109,6 @@ const resolveAccountConfigured = async ( return configured !== false; }; -const buildAccountSnapshot = (params: { - plugin: ChannelPlugin; - account: unknown; - cfg: OpenClawConfig; - accountId: string; - enabled: boolean; - configured: boolean; -}): ChannelAccountSnapshot => { - const described = params.plugin.config.describeAccount?.(params.account, params.cfg); - return { - enabled: params.enabled, - configured: params.configured, - ...described, - accountId: params.accountId, - }; -}; - -const formatAllowFrom = (params: { - plugin: ChannelPlugin; - cfg: OpenClawConfig; - accountId?: string | null; - allowFrom: Array; -}) => { - if (params.plugin.config.formatAllowFrom) { - return params.plugin.config.formatAllowFrom({ - cfg: params.cfg, - accountId: params.accountId, - allowFrom: params.allowFrom, - }); - } - return params.allowFrom.map((entry) => String(entry).trim()).filter(Boolean); -}; - const buildAccountNotes = (params: { plugin: ChannelPlugin; cfg: OpenClawConfig; @@ -177,7 +148,7 @@ const buildAccountNotes = (params: { const allowFrom = plugin.config.resolveAllowFrom?.({ cfg, accountId: snapshot.accountId }) ?? snapshot.allowFrom; if (allowFrom?.length) { - const formatted = formatAllowFrom({ + const formatted = formatChannelAllowFrom({ plugin, cfg, accountId: snapshot.accountId, @@ -349,7 +320,7 @@ export async function buildChannelsTable( const account = plugin.config.resolveAccount(cfg, accountId); const enabled = resolveAccountEnabled(plugin, account, cfg); const configured = await resolveAccountConfigured(plugin, account, cfg); - const snapshot = buildAccountSnapshot({ + const snapshot = buildChannelAccountSnapshot({ plugin, cfg, accountId, diff --git a/src/infra/channel-summary.ts b/src/infra/channel-summary.ts index d56282d77e..c0dd13ab20 100644 --- a/src/infra/channel-summary.ts +++ b/src/infra/channel-summary.ts @@ -1,4 +1,8 @@ import type { ChannelAccountSnapshot, ChannelPlugin } from "../channels/plugins/types.js"; +import { + buildChannelAccountSnapshot, + formatChannelAllowFrom, +} from "../channels/account-summary.js"; import { listChannelPlugins } from "../channels/plugins/index.js"; import { type OpenClawConfig, loadConfig } from "../config/config.js"; import { DEFAULT_ACCOUNT_ID } from "../routing/session-key.js"; @@ -60,41 +64,6 @@ const resolveAccountConfigured = async ( return true; }; -const buildAccountSnapshot = (params: { - plugin: ChannelPlugin; - account: unknown; - cfg: OpenClawConfig; - accountId: string; - enabled: boolean; - configured: boolean; -}): ChannelAccountSnapshot => { - const described = params.plugin.config.describeAccount - ? params.plugin.config.describeAccount(params.account, params.cfg) - : undefined; - return { - enabled: params.enabled, - configured: params.configured, - ...described, - accountId: params.accountId, - }; -}; - -const formatAllowFrom = (params: { - plugin: ChannelPlugin; - cfg: OpenClawConfig; - accountId?: string | null; - allowFrom: Array; -}) => { - if (params.plugin.config.formatAllowFrom) { - return params.plugin.config.formatAllowFrom({ - cfg: params.cfg, - accountId: params.accountId, - allowFrom: params.allowFrom, - }); - } - return params.allowFrom.map((entry) => String(entry).trim()).filter(Boolean); -}; - const buildAccountDetails = (params: { entry: ChannelAccountEntry; plugin: ChannelPlugin; @@ -132,7 +101,7 @@ const buildAccountDetails = (params: { } if (params.includeAllowFrom && snapshot.allowFrom?.length) { - const formatted = formatAllowFrom({ + const formatted = formatChannelAllowFrom({ plugin: params.plugin, cfg: params.cfg, accountId: snapshot.accountId, @@ -166,7 +135,7 @@ export async function buildChannelSummary( const account = plugin.config.resolveAccount(effective, accountId); const enabled = resolveAccountEnabled(plugin, account, effective); const configured = await resolveAccountConfigured(plugin, account, effective); - const snapshot = buildAccountSnapshot({ + const snapshot = buildChannelAccountSnapshot({ plugin, account, cfg: effective,