import "server-only"; import { and, asc, desc, eq, sql } from "drizzle-orm"; import { db } from "@/lib/db/client"; import { modelFiles, models, modelTags, tags } from "@/lib/db/schema"; import { modelFamilies } from "@/lib/hub/metadata"; import { fileRole } from "@/lib/file-roles"; import { quantGroup } from "@/lib/selection"; import type { Tag } from "@/lib/tags"; export type LibraryRow = Awaited>[number]; /** Models with selected-file totals, quant labels, Hub metadata and user tags. */ export async function listLibrary() { const d = db(); const rows = await d .select({ id: models.id, repoId: models.repoId, trackedRevision: models.trackedRevision, pinnedSha: models.pinnedSha, remoteSha: models.remoteSha, status: models.status, hubState: models.hubState, createdAt: models.createdAt, downloadedAt: models.downloadedAt, baseModels: models.baseModels, architecture: models.architecture, pipelineTag: models.pipelineTag, license: models.license, hubTags: models.hubTags, fileCount: sql`count(${modelFiles.id})`, totalBytes: sql`coalesce(sum(${modelFiles.size}), 0)`, }) .from(models) .leftJoin(modelFiles, and(eq(modelFiles.modelId, models.id), eq(modelFiles.selected, true))) .groupBy(models.id) .orderBy(desc(models.createdAt)); const selected = await d .select({ modelId: modelFiles.modelId, path: modelFiles.path }) .from(modelFiles) .where(eq(modelFiles.selected, true)); // Only model weights count as a quant (not MTP heads, mmproj or imatrix files). const quants = groupBy(selected, (f) => f.modelId, (f) => (fileRole(f.path) === "model" ? quantGroup(f.path) : null)); const tagRows = await d .select({ modelId: modelTags.modelId, id: tags.id, name: tags.name, color: tags.color }) .from(modelTags) .innerJoin(tags, eq(tags.id, modelTags.tagId)) .orderBy(asc(tags.slug)); const tagsByModel = new Map(); for (const t of tagRows) tagsByModel.set(t.modelId, [...(tagsByModel.get(t.modelId) ?? []), { id: t.id, name: t.name, color: t.color }]); return rows.map((r) => ({ ...r, author: r.repoId.split("/")[0], families: modelFamilies(r.repoId, r.baseModels), baseModels: r.baseModels ?? [], hubTags: r.hubTags ?? [], quants: [...(quants.get(r.id) ?? [])].sort(), tags: tagsByModel.get(r.id) ?? [], })); } export async function getModel(id: string) { const d = db(); const model = await d.query.models.findFirst({ where: eq(models.id, id) }); if (!model) return null; const files = await d .select() .from(modelFiles) .where(eq(modelFiles.modelId, id)) .orderBy(asc(modelFiles.path)); return { model, files }; } function groupBy(items: T[], key: (t: T) => string, value: (t: T) => string | null) { const out = new Map>(); for (const item of items) { const v = value(item); if (v == null) continue; const k = key(item); if (!out.has(k)) out.set(k, new Set()); out.get(k)!.add(v); } return out; } /** Look up a model by "org/name" (case-insensitive, like the Hub). */ export async function getModelByRepo(repoId: string) { const row = await db() .select({ id: models.id }) .from(models) .where(sql`lower(${models.repoId}) = lower(${repoId})`) .limit(1); return row[0] ? getModel(row[0].id) : null; } export type RelatedModel = { repoId: string; quants: string[]; status: string }; /** Other models in the library that share a base model (e.g. another quant of the same Qwen). */ export async function relatedModels(modelId: string, baseModels: string[]): Promise { if (baseModels.length === 0) return []; const all = await listLibrary(); return all .filter((m) => m.id !== modelId && m.baseModels.some((b) => baseModels.includes(b))) .map((m) => ({ repoId: m.repoId, quants: m.quants, status: m.status })); }