Spaces:
Running
Running
Download src/Thread.tsx from dsh-marl/trajectory-explorer: direct link, hf CLI and curl.
- Browser
- Download file 10.9 kB
-
https://huggingface.co/spaces/dsh-marl/trajectory-explorer/resolve/main/src/Thread.tsx
- Command line
-
hf download hf://spaces/dsh-marl/trajectory-explorer/src/Thread.tsx
-
curl -L -o Thread.tsx https://huggingface.co/spaces/dsh-marl/trajectory-explorer/resolve/main/src/Thread.tsx
10.9 kB
| // 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> | |
| ); | |
| } | |