# tengsal/rag architecture

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

- Interactive diagram: https://gitdiagram.com/tengsal/rag
- Repository: https://github.com/tengsal/rag

## Overview

This repository combines a campus knowledge assistant and administration portal, a Python retrieval/sentiment backend, a separate authentication service, and a career-counselling voice agent. The visible user journey is campus chat through retrieval to an answer; other major capabilities include managing documents and notices, analytics, authentication, and outbound counselling calls. Several links among the frontend, Python API, and voice services are only suggested by the tree, so they are left unverified rather than asserted.

Main entry points: [`career_counselling_ai_agent/src/worker.ts`](https://github.com/tengsal/rag/blob/main/career_counselling_ai_agent/src/worker.ts) (Voice worker), [`auth-server/src/services/auth.service.ts`](https://github.com/tengsal/rag/blob/main/auth-server/src/services/auth.service.ts) (Auth service), [`auth-server/src/routes/auth.routes.ts`](https://github.com/tengsal/rag/blob/main/auth-server/src/routes/auth.routes.ts) (Auth routes), [`src/components/chat-content.tsx`](https://github.com/tengsal/rag/blob/main/src/components/chat-content.tsx) (Chat interface)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_campus["Campus Assistant"]
  node_chatui["Chat interface<br/>[chat-content.tsx]"]
  node_chatapi["Chat endpoint<br/>[route.ts]"]
  node_ragclient["RAG client<br/>[rag-api.ts]"]
  node_admin["Admin portal<br/>[page.tsx]"]
  node_contentapi["Content APIs<br/>[route.ts]"]
  node_campusdb[("Campus data models<br/>[index.ts]")]
  node_analytics["Analytics endpoint<br/>[route.ts]"]
  node_sentimentapi["Sentiment endpoint<br/>[route.ts]"]
end

subgraph group_retrieval["Retrieval Backend"]
  node_pyapi["Python API<br/>[api.py]"]
  node_mainentry["Retriever entry<br/>[main.py]"]
  node_queryparser["Query parser<br/>[query_parser.py]"]
  node_structured["Structured retrieval"]
  node_ragretriever["RAG retrieval<br/>[rag_retriever.py]"]
  node_sentiment["Sentiment analysis<br/>[analyzer.py]"]
  node_sentimentcollect["Sentiment collection<br/>[collector.py]"]
end

subgraph group_auth["Authentication"]
  node_authroutes["Auth routes<br/>[auth.routes.ts]"]
  node_authcontroller["Auth controller<br/>[auth.controller.ts]"]
  node_authservice["Auth service<br/>[auth.service.ts]"]
  node_users[("User records<br/>[user.model.ts]")]
  node_jwt["JWT utilities<br/>[jwt.ts]"]
  node_authmiddleware["JWT middleware<br/>[auth.middleware.ts]"]
end

subgraph group_voice["Voice Counselling"]
  node_voiceapi["Call dispatch API<br/>[server.ts]"]
  node_voiceworker["Voice worker<br/>[worker.ts]"]
  node_stt["Speech recognition<br/>[deepgram.ts]"]
  node_llm["Conversation model<br/>[llm.ts]"]
  node_tts["Speech synthesis<br/>[tts.ts]"]
  node_leadrecords[("Counselling leads<br/>[AdtuLead.ts]")]
end

node_visitor(("Campus user"))
node_calloperator(("Call operator"))
node_google["Google OAuth"]
node_livekit["LiveKit calling"]

node_visitor -.->|"asks"| node_chatui
node_chatui -.->|"submits query"| node_chatapi
node_chatapi -.->|"requests retrieval"| node_ragclient
node_ragclient -.->|"calls API"| node_pyapi
node_mainentry -->|"invokes"| node_structured
node_structured -->|"parses queries"| node_queryparser
node_pyapi -.->|"uses retrieval"| node_ragretriever
node_admin -.->|"manages content"| node_contentapi
node_contentapi -->|"reads and writes"| node_campusdb
node_analytics -->|"reads"| node_campusdb
node_sentimentapi -.->|"requests analysis"| node_sentiment
node_sentimentcollect -.->|"collects for analysis"| node_sentiment
node_authroutes -->|"routes requests"| node_authcontroller
node_authcontroller -->|"delegates"| node_authservice
node_authservice -->|"reads and writes"| node_users
node_authservice -->|"generates tokens"| node_jwt
node_authroutes -->|"protects routes"| node_authmiddleware
node_authmiddleware -->|"verifies tokens"| node_jwt
node_google -->|"verifies credentials"| node_authroutes
node_calloperator -.->|"dispatches calls"| node_voiceapi
node_voiceapi -.->|"dispatches agent"| node_livekit
node_livekit -.->|"starts worker"| node_voiceworker
node_voiceworker -->|"transcribes speech"| node_stt
node_voiceworker -->|"generates replies"| node_llm
node_voiceworker -->|"synthesizes speech"| node_tts
node_voiceworker -->|"records leads"| node_leadrecords

click node_chatui "https://github.com/tengsal/rag/blob/main/src/components/chat-content.tsx"
click node_chatapi "https://github.com/tengsal/rag/blob/main/src/app/api/chat/route.ts"
click node_ragclient "https://github.com/tengsal/rag/blob/main/src/lib/rag-api.ts"
click node_pyapi "https://github.com/tengsal/rag/blob/main/backend/api.py"
click node_mainentry "https://github.com/tengsal/rag/blob/main/main.py"
click node_queryparser "https://github.com/tengsal/rag/blob/main/backend/retrieval/query_parser.py"
click node_structured "https://github.com/tengsal/rag/blob/main/backend/retrieval/structured_retriever.py"
click node_ragretriever "https://github.com/tengsal/rag/blob/main/backend/retrieval/rag_retriever.py"
click node_sentiment "https://github.com/tengsal/rag/blob/main/backend/sentiment/analyzer.py"
click node_sentimentcollect "https://github.com/tengsal/rag/blob/main/backend/sentiment/collector.py"
click node_admin "https://github.com/tengsal/rag/blob/main/src/app/admin/page.tsx"
click node_contentapi "https://github.com/tengsal/rag/blob/main/src/app/api/documents/route.ts"
click node_campusdb "https://github.com/tengsal/rag/blob/main/src/lib/models/index.ts"
click node_analytics "https://github.com/tengsal/rag/blob/main/src/app/api/dashboard/route.ts"
click node_sentimentapi "https://github.com/tengsal/rag/blob/main/src/app/api/sentiment/route.ts"
click node_authroutes "https://github.com/tengsal/rag/blob/main/auth-server/src/routes/auth.routes.ts"
click node_authcontroller "https://github.com/tengsal/rag/blob/main/auth-server/src/controllers/auth.controller.ts"
click node_authservice "https://github.com/tengsal/rag/blob/main/auth-server/src/services/auth.service.ts"
click node_users "https://github.com/tengsal/rag/blob/main/auth-server/src/models/user.model.ts"
click node_jwt "https://github.com/tengsal/rag/blob/main/auth-server/src/utils/jwt.ts"
click node_authmiddleware "https://github.com/tengsal/rag/blob/main/auth-server/src/middleware/auth.middleware.ts"
click node_voiceapi "https://github.com/tengsal/rag/blob/main/career_counselling_ai_agent/src/server.ts"
click node_voiceworker "https://github.com/tengsal/rag/blob/main/career_counselling_ai_agent/src/worker.ts"
click node_stt "https://github.com/tengsal/rag/blob/main/career_counselling_ai_agent/src/plugins/deepgram.ts"
click node_llm "https://github.com/tengsal/rag/blob/main/career_counselling_ai_agent/src/plugins/llm.ts"
click node_tts "https://github.com/tengsal/rag/blob/main/career_counselling_ai_agent/src/plugins/tts.ts"
click node_leadrecords "https://github.com/tengsal/rag/blob/main/career_counselling_ai_agent/src/models/AdtuLead.ts"

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_chatui,node_chatapi,node_ragclient,node_admin,node_contentapi,node_campusdb,node_analytics,node_sentimentapi,node_visitor toneBlue
class node_pyapi,node_mainentry,node_queryparser,node_structured,node_ragretriever,node_sentiment,node_sentimentcollect toneAmber
class node_authroutes,node_authcontroller,node_authservice,node_users,node_jwt,node_authmiddleware toneMint
class node_voiceapi,node_voiceworker,node_stt,node_llm,node_tts,node_leadrecords toneRose
class node_calloperator,node_google,node_livekit toneIndigo
```

## Components

### Campus Assistant

- [Chat interface](https://github.com/tengsal/rag/blob/main/src/components/chat-content.tsx): `src/components/chat-content.tsx`
- [Chat endpoint](https://github.com/tengsal/rag/blob/main/src/app/api/chat/route.ts): `src/app/api/chat/route.ts`
- [RAG client](https://github.com/tengsal/rag/blob/main/src/lib/rag-api.ts): `src/lib/rag-api.ts`
- [Admin portal](https://github.com/tengsal/rag/blob/main/src/app/admin/page.tsx): `src/app/admin/page.tsx`
- [Content APIs](https://github.com/tengsal/rag/blob/main/src/app/api/documents/route.ts): `src/app/api/documents/route.ts`
- [Campus data models](https://github.com/tengsal/rag/blob/main/src/lib/models/index.ts): `src/lib/models/index.ts`
- [Analytics endpoint](https://github.com/tengsal/rag/blob/main/src/app/api/dashboard/route.ts): `src/app/api/dashboard/route.ts`
- [Sentiment endpoint](https://github.com/tengsal/rag/blob/main/src/app/api/sentiment/route.ts): `src/app/api/sentiment/route.ts`

### Retrieval Backend

- [Python API](https://github.com/tengsal/rag/blob/main/backend/api.py): `backend/api.py`
- [Retriever entry](https://github.com/tengsal/rag/blob/main/main.py): `main.py`
- [Query parser](https://github.com/tengsal/rag/blob/main/backend/retrieval/query_parser.py): `backend/retrieval/query_parser.py`
- [Structured retrieval](https://github.com/tengsal/rag/blob/main/backend/retrieval/structured_retriever.py): `backend/retrieval/structured_retriever.py`
- [RAG retrieval](https://github.com/tengsal/rag/blob/main/backend/retrieval/rag_retriever.py): `backend/retrieval/rag_retriever.py`
- [Sentiment analysis](https://github.com/tengsal/rag/blob/main/backend/sentiment/analyzer.py): `backend/sentiment/analyzer.py`
- [Sentiment collection](https://github.com/tengsal/rag/blob/main/backend/sentiment/collector.py): `backend/sentiment/collector.py`

### Authentication

- [Auth routes](https://github.com/tengsal/rag/blob/main/auth-server/src/routes/auth.routes.ts): `auth-server/src/routes/auth.routes.ts`
- [Auth controller](https://github.com/tengsal/rag/blob/main/auth-server/src/controllers/auth.controller.ts): `auth-server/src/controllers/auth.controller.ts`
- [Auth service](https://github.com/tengsal/rag/blob/main/auth-server/src/services/auth.service.ts): `auth-server/src/services/auth.service.ts`
- [User records](https://github.com/tengsal/rag/blob/main/auth-server/src/models/user.model.ts): `auth-server/src/models/user.model.ts`
- [JWT utilities](https://github.com/tengsal/rag/blob/main/auth-server/src/utils/jwt.ts): `auth-server/src/utils/jwt.ts`
- [JWT middleware](https://github.com/tengsal/rag/blob/main/auth-server/src/middleware/auth.middleware.ts): `auth-server/src/middleware/auth.middleware.ts`

### Voice Counselling

- [Call dispatch API](https://github.com/tengsal/rag/blob/main/career_counselling_ai_agent/src/server.ts): `career_counselling_ai_agent/src/server.ts`
- [Voice worker](https://github.com/tengsal/rag/blob/main/career_counselling_ai_agent/src/worker.ts): `career_counselling_ai_agent/src/worker.ts`
- [Speech recognition](https://github.com/tengsal/rag/blob/main/career_counselling_ai_agent/src/plugins/deepgram.ts): `career_counselling_ai_agent/src/plugins/deepgram.ts`
- [Conversation model](https://github.com/tengsal/rag/blob/main/career_counselling_ai_agent/src/plugins/llm.ts): `career_counselling_ai_agent/src/plugins/llm.ts`
- [Speech synthesis](https://github.com/tengsal/rag/blob/main/career_counselling_ai_agent/src/plugins/tts.ts): `career_counselling_ai_agent/src/plugins/tts.ts`
- [Counselling leads](https://github.com/tengsal/rag/blob/main/career_counselling_ai_agent/src/models/AdtuLead.ts): `career_counselling_ai_agent/src/models/AdtuLead.ts`

### Other components

- **Campus user**
- **Call operator**
- **Google OAuth**
- **LiveKit calling**

## Connections

- Campus user → Chat interface: asks
- Chat interface → Chat endpoint: submits query
- Chat endpoint → RAG client: requests retrieval
- RAG client → Python API: calls API
- Retriever entry → Structured retrieval: invokes (evidence: `main.py`)
- Structured retrieval → Query parser: parses queries (evidence: `backend/retrieval/structured_retriever.py`)
- Python API → RAG retrieval: uses retrieval
- Admin portal → Content APIs: manages content
- Content APIs → Campus data models: reads and writes (evidence: `src/app/api/documents/route.ts`)
- Analytics endpoint → Campus data models: reads (evidence: `src/app/api/dashboard/route.ts`)
- Sentiment endpoint → Sentiment analysis: requests analysis
- Sentiment collection → Sentiment analysis: collects for analysis
- Auth routes → Auth controller: routes requests (evidence: `auth-server/src/routes/auth.routes.ts`)
- Auth controller → Auth service: delegates (evidence: `auth-server/src/controllers/auth.controller.ts`)
- Auth service → User records: reads and writes (evidence: `auth-server/src/services/auth.service.ts`)
- Auth service → JWT utilities: generates tokens (evidence: `auth-server/src/services/auth.service.ts`)
- Auth routes → JWT middleware: protects routes (evidence: `auth-server/src/routes/auth.routes.ts`)
- JWT middleware → JWT utilities: verifies tokens (evidence: `auth-server/src/middleware/auth.middleware.ts`)
- Google OAuth → Auth routes: verifies credentials
- Call operator → Call dispatch API: dispatches calls
- Call dispatch API → LiveKit calling: dispatches agent
- LiveKit calling → Voice worker: starts worker
- Voice worker → Speech recognition: transcribes speech (evidence: `career_counselling_ai_agent/src/worker.ts`)
- Voice worker → Conversation model: generates replies (evidence: `career_counselling_ai_agent/src/worker.ts`)
- Voice worker → Speech synthesis: synthesizes speech (evidence: `career_counselling_ai_agent/src/worker.ts`)
- Voice worker → Counselling leads: records leads (evidence: `career_counselling_ai_agent/src/worker.ts`)
