Skip to content

🀯 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.

12.1 KB
Raw Blame
---
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
```