# mihirmaru22/global_mind architecture

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

- Interactive diagram: https://gitdiagram.com/mihirmaru22/global_mind
- Repository: https://github.com/mihirmaru22/global_mind

## Overview

#### Purpose and entry points
GlobleMind/LocalMind is a FastAPI-backed RAG system for ingesting enterprise documents, performing hybrid retrieval, and streaming cited answers. Primary runtime entry point: `src/main.py`; command-line entry point: `src/cli.py`. The built React SPA is served by the backend from `frontend/`; editable UI source is under `LocalMind_UI/src/main.jsx`.

#### API and interaction boundary
FastAPI separates document ingestion, query execution, and UI/state endpoints:
- `src/api/upload.py` accepts batch uploads and emits ingestion progress through SSE.
- `src/api/query.py` exposes RAG queries with metadata filters.
- `src/api/ui.py` supports chat CRUD, persisted settings, document listing, provider selection, and UI-oriented streaming.
The browser communicates through `LocalMind_UI/src/services/api.js`; client-wide chat/session state is held in `LocalMind_UI/src/store/store.js`.

#### Ingestion orchestration and identity
`src/pipeline/ingestion.py` coordinates the sequential stages and should only commit successful documents. `src/core/ingestion_registry.py` establishes SHA-256 content-addressed identity, preventing duplicate processing while allowing same-named but different files. `src/pipeline/folder_ingestion.py` is the boundary for inbox scanning and concurrent folder ingestion. Uploaded/path-derived files are protected by `src/core/paths.py`.

#### Document transformation pipeline
The ingestion pipeline is decomposed into atomic stages:
- Detect/classify/parse: `src/stages/s01_file_detection.py`, `src/stages/s02_classification.py`, `src/stages/s03_parsing.py`.
- Enrich difficult content through OCR, layout, tables, and visual descriptions: `src/stages/s04_ocr.py`, `src/stages/s05_layout.py`, `src/stages/s06_tables.py`, `src/stages/s07_s08_visuals.py`.
- Build indexable records through chunking, embeddings, and vector commit: `src/stages/s09_chunking.py`, `src/stages/s10_embeddings.py`, `src/stages/s11_vector_store.py`.
Typed cross-stage contracts reside in `src/models/schemas.py`; extraction quality is gated by `src/core/confidence.py`.

#### Query, retrieval, and generation
`src/pipeline/query.py` controls the answer path: embed query, hybrid dense/sparse retrieval, rerank, context construction, and streaming generation. Retrieval/generation behavior is consolidated in `src/stages/s12_s13_s14_retrieval.py`; Jina supplies embeddings and cross-encoder reranking, while Qdrant provides dense+sparse vector storage and Reciprocal Rank Fusion. Answers stream over SSE and are rendered with Markdown, citations, thinking traces, and diagrams in `LocalMind_UI/src/components/Message.jsx`, `LocalMind_UI/src/components/ThinkingTrace.jsx`, and `LocalMind_UI/src/components/MermaidDiagram.jsx`.

#### Provider resiliency and configuration
`src/core/provider_client.py` implements provider routing across Gemini, Groq, NVIDIA NIM, OpenRouter, and Jina-dependent capabilities. It applies task-specific chains defined in `config/providers.yaml`, optional session soft-pinning, and fallback on provider failures. `src/core/rate_limiter.py` tracks free-tier RPM/RPD budgets and backoff; `src/core/config.py` binds environment configuration and operational limits.

#### Structured-data and persistence boundaries
Optional text-to-SQL is separate from vector RAG: `src/stages/s12b_sql_retrieval.py` generates and validates read-only SQL, while `src/core/db_client.py` executes against SQLite or MySQL and `src/core/sql_dialects.py` defines engine-specific behavior. Application/UI state uses JSON-backed storage via `src/core/state.py`, while document vectors live in Qdrant; the local SQLite database is only for structured-data retrieval. Deployment targets Python 3.11+, FastAPI, React/Vite, Docker (`Dockerfile`), and Render (`render.yaml`).

