TERN / assets /app.js
chris265100's picture
Update TERN package, release test questions with private SQL, and accept integer prediction IDs
028437a verified
Raw History Blame Contribute Delete
23.6 kB
/* Buildless UI. Email delivery is opt-in through submission-config.js. */
(() => {
"use strict";
const $ = (selector, root = document) => root.querySelector(selector);
const $$ = (selector, root = document) => [...root.querySelectorAll(selector)];
const escapeHTML = value => String(value).replace(/[&<>"']/g, character => ({"&":"&amp;", "<":"&lt;", ">":"&gt;", '"':"&quot;", "'":"&#39;"}[character]));
const leaderboard = window.TERN_LEADERBOARD;
const logo = '<svg viewBox="0 0 48 48" aria-hidden="true"><path d="M5 15C19 12 24 18 27 25C33 12 40 12 46 14C35 18 30 27 27 38C23 24 16 18 5 15Z" fill="currentColor"/><path d="M10 30C16 28 21 30 24 34" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round"/></svg>';
const brand = `<a class="brand" href="#overview" aria-label="TERN home">${logo}<span class="brand-name">TERN</span></a>`;
const links = [{id:"overview",name:"About",href:"#overview"},{id:"leaderboard",name:"Leaderboard",href:"#leaderboard"},{id:"dataset",name:"Dataset",href:"#dataset"},{id:"examples",name:"Examples",href:"#examples"}];
const header = $("[data-site-header]");
header.className = "site-header";
header.innerHTML = `<div class="container header-inner"><div class="brand-group">${brand}</div><button type="button" class="mobile-menu-button" aria-label="Open navigation" aria-expanded="false" aria-controls="site-navigation">☰</button><nav id="site-navigation" class="site-nav" aria-label="Main navigation">${links.map(link => `<a class="nav-link" href="${link.href}">${link.name}</a>`).join("")}<a class="nav-submit" href="#submit">Submission</a></nav></div>`;
const menuButton = $(".mobile-menu-button");
function closeMenu() {
$(".site-nav").classList.remove("is-open");
menuButton.setAttribute("aria-expanded", "false");
menuButton.setAttribute("aria-label", "Open navigation");
menuButton.textContent = "☰";
}
menuButton.addEventListener("click", () => {
const open = menuButton.getAttribute("aria-expanded") !== "true";
$(".site-nav").classList.toggle("is-open", open);
menuButton.setAttribute("aria-expanded", String(open));
menuButton.setAttribute("aria-label", open ? "Close navigation" : "Open navigation");
menuButton.textContent = open ? "×" : "☰";
});
document.addEventListener("keydown", event => {
if (event.key === "Escape" && menuButton.getAttribute("aria-expanded") === "true") {
closeMenu();
menuButton.focus();
}
});
document.addEventListener("click", event => {
if (!header.contains(event.target)) closeMenu();
});
matchMedia("(max-width: 640px)").addEventListener("change", closeMenu);
$$(".site-nav a").forEach(link => link.addEventListener("click", closeMenu));
const navSections = $$("[data-nav-section]");
const navLinks = $$(".site-nav a");
function updateActiveNav() {
const active = [...navSections].reverse().find(section => section.getBoundingClientRect().top <= 160) || navSections[0];
navLinks.forEach(link => {
const selected = link.hash === `#${active.id}`;
link.classList.toggle("is-active",selected);
if (selected) link.setAttribute("aria-current","location");
else link.removeAttribute("aria-current");
});
}
let scrollPending = false;
window.addEventListener("scroll",() => {
if (scrollPending) return;
scrollPending = true;
requestAnimationFrame(() => { updateActiveNav(); scrollPending = false; });
},{passive:true});
function revealHashTarget() {
let id;
try { id = decodeURIComponent(location.hash.slice(1)); } catch (_) { return; }
const target = document.getElementById(id);
if (!target) return;
let parent = target.parentElement;
let expanded = false;
while (parent) {
if (parent.tagName === "DETAILS" && !parent.open) { parent.open = true; expanded = true; }
parent = parent.parentElement;
}
if (expanded) requestAnimationFrame(() => target.scrollIntoView());
updateActiveNav();
}
window.addEventListener("hashchange",revealHashTarget);
revealHashTarget();
updateActiveNav();
const footer = $("[data-site-footer]");
footer.className = "site-footer";
footer.innerHTML = `<div class="container footer-inner"><span>TERN · Research preview</span><a href="#overview">Back to top ↑</a></div>`;
let toastTimer;
function toast(message) {
const element = $(".toast");
element.textContent = message;
element.hidden = false;
clearTimeout(toastTimer);
toastTimer = setTimeout(() => { element.hidden = true; }, 3500);
}
const sampleInput = {
id: "test_001",
question: "What was the average power of device_01 between 08:00 and 12:00 UTC on January 1, 2026?",
database: "root.energy",
schema: {root: "root.energy", devices: {device_01: {power: "FLOAT"}}},
timezone: "UTC"
};
const predictionExample = [{id:"test_001",predicted_sql:"SELECT AVG(power) FROM root.energy.device_01 WHERE time >= '2026-01-01T08:00:00Z' AND time < '2026-01-01T12:00:00Z'"}];
const copyText = {
"sample-input": JSON.stringify(sampleInput, null, 2),
predictions: JSON.stringify(predictionExample, null, 2),
citation: "% TERN benchmark citation placeholder.\n% Replace with the official BibTeX entry when the paper is released."
};
if ($("[data-sample-input]")) $("[data-sample-input]").textContent = copyText["sample-input"];
if ($("[data-prediction-example]")) $("[data-prediction-example]").textContent = copyText.predictions;
async function copyToClipboard(value) {
if (navigator.clipboard && window.isSecureContext) {
try { await navigator.clipboard.writeText(value); return; } catch (_) { /* File URL / permission fallback. */ }
}
const field = document.createElement("textarea");
field.value = value;
field.style.cssText = "position:fixed;left:-9999px;top:0;";
document.body.append(field);
field.select();
const copied = document.execCommand("copy");
field.remove();
if (!copied) throw new Error("Clipboard is unavailable in this browser.");
}
$$("[data-copy], [data-copy-source]").forEach(button => button.addEventListener("click", async () => {
try {
const source = button.dataset.copySource;
await copyToClipboard(source ? document.getElementById(source).textContent : copyText[button.dataset.copy]);
toast(button.dataset.copy === "citation" ? "Citation placeholder copied. Official citation coming at release." : "Copied to clipboard.");
} catch (_) { toast("Clipboard access is unavailable. You can select and copy the text directly."); }
}));
const scoreKeys = ["overall", "foundation", "standard", "analytical"];
const scoreLabels = {overall:"Overall EX", foundation:"Foundation EX", standard:"Standard EX", analytical:"Analytical EX"};
const methodLabel = entry => `${entry.name}${entry.setting ? ` (${entry.setting})` : ""}`;
const rankHTML = rank => `<span class="rank-number rank-${rank}">${String(rank).padStart(2,"0")}</span>`;
const settingHTML = setting => setting ? `<span class="setting-tag">${escapeHTML(setting)}</span>` : "";
const paperHTML = (paper, fullTitle = false) => paper ? `<a class="method-paper" href="${escapeHTML(paper.url)}" target="_blank" rel="noopener noreferrer" title="${escapeHTML(`${paper.kind}: ${paper.title}`)}" aria-label="Read ${escapeHTML(paper.title)}">${fullTitle ? escapeHTML(paper.title) : `[${escapeHTML(paper.label)}]`}</a>` : "";
if ($("[data-full-leaderboard]")) {
let sortKey = "overall";
let ascending = false;
const tabs = $$("[data-tier]");
const bestScores = Object.fromEntries(scoreKeys.map(key => [key, Math.max(...leaderboard.entries.map(entry => entry[key]))]));
const renderBoard = () => {
const search = $("#method-search").value.trim().toLowerCase();
const sorted = [...leaderboard.entries].sort((a,b) => (ascending ? 1 : -1) * (a[sortKey] - b[sortKey]) || a.id.localeCompare(b.id));
const ranks = new Map(sorted.map((entry,index) => [entry.id,index + 1]));
const filtered = sorted.filter(entry => {
const paper = leaderboard.papers[entry.paper];
const searchable = `${methodLabel(entry)} ${entry.category !== "nl2sql" ? "Baseline" : ""} ${paper?.label || ""} ${paper?.title || ""}`.toLowerCase();
return !search || searchable.includes(search);
});
$("[data-full-leaderboard]").innerHTML = filtered.map(entry => {
const paper = leaderboard.papers[entry.paper];
const metadata = entry.category !== "nl2sql" ? '<em class="method-baseline">Baseline</em>' : paperHTML(paper);
return `<tr><td>${rankHTML(ranks.get(entry.id))}</td><td><span class="method-title"><button type="button" data-method="${entry.id}" aria-label="View details for ${escapeHTML(methodLabel(entry))}">${escapeHTML(entry.name)}</button>${settingHTML(entry.setting)}</span>${metadata ? `<span class="method-inline-meta">${metadata}</span>` : ""}</td>${scoreKeys.map(key => `<td class="numeric ${key === sortKey ? "score-primary" : ""} ${entry[key] === bestScores[key] ? "score-best" : ""}">${entry[key].toFixed(2)}</td>`).join("")}<td><button type="button" class="details-button" data-method="${entry.id}" aria-label="View method details for ${escapeHTML(methodLabel(entry))}">i</button></td></tr>`;
}).join("");
$("[data-empty-state]").hidden = filtered.length > 0;
$("[data-result-count]").textContent = `Showing ${filtered.length} of ${leaderboard.entries.length} methods`;
$$("[data-sort-header]").forEach(th => {
const selected = th.dataset.sortHeader === sortKey;
if (selected) th.setAttribute("aria-sort",ascending ? "ascending" : "descending");
else th.removeAttribute("aria-sort");
$("span",th).textContent = selected ? (ascending ? "↑" : "↓") : "↕";
});
};
function selectTier(tab) {
tabs.forEach(button => {
const active = button === tab;
button.classList.toggle("is-active",active);
button.setAttribute("aria-selected",String(active));
button.tabIndex = active ? 0 : -1;
});
$("#board-panel").setAttribute("aria-labelledby",tab.id);
sortKey = tab.dataset.tier;
ascending = false;
renderBoard();
}
tabs.forEach((tab,index) => {
tab.addEventListener("click",() => selectTier(tab));
tab.addEventListener("keydown",event => {
let nextIndex;
if (event.key === "ArrowRight") nextIndex = (index + 1) % tabs.length;
if (event.key === "ArrowLeft") nextIndex = (index + tabs.length - 1) % tabs.length;
if (event.key === "Home") nextIndex = 0;
if (event.key === "End") nextIndex = tabs.length - 1;
if (nextIndex !== undefined) { event.preventDefault(); selectTier(tabs[nextIndex]); tabs[nextIndex].focus(); }
});
});
$$("[data-sort]").forEach(button => button.addEventListener("click",() => {
const key = button.dataset.sort;
const nextAscending = sortKey === key ? !ascending : false;
selectTier(tabs.find(tab => tab.dataset.tier === key));
ascending = nextAscending;
renderBoard();
}));
$("#method-search").addEventListener("input",renderBoard);
$("[data-reset-filters]").addEventListener("click",() => { $("#method-search").value = ""; renderBoard(); });
$("[data-full-leaderboard]").addEventListener("click",event => {
const button = event.target.closest("[data-method]");
if (!button) return;
const entry = leaderboard.entries.find(item => item.id === button.dataset.method);
const paper = leaderboard.papers[entry.paper];
$("[data-method-details]").innerHTML = `<div class="dialog-header"><div><div class="eyebrow">TERN BENCHMARK RESULT</div><h2 id="method-dialog-title">${escapeHTML(methodLabel(entry))}</h2></div><button type="button" data-close-dialog aria-label="Close method details">×</button></div><div class="dialog-scores">${scoreKeys.map(key => `<div><small>${scoreLabels[key]}</small><strong>${entry[key].toFixed(2)}</strong></div>`).join("")}</div><dl class="dialog-facts"><div><dt>Category</dt><dd>${escapeHTML(leaderboard.categories[entry.category])}</dd></div>${entry.setting ? `<div><dt>Setting</dt><dd>${entry.setting === "SFT" ? "Supervised fine-tuning (SFT)" : "In-context learning (ICL)"}</dd></div>` : ""}${paper ? `<div><dt>Paper</dt><dd>${paperHTML(paper,true)}</dd></div>` : ""}</dl>`;
$("[data-close-dialog]").addEventListener("click",() => $("#method-dialog").close());
$("#method-dialog").showModal();
});
$("#method-dialog").addEventListener("click",event => {
if (event.target === event.currentTarget) {
const rect = event.currentTarget.getBoundingClientRect();
if (event.clientX < rect.left || event.clientX > rect.right || event.clientY < rect.top || event.clientY > rect.bottom) event.currentTarget.close();
}
});
renderBoard();
}
if ($("#submission-form")) {
const form = $("#submission-form");
const result = $("#validation-result");
const message = $("#file-message");
const fileInput = $("#prediction-file");
const emailDelivery = window.TERN_EMAIL_SUBMISSION;
const sendButton = $("#send-submission");
let selectedDocument = null;
let selectedFilename = "";
let selectedFile = null;
let fileSequence = 0;
let downloadableSummary = null;
const fileLimit = emailDelivery.MAX_FILE_BYTES;
const hideResult = () => { result.hidden = true; downloadableSummary = null; };
form.addEventListener("input",event => { hideResult(); event.target.removeAttribute("aria-invalid"); });
form.addEventListener("change",hideResult);
function refreshDeliveryStatus() {
const availability = emailDelivery.getAvailability();
sendButton.disabled = !availability.ready;
$("#email-submission-notice").textContent = availability.message;
$("[data-email-submission-status]").textContent = availability.ready ? "Email submissions" : "Email submissions coming soon";
return availability;
}
refreshDeliveryStatus();
function fileMessage(text, error = false) {
message.textContent = text;
message.classList.toggle("is-error",error);
message.hidden = false;
}
function validateShape(data) {
const errors = [];
if (!Array.isArray(data) || !data.length) return ["The prediction file must be a non-empty JSON array of objects with id and predicted_sql."];
const ids = new Set();
data.forEach((entry,index) => {
const prefix = `Prediction ${index + 1}`;
if (!entry || typeof entry !== "object" || Array.isArray(entry)) { errors.push(`${prefix}: expected an object.`); return; }
const validId = (typeof entry.id === "string" && entry.id.trim() && entry.id.length <= 200)
|| (Number.isSafeInteger(entry.id) && entry.id > 0);
if (!validId) errors.push(`${prefix}: id must be a positive safe integer or a non-empty string of at most 200 characters.`);
else if (ids.has(String(entry.id))) errors.push(`${prefix}: duplicate id "${entry.id}".`);
else ids.add(String(entry.id));
if (typeof entry.predicted_sql !== "string" || !entry.predicted_sql.trim()) errors.push(`${prefix}: predicted_sql must be a non-empty string.`);
else if (entry.predicted_sql.length > 100000) errors.push(`${prefix}: SQL exceeds the 100,000-character preview limit.`);
for (const key of Object.keys(entry)) if (!["id","predicted_sql"].includes(key)) errors.push(`${prefix}: unknown field "${key}".`);
});
return errors;
}
async function readFile(file) {
const sequence = ++fileSequence;
hideResult();
selectedDocument = null;
selectedFilename = "";
selectedFile = null;
if (!file) return;
$("[data-file-label]").textContent = file.name;
if (file.size > fileLimit) { fileMessage("This file exceeds the 10 MB preview limit.",true); return; }
if (!/\.json$/i.test(file.name)) { fileMessage("Choose a .json prediction file.",true); return; }
try {
const text = await file.text();
if (sequence !== fileSequence) return;
const data = JSON.parse(text.replace(/^\uFEFF/,""));
selectedDocument = data;
selectedFilename = file.name;
selectedFile = file;
const count = Array.isArray(data) ? data.length : 0;
fileMessage(`${file.name} loaded · ${count} prediction${count === 1 ? "" : "s"} · ${(file.size / 1024).toFixed(1)} KB. Ready for local validation.`);
} catch (_) {
if (sequence === fileSequence) fileMessage("This file could not be read as JSON. Check its syntax and try again.",true);
}
}
fileInput.addEventListener("change",() => readFile(fileInput.files[0]));
const dropzone = $("#prediction-dropzone");
["dragenter","dragover"].forEach(type => dropzone.addEventListener(type,event => { event.preventDefault(); dropzone.classList.add("is-dragging"); }));
["dragleave","drop"].forEach(type => dropzone.addEventListener(type,event => { event.preventDefault(); dropzone.classList.remove("is-dragging"); }));
dropzone.addEventListener("drop",event => {
if (event.dataTransfer.files.length !== 1) {
fileSequence++;
selectedDocument = null;
selectedFilename = "";
selectedFile = null;
fileInput.value = "";
hideResult();
$("[data-file-label]").textContent = "Drop your predictions.json here";
fileMessage("Choose one JSON prediction file at a time.",true);
return;
}
fileInput.value = "";
readFile(event.dataTransfer.files[0]);
});
function validateSubmission() {
const errors = [];
let firstInvalid = null;
["method-name","team-name"].forEach(id => {
const field = $(`#${id}`);
const valid = Boolean(field.value.trim());
field.setAttribute("aria-invalid",String(!valid));
if (!valid) { errors.push(`${({"method-name":"Method name","team-name":"Team / Organization"})[id]} is required.`); firstInvalid ||= field; }
});
const contactField = $("#contact-email");
const contactValid = Boolean(contactField.value.trim()) && contactField.validity.valid;
contactField.setAttribute("aria-invalid", String(!contactValid));
if (!contactValid) { errors.push("A valid contact email is required for submission feedback."); firstInvalid ||= contactField; }
const urlField = $("#resource-url");
const url = urlField.value.trim();
if (url) {
let valid = false;
try { valid = ["http:","https:"].includes(new URL(url).protocol); } catch (_) { /* Invalid URL. */ }
urlField.setAttribute("aria-invalid",String(!valid));
if (!valid) { errors.push("Paper / Code URL must be a valid http:// or https:// address."); firstInvalid ||= urlField; }
}
if (!$("#submission-agreement").checked) errors.push("Confirm the prediction declaration before validation.");
if (!selectedDocument) errors.push("Add a valid JSON prediction file.");
else errors.push(...validateShape(selectedDocument));
result.hidden = false;
result.classList.toggle("is-error",errors.length > 0);
if (errors.length) {
downloadableSummary = null;
result.innerHTML = `<h3>Please review your submission</h3><ul>${errors.slice(0,10).map(error => `<li>${escapeHTML(error)}</li>`).join("")}</ul>${errors.length > 10 ? `<p>And ${errors.length - 10} more issue(s). Fix the file and validate again.</p>` : ""}<p>No file has been uploaded.</p>`;
if (firstInvalid) firstInvalid.focus();
else result.scrollIntoView({behavior:"smooth",block:"nearest"});
return null;
}
const values = new FormData(form);
downloadableSummary ||= {
submission_id: emailDelivery.createSubmissionId(),
submitted_at_utc: new Date().toISOString(),
submission_type: "leaderboard_review",
method: values.get("method").trim(), team: values.get("team").trim(), model: values.get("model").trim() || null,
contact_email: contactField.value.trim(),
setting: values.get("setting") || null, resource_url: url || null,
filename: selectedFilename, prediction_count: selectedDocument.length,
local_format_check: "passed", official_evaluation: "not_performed"
};
result.innerHTML = `<div class="eyebrow">LOCAL CHECK COMPLETE</div><h3 style="margin-top:8px">Your file structure looks good.</h3><div class="validation-stats"><span>${selectedDocument.length} prediction${selectedDocument.length === 1 ? "" : "s"}</span><span>Unique IDs</span><span>Required fields</span></div><p><strong>${escapeHTML(downloadableSummary.method)}</strong> · ${escapeHTML(downloadableSummary.team)}</p><p>SQL syntax, semantics, and question coverage have not been verified. No file has been sent and no EX score was created.</p><button type="button" class="button button-outline" data-download-summary>Download submission summary <span aria-hidden="true">↓</span></button>`;
$("[data-download-summary]").addEventListener("click",() => downloadJSON(downloadableSummary,"tern-submission-summary.json"));
result.scrollIntoView({behavior:"smooth",block:"nearest"});
return downloadableSummary;
}
$("#validate-submission").addEventListener("click",validateSubmission);
form.addEventListener("submit",event => {
event.preventDefault();
const metadata = validateSubmission();
if (!metadata) return;
try {
const availability = refreshDeliveryStatus();
if (!availability.ready) throw new Error(availability.message);
emailDelivery.send(metadata, selectedFile);
result.innerHTML = `<div class="eyebrow">EMAIL SUBMISSION</div><h3 style="margin-top:8px">Finish submission on the email service page.</h3><p>Submission ID: <strong>${escapeHTML(metadata.submission_id)}</strong></p><p>Complete any confirmation on the page opened by the email service. This page does not confirm email delivery. The organizers will review your submission before leaderboard inclusion; no online EX score was generated.</p><button type="button" class="button button-outline" data-reopen-delivery>Open submission page again</button>`;
$("[data-reopen-delivery]").addEventListener("click",() => form.requestSubmit(sendButton));
} catch (error) {
result.classList.add("is-error");
result.innerHTML = `<h3>Submission has not been confirmed</h3><p>${escapeHTML(error.message || "Please try again.")}</p><p>Your file remains available for local validation.</p>`;
}
result.scrollIntoView({behavior:"smooth",block:"nearest"});
});
}
function downloadJSON(data, filename) {
const url = URL.createObjectURL(new Blob([JSON.stringify(data,null,2) + "\n"],{type:"application/json"}));
const anchor = document.createElement("a");
anchor.href = url;
anchor.download = filename;
document.body.append(anchor);
anchor.click();
anchor.remove();
setTimeout(() => URL.revokeObjectURL(url),1000);
}
})();