fix(whatsapp): preserve group message IDs and normalize reaction participants

This commit is contained in:
Jake
2026-01-10 18:06:53 +13:00
committed by Peter Steinberger
parent 9f9098406c
commit 4933113252
4 changed files with 60 additions and 9 deletions

View File

@@ -826,7 +826,13 @@ export async function monitorWebProvider(
DEFAULT_GROUP_HISTORY_LIMIT;
const groupHistories = new Map<
string,
Array<{ sender: string; body: string; timestamp?: number }>
Array<{
sender: string;
body: string;
timestamp?: number;
id?: string;
senderJid?: string;
}>
>();
const groupMemberNames = new Map<string, Map<string, string>>();
const sleep =
@@ -1104,6 +1110,8 @@ export async function monitorWebProvider(
sender: string;
body: string;
timestamp?: number;
id?: string;
senderJid?: string;
}>;
suppressGroupHistoryClear?: boolean;
},
@@ -1123,14 +1131,17 @@ export async function monitorWebProvider(
if (historyWithoutCurrent.length > 0) {
const lineBreak = "\\n";
const historyText = historyWithoutCurrent
.map((m) =>
formatAgentEnvelope({
.map((m) => {
const bodyWithId = m.id
? `${m.body}\n[message_id: ${m.id}]`
: m.body;
return formatAgentEnvelope({
provider: "WhatsApp",
from: conversationId,
timestamp: m.timestamp,
body: `${m.sender}: ${m.body}`,
}),
)
body: `${m.sender}: ${bodyWithId}`,
});
})
.join(lineBreak);
combinedBody = buildHistoryContext({
historyText,
@@ -1554,11 +1565,19 @@ export async function monitorWebProvider(
sender: string;
body: string;
timestamp?: number;
id?: string;
senderJid?: string;
}>);
const sender =
msg.senderName && msg.senderE164
? `${msg.senderName} (${msg.senderE164})`
: (msg.senderName ?? msg.senderE164 ?? "Unknown");
history.push({
sender: msg.senderName ?? msg.senderE164 ?? "Unknown",
sender,
body: msg.body,
timestamp: msg.timestamp,
id: msg.id,
senderJid: msg.senderJid,
});
while (history.length > groupHistoryLimit) history.shift();
groupHistories.set(groupHistoryKey, history);

View File

@@ -620,7 +620,7 @@ export async function monitorWebInbox(options: {
remoteJid: jid,
id: messageId,
fromMe,
participant,
participant: participant ? toWhatsappJid(participant) : undefined,
},
},
});

View File

@@ -1402,4 +1402,36 @@ describe("web monitor inbox", () => {
await listener.close();
});
it("normalizes participant phone numbers to JIDs in sendReaction", async () => {
const listener = await monitorWebInbox({
verbose: false,
onMessage: vi.fn(),
accountId: ACCOUNT_ID,
authDir,
});
const sock = await createWaSocket();
await listener.sendReaction(
"12345@g.us",
"msg123",
"👍",
false,
"+6421000000",
);
expect(sock.sendMessage).toHaveBeenCalledWith("12345@g.us", {
react: {
text: "👍",
key: {
remoteJid: "12345@g.us",
id: "msg123",
fromMe: false,
participant: "6421000000@s.whatsapp.net",
},
},
});
await listener.close();
});
});