Changed around line 1
+ document.addEventListener('DOMContentLoaded', () => {
+ const startBtn = document.getElementById('startBtn');
+ const statusText = document.getElementById('statusText');
+ const conversation = document.getElementById('conversation');
+ const pulseRing = document.querySelector('.pulse-ring');
+
+ let recognition = null;
+ let synthesis = window.speechSynthesis;
+
+ // Initialize speech recognition
+ if ('webkitSpeechRecognition' in window) {
+ recognition = new webkitSpeechRecognition();
+ recognition.continuous = false;
+ recognition.interimResults = false;
+ recognition.lang = 'en-US';
+ } else {
+ startBtn.disabled = true;
+ statusText.textContent = 'Speech recognition not supported';
+ }
+
+ function addMessage(text, isUser = true) {
+ const messageDiv = document.createElement('div');
+ messageDiv.className = `message ${isUser ? 'user-message' : 'ai-message'}`;
+ messageDiv.textContent = text;
+ conversation.appendChild(messageDiv);
+ conversation.scrollTop = conversation.scrollHeight;
+ }
+
+ async function processQuery(query) {
+ // Simulate RAG + LLM processing
+ // Replace this with actual API calls to your backend
+ const response = await new Promise(resolve => {
+ setTimeout(() => {
+ resolve(`I understood your question: "${query}". This is a simulated response from the LLM system.`);
+ }, 1000);
+ });
+
+ return response;
+ }
+
+ function speak(text) {
+ const utterance = new SpeechSynthesisUtterance(text);
+ synthesis.speak(utterance);
+ }
+
+ let isListening = false;
+
+ startBtn.addEventListener('click', () => {
+ if (!recognition) return;
+
+ if (!isListening) {
+ recognition.start();
+ startBtn.querySelector('.label').textContent = 'Stop Listening';
+ statusText.textContent = 'Listening...';
+ pulseRing.classList.add('active');
+ isListening = true;
+ } else {
+ recognition.stop();
+ startBtn.querySelector('.label').textContent = 'Start Listening';
+ statusText.textContent = 'Ready to listen...';
+ pulseRing.classList.remove('active');
+ isListening = false;
+ }
+ });
+
+ if (recognition) {
+ recognition.onresult = async (event) => {
+ const query = event.results[0][0].transcript;
+ addMessage(query, true);
+
+ // Process the query
+ const response = await processQuery(query);
+ addMessage(response, false);
+
+ // Speak the response
+ speak(response);
+
+ // Reset the button state
+ isListening = false;
+ startBtn.querySelector('.label').textContent = 'Start Listening';
+ statusText.textContent = 'Ready to listen...';
+ pulseRing.classList.remove('active');
+ };
+
+ recognition.onerror = (event) => {
+ statusText.textContent = `Error: ${event.error}`;
+ pulseRing.classList.remove('active');
+ isListening = false;
+ startBtn.querySelector('.label').textContent = 'Start Listening';
+ };
+ }
+ });