All files / src/views catalog-helpers.ts

100% Statements 96/96
100% Branches 60/60
100% Functions 32/32
100% Lines 81/81

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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244                                  12x 12x           12x             12x             1083x         4403x           1617x         261x         852x         411x 411x 369x 411x 366x         931x     182x       12x             3330x 3330x 3330x 3390x 3175x 1954x 1954x 1954x 61x   1954x         43x 43x 127x 127x 127x 94x   33x       43x 15x 15x 8x         120x       115x 107x         8x             5x       642x       12x             12x               160x 55x   27x 27x         245x           218x   27x                       245x 26x 26x 104x 104x   4x 4x 4x   104x   26x         8x 8x           17x 101x           18x 97x 199x         7x 7x 6x 6x 6x   6x 3x 3x 3x 2x 2x        
import type { App } from "obsidian";
import type { CatalogEntry, CatalogSource, CatalogTab, KeyStatus, ModelTask, ServerMode } from "../types";
import {
    CATALOG_SOURCE,
    CATALOG_TAB,
    HARDWARE_FIT,
    HOSTED_SOURCES,
    KEY_STATUS,
    MODEL_COMPAT,
    MODEL_TASK,
    PLATFORM,
    SERVER_MODE,
} from "../types";
import { MESSAGES } from "../locales/en";
import type { LilbeeClient } from "../api";
import { err, ok, type Result } from "../result";
 
const DISCOVER_RAIL_LIMIT = 12;
const TASK_TO_TAB: Record<ModelTask, CatalogTab> = {
    [MODEL_TASK.CHAT]: CATALOG_TAB.CHAT,
    [MODEL_TASK.EMBEDDING]: CATALOG_TAB.EMBED,
    [MODEL_TASK.VISION]: CATALOG_TAB.VISION,
    [MODEL_TASK.RERANK]: CATALOG_TAB.RERANK,
};
const TAB_TO_TASK: Partial<Record<CatalogTab, ModelTask>> = {
    [CATALOG_TAB.CHAT]: MODEL_TASK.CHAT,
    [CATALOG_TAB.EMBED]: MODEL_TASK.EMBEDDING,
    [CATALOG_TAB.VISION]: MODEL_TASK.VISION,
    [CATALOG_TAB.RERANK]: MODEL_TASK.RERANK,
};
 
export const KEY_STATUS_PILL_CLASS = {
    READY: "lilbee-key-status-pill-ready",
    NEEDS_KEY: "lilbee-key-status-pill-needs-key",
} as const;
 
/** Hosted rows (frontier + local servers): selectable, no download. */
export function hostedRowsOnly(rows: CatalogEntry[]): CatalogEntry[] {
    return rows.filter((row) => HOSTED_SOURCES.has(row.source));
}
 
/** Everything that isn't hosted — native catalog rows the server can download. */
export function localRowsOnly(rows: CatalogEntry[]): CatalogEntry[] {
    return rows.filter((row) => !HOSTED_SOURCES.has(row.source));
}
 
/** A hosted row is usable unless it's a frontier model still missing its key.
 * Local servers (Ollama, LM Studio) report `key_status` null, so they always pass. */
export function isUsableHostedRow(row: CatalogEntry): boolean {
    return HOSTED_SOURCES.has(row.source) && row.key_status !== KEY_STATUS.MISSING_KEY;
}
 
/** True when at least one hosted row is ready to select right now. */
export function hasReadyHostedRow(rows: CatalogEntry[]): boolean {
    return rows.some(isUsableHostedRow);
}
 
/** Local-server sources (Ollama, LM Studio) lead hosted listings; frontier trails. Lower sorts first. */
function hostedSourceRank(source: CatalogSource): number {
    return source === CATALOG_SOURCE.FRONTIER ? 1 : 0;
}
 
/** Local-first, then provider, then name — deterministic ordering for hosted rows. */
function compareHostedRows(a: CatalogEntry, b: CatalogEntry): number {
    const rankDiff = hostedSourceRank(a.source) - hostedSourceRank(b.source);
    if (rankDiff !== 0) return rankDiff;
    const providerDiff = (a.provider ?? "").localeCompare(b.provider ?? "");
    if (providerDiff !== 0) return providerDiff;
    return a.display_name.localeCompare(b.display_name);
}
 
