From bb1df9e6811923fa001d7acd97014e77e15c07a7 Mon Sep 17 00:00:00 2001 From: Val Alexander <68980965+BunsDev@users.noreply.github.com> Date: Sat, 2 May 2026 23:24:07 -0500 Subject: [PATCH] fix(whatsapp): prefer lid mention resolution --- .../src/inbound/outbound-mentions.test.ts | 31 +++++++++++++++++++ .../whatsapp/src/inbound/outbound-mentions.ts | 12 +++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/extensions/whatsapp/src/inbound/outbound-mentions.test.ts b/extensions/whatsapp/src/inbound/outbound-mentions.test.ts index 03b774639cc..4457e0e9450 100644 --- a/extensions/whatsapp/src/inbound/outbound-mentions.test.ts +++ b/extensions/whatsapp/src/inbound/outbound-mentions.test.ts @@ -51,6 +51,37 @@ describe("resolveWhatsAppOutboundMentions", () => { }); }); + it("prefers explicit LID metadata over a phone JID id", () => { + expect( + resolveWhatsAppOutboundMentions({ + chatJid: "120363000000000000@g.us", + text: "ping @15551234567 and @277038292303944", + participants: [ + { + id: "15551234567@s.whatsapp.net", + lid: "277038292303944@lid", + }, + ], + }), + ).toEqual({ + text: "ping @277038292303944 and @277038292303944", + mentionedJids: ["277038292303944@lid"], + }); + }); + + it("uses bare digit tokens for LIDs before phone numbers when participant keys collide", () => { + expect( + resolveWhatsAppOutboundMentions({ + chatJid: "120363000000000000@g.us", + text: "ping @277038292303944 and @+277038292303944", + participants: [{ id: "277038292303944@s.whatsapp.net" }, { id: "277038292303944@lid" }], + }), + ).toEqual({ + text: "ping @277038292303944 and @+277038292303944", + mentionedJids: ["277038292303944@lid", "277038292303944@s.whatsapp.net"], + }); + }); + it("applies LID rewrites by match position while skipping code spans", () => { expect( resolveWhatsAppOutboundMentions({ diff --git a/extensions/whatsapp/src/inbound/outbound-mentions.ts b/extensions/whatsapp/src/inbound/outbound-mentions.ts index 1a96339aed0..ed165edbd48 100644 --- a/extensions/whatsapp/src/inbound/outbound-mentions.ts +++ b/extensions/whatsapp/src/inbound/outbound-mentions.ts @@ -128,9 +128,13 @@ function participantValues(participant: WhatsAppOutboundMentionParticipant): { function chooseMentionJid(participant: WhatsAppOutboundMentionParticipant): string | null { const values = participantValues(participant); + const idJid = normalizeKnownUserJid(values.id ?? ""); + const lidJid = normalizeKnownUserJid(values.lid ?? ""); return ( - normalizeKnownUserJid(values.id ?? "") ?? - normalizeKnownUserJid(values.lid ?? "") ?? + (idJid && isLidJid(idJid) ? idJid : null) ?? + (lidJid && isLidJid(lidJid) ? lidJid : null) ?? + idJid ?? + lidJid ?? normalizeKnownUserJid(values.phoneNumber ?? "") ?? normalizeKnownUserJid(values.e164 ?? "") ); @@ -210,7 +214,9 @@ export function resolveWhatsAppOutboundMentions(params: { } const token = match[0]; const digits = match[1].replace(/\D/g, ""); - const target = byPhone.get(digits) ?? byLid.get(digits); + const target = token.startsWith("@+") + ? (byPhone.get(digits) ?? byLid.get(digits)) + : (byLid.get(digits) ?? byPhone.get(digits)); if (!target) { continue; }