veltrixcode's picture
Upload 17 files
3950291 verified
Raw History Blame Contribute Delete
7.58 kB
// 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 = `<div class="aui-bubble">${escapeHtml(text)}</div>`;
stream.appendChild(row);
const viewport = document.getElementById("chatViewport");
viewport.scrollTop = viewport.scrollHeight;
}
function appendLoadingAssistant(id) {
const stream = document.getElementById("messagesStream");
const row = document.createElement("div");
row.id = id;
row.className = "aui-message-row aui-message-assistant";
row.innerHTML = `
<div class="aui-thinking-pulse">
<div class="aui-shimmer-spark"></div>
<span>Searching statutory context…</span>
</div>
`;
stream.appendChild(row);
const viewport = document.getElementById("chatViewport");
viewport.scrollTop = viewport.scrollHeight;
}
function updateAssistantMessage(id, data) {
const row = document.getElementById(id);
if (!row) return;
let html = "";
// Collapsible thinking block
if (data.thinking && data.thinking.trim()) {
html += `
<details class="aui-thinking-box">
<summary class="aui-thinking-header">
<svg width="10" height="10" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5"><path d="M9 18l6-6-6-6"/></svg>
<span>Thought process (${data.thinking.length} chars)</span>
</summary>
<div class="aui-thinking-content">${escapeHtml(data.thinking)}</div>
</details>
`;
}
// Main answer
html += `<div class="aui-bubble">${marked.parse(data.answer || "")}</div>`;
// Citations
if (data.used_rag && data.retrieved_chunks && data.retrieved_chunks.length > 0) {
html += `
<div class="aui-citations-box">
<div class="aui-citations-header" onclick="this.nextElementSibling.classList.toggle('hidden')">
<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M4 19.5A2.5 2.5 0 016.5 17H20M4 4.5A2.5 2.5 0 016.5 2H20v20H6.5A2.5 2.5 0 014 19.5v-15z"/></svg>
<span>${data.retrieved_chunks.length} book excerpt${data.retrieved_chunks.length > 1 ? "s" : ""} used</span>
</div>
<div class="aui-citation-list">
`;
data.retrieved_chunks.forEach(src => {
html += `
<div class="aui-citation-card">
<div class="aui-citation-meta">
<span>${escapeHtml(src.title)} — p.${src.page_number}</span>
<span class="aui-citation-score">${src.retrieval_score}</span>
</div>
<div class="aui-citation-text">${escapeHtml(src.raw_text)}</div>
</div>
`;
});
html += `</div></div>`;
}
row.innerHTML = html;
}
function updateAssistantError(id, err) {
const row = document.getElementById(id);
if (!row) return;
row.innerHTML = `
<div class="aui-bubble" style="color:#d4756b; border-left: 2px solid #8b3a35; padding-left: 12px;">
<strong>Something went wrong.</strong> ${escapeHtml(err)}
</div>
`;
}
function escapeHtml(str) {
return (str || "")
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;");
}
document.addEventListener("DOMContentLoaded", init);