File size: 10,756 Bytes
effe186
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
"use strict";

const state = { data: null, cases: [], filtered: [], current: null };
const byId = (id) => document.getElementById(id);

function setText(id, value) {
  byId(id).textContent = value == null ? "" : String(value);
}

function clear(node) {
  while (node.firstChild) node.removeChild(node.firstChild);
}

function addOption(select, label, value) {
  const option = document.createElement("option");
  option.value = value;
  option.textContent = label;
  select.appendChild(option);
}

function displayCategory(value) {
  return String(value).replaceAll("_", " ");
}

function shortUid(uid) {
  return `${uid.slice(0, 12)}...${uid.slice(-8)}`;
}

function caseLabel(item) {
  const strata = item.review_strata;
  return `${item.split_label} | ${displayCategory(item.category)} | ${strata.rating} stars | ${strata.length_band} | ${shortUid(item.uid)}`;
}

function populateSelect(id, values, includeAll = true) {
  const select = byId(id);
  clear(select);
  if (includeAll) addOption(select, "All", "All");
  values.forEach((value) => addOption(select, displayCategory(value), value));
}

function populateFilters() {
  const unique = (values) => [...new Set(values)].sort();
  populateSelect("split-filter", unique(state.cases.map((item) => item.split_label)));
  populateSelect("category-filter", unique(state.cases.map((item) => item.category)));
  populateSelect("rating-filter", unique(state.cases.map((item) => String(item.review_strata.rating))));
  populateSelect("length-filter", ["Short", "Medium", "Long"]);
}

function selectedValue(id) {
  return byId(id).value;
}

function applyFilters(preferredCaseId = null) {
  const split = selectedValue("split-filter");
  const category = selectedValue("category-filter");
  const rating = selectedValue("rating-filter");
  const length = selectedValue("length-filter");
  state.filtered = state.cases.filter((item) =>
    (split === "All" || item.split_label === split) &&
    (category === "All" || item.category === category) &&
    (rating === "All" || String(item.review_strata.rating) === rating) &&
    (length === "All" || item.review_strata.length_band === length)
  );
  const select = byId("case-select");
  clear(select);
  state.filtered.forEach((item) => addOption(select, caseLabel(item), item.case_id));
  const chosen = state.filtered.find((item) => item.case_id === preferredCaseId) || state.filtered[0];
  byId("empty-state").hidden = Boolean(chosen);
  document.querySelectorAll(".panel, .tabs, .case-band").forEach((node) => {
    node.style.display = chosen ? "" : "none";
  });
  if (chosen) {
    select.value = chosen.case_id;
    renderCase(chosen);
  }
}

function addDefinition(list, label, value) {
  const wrapper = document.createElement("div");
  const term = document.createElement("dt");
  const detail = document.createElement("dd");
  term.textContent = label;
  detail.textContent = value == null || value === "" ? "-" : String(value);
  wrapper.append(term, detail);
  list.appendChild(wrapper);
}

function renderMetrics(item) {
  const metrics = byId("case-metrics");
  clear(metrics);
  addDefinition(metrics, "User", shortUid(item.uid));
  addDefinition(metrics, "Source", item.split_label);
  addDefinition(metrics, "Category", displayCategory(item.category));
  addDefinition(metrics, "Target", `${item.review_strata.rating} / 5, ${item.review_strata.length_band}`);
  addDefinition(metrics, "Pool", `${item.candidate_pool.valid_candidates} valid / ${item.candidate_pool.error_candidates} errors`);
}

function renderDetails(id, details) {
  const list = byId(id);
  clear(list);
  Object.entries(details).forEach(([key, value]) => {
    const term = document.createElement("dt");
    const detail = document.createElement("dd");
    term.textContent = key;
    detail.textContent = value;
    list.append(term, detail);
  });
}

function renderTarget(item) {
  const target = item.target;
  const product = target.product;
  setText("target-title", product.title);
  setText("target-rating", `${target.rating} / 5 | ${target.date || "unknown date"}`);
  setText("target-review", target.text);
  setText("target-description", product.description_excerpt || "No description available.");
  const features = byId("target-features");
  clear(features);
  (product.features.length ? product.features : ["No features available."]).forEach((feature) => {
    const row = document.createElement("li");
    row.textContent = feature;
    features.appendChild(row);
  });
  renderDetails("target-details", product.details);
}

function appendCell(row, value, className = "") {
  const cell = document.createElement("td");
  cell.textContent = value == null ? "-" : String(value);
  if (className) cell.className = className;
  row.appendChild(cell);
}

function renderHistory(item) {
  const body = byId("history-body");
  clear(body);
  const demos = item.history.sampled_demonstrations;
  setText("history-count", `${demos.length} shown / ${item.history.total_demonstrations} total`);
  demos.forEach((demo) => {
    const row = document.createElement("tr");
    appendCell(row, demo.date);
    appendCell(row, demo.rating);
    appendCell(row, demo.length_band);
    appendCell(row, demo.product.title);
    appendCell(row, demo.text);
    body.appendChild(row);
  });
}

function modelName(model) {
  return String(model || "unknown").split("/").pop();
}

function candidateLabel(candidate, index) {
  return `${index + 1}. ${candidate.family} | ${modelName(candidate.model)} | ${candidate.judge_score.toFixed(2)} | ${candidate.sample_role}`;
}

