2024-08-12 18:23:42 +02:00
|
|
|
import { marked } from "https://cdn.jsdelivr.net/npm/marked/lib/marked.esm.js";
|
|
|
|
|
|
|
|
const promptInput = document.getElementById("userInput");
|
|
|
|
const chatContainer = document.getElementById("chatContainer");
|
|
|
|
const typingIndicator = document.getElementById("typingIndicator");
|
|
|
|
const sendButton = document.getElementById("sendButton");
|
|
|
|
|
2024-08-13 07:19:37 +02:00
|
|
|
const session_uuid = self.crypto.randomUUID();
|
|
|
|
|
2024-08-12 18:23:42 +02:00
|
|
|
sendButton.addEventListener("click", () => {
|
|
|
|
sendMessage();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2024-08-13 09:38:17 +02:00
|
|
|
promptInput.addEventListener("input", () => {
|
|
|
|
if (promptInput.value.split("\n").length == 1) {
|
|
|
|
promptInput.style.height = "2.5rem";
|
|
|
|
} else {
|
|
|
|
promptInput.style.height = "";
|
|
|
|
promptInput.style.height = promptInput.scrollHeight + "px";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
promptInput.addEventListener("keypress", () => {
|
|
|
|
handleKeyPress(event);
|
|
|
|
});
|
2024-08-12 18:23:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
async function sendMessage() {
|
|
|
|
const prompt = promptInput.value.trim();
|
|
|
|
if (!prompt) {
|
|
|
|
alert("Please enter a message.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
addMessage(prompt, 'user');
|
|
|
|
promptInput.value = "";
|
|
|
|
|
|
|
|
showTypingIndicator();
|
|
|
|
|
|
|
|
const generatedText = await generateText(prompt);
|
|
|
|
addMessage(generatedText, 'bot');
|
|
|
|
|
|
|
|
hideTypingIndicator();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function generateText(prompt) {
|
|
|
|
try {
|
2024-08-12 18:46:55 +02:00
|
|
|
const response = await fetch("./generate_text_stream", {
|
2024-08-12 18:23:42 +02:00
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
2024-08-13 07:19:37 +02:00
|
|
|
body: JSON.stringify({ session_uuid ,prompt }),
|
2024-08-12 18:23:42 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
console.error("Error:", response.statusText);
|
|
|
|
return "Error occurred while generating response.";
|
|
|
|
}
|
|
|
|
|
|
|
|
const reader = response.body.getReader();
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
let isFinished = false;
|
|
|
|
let generatedTextContent = "";
|
|
|
|
|
|
|
|
while (!isFinished) {
|
|
|
|
const { done, value } = await reader.read();
|
|
|
|
if (done) {
|
|
|
|
isFinished = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
generatedTextContent += decoder.decode(value, {stream: true});
|
|
|
|
}
|
|
|
|
|
|
|
|
return generatedTextContent;
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error:", error);
|
|
|
|
return "An error occurred.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function addMessage(rawText, type) {
|
|
|
|
const messageDiv = document.createElement("div");
|
|
|
|
messageDiv.className = `message ${type}`;
|
|
|
|
var text = marked.parse(rawText)
|
|
|
|
messageDiv.innerHTML = `<div class="message-bubble fadeIn">${text}</div>`;
|
|
|
|
chatContainer.appendChild(messageDiv);
|
|
|
|
|
|
|
|
chatContainer.scrollTop = chatContainer.scrollHeight;
|
|
|
|
|
|
|
|
hideTypingIndicator();
|
|
|
|
}
|
|
|
|
|
|
|
|
let typingTimeout;
|
|
|
|
|
|
|
|
function showTypingIndicator() {
|
|
|
|
clearTimeout(typingTimeout);
|
|
|
|
typingIndicator.style.display = "inline-block";
|
|
|
|
}
|
|
|
|
|
|
|
|
function hideTypingIndicator() {
|
|
|
|
typingTimeout = setTimeout(() => {
|
|
|
|
typingIndicator.style.display = "none";
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleKeyPress(event) {
|
2024-08-13 09:38:17 +02:00
|
|
|
if (event.key == "Enter" && !event.shiftKey) {
|
|
|
|
event.preventDefault();
|
2024-08-12 18:23:42 +02:00
|
|
|
sendMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-13 07:19:37 +02:00
|
|
|
window.onload = () => addMessage("This session is: " + session_uuid, 'bot');
|