# adt109119/llamacpp-distributed-inference architecture

> Architecture diagram of the GitHub repository adt109119/llamacpp-distributed-inference, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-08-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/adt109119/llamacpp-distributed-inference
- Repository: https://github.com/adt109119/llamacpp-distributed-inference

## Overview

##### Project Type and Purpose
- This is a full-stack desktop application built with Electron for distributed LLM inference.
- The goal is to let multiple machines on a LAN collaborate on llama.cpp model inference, exposing:
- A local HTTP API server (for UI and external API calls)
- Remote RPC nodes (llama.cpp servers)
- A modern GUI (Electron renderer)
- mDNS-based service discovery and manual node management

##### Main Components
a. Electron Main Process (src/main)
- Orchestrates startup:
- Spawns local `rpc-server` and `llama-server` binaries (from bin/)
- Hosts local HTTP API on port 8080 (Node.js/Express)
- Manages mDNS discovery (bonjour-service)
- Reads/writes config via electron-store
- Communicates with renderer via IPC and preload.
b. Preload Script (src/preload)
- Exposes safe IPC channels to the renderer for:
- Fetching node list, logs, status
- Toggling theme, updating settings
c. Renderer (src/renderer)
- UI built with HTML/CSS/JS
- Displays model management, node discovery, logs, controls for starting/stopping services, theme toggle
d. Local HTTP API Server
- Listens on localhost:8080
- Implements OpenAI-compatible endpoints (`/v1/chat/completions`, `/v1/models`, `/health`)
- Forwards inference requests to local llama.cpp process or remote RPC nodes
e. Remote RPC Nodes (llama.cpp `rpc-server`)
- Each remote machine runs `rpc-server` on port 50052
- Performs gRPC inference tasks when called by the API server
f. mDNS Service Discovery
- Uses MDNS port 5353 to broadcast/receive cluster node announcements
- Automatically builds a list of available nodes
g. Models Folder
- Contains `.gguf` model files scanned at startup
- The API server loads model metadata for dropdown selection
h. Binaries Folder (`bin/`)
- Contains platform-specific executables for llama.cpp servers

##### Relationships and Data Flows
- Renderer ↔ Preload: IPC for all UI actions (node add/remove, theme change).
- Main Process ↔ Preload: IPC handlers to execute backend operations.
- Main Process → HTTP API Server: Direct instantiation or child process.
- HTTP API Server ↔ RPC Nodes: gRPC calls over port 50052 for inference tasks.
- HTTP API Server ↔ Local llama.cpp: Direct spawning of `llama-server` for local inference.
- Main Process ↔ mDNS: Broadcast and listen for node announcements every 30s.
- Renderer ← HTTP API (optional): for direct REST API testing (curl examples).

##### Architectural Patterns & Design Principles
- Electron multi-process architecture (main, preload, renderer)
- IPC boundary to secure renderer
- Modular separation: UI vs. business logic vs. external binaries
- Service discovery pattern (mDNS + manual override)
- Facade pattern: HTTP API facade abstracts local vs. remote inference
- Pluggable binaries: llama.cpp RPC server and inference server loaded per OS

##### Essential Elements to Capture
- Processes: Electron main, Preload, Renderer
- Child processes: llama-server, rpc-server
- Network protocols: HTTP, gRPC, mDNS
- Filesystem interactions: models folder, binary folder
- Service discovery vs. manual node management
- Configuration storage: electron-store
- Theme toggle and settings persistence

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% UI Layer
    subgraph "UI Layer"
        direction TB
        Renderer["Renderer UI<br/>(HTML/CSS/JS)"]:::ui
        Preload["Preload Script<br/>(IPC Exposure)"]:::ui
    end

    %% Application Layer
    subgraph "Application Layer"
        direction TB
        Main["Electron Main Process<br/>(Node.js)"]:::backend
        Utils["Main Utilities<br/>(mDNS, spawn, config)"]:::backend
    end

    %% Service Layer
    subgraph "Service Layer"
        direction TB
        API["Local HTTP API Server<br/>(Express on :8080)"]:::backend
        LocalLLama["Local llama-server<br/>(llama.cpp binary)"]:::external
    end

    %% External Nodes Layer
    subgraph "External Nodes Layer"
        direction TB
        RPCNodes["Remote RPC Nodes<br/>(gRPC on :50052)"]:::external
    end

    %% File System Layer
    subgraph "File System"
        direction TB
        Models["Models Folder<br/>(.gguf files)"]:::fs
        Scripts["Binary Download Script"]:::fs
    end

    %% mDNS Service
    MDNS["mDNS Service<br/>(bonjour-service on :5353)"]:::external

    %% Connections
    Renderer -->|"IPC (requests)"| Preload
    Preload -->|"IPC (handlers)"| Main
    Main -->|"spawns/instantiates"| API
    Main -->|"uses"| Utils
    Main -->|"mDNS broadcast/listen"| MDNS
    API -->|"spawns"| LocalLLama
    API -->|"gRPC"| RPCNodes
    Models -->|"load metadata"| API
    Models -->|"scan at startup"| Main
    Scripts -->|"download binaries"| Utils

    %% Legend
    subgraph "Legend"
        direction TB
        UNode["UI Component"]:::ui
        BNode["Backend Component"]:::backend
        ENode["External Service"]:::external
        FSNode["File System"]:::fs
    end

    %% Click Events
    click Main "https://github.com/adt109119/llamacpp-distributed-inference/blob/main/src/main/index.js"
    click Utils "https://github.com/adt109119/llamacpp-distributed-inference/blob/main/src/main/utils.js"
    click Preload "https://github.com/adt109119/llamacpp-distributed-inference/blob/main/src/preload/index.js"
    click Renderer "https://github.com/adt109119/llamacpp-distributed-inference/blob/main/src/renderer/app.js"
    click Renderer "https://github.com/adt109119/llamacpp-distributed-inference/blob/main/src/renderer/index.html"
    click Scripts "https://github.com/adt109119/llamacpp-distributed-inference/blob/main/scripts/download-binaries.js"
    click Models "https://github.com/adt109119/llamacpp-distributed-inference/tree/main/models/"

    %% Styles
    classDef ui fill:#cce5ff,stroke:#333,stroke-width:1px;
    classDef backend fill:#ccffcc,stroke:#333,stroke-width:1px;
    classDef external fill:#ffe5b4,stroke:#333,stroke-width:1px;
    classDef fs fill:#e0e0e0,stroke:#333,stroke-width:1px;
```
