From ae8915632f4fc8e837ebfe6dfed0a9b66d6f607c Mon Sep 17 00:00:00 2001 From: etvt <149910696+etvt@users.noreply.github.com> Date: Fri, 22 Dec 2023 20:05:53 +0100 Subject: [PATCH] Fix default chat URL. Send chat message on ENTER. --- src/aitestdrive_ui/common/default_config.py | 2 +- src/aitestdrive_ui/resources/static/app.js | 31 +++++++++++++-------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/aitestdrive_ui/common/default_config.py b/src/aitestdrive_ui/common/default_config.py index bd12b8c..e322198 100644 --- a/src/aitestdrive_ui/common/default_config.py +++ b/src/aitestdrive_ui/common/default_config.py @@ -1,3 +1,3 @@ DEFAULT_PORT = 80 -DEFAULT_CHAT_API_URL = "/api/chat" +DEFAULT_CHAT_API_URL = "/api/chat/" diff --git a/src/aitestdrive_ui/resources/static/app.js b/src/aitestdrive_ui/resources/static/app.js index 642f0f9..088ba9d 100644 --- a/src/aitestdrive_ui/resources/static/app.js +++ b/src/aitestdrive_ui/resources/static/app.js @@ -1,24 +1,33 @@ let chatHistory = []; -document.getElementById('sendButton').addEventListener('click', () => { +document.getElementById('sendButton').addEventListener('click', sendMessage); + +document.getElementById('chatInput').addEventListener('keypress', function (event) { + if (event.key === 'Enter') { + event.preventDefault(); // Prevent the default action to avoid form submission + sendMessage(); + } +}); + +function sendMessage() { const input = document.getElementById('chatInput'); const message = input.value; - chatHistory.push({ content: message, role: 'user' }); + chatHistory.push({content: message, role: 'user'}); fetch(chat_api_url, { method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ history: chatHistory }) + headers: {'Content-Type': 'application/json'}, + body: JSON.stringify({history: chatHistory}) }) - .then(response => response.json()) - .then(data => { - chatHistory.push({ content: data.content, role: data.role }); - updateChatHistoryDisplay(); - }) - .catch(error => console.error('Error:', error)); + .then(response => response.json()) + .then(data => { + chatHistory.push({content: data.content, role: data.role}); + updateChatHistoryDisplay(); + }) + .catch(error => console.error('Error:', error)); input.value = ''; -}); +} function updateChatHistoryDisplay() { const historyDiv = document.getElementById('chatHistory');