# farhanhafeez985/echochat architecture

> Architecture diagram of the GitHub repository farhanhafeez985/echochat, 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/farhanhafeez985/echochat
- Repository: https://github.com/farhanhafeez985/echochat

## Overview

#### Purpose and entry points
This is a full-stack knowledge-chat application: users authenticate, ingest documents into a retrieval index, and chat with an LLM augmented by retrieved source material. The backend service entry point is `backend/app/main.py`; its orchestration layer is `backend/app/agent.py`. The Next.js frontend starts at `frontend/app/layout.tsx` and exposes the main chat route at `frontend/app/page.tsx`.

#### Backend API, configuration, and persistence
The backend is a Python application with centralized runtime configuration in `backend/app/config.py` and database setup in `backend/app/database.py`. API serialization is separated in `backend/app/serializers.py`. These form the boundary between HTTP-facing application code and persistent/authenticated domain state.

#### Authentication and user boundary
Authentication is a dedicated backend subsystem: domain models and request/response schemas live in `backend/app/auth/models.py` and `backend/app/auth/schemas.py`; credential/token handling is in `backend/app/auth/security.py`. Request-level identity enforcement is mediated through `backend/app/auth/dependencies.py`, while business operations are concentrated in `backend/app/auth/service.py`. The frontend consumes this boundary through `frontend/hooks/useAuth.ts` and `frontend/lib/auth.ts`.

#### Document ingestion pipeline
Knowledge documents pass through extraction, chunking, and ingestion stages: `backend/app/documents/extractor.py`, `backend/app/documents/chunker.py`, and `backend/app/documents/ingestion.py`. Ingestion converts source content into retrieval-ready chunks and hands them to the RAG storage/indexing layer. The knowledge-management UI is rooted at `frontend/app/knowledge/page.tsx`, with item-level navigation under `frontend/app/knowledge/[id]/page.tsx`.

#### RAG indexing and retrieval
The RAG subsystem owns embedding generation, vector-store access, retrieval, reranking, and context assembly. Core paths are `backend/app/rag/embeddings.py`, `backend/app/rag/qdrant.py`, `backend/app/rag/retriever.py`, `backend/app/rag/reranker.py`, and `backend/app/rag/context.py`. `backend/app/rag/repository.py` provides a repository boundary around indexed data, while `backend/app/rag/models.py` defines retrieval-domain data. Qdrant is the material external vector database; `backend/app/rag/fake_retriever.py` suggests an alternate non-production retrieval implementation.

#### LLM agent and conversational state
`backend/app/agent.py` coordinates user input, retrieval context, prompts, tool use, and model calls. LLM-provider integration is isolated in `backend/app/llm/llm_client.py`, and callable agent capabilities are defined in `backend/app/llm/tools.py`. Prompt construction is centralized in `backend/app/prompts.py`; conversational/session memory is represented by `backend/app/memory.py`. The key control flow is: authenticated user message → agent → retrieve/rerank/context → LLM call and optional tools → answer with source references.

#### Frontend interaction layer
The frontend is a TypeScript/Next.js application with chat state and transport logic in `frontend/hooks/useChat.ts` and `frontend/lib/api.ts`. `frontend/components/ChatContainer.tsx` composes message display and input, while `frontend/components/SourceReferences.tsx` renders retrieval provenance returned by the backend. Voice interaction is a distinct client capability via `frontend/hooks/useVoice.ts` and `frontend/components/VoiceCallModal.tsx`. Account and application navigation are surfaced through `frontend/app/login/page.tsx`, `frontend/app/signup/page.tsx`, `frontend/app/profile/page.tsx`, and `frontend/app/settings/page.tsx`.

