Skip to content

fix(whatsapp): model forwarded and product-inquiry context variants in WhatsAppInboundMessage - #723

Merged
dancer merged 3 commits into
vercel:mainfrom
ricardolx:fix/whatsapp-context-variants
Aug 3, 2026
Merged

dancer merged 3 commits into
vercel:mainfrom
ricardolx:fix/whatsapp-context-variants

Conversation

@ricardolx

Copy link
Copy Markdown
Contributor

Problem

WhatsAppInboundMessage.context is typed as:

/** Context for quoted replies */
context?: {
  from: string;
  id: string;
};

But Meta's Cloud API webhook sends mutually exclusive context shapes depending on message origin (text messages webhook reference):

  • Quoted replies (and interactions with a business message): from + id
  • Forwarded messages: only forwarded ("only included if forwarded 5 times or less") or frequently_forwarded ("only included if forwarded more than 5 times") — no id, no from
  • Catalog product inquiries: from + id + referred_product

Because the type declares id required whenever context is present, downstream code like message.raw.message.context?.id.trim() type-checks cleanly and then throws a TypeError at runtime the first time a user forwards a message to the bot — context exists, id doesn't. We hit exactly this in production code building reply-to handling on top of the adapter.

Change

  • All context fields are optional, and the forwarded/product-inquiry fields (forwarded, frequently_forwarded, referred_product) are added, with doc comments noting which variant each field belongs to.
  • Changeset included (@chat-adapter/whatsapp: patch).

The adapter itself never reads context at runtime, so this is a type-only change. Consumers who currently dereference context.id unguarded will get a compile error after upgrading — intentionally, since that code is a latent runtime crash on forwarded messages.

🤖 Generated with Claude Code

@ricardolx
ricardolx requested a review from a team as a code owner July 17, 2026 01:44
@vercel

vercel Bot commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

@ricardolx is attempting to deploy a commit to the Vercel Labs Team on Vercel.

A member of the Team first needs to authorize it.

@ricardolx ricardolx closed this Jul 17, 2026
The inbound message context type declared `from` and `id` as required,
but Meta sends mutually exclusive context shapes: quoted replies carry
from/id, forwarded messages carry only forwarded/frequently_forwarded,
and catalog product inquiries add referred_product. Code narrowed by the
old type dereferences context.id and crashes on forwarded messages.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Signed-off-by: Ricardo Rivero <ricardo@enzadon.com>
@ricardolx ricardolx reopened this Jul 28, 2026
@ricardolx
ricardolx force-pushed the fix/whatsapp-context-variants branch from 048d968 to c8dfa6c Compare July 28, 2026 21:21
@ricardolx

Copy link
Copy Markdown
Contributor Author

Reopening this — I closed it prematurely shortly after opening, which was a mistake on my part. The underlying issue is still present on main: WhatsAppInboundMessage.context still declares from and id as required, so consumers narrowing on that type can still dereference context.id and hit a runtime TypeError on forwarded messages.

Rebased onto current main and added the DCO sign-off. The diff is unchanged — still type-only, 2 files. The adapter never reads context at runtime (the sole reference in packages/adapter-whatsapp/src is a log string), so there's no behavioral change here.

Happy to adjust the shape if you'd prefer a discriminated union over all-optional fields.

@dancer
dancer merged commit 16879fd into vercel:main Aug 3, 2026
11 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants