Skip to content

LexAI Upload any legal contract. Get instant AI powered risk analysis, clause by clause review, obligation tracking, and expiry alerts all in real-time.

Notifications You must be signed in to change notification settings

web-dev-vishal/LexAI

Repository files navigation

βš–οΈ LexAI

AI-Powered Contract Intelligence Platform

Upload any legal contract. Get instant AI-powered risk analysis, clause-by-clause review, obligation tracking, and expiry alerts β€” all in real-time.

Node.js Express MongoDB Redis RabbitMQ License: ISC


🎯 What It Does

Feature Description
πŸ“„ Contract Upload Upload PDF, DOCX, or plain text contracts
πŸ€– AI Analysis Risk scoring, clause flagging, obligation extraction via OpenRouter LLM
⚑ Real-Time Updates WebSocket notifications when analysis completes
πŸ“Š Version Comparison AI-powered diff between contract versions
πŸ”” Expiry Alerts Automated email + socket alerts before contract expiry
🏒 Multi-Tenant Organization-based isolation with RBAC (admin/manager/viewer)
πŸ“ˆ Quota Management Redis-based monthly analysis limits per subscription tier
πŸ” Enterprise Security JWT rotation, token blacklist, rate limiting, input validation

πŸ› οΈ Tech Stack

Category Technology
Runtime Node.js β‰₯ 20 (ES Modules)
Framework Express.js 4.19
Database MongoDB 8 + Mongoose ODM
Cache & Pub/Sub Redis (ioredis)
Message Queue RabbitMQ (amqplib)
Real-Time Socket.io with Redis adapter
AI Engine OpenRouter API (Llama 3.1, Mistral 7B)
Auth JWT (access + refresh tokens) with bcrypt
Validation Joi + Zod
Email Nodemailer (SMTP)
File Parsing pdf-parse, mammoth (DOCX)
Logging Winston + Morgan
Security Helmet, CORS, express-mongo-sanitize, rate limiting
Scheduling node-cron

πŸ“ Folder Structure

LexAI/
β”œβ”€β”€ server.js              # API entry point (HTTP + Socket.io + cron)
β”œβ”€β”€ worker.js              # Background worker (RabbitMQ consumers)
β”œβ”€β”€ package.json           # Dependencies & scripts
β”œβ”€β”€ scripts/seed.js        # First admin user seed script
β”‚
└── src/
    β”œβ”€β”€ app.js             # Express middleware & route setup
    β”œβ”€β”€ config/            # DB, Redis, RabbitMQ, Socket.io, env validation
    β”œβ”€β”€ constants/         # HTTP codes, plans, queues, roles
    β”œβ”€β”€ models/            # 7 Mongoose models
    β”œβ”€β”€ services/          # 13 business logic services
    β”œβ”€β”€ controllers/       # 7 HTTP request handlers
    β”œβ”€β”€ middleware/         # 7 middleware (auth, RBAC, validation, rate limit)
    β”œβ”€β”€ validators/        # 4 Joi schema files
    β”œβ”€β”€ routes/            # 8 Express routers
    β”œβ”€β”€ utils/             # 8 shared utilities
    β”œβ”€β”€ sockets/           # Socket.io event bridge
    β”œβ”€β”€ workers/           # RabbitMQ consumers (analysis + alerts)
    └── jobs/              # Cron jobs (daily expiry scan)

πŸ“– For a detailed explanation of every file and how data flows through the system, see PROJECT_GUIDE.md.


βš™οΈ Environment Variables

Create a .env file in the project root:

# App
NODE_ENV=development
PORT=3000
API_VERSION=v1

# MongoDB
MONGO_URI=mongodb://localhost:27017/lexai

# Redis
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=

# RabbitMQ
RABBITMQ_URL=amqp://guest:guest@localhost:5672
ANALYSIS_QUEUE=lexai.analysis.queue
ALERT_QUEUE=lexai.alert.queue
DLX_EXCHANGE=lexai.dlx

# JWT (use strong, random 32+ character strings)
JWT_ACCESS_SECRET=your-access-secret-at-least-32-chars-long
JWT_REFRESH_SECRET=your-refresh-secret-at-least-32-chars-long
JWT_ACCESS_EXPIRY=15m
JWT_REFRESH_EXPIRY=7d

# OpenRouter AI
OPENROUTER_API_KEY=sk-or-v1-your-key
OPENROUTER_BASE_URL=https://openrouter.ai/api/v1
AI_PRIMARY_MODEL=meta-llama/llama-3.1-8b-instruct:free
AI_FALLBACK_MODEL=mistralai/mistral-7b-instruct:free

# Rate Limiting
RATE_LIMIT_WINDOW_MS=60000
RATE_LIMIT_MAX=100

# File Upload
MAX_FILE_SIZE_MB=5
ALLOWED_MIME_TYPES=application/pdf,application/vnd.openxmlformats-officedocument.wordprocessingml.document,text/plain

# CORS
ALLOWED_ORIGINS=http://localhost:3000,http://localhost:5173

# Email (Ethereal for testing)
SMTP_HOST=smtp.ethereal.email
SMTP_PORT=587
SMTP_USER=
SMTP_PASS=
EMAIL_FROM=noreply@lexai.io

