File size: 3,761 Bytes
c52f47a
 
67acd34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d7b4ed
 
 
 
 
 
 
 
 
67acd34
 
 
 
 
 
 
c52f47a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67acd34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c52f47a
67acd34
 
 
 
 
 
 
 
 
 
4d7b4ed
 
 
 
 
 
 
 
 
 
 
67acd34
 
 
 
 
 
 
 
 
 
 
 
c52f47a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67acd34
 
 
 
 
 
 
 
 
 
 
 
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
import { adminFetch } from "./admin";

export type Difficulty = "easy" | "medium" | "hard";
export type PuzzleType =
  | "bridges"
  | "flow_free"
  | "galaxies"
  | "loopy"
  | "pattern"
  | "undead";

export type SessionResponse = {
  session_id: string;
  engine: string;
  puzzle_type: PuzzleType;
  difficulty: Difficulty;
  puzzle_id: string;
  args: string;
  status: string;
  started_at: string | null;
  payload: {
    problem_ascii: string;
    current_board_ascii: string;
    image_base64: string | null;
  };
};

export type PuzzleOption = {
  puzzle_id: string;
  title: string;
  sequence: number;
  puzzle_type: PuzzleType;
  difficulty: Difficulty;
  args: string;
};

export type SubmitResponse = {
  solved: boolean;
  elapsed_ms: number | null;
  status: string;
  verification: Record<string, unknown>;
};

export type LLMResults = {
  puzzle_id: string;
  models_solved: string[];
  models_failed: string[];
};

export type LeaderboardEntry = {
  player_name: string;
  elapsed_ms: number | null;
  submission_count: number;
  solved: boolean;
  started_at: string | null;
  submitted_at: string | null;
};

export type Leaderboard = {
  puzzle_id: string;
  entries: LeaderboardEntry[];
};

async function handle<T>(response: Response): Promise<T> {
  if (!response.ok) {
    let message = response.statusText;
    try {
      const payload = (await response.json()) as { detail?: string };
      message = payload.detail ?? message;
    } catch {
      // Fall back to the HTTP status text.
    }
    throw new Error(message);
  }
  return (await response.json()) as T;
}

export async function createSession(input: {
  player_name: string;
  puzzle_type: PuzzleType;
  difficulty: Difficulty;
  puzzle_id?: string;
}): Promise<SessionResponse> {
  return handle<SessionResponse>(
    await fetch("/api/sessions", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(input),
    }),
  );
}

export async function fetchPuzzleOptions(
  puzzleType: PuzzleType,
  difficulty: Difficulty,
): Promise<PuzzleOption[]> {
  const params = new URLSearchParams({
    puzzle_type: puzzleType,
    difficulty,
  });
  return handle<PuzzleOption[]>(await fetch(`/api/puzzles?${params.toString()}`));
}

export async function fetchSession(sessionId: string): Promise<SessionResponse> {
  return handle<SessionResponse>(await fetch(`/api/sessions/${sessionId}`));
}

export async function readySession(
  sessionId: string,
): Promise<{ status: string; started_at: string | null }> {
  return handle<{ status: string; started_at: string | null }>(
    await fetch(`/api/sessions/${sessionId}/ready`, { method: "POST" }),
  );
}

export async function fetchLeaderboard(
  puzzleId: string,
  options: { includeTest?: boolean } = {},
): Promise<Leaderboard> {
  const params = new URLSearchParams();
  if (options.includeTest) {
    params.set("include_test", "true");
  }
  const qs = params.toString() ? `?${params.toString()}` : "";
  const response = await adminFetch(
    `/api/admin/leaderboard/${encodeURIComponent(puzzleId)}${qs}`,
  );
  return handle<Leaderboard>(response);
}

export async function fetchLLMResults(puzzleId: string): Promise<LLMResults | null> {
  const response = await fetch(`/api/llm-results/${encodeURIComponent(puzzleId)}`);
  if (response.status === 404) {
    return null;
  }
  return handle<LLMResults>(response);
}

export async function submitSession(
  sessionId: string,
  boardAscii: string,
): Promise<SubmitResponse> {
  return handle<SubmitResponse>(
    await fetch(`/api/sessions/${sessionId}/submit`, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ board_ascii: boardAscii }),
    }),
  );
}