Skip to content

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

Go
9.7 KB
Raw Blame
#!/usr/bin/env bash
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

# build.sh — NVIDIA Personal AI Router build script for Linux and macOS.
#
# Mirrors build.bat. Reads versions.json with jq, builds the twelve worker
# binaries with -X main.Version=... ldflags, then copies them into the
# repo-root staging bundle at:
#
#   build/bin/
#
# nvpair-ui-broker is the backend entry point in that bundle. Product packaging
# places the graphical UI alongside the bundle; the UI launches the broker, which
# supervises the workers. Requires bash, go, and jq on PATH.
#
# Windows uses build.bat; this script intentionally refuses to run there.

set -euo pipefail

ROOT="$(cd "$(dirname "$0")" && pwd)"
VERSIONS_FILE="$ROOT/versions.json"

case "$(uname -s)" in
    Linux)  PLATFORM="linux"  ;;
    Darwin) PLATFORM="darwin" ;;
    *)
        echo "ERROR: unsupported platform '$(uname -s)'. Use build.bat on Windows." >&2
        exit 1
        ;;
esac

check_tool() {
    local tool="$1" hint="$2"
    if ! command -v "$tool" >/dev/null 2>&1; then
        echo "ERROR: $tool not found in PATH." >&2
        echo "       $hint" >&2
        exit 1
    fi
}
check_tool go "Install Go 1.25+ from https://go.dev/dl/"
check_tool jq "Install jq: 'sudo apt install -y jq' on Linux, 'brew install jq' on macOS."

if [[ ! -f "$VERSIONS_FILE" ]]; then
    echo "ERROR: versions.json not found at $VERSIONS_FILE" >&2
    exit 1
fi

echo "========================================"
echo " Reading versions from $VERSIONS_FILE"
echo "========================================"
echo

# Mirror build.bat's --arg trick: component keys contain hyphens, which jq's
# bare-identifier syntax would parse as subtraction. Passing the key as a
# string variable sidesteps the ambiguity.
V_SERVICES=$(jq -r '.services'                                 "$VERSIONS_FILE")
V_PROXY=$(  jq -r --arg k 'nvpair-proxy'     '.components[$k]' "$VERSIONS_FILE")
V_NINFO=$(  jq -r --arg k 'nvpair-node-info'    '.components[$k]' "$VERSIONS_FILE")
V_NSCAN=$(  jq -r --arg k 'nvpair-node-scanner' '.components[$k]' "$VERSIONS_FILE")
V_MNODES=$( jq -r --arg k 'nvpair-manual-nodes' '.components[$k]' "$VERSIONS_FILE")
V_WLMGR=$(  jq -r --arg k 'nvpair-workload-manager' '.components[$k]' "$VERSIONS_FILE")
V_ERRORS=$( jq -r --arg k 'nvpair-errors'       '.components[$k]' "$VERSIONS_FILE")
V_ENGMGR=$( jq -r --arg k 'nvpair-engine-manager' '.components[$k]' "$VERSIONS_FILE")
V_NSETTINGS=$(jq -r --arg k 'nvpair-node-settings' '.components[$k]' "$VERSIONS_FILE")
V_BROKER=$( jq -r --arg k 'nvpair-ui-broker'    '.components[$k]' "$VERSIONS_FILE")
V_CLUMGR=$( jq -r --arg k 'nvpair-cluster-manager' '.components[$k]' "$VERSIONS_FILE")
V_SCHED=$(  jq -r --arg k 'nvpair-job-scheduler' '.components[$k]' "$VERSIONS_FILE")
V_TUI=$(    jq -r --arg k 'nvpair-tui'          '.components[$k]' "$VERSIONS_FILE")

# The release version, which lives in desktop/package.json rather than here.
#
# nvpair-tui's update notice compares this against the published release tag, so
# it is the only one of PAIR's three numbers that can answer "is there a newer
# PAIR than mine". The services suite version is currently the larger number, so
# stamping that instead would not merely be wrong, it would be silently wrong:
# every check would conclude this build is ahead of the feed and say nothing.
PACKAGE_JSON="$ROOT/../desktop/package.json"
V_RELEASE=$(jq -r '.version' "$PACKAGE_JSON" 2>/dev/null)