# External APIs
REST_COUNTRIES_URL=https://restcountries.com/v3.1
WORLD_TIME_API_URL=https://worldtimeapi.org/api

πŸš€ How to Run Locally

Prerequisites

  • Node.js β‰₯ 20
  • MongoDB (local or Atlas)
  • Redis (local or cloud)
  • RabbitMQ (local or CloudAMQP)

1. Install Dependencies

git clone https://github.com/YOUR_USERNAME/LexAI.git
cd LexAI
npm install

2. Start Infrastructure (Docker option)

If you have Docker:

docker-compose up -d    # Starts MongoDB, Redis, RabbitMQ

3. Configure Environment

cp .env.example .env    # Copy and fill in your values

4. Seed Admin User

npm run seed

5. Start the API Server

npm run dev             # Development with auto-reload
# or
npm start               # Production

6. Start the Background Worker

In a separate terminal:

npm run dev:worker      # Development
# or
npm run start:worker    # Production

7. Verify

curl http://localhost:3000/health

πŸ“‘ API Endpoints

Authentication

Method Endpoint Auth Description
POST /api/v1/auth/register ❌ Register new user
POST /api/v1/auth/verify-email ❌ Verify email token
POST /api/v1/auth/login ❌ Login, get tokens
POST /api/v1/auth/refresh-token πŸͺ Refresh access token
POST /api/v1/auth/logout βœ… Blacklist current token
POST /api/v1/auth/forgot-password ❌ Request password reset
POST /api/v1/auth/reset-password ❌ Reset with token

Users

Method Endpoint Auth Description
GET /api/v1/users/me βœ… Get my profile + quota
PATCH /api/v1/users/me βœ… Update my name
PATCH /api/v1/users/me/password βœ… Change password
GET /api/v1/users/:id πŸ”’ Admin Get user by ID

Organizations

Method Endpoint Auth Description
POST /api/v1/orgs βœ… Create organization
GET /api/v1/orgs/:orgId βœ… Get org details
PATCH /api/v1/orgs/:orgId πŸ”’ Admin/Mgr Update org
POST /api/v1/orgs/:orgId/invite πŸ”’ Admin/Mgr Invite member
POST /api/v1/orgs/:orgId/invite/accept ❌ Accept invite
PATCH /api/v1/orgs/:orgId/members/:userId/role πŸ”’ Admin Change role
DELETE /api/v1/orgs/:orgId/members/:userId πŸ”’ Admin Remove member

Contracts

Method Endpoint Auth Description
POST /api/v1/contracts βœ… Upload contract (file/text)
GET /api/v1/contracts βœ… List with pagination/filter
GET /api/v1/contracts/:id βœ… Get full contract
PATCH /api/v1/contracts/:id βœ… Update metadata
DELETE /api/v1/contracts/:id πŸ”’ Admin/Mgr Soft delete
POST /api/v1/contracts/:id/versions βœ… Upload new version
GET /api/v1/contracts/:id/versions βœ… Version history
POST /api/v1/contracts/:id/compare βœ… Compare versions (AI)
GET /api/v1/contracts/:id/audit βœ… Audit trail

Analyses

Method Endpoint Auth Description
POST /api/v1/analyses βœ… Request AI analysis
GET /api/v1/analyses/:id βœ… Get analysis result
GET /api/v1/analyses/contract/:contractId βœ… All analyses for contract

Admin

Method Endpoint Auth Description
GET /api/v1/admin/stats πŸ”’ Admin Platform statistics
GET /api/v1/admin/queue/status πŸ”’ Admin RabbitMQ queue status
GET /api/v1/admin/users πŸ”’ Admin List all users
GET /api/v1/admin/audit-logs πŸ”’ Admin Global audit logs

Health

Method Endpoint Auth Description
GET /health ❌ Service health check

πŸ“– For complete request/response examples with dummy data, see POSTMAN_COLLECTION.md.


πŸ“Š Entity-Relationship Diagram

πŸ“– Full ER diagram with all fields and data types is in PROJECT_GUIDE.md.

Quick overview of model relationships:

User ──belongs to──▢ Organization
Organization ──has many──▢ Contract, Invitation, AuditLog, Notification
Contract ──has many──▢ Analysis, Version (embedded), Party (embedded)
Analysis ──contains──▢ Clause (embedded)

🚒 Deployment

LexAI is deployment-ready for Render.com (free tier supported).

πŸ“– Full step-by-step deployment guide with GitHub setup, service configuration, env vars, and common error fixes: DEPLOYMENT.md.

Live URL format after deployment:

https://lexai-api.onrender.com/health
https://lexai-api.onrender.com/api/v1/auth/login

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Commit changes: git commit -m 'feat: add amazing feature'
  4. Push to branch: git push origin feature/amazing-feature
  5. Open a Pull Request

Commit Convention

Use Conventional Commits:

Prefix Usage
feat: New feature
fix: Bug fix
docs: Documentation only
refactor: Code change, no feature/bug fix
test: Adding/updating tests
chore: Maintenance tasks

πŸ“œ License

This project is licensed under the ISC License. See LICENSE for details.


Built with ❀️ by the LexAI Team

About

LexAI Upload any legal contract. Get instant AI powered risk analysis, clause by clause review, obligation tracking, and expiry alerts all in real-time.

Topics

Resources

Stars

Watchers

Forks

Contributors