# jsv4/opencontracts architecture

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

- Interactive diagram: https://gitdiagram.com/jsv4/opencontracts
- Repository: https://github.com/jsv4/opencontracts

## Overview

This project is a full‐stack enterprise document analytics platform built around analyzing, annotating, and extracting data from documents (primarily PDFs and text formats). It uses a Django/Python backend that is integrated with a highly modular, pluggable document processing pipeline (parsers, embedders, thumbnailers) and a React/TypeScript frontend. There are several supporting components such as Docker configuration for local and production deployments, CI/CD pipelines, and integrations with vector databases (pgvector), GraphQL, and LlamaIndex for data extraction.

##### Main Components
- Frontend Application – Built with React (located under the “frontend” directory). This component includes pages and UI widgets for document management, annotation, data grid displays, and querying.
- Backend API and Web Application – Mainly built with Django (see “opencontractserver”, “config” directories, and “manage.py”). It handles business logic, authentication, document and annotation storage, and rendering of GraphQL endpoints.
- Document Processing Pipelines – A modular pipeline system (documented in the “docs/pipelines” folder) including custom parsers (e.g., Docling, NLM-Ingest), embedders, and thumbnail generators. These components are key for extracting text, generating vector embeddings, and creating document previews.
- Celery Services – Background workers for asynchronous tasks related to document parsing, analysis, and data extraction. These can be shown as separate worker components in the backend.
- Database Layer – Postgres is used along with pgvector for handling both structured metadata and vector embeddings. The project contains SQL initialization and maintenance scripts (see “compose/production/postgres”).
- Reverse Proxy and Deployment Components – Docker Compose configurations for local and production setup including reverse proxy setup (via Traefik) and various container orchestration files.
- External Integrations – LlamaIndex integration for enhanced vector search and data extraction workflows, as well as integration with external analyzers and annotation interfaces.

##### Relationships and Interactions
- The Frontend communicates with the Backend via REST APIs and GraphQL endpoints.
- The Backend processes incoming document uploads, kicks off Celery tasks for document processing (parsing, embedding, thumbnailing), and stores results in the Postgres database.
- The Document Processing Pipeline is triggered from the Backend and can be extended via a plug-in architecture.
- The system supports real-time notifications and websockets (see “config/websocket”) to keep the frontend updated with document annotation or analysis statuses.
- External services (e.g., LlamaIndex, authentication providers such as Auth0) are integrated via API calls and middleware.

##### Architectural Patterns and Technologies
- MVC/MVT Pattern – The Django application adheres to the Model-View-Template pattern.
- Microservices/Plugin Architecture – The pluggable document processing pipelines allow for easy extension of parsers, embedders, and thumbnail generators.
- Asynchronous Task Processing – Celery-based background processing of heavy tasks ensures responsiveness.
- Containerized Deployment – Docker Compose configuration for both production and local environments, including separate services for Django, Celery, Postgres, and Traefik.
- Modern Frontend Architecture – The React application with TypeScript, state management (using context and hooks), and integration with GraphQL.

##### Details
- Clear dependency paths and add notes regarding the pluggable architecture design for custom pipelines.

## Diagram (Mermaid)

```mermaid
flowchart TD

    %% User Interface Layer
    subgraph "User Interface"
        FE["Frontend (React/TypeScript)"]:::frontend
    end

    %% Backend Services Layer
    subgraph "Backend Services"
        BE["Backend (Django/GraphQL)"]:::backend
        BC["Django Config"]:::backend
        BOS["OpenContractServer"]:::backend

        subgraph "Celery Workers"
            CW1["Celery App"]:::celery
            CW2["Local Celery Config"]:::celery
            CW3["Production Celery Config"]:::celery
        end

        subgraph "Document Processing Pipeline"
            DP1["Docs Pipelines"]:::pipeline
            DP2["Server Pipeline"]:::pipeline
        end
    end

    %% Infrastructure Layer
    subgraph "Infrastructure"
        DB["Postgres Database (pgvector)"]:::database
        subgraph "Deployment"
            RP["Reverse Proxy (Traefik)"]:::infra
            DC1["Local Compose"]:::deployment
            DC2["Production Compose"]:::deployment
        end
        subgraph "External Services"
            ES1["LlamaIndex Integration"]:::external
            ES2["Auth0 Integration"]:::external
            ES3["Websocket (Real-time)"]:::external
        end
    end

    %% Interactions
    FE -->|"REST/GraphQL"| BE
    RP -->|"Routes-to"| FE
    RP -->|"Routes-to"| BE
    DC1 -->|"Orchestrates"| RP
    DC2 -->|"Orchestrates"| RP

    BE -->|"Triggers-Task"| CW1
    BE -->|"Includes"| BC
    BE -->|"Includes"| BOS
    CW1 -->|"Processes-Document"| DP1
    DP1 -->|"Saves-Results"| DB
    BE -->|"Reads/Writes"| DB
    BE -->|"Calls"| ES1
    BE -->|"Calls"| ES2
    BE -->|"Pushes-Updates"| ES3

    %% Click Events
    click FE "https://github.com/jsv4/opencontracts/tree/main/frontend/"
    click BE "https://github.com/jsv4/opencontracts/blob/main/manage.py"
    click BC "https://github.com/jsv4/opencontracts/tree/main/config/"
    click BOS "https://github.com/jsv4/opencontracts/tree/main/opencontractserver/"
    click DP1 "https://github.com/jsv4/opencontracts/tree/main/docs/pipelines/"
    click DP2 "https://github.com/jsv4/opencontracts/tree/main/opencontractserver/pipeline/"
    click CW1 "https://github.com/jsv4/opencontracts/blob/main/config/celery_app.py"
    click CW2 "https://github.com/jsv4/opencontracts/tree/main/compose/local/django/celery/"
    click CW3 "https://github.com/jsv4/opencontracts/tree/main/compose/production/django/celery/"
    click DB "https://github.com/jsv4/opencontracts/tree/main/compose/production/postgres/"
    click RP "https://github.com/jsv4/opencontracts/tree/main/compose/production/traefik/"
    click DC1 "https://github.com/jsv4/opencontracts/tree/main/compose/local/"
    click DC2 "https://github.com/jsv4/opencontracts/tree/main/compose/production/"
    click ES1 "https://github.com/jsv4/opencontracts/blob/main/requirements/processors/llama_index.txt"
    click ES2 "https://github.com/jsv4/opencontracts/tree/main/config/graphql_auth0_auth/"
    click ES3 "https://github.com/jsv4/opencontracts/tree/main/config/websocket/"

    %% Styles
    classDef frontend fill:#FFD700,stroke:#333,stroke-width:2px;
    classDef backend fill:#87CEEB,stroke:#333,stroke-width:2px;
    classDef celery fill:#FFB6C1,stroke:#333,stroke-width:2px;
    classDef pipeline fill:#90EE90,stroke:#333,stroke-width:2px;
    classDef database fill:#F08080,stroke:#333,stroke-width:2px;
    classDef infra fill:#DDA0DD,stroke:#333,stroke-width:2px;
    classDef deployment fill:#ADD8E6,stroke:#333,stroke-width:2px;
    classDef external fill:#FFA07A,stroke:#333,stroke-width:2px;
```
