// JurisAI — Thread State Management let conversationHistory = []; async function init() { try { const res = await fetch("/api/info"); const data = await res.json(); // Populate runtime models const modelSelect = document.getElementById("modelSelect"); modelSelect.innerHTML = ""; for (const [name, id] of Object.entries(data.models)) { const opt = document.createElement("option"); opt.value = id; opt.textContent = name; if (id === data.default_model) opt.selected = true; modelSelect.appendChild(opt); } // Populate curriculum chapters const chSelect = document.getElementById("chapterSelect"); chSelect.innerHTML = ""; data.chapters.forEach(ch => { const opt = document.createElement("option"); opt.value = ch; opt.textContent = ch === "All" ? "All 7 Acts & Chapters" : ch; chSelect.appendChild(opt); }); document.getElementById("chunkCount").textContent = data.total_chunks.toLocaleString(); } catch (e) { console.error("Failed to load initial info:", e); } // Auto-resize textarea const textarea = document.getElementById("queryInput"); textarea.addEventListener("input", () => { textarea.style.height = "auto"; textarea.style.height = Math.min(textarea.scrollHeight, 140) + "px"; }); } function setStatus(text, active = false) { const indicator = document.getElementById("statusIndicator"); const dot = indicator.querySelector(".aui-status-dot"); const label = indicator.querySelector("span:last-child"); label.textContent = text; dot.style.background = active ? "#C9A96E" : "#4CAF72"; } function handleKeyDown(e) { if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); handleFormSubmit(e); } } function sendPreset(text) { const input = document.getElementById("queryInput"); input.value = text; handleFormSubmit(); } function clearChat() { conversationHistory = []; document.getElementById("messagesStream").innerHTML = ""; const hero = document.getElementById("welcomeHero"); hero.style.display = "flex"; setStatus("Ready"); } async function handleFormSubmit(e) { if (e) e.preventDefault(); const input = document.getElementById("queryInput"); const query = input.value.trim(); if (!query) return; input.value = ""; input.style.height = "auto"; document.getElementById("welcomeHero").style.display = "none"; appendMessage("user", query); const assistantRowId = "asst-" + Date.now(); appendLoadingAssistant(assistantRowId); const model = document.getElementById("modelSelect").value; const chapter = document.getElementById("chapterSelect").value; const sendBtn = document.getElementById("sendBtn"); sendBtn.disabled = true; setStatus("Reasoning…", true); // Scroll to bottom const viewport = document.getElementById("chatViewport"); viewport.scrollTop = viewport.scrollHeight; try { const res = await fetch("/api/chat", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ query, history: conversationHistory, model, chapter_filter: chapter, top_k: 5 }) }); const data = await res.json(); updateAssistantMessage(assistantRowId, data); conversationHistory.push({ role: "user", content: query }); conversationHistory.push({ role: "assistant", content: data.answer || "" }); } catch (err) { updateAssistantError(assistantRowId, err.message); } finally { sendBtn.disabled = false; setStatus("Ready"); viewport.scrollTop = viewport.scrollHeight; } } function appendMessage(role, text) { const stream = document.getElementById("messagesStream"); const row = document.createElement("div"); row.className = `aui-message-row aui-message-${role}`; row.innerHTML = `