Overview
Relevant source files
NextChat is a cross-platform AI chat client built on Next.js 14 that provides a unified interface for interacting with multiple Large Language Model (LLM) providers. The system implements a privacy-first architecture where chat data is stored locally (browser IndexedDB and localStorage) with optional cloud synchronization via WebDAV or UpStash Redis.
The application supports multiple deployment targets: web (Vercel/Zeabur), containerized (Docker), and native desktop applications via Tauri for Windows, macOS, and Linux.
System Purpose and Design Philosophy
NextChat serves as a universal client for LLM services with the following design principles:
- Privacy-First Architecture: All user data (chat sessions, configuration, masks, prompts) is stored locally. The application never sends user data to NextChat servers. README.md85
- Provider Abstraction: The
ClientApifactory pattern in app/client/api.ts136-183 provides a unifiedLLMApiinterface that abstracts provider-specific implementations. This enables seamless switching between OpenAI, Anthropic, Google, and 10+ other providers. - Flexible Deployment: The system supports multiple build modes (
standalone,export, desktop app) configured via theBUILD_MODEandBUILD_APPenvironment variables. package.json9-15 - Extensibility: Support for Model Context Protocol (MCP) enables external tool calling and plugin integration. README.md54-58
System Architecture: Deployment to Runtime
The following diagram illustrates the flow from deployment targets through the application layers to external services.
Sources: app/layout.tsx30-72 app/page.tsx7-18 app/constant.ts44-57 app/client/api.ts136-183 app/api/auth.ts27-129
Core Components and Data Flow
NextChat's logic is partitioned between a persistent state layer and a provider-agnostic API layer.
Chat Lifecycle: User Input to LLM Response
The application handles the assembly of conversation context, including system prompts and memory, before dispatching to the selected provider.
Sources: app/client/api.ts136-183 app/api/auth.ts27-129 app/config/server.ts132-183
Storage Architecture
Sources: app/constant.ts90-101 app/store/access.ts156-164
Deployment Methods
NextChat supports various deployment strategies, primarily managed through environment variables and build-time flags.
| Method | Build Target | Key Command |
|---|---|---|
| Vercel | Standalone | next build package.json9 |
| Static | Export | BUILD_MODE=export next build package.json12 |
| Desktop | Tauri App | yarn tauri build package.json15 |
Configuration Exposure
Configuration is managed across two layers:
- Server-side: app/config/server.ts -
getServerSideConfig()reads sensitive environment variables (e.g.,OPENAI_API_KEY,CODE). app/config/server.ts132-183 - Client-side: app/config/client.ts -
getClientConfig()provides safe runtime settings. - Authentication: The
authfunction in app/api/auth.ts validates user access codes against the hashedCODEenvironment variable and injects system API keys if necessary. app/api/auth.ts27-129
LLM Provider Integration
NextChat supports 13+ LLM providers. Each implementation follows the LLMApi abstract class.
Sources: app/constant.ts120-137 app/client/api.ts136-183
Provider Request Routing
Requests from the client are routed to provider-specific endpoints defined in ApiPath.
- OpenAI:
/api/openaiapp/constant.ts62 - Anthropic:
/api/anthropicapp/constant.ts63 - Google:
/api/googleapp/constant.ts64
These routes are handled by the dynamic proxy at app/api/[provider]/[...path]/route.ts, which applies server-side authentication and headers. app/api/auth.ts107-121
