# shraddesh29/ai_chatbot architecture

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

- Interactive diagram: https://gitdiagram.com/shraddesh29/ai_chatbot
- Repository: https://github.com/shraddesh29/ai_chatbot

## Overview

##### Project type and purpose
- This is a full-stack MERN (MongoDB–Express–React–Node) web application that provides an AI-powered chatbot interface.
- Frontend: React + TypeScript + Material-UI, bundler via Vite.
- Backend: Node.js + Express, structured with MVC-style controllers, routes, models, and utilities.
- Persistence: MongoDB for user data and chat history through Mongoose models.
- External AI: Grok’s LLaMA-3.3 API for generating streamed chatbot responses.
- Security: JWT authentication stored in secure cookies.

##### File structure
- Top level splits into “frontend/” and “backend/”.
- backend/src/ contains:
- app.ts and index.ts (Express app setup & server entry)
- controllers/ (chat + user business logic)
- routes/ (chat-routes, user-routes, index)
- models/ (User schema)
- db/ (MongoDB connection)
- utils/ (JWT token manager, validators, constants)
- frontend/src/ contains:
- pages/ (Home, Chat, Login, Signup, NotFound)
- components/ (UI atoms and molecules: Header, Footer, ChatItem, TypingAnim, Shared inputs)
- context/AuthContext.tsx (auth state & provider)
- helpers/ (axiosInstance, API communicator)
- assets, styling, and app entry (main.tsx).

##### README for additional insights
- Project description, features (multi-personality chat, JWT auth, chat history CRUD).
- Tech stack table.
- Setup instructions referencing .env for MONGODB_URI, JWT_SECRET, GROK_API_KEY.
- Mentions streamed responses from Grok LLaMA API.

