The worker scans MODELS_DIR (startup, every 2 min, on "Rescan") for <org>/<name> folders that no model uses and stores them in local_folders. The library lists them; Import opens Add model prefilled with the folder, the commit hf recorded and the files on disk preselected. The import job runs hf download into the existing folder: hf 2.1.1 hashes files it didn't download itself, keeps matching ones and re-downloads the rest (verified in Docker, including a corrupted file being replaced). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
42 lines
1.8 KiB
TypeScript
42 lines
1.8 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { z } from "zod";
|
|
import { withAuth } from "@/lib/api-auth";
|
|
import { apiError, handleApiError } from "@/lib/api-errors";
|
|
import { REPO_ID_RE, SHA_RE } from "@/lib/hub/client";
|
|
import { queueDownload } from "@/lib/jobs";
|
|
import { addModel, ModelError } from "@/lib/models";
|
|
import { addTag, normalizeTagName, TagError } from "@/lib/tags";
|
|
|
|
const Body = z.object({
|
|
repoId: z.string().regex(REPO_ID_RE),
|
|
revision: z.string().trim().min(1).max(255),
|
|
sha: z.string().regex(SHA_RE, "must be a full 40-character commit SHA"),
|
|
include: z.array(z.string().min(1).max(1024)).max(10_000),
|
|
exclude: z.array(z.string().min(1).max(1024)).max(1_000),
|
|
download: z.boolean().default(true),
|
|
tags: z.array(z.string()).max(50).default([]),
|
|
/** Import an existing folder from the models folder (as found by the worker's scan). */
|
|
localPath: z.string().min(1).max(512).optional(),
|
|
});
|
|
|
|
/** Add a model to the library, pinned to the SHA the user reviewed, and queue its download. */
|
|
export const POST = withAuth(async (req) => {
|
|
try {
|
|
const body = Body.parse(await req.json());
|
|
body.tags.forEach(normalizeTagName); // validate before anything is written
|
|
const { id } = await addModel({
|
|
repoId: body.repoId,
|
|
revision: body.revision,
|
|
sha: body.sha,
|
|
rule: { include: body.include, exclude: body.exclude },
|
|
localPath: body.localPath,
|
|
});
|
|
for (const tag of body.tags) await addTag(id, tag);
|
|
const job = body.download ? await queueDownload(id, body.localPath ? "import" : "initial") : null;
|
|
return NextResponse.json({ id, jobId: job?.jobId ?? null }, { status: 201 });
|
|
} catch (err) {
|
|
if (err instanceof ModelError || err instanceof TagError) return apiError(err.status, err.code, err.message);
|
|
return handleApiError(err);
|
|
}
|
|
});
|