if [[ -z "$V_SERVICES" || "$V_SERVICES" == "null" ]]; then
    echo "ERROR: failed to parse versions.json" >&2
    exit 1
fi

if [[ -z "$V_RELEASE" || "$V_RELEASE" == "null" ]]; then
    echo "ERROR: failed to read .version from $PACKAGE_JSON" >&2
    echo "       nvpair-tui's update notice is stamped from the release version." >&2
    exit 1
fi

printf '  release           = %s\n' "$V_RELEASE"
printf '  services          = %s\n' "$V_SERVICES"
printf '  nvpair-proxy      = %s\n' "$V_PROXY"
printf '  nvpair-node-info     = %s\n' "$V_NINFO"
printf '  nvpair-node-scanner  = %s\n' "$V_NSCAN"
printf '  nvpair-manual-nodes  = %s\n' "$V_MNODES"
printf '  nvpair-workload-mgr  = %s\n' "$V_WLMGR"
printf '  nvpair-errors        = %s\n' "$V_ERRORS"
printf '  nvpair-engine-manager= %s\n' "$V_ENGMGR"
printf '  nvpair-node-settings = %s\n' "$V_NSETTINGS"
printf '  nvpair-ui-broker     = %s\n' "$V_BROKER"
printf '  nvpair-cluster-mgr   = %s\n' "$V_CLUMGR"
printf '  nvpair-job-scheduler = %s\n' "$V_SCHED"
printf '  nvpair-tui           = %s\n' "$V_TUI"
echo

echo "========================================"
echo " Building all components (platform: $PLATFORM)"
echo "========================================"
echo

build_subbinary() {
    local idx="$1" name="$2" version="$3"
    echo "[$idx/13] Building $name (v$version)..."
    (cd "$ROOT/$name" && go build -ldflags "-X main.Version=$version" -o "$name" .)
    echo "      OK"
}
build_subbinary 1 nvpair-proxy       "$V_PROXY"
build_subbinary 2 nvpair-node-info     "$V_NINFO"
build_subbinary 3 nvpair-node-scanner  "$V_NSCAN"
build_subbinary 4 nvpair-manual-nodes  "$V_MNODES"
build_subbinary 5 nvpair-workload-manager "$V_WLMGR"
build_subbinary 6 nvpair-errors        "$V_ERRORS"
build_subbinary 7 nvpair-engine-manager "$V_ENGMGR"
build_subbinary 8 nvpair-node-settings "$V_NSETTINGS"
build_subbinary 9 nvpair-ui-broker     "$V_BROKER"
build_subbinary 10 nvpair-cluster-manager "$V_CLUMGR"
build_subbinary 11 nvpair-job-scheduler   "$V_SCHED"
# nvpair-tui also carries the release version: that is what the update notice
# compares against the published tag, and its own component version means
# nothing to that comparison. A -X on a symbol path that does not exist fails
# silently, so verify the stamp rather than assuming it.
echo "[12/13] Building nvpair-tui (v$V_TUI)..."
(cd "$ROOT/nvpair-tui" && go build \
    -ldflags "-X main.Version=$V_TUI -X nvpair-tui/ui.ReleaseVersion=$V_RELEASE" \
    -o nvpair-tui .)
echo "      OK"

# inference-dispatcher is built here but is not a worker: it speaks no JSON-RPC,
# nothing supervises it, and it is deliberately absent from versions.json. It is
# an ordinary HTTP client that the Inference Demo spawns once per request, and it
# is built here because the terminal interface runs the same demo and ships from
# this bundle — a demo the desktop app can run and the terminal cannot is not a
# useful distinction to an operator.
#
# Its module lives outside this tree, at the monorepo root, for the same reason:
# it is not a service. The path is relative to services/, so a checkout without
# it fails loudly here rather than producing a bundle that is quietly missing a
# feature.
DISPATCHER_SRC="$ROOT/../scripts/inference-dispatcher"
echo "[13/13] Building inference-dispatcher (v$V_SERVICES)..."
if [ ! -d "$DISPATCHER_SRC" ]; then
    echo "ERROR: $DISPATCHER_SRC not found." >&2
    echo "       The Inference Demo client lives at scripts/inference-dispatcher" >&2
    echo "       in the monorepo root; this bundle cannot be built without it." >&2
    exit 1
