# tirth-chokshi/querysage-ai architecture

> Architecture diagram of the GitHub repository tirth-chokshi/querysage-ai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-05). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tirth-chokshi/querysage-ai
- Repository: https://github.com/tirth-chokshi/querysage-ai

## Overview

##### Main Components

A. Frontend Layer:
- Next.js Client Components (/src/components)
- Authentication UI (/src/components/user-auth-form.jsx)
- Chat Interface (/src/components/ChatInterface.jsx)
- Dashboard Components (/src/app/dashboard)

B. API Layer:
- Authentication Routes (/src/app/api/auth)
- Query Management (/src/app/api/query)
- Chat API (/src/pages/api/chat.js)
- Database Operations (/src/pages/api/testConnection.js, query.js)

C. Database Layer:
- MongoDB Models (/src/models)
- External Database Connections (based on DataSource table)

D. Authentication System:
- NextAuth.js Integration (/src/app/api/auth/[...nextauth])

**[Client Layer]**
- ↑↓
**[API Layer]**
- ↑↓
**[Service Layer]**
- ↑↓
**[Data Layer]**

##### Key Relationships to Highlight

a. User Flow:
- Authentication flow through NextAuth.js
- Dashboard access and permissions
- Chat interface interactions

b. Data Flow:
- Natural language query processing
- Database connection management
- Query execution and results handling
- History tracking

##### Specific Components to Detail

Frontend:
- Chat Interface
- Query Builder
- Results Visualization
- User Dashboard
- Authentication Forms

API Endpoints:
- Query Processing
- Database Connections
- Chat Management
- Data Visualization

Database Schema:
- User Management
- Query Storage
- Results Caching
- Error Logging
- System Settings

##### Integration Points

- NextAuth.js with MongoDB
- External Database Connections
- AI Query Processing
- Real-time Chat Features

##### Additional Elements

- Version information
- Scaling components
- Security boundaries
- Error handling paths

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client
        UI[Chat Interface]
        Auth[Authentication UI]
        Dash[Dashboard]
        Vis[Results Visualization]
    end

    subgraph API
        AuthAPI[Authentication API]
        QueryAPI[Query Management API]
        ChatAPI[Chat API]
        ConnAPI[Connection Testing API]
        DBSumAPI[Database Summary API]
        HistAPI[Chat History API]
    end

    subgraph Services
        AuthService[Authentication Service]
        QueryService[Query Processing Service]
        DBConnService[Database Connection Service]
        AIService[AI Processing Service]
    end

    subgraph Data
        MongoDB[(MongoDB)]
        ExtDB[(External Databases)]
        subgraph Models
            UserModel[User Model]
            ChatModel[Chat Model]
            MessageModel[Message Model]
        end
    end

    %% Client Layer Connections
    UI --> ChatAPI
    UI --> QueryAPI
    Auth --> AuthAPI
    Dash --> DBSumAPI
    Dash --> HistAPI

    %% API Layer Connections
    AuthAPI --> AuthService
    QueryAPI --> QueryService
    ChatAPI --> AIService
    ConnAPI --> DBConnService
    DBSumAPI --> DBConnService
    HistAPI --> QueryService

    %% Service Layer Connections
    AuthService --> MongoDB
    QueryService --> ExtDB
    QueryService --> MongoDB
    DBConnService --> ExtDB
    AIService --> QueryService

    %% Data Layer Connections
    MongoDB --> UserModel
    MongoDB --> ChatModel
    MongoDB --> MessageModel

    %% Click Events
    click UI "https://github.com/Tirth-chokshi/QuerySage-AI/blob/master//src/components/ChatInterface.jsx"
    click Auth "https://github.com/Tirth-chokshi/QuerySage-AI/blob/master//src/components/user-auth-form.jsx"
    click Dash "https://github.com/Tirth-chokshi/QuerySage-AI/tree/master//src/app/dashboard/"
    click AuthAPI "https://github.com/Tirth-chokshi/QuerySage-AI/blob/master//src/app/api/auth/[...nextauth]/route.js"
    click QueryAPI "https://github.com/Tirth-chokshi/QuerySage-AI/blob/master//src/pages/api/query.js"
    click ChatAPI "https://github.com/Tirth-chokshi/QuerySage-AI/blob/master//src/pages/api/chat.js"
    click DBConnService "https://github.com/Tirth-chokshi/QuerySage-AI/blob/master//src/lib/dbConnect.js"
    click Models "https://github.com/Tirth-chokshi/QuerySage-AI/tree/master//src/models/"
    click ConnAPI "https://github.com/Tirth-chokshi/QuerySage-AI/blob/master//src/pages/api/testConnection.js"
    click HistAPI "https://github.com/Tirth-chokshi/QuerySage-AI/blob/master//src/pages/api/getChatHistory.js"
    click DBSumAPI "https://github.com/Tirth-chokshi/QuerySage-AI/blob/master//src/pages/api/getDatabaseSummary.js"
    click UserModel "https://github.com/Tirth-chokshi/QuerySage-AI/blob/master//src/models/User.js"
    click ChatModel "https://github.com/Tirth-chokshi/QuerySage-AI/blob/master//src/models/Chat.js"
    click MessageModel "https://github.com/Tirth-chokshi/QuerySage-AI/blob/master//src/models/Message.js"
    click QueryAPI "https://github.com/Tirth-chokshi/QuerySage-AI/blob/master//src/app/api/query/route.ts"
    click ConnAPI "https://github.com/Tirth-chokshi/QuerySage-AI/blob/master//src/app/api/testConn/route.ts"

    %% Styling
    classDef client fill:#a8d1ff,stroke:#333,stroke-width:2px
    classDef api fill:#90EE90,stroke:#333,stroke-width:2px
    classDef service fill:#FFE5B4,stroke:#333,stroke-width:2px
    classDef data fill:#FFB6C1,stroke:#333,stroke-width:2px
    classDef model fill:#DDA0DD,stroke:#333,stroke-width:2px

    class UI,Auth,Dash,Vis client
    class AuthAPI,QueryAPI,ChatAPI,ConnAPI,DBSumAPI,HistAPI api
    class AuthService,QueryService,DBConnService,AIService service
    class MongoDB,ExtDB data
    class UserModel,ChatModel,MessageModel model
```
