|
| 1 | +--- |
| 2 | +description: Project directory structure overview |
| 3 | +alwaysApply: false |
| 4 | +--- |
| 5 | + |
| 6 | +# LobeChat Project Structure |
| 7 | + |
| 8 | +## Directory Structure |
| 9 | + |
| 10 | +note: some files are not shown for simplicity. |
| 11 | + |
| 12 | +```plaintext |
| 13 | +lobe-chat/ |
| 14 | +├── apps/ # Applications directory |
| 15 | +│ └── desktop/ # Electron desktop application |
| 16 | +│ ├── src/ # Desktop app source code |
| 17 | +│ └── resources/ # Desktop app resources |
| 18 | +├── docs/ # Project documentation |
| 19 | +│ ├── development/ # Development docs |
| 20 | +│ ├── self-hosting/ # Self-hosting docs |
| 21 | +│ └── usage/ # Usage guides |
| 22 | +├── locales/ # Internationalization files |
| 23 | +│ ├── en-US/ # English |
| 24 | +│ └── zh-CN/ # Simplified Chinese |
| 25 | +├── packages/ # Monorepo packages directory |
| 26 | +│ ├── const/ # Constants definition package |
| 27 | +│ ├── database/ # Database related package |
| 28 | +│ ├── model-runtime/ # AI model runtime package |
| 29 | +│ ├── types/ # TypeScript type definitions |
| 30 | +│ ├── utils/ # Utility functions package |
| 31 | +│ ├── file-loaders/ # File processing packages |
| 32 | +│ ├── prompts/ # AI prompt management |
| 33 | +│ └── web-crawler/ # Web crawling functionality |
| 34 | +├── public/ # Static assets |
| 35 | +│ ├── icons/ # Application icons |
| 36 | +│ ├── images/ # Image resources |
| 37 | +│ └── screenshots/ # Application screenshots |
| 38 | +├── scripts/ # Build and tool scripts |
| 39 | +├── src/ # Main application source code (see below) |
| 40 | +├── tests/ # Test configuration |
| 41 | +├── .cursor/ # Cursor AI configuration |
| 42 | +├── docker-compose/ # Docker configuration |
| 43 | +├── package.json # Project dependencies |
| 44 | +├── pnpm-workspace.yaml # pnpm monorepo configuration |
| 45 | +├── next.config.ts # Next.js configuration |
| 46 | +├── drizzle.config.ts # Drizzle ORM configuration |
| 47 | +└── tsconfig.json # TypeScript configuration |
| 48 | +``` |
| 49 | + |
| 50 | +## Core Source Directory (`src/`) |
| 51 | + |
| 52 | +```plaintext |
| 53 | +src/ |
| 54 | +├── app/ # Next.js App Router routes |
| 55 | +│ ├── (backend)/ # Backend API routes |
| 56 | +│ │ ├── api/ # REST API endpoints |
| 57 | +│ │ │ ├── auth/ # Authentication endpoints |
| 58 | +│ │ │ └── webhooks/ # Webhook handlers for various auth providers |
| 59 | +│ │ ├── middleware/ # Request middleware |
| 60 | +│ │ ├── oidc/ # OpenID Connect endpoints |
| 61 | +│ │ ├── trpc/ # tRPC API routes |
| 62 | +│ │ │ ├── async/ # Async tRPC endpoints |
| 63 | +│ │ │ ├── edge/ # Edge runtime endpoints |
| 64 | +│ │ │ ├── lambda/ # Lambda runtime endpoints |
| 65 | +│ │ │ └── tools/ # Tools-specific endpoints |
| 66 | +│ │ └── webapi/ # Web API endpoints |
| 67 | +│ │ ├── chat/ # Chat-related APIs for various providers |
| 68 | +│ │ ├── models/ # Model management APIs |
| 69 | +│ │ ├── plugin/ # Plugin system APIs |
| 70 | +│ │ ├── stt/ # Speech-to-text APIs |
| 71 | +│ │ ├── text-to-image/ # Image generation APIs |
| 72 | +│ │ └── tts/ # Text-to-speech APIs |
| 73 | +│ ├── [variants]/ # Page route variants |
| 74 | +│ │ ├── (main)/ # Main application routes |
| 75 | +│ │ │ ├── chat/ # Chat interface and workspace |
| 76 | +│ │ │ ├── discover/ # Discover page (assistants, models, providers) |
| 77 | +│ │ │ ├── files/ # File management interface |
| 78 | +│ │ │ ├── image/ # Image generation interface |
| 79 | +│ │ │ ├── profile/ # User profile and stats |
| 80 | +│ │ │ ├── repos/ # Knowledge base repositories |
| 81 | +│ │ │ └── settings/ # Application settings |
| 82 | +│ │ └── @modal/ # Modal routes |
| 83 | +│ └── manifest.ts # PWA configuration |
| 84 | +├── components/ # Global shared components |
| 85 | +│ ├── Analytics/ # Analytics tracking components |
| 86 | +│ ├── Error/ # Error handling components |
| 87 | +│ └── Loading/ # Loading state components |
| 88 | +├── config/ # Application configuration |
| 89 | +│ ├── aiModels/ # AI model configurations |
| 90 | +│ └── modelProviders/ # Model provider configurations |
| 91 | +├── features/ # Feature components (UI Layer) |
| 92 | +│ ├── AgentSetting/ # Agent configuration and management |
| 93 | +│ ├── ChatInput/ # Chat input with file upload and tools |
| 94 | +│ ├── Conversation/ # Message display and interaction |
| 95 | +│ ├── FileManager/ # File upload and knowledge base |
| 96 | +│ └── PluginStore/ # Plugin marketplace and management |
| 97 | +├── hooks/ # Custom React hooks |
| 98 | +├── layout/ # Global layout components |
| 99 | +│ ├── AuthProvider/ # Authentication provider |
| 100 | +│ └── GlobalProvider/ # Global state provider |
| 101 | +├── libs/ # External library integrations |
| 102 | +│ ├── analytics/ # Analytics services integration |
| 103 | +│ ├── next-auth/ # NextAuth.js configuration |
| 104 | +│ └── oidc-provider/ # OIDC provider implementation |
| 105 | +├── locales/ # Internationalization resources |
| 106 | +│ └── default/ # Default language definitions |
| 107 | +├── migrations/ # Client-side data migrations |
| 108 | +├── server/ # Server-side code |
| 109 | +│ ├── modules/ # Server modules |
| 110 | +│ ├── routers/ # tRPC routers |
| 111 | +│ └── services/ # Server services |
| 112 | +├── services/ # Client service layer |
| 113 | +│ ├── aiModel/ # AI model services |
| 114 | +│ ├── session/ # Session services |
| 115 | +│ └── message/ # Message services |
| 116 | +├── store/ # Zustand state management |
| 117 | +│ ├── agent/ # Agent state |
| 118 | +│ ├── chat/ # Chat state |
| 119 | +│ └── user/ # User state |
| 120 | +├── styles/ # Global styles |
| 121 | +├── tools/ # Built-in tool system |
| 122 | +│ ├── artifacts/ # Code artifacts and preview |
| 123 | +│ └── web-browsing/ # Web search and browsing |
| 124 | +├── types/ # TypeScript type definitions |
| 125 | +└── utils/ # Utility functions |
| 126 | + ├── client/ # Client-side utilities |
| 127 | + └── server/ # Server-side utilities |
| 128 | +``` |
| 129 | + |
| 130 | +## Key Monorepo Packages |
| 131 | + |
| 132 | +```plaintext |
| 133 | +packages/ |
| 134 | +├── const/ # Global constants and configurations |
| 135 | +├── database/ # Database schemas and models |
| 136 | +│ ├── src/models/ # Data models (CRUD operations) |
| 137 | +│ ├── src/schemas/ # Drizzle database schemas |
| 138 | +│ ├── src/repositories/ # Complex query layer |
| 139 | +│ └── migrations/ # Database migration files |
| 140 | +├── model-runtime/ # AI model runtime |
| 141 | +│ └── src/ |
| 142 | +│ ├── openai/ # OpenAI provider integration |
| 143 | +│ ├── anthropic/ # Anthropic provider integration |
| 144 | +│ ├── google/ # Google AI provider integration |
| 145 | +│ ├── ollama/ # Ollama local model integration |
| 146 | +│ ├── types/ # Runtime type definitions |
| 147 | +│ └── utils/ # Runtime utilities |
| 148 | +├── types/ # Shared TypeScript type definitions |
| 149 | +│ └── src/ |
| 150 | +│ ├── agent/ # Agent-related types |
| 151 | +│ ├── message/ # Message and chat types |
| 152 | +│ ├── user/ # User and session types |
| 153 | +│ └── tool/ # Tool and plugin types |
| 154 | +├── utils/ # Shared utility functions |
| 155 | +│ └── src/ |
| 156 | +│ ├── client/ # Client-side utilities |
| 157 | +│ ├── server/ # Server-side utilities |
| 158 | +│ ├── fetch/ # HTTP request utilities |
| 159 | +│ └── tokenizer/ # Token counting utilities |
| 160 | +├── file-loaders/ # File loaders (PDF, DOCX, etc.) |
| 161 | +├── prompts/ # AI prompt management |
| 162 | +└── web-crawler/ # Web crawling functionality |
| 163 | +``` |
| 164 | + |
| 165 | +## Architecture Layers |
| 166 | + |
| 167 | +### 1. **Presentation Layer** |
| 168 | + |
| 169 | +- Business-specific feature components and reusable UI components |
| 170 | +- Global layout providers and responsive design wrappers |
| 171 | + |
| 172 | +### 2. **State Management Layer** |
| 173 | + |
| 174 | +- Zustand-based client state with domain-specific slices |
| 175 | +- Actions and selectors for predictable state updates |
| 176 | + |
| 177 | +### 3. **Client Service Layer** |
| 178 | + |
| 179 | +- Environment-adaptive services (local Model vs remote tRPC) |
| 180 | +- Dual implementation pattern for multi-runtime compatibility |
| 181 | + |
| 182 | +### 4. **API Interface Layer** |
| 183 | + |
| 184 | +- Type-safe tRPC routers organized by runtime environment |
| 185 | +- Request routing and validation |
| 186 | + |
| 187 | +### 5. **Server Service Layer** |
| 188 | + |
| 189 | +- Platform-agnostic business logic with implementation abstractions |
| 190 | +- Reusable, testable service composition |
| 191 | + |
| 192 | +### 6. **Data Access Layer** |
| 193 | + |
| 194 | +- **Repository**: Complex queries, joins, and transaction management |
| 195 | +- **Model**: Basic CRUD operations and single-table queries |
| 196 | +- **Schema**: Drizzle ORM definitions and migration management |
| 197 | + |
| 198 | +### 7. **Integration & Extensions** |
| 199 | + |
| 200 | +- **External**: Third-party service integrations and library wrappers |
| 201 | +- **Built-in**: AI runtime, tool system, file processing, and web crawling |
| 202 | + |
| 203 | +## Data Flow Architecture |
| 204 | + |
| 205 | +### Unified Flow Pattern |
| 206 | + |
| 207 | +``` |
| 208 | +UI Layer → State Management → Client Service → [Environment Branch] → Database |
| 209 | + ↓ ↓ ↓ ↓ ↓ |
| 210 | + React Zustand Environment Local/Remote PGLite/ |
| 211 | +Components Store Adaptation Routing PostgreSQL |
| 212 | +``` |
| 213 | + |
| 214 | +### Environment-Specific Routing |
| 215 | + |
| 216 | +| Mode | UI | Service Route | Database | |
| 217 | +| --------------- | -------- | ---------------------- | ------------------- | |
| 218 | +| **Browser/PWA** | React | Direct Model Access | PGLite (Local) | |
| 219 | +| **Server** | React | tRPC → Server Services | PostgreSQL (Remote) | |
| 220 | +| **Desktop** | Electron | tRPC → Local Node.js | PGLite/PostgreSQL\* | |
| 221 | + |
| 222 | +_\*Depends on cloud sync configuration_ |
| 223 | + |
| 224 | +### Key Characteristics |
| 225 | + |
| 226 | +- **Type Safety**: End-to-end type safety via tRPC and Drizzle ORM |
| 227 | +- **Local/Remote Dual Mode**: PGLite enables user data ownership and local control |
0 commit comments