##### Elements
- Blue rectangles for compute components (Client, API server).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend (React SPA)" 
        direction TB
        SPA["React SPA"]:::frontend
        App["App Component"]:::frontend
        AuthCtx["AuthContext & Provider"]:::frontend
        Axios["Axios Instance"]:::http
        APIComm["API Communicator"]:::http
        subgraph "Pages" 
            direction TB
            Home["Home Page"]:::frontend
            ChatP["Chat Page"]:::frontend
            Login["Login Page"]:::frontend
            Signup["Signup Page"]:::frontend
            NotFound["NotFound Page"]:::frontend
        end
        subgraph "UI Components"
            direction TB
            Header["Header"]:::frontend
            Footer["Footer"]:::frontend
            ChatItem["ChatItem"]:::frontend
            TypingAnim["Typing Animation"]:::frontend
            Inputs["Shared Inputs & Logo"]:::frontend
        end
    end

    %% Backend Layer
    subgraph "Backend (Express API)"
        direction TB
        AppConf["App Configuration"]:::backend
        Server["Server Entry Point"]:::backend
        subgraph "Routes"
            direction TB
            RootRouter["Root Router"]:::backend
            ChatRoutes["Chat Routes"]:::backend
            UserRoutes["User Routes"]:::backend
        end
        subgraph "Controllers"
            direction TB
            ChatCtrl["ChatController"]:::backend
            UserCtrl["UserController"]:::backend
        end
        subgraph "Utils & Services"
            direction TB
            JWTMgr["JWT Token Manager"]:::backend
            Validators["Input Validators"]:::backend
            Constants["Constants"]:::backend
        end
        DBConn["Database Connector"]:::backend
    end

    %% External Services
    DB["MongoDB Database"]:::database
    AI["Grok LLaMA-3.3 API"]:::external

    %% Data Flow
    SPA --> App
    App --> AuthCtx
    App --> Axios
    App --> APIComm
    App --> Home
    App --> ChatP
    App --> Login
    App --> Signup
    App --> NotFound
    App --> Header
    App --> Footer
    App --> ChatItem
    App --> TypingAnim
    App --> Inputs

    APIComm -->|HTTP Requests| RootRouter
    Axios -->|HTTP Requests| RootRouter

    RootRouter --> ChatRoutes
    RootRouter --> UserRoutes

    ChatRoutes --> ChatCtrl
    UserRoutes --> UserCtrl

    ChatCtrl -->|validate JWT| JWTMgr
    ChatCtrl -->|validate input| Validators
    ChatCtrl -->|use constants| Constants
    ChatCtrl -->|connect| DBConn
    ChatCtrl -->|save/retrieve| DB

    UserCtrl -->|validate JWT| JWTMgr
    UserCtrl -->|validate input| Validators
    UserCtrl -->|use constants| Constants
    UserCtrl -->|connect| DBConn
    UserCtrl -->|save/retrieve| DB

    ChatCtrl -->|stream prompt & personality| AI
    AI -->|streamed response| ChatCtrl
    ChatCtrl -->|stream chunks| APIComm

    %% Auth flow (dashed)
    UserCtrl -.->|set secure cookie| AuthCtx
    AuthCtx -.->|attach JWT| Axios

    %% Click Events
    click SPA "https://github.com/shraddesh29/ai_chatbot/blob/main/frontend/src/main.tsx"
    click App "https://github.com/shraddesh29/ai_chatbot/blob/main/frontend/src/App.tsx"
    click AuthCtx "https://github.com/shraddesh29/ai_chatbot/blob/main/frontend/src/context/AuthContext.tsx"
    click Axios "https://github.com/shraddesh29/ai_chatbot/blob/main/frontend/src/helpers/axiosInstance.ts"
    click APIComm "https://github.com/shraddesh29/ai_chatbot/blob/main/frontend/src/helpers/api-communicator.ts"
    click Home "https://github.com/shraddesh29/ai_chatbot/blob/main/frontend/src/pages/Home.tsx"
    click ChatP "https://github.com/shraddesh29/ai_chatbot/blob/main/frontend/src/pages/Chat.tsx"
    click Login "https://github.com/shraddesh29/ai_chatbot/blob/main/frontend/src/pages/Login.tsx"
    click Signup "https://github.com/shraddesh29/ai_chatbot/blob/main/frontend/src/pages/Signup.tsx"
    click NotFound "https://github.com/shraddesh29/ai_chatbot/blob/main/frontend/src/pages/NotFound.tsx"
    click Header "https://github.com/shraddesh29/ai_chatbot/blob/main/frontend/src/components/Header.tsx"
    click Footer "https://github.com/shraddesh29/ai_chatbot/blob/main/frontend/src/components/footer/Footer.tsx"
    click ChatItem "https://github.com/shraddesh29/ai_chatbot/blob/main/frontend/src/components/chat/ChatItem.tsx"
    click TypingAnim "https://github.com/shraddesh29/ai_chatbot/blob/main/frontend/src/components/typer/TypingAnim.tsx"
    click Inputs "https://github.com/shraddesh29/ai_chatbot/blob/main/frontend/src/components/shared/CustomizedInput.tsx"
    click Inputs "https://github.com/shraddesh29/ai_chatbot/blob/main/frontend/src/components/shared/Logo.tsx"
    click Inputs "https://github.com/shraddesh29/ai_chatbot/blob/main/frontend/src/components/shared/NavigationLink.tsx"
    click AppConf "https://github.com/shraddesh29/ai_chatbot/blob/main/backend/src/app.ts"
    click Server "https://github.com/shraddesh29/ai_chatbot/blob/main/backend/src/index.ts"
    click RootRouter "https://github.com/shraddesh29/ai_chatbot/blob/main/backend/src/routes/index.ts"
    click ChatRoutes "https://github.com/shraddesh29/ai_chatbot/blob/main/backend/src/routes/chat-routes.ts"
    click UserRoutes "https://github.com/shraddesh29/ai_chatbot/blob/main/backend/src/routes/user-routes.ts"
    click ChatCtrl "https://github.com/shraddesh29/ai_chatbot/blob/main/backend/src/controllers/chat-controllers.ts"
    click UserCtrl "https://github.com/shraddesh29/ai_chatbot/blob/main/backend/src/controllers/user-controller.ts"
    click DBConn "https://github.com/shraddesh29/ai_chatbot/blob/main/backend/src/db/connection.ts"
    click JWTMgr "https://github.com/shraddesh29/ai_chatbot/blob/main/backend/src/utils/token-manager.ts"
    click Validators "https://github.com/shraddesh29/ai_chatbot/blob/main/backend/src/utils/validators.ts"
    click Constants "https://github.com/shraddesh29/ai_chatbot/blob/main/backend/src/utils/constants.ts"
    click AI "https://github.com/shraddesh29/ai_chatbot/blob/main/backend/dist/config/openai-config.js"

    %% Styles
    classDef frontend fill:#6ECFF6,stroke:#0366d6,color:#000
    classDef backend fill:#F6D186,stroke:#d67b03,color:#000
    classDef http fill:#C8E6C9,stroke:#388E3C,color:#000
    classDef database fill:#FFF59D,stroke:#FBC02D,color:#000
    classDef external fill:#E1BEE7,stroke:#8E24AA,color:#000
```
