feat: add llama-swap HTTP client
This commit is contained in:
@@ -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<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" }));
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user