Brings back the token API that 0.15 removed with deployments, for models only, ported onto the current code (local models, lean client bundles, MiB-rounded disk space): - Scoped bearer tokens (models:read, downloads:create, models:verify, tags:write), stored as SHA-256, managed in Settings -> API tokens. Only /api/v1 accepts them; the rest stays OIDC-only. - /api/v1: Hub lookup, library with the search filters, download requests through the add-model/queue path (idempotent), repair of missing or broken files, verify / deep verify, tags, job status with ?wait=, cancelling or withdrawing the token's own jobs, disk space. - Guardrails for downloads and repairs: approval (default ask), max size per request, minimum free space incl. reserved bytes, agent jobs at once, author allowlist (new downloads only). Approve/reject in the UI, "requested by" badges and filter, activity log per token. - Migration 0027 adds api_tokens, agent_activity and requested_by columns on models and jobs; job state pending_approval. - docs/api.md, docs/agent/SKILL.md and the kaname curl helper; DB-backed API tests. The login gate test now expects /api/v1 to be left to token auth. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
50 lines
1.8 KiB
TypeScript
50 lines
1.8 KiB
TypeScript
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);
|
|
}
|
|
});
|