From b4cba304e237b25b8a2221652be41ec6ae22f65e Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Wed, 18 Feb 2026 22:39:54 +0000 Subject: [PATCH] refactor(outbound): reuse required channel/plugin resolution --- src/infra/outbound/message.ts | 42 +++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/src/infra/outbound/message.ts b/src/infra/outbound/message.ts index 88e56d84a4..e7e0a0a93c 100644 --- a/src/infra/outbound/message.ts +++ b/src/infra/outbound/message.ts @@ -103,6 +103,27 @@ export type MessagePollResult = { dryRun?: boolean; }; +async function resolveRequiredChannel(params: { + cfg: OpenClawConfig; + channel?: string; +}): Promise { + const channel = params.channel?.trim() + ? normalizeChannelId(params.channel) + : (await resolveMessageChannelSelection({ cfg: params.cfg })).channel; + if (!channel) { + throw new Error(`Unknown channel: ${params.channel}`); + } + return channel; +} + +function resolveRequiredPlugin(channel: string) { + const plugin = getChannelPlugin(channel); + if (!plugin) { + throw new Error(`Unknown channel: ${channel}`); + } + return plugin; +} + function resolveGatewayOptions(opts?: MessageGatewayOptions) { // Security: backend callers (tools/agents) must not accept user-controlled gateway URLs. // Use config-derived gateway target only. @@ -144,16 +165,8 @@ async function callMessageGateway(params: { export async function sendMessage(params: MessageSendParams): Promise { const cfg = params.cfg ?? loadConfig(); - const channel = params.channel?.trim() - ? normalizeChannelId(params.channel) - : (await resolveMessageChannelSelection({ cfg })).channel; - if (!channel) { - throw new Error(`Unknown channel: ${params.channel}`); - } - const plugin = getChannelPlugin(channel); - if (!plugin) { - throw new Error(`Unknown channel: ${channel}`); - } + const channel = await resolveRequiredChannel({ cfg, channel: params.channel }); + const plugin = resolveRequiredPlugin(channel); const deliveryMode = plugin.outbound?.deliveryMode ?? "direct"; const normalizedPayloads = normalizeReplyPayloadsForDelivery([ { @@ -256,12 +269,7 @@ export async function sendMessage(params: MessageSendParams): Promise { const cfg = params.cfg ?? loadConfig(); - const channel = params.channel?.trim() - ? normalizeChannelId(params.channel) - : (await resolveMessageChannelSelection({ cfg })).channel; - if (!channel) { - throw new Error(`Unknown channel: ${params.channel}`); - } + const channel = await resolveRequiredChannel({ cfg, channel: params.channel }); const pollInput: PollInput = { question: params.question, @@ -270,7 +278,7 @@ export async function sendPoll(params: MessagePollParams): Promise