refactor: share outbound poll and signal route helpers

This commit is contained in:
Peter Steinberger 2026-03-13 22:06:32 +00:00
parent 86caf454f4
commit 07900303f4
2 changed files with 62 additions and 34 deletions

View File

@ -100,6 +100,32 @@ export type MessagePollResult = {
dryRun?: boolean;
};
function buildMessagePollResult(params: {
channel: string;
to: string;
normalized: {
question: string;
options: string[];
maxSelections: number;
durationSeconds?: number | null;
durationHours?: number | null;
};
result?: MessagePollResult["result"];
dryRun?: boolean;
}): MessagePollResult {
return {
channel: params.channel,
to: params.to,
question: params.normalized.question,
options: params.normalized.options,
maxSelections: params.normalized.maxSelections,
durationSeconds: params.normalized.durationSeconds ?? null,
durationHours: params.normalized.durationHours ?? null,
via: "gateway",
...(params.dryRun ? { dryRun: true } : { result: params.result }),
};
}
async function resolveRequiredChannel(params: {
cfg: OpenClawConfig;
channel?: string;
@ -291,17 +317,12 @@ export async function sendPoll(params: MessagePollParams): Promise<MessagePollRe
: normalizePollInput(pollInput);
if (params.dryRun) {
return {
return buildMessagePollResult({
channel,
to: params.to,
question: normalized.question,
options: normalized.options,
maxSelections: normalized.maxSelections,
durationSeconds: normalized.durationSeconds ?? null,
durationHours: normalized.durationHours ?? null,
via: "gateway",
normalized,
dryRun: true,
};
});
}
const result = await callMessageGateway<{
@ -329,15 +350,10 @@ export async function sendPoll(params: MessagePollParams): Promise<MessagePollRe
},
});
return {
return buildMessagePollResult({
channel,
to: params.to,
question: normalized.question,
options: normalized.options,
maxSelections: normalized.maxSelections,
durationSeconds: normalized.durationSeconds ?? null,
durationHours: normalized.durationHours ?? null,
via: "gateway",
normalized,
result,
};
});
}

View File

@ -76,6 +76,24 @@ function formatAttachmentSummaryPlaceholder(contentTypes: Array<string | undefin
return `[${parts.join(" + ")} attached]`;
}
function resolveSignalInboundRoute(params: {
cfg: SignalEventHandlerDeps["cfg"];
accountId: SignalEventHandlerDeps["accountId"];
isGroup: boolean;
groupId?: string;
senderPeerId: string;
}) {
return resolveAgentRoute({
cfg: params.cfg,
channel: "signal",
accountId: params.accountId,
peer: {
kind: params.isGroup ? "group" : "direct",
id: params.isGroup ? (params.groupId ?? "unknown") : params.senderPeerId,
},
});
}
export function createSignalEventHandler(deps: SignalEventHandlerDeps) {
type SignalInboundEntry = {
senderName: string;
@ -106,14 +124,12 @@ export function createSignalEventHandler(deps: SignalEventHandlerDeps) {
directLabel: entry.senderName,
directId: entry.senderDisplay,
});
const route = resolveAgentRoute({
const route = resolveSignalInboundRoute({
cfg: deps.cfg,
channel: "signal",
accountId: deps.accountId,
peer: {
kind: entry.isGroup ? "group" : "direct",
id: entry.isGroup ? (entry.groupId ?? "unknown") : entry.senderPeerId,
},
isGroup: entry.isGroup,
groupId: entry.groupId,
senderPeerId: entry.senderPeerId,
});
const storePath = resolveStorePath(deps.cfg.session?.store, {
agentId: route.agentId,
@ -412,14 +428,12 @@ export function createSignalEventHandler(deps: SignalEventHandlerDeps) {
}
const senderPeerId = resolveSignalPeerId(params.sender);
const route = resolveAgentRoute({
const route = resolveSignalInboundRoute({
cfg: deps.cfg,
channel: "signal",
accountId: deps.accountId,
peer: {
kind: isGroup ? "group" : "direct",
id: isGroup ? (groupId ?? "unknown") : senderPeerId,
},
isGroup,
groupId,
senderPeerId,
});
const groupLabel = isGroup ? `${groupName ?? "Signal Group"} id:${groupId}` : undefined;
const messageId = params.reaction.targetSentTimestamp
@ -610,14 +624,12 @@ export function createSignalEventHandler(deps: SignalEventHandlerDeps) {
return;
}
const route = resolveAgentRoute({
const route = resolveSignalInboundRoute({
cfg: deps.cfg,
channel: "signal",
accountId: deps.accountId,
peer: {
kind: isGroup ? "group" : "direct",
id: isGroup ? (groupId ?? "unknown") : senderPeerId,
},
isGroup,
groupId,
senderPeerId,
});
const mentionRegexes = buildMentionRegexes(deps.cfg, route.agentId);
const wasMentioned = isGroup && matchesMentionPatterns(messageText, mentionRegexes);