# coaidev/coai architecture

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

- Interactive diagram: https://gitdiagram.com/coaidev/coai
- Repository: https://github.com/coaidev/coai

## Overview

Based on our analysis of the file structure and README content, Chat Nio is a comprehensive, full-stack AIGC solution designed to serve both web clients and API consumers. It combines a feature-rich React-based frontend (including support for multi-device and PWA) with a robust Golang-based backend that manages authentication, AI model integrations, billing, channel management, and administrative functions. In addition, there are multiple “adapter” modules that encapsulate integrations to various AI service providers (e.g., OpenAI, Azure OpenAI, Midjourney, etc.) to support a wide range of models and functionalities.

- Frontend Application:
- Backend Application:
– Use the Golang code (main.go, and folders such as “manager”, “auth”, “channel”, “connection”, “globals”, “middleware”, “utils”) as one major block. This layer is implemented with Gin and handles the API endpoints, business logic, and channel/scheduling for AI requests.
- Adapter Modules:
- Administrative / Management Services:
- Data Persistence & Caching:
- Additional Modules:
– The “addition” folder includes extra functionalities (article generation, card services, web search, etc.) that may be drawn as independent modules that interact with the backend.
- External Services & Integrations:
- API Gateway / Proxy:
– Note that the system supports OpenAI API compatible endpoints (/v1/chat/completions, /v1/images, etc.), so show that there is a proxy layer in the backend for API distribution.

##### Relationships and Interactions
- Link the backend with the adapter modules – indicate that certain components in the backend (such as routers and managers) call out to specific adapters. For example, a chat request may flow from the backend’s “manager” layer to one of the “adapter” implementations (Azure, OpenAI, etc.), each of which then follows its own chain of processing (e.g., chat.go, processor.go).
- If applicable, show that the admin panel (internal route under frontend or via separate protected endpoints) communicates with backend management APIs.

##### Architectural Patterns and Design Principles
- Modularization: Emphasize that the project is divided into clear logical modules (frontend, backend, adapters, admin) with separation of concerns.
- API Proxying & Gateway: Note that the backend is structured to support both UI-based interactions and OpenAI-compatible API calls, which is central to its design.
- Microservice/Adapter Pattern: Even though it’s a monorepo, the “adapter” folder demonstrates an adapter pattern helping integrate various external AI engines.
- PWA & Desktop: The frontend is designed both as a web app and a desktop application (via Tauri), so indicate this dual-consumer support.
- Scalability & Caching: Show caching layers (Redis) and load-balanced channels in the channel management modules to underline scalability.

