// Attach the input event listener to the textarea for auto-resizing document.getElementById('user-input').addEventListener('input', autoResizeTextarea); async function sendPrompt() { const textarea = document.getElementById('user-input'); const userInput = textarea.value; if (userInput.trim() === "") return; textarea.value = ''; textarea.style.height = '55px'; textarea.focus(); appendUserMessage(userInput); // Add assistant typing animation before making the fetch request console.log("Adding assistant typing animation"); let assistantDiv = appendAssistantMessage(); // This function adds the typing dots console.log("Assistant typing animation added"); // Force a reflow/repaint to ensure the typing animation is visible await new Promise(resolve => requestAnimationFrame(resolve)); try { // Start the fetch request console.log("Sending fetch request..."); const response = await fetch('http://127.0.0.1:8000/send_prompt/', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ prompt: userInput }) }); console.log("Fetch request sent, awaiting response..."); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const reader = response.body.getReader(); const decoder = new TextDecoder(); // Read the streaming response and append it to the assistant message while (true) { const { done, value } = await reader.read(); if (done) break; const chunk = decoder.decode(value); // Remove typing animation (dots) and display the response text const typingAnimation = assistantDiv.querySelector('.typing-animation'); if (typingAnimation) { typingAnimation.remove(); // Remove the dots when response starts coming in } else { console.error("Typing animation element not found in assistantDiv"); } console.log("Response chunk received:", chunk); assistantDiv.querySelector('p').textContent += chunk; const chatWindow = document.getElementById('chat-container'); chatWindow.scrollTop = chatWindow.scrollHeight; // Auto-scroll to bottom } } catch (error) { console.error("Error fetching response:", error); assistantDiv.querySelector('p').textContent = "Error getting response. Please try again."; } } // Function to append user's outgoing message to the chat window function appendUserMessage(message) { const chatWindow = document.getElementById('chat-container'); const outgoingChat = document.createElement('div'); outgoingChat.classList.add('message-bubble', 'outgoing'); outgoingChat.innerHTML = `