# paijoe29/wa-gemini architecture

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

- Interactive diagram: https://gitdiagram.com/paijoe29/wa-gemini
- Repository: https://github.com/paijoe29/wa-gemini

## Overview

##### Project Type and Purpose
- This is a full-stack client–server application (“WhatsApp Gateway Unofficial”) composed of:
- A Node.js/Express backend leveraging the Baileys library to connect to WhatsApp via QR code and manage sessions.
- A React + Vite + TailAdmin frontend providing UI for QR login, messaging, media upload, inbox, settings, and logs.
- Its purpose is to simulate or automate WhatsApp messaging for learning/internal testing, exposing REST and real-time APIs to the frontend.

##### File Structure Analysis
Top‐level separation:
- /backend – Express server
– server.js (entry point)
– routes/ → high-level REST endpoints (/api/contacts, /api/whatsapp, /api/gemini, etc.)
– controllers/ → HTTP handler logic
– services/ → core business logic (WebSocket, Baileys integration, conversation history, Gemini calls)
– middleware/ → auth, error handling
– data/ → local JSON stores (categories, conversation_history)
– utils/ → logging, config
- /src (frontend) – React app
– main.tsx & App.tsx (entry points)
– pages/WhatsApp – per-feature pages (Inbox, SendMessage, Settings, Logs, QRLogin)
– components/whatsapp – reusable UI pieces (MessageForm, QRCode, InboxTable)
– services/ – client HTTP & real-time wrappers (apiService, websocketService, sseService)
– context/hooks/layout/lib – cross-cutting concerns (auth, theme, localDb, Supabase helper)
- config files (package.json, tsconfig.json, vite.config.ts) and public assets.

##### Key Architecture Insights from README
- Backend exposes a RESTful API under /api plus real-time channels (WebSocket or SSE) for live inbox/log updates.
- Uses Baileys to connect with WhatsApp’s WebSocket-like protocol; sessions are stored locally.
- Uses an external Gemini AI API for auto-reply features, secured by server-side .env.
- Frontend communicates via HTTP + API_KEY header for authenticated access, plus real-time updates.

a. Main Components
1. Frontend Client
– Browser (React/Vite app)
– UI Modules: QRLogin, Dashboard, SendMessage, SendMedia, InboxViewer, Settings, LogViewer
– Client Services: HTTP client, WebSocket client, SSE client, local session store (localDb)
2. Backend Server
– Express HTTP API layer (routes/controllers)
– Authentication middleware (API key check)
– Business services:
- WhatsApp Service (Baileys wrapper)
- ConversationHistory Service (persist JSON)
- Gemini AI Service (external API calls)
- WebSocket Service (push real-time events)
– Data stores (local JSON files under /data or file-based storage)
3. External Dependencies
– WhatsApp Network (via Baileys WebSocket protocol)
– Gemini AI API (HTTPS REST)
4. Supporting Infrastructure
– Developer’s machine or Docker host
– (Optional) Supabase if configured in lib/supabase.ts for any further storage
b. Relationships & Data Flows
1. User in browser → HTTP(S) → Express API endpoints (/api/…)
2. User triggers QRLogin → backend uses Baileys to generate QR → frontend polls /api/whatsapp/qr → displays QR
3. On QR scan, Baileys session established → WebSocket messages flow both ways (message delivery, incoming messages)
4. For real-time updates, backend pushes events via WebSocket or SSE to the client UI (inbox, logs)
5. Sending a message or media → frontend POST /api/whatsapp/send → controller → WhatsApp Service → Baileys → WhatsApp network
6. Incoming messages from WhatsApp → Baileys event handlers → ConversationHistory Service → JSON store → WebSocket push to frontend
7. Auto-reply: incoming message → controller/service triggers Gemini AI Service → external Gemini API → response → send back via WhatsApp Service
c. Architectural Patterns & Design Principles
- Layered architecture: HTTP routes → controllers → services → data store
- Single responsibility: each service handles a discrete concern (storage, messaging, AI)
- Real-time event propagation via WebSocket/SSE
- Environment-based configuration, secrets stored server-side only
d. Key Technologies & Frameworks
- Backend: Node.js, Express, Baileys, pnpm, JSON file storage
- Frontend: React, Vite, TailAdmin UI, TypeScript, WebSocket/SSE browsers APIs
- External: WhatsApp Web protocol, Gemini AI API

