fix(voice): align outbound WhatsApp call lifecycle with real pickup

A pile of related fixes around the dashboard's WhatsApp call flow:

- ConversationHeader / Contacts/VoiceCallButton: drop the immediate
  setCallActive at initiate time. The call sits in incomingCalls
  (callDirection: outbound) until the backend signals real pickup, so
  the duration timer never starts pre-pickup. Phone button is disabled
  whenever there is an active or incoming call.

- FloatingCallWidget:
  * Loop a ringtone (bell.mp3) for inbound ringing only.
  * Hide the green Join button for outbound — the agent has nothing to
    "join", and clicking it routed through acceptIncomingCall →
    prepareInboundAnswer → cleanup() and tore down the live outbound
    session before the API 409 ("already accepted by another agent").
  * Auto-join watcher skips whatsapp outbound (Twilio's joinConference
    flow only).

- useCallSession:
  * joinCall short-circuits for outbound calls — defense-in-depth so
    no future surface can re-trigger the destroyed-session bug.
  * endCall + outbound rejectIncomingCall pass call.callId to
    endActiveCall, so terminate fires even if module state was wiped.

- useWhatsappCallSession:
  * New recorderArmed flag, reset by cleanup. ontrack only calls
    setupRecorder when armed.
  * Inbound's acceptIncomingCall arms the recorder before the API
    round-trip (agent click = pickup).
  * armOutboundRecorder exported for the cable handler when ACCEPTED
    arrives.
  * endActiveCall accepts a callIdOverride to fall back when the
    module's activeCallId was nulled by an earlier cleanup.

- actionCable:
  * Split the cable contract: outbound_connected only applies the SDP
    answer (tunnel-up signal); outbound_accepted (new) is the real
    pickup signal — flips active and arms the recorder.
This commit is contained in:
Tanmay Deep Sharma
2026-05-05 14:15:40 +07:00
parent 4a79abb367
commit e6a35193f3
6 changed files with 156 additions and 44 deletions
@@ -106,8 +106,11 @@ export function useCallSession() {
const findCall = callSid => callsStore.calls.find(c => c.callSid === callSid);
const endCall = async ({ conversationId, inboxId, callSid }) => {
if (isWhatsappCall(findCall(callSid))) {
await whatsappSession.endActiveCall();
const call = findCall(callSid);
if (isWhatsappCall(call)) {
// Pass call.callId so a wiped module state (e.g. a prior accept attempt
// tore down the WebRTC session) doesn't stop us hitting /terminate.
await whatsappSession.endActiveCall(call.callId);
durationTimer.stop();
callsStore.clearActiveCall();
return;
@@ -122,9 +125,15 @@ export function useCallSession() {
const joinCall = async ({ conversationId, inboxId, callSid }) => {
if (isJoining.value) return null;
const call = findCall(callSid);
// Outbound calls were initiated by this agent — there is no inbound offer
// to accept and the WebRTC session is already mid-handshake. Routing
// through acceptIncomingCall would call prepareInboundAnswer → cleanup()
// and destroy the live outbound session, then 409 from the backend.
if (call?.callDirection === 'outbound') return null;
isJoining.value = true;
try {
const call = findCall(callSid);
if (isWhatsappCall(call)) {
await whatsappSession.acceptIncomingCall({
callId: call.callId,
@@ -174,7 +183,14 @@ export function useCallSession() {
const rejectIncomingCall = callSid => {
const call = findCall(callSid);
if (isWhatsappCall(call) && call?.callId) {
whatsappSession.rejectIncomingCall(call.callId);
// Outbound calls that are still ringing must be terminated, not rejected
// (reject is the inbound-side verb on Meta's API). Pass call.callId so
// a wiped module state still hits /terminate.
if (call.callDirection === 'outbound') {
whatsappSession.endActiveCall(call.callId);
} else {
whatsappSession.rejectIncomingCall(call.callId);
}
} else {
TwilioVoiceClient.endClientCall();
}