Main entry points: [`backend/app/main.py`](https://github.com/farhanhafeez985/echochat/blob/main/backend/app/main.py) (Backend API), [`backend/app/agent.py`](https://github.com/farhanhafeez985/echochat/blob/main/backend/app/agent.py) (Chat agent), [`frontend/components/ChatContainer.tsx`](https://github.com/farhanhafeez985/echochat/blob/main/frontend/components/ChatContainer.tsx) (Chat UI), [`frontend/app/layout.tsx`](https://github.com/farhanhafeez985/echochat/blob/main/frontend/app/layout.tsx) (App shell & routes)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Next.js Frontend"]
  node_app_shell["App shell &amp; routes<br/>Next.js routes<br/>[layout.tsx]"]
  node_chat_ui["Chat UI<br/>React component<br/>[ChatContainer.tsx]"]
  node_chat_transport["Chat state &amp; API<br/>frontend transport<br/>[useChat.ts]"]
  node_auth_client["Auth client<br/>frontend auth<br/>[useAuth.ts]"]
  node_knowledge_ui["Knowledge UI<br/>Next.js route<br/>[page.tsx]"]
  node_source_references["Source references<br/>React component"]
  node_voice_ui["Voice interaction<br/>client capability<br/>[useVoice.ts]"]
end

subgraph group_backend["Backend Application"]
  node_api_service["Backend API<br/>Python service<br/>[main.py]"]
  node_api_boundary["Serialization &amp; database<br/>application boundary<br/>[serializers.py]"]
  node_agent{{"Chat agent<br/>orchestrator<br/>[agent.py]"}}
  node_memory["Conversation memory<br/>session state<br/>[memory.py]"]
  node_prompts["Prompt builder<br/>prompting<br/>[prompts.py]"]
  node_llm{{"LLM client &amp; tools<br/>provider integration<br/>[llm_client.py]"}}
end

subgraph group_auth["Authentication"]
  node_auth_boundary["Auth service &amp; identity<br/>authentication boundary<br/>[service.py]"]
end

subgraph group_rag["Knowledge Retrieval"]
  node_document_ingestion["Document ingestion<br/>ingestion pipeline<br/>[ingestion.py]"]
  node_retrieval["Retrieve &amp; rerank<br/>retrieval pipeline<br/>[retriever.py]"]
  node_context["Context assembly<br/>RAG context<br/>[context.py]"]
  node_rag_repository["RAG repository<br/>index repository<br/>[repository.py]"]
  node_qdrant[("Qdrant vector store<br/>external vector database<br/>[qdrant.py]")]
end

node_app_shell -->|"renders"| node_chat_ui
node_chat_ui -->|"messages"| node_chat_transport
node_chat_transport -->|"chat API"| node_api_service
node_app_shell -->|"routes to"| node_knowledge_ui
node_knowledge_ui -->|"document API"| node_api_service
node_app_shell -->|"auth flows"| node_auth_client
node_auth_client -->|"authenticate"| node_api_service
node_chat_ui -->|"shows provenance"| node_source_references
node_voice_ui -->|"voice input"| node_chat_ui
node_api_service -->|"enforces identity"| node_auth_boundary
node_api_service -->|"serializes &amp; persists"| node_api_boundary
node_api_service -->|"authenticated message"| node_agent
node_api_service -->|"uploads documents"| node_document_ingestion
node_document_ingestion -->|"indexes chunks"| node_rag_repository
node_rag_repository -->|"stores vectors"| node_qdrant
node_agent -->|"reads &amp; updates"| node_memory
node_agent -->|"queries"| node_retrieval
node_retrieval -->|"searches"| node_rag_repository
node_retrieval -->|"ranked chunks"| node_context
node_agent -->|"builds prompt"| node_prompts
node_context -->|"source context"| node_prompts
node_agent -->|"model call &amp; tools"| node_llm
node_agent -->|"answer &amp; sources"| node_api_service

click node_app_shell "https://github.com/farhanhafeez985/echochat/blob/main/frontend/app/layout.tsx"
click node_chat_ui "https://github.com/farhanhafeez985/echochat/blob/main/frontend/components/ChatContainer.tsx"
click node_chat_transport "https://github.com/farhanhafeez985/echochat/blob/main/frontend/hooks/useChat.ts"
click node_auth_client "https://github.com/farhanhafeez985/echochat/blob/main/frontend/hooks/useAuth.ts"
click node_knowledge_ui "https://github.com/farhanhafeez985/echochat/blob/main/frontend/app/knowledge/page.tsx"
click node_source_references "https://github.com/farhanhafeez985/echochat/blob/main/frontend/components/SourceReferences.tsx"
click node_voice_ui "https://github.com/farhanhafeez985/echochat/blob/main/frontend/hooks/useVoice.ts"
click node_api_service "https://github.com/farhanhafeez985/echochat/blob/main/backend/app/main.py"
click node_api_boundary "https://github.com/farhanhafeez985/echochat/blob/main/backend/app/serializers.py"
click node_agent "https://github.com/farhanhafeez985/echochat/blob/main/backend/app/agent.py"
click node_memory "https://github.com/farhanhafeez985/echochat/blob/main/backend/app/memory.py"
click node_prompts "https://github.com/farhanhafeez985/echochat/blob/main/backend/app/prompts.py"
click node_llm "https://github.com/farhanhafeez985/echochat/blob/main/backend/app/llm/llm_client.py"
click node_auth_boundary "https://github.com/farhanhafeez985/echochat/blob/main/backend/app/auth/service.py"
click node_document_ingestion "https://github.com/farhanhafeez985/echochat/blob/main/backend/app/documents/ingestion.py"
click node_retrieval "https://github.com/farhanhafeez985/echochat/blob/main/backend/app/rag/retriever.py"
click node_context "https://github.com/farhanhafeez985/echochat/blob/main/backend/app/rag/context.py"
click node_rag_repository "https://github.com/farhanhafeez985/echochat/blob/main/backend/app/rag/repository.py"
click node_qdrant "https://github.com/farhanhafeez985/echochat/blob/main/backend/app/rag/qdrant.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_app_shell,node_chat_ui,node_chat_transport,node_auth_client,node_knowledge_ui,node_source_references,node_voice_ui toneBlue
class node_api_service,node_api_boundary,node_agent,node_memory,node_prompts,node_llm toneAmber
class node_auth_boundary toneMint
class node_document_ingestion,node_retrieval,node_context,node_rag_repository,node_qdrant toneRose
```

## Components

### Next.js Frontend

- [App shell & routes](https://github.com/farhanhafeez985/echochat/blob/main/frontend/app/layout.tsx): Next.js routes · Chat, knowledge, account, and navigation routes. · `frontend/app/layout.tsx`
- [Chat UI](https://github.com/farhanhafeez985/echochat/blob/main/frontend/components/ChatContainer.tsx): React component · `frontend/components/ChatContainer.tsx`
- [Chat state & API](https://github.com/farhanhafeez985/echochat/blob/main/frontend/hooks/useChat.ts): frontend transport · `frontend/hooks/useChat.ts`
- [Auth client](https://github.com/farhanhafeez985/echochat/blob/main/frontend/hooks/useAuth.ts): frontend auth · `frontend/hooks/useAuth.ts`
- [Knowledge UI](https://github.com/farhanhafeez985/echochat/blob/main/frontend/app/knowledge/page.tsx): Next.js route · `frontend/app/knowledge/page.tsx`
- [Source references](https://github.com/farhanhafeez985/echochat/blob/main/frontend/components/SourceReferences.tsx): React component · `frontend/components/SourceReferences.tsx`
- [Voice interaction](https://github.com/farhanhafeez985/echochat/blob/main/frontend/hooks/useVoice.ts): client capability · `frontend/hooks/useVoice.ts`

### Backend Application

- [Backend API](https://github.com/farhanhafeez985/echochat/blob/main/backend/app/main.py): Python service · `backend/app/main.py`
- [Serialization & database](https://github.com/farhanhafeez985/echochat/blob/main/backend/app/serializers.py): application boundary · HTTP serialization and persistent domain setup. · `backend/app/serializers.py`
- [Chat agent](https://github.com/farhanhafeez985/echochat/blob/main/backend/app/agent.py): orchestrator · Coordinates retrieval, prompting, tools, and responses. · `backend/app/agent.py`
- [Conversation memory](https://github.com/farhanhafeez985/echochat/blob/main/backend/app/memory.py): session state · `backend/app/memory.py`
- [Prompt builder](https://github.com/farhanhafeez985/echochat/blob/main/backend/app/prompts.py): prompting · `backend/app/prompts.py`
- [LLM client & tools](https://github.com/farhanhafeez985/echochat/blob/main/backend/app/llm/llm_client.py): provider integration · `backend/app/llm/llm_client.py`

### Authentication

- [Auth service & identity](https://github.com/farhanhafeez985/echochat/blob/main/backend/app/auth/service.py): authentication boundary · Validates credentials and enforces request identity. · `backend/app/auth/service.py`

### Knowledge Retrieval

- [Document ingestion](https://github.com/farhanhafeez985/echochat/blob/main/backend/app/documents/ingestion.py): ingestion pipeline · Extracts and chunks documents for indexing. · `backend/app/documents/ingestion.py`
- [Retrieve & rerank](https://github.com/farhanhafeez985/echochat/blob/main/backend/app/rag/retriever.py): retrieval pipeline · Finds and ranks relevant indexed chunks. · `backend/app/rag/retriever.py`
- [Context assembly](https://github.com/farhanhafeez985/echochat/blob/main/backend/app/rag/context.py): RAG context · `backend/app/rag/context.py`
- [RAG repository](https://github.com/farhanhafeez985/echochat/blob/main/backend/app/rag/repository.py): index repository · Boundary around embeddings and indexed data. · `backend/app/rag/repository.py`
- [Qdrant vector store](https://github.com/farhanhafeez985/echochat/blob/main/backend/app/rag/qdrant.py): external vector database · `backend/app/rag/qdrant.py`

## Connections

- App shell & routes → Chat UI: renders
- Chat UI → Chat state & API: messages
- Chat state & API → Backend API: chat API
- App shell & routes → Knowledge UI: routes to
- Knowledge UI → Backend API: document API
- App shell & routes → Auth client: auth flows
- Auth client → Backend API: authenticate
- Chat UI → Source references: shows provenance
- Voice interaction → Chat UI: voice input
- Backend API → Auth service & identity: enforces identity
- Backend API → Serialization & database: serializes & persists
- Backend API → Chat agent: authenticated message
- Backend API → Document ingestion: uploads documents
- Document ingestion → RAG repository: indexes chunks
- RAG repository → Qdrant vector store: stores vectors
- Chat agent → Conversation memory: reads & updates
- Chat agent → Retrieve & rerank: queries
- Retrieve & rerank → RAG repository: searches
- Retrieve & rerank → Context assembly: ranked chunks
- Chat agent → Prompt builder: builds prompt
- Context assembly → Prompt builder: source context
- Chat agent → LLM client & tools: model call & tools
- Chat agent → Backend API: answer & sources
