Skip to content

Latest commit

 

History

History
294 lines (245 loc) · 15 KB

frontend-api.md

File metadata and controls

294 lines (245 loc) · 15 KB

Frontend API

The supported renderer service API is window.pairApi. It is available only in Electron and is backed by the preload service bridge.

The source of truth is:

  • src/shared/types/ws-channels.ts for logical invoke and push channels;
  • src/ui/api/pair-api.ts for the renderer-facing API;
  • src/shared/types/ipc-channels.ts for Electron-native IPC.

Transport

flowchart LR
    UI["Renderer"]
    API["window.pairApi"]
    Preload["Preload transport"]
    Main["Electron main"]
    Broker["nvpair-ui-broker"]

    UI --> API
    API --> Preload
    Preload <-->|"service-bridge IPC"| Main
    Main <-->|"stdio JSON-RPC"| Broker
Loading

WsInvokeChannelMap and WsPushChannelMap describe logical service messages. They do not imply a WebSocket connection. Browser clients are not supported.

Renderer API

pairApi.app

  • getInitial() returns connection state and the local node ID.

pairApi.connection

  • onStateRequestRefresh(callback) requests a fresh domain snapshot.
  • onClusterIdentity(callback) reports local cluster identity changes.

pairApi.nodes

  • getInitial() returns the current node map and fetch status.
  • removeMember(nodeId) removes a cluster member.
  • onUpsert(callback) reports node additions and updates.
  • onRemove(callback) reports node removals.
  • onMembersChanged(callback) reports a complete membership snapshot.

pairApi.cluster

  • getInitial() returns identity, settings, membership, and all live inbound invites.
  • inviteNode(ipAddress) starts PIN pairing.
  • inviteStatus(inviteId) polls an outbound pairing session.
  • respondToInvite(inviteId, accept, pin?) accepts or declines an invite.
  • cancelInvite(inviteId) cancels an in-flight outbound invite, invalidating its PIN backend-side and notifying the joiner.
  • abandonIfSolo() removes a cluster created only for an unsuccessful invite.
  • onInviteReceived(callback) reports a freshly-arrived inbound pairing request.
  • onPendingInvitesChanged(callback) reports the full authoritative set of live inbound invites (re-emitted on every add or prune).

pairApi.discovery

  • getNodes() returns discoverable nodes. Each AvailableNode carries the normalized id (the host UUID for local and verified rows, or the discovery record ID for unverified rows), the display name (hostname), the reachable ipAddress/port, and the trusted and clustered flags. A clustered node already belongs to some cluster and is rendered non-invitable. Broker records also carry discoveryRecordId and identityVerification (local, pinned, or unverified). The bridge retains these provenance fields through proxy updates. Broker withdrawal deletes a pinned peer's row, including cached proxy presence. For other records, a surviving proxy keeps the row while its broker provenance and hardware are cleared. Public hardware readings cannot change provenance. Broker snapshots carry optional full hardware in info, and live discovery:node-info updates refresh the same record. Electron matches record ID, verification, and verified host identity before projecting GPU, CPU, memory, and VRAM fields. Workers collect local IPC measurements, pinned peer measurements, and sanitized unverified previews. The broker's info.observedAt supplies a stable Unix millisecond display observation time, independently of GPU sample age. Electron emits metrics:update once for each newer observation, including unchanged readings, and ignores replayed or older measurements. Changes to utilization, used memory, sample time, or directory freshness do not emit structural node or engine updates. Charts also reject non-increasing times.
  • onNodesChanged(callback) reports discovery changes.

