diff --git a/src/lib/llamaswap.ts b/src/lib/llamaswap.ts new file mode 100644 index 0000000..68fb9e6 --- /dev/null +++ b/src/lib/llamaswap.ts @@ -0,0 +1,33 @@ +import { type LlamaSwapConfig } from "./util"; + +const TIMEOUT_MS = 5000; + +export interface ModelInfo { + id: string; + status: string; +} + +function headersFor(cfg: LlamaSwapConfig): Record { + const headers: Record = {}; + if (cfg.apiKey) headers["Authorization"] = `Bearer ${cfg.apiKey}`; + return headers; +} + +export async function fetchMetrics(cfg: LlamaSwapConfig): Promise { + const res = await fetch(`${cfg.baseUrl}/metrics`, { + headers: headersFor(cfg), + signal: AbortSignal.timeout(TIMEOUT_MS), + }); + if (!res.ok) throw new Error(`metrics HTTP ${res.status}`); + return await res.text(); +} + +export async function fetchModels(cfg: LlamaSwapConfig): Promise { + const res = await fetch(`${cfg.baseUrl}/v1/models`, { + headers: headersFor(cfg), + signal: AbortSignal.timeout(TIMEOUT_MS), + }); + if (!res.ok) throw new Error(`models HTTP ${res.status}`); + const body = (await res.json()) as { data?: { id: string; status?: { value?: string } }[] }; + return (body.data ?? []).map((m) => ({ id: m.id, status: m.status?.value ?? "unknown" })); +}