← Back to PRs

#21890: fix(whatsapp): propagate fromMe flag through DM message pipeline

by mactsk open 2026-02-20 14:02 View on GitHub →
channel: whatsapp-web size: S
## Summary Fixes #12482 In WhatsApp DMs, the agent cannot distinguish between messages sent by the gateway owner and messages from the contact. This makes it impossible for the agent to follow a two-way conversation correctly. ### Changes - Add `fromMe?: boolean` field to `WebInboundMessage` type - Propagate `Boolean(msg.key?.fromMe)` from the Baileys message into the inbound message - Pass `fromMe` through `buildInboundLine()` to `formatInboundEnvelope()` - In DM envelopes, append `(you)` to the `from` field when `fromMe` is true, producing `[WhatsApp +1555 (you)] message` for owner messages ### Security - `Boolean(msg.key?.fromMe)` safely defaults to `false` for null/undefined - The `(you)` marker passes through `sanitizeEnvelopeHeaderPart` (no injection risk) - The marker only appears in the agent's internal transcript, never in outbound messages - `fromMe` field is optional throughout — fully backward-compatible ### Test plan - [x] DM with `fromMe: true` → envelope contains `(you)` - [x] DM with `fromMe: false` → no `(you)` marker - [x] Group with `fromMe: true` → no `(you)` marker (groups unaffected) - [x] `fromMe` omitted → no `(you)` marker (backward compat) - [x] All existing envelope tests pass (16/16) 🤖 Generated with [Claude Code](https://claude.ai/claude-code) <!-- greptile_comment --> <h3>Greptile Summary</h3> Adds a `fromMe` flag to the WhatsApp inbound message pipeline so the agent can distinguish between messages sent by the gateway owner and messages from the contact in DMs. The flag flows from Baileys' `msg.key.fromMe` through `WebInboundMessage` → `buildInboundLine()` → `formatInboundEnvelope()`, where DM messages with `fromMe: true` get a `(you)` suffix appended to the `from` field in the envelope header (e.g., `[WhatsApp +1555 (you)] message`). - New optional `fromMe?: boolean` field added to `WebInboundMessage` type, ensuring backward compatibility - `Boolean(msg.key?.fromMe)` safely coerces null/undefined to `false` - The `(you)` marker only applies to DM messages (not groups), and passes through `sanitizeEnvelopeHeaderPart` safely - 4 new test cases cover DM fromMe true/false, group fromMe true, and omitted fromMe scenarios <h3>Confidence Score: 5/5</h3> - This PR is safe to merge — it's a well-scoped, backward-compatible addition with thorough test coverage. - The change is minimal and focused: a single optional boolean field threaded through the existing pipeline, with a small formatting change gated behind two conditions (isDirect && fromMe). All edge cases are tested (4 new tests). The field is optional throughout, so no existing callers break. The Boolean() coercion safely handles null/undefined. No security concerns — the marker only appears in internal agent transcripts. - No files require special attention. <sub>Last reviewed commit: 7fc1a3b</sub> <!-- greptile_other_comments_section --> <sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</sub> <!-- /greptile_comment -->

Most Similar PRs