# prabhu-acharya/nyayaai-legal-assistant architecture

> Architecture diagram of the GitHub repository prabhu-acharya/nyayaai-legal-assistant, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-07). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/prabhu-acharya/nyayaai-legal-assistant
- Repository: https://github.com/prabhu-acharya/nyayaai-legal-assistant

## Overview

1. **Overall shape**
   NyayaAI is a two-tier MERN application: a React/Vite SPA in `client/` and an Express/MongoDB API in `server/`. The browser never talks directly to MongoDB or AI/payment providers; all meaningful operations flow through the backend.

2. **Frontend shell and routing**
   The client is organized around `main.jsx`, `App.jsx`, and route-level pages in `src/pages/`. The main user journeys are Landing, Login/Register, Dashboard, Profile, Contract Generator, Custom Contract Builder, and Document Analyzer. `DashboardLayout` provides the authenticated app frame, while `ProtectedRoute` gates private screens.

3. **Frontend state and API boundary**
   `AuthContext.jsx` holds login/session state and likely user identity across the app. `src/services/api.js` is the main boundary for HTTP calls to the backend, so the React layer stays thin and delegates persistence, auth, AI, and payments to server endpoints.

4. **Reusable UI and interaction components**
   The client’s `components/` folder contains domain-specific UI blocks such as chat bubbles, chat input/sidebar, citation cards, contract form/preview, e-signature, premium/terms modals, and top navigation. `useChatSession` and `useMicInput` are the primary interaction hooks, indicating chat state and voice input are encapsulated outside page components.

5. **Backend entry and infrastructure**
   The server starts from `server.js` and `app.js`, with configuration split into `config/db.js`, `config/logger.js`, and `config/razorpay.js`. This separation shows a standard Express service with centralized startup, DB connection management, logging, and payment configuration.

6. **API routing and feature boundaries**
   The backend is split by business capability into `chatRoutes`, `contractRoutes`, `documentRoutes`, `paymentRoutes`, `queryRoutes`, and `userRoutes`. Each route module maps to a controller in `controllers/`, keeping chat, contracts, document analysis, payments, legal queries, and user management isolated.

7. **Persistence model**
   MongoDB/Mongoose models in `models/` persist the main domain objects: `User`, `ChatSession`, `Contract`, and `Document`. This implies long-lived user accounts plus saved conversational and generated-asset history, not just transient API responses.

8. **AI and legal intelligence layer**
   The `services/` folder contains `contractAI.service.js`, `documentAI.service.js`, and `indianKanoon.js`. These are the system’s AI/legal intelligence boundary: controllers likely assemble prompts and context, then these services call Groq Llama 3.3 70B and legal sources such as Indian Kanoon to produce answers, summaries, or contract drafts.

9. **Data enrichment and retrieval**
   `middleware/ragMiddleware.js` and `data/bns-map.json` suggest retrieval-augmented or rules-based enrichment for legal queries. This layer likely injects statute mappings, citations, or supporting context before the AI response is generated, especially for Indian-law-specific outputs.

10. **Auth, uploads, and request protection**
   `middleware/authMiddleware.js` protects private APIs with JWT-based authentication, `uploadMiddleware.js` handles file ingestion for document analysis, and `ProtectedRoute` mirrors this protection on the client. That creates a clean split between public pages, authenticated dashboard flows, and premium/private actions.

11. **Payments and entitlement**
   Razorpay is integrated through the payment config, routes, and controller layer. This likely controls free vs premium access, with the frontend showing `PremiumModal` and the backend enforcing plan-dependent capabilities.

12. **Runtime and deployment signals**
   The stack is React 19 + Vite on the frontend and Node.js/Express 5 on the backend, with MongoDB as the durable store. `vercel.json` in the client suggests the SPA is deployment-friendly on Vercel, while the API remains a separate Node service.

13. **Implementation maturity**
   The presence of `contractController.week9.js` and `contractAI.week9.js` indicates older iterations are retained alongside the current implementation. For architecture diagrams, the main production path should be drawn from the non-week9 controllers/services, with the week9 files treated as legacy or experimental variants.

