Milestone (b): repo lookup and file selection

- Hub API client: resolve revision to SHA, paginated recursive file tree,
  gated-access probe, readable error mapping
- Add-model page with quant quick-select, fnmatch-compatible include/exclude
  globs, selection summary and free-space hint
- Models stored pinned to the reviewed SHA with full file list and selection rule
- Library list, model detail page, remove (never-downloaded models only)
- Worker reports MODELS_DIR free space; unit tests for selection logic

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Pascal LinxweilerandClaude Opus 5.5 committed 2026-10-05 15:39:45 +02:00
1 parent 891e496157
commit 8f7ac60ccf
35 files changed
+2567 -19

No files matched your search

+104
View File
@@ -0,0 +1,104 @@
import "server-only";
import { and, eq, sql } from "drizzle-orm";
import { ulid } from "ulid";
import { db } from "@/lib/db/client";
import { jobs, modelFiles, modelRevisions, models } from "@/lib/db/schema";
import { checkFileAccess, getRepoInfo, listFiles } from "@/lib/hub/client";
import { isSelected, type SelectionRule } from "@/lib/selection";
export class ModelError extends Error {
constructor(
public code: "already_exists" | "empty_selection" | "not_found" | "not_removable",
message: string,
public status: number,
) {
super(message);
}
}
const INSERT_CHUNK = 500;
/**
* Add a model pinned to `sha`. The file list is re-fetched from the Hub at that commit
* (the client's copy isn't trusted) and the selection rule is applied server-side.
*/
export async function addModel(input: {
repoId: string;
revision: string;
sha: string;
rule: SelectionRule;
}): Promise<{ id: string }> {
const info = await getRepoInfo(input.repoId, input.sha);
const repoId = info.repoId;
const existing = await db().query.models.findFirst({
columns: { id: true },
where: and(eq(models.repoType, "model"), eq(models.repoId, repoId)),
});
if (existing) throw new ModelError("already_exists", `${repoId} is already in the library.`, 409);
const files = await listFiles(repoId, info.sha);
const selectedFiles = files.filter((f) => isSelected(f.path, input.rule));
if (selectedFiles.length === 0) {
throw new ModelError("empty_selection", "No files selected.", 400);
}
if (info.gated) {
const smallest = selectedFiles.reduce((a, b) => (b.size < a.size ? b : a));
await checkFileAccess(repoId, info.sha, smallest.path);
}
const d = db();
const modelId = ulid();
const fileRows = files.map((f) => ({
modelId,
path: f.path,
size: f.size,
blobOid: f.blobOid,
lfsSha256: f.lfsSha256,
selected: isSelected(f.path, input.rule),
}));
// One atomic batch: model, its first pinned revision and the full file list.
await d.batch([
d.insert(models).values({
id: modelId,
repoId,
trackedRevision: input.revision,
pinnedSha: info.sha,
localPath: repoId,
includePatterns: input.rule.include,
excludePatterns: input.rule.exclude,
}),
d.insert(modelRevisions).values({ modelId, sha: info.sha }),
...chunk(fileRows, INSERT_CHUNK).map((c) => d.insert(modelFiles).values(c)),
]);
return { id: modelId };
}
/** Remove a model from the library. Only allowed before anything was downloaded (for now). */
export async function removeModel(id: string): Promise<void> {
const d = db();
const model = await d.query.models.findFirst({ where: eq(models.id, id) });
if (!model) throw new ModelError("not_found", "Model not found.", 404);
const [{ n }] = await d
.select({ n: sql<number>`count(*)` })
.from(jobs)
.where(eq(jobs.modelId, id));
if (model.status !== "pending" || n > 0) {
throw new ModelError(
"not_removable",
"Only models that were never downloaded can be removed yet. Deleting downloaded models comes in a later milestone.",
409,
);
}
await d.batch([
d.delete(modelFiles).where(eq(modelFiles.modelId, id)),
d.delete(modelRevisions).where(eq(modelRevisions.modelId, id)),
d.delete(models).where(eq(models.id, id)),
]);
}
function chunk<T>(items: T[], size: number): T[][] {
const out: T[][] = [];
for (let i = 0; i < items.length; i += size) out.push(items.slice(i, i + size));
return out;
}