function renderCandidates(item) {
  const candidates = item.candidate_pool.sampled_candidates;
  const select = byId("candidate-select");
  const body = byId("candidate-body");
  clear(select);
  clear(body);
  candidates.forEach((candidate, index) => {
    addOption(select, candidateLabel(candidate, index), String(index));
    const row = document.createElement("tr");
    appendCell(row, candidate.family);
    appendCell(row, candidate.sample_role);
    appendCell(row, modelName(candidate.model));
    appendCell(row, candidate.gen_label);
    appendCell(row, candidate.judge_score.toFixed(2), `score-${candidate.score_band.toLowerCase()}`);
    appendCell(row, candidate.eligible_views.join(", "));
    appendCell(row, candidate.response.slice(0, 220));
    body.appendChild(row);
  });
  renderCandidate(candidates[0]);
}

function renderCandidate(candidate) {
  if (!candidate) return;
  setText("candidate-recipe", `${modelName(candidate.model)} | ${candidate.gen_label}`);
  setText("candidate-family", `${candidate.family} candidate`);
  const score = byId("candidate-score");
  score.textContent = candidate.judge_score.toFixed(2);
  score.className = `score ${candidate.score_band.toLowerCase()}`;
  setText("candidate-response", candidate.response);
  renderDetails("candidate-metadata", {
    Selection: candidate.sample_role,
    "ICL examples": candidate.n_icl,
    Privilege: candidate.privileged || "none",
    "Refine round": candidate.refine_round,
    Views: candidate.eligible_views.join(", "),
  });
  setText("judge-key-points", candidate.judge_key_points || "Not available.");
  setText("judge-thought", candidate.judge_thought || "Not available.");
}

function scoreValue(value) {
  return value == null ? "-" : Number(value).toFixed(3);
}

function renderPool(item) {
  const pool = item.candidate_pool;
  const body = byId("pool-body");
  clear(body);
  Object.entries(pool.family_stats).forEach(([family, stats]) => {
    const row = document.createElement("tr");
    appendCell(row, family);
    appendCell(row, stats.expected);
    appendCell(row, stats.valid);
    appendCell(row, stats.error, stats.error ? "status-error" : "status-ok");
    appendCell(row, scoreValue(stats.score_min));
    appendCell(row, scoreValue(stats.score_median));
    appendCell(row, scoreValue(stats.score_mean));
    appendCell(row, scoreValue(stats.score_max));
    body.appendChild(row);
  });
  const status = pool.error_candidates
    ? `${pool.error_candidates} unavailable slots are retained in coverage counts`
    : "All candidate slots are valid";
  setText("pool-status", status);
  byId("pool-status").className = `section-stat ${pool.error_candidates ? "status-error" : "status-ok"}`;
}

function renderCase(item) {
  state.current = item;
  renderMetrics(item);
  renderTarget(item);
  renderHistory(item);
  renderCandidates(item);
  renderPool(item);
  history.replaceState(null, "", `#${encodeURIComponent(item.case_id)}`);
}

function activateTab(name) {
  document.querySelectorAll(".tab").forEach((tab) => {
    const active = tab.dataset.tab === name;
    tab.classList.toggle("active", active);
    tab.setAttribute("aria-selected", String(active));
  });
  document.querySelectorAll(".panel").forEach((panel) => {
    const active = panel.id === `${name}-panel`;
    panel.classList.toggle("active", active);
    panel.hidden = !active;
  });
}

function bindEvents() {
  ["split-filter", "category-filter", "rating-filter", "length-filter"].forEach((id) => {
    byId(id).addEventListener("change", () =>
      applyFilters(state.current ? state.current.case_id : null)
    );
  });
  byId("reset-filters").addEventListener("click", () => {
    ["split-filter", "category-filter", "rating-filter", "length-filter"].forEach((id) => {
      byId(id).value = "All";
    });
    applyFilters();
  });
  byId("case-select").addEventListener("change", (event) => {
    const item = state.cases.find((entry) => entry.case_id === event.target.value);
    if (item) renderCase(item);
  });
  byId("candidate-select").addEventListener("change", (event) => {
    const candidates = state.current ? state.current.candidate_pool.sampled_candidates : [];
    renderCandidate(candidates[Number(event.target.value)]);
  });
  document.querySelectorAll(".tab").forEach((tab) => {
    tab.addEventListener("click", () => activateTab(tab.dataset.tab));
  });
}

async function start() {
  try {
    const response = await fetch("examples.json", { cache: "no-store" });
    if (!response.ok) throw new Error(`Snapshot request failed: ${response.status}`);
    state.data = await response.json();
    state.cases = state.data.cases;
    setText("notice", state.data.notice);
    const generated = new Date(state.data.generated_at).toLocaleString();
    setText(
      "snapshot-meta",
      `${state.data.selection.selected_cases} users | ${generated} | active checkpoint`
    );
    populateFilters();
    bindEvents();
    const requested = decodeURIComponent(location.hash.slice(1));
    applyFilters(requested || null);
  } catch (error) {
    setText("notice", `Unable to load the review snapshot: ${error.message}`);
    byId("notice").classList.add("status-error");
  }
}

start();