How prabhu-acharya/nyayaai-legal-assistant works
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: App.jsx App shell · pages Pages · app.js API app · controllers Controllers
Components 19
Client
React/Vite single-page app and its UI layer
- Main · Client entry that mounts the app
client/src/main.jsx - App shell · Top-level client routing and composition
client/src/App.jsx - Auth state · Session and identity state shared across the SPA
client/src/components/AuthContext.jsx - API client · Frontend HTTP boundary to backend services
client/src/services/api.js - Dashboard frame · Authenticated app layout and navigation shell
client/src/layouts/DashboardLayout.jsx - Protected route · Client-side gate for authenticated screens
client/src/components/ProtectedRoute.jsx - Pages · Route-level user journeys
client/src/pages - Chat UI · Reusable chat interaction components
client/src/components - Contract UI · Forms, previews, signatures, and premium prompts
client/src/components - Voice input · Mic-driven input hook for conversational flows
client/src/hooks/useMicInput.js
Server
Express API, persistence, AI, and integrations
- Server boot · Express application startup and process entry
server/server.js - API app · Express app composition and middleware wiring
server/app.js - Config · Database, logging, and payment configuration
server/config - Routes · Capability-specific HTTP route modules
server/routes - Controllers · Business logic entry points for each API area
server/controllers - Models · Persistent domain objects stored in MongoDB
server/models - Middleware · Auth, upload, and retrieval enrichment layers
server/middleware - AI services · Contract, document, and legal intelligence services
server/services - BNS map · Reference data for legal retrieval and enrichment
server/data/bns-map.json
Connections 22
- 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