# lobehub/lobe-chat architecture

> Architecture diagram of the GitHub repository lobehub/lobe-chat, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/lobehub/lobe-chat
- Repository: https://github.com/lobehub/lobe-chat

## Overview

##### Main Components

a) Frontend Layer:
- Next.js Application (src/app)
- Authentication Routes (/auth)
- Main Application Routes (/chat, /settings, etc.)
- Modal System (@modal)
- Mobile & Desktop Layouts
- Internationalization System (locales/)

b) Core Services Layer:
- Agent Runtime System (src/libs/agent-runtime)
- Plugin System (src/libs/plugin)
- Database Layer
- Client Database (IndexedDB/LocalStorage)
- Server Database (PostgreSQL)
- Authentication Services
- Next-Auth
- Clerk Integration

c) Backend Layer:
- API Routes (src/app/api)
- TRPC Endpoints (src/app/trpc)
- WebAPI Services (src/app/webapi)

d) External Services Integration:
- Multiple LLM Providers (OpenAI, Anthropic, etc.)
- Storage Services (S3)
- Authentication Providers
- Plugin Gateway

##### Data Flow Relationships

- User Interface → Core Services → Backend Services → External APIs
- Plugin System ↔ Core Services ↔ LLM Providers
- Authentication Flow → User Sessions → Protected Resources
- File Upload → Storage Services → Knowledge Base

##### Key Architectural Patterns

- Feature-based Module Organization
- Service Layer Pattern
- Repository Pattern for Data Access
- Plugin Architecture
- Event-driven State Management

a) Visual Elements:
- Clear boundaries between systems
- Database symbols

b) Labels and Annotations:
- Component names
- API endpoints
- Data flow descriptions
- Integration points
- Authentication flows

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Legend
        UI[User Interface]:::frontend
        CS[Core Service]:::core
        EX[External Service]:::external
        IN[Infrastructure]:::infra
        SEC[Security Component]:::security
    end

    subgraph "Frontend Layer"
        MainRoutes["Main App Routes"]:::frontend
        AuthRoutes["Auth Routes"]:::frontend
        ModalSystem["Modal System"]:::frontend
        I18N["Internationalization"]:::frontend
        
        Features["Features"]:::frontend
        Chat["Chat Implementation"]:::frontend
        PluginMgmt["Plugin Management"]:::frontend
        FileMgmt["File Management"]:::frontend
        Settings["Settings Management"]:::frontend
    end

    subgraph "Core Services Layer"
        AgentRuntime["Agent Runtime"]:::core
        PluginSystem["Plugin System"]:::core
        
        subgraph "State Management"
            GlobalState["Global State"]:::core
            ChatState["Chat State"]:::core
            UserState["User State"]:::core
        end
        
        subgraph "Database Layer"
            ClientDB[(Client Database)]:::infra
            ServerDB[(Server Database)]:::infra
        end
        
        subgraph "Authentication"
            NextAuth["Next-Auth"]:::security
            ClerkAuth["Clerk Integration"]:::security
            AuthProvider["Auth Provider"]:::security
        end
    end

    subgraph "Backend Layer"
        APIRoutes["API Routes"]:::core
        TRPCEndpoints["TRPC Endpoints"]:::core
        WebAPIServices["WebAPI Services"]:::core
    end

    subgraph "External Services"
        LLMProviders["LLM Providers"]:::external
        Storage["Storage Services"]:::external
        AuthProviders["Auth Providers"]:::external
        PluginGateway["Plugin Gateway"]:::external
    end

    MainRoutes --> Features
    Features --> AgentRuntime
    PluginSystem --> LLMProviders
    AgentRuntime --> APIRoutes
    APIRoutes --> LLMProviders
    AuthRoutes --> AuthProvider
    AuthProvider --> AuthProviders
    FileMgmt --> Storage
    PluginMgmt --> PluginGateway
    
    %% Component Mappings
    click MainRoutes "https://github.com/lobehub/lobe-chat/tree/main/src/app/(main)"
    click AuthRoutes "https://github.com/lobehub/lobe-chat/tree/main/src/app/(auth)"
    click ModalSystem "https://github.com/lobehub/lobe-chat/tree/main/src/app/@modal"
    click I18N "https://github.com/lobehub/lobe-chat/tree/main//locales"
    click AgentRuntime "https://github.com/lobehub/lobe-chat/tree/main/src/libs/agent-runtime"
    click PluginSystem "https://github.com/lobehub/lobe-chat/tree/main/src/libs/plugin"
    click ClientDB "https://github.com/lobehub/lobe-chat/tree/main/src/database/client"
    click ServerDB "https://github.com/lobehub/lobe-chat/tree/main/src/database/server"
    click NextAuth "https://github.com/lobehub/lobe-chat/tree/main/src/libs/next-auth"
    click AuthProvider "https://github.com/lobehub/lobe-chat/tree/main/src/layout/AuthProvider"
    click APIRoutes "https://github.com/lobehub/lobe-chat/tree/main/src/app/(backend)/api"
    click TRPCEndpoints "https://github.com/lobehub/lobe-chat/tree/main/src/app/(backend)/trpc"
    click WebAPIServices "https://github.com/lobehub/lobe-chat/tree/main/src/app/(backend)/webapi"
    click Chat "https://github.com/lobehub/lobe-chat/tree/main/src/features/Conversation"
    click PluginMgmt "https://github.com/lobehub/lobe-chat/tree/main/src/features/PluginStore"
    click FileMgmt "https://github.com/lobehub/lobe-chat/tree/main/src/features/FileManager"
    click Settings "https://github.com/lobehub/lobe-chat/tree/main/src/features/Setting"
    click GlobalState "https://github.com/lobehub/lobe-chat/tree/main/src/store/global"
    click ChatState "https://github.com/lobehub/lobe-chat/tree/main/src/store/chat"
    click UserState "https://github.com/lobehub/lobe-chat/tree/main/src/store/user"

    classDef frontend fill:#2196F3,stroke:#1565C0,color:white
    classDef core fill:#4CAF50,stroke:#2E7D32,color:white
    classDef external fill:#FF9800,stroke:#EF6C00,color:white
    classDef infra fill:#9E9E9E,stroke:#616161,color:white
    classDef security fill:#F44336,stroke:#C62828,color:white
```
