import { withToken } from "@/lib/api-tokens"; import { parseModelFilters } from "@/lib/api-v1-filters"; import { size, v1Catch, v1Json } from "@/lib/api-v1"; import { listLibrary } from "@/lib/library"; import { matchesFilters, matchesQuery, type SearchableModel } from "@/lib/search"; export const dynamic = "force-dynamic"; /** * The local library, filtered like the library page: ?q= (all terms must match) and facets * author, family, quant, tag, status, requested_by (repeat or comma-separate values). * Status values: pending, downloading, ready, incomplete, failed, deleting, pending_approval, * update_available, repo_missing, commit_missing. */ export const GET = withToken("models:read", async (req) => { try { const { query, filters } = parseModelFilters(new URL(req.url).searchParams); const rows = await listLibrary(); const out = rows .filter((r) => { const m: SearchableModel = { repoId: r.repoId, author: r.author, families: r.families, baseModels: r.baseModels, architecture: r.architecture, quants: r.quants, tags: r.tags.map((t) => t.name), hubTags: r.hubTags, pipelineTag: r.pipelineTag, license: r.license, status: r.status, hubStatus: r.hubState && r.hubState !== "available" ? r.hubState : r.updateAvailable ? "update_available" : null, requestedBy: r.requestedBy, pendingApproval: r.pendingApproval, }; return matchesQuery(m, query) && matchesFilters(m, filters); }) .map((r) => ({ id: r.id, repoId: r.repoId, status: r.status, pendingApproval: r.pendingApproval, pinnedSha: r.pinnedSha, quants: r.quants, tags: r.tags.map((t) => t.name), selectedFiles: r.fileCount, selectedBytes: r.totalBytes, onDiskBytes: r.presentBytes, onDisk: size(r.presentBytes), updateAvailable: r.updateAvailable, hubState: r.hubState, requestedBy: r.requestedBy, createdAt: r.createdAt.toISOString(), downloadedAt: r.downloadedAt?.toISOString() ?? null, })); return v1Json({ count: out.length, models: out }); } catch (err) { return v1Catch(err); } });