/** Selectable hosted rows, local-first: local servers always, frontier only with a ready key. Returns [ref, label]. */
export function hostedOptions(rows: CatalogEntry[]): Array<[string, string]> {
    return rows
        .filter(isUsableHostedRow)
        .sort(compareHostedRows)
        .map((e) => [e.hf_repo, `${e.display_name}${e.provider ? ` [${e.provider}]` : ""}`]);
}
 
/** Page cap for a selector's catalog read, whatever page size the caller passes. */
const MAX_SELECTOR_PAGES = 50;
 
/** Catalog rows for a model selector: pages until the listing passes its leading hosted rows. */
export async function catalogThroughHosted(
    api: Pick<LilbeeClient, "catalog">,
    params: { task: ModelTask; limit?: number },
): Promise<Result<CatalogEntry[], Error>> {
    const models: CatalogEntry[] = [];
    let offset = 0;
    for (let page = 0; page < MAX_SELECTOR_PAGES; page++) {
        const result = await api.catalog({ ...params, offset });
        if (result.isErr()) return err(result.error);
        const { value } = result;
        models.push(...value.models);
        if (!value.has_more || value.models.some((m) => m.source === CATALOG_SOURCE.NATIVE)) break;
        offset = value.offset + value.limit;
    }
    return ok(models);
}
 
/** Hosted rows grouped by provider, local-server groups before frontier, providers alphabetical within a rank. */
export function groupByProvider(rows: CatalogEntry[]): [string, CatalogEntry[]][] {
    const groups = new Map<string, CatalogEntry[]>();
    for (const row of rows) {
        const provider = row.provider ?? "";
        const existing = groups.get(provider);
        if (existing) {
            existing.push(row);
        } else {
            groups.set(provider, [row]);
        }
    }
    // Each provider maps to one source, so rank the group by its first row's source.
    return [...groups.entries()].sort(([aProvider, aRows], [bProvider, bRows]) => {
        const rankDiff = hostedSourceRank(aRows[0].source) - hostedSourceRank(bRows[0].source);
        if (rankDiff !== 0) return rankDiff;
        return aProvider.localeCompare(bProvider);
    });
}
 
export function renderProviderPill(parent: HTMLElement, provider: string): HTMLElement {
    return parent.createSpan({ cls: "lilbee-provider-pill", text: provider });
}
 
export function renderKeyStatusPill(parent: HTMLElement, status: KeyStatus): HTMLElement {
    if (status === KEY_STATUS.READY) {
        return parent.createSpan({
            cls: `lilbee-key-status-pill ${KEY_STATUS_PILL_CLASS.READY}`,
            text: MESSAGES.PILL_KEY_READY,
        });
    }
    return parent.createSpan({
        cls: `lilbee-key-status-pill ${KEY_STATUS_PILL_CLASS.NEEDS_KEY}`,
        text: MESSAGES.PILL_KEY_NEEDS_KEY,
    });
}
 
export function taskToTabId(task: ModelTask): CatalogTab {
    return TASK_TO_TAB[task];
}
 
export function tabIdToTask(tab: CatalogTab): ModelTask | null {
    return TAB_TO_TASK[tab] ?? null;
}
 
/** Role order for the For You rail, matching the server's own role ordering. */
const FOR_YOU_ROLE_ORDER: ModelTask[] = [MODEL_TASK.CHAT, MODEL_TASK.EMBEDDING, MODEL_TASK.VISION, MODEL_TASK.RERANK];
 
/**
 * The GPU toolkit a quant is built for: the toolkit name glued to the version or the precision it
 * was built against, as in ROCmFP4 or CUDA12. A toolkit name standing on its own as a word is a
 * subject or a publisher, not a build, so Barracuda and Cuda-Kernel-Coder are both left alone.
 */
const VENDOR_QUANT = /(^|[^a-z0-9])(rocm|cuda)(\d|fp|bf|q)/i;
 
