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"; 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(); // Merge: keep existing presets unless explicitly overwritten const next: DashboardConfig = { model: body.model ?? current.model, ctxSize: body.ctxSize ?? current.ctxSize, parallel: body.parallel ?? current.parallel, nGpuLayers: body.nGpuLayers ?? current.nGpuLayers, threads: body.threads ?? current.threads, draftModel: body.draftModel ?? current.draftModel, specType: body.specType ?? current.specType, specDraftNMax: body.specDraftNMax ?? current.specDraftNMax, batchSize: body.batchSize ?? current.batchSize, contextOverflow: body.contextOverflow ?? current.contextOverflow, extraArgs: body.extraArgs ?? current.extraArgs, presets: body.presets ?? current.presets, }; 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; }; const current = await loadConfig(); 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(current), createdAt: new Date().toISOString(), }; const next: DashboardConfig = { ...current, presets: [...current.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; }