(function () { const chatHtml = `
An error occured in serving your query. please try again
" scrollToBottom(); return } const reader = response.body.getReader(); const decoder = new TextDecoder("utf-8"); let botAnswer = ""; while (true) { const { done, value } = await reader.read(); if (done) break; botAnswer += decoder.decode(value, { stream: true }); typingBubble.innerHTML = marked.parse(botAnswer); scrollToBottom(); } typingBubble.remove(); const botBubble = createBubble(marked.parse(botAnswer), "bot"); chatMessagesListContainer.appendChild(botBubble); scrollToBottom(); } // speech to text (voice input) let recognition; let isListening = false; if ("webkitSpeechRecognition" in window) { recognition = new webkitSpeechRecognition(); recognition.continuous = false; recognition.interimResults = false; recognition.lang = "en-US"; recognition.onstart = () => { isListening = true microphoneButton.classList.add("listening") } recognition.onend = () => { isListening = false; microphoneButton.classList.remove("listening") } recognition.onresult = (e) => { const transcript = e.results[0][0].transcript; textInput.value = transcript } } else { microphoneButton.disabled = true microphoneButton.title = "Speech recognition not supported in this browser" } microphoneButton.addEventListener("click", () => { if (!recognition) return if (!isListening) recognition.start() else recognition.stop() }) document.querySelectorAll(".suggestionsButton").forEach((suggestionBtn) => { suggestionBtn.addEventListener("click", () => { const question = suggestionBtn.innerHTML.trim() textInput.value = question sendMessage(question) }) }) } async function loadLibrary(src) { return new Promise((resolve, reject) => { const script = document.createElement('script'); script.src = src; script.onload = resolve; script.onerror = reject; document.head.appendChild(script); }); } loadLibrary("https://cdn.jsdelivr.net/npm/marked/marked.min.js"); document.addEventListener('DOMContentLoaded', () => { // load the tailwindcss library before injecting the widget to the dom // this is done to mitigate the case where initial UI renders without any styles (bad UX) loadLibrary("https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4").then(onDomContentLoaded) }); })()