Files
llama-watch/src/lib/llamaswap.ts
T

34 lines
1.1 KiB
TypeScript

import { type LlamaSwapConfig } from "./util";
const TIMEOUT_MS = 5000;
export interface ModelInfo {
id: string;
status: string;
}
function headersFor(cfg: LlamaSwapConfig): Record<string, string> {
const headers: Record<string, string> = {};
if (cfg.apiKey) headers["Authorization"] = `Bearer ${cfg.apiKey}`;
return headers;
}
export async function fetchMetrics(cfg: LlamaSwapConfig): Promise<string> {
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<ModelInfo[]> {
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" }));
}