import { NextRequest, NextResponse } from "next/server"; import { startModelDownload, publicJob } from "@/lib/downloads"; export const dynamic = "force-dynamic"; export const runtime = "nodejs"; export const maxDuration = 600; /** * POST /api/models/download * * Body: { url: string, filename?: string } * * Starts downloading a .gguf file from the given URL into * gpu/llama_models/. Returns the job id immediately — caller polls * /api/models/download/status for progress. * * Uses curl under the hood (available in the Alpine-based runtime image): * curl -L -f -S --progress-bar -o .part * * Redirects are followed (HuggingFace, Cloudflare R2, etc.). * On success, the .part suffix is stripped via atomic rename. */ export async function POST(req: NextRequest) { const body = (await req.json()) as { url?: string; filename?: string }; if (!body.url || typeof body.url !== "string") { return NextResponse.json( { ok: false, error: "url is required (must be a string)" }, { status: 400 } ); } try { const job = await startModelDownload(body.url, body.filename); return NextResponse.json({ ok: true, job: publicJob(job) }); } catch (err) { return NextResponse.json( { ok: false, error: (err as Error).message }, { status: 400 } ); } }