# aimod-cc/agent2api architecture

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

- Interactive diagram: https://gitdiagram.com/aimod-cc/agent2api
- Repository: https://github.com/aimod-cc/agent2api

## Overview

Agent2API is a local multi-provider AI gateway packaged with a Tauri desktop application. An OpenAI-compatible client sends chat requests to the local HTTP server; the gateway resolves the requested model, selects eligible accounts in global priority order, adapts and forwards requests to provider services, and returns the response. The desktop UI manages accounts, models, keys, settings, tasks, and reports. The README documents the end-to-end behavior; source excerpts and the SOURCE INDEX clarify pipeline, catalog, persistence, and scheduled-task responsibilities. Unsampled implementation details are represented only where documented.

Main entry points: [`desktop-tauri/src-tauri/server/src/server/api/pipeline.rs`](https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/api/pipeline.rs) (Request pipeline), [`desktop-tauri/src-tauri/server/src/server/http.rs`](https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/http.rs) (HTTP server), [`desktop-tauri/src-tauri/server/src/server/core/providers/catalog.rs`](https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/core/providers/catalog.rs) (Model catalog), [`desktop-tauri/src-tauri/server/src/server/core/upstream/provider_loop.rs`](https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/core/upstream/provider_loop.rs) (Upstream forwarding)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_gateway["Gateway runtime"]
  node_http["HTTP server<br/>[http.rs]"]
  node_api["Chat API routes<br/>[chat.rs]"]
  node_pipeline["Request pipeline<br/>[pipeline.rs]"]
  node_catalog["Model catalog<br/>[catalog.rs]"]
  node_rules["Model rules<br/>[mod.rs]"]
  node_upstream["Upstream forwarding<br/>[provider_loop.rs]"]
end

subgraph group_accounts["Accounts and providers"]
  node_routing["Account routing<br/>[routing.rs]"]
  node_account_store[("Account store<br/>[store_crud.rs]")]
  node_adapters["Provider adapters<br/>[adapter.rs]"]
  node_auth["Login and credentials<br/>[login.rs]"]
end

subgraph group_management["Management and persistence"]
  node_config[("Gateway configuration<br/>[mod.rs]")]
  node_db[("SQLite database<br/>[mod.rs]")]
  node_tasks["Scheduled tasks<br/>[scheduled_tasks.rs]"]
  node_stats[("Request statistics<br/>[request_stats.rs]")]
  node_logs[("Runtime logging<br/>[logging.rs]")]
end

subgraph group_desktop["Desktop interface"]
  node_ui_accounts["Account management<br/>[accounts-page.tsx]"]
  node_ui_models["Model management<br/>[models-page.tsx]"]
  node_ui_settings["Settings and tasks<br/>[settings-page.tsx]"]
  node_ui_reports["Usage reports<br/>[report-page.tsx]"]
end

node_client(("OpenAI client"))
node_providers{{"AI providers"}}

node_client -->|"sends requests"| node_http
node_http -->|"routes chat"| node_api
node_api -->|"processes request"| node_pipeline
node_pipeline -->|"resolves model"| node_catalog
node_catalog -->|"applies overrides"| node_rules
node_pipeline -->|"selects candidates"| node_routing
node_routing -->|"reads accounts"| node_account_store
node_upstream -->|"builds provider requests"| node_adapters
node_upstream -->|"forwards requests"| node_providers
node_pipeline -->|"forwards chat"| node_upstream
node_auth -.->|"stores credentials"| node_account_store
node_catalog -->|"gets model listings"| node_adapters
node_config -->|"persists settings"| node_db
node_account_store -->|"persists accounts"| node_db
node_tasks -->|"reads task settings"| node_config
node_pipeline -->|"records requests"| node_stats
node_pipeline -.->|"logs failures"| node_logs
node_ui_accounts -.->|"manages accounts"| node_http
node_ui_models -.->|"manages models"| node_http
node_ui_settings -.->|"configures gateway"| node_http
node_ui_reports -.->|"views reports"| node_http

