Skip to content

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

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

import type { ServiceError } from '@/shared/types/errors'
import type { EngineProcessStatus, EngineStatusData, EngineType } from '@/shared/types/engines'
import { engineTypeFromManagerName, isEngineType } from '@/shared/utils/engines'

type WelcomeInstallOutcome = 'pending' | 'complete' | 'failed'

/**
 * A `ServiceError.engineType` reaches us in either spelling: the backend stamps
 * the engine-manager id, while errors PAIR raises itself already use
 * `EngineType`. Null for an absent or unrecognized engine.
 */
function errorEngineType(value: string | undefined): EngineType | null {
    if (!value) return null
    return engineTypeFromManagerName(value) ?? (isEngineType(value) ? value : null)
}

export function isWelcomeEngineInstalled(status: EngineProcessStatus | undefined): boolean {
    return status === 'running' || status === 'stopped'
}

/** Unknown means the authoritative engine snapshot has not identified this engine yet. */
export function isWelcomeEngineInstallable(status: EngineProcessStatus | undefined): boolean {
    return status === 'not-installed'
}

export function areWelcomeEnginesInstalled(
    statusByNode: ReadonlyMap<string, ReadonlyMap<EngineType, EngineStatusData>>,
    nodeId: string,
    candidates: readonly EngineType[]
): boolean {
    const statuses = statusByNode.get(nodeId)
    return candidates.every(engine =>
        isWelcomeEngineInstalled(statuses?.get(engine)?.processStatus)
    )
}

export function hasWelcomeEngineStatuses(
    statusByNode: ReadonlyMap<string, ReadonlyMap<EngineType, unknown>>,
    nodeId: string,
    candidates: readonly EngineType[]
): boolean {
    const statuses = statusByNode.get(nodeId)
    return candidates.every(engine => statuses?.has(engine) === true)
}

export function getWelcomeInstallOutcome(
    statuses: Array<{ engineType: EngineType; status: EngineProcessStatus }>,
    started: ReadonlySet<EngineType>,
    failed: ReadonlySet<EngineType> = new Set()
): WelcomeInstallOutcome {
    if (statuses.every(({ status }) => isWelcomeEngineInstalled(status))) {
        return 'complete'
    }
    if (
        statuses.every(
            ({ engineType, status }) =>
                isWelcomeEngineInstalled(status) ||
                (failed.has(engineType) && status === 'not-installed') ||
                (started.has(engineType) && status === 'not-installed')
        )
    ) {
        return 'failed'
    }
    return 'pending'
}

export function targetForInstallError(
    error: ServiceError,
    targets: readonly EngineType[]
): EngineType | null {
    const explicit = errorEngineType(error.engineType)
    if (explicit && targets.includes(explicit)) return explicit

    const text = `${error.id} ${error.message}`.toLowerCase()
    return (
        targets.find(engineType => {
            const aliases = [
                engineType,
                engineType.replaceAll('-', ''),
                engineType.replaceAll('-', ' ')
            ]
            return aliases.some(alias => text.includes(alias))
        }) ?? null
    )
}

export function isTargetInstallError(
    error: ServiceError,
    nodeId: string,
    targets: readonly EngineType[]
): boolean {
    if (error.nodeId && error.nodeId !== nodeId) return false
    // An engine we cannot resolve is not one of the targets either.
    const errorEngine = errorEngineType(error.engineType)
    if (error.engineType && (!errorEngine || !targets.includes(errorEngine))) return false
    return (
        error.operation === 'install' ||
        error.id.toLowerCase().includes('install') ||
        /\binstall(?:ing|ation|ed)?\b/i.test(error.message)
    )
}