# aparna-adev/docsenseai architecture

> Architecture diagram of the GitHub repository aparna-adev/docsenseai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/aparna-adev/docsenseai
- Repository: https://github.com/aparna-adev/docsenseai

## Overview

#### Purpose and entry points
Simple RAG is a tenant-aware document retrieval and chat system. It exposes a FastAPI backend and a React/Vite client. Backend startup is `backend/app/main.py`; durable ingestion runs separately through `backend/app/worker.py`; vector backfills use `backend/app/reindex.py`. The browser entry is `frontend/src/main.tsx`, rendering the application in `frontend/src/App.tsx`.

#### API, identity, and tenancy
FastAPI routes partition the system into authentication, projects/collections, document lifecycle, ingestion, search, chat, and health operations. JWT-based authentication and account handling form the primary tenant boundary; request-scoped authorization must constrain both metadata and retrieval.
- `backend/app/auth.py`
- `backend/app/routes/auth.py`
- `backend/app/routes/projects.py`
- `backend/app/routes/documents.py`

#### Durable document-ingestion pipeline
The API accepts and validates uploads, stores metadata/files, and creates durable SQLite-backed ingestion jobs rather than performing extraction synchronously. The worker claims jobs idempotently, supports retries/cancellation/stale-lock recovery, then parses documents, extracts structured content, chunks it, embeds it, and upserts vectors. Versioning means retrieval should target current document versions.
- `backend/app/routes/upload.py`
- `backend/app/routes/ingestion.py`
- `backend/app/services/ingestion_jobs.py`
- `backend/app/services/document_loader.py`
- `backend/app/services/chunking.py`
- `backend/app/services/embeddings.py`

#### Content-format processing boundary
Ingestion has specialized paths for PDFs/layout, images with OCR/vision, spreadsheets/workbooks, structured CSV, folders, and ZIP archives. These normalize diverse sources into document content/chunks before they enter common embedding and vector-store stages.
- `backend/app/services/pdf_layout.py`
- `backend/app/services/image_processor/`
- `backend/app/services/workbook_analysis.py`
- `backend/app/services/structured_ingestion.py`
- `backend/app/services/zip_archives.py`

#### Retrieval and RAG answer pipeline
Search combines vector retrieval with keyword search, source filtering/selection, and access controls. Chat builds bounded conversation context, retrieves eligible sources, applies the RAG prompt, and calls the Groq LLM client; diagnostics expose decision and retrieval visibility. Citation/source extraction is a first-class response concern.
- `backend/app/routes/chat.py`
- `backend/app/services/rag_service.py`
- `backend/app/services/vector_search.py`
- `backend/app/services/keyword_search.py`
- `backend/app/services/source_selection.py`
- `backend/app/services/groq_client.py`

#### Persistence, infrastructure, and operations
SQLite is the durable system of record for users, documents, versions, and jobs; Qdrant holds retrieval vectors and is required for production readiness. Uploaded originals are stored under local backend data/storage abstractions. The service provides readiness and metrics endpoints; Qdrant can be run via Compose.
- `backend/app/database.py`
- `backend/app/services/vector_store.py`
- `backend/app/services/storage.py`
- `backend/app/routes/health.py`
- `docker-compose.yml`

#### Frontend integration
The React SPA communicates with the FastAPI base URL via a centralized API service. Its primary product surfaces are chat, document library/upload, account/settings, and dashboard views; shared application state is held in context.
- `frontend/src/services/api.ts`
- `frontend/src/context/AppContext.tsx`
- `frontend/src/components/ChatArea.tsx`
- `frontend/src/components/LibraryPage.tsx`
- `frontend/src/components/UploadDocumentsModal.tsx`

