mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 10:17:39 +00:00
refactor(outbound): reuse required channel/plugin resolution
This commit is contained in:
@@ -103,6 +103,27 @@ export type MessagePollResult = {
|
|||||||
dryRun?: boolean;
|
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) {
|
function resolveGatewayOptions(opts?: MessageGatewayOptions) {
|
||||||
// Security: backend callers (tools/agents) must not accept user-controlled gateway URLs.
|
// Security: backend callers (tools/agents) must not accept user-controlled gateway URLs.
|
||||||
// Use config-derived gateway target only.
|
// Use config-derived gateway target only.
|
||||||
@@ -144,16 +165,8 @@ async function callMessageGateway<T>(params: {
|
|||||||
|
|
||||||
export async function sendMessage(params: MessageSendParams): Promise<MessageSendResult> {
|
export async function sendMessage(params: MessageSendParams): Promise<MessageSendResult> {
|
||||||
const cfg = params.cfg ?? loadConfig();
|
const cfg = params.cfg ?? loadConfig();
|
||||||
const channel = params.channel?.trim()
|
const channel = await resolveRequiredChannel({ cfg, channel: params.channel });
|
||||||
? normalizeChannelId(params.channel)
|
const plugin = resolveRequiredPlugin(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 deliveryMode = plugin.outbound?.deliveryMode ?? "direct";
|
const deliveryMode = plugin.outbound?.deliveryMode ?? "direct";
|
||||||
const normalizedPayloads = normalizeReplyPayloadsForDelivery([
|
const normalizedPayloads = normalizeReplyPayloadsForDelivery([
|
||||||
{
|
{
|
||||||
@@ -256,12 +269,7 @@ export async function sendMessage(params: MessageSendParams): Promise<MessageSen
|
|||||||
|
|
||||||
export async function sendPoll(params: MessagePollParams): Promise<MessagePollResult> {
|
export async function sendPoll(params: MessagePollParams): Promise<MessagePollResult> {
|
||||||
const cfg = params.cfg ?? loadConfig();
|
const cfg = params.cfg ?? loadConfig();
|
||||||
const channel = params.channel?.trim()
|
const channel = await resolveRequiredChannel({ cfg, channel: params.channel });
|
||||||
? normalizeChannelId(params.channel)
|
|
||||||
: (await resolveMessageChannelSelection({ cfg })).channel;
|
|
||||||
if (!channel) {
|
|
||||||
throw new Error(`Unknown channel: ${params.channel}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const pollInput: PollInput = {
|
const pollInput: PollInput = {
|
||||||
question: params.question,
|
question: params.question,
|
||||||
@@ -270,7 +278,7 @@ export async function sendPoll(params: MessagePollParams): Promise<MessagePollRe
|
|||||||
durationSeconds: params.durationSeconds,
|
durationSeconds: params.durationSeconds,
|
||||||
durationHours: params.durationHours,
|
durationHours: params.durationHours,
|
||||||
};
|
};
|
||||||
const plugin = getChannelPlugin(channel);
|
const plugin = resolveRequiredPlugin(channel);
|
||||||
const outbound = plugin?.outbound;
|
const outbound = plugin?.outbound;
|
||||||
if (!outbound?.sendPoll) {
|
if (!outbound?.sendPoll) {
|
||||||
throw new Error(`Unsupported poll channel: ${channel}`);
|
throw new Error(`Unsupported poll channel: ${channel}`);
|
||||||
|
|||||||
Reference in New Issue
Block a user