##### Final Advice
- Be granular: show REST endpoints, event channels, and storage interactions.
- Both synchronous (HTTP) and asynchronous (WebSocket/SSE) flows.
- Security boundaries (API key check, environment-only secrets).
- Baileys WebSocket connection as the bridge to WhatsApp.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Frontend Layer
    subgraph "Frontend Client"
        direction TB
        FE_Main["main.tsx"]:::frontend
        FE_App["App.tsx"]:::frontend

        subgraph "UI Modules"
            direction TB
            FE_QR["QRLogin"]:::frontend
            FE_Dashboard["Dashboard"]:::frontend
            FE_SendMsg["SendMessage"]:::frontend
            FE_SendMedia["SendMedia"]:::frontend
            FE_Inbox["InboxViewer"]:::frontend
            FE_Settings["Settings"]:::frontend
            FE_Logs["LogViewer"]:::frontend
        end

        subgraph "Client Services"
            direction TB
            FE_API["apiService"]:::frontend
            FE_WS["websocketService"]:::frontend
            FE_SSE["sseService"]:::frontend
            FE_LocalDb["localDb"]:::frontend
            FE_Auth["AuthContext"]:::frontend
        end

        FE_Main --> FE_App
        FE_App --> FE_QR
        FE_App --> FE_Dashboard
        FE_App --> FE_SendMsg
        FE_App --> FE_SendMedia
        FE_App --> FE_Inbox
        FE_App --> FE_Settings
        FE_App --> FE_Logs

        FE_QR --> FE_API
        FE_QR --> FE_WS
        FE_QR --> FE_LocalDb

        FE_Dashboard --> FE_API
        FE_Dashboard --> FE_WS

        FE_SendMsg --> FE_API
        FE_SendMedia --> FE_API
        FE_Inbox --> FE_SSE
        FE_Logs --> FE_SSE
        FE_Settings --> FE_API

    end

    %% Backend Layer
    subgraph "Backend Server"
        direction TB

        subgraph "API Layer"
            direction TB
            BE_Server["server.js"]:::backend
            BE_Routes_Index["routes/index.js"]:::backend
            BE_Routes_Contacts["routes/contacts.js"]:::backend
            BE_Middleware["auth.js"]:::backend
        end

        subgraph "Controllers"
            direction TB
            BE_WhatsappCtrl["whatsapp.js"]:::backend
            BE_ContactsCtrl["contacts.js"]:::backend
            BE_CategoriesCtrl["contactCategories.js"]:::backend
            BE_GeminiCtrl["gemini.js"]:::backend
        end

        subgraph "Domain Services"
            direction TB
            BE_WhatsApp["whatsapp.js"]:::backend
            BE_ConvHistorySvc["conversationHistory.js"]:::backend
            BE_GeminiSvc["gemini.js"]:::backend
            BE_WSService["websocket.js"]:::backend
            BE_LocalStorageSvc["localStorageService.js"]:::backend
        end

        subgraph "Data Layer"
            direction TB
            BE_Categories[(categories.json)]:::data
            BE_ConvHistoryData[(conversation_history.json)]:::data
        end

        BE_Server --> BE_Routes_Index
        BE_Routes_Index --> BE_WhatsappCtrl
        BE_Routes_Index --> BE_ContactsCtrl
        BE_Routes_Index --> BE_CategoriesCtrl
        BE_Routes_Index --> BE_GeminiCtrl
        BE_Routes_Index --> BE_Middleware

        BE_WhatsappCtrl --> BE_WhatsApp
        BE_GeminiCtrl --> BE_GeminiSvc
        BE_ContactsCtrl --> BE_LocalStorageSvc
        BE_CategoriesCtrl --> BE_LocalStorageSvc

        BE_ConvHistorySvc --> BE_ConvHistoryData
        BE_LocalStorageSvc --> BE_Categories

        BE_WhatsApp --> BE_LocalStorageSvc
        BE_WhatsappCtrl --> BE_ConvHistorySvc

        BE_WSService --> FE_WS
        BE_WSService --> FE_SSE

    end

    %% External Systems
    subgraph "External Systems"
        direction TB
        WhatsAppNetwork["WhatsApp Network"]:::external
        GeminiAPI["Gemini AI API"]:::external
    end

    %% Connections
    FE_API -->|"/api/* HTTP" | BE_Server
    FE_WS -->|"WebSocket"| BE_WSService
    FE_SSE -->|"SSE"| BE_WSService

    BE_WhatsApp -->|"Baileys WS"| WhatsAppNetwork
    BE_GeminiSvc -->|"HTTPS"| GeminiAPI

    %% Click Events - Frontend
    click FE_Main "https://github.com/paijoe29/wa-gemini/blob/main/src/main.tsx"
    click FE_App "https://github.com/paijoe29/wa-gemini/blob/main/src/App.tsx"
    click FE_QR "https://github.com/paijoe29/wa-gemini/blob/main/src/pages/WhatsApp/QRLogin.tsx"
    click FE_Dashboard "https://github.com/paijoe29/wa-gemini/blob/main/src/pages/WhatsApp/Dashboard.tsx"
    click FE_SendMsg "https://github.com/paijoe29/wa-gemini/blob/main/src/pages/WhatsApp/SendMessage.tsx"
    click FE_SendMedia "https://github.com/paijoe29/wa-gemini/blob/main/src/pages/WhatsApp/SendMedia.tsx"
    click FE_Inbox "https://github.com/paijoe29/wa-gemini/blob/main/src/pages/WhatsApp/Inbox.tsx"
    click FE_Settings "https://github.com/paijoe29/wa-gemini/blob/main/src/pages/WhatsApp/Settings.tsx"
    click FE_Logs "https://github.com/paijoe29/wa-gemini/blob/main/src/pages/WhatsApp/Logs.tsx"
    click FE_API "https://github.com/paijoe29/wa-gemini/blob/main/src/services/apiService.ts"
    click FE_WS "https://github.com/paijoe29/wa-gemini/blob/main/src/services/websocketService.ts"
    click FE_SSE "https://github.com/paijoe29/wa-gemini/blob/main/src/services/sseService.ts"
    click FE_LocalDb "https://github.com/paijoe29/wa-gemini/blob/main/src/lib/localDb.ts"
    click FE_Auth "https://github.com/paijoe29/wa-gemini/blob/main/src/context/AuthContext.tsx"

    %% Click Events - Backend
    click BE_Server "https://github.com/paijoe29/wa-gemini/blob/main/backend/server.js"
    click BE_Routes_Index "https://github.com/paijoe29/wa-gemini/blob/main/backend/routes/index.js"
    click BE_Routes_Contacts "https://github.com/paijoe29/wa-gemini/blob/main/backend/routes/contacts.js"
    click BE_Middleware "https://github.com/paijoe29/wa-gemini/blob/main/backend/middleware/auth.js"
    click BE_WhatsappCtrl "https://github.com/paijoe29/wa-gemini/blob/main/backend/controllers/whatsapp.js"
    click BE_ContactsCtrl "https://github.com/paijoe29/wa-gemini/blob/main/backend/controllers/contacts.js"
    click BE_CategoriesCtrl "https://github.com/paijoe29/wa-gemini/blob/main/backend/controllers/contactCategories.js"
    click BE_GeminiCtrl "https://github.com/paijoe29/wa-gemini/blob/main/backend/controllers/gemini.js"
    click BE_WhatsApp "https://github.com/paijoe29/wa-gemini/blob/main/backend/services/whatsapp.js"
    click BE_ConvHistorySvc "https://github.com/paijoe29/wa-gemini/blob/main/backend/services/conversationHistory.js"
    click BE_GeminiSvc "https://github.com/paijoe29/wa-gemini/blob/main/backend/services/gemini.js"
    click BE_WSService "https://github.com/paijoe29/wa-gemini/blob/main/backend/services/websocket.js"
    click BE_LocalStorageSvc "https://github.com/paijoe29/wa-gemini/blob/main/backend/services/localStorageService.js"
    click BE_Categories "https://github.com/paijoe29/wa-gemini/blob/main/backend/data/categories.json"
    click BE_ConvHistoryData "https://github.com/paijoe29/wa-gemini/blob/main/backend/data/conversation_history.json"
    click BE_Config "https://github.com/paijoe29/wa-gemini/blob/main/backend/config.js"
    click BE_Env "https://github.com/paijoe29/wa-gemini/blob/main/backend/.env.example"

    %% Styles
    classDef frontend fill:#D0E8FF,stroke:#333,stroke-width:1px
    classDef backend fill:#D0FFD8,stroke:#333,stroke-width:1px
    classDef data fill:#FFE0B2,stroke:#333,stroke-width:1px
    classDef external fill:#E0E0E0,stroke:#333,stroke-width:1px
```
