Files
Kaname/src/lib/runtimes.ts
T
Pascal LinxweilerandClaude Opus 5.5 ee6c623e2a
Release / release (push) Successful in 3m25s
Release 0.11.0: benchmark metadata and system info
Every run records GPUs (PCIe link under load and maximum, ReBAR, P2P),
runtime image/tag/digest and the engine version the server reports,
model, quant, KV cache, context and concurrency, plus a whitelisted
system summary and a description. Shown on the run page, compare and
sweep views; passed to BetterBench as notes and to the launch80 preview
as sections. Settings -> Hardware for manual values.

Also: scripts/intel-probe.sh (read-only diagnostics for Intel hosts),
and --quick is no longer passed together with --passes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 19:07:04 +02:00

105 lines
3.8 KiB
TypeScript

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<Runtime[]> {
return db().select().from(runtimes).orderBy(asc(runtimes.createdAt));
}
export async function getRuntime(id: string): Promise<Runtime> {
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<Runtime> {
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<Runtime> {
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<void> {
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<void> {
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<DockerHostInfo | null> {
try {
const row = await db().query.workerState.findFirst({ columns: { docker: true }, where: eq(workerState.id, "worker") });
return row?.docker ?? null;
} catch {
return null;
}
}