All files / src/components model-card.ts

100% Statements 94/94
100% Branches 58/58
100% Functions 15/15
100% Lines 85/85

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210            6x           6x           6x               233x 233x 233x 233x 233x   233x 233x 233x 233x 233x 157x     233x 44x 44x 4x 3x       233x       233x 233x 233x 146x 146x               146x 146x 2x 2x           233x 233x 233x 233x 50x   233x 3x   233x                       276x 276x 5x       5x         233x 233x 233x 233x       466x 464x 464x       233x 233x 233x 233x       233x 66x   233x 10x                     233x 52x   181x 56x   125x 29x           96x       157x   157x 13x         13x 12x 12x   13x     144x 55x       55x 51x 51x   55x       55x 51x 51x   89x       2x           87x       87x 84x 84x        
import { setIcon } from "obsidian";
import type { CatalogEntry, HardwareFit, ModelCardOptions } from "../types";
import { HARDWARE_FIT, HOSTED_SOURCES, MODEL_COMPAT, MODEL_TASK } from "../types";
import { MESSAGES } from "../locales/en";
import { formatAbbreviatedCount } from "../utils";
 
const FIT_RAIL_CLASS: Record<HardwareFit, string> = {
    [HARDWARE_FIT.FITS]: "is-fits",
    [HARDWARE_FIT.TIGHT]: "is-tight",
    [HARDWARE_FIT.WONT_RUN]: "is-wont-run",
};
 
const FIT_LABEL: Record<HardwareFit, string> = {
    [HARDWARE_FIT.FITS]: MESSAGES.LABEL_FIT_FITS,
    [HARDWARE_FIT.TIGHT]: MESSAGES.LABEL_FIT_TIGHT,
    [HARDWARE_FIT.WONT_RUN]: MESSAGES.LABEL_FIT_WONT_RUN,
};
 
const TASK_TAG_CLS: Record<string, string> = {
    [MODEL_TASK.CHAT]: "is-chat",
    [MODEL_TASK.EMBEDDING]: "is-embed",
    [MODEL_TASK.VISION]: "is-vision",
    [MODEL_TASK.RERANK]: "is-rerank",
};
 
export function renderModelCard(container: HTMLElement, entry: CatalogEntry, options: ModelCardOptions): HTMLElement {
    const card = container.createDiv({ cls: "lilbee-model-card" });
    card.dataset.repo = entry.hf_repo;
    if (options.isActive) card.addClass("is-selected");
    if (entry.fit && FIT_RAIL_CLASS[entry.fit]) card.addClass(FIT_RAIL_CLASS[entry.fit]);
    if (entry.compat === MODEL_COMPAT.UNSUPPORTED) card.addClass("is-unsupported");
 
    renderCardHead(card, entry, options);
    renderCardTags(card, entry);
    renderCardSpecs(card, entry);
    renderCardStatus(card, entry, options);
    if (options.showActions) {
        renderCardActions(card, entry, options);
    }
 
    if (options.onClick) {
        const handler = options.onClick;
        card.addEventListener("click", (e: Event) => {
            if ((e.target as HTMLElement)?.tagName === "BUTTON") return;
            handler(entry);
        });
    }
 
    return card;
}
 
function renderCardHead(card: HTMLElement, entry: CatalogEntry, options: ModelCardOptions): void {
    const head = card.createDiv({ cls: "lilbee-model-card-head" });
    head.createDiv({ cls: "lilbee-model-card-name", text: entry.display_name });
    if (options.onInfo) {
        const onInfo = options.onInfo;
        const infoBtn = head.createEl("button", {
            cls: "lilbee-model-card-info",
            attr: {
                type: "button",
                "aria-label": MESSAGES.LABEL_MODEL_INFO_BTN,
                title: MESSAGES.LABEL_MODEL_INFO_BTN,
            },
        });
        setIcon(infoBtn, "info");
        infoBtn.addEventListener("click", (e: Event) => {
            e.stopPropagation();
            onInfo(entry);
        });
    }
}
 
function renderCardTags(card: HTMLElement, entry: CatalogEntry): void {
    const tags = card.createDiv({ cls: "lilbee-model-card-tags" });
    const taskCls = TASK_TAG_CLS[entry.task] ?? "is-chat";
    tags.createSpan({ text: entry.task, cls: `lilbee-tag lilbee-tag-task ${taskCls}` });
    if (entry.featured) {
        tags.createSpan({ text: MESSAGES.LABEL_PICK, cls: "lilbee-tag lilbee-tag-featured" });
    }
    if (HOSTED_SOURCES.has(entry.source)) {
        tags.createSpan({ text: entry.provider ?? entry.source, cls: "lilbee-tag lilbee-tag-provider" });
    }
    renderCompatTag(tags, entry);
}
 
/**
 * Render the compatibility badge for a model. Only models the server has
 * confirmed it can't run get a badge (Unsupported). `unknown` compat just means
 * the architecture wasn't probed — true for the curated "Our picks" (which
 * carry no arch metadata) and for un-probed HF search hits — so it renders no
 * badge, exactly like a supported model. Badging those "Unverified" was noise
 * and made trusted featured models look broken.
 */
