Repository navigation
Description
Summary
Add real-time voice call support to Trinity agents over WhatsApp, using the WhatsApp Business Calling API (GA July 2025) routed through Twilio and bridged to the existing Gemini Live voice mode.
Context
WhatsApp Business Calling API became generally available in July 2025 via both Meta's Cloud API and Twilio Programmable Voice. Trinity already uses Twilio for WhatsApp messaging (WHATSAPP-001), making this a natural extension.
The existing browser-based Gemini voice mode (routers/voice.py, services/gemini_voice.py) already implements the full speech-to-speech pipeline including Gemini Live API integration and run_task tool calling to Claude agents. The WhatsApp call path reuses this pipeline with Twilio Media Streams as the audio I/O layer instead of the browser.
Architecture:
WhatsApp user → Twilio (inbound call) → Media Streams WebSocket
→ [µ-law 8kHz ↔ PCM 16kHz conversion]
→ Gemini Live API (existing gemini_voice.py)
→ run_task tool → Claude agent
→ TTS audio back through same path → WhatsApp user
The audio format difference (Twilio: µ-law 8kHz / Gemini: PCM 16kHz) is handled by Python stdlib audioop — no new dependencies.
Acceptance Criteria
- Inbound WhatsApp calls to a business number are answered by the bound Trinity agent
- Audio streams bidirectionally between caller and Gemini Live API with correct codec conversion (µ-law 8kHz ↔ PCM 16kHz/24kHz)
-
run_tasktool calling routes complex queries to the Claude agent during the call - Call transcripts are persisted as
ChatMessagerows in the database (same as browser voice mode) - Per-agent voice system prompt is applied to the call session
- TwiML webhook endpoint returns
<Connect><Stream>to initiate Media Streams - WhatsApp binding config (in
whatsapp_bindingstable) includes a flag to enable/disable voice calls - Graceful call termination on agent stop or Twilio disconnect
Technical Notes
Files to create/modify:
src/backend/routers/voice.py— add new/ws/whatsapp-call/{agent_name}WebSocket endpoint for Twilio Media Streamssrc/backend/services/gemini_voice.py— reuse as-is; extractGeminiVoiceSessionto accept generic audio I/O callbackssrc/backend/adapters/whatsapp_adapter.py— addsendVoice/ call control helperssrc/backend/adapters/transports/twilio_webhook.py— handlecallevent type, return TwiMLsrc/backend/db/— addvoice_enabledflag towhatsapp_bindings
External requirements:
- Meta Business Verification (for the WhatsApp Business account)
- Twilio Programmable Voice enabled on the account
- Messaging limit ≥ 2,000 BIC/24h (only affects outbound calls, not inbound)
References:
- Twilio WhatsApp Business Calling docs
- Meta Cloud API Calling
- Existing voice implementation:
src/backend/routers/voice.py,src/backend/services/gemini_voice.py - Existing WhatsApp adapter:
src/backend/adapters/whatsapp_adapter.py
Activity