# ahqsoftorg/ahqai architecture

> Architecture diagram of the GitHub repository ahqsoftorg/ahqai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ahqsoftorg/ahqai
- Repository: https://github.com/ahqsoftorg/ahqai

## Overview

#### Purpose and topology
ahq-ai is a multi-runtime AI/chat system centered on a Rust server that hosts or controls llama.cpp inference, a Tauri-based cross-platform client, and supporting administration, runtime, speech, and installation utilities. The top-level entry points are:
- `server/src/main.rs`
- `client/src/main.tsx`
- `client/src-tauri/src/main.rs`
- `ahqai-admin/src/main.rs`

#### Server bootstrap and API composition
The server executable initializes application services and composes distinct API domains: HTTP transport, authentication, chat, admin, FFI, and llama operations. `server/src/server/mod.rs` is the principal server boundary; `server/src/server/apiset.rs` likely binds enabled API sets/routes.
- `server/src/main.rs`
- `server/src/server/mod.rs`
- `server/src/server/http/mod.rs`

Control flow is client request/connection → HTTP/auth boundary → chat or management API → model/runtime or persistence backend → streamed or regular response.

#### Authentication, identity, and persistence abstraction
Authentication is separated from request handlers and supports pluggable backing stores. Password/key handling is implemented locally, while authentication servers support in-memory Moka, MongoDB, and TiKV; cache implementations include Moka and Redis. This is the principal external-service boundary for user/session state.
- `server/src/auth/mod.rs`
- `server/src/auth/authserver/mongodb/mod.rs`
- `server/src/auth/cache/redis/mod.rs`

Shared database/error contracts reside in `server/src/structs/db.rs` and `server/src/structs/error.rs`, insulating API and UI layers from concrete storage providers.

#### Llama inference and native-runtime boundary
The server’s llama subsystem owns model-facing requests and inference structures, while an FFI module forms the native integration boundary. Server-side UI includes model and lifecycle management, suggesting configuration-driven loading and operational control of local models.
- `server/src/server/llama/mod.rs`
- `server/src/server/ffi/mod.rs`
- `server/src/ui/llama/manager.rs`

`getllamacpp` is a separate Rust acquisition/build utility for llama.cpp, so model runtime provisioning is intentionally outside ordinary request handling:
- `getllamacpp/src/main.rs`
- `getllamacpp/src/download/mod.rs`
- `getllamacpp/src/compile/mod.rs`

#### Client application and server protocol
The client is a React/TypeScript application built with Vite. It presents splash, server setup, chat, sidebar, and settings views; server communication is centralized rather than embedded in components. Chat and WebSocket code are the key protocol consumers.
- `client/src/main.tsx`
- `client/src/App/App/Chat/index.tsx`
- `client/src/App/server/ws/index.ts`

The UI maintains local server and chat records through a store/database layer, then uses REST, WebSocket, keyboard, and key modules to interact with configured servers.
- `client/src/App/store/index.ts`
- `client/src/App/store/db/chats.ts`
- `client/src/App/server/rest.ts`

#### Desktop/mobile host and secure plugin layer
Tauri packages the web client for desktop and mobile. The Rust host owns native startup, SQL migrations/storage, capabilities, and platform packaging. A separate plugin exposes desktop/mobile commands and integrity checks to guest JavaScript, creating a deliberate privileged boundary.
- `client/src-tauri/src/lib.rs`
- `client/src-tauri/src/sql.rs`
- `client/src-plugin/src/lib.rs`

Platform-specific implementations are present for Android and Apple:
- `client/src-plugin/android/src/main/java/AhqAi.kt`
- `client/src-plugin/ios/Sources/AhqAi.swift`
- `client/src-plugin/src/commands.rs`

#### Operational tools, runtimes, and delivery
`ahqai-admin` is a separate Rust administration client/API surface for general, user, and copy operations. `ahqrt` provides reusable Rust runtime primitives, including futures, memory/container types, and C bindings; `sherpa` wraps native speech-recognition functionality. CI separately builds server, client, Apple client, documentation pages, and Rust linting.
- `ahqai-admin/src/api/mod.rs`
- `ahqrt/src/lib.rs`
- `sherpa/sherpac/src/lib.rs`

Relevant delivery infrastructure:
- `.github/workflows/server.yml`
- `.github/workflows/client.yml`
- `.github/workflows/client-apple.yml`

