Files
Kaname/src/lib/hub/refresh.ts
T
Pascal LinxweilerandClaude Opus 5.5 80ccf800df Import folders without a Hub repo as local models
The Import form gets "Add as local model": every file in the folder,
kept as it is, named after the folder. A new models.source column
(hub | local) marks them; local models skip Hub checks, metadata,
downloads, updates and deep verify, and Verify checks existence and
size only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 00:14:56 +01:00

43 lines
1.5 KiB
TypeScript

import { and, eq, isNull } from "drizzle-orm";
import { db } from "@/lib/db/client";
import { models } from "@/lib/db/schema";
import { getAuthorAvatar, getRepoInfo } from "./client";
import type { RepoMetadata } from "./metadata";
/** Columns to write on `models` for fetched Hub metadata. */
export function metadataColumns(meta: RepoMetadata, authorAvatarUrl: string | null) {
return {
baseModels: meta.baseModels,
baseRelation: meta.baseRelation,
authorAvatarUrl,
architecture: meta.architecture,
pipelineTag: meta.pipelineTag,
license: meta.license,
hubTags: meta.hubTags,
metadataFetchedAt: new Date(),
};
}
/**
* Fetch Hub metadata for models that don't have it yet (added before metadata existed).
* Returns how many were updated; failures are reported but don't stop the others.
*/
export async function backfillMetadata(onError: (repoId: string, err: unknown) => void): Promise<number> {
const missing = await db()
.select({ id: models.id, repoId: models.repoId, pinnedSha: models.pinnedSha })
.from(models)
.where(and(eq(models.source, "hub"), isNull(models.metadataFetchedAt)));
let updated = 0;
for (const m of missing) {
try {
const info = await getRepoInfo(m.repoId, m.pinnedSha);
const avatar = await getAuthorAvatar(m.repoId.split("/")[0]);
await db().update(models).set(metadataColumns(info.metadata, avatar)).where(eq(models.id, m.id));
updated++;
} catch (err) {
onError(m.repoId, err);
}
}
return updated;
}