/**
 * A quant built for a GPU toolkit macOS does not have. The catalog reports no vendor, so the name
 * is the only signal. The model loads on the server's host, which is this machine only in managed
 * mode; an external server runs on a host whose GPU we don't know, so every row stays.
 */
export function isForeignVendorQuant(entry: CatalogEntry, serverMode: ServerMode): boolean {
    if (serverMode !== SERVER_MODE.MANAGED) return false;
    if (process.platform !== PLATFORM.DARWIN || HOSTED_SOURCES.has(entry.source)) return false;
    // The first repo segment names who published the file, not how it was built, so it is dropped.
    const modelName = entry.hf_repo.slice(entry.hf_repo.indexOf("/") + 1);
    return VENDOR_QUANT.test(`${modelName} ${entry.gguf_filename}`);
}
 
/** A catalog row a user can run: featured, supported, not known unrunnable, not built for another vendor's GPU, and key-ready when hosted. */
export function isRunnablePick(entry: CatalogEntry, serverMode: ServerMode): boolean {
    if (
        !entry.featured ||
        entry.compat !== MODEL_COMPAT.SUPPORTED ||
        entry.fit === HARDWARE_FIT.WONT_RUN ||
        isForeignVendorQuant(entry, serverMode)
    ) {
        return false;
    }
    return !HOSTED_SOURCES.has(entry.source) || isUsableHostedRow(entry);
}
 
/**
 * One runnable pick per role, in role order.
 *
 * Featured is not a curated list — the server resolves it from HuggingFace
 * trending at runtime, so a pick is only as safe as its hardware fit and its
 * provider key. Rows with no fit chip are kept (a failed probe must not empty
 * the rail) but rank behind known fits.
 */
export function forYouRail(entries: CatalogEntry[], serverMode: ServerMode): CatalogEntry[] {
    const runnable = entries.filter((entry) => isRunnablePick(entry, serverMode));
    const picks: CatalogEntry[] = [];
    for (const task of FOR_YOU_ROLE_ORDER) {
        const best = runnable
            .filter((e) => e.task === task)
            .sort((a, b) => {
                const [aRank, aName] = forYouSortKey(a);
                const [bRank, bName] = forYouSortKey(b);
                return aRank !== bRank ? aRank - bRank : aName.localeCompare(bName);
            })[0];
        if (best) picks.push(best);
    }
    return picks;
}
 
/** Fit rank: known fits first, then unknown (null), then tight. Lower sorts first. */
function forYouSortKey(entry: CatalogEntry): [number, string] {
    const rank = entry.fit === HARDWARE_FIT.FITS ? 0 : entry.fit === null || entry.fit === undefined ? 1 : 2;
    return [rank, entry.display_name.toLowerCase()];
}
 
export function yourCollectionRail(entries: CatalogEntry[]): CatalogEntry[] {
    // Hosted rows report installed=true because they are usable, not because
    // they are on disk. "Your collection" answers what this machine holds.
    return localRowsOnly(entries)
        .filter((e) => e.installed)
        .slice(0, DISCOVER_RAIL_LIMIT);
}
 
/** Most-downloaded native rows the picks rail did not already claim. */
export function freshRail(entries: CatalogEntry[]): CatalogEntry[] {
    return localRowsOnly(entries)
        .filter((e) => !e.featured)
        .sort((a, b) => b.downloads - a.downloads)
        .slice(0, DISCOVER_RAIL_LIMIT);
}
 
export function deepLinkToApiKeySettings(app: App, provider: string): void {
    const settingApi = (app as App & { setting?: { open(): void; openTabById(id: string): void } }).setting;
    if (!settingApi) return;
    settingApi.open();
    settingApi.openTabById("lilbee");
    window.setTimeout(() => {
        // Timer can fire after the modal closes; Node test envs don't have a global document.
        if (typeof activeDocument === "undefined") return;
        const escaped = provider.toLowerCase().replace(/[^a-z0-9-]/g, "-");
        const target = activeDocument.querySelector(`[data-lilbee-api-key="${escaped}"]`);
        if (target instanceof HTMLElement) {
            target.scrollIntoView({ behavior: "smooth", block: "center" });
            target.focus();
        }
    }, 50);
}