const $ = (id) => document.getElementById(id); const form = $("search-form"); const query = $("query"); function showError(message) { $("error").textContent = message; $("error").hidden = !message; } async function post(path, body) { const res = await fetch(path, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), }); const data = await res.json().catch(() => ({})); if (!res.ok) { const detail = Array.isArray(data.detail) ? data.detail.map((d) => `${d.loc.at(-1)}: ${d.msg}`).join("; ") : data.detail || res.statusText; throw new Error(detail); } return data; } function sources(occurrences) { const shown = occurrences.slice(0, 3).map((o) => `${o.contract} #${o.position + 1}`); if (occurrences.length > 3) shown.push(`+${occurrences.length - 3} more`); return shown.join(", "); } function renderResults(data) { const box = $("results"); box.replaceChildren(); const summary = document.createElement("p"); summary.className = "summary"; summary.textContent = `Top ${data.results.length} by ${data.method}`; box.append(summary); if (!data.results.length) { const p = document.createElement("p"); p.className = "empty"; p.textContent = "No matching sentences."; box.append(p); return; } for (const hit of data.results) { const card = document.createElement("article"); card.className = "card"; card.innerHTML = ` ${hit.rank}
`; card.querySelector(".text").textContent = hit.text; card.querySelector(".source").textContent = `Source: ${sources(hit.occurrences)}`; card.title = hit.occurrences.map((o) => `${o.contract} #${o.position + 1}: ${o.original}`).join("\n"); box.append(card); } } async function runSearch() { const text = query.value.trim(); if (!text) { showError("Enter a sentence to search."); return; } showError(""); $("search-btn").disabled = true; try { renderResults(await post("/search", { query: text, k: Number($("k").value) })); } catch (err) { showError(`Search failed: ${err.message}`); } finally { $("search-btn").disabled = false; } } async function runClassify() { const text = query.value.trim(); if (!text) { showError("Enter a sentence to classify."); return; } showError(""); $("classify-btn").disabled = true; try { const r = await post("/classify", { text }); const box = $("classification"); box.className = `classification ${r.potentially_unfair ? "unfair" : "fair"}`; box.replaceChildren(); const strong = document.createElement("strong"); strong.textContent = r.potentially_unfair ? "Potentially unfair clause" : "Not flagged as potentially unfair"; const detail = document.createElement("span"); detail.textContent = ` (model score ${r.probability.toFixed(2)}, threshold ${r.threshold.toFixed(2)})`; const note = document.createElement("small"); note.textContent = r.note; box.append(strong, detail, note); box.hidden = false; } catch (err) { showError(`Classification failed: ${err.message}`); } finally { $("classify-btn").disabled = false; } } async function init() { try { const res = await fetch("/ready"); const info = await res.json(); if (!info.ready) throw new Error("not ready"); $("method").textContent = info.method; $("status").textContent = `${info.corpus_groups.toLocaleString()} unique sentences indexed`; $("classify-btn").hidden = !info.classifier; } catch { $("status").textContent = "Service is starting or unavailable — try again shortly."; } } form.addEventListener("submit", (e) => { e.preventDefault(); runSearch(); }); $("classify-btn").addEventListener("click", runClassify); query.addEventListener("keydown", (e) => { if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) { e.preventDefault(); runSearch(); } }); query.addEventListener("input", () => { $("classification").hidden = true; }); init();