Main entry points: [`backend/app/main.py`](https://github.com/aparna-adev/docsenseai/blob/main/backend/app/main.py) (FastAPI server), [`backend/app/database.py`](https://github.com/aparna-adev/docsenseai/blob/main/backend/app/database.py) (SQLite), [`frontend/src/App.tsx`](https://github.com/aparna-adev/docsenseai/blob/main/frontend/src/App.tsx) (Application shell), [`backend/app/routes/upload.py`](https://github.com/aparna-adev/docsenseai/blob/main/backend/app/routes/upload.py) (Upload and ingestion API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["React client"]
  node_spa["React SPA<br/>Vite app<br/>[main.tsx]"]
  node_app["Application shell<br/>React UI<br/>[App.tsx]"]
  node_client_api["API client<br/>frontend service<br/>[api.ts]"]
  node_app_state["App state<br/>React context<br/>[AppContext.tsx]"]
end

subgraph group_api["Backend API"]
  node_fastapi["FastAPI server<br/>API runtime<br/>[main.py]"]
  node_identity["JWT identity<br/>auth boundary<br/>[auth.py]"]
  node_resource_routes["Project and document API<br/>routes<br/>[documents.py]"]
  node_upload_routes["Upload and ingestion API<br/>routes<br/>[upload.py]"]
  node_chat_routes["Chat API<br/>routes<br/>[chat.py]"]
end

subgraph group_ingestion["Durable ingestion"]
  node_ingestion_jobs["Job coordinator<br/>durable jobs<br/>[ingestion_jobs.py]"]
  node_worker["Ingestion worker<br/>worker runtime<br/>[worker.py]"]
  node_content_processing["Content processors<br/>format pipeline<br/>[document_loader.py]"]
  node_chunking["Chunking<br/>content stage<br/>[chunking.py]"]
  node_embeddings["Embeddings<br/>vector stage<br/>[embeddings.py]"]
end

subgraph group_retrieval["Retrieval and RAG"]
  node_rag_service["RAG orchestration<br/>answer pipeline<br/>[rag_service.py]"]
  node_retrieval["Eligible-source retrieval<br/>hybrid search<br/>[vector_search.py]"]
  node_groq["Groq LLM<br/>LLM client<br/>[groq_client.py]"]
end

subgraph group_data["Persistence and infrastructure"]
  node_sqlite[("SQLite<br/>system of record<br/>[database.py]")]
  node_storage[("Original file storage<br/>storage service<br/>[storage.py]")]
  node_qdrant[("Qdrant<br/>vector database<br/>[vector_store.py]")]
  node_reindex["Vector reindexer<br/>maintenance runtime<br/>[reindex.py]"]
end

node_spa -->|"renders"| node_app
node_app -->|"uses"| node_client_api
node_app -->|"shares state"| node_app_state
node_client_api -->|"HTTP API"| node_fastapi
node_fastapi -->|"authenticates"| node_identity
node_fastapi -->|"serves"| node_resource_routes
node_fastapi -->|"serves"| node_upload_routes
node_fastapi -->|"serves"| node_chat_routes
node_identity -->|"accounts and tenancy"| node_sqlite
node_resource_routes -->|"document metadata"| node_sqlite
node_upload_routes -->|"stores originals"| node_storage
node_upload_routes -->|"enqueues"| node_ingestion_jobs
node_ingestion_jobs -->|"persists jobs"| node_sqlite
node_worker -->|"claims jobs"| node_ingestion_jobs
node_worker -->|"processes"| node_content_processing
node_content_processing -->|"normalized content"| node_chunking
node_chunking -->|"chunks"| node_embeddings
node_embeddings -->|"upserts vectors"| node_qdrant
node_chat_routes -->|"answers"| node_rag_service
node_rag_service -->|"retrieves sources"| node_retrieval
node_retrieval -->|"vector search"| node_qdrant
node_retrieval -->|"keywords and access metadata"| node_sqlite
node_rag_service -->|"RAG prompt"| node_groq
node_reindex -->|"backfills vectors"| node_qdrant

click node_spa "https://github.com/aparna-adev/docsenseai/blob/main/frontend/src/main.tsx"
click node_app "https://github.com/aparna-adev/docsenseai/blob/main/frontend/src/App.tsx"
click node_client_api "https://github.com/aparna-adev/docsenseai/blob/main/frontend/src/services/api.ts"
click node_app_state "https://github.com/aparna-adev/docsenseai/blob/main/frontend/src/context/AppContext.tsx"
click node_fastapi "https://github.com/aparna-adev/docsenseai/blob/main/backend/app/main.py"
click node_identity "https://github.com/aparna-adev/docsenseai/blob/main/backend/app/auth.py"
click node_resource_routes "https://github.com/aparna-adev/docsenseai/blob/main/backend/app/routes/documents.py"
click node_upload_routes "https://github.com/aparna-adev/docsenseai/blob/main/backend/app/routes/upload.py"
click node_chat_routes "https://github.com/aparna-adev/docsenseai/blob/main/backend/app/routes/chat.py"
click node_ingestion_jobs "https://github.com/aparna-adev/docsenseai/blob/main/backend/app/services/ingestion_jobs.py"
click node_worker "https://github.com/aparna-adev/docsenseai/blob/main/backend/app/worker.py"
click node_content_processing "https://github.com/aparna-adev/docsenseai/blob/main/backend/app/services/document_loader.py"
click node_chunking "https://github.com/aparna-adev/docsenseai/blob/main/backend/app/services/chunking.py"
click node_embeddings "https://github.com/aparna-adev/docsenseai/blob/main/backend/app/services/embeddings.py"
click node_rag_service "https://github.com/aparna-adev/docsenseai/blob/main/backend/app/services/rag_service.py"
click node_retrieval "https://github.com/aparna-adev/docsenseai/blob/main/backend/app/services/vector_search.py"
click node_groq "https://github.com/aparna-adev/docsenseai/blob/main/backend/app/services/groq_client.py"
click node_sqlite "https://github.com/aparna-adev/docsenseai/blob/main/backend/app/database.py"
click node_storage "https://github.com/aparna-adev/docsenseai/blob/main/backend/app/services/storage.py"
click node_qdrant "https://github.com/aparna-adev/docsenseai/blob/main/backend/app/services/vector_store.py"
click node_reindex "https://github.com/aparna-adev/docsenseai/blob/main/backend/app/reindex.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_spa,node_app,node_client_api,node_app_state toneBlue
class node_fastapi,node_identity,node_resource_routes,node_upload_routes,node_chat_routes toneAmber
class node_ingestion_jobs,node_worker,node_content_processing,node_chunking,node_embeddings toneMint
class node_rag_service,node_retrieval,node_groq toneRose
class node_sqlite,node_storage,node_qdrant,node_reindex toneIndigo
```

## Components

### React client

- [React SPA](https://github.com/aparna-adev/docsenseai/blob/main/frontend/src/main.tsx): Vite app · `frontend/src/main.tsx`
- [Application shell](https://github.com/aparna-adev/docsenseai/blob/main/frontend/src/App.tsx): React UI · Provides chat, library, upload, dashboard, and account surfaces. · `frontend/src/App.tsx`
- [API client](https://github.com/aparna-adev/docsenseai/blob/main/frontend/src/services/api.ts): frontend service · `frontend/src/services/api.ts`
- [App state](https://github.com/aparna-adev/docsenseai/blob/main/frontend/src/context/AppContext.tsx): React context · `frontend/src/context/AppContext.tsx`

### Backend API

- [FastAPI server](https://github.com/aparna-adev/docsenseai/blob/main/backend/app/main.py): API runtime · `backend/app/main.py`
- [JWT identity](https://github.com/aparna-adev/docsenseai/blob/main/backend/app/auth.py): auth boundary · Establishes account and tenant authorization for requests. · `backend/app/auth.py`
- [Project and document API](https://github.com/aparna-adev/docsenseai/blob/main/backend/app/routes/documents.py): routes · `backend/app/routes/documents.py`
- [Upload and ingestion API](https://github.com/aparna-adev/docsenseai/blob/main/backend/app/routes/upload.py): routes · Validates uploads and creates durable ingestion jobs. · `backend/app/routes/upload.py`
- [Chat API](https://github.com/aparna-adev/docsenseai/blob/main/backend/app/routes/chat.py): routes · `backend/app/routes/chat.py`

### Durable ingestion

- [Job coordinator](https://github.com/aparna-adev/docsenseai/blob/main/backend/app/services/ingestion_jobs.py): durable jobs · Claims jobs idempotently and handles retry, cancellation, and stale locks. · `backend/app/services/ingestion_jobs.py`
- [Ingestion worker](https://github.com/aparna-adev/docsenseai/blob/main/backend/app/worker.py): worker runtime · `backend/app/worker.py`
- [Content processors](https://github.com/aparna-adev/docsenseai/blob/main/backend/app/services/document_loader.py): format pipeline · Normalizes PDF, image/OCR, workbook, CSV, folder, and ZIP content. · `backend/app/services/document_loader.py`
- [Chunking](https://github.com/aparna-adev/docsenseai/blob/main/backend/app/services/chunking.py): content stage · `backend/app/services/chunking.py`
- [Embeddings](https://github.com/aparna-adev/docsenseai/blob/main/backend/app/services/embeddings.py): vector stage · `backend/app/services/embeddings.py`

### Retrieval and RAG

- [RAG orchestration](https://github.com/aparna-adev/docsenseai/blob/main/backend/app/services/rag_service.py): answer pipeline · Builds bounded context, prompts the LLM, and returns citations. · `backend/app/services/rag_service.py`
- [Eligible-source retrieval](https://github.com/aparna-adev/docsenseai/blob/main/backend/app/services/vector_search.py): hybrid search · Combines vector search, keyword search, selection, and access constraints. · `backend/app/services/vector_search.py`
- [Groq LLM](https://github.com/aparna-adev/docsenseai/blob/main/backend/app/services/groq_client.py): LLM client · `backend/app/services/groq_client.py`

### Persistence and infrastructure

- [SQLite](https://github.com/aparna-adev/docsenseai/blob/main/backend/app/database.py): system of record · Stores users, documents, versions, and ingestion jobs. · `backend/app/database.py`
- [Original file storage](https://github.com/aparna-adev/docsenseai/blob/main/backend/app/services/storage.py): storage service · `backend/app/services/storage.py`
- [Qdrant](https://github.com/aparna-adev/docsenseai/blob/main/backend/app/services/vector_store.py): vector database · Holds retrieval vectors for current document versions. · `backend/app/services/vector_store.py`
- [Vector reindexer](https://github.com/aparna-adev/docsenseai/blob/main/backend/app/reindex.py): maintenance runtime · Backfills vectors independently of ingestion. · `backend/app/reindex.py`

## Connections

- React SPA → Application shell: renders
- Application shell → API client: uses
- Application shell → App state: shares state
- API client → FastAPI server: HTTP API
- FastAPI server → JWT identity: authenticates
- FastAPI server → Project and document API: serves
- FastAPI server → Upload and ingestion API: serves
- FastAPI server → Chat API: serves
- JWT identity → SQLite: accounts and tenancy
- Project and document API → SQLite: document metadata
- Upload and ingestion API → Original file storage: stores originals
- Upload and ingestion API → Job coordinator: enqueues
- Job coordinator → SQLite: persists jobs
- Ingestion worker → Job coordinator: claims jobs
- Ingestion worker → Content processors: processes
- Content processors → Chunking: normalized content
- Chunking → Embeddings: chunks
- Embeddings → Qdrant: upserts vectors
- Chat API → RAG orchestration: answers
- RAG orchestration → Eligible-source retrieval: retrieves sources
- Eligible-source retrieval → Qdrant: vector search
- Eligible-source retrieval → SQLite: keywords and access metadata
- RAG orchestration → Groq LLM: RAG prompt
- Vector reindexer → Qdrant: backfills vectors
