diff --git a/public/js/chat.js b/public/js/chat.js index 8e09e8e..395917a 100644 --- a/public/js/chat.js +++ b/public/js/chat.js @@ -1,207 +1,201 @@ - - + // Handles key down event to send message on Enter + function handleKeyDown(event) { + if (event.key === 'Enter' && !event.shiftKey) { + event.preventDefault(); + sendMessage(); + } +} - - - - - +// Sends a message to the chat API +async function sendMessage() { + const messageInput = document.getElementById('messageInput'); + let message = messageInput.value.trim(); - Chat with AI - <%= process.env.OWNER_NAME %> - - - - - - - - - - - - - - - + if (message === '') return; - + // Encode the message to avoid XSS attacks + message = he.encode(message); - - + // Display the user's message in the chat + displayMessage(message, 'user'); + messageInput.value = ''; // Clear the input + toggleLoading(true); // Show loading indicator - -
-
-

Chat with <%= process.env.OWNER_NAME %>'s AI

-

Ask any question, and the AI will respond to you!

+ try { + const response = await fetch('https://infer.x64.world/chat', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ message: message }) + }); - -
-
+ if (response.ok) { + const data = await response.json(); + displayMessage(data.content, 'assistant'); + } else { + handleErrorResponse(response.status); + } + } catch (error) { + displayMessage('Error: ' + error.message, 'assistant'); + } finally { + toggleLoading(false); // Hide loading indicator + } +} - - +// Toggles the loading indicator +function toggleLoading(show) { + const loadingElement = document.getElementById('loading'); + loadingElement.style.display = show ? 'block' : 'none'; +} - - -
+// Displays a message in the chat window +function displayMessage(content, sender) { + const messages = document.getElementById('messages'); + const messageElement = document.createElement('div'); + messageElement.classList.add('message', sender); - - - + // Decode HTML entities and render Markdown + const decodedContent = he.decode(content); + const htmlContent = marked(decodedContent); + messageElement.innerHTML = htmlContent; -
-
+ messages.appendChild(messageElement); + messages.scrollTop = messages.scrollHeight; // Scroll to the bottom of the chat - - + // Highlight code blocks if any + document.querySelectorAll('pre code').forEach((block) => { + hljs.highlightElement(block); + if (sender === 'assistant') { + addCopyButton(block); // Add copy button to code blocks + } + }); - - - - - - + // Add "Copy Full Response" button after each assistant response + if (sender === 'assistant') { + addCopyFullResponseButton(messages, messageElement); + } +} - - - - - - +// Resets the chat on page load without displaying the success message +document.addEventListener('DOMContentLoaded', function () { + resetChat(false); +}); \ No newline at end of file diff --git a/views/chat.ejs b/views/chat.ejs index e6dff95..4041c51 100644 --- a/views/chat.ejs +++ b/views/chat.ejs @@ -92,210 +92,7 @@ - - + \ No newline at end of file