Skip to content

Router that virtually distributes inference across connected devices in the home.

Go
2.0 KB
Raw Blame
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

import { ModelEntry, SORT_FIELD } from '@/ui/types/model-hub'
import { EngineType } from '@/shared/types/engines'
import type { EngineHubModel } from '@/shared/types/engine-api'
import { EngineCapabilities } from '@/ui/constants/engine-capabilities'
import { EngineHubConfig } from '@/ui/types/engine-manifest'

export const sortFieldLabel = (sort: SORT_FIELD) => {
    switch (sort) {
        case 'lastModified':
            return 'Updated'
        case 'name':
            return 'Name'
        case 'size':
            return 'Size'
        default:
            return sort
    }
}

function getEngineHub(engine: EngineType): EngineHubConfig | undefined {
    return EngineCapabilities[engine]?.engineHub
}

/** Map a normalized hub row from the Electron-main module into a display entry. */
function hubModelToEntry(m: EngineHubModel): ModelEntry {
    return {
        id: m.id,
        name: m.name,
        author: m.author,
        url: m.url,
        size: m.size,
        updatedAt: m.updatedAt ? new Date(m.updatedAt) : new Date(),
        family: m.family,
        parameterSize: m.parameterSize
    }
}

export function mergeModelHubResults(
    populated: readonly ModelEntry[],
    searched: readonly ModelEntry[]
): ModelEntry[] {
    const byId = new Map<string, ModelEntry>()
    for (const model of populated) byId.set(model.id, model)
    for (const model of searched) byId.set(model.id, model)
    return Array.from(byId.values())
}

/**
 * Fetch an engine's populated catalog or submit an explicit upstream query.
 * Electron main owns every upstream source; the renderer only maps display rows.
 */
export async function searchEngineHub(engine: EngineType, query?: string): Promise<ModelEntry[]> {
    if (!getEngineHub(engine)) return []
    const { models } = await window.pairApi.engines.searchHub(engine, query)
    return models.map(hubModelToEntry)
}