# samuelpoiani/aichat architecture

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

- Interactive diagram: https://gitdiagram.com/samuelpoiani/aichat
- Repository: https://github.com/samuelpoiani/aichat

## Overview

##### Project Overview and Purpose
- AIChat is a full-stack chat application that lets users interact in real time with Groq AI.
- The goal is to provide a web-based chat interface that supports multiple chat sessions, persistent chat history, and real-time AI responses.
- The project uses Node.js with Express for the backend, a set of client-side JavaScript files for the frontend, Prisma for database interactions with SQLite, and an external Groq AI service.

##### Main Components
a. Frontend (Client-Side Interface):
- "public" folder where the HTML (index.html) and associated JavaScript files (chatService.js, eventHandlers.js, main.js, messageHandler.js, uiManager.js, utils.js) reside.

b. Backend (Server-Side Application):
- "server" directory as the backend layer. Inside this, you have:
- Controllers (chatController.js, messageController.js) – manage handling incoming requests and business logic.
- Routes (chatRoutes.js) – define the API endpoints.
- DB (config.js under server/db) – configuration for database connectivity.
- Services (groqService.js) – handles communication with the Groq AI API.
- This entire portion as “API Server” or “Backend Service.”

c. Database:
- A component for the database. Refer to the “prisma” folder where the schema (schema.prisma) is used to define and set up the SQLite database.

d. External Service (Groq AI):
- External Groq AI API as a separate component.
- That the backend’s groqService communicates with this external service to get AI responses.

##### Relationships and Data Flow
- From the Client/Browser UI to the API Server to indicate when users send chat messages or request chat history.
- The controllers process user input.
- Depending on the request, the server will either interact with the database (for persistent chat history) or call the Groq AI API (for generating responses).

##### Architectural Patterns and Principles
- A separation of concerns:
- The frontend is solely responsible for presenting the UI and handling user interactions (using modular JavaScript).
- The backend handles business logic, routing, and data persistence.
- Clear modularity exists in the project, adhering to the MVC-like pattern where controllers and routes organize request processing.

##### Putting Everything Together
- Inside the API Server, detail the routing flow: requests received via Express routes are handled by controllers, which then may call the groqService (for AI responses) or interact with the Database.
- Separate connections:
- From API Server to the “Database (SQLite/Prisma)” component.
- From API Server to the “External Groq AI Service”.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    client["Client/Browser UI"]:::frontend

    %% API Server Subgraph
    subgraph "API Server"
        routes["Routes (chatRoutes.js)"]:::backend
        controllers["Controllers (chatController.js, messageController.js)"]:::backend
        dbconfig["Database Config (config.js)"]:::backend
        groqsvc["Groq Service (groqService.js)"]:::backend
        routes -->|"processRequest"| controllers
        controllers -->|"storeHistory"| dbconfig
        controllers -->|"requestAI"| groqsvc
    end

    %% Database and External Service
    db["Database (SQLite via Prisma)"]:::database
    external["External Groq AI Service"]:::external

    %% Data Flow
    client -->|"sendMessage"| routes
    dbconfig -->|"DBQuery:RetrieveStoreHistory"| db
    groqsvc -->|"callGroqAPI"| external
    external -->|"returnAIResponse"| groqsvc
    controllers -->|"sendResponse"| client

    %% Click Events
    click client "https://github.com/samuelpoiani/aichat/tree/main/public"
    click routes "https://github.com/samuelpoiani/aichat/blob/main/server/routes/chatRoutes.js"
    click controllers "https://github.com/samuelpoiani/aichat/tree/main/server/controllers"
    click dbconfig "https://github.com/samuelpoiani/aichat/blob/main/server/db/config.js"
    click groqsvc "https://github.com/samuelpoiani/aichat/blob/main/server/services/groqService.js"
    click db "https://github.com/samuelpoiani/aichat/tree/main/prisma"

    %% Styles
    classDef frontend fill:#B3E5FC,stroke:#0288D1,stroke-width:2px;
    classDef backend fill:#FFE0B2,stroke:#F57C00,stroke-width:2px;
    classDef database fill:#C8E6C9,stroke:#388E3C,stroke-width:2px;
    classDef external fill:#F8BBD0,stroke:#D81B60,stroke-width:2px;
```
