Router that virtually distributes inference across connected devices in the home.
Go
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
/**
* Makes the `nvpair` command reachable from any terminal. `nvpair` is the bundled
* terminal UI (`nvpair-tui`), which spawns and supervises its own broker, so the
* launcher is a one-line wrapper around the packaged Go binary:
*
* <resources>/cli-bin/nvpair-tui "$@"
*
* The launcher is generated at runtime with an absolute path from
* `process.resourcesPath`, so there is never any guessing of the platform binary
* name or layout. It is placed on PATH:
* - Windows: `<userData>\bin\nvpair.cmd`, and `<userData>\bin` is added to the
* per-user PATH (no elevation needed; persists for new terminals).
* - Linux: `~/.local/bin/nvpair` (already on PATH for typical setups).
* - macOS: `/usr/local/bin/nvpair` when writable; otherwise skipped (best-effort).
*
* Best-effort and non-blocking: any failure is logged and ignored so it can never
* delay or break startup.
*/
import fs from 'fs'
import os from 'os'
import path from 'path'
import { execFile } from 'child_process'
import { app } from 'electron'
import { currentPlatform } from '@/shared/utils/platform'
import { modularBinaryFileName } from '@/shared/constants/modular-binaries'
import { APP_DISPLAY_NAME } from '@/shared/constants/app'
import { createStructuredLogger } from '@/shared/utils/log'
import getErrorString from '@/shared/utils/get-error-string'
const log = createStructuredLogger('app')
function tuiPath(): string {
return path.join(
process.resourcesPath,
'cli-bin',
modularBinaryFileName('nvpair-tui', currentPlatform())
)
}
/** Ensure `nvpair` is on PATH. Packaged builds only — never touch a dev machine. */
export function ensureNvpairOnPath(): void {
if (!app.isPackaged) return
try {
const tui = tuiPath()
if (!fs.existsSync(tui)) return
if (currentPlatform() === 'win32') ensureWindows(tui)
else ensurePosix(tui)
} catch (err) {
log.warn({
sublevel: 'cli',
message: `Could not install nvpair launcher: ${getErrorString(err)}`
})
}
}
function ensureWindows(tui: string): void {
const binDir = path.join(app.getPath('userData'), 'bin')
fs.mkdirSync(binDir, { recursive: true })
const launcher = path.join(binDir, 'nvpair.cmd')
const contents = '@echo off\r\n' + `"${tui}" %*\r\n`
writeIfChanged(launcher, contents)
addToWindowsUserPath(binDir)
}
function ensurePosix(tui: string): void {
const binDir = pickPosixBinDir()
if (!binDir) return
const launcher = path.join(binDir, 'nvpair')
const contents =
'#!/bin/sh\n' +
`# Auto-generated by ${APP_DISPLAY_NAME}. Runs the bundled terminal UI.\n` +
`exec "${tui}" "$@"\n`
writeIfChanged(launcher, contents)
try {
fs.chmodSync(launcher, 0o755)
} catch {
// Mode may already be correct; ignore.
}
}
/** First writable, on-PATH bin dir for the platform, created if needed. */
function pickPosixBinDir(): string | null {
const candidates =
currentPlatform() === 'darwin'
? ['/usr/local/bin']
: [path.join(os.homedir(), '.local', 'bin'), '/usr/local/bin']
for (const dir of candidates) {
try {
fs.mkdirSync(dir, { recursive: true })
fs.accessSync(dir, fs.constants.W_OK)
return dir
} catch {
continue
}
}
return null
}
function writeIfChanged(file: string, contents: string): void {
try {
if (fs.existsSync(file) && fs.readFileSync(file, 'utf8') === contents) return
} catch {
// Unreadable existing file — overwrite below.
}
fs.writeFileSync(file, contents)
}
/**
* Append `dir` to the current user's PATH (HKCU) via PowerShell, idempotently.
* `[Environment]::SetEnvironmentVariable(..., 'User')` persists it and broadcasts
* WM_SETTINGCHANGE so new shells pick it up without a reboot.
*/
function addToWindowsUserPath(dir: string): void {
const escaped = dir.replace(/'/g, "''")
const script = [
`$d = '${escaped}'`,
`$p = [Environment]::GetEnvironmentVariable('Path','User')`,
`if ($null -eq $p) { $p = '' }`,
`if (-not ($p.Split(';') -contains $d)) {`,
` $np = if ($p -eq '') { $d } else { $p.TrimEnd(';') + ';' + $d }`,
` [Environment]::SetEnvironmentVariable('Path', $np, 'User')`,
`}`
].join('; ')
execFile(
'powershell',
['-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-Command', script],
err => {
if (err) {
log.warn({
sublevel: 'cli',
message: `Could not update PATH: ${getErrorString(err)}`
})
}
}
)
}