Main entry points: [`server/src/auth/mod.rs`](https://github.com/ahqsoftorg/ahqai/blob/main/server/src/auth/mod.rs) (Authentication), [`client/src/main.tsx`](https://github.com/ahqsoftorg/ahqai/blob/main/client/src/main.tsx) (React client), [`server/src/server/mod.rs`](https://github.com/ahqsoftorg/ahqai/blob/main/server/src/server/mod.rs) (Server composition), [`server/src/server/ffi/mod.rs`](https://github.com/ahqsoftorg/ahqai/blob/main/server/src/server/ffi/mod.rs) (Native FFI)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_server["Server"]
  node_server_main["Server bootstrap<br/>Rust executable<br/>[main.rs]"]
  node_server_boundary["Server composition<br/>API host<br/>[mod.rs]"]
  node_http["HTTP transport<br/>request boundary<br/>[mod.rs]"]
  node_auth["Authentication<br/>identity service<br/>[mod.rs]"]
  node_mongo_auth[("Mongo auth store<br/>external store adapter<br/>[mod.rs]")]
  node_redis_cache[("Redis cache<br/>external cache adapter<br/>[mod.rs]")]
  node_db_contracts["Database contracts<br/>shared abstractions<br/>[db.rs]"]
  node_llama_api["Llama operations<br/>inference service<br/>[mod.rs]"]
  node_ffi{{"Native FFI<br/>runtime boundary<br/>[mod.rs]"}}
  node_model_manager["Model manager UI<br/>operations UI<br/>[manager.rs]"]
  node_admin["Admin client<br/>Rust administration API<br/>[mod.rs]"]
end

subgraph group_client["Client"]
  node_web_client["React client<br/>web application<br/>[main.tsx]"]
  node_chat_view["Chat view<br/>UI feature<br/>[index.tsx]"]
  node_server_protocol["Server protocol<br/>REST/WebSocket client<br/>[index.ts]"]
  node_client_store[("Client store<br/>local state<br/>[index.ts]")]
  node_tauri_host["Tauri host<br/>desktop/mobile host<br/>[lib.rs]"]
  node_tauri_sql[("Tauri SQL storage<br/>local database<br/>[sql.rs]")]
  node_secure_plugin{{"Secure plugin<br/>privileged bridge<br/>[lib.rs]"}}
end

subgraph group_native["Native tooling"]
  node_llamacpp_tool["llama.cpp provisioner<br/>Rust utility<br/>[main.rs]"]
  node_runtime["ahqrt<br/>Rust runtime primitives<br/>[lib.rs]"]
  node_speech{{"Sherpa speech runtime<br/>native speech wrapper<br/>[lib.rs]"}}
  node_server_ci["Server CI<br/>delivery workflow<br/>[server.yml]"]
  node_client_ci["Client CI<br/>delivery workflow<br/>[client.yml]"]
end

node_server_main -->|"initializes"| node_server_boundary
node_server_boundary -->|"serves"| node_http
node_http -->|"authenticates"| node_auth
node_auth -.->|"pluggable identity store"| node_mongo_auth
node_auth -.->|"cache backend"| node_redis_cache
node_auth -->|"uses contracts"| node_db_contracts
node_server_boundary -->|"routes inference"| node_llama_api
node_llama_api -->|"invokes native runtime"| node_ffi
node_model_manager -->|"manages models"| node_llama_api
node_web_client -->|"renders"| node_chat_view
node_chat_view -->|"sends chat"| node_server_protocol
node_web_client -->|"persists local records"| node_client_store
node_server_protocol -->|"REST/WebSocket requests"| node_http
node_tauri_host -->|"packages guest UI"| node_web_client
node_tauri_host -->|"owns"| node_tauri_sql
node_web_client -->|"calls privileged commands"| node_secure_plugin
node_admin -->|"management operations"| node_server_boundary
node_llamacpp_tool -.->|"provisions llama.cpp"| node_ffi
node_runtime -.->|"native primitives"| node_ffi
node_speech -.->|"speech capability"| node_ffi
node_server_ci -.->|"builds"| node_server_main
node_client_ci -.->|"builds"| node_tauri_host

click node_server_main "https://github.com/ahqsoftorg/ahqai/blob/main/server/src/main.rs"
click node_server_boundary "https://github.com/ahqsoftorg/ahqai/blob/main/server/src/server/mod.rs"
click node_http "https://github.com/ahqsoftorg/ahqai/blob/main/server/src/server/http/mod.rs"
click node_auth "https://github.com/ahqsoftorg/ahqai/blob/main/server/src/auth/mod.rs"
click node_mongo_auth "https://github.com/ahqsoftorg/ahqai/blob/main/server/src/auth/authserver/mongodb/mod.rs"
click node_redis_cache "https://github.com/ahqsoftorg/ahqai/blob/main/server/src/auth/cache/redis/mod.rs"
click node_db_contracts "https://github.com/ahqsoftorg/ahqai/blob/main/server/src/structs/db.rs"
click node_llama_api "https://github.com/ahqsoftorg/ahqai/blob/main/server/src/server/llama/mod.rs"
click node_ffi "https://github.com/ahqsoftorg/ahqai/blob/main/server/src/server/ffi/mod.rs"
click node_model_manager "https://github.com/ahqsoftorg/ahqai/blob/main/server/src/ui/llama/manager.rs"
click node_web_client "https://github.com/ahqsoftorg/ahqai/blob/main/client/src/main.tsx"
click node_chat_view "https://github.com/ahqsoftorg/ahqai/blob/main/client/src/App/App/Chat/index.tsx"
click node_server_protocol "https://github.com/ahqsoftorg/ahqai/blob/main/client/src/App/server/ws/index.ts"
click node_client_store "https://github.com/ahqsoftorg/ahqai/blob/main/client/src/App/store/index.ts"
click node_tauri_host "https://github.com/ahqsoftorg/ahqai/blob/main/client/src-tauri/src/lib.rs"
click node_tauri_sql "https://github.com/ahqsoftorg/ahqai/blob/main/client/src-tauri/src/sql.rs"
click node_secure_plugin "https://github.com/ahqsoftorg/ahqai/blob/main/client/src-plugin/src/lib.rs"
click node_admin "https://github.com/ahqsoftorg/ahqai/blob/main/ahqai-admin/src/api/mod.rs"
click node_llamacpp_tool "https://github.com/ahqsoftorg/ahqai/blob/main/getllamacpp/src/main.rs"
click node_runtime "https://github.com/ahqsoftorg/ahqai/blob/main/ahqrt/src/lib.rs"
click node_speech "https://github.com/ahqsoftorg/ahqai/blob/main/sherpa/sherpac/src/lib.rs"
click node_server_ci "https://github.com/ahqsoftorg/ahqai/blob/main/.github/workflows/server.yml"
click node_client_ci "https://github.com/ahqsoftorg/ahqai/blob/main/.github/workflows/client.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_server_main,node_server_boundary,node_http,node_auth,node_mongo_auth,node_redis_cache,node_db_contracts,node_llama_api,node_ffi,node_model_manager,node_admin toneBlue
class node_web_client,node_chat_view,node_server_protocol,node_client_store,node_tauri_host,node_tauri_sql,node_secure_plugin toneAmber
class node_llamacpp_tool,node_runtime,node_speech,node_server_ci,node_client_ci toneMint
```

## Components

### Server

Rust API, identity, and inference host.

- [Server bootstrap](https://github.com/ahqsoftorg/ahqai/blob/main/server/src/main.rs): Rust executable · `server/src/main.rs`
- [Server composition](https://github.com/ahqsoftorg/ahqai/blob/main/server/src/server/mod.rs): API host · Composes enabled API domains and application services. · `server/src/server/mod.rs`
- [HTTP transport](https://github.com/ahqsoftorg/ahqai/blob/main/server/src/server/http/mod.rs): request boundary · `server/src/server/http/mod.rs`
- [Authentication](https://github.com/ahqsoftorg/ahqai/blob/main/server/src/auth/mod.rs): identity service · Separates password/key handling from API handlers. · `server/src/auth/mod.rs`
- [Mongo auth store](https://github.com/ahqsoftorg/ahqai/blob/main/server/src/auth/authserver/mongodb/mod.rs): external store adapter · `server/src/auth/authserver/mongodb/mod.rs`
- [Redis cache](https://github.com/ahqsoftorg/ahqai/blob/main/server/src/auth/cache/redis/mod.rs): external cache adapter · `server/src/auth/cache/redis/mod.rs`
- [Database contracts](https://github.com/ahqsoftorg/ahqai/blob/main/server/src/structs/db.rs): shared abstractions · Storage and error contracts isolate concrete providers. · `server/src/structs/db.rs`
- [Llama operations](https://github.com/ahqsoftorg/ahqai/blob/main/server/src/server/llama/mod.rs): inference service · Owns model-facing requests and inference structures. · `server/src/server/llama/mod.rs`
- [Native FFI](https://github.com/ahqsoftorg/ahqai/blob/main/server/src/server/ffi/mod.rs): runtime boundary · `server/src/server/ffi/mod.rs`
- [Model manager UI](https://github.com/ahqsoftorg/ahqai/blob/main/server/src/ui/llama/manager.rs): operations UI · Controls model configuration and lifecycle. · `server/src/ui/llama/manager.rs`
- [Admin client](https://github.com/ahqsoftorg/ahqai/blob/main/ahqai-admin/src/api/mod.rs): Rust administration API · Provides general, user, and copy operations. · `ahqai-admin/src/api/mod.rs`

### Client

React UI and Tauri host.

- [React client](https://github.com/ahqsoftorg/ahqai/blob/main/client/src/main.tsx): web application · `client/src/main.tsx`
- [Chat view](https://github.com/ahqsoftorg/ahqai/blob/main/client/src/App/App/Chat/index.tsx): UI feature · `client/src/App/App/Chat/index.tsx`
- [Server protocol](https://github.com/ahqsoftorg/ahqai/blob/main/client/src/App/server/ws/index.ts): REST/WebSocket client · Centralizes configured-server communication. · `client/src/App/server/ws/index.ts`
- [Client store](https://github.com/ahqsoftorg/ahqai/blob/main/client/src/App/store/index.ts): local state · Maintains server and chat records. · `client/src/App/store/index.ts`
- [Tauri host](https://github.com/ahqsoftorg/ahqai/blob/main/client/src-tauri/src/lib.rs): desktop/mobile host · Owns native startup, storage, migrations, and packaging. · `client/src-tauri/src/lib.rs`
- [Tauri SQL storage](https://github.com/ahqsoftorg/ahqai/blob/main/client/src-tauri/src/sql.rs): local database · `client/src-tauri/src/sql.rs`
- [Secure plugin](https://github.com/ahqsoftorg/ahqai/blob/main/client/src-plugin/src/lib.rs): privileged bridge · Exposes native commands and integrity checks to guest JavaScript. · `client/src-plugin/src/lib.rs`

### Native tooling

Provisioning and reusable native runtimes.

- [llama.cpp provisioner](https://github.com/ahqsoftorg/ahqai/blob/main/getllamacpp/src/main.rs): Rust utility · Downloads and compiles the inference runtime outside request handling. · `getllamacpp/src/main.rs`
- [ahqrt](https://github.com/ahqsoftorg/ahqai/blob/main/ahqrt/src/lib.rs): Rust runtime primitives · Provides reusable futures, memory/container types, and C bindings. · `ahqrt/src/lib.rs`
- [Sherpa speech runtime](https://github.com/ahqsoftorg/ahqai/blob/main/sherpa/sherpac/src/lib.rs): native speech wrapper · `sherpa/sherpac/src/lib.rs`
- [Server CI](https://github.com/ahqsoftorg/ahqai/blob/main/.github/workflows/server.yml): delivery workflow · `.github/workflows/server.yml`
- [Client CI](https://github.com/ahqsoftorg/ahqai/blob/main/.github/workflows/client.yml): delivery workflow · `.github/workflows/client.yml`

## Connections

- Server bootstrap → Server composition: initializes
- Server composition → HTTP transport: serves
- HTTP transport → Authentication: authenticates
- Authentication → Mongo auth store: pluggable identity store
- Authentication → Redis cache: cache backend
- Authentication → Database contracts: uses contracts
- Server composition → Llama operations: routes inference
- Llama operations → Native FFI: invokes native runtime
- Model manager UI → Llama operations: manages models
- React client → Chat view: renders
- Chat view → Server protocol: sends chat
- React client → Client store: persists local records
- Server protocol → HTTP transport: REST/WebSocket requests
- Tauri host → React client: packages guest UI
- Tauri host → Tauri SQL storage: owns
- React client → Secure plugin: calls privileged commands
- Admin client → Server composition: management operations
- llama.cpp provisioner → Native FFI: provisions llama.cpp
- ahqrt → Native FFI: native primitives
- Sherpa speech runtime → Native FFI: speech capability
- Server CI → Server bootstrap: builds
- Client CI → Tauri host: builds
