| document.addEventListener('DOMContentLoaded', () => { |
| const chatbox = document.getElementById('chatbox'); |
| const userInput = document.getElementById('userInput'); |
| const sendBtn = document.getElementById('sendBtn'); |
| const clearMemoryBtn = document.getElementById('clearMemoryBtn'); |
|
|
| |
| function appendMessage(sender, text) { |
| const messageElement = document.createElement('div'); |
| messageElement.classList.add('message', `${sender}-message`); |
|
|
| const p = document.createElement('p'); |
| |
| p.textContent = text; |
|
|
| messageElement.appendChild(p); |
| chatbox.appendChild(messageElement); |
|
|
| |
| chatbox.scrollTop = chatbox.scrollHeight; |
| } |
|
|
| |
| async function sendMessage() { |
| const messageText = userInput.value.trim(); |
| if (messageText === "") { |
| return; |
| } |
|
|
| |
| appendMessage('user', messageText); |
|
|
| |
| userInput.value = ''; |
| userInput.disabled = true; |
| sendBtn.disabled = true; |
| if (clearMemoryBtn) clearMemoryBtn.disabled = true; |
|
|
|
|
| |
| const loadingElement = document.createElement('div'); |
| loadingElement.classList.add('message', 'assistant-message', 'loading-indicator'); |
| loadingElement.textContent = 'AI is thinking...'; |
| chatbox.appendChild(loadingElement); |
| chatbox.scrollTop = chatbox.scrollHeight; |
|
|
|
|
| try { |
| const response = await fetch('/chat', { |
| method: 'POST', |
| headers: { |
| 'Content-Type': 'application/json', |
| }, |
| body: JSON.stringify({ message: messageText }), |
| }); |
|
|
| |
| if (chatbox.contains(loadingElement)) { |
| chatbox.removeChild(loadingElement); |
| } |
|
|
|
|
| if (!response.ok) { |
| const errorData = await response.json().catch(() => ({})); |
| const errorMessage = errorData.response || `Server returned status ${response.status}: ${response.statusText}`; |
| throw new Error(`Chat request failed: ${errorMessage}`); |
| } |
|
|
| const data = await response.json(); |
| appendMessage('assistant', data.response); |
|
|
| } catch (error) { |
| console.error('Error sending message:', error); |
| |
| if (chatbox.contains(loadingElement)) { |
| chatbox.removeChild(loadingElement); |
| } |
| appendMessage('assistant', `Error: Could not get a response. ${error.message}`); |
| } finally { |
| |
| userInput.disabled = false; |
| sendBtn.disabled = false; |
| if (clearMemoryBtn) clearMemoryBtn.disabled = false; |
| userInput.focus(); |
| } |
| } |
|
|
| |
| async function clearMemory() { |
| if (confirm("Are you sure you want to clear the chat history? This will start a new conversation.")) { |
| |
| userInput.disabled = true; |
| sendBtn.disabled = true; |
| if (clearMemoryBtn) clearMemoryBtn.disabled = true; |
|
|
| try { |
| const response = await fetch('/clear_memory', { |
| method: 'POST', |
| }); |
|
|
| if (!response.ok) { |
| const errorData = await response.json().catch(() => ({})); |
| const errorMessage = errorData.status || `Server returned status ${response.status}: ${response.statusText}`; |
| throw new Error(`Clear memory request failed: ${errorMessage}`); |
| } |
|
|
| |
| chatbox.innerHTML = ''; |
| appendMessage('assistant', 'Chat history cleared. Starting a new conversation!'); |
| appendMessage('assistant', 'Hello! I\'m an AI chatbot with memory. How can I help you today?'); |
|
|
|
|
| } catch (error) { |
| console.error('Error clearing memory:', error); |
| appendMessage('assistant', `Error: Could not clear chat history. ${error.message}`); |
| } finally { |
| |
| userInput.disabled = false; |
| sendBtn.disabled = false; |
| if (clearMemoryBtn) clearMemoryBtn.disabled = false; |
| userInput.focus(); |
| } |
| } |
| } |
|
|
|
|
| |
| sendBtn.addEventListener('click', sendMessage); |
| userInput.addEventListener('keypress', function(event) { |
| if (event.key === 'Enter') { |
| event.preventDefault(); |
| sendMessage(); |
| } |
| }); |
|
|
| |
| if (clearMemoryBtn) { |
| clearMemoryBtn.addEventListener('click', clearMemory); |
| } |
|
|
| |
| }); |