Spaces:
Running
Running
File size: 10,887 Bytes
d12e2ce | 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 | // A full episode of the math / AgentGym records.
// Thread: the lead's conversation as a channel, each teammate as a thread opened in a side pane (as in Slack).
// GameReview: the environment actions as a move list beside the current state, with the score below (as in lichess).
import { useEffect, useMemo, useRef, useState } from "react";
import { LineChart } from "./Charts";
import { normMsg, type RecAgent, type RecEpisode } from "./records";
import type { Message } from "./types";
const initial = (a: RecAgent) => (a.role === "lead" ? "L" : (a.label || a.id)[0].toUpperCase());
const parse = (s: string) => { try { return JSON.parse(s); } catch { return null; } };
const one = (s: string) => (s || "").trim().split("\n", 1)[0];
function Clamp({ text, lines, className }: { text: string; lines: number; className: string }) {
const [open, setOpen] = useState(false);
const long = text.split("\n").length > lines || text.length > lines * 110;
return (
<>
<div className={className + (open || !long ? "" : " clamp")} style={{ WebkitLineClamp: open ? undefined : lines }}>{text}</div>
{long && <button className="linkbtn" onClick={() => setOpen(!open)}>{open ? "收起" : "展开"}</button>}
</>
);
}
function ToolCall({ name, args, result }: { name: string; args: string; result?: Message }) {
const [open, setOpen] = useState(false);
const a = parse(args);
const cmd = name === "bash" ? String(a?.command ?? args) : a && typeof a === "object"
? Object.entries(a).map(([k, v]) => `${k}: ${typeof v === "string" ? v : JSON.stringify(v)}`).join("\n") : args;
return (
<div className="att">
<div className="att-h" onClick={() => setOpen(!open)}>
<span className="att-tool">{name}</span><span className="att-cmd">{one(cmd)}</span>
{result && <span className="att-more">{open ? "收起" : "看输出"}</span>}
</div>
{open && <>
{cmd.includes("\n") && <pre className="att-full">{cmd}</pre>}
{result && <pre className="att-out">{result.content || "(无输出)"}</pre>}
</>}
</div>
);
}
function MessageView({ m, agent, results, threads, onThread, openThread, firstUser }: {
m: Message; agent: RecAgent; firstUser: boolean; results: Map<string, Message>; threads: RecAgent[]; onThread: (id: string) => void; openThread: string | null;
}) {
if (m.role === "tool") return null;
if (m.role === "system" || m.role === "user") return (
<div className="smsg sys"><div className="s-av sys">{m.role === "system" ? "S" : "任"}</div>
<div className="s-b"><div className="s-name">{m.role === "system" ? "系统提示" : firstUser ? "任务" : "运行环境插入的消息"}</div><Clamp text={m.content} lines={4} className="s-text dim" /></div></div>
);
return (
<div className="smsg">
<div className={"s-av " + (agent.role === "lead" ? "lead" : "mate")}>{initial(agent)}</div>
<div className="s-b">
<div className="s-name">{agent.label || agent.id}</div>
{m.reasoning && <Clamp text={m.reasoning} lines={3} className="s-text think" />}
{m.content && <Clamp text={m.content} lines={14} className="s-text" />}
{m.tool_calls.map((tc) => <ToolCall key={tc.id || tc.name} name={tc.name} args={tc.arguments} result={results.get(tc.id)} />)}
{threads.map((t) => (
<button key={t.id} className={"threadlink" + (openThread === t.id ? " on" : "")} onClick={() => onThread(t.id)}>
<span className="s-av mate sm">{initial(t)}</span>
<b>{t.label || t.id}</b><span>{t.messages.filter((x) => x.role === "assistant").length} 条回复</span>
<span className="tl-go">{openThread === t.id ? "正在看" : "查看话题 ›"}</span>
</button>
))}
</div>
</div>
);
}
function Conversation({ agent, all, openThread, onThread }: { agent: RecAgent; all: RecAgent[]; openThread: string | null; onThread: (id: string) => void }) {
const msgs = useMemo(() => agent.messages.map((m) => normMsg(m)), [agent]);
const results = useMemo(() => new Map(msgs.filter((m) => m.role === "tool" && m.tool_call_id).map((m) => [m.tool_call_id, m])), [msgs]);
const threadsAt = (i: number) => agent.spawns.filter((s) => s.message === i).map((s) => all.find((a) => a.id === s.agent)).filter((a): a is RecAgent => !!a);
const firstUser = msgs.findIndex((m) => m.role === "user");
return <>{msgs.map((m, i) => <MessageView key={i} m={m} agent={agent} firstUser={i === firstUser} results={results} threads={threadsAt(i)} onThread={onThread} openThread={openThread} />)}</>;
}
export default function Thread({ ep }: { ep: RecEpisode }) {
const lead = ep.agents.find((a) => a.role === "lead") ?? ep.agents[0];
const [open, setOpen] = useState<string | null>(null);
const mate = ep.agents.find((a) => a.id === open);
const orphan = ep.agents.filter((a) => a !== lead && !lead.spawns.some((s) => s.agent === a.id));
useEffect(() => setOpen(null), [ep]);
return (
<div className={"slack" + (mate ? " with-thread" : "")}>
<section className="pane chan">
<div className="pane-h"><h3># lead</h3><span className="sub">{lead.messages.length} 条消息{ep.agents.length > 1 ? ` · ${ep.agents.length - 1} 个队友,点「查看话题」看队友那边` : ""}</span></div>
<div className="pane-b sbody">
<Conversation agent={lead} all={ep.agents} openThread={open} onThread={(id) => setOpen(open === id ? null : id)} />
{orphan.length > 0 && (
<div className="smsg sys"><div className="s-av sys">+</div><div className="s-b"><div className="s-name">没对上派出时间的队友</div>
{orphan.map((t) => <button key={t.id} className="threadlink" onClick={() => setOpen(t.id)}><span className="s-av mate sm">{initial(t)}</span><b>{t.label || t.id}</b><span className="tl-go">查看话题 ›</span></button>)}</div></div>
)}
</div>
</section>
{mate && (
<section className="pane thread">
<div className="pane-h"><h3>话题 · {mate.label || mate.id}</h3><span className="sub">{mate.reward != null ? `得分 ${mate.reward}` : ""}</span>
<button className="iconbtn" style={{ marginLeft: "auto" }} onClick={() => setOpen(null)}>✕</button></div>
<div className="pane-b sbody"><Conversation agent={mate} all={ep.agents} openThread={null} onThread={() => {}} /></div>
</section>
)}
</div>
);
}
// ---------- game review ----------
export function GameReview({ ep }: { ep: RecEpisode }) {
const steps = ep.env_steps;
const [k, setK] = useState(0);
const list = useRef<HTMLDivElement>(null);
useEffect(() => setK(0), [ep]);
useEffect(() => {
const f = (e: KeyboardEvent) => {
if ((e.target as HTMLElement)?.tagName === "INPUT" || (e.target as HTMLElement)?.tagName === "SELECT") return;
if (e.key === "ArrowRight" || e.key === "ArrowDown") { setK((x) => Math.min(steps.length - 1, x + 1)); e.preventDefault(); }
if (e.key === "ArrowLeft" || e.key === "ArrowUp") { setK((x) => Math.max(0, x - 1)); e.preventDefault(); }
if (e.key === "Home") setK(0);
if (e.key === "End") setK(steps.length - 1);
};
addEventListener("keydown", f); return () => removeEventListener("keydown", f);
}, [steps.length]);
useEffect(() => { list.current?.querySelector(`[data-k="${k}"]`)?.scrollIntoView({ block: "nearest" }); }, [k]);
// carry the last known score forward, so the bar and the curve exist at every move
const scores = useMemo(() => { let last: number | null = null; return steps.map((s) => (s.score != null ? (last = s.score) : last)); }, [steps]);
const cur = steps[k];
const sc = scores[k];
const label = (a: string) => ep.agents.find((x) => x.id === a)?.label || a;
const tgt = (t: string) => (t === "main" ? "主局" : t.replace(/^copy-?/, "副本 "));
const maxScore = Math.max(1, ...scores.filter((x): x is number => x != null));
return (
<div className="review">
<section className="pane board">
<div className="pane-h"><h3>环境</h3><span className="sub">第 {k + 1} / {steps.length} 步 · ← → 逐步</span></div>
<div className="board-b">
<div className="evalbar" title={sc == null ? "这一步没有得分" : `得分 ${sc}`}>
<i style={{ height: `${sc == null ? 0 : (100 * sc) / maxScore}%` }} /><span>{sc == null ? "–" : sc.toFixed(2)}</span>
</div>
<div className="board-main">
{cur && <>
<div className="b-who"><span className={"tag " + (cur.target === "main" ? "plain" : "warn")}>{tgt(cur.target)}</span><b>{label(cur.agent)}</b></div>
<div className="b-act">{cur.action}</div>
<div className="b-k">环境返回</div>
<pre className="b-obs">{cur.observation || "(无)"}</pre>
</>}
<details className="b-task"><summary>任务原文</summary><pre>{ep.instruction}</pre></details>
</div>
</div>
</section>
<section className="pane moves">
<div className="pane-h"><h3>动作</h3><span className="sub">主局是正式的一局,副本是试走用的拷贝</span>
<span style={{ flex: 1 }} />
<button className="btn" onClick={() => setK(0)}>⏮</button><button className="btn" onClick={() => setK(Math.max(0, k - 1))}>‹</button>
<button className="btn" onClick={() => setK(Math.min(steps.length - 1, k + 1))}>›</button><button className="btn" onClick={() => setK(steps.length - 1)}>⏭</button></div>
<div className="pane-b mlist" ref={list}>
{steps.map((s, i) => {
const d = scores[i] != null && (i === 0 ? scores[i]! : scores[i]! - (scores[i - 1] ?? 0));
return (
<div key={i} data-k={i} className={"mv" + (i === k ? " on" : "") + (s.target === "main" ? "" : " copy")} onClick={() => setK(i)}>
<span className="mv-n">{i + 1}</span>
<span className="mv-t">{tgt(s.target)}</span>
<span className="mv-a">{s.action}</span>
{typeof d === "number" && d !== 0 && <span className={"mv-d " + (d > 0 ? "up" : "down")}>{d > 0 ? "+" : ""}{d.toFixed(2)}</span>}
</div>
);
})}
</div>
</section>
<section className="pane curve">
<div className="pane-h"><h3>得分随步数</h3><span className="sub">点曲线上的一步跳过去</span></div>
<div className="card-b" onClick={() => {}}>
<LineChart xs={steps.map((_, i) => i + 1)} series={[{ name: "得分", color: "var(--orch)", values: new Map(scores.map((v, i) => [i + 1, v])) }]}
fmt={(v) => v.toFixed(2)} height={170} hover={k + 1} onHover={(x) => { if (x != null) setK(x - 1); }} />
</div>
</section>
</div>
);
}
|