// Pure helpers that turn Hub model info into searchable metadata. Shared by web and worker. export type RepoMetadata = { author: string | null; baseModels: string[]; /** "quantized", "finetune", "adapter", "merge" (from base_model:: tags). */ baseRelation: string | null; architecture: string | null; pipelineTag: string | null; license: string | null; hubTags: string[]; }; type HubModelInfo = { id?: string; author?: string; pipeline_tag?: string; tags?: string[]; cardData?: { base_model?: string | string[]; license?: string | string[] } | null; gguf?: { architecture?: string } | null; config?: { model_type?: string } | null; }; /** Hub tags that carry no useful search signal (or are stored separately). */ const NOISE_TAG = /^(arxiv|region|deploy|base_model|license|doi|dataset):|^(endpoints_compatible|autotrain_compatible|eval-results|text-generation-inference|conversational)$/; const asArray = (v: string | string[] | undefined | null): string[] => v == null ? [] : (Array.isArray(v) ? v : [v]).filter((x): x is string => typeof x === "string" && x.length > 0); export function parseRepoMetadata(info: HubModelInfo): RepoMetadata { const tags = info.tags ?? []; // Prefer the model card; fall back to "base_model::" / "base_model:" tags. let baseModels = asArray(info.cardData?.base_model); if (baseModels.length === 0) { baseModels = tags .filter((t) => t.startsWith("base_model:")) .map((t) => t.split(":").at(-1)!) .filter((id) => id.includes("/")); } const relation = tags.map((t) => t.match(/^base_model:([a-z_]+):/)?.[1]).find(Boolean) ?? null; const license = asArray(info.cardData?.license)[0] ?? tags.find((t) => t.startsWith("license:"))?.slice(8) ?? null; return { author: info.author ?? info.id?.split("/")[0] ?? null, baseModels: [...new Set(baseModels)], baseRelation: baseModels.length > 0 ? relation : null, architecture: info.gguf?.architecture ?? info.config?.model_type ?? null, pipelineTag: info.pipeline_tag ?? null, license, hubTags: [...new Set(tags.filter((t) => !NOISE_TAG.test(t)))], }; } /** * The model "family" for filtering: the org of the base model(s) (unsloth/Qwen3-8B-GGUF → "Qwen"), * or the repo's own org for original models without a base model. */ export function modelFamilies(repoId: string, baseModels: string[] | null | undefined): string[] { const orgs = (baseModels ?? []).map((b) => b.split("/")[0]).filter(Boolean); return orgs.length > 0 ? [...new Set(orgs)] : [repoId.split("/")[0]]; }