mirror of
https://github.com/openclaw/openclaw.git
synced 2026-02-19 18:39:20 -05:00
refactor(outbound): reuse required channel/plugin resolution
This commit is contained in:
@@ -103,6 +103,27 @@ export type MessagePollResult = {
|
||||
dryRun?: boolean;
|
||||
};
|
||||
|
||||
async function resolveRequiredChannel(params: {
|
||||
cfg: OpenClawConfig;
|
||||
channel?: string;
|
||||
}): Promise<string> {
|
||||
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<T>(params: {
|
||||
|
||||
export async function sendMessage(params: MessageSendParams): Promise<MessageSendResult> {
|
||||
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<MessageSen
|
||||
|
||||
export async function sendPoll(params: MessagePollParams): Promise<MessagePollResult> {
|
||||
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<MessagePollRe
|
||||
durationSeconds: params.durationSeconds,
|
||||
durationHours: params.durationHours,
|
||||
};
|
||||
const plugin = getChannelPlugin(channel);
|
||||
const plugin = resolveRequiredPlugin(channel);
|
||||
const outbound = plugin?.outbound;
|
||||
if (!outbound?.sendPoll) {
|
||||
throw new Error(`Unsupported poll channel: ${channel}`);
|
||||
|
||||
Reference in New Issue
Block a user