# anuj2731997/ai-chat architecture

> Architecture diagram of the GitHub repository anuj2731997/ai-chat, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/anuj2731997/ai-chat
- Repository: https://github.com/anuj2731997/ai-chat

## Overview

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: [`src/modules/chat`](https://github.com/anuj2731997/ai-chat/tree/main/src/modules/chat) (Chat module), [`src/modules/chat/components/chat-message-view.jsx`](https://github.com/anuj2731997/ai-chat/blob/main/src/modules/chat/components/chat-message-view.jsx) (Message UI), [`src/app/(root)/layout.jsx`](https://github.com/anuj2731997/ai-chat/blob/main/src/app/(root)/layout.jsx) (Root shell), [`src/app/api/chat/route.js`](https://github.com/anuj2731997/ai-chat/blob/main/src/app/api/chat/route.js) (Chat API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_app["App routes"]
  node_node_app_layout["App layout<br/>next layout<br/>[layout.js]"]
  node_node_auth_layout["Auth shell<br/>route layout<br/>[layout.jsx]"]
  node_node_root_layout["Root shell<br/>route layout<br/>[layout.jsx]"]
  node_node_root_page["Home page<br/>route page<br/>[page.jsx]"]
  node_node_chat_page["Chat page<br/>route page<br/>[page.jsx]"]
  node_node_auth_route["Auth API<br/>route handler<br/>[route.js]"]
  node_node_models_route["Models API<br/>route handler<br/>[route.js]"]
  node_node_chat_route["Chat API<br/>route handler<br/>[route.js]"]
end

subgraph group_group_features["Feature modules"]
  node_node_auth_module["Auth module<br/>feature"]
  node_node_chat_module["Chat module<br/>feature"]
  node_node_ai_agent["AI agent<br/>hook<br/>[ai-agent.js]"]
  node_node_chat_store["Chat store<br/>state store<br/>[chat-store.js]"]
  node_node_chat_actions["Chat actions<br/>[index.js]"]
  node_node_message_view["Message UI<br/>chat components"]
  node_node_message_form["Composer<br/>chat component"]
end

subgraph group_group_shared["Shared UI"]
  node_node_ai_elements["AI renderers<br/>rendering kit"]
  node_node_ui_primitives["UI kit<br/>design system"]
  node_node_providers["Providers"]
  node_node_utils["Core libs<br/>shared lib"]
end

subgraph group_group_infra["Infra"]
  node_node_db[("Database<br/>postgres/prisma<br/>[schema.prisma]")]
end

node_node_app_layout -->|"wraps"| node_node_auth_layout
node_node_app_layout -->|"wraps"| node_node_root_layout
node_node_root_layout -->|"contains"| node_node_root_page
node_node_root_layout -->|"contains"| node_node_chat_page
node_node_auth_layout -->|"supports"| node_node_auth_route
node_node_auth_module -->|"uses"| node_node_auth_route
node_node_auth_module -->|"uses"| node_node_utils
node_node_chat_module -->|"orchestrates"| node_node_chat_actions
node_node_chat_module -->|"state"| node_node_chat_store
node_node_chat_module -->|"renders"| node_node_message_view
node_node_message_view -->|"contains"| node_node_message_form
node_node_message_view -->|"sends"| node_node_chat_route
node_node_chat_route -->|"delegates"| node_node_ai_agent
node_node_chat_route -->|"queries"| node_node_models_route
node_node_ai_agent -->|"loads"| node_node_models_route
node_node_ai_agent -->|"uses"| node_node_utils
node_node_chat_actions -->|"persists"| node_node_db
node_node_utils -->|"connects"| node_node_db
node_node_message_view -->|"renders"| node_node_ai_elements
node_node_ai_elements -->|"built on"| node_node_ui_primitives
node_node_providers -->|"wraps"| node_node_ui_primitives
node_node_chat_module -->|"uses"| node_node_providers

click node_node_app_layout "https://github.com/anuj2731997/ai-chat/blob/main/src/app/layout.js"
click node_node_auth_layout "https://github.com/anuj2731997/ai-chat/blob/main/src/app/(auth)/layout.jsx"
click node_node_root_layout "https://github.com/anuj2731997/ai-chat/blob/main/src/app/(root)/layout.jsx"
click node_node_root_page "https://github.com/anuj2731997/ai-chat/blob/main/src/app/(root)/page.jsx"
click node_node_chat_page "https://github.com/anuj2731997/ai-chat/blob/main/src/app/(root)/chat/[chatId]/page.jsx"
click node_node_auth_route "https://github.com/anuj2731997/ai-chat/blob/main/src/app/api/auth/[...all]/route.js"
click node_node_models_route "https://github.com/anuj2731997/ai-chat/blob/main/src/app/api/ai/get-models/route.js"
click node_node_chat_route "https://github.com/anuj2731997/ai-chat/blob/main/src/app/api/chat/route.js"
click node_node_auth_module "https://github.com/anuj2731997/ai-chat/tree/main/src/modules/authentication"
click node_node_chat_module "https://github.com/anuj2731997/ai-chat/tree/main/src/modules/chat"
click node_node_ai_agent "https://github.com/anuj2731997/ai-chat/blob/main/src/modules/ai-agent/hook/ai-agent.js"
click node_node_chat_store "https://github.com/anuj2731997/ai-chat/blob/main/src/modules/chat/store/chat-store.js"
click node_node_chat_actions "https://github.com/anuj2731997/ai-chat/blob/main/src/modules/chat/actions/index.js"
click node_node_message_view "https://github.com/anuj2731997/ai-chat/blob/main/src/modules/chat/components/chat-message-view.jsx"
click node_node_message_form "https://github.com/anuj2731997/ai-chat/blob/main/src/modules/chat/components/chat-message-form.jsx"
click node_node_ai_elements "https://github.com/anuj2731997/ai-chat/tree/main/src/components/ai-elements"
click node_node_ui_primitives "https://github.com/anuj2731997/ai-chat/tree/main/src/components/ui"
click node_node_providers "https://github.com/anuj2731997/ai-chat/tree/main/src/components/providers"
click node_node_utils "https://github.com/anuj2731997/ai-chat/tree/main/src/lib"
click node_node_db "https://github.com/anuj2731997/ai-chat/blob/main/prisma/schema.prisma"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_node_app_layout,node_node_auth_layout,node_node_root_layout,node_node_root_page,node_node_chat_page,node_node_auth_route,node_node_models_route,node_node_chat_route toneBlue
class node_node_auth_module,node_node_chat_module,node_node_ai_agent,node_node_chat_store,node_node_chat_actions,node_node_message_view,node_node_message_form toneAmber
class node_node_ai_elements,node_node_ui_primitives,node_node_providers,node_node_utils toneMint
class node_node_db toneRose
```

## Components

### App routes

Next.js App Router composition, route handlers, and page shells.

- [App layout](https://github.com/anuj2731997/ai-chat/blob/main/src/app/layout.js): next layout · Global application wrapper and top-level styling shell. · `src/app/layout.js`
- [Auth shell](https://github.com/anuj2731997/ai-chat/blob/main/src/app/(auth)/layout.jsx): route layout · Unauthenticated route group wrapper for sign-in flows. · `src/app/(auth)/layout.jsx`
- [Root shell](https://github.com/anuj2731997/ai-chat/blob/main/src/app/(root)/layout.jsx): route layout · Main application shell for authenticated pages. · `src/app/(root)/layout.jsx`
- [Home page](https://github.com/anuj2731997/ai-chat/blob/main/src/app/(root)/page.jsx): route page · Landing or chat entry page at the app root. · `src/app/(root)/page.jsx`
- [Chat page](https://github.com/anuj2731997/ai-chat/blob/main/src/app/(root)/chat/[chatId]/page.jsx): route page · Dynamic chat route for an individual conversation. · `src/app/(root)/chat/[chatId]/page.jsx`
- [Auth API](https://github.com/anuj2731997/ai-chat/blob/main/src/app/api/auth/[...all]/route.js): route handler · Catch-all Better Auth endpoint for sign-in and session flows. · `src/app/api/auth/[...all]/route.js`
- [Models API](https://github.com/anuj2731997/ai-chat/blob/main/src/app/api/ai/get-models/route.js): route handler · Endpoint that supplies available models to the UI. · `src/app/api/ai/get-models/route.js`
- [Chat API](https://github.com/anuj2731997/ai-chat/blob/main/src/app/api/chat/route.js): route handler · 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](https://github.com/anuj2731997/ai-chat/tree/main/src/modules/authentication): feature · Authentication actions and user-facing auth components. · `src/modules/authentication`
- [Chat module](https://github.com/anuj2731997/ai-chat/tree/main/src/modules/chat): feature · Primary chat workspace orchestration and interaction logic. · `src/modules/chat`
- [AI agent](https://github.com/anuj2731997/ai-chat/blob/main/src/modules/ai-agent/hook/ai-agent.js): hook · Client-side AI request orchestration around the model provider. · `src/modules/ai-agent/hook/ai-agent.js`
- [Chat store](https://github.com/anuj2731997/ai-chat/blob/main/src/modules/chat/store/chat-store.js): state store · Local UI and session state for the chat experience. · `src/modules/chat/store/chat-store.js`
- [Chat actions](https://github.com/anuj2731997/ai-chat/blob/main/src/modules/chat/actions/index.js): actions · Server/client action layer for chat lifecycle and persistence. · `src/modules/chat/actions/index.js`
- [Message UI](https://github.com/anuj2731997/ai-chat/blob/main/src/modules/chat/components/chat-message-view.jsx): chat components · Sidebar, header, composer, selector, and delete modal for chat. · `src/modules/chat/components/chat-message-view.jsx`
- [Composer](https://github.com/anuj2731997/ai-chat/blob/main/src/modules/chat/components/chat-message-form.jsx): chat component · 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](https://github.com/anuj2731997/ai-chat/tree/main/src/components/ai-elements): rendering kit · Specialized assistant content renderers for code, citations, tool calls, images, and web previews. · `src/components/ai-elements`
- [UI kit](https://github.com/anuj2731997/ai-chat/tree/main/src/components/ui): design system · Shared Radix/shadcn-style primitive component library. · `src/components/ui`
- [Providers](https://github.com/anuj2731997/ai-chat/tree/main/src/components/providers): app providers · Theme and query providers for app-wide context. · `src/components/providers`
- [Core libs](https://github.com/anuj2731997/ai-chat/tree/main/src/lib): shared lib · Auth, database, prompt, and utility helpers. · `src/lib`

### Infra

Database and persistence boundary.

- [Database](https://github.com/anuj2731997/ai-chat/blob/main/prisma/schema.prisma): postgres/prisma · Prisma schema and migration-backed PostgreSQL persistence layer. · `prisma/schema.prisma`

## Connections

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