File size: 2,055 Bytes
8b41737
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { DatasetInfo, EpisodeData, Preset } from "./types";

const BASE = "/api/arena";
const PRESETS_BASE = "/api/presets/arena";

async function fetchJSON<T>(url: string, opts?: RequestInit): Promise<T> {
  const res = await fetch(`${BASE}${url}`, {
    headers: { "Content-Type": "application/json" },
    ...opts,
  });
  if (!res.ok) {
    const err = await res.json().catch(() => ({ error: res.statusText }));
    throw new Error(err.error || res.statusText);
  }
  return res.json();
}

async function fetchPresetsJSON<T>(url: string, opts?: RequestInit): Promise<T> {
  const res = await fetch(`${PRESETS_BASE}${url}`, {
    headers: { "Content-Type": "application/json" },
    ...opts,
  });
  if (!res.ok) {
    const err = await res.json().catch(() => ({ error: res.statusText }));
    throw new Error(err.error || res.statusText);
  }
  return res.json();
}

export const api = {
  loadDataset(repo: string, split?: string) {
    return fetchJSON<DatasetInfo & { episodes_per_env: Record<string, number> }>("/datasets/load", {
      method: "POST",
      body: JSON.stringify({ repo, split }),
    });
  },

  listDatasets() {
    return fetchJSON<DatasetInfo[]>("/datasets/");
  },

  getEpisode(dsId: string, envId: string, idx: number) {
    return fetchJSON<EpisodeData>(`/datasets/${dsId}/episode/${encodeURIComponent(envId)}/${idx}`);
  },

  unloadDataset(dsId: string) {
    return fetchJSON<{ status: string }>(`/datasets/${dsId}`, { method: "DELETE" });
  },

  listPresets() {
    return fetchPresetsJSON<Preset[]>("");
  },

  createPreset(name: string, repo: string, split?: string) {
    return fetchPresetsJSON<Preset>("", {
      method: "POST",
      body: JSON.stringify({ name, repo, split }),
    });
  },

  updatePreset(id: string, updates: { name?: string; split?: string }) {
    return fetchPresetsJSON<Preset>(`/${id}`, {
      method: "PUT",
      body: JSON.stringify(updates),
    });
  },

  deletePreset(id: string) {
    return fetchPresetsJSON<{ status: string }>(`/${id}`, { method: "DELETE" });
  },
};