| import type { |
| ArtifactCacheEntry, |
| ArtifactCachePayload, |
| ArtifactSourceCount, |
| BatchRunInput, |
| BatchRunResponse, |
| ResearchRun, |
| ResearchRunInput, |
| SamplesResponse, |
| } from "@fello/contracts"; |
|
|
| const API_BASE_URL = |
| process.env.NEXT_PUBLIC_API_BASE_URL?.replace(/\/$/, "") ?? |
| "http://127.0.0.1:8000"; |
|
|
| async function fetchJson<T>(path: string, init?: RequestInit): Promise<T> { |
| const response = await fetch(`${API_BASE_URL}${path}`, { |
| ...init, |
| headers: { |
| "Content-Type": "application/json", |
| ...(init?.headers ?? {}), |
| }, |
| cache: "no-store", |
| }); |
|
|
| if (!response.ok) { |
| const detail = await response.text(); |
| throw new Error(detail || `Request failed with status ${response.status}`); |
| } |
|
|
| return (await response.json()) as T; |
| } |
|
|
| export function getApiBaseUrl(): string { |
| return API_BASE_URL; |
| } |
|
|
| export function getSamples(): Promise<SamplesResponse> { |
| return fetchJson<SamplesResponse>("/samples"); |
| } |
|
|
| export function createRun(payload: ResearchRunInput): Promise<ResearchRun> { |
| return fetchJson<ResearchRun>("/runs", { |
| method: "POST", |
| body: JSON.stringify(payload), |
| }); |
| } |
|
|
| export function createBatchRuns(payload: BatchRunInput): Promise<BatchRunResponse> { |
| return fetchJson<BatchRunResponse>("/runs/batch", { |
| method: "POST", |
| body: JSON.stringify(payload), |
| }); |
| } |
|
|
| export function getRun(runId: string): Promise<ResearchRun> { |
| return fetchJson<ResearchRun>(`/runs/${runId}`); |
| } |
|
|
| export function listDebugRuns(limit = 60): Promise<ResearchRun[]> { |
| const value = Number.isFinite(limit) ? Math.max(1, Math.min(200, limit)) : 60; |
| return fetchJson<ResearchRun[]>(`/debug/runs?limit=${value}`); |
| } |
|
|
| export function listArtifactSources(): Promise<ArtifactSourceCount[]> { |
| return fetchJson<ArtifactSourceCount[]>("/debug/artifacts/sources"); |
| } |
|
|
| export function listArtifacts(params?: { source?: string; limit?: number }): Promise<ArtifactCacheEntry[]> { |
| const source = params?.source?.trim() ?? ""; |
| const rawLimit = params?.limit ?? 80; |
| const limit = Number.isFinite(rawLimit) ? Math.max(1, Math.min(400, rawLimit)) : 80; |
| const query = new URLSearchParams(); |
| if (source) { |
| query.set("source", source); |
| } |
| query.set("limit", String(limit)); |
| return fetchJson<ArtifactCacheEntry[]>(`/debug/artifacts?${query.toString()}`); |
| } |
|
|
| export function getArtifact(artifactId: number): Promise<ArtifactCachePayload> { |
| return fetchJson<ArtifactCachePayload>(`/debug/artifacts/${artifactId}`); |
| } |
|
|