# avinashkrishnaaqe/ai-chat-app architecture

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

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

## Overview

This is an AI chat application with a Next.js client and FastAPI backend. The main workflow is user authentication, conversation management, and streamed model responses, with persistent conversation history and refresh-token sessions. The graph includes the documented model-selection and chat UI components, plus the backend authentication, chat, and LLM layers. Evidence is limited to the supplied excerpts and SOURCE INDEX; links for unsampled frontend-to-chat API wiring are omitted where no relationship is shown.

Main entry points: [`backend/app/services/auth_service.py`](https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/backend/app/services/auth_service.py) (Auth service), [`backend/app/services/chat_service.py`](https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/backend/app/services/chat_service.py) (Conversation service), [`backend/app/api/v1/auth.py`](https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/backend/app/api/v1/auth.py) (Auth endpoints), [`frontend/src/components/chat/ChatWindow.tsx`](https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/frontend/src/components/chat/ChatWindow.tsx) (Chat window)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Web client"]
  node_chat_window["Chat window<br/>[ChatWindow.tsx]"]
  node_composer["Message composer<br/>[Composer.tsx]"]
  node_model_picker["Model picker<br/>[ModelPicker.tsx]"]
  node_quick_prompts["Quick prompts<br/>[QuickPrompts.tsx]"]
  node_message_list["Message list<br/>[MessageList.tsx]"]
  node_message_bubble["Message bubble<br/>[MessageBubble.tsx]"]
  node_sidebar["Chat sidebar<br/>[Sidebar.tsx]"]
  node_chat_api["Chat API client<br/>[chat.ts]"]
  node_socket["Chat socket<br/>[chatSocket.ts]"]
  node_api_client["API request client<br/>[client.ts]"]
  node_auth_store["Auth state<br/>[useAuthStore.ts]"]
  node_auth_api["Auth API client<br/>[auth.ts]"]
end

subgraph group_api["API and identity"]
  node_auth_routes["Auth endpoints<br/>[auth.py]"]
  node_chat_routes["Chat endpoints<br/>[chats.py]"]
  node_ws_routes["Streaming endpoint<br/>[ws_chat.py]"]
  node_app_entry["API application<br/>[main.py]"]
  node_auth_service["Auth service<br/>[auth_service.py]"]
  node_dependencies["Auth dependencies<br/>[deps.py]"]
end

subgraph group_chat["Conversation and generation"]
  node_chat_service["Conversation service<br/>[chat_service.py]"]
  node_llm_service["LLM service<br/>[llm_service.py]"]
end

subgraph group_data["Persistence and security"]
  node_database[("SQL database<br/>[session.py]")]
  node_user_model["User records<br/>[user.py]"]
  node_refresh_model["Refresh tokens<br/>[refresh_token.py]"]
  node_conversation_model["Conversations<br/>[conversation.py]"]
  node_message_model["Messages<br/>[message.py]"]
  node_security["Token security<br/>[security.py]"]
end

node_user(("User"))
node_model_provider{{"OpenRouter models"}}

node_user -.->|"uses"| node_chat_window
node_chat_window -->|"renders"| node_message_list
node_chat_window -->|"renders"| node_composer
node_chat_window -->|"renders"| node_quick_prompts
node_composer -->|"renders"| node_model_picker
node_message_list -.->|"renders"| node_message_bubble
node_api_client -->|"reads token"| node_auth_store
node_api_client -->|"updates session"| node_auth_store
node_auth_api -->|"uses requests"| node_api_client
node_api_client -->|"refreshes session"| node_auth_routes
node_user -.->|"authenticates"| node_auth_routes
node_app_entry -->|"registers"| node_auth_routes
node_app_entry -->|"registers"| node_chat_routes
node_app_entry -->|"registers"| node_ws_routes
node_auth_routes -->|"calls"| node_auth_service
node_auth_routes -->|"authenticates via"| node_dependencies
node_chat_routes -->|"calls"| node_chat_service
node_chat_routes -->|"generates via"| node_llm_service
node_ws_routes -->|"calls"| node_chat_service
node_ws_routes -->|"streams via"| node_llm_service
node_llm_service -->|"requests completion"| node_model_provider
node_auth_service -->|"reads and writes"| node_user_model
node_auth_service -->|"reads and writes"| node_refresh_model
node_auth_service -->|"creates and verifies tokens"| node_security
node_chat_service -->|"reads and writes"| node_conversation_model
node_chat_service -->|"reads and writes"| node_message_model
node_auth_service -.->|"persists records"| node_database
node_chat_service -.->|"persists history"| node_database
node_dependencies -->|"decodes token"| node_security
node_dependencies -.->|"loads user"| node_database

