Spaces:
Running

File size: 11,024 Bytes
3de9c7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/* Full transcript of a run (build/transcripts.py writes data/tx/<task>/<run>.json.gz): a closed section that loads the
   run's record when opened. Runs of tool calls fold into one line each; "Messages only" hides them. */
(() => {
const esc = s => String(s ?? "").replace(/[&<>"]/g, c => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" }[c]));
const num = n => n.toLocaleString("en-US"), pl = (n, w) => `${num(n)} ${w}${n === 1 ? "" : "s"}`;
const PAGE = 150, FOLD = 3;

function section(task, rid, judgeOnly) {
  return `<section class="txs" id="bTx" data-task="${esc(task)}" data-run="${esc(rid)}" aria-labelledby="txH">
  <div class="txs-hd"><div class="txs-tt"><span class="lab">Full transcript</span><h2 id="txH">Everything that happened in this run</h2>
  <p>${judgeOnly ? "The transcript is the whole record of this run" : "The steps above tell this run's story. The transcript is the whole record"}, on the task's clock: the ask, everything the agent wrote, every tool call it made with its result, and each message from a colleague or a scheduled event. The colleagues' and the judge's own reasoning is not included.</p></div>
  <button class="btn sec txs-go" type="button">Show the full transcript</button></div>
  <div class="txs-body" hidden></div></section>`;
}

function wire(root) {
  if (!root) return;
  const go = root.querySelector(".txs-go"), body = root.querySelector(".txs-body");
  let tx = null, loading = false;
  go.addEventListener("click", async () => {
    if (tx) { const open = body.hidden; body.hidden = !open; go.textContent = open ? "Hide the transcript" : "Show the full transcript"; return; }
    if (loading) return; loading = true; go.textContent = "Loading…";
    try { tx = await load(root.dataset.task, root.dataset.run); }
    catch (e) { go.textContent = "Show the full transcript"; loading = false; body.hidden = false; body.innerHTML = e.message === "404" ? `<p class="txs-err">This run has no transcript yet.</p>` : `<p class="txs-err">The transcript did not load. Check the connection and try again.</p>`; return; }
    body.hidden = false; go.textContent = "Hide the transcript"; draw(body, tx);
  });
}

async function load(task, rid) {
  const r = await fetch(`data/tx/${encodeURIComponent(task)}/${encodeURIComponent(rid)}.json.gz`, { credentials: "same-origin" });
  if (!r.ok) throw new Error(String(r.status));
  const buf = new Uint8Array(await r.arrayBuffer());
  if (buf[0] !== 0x1f || buf[1] !== 0x8b) return JSON.parse(new TextDecoder().decode(buf)); /* the server already unzipped it */
  const s = new Blob([buf]).stream().pipeThrough(new DecompressionStream("gzip"));
  return JSON.parse(await new Response(s).text());
}

function draw(body, tx) {
  const ev = tx.events, n = k => ev.filter(e => k.includes(e.k)).length;
  const pre = ev.filter(e => e.k === "beat" && e.before), rest = ev.filter(e => !(e.k === "beat" && e.before));
  const items = fold(rest);
  let mode = "all", shown = 0;
  body.innerHTML = `<div class="txs-bar"><p class="txs-n mono">${num(ev.length - pre.length)} events · ${num(n(["send"]))} sent or written by the agent · ${num(n(["msg", "beat"]) - pre.length)} from colleagues and scheduled events · ${pl(n(["say"]), "note")} by the agent · ${pl(n(["call", "local"]), "other tool call")}</p>
    <div class="txs-mode" role="group" aria-label="Show"><button type="button" data-m="all" aria-pressed="true">Everything</button><button type="button" data-m="msg" aria-pressed="false">Messages only</button></div></div>
    ${pre.length ? `<details class="tx-pre"><summary>Before the run starts · ${pre.length} scheduled event${pre.length > 1 ? "s" : ""} set up the world</summary><ol class="tx-l">${pre.map(row).join("")}</ol></details>` : ""}
    <ol class="tx-l" id="txL"></ol><button class="btn sec txs-more" type="button" hidden></button>`;
  const list = body.querySelector("#txL"), more = body.querySelector(".txs-more");
  const pool = () => mode === "all" ? items : items.filter(x => !x.burst && !["call", "local"].includes(x.e.k));
  const page = () => { const p = pool(), next = p.slice(shown, shown + PAGE); list.insertAdjacentHTML("beforeend", next.map(item).join("")); shown += next.length;
    more.hidden = shown >= p.length; more.textContent = `Show the next ${num(Math.min(PAGE, p.length - shown))} of ${num(p.length - shown)} more`; };
  const reset = () => { list.innerHTML = ""; shown = 0; page(); };
  more.addEventListener("click", page);
  body.addEventListener("click", e => { const c = e.target.closest("button.tx-c"); if (!c) return; const p = c.nextElementSibling, open = p.hidden; p.hidden = !open; c.setAttribute("aria-expanded", String(open)); });
  body.querySelector(".txs-mode").addEventListener("click", e => { const b = e.target.closest("[data-m]"); if (!b || b.dataset.m === mode) return; mode = b.dataset.m;
    body.querySelectorAll(".txs-mode [data-m]").forEach(x => x.setAttribute("aria-pressed", String(x === b))); reset(); });
  list.addEventListener("toggle", e => { const d = e.target; if (d.classList && d.classList.contains("tx-burst") && d.open && !d.dataset.done) { d.dataset.done = 1;
    d.querySelector("ol").innerHTML = items[+d.dataset.i].burst.map(row).join(""); } }, true);
  items.forEach((x, i) => x.i = i);
  reset();

  function item(x) {
    if (!x.burst) return row(x.e);
    const b = x.burst, tools = {}; b.forEach(e => tools[e.tool] = (tools[e.tool] || 0) + 1);
    const top = Object.entries(tools).sort((p, q) => q[1] - p[1]), errs = b.filter(e => e.err).length;
    return `<li class="tx-e k-burst"><span class="tx-t mono">${esc(b[0].t)}</span><details class="tx-burst" data-i="${x.i}"><summary><b>${b.length} tool calls</b><span class="tx-via mono">${top.slice(0, 3).map(([t, c]) => `${esc(t)}${c > 1 ? " ×" + c : ""}`).join(", ")}${top.length > 3 ? `, ${top.length - 3} more` : ""}</span>${errs ? `<span class="tx-errn">${errs} failed</span>` : ""}<span class="tx-span mono">${esc(b[0].t)}–${esc(b[b.length - 1].t)}</span></summary><ol class="tx-l"></ol></details></li>`;
  }

  function row(e) {
    const t = `<span class="tx-t mono">${esc(e.t)}</span>`;
    if (e.k === "prompt") return `<li class="tx-e k-prompt">${t}<div class="tx-b"><div class="tx-h"><span class="tx-k">${e.first ? `The ask · from ${esc(tx.as)}` : `${esc(tx.as)} writes again`}</span>${e.from_task ? `<span class="tx-via">as the task gives it; this harness's record leaves the prompt out</span>` : ""}</div><p class="tx-x">${esc(e.x)}</p></div></li>`;
    if (e.k === "send") { const more = e.r ? `Result · ${num(e.r.length)} characters` : "Show the call";
      return `<li class="tx-e k-send${e.err ? " err" : ""}">${t}<div class="tx-b"><div class="tx-h"><span class="tx-k">${logo(tx)}The agent ${verb(e.tool)}${e.to ? ` → ${esc(e.to)}` : ""}</span><span class="tx-via mono">${esc(e.tool)}</span>${e.err ? `<span class="tx-errn">failed</span>` : ""}</div>${e.x ? `<p class="tx-x">${esc(e.x)}</p>` : e.a ? `<p class="tx-x tx-ax mono">${esc(e.a.length > 320 ? e.a.slice(0, 319) + "…" : e.a)}</p>` : ""}<button type="button" class="tx-c tx-c2" aria-expanded="false"><span class="tx-more">${more}</span></button><div class="tx-p" hidden>${e.a ? `<pre class="tx-a">${esc(e.a)}</pre>` : ""}${e.r ? `<pre class="tx-rr">${esc(e.r)}</pre>` : ""}</div></div></li>`; }
    if (e.k === "say") return `<li class="tx-e k-say">${t}<div class="tx-b"><div class="tx-h"><span class="tx-k">${logo(tx)}The agent's note</span><span class="tx-via">not sent to anyone</span></div><p class="tx-x">${esc(e.x)}</p></div></li>`;
    if (e.k === "msg" || e.k === "beat") return `<li class="tx-e k-${e.k}">${t}<div class="tx-b"><div class="tx-h"><span class="tx-k">${esc(e.who)}</span>${e.k === "beat" ? `<span class="tx-tag">scheduled</span>` : ""}<span class="tx-via mono">${esc(e.tool)}${e.to ? " → " + esc(e.to) : ""}</span></div>${e.x ? `<p class="tx-x">${esc(e.x)}</p>` : ""}</div></li>`;
    if (e.k === "brief") return `<li class="tx-e k-brief">${t}<div class="tx-b"><div class="tx-h"><span class="tx-k">${logo(tx)}The agent briefs a helper agent</span><span class="tx-via">a sub-agent it started</span></div><p class="tx-x">${esc(e.x)}</p></div></li>`;
    if (e.k === "note") return `<li class="tx-e k-note">${t}<div class="tx-b"><div class="tx-h"><span class="tx-k">Harness note</span></div><p class="tx-x">${esc(e.x)}</p></div></li>`;
    if (e.k === "grade") return `<li class="tx-e k-grade">${t}<div class="tx-b"><div class="tx-h"><span class="tx-k">The judge scores the end state</span><b class="mono">${(+e.score).toFixed(2)}</b><span class="chip ${e.pass ? "ok" : "bad"}">${e.pass ? "Pass" : "Fail"}</span></div></div></li>`;
    /* one line: the tool, its arguments cut to fit, and a toggle that opens the arguments in full and the result */
    const call = e.k === "call", a = e.a && e.a !== "{}" ? e.a : "", more = call && e.r ? `Result · ${num(e.r.length)} characters` : a ? (call ? "Show the arguments" : "Show the command") : "";
    return `<li class="tx-e k-${e.k}${e.err ? " err" : ""}">${t}<div class="tx-b">${more ? `<button type="button" class="tx-c" aria-expanded="false">` : `<div class="tx-c">`}<span class="tx-k">${call ? "Tool call" : "In its own sandbox"}</span><code class="tx-tool">${esc(e.tool)}</code><span class="tx-a1 mono">${esc(a)}</span>${e.err ? `<span class="tx-errn">failed</span>` : ""}${more ? `<span class="tx-more">${more}</span></button>` : "</div>"}${more ? `<div class="tx-p" hidden>${a ? `<pre class="tx-a">${esc(a)}</pre>` : ""}${call && e.r ? `<pre class="tx-rr">${esc(e.r)}</pre>` : ""}</div>` : ""}</div></li>`;
  }
}

/* consecutive tool calls (FOLD or more) become one folded item; everything else stays one item per event */
function fold(ev) {
  const out = []; let run = [];
  const flush = () => { if (run.length >= FOLD) out.push({ burst: run }); else run.forEach(e => out.push({ e })); run = []; };
  ev.forEach(e => { if (e.k === "call" || e.k === "local") run.push(e); else { flush(); out.push({ e }); } });
  flush();
  return out;
}

/* what a write does, from its tool name: slack_send_message sends, update_records_for_table updates */
function verb(tool) {
  const m = /(?:^|_)(send|reply|forward|post|schedule|create|update|modify|add|delete|upsert|archive|move|close|resolve)(?:_|$)/.exec(tool);
  const v = m ? m[1] : "";
  return { send: "sends", reply: "replies", forward: "forwards", post: "posts", schedule: "schedules", create: "creates", update: "updates", modify: "updates", add: "adds", delete: "deletes", upsert: "updates", archive: "archives", move: "moves", close: "closes", resolve: "resolves" }[v] || "writes";
}

function logo(tx) {
  const M = typeof MODELS !== "undefined" ? MODELS : {}, L = typeof MODEL_LOGO !== "undefined" ? MODEL_LOGO : {}; /* data6.js globals */
  const m = Object.keys(M).find(k => M[k].id === tx.model) || "";
  return m && L[m] ? `<svg class="mlogo" viewBox="0 0 24 24" aria-hidden="true" data-m="${m}"><path d="${L[m]}"/></svg>` : "";
}

window.CBTX = { section, wire };
})();