# saqib632/bookshelf architecture

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

- Interactive diagram: https://gitdiagram.com/saqib632/bookshelf
- Repository: https://github.com/saqib632/bookshelf

## Overview

This is an authenticated book-chat application with document ingestion and grounded RAG/CRAG answers. The principal flow is upload → asynchronous extraction/chunking/indexing → scoped retrieval and corrective retry → answer with citations. The README describes the implementation as awaiting architecture approval, while the supplied tree and excerpts contain runtime code; unsampled frontend/API wiring and some route details are therefore shown only where documented or left unlinked.

Main entry points: [`backend/app/api.py`](https://github.com/saqib632/bookshelf/blob/main/backend/app/api.py) (REST routes), [`backend/app/workers/tasks.py`](https://github.com/saqib632/bookshelf/blob/main/backend/app/workers/tasks.py) (Processing worker), [`backend/app/db.py`](https://github.com/saqib632/bookshelf/blob/main/backend/app/db.py) (MongoDB records), [`backend/app/services.py`](https://github.com/saqib632/bookshelf/blob/main/backend/app/services.py) (Chat orchestration)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["User experience"]
  node_web["Chat interface<br/>[page.tsx]"]
  node_frontend_api["HTTP client<br/>[api.ts]"]
end

subgraph group_api["API and identity"]
  node_routes["REST routes<br/>[api.py]"]
  node_identity["Token identity<br/>[auth.py]"]
  node_schemas["Request schemas<br/>[models.py]"]
  node_api_server["API runtime<br/>[main.py]"]
end

subgraph group_ingestion["Document ingestion"]
  node_job_dispatch["Job dispatch<br/>[api.py]"]
  node_redis["Redis broker"]
  node_worker["Processing worker<br/>[tasks.py]"]
  node_extract_chunk["Extract and chunk<br/>[processing.py]"]
  node_temp_files["Temporary uploads"]
end

subgraph group_rag["Grounded chat"]
  node_chat_service["Chat orchestration<br/>[services.py]"]
  node_crag["CRAG retrieval<br/>[services.py]"]
  node_answering["Grounded answering<br/>[providers.py]"]
end

subgraph group_state["Persistence and providers"]
  node_embedding["Embedding provider<br/>[providers.py]"]
  node_vector_store[("Vector search<br/>[providers.py]")]
  node_mongo[("MongoDB records<br/>[db.py]")]
  node_app_config["Runtime settings<br/>[config.py]"]
end

node_user(("Book owner"))

node_user -.->|"asks and uploads"| node_web
node_web -.->|"uses client"| node_frontend_api
node_frontend_api -.->|"sends requests"| node_routes
node_api_server -->|"registers routes"| node_routes
node_routes -->|"authenticates"| node_identity
node_routes -->|"validates payloads"| node_schemas
node_identity -->|"loads user"| node_mongo
node_routes -->|"reads and writes"| node_mongo
node_routes -->|"dispatches upload"| node_job_dispatch
node_job_dispatch -.->|"queues job"| node_redis
node_worker -->|"uses broker"| node_redis
node_worker -->|"updates job records"| node_mongo
node_worker -->|"reads and deletes"| node_temp_files
node_routes -->|"stages upload"| node_temp_files
node_worker -->|"extracts and chunks"| node_extract_chunk
node_worker -->|"embeds chunks"| node_embedding
node_worker -->|"upserts vectors"| node_vector_store
node_routes -->|"creates chat response"| node_chat_service
node_chat_service -->|"runs retrieval loop"| node_crag
node_crag -->|"reads chunks"| node_mongo
node_crag -->|"embeds query"| node_embedding
node_crag -->|"searches vectors"| node_vector_store
node_chat_service -->|"generates answer"| node_answering
node_chat_service -->|"stores chat and citations"| node_mongo
node_api_server -->|"opens database"| node_mongo
node_api_server -->|"loads settings"| node_app_config
node_embedding -->|"selects provider"| node_app_config

click node_web "https://github.com/saqib632/bookshelf/blob/main/frontend/src/app/page.tsx"
click node_frontend_api "https://github.com/saqib632/bookshelf/blob/main/frontend/src/lib/api.ts"
click node_routes "https://github.com/saqib632/bookshelf/blob/main/backend/app/api.py"
click node_identity "https://github.com/saqib632/bookshelf/blob/main/backend/app/auth.py"
click node_schemas "https://github.com/saqib632/bookshelf/blob/main/backend/app/models.py"
click node_job_dispatch "https://github.com/saqib632/bookshelf/blob/main/backend/app/api.py"
click node_worker "https://github.com/saqib632/bookshelf/blob/main/backend/app/workers/tasks.py"
click node_extract_chunk "https://github.com/saqib632/bookshelf/blob/main/backend/app/processing.py"
click node_chat_service "https://github.com/saqib632/bookshelf/blob/main/backend/app/services.py"
click node_crag "https://github.com/saqib632/bookshelf/blob/main/backend/app/services.py"
click node_answering "https://github.com/saqib632/bookshelf/blob/main/backend/app/providers.py"
click node_embedding "https://github.com/saqib632/bookshelf/blob/main/backend/app/providers.py"
click node_vector_store "https://github.com/saqib632/bookshelf/blob/main/backend/app/providers.py"
click node_mongo "https://github.com/saqib632/bookshelf/blob/main/backend/app/db.py"
click node_app_config "https://github.com/saqib632/bookshelf/blob/main/backend/app/config.py"
click node_api_server "https://github.com/saqib632/bookshelf/blob/main/backend/app/main.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_web,node_frontend_api toneBlue
class node_routes,node_identity,node_schemas,node_api_server toneAmber
class node_job_dispatch,node_redis,node_worker,node_extract_chunk,node_temp_files toneMint
class node_chat_service,node_crag,node_answering toneRose
class node_embedding,node_vector_store,node_mongo,node_app_config,node_user toneIndigo
```

## Components

### User experience

- [Chat interface](https://github.com/saqib632/bookshelf/blob/main/frontend/src/app/page.tsx): `frontend/src/app/page.tsx`
- [HTTP client](https://github.com/saqib632/bookshelf/blob/main/frontend/src/lib/api.ts): `frontend/src/lib/api.ts`

### API and identity

- [REST routes](https://github.com/saqib632/bookshelf/blob/main/backend/app/api.py): `backend/app/api.py`
- [Token identity](https://github.com/saqib632/bookshelf/blob/main/backend/app/auth.py): `backend/app/auth.py`
- [Request schemas](https://github.com/saqib632/bookshelf/blob/main/backend/app/models.py): `backend/app/models.py`
- [API runtime](https://github.com/saqib632/bookshelf/blob/main/backend/app/main.py): `backend/app/main.py`

### Document ingestion

- [Job dispatch](https://github.com/saqib632/bookshelf/blob/main/backend/app/api.py): `backend/app/api.py`
- **Redis broker**
- [Processing worker](https://github.com/saqib632/bookshelf/blob/main/backend/app/workers/tasks.py): `backend/app/workers/tasks.py`
- [Extract and chunk](https://github.com/saqib632/bookshelf/blob/main/backend/app/processing.py): `backend/app/processing.py`
- **Temporary uploads**

### Grounded chat

- [Chat orchestration](https://github.com/saqib632/bookshelf/blob/main/backend/app/services.py): `backend/app/services.py`
- [CRAG retrieval](https://github.com/saqib632/bookshelf/blob/main/backend/app/services.py): `backend/app/services.py`
- [Grounded answering](https://github.com/saqib632/bookshelf/blob/main/backend/app/providers.py): `backend/app/providers.py`

### Persistence and providers

- [Embedding provider](https://github.com/saqib632/bookshelf/blob/main/backend/app/providers.py): `backend/app/providers.py`
- [Vector search](https://github.com/saqib632/bookshelf/blob/main/backend/app/providers.py): `backend/app/providers.py`
- [MongoDB records](https://github.com/saqib632/bookshelf/blob/main/backend/app/db.py): `backend/app/db.py`
- [Runtime settings](https://github.com/saqib632/bookshelf/blob/main/backend/app/config.py): `backend/app/config.py`

### Other components

- **Book owner**

## Connections

- Book owner → Chat interface: asks and uploads (evidence: `README.md`)
- Chat interface → HTTP client: uses client
- HTTP client → REST routes: sends requests (evidence: `README.md`)
- API runtime → REST routes: registers routes (evidence: `backend/app/main.py`)
- REST routes → Token identity: authenticates (evidence: `backend/app/api.py`)
- REST routes → Request schemas: validates payloads (evidence: `backend/app/api.py`)
- Token identity → MongoDB records: loads user (evidence: `backend/app/auth.py`)
- REST routes → MongoDB records: reads and writes (evidence: `backend/app/api.py`)
- REST routes → Job dispatch: dispatches upload (evidence: `backend/app/api.py`)
- Job dispatch → Redis broker: queues job
- Processing worker → Redis broker: uses broker (evidence: `backend/app/workers/tasks.py`)
- Processing worker → MongoDB records: updates job records (evidence: `backend/app/workers/tasks.py`)
- Processing worker → Temporary uploads: reads and deletes (evidence: `backend/app/workers/tasks.py`)
- REST routes → Temporary uploads: stages upload (evidence: `backend/app/api.py`)
- Processing worker → Extract and chunk: extracts and chunks (evidence: `backend/app/workers/tasks.py`)
- Processing worker → Embedding provider: embeds chunks (evidence: `backend/app/workers/tasks.py`)
- Processing worker → Vector search: upserts vectors (evidence: `backend/app/workers/tasks.py`)
- REST routes → Chat orchestration: creates chat response (evidence: `backend/app/api.py`)
- Chat orchestration → CRAG retrieval: runs retrieval loop (evidence: `backend/app/services.py`)
- CRAG retrieval → MongoDB records: reads chunks (evidence: `backend/app/services.py`)
- CRAG retrieval → Embedding provider: embeds query (evidence: `backend/app/services.py`)
- CRAG retrieval → Vector search: searches vectors (evidence: `backend/app/services.py`)
- Chat orchestration → Grounded answering: generates answer (evidence: `backend/app/services.py`)
- Chat orchestration → MongoDB records: stores chat and citations (evidence: `backend/app/services.py`)
- API runtime → MongoDB records: opens database (evidence: `backend/app/main.py`)
- API runtime → Runtime settings: loads settings (evidence: `backend/app/main.py`)
- Embedding provider → Runtime settings: selects provider (evidence: `backend/app/providers.py`)
