Download app.js from timchen0618/phantom-wiki-100q-viewer: direct link, hf CLI and curl.
- Browser
- Download file 34.8 kB
-
https://huggingface.co/spaces/timchen0618/phantom-wiki-100q-viewer/resolve/main/app.js
- Command line
-
hf download hf://spaces/timchen0618/phantom-wiki-100q-viewer/app.js
-
curl -L -o app.js https://huggingface.co/spaces/timchen0618/phantom-wiki-100q-viewer/resolve/main/app.js
34.8 kB
| (function () { | |
| "use strict"; | |
| var $ = function (id) { return document.getElementById(id); }; | |
| var MODES = ["corpus", "eval", "structures", "runs", "compare"]; | |
| var MODE_KEY = "phantomWiki100q.mode"; | |
| var RECORD_CACHE_LIMIT = 18; | |
| var state = { | |
| manifest: null, | |
| mode: "corpus", | |
| qid: null, | |
| corpus: [], | |
| corpusView: [], | |
| corpusIndex: 0, | |
| docByTitle: {}, | |
| eval: [], | |
| evalView: [], | |
| evalIndex: 0, | |
| structuresIndex: null, | |
| structuresView: [], | |
| structuresIndexPosition: 0, | |
| structureCache: new Map(), | |
| runSlot: null, | |
| runViewMode: "result", | |
| runIndexes: {}, | |
| runViews: {}, | |
| runPositions: {}, | |
| runCache: new Map(), | |
| compareIndex: null, | |
| compareView: [], | |
| comparePosition: 0, | |
| compareCache: new Map(), | |
| }; | |
| function esc(value) { | |
| return String(value === null || value === undefined ? "" : value) | |
| .replace(/&/g, "&") | |
| .replace(/</g, "<") | |
| .replace(/>/g, ">") | |
| .replace(/"/g, """); | |
| } | |
| function short(value, limit) { | |
| value = String(value || "").replace(/\s+/g, " ").trim(); | |
| return value.length > limit ? value.slice(0, limit - 1) + "…" : value; | |
| } | |
| function pct(value, digits) { | |
| return (Number(value || 0) * 100).toFixed(digits === undefined ? 2 : digits) + "%"; | |
| } | |
| function number(value) { | |
| return typeof value === "number" ? value.toLocaleString() : "—"; | |
| } | |
| function money(value) { | |
| return typeof value === "number" ? "$" + value.toFixed(4) : "—"; | |
| } | |
| async function fetchJson(path) { | |
| var response = await fetch(path); | |
| if (!response.ok) throw new Error(path + " returned HTTP " + response.status); | |
| return response.json(); | |
| } | |
| function cacheGet(cache, key) { | |
| if (!cache.has(key)) return null; | |
| var value = cache.get(key); | |
| cache.delete(key); | |
| cache.set(key, value); | |
| return value; | |
| } | |
| function cacheSet(cache, key, value) { | |
| if (cache.has(key)) cache.delete(key); | |
| cache.set(key, value); | |
| while (cache.size > RECORD_CACHE_LIMIT) { | |
| cache.delete(cache.keys().next().value); | |
| } | |
| } | |
| function field(label, value) { | |
| return '<div class="field"><div class="field-label">' + esc(label) + | |
| '</div><div class="field-value">' + value + "</div></div>"; | |
| } | |
| function statusBadge(status) { | |
| var labels = { | |
| correct: "exact", | |
| partial: "partial", | |
| incorrect: "incorrect", | |
| missing: "missing", | |
| }; | |
| return '<span class="status ' + esc(status) + '">' + | |
| esc(labels[status] || status) + "</span>"; | |
| } | |
| function goldHtml(values, label) { | |
| values = Array.isArray(values) ? values : [values]; | |
| values = values.filter(function (value) { | |
| return value !== null && value !== undefined && String(value).trim(); | |
| }); | |
| var items = values.length ? values.map(function (value) { | |
| value = String(value); | |
| if (Object.prototype.hasOwnProperty.call(state.docByTitle, value)) { | |
| return '<a class="gold-item person-link" data-person="' + esc(value) + | |
| '">' + esc(value) + " ↗</a>"; | |
| } | |
| return '<span class="gold-item">' + esc(value) + "</span>"; | |
| }).join("") : '<span class="muted">none</span>'; | |
| return '<div class="gold-block"><div class="gold-label">' + | |
| esc(label || "Gold") + (values.length > 1 ? " · " + values.length : "") + | |
| '</div><div class="gold-items">' + items + "</div></div>"; | |
| } | |
| function wirePersonLinks(container) { | |
| container.querySelectorAll(".person-link").forEach(function (link) { | |
| link.addEventListener("click", function () { | |
| openPerson(link.dataset.person); | |
| }); | |
| }); | |
| } | |
| function setSharedQid(qid) { | |
| if (qid) state.qid = String(qid); | |
| } | |
| function sharedIndex(rows) { | |
| if (!state.qid) return -1; | |
| return rows.findIndex(function (row) { | |
| return String(row.id || row.qid) === state.qid; | |
| }); | |
| } | |
| function manifestRun(slot) { | |
| return (state.manifest.runs || []).find(function (run) { | |
| return run.slot === slot; | |
| }); | |
| } | |
| function runStyle(run) { | |
| return ' style="--run-accent:' + esc(run.accent || "#a78bfa") + '"'; | |
| } | |
| function controlsFor(mode) { | |
| return { | |
| corpus: "corpusControls", | |
| eval: "evalControls", | |
| structures: "structureControls", | |
| runs: "runControls", | |
| compare: "compareControls", | |
| }[mode]; | |
| } | |
| function viewFor(mode) { | |
| return { | |
| corpus: "corpusView", | |
| eval: "evalView", | |
| structures: "structuresView", | |
| runs: "runsView", | |
| compare: "compareView", | |
| }[mode]; | |
| } | |
| async function setMode(mode) { | |
| if (MODES.indexOf(mode) === -1) mode = "corpus"; | |
| state.mode = mode; | |
| localStorage.setItem(MODE_KEY, mode); | |
| document.querySelectorAll("#mainTabs button").forEach(function (button) { | |
| button.classList.toggle("active", button.dataset.mode === mode); | |
| }); | |
| MODES.forEach(function (candidate) { | |
| $(controlsFor(candidate)).hidden = candidate !== mode; | |
| $(viewFor(candidate)).hidden = candidate !== mode; | |
| }); | |
| $("loading").hidden = true; | |
| if (mode === "corpus") renderCorpus(); | |
| if (mode === "eval") filterEval(); | |
| if (mode === "structures") await loadStructures(); | |
| if (mode === "runs") await loadCurrentRun(); | |
| if (mode === "compare") await loadCompare(); | |
| } | |
| function rebuildSelect(select, rows, position, labeler) { | |
| select.innerHTML = ""; | |
| rows.forEach(function (row, index) { | |
| var option = document.createElement("option"); | |
| option.value = String(index); | |
| option.textContent = labeler(row); | |
| select.appendChild(option); | |
| }); | |
| select.value = String(position || 0); | |
| } | |
| function move(position, delta, length) { | |
| if (!length) return 0; | |
| return Math.max(0, Math.min(length - 1, position + delta)); | |
| } | |
| function openPerson(title) { | |
| $("corpusSearch").value = ""; | |
| state.corpusView = state.corpus.slice(); | |
| state.corpusIndex = state.docByTitle[title] || 0; | |
| rebuildCorpusSelect(); | |
| setMode("corpus"); | |
| } | |
| function filterCorpus() { | |
| var query = $("corpusSearch").value.trim().toLowerCase(); | |
| state.corpusView = state.corpus.filter(function (row) { | |
| return !query || String(row.title).toLowerCase().indexOf(query) >= 0; | |
| }); | |
| if (state.corpusIndex >= state.corpusView.length) state.corpusIndex = 0; | |
| rebuildCorpusSelect(); | |
| renderCorpus(); | |
| } | |
| function rebuildCorpusSelect() { | |
| rebuildSelect( | |
| $("corpusSelect"), | |
| state.corpusView, | |
| state.corpusIndex, | |
| function (row) { return short(row.title, 52); } | |
| ); | |
| } | |
| function renderCorpus() { | |
| var host = $("corpusView"); | |
| if (!state.corpusView.length) { | |
| host.innerHTML = '<div class="empty-state">No people match this filter.</div>'; | |
| $("corpusCounter").textContent = "0 / 0"; | |
| return; | |
| } | |
| var row = state.corpusView[state.corpusIndex]; | |
| $("corpusSelect").value = String(state.corpusIndex); | |
| $("corpusCounter").textContent = | |
| (state.corpusIndex + 1) + " / " + state.corpusView.length; | |
| var article = row.article || ""; | |
| var rendered; | |
| try { | |
| rendered = window.marked ? marked.parse(article) : "<pre>" + esc(article) + "</pre>"; | |
| } catch (error) { | |
| rendered = "<pre>" + esc(article) + "</pre>"; | |
| } | |
| host.innerHTML = '<article class="card document-card"><div class="card-body">' + | |
| '<div class="eyebrow">Corpus document</div><h2>' + esc(row.title) + | |
| '</h2></div><div class="document-content">' + rendered + "</div></article>"; | |
| host.scrollTop = 0; | |
| } | |
| function evalFacetValues(key) { | |
| return Array.from(new Set(state.eval.map(function (row) { | |
| return String((row.meta || {})[key] || ""); | |
| }).filter(Boolean))).sort(function (left, right) { | |
| return Number(left) - Number(right); | |
| }); | |
| } | |
| function populateFacet(select, values) { | |
| values.forEach(function (value) { | |
| var option = document.createElement("option"); | |
| option.value = value; | |
| option.textContent = value; | |
| select.appendChild(option); | |
| }); | |
| } | |
| function filterEval() { | |
| var query = $("evalSearch").value.trim().toLowerCase(); | |
| var difficulty = $("evalDifficulty").value; | |
| var type = $("evalType").value; | |
| state.evalView = state.eval.filter(function (row) { | |
| var meta = row.meta || {}; | |
| if (difficulty && String(meta.Difficulty) !== difficulty) return false; | |
| if (type && String(meta.Type) !== type) return false; | |
| if (!query) return true; | |
| return [ | |
| row.id, | |
| row.question, | |
| (row.answer || []).join(" "), | |
| ].join(" ").toLowerCase().indexOf(query) >= 0; | |
| }); | |
| var shared = sharedIndex(state.evalView); | |
| if (shared >= 0) state.evalIndex = shared; | |
| else if (state.evalIndex >= state.evalView.length) state.evalIndex = 0; | |
| rebuildSelect( | |
| $("evalSelect"), | |
| state.evalView, | |
| state.evalIndex, | |
| function (row) { return short(row.question, 55); } | |
| ); | |
| renderEval(); | |
| } | |
| function renderEval() { | |
| var host = $("evalView"); | |
| if (!state.evalView.length) { | |
| host.innerHTML = '<div class="empty-state">No questions match these filters.</div>'; | |
| $("evalCounter").textContent = "0 / 0"; | |
| return; | |
| } | |
| var row = state.evalView[state.evalIndex]; | |
| var meta = row.meta || {}; | |
| setSharedQid(row.id); | |
| $("evalSelect").value = String(state.evalIndex); | |
| $("evalCounter").textContent = (state.evalIndex + 1) + " / " + state.evalView.length; | |
| var support = (row.supporting_titles || []).length | |
| ? goldHtml(row.supporting_titles, "Supporting people") | |
| : '<div class="muted">The answer is an attribute value rather than a person document.</div>'; | |
| host.innerHTML = '<article class="card"><div class="card-body">' + | |
| '<div class="badges"><span class="badge qid">' + esc(row.id) + "</span>" + | |
| '<span class="badge diff">difficulty ' + esc(meta.Difficulty) + "</span>" + | |
| '<span class="badge type">type ' + esc(meta.Type) + "</span></div>" + | |
| "<h2>" + esc(row.question) + "</h2>" + | |
| goldHtml(row.answer || [], "Gold answer") + | |
| field("Question template", '<span class="muted">' + esc(row.template || "—") + "</span>") + | |
| field( | |
| "Ground-truth Prolog chain", | |
| row.prolog && row.prolog.length | |
| ? "<pre>" + esc(row.prolog.join("\n")) + "</pre>" | |
| : '<span class="muted">No Prolog chain retained.</span>' | |
| ) + | |
| field("Supporting documents", support) + | |
| "</div></article>"; | |
| wirePersonLinks(host); | |
| } | |
| async function loadStructures() { | |
| if (!state.structuresIndex) { | |
| $("structuresView").innerHTML = | |
| '<div class="loading">Loading the structures index…</div>'; | |
| state.structuresIndex = await fetchJson(state.manifest.files.structures); | |
| } | |
| filterStructures(); | |
| } | |
| function filterStructures() { | |
| var rows = state.structuresIndex.rows || []; | |
| var query = $("structureSearch").value.trim().toLowerCase(); | |
| state.structuresView = rows.filter(function (row) { | |
| if (!query) return true; | |
| return [ | |
| row.qid, | |
| row.question, | |
| (row.doc_ids || []).join(" "), | |
| (row.source_doc_ids || []).join(" "), | |
| ].join(" ").toLowerCase().indexOf(query) >= 0; | |
| }); | |
| var shared = sharedIndex(state.structuresView); | |
| if (shared >= 0) state.structuresIndexPosition = shared; | |
| else if (state.structuresIndexPosition >= state.structuresView.length) { | |
| state.structuresIndexPosition = 0; | |
| } | |
| rebuildSelect( | |
| $("structureSelect"), | |
| state.structuresView, | |
| state.structuresIndexPosition, | |
| function (row) { return row.qid.slice(0, 8) + " · " + short(row.question, 48); } | |
| ); | |
| renderStructureRecord(); | |
| } | |
| async function decodeStructureRecord(item) { | |
| var cached = cacheGet(state.structureCache, item.qid); | |
| if (cached) return cached; | |
| var response = await fetch("data/structures/" + item.path); | |
| if (!response.ok) throw new Error(item.path + " returned HTTP " + response.status); | |
| var encoded = (await response.text()).trim(); | |
| var binary = atob(encoded); | |
| var bytes = new Uint8Array(binary.length); | |
| for (var i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i); | |
| if (typeof DecompressionStream === "undefined") { | |
| throw new Error("This browser does not support gzip structure shards."); | |
| } | |
| var stream = new Blob([bytes]).stream().pipeThrough(new DecompressionStream("gzip")); | |
| var record = JSON.parse(await new Response(stream).text()); | |
| cacheSet(state.structureCache, item.qid, record); | |
| return record; | |
| } | |
| function structureShapeHtml(shape) { | |
| return '<details class="structure-shape"><summary><b>' + | |
| esc(shape.unit_name || shape.shape_id) + '</b><code>' + | |
| esc(shape.shape_id) + " · " + esc(shape.format || "text") + | |
| '</code></summary>' + | |
| (shape.unit_description | |
| ? '<div class="shape-description">' + esc(shape.unit_description) + "</div>" | |
| : "") + | |
| "<pre>" + esc(shape.content || "(empty artifact)") + "</pre></details>"; | |
| } | |
| function structureDocHtml(doc, position) { | |
| var sourceIds = (doc.source_doc_ids || []).map(function (value) { | |
| return "<code>" + esc(value) + "</code>"; | |
| }).join(" "); | |
| return '<details class="structure-doc"' + (position === 0 ? " open" : "") + | |
| '><summary><span>' + esc(doc.doc_id) + "</span><span>" + | |
| number(doc.n_structures) + ' structures</span></summary><div class="structure-body">' + | |
| '<div class="source-ids"><b>Source documents:</b> ' + sourceIds + "</div>" + | |
| '<details class="source-text"><summary>Supporting text</summary><pre>' + | |
| esc(doc.contents || "") + "</pre></details>" + | |
| ((doc.structures || []).map(structureShapeHtml).join("") || | |
| '<div class="empty-state">No structures for this document.</div>') + | |
| "</div></details>"; | |
| } | |
| async function renderStructureRecord() { | |
| var host = $("structuresView"); | |
| if (!state.structuresView.length) { | |
| host.innerHTML = '<div class="empty-state">No structures match this filter.</div>'; | |
| $("structureCounter").textContent = "0 / 0"; | |
| return; | |
| } | |
| var item = state.structuresView[state.structuresIndexPosition]; | |
| setSharedQid(item.qid); | |
| $("structureSelect").value = String(state.structuresIndexPosition); | |
| $("structureCounter").textContent = | |
| (state.structuresIndexPosition + 1) + " / " + state.structuresView.length; | |
| host.innerHTML = '<div class="loading">Loading ' + esc(item.qid) + "…</div>"; | |
| try { | |
| var record = await decodeStructureRecord(item); | |
| if (state.mode !== "structures" || state.qid !== record.qid) return; | |
| var meta = state.structuresIndex.meta || {}; | |
| host.innerHTML = '<article class="card structure-summary"><div class="card-body">' + | |
| '<div class="section-heading"><div><div class="eyebrow">Structures v3</div>' + | |
| "<h2>" + esc(record.question) + '</h2></div><span class="count-pill">' + | |
| number(record.n_structures) + ' structures</span></div>' + | |
| '<div class="badges"><span class="badge qid">' + esc(record.qid) + "</span>" + | |
| '<span class="badge">' + number(record.n_source_docs) + " source docs</span>" + | |
| '<span class="badge">' + number(meta.n_qids) + " matched questions</span></div>" + | |
| goldHtml(record.gold_answers || [], "Gold answer") + | |
| "</div></article>" + | |
| (record.docs || []).map(structureDocHtml).join(""); | |
| wirePersonLinks(host); | |
| } catch (error) { | |
| host.innerHTML = '<div class="empty-state">Failed to load structures: ' + | |
| esc(error) + "</div>"; | |
| } | |
| } | |
| async function ensureRunIndex(slot) { | |
| if (state.runIndexes[slot]) return state.runIndexes[slot]; | |
| var run = manifestRun(slot); | |
| state.runIndexes[slot] = await fetchJson(run.index); | |
| state.runPositions[slot] = state.runPositions[slot] || 0; | |
| return state.runIndexes[slot]; | |
| } | |
| async function loadCurrentRun() { | |
| if (!state.runSlot) state.runSlot = state.manifest.runs[0].slot; | |
| $("runSelect").value = state.runSlot; | |
| $("runsView").innerHTML = '<div class="loading">Loading run index…</div>'; | |
| await ensureRunIndex(state.runSlot); | |
| filterRun(); | |
| } | |
| function filterRun() { | |
| var slot = state.runSlot; | |
| var index = state.runIndexes[slot]; | |
| if (!index) return; | |
| var query = $("runSearch").value.trim().toLowerCase(); | |
| var status = $("runStatus").value; | |
| var rows = (index.records || []).filter(function (row) { | |
| if (status && row.status !== status) return false; | |
| if (!query) return true; | |
| return [ | |
| row.qid, | |
| row.question, | |
| (row.gold || []).join(" "), | |
| ].join(" ").toLowerCase().indexOf(query) >= 0; | |
| }); | |
| state.runViews[slot] = rows; | |
| var shared = sharedIndex(rows); | |
| if (shared >= 0) state.runPositions[slot] = shared; | |
| else if (state.runPositions[slot] >= rows.length) state.runPositions[slot] = 0; | |
| rebuildSelect( | |
| $("runQidSelect"), | |
| rows, | |
| state.runPositions[slot], | |
| function (row) { return row.qid.slice(0, 8) + " · " + short(row.question, 46); } | |
| ); | |
| renderRunRecord(); | |
| } | |
| async function ensureRunRecord(slot, qid, kind) { | |
| var key = slot + "/" + kind + "/" + qid; | |
| var cached = cacheGet(state.runCache, key); | |
| if (cached) return cached; | |
| var run = manifestRun(slot); | |
| var root = kind === "trajectory" ? run.trajectories : run.results; | |
| var record = await fetchJson(root + "/" + qid + ".json"); | |
| cacheSet(state.runCache, key, record); | |
| return record; | |
| } | |
| function metric(label, value, note) { | |
| return '<div class="metric"><div class="metric-label">' + esc(label) + | |
| '</div><div class="metric-value">' + value + '</div><div class="metric-note">' + | |
| esc(note || "") + "</div></div>"; | |
| } | |
| function operationHtml(execution) { | |
| var tokens = execution.tokens || {}; | |
| var stop = execution.failure_reason || execution.stop_reason || | |
| ((execution.finish_reasons || []).length | |
| ? execution.finish_reasons[execution.finish_reasons.length - 1] | |
| : "—"); | |
| return '<div class="ops-grid">' + | |
| '<div class="ops-item"><b>Stop / failure</b><code>' + esc(stop) + "</code></div>" + | |
| '<div class="ops-item"><b>Agent work</b>' + | |
| number(execution.turns) + " turns · " + number(execution.tool_calls) + | |
| " tool calls</div>" + | |
| '<div class="ops-item"><b>Tokens</b>' + | |
| number(tokens.total_tokens) + " total · " + number(tokens.prompt_tokens) + | |
| " prompt · " + number(tokens.completion_tokens) + " completion</div>" + | |
| '<div class="ops-item"><b>Latency</b>' + | |
| (typeof execution.latency_ms === "number" | |
| ? (execution.latency_ms / 3600000).toFixed(2) + " hours" | |
| : "—") + "</div></div>"; | |
| } | |
| function provenanceHtml(provenance) { | |
| return field( | |
| "Provenance", | |
| '<div class="ops-grid">' + | |
| '<div class="ops-item"><b>AML job</b><code>' + esc(provenance.job || "—") + | |
| '</code></div><div class="ops-item"><b>Prompt</b>' + | |
| esc(provenance.prompt || "—") + '</div><div class="ops-item"><b>Prompt SHA</b><code>' + | |
| esc(provenance.rendered_prompt_sha256 || "—") + | |
| '</code></div><div class="ops-item"><b>Model / relation format</b>' + | |
| esc(provenance.model || "—") + " · " + esc(provenance.relation_format || "—") + | |
| "</div></div>" | |
| ); | |
| } | |
| function runHeaderMetrics(run) { | |
| return '<div class="metric-grid">' + | |
| metric("Mean answer F1", pct(run.mean_f1), run.exact + " exact / " + run.total) + | |
| metric("Coverage", pct(run.coverage), run.answered + " answered / " + run.total) + | |
| metric("Cost / question", money(run.cost_per_question_usd), "prediction agent only") + | |
| metric("Prompt", esc(run.prompt || "—"), run.variant || "") + | |
| "</div>"; | |
| } | |
| function resultHtml(run, record) { | |
| var score = record.score || {}; | |
| var execution = record.execution || {}; | |
| return runHeaderMetrics(run) + | |
| '<article class="card"' + runStyle(run) + '><div class="card-body">' + | |
| '<div class="badges"><span class="badge qid">' + esc(record.qid) + "</span>" + | |
| statusBadge(execution.status) + | |
| '<span class="badge">F1 ' + pct(score.f1) + "</span></div>" + | |
| "<h2>" + esc(record.question) + "</h2>" + | |
| goldHtml(record.gold || [], "Gold answer") + | |
| field( | |
| "Prediction", | |
| record.prediction | |
| ? '<pre class="prediction">' + esc(record.prediction) + "</pre>" | |
| : '<div class="empty-state">No answer returned · ' + | |
| esc(execution.failure_reason || "unknown failure") + "</div>" | |
| ) + | |
| field( | |
| "Deterministic answer-set score", | |
| '<div class="ops-grid"><div class="ops-item"><b>Precision</b>' + | |
| pct(score.precision) + '</div><div class="ops-item"><b>Recall</b>' + | |
| pct(score.recall) + '</div><div class="ops-item"><b>F1</b>' + | |
| pct(score.f1) + '</div><div class="ops-item"><b>Set overlap</b>' + | |
| number(score.n_hit) + " / " + number(score.n_gold) + " gold</div></div>" | |
| ) + | |
| operationHtml(execution) + | |
| provenanceHtml(record.provenance || {}) + | |
| "</div></article>"; | |
| } | |
| function trajectoryHtml(run, record) { | |
| return runHeaderMetrics(run) + | |
| '<article class="card"' + runStyle(run) + '><div class="card-body">' + | |
| '<div class="badges"><span class="badge qid">' + esc(record.qid) + "</span>" + | |
| statusBadge((record.execution || {}).status) + | |
| '<span class="badge">F1 ' + pct((record.score || {}).f1) + "</span></div>" + | |
| "<h2>" + esc(record.question) + "</h2>" + | |
| goldHtml(record.gold || [], "Gold answer") + | |
| field( | |
| "Final prediction", | |
| record.prediction | |
| ? '<pre class="prediction">' + esc(record.prediction) + "</pre>" | |
| : '<span class="muted">No answer returned.</span>' | |
| ) + | |
| operationHtml(record.execution || {}) + | |
| provenanceHtml(record.provenance || {}) + | |
| ((record.attempt_log || []).length | |
| ? '<details class="field"><summary class="field-label">Attempt log</summary><pre>' + | |
| esc(JSON.stringify(record.attempt_log, null, 2)) + "</pre></details>" | |
| : "") + | |
| window.TrajectoryUI.render(record.events || [], run.label + " trajectory") + | |
| "</div></article>"; | |
| } | |
| async function renderRunRecord() { | |
| var slot = state.runSlot; | |
| var rows = state.runViews[slot] || []; | |
| var position = state.runPositions[slot] || 0; | |
| var host = $("runsView"); | |
| if (!rows.length) { | |
| host.innerHTML = '<div class="empty-state">No run records match these filters.</div>'; | |
| $("runCounter").textContent = "0 / 0"; | |
| return; | |
| } | |
| var summary = rows[position]; | |
| var run = manifestRun(slot); | |
| setSharedQid(summary.qid); | |
| $("runQidSelect").value = String(position); | |
| $("runCounter").textContent = (position + 1) + " / " + rows.length; | |
| host.innerHTML = '<div class="loading">Loading ' + esc(summary.qid) + "…</div>"; | |
| try { | |
| var record = await ensureRunRecord( | |
| slot, | |
| summary.qid, | |
| state.runViewMode | |
| ); | |
| if (state.mode !== "runs" || state.runSlot !== slot || state.qid !== record.qid) { | |
| return; | |
| } | |
| host.innerHTML = state.runViewMode === "trajectory" | |
| ? trajectoryHtml(run, record) | |
| : resultHtml(run, record); | |
| wirePersonLinks(host); | |
| window.TrajectoryUI.bind(host); | |
| } catch (error) { | |
| host.innerHTML = '<div class="empty-state">Failed to load run record: ' + | |
| esc(error) + "</div>"; | |
| } | |
| } | |
| async function loadCompare() { | |
| if (!state.compareIndex) { | |
| $("compareView").innerHTML = | |
| '<div class="loading">Loading comparison index…</div>'; | |
| state.compareIndex = await fetchJson(state.manifest.files.compare); | |
| } | |
| filterCompare(); | |
| } | |
| function filterCompare() { | |
| var query = $("compareSearch").value.trim().toLowerCase(); | |
| var filter = $("compareFilter").value; | |
| state.compareView = (state.compareIndex.records || []).filter(function (row) { | |
| if (filter === "disagreement" && !row.disagreement) return false; | |
| if (filter === "missing" && !row.any_missing) return false; | |
| if (filter === "perfect" && !Object.values(row.exact || {}).some(Boolean)) return false; | |
| return !query || [row.qid, row.question].join(" ").toLowerCase().indexOf(query) >= 0; | |
| }); | |
| var shared = sharedIndex(state.compareView); | |
| if (shared >= 0) state.comparePosition = shared; | |
| else if (state.comparePosition >= state.compareView.length) state.comparePosition = 0; | |
| rebuildSelect( | |
| $("compareSelect"), | |
| state.compareView, | |
| state.comparePosition, | |
| function (row) { return row.qid.slice(0, 8) + " · " + short(row.question, 48); } | |
| ); | |
| renderCompareRecord(); | |
| } | |
| async function ensureCompareRecord(qid) { | |
| var cached = cacheGet(state.compareCache, qid); | |
| if (cached) return cached; | |
| var record = await fetchJson("data/compare/records/" + qid + ".json"); | |
| cacheSet(state.compareCache, qid, record); | |
| return record; | |
| } | |
| function compareScoreHtml(slot, record) { | |
| var run = manifestRun(slot); | |
| return '<div class="compare-score"' + runStyle(run) + '><h3>' + | |
| esc(run.label) + '</h3><strong>' + pct((record.score || {}).f1) + | |
| '</strong><span>' + esc((record.execution || {}).status) + " · " + | |
| number((record.execution || {}).turns) + " turns</span></div>"; | |
| } | |
| function compareRunHtml(slot, record) { | |
| var run = manifestRun(slot); | |
| return '<article class="card compare-run"' + runStyle(run) + | |
| '><div class="card-body"><div class="section-heading"><div>' + | |
| '<div class="eyebrow">' + esc(run.variant || run.family) + "</div><h3>" + | |
| esc(run.label) + '</h3></div>' + | |
| statusBadge((record.execution || {}).status) + "</div>" + | |
| field( | |
| "Prediction", | |
| record.prediction | |
| ? '<pre class="prediction">' + esc(record.prediction) + "</pre>" | |
| : '<span class="muted">No answer returned · ' + | |
| esc((record.execution || {}).failure_reason || "unknown failure") + | |
| "</span>" | |
| ) + | |
| field( | |
| "Score", | |
| "precision " + pct((record.score || {}).precision) + | |
| " · recall " + pct((record.score || {}).recall) + | |
| " · F1 " + pct((record.score || {}).f1) | |
| ) + | |
| operationHtml(record.execution || {}) + | |
| "</div></article>"; | |
| } | |
| async function renderCompareRecord() { | |
| var host = $("compareView"); | |
| if (!state.compareView.length) { | |
| host.innerHTML = '<div class="empty-state">No comparison rows match this filter.</div>'; | |
| $("compareCounter").textContent = "0 / 0"; | |
| return; | |
| } | |
| var summary = state.compareView[state.comparePosition]; | |
| setSharedQid(summary.qid); | |
| $("compareSelect").value = String(state.comparePosition); | |
| $("compareCounter").textContent = | |
| (state.comparePosition + 1) + " / " + state.compareView.length; | |
| host.innerHTML = '<div class="loading">Loading comparison ' + | |
| esc(summary.qid) + "…</div>"; | |
| try { | |
| var record = await ensureCompareRecord(summary.qid); | |
| if (state.mode !== "compare" || state.qid !== record.qid) return; | |
| var slots = state.compareIndex.slots || []; | |
| host.innerHTML = '<article class="card"><div class="card-body">' + | |
| '<div class="badges"><span class="badge qid">' + esc(record.qid) + | |
| '</span><span class="badge">' + slots.length + " runs</span></div>" + | |
| "<h2>" + esc(record.question) + "</h2>" + | |
| goldHtml(record.gold || [], "Gold answer") + | |
| "</div></article>" + | |
| '<div class="compare-scores">' + slots.map(function (slot) { | |
| return compareScoreHtml(slot, record.runs[slot]); | |
| }).join("") + "</div>" + | |
| slots.map(function (slot) { | |
| return compareRunHtml(slot, record.runs[slot]); | |
| }).join(""); | |
| wirePersonLinks(host); | |
| } catch (error) { | |
| host.innerHTML = '<div class="empty-state">Failed to load comparison: ' + | |
| esc(error) + "</div>"; | |
| } | |
| } | |
| function bindEvents() { | |
| document.querySelectorAll("#mainTabs button").forEach(function (button) { | |
| button.addEventListener("click", function () { | |
| setMode(button.dataset.mode); | |
| }); | |
| }); | |
| $("corpusSearch").addEventListener("input", filterCorpus); | |
| $("corpusSelect").addEventListener("change", function () { | |
| state.corpusIndex = Number(this.value) || 0; | |
| renderCorpus(); | |
| }); | |
| $("corpusPrev").addEventListener("click", function () { | |
| state.corpusIndex = move(state.corpusIndex, -1, state.corpusView.length); | |
| renderCorpus(); | |
| }); | |
| $("corpusNext").addEventListener("click", function () { | |
| state.corpusIndex = move(state.corpusIndex, 1, state.corpusView.length); | |
| renderCorpus(); | |
| }); | |
| ["evalSearch", "evalDifficulty", "evalType"].forEach(function (id) { | |
| $(id).addEventListener(id === "evalSearch" ? "input" : "change", filterEval); | |
| }); | |
| $("evalSelect").addEventListener("change", function () { | |
| state.evalIndex = Number(this.value) || 0; | |
| renderEval(); | |
| }); | |
| $("evalPrev").addEventListener("click", function () { | |
| state.evalIndex = move(state.evalIndex, -1, state.evalView.length); | |
| renderEval(); | |
| }); | |
| $("evalNext").addEventListener("click", function () { | |
| state.evalIndex = move(state.evalIndex, 1, state.evalView.length); | |
| renderEval(); | |
| }); | |
| $("structureSearch").addEventListener("input", filterStructures); | |
| $("structureSelect").addEventListener("change", function () { | |
| state.structuresIndexPosition = Number(this.value) || 0; | |
| renderStructureRecord(); | |
| }); | |
| $("structurePrev").addEventListener("click", function () { | |
| state.structuresIndexPosition = move( | |
| state.structuresIndexPosition, -1, state.structuresView.length | |
| ); | |
| renderStructureRecord(); | |
| }); | |
| $("structureNext").addEventListener("click", function () { | |
| state.structuresIndexPosition = move( | |
| state.structuresIndexPosition, 1, state.structuresView.length | |
| ); | |
| renderStructureRecord(); | |
| }); | |
| $("runSelect").addEventListener("change", async function () { | |
| state.runSlot = this.value; | |
| await loadCurrentRun(); | |
| }); | |
| document.querySelectorAll("#runSubtabs button").forEach(function (button) { | |
| button.addEventListener("click", function () { | |
| state.runViewMode = button.dataset.runView; | |
| document.querySelectorAll("#runSubtabs button").forEach(function (candidate) { | |
| candidate.classList.toggle("active", candidate === button); | |
| }); | |
| renderRunRecord(); | |
| }); | |
| }); | |
| $("runSearch").addEventListener("input", filterRun); | |
| $("runStatus").addEventListener("change", filterRun); | |
| $("runQidSelect").addEventListener("change", function () { | |
| state.runPositions[state.runSlot] = Number(this.value) || 0; | |
| renderRunRecord(); | |
| }); | |
| $("runPrev").addEventListener("click", function () { | |
| var rows = state.runViews[state.runSlot] || []; | |
| state.runPositions[state.runSlot] = move( | |
| state.runPositions[state.runSlot] || 0, -1, rows.length | |
| ); | |
| renderRunRecord(); | |
| }); | |
| $("runNext").addEventListener("click", function () { | |
| var rows = state.runViews[state.runSlot] || []; | |
| state.runPositions[state.runSlot] = move( | |
| state.runPositions[state.runSlot] || 0, 1, rows.length | |
| ); | |
| renderRunRecord(); | |
| }); | |
| $("compareSearch").addEventListener("input", filterCompare); | |
| $("compareFilter").addEventListener("change", filterCompare); | |
| $("compareSelect").addEventListener("change", function () { | |
| state.comparePosition = Number(this.value) || 0; | |
| renderCompareRecord(); | |
| }); | |
| $("comparePrev").addEventListener("click", function () { | |
| state.comparePosition = move( | |
| state.comparePosition, -1, state.compareView.length | |
| ); | |
| renderCompareRecord(); | |
| }); | |
| $("compareNext").addEventListener("click", function () { | |
| state.comparePosition = move( | |
| state.comparePosition, 1, state.compareView.length | |
| ); | |
| renderCompareRecord(); | |
| }); | |
| document.addEventListener("keydown", function (event) { | |
| if (event.key !== "ArrowLeft" && event.key !== "ArrowRight") return; | |
| var tag = String(event.target.tagName || "").toLowerCase(); | |
| if (["input", "select", "textarea"].indexOf(tag) >= 0) return; | |
| var button = { | |
| corpus: event.key === "ArrowLeft" ? "corpusPrev" : "corpusNext", | |
| eval: event.key === "ArrowLeft" ? "evalPrev" : "evalNext", | |
| structures: event.key === "ArrowLeft" ? "structurePrev" : "structureNext", | |
| runs: event.key === "ArrowLeft" ? "runPrev" : "runNext", | |
| compare: event.key === "ArrowLeft" ? "comparePrev" : "compareNext", | |
| }[state.mode]; | |
| if (button) $(button).click(); | |
| }); | |
| } | |
| function populateRunSelect() { | |
| var select = $("runSelect"); | |
| select.innerHTML = ""; | |
| state.manifest.runs.forEach(function (run) { | |
| var option = document.createElement("option"); | |
| option.value = run.slot; | |
| option.textContent = run.label; | |
| select.appendChild(option); | |
| }); | |
| state.runSlot = state.manifest.runs[0].slot; | |
| select.value = state.runSlot; | |
| } | |
| function renderScope() { | |
| $("scopeCard").innerHTML = | |
| '<b>' + esc(state.manifest.total_qids) + " matched questions</b><br>" + | |
| esc(state.manifest.universe) + "<br><code>" + | |
| esc(state.manifest.qid_sha256) + "</code>"; | |
| $("sidebarFooter").innerHTML = | |
| esc(state.manifest.corpus_documents) + " corpus documents · " + | |
| esc(state.manifest.runs.length) + " completed runs<br>" + | |
| "All scores use deterministic normalized answer-set F1."; | |
| } | |
| async function init() { | |
| bindEvents(); | |
| try { | |
| state.manifest = await fetchJson("data/manifest.json"); | |
| var data = await Promise.all([ | |
| fetchJson(state.manifest.files.corpus), | |
| fetchJson(state.manifest.files.eval), | |
| ]); | |
| state.corpus = data[0] || []; | |
| state.eval = data[1] || []; | |
| state.corpus.forEach(function (row, index) { | |
| state.docByTitle[row.title] = index; | |
| }); | |
| state.corpusView = state.corpus.slice(); | |
| state.evalView = state.eval.slice(); | |
| rebuildCorpusSelect(); | |
| populateFacet($("evalDifficulty"), evalFacetValues("Difficulty")); | |
| populateFacet($("evalType"), evalFacetValues("Type")); | |
| populateRunSelect(); | |
| renderScope(); | |
| var savedMode = localStorage.getItem(MODE_KEY); | |
| await setMode(MODES.indexOf(savedMode) >= 0 ? savedMode : "corpus"); | |
| } catch (error) { | |
| $("loading").hidden = false; | |
| $("loading").textContent = "Failed to initialize viewer: " + error; | |
| } | |
| } | |
| init(); | |
| })(); | |