# zhangheng2022/onechatgpt architecture

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

- Interactive diagram: https://gitdiagram.com/zhangheng2022/onechatgpt
- Repository: https://github.com/zhangheng2022/onechatgpt

## Overview

##### Project Overview and Purpose
- OneChatGPT is an open-source multi-model AI chat platform built with Flutter. Its primary purpose is to offer a unified chat experience that integrates several large language models (OpenAI ChatGPT, Google Gemini, 百度文心一言, and 智谱 ChatGLM). Although mostly focused on Android as noted in the README, the project is set up to compile on multiple platforms (Android, iOS, macOS, Windows, Linux, and web) through Flutter’s cross-platform capabilities.

##### Analyzing the File Structure
- Top-Level Directories for Platforms: Notice separate directories for android, ios, macos, linux, windows, and web. This division indicates that platform-specific configurations, builds, and resources are kept isolated from the core logic.
- Core Application Code: The lib directory is the heart of the Flutter application. It is further segregated into modules like config, database, models, state, ui, services, and utilities. This modular organization suggests a clean separation between configuration, data handling, UI presentation, and business logic.
- Additional Directories: The presence of .github, pubspec.yaml, and various configuration files tells us that CI/CD pipelines, dependencies, and build instructions are managed systematically.

##### Key Architectural Components
a. UI Layer:
- Located primarily under the lib/ui directory.
- Contains sub-modules for authentication (login, register, forget_password), chat interface (chat_home, chat widgets), FAQ, indexing (home, person, navigation scaffold), and user info.
- This layer is responsible for rendering the responsive interface and managing user interactions.

b. Business Logic and State Management:
- The lib/state directory contains state handling for authentication, model settings, and theme management.
- Linking to UI via state management techniques (Provider, Bloc or similar) to maintain data consistency.

c. Data Layer:
- The lib/database directory holds definitions for chat records and related details, indicating local persistence (likely using SQLite or Drift-generated code).
- The lib/models folder houses data models for network communication (chat models, request and response schemas) as well as app settings.

d. Service Layer:
- Found in lib/services and includes utilities such as the dio_singleton (which likely handles API requests using the Dio HTTP client).
- This layer bridges the gap between the UI and external services/APIs (for connecting to various AI models).

e. Routing and Configuration:

f. External Services:
- Since the project integrates multiple AI models, include a component (or external APIs node) showing connections to external services like OpenAI, Google Gemini, etc.

g. Platform-Specific Modules:
- Although the business logic resides in the lib folder, native code for each platform (Android, iOS, etc.) shows integration layers. These modules handle platform-specific initialization, configuration, and resource management.

##### Final Touches
- That every component (including smaller widgets or utilities if they play a significant role) is clearly labeled.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% UI Layer
    subgraph "UI Layer"
        UI1["Authentication"]:::ui
        UI2["Chat Interface"]:::ui
        UI3["FAQ"]:::ui
        UI4["Navigation/Indexing"]:::ui
        UI5["User Info"]:::ui
    end

    %% Business Logic & State Management
    subgraph "Business Logic/State Management"
        BL["Business Logic & State Management"]:::logic
    end

    %% Data Layer
    subgraph "Data Layer"
        DL1["Local Persistence"]:::data
        DL2["Data Models"]:::data
    end

    %% Service Layer
    subgraph "Service Layer"
        SL["API Service"]:::service
    end

    %% Routing & Configuration
    subgraph "Routing & Configuration"
        RC["Routing & Configuration"]:::config
    end

    %% External APIs
    subgraph "External APIs"
        EX1["OpenAI ChatGPT"]:::external
        EX2["Google Gemini"]:::external
        EX3["百度文心一言"]:::external
        EX4["智谱 ChatGLM"]:::external
    end

    %% Platform-Specific Modules
    subgraph "Platform Modules"
        PM1["Android"]:::platform
        PM2["iOS"]:::platform
        PM3["macOS"]:::platform
        PM4["Linux"]:::platform
        PM5["Windows"]:::platform
        PM6["Web"]:::platform
    end

    %% Connections from UI Layer to Business Logic
    UI1 -->|"triggers"| BL
    UI2 -->|"triggers"| BL
    UI3 -->|"triggers"| BL
    UI4 -->|"triggers"| BL
    UI5 -->|"triggers"| BL

    %% Navigation uses Routing & Config
    UI4 -->|"navigates via"| RC
    RC -->|"routes to"| BL

    %% Business Logic interacts with Data Layer and Service Layer
    BL -->|"reads/writes"| DL1
    BL -->|"reads/writes"| DL2
    BL -->|"calls"| SL

    %% Service Layer calls External APIs
    SL -->|"calls"| EX1
    SL -->|"calls"| EX2
    SL -->|"calls"| EX3
    SL -->|"calls"| EX4

    %% Routing & Configuration integration with Platform Modules
    RC -->|"bridges to"| PM1
    RC -->|"bridges to"| PM2
    RC -->|"bridges to"| PM3
    RC -->|"bridges to"| PM4
    RC -->|"bridges to"| PM5
    RC -->|"bridges to"| PM6

    %% Click Events
    click UI1 "https://github.com/zhangheng2022/onechatgpt/tree/develop/lib/ui/auth"
    click UI2 "https://github.com/zhangheng2022/onechatgpt/tree/develop/lib/ui/chat"
    click UI3 "https://github.com/zhangheng2022/onechatgpt/blob/develop/lib/ui/faq/faq.dart"
    click UI4 "https://github.com/zhangheng2022/onechatgpt/tree/develop/lib/ui/index"
    click UI5 "https://github.com/zhangheng2022/onechatgpt/tree/develop/lib/ui/userinfo"
    click BL "https://github.com/zhangheng2022/onechatgpt/tree/develop/lib/state"
    click DL1 "https://github.com/zhangheng2022/onechatgpt/tree/develop/lib/database"
    click DL2 "https://github.com/zhangheng2022/onechatgpt/tree/develop/lib/models"
    click SL "https://github.com/zhangheng2022/onechatgpt/blob/develop/lib/services/dio_singleton.dart"
    click RC "https://github.com/zhangheng2022/onechatgpt/blob/develop/lib/router.dart"
    click PM1 "https://github.com/zhangheng2022/onechatgpt/tree/develop/android"
    click PM2 "https://github.com/zhangheng2022/onechatgpt/tree/develop/ios"
    click PM3 "https://github.com/zhangheng2022/onechatgpt/tree/develop/macos"
    click PM4 "https://github.com/zhangheng2022/onechatgpt/tree/develop/linux"
    click PM5 "https://github.com/zhangheng2022/onechatgpt/tree/develop/windows"
    click PM6 "https://github.com/zhangheng2022/onechatgpt/tree/develop/web"

    %% Styles
    classDef ui fill:#ffe6f0,stroke:#cc3366,stroke-width:2px;
    classDef logic fill:#e6f0ff,stroke:#3366cc,stroke-width:2px;
    classDef data fill:#e6ffe6,stroke:#339933,stroke-width:2px;
    classDef service fill:#fff0cc,stroke:#cc9933,stroke-width:2px;
    classDef config fill:#ccffff,stroke:#33cccc,stroke-width:2px;
    classDef external fill:#fff2cc,stroke:#cc6633,stroke-width:2px;
    classDef platform fill:#f2f2f2,stroke:#999999,stroke-width:2px;
```
