| import { API_BASE } from "./utils" |
|
|
| export async function apiFetch(path: string, opts: RequestInit = {}) { |
| const res = await fetch(`${API_BASE}${path}`, { |
| ...opts, |
| headers: { |
| "Content-Type": "application/json", |
| ...(opts.headers || {}), |
| }, |
| }) |
| if (!res.ok) { |
| const txt = await res.text() |
| let msg = txt |
| try { msg = JSON.parse(txt).detail || txt } catch {} |
| throw new Error(msg) |
| } |
| |
| if (res.headers.get("content-type")?.includes("text/event-stream")) return res |
| const ct = res.headers.get("content-type") |
| if (ct?.includes("application/json")) return res.json() |
| return res |
| } |
|
|
| export async function loadModel(payload: any) { |
| return apiFetch("/api/model/load", { method: "POST", body: JSON.stringify(payload) }) |
| } |
| export async function getModelStatus() { return apiFetch("/api/model/status") } |
| export async function unloadModel() { return apiFetch("/api/model/unload", { method: "DELETE" }) } |
| export async function validatePath(path: string) { return apiFetch(`/api/model/validate?path=${encodeURIComponent(path)}`) } |
| export async function getTelemetry() { return apiFetch("/api/telemetry/") } |
| export async function getHistory(limit=120) { return apiFetch(`/api/telemetry/history?limit=${limit}`) } |
| export async function runBenchmark(payload: any) { return apiFetch("/api/benchmark/run", { method: "POST", body: JSON.stringify(payload) }) } |
| export async function getBenchmarkSuites() { return apiFetch("/api/benchmark/suites") } |
| export async function getBenchmarkResults() { return apiFetch("/api/benchmark/results") } |
| export async function scanCustomFolder(folder_path: string) { return apiFetch(`/api/benchmark/custom/scan?folder_path=${encodeURIComponent(folder_path)}`, { method: "POST"}) } |
| export async function runCustomFromFolder(folder_path: string, judge_mode="regex", temperature=0.2) { return apiFetch(`/api/benchmark/custom/run-from-folder?folder_path=${encodeURIComponent(folder_path)}&judge_mode=${judge_mode}&temperature=${temperature}`, { method: "POST"}) } |
| export async function uploadCustomDataset(files: FileList, judge_mode="regex") { |
| const fd = new FormData() |
| Array.from(files).forEach(f=> fd.append("files", f)) |
| fd.append("judge_mode", judge_mode) |
| const res = await fetch(`${API_BASE}/api/benchmark/custom/upload`, { method: "POST", body: fd }) |
| if (!res.ok) throw new Error(await res.text()) |
| return res.json() |
| } |
| export async function getCustomFormats() { return apiFetch("/api/benchmark/custom/formats") } |
| export async function createShare(report_id: string) { return apiFetch(`/api/share/${report_id}`, { method: "POST"}) } |
| export async function getSharedReport(token: string) { return apiFetch(`/api/share/${token}`) } |
| export async function getPdfPreview() { return apiFetch(`/api/export/pdf/preview`) } |
| export async function runBenchmarkStream(payload: any, onEvent: (ev:any)=>void) { |
| const res = await fetch(`${API_BASE}/api/benchmark/run-stream`, { |
| method: "POST", |
| headers: {"Content-Type":"application/json"}, |
| body: JSON.stringify(payload) |
| }) |
| if (!res.ok) { |
| const txt = await res.text() |
| throw new Error(txt) |
| } |
| const reader = res.body?.getReader() |
| const decoder = new TextDecoder() |
| let buffer="" |
| if (!reader) throw new Error("No reader") |
| while(true){ |
| const {done, value} = await reader.read() |
| if (done) break |
| buffer += decoder.decode(value, {stream:true}) |
| const parts = buffer.split("\n\n") |
| buffer = parts.pop() || "" |
| for (const part of parts){ |
| if (part.startsWith("data: ")){ |
| const data = part.slice(6) |
| try{ const j=JSON.parse(data); onEvent(j) }catch{} |
| } |
| } |
| } |
| } |
|
|
| |
| export async function streamGenerate(payload: any, onChunk: (chunk: any)=>void, onDone?: ()=>void, onError?: (e:any)=>void) { |
| try { |
| const res = await fetch(`${API_BASE}/api/generate`, { |
| method: "POST", |
| headers: {"Content-Type":"application/json"}, |
| body: JSON.stringify({...payload, stream: true}) |
| }) |
| if (!res.ok) { |
| const txt = await res.text() |
| throw new Error(txt) |
| } |
| const reader = res.body?.getReader() |
| const decoder = new TextDecoder() |
| let buffer = "" |
| if (!reader) throw new Error("No reader") |
| while (true) { |
| const { done, value } = await reader.read() |
| if (done) break |
| buffer += decoder.decode(value, { stream: true }) |
| const lines = buffer.split("\n\n") |
| buffer = lines.pop() || "" |
| for (const line of lines) { |
| if (line.startsWith("data: ")) { |
| const data = line.slice(6) |
| try { |
| const json = JSON.parse(data) |
| onChunk(json) |
| } catch {} |
| } |
| } |
| } |
| onDone?.() |
| } catch (e) { |
| onError?.(e) |
| } |
| } |
|
|