From a82f87203b36519274b4058904a6c204cd8655fc Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Wed, 6 May 2026 16:01:21 +0530 Subject: [PATCH] feat: call callbacks at the right time --- ui/public/embed/dograh-widget.js | 34 +++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/ui/public/embed/dograh-widget.js b/ui/public/embed/dograh-widget.js index d6b5bc3..f7c8753 100644 --- a/ui/public/embed/dograh-widget.js +++ b/ui/public/embed/dograh-widget.js @@ -627,11 +627,6 @@ async function startCall() { updateStatus('connecting', 'Connecting...', 'Please wait while we establish the connection'); - // Trigger call start callback - if (state.callbacks.onCallStart) { - state.callbacks.onCallStart(); - } - try { // Initialize session if using embed token if (state.config.token) { @@ -785,9 +780,19 @@ console.log('ICE connection state:', state.pc.iceConnectionState); if (state.pc.iceConnectionState === 'connected' || state.pc.iceConnectionState === 'completed') { + const wasAlreadyConnected = state.callStartedAt !== null; updateStatus('connected', 'Connected', 'Your voice call is now active'); - state.callStartedAt = Date.now(); - emitMessage('dograh:call_started', {}); + if (!wasAlreadyConnected) { + state.callStartedAt = Date.now(); + emitMessage('dograh:call_started', {}); + if (state.callbacks.onCallStart) { + state.callbacks.onCallStart({ + agentId: state.config.workflowId || null, + token: state.config.token || null, + workflowRunId: state.workflowRunId || null + }); + } + } } else if (state.pc.iceConnectionState === 'failed' || state.pc.iceConnectionState === 'disconnected') { updateStatus('failed', 'Connection lost', 'The call has been disconnected'); stopCall(); @@ -925,15 +930,20 @@ ? Math.round((Date.now() - state.callStartedAt) / 1000) : 0; emitMessage('dograh:call_ended', { durationSeconds }); + + // Trigger call end callback with the same identifiers before we clear them + if (state.callbacks.onCallEnd) { + state.callbacks.onCallEnd({ + agentId: state.config.workflowId || null, + token: state.config.token || null, + workflowRunId: state.workflowRunId || null, + durationSeconds + }); + } state.callStartedAt = null; updateStatus('idle', 'Call ended', 'Click below to start a new call'); - // Trigger call end callback - if (state.callbacks.onCallEnd) { - state.callbacks.onCallEnd(); - } - // Close WebSocket if (state.ws) { state.ws.close();