// What a ready inference server says about itself: version, slots and context. llama.cpp: GET /props // (build_info "b11429-d81235049", total_slots, default_generation_settings.n_ctx; checked with build // 11429). vLLM: GET /version and GET /v1/models (max_model_len). Startup log lines as a fallback. import type { EngineInfo, RuntimeEngine } from "@/lib/db/schema"; type Json = Record; const int = (v: unknown) => (typeof v === "number" && Number.isInteger(v) && v > 0 ? v : null); const str = (v: unknown) => (typeof v === "string" && v.trim() ? v.trim().slice(0, 100) : null); export function parseLlamaProps(props: Json | null): Omit { const gen = (props?.default_generation_settings ?? {}) as Json; return { version: str(props?.build_info), slots: int(props?.total_slots), contextSize: int(gen.n_ctx) }; } export function parseVllm(version: Json | null, models: Json | null): Omit { const data = Array.isArray(models?.data) ? (models.data as Json[]) : []; return { version: str(version?.version), slots: null, contextSize: int(data[0]?.max_model_len) }; } /** "build: 11429 (d8123504)" (llama.cpp), "vLLM API server version 0.31.0" (vLLM and most forks). */ export function versionFromLog(engine: RuntimeEngine, lines: string[]): string | null { const re = engine === "llama.cpp" ? /\bbuild[:=]\s*(\d+)\s*\(([0-9a-f]+)\)/ : /\bversion\s+v?(\d+\.\d+[\w.+-]*)/i; for (const l of lines) { const m = l.match(re); if (m) return engine === "llama.cpp" ? `b${m[1]}-${m[2]}` : m[1]; } return null; } async function getJson(url: string, key: string | null): Promise { try { const res = await fetch(url, { headers: key ? { Authorization: `Bearer ${key}` } : {}, signal: AbortSignal.timeout(3_000) }); return res.ok ? ((await res.json()) as Json) : null; } catch { return null; } } /** Ask the server at http://name:port; `key` when the deployment requires an API key. */ export async function fetchEngineInfo(engine: RuntimeEngine, base: string, key: string | null): Promise> { if (engine === "llama.cpp") return parseLlamaProps(await getJson(`${base}/props`, key)); const [version, models] = await Promise.all([getJson(`${base}/version`, key), getJson(`${base}/v1/models`, key)]); return parseVllm(version, models); }