Flask / static /script.js
UntilDot's picture
Upload 10 files
a84172d verified
raw
history blame
2.45 kB
// === Theme Toggle ===
const themeToggle = document.getElementById("themeToggle");
const toggleConfig = document.getElementById("toggleConfig");
const configPanel = document.getElementById("configPanel");
const html = document.documentElement;
function setInitialTheme() {
const savedTheme = localStorage.getItem("theme");
if (savedTheme === "dark") {
html.classList.add("dark");
} else if (savedTheme === "light") {
html.classList.remove("dark");
} else {
const prefersDark = window.matchMedia(
"(prefers-color-scheme: dark)",
).matches;
if (prefersDark) html.classList.add("dark");
else html.classList.remove("dark");
}
}
setInitialTheme();
themeToggle.addEventListener("click", () => {
const isDark = html.classList.toggle("dark");
localStorage.setItem("theme", isDark ? "dark" : "light");
});
toggleConfig.addEventListener("click", () => {
configPanel.classList.toggle("hidden");
});
// === Chat Handling ===
const chatForm = document.getElementById("chatForm");
const userInput = document.getElementById("userInput");
const chatContainer = document.getElementById("chatContainer");
const modelA = document.getElementById("modelA");
const modelB = document.getElementById("modelB");
const modelC = document.getElementById("modelC");
function appendMessage(role, text) {
const div = document.createElement("div");
div.className = `p-3 rounded shadow max-w-2xl ${role === "user" ? "bg-blue text-fg0 self-end" : "bg-green text-fg0 self-start"}`;
div.innerText = text;
chatContainer.appendChild(div);
chatContainer.scrollTop = chatContainer.scrollHeight;
}
chatForm.addEventListener("submit", async (e) => {
e.preventDefault();
const prompt = userInput.value.trim();
if (!prompt) return;
appendMessage("user", prompt);
userInput.value = "";
appendMessage("bot", "Thinking...");
const settings = {
models: {
"LLM-A": modelA.value,
"LLM-B": modelB.value,
"LLM-C": modelC.value,
},
};
const response = await fetch("/chat", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ prompt, settings }),
});
chatContainer.lastChild.remove(); // remove 'Thinking...'
if (response.ok) {
const data = await response.json();
appendMessage("bot", data.response);
} else {
appendMessage(
"bot",
"An error occurred. Please check your model selections.",
);
}
});