import { z } from "zod"; import { act } from "@/lib/agent-activity"; import { requestDownload } from "@/lib/agent-downloads"; import { withToken } from "@/lib/api-tokens"; import { size, v1Catch, v1Json } from "@/lib/api-v1"; import { REPO_ID_RE, SHA_RE } from "@/lib/hub/client"; export const dynamic = "force-dynamic"; const Body = z .object({ repoId: z.string().trim().regex(REPO_ID_RE, 'must be "org/name"'), sha: z.string().trim().regex(SHA_RE, "must be the full 40-character commit SHA from GET /api/v1/hub/lookup"), revision: z.string().trim().min(1).max(255).optional(), quant: z.string().trim().min(1).max(64).optional(), include: z.array(z.string().trim().min(1).max(1024)).min(1).max(100).optional(), tags: z.array(z.string()).max(20).optional(), }) .strict() .refine((b) => !!b.quant !== !!b.include, { message: 'pass exactly one of "quant" (e.g. "Q4_K_M") or "include" (globs)' }); /** * Request a download, within the guardrails. 201: queued or waiting for approval; 200: the same * repo + commit + files was requested before, nothing new was queued. */ export const POST = withToken("downloads:create", async (req, { token }) => { try { const body = Body.parse(await req.json()); const r = await act(token, "download.request", { type: "model" }, async () => { const out = await requestDownload(token, body); return { ...out, id: out.modelId }; }); return v1Json( { created: r.created, modelId: r.modelId, jobId: r.jobId, state: r.state, requestedBytes: r.requestedBytes, requestedSize: size(r.requestedBytes), files: r.files, message: r.message.replace("{jobId}", r.jobId ?? "{jobId}"), }, r.created ? 201 : 200, ); } catch (err) { return v1Catch(err); } });