import "server-only"; import { asc, eq } from "drizzle-orm"; import { z } from "zod"; import { db } from "@/lib/db/client"; import { type DockerHostInfo, RUNTIME_ACCELERATORS, runtimes, workerState } from "@/lib/db/schema"; import { deploymentsUsing } from "@/lib/deployments"; import { type RuntimeInput, RUNTIME_PRESETS, RuntimeValidationError, validateRuntime } from "@/lib/runtimes-shared"; export type Runtime = typeof runtimes.$inferSelect; /** Request body for creating/updating a runtime (validated further by validateRuntime). */ export const RuntimeBody = z.object({ name: z.string().max(200), engine: z.enum(["llama.cpp", "vllm"]), accelerator: z.enum(RUNTIME_ACCELERATORS).optional(), image: z.string().max(512), description: z.string().max(2000).nullable().optional(), engineName: z.string().max(60).nullable().optional(), env: z.record(z.string(), z.string().max(4000)).optional(), extraArgs: z.array(z.string().max(4000)).max(200).optional(), devices: z.array(z.string().max(200)).max(20).optional(), shmSizeMb: z.number().int().nullable().optional(), numaTuning: z.boolean().optional(), }); export class RuntimeError extends Error { constructor( public code: "invalid" | "not_found", message: string, public status: number, ) { super(message); } } function validated(input: RuntimeInput) { try { return validateRuntime(input); } catch (err) { if (err instanceof RuntimeValidationError) throw new RuntimeError("invalid", err.message, 400); throw err; } } export async function listRuntimes(): Promise { return db().select().from(runtimes).orderBy(asc(runtimes.createdAt)); } export async function getRuntime(id: string): Promise { const r = await db().query.runtimes.findFirst({ where: eq(runtimes.id, id) }); if (!r) throw new RuntimeError("not_found", "Runtime not found.", 404); return r; } export async function createRuntime(input: RuntimeInput & { preset?: string }): Promise { const v = validated(input); const preset = input.preset && RUNTIME_PRESETS.some((p) => p.id === input.preset) ? input.preset : null; const [row] = await db() .insert(runtimes) .values({ ...v, preset }) .returning(); return row; } export async function updateRuntime(id: string, input: RuntimeInput): Promise { const current = await getRuntime(id); const v = validated(input); const imageChanged = v.image !== current.image; const [row] = await db() .update(runtimes) .set({ ...v, // A different image has to be looked up (and maybe pulled) again. ...(imageChanged ? { imageState: "unknown", imageId: null, imageSize: null, imageCreated: null, imageDigest: null, pullError: null } : {}), }) .where(eq(runtimes.id, id)) .returning(); return row; } export async function deleteRuntime(id: string): Promise { await getRuntime(id); const using = await deploymentsUsing({ runtimeId: id }); if (using.length > 0) { throw new RuntimeError("invalid", `Deployments use this runtime (${using.map((u) => u.name).join(", ")}). Delete them first.`, 409); } await db().delete(runtimes).where(eq(runtimes.id, id)); } /** Ask the worker to pull (or re-pull, to update a moving tag) the runtime's image. */ export async function requestPull(id: string): Promise { await getRuntime(id); await db().update(runtimes).set({ pullRequestedAt: new Date(), pullError: null }).where(eq(runtimes.id, id)); } /** Docker facts as last reported by the worker; null when DOCKER_HOST isn't configured. */ export async function getDockerInfo(): Promise { try { const row = await db().query.workerState.findFirst({ columns: { docker: true }, where: eq(workerState.id, "worker") }); return row?.docker ?? null; } catch { return null; } }