# hsuehdev/definiscope architecture

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

- Interactive diagram: https://gitdiagram.com/hsuehdev/definiscope
- Repository: https://github.com/hsuehdev/definiscope

## Overview

##### Project type and purpose
- This is a full-stack, document-analysis and chat platform.
- Users upload documents (PDF, etc.), the system splits and processes them, stores file data, and provides a chat interface with real-time WebSocket messages and AI-based Q&A on the content.

##### Key architectural building blocks
By inspecting the file tree and README we see these main containers and services:
- Frontend (React + Vite)
- Reverse proxy (Nginx)
- Backend API (FastAPI)
- Celery workers & Beat scheduler
- Split-Sentences microservice (FastAPI)
- Message broker & cache (Redis)
- Object storage (MinIO)
- Relational database (PostgreSQL master-slave)

- Browser (React app)
- Nginx proxy
- Frontend dev server (in local mode) or built static files served by Nginx
- FastAPI backend
- Celery worker(s)
- Celery Beat scheduler
- Split-Sentences service
- Redis (broker & cache)
- MinIO (S3-compatible object store)
- PostgreSQL primary & replica

b. Component-level view inside Backend
- API endpoints (/auth, /chat, /files, WebSocket)
- Core modules (security, JWT, config, websocket manager)
- Database layer (SQLAlchemy models, Alembic migrations)
- Services (file_service, n8n integrations, storage client)
- Tasks (chat_processing, file_processing, scheduled jobs)

##### Relationships and data flows
- Client ⇄ Nginx ⇄ Frontend assets & API/WebSocket
- Frontend → WebSocket → Backend (real-time chat & progress updates)
- Backend → PostgreSQL (CRUD & query storage)
- Backend → MinIO (upload/download file chunks)
- Backend → Redis (publish/subscribe for WebSocket events & Celery broker)
- Celery workers ← Redis broker → backend tasks (file splitting, chat processing)
- Celery Beat → Redis → scheduled tasks
- Backend or workers → Split-Sentences service (over HTTP) → returns sentence data

##### Architectural patterns and principles
- Microservices: separate “split_sentences” service
- Event-driven: Celery + Redis for async file/chat jobs
- MVC-style layering: routers, services, models, schemas
- Horizontal scaling: multiple Celery workers, database read replica, MinIO cluster
- Single entry point: Nginx handles HTTP & WS proxying

##### Technologies & frameworks
- React, TypeScript, Vite, Playwright, Vitest
- FastAPI, Pydantic, Alembic, SQLAlchemy
- Celery, Redis, Beat
- PostgreSQL (primary/replica), MinIO
- Nginx
- Docker & Docker-Compose

