// Where the explorer reads from: in development the local export (public/data), on the Space the private dataset, // with the signed-in viewer's own token. import type { RecEpisode, RecIndex, RecRow } from "./records"; import type { Episode, Index, Version } from "./types"; // One dataset per task line. kind "cb" = full CooperBench trajectories, "records" = curves, scores, per-episode stats and a few conversations. export interface Project { id: string; name: string; dataset: string; kind: "cb" | "records" } export const PROJECTS: Project[] = [ { id: "cooperbench", name: "CooperBench", dataset: "dsh-marl/cooperbench-grow-trajectories", kind: "cb" }, { id: "math", name: "数学", dataset: "dsh-marl/math-team-runs", kind: "records" }, { id: "agentgym", name: "AgentGym", dataset: "dsh-marl/agentgym-team-runs", kind: "records" }, ]; let project: Project = PROJECTS[0]; export const setProject = (p: Project) => { project = p; }; let token: string | null = null; export const setToken = (t: string | null) => { token = t; }; const url = (path: string) => import.meta.env.DEV ? `${import.meta.env.BASE_URL}data/${project.id}/${path}` : `https://huggingface.co/datasets/${project.dataset}/resolve/main/${path}`; async function bytes(path: string): Promise { const r = await fetch(url(path), { headers: token ? { Authorization: `Bearer ${token}` } : {} }); if ((r.status === 401 || r.status === 403) && !import.meta.env.DEV) dispatchEvent(new Event("auth-expired")); if (!r.ok) throw new Error(`${r.status} ${path}`); return new Uint8Array(await r.arrayBuffer()); } async function json(path: string): Promise { let b = await bytes(path); if (b[0] === 0x1f && b[1] === 0x8b) { // gzip, unless the server already undid it const s = new Blob([b]).stream().pipeThrough(new DecompressionStream("gzip")); b = new Uint8Array(await new Response(s).arrayBuffer()); } return JSON.parse(new TextDecoder().decode(b)) as T; } const cache = new Map>(); function cached(path: string): Promise { const k = project.id + ":" + path; if (!cache.has(k)) cache.set(k, json(path).catch((e) => { cache.delete(k); throw e; })); return cache.get(k) as Promise; } export const loadVersions = () => cached("versions.json"); export const loadIndex = (v: string) => cached(`${v}/site/index.json`); export const loadEpisode = (v: string, id: string) => { const [run, ...rest] = id.split("/"); return cached(`${v}/site/episodes/${run}/${rest.join("/")}.json.gz`); }; export const loadRecIndex = (v: string) => cached(`${v}/site/index.json`); export const loadRecRows = (v: string) => cached(`${v}/site/episodes.json.gz`); export const loadRecEpisode = (v: string, file: string) => cached(`${v}/site/${file}`);