lollms-webui/static/js/main.js

144 lines
4.9 KiB
JavaScript
Raw Normal View History

2023-04-13 10:31:48 +00:00
function update_main(){
const chatWindow = document.getElementById('chat-window');
const chatForm = document.getElementById('chat-form');
const userInput = document.getElementById('user-input');
2023-04-23 22:19:15 +00:00
const stopGeneration = document.getElementById("stop-generation")
stopGeneration.addEventListener('click', (event) =>{
event.preventDefault();
console.log("Stop clicked");
2023-04-23 22:19:15 +00:00
fetch('/stop_gen')
.then(response => response.json())
.then(data => {
console.log(data);
});
})
2023-04-13 20:44:26 +00:00
function submit_form(){
console.log("Submitting")
2023-04-13 10:31:48 +00:00
// get user input and clear input field
message = userInput.value;
userInput.value = '';
// add user message to chat window
const sendbtn = document.querySelector("#submit-input")
const waitAnimation = document.querySelector("#wait-animation")
const stopGeneration = document.querySelector("#stop-generation")
sendbtn.style.display="none";
waitAnimation.style.display="block";
stopGeneration.style.display = "block";
console.log("Sending message to bot")
2023-04-16 11:32:14 +00:00
user_msg = addMessage('',message, 0, 0, can_edit=true);
bot_msg = addMessage('', '', 0, 0, can_edit=true);
2023-04-24 21:11:32 +00:00
// scroll to bottom of chat window
chatWindow.scrollTop = chatWindow.scrollHeight;
fetch('/generate', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ message })
}).then(function(response) {
const stream = new ReadableStream({
start(controller) {
const reader = response.body.getReader();
function push() {
reader.read().then(function(result) {
if (result.done) {
sendbtn.style.display="block";
waitAnimation.style.display="none";
stopGeneration.style.display = "none";
console.log(result)
controller.close();
return;
}
controller.enqueue(result.value);
push();
})
}
push();
}
});
const textDecoder = new TextDecoder();
const readableStreamDefaultReader = stream.getReader();
let entry_counter = 0
function readStream() {
readableStreamDefaultReader.read().then(function(result) {
if (result.done) {
return;
}
text = textDecoder.decode(result.value);
2023-04-17 21:38:08 +00:00
// The server will first send a json containing information about the message just sent
if(entry_counter==0)
{
// We parse it and
infos = JSON.parse(text);
2023-04-17 21:22:58 +00:00
user_msg.setSender(infos.user);
user_msg.setMessage(infos.message);
user_msg.setID(infos.id);
bot_msg.setSender(infos.bot);
bot_msg.setID(infos.response_id);
bot_msg.messageTextElement;
bot_msg.hiddenElement;
entry_counter ++;
}
else{
entry_counter ++;
prefix = "FINAL:";
if(text.startsWith(prefix)){
text = text.substring(prefix.length);
bot_msg.hiddenElement.innerHTML = text
bot_msg.messageTextElement.innerHTML = text
2023-04-13 10:31:48 +00:00
}
else{
// For the other enrtries, these are just the text of the chatbot
2023-04-24 20:09:40 +00:00
txt = bot_msg.hiddenElement.innerHTML;
txt += text
bot_msg.hiddenElement.innerHTML = txt;
bot_msg.messageTextElement.innerHTML = txt;
// scroll to bottom of chat window
chatWindow.scrollTop = chatWindow.scrollHeight;
2023-04-17 10:54:48 +00:00
2023-04-13 10:31:48 +00:00
}
}
readStream();
});
}
readStream();
});
}
chatForm.addEventListener('submit', event => {
event.preventDefault();
submit_form();
2023-04-13 20:44:26 +00:00
});
userInput.addEventListener("keyup", function(event) {
// Check if Enter key was pressed while holding Shift
// Also check if Shift + Ctrl keys were pressed while typing
// These combinations override the submit action
const shiftPressed = event.shiftKey;
const ctrlPressed = event.ctrlKey && !event.metaKey;
if ((!shiftPressed) && event.key === "Enter") {
submit_form();
}
// Restore original functionality for the remaining cases
else if (!shiftPressed && ctrlPressed) {
setTimeout(() => {
userInput.focus();
contentEditable.value += event.data;
lastValue.innerHTML = userInput.value;
}, 0);
}
});
2023-04-13 20:44:26 +00:00
}