How anuj2731997/ai-chat works
1. Overall structure
This is a Next.js App Router application organized as a full-stack AI chat product. The repo is split into route-level app code, feature modules, shared UI primitives, and server-side infrastructure for auth, AI access, and persistence.
2. Route and layout boundaries
src/app is the top-level composition layer. It separates unauthenticated screens under (auth) from the main application shell under (root). The root area includes the landing/chat entry point and the dynamic chat page at /chat/[chatId], while src/app/layout.js and route-group layouts provide the app-wide and shell-specific wrappers.
3. Authentication subsystem
Auth is handled by Better Auth, exposed through the catch-all API route src/app/api/auth/[...all]/route.js. Supporting code lives in src/lib/auth.js and src/lib/auth-client.js, which likely split server and client auth setup. The src/modules/authentication feature contains auth-specific actions and UI, including the user button component.
4. Chat product surface
The main product surface is the chat workspace in src/modules/chat. It includes the sidebar, header, welcome tabs, message form, message view, model selector, and delete modal. This feature module is the primary orchestration layer for the interactive chat experience and keeps route pages relatively thin.
5. AI request and model flow
AI integration centers on src/app/api/chat/route.js and the src/modules/ai-agent hook. The app uses @ai-sdk/react with the OpenRouter provider, so model calls are routed through a provider abstraction rather than directly to a single model vendor. The src/app/api/ai/get-models/route.js endpoint feeds the model selector with available models.
6. Message rendering and rich assistant output
The src/components/ai-elements directory is a specialized rendering layer for assistant content. It includes components for code blocks, citations, reasoning, tool calls, images, conversation nodes, and web previews. This suggests the app can render structured AI output, not just plain text.
7. Persistence layer
Prisma is the persistence boundary. prisma/schema.prisma and the migration history indicate the database schema covers auth-related data and chat-related data. The app uses PostgreSQL as the backing store, with src/lib/db.js likely providing the Prisma client to server-side code.
8. State management and data fetching
The repo uses React Query and Zustand together. src/components/providers/query-provider.jsx wraps server-state caching/fetching, while src/modules/chat/store/chat-store.js handles local UI/session state for the chat experience. This split suggests remote chat data and local interaction state are intentionally separated.
9. Shared UI and app shell
src/components/ui is a broad design-system layer built around Radix/shadcn-style primitives. src/components/providers/theme-provider.jsx and src/components/ui/mode-toggle.jsx indicate app-wide theming support. These components are shared across features rather than tied to chat or auth.
10. Cross-cutting libraries
src/lib/prompt.js centralizes prompt construction, and src/lib/utils.js holds shared helpers. src/hooks/use-mobile.js and src/components/providers support responsive behavior and app context. These are small but important glue layers that keep feature code focused.
11. Runtime and infrastructure
PostgreSQL is containerized via docker-compose.yml, with local setup oriented around port 6000. The app is configured for Next.js production deployment, and the README’s environment variables show three external dependencies that materially affect architecture: Better Auth secrets, GitHub OAuth credentials, and an OpenRouter API key.
12. How a request typically flows
A user authenticates through the auth route, enters the root app shell, selects or opens a chat, and sends a message through the chat UI. The chat feature then uses its action layer and AI hook/API route to contact the model provider, streams or receives the response, renders it through the AI element components, and persists relevant chat data through Prisma/PostgreSQL.
Main entry points: chat Chat module · chat-message-view.jsx Message UI · layout.jsx Root shell · route.js Chat API
Components 20
App routes
Next.js App Router composition, route handlers, and page shells.
- App layout · Global application wrapper and top-level styling shell.
src/app/layout.js - Auth shell · Unauthenticated route group wrapper for sign-in flows.
src/app/(auth)/layout.jsx - Root shell · Main application shell for authenticated pages.
src/app/(root)/layout.jsx - Home page · Landing or chat entry page at the app root.
src/app/(root)/page.jsx - Chat page · Dynamic chat route for an individual conversation.
src/app/(root)/chat/[chatId]/page.jsx - Auth API · Catch-all Better Auth endpoint for sign-in and session flows.
src/app/api/auth/[...all]/route.js - Models API · Endpoint that supplies available models to the UI.
src/app/api/ai/get-models/route.js - Chat API · Server endpoint for AI chat requests and streaming responses.
src/app/api/chat/route.js
Feature modules
Chat, auth, and AI orchestration modules.
- Auth module · Authentication actions and user-facing auth components.
src/modules/authentication - Chat module · Primary chat workspace orchestration and interaction logic.
src/modules/chat - AI agent · Client-side AI request orchestration around the model provider.
src/modules/ai-agent/hook/ai-agent.js - Chat store · Local UI and session state for the chat experience.
src/modules/chat/store/chat-store.js - Chat actions · Server/client action layer for chat lifecycle and persistence.
src/modules/chat/actions/index.js - Message UI · Sidebar, header, composer, selector, and delete modal for chat.
src/modules/chat/components/chat-message-view.jsx - Composer · Message input form used to submit chat prompts.
src/modules/chat/components/chat-message-form.jsx
Shared UI
Reusable design system, AI renderers, providers, and hooks.
- AI renderers · Specialized assistant content renderers for code, citations, tool calls, images, and web previews.
src/components/ai-elements - UI kit · Shared Radix/shadcn-style primitive component library.
src/components/ui - Providers · Theme and query providers for app-wide context.
src/components/providers - Core libs · Auth, database, prompt, and utility helpers.
src/lib
Infra
Database and persistence boundary.
- Database · Prisma schema and migration-backed PostgreSQL persistence layer.
prisma/schema.prisma
Connections 22
- App layout → Auth shell: wraps · Global app layout composes the auth route group shell.
- App layout → Root shell: wraps · Global app layout composes the main app shell.
- Root shell → Home page: contains · Root shell renders the main entry page.
- Root shell → Chat page: contains · Root shell routes into the dynamic chat page.
- Auth shell → Auth API: supports · Auth shell pairs with the Better Auth endpoint.
- Auth module → Auth API: uses · Auth feature relies on the auth API route.
- Auth module → Core libs: uses · Auth feature consumes shared auth helpers.
- Chat module → Chat actions: orchestrates · Chat feature coordinates actions for chat lifecycle.
- Chat module → Chat store: state · Chat feature keeps local interaction state in Zustand.
- Chat module → Message UI: renders · Chat feature assembles the visible chat workspace.
- Message UI → Composer: contains · Chat UI includes the message composer.
- Message UI → Chat API: sends · Composer-driven requests are submitted to the chat API.
- Chat API → AI agent: delegates · Chat API delegates model interaction to the AI hook layer.
- Chat API → Models API: queries · UI and server use model metadata from the models endpoint.
- AI agent → Models API: loads · AI layer can consume model listings for provider selection.
- AI agent → Core libs: uses · AI orchestration relies on prompt and helper utilities.
- Chat actions → Database: persists · Chat actions read and write chat data through Prisma/PostgreSQL.
- Core libs → Database: connects · Database client lives in the shared lib boundary.
- Message UI → AI renderers: renders · Chat messages and assistant output use specialized AI renderers.
- AI renderers → UI kit: built on · AI renderers compose shared UI primitives.
- Providers → UI kit: wraps · Theme and query providers sit above shared UI components.
- Chat module → Providers: uses · Chat experience depends on app-wide providers.