π€― LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
---
title: Directory Structure
description: >-
Explore the organized directory structure of LobeHub, including app,
components, and services.
tags:
- LobeHub
- Directory Structure
- Next.js
- App Router
- API Architecture
---
# Directory Structure
LobeHub uses a Monorepo architecture (`@lobechat/` namespace).
The top-level directory structure is as follows:
```bash
lobehub/
βββ apps/
β βββ cli/ # LobeHub CLI
β βββ desktop/ # Electron desktop app
β βββ server/ # Standalone server (tRPC routers, services, modules)
βββ packages/ # Shared packages (@lobechat/*)
β βββ agent-runtime/ # Agent runtime
β βββ database/ # Database schemas, models, repositories
β βββ model-runtime/ # Model runtime (AI provider adapters)
β βββ builtin-tool-*/ # Built-in tool packages
β βββ builtin-tools/ # Built-in tools registry (inspectors, interventions, renders, etc.)
β βββ business/ # Cloud business slot packages
β βββ context-engine/ # Context engine
β βββ conversation-flow/ # Conversation flow
β βββ editor-runtime/ # Editor runtime
β βββ file-loaders/ # File loaders
β βββ prompts/ # Prompt templates
β βββ app-config/ # App configuration (client and server env vars)
β βββ env/ # Environment variable definitions and validation
β βββ locales/ # i18n default language files (English) and resources
β βββ ... # More shared packages
βββ src/ # Main app source code (see below)
βββ locales/ # i18n translation files (zh-CN, en-US, etc.)
βββ e2e/ # E2E tests (Cucumber + Playwright)
βββ docs/ # Documentation
```
## src Directory
`config/`, `envs/`, `locales/`, and `tools/` have moved out of `src/` into standalone packages β
`packages/app-config`, `packages/env`, `packages/locales`, and `packages/builtin-tools`.
The `@/config/*`, `@/envs/*`, and `@/locales/*` path aliases resolve to those packages first
(see `tsconfig.json`), so most imports don't need to change.
```bash
src/
βββ app/ # Next.js App Router: backend API routes + SPA/auth HTML shell serving
β βββ (backend)/ # Backend API routes (auth, webhooks, trpc, webapi, oidc, oauth)
β βββ spa/ # SPA HTML template route (serves the Vite-built SPA bundle)
β βββ spa-auth/ # Auth pages HTML template route
βββ business/ # Cloud-only business logic (client/server)
βββ components/ # Reusable UI components
βββ const/ # Application constants and enums
βββ features/ # Business feature modules (Agent settings, plugin dev, etc.)
βββ helpers/ # Utility helper functions
βββ hooks/ # Reusable custom Hooks
βββ layout/ # Global layout components (AuthProvider, GlobalProvider)
βββ libs/ # Third-party integrations (better-auth, OIDC, tRPC, MCP, etc.)
βββ routes/ # SPA page segments (layout + page files), grouped by platform
β βββ (main)/ # Desktop routes
β βββ (mobile)/ # Mobile routes
β βββ (desktop)/ # Desktop-only routes (e.g. desktop onboarding)
β βββ (popup)/ # Popup window routes
β βββ auth/ # Auth pages (signin, signup, oauth, ...)
β βββ onboarding/ # Onboarding flow
β βββ share/ # Share pages
βββ server/ # Remaining server-side modules not yet moved to apps/server
β # (SEO metadata, SPA HTML rendering, composio services)
βββ services/ # Client-side service interfaces
βββ spa/ # SPA entry points and React Router config
β βββ entry.web.tsx / entry.mobile.tsx / entry.desktop.tsx / entry.popup.tsx / entry.auth.tsx
β βββ router/ # Router configs (desktopRouter.config.tsx, mobileRouter.config.tsx,
β # popupRouter.config.tsx, authRouter.config.tsx)
βββ store/ # Zustand state management
βββ styles/ # Global styles and CSS-in-JS configurations
βββ types/ # TypeScript type definitions
βββ utils/ # General utility functions
βββ auth.ts # Authentication configuration (Better Auth)
βββ instrumentation.ts # App monitoring and telemetry setup
βββ proxy.ts # Next.js middleware proxy configuration
```
## app, routes, and spa Directories
Page routing is now split across three directories:
- **`src/app/`** β Next.js App Router. Only handles backend API routes (`(backend)/`) and
the two Next.js routes that serve the SPA's HTML shell: `spa/[variants]/[[...path]]/route.ts`
(main app) and `spa-auth/[locale]/[[...path]]/route.ts` (auth pages).
- **`src/routes/`** β SPA page segments, grouped by platform route group
(`(main)`, `(mobile)`, `(desktop)`, `(popup)`), plus `auth/`, `onboarding/`, and `share/`.
These are thin files that delegate to `src/features/*` for actual UI and business logic.
- **`src/spa/`** β SPA entry points (`entry.web.tsx`, `entry.mobile.tsx`, `entry.desktop.tsx`,
`entry.popup.tsx`, `entry.auth.tsx`) and React Router config under `router/`.
```bash
app/
βββ (backend)/ # Backend API routes and services
β βββ api/ # REST API endpoints (auth, webhooks)
β βββ f/ # File service
β βββ market/ # Market service
β βββ middleware/ # Request middleware
β βββ oauth/ # OAuth routes
β βββ oidc/ # OpenID Connect routes
β βββ trpc/ # tRPC API endpoints
β β βββ async/ # Async tRPC routes
β β βββ desktop/ # Desktop tRPC routes
β β βββ lambda/ # Lambda tRPC routes
β β βββ tools/ # Tools tRPC routes
β βββ webapi/ # Web API endpoints (chat, models, tts, etc.)
βββ spa/[variants]/[[...path]]/route.ts # Serves the main SPA HTML shell
βββ spa-auth/[locale]/[[...path]]/route.ts # Serves the auth pages HTML shell
βββ [variants]/metadata.ts # Shared SEO metadata for variant routes
βββ manifest.ts # PWA manifest
βββ robots.tsx # Robots.txt generation
βββ sitemap.tsx # Sitemap generation
```
```bash
src/routes/
βββ (main)/ # Desktop routes: agent, group, home, resource, settings, memory, ...
βββ (mobile)/ # Mobile routes: (home), chat, community, me, settings
βββ (desktop)/ # Desktop-only routes (e.g. desktop-onboarding)
βββ (popup)/ # Popup window routes: agent, group
βββ auth/ # Auth pages: signin, signup, oauth, reset-password, verify-email, ...
βββ onboarding/ # Onboarding flow
βββ share/ # Share pages: t/[id], page/[id]
```
```bash
src/spa/
βββ entry.web.tsx # Desktop web entry
βββ entry.mobile.tsx # Mobile entry
βββ entry.desktop.tsx # Electron desktop entry
βββ entry.popup.tsx # Popup window entry
βββ entry.auth.tsx # Auth pages entry
βββ router/
βββ desktopRouter.config.tsx # Desktop React Router routes
βββ desktopRouter.config.desktop.tsx # Desktop (Electron) variant, kept in sync
βββ mobileRouter.config.tsx # Mobile React Router routes
βββ popupRouter.config.tsx # Popup window routes
βββ authRouter.config.tsx # Auth pages routes
```
### Architecture Explanation
**Route Groups:**
- `(backend)` β All server-side API routes, middleware, and backend services
- `(main)` / `(mobile)` / `(desktop)` / `(popup)` β Platform-specific SPA route groups under `src/routes/`
**Platform Organization:**
- Supports multiple platforms (web, desktop, mobile) through route organization
- Desktop-specific routes under `(desktop)/`
- Mobile-specific routes under `(mobile)/`
- Shared layout components in `_layout/` directories
**API Architecture:**
- REST APIs: `(backend)/api/` and `(backend)/webapi/`
- tRPC endpoints (`apps/server/src/routers/`): grouped by runtime
- `lambda/` β Main business routers (agent, session, message,
topic, file, knowledge, settings, etc.)
- `async/` β Long-running async operations
(file processing, image generation, RAG evaluation)
- `tools/` β Tool invocations (search, MCP, market, composio)
- `mobile/` β Mobile-specific routers
**Data Flow:**
Using a typical user action (e.g., updating Agent config) as an example, here's how data flows through each layer:
```plaintext
React UI (src/features/, src/routes/)
β User interaction triggers event
βΌ
Store Actions (src/store/)
β Zustand action updates local state, calls service
βΌ
Client Service (src/services/)
β Wraps tRPC client call, prepares request params
βΌ
tRPC Router (apps/server/src/routers/lambda/)
β Validates input (zod), routes to service
βΌ
Server Service (apps/server/src/services/)
β Executes business logic, calls DB model
βΌ
DB Model (packages/database/src/models/)
β Wraps Drizzle ORM queries
βΌ
PostgreSQL
```
For data reads, the flow is reversed: UI consumes data via store selectors, and stores fetch from the backend via SWR + tRPC queries.
### Routing Architecture
The project uses hybrid routing:
Next.js App Router serves the SPA's HTML shell and static/auth pages,
while React Router DOM powers the main SPA once the bundle loads in the browser.
**Entry**: The Next.js route `src/app/spa/[variants]/[[...path]]/route.ts`
renders the HTML shell (desktop or mobile template based on device type),
which then boots the matching Vite entry in `src/spa/` (`entry.web.tsx`, `entry.mobile.tsx`,
or `entry.desktop.tsx`) to mount the React Router app.
**Key configuration files:**
- Desktop router: `src/spa/router/desktopRouter.config.tsx` (kept in sync with `desktopRouter.config.desktop.tsx`)
- Mobile router: `src/spa/router/mobileRouter.config.tsx`
- Popup router: `src/spa/router/popupRouter.config.tsx`
- Auth router: `src/spa/router/authRouter.config.tsx`
- Router utilities: `src/utils/router.tsx`
**Desktop SPA Routes (React Router DOM):**
```bash
/ # Home
/agent/:aid # Agent conversation
/agent/:aid/profile # Agent profile
/agent/:aid/cron/:cronId # Cron task detail
/group/:gid # Group conversation
/group/:gid/profile # Group profile
/community # Community discovery (agent, model, provider, mcp)
/community/agent/:slug # Agent detail page
/community/model/:slug # Model detail page
/community/provider/:slug # Provider detail page
/community/mcp/:slug # MCP detail page
/resource # Resource management
/resource/library/:id # Knowledge base detail
/settings/:tab # Settings (profile, provider, etc.)
/settings/provider/:id # Model provider configuration
/memory # Memory management
/image # Image generation
/page/:id # Page detail
/share/t/:id # Share topic
/onboarding # Onboarding flow
```
**Mobile SPA Routes (React Router DOM):**
```bash
/ # Home
/agent/:aid # Agent conversation
/community # Community discovery
/settings # Settings home
/settings/:tab # Settings detail
/settings/provider/:id # Model provider configuration
/me # Personal center
/me/profile # Profile
/me/settings # Personal settings
/share/t/:id # Share topic
/onboarding # Onboarding flow
```