Commit 631e60c
fix: handle agent-initiated disconnect and guard handleEndCall (#25)
* diag: instrument ElevenLabs callbacks to capture disconnect reason
Add diagnostic logging to understand why the WebSocket closes
immediately after the agent starts speaking:
- onDisconnect now logs reason ("error"|"agent"|"user"), closeCode,
closeReason, and uptime-since-connect so we can see the root cause
- onError now logs the full message + context object (not just a
string cast) — the context carries the raw Event/closeCode
- onModeChange logs a timestamp so we can measure the speaking→dead
delta precisely
- connectTimeRef tracks the connect timestamp for uptime calculation
No behaviour changes — diagnostics only. The actual fix will follow
once we have real disconnect-reason data from a reproduction.
https://claude.ai/code/session_01SGdxNUC1TVMDtW73TZbxjW
* fix: prevent double endSession causing WebSocket already-closing error
When the user clicks End Call / Back:
1. handleEndCall calls conversation.endSession() — socket enters CLOSING state
2. onBack() triggers setMode('select') — UserPhoneInterface unmounts
3. The unmount cleanup fired endSession() again on the already-closed socket
→ "WebSocket is already in CLOSING or CLOSED state"
Guard the cleanup to only call endSession when status !== 'disconnected'.
https://claude.ai/code/session_01SGdxNUC1TVMDtW73TZbxjW
* fix: handle agent-initiated disconnect and guard handleEndCall
Two related issues:
1. When the ElevenLabs agent closes the call (reason: "agent", e.g. says
"thank you for calling" then hangs up), the WebSocket is already CLOSED.
If the user then clicks End Call, conversation.endSession() threw
"WebSocket is already in CLOSING or CLOSED state".
Fix: guard handleEndCall to skip endSession() if status === 'disconnected'.
2. When the agent closes the call, onDisconnect fired but the call session
was never cleaned up — the UI stayed in the "active call" state with no
way to restart. Fix: call endCall() in onDisconnect for reason !== 'user'.
https://claude.ai/code/session_01SGdxNUC1TVMDtW73TZbxjW
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent c438a42 commit 631e60c
1 file changed
+14
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
76 | 76 | | |
77 | 77 | | |
78 | 78 | | |
79 | | - | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
80 | 86 | | |
81 | 87 | | |
82 | 88 | | |
| |||
198 | 204 | | |
199 | 205 | | |
200 | 206 | | |
201 | | - | |
202 | | - | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
203 | 214 | | |
204 | 215 | | |
205 | 216 | | |
| |||
0 commit comments