click node_http "https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/http.rs"
click node_api "https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/api/chat.rs"
click node_pipeline "https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/api/pipeline.rs"
click node_catalog "https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/core/providers/catalog.rs"
click node_rules "https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/core/model_rules/mod.rs"
click node_routing "https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/core/routing.rs"
click node_account_store "https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/core/account_store/store_crud.rs"
click node_adapters "https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/core/providers/adapter.rs"
click node_upstream "https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/core/upstream/provider_loop.rs"
click node_auth "https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/core/login.rs"
click node_config "https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/config/mod.rs"
click node_db "https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/db/mod.rs"
click node_tasks "https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/core/scheduled_tasks.rs"
click node_stats "https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/request_stats.rs"
click node_logs "https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/logging.rs"
click node_ui_accounts "https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/ui-islands/src/islands/accounts-page.tsx"
click node_ui_models "https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/ui-islands/src/islands/models-page.tsx"
click node_ui_settings "https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/ui-islands/src/islands/settings-page.tsx"
click node_ui_reports "https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/ui-islands/src/islands/report-page.tsx"

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_http,node_api,node_pipeline,node_catalog,node_rules,node_upstream,node_client toneBlue
class node_routing,node_account_store,node_adapters,node_auth toneAmber
class node_config,node_db,node_tasks,node_stats,node_logs toneMint
class node_ui_accounts,node_ui_models,node_ui_settings,node_ui_reports toneRose
class node_providers toneIndigo
```

## Components

### Gateway runtime

- [HTTP server](https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/http.rs): `desktop-tauri/src-tauri/server/src/server/http.rs`
- [Chat API routes](https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/api/chat.rs): `desktop-tauri/src-tauri/server/src/server/api/chat.rs`
- [Request pipeline](https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/api/pipeline.rs): `desktop-tauri/src-tauri/server/src/server/api/pipeline.rs`
- [Model catalog](https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/core/providers/catalog.rs): `desktop-tauri/src-tauri/server/src/server/core/providers/catalog.rs`
- [Model rules](https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/core/model_rules/mod.rs): `desktop-tauri/src-tauri/server/src/server/core/model_rules/mod.rs`
- [Upstream forwarding](https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/core/upstream/provider_loop.rs): `desktop-tauri/src-tauri/server/src/server/core/upstream/provider_loop.rs`

### Accounts and providers

- [Account routing](https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/core/routing.rs): `desktop-tauri/src-tauri/server/src/server/core/routing.rs`
- [Account store](https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/core/account_store/store_crud.rs): `desktop-tauri/src-tauri/server/src/server/core/account_store/store_crud.rs`
- [Provider adapters](https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/core/providers/adapter.rs): `desktop-tauri/src-tauri/server/src/server/core/providers/adapter.rs`
- [Login and credentials](https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/core/login.rs): `desktop-tauri/src-tauri/server/src/server/core/login.rs`

### Management and persistence

- [Gateway configuration](https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/config/mod.rs): `desktop-tauri/src-tauri/server/src/server/config/mod.rs`
- [SQLite database](https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/db/mod.rs): `desktop-tauri/src-tauri/server/src/server/db/mod.rs`
- [Scheduled tasks](https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/core/scheduled_tasks.rs): `desktop-tauri/src-tauri/server/src/server/core/scheduled_tasks.rs`
- [Request statistics](https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/request_stats.rs): `desktop-tauri/src-tauri/server/src/server/request_stats.rs`
- [Runtime logging](https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/src-tauri/server/src/server/logging.rs): `desktop-tauri/src-tauri/server/src/server/logging.rs`

### Desktop interface

- [Account management](https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/ui-islands/src/islands/accounts-page.tsx): `desktop-tauri/ui-islands/src/islands/accounts-page.tsx`
- [Model management](https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/ui-islands/src/islands/models-page.tsx): `desktop-tauri/ui-islands/src/islands/models-page.tsx`
- [Settings and tasks](https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/ui-islands/src/islands/settings-page.tsx): `desktop-tauri/ui-islands/src/islands/settings-page.tsx`
- [Usage reports](https://github.com/aimod-cc/agent2api/blob/main/desktop-tauri/ui-islands/src/islands/report-page.tsx): `desktop-tauri/ui-islands/src/islands/report-page.tsx`

### Other components

- **OpenAI client**
- **AI providers**

## Connections

- OpenAI client → HTTP server: sends requests
- HTTP server → Chat API routes: routes chat
- Chat API routes → Request pipeline: processes request (evidence: `desktop-tauri/src-tauri/server/src/server/api/pipeline.rs`)
- Request pipeline → Model catalog: resolves model (evidence: `desktop-tauri/src-tauri/server/src/server/api/pipeline.rs`)
- Model catalog → Model rules: applies overrides (evidence: `desktop-tauri/src-tauri/server/src/server/core/providers/catalog.rs`)
- Request pipeline → Account routing: selects candidates
- Account routing → Account store: reads accounts (evidence: `desktop-tauri/src-tauri/server/src/server/core/routing.rs`)
- Upstream forwarding → Provider adapters: builds provider requests (evidence: `desktop-tauri/src-tauri/server/src/server/core/providers/adapter.rs`)
- Upstream forwarding → AI providers: forwards requests
- Request pipeline → Upstream forwarding: forwards chat
- Login and credentials → Account store: stores credentials
- Model catalog → Provider adapters: gets model listings (evidence: `desktop-tauri/src-tauri/server/src/server/core/providers/catalog.rs`)
- Gateway configuration → SQLite database: persists settings (evidence: `desktop-tauri/src-tauri/server/src/server/config/mod.rs`)
- Account store → SQLite database: persists accounts
- Scheduled tasks → Gateway configuration: reads task settings (evidence: `desktop-tauri/src-tauri/server/src/server/core/scheduled_tasks.rs`)
- Request pipeline → Request statistics: records requests (evidence: `desktop-tauri/src-tauri/server/src/server/api/pipeline.rs`)
- Request pipeline → Runtime logging: logs failures (evidence: `desktop-tauri/src-tauri/server/src/server/api/pipeline.rs`)
- Account management → HTTP server: manages accounts
- Model management → HTTP server: manages models
- Settings and tasks → HTTP server: configures gateway
- Usage reports → HTTP server: views reports
