import { NextRequest, NextResponse } from "next/server"; import { loadConfig, saveConfig, DEFAULT_CONFIG, type DashboardConfig, type Preset, } from "@/lib/docker"; export const dynamic = "force-dynamic"; export const runtime = "nodejs"; /** * Merge a Partial into a full DashboardConfig. All known * fields are copied; missing fields keep their existing values. */ function mergeConfig( base: DashboardConfig, patch: Partial | undefined ): DashboardConfig { if (!patch) return base; return { model: patch.model ?? base.model, ctxSize: patch.ctxSize ?? base.ctxSize, parallel: patch.parallel ?? base.parallel, nGpuLayers: patch.nGpuLayers ?? base.nGpuLayers, threads: patch.threads ?? base.threads, draftModel: patch.draftModel ?? base.draftModel, mmprojPath: patch.mmprojPath ?? base.mmprojPath, specType: patch.specType ?? base.specType, specDraftNMax: patch.specDraftNMax ?? base.specDraftNMax, batchSize: patch.batchSize ?? base.batchSize, ubatchSize: patch.ubatchSize ?? base.ubatchSize, contextOverflow: patch.contextOverflow ?? base.contextOverflow, cacheTypeK: patch.cacheTypeK ?? base.cacheTypeK, cacheTypeV: patch.cacheTypeV ?? base.cacheTypeV, defragThold: patch.defragThold ?? base.defragThold, flashAttn: patch.flashAttn ?? base.flashAttn, noMmap: patch.noMmap ?? base.noMmap, mlock: patch.mlock ?? base.mlock, contBatching: patch.contBatching ?? base.contBatching, embeddings: patch.embeddings ?? base.embeddings, metrics: patch.metrics ?? base.metrics, perf: patch.perf ?? base.perf, ropeFreqScale: patch.ropeFreqScale ?? base.ropeFreqScale, ropeScale: patch.ropeScale ?? base.ropeScale, yarnExtFactor: patch.yarnExtFactor ?? base.yarnExtFactor, yarnAttnFactor: patch.yarnAttnFactor ?? base.yarnAttnFactor, yarnBetaFast: patch.yarnBetaFast ?? base.yarnBetaFast, yarnBetaSlow: patch.yarnBetaSlow ?? base.yarnBetaSlow, systemPrompt: patch.systemPrompt ?? base.systemPrompt, topK: patch.topK ?? base.topK, topP: patch.topP ?? base.topP, minP: patch.minP ?? base.minP, temperature: patch.temperature ?? base.temperature, repeatPenalty: patch.repeatPenalty ?? base.repeatPenalty, nPredict: patch.nPredict ?? base.nPredict, reasoningFormat: patch.reasoningFormat ?? base.reasoningFormat, jinja: patch.jinja ?? base.jinja, extraArgs: patch.extraArgs ?? base.extraArgs, presets: patch.presets ?? base.presets, }; } export async function GET() { const config = await loadConfig(); return NextResponse.json({ config, defaults: DEFAULT_CONFIG }); } export async function PUT(req: NextRequest) { const body = (await req.json()) as Partial; const current = await loadConfig(); const next = mergeConfig(current, body); await saveConfig(next); return NextResponse.json({ config: next }); } // ----- Presets ------------------------------------------------------------- export async function POST(req: NextRequest) { const body = (await req.json()) as { action: "save-preset" | "delete-preset" | "load-preset" | "reset"; name?: string; id?: string; config?: Partial; }; const current = await loadConfig(); // If the caller passed a `config` in the body, merge it into `current` // before performing the action. This is critical for `save-preset`: if the // user has edited the form but not clicked "Apply" yet, the persisted // config.json still has the old values, and saving a preset would snapshot // the OLD config — not what's currently in the UI. const effective = mergeConfig(current, body.config); if (body.action === "save-preset") { if (!body.name) { return NextResponse.json( { error: "name is required" }, { status: 400 } ); } const preset: Preset = { id: `preset-${Date.now()}`, name: body.name, config: stripPresets(effective), createdAt: new Date().toISOString(), }; const next: DashboardConfig = { ...effective, presets: [ ...effective.presets.filter((p) => p.name !== body.name), preset, ], }; await saveConfig(next); return NextResponse.json({ config: next }); } if (body.action === "delete-preset") { if (!body.id) { return NextResponse.json({ error: "id is required" }, { status: 400 }); } const next: DashboardConfig = { ...current, presets: current.presets.filter((p) => p.id !== body.id), }; await saveConfig(next); return NextResponse.json({ config: next }); } if (body.action === "load-preset") { if (!body.id) { return NextResponse.json({ error: "id is required" }, { status: 400 }); } const preset = current.presets.find((p) => p.id === body.id); if (!preset) { return NextResponse.json({ error: "preset not found" }, { status: 404 }); } const next: DashboardConfig = { ...preset.config, presets: current.presets, }; await saveConfig(next); return NextResponse.json({ config: next }); } if (body.action === "reset") { await saveConfig({ ...DEFAULT_CONFIG, presets: current.presets }); return NextResponse.json({ config: { ...DEFAULT_CONFIG, presets: current.presets } }); } return NextResponse.json({ error: "unknown action" }, { status: 400 }); } function stripPresets(cfg: DashboardConfig): Omit { const { presets: _presets, ...rest } = cfg; return rest; }