Skip to content

feat(whatsapp): outbound media attachments via Twilio MediaUrl (#1315) - #1318

Merged
dolho merged 1 commit into
devfrom
feature/1315-whatsapp-outbound-media
Jun 23, 2026
Merged

dolho merged 1 commit into
devfrom
feature/1315-whatsapp-outbound-media

Conversation

@vybe

@vybe vybe commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Summary

  • WhatsApp agents can now send files to users. WhatsAppAdapter.send_response delivers ChannelResponse.files as Twilio MediaUrl attachments (one message per file, text sent first), reaching parity with the Slack adapter — previously these files were silently dropped.
  • Twilio fetches a MediaUrl server-side, so each file is persisted to FILES-001 storage and handed to Twilio as its public ?sig= URL. New create_share_from_bytes() is the bytes-input twin of create_share; both now share the extracted _persist_and_register helper (MIME-blocklist → quota → disk-check → persist → DB → URL).
  • Gated on the per-agent file_sharing_enabled toggle; short 1h share TTL (the existing cleanup-service reaper purges expired shares).

Changes

  • src/backend/adapters/whatsapp_adapter.py — _prepare_outbound_media (persist + classify, per-file isolated), send_response restructure (markdown-convert text first, append download links verbatim, then one media message per file), _send_message gains media_url, MIME-cap helper + constants.
  • src/backend/services/agent_shared_files_service.py — extract _persist_and_register + _validate_expires_in; add create_share_from_bytes. create_share behavior unchanged.
  • tests/unit/test_whatsapp_outbound_media.py — 42 tests.
  • docs/memory/requirements.md, docs/memory/feature-flows/whatsapp-integration.md — outbound-media flow documented.

Design notes

  • Caps (image/audio/video ≈5 MB, documents ≈16 MB) are checked on the detected MIME (the download endpoint serves Content-Type: mime_type, which Twilio reads to render media), not the code-fence language hint. application/octet-stream/unknown → text-link fallback.
  • Graceful degradation, never silent drop: no public HTTPS public_chat_url / unsupported MIME / oversized → the file's download link is appended to the text body + WARN log.
  • Per-file isolation: a rejected or failed file (quota/disk/MIME HTTPException, or any error) is logged and skipped — it never aborts the text reply or sibling files.
  • No revoke-after-send: Twilio fetches the URL asynchronously after the POST 2xx, so short-TTL + the existing reaper is the correct bound (revoking on the ack would race the fetch).
  • No new idempotency: twilio_webhook dedups inbound by MessageSid before dispatch, so send_response (and each mint) runs exactly once.
  • v1 scope = deliver ChannelResponse.files artifacts only; promoting in-text FILES-001 URLs to MediaUrl is deferred (feat(whatsapp): outbound media attachments — deliver ChannelResponse.files to WhatsApp users via Twilio MediaUrl #1315 option b). Telegram has the same outbound-drop bug but uses direct sendDocument (no URL needed) — tracked as a follow-up.

Test Plan

  • New tests pass: pytest tests/unit/test_whatsapp_outbound_media.py (42 passed)
  • FILES-001 / channel-router regression green (359 passed across whatsapp/slack/telegram/message_router/twilio/shared-file tests)
  • Manual: WhatsApp agent with file_sharing_enabled + public HTTPS public_chat_url returns a csv/json code-fence → user receives the file inline

Fixes #1315

🤖 Generated with Claude Code

WhatsApp agents can now send files to users. send_response delivers
ChannelResponse.files as Twilio MediaUrl attachments (one message per file,
text first), reaching parity with the Slack adapter.

- New create_share_from_bytes() persists in-memory bytes through the FILES-001
  pipeline (MIME-blocklist/quota/disk/DB) and mints a public ?sig= URL; both it
  and create_share now share the extracted _persist_and_register helper.
- Per-agent file_sharing_enabled gate; 1h share TTL (cleanup reaper purges).
- Caps (image/audio/video ~5MB, documents ~16MB) on the detected MIME; graceful
  text-link fallback when public_chat_url is unset/non-HTTPS, the MIME is
  unsupported, or the file is oversized — never silently dropped.
- Per-file isolation: a rejected/failed file never aborts the text or siblings.
- 42 unit tests; requirements.md + whatsapp-integration.md updated.

Fixes #1315

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@dolho

dolho commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

/review — #1318 (WhatsApp outbound media)

Reviewed the diff against dev. This is a well-engineered PR — CI green, clean refactor, thorough tests and design notes. No blockers; two minor/informational notes.

✅ Verified good

  • Refactor is safe: _persist_and_register cleanly extracts the shared tail; create_share keeps its exact pipeline (gate → expiry → extract → persist), and create_share_from_bytes mirrors the same gate/validation. create_share behavior unchanged as claimed.
  • Security sound: reuses the proven FILES-001 ?sig= signed-URL mechanism (no SSRF — Twilio fetches the platform's own signed URL), gated on file_sharing_enabled, short 1h TTL + the existing reaper. The "no revoke-after-send" reasoning is correct (Twilio fetches async after the 2xx, so revoking on ack would race the fetch).
  • Per-file isolation (HTTPException + generic except → continue), text-first ordering, HTTPS-or-degrade, MIME-cap on the detected MIME, and fallback_links appended verbatim (correctly bypassing markdown so a __ in the sig token isn't mangled). Covered by test_gate_off_*, test_*_falls_back_to_link, test_*_isolated_per_file.

🟡 Informational

[I1] "never silent drop" is slightly overstated for pre-persist rejections (Confidence: 7/10)
whatsapp_adapter.py:_prepare_outbound_media — oversized / non-HTTPS / unsupported-MIME degrade to a link because a share URL already exists. But quota (413), disk (507), and MIME-blocklist (400) raise inside create_share_from_bytes before a URL is minted, so the except HTTPException → continue path appends no fallback link — the file is dropped with only a WARN, invisible to the user. MIME-block dropping is correct (security); but for quota/disk the user gets the text with no indication a file was meant to attach. Consider appending a brief 📎 <name> — couldn't attach (storage limit) note in those two cases so it's not silent end-to-end. Low priority.

[I2] Sync persist in the async send path (Confidence: 6/10)
create_share_from_bytes is sync (open().write() + db.create_agent_shared_file()) and is called directly inside the async _prepare_outbound_media — it blocks the event loop during each file's disk+DB write. Not introduced here — it mirrors the existing async create_share, which also persists synchronously inline — so it's a pre-existing pattern, not a regression. Worth a future asyncio.to_thread wrap for both paths (files up to ~16 MB), but out of scope for this PR.

Summary

  • Critical: 0
  • Informational: 2 — neither blocks; I1 (quota/disk silent drop) is the one worth a quick look since the PR advertises "never silent drop".
  • Tests (27 fns) and docs are thorough; CI green.

Nice work. LGTM modulo the I1 wording/UX nit.

Automated /review (correctness · security · async/IO · scope). FILES-001 reuse + SSRF posture verified.

@dolho
dolho merged commit 3de821b into dev Jun 23, 2026
17 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