Files
Kaname/src/lib/library.ts
T
Pascal LinxweilerandClaude Opus 5.5 3603742637 Flag models deleted from the Hub; tag picker, tags in settings, account menu
- Worker checks each model's repo and pinned commit on the Hub every
  UPDATE_CHECK_INTERVAL_HOURS and after failed downloads; missing repos or
  commits are flagged (badge, banner, Status filter) and block new
  downloads. Local files are never touched. "Check now" on the model page.
- Tag picker dropdown ("Create new tag…" first, then existing tags) on the
  model page and in Add model.
- Settings: separate File labels and Tags pages; create tags there. Unused
  tags are no longer deleted automatically.
- Header: app icon, nav with active state, account menu with the OIDC
  profile picture, theme switch and sign out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 22:53:57 +02:00

109 lines
3.9 KiB
TypeScript

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<ReturnType<typeof listLibrary>>[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<number>`count(${modelFiles.id})`,
totalBytes: sql<number>`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<string, Tag[]>();
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<T>(items: T[], key: (t: T) => string, value: (t: T) => string | null) {
const out = new Map<string, Set<string>>();
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<RelatedModel[]> {
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 }));
}