# digibranders/oye-chats-platform architecture

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

- Interactive diagram: https://gitdiagram.com/digibranders/oye-chats-platform
- Repository: https://github.com/digibranders/oye-chats-platform

## Overview

#### Purpose and Entry Points
OyeChats is a multi-tenant SaaS platform for creating website-embedded, RAG-backed chatbots with live human handoff, lead qualification, and subscription billing. Its deployable surfaces are:
- FastAPI API, REST/SSE/WebSocket: `api/app/main.py`
- Embeddable widget loader and React application: `widget/src/loader.js`, `widget/src/app-entry.jsx`
- Authenticated customer/operator dashboard SPA: `app/src/main.jsx`, `app/src/app/App.tsx`

#### API Composition and Tenant Boundary
`api/app/main.py` wires middleware and root-mounted route modules. Route handlers establish request/auth/public-widget boundaries, then delegate business logic to services; persistence is centralized through SQLAlchemy models, repository, and sessions.
- Public conversational and widget configuration APIs: `api/app/api/chat_routes.py`, `api/app/api/bot_routes.py`
- Authenticated workspace/admin APIs: `api/app/api/auth.py`, `api/app/api/client_routes.py`
- Data boundary: `api/app/db/models.py`, `api/app/db/repository.py`, `api/app/db/session.py`

PostgreSQL plus pgvector is the durable system of record; migrations define evolving tenancy, bots, sessions, documents, subscriptions, invoices, credits, and operational state.

#### Knowledge Acquisition and RAG
Knowledge enters through uploads or crawls, then moves through extraction, cleaning, chunking, embedding, optional enrichment, and durable indexing. Crawling is independently orchestrated and can discover URLs, track progress/state, and recrawl.
- Pipeline stages: `api/app/ingestion/pipeline.py`, `api/app/ingestion/extraction.py`, `api/app/ingestion/embedder.py`
- Crawl boundary: `api/app/services/crawl_orchestrator.py`, `api/app/services/crawler_service.py`
- Retrieval/answer context: `api/app/services/rag_service.py`, `api/app/services/reranker.py`, `api/app/services/relevance_gate.py`

The runtime uses OpenAI embeddings and pgvector/vector-plus-keyword retrieval, with guards around relevance, grounding, quotas, and tenant isolation.

#### Visitor Chat and Live Handoff
The widget loads from a single host-page script, mounts an isolated root, lazy-loads its UI, and calls the API using its bot key. Chat routes stream AI replies over SSE; live-chat traffic uses WebSockets and shared coordination/backplane services.
- Embed/bootstrap and UI state: `widget/src/loader.js`, `widget/src/widget-controller.js`, `widget/src/lib/chatModeMachine.js`
- Chat execution: `api/app/api/chat_routes.py`, `api/app/services/llm_service.py`, `api/app/services/session_state_machine.py`
- Operator handoff: `api/app/api/ws_routes.py`, `api/app/services/live_chat_service.py`, `api/app/services/live_chat_routing_service.py`

LLM invocation is mediated by LiteLLM with OpenAI primary and Gemini fallback. Conversation outcomes feed lead, qualification, feedback, and analytics services.

#### Dashboard and Operator Workflows
The React dashboard is the control plane for workspace users: agent setup, knowledge/crawl management, bot experience/channel configuration, operator inbox, leads, analytics, and billing. Route protection and shared contexts carry identity, selected bot, workspace, entitlements, notifications, and crawl status.
- Application routing/security shell: `app/src/app/routes.tsx`, `app/src/app/ProtectedLayout.tsx`
- Agent configuration and knowledge workflows: `app/src/features/agents/AgentLayout.tsx`, `app/src/features/agents/knowledge/KnowledgePage.tsx`
- Operator console: `app/src/features/inbox/InboxPage.tsx`, `app/src/features/inbox/useOperatorSocket.ts`

#### Billing, Credits, and External Events
Plans and entitlements gate feature access and consumption. A FIFO credit ledger supports ingestion/chat usage and top-ups; subscriptions, invoices, promotions, and dunning are managed server-side. Inbound payment webhooks are verified and made idempotent before state mutation.
- Billing APIs and webhooks: `api/app/api/subscription_routes.py`, `api/app/api/webhook_billing_routes.py`
- Core domain services: `api/app/services/plan_entitlements_service.py`, `api/app/services/credit_service.py`, `api/app/services/razorpay_service.py`
- Dashboard billing workflows: `app/src/features/workspace/BillingPage.tsx`, `app/src/features/workspace/billing/usePlanCheckout.ts`

