(function () {
'use strict';
const state = {
index: null,
datasetId: null,
search: '',
filtered: [],
selected: 0,
cache: new Map(),
};
const dom = {
tabs: document.getElementById('datasetTabs'),
search: document.getElementById('search'),
list: document.getElementById('docList'),
main: document.getElementById('main'),
meta: document.getElementById('runMeta'),
prev: document.getElementById('prevBtn'),
next: document.getElementById('nextBtn'),
counter: document.getElementById('counter'),
};
const esc = (value) => String(value ?? '')
.replace(/&/g, '&').replace(//g, '>')
.replace(/"/g, '"').replace(/'/g, ''');
function dataset() {
return state.index.datasets.find((row) => row.id === state.datasetId);
}
function renderMarkdown(value) {
if (window.marked && window.marked.parse) {
try { return window.marked.parse(String(value ?? '')); } catch (_) {}
}
return `
${esc(value)}`;
}
function parseCsv(text) {
const rows = [];
let row = [], field = '', quoted = false;
for (let i = 0; i < text.length; i++) {
const ch = text[i];
if (quoted) {
if (ch === '"' && text[i + 1] === '"') { field += '"'; i++; }
else if (ch === '"') quoted = false;
else field += ch;
} else if (ch === '"') quoted = true;
else if (ch === ',') { row.push(field); field = ''; }
else if (ch === '\n') { row.push(field.replace(/\r$/, '')); rows.push(row); row = []; field = ''; }
else field += ch;
}
if (field || row.length) { row.push(field); rows.push(row); }
return rows;
}
function renderArtifactContent(artifact) {
if (artifact.format === 'csv') {
const rows = parseCsv(artifact.content);
if (!rows.length) return '';
return `${rows[0].map((cell) => `| ${esc(cell)} | `).join('')}
${rows.slice(1).map((row) => `${row.map((cell) => `| ${esc(cell)} | `).join('')}
`).join('')}
`;
}
if (artifact.format === 'json' || artifact.format === 'jsonl') {
try {
const value = artifact.format === 'json'
? JSON.parse(artifact.content)
: artifact.content.split(/\r?\n/).filter(Boolean).map((line) => JSON.parse(line));
return `${esc(JSON.stringify(value, null, 2))}`;
} catch (_) {}
}
if (artifact.format === 'md') return renderMarkdown(artifact.content);
return `${esc(artifact.content)}`;
}
function renderTabs() {
dom.tabs.innerHTML = state.index.datasets.map((row) =>
``
).join('');
dom.tabs.querySelectorAll('button').forEach((button) => {
button.addEventListener('click', () => setDataset(button.dataset.id));
});
}
function renderRuns() {
const ds = dataset();
dom.meta.innerHTML = ds.runs.map((item) => {
const qualityText = item.quality
? `${item.quality.final_scaffold_files ?? '—'} files · ${item.quality.total_failures ?? '—'} failures`
: (item.status === 'pending' ? 'Output intentionally blank while AML runs' : 'No schema-v3 audit report');
return `
${esc(item.label)}
${esc(item.job)}
schema: ${esc(item.schema)} · status: ${esc(item.status)}
${esc(qualityText)}
`;
}).join('');
}
function recompute() {
const query = state.search.trim().toLowerCase();
state.filtered = dataset().records.filter((row) => {
if (!query) return true;
return `${row.doc_id} ${row.source_preview}`.toLowerCase().includes(query);
});
if (state.selected >= state.filtered.length) state.selected = Math.max(0, state.filtered.length - 1);
}
function renderList() {
const runs = dataset().runs;
dom.list.innerHTML = state.filtered.map((row, index) => {
const counts = runs.map((item) => `${item.id === 'current' ? 'C' : 'L'} ${row.run_counts[item.id] ?? 0}`).join(' · ');
return `
${counts}
${esc(row.doc_id)}
${esc(row.source_preview)}
`;
}).join('');
dom.list.querySelectorAll('li').forEach((item) => {
item.addEventListener('click', () => { state.selected = Number(item.dataset.index); render(); });
});
dom.counter.textContent = state.filtered.length ? `${state.selected + 1} / ${state.filtered.length}` : '0 / 0';
dom.prev.disabled = state.selected <= 0;
dom.next.disabled = state.selected >= state.filtered.length - 1;
}
async function loadRecord(summary) {
if (!state.cache.has(summary.record)) {
state.cache.set(summary.record, fetch(summary.record).then((response) => {
if (!response.ok) throw new Error(`Failed to load ${summary.record}: ${response.status}`);
return response.json();
}));
}
return state.cache.get(summary.record);
}
function renderStructures(record, selectedRun) {
const value = record.runs[selectedRun.id];
if (selectedRun.status === 'pending') {
return `Current run pending${esc(selectedRun.job)}
This panel is intentionally blank. The source document and legacy extraction remain available.
`;
}
if (!value.artifacts.length) {
return 'No structure producedThe document was part of this run, but no applicable scaffold artifact was emitted.
';
}
const groups = new Map();
value.artifacts.forEach((artifact) => {
if (!groups.has(artifact.shape)) groups.set(artifact.shape, []);
groups.get(artifact.shape).push(artifact);
});
return [...groups.entries()].map(([shape, artifacts]) =>
`
${esc(artifacts[0].shape_label)}${artifacts.length} artifact${artifacts.length === 1 ? '' : 's'}
${artifacts.map((artifact) => `
${esc(artifact.filename)}
${esc(artifact.unit_description || artifact.raw_shape)} · raw shape: ${esc(artifact.raw_shape)}
${renderArtifactContent(artifact)}
`).join('')}
`
).join('');
}
async function renderMain() {
if (!state.filtered.length) {
dom.main.innerHTML = 'No matching documents.
';
return;
}
const summary = state.filtered[state.selected];
dom.main.innerHTML = 'Loading document...
';
try {
const record = await loadRecord(summary);
const runs = dataset().runs;
const countBadges = runs.map((item) =>
`${esc(item.label)}: ${record.runs[item.id].n_artifacts}`
).join('');
dom.main.innerHTML = `
${countBadges}
${esc(record.doc_id)}
${esc(record.dataset_label)} · source ${esc(record.source.source_path)} · SHA-256 ${esc(record.source.sha256)}
Source document
${renderMarkdown(record.source.contents)}
${runs.map((item) => `
${esc(item.label)}
${renderStructures(record, item)}
`).join('')}
`;
} catch (error) {
dom.main.innerHTML = `${esc(error.message)}
`;
}
}
function render() {
renderTabs();
renderRuns();
recompute();
renderList();
renderMain();
}
function setDataset(id) {
state.datasetId = id;
state.selected = 0;
state.search = '';
dom.search.value = '';
render();
}
dom.search.addEventListener('input', () => { state.search = dom.search.value; state.selected = 0; render(); });
dom.prev.addEventListener('click', () => { if (state.selected > 0) { state.selected--; render(); } });
dom.next.addEventListener('click', () => { if (state.selected < state.filtered.length - 1) { state.selected++; render(); } });
document.addEventListener('keydown', (event) => {
if (event.target.matches('input, select')) return;
if (event.key === 'ArrowLeft') dom.prev.click();
if (event.key === 'ArrowRight') dom.next.click();
});
fetch('data/index.json')
.then((response) => response.json())
.then((index) => {
state.index = index;
state.datasetId = index.datasets[0].id;
render();
})
.catch((error) => {
dom.main.innerHTML = `Failed to load viewer: ${esc(error.message)}
`;
});
})();