Main entry points: [`LocalMind_UI/src/services/api.js`](https://github.com/mihirmaru22/global_mind/blob/main/LocalMind_UI/src/services/api.js) (API client), [`src/pipeline/query.py`](https://github.com/mihirmaru22/global_mind/blob/main/src/pipeline/query.py) (Query pipeline), [`src/stages/s01_file_detection.py`](https://github.com/mihirmaru22/global_mind/blob/main/src/stages/s01_file_detection.py) (Transformation stages), [`src/api/ui.py`](https://github.com/mihirmaru22/global_mind/blob/main/src/api/ui.py) (UI/state API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Browser UI"]
  node_spa["React SPA<br/>frontend"]
  node_client_api["API client<br/>frontend service<br/>[api.js]"]
  node_client_state["Chat/session state<br/>frontend store<br/>[store.js]"]
  node_answer_rendering["Answer rendering<br/>UI components<br/>[Message.jsx]"]
end

subgraph group_api["FastAPI boundary"]
  node_app{{"FastAPI runtime<br/>application entry<br/>[main.py]"}}
  node_upload_api["Upload API<br/>SSE endpoint<br/>[upload.py]"]
  node_query_api["Query API<br/>query endpoint<br/>[query.py]"]
  node_ui_api["UI/state API<br/>UI endpoint<br/>[ui.py]"]
end

subgraph group_ingestion["Document ingestion"]
  node_ingestion{{"Ingestion orchestrator<br/>pipeline<br/>[ingestion.py]"}}
  node_identity["Document identity<br/>content registry"]
  node_transform["Transformation stages<br/>pipeline stages"]
  node_contracts["Stage contracts and quality<br/>typed schemas<br/>[schemas.py]"]
  node_folder_ingestion["Folder ingestion<br/>inbox boundary"]
end

subgraph group_rag["Retrieval and generation"]
  node_query_pipeline{{"Query pipeline<br/>RAG orchestrator<br/>[query.py]"}}
  node_retrieval["Hybrid retrieval and generation<br/>RAG stages"]
  node_sql_retrieval["Text-to-SQL<br/>optional retrieval"]
end

subgraph group_platform["Providers and storage"]
  node_providers["Provider router<br/>resilient LLM client<br/>[provider_client.py]"]
  node_qdrant[("Qdrant<br/>vector database")]
  node_structured_db[("SQLite / MySQL<br/>structured database<br/>[db_client.py]")]
  node_state[("JSON application state<br/>state storage<br/>[state.py]")]
end

node_spa -->|"uses"| node_client_api
node_client_state -->|"drives requests"| node_client_api
node_client_api -->|"uploads"| node_upload_api
node_client_api -->|"queries"| node_query_api
node_client_api -->|"chat and settings"| node_ui_api
node_query_api -->|"RAG request"| node_query_pipeline
node_ui_api -->|"UI streaming"| node_query_pipeline
node_query_pipeline -->|"retrieve and generate"| node_retrieval
node_retrieval -->|"embeddings, reranking, LLM"| node_providers
node_retrieval -->|"hybrid search"| node_qdrant
node_query_pipeline -.->|"optional structured query"| node_sql_retrieval
node_sql_retrieval -->|"read-only SQL"| node_structured_db
node_upload_api -->|"documents and progress"| node_ingestion
node_folder_ingestion -->|"local documents"| node_ingestion
node_ingestion -->|"deduplicate"| node_identity
node_ingestion -->|"process accepted files"| node_transform
node_transform -->|"validated records"| node_contracts
node_transform -->|"enrichment and embeddings"| node_providers
node_transform -->|"commit vectors"| node_qdrant
node_ui_api -->|"persist UI state"| node_state
node_query_pipeline -->|"SSE answer stream"| node_answer_rendering
node_app -->|"mounts"| node_upload_api
node_app -->|"mounts"| node_query_api
node_app -->|"mounts"| node_ui_api

click node_client_api "https://github.com/mihirmaru22/global_mind/blob/main/LocalMind_UI/src/services/api.js"
click node_client_state "https://github.com/mihirmaru22/global_mind/blob/main/LocalMind_UI/src/store/store.js"
click node_answer_rendering "https://github.com/mihirmaru22/global_mind/blob/main/LocalMind_UI/src/components/Message.jsx"
click node_app "https://github.com/mihirmaru22/global_mind/blob/main/src/main.py"
click node_upload_api "https://github.com/mihirmaru22/global_mind/blob/main/src/api/upload.py"
click node_query_api "https://github.com/mihirmaru22/global_mind/blob/main/src/api/query.py"
click node_ui_api "https://github.com/mihirmaru22/global_mind/blob/main/src/api/ui.py"
click node_ingestion "https://github.com/mihirmaru22/global_mind/blob/main/src/pipeline/ingestion.py"
click node_identity "https://github.com/mihirmaru22/global_mind/blob/main/src/core/ingestion_registry.py"
click node_transform "https://github.com/mihirmaru22/global_mind/blob/main/src/stages/s01_file_detection.py"
click node_contracts "https://github.com/mihirmaru22/global_mind/blob/main/src/models/schemas.py"
click node_folder_ingestion "https://github.com/mihirmaru22/global_mind/blob/main/src/pipeline/folder_ingestion.py"
click node_query_pipeline "https://github.com/mihirmaru22/global_mind/blob/main/src/pipeline/query.py"
click node_retrieval "https://github.com/mihirmaru22/global_mind/blob/main/src/stages/s12_s13_s14_retrieval.py"
click node_sql_retrieval "https://github.com/mihirmaru22/global_mind/blob/main/src/stages/s12b_sql_retrieval.py"
click node_providers "https://github.com/mihirmaru22/global_mind/blob/main/src/core/provider_client.py"
click node_structured_db "https://github.com/mihirmaru22/global_mind/blob/main/src/core/db_client.py"
click node_state "https://github.com/mihirmaru22/global_mind/blob/main/src/core/state.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_client_api,node_client_state,node_answer_rendering toneBlue
class node_app,node_upload_api,node_query_api,node_ui_api toneAmber
class node_ingestion,node_identity,node_transform,node_contracts,node_folder_ingestion toneMint
class node_query_pipeline,node_retrieval,node_sql_retrieval toneRose
class node_providers,node_qdrant,node_structured_db,node_state toneIndigo
```

## Components

### Browser UI

- **React SPA**: frontend · Built UI is served by the backend.
- [API client](https://github.com/mihirmaru22/global_mind/blob/main/LocalMind_UI/src/services/api.js): frontend service · `LocalMind_UI/src/services/api.js`
- [Chat/session state](https://github.com/mihirmaru22/global_mind/blob/main/LocalMind_UI/src/store/store.js): frontend store · `LocalMind_UI/src/store/store.js`
- [Answer rendering](https://github.com/mihirmaru22/global_mind/blob/main/LocalMind_UI/src/components/Message.jsx): UI components · Renders Markdown, citations, traces, and diagrams. · `LocalMind_UI/src/components/Message.jsx`

### FastAPI boundary

- [FastAPI runtime](https://github.com/mihirmaru22/global_mind/blob/main/src/main.py): application entry · Serves API routes and built SPA. · `src/main.py`
- [Upload API](https://github.com/mihirmaru22/global_mind/blob/main/src/api/upload.py): SSE endpoint · Accepts batches and reports ingestion progress. · `src/api/upload.py`
- [Query API](https://github.com/mihirmaru22/global_mind/blob/main/src/api/query.py): query endpoint · `src/api/query.py`
- [UI/state API](https://github.com/mihirmaru22/global_mind/blob/main/src/api/ui.py): UI endpoint · Chat, settings, documents, providers, and UI streaming. · `src/api/ui.py`

### Document ingestion

- [Ingestion orchestrator](https://github.com/mihirmaru22/global_mind/blob/main/src/pipeline/ingestion.py): pipeline · Commits only successfully processed documents. · `src/pipeline/ingestion.py`
- [Document identity](https://github.com/mihirmaru22/global_mind/blob/main/src/core/ingestion_registry.py): content registry · SHA-256 deduplication permits same-named changed files. · `src/core/ingestion_registry.py`
- [Transformation stages](https://github.com/mihirmaru22/global_mind/blob/main/src/stages/s01_file_detection.py): pipeline stages · Detection, parsing, OCR, layout, tables, visuals, chunking, embeddings, and vector commit. · `src/stages/s01_file_detection.py`
- [Stage contracts and quality](https://github.com/mihirmaru22/global_mind/blob/main/src/models/schemas.py): typed schemas · Typed records and extraction confidence gates. · `src/models/schemas.py`
- [Folder ingestion](https://github.com/mihirmaru22/global_mind/blob/main/src/pipeline/folder_ingestion.py): inbox boundary · Scans and concurrently ingests protected local files. · `src/pipeline/folder_ingestion.py`

### Retrieval and generation

- [Query pipeline](https://github.com/mihirmaru22/global_mind/blob/main/src/pipeline/query.py): RAG orchestrator · Builds context and streams cited answers. · `src/pipeline/query.py`
- [Hybrid retrieval and generation](https://github.com/mihirmaru22/global_mind/blob/main/src/stages/s12_s13_s14_retrieval.py): RAG stages · Dense/sparse retrieval, RRF, reranking, and streaming generation. · `src/stages/s12_s13_s14_retrieval.py`
- [Text-to-SQL](https://github.com/mihirmaru22/global_mind/blob/main/src/stages/s12b_sql_retrieval.py): optional retrieval · Generates and validates read-only structured-data queries. · `src/stages/s12b_sql_retrieval.py`

### Providers and storage

- [Provider router](https://github.com/mihirmaru22/global_mind/blob/main/src/core/provider_client.py): resilient LLM client · Routes and falls back across Gemini, Groq, NVIDIA NIM, OpenRouter, and Jina. · `src/core/provider_client.py`
- **Qdrant**: vector database · Dense and sparse document vectors.
- [SQLite / MySQL](https://github.com/mihirmaru22/global_mind/blob/main/src/core/db_client.py): structured database · Used only for structured-data retrieval. · `src/core/db_client.py`
- [JSON application state](https://github.com/mihirmaru22/global_mind/blob/main/src/core/state.py): state storage · Persists UI and application state. · `src/core/state.py`

## Connections

- React SPA → API client: uses
- Chat/session state → API client: drives requests
- API client → Upload API: uploads
- API client → Query API: queries
- API client → UI/state API: chat and settings
- Query API → Query pipeline: RAG request
- UI/state API → Query pipeline: UI streaming
- Query pipeline → Hybrid retrieval and generation: retrieve and generate
- Hybrid retrieval and generation → Provider router: embeddings, reranking, LLM
- Hybrid retrieval and generation → Qdrant: hybrid search
- Query pipeline → Text-to-SQL: optional structured query
- Text-to-SQL → SQLite / MySQL: read-only SQL
- Upload API → Ingestion orchestrator: documents and progress
- Folder ingestion → Ingestion orchestrator: local documents
- Ingestion orchestrator → Document identity: deduplicate
- Ingestion orchestrator → Transformation stages: process accepted files
- Transformation stages → Stage contracts and quality: validated records
- Transformation stages → Provider router: enrichment and embeddings
- Transformation stages → Qdrant: commit vectors
- UI/state API → JSON application state: persist UI state
- Query pipeline → Answer rendering: SSE answer stream
- FastAPI runtime → Upload API: mounts
- FastAPI runtime → Query API: mounts
- FastAPI runtime → UI/state API: mounts