pairApi.engines

  • getInitialState() returns engine statuses, models, active progress, and available updates.
  • toggle, install, uninstall, and update manage engine lifecycle.
  • pullModel, cancelModelPull, loadModel, unloadModel, deleteModel, and setModelExpiry manage models.
  • searchHub(engineType, nodeId) returns the curated model catalog for a node's engine.
  • onStateChanged, onProgress, and onProgressRemove expose backend truth.
  • getSettings(target) reads the owning node's authoritative settings snapshot (server port, proxy port, engine arguments) for one engine.
  • previewSettings(request) validates a draft against the owner without persisting or touching a process, returning normalized settings, per-field errors, any port conflict, and whether applying restarts or rebinds.
  • applySettings(request) commits a draft and waits for the owner's stop, rebind, and restart/readiness operation before returning its receipt. This can take minutes (vLLM permits thirty minutes for readiness; the desktop call allows thirty-four minutes). The receipt identifies the accepted operation; use onSettingsChanged for its authoritative applied or failed state. Once durably accepted, the owner continues even if the caller disconnects or loses the response; reload the snapshot before retrying.
  • onSettingsChanged(callback) reports a settings snapshot for any node — the only source of applied settings state.
  • onSettingsDisconnected(callback) reports that a node's settings authority became unreachable, so its cached snapshot is stale.

