This commit is contained in:
Oliver
2025-07-29 12:14:20 -03:00
parent 95f1cb9a4c
commit ae00faa031

View File

@@ -1,48 +1,62 @@
console.log('INIT ELEVENLABS')
import { Conversation } from 'https://cdn.skypack.dev/@elevenlabs/client'; import { Conversation } from 'https://cdn.skypack.dev/@elevenlabs/client';
let conversation = null; let conversation = null;
let inCall = false;
console.log("INIT")
document.addEventListener('JSsucks', () => { document.addEventListener('JSsucks', () => {
console.log("JSsucks")
const callBtn = document.getElementById('callBtn');
callBtn.addEventListener('click', async () => {
console.log('Requesting microphone access...');
try { const callBtn = document.getElementById('callBtn');
await navigator.mediaDevices.getUserMedia({ audio: true });
} catch (error) {
console.log('Microphone access denied.');
return;
}
console.log('Connecting to agent...'); callBtn.addEventListener('click', async () => {
if (!inCall) {
console.log('Requesting microphone access...');
try { try {
await navigator.mediaDevices.getUserMedia({ audio: true });
} catch (error) {
console.log('Microphone access denied.');
return;
}
conversation = await Conversation.startSession({ console.log('Connecting to agent...');
agentId: 'agent_01jx2xm2w4exwvjhbq52js687f',
connectionType: 'websocket', // or "webrtc" try {
onConnect: () => { conversation = await Conversation.startSession({
console.log('✅ Connected to AI Agent!'); agentId: 'agent_01jx2xm2w4exwvjhbq52js687f',
}, connectionType: 'websocket',
onDisconnect: () => { onConnect: () => {
console.log('🔌 Disconnected.'); console.log('✅ Connected to AI Agent!');
}, callBtn.style.color = 'red';
onError: (err) => { inCall = true;
console.log(`❌ Error: ${err.message || err}`); },
}, onDisconnect: () => {
onMessage: (msg) => { console.log('🔌 Disconnected.');
console.log('Agent:', msg); callBtn.style.color = 'grey';
}, inCall = false;
}); },
} catch (err) { onError: (err) => {
console.log('Failed to start session.'); console.log(`❌ Error: ${err.message || err}`);
console.error(err); },
} onMessage: (msg) => {
}) console.log('Agent:', msg);
},
});
} catch (err) {
console.log('Failed to start session.');
console.error(err);
}
} else {
console.log('Hanging up...');
try {
await conversation.endSession();
conversation = null;
inCall = false;
callBtn.style.color = 'grey';
console.log('✅ Call ended.');
} catch (err) {
console.error('Error ending the call:', err);
}
}
});
}); });