Skip to content

Commit

Permalink
Fix default chat URL. Send chat message on ENTER.
Browse files Browse the repository at this point in the history
  • Loading branch information
etvt committed Dec 22, 2023
1 parent e451aa0 commit ae89156
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/aitestdrive_ui/common/default_config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
DEFAULT_PORT = 80

DEFAULT_CHAT_API_URL = "/api/chat"
DEFAULT_CHAT_API_URL = "/api/chat/"
31 changes: 20 additions & 11 deletions src/aitestdrive_ui/resources/static/app.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down

0 comments on commit ae89156

Please sign in to comment.