Razorpay is the primary INR rail, Stripe is fallback; Brevo handles transactional mail, and Cloudflare R2 stores uploaded/media assets.

#### Runtime, Async Work, and Deployment
FastAPI serves request paths while Redis supports ARQ jobs, cache, rate limits, and cross-process live-chat coordination. Long-running ingestion, crawl, email, invoice, reconciliation, and scheduled work are dispatched through the worker abstraction.
- Async execution: `api/app/worker/tasks.py`, `api/app/worker/enqueue.py`
- Production process topology: `api/systemd/oyechats-api.service`, `api/systemd/oyechats-worker.service`, `api/systemd/oyechats-ws.service`
- Edge/deploy configuration: `api/nginx/oyechats-api.conf`, `.github/workflows/deploy-api.yml`, `.github/workflows/deploy-widget.yml`

Production targets a DigitalOcean host behind Nginx/Cloudflare; Sentry and Langfuse provide observability, with Langfuse configurable/disabled for resource limits.

Main entry points: [`api/app/db/models.py`](https://github.com/digibranders/oye-chats-platform/blob/main/api/app/db/models.py) (PostgreSQL + pgvector), [`api/app/services/llm_service.py`](https://github.com/digibranders/oye-chats-platform/blob/main/api/app/services/llm_service.py) (Chat execution), [`api/app/worker/tasks.py`](https://github.com/digibranders/oye-chats-platform/blob/main/api/app/worker/tasks.py) (ARQ workers), [`api/app/api/client_routes.py`](https://github.com/digibranders/oye-chats-platform/blob/main/api/app/api/client_routes.py) (Workspace APIs)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_clients["Customer surfaces"]
  node_dashboard["Dashboard SPA<br/>React control plane<br/>[main.jsx]"]
  node_dashboard_routes["Protected dashboard shell<br/>routing and identity<br/>[routes.tsx]"]
  node_widget_loader["Embed widget<br/>loader and React UI<br/>[loader.js]"]
  node_widget_state["Widget chat state<br/>mode state machine<br/>[chatModeMachine.js]"]
end

subgraph group_api["API runtime"]
  node_api_app["FastAPI application<br/>REST SSE WebSocket host<br/>[main.py]"]
  node_public_chat["Public chat APIs<br/>widget and SSE routes<br/>[chat_routes.py]"]
  node_workspace_api["Workspace APIs<br/>authenticated routes<br/>[client_routes.py]"]
  node_chat_runtime["Chat execution<br/>LLM and session orchestration<br/>[llm_service.py]"]
  node_rag["RAG retrieval<br/>retrieval and relevance<br/>[rag_service.py]"]
  node_live_handoff["Live chat handoff<br/>WebSocket operator routing"]
  node_billing["Billing and credits<br/>entitlements ledger<br/>[credit_service.py]"]
  node_billing_webhooks["Payment webhooks<br/>verified inbound events"]
end

subgraph group_async["Async processing"]
  node_ingestion["Knowledge pipeline<br/>extract chunk embed index<br/>[pipeline.py]"]
  node_crawl["Crawl orchestration<br/>URL discovery and recrawl"]
  node_worker["ARQ workers<br/>background task executor<br/>[tasks.py]"]
end

subgraph group_data["Data and external systems"]
  node_postgres[("PostgreSQL + pgvector<br/>durable tenant store<br/>[models.py]")]
  node_redis[("Redis<br/>cache queue coordination")]
  node_llm_providers{{"LLM providers<br/>LiteLLM OpenAI Gemini"}}
  node_payments{{"Payment providers<br/>Razorpay and Stripe"}}
  node_asset_store[("Cloudflare R2<br/>uploaded media storage")]
end

node_dashboard -->|"boots through"| node_dashboard_routes
node_dashboard_routes -->|"authenticated requests"| node_workspace_api
node_dashboard -->|"operator socket"| node_live_handoff
node_widget_loader -->|"drives UI modes"| node_widget_state
node_widget_loader -->|"bot-key requests"| node_public_chat
node_widget_state -->|"handoff traffic"| node_live_handoff
node_api_app -->|"mounts"| node_public_chat
node_api_app -->|"mounts"| node_workspace_api
node_public_chat -->|"streams replies"| node_chat_runtime
node_chat_runtime -->|"retrieves context"| node_rag
node_chat_runtime -->|"invokes"| node_llm_providers
node_chat_runtime -->|"persists sessions"| node_postgres
node_rag -->|"vector and keyword search"| node_postgres
node_workspace_api -->|"dispatches long work"| node_worker
node_worker -->|"runs"| node_ingestion
node_worker -->|"runs"| node_crawl
node_ingestion -->|"reads uploads"| node_asset_store
node_ingestion -->|"indexes embeddings"| node_postgres
node_crawl -->|"supplies pages"| node_ingestion
node_live_handoff -->|"cross-process coordination"| node_redis
node_live_handoff -->|"stores routing state"| node_postgres
node_workspace_api -->|"plans and usage"| node_billing
node_billing_webhooks -->|"verified events"| node_billing
node_billing_webhooks -->|"receives events"| node_payments
node_billing -->|"ledger and subscriptions"| node_postgres
node_worker -->|"ARQ queue"| node_redis

click node_dashboard "https://github.com/digibranders/oye-chats-platform/blob/main/app/src/main.jsx"
click node_dashboard_routes "https://github.com/digibranders/oye-chats-platform/blob/main/app/src/app/routes.tsx"
click node_widget_loader "https://github.com/digibranders/oye-chats-platform/blob/main/widget/src/loader.js"
click node_widget_state "https://github.com/digibranders/oye-chats-platform/blob/main/widget/src/lib/chatModeMachine.js"
click node_api_app "https://github.com/digibranders/oye-chats-platform/blob/main/api/app/main.py"
click node_public_chat "https://github.com/digibranders/oye-chats-platform/blob/main/api/app/api/chat_routes.py"
click node_workspace_api "https://github.com/digibranders/oye-chats-platform/blob/main/api/app/api/client_routes.py"
click node_chat_runtime "https://github.com/digibranders/oye-chats-platform/blob/main/api/app/services/llm_service.py"
click node_rag "https://github.com/digibranders/oye-chats-platform/blob/main/api/app/services/rag_service.py"
click node_live_handoff "https://github.com/digibranders/oye-chats-platform/blob/main/api/app/services/live_chat_routing_service.py"
click node_billing "https://github.com/digibranders/oye-chats-platform/blob/main/api/app/services/credit_service.py"
click node_billing_webhooks "https://github.com/digibranders/oye-chats-platform/blob/main/api/app/api/webhook_billing_routes.py"
click node_ingestion "https://github.com/digibranders/oye-chats-platform/blob/main/api/app/ingestion/pipeline.py"
click node_crawl "https://github.com/digibranders/oye-chats-platform/blob/main/api/app/services/crawl_orchestrator.py"
click node_worker "https://github.com/digibranders/oye-chats-platform/blob/main/api/app/worker/tasks.py"
click node_postgres "https://github.com/digibranders/oye-chats-platform/blob/main/api/app/db/models.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_dashboard,node_dashboard_routes,node_widget_loader,node_widget_state toneBlue
class node_api_app,node_public_chat,node_workspace_api,node_chat_runtime,node_rag,node_live_handoff,node_billing,node_billing_webhooks toneAmber
class node_ingestion,node_crawl,node_worker toneMint
class node_postgres,node_redis,node_llm_providers,node_payments,node_asset_store toneRose
```

## Components

### Customer surfaces

- [Dashboard SPA](https://github.com/digibranders/oye-chats-platform/blob/main/app/src/main.jsx): React control plane · Workspace configuration, inbox, analytics, and billing. · `app/src/main.jsx`
- [Protected dashboard shell](https://github.com/digibranders/oye-chats-platform/blob/main/app/src/app/routes.tsx): routing and identity · `app/src/app/routes.tsx`
- [Embed widget](https://github.com/digibranders/oye-chats-platform/blob/main/widget/src/loader.js): loader and React UI · Host-page bootstrap uses the public bot key. · `widget/src/loader.js`
- [Widget chat state](https://github.com/digibranders/oye-chats-platform/blob/main/widget/src/lib/chatModeMachine.js): mode state machine · `widget/src/lib/chatModeMachine.js`

### API runtime

- [FastAPI application](https://github.com/digibranders/oye-chats-platform/blob/main/api/app/main.py): REST SSE WebSocket host · Root-mounted routes and request middleware establish API boundaries. · `api/app/main.py`
- [Public chat APIs](https://github.com/digibranders/oye-chats-platform/blob/main/api/app/api/chat_routes.py): widget and SSE routes · `api/app/api/chat_routes.py`
- [Workspace APIs](https://github.com/digibranders/oye-chats-platform/blob/main/api/app/api/client_routes.py): authenticated routes · `api/app/api/client_routes.py`
- [Chat execution](https://github.com/digibranders/oye-chats-platform/blob/main/api/app/services/llm_service.py): LLM and session orchestration · Applies session state, grounding, quotas, and tenant isolation. · `api/app/services/llm_service.py`
- [RAG retrieval](https://github.com/digibranders/oye-chats-platform/blob/main/api/app/services/rag_service.py): retrieval and relevance · Combines vector and keyword search before answer generation. · `api/app/services/rag_service.py`
- [Live chat handoff](https://github.com/digibranders/oye-chats-platform/blob/main/api/app/services/live_chat_routing_service.py): WebSocket operator routing · `api/app/services/live_chat_routing_service.py`
- [Billing and credits](https://github.com/digibranders/oye-chats-platform/blob/main/api/app/services/credit_service.py): entitlements ledger · Plans and FIFO credits gate feature access and consumption. · `api/app/services/credit_service.py`
- [Payment webhooks](https://github.com/digibranders/oye-chats-platform/blob/main/api/app/api/webhook_billing_routes.py): verified inbound events · Idempotently mutates subscription and invoice state. · `api/app/api/webhook_billing_routes.py`

### Async processing

- [Knowledge pipeline](https://github.com/digibranders/oye-chats-platform/blob/main/api/app/ingestion/pipeline.py): extract chunk embed index · Processes uploaded and crawled content into tenant knowledge. · `api/app/ingestion/pipeline.py`
- [Crawl orchestration](https://github.com/digibranders/oye-chats-platform/blob/main/api/app/services/crawl_orchestrator.py): URL discovery and recrawl · `api/app/services/crawl_orchestrator.py`
- [ARQ workers](https://github.com/digibranders/oye-chats-platform/blob/main/api/app/worker/tasks.py): background task executor · Runs ingestion, crawl, mail, invoice, reconciliation, and scheduled work. · `api/app/worker/tasks.py`

### Data and external systems

- [PostgreSQL + pgvector](https://github.com/digibranders/oye-chats-platform/blob/main/api/app/db/models.py): durable tenant store · System of record for tenants, bots, conversations, knowledge, and billing. · `api/app/db/models.py`
- **Redis**: cache queue coordination · Supports ARQ, rate limits, cache, and live-chat coordination.
- **LLM providers**: LiteLLM OpenAI Gemini · OpenAI is primary with Gemini fallback.
- **Payment providers**: Razorpay and Stripe · Razorpay is primary INR rail; Stripe is fallback.
- **Cloudflare R2**: uploaded media storage

## Connections

- Dashboard SPA → Protected dashboard shell: boots through
- Protected dashboard shell → Workspace APIs: authenticated requests
- Dashboard SPA → Live chat handoff: operator socket
- Embed widget → Widget chat state: drives UI modes
- Embed widget → Public chat APIs: bot-key requests
- Widget chat state → Live chat handoff: handoff traffic
- FastAPI application → Public chat APIs: mounts
- FastAPI application → Workspace APIs: mounts
- Public chat APIs → Chat execution: streams replies
- Chat execution → RAG retrieval: retrieves context
- Chat execution → LLM providers: invokes
- Chat execution → PostgreSQL + pgvector: persists sessions
- RAG retrieval → PostgreSQL + pgvector: vector and keyword search
- Workspace APIs → ARQ workers: dispatches long work
- ARQ workers → Knowledge pipeline: runs
- ARQ workers → Crawl orchestration: runs
- Knowledge pipeline → Cloudflare R2: reads uploads
- Knowledge pipeline → PostgreSQL + pgvector: indexes embeddings
- Crawl orchestration → Knowledge pipeline: supplies pages
- Live chat handoff → Redis: cross-process coordination
- Live chat handoff → PostgreSQL + pgvector: stores routing state
- Workspace APIs → Billing and credits: plans and usage
- Payment webhooks → Billing and credits: verified events
- Payment webhooks → Payment providers: receives events
- Billing and credits → PostgreSQL + pgvector: ledger and subscriptions
- ARQ workers → Redis: ARQ queue