export function renderCompatTag(tags: HTMLElement, entry: CatalogEntry): void {
    const architecture = entry.architecture ?? "";
    if (entry.compat === MODEL_COMPAT.UNSUPPORTED) {
        const tag = tags.createSpan({
            text: MESSAGES.LABEL_COMPAT_UNSUPPORTED,
            cls: "lilbee-tag lilbee-tag-compat is-unsupported",
        });
        tag.setAttribute("title", MESSAGES.TOOLTIP_COMPAT_UNSUPPORTED(architecture));
    }
}
 
function renderCardSpecs(card: HTMLElement, entry: CatalogEntry): void {
    const specs = card.createDiv({ cls: "lilbee-model-card-specs" });
    specs.createEl("strong", { text: `${entry.size_gb} GB` });
    appendSpecPart(specs, entry.quality_tier);
    appendSpecPart(specs, entry.param_count ? `${entry.param_count} params` : "");
}
 
function appendSpecPart(parent: HTMLElement, text: string): void {
    if (!text) return;
    parent.createSpan({ cls: "lilbee-model-card-specs-sep" });
    parent.createSpan({ text });
}
 
function renderCardStatus(card: HTMLElement, entry: CatalogEntry, options: ModelCardOptions): void {
    const status = card.createDiv({ cls: "lilbee-model-card-status" });
    const tone = statusTone(entry, options);
    status.createSpan({ cls: `lilbee-model-card-status-dot ${tone.dotCls}` });
    const label = status.createSpan({
        text: tone.label,
        cls: `lilbee-model-card-status-label ${tone.labelCls}`,
    });
    if (entry.installed) {
        label.setAttribute("title", MESSAGES.TOOLTIP_MODEL_INSTALLED_SHARED);
    }
    if (entry.fit && FIT_LABEL[entry.fit]) {
        status.createSpan({
            text: FIT_LABEL[entry.fit],
            cls: "lilbee-model-card-fit-text",
        });
    }
}
 
function statusTone(
    entry: CatalogEntry,
    options: ModelCardOptions,
): { dotCls: string; labelCls: string; label: string } {
    if (options.isActive) {
        return { dotCls: "is-active", labelCls: "is-active", label: MESSAGES.LABEL_ACTIVE };
    }
    if (entry.installed) {
        return { dotCls: "is-installed", labelCls: "is-installed", label: MESSAGES.LABEL_INSTALLED };
    }
    if (entry.downloads > 0) {
        return {
            dotCls: "is-muted",
            labelCls: "is-muted",
            label: MESSAGES.LABEL_DOWNLOADS_COUNT(formatAbbreviatedCount(entry.downloads)),
        };
    }
    return { dotCls: "is-muted", labelCls: "is-muted", label: "" };
}
 
function renderCardActions(card: HTMLElement, entry: CatalogEntry, options: ModelCardOptions): void {
    const actions = card.createDiv({ cls: "lilbee-model-card-actions" });
 
    if (options.isActive) {
        const btn = actions.createEl("button", {
            text: MESSAGES.LABEL_ACTIVE,
            cls: "lilbee-btn lilbee-btn-active",
            attr: { disabled: "true" },
        });
        if (options.onUse) {
            const handler = options.onUse;
            btn.addEventListener("click", () => handler(entry, btn));
        }
        return;
    }
 
    if (entry.installed) {
        const useBtn = actions.createEl("button", {
            text: MESSAGES.BUTTON_USE,
            cls: "lilbee-btn lilbee-btn-primary lilbee-catalog-use",
        });
        if (options.onUse) {
            const handler = options.onUse;
            useBtn.addEventListener("click", () => handler(entry, useBtn));
        }
        const removeBtn = actions.createEl("button", {
            text: MESSAGES.BUTTON_REMOVE,
            cls: "lilbee-btn lilbee-btn-danger lilbee-catalog-remove",
        });
        if (options.onRemove) {
            const handler = options.onRemove;
            removeBtn.addEventListener("click", () => handler(entry, removeBtn));
        }
    } else if (entry.compat === MODEL_COMPAT.UNSUPPORTED) {
        // Unsupported models can't run on this server — render the pull action
        // gated (disabled, secondary look) rather than letting a download that
        // can never load go through.
        actions.createEl("button", {
            text: MESSAGES.BUTTON_PULL,
            cls: "lilbee-btn lilbee-catalog-pull is-gated",
            attr: { disabled: "true", title: MESSAGES.TOOLTIP_PULL_UNSUPPORTED },
        });
    } else {
        const pullBtn = actions.createEl("button", {
            text: MESSAGES.BUTTON_PULL,
            cls: "lilbee-btn lilbee-btn-primary lilbee-catalog-pull",
        });
        if (options.onPull) {
            const handler = options.onPull;
            pullBtn.addEventListener("click", () => handler(entry, pullBtn));
        }
    }
}