Main entry points: [`client/src/App.jsx`](https://github.com/prabhu-acharya/nyayaai-legal-assistant/blob/main/client/src/App.jsx) (App shell), [`client/src/pages`](https://github.com/prabhu-acharya/nyayaai-legal-assistant/tree/main/client/src/pages) (Pages), [`server/app.js`](https://github.com/prabhu-acharya/nyayaai-legal-assistant/blob/main/server/app.js) (API app), [`server/controllers`](https://github.com/prabhu-acharya/nyayaai-legal-assistant/tree/main/server/controllers) (Controllers)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Client"]
  node_client_main["Main<br/>bootstrap<br/>[main.jsx]"]
  node_client_app["App shell<br/>router-shell<br/>[App.jsx]"]
  node_client_auth(("Auth state<br/>context<br/>[AuthContext.jsx]"))
  node_client_api["API client<br/>http-boundary<br/>[api.js]"]
  node_client_layout["Dashboard frame<br/>layout"]
  node_client_protected{{"Protected route<br/>route-guard<br/>[ProtectedRoute.jsx]"}}
  node_client_pages["Pages<br/>views"]
  node_client_chat["Chat UI<br/>conversation-ui"]
  node_client_contract["Contract UI<br/>contract-ui"]
  node_client_media(("Voice input<br/>interaction-hook<br/>[useMicInput.js]"))
end

subgraph group_server["Server"]
  node_server_boot["Server boot<br/>startup<br/>[server.js]"]
  node_server_app["API app<br/>express-app<br/>[app.js]"]
  node_server_config["Config"]
  node_server_routes["Routes"]
  node_server_ctrl["Controllers<br/>request-handlers"]
  node_server_models[("Models")]
  node_server_mw["Middleware<br/>request-pipeline"]
  node_server_services["AI services<br/>domain-services"]
  node_server_data["BNS map<br/>legal-data<br/>[bns-map.json]"]
end

node_client_main -->|"bootstraps"| node_client_app
node_client_app -->|"uses"| node_client_auth
node_client_app -->|"guards"| node_client_protected
node_client_app -->|"renders"| node_client_layout
node_client_app -->|"routes to"| node_client_pages
node_client_pages -->|"uses"| node_client_chat
node_client_pages -->|"uses"| node_client_contract
node_client_pages -->|"calls"| node_client_api
node_client_chat -->|"uses"| node_client_media
node_client_contract -->|"calls"| node_client_api
node_client_auth -->|"authenticates"| node_client_api
node_server_boot -->|"loads"| node_server_app
node_server_app -->|"wires"| node_server_config
node_server_app -->|"applies"| node_server_mw
node_server_app -->|"mounts"| node_server_routes
node_server_routes -->|"dispatches to"| node_server_ctrl
node_server_ctrl -->|"persists"| node_server_models
node_server_ctrl -->|"invokes"| node_server_services
node_server_mw -->|"enriches with"| node_server_data
node_server_mw -->|"protects"| node_server_ctrl
node_server_services -->|"reads"| node_server_data
node_server_services -->|"uses"| node_server_config

click node_client_main "https://github.com/prabhu-acharya/nyayaai-legal-assistant/blob/main/client/src/main.jsx"
click node_client_app "https://github.com/prabhu-acharya/nyayaai-legal-assistant/blob/main/client/src/App.jsx"
click node_client_auth "https://github.com/prabhu-acharya/nyayaai-legal-assistant/blob/main/client/src/components/AuthContext.jsx"
click node_client_api "https://github.com/prabhu-acharya/nyayaai-legal-assistant/blob/main/client/src/services/api.js"
click node_client_layout "https://github.com/prabhu-acharya/nyayaai-legal-assistant/blob/main/client/src/layouts/DashboardLayout.jsx"
click node_client_protected "https://github.com/prabhu-acharya/nyayaai-legal-assistant/blob/main/client/src/components/ProtectedRoute.jsx"
click node_client_pages "https://github.com/prabhu-acharya/nyayaai-legal-assistant/tree/main/client/src/pages"
click node_client_chat "https://github.com/prabhu-acharya/nyayaai-legal-assistant/tree/main/client/src/components"
click node_client_contract "https://github.com/prabhu-acharya/nyayaai-legal-assistant/tree/main/client/src/components"
click node_client_media "https://github.com/prabhu-acharya/nyayaai-legal-assistant/blob/main/client/src/hooks/useMicInput.js"
click node_server_boot "https://github.com/prabhu-acharya/nyayaai-legal-assistant/blob/main/server/server.js"
click node_server_app "https://github.com/prabhu-acharya/nyayaai-legal-assistant/blob/main/server/app.js"
click node_server_config "https://github.com/prabhu-acharya/nyayaai-legal-assistant/tree/main/server/config"
click node_server_routes "https://github.com/prabhu-acharya/nyayaai-legal-assistant/tree/main/server/routes"
click node_server_ctrl "https://github.com/prabhu-acharya/nyayaai-legal-assistant/tree/main/server/controllers"
click node_server_models "https://github.com/prabhu-acharya/nyayaai-legal-assistant/tree/main/server/models"
click node_server_mw "https://github.com/prabhu-acharya/nyayaai-legal-assistant/tree/main/server/middleware"
click node_server_services "https://github.com/prabhu-acharya/nyayaai-legal-assistant/tree/main/server/services"
click node_server_data "https://github.com/prabhu-acharya/nyayaai-legal-assistant/blob/main/server/data/bns-map.json"

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_client_main,node_client_app,node_client_auth,node_client_api,node_client_layout,node_client_protected,node_client_pages,node_client_chat,node_client_contract,node_client_media toneBlue
class node_server_boot,node_server_app,node_server_config,node_server_routes,node_server_ctrl,node_server_models,node_server_mw,node_server_services,node_server_data toneAmber
```

## Components

### Client

React/Vite single-page app and its UI layer

- [Main](https://github.com/prabhu-acharya/nyayaai-legal-assistant/blob/main/client/src/main.jsx): bootstrap · Client entry that mounts the app · `client/src/main.jsx`
- [App shell](https://github.com/prabhu-acharya/nyayaai-legal-assistant/blob/main/client/src/App.jsx): router-shell · Top-level client routing and composition · `client/src/App.jsx`
- [Auth state](https://github.com/prabhu-acharya/nyayaai-legal-assistant/blob/main/client/src/components/AuthContext.jsx): context · Session and identity state shared across the SPA · `client/src/components/AuthContext.jsx`
- [API client](https://github.com/prabhu-acharya/nyayaai-legal-assistant/blob/main/client/src/services/api.js): http-boundary · Frontend HTTP boundary to backend services · `client/src/services/api.js`
- [Dashboard frame](https://github.com/prabhu-acharya/nyayaai-legal-assistant/blob/main/client/src/layouts/DashboardLayout.jsx): layout · Authenticated app layout and navigation shell · `client/src/layouts/DashboardLayout.jsx`
- [Protected route](https://github.com/prabhu-acharya/nyayaai-legal-assistant/blob/main/client/src/components/ProtectedRoute.jsx): route-guard · Client-side gate for authenticated screens · `client/src/components/ProtectedRoute.jsx`
- [Pages](https://github.com/prabhu-acharya/nyayaai-legal-assistant/tree/main/client/src/pages): views · Route-level user journeys · `client/src/pages`
- [Chat UI](https://github.com/prabhu-acharya/nyayaai-legal-assistant/tree/main/client/src/components): conversation-ui · Reusable chat interaction components · `client/src/components`
- [Contract UI](https://github.com/prabhu-acharya/nyayaai-legal-assistant/tree/main/client/src/components): contract-ui · Forms, previews, signatures, and premium prompts · `client/src/components`
- [Voice input](https://github.com/prabhu-acharya/nyayaai-legal-assistant/blob/main/client/src/hooks/useMicInput.js): interaction-hook · Mic-driven input hook for conversational flows · `client/src/hooks/useMicInput.js`

### Server

Express API, persistence, AI, and integrations

- [Server boot](https://github.com/prabhu-acharya/nyayaai-legal-assistant/blob/main/server/server.js): startup · Express application startup and process entry · `server/server.js`
- [API app](https://github.com/prabhu-acharya/nyayaai-legal-assistant/blob/main/server/app.js): express-app · Express app composition and middleware wiring · `server/app.js`
- [Config](https://github.com/prabhu-acharya/nyayaai-legal-assistant/tree/main/server/config): infra-config · Database, logging, and payment configuration · `server/config`
- [Routes](https://github.com/prabhu-acharya/nyayaai-legal-assistant/tree/main/server/routes): api-routes · Capability-specific HTTP route modules · `server/routes`
- [Controllers](https://github.com/prabhu-acharya/nyayaai-legal-assistant/tree/main/server/controllers): request-handlers · Business logic entry points for each API area · `server/controllers`
- [Models](https://github.com/prabhu-acharya/nyayaai-legal-assistant/tree/main/server/models): mongoose-models · Persistent domain objects stored in MongoDB · `server/models`
- [Middleware](https://github.com/prabhu-acharya/nyayaai-legal-assistant/tree/main/server/middleware): request-pipeline · Auth, upload, and retrieval enrichment layers · `server/middleware`
- [AI services](https://github.com/prabhu-acharya/nyayaai-legal-assistant/tree/main/server/services): domain-services · Contract, document, and legal intelligence services · `server/services`
- [BNS map](https://github.com/prabhu-acharya/nyayaai-legal-assistant/blob/main/server/data/bns-map.json): legal-data · Reference data for legal retrieval and enrichment · `server/data/bns-map.json`

## Connections

- Main → App shell: bootstraps · Mounts the SPA root
- App shell → Auth state: uses · Reads login/session state
- App shell → Protected route: guards · Wraps private routes
- App shell → Dashboard frame: renders · Uses the dashboard shell for authenticated views
- App shell → Pages: routes to · Connects top-level pages
- Pages → Chat UI: uses · Pages compose shared chat components
- Pages → Contract UI: uses · Pages compose contract-generation UI
- Pages → API client: calls · Delegates backend communication
- Chat UI → Voice input: uses · Voice input feeds chat flows
- Contract UI → API client: calls · Submits contract, document, and payment actions
- Auth state → API client: authenticates · Shares credentials and user identity with API calls
- Server boot → API app: loads · Starts the Express app
- API app → Config: wires · Initializes DB, logging, and payment config
- API app → Middleware: applies · Installs middleware pipeline
- API app → Routes: mounts · Registers API routes
- Routes → Controllers: dispatches to · Routes delegate into controllers
- Controllers → Models: persists · Stores users, sessions, contracts, and documents
- Controllers → AI services: invokes · Calls AI and external intelligence services
- Middleware → BNS map: enriches with · Adds legal reference context
- Middleware → Controllers: protects · Auth and upload controls shape request handling
- AI services → BNS map: reads · Uses statutory mapping and retrieval hints
- AI services → Config: uses · Relies on external API/payment configuration
