# chi-kai/charon architecture

> Architecture diagram of the GitHub repository chi-kai/charon, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/chi-kai/charon
- Repository: https://github.com/chi-kai/charon

## Overview

##### Project Type & Purpose
- The project is a full-stack backend service (built in Rust using Cargo) that manages AI API routing and extensibility (with plugins such as caching).
- It leverages high-performance networking (based on the Pingora framework) to accept requests, route them appropriately, and manage API key authentication.
- Containerized deployment is supported via Docker and Kubernetes, making it a distributed system that can operate in cloud or local environments.

##### File Structure
- Top-Level Files: The presence of Cargo.toml and Cargo.lock indicate a Rust project. The Dockerfile, docker-compose.yml, and the entire k8s directory show container orchestration and deployment configurations.
- The main source code is in the src folder with key modules:
- src/main.rs – entry point for the application.
- src/gateway.rs – likely responsible for handling incoming requests.
- src/config.rs – for parsing and managing configuration (using YAML as indicated by config.yaml).
- src/mods – contains extendable modules; here the “cache” module is prominent with multiple implementations (memory cache, redis cache, etc.).
- The k8s folder holds Kubernetes deployment, service, ingress, configmap, secrets, and namespace definitions, establishing how the system interfaces with Kubernetes clusters.

##### Main Components and Their Relationships
a. Gateway/Router:
- Central component that accepts client requests, parses configuration, and routes to specific AI services.
- Interacts with the configuration module to determine routing patterns and API key usage.
b. Configuration Module:
- Handles the YAML configuration (config.yaml), determines listening parameters (host, port), routing definitions, and plugin settings.
- Connects with both the Gateway and any plugin implementations.
c. Plugin Modules (Cache):
- Implements caching logic (e.g., memory cache, redis cache, vector cache) to optionally store and retrieve AI responses.
- These modules are designed to be extensible so that additional functionality (like content detection) can be integrated seamlessly.
d. Downstream AI Services:
- External APIs such as Qwen and DeepSeek are the target endpoints after request routing.
- The system uses API keys attached to each route; make sure to show that these endpoints are external.
e. Deployment & Orchestration Components:
- Docker: The Dockerfile and docker-compose.yml encapsulate the application for containerized deployments.
- Kubernetes: The k8s directory configures how the service is deployed, scaled, and managed inside a Kubernetes cluster. This includes namespace, configmap, secrets, deployment, service, and ingress configurations.

##### Architectural Patterns and Design Principles
- Modular Architecture: By separating core functionality (gateway, config, and mods), the design enables extensibility and easy integration of new plugins or services.
- Microservice Readiness: Although the project is a single backend, the defined routes and modular plugin support imply that it can be scaled and integrated like a microservices setup.
- Separation of Concerns: Routing logic, configuration management, and caching are clearly delineated among separate modules.
- Containerization and Orchestration: The inclusion of Docker and Kubernetes files indicates a focus on portability, scalability, and ease of deployment.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Request Handling
    A["Client Request"]:::client
    B["AI Gateway/Router"]:::gateway
    A -->|"HTTP_Request"| B

    %% Configuration & Plugin Management
    C["Configuration Module"]:::config
    F["Config File"]:::config
    C -->|"Uses"| F
    B -->|"Load_Config"| C

    subgraph "Cache Plugin Modules"
        D["Cache Core"]:::plugin
        E1["Memory Cache"]:::plugin
        E2["Redis Cache"]:::plugin
        E3["Vector Cache"]:::plugin
        D -->|"Implments"| E1
        D -->|"Implments"| E2
        D -->|"Implments"| E3
    end
    B -->|"Cache_Lookup"| D
    C -->|"Configures"| D

    %% External AI Services
    G["AI Service: Qwen"]:::external
    H["AI Service: DeepSeek"]:::external
    B -->|"Route_Qwen"| G
    B -->|"Route_DeepSeek"| H

    %% Deployment & Orchestration
    subgraph "Deployment & Orchestration"
        subgraph "Docker"
            D1["Dockerfile"]:::deploy
            D2["docker-compose.yml"]:::deploy
        end
        subgraph "Kubernetes"
            K1["configmap.yaml"]:::deploy
            K2["deployment.yaml"]:::deploy
            K3["ingress.yaml"]:::deploy
            K4["namespace.yaml"]:::deploy
            K5["secrets.yaml"]:::deploy
            K6["service.yaml"]:::deploy
        end
    end
    B -->|"Deployed_via"| D1
    B -->|"Orchestrated_by"| K2

    %% Click Events
    click B "https://github.com/chi-kai/charon/blob/main/src/gateway.rs"
    click C "https://github.com/chi-kai/charon/blob/main/src/config.rs"
    click F "https://github.com/chi-kai/charon/blob/main/config.yaml"
    click D "https://github.com/chi-kai/charon/blob/main/src/mods/cache/mod.rs"
    click E1 "https://github.com/chi-kai/charon/blob/main/src/mods/cache/memory_cache.rs"
    click E2 "https://github.com/chi-kai/charon/blob/main/src/mods/cache/redis_cache.rs"
    click E3 "https://github.com/chi-kai/charon/blob/main/src/mods/cache/vector_cache.rs"
    click D1 "https://github.com/chi-kai/charon/tree/main/Dockerfile"
    click D2 "https://github.com/chi-kai/charon/blob/main/docker-compose.yml"
    click K1 "https://github.com/chi-kai/charon/blob/main/k8s/configmap.yaml"
    click K2 "https://github.com/chi-kai/charon/blob/main/k8s/deployment.yaml"
    click K3 "https://github.com/chi-kai/charon/blob/main/k8s/ingress.yaml"
    click K4 "https://github.com/chi-kai/charon/blob/main/k8s/namespace.yaml"
    click K5 "https://github.com/chi-kai/charon/blob/main/k8s/secrets.yaml"
    click K6 "https://github.com/chi-kai/charon/blob/main/k8s/service.yaml"

    %% Styles
    classDef gateway fill:#fde2e2,stroke:#611f69,stroke-width:2px;
    classDef config fill:#cde8f8,stroke:#1c6ea4,stroke-width:2px;
    classDef plugin fill:#d5e8d4,stroke:#82b366,stroke-width:2px;
    classDef external fill:#fff2cc,stroke:#d6b656,stroke-width:2px;
    classDef deploy fill:#e1d5e7,stroke:#9673a6,stroke-width:2px;
    classDef client fill:#f9d5bb,stroke:#e08764,stroke-width:2px;
```
