mirror of
https://github.com/dograh-hq/dograh.git
synced 2026-07-04 10:52:17 +02:00
feat: add agent lifecycle events in widget
This commit is contained in:
parent
6a473a9db5
commit
df1b3a5b9b
1 changed files with 30 additions and 0 deletions
|
|
@ -29,6 +29,7 @@
|
||||||
connectionStatus: 'idle', // idle, connecting, connected, failed
|
connectionStatus: 'idle', // idle, connecting, connected, failed
|
||||||
audioElement: null,
|
audioElement: null,
|
||||||
turnCredentials: null, // TURN server credentials
|
turnCredentials: null, // TURN server credentials
|
||||||
|
callStartedAt: null, // Timestamp when call connected (for duration tracking)
|
||||||
callbacks: {
|
callbacks: {
|
||||||
onReady: null,
|
onReady: null,
|
||||||
onCallStart: null,
|
onCallStart: null,
|
||||||
|
|
@ -761,6 +762,8 @@
|
||||||
|
|
||||||
if (state.pc.iceConnectionState === 'connected' || state.pc.iceConnectionState === 'completed') {
|
if (state.pc.iceConnectionState === 'connected' || state.pc.iceConnectionState === 'completed') {
|
||||||
updateStatus('connected', 'Connected', 'Your voice call is now active');
|
updateStatus('connected', 'Connected', 'Your voice call is now active');
|
||||||
|
state.callStartedAt = Date.now();
|
||||||
|
emitMessage('dograh:call_started', {});
|
||||||
} else if (state.pc.iceConnectionState === 'failed' || state.pc.iceConnectionState === 'disconnected') {
|
} else if (state.pc.iceConnectionState === 'failed' || state.pc.iceConnectionState === 'disconnected') {
|
||||||
updateStatus('failed', 'Connection lost', 'The call has been disconnected');
|
updateStatus('failed', 'Connection lost', 'The call has been disconnected');
|
||||||
stopCall();
|
stopCall();
|
||||||
|
|
@ -893,6 +896,13 @@
|
||||||
* Stop voice call
|
* Stop voice call
|
||||||
*/
|
*/
|
||||||
function stopCall() {
|
function stopCall() {
|
||||||
|
// Emit end message before clearing state so identifiers are still available
|
||||||
|
const durationSeconds = state.callStartedAt
|
||||||
|
? Math.round((Date.now() - state.callStartedAt) / 1000)
|
||||||
|
: 0;
|
||||||
|
emitMessage('dograh:call_ended', { durationSeconds });
|
||||||
|
state.callStartedAt = null;
|
||||||
|
|
||||||
updateStatus('idle', 'Call ended', 'Click below to start a new call');
|
updateStatus('idle', 'Call ended', 'Click below to start a new call');
|
||||||
|
|
||||||
// Trigger call end callback
|
// Trigger call end callback
|
||||||
|
|
@ -932,6 +942,26 @@
|
||||||
setTimeout(() => startCall(), 500);
|
setTimeout(() => startCall(), 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Emit a postMessage event to the host window
|
||||||
|
* Allows the embedding website to listen for agent lifecycle events via:
|
||||||
|
* window.addEventListener('message', (event) => { ... })
|
||||||
|
*/
|
||||||
|
function emitMessage(eventType, detail) {
|
||||||
|
const containerId = state.config.embedMode === 'inline'
|
||||||
|
? state.config.containerId
|
||||||
|
: 'dograh-widget';
|
||||||
|
const message = {
|
||||||
|
type: eventType,
|
||||||
|
agentId: state.config.workflowId || null,
|
||||||
|
token: state.config.token || null,
|
||||||
|
workflowRunId: state.workflowRunId || null,
|
||||||
|
containerId: containerId,
|
||||||
|
...detail
|
||||||
|
};
|
||||||
|
window.postMessage(message, '*');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate unique peer ID
|
* Generate unique peer ID
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue