diff --git a/src/web/auto-reply/monitor/broadcast.ts b/src/web/auto-reply/monitor/broadcast.ts index d220c9a82..1dc51bef1 100644 --- a/src/web/auto-reply/monitor/broadcast.ts +++ b/src/web/auto-reply/monitor/broadcast.ts @@ -11,6 +11,39 @@ import { whatsappInboundLog } from "../loggers.js"; import type { WebInboundMsg } from "../types.js"; import type { GroupHistoryEntry } from "./process-message.js"; +function buildBroadcastRouteKeys(params: { + cfg: ReturnType; + msg: WebInboundMsg; + route: ReturnType; + peerId: string; + agentId: string; +}) { + const sessionKey = buildAgentSessionKey({ + agentId: params.agentId, + channel: "whatsapp", + accountId: params.route.accountId, + peer: { + kind: params.msg.chatType === "group" ? "group" : "direct", + id: params.peerId, + }, + dmScope: params.cfg.session?.dmScope, + identityLinks: params.cfg.session?.identityLinks, + }); + const mainSessionKey = buildAgentMainSessionKey({ + agentId: params.agentId, + mainKey: DEFAULT_MAIN_KEY, + }); + + return { + sessionKey, + mainSessionKey, + lastRoutePolicy: deriveLastRoutePolicy({ + sessionKey, + mainSessionKey, + }), + }; +} + export async function maybeBroadcastMessage(params: { cfg: ReturnType; msg: WebInboundMsg; @@ -52,41 +85,17 @@ export async function maybeBroadcastMessage(params: { whatsappInboundLog.warn(`Broadcast agent ${agentId} not found in agents.list; skipping`); return false; } + const routeKeys = buildBroadcastRouteKeys({ + cfg: params.cfg, + msg: params.msg, + route: params.route, + peerId: params.peerId, + agentId: normalizedAgentId, + }); const agentRoute = { ...params.route, agentId: normalizedAgentId, - sessionKey: buildAgentSessionKey({ - agentId: normalizedAgentId, - channel: "whatsapp", - accountId: params.route.accountId, - peer: { - kind: params.msg.chatType === "group" ? "group" : "direct", - id: params.peerId, - }, - dmScope: params.cfg.session?.dmScope, - identityLinks: params.cfg.session?.identityLinks, - }), - mainSessionKey: buildAgentMainSessionKey({ - agentId: normalizedAgentId, - mainKey: DEFAULT_MAIN_KEY, - }), - lastRoutePolicy: deriveLastRoutePolicy({ - sessionKey: buildAgentSessionKey({ - agentId: normalizedAgentId, - channel: "whatsapp", - accountId: params.route.accountId, - peer: { - kind: params.msg.chatType === "group" ? "group" : "direct", - id: params.peerId, - }, - dmScope: params.cfg.session?.dmScope, - identityLinks: params.cfg.session?.identityLinks, - }), - mainSessionKey: buildAgentMainSessionKey({ - agentId: normalizedAgentId, - mainKey: DEFAULT_MAIN_KEY, - }), - }), + ...routeKeys, }; try {