##### Specify Technologies, Frameworks, and Integration Points
- Frontend: React, Redux, Tailwind CSS, Vite, PWA, and Tauri for desktop.
- Backend: Golang, Gin framework, Redis, MySQL.
- Adapters: Custom Golang modules for different AI providers (Azure, OpenAI, Bing, Claude, etc.).
- Infrastructure: Docker, Docker Compose, a variety of CI/CD workflows, and deployment scripts.
- Others: WebSockets (for real-time communication) and support for file parsing, Markdown rendering, etc.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend Layer"
        F["Frontend (React+Redux, Tailwind, Vite, PWA, Tauri)"]:::frontend
    end

    %% API Gateway/Proxy Layer
    subgraph "API Gateway/Proxy Layer"
        AG1["Manager Router"]:::gateway
        AG2["Auth Router"]:::gateway
    end

    %% Backend Layer
    subgraph "Backend Layer"
        B["Backend (Golang + Gin)"]:::backend
        BG["main.go"]:::backend
        AUTH["Auth Module"]:::backend
        CH["Channel Module"]:::backend
        CONN["Database Connection"]:::backend
        GLOB["Globals"]:::backend
        MID["Middleware"]:::backend
        MAN["Manager Module"]:::backend
        UTIL["Cache Utils"]:::backend
        ADMIN["Admin Dashboard"]:::admin
    end

    %% Adapter Modules
    subgraph "Adapter Modules"
        AD_OpenAI["Adapter: OpenAI"]:::adapter
        AD_AZURE["Adapter: Azure"]:::adapter
        AD_BING["Adapter: Bing"]:::adapter
        AD_CLAUDE["Adapter: Claude"]:::adapter
    end

    %% Additional Modules
    subgraph "Additional Modules"
        ADD["Additional Services"]:::additional
    end

    %% Data Persistence & Caching
    subgraph "Data Persistence & Caching"
        DB["MySQL Database"]:::database
        REDIS["Redis Cache"]:::database
    end

    %% External Services & Integrations
    subgraph "External Services & Integrations"
        EXT_AI["External: AI Services"]:::external
        EXT_CLOUD["External: Cloud Storage (S3/R2/MinIO)"]:::external
        EXT_SEARCH["External: SearXNG Search"]:::external
    end

    %% Connections between layers
    F -->|"HTTP_API"| AG1
    F -->|"HTTP_API"| AG2

    AG1 -->|"ForwardRequest"| B
    AG2 -->|"ForwardRequest"| B

    %% Backend internal module breakdown
    B -->|"loads"| BG
    B -->|"loads"| AUTH
    B -->|"loads"| CH
    B -->|"loads"| CONN
    B -->|"loads"| GLOB
    B -->|"loads"| MID
    B -->|"loads"| MAN
    B -->|"loads"| UTIL
    B -->|"loads"| ADMIN

    %% Backend to Adapter Modules
    B -->|"AdapterInvocation"| AD_OpenAI
    B -->|"AdapterInvocation"| AD_AZURE
    B -->|"AdapterInvocation"| AD_BING
    B -->|"AdapterInvocation"| AD_CLAUDE

    %% Adapter Modules calling external AI services
    AD_OpenAI -->|"ExternalCall"| EXT_AI
    AD_AZURE -->|"ExternalCall"| EXT_AI
    AD_BING -->|"ExternalCall"| EXT_AI
    AD_CLAUDE -->|"ExternalCall"| EXT_AI

    %% Backend to Data Persistence & Caching
    B -->|"DBQuery"| DB
    B -->|"CacheQuery"| REDIS

    %% Backend to Additional Modules
    B -->|"AdditionalOps"| ADD

    %% Additional Modules to External Services
    ADD -->|"FileParsing"| EXT_CLOUD
    ADD -->|"SearchQuery"| EXT_SEARCH

    %% Frontend to Admin Dashboard (Admin Panel Access)
    F -->|"AdminAccess"| ADMIN

    %% Click Events
    click F "https://github.com/coaidev/coai/tree/main/app"
    click BG "https://github.com/coaidev/coai/blob/main/main.go"
    click AUTH "https://github.com/coaidev/coai/tree/main/auth"
    click CH "https://github.com/coaidev/coai/tree/main/channel"
    click CONN "https://github.com/coaidev/coai/tree/main/connection"
    click GLOB "https://github.com/coaidev/coai/tree/main/globals"
    click MID "https://github.com/coaidev/coai/tree/main/middleware"
    click MAN "https://github.com/coaidev/coai/tree/main/manager"
    click UTIL "https://github.com/coaidev/coai/tree/main/utils/cache"
    click ADMIN "https://github.com/coaidev/coai/tree/main/admin"
    click ADMIN "https://github.com/coaidev/coai/tree/main/app/src/admin"
    click AD_OpenAI "https://github.com/coaidev/coai/tree/main/adapter/openai"
    click AD_AZURE "https://github.com/coaidev/coai/tree/main/adapter/azure"
    click AD_BING "https://github.com/coaidev/coai/tree/main/adapter/bing"
    click AD_CLAUDE "https://github.com/coaidev/coai/tree/main/adapter/claude"
    click AG1 "https://github.com/coaidev/coai/tree/main/manager/router"
    click AG2 "https://github.com/coaidev/coai/tree/main/auth/router"
    click ADD "https://github.com/coaidev/coai/tree/main/addition"
    click DB "https://github.com/coaidev/coai/tree/main/connection"
    click REDIS "https://github.com/coaidev/coai/tree/main/utils/cache"

    %% Styles
    classDef frontend fill:#D6E9C6,stroke:#4CAF50,stroke-width:2px;
    classDef backend fill:#CCE5FF,stroke:#337AB7,stroke-width:2px;
    classDef adapter fill:#F2DEDE,stroke:#A94442,stroke-width:2px;
    classDef admin fill:#FCF8E3,stroke:#8A6D3B,stroke-width:2px;
    classDef database fill:#FFF2CC,stroke:#D6B656,stroke-width:2px;
    classDef gateway fill:#E8F4F8,stroke:#89CFF0,stroke-width:2px;
    classDef additional fill:#E2D1F9,stroke:#8E44AD,stroke-width:2px;
    classDef external fill:#F0E68C,stroke:#B8860B,stroke-width:2px;
```