- Browser → Nginx → Frontend assets
- Browser → Nginx → Backend HTTP/WS
- Backend ↔ Postgres
- Backend ↔ MinIO
- Backend & Beat ↔ Redis (broker & pub/sub) ↔ Celery
- Backend ↔ Split-Sentences Service (HTTP)
- Labels: protocols, key frameworks, directional flows.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client Layer"
        Browser["Browser (React + Vite)"]:::ui
    end
    click Browser "https://github.com/hsuehdev/definiscope/tree/main/frontend/"

    %% Proxy & Frontend
    subgraph "Proxy Layer"
        Nginx["Nginx Reverse Proxy"]:::proxy
        subgraph "Frontend Server"
            FrontendDev["Vite Dev Server"]:::frontend
            FrontendStatic["Built Static Files"]:::frontend
        end
    end
    click Nginx "https://github.com/hsuehdev/definiscope/tree/main/nginx/"
    click FrontendDev "https://github.com/hsuehdev/definiscope/tree/main/frontend/"
    click FrontendStatic "https://github.com/hsuehdev/definiscope/tree/main/frontend/"

    %% Backend Services
    subgraph "Backend Services"
        Backend["FastAPI Backend"]:::api
        subgraph "API Endpoints"
            Auth["/auth"]:::api
            Chat["/chat"]:::api
            Files["/files"]:::api
            WS["/ws (WebSocket)"]:::api
        end
        subgraph "WebSocket"
            WSManager["Manager"]:::api
            WSRouter["Router"]:::api
        end
        subgraph "Core"
            Config["Config"]:::core
            Security["Security"]:::core
        end
        subgraph "Services"
            FileSvc["file_service"]:::service
            N8N["n8n_client"]:::service
            StorageCl["storage_client"]:::service
        end
        subgraph "Tasks"
            ChatTask["chat_processing"]:::task
            FileTask["file_processing"]:::task
            Scheduled["scheduled jobs"]:::task
        end
        subgraph "Data Layer"
            Models["SQLAlchemy Models"]:::db
            Schemas["Pydantic Schemas"]:::db
            DBSession["DB Session"]:::db
            Alembic["Alembic Migrations"]:::db
        end
    end
    click Backend "https://github.com/hsuehdev/definiscope/blob/main/backend/app/main.py"
    click Auth "https://github.com/hsuehdev/definiscope/blob/main/backend/app/api/endpoints/auth.py"
    click Chat "https://github.com/hsuehdev/definiscope/blob/main/backend/app/api/endpoints/chat.py"
    click Files "https://github.com/hsuehdev/definiscope/blob/main/backend/app/api/endpoints/files.py"
    click WS "https://github.com/hsuehdev/definiscope/blob/main/backend/app/api/endpoints/ws.py"
    click WSManager "https://github.com/hsuehdev/definiscope/blob/main/backend/app/api/websockets/manager.py"
    click WSRouter "https://github.com/hsuehdev/definiscope/blob/main/backend/app/api/websockets/router.py"
    click Config "https://github.com/hsuehdev/definiscope/blob/main/backend/app/core/config.py"
    click Security "https://github.com/hsuehdev/definiscope/blob/main/backend/app/core/security.py"
    click FileSvc "https://github.com/hsuehdev/definiscope/blob/main/backend/app/services/file_service.py"
    click N8N "https://github.com/hsuehdev/definiscope/blob/main/backend/app/services/n8n_client.py"
    click StorageCl "https://github.com/hsuehdev/definiscope/blob/main/backend/app/services/storage_client.py"
    click ChatTask "https://github.com/hsuehdev/definiscope/blob/main/backend/app/tasks/chat_processing.py"
    click FileTask "https://github.com/hsuehdev/definiscope/blob/main/backend/app/tasks/file_processing.py"
    click Scheduled "https://github.com/hsuehdev/definiscope/tree/main/backend/app/tasks/scheduled/"
    click Models "https://github.com/hsuehdev/definiscope/tree/main/backend/app/models/"
    click Schemas "https://github.com/hsuehdev/definiscope/tree/main/backend/app/schemas/"
    click DBSession "https://github.com/hsuehdev/definiscope/blob/main/backend/app/db/session.py"
    click Alembic "https://github.com/hsuehdev/definiscope/tree/main/backend/alembic/"

    %% Async Processing
    subgraph "Async Processing"
        Worker["Celery Workers"]:::worker
        Beat["Celery Beat Scheduler"]:::worker
    end
    click Worker "https://github.com/hsuehdev/definiscope/blob/main/backend/app/worker.py"
    click Worker "https://github.com/hsuehdev/definiscope/blob/main/backend/Dockerfile.worker"
    click Beat "https://github.com/hsuehdev/definiscope/blob/main/backend/app/beat.py"
    click Beat "https://github.com/hsuehdev/definiscope/blob/main/backend/Dockerfile.beat"

    %% Split-Sentences Microservice
    Split["Split-Sentences Service"]:::micro
    click Split "https://github.com/hsuehdev/definiscope/blob/main/split_sentences/app/main.py"
    click Split "https://github.com/hsuehdev/definiscope/blob/main/split_sentences/app/services/pdf_processor.py"

    %% Datastores
    subgraph "Datastores"
        Redis["Redis (Broker & Cache)"]:::storage
        MinIO["MinIO (Object Storage)"]:::storage
        PostgresPrimary["PostgreSQL Primary"]:::storage
        PostgresReplica["PostgreSQL Replica"]:::storage
    end
    click Redis "https://github.com/hsuehdev/definiscope/blob/main/backend/app/utils/redis_publisher.py"
    click MinIO "https://github.com/hsuehdev/definiscope/blob/main/backend/app/core/minio_client.py"
    click MinIO "https://github.com/hsuehdev/definiscope/blob/main/backend/app/core/minio_client_improvements.py"
    click PostgresPrimary "https://github.com/hsuehdev/definiscope/tree/main/database/postgres/"
    click PostgresReplica "https://github.com/hsuehdev/definiscope/tree/main/database/postgres/"

    %% Connections
    Browser -->|HTTP| Nginx
    Browser -->|WS| Nginx
    Nginx -->|serves static| FrontendStatic
    Nginx -->|HTTP/WS proxy| Backend
    FrontendDev -->|HTTP/JSON| Backend

    Backend -->|HTTP/JSON| Split
    Backend -->|AMQP via Redis| Worker
    Beat -->|AMQP via Redis| Worker
    Worker -->|HTTP/JSON| Split
    Backend -->|pub/sub| Redis
    Beat -->|pub/sub| Redis

    Backend -->|CRUD| PostgresPrimary
    Backend -->|Read-Replica| PostgresReplica
    Backend -->|S3 API| MinIO
    Worker -->|S3 API| MinIO

    %% Styles
    classDef ui fill:#D6E9C6,stroke:#3C763D
    classDef proxy fill:#FCF8E3,stroke:#8A6D3B
    classDef frontend fill:#D9EDF7,stroke:#31708F
    classDef api fill:#BEE5EB,stroke:#0C5460
    classDef core fill:#F2DEDE,stroke:#A94442
    classDef service fill:#E2E2E2,stroke:#666666
    classDef task fill:#F5F5DC,stroke:#B2BEB5
    classDef db fill:#F0F8FF,stroke:#4682B4
    classDef worker fill:#FFF0F5,stroke:#DB7093
    classDef micro fill:#FFF5EE,stroke:#CD5C5C
    classDef storage fill:#F5F5F5,stroke:#A9A9A9
```
