# e-wakil/legalgpt architecture

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

- Interactive diagram: https://gitdiagram.com/e-wakil/legalgpt
- Repository: https://github.com/e-wakil/legalgpt

## Overview

LegalGPT Nepal is a web legal-advice product: a citizen signs in, opens a protected chat, submits a legal question, and receives a streamed AI response. The sampled backend shows chat routing, persistence-related models, and an HTTP inference client; the README documents RAG retrieval and citations, but those internals are not present in the tree. The AI worker and its legal persona are separate runtime components. Edges reflect shown imports/calls or README architecture; unshown RAG and streaming internals are not inferred.

Main entry points: [`backend/app/api/v1/endpoints/chat.py`](https://github.com/e-wakil/legalgpt/blob/main/backend/app/api/v1/endpoints/chat.py) (Chat endpoint), [`ai/ai_worker.py`](https://github.com/e-wakil/legalgpt/blob/main/ai/ai_worker.py) (AI worker), [`backend/app/models/models.py`](https://github.com/e-wakil/legalgpt/blob/main/backend/app/models/models.py) (User and chat records), [`frontend/src/App.tsx`](https://github.com/e-wakil/legalgpt/blob/main/frontend/src/App.tsx) (App bootstrap)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Web experience"]
  node_app["App bootstrap<br/>[App.tsx]"]
  node_routes["Page routing<br/>[Router.tsx]"]
  node_chatpage["Chat page<br/>[ChatPage.tsx]"]
  node_chatui["Chat interface"]
  node_sidebar["Conversation sidebar<br/>[SidebarSection.tsx]"]
  node_websocket["Chat WebSocket hook"]
end

subgraph group_identity["Identity and access"]
  node_protected["Chat access gate"]
  node_userstore["User state<br/>[userStore.tsx]"]
  node_httpclient["HTTP client<br/>[axiosInstance.ts]"]
  node_authapi["Google sign-in API<br/>[auth.py]"]
  node_security["Token security<br/>[security.py]"]
end

subgraph group_backend["API and chat"]
  node_api["API router<br/>[api.py]"]
  node_chatendpoint["Chat endpoint<br/>[chat.py]"]
  node_dependencies["Auth dependencies<br/>[deps.py]"]
  node_chatservice["Chat service<br/>[chat_service.py]"]
  node_aiservice["AI inference client<br/>[ai_service.py]"]
  node_schemas["API schemas<br/>[schemas.py]"]
end

subgraph group_data["Application data"]
  node_database[("SQL database<br/>[database.py]")]
  node_models["User and chat records<br/>[models.py]"]
end

subgraph group_inference["Legal inference"]
  node_worker["AI worker<br/>[ai_worker.py]"]
  node_persona["Legal persona prompt<br/>[legal_persona.py]"]
  node_legalcorpus[("Legal corpus retrieval")]
  node_llm["Mistral model"]
end

node_citizen(("Citizen"))

node_citizen -->|"uses"| node_app
node_app -->|"renders"| node_routes
node_routes -->|"guards chat"| node_protected
node_protected -->|"checks access"| node_httpclient
node_routes -->|"routes to"| node_chatpage
node_chatpage -->|"renders"| node_sidebar
node_chatpage -->|"renders"| node_chatui
node_app -->|"checks session"| node_httpclient
node_app -->|"updates user"| node_userstore
node_sidebar -->|"loads conversations"| node_httpclient
node_chatui -.->|"uses chat stream"| node_websocket
node_httpclient -.->|"calls API"| node_api
node_authapi -->|"verifies Google token"| node_security
node_authapi -->|"reads and creates user"| node_models
node_authapi -->|"gets session"| node_database
node_api -->|"registers routes"| node_chatendpoint
node_chatendpoint -->|"authenticates"| node_dependencies
node_chatendpoint -->|"requests inference"| node_aiservice
node_chatendpoint -->|"uses chat records"| node_models
node_chatendpoint -->|"uses schemas"| node_schemas
node_dependencies -->|"queries session"| node_database
node_dependencies -->|"loads user"| node_models
node_models -->|"maps records"| node_database
node_aiservice -.->|"posts query"| node_worker
node_worker -->|"uses prompt"| node_persona
node_worker -.->|"retrieves context"| node_legalcorpus
node_worker -.->|"generates answer"| node_llm
node_worker -.->|"streams tokens"| node_aiservice
node_chatservice -.->|"supports chat records"| node_models

click node_app "https://github.com/e-wakil/legalgpt/blob/main/frontend/src/App.tsx"
click node_routes "https://github.com/e-wakil/legalgpt/blob/main/frontend/src/Routes/Router.tsx"
click node_protected "https://github.com/e-wakil/legalgpt/blob/main/frontend/src/Routes/ProtectedRoutes.tsx"
click node_chatpage "https://github.com/e-wakil/legalgpt/blob/main/frontend/src/pages/ChatPage.tsx"
click node_chatui "https://github.com/e-wakil/legalgpt/blob/main/frontend/src/sections/chat/MessageInterfaceSection.tsx"
click node_sidebar "https://github.com/e-wakil/legalgpt/blob/main/frontend/src/sections/chat/SidebarSection.tsx"
click node_websocket "https://github.com/e-wakil/legalgpt/blob/main/frontend/src/hooks/useChatWebSocket.ts"
click node_userstore "https://github.com/e-wakil/legalgpt/blob/main/frontend/src/store/userStore.tsx"
click node_httpclient "https://github.com/e-wakil/legalgpt/blob/main/frontend/src/api/axiosInstance.ts"
click node_authapi "https://github.com/e-wakil/legalgpt/blob/main/backend/app/api/v1/endpoints/auth.py"
click node_security "https://github.com/e-wakil/legalgpt/blob/main/backend/app/core/security.py"
click node_api "https://github.com/e-wakil/legalgpt/blob/main/backend/app/api/v1/api.py"
click node_chatendpoint "https://github.com/e-wakil/legalgpt/blob/main/backend/app/api/v1/endpoints/chat.py"
click node_dependencies "https://github.com/e-wakil/legalgpt/blob/main/backend/app/api/deps.py"
click node_chatservice "https://github.com/e-wakil/legalgpt/blob/main/backend/app/services/chat_service.py"
click node_aiservice "https://github.com/e-wakil/legalgpt/blob/main/backend/app/services/ai_service.py"
click node_database "https://github.com/e-wakil/legalgpt/blob/main/backend/app/core/database.py"
click node_models "https://github.com/e-wakil/legalgpt/blob/main/backend/app/models/models.py"
click node_schemas "https://github.com/e-wakil/legalgpt/blob/main/backend/app/schemas/schemas.py"
click node_worker "https://github.com/e-wakil/legalgpt/blob/main/ai/ai_worker.py"
click node_persona "https://github.com/e-wakil/legalgpt/blob/main/ai/prompts/legal_persona.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,node_routes,node_chatpage,node_chatui,node_sidebar,node_websocket toneBlue
class node_protected,node_userstore,node_httpclient,node_authapi,node_security toneAmber
class node_api,node_chatendpoint,node_dependencies,node_chatservice,node_aiservice,node_schemas toneMint
class node_database,node_models toneRose
class node_worker,node_persona,node_legalcorpus,node_llm,node_citizen toneIndigo
```

## Components

### Web experience

- [App bootstrap](https://github.com/e-wakil/legalgpt/blob/main/frontend/src/App.tsx): `frontend/src/App.tsx`
- [Page routing](https://github.com/e-wakil/legalgpt/blob/main/frontend/src/Routes/Router.tsx): `frontend/src/Routes/Router.tsx`
- [Chat page](https://github.com/e-wakil/legalgpt/blob/main/frontend/src/pages/ChatPage.tsx): `frontend/src/pages/ChatPage.tsx`
- [Chat interface](https://github.com/e-wakil/legalgpt/blob/main/frontend/src/sections/chat/MessageInterfaceSection.tsx): `frontend/src/sections/chat/MessageInterfaceSection.tsx`
- [Conversation sidebar](https://github.com/e-wakil/legalgpt/blob/main/frontend/src/sections/chat/SidebarSection.tsx): `frontend/src/sections/chat/SidebarSection.tsx`
- [Chat WebSocket hook](https://github.com/e-wakil/legalgpt/blob/main/frontend/src/hooks/useChatWebSocket.ts): `frontend/src/hooks/useChatWebSocket.ts`

### Identity and access

- [Chat access gate](https://github.com/e-wakil/legalgpt/blob/main/frontend/src/Routes/ProtectedRoutes.tsx): `frontend/src/Routes/ProtectedRoutes.tsx`
- [User state](https://github.com/e-wakil/legalgpt/blob/main/frontend/src/store/userStore.tsx): `frontend/src/store/userStore.tsx`
- [HTTP client](https://github.com/e-wakil/legalgpt/blob/main/frontend/src/api/axiosInstance.ts): `frontend/src/api/axiosInstance.ts`
- [Google sign-in API](https://github.com/e-wakil/legalgpt/blob/main/backend/app/api/v1/endpoints/auth.py): `backend/app/api/v1/endpoints/auth.py`
- [Token security](https://github.com/e-wakil/legalgpt/blob/main/backend/app/core/security.py): `backend/app/core/security.py`

### API and chat

- [API router](https://github.com/e-wakil/legalgpt/blob/main/backend/app/api/v1/api.py): `backend/app/api/v1/api.py`
- [Chat endpoint](https://github.com/e-wakil/legalgpt/blob/main/backend/app/api/v1/endpoints/chat.py): `backend/app/api/v1/endpoints/chat.py`
- [Auth dependencies](https://github.com/e-wakil/legalgpt/blob/main/backend/app/api/deps.py): `backend/app/api/deps.py`
- [Chat service](https://github.com/e-wakil/legalgpt/blob/main/backend/app/services/chat_service.py): `backend/app/services/chat_service.py`
- [AI inference client](https://github.com/e-wakil/legalgpt/blob/main/backend/app/services/ai_service.py): `backend/app/services/ai_service.py`
- [API schemas](https://github.com/e-wakil/legalgpt/blob/main/backend/app/schemas/schemas.py): `backend/app/schemas/schemas.py`

### Application data

- [SQL database](https://github.com/e-wakil/legalgpt/blob/main/backend/app/core/database.py): `backend/app/core/database.py`
- [User and chat records](https://github.com/e-wakil/legalgpt/blob/main/backend/app/models/models.py): `backend/app/models/models.py`

### Legal inference

- [AI worker](https://github.com/e-wakil/legalgpt/blob/main/ai/ai_worker.py): `ai/ai_worker.py`
- [Legal persona prompt](https://github.com/e-wakil/legalgpt/blob/main/ai/prompts/legal_persona.py): `ai/prompts/legal_persona.py`
- **Legal corpus retrieval**
- **Mistral model**

### Other components

- **Citizen**

## Connections

- Citizen → App bootstrap: uses
- App bootstrap → Page routing: renders (evidence: `frontend/src/App.tsx`)
- Page routing → Chat access gate: guards chat (evidence: `frontend/src/Routes/Router.tsx`)
- Chat access gate → HTTP client: checks access (evidence: `frontend/src/Routes/ProtectedRoutes.tsx`)
- Page routing → Chat page: routes to (evidence: `frontend/src/Routes/Router.tsx`)
- Chat page → Conversation sidebar: renders (evidence: `frontend/src/pages/ChatPage.tsx`)
- Chat page → Chat interface: renders (evidence: `frontend/src/pages/ChatPage.tsx`)
- App bootstrap → HTTP client: checks session (evidence: `frontend/src/App.tsx`)
- App bootstrap → User state: updates user (evidence: `frontend/src/App.tsx`)
- Conversation sidebar → HTTP client: loads conversations (evidence: `frontend/src/sections/chat/SidebarSection.tsx`)
- Chat interface → Chat WebSocket hook: uses chat stream
- HTTP client → API router: calls API
- Google sign-in API → Token security: verifies Google token (evidence: `backend/app/api/v1/endpoints/auth.py`)
- Google sign-in API → User and chat records: reads and creates user (evidence: `backend/app/api/v1/endpoints/auth.py`)
- Google sign-in API → SQL database: gets session (evidence: `backend/app/api/v1/endpoints/auth.py`)
- API router → Chat endpoint: registers routes (evidence: `backend/app/api/v1/api.py`)
- Chat endpoint → Auth dependencies: authenticates (evidence: `backend/app/api/v1/endpoints/chat.py`)
- Chat endpoint → AI inference client: requests inference (evidence: `backend/app/api/v1/endpoints/chat.py`)
- Chat endpoint → User and chat records: uses chat records (evidence: `backend/app/api/v1/endpoints/chat.py`)
- Chat endpoint → API schemas: uses schemas (evidence: `backend/app/api/v1/endpoints/chat.py`)
- Auth dependencies → SQL database: queries session (evidence: `backend/app/api/deps.py`)
- Auth dependencies → User and chat records: loads user (evidence: `backend/app/api/deps.py`)
- User and chat records → SQL database: maps records (evidence: `backend/app/models/models.py`)
- AI inference client → AI worker: posts query
- AI worker → Legal persona prompt: uses prompt (evidence: `ai/ai_worker.py`)
- AI worker → Legal corpus retrieval: retrieves context
- AI worker → Mistral model: generates answer
- AI worker → AI inference client: streams tokens
- Chat service → User and chat records: supports chat records