click node_chat_window "https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/frontend/src/components/chat/ChatWindow.tsx"
click node_composer "https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/frontend/src/components/chat/Composer.tsx"
click node_model_picker "https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/frontend/src/components/chat/ModelPicker.tsx"
click node_quick_prompts "https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/frontend/src/components/chat/QuickPrompts.tsx"
click node_message_list "https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/frontend/src/components/chat/MessageList.tsx"
click node_message_bubble "https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/frontend/src/components/chat/MessageBubble.tsx"
click node_sidebar "https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/frontend/src/components/sidebar/Sidebar.tsx"
click node_chat_api "https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/frontend/src/lib/api/chat.ts"
click node_socket "https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/frontend/src/lib/ws/chatSocket.ts"
click node_api_client "https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/frontend/src/lib/api/client.ts"
click node_auth_store "https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/frontend/src/store/useAuthStore.ts"
click node_auth_api "https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/frontend/src/lib/api/auth.ts"
click node_auth_routes "https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/backend/app/api/v1/auth.py"
click node_chat_routes "https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/backend/app/api/v1/chats.py"
click node_ws_routes "https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/backend/app/api/v1/ws_chat.py"
click node_app_entry "https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/backend/app/main.py"
click node_auth_service "https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/backend/app/services/auth_service.py"
click node_chat_service "https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/backend/app/services/chat_service.py"
click node_llm_service "https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/backend/app/services/llm_service.py"
click node_database "https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/backend/app/db/session.py"
click node_user_model "https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/backend/app/models/user.py"
click node_refresh_model "https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/backend/app/models/refresh_token.py"
click node_conversation_model "https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/backend/app/models/conversation.py"
click node_message_model "https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/backend/app/models/message.py"
click node_security "https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/backend/app/core/security.py"
click node_dependencies "https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/backend/app/api/deps.py"

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_chat_window,node_composer,node_model_picker,node_quick_prompts,node_message_list,node_message_bubble,node_sidebar,node_chat_api,node_socket,node_api_client,node_auth_store,node_auth_api,node_user toneBlue
class node_auth_routes,node_chat_routes,node_ws_routes,node_app_entry,node_auth_service,node_dependencies toneAmber
class node_chat_service,node_llm_service,node_model_provider toneMint
class node_database,node_user_model,node_refresh_model,node_conversation_model,node_message_model,node_security toneRose
```

## Components

### Web client

- [Chat window](https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/frontend/src/components/chat/ChatWindow.tsx): `frontend/src/components/chat/ChatWindow.tsx`
- [Message composer](https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/frontend/src/components/chat/Composer.tsx): `frontend/src/components/chat/Composer.tsx`
- [Model picker](https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/frontend/src/components/chat/ModelPicker.tsx): `frontend/src/components/chat/ModelPicker.tsx`
- [Quick prompts](https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/frontend/src/components/chat/QuickPrompts.tsx): `frontend/src/components/chat/QuickPrompts.tsx`
- [Message list](https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/frontend/src/components/chat/MessageList.tsx): `frontend/src/components/chat/MessageList.tsx`
- [Message bubble](https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/frontend/src/components/chat/MessageBubble.tsx): `frontend/src/components/chat/MessageBubble.tsx`
- [Chat sidebar](https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/frontend/src/components/sidebar/Sidebar.tsx): `frontend/src/components/sidebar/Sidebar.tsx`
- [Chat API client](https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/frontend/src/lib/api/chat.ts): `frontend/src/lib/api/chat.ts`
- [Chat socket](https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/frontend/src/lib/ws/chatSocket.ts): `frontend/src/lib/ws/chatSocket.ts`
- [API request client](https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/frontend/src/lib/api/client.ts): `frontend/src/lib/api/client.ts`
- [Auth state](https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/frontend/src/store/useAuthStore.ts): `frontend/src/store/useAuthStore.ts`
- [Auth API client](https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/frontend/src/lib/api/auth.ts): `frontend/src/lib/api/auth.ts`

### API and identity

- [Auth endpoints](https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/backend/app/api/v1/auth.py): `backend/app/api/v1/auth.py`
- [Chat endpoints](https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/backend/app/api/v1/chats.py): `backend/app/api/v1/chats.py`
- [Streaming endpoint](https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/backend/app/api/v1/ws_chat.py): `backend/app/api/v1/ws_chat.py`
- [API application](https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/backend/app/main.py): `backend/app/main.py`
- [Auth service](https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/backend/app/services/auth_service.py): `backend/app/services/auth_service.py`
- [Auth dependencies](https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/backend/app/api/deps.py): `backend/app/api/deps.py`

### Conversation and generation

- [Conversation service](https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/backend/app/services/chat_service.py): `backend/app/services/chat_service.py`
- [LLM service](https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/backend/app/services/llm_service.py): `backend/app/services/llm_service.py`

### Persistence and security

- [SQL database](https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/backend/app/db/session.py): `backend/app/db/session.py`
- [User records](https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/backend/app/models/user.py): `backend/app/models/user.py`
- [Refresh tokens](https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/backend/app/models/refresh_token.py): `backend/app/models/refresh_token.py`
- [Conversations](https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/backend/app/models/conversation.py): `backend/app/models/conversation.py`
- [Messages](https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/backend/app/models/message.py): `backend/app/models/message.py`
- [Token security](https://github.com/avinashkrishnaaqe/ai-chat-app/blob/master/backend/app/core/security.py): `backend/app/core/security.py`

### Other components

- **User**
- **OpenRouter models**

## Connections

- User → Chat window: uses
- Chat window → Message list: renders (evidence: `frontend/src/components/chat/ChatWindow.tsx`)
- Chat window → Message composer: renders (evidence: `frontend/src/components/chat/ChatWindow.tsx`)
- Chat window → Quick prompts: renders (evidence: `frontend/src/components/chat/ChatWindow.tsx`)
- Message composer → Model picker: renders (evidence: `frontend/src/components/chat/Composer.tsx`)
- Message list → Message bubble: renders
- API request client → Auth state: reads token (evidence: `frontend/src/lib/api/client.ts`)
- API request client → Auth state: updates session (evidence: `frontend/src/lib/api/client.ts`)
- Auth API client → API request client: uses requests (evidence: `frontend/src/lib/api/auth.ts`)
- API request client → Auth endpoints: refreshes session (evidence: `frontend/src/lib/api/client.ts`)
- User → Auth endpoints: authenticates
- API application → Auth endpoints: registers (evidence: `backend/app/main.py`)
- API application → Chat endpoints: registers (evidence: `backend/app/main.py`)
- API application → Streaming endpoint: registers (evidence: `backend/app/main.py`)
- Auth endpoints → Auth service: calls (evidence: `backend/app/api/v1/auth.py`)
- Auth endpoints → Auth dependencies: authenticates via (evidence: `backend/app/api/v1/auth.py`)
- Chat endpoints → Conversation service: calls (evidence: `backend/app/api/v1/chats.py`)
- Chat endpoints → LLM service: generates via (evidence: `backend/app/api/v1/chats.py`)
- Streaming endpoint → Conversation service: calls (evidence: `backend/app/api/v1/ws_chat.py`)
- Streaming endpoint → LLM service: streams via (evidence: `backend/app/api/v1/ws_chat.py`)
- LLM service → OpenRouter models: requests completion (evidence: `backend/app/services/llm_service.py`)
- Auth service → User records: reads and writes (evidence: `backend/app/services/auth_service.py`)
- Auth service → Refresh tokens: reads and writes (evidence: `backend/app/services/auth_service.py`)
- Auth service → Token security: creates and verifies tokens (evidence: `backend/app/services/auth_service.py`)
- Conversation service → Conversations: reads and writes (evidence: `backend/app/services/chat_service.py`)
- Conversation service → Messages: reads and writes (evidence: `backend/app/services/chat_service.py`)
- Auth service → SQL database: persists records
- Conversation service → SQL database: persists history
- Auth dependencies → Token security: decodes token (evidence: `backend/app/api/deps.py`)
- Auth dependencies → SQL database: loads user (evidence: `backend/app/api/deps.py`)
