File size: 3,156 Bytes
f86ef5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type {
  ApiInfo,
  BaselineResponse,
  GraderResponse,
  HealthResponse,
  StepResponse,
  TaskInfo,
  TaskObservation,
} from '../types'

const PROD = import.meta.env.PROD
const devEnvBase = import.meta.env.VITE_API_URL?.trim()
const normalizedDevEnvBase = devEnvBase ? devEnvBase.replace(/\/+$/, '') : ''
const BASE = PROD ? '/api' : normalizedDevEnvBase || 'http://localhost:8000'

export class ApiError extends Error {
  status: number
  body: string
  url: string

  constructor(status: number, body: string, url: string, message: string) {
    super(message)
    this.name = 'ApiError'
    this.status = status
    this.body = body
    this.url = url
  }
}

function buildUrl(path: string): string {
  const normalizedPath = path.startsWith('/') ? path : `/${path}`
  return `${BASE}${normalizedPath}`
}

async function request<T>(path: string, options?: RequestInit): Promise<T> {
  const url = buildUrl(path)

  try {
    const res = await fetch(url, options)
    if (!res.ok) {
      const err = await res.text()
      throw new ApiError(res.status, err, url, `API error ${res.status}: ${err}`)
    }
    return res.json() as Promise<T>
  } catch (error) {
    if (error instanceof ApiError) {
      throw error
    }
    const message = error instanceof Error ? error.message : String(error)
    throw new ApiError(0, message, url, `Network error: ${message}`)
  }
}

export function getApiInfo(): ApiInfo {
  return {
    base: BASE,
    mode: PROD ? 'proxy' : normalizedDevEnvBase ? 'direct' : 'local',
    env: PROD ? '(disabled in production)' : normalizedDevEnvBase || '(not set)',
  }
}

export async function fetchHealth(): Promise<HealthResponse> {
  return request<HealthResponse>('/health')
}

export async function fetchTasks(): Promise<TaskInfo[]> {
  const data = await request<{
    tasks: string[]
    details: Array<{ id: string; name: string; difficulty: string }>
  }>('/tasks')
  return data.details.map((task) => ({
    id: task.id,
    name: task.name,
    difficulty: task.difficulty,
  }))
}

export async function resetTask(taskId?: string): Promise<TaskObservation> {
  return request<TaskObservation>('/reset', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(taskId ? { task_id: taskId } : {}),
  })
}

export async function submitQuery(query: string): Promise<StepResponse> {
  return request<StepResponse>('/step', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      action: {
        action_type: 'submit_query',
        query,
      },
    }),
  })
}

export async function gradeTask(taskId?: string): Promise<GraderResponse> {
  return request<GraderResponse>('/grader', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(taskId ? { task_id: taskId } : {}),
  })
}

export async function fetchBaseline(taskIds?: string[]): Promise<BaselineResponse> {
  return request<BaselineResponse>('/baseline', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(taskIds?.length ? { tasks: taskIds } : {}),
  })
}