EngineType currently includes ollama, lm-studio, llama-cpp, and vllm. vLLM serves one model per process, so unloadModel (Eject) stops the engine, which frees its GPU memory, and loadModel on a stopped engine starts it. Its model list is the Hugging Face cache, which stays available while the engine is stopped, deleteModel removes a cached model (not the one loaded in vLLM; deleting an ejected or stopped vLLM's model leaves it with no model selected until loadModel picks one), pullModel downloads a Hugging Face repo into that cache (no running engine needed), and loadModel on a cached model switches the served model by restarting vLLM on it. searchHub(engineType, nodeId, query?) serves Ollama's locked local catalog, cached live Hugging Face catalogs for LM Studio and vLLM, and llama.cpp's catalog from the node's selected download source. For a peer's llama.cpp it first reads that peer's saved model download source and lists that source, Hugging Face or ModelScope, even when this device selected the other; a peer whose source cannot be read, or that does not report one, rejects the call rather than falling back. This device's own llama.cpp uses its own selection without a settings read. It returns the model rows and the backend's searchable flag, which controls whether the model hub submits an upstream query or filters the populated list locally. An unavailable catalog reports searchable: null, so an explicit query can retry after an initial browse failure. A non-empty llama.cpp or vLLM query performs an explicit bounded search: GGUF repositories in the node's llama.cpp download source, or public Hugging Face safetensors chat models for vLLM; other calls return the populated engine catalog. llama.cpp pull IDs are exact owner/repository:quantization values; load, unload, and delete all use that same exact id.

Port and launch changes work on a clustered peer as well as the local device, with one exception: managed CORS origin settings are local-only. The owning node rejects a change to browser access policy relayed from a peer. Other environment assignments pass through without an engine-option catalog.

EngineCommandPayload.command supports:

  • toggle;
  • install;
  • installAll;
  • uninstall;
  • update;
  • pullModel;
  • cancelModelPull;
  • loadModel;
  • unloadModel;
  • deleteModel;
  • setModelExpiry.

Commands return no state. Renderer stores update from engines:state-changed, engines:progress-changed, and engines:progress-cleared.

The model-bearing commands — pullModel, cancelModelPull, loadModel, unloadModel, deleteModel, and setModelExpiry — carry the target in model. A node can have several downloads outstanding, because each engine fetches one model at a time and queues the rest while different engines download side by side. Naming the model is what lets each one be cancelled, and progress-tracked, on its own.

cancelModelPull reaches engine:cancel-pull, or engine:remote-cancel-pull when nodeId names a peer. The download is not cancelled when the command returns: the row moves to Canceling and clears when the pull itself settles. A cancel whose request outlives its budget leaves the row in Canceling, because the backend is still working on it, and stays available to issue again. A download that ends while its cancel is under way — one that finished first, or whose engine could not confirm the stop — shows how it ended rather than Canceling.

pairApi.workloads

  • getInitial() returns active workloads.
  • onUpsert(callback) reports workload creation and state changes.
  • onRemove(callback) reports removal with both workload ID and origin node.

pairApi.errors

  • getInitial() returns active service errors.
  • clear(id) dismisses an error.
  • onUpdate(callback) reports the complete current error list.

pairApi.metrics

  • onUpdate(callback) reports node CPU, memory, GPU, and VRAM metrics.

Logical invoke channels

Channel Request Response
app:get-initial void AppInitialSnapshot
nodes:get-initial void node map and fetch status
nodes:remove-member { nodeId } { nodeId, removed }
discovery:get-nodes void AvailableNode[]
cluster:get-initial void ClusterInitialSnapshot
cluster:invite-node { ipAddress } Invite
cluster:invite-status { inviteId } Invite
cluster:respond-to-invite { inviteId, accept, pin? } Invite
cluster:cancel-invite { inviteId } Invite
cluster:abandon-if-solo void null
engines:get-initial void EngineInitialState
engines:get-settings EngineSettingsTarget EngineSettingsSnapshot
engines:preview-settings EngineSettingsRequest EngineSettingsPreview
engines:apply-settings EngineSettingsRequest EngineSettingsReceipt
engine:command EngineCommandPayload null
engine:search-hub { engineType, nodeId, query? } EngineHubSearchResponse
errors:get-initial void ServiceError[]
errors:clear error ID null
workloads:get-initial void workload map

Logical push channels

Channel Payload
nodes:upsert NodeItem
nodes:remove node ID
nodes:changed ClusterNode[]
engines:state-changed EngineStatePatch
engines:settings-changed EngineSettingsSnapshot
engines:settings-disconnected { nodeId }
engines:progress-changed EngineProgress
engines:progress-cleared { key }
metrics:update NodeItemMetrics
workloads:upsert Workload
workloads:remove { workloadId, originatedFrom }
errors:update ServiceError[]
cluster:invite-received Invite
cluster:pending-invites-changed Invite[]
discovery:nodes-changed AvailableNode[]
connection:cluster-identity ClusterIdentityPayload
state:request-refresh void

Electron-native API

window.windowApi covers operations that require Electron main:

  • window, tray, and clipboard operations;
  • service start, stop, restart, status, logging, versions, and licenses;
  • first-run completion;
  • wipe all PAIR-owned app data (getWipePlan() / wipeAppData() — packaged builds stop, delete, and relaunch; unpackaged builds stop, delete, and quit with a prompt to restart manually);
  • update check, download, install, and status;
  • the node-local Inference Demo (inferenceDemo.getState() / start() / stop() and the demo:state push). start() rejects if a demo is already running on this node or if no Ollama, LM Studio, or llama.cpp proxy exposed a text-generation model; callers show that as an ordinary error banner. Demo state is not synchronized across nodes.

demo:state is a main-to-renderer broadcast rather than a service push, so it is typed in IpcPushChannelMap (shared/types/ipc-channels.ts) instead of the logical push table above.

Service status remains connecting until the broker reports app:ready. Ollama and LM Studio proxy readiness is an asynchronous capability signal and does not gate the connection. ServiceStatus.error carries the latest startup or runtime failure; successful readiness and deliberate stops clear it, which is how a crash is told apart from a stop the user asked for.

The service:status broadcast is the only signal that separates a stopped service from a slow-starting one, because the service bridge cannot report its own absence. Overview and the tray read it through service-status.store.ts and show a stopped state with a Start control rather than an open-ended spinner, and the renderer re-reads every snapshot when the connector returns to connected.

Every invoke is declared in IpcChannelMap and handled through safeHandle(). IPC responses use the typed IpcResult<T> envelope.

Subscription lifecycle

Every subscription returns an unsubscribe callback. Stores and components must call it during teardown. Initial snapshots should be fetched before or alongside subscription setup so a renderer reload reconstructs state from the service.

Local HTTP

An inference feature may use engine HTTP endpoints as a local third-party client. Those requests are separate from the renderer control API and do not replace window.pairApi. Nothing in the renderer does this today.