Files
Kaname/src/lib/hub/availability.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

74 lines
3.2 KiB
TypeScript

import { asc, eq, isNull, lt, or } from "drizzle-orm";
import { db } from "@/lib/db/client";
import { models, type HubState } from "@/lib/db/schema";
import { getRepoInfo, HubError } from "./client";
type CheckedModel = Pick<typeof models.$inferSelect, "id" | "repoId" | "pinnedSha" | "hubMissingSince">;
/** Why new downloads are impossible, or null if the Hub still has the repo at the pinned commit. */
export function hubMissingMessage(model: { repoId: string; hubState: HubState | null }): string | null {
if (model.hubState === "repo_missing") {
return `${model.repoId} is no longer on Hugging Face (deleted, or made private), so nothing more can be downloaded. Your local files are kept.`;
}
if (model.hubState === "commit_missing") {
return `The pinned commit of ${model.repoId} is no longer on Hugging Face (history rewritten), so nothing more can be downloaded from it. Your local files are kept.`;
}
return null;
}
export type HubCheckResult = { state: HubState } | { state: null; error: string };
/**
* Is the repo, and the pinned commit, still on the Hub? Stores the result on the model. Only a
* definite answer changes `hubState`; network errors, rate limits or a bad token are kept as
* `lastCheckError`. Never touches local files.
*/
export async function checkHubAvailability(model: CheckedModel): Promise<HubCheckResult> {
let result: HubCheckResult;
try {
await getRepoInfo(model.repoId, model.pinnedSha);
result = { state: "available" };
} catch (err) {
if (err instanceof HubError && err.code === "repo_not_found") result = { state: "repo_missing" };
else if (err instanceof HubError && err.code === "revision_not_found") result = { state: "commit_missing" };
// Gated repos exist; access is a download concern, not availability.
else if (err instanceof HubError && (err.code === "gated_no_token" || err.code === "gated_no_access")) {
result = { state: "available" };
} else result = { state: null, error: err instanceof Error ? err.message : String(err) };
}
const now = new Date();
await db()
.update(models)
.set(
result.state === null
? { lastCheckedAt: now, lastCheckError: result.error }
: {
lastCheckedAt: now,
lastCheckError: null,
hubState: result.state,
hubMissingSince: result.state === "available" ? null : (model.hubMissingSince ?? now),
},
)
.where(eq(models.id, model.id));
return result;
}
/** Models whose last Hub check is older than `intervalMs` (or never ran), oldest first. */
export async function modelsDueForHubCheck(intervalMs: number, limit = 50): Promise<CheckedModel[]> {
const cutoff = new Date(Date.now() - intervalMs);
return db()
.select({ id: models.id, repoId: models.repoId, pinnedSha: models.pinnedSha, hubMissingSince: models.hubMissingSince })
.from(models)
.where(or(isNull(models.lastCheckedAt), lt(models.lastCheckedAt, cutoff)))
.orderBy(asc(models.lastCheckedAt))
.limit(limit);
}
export async function getCheckedModel(id: string): Promise<CheckedModel | undefined> {
return db().query.models.findFirst({
columns: { id: true, repoId: true, pinnedSha: true, hubMissingSince: true },
where: eq(models.id, id),
});
}