// Which downloaded files a runtime can serve. Pure; shared by the server and the deployment form. import type { RuntimeEngine } from "@/lib/db/schema"; import { groupSplitFiles } from "@/lib/file-groups"; import { fileRole } from "@/lib/file-roles"; export type LocalFile = { path: string; size: number }; export type CandidateFile = { path: string; size: number; parts: number }; export type DraftFile = CandidateFile & { type: "draft-mtp" | "draft-simple" }; export type ModelCandidates = { /** llama.cpp: loadable GGUF weights (split files as their first part), plus optional extras. */ gguf: CandidateFile[]; mmproj: CandidateFile[]; drafts: DraftFile[]; /** vLLM: the repo folder has a config.json and safetensors weights. */ vllm: { size: number } | null; }; const isGguf = (p: string) => p.toLowerCase().endsWith(".gguf"); /** `files`: the model's files that are on disk (present or verified). */ export function modelCandidates(files: LocalFile[]): ModelCandidates { const entries = (pred: (p: string) => boolean) => groupSplitFiles(files.filter((f) => pred(f.path))).map((g) => ({ path: g.parts[0].path, size: g.size, parts: g.totalParts })); const gguf = entries((p) => isGguf(p) && fileRole(p) === "model"); const mmproj = entries((p) => isGguf(p) && fileRole(p) === "vision"); const drafts: DraftFile[] = [ ...entries((p) => isGguf(p) && fileRole(p) === "mtp").map((f) => ({ ...f, type: "draft-mtp" as const })), ...entries((p) => isGguf(p) && fileRole(p) === "draft").map((f) => ({ ...f, type: "draft-simple" as const })), ]; const hasConfig = files.some((f) => f.path === "config.json"); const safetensors = files.filter((f) => f.path.endsWith(".safetensors") && fileRole(f.path) === "model"); const vllm = hasConfig && safetensors.length > 0 ? { size: files.reduce((s, f) => s + f.size, 0) } : null; return { gguf, mmproj, drafts, vllm }; } export function servableBy(c: ModelCandidates, engine: RuntimeEngine): boolean { return engine === "llama.cpp" ? c.gguf.length > 0 : c.vllm !== null; }