File size: 1,894 Bytes
b89c27d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Thin wrappers around the FastAPI endpoints.

export async function envReset(params = {}) {
  const r = await fetch('/reset', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(params),
  })
  if (!r.ok) throw new Error(`reset failed: ${r.status}`)
  return r.json()
}

export async function envStep(action) {
  const r = await fetch('/step', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ action }),
  })
  if (!r.ok) throw new Error(`step failed: ${r.status}`)
  return r.json()
}

export async function getLandscape(params) {
  const r = await fetch('/api/landscape', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(params),
  })
  if (!r.ok) throw new Error(`landscape failed: ${r.status}`)
  return r.json()
}

export async function getBaselineRace(params) {
  const r = await fetch('/api/baseline_race', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(params),
  })
  if (!r.ok) throw new Error(`baseline_race failed: ${r.status}`)
  return r.json()
}

export async function runArena(params) {
  const r = await fetch('/api/arena', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(params),
  })
  if (!r.ok) throw new Error(`arena failed: ${r.status}`)
  return r.json()
}

export function llmRunStream(params, onEvent) {
  // SSE stream: each chunk is a JSON event
  const url = '/api/llm_run?' + new URLSearchParams(params).toString()
  const es = new EventSource(url)
  es.onmessage = ev => {
    try {
      onEvent(JSON.parse(ev.data))
    } catch (e) {
      console.error('bad SSE payload', ev.data, e)
    }
  }
  es.addEventListener('end', () => es.close())
  es.onerror = () => es.close()
  return () => es.close()
}