// Episode-to-scene map read from a rollout dataset's card (README.md). // // Rollout READMEs carry a markdown table that maps episode ranges to the // (bowl point, ball point) scene, e.g. // // | Episodes | Scene | | Scene | Episodes | Bowl–Ball | Status | // |---|---| |---|---|---|---| // | 0-4 | 1-6 | | 1 | 0–4 | 1-6 | valid | // | 5-9 | 1-5 | | — | 20–24 | 1-2 | **discard** … | // // The parser is header driven: the episodes column is the one whose header // mentions "episode"; the scene column is the one headed "bowl" (or // "bowl–ball"), else the one headed "scene" whose cells look like "1-6"; a // "status" column marks discards. Ranges accept "-", "–" and "—". Anything // that does not fit is ignored, so a README without such a table yields an // empty map and the panel simply leaves the scene field for the reviewer. import { hubResolveUrl } from "./repoRef"; import { getAuthToken } from "./auth"; const HUB = "https://huggingface.co"; const SCENE_RE = /^\d+\s*[-–—]\s*\d+$/; const RANGE_RE = /^(\d+)\s*(?:[-–—]\s*(\d+))?$/; export interface SceneEntry { scene: string; /** the card marks the episode as discarded / invalid */ discard: boolean; } export type SceneMap = Map; function splitRow(line: string): string[] { return line .trim() .replace(/^\|/, "") .replace(/\|$/, "") .split("|") .map((c) => c.replace(/\*\*/g, "").replace(/`/g, "").trim()); } function isSeparator(line: string): boolean { return ( /^\s*\|?\s*:?-{2,}/.test(line) && !/[a-z0-9]/i.test(line.replace(/[-:|\s]/g, "")) ); } /** Normalise a scene cell: "1–6" and "1 - 6" become "1-6". */ function normScene(s: string): string { return s.replace(/\s+/g, "").replace(/[–—]/g, "-"); } /** Parse every markdown table in the README; later tables override earlier * ones for the same episode (a card that lists a session twice). */ export function parseSceneMap(readme: string): SceneMap { const map: SceneMap = new Map(); const lines = readme.split(/\r?\n/); for (let i = 0; i + 1 < lines.length; i++) { if (!lines[i].includes("|") || !isSeparator(lines[i + 1])) continue; const header = splitRow(lines[i]).map((h) => h.toLowerCase()); const epCol = header.findIndex((h) => /episode/.test(h)); if (epCol < 0) continue; let sceneCol = header.findIndex( (h) => /bowl/.test(h) && h !== header[epCol], ); const statusCol = header.findIndex((h) => /status/.test(h)); // collect body rows const rows: string[][] = []; for (let j = i + 2; j < lines.length && lines[j].includes("|"); j++) { rows.push(splitRow(lines[j])); } if (sceneCol < 0) { // a "scene" column counts only when its cells look like "1-6" const cand = header.findIndex((h, k) => /scene/.test(h) && k !== epCol); if (cand >= 0 && rows.some((r) => SCENE_RE.test(r[cand] ?? ""))) sceneCol = cand; } if (sceneCol < 0) continue; for (const r of rows) { const m = RANGE_RE.exec(r[epCol] ?? ""); const scene = r[sceneCol] ?? ""; if (!m || !SCENE_RE.test(scene)) continue; const lo = Number(m[1]); const hi = m[2] !== undefined ? Number(m[2]) : lo; if (hi < lo || hi - lo > 10000) continue; const discard = statusCol >= 0 && /discard|invalid|exclude/i.test(r[statusCol] ?? ""); for (let e = lo; e <= hi; e++) map.set(e, { scene: normScene(scene), discard }); } i += 1 + rows.length; } return map; } /** Fetch and parse the dataset card. Empty map when there is none. */ export async function fetchSceneMap(repoId: string): Promise { const token = getAuthToken(); try { const res = await fetch( hubResolveUrl(`${HUB}/datasets`, repoId, "README.md"), { headers: token ? { Authorization: `Bearer ${token}` } : {}, cache: "no-store", }, ); if (!res.ok) return new Map(); return parseSceneMap(await res.text()); } catch { return new Map(); } }