fi
(cd "$DISPATCHER_SRC" && go build -ldflags "-X main.Version=$V_SERVICES" -o inference-dispatcher .)
echo "      OK"

BIN_OUT="$ROOT/build/bin"

echo
echo "========================================"
echo " Copying binaries to $BIN_OUT"
echo "========================================"
echo

# Start from an empty bin dir. In any reused build workspace, a binary built for
# a different revision -- for example, a component that doesn't exist here --
# could otherwise linger and ride into the bundle. Wipe and recreate the
# directory, then copy the fresh set so it contains exactly this revision's
# components.
rm -rf "$BIN_OUT"
mkdir -p "$BIN_OUT"
cp "$ROOT/nvpair-proxy/nvpair-proxy"               "$BIN_OUT/nvpair-proxy"
cp "$ROOT/nvpair-node-info/nvpair-node-info"       "$BIN_OUT/nvpair-node-info"
cp "$ROOT/nvpair-node-scanner/nvpair-node-scanner" "$BIN_OUT/nvpair-node-scanner"
cp "$ROOT/nvpair-manual-nodes/nvpair-manual-nodes" "$BIN_OUT/nvpair-manual-nodes"
cp "$ROOT/nvpair-workload-manager/nvpair-workload-manager" "$BIN_OUT/nvpair-workload-manager"
cp "$ROOT/nvpair-errors/nvpair-errors"             "$BIN_OUT/nvpair-errors"
cp "$ROOT/nvpair-engine-manager/nvpair-engine-manager" "$BIN_OUT/nvpair-engine-manager"
cp "$ROOT/nvpair-node-settings/nvpair-node-settings" "$BIN_OUT/nvpair-node-settings"
cp "$ROOT/nvpair-ui-broker/nvpair-ui-broker"       "$BIN_OUT/nvpair-ui-broker"
cp "$ROOT/nvpair-cluster-manager/nvpair-cluster-manager" "$BIN_OUT/nvpair-cluster-manager"
cp "$ROOT/nvpair-job-scheduler/nvpair-job-scheduler" "$BIN_OUT/nvpair-job-scheduler"
cp "$ROOT/nvpair-tui/nvpair-tui"                   "$BIN_OUT/nvpair-tui"
# Beside the binaries it is not one of: nvpair-tui resolves the broker next to
# its own executable, and the demo finds this the same way.
cp "$DISPATCHER_SRC/inference-dispatcher"          "$BIN_OUT/inference-dispatcher"

echo
echo "========================================"
echo " Build complete (services v$V_SERVICES)"
echo "========================================"
echo
printf '  Proxy:        %s\n' "$BIN_OUT/nvpair-proxy"
printf '  Node Info:    %s\n' "$BIN_OUT/nvpair-node-info"
printf '  Node Scanner: %s\n' "$BIN_OUT/nvpair-node-scanner"
printf '  Manual Nodes: %s\n' "$BIN_OUT/nvpair-manual-nodes"
printf '  Workload Mgr: %s\n' "$BIN_OUT/nvpair-workload-manager"
printf '  Errors:       %s\n' "$BIN_OUT/nvpair-errors"
printf '  Engine Mgr:   %s\n' "$BIN_OUT/nvpair-engine-manager"
printf '  Node Settings:%s\n' " $BIN_OUT/nvpair-node-settings"
printf '  UI Broker:    %s\n' "$BIN_OUT/nvpair-ui-broker"
printf '  Cluster Mgr:  %s\n' "$BIN_OUT/nvpair-cluster-manager"
printf '  Job Scheduler:%s\n' " $BIN_OUT/nvpair-job-scheduler"
printf '  TUI:          %s\n' "$BIN_OUT/nvpair-tui"
printf '  Demo client:  %s\n' "$BIN_OUT/inference-dispatcher"
echo