# aravindg-atwork/knowledge-os architecture

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

- Interactive diagram: https://gitdiagram.com/aravindg-atwork/knowledge-os
- Repository: https://github.com/aravindg-atwork/knowledge-os

## Overview

#### Purpose and entry points
Enterprise Knowledge Hub AI is a workspace-scoped knowledge/RAG system. It ingests Google Drive and Gmail content, indexes it, and serves permission-aware chat with source citations and document viewing. Backend entry point: `backend/app/main.py`; API composition: `backend/app/api/v1/router.py`. The React SPA starts at `frontend/src/main.tsx` and routes through `frontend/src/app/routes.tsx`.

#### API, identity, and tenancy boundary
FastAPI exposes auth, workspace, invitation, connector, document, and chat endpoints. Authentication/token handling and dependency-based request authorization sit at `backend/app/api/deps.py`, `backend/app/api/v1/auth.py`, and `backend/app/core/security.py`. Workspace membership, roles, and isolation are modeled and enforced through `backend/app/models/workspace.py`, `backend/app/services/tenancy_service.py`, and `backend/app/services/document_access_service.py`; this scope must propagate into repository and vector retrieval operations.

#### Workspace-facing frontend
The SPA owns session state and protected-route behavior in `frontend/src/app/AuthContext.tsx`, `frontend/src/app/guards.tsx`, and `frontend/src/app/AppShell.tsx`. Feature pages call the backend through API modules: chat (`frontend/src/features/chat/ChatPage.tsx`), documents (`frontend/src/features/documents/DocumentListPage.tsx`), and workspace/member settings (`frontend/src/features/settings/WorkspaceSettingsPage.tsx`). `frontend/src/components/WorkspaceSwitcher.tsx` makes workspace context explicit in the client.

#### Connectors and synchronization
A connector registry and common interface separate sync orchestration from provider implementations: `backend/app/connectors/base.py`, `backend/app/connectors/registry.py`, and `backend/app/connectors/types.py`. Google Drive and Gmail each support mock fixture-backed and real OAuth/API clients under `backend/app/connectors/google_drive/connector.py`, `backend/app/connectors/gmail/connector.py`, and `backend/app/connectors/google_oauth.py`. Sync state and change detection persist through `backend/app/models/sync_state.py` and `backend/app/services/sync_service.py`.

#### Asynchronous ingestion pipeline
Connector sync work is dispatched through Celery, separating API/OAuth requests from potentially expensive fetch and indexing jobs: `backend/app/workers/celery_app.py`, `backend/app/workers/tasks_sync.py`, and `backend/app/workers/tasks_ingestion.py`. The pipeline extracts source bytes/text, chunks normalized content, computes checksums/version behavior, embeds chunks, and writes vectors: `backend/app/pipeline/pipeline_service.py`, `backend/app/pipeline/extractors/`, and `backend/app/pipeline/chunking/recursive_chunker.py`. Documents and chunks remain relational records in `backend/app/models/document.py` and `backend/app/models/chunk.py`.

#### Retrieval and answer generation
Chat requests flow from `backend/app/api/v1/chat.py` into RAG/retrieval services. `backend/app/services/rag_service.py` coordinates query embedding, permission-filtered vector search, LLM answer generation, and citations; `backend/app/services/retrieval_service.py` constructs retrieval results. Vector-store abstraction and Qdrant implementation are in `backend/app/vectorstore/base.py`, `backend/app/vectorstore/qdrant_store.py`, and `backend/app/vectorstore/payload_schema.py`, where workspace/access metadata forms the critical search boundary.

#### Providers and runtime infrastructure
AI providers are selected through `backend/app/ai/factory.py`: local Ollama or Mistral for LLMs (`backend/app/ai/llm/ollama_llm.py`, `backend/app/ai/llm/mistral_llm.py`), with local sentence-transformer or Ollama embeddings. PostgreSQL is the system of record via SQLAlchemy/Alembic (`backend/app/db/session.py`, `backend/app/db/migrations/`); Qdrant stores vectors; Redis brokers Celery tasks; Ollama runs local models. Service topology is defined by `docker-compose.yml`, with development overrides in `docker-compose.dev.yml`; production controls are documented in `docs/PRODUCTION.md`.

Main entry points: [`backend/app/pipeline/pipeline_service.py`](https://github.com/aravindg-atwork/knowledge-os/blob/main/backend/app/pipeline/pipeline_service.py) (Ingestion pipeline), [`docker-compose.yml`](https://github.com/aravindg-atwork/knowledge-os/blob/main/docker-compose.yml) (Container topology), [`backend/app/api/v1/router.py`](https://github.com/aravindg-atwork/knowledge-os/blob/main/backend/app/api/v1/router.py) (API endpoints), [`backend/app/services/sync_service.py`](https://github.com/aravindg-atwork/knowledge-os/blob/main/backend/app/services/sync_service.py) (Sync state & orchestration)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Workspace SPA"]
  node_spa["React SPA<br/>frontend<br/>[main.tsx]"]
  node_client_session["Session &amp; route guards<br/>frontend auth<br/>[AuthContext.tsx]"]
  node_workspace_ui["Workspace UI<br/>frontend features"]
end

subgraph group_api["Backend API &amp; Tenancy"]
  node_api_app["FastAPI application<br/>API server<br/>[main.py]"]
  node_authz["Auth &amp; request authorization<br/>security boundary<br/>[deps.py]"]
  node_tenancy["Workspace tenancy<br/>access service<br/>[tenancy_service.py]"]
  node_api_endpoints["API endpoints<br/>route modules<br/>[router.py]"]
end

subgraph group_sync["Connector Sync &amp; Ingestion"]
  node_connectors["Connector registry<br/>connector interface<br/>[registry.py]"]
  node_google_connectors["Google Drive &amp; Gmail<br/>provider connectors<br/>[connector.py]"]
  node_sync_service["Sync state &amp; orchestration<br/>sync service<br/>[sync_service.py]"]
  node_celery_workers["Celery sync &amp; ingestion workers<br/>async workers<br/>[tasks_sync.py]"]
  node_ingestion["Ingestion pipeline<br/>content pipeline"]
end

subgraph group_retrieval["Retrieval &amp; AI"]
  node_rag["RAG orchestration<br/>chat service<br/>[rag_service.py]"]
  node_retrieval["Permission-filtered retrieval<br/>retrieval service"]
  node_vector_store["Qdrant vector store<br/>vector adapter<br/>[qdrant_store.py]"]
  node_ai_factory["LLM &amp; embedding providers<br/>AI factory<br/>[factory.py]"]
end

subgraph group_infra["Data &amp; Runtime"]
  node_postgres[("PostgreSQL<br/>system of record<br/>[session.py]")]
  node_qdrant[("Qdrant<br/>vector database")]
  node_redis["Redis<br/>task broker"]
  node_ollama["Ollama<br/>local model runtime"]
  node_compose["Container topology<br/>deployment topology<br/>[docker-compose.yml]"]
end

node_spa -->|"initializes"| node_client_session
node_client_session -->|"protects"| node_workspace_ui
node_workspace_ui -->|"calls workspace APIs"| node_api_app
node_api_app -->|"mounts"| node_api_endpoints
node_api_endpoints -->|"authorizes requests"| node_authz
node_authz -->|"resolves workspace scope"| node_tenancy
node_api_endpoints -->|"starts connector sync"| node_sync_service
node_api_endpoints -->|"sends chat requests"| node_rag
node_tenancy -->|"checks memberships and access"| node_postgres
node_sync_service -->|"orchestrates provider sync"| node_connectors
node_connectors -->|"selects"| node_google_connectors
node_sync_service -->|"dispatches jobs"| node_celery_workers
node_celery_workers -->|"uses broker"| node_redis
node_celery_workers -->|"runs indexing"| node_ingestion
node_google_connectors -->|"supplies source content"| node_ingestion
node_ingestion -->|"writes documents and chunks"| node_postgres
node_ingestion -->|"requests embeddings"| node_ai_factory
node_ingestion -->|"indexes vectors"| node_vector_store
node_rag -->|"retrieves context"| node_retrieval
node_retrieval -->|"applies access scope"| node_tenancy
node_retrieval -->|"searches scoped vectors"| node_vector_store
node_vector_store -->|"reads and writes"| node_qdrant
node_rag -->|"embeds queries and generates answers"| node_ai_factory
node_ai_factory -.->|"uses local models"| node_ollama
node_compose -.->|"runs"| node_postgres
node_compose -.->|"runs"| node_qdrant
node_compose -.->|"runs"| node_redis
node_compose -.->|"runs"| node_ollama

click node_spa "https://github.com/aravindg-atwork/knowledge-os/blob/main/frontend/src/main.tsx"
click node_client_session "https://github.com/aravindg-atwork/knowledge-os/blob/main/frontend/src/app/AuthContext.tsx"
click node_workspace_ui "https://github.com/aravindg-atwork/knowledge-os/blob/main/frontend/src/components/WorkspaceSwitcher.tsx"
click node_api_app "https://github.com/aravindg-atwork/knowledge-os/blob/main/backend/app/main.py"
click node_authz "https://github.com/aravindg-atwork/knowledge-os/blob/main/backend/app/api/deps.py"
click node_tenancy "https://github.com/aravindg-atwork/knowledge-os/blob/main/backend/app/services/tenancy_service.py"
click node_api_endpoints "https://github.com/aravindg-atwork/knowledge-os/blob/main/backend/app/api/v1/router.py"
click node_connectors "https://github.com/aravindg-atwork/knowledge-os/blob/main/backend/app/connectors/registry.py"
click node_google_connectors "https://github.com/aravindg-atwork/knowledge-os/blob/main/backend/app/connectors/google_drive/connector.py"
click node_sync_service "https://github.com/aravindg-atwork/knowledge-os/blob/main/backend/app/services/sync_service.py"
click node_celery_workers "https://github.com/aravindg-atwork/knowledge-os/blob/main/backend/app/workers/tasks_sync.py"
click node_ingestion "https://github.com/aravindg-atwork/knowledge-os/blob/main/backend/app/pipeline/pipeline_service.py"
click node_rag "https://github.com/aravindg-atwork/knowledge-os/blob/main/backend/app/services/rag_service.py"
click node_retrieval "https://github.com/aravindg-atwork/knowledge-os/blob/main/backend/app/services/retrieval_service.py"
click node_vector_store "https://github.com/aravindg-atwork/knowledge-os/blob/main/backend/app/vectorstore/qdrant_store.py"
click node_ai_factory "https://github.com/aravindg-atwork/knowledge-os/blob/main/backend/app/ai/factory.py"
click node_postgres "https://github.com/aravindg-atwork/knowledge-os/blob/main/backend/app/db/session.py"
click node_compose "https://github.com/aravindg-atwork/knowledge-os/blob/main/docker-compose.yml"

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_session,node_workspace_ui toneBlue
class node_api_app,node_authz,node_tenancy,node_api_endpoints toneAmber
class node_connectors,node_google_connectors,node_sync_service,node_celery_workers,node_ingestion toneMint
class node_rag,node_retrieval,node_vector_store,node_ai_factory toneRose
class node_postgres,node_qdrant,node_redis,node_ollama,node_compose toneIndigo
```

## Components

### Workspace SPA

- [React SPA](https://github.com/aravindg-atwork/knowledge-os/blob/main/frontend/src/main.tsx): frontend · Workspace-scoped user interface. · `frontend/src/main.tsx`
- [Session & route guards](https://github.com/aravindg-atwork/knowledge-os/blob/main/frontend/src/app/AuthContext.tsx): frontend auth · Maintains session and protected workspace routes. · `frontend/src/app/AuthContext.tsx`
- [Workspace UI](https://github.com/aravindg-atwork/knowledge-os/blob/main/frontend/src/components/WorkspaceSwitcher.tsx): frontend features · Chat, documents, settings, and workspace switching. · `frontend/src/components/WorkspaceSwitcher.tsx`

### Backend API & Tenancy

- [FastAPI application](https://github.com/aravindg-atwork/knowledge-os/blob/main/backend/app/main.py): API server · Composes versioned workspace APIs. · `backend/app/main.py`
- [Auth & request authorization](https://github.com/aravindg-atwork/knowledge-os/blob/main/backend/app/api/deps.py): security boundary · Token handling and dependency-based authorization. · `backend/app/api/deps.py`
- [Workspace tenancy](https://github.com/aravindg-atwork/knowledge-os/blob/main/backend/app/services/tenancy_service.py): access service · Enforces membership, roles, and document access scope. · `backend/app/services/tenancy_service.py`
- [API endpoints](https://github.com/aravindg-atwork/knowledge-os/blob/main/backend/app/api/v1/router.py): route modules · Auth, workspace, connector, document, and chat routes. · `backend/app/api/v1/router.py`

### Connector Sync & Ingestion

- [Connector registry](https://github.com/aravindg-atwork/knowledge-os/blob/main/backend/app/connectors/registry.py): connector interface · Selects provider implementations for sync. · `backend/app/connectors/registry.py`
- [Google Drive & Gmail](https://github.com/aravindg-atwork/knowledge-os/blob/main/backend/app/connectors/google_drive/connector.py): provider connectors · OAuth/API and fixture-backed source clients. · `backend/app/connectors/google_drive/connector.py`
- [Sync state & orchestration](https://github.com/aravindg-atwork/knowledge-os/blob/main/backend/app/services/sync_service.py): sync service · Tracks source changes and dispatches sync work. · `backend/app/services/sync_service.py`
- [Celery sync & ingestion workers](https://github.com/aravindg-atwork/knowledge-os/blob/main/backend/app/workers/tasks_sync.py): async workers · Runs expensive fetch and indexing jobs. · `backend/app/workers/tasks_sync.py`
- [Ingestion pipeline](https://github.com/aravindg-atwork/knowledge-os/blob/main/backend/app/pipeline/pipeline_service.py): content pipeline · Extracts, chunks, versions, embeds, and indexes content. · `backend/app/pipeline/pipeline_service.py`

### Retrieval & AI

- [RAG orchestration](https://github.com/aravindg-atwork/knowledge-os/blob/main/backend/app/services/rag_service.py): chat service · Generates answers and citations from scoped retrieval. · `backend/app/services/rag_service.py`
- [Permission-filtered retrieval](https://github.com/aravindg-atwork/knowledge-os/blob/main/backend/app/services/retrieval_service.py): retrieval service · Builds scoped vector retrieval results. · `backend/app/services/retrieval_service.py`
- [Qdrant vector store](https://github.com/aravindg-atwork/knowledge-os/blob/main/backend/app/vectorstore/qdrant_store.py): vector adapter · Applies workspace and access metadata during search. · `backend/app/vectorstore/qdrant_store.py`
- [LLM & embedding providers](https://github.com/aravindg-atwork/knowledge-os/blob/main/backend/app/ai/factory.py): AI factory · Selects local or hosted generation and embedding providers. · `backend/app/ai/factory.py`

### Data & Runtime

- [PostgreSQL](https://github.com/aravindg-atwork/knowledge-os/blob/main/backend/app/db/session.py): system of record · Stores workspace, sync, document, and chunk records. · `backend/app/db/session.py`
- **Qdrant**: vector database
- **Redis**: task broker
- **Ollama**: local model runtime
- [Container topology](https://github.com/aravindg-atwork/knowledge-os/blob/main/docker-compose.yml): deployment topology · Defines local service composition. · `docker-compose.yml`

## Connections

- React SPA → Session & route guards: initializes
- Session & route guards → Workspace UI: protects
- Workspace UI → FastAPI application: calls workspace APIs
- FastAPI application → API endpoints: mounts
- API endpoints → Auth & request authorization: authorizes requests
- Auth & request authorization → Workspace tenancy: resolves workspace scope
- API endpoints → Sync state & orchestration: starts connector sync
- API endpoints → RAG orchestration: sends chat requests
- Workspace tenancy → PostgreSQL: checks memberships and access
- Sync state & orchestration → Connector registry: orchestrates provider sync
- Connector registry → Google Drive & Gmail: selects
- Sync state & orchestration → Celery sync & ingestion workers: dispatches jobs
- Celery sync & ingestion workers → Redis: uses broker
- Celery sync & ingestion workers → Ingestion pipeline: runs indexing
- Google Drive & Gmail → Ingestion pipeline: supplies source content
- Ingestion pipeline → PostgreSQL: writes documents and chunks
- Ingestion pipeline → LLM & embedding providers: requests embeddings
- Ingestion pipeline → Qdrant vector store: indexes vectors
- RAG orchestration → Permission-filtered retrieval: retrieves context
- Permission-filtered retrieval → Workspace tenancy: applies access scope
- Permission-filtered retrieval → Qdrant vector store: searches scoped vectors
- Qdrant vector store → Qdrant: reads and writes
- RAG orchestration → LLM & embedding providers: embeds queries and generates answers
- LLM & embedding providers → Ollama: uses local models
- Container topology → PostgreSQL: runs
- Container topology → Qdrant: runs
- Container topology → Redis: runs
- Container topology → Ollama: runs
