# πŸ›οΈ SaulGPT β€” System Architecture & Component Specification **Document Version:** 2.2.0 **Status:** Active Production **Hosting Architecture:** Hugging Face Spaces (ZeroGPU Backend) + Supabase Managed PostgreSQL + Firebase Hosting (Web SPA) --- ## 1. High-Level Architecture Overview SaulGPT is an AI-powered Indian Legal Intelligence platform operating on a **Two-Tier Neuro-Symbolic Architecture**: 1. **The Multi-Agent Workflow Layer (UX & Client Lifecycle):** Omni-Box Routing $\rightarrow$ 3-Phase Virtual Counsel (Discovery $\rightarrow$ Strategy $\rightarrow$ Execution) $\rightarrow$ Pre-Trial Studio & Moot Court Arena. 2. **The 6-Layer Deterministic Substantive Legal Engine:** 6-Layer RAG $\rightarrow$ Actor-Critic Process Reward Model $\rightarrow$ 4-Family Document Drafter $\rightarrow$ Supabase PostgreSQL Cloud Persistence. ``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Frontend (React 18 + Vite 5 SPA) β”‚ β”‚ Hosted at: https://legalgpt-india.web.app (Firebase Hosting) β”‚ β”‚ β”‚ β”‚ β”œβ”€β”€ App.jsx Core Omni-Box chat & session state β”‚ β”‚ β”œβ”€β”€ AuthPage.jsx JWT Auth & Guest Session Manager β”‚ β”‚ β”œβ”€β”€ ConversationsSidebar.jsx Multi-Device Supabase chat history β”‚ β”‚ β”œβ”€β”€ TrialStudio.jsx Pre-Trial Moot Court & Cross-Examiner β”‚ β”‚ β”œβ”€β”€ EvidenceUploader.jsx Sec 63 BSA Digital Evidence Processor β”‚ β”‚ β”œβ”€β”€ TriageCards.jsx Strategic SWOT Option Cards β”‚ β”‚ β”œβ”€β”€ VetoCard.jsx Scrutiny Gatekeeper & Limitation Alert β”‚ β”‚ β”œβ”€β”€ BNSCorrectionNotice.jsx 2024 Transition Warnings (BNS/BNSS/BSA) β”‚ β”‚ β”œβ”€β”€ JurisdictionBadge.jsx Court Pecuniary / Territorial Badging β”‚ β”‚ β”œβ”€β”€ UrgencyBanner.jsx Statutory Limitation Countdown β”‚ β”‚ β”œβ”€β”€ ApiKeyModal.jsx BYOK Groq API Key Management β”‚ β”‚ └── usePDF.js Browser-Side Vector PDF Generator β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ HTTPS REST (port 443 / 7860) β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Backend API Server (Hugging Face Spaces ZeroGPU) β”‚ β”‚ Hosted at: https://lvvbaaal-legalgpt-api.hf.space β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ app.py (ZeroGPU Gradio 5.20 + FastAPI Unification) β”‚ β”‚ β”‚ β”‚ - demo.launch(server_port=7860, prevent_thread_lock=True) β”‚ β”‚ β”‚ β”‚ - server_app.include_router(fastapi_app.router) β”‚ β”‚ β”‚ β”‚ - demo.block_thread() keeps server active β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ api_server.py (Core Dispatcher & Route Handlers) β”‚ β”‚ β”‚ β”‚ - Omni-Box Autonomous Intent Routing (Layer 1) β”‚ β”‚ β”‚ β”‚ - 3-Phase Discovery State Machine (triage_state.py) β”‚ β”‚ β”‚ β”‚ - 4-Family Document Drafting Engine (interview_state.py) β”‚ β”‚ β”‚ β”‚ - Adversarial IRAC Advocate Mode (irac_agent.py) β”‚ β”‚ β”‚ β”‚ - Pre-Trial Moot Court & Precedent Engine (trial_studio/) β”‚ β”‚ β”‚ β”‚ - Sec 63 BSA Electronic Certificate Generator β”‚ β”‚ β”‚ β”‚ - Contract Risk Evaluator (layer6_evaluator.py) β”‚ β”‚ β”‚ β”‚ - Actor-Critic Fine-Grained PRM (actor_critic.py) β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β–Ό β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ SUPABASE POSTGRESQL CLOUD DATABASE β”‚ β”‚ LOCAL CHROMADB VECTOR STORE β”‚ β”‚ Host: aws-0-ap-southeast-1.pooler... β”‚ β”‚ Path: data/vector_db/ β”‚ β”‚ Port: 5432 (Session) / 6543 (Tx) β”‚ β”‚ Model: all-MiniLM-L6-v2 (CPU-pinned) β”‚ β”‚ Tables: users, conversations, messagesβ”‚ β”‚ Collections: saulgpt_indian_laws, β”‚ β”‚ Engine: IPv4 Connection Pooler β”‚ β”‚ landmark_precedents β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` --- ## 2. The 3-Phase Virtual Counsel Workflow ``` [User Input] ──► Phase 1: Discovery (Max 3 Turns) β”‚ β”œβ”€β”€ Turn 1: Story & Relationship β”œβ”€β”€ Turn 2: Key Evidence & Dates └── Turn 3: Primary Desired Outcome β”‚ β–Ό Phase 2: Strategy (SWOT Matrix) β”‚ β”œβ”€β”€ 4 Action Paths (path_a .. path_d) β”œβ”€β”€ Success Probability & Time Horizon └── Counsel Override for Exceptional Cases β”‚ β–Ό Phase 3: Execution β”œβ”€β”€ Document Factory (.docx & .pdf) β”œβ”€β”€ Procedural Pathfinder Roadmap └── Pre-Trial Moot Court Rehearsal ``` --- ## 3. The 6-Layer Deterministic RAG Pipeline | Layer | Component | File | Key Capabilities & Device Pinning | | :---: | :--- | :--- | :--- | | **0** | Conversational Memory | `pipeline_orchestrator.py` | Sliding window of 6 verbatim turns; older turns compressed. | | **1** | Query Understanding | `layer1_understanding.py` | SpaCy NER, Devanagari Hindi HyDE translation, blank model fallback. | | **2** | Hybrid Retrieval | `layer2_retrieval.py` | Dense ChromaDB (2,449+ chunks) + BM25 + CrossEncoder (`device="cpu"`). | | **3** | Big Model Reasoning | `layer3_reasoning.py` | Groq `qwen/qwen3.8-27b` & `openai/gpt-oss-120b` synthesis. | | **4** | Validation & Guardrails | `layer4_validation.py`, `actor_critic.py` | NLI DeBERTa (`device="cpu"`), Actor-Critic FG-PRM (`max_tokens=800`), 2024 Transition concordance, SC Struck-Down Firewall. | | **5** | External Search & Triplifier | `layer5_external.py`, `auto_triplifier.py` | Live DuckDuckGo & IndianKanoon scraping; real-time $(h, r, t)$ ingestion. | | **6** | Evaluator & Relational GNN | `layer6_evaluator.py`, `gnn_engine.py` | 25K character contract review (ICA 1872), PyTorch GNN TransE/RotatE ($\mathbb{R}^{64}$). | --- ## 4. Court-Grade Document Factory (4 Families) The document drafting engine (`backend/document_generator.py` and `backend/interview_state.py`) implements a 4-Family Architecture: ``` DOCUMENT FACTORY β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β–Ό β–Ό β–Ό β–Ό LETTER PLEADING AFFIDAVIT AGREEMENT (Legal Notice, (Order VII CPC (Sworn Deponent (Commercial S.138 NIA, Plaints, Civil Declarations, Leases, SLAs, Demand RPAD) Petitions) Stamp Margin) NDAs) ``` ### Key Technical Guardrails: 1. **Dynamic Prompt Sanitization:** Strips all `[User Goal: document]` tags and conversational instruction artifacts before document generation. 2. **Deterministic Fallback Spec Engine (`build_fallback_spec`):** Automatically activated if LLM generation fails or times out. Synthesizes an authentic legal document with formal statutory clauses. 3. **Notice Elements:** RPAD header, Sender `FROM:` block with Date, Recipient `TO:` block, formal heading under Section 138 NIA / relevant Act, numbered factual recitals, statutory 15-day demand clause, enclosures, and advocate signature. 4. **Order VII Rule 11 CPC Triad of Survival:** Mandatory limitation clause, jurisdiction clause, and court valuation clause for plaints. 5. **Dual Cloud Storage & Delivery:** Cached in-memory in `SESSION_DOCUMENTS` and on disk in `./generated_docs/{session_id}.docx`. Delivered via Axios blob stream and browser jsPDF. --- ## 5. Persistence Architecture: Supabase Cloud PostgreSQL To support high-concurrency cloud deployments across mobile and desktop clients without file lock errors, SaulGPT utilizes **Supabase Managed PostgreSQL** alongside local SQLite: - **Connection Pooler:** Routed through `aws-0-ap-southeast-1.pooler.supabase.com:5432` (Session Mode) via IPv4, ensuring container accessibility without IPv6 requirements. - **Relational Tables:** - `users`: User registration, bcrypt password hashes, timestamps. - `conversations`: Chat sessions with titles, linked to `user_id`. - `messages`: Multi-turn user and assistant messages with structured metadata JSON. - **Failover & Safety:** Automatic fallback to SQLite if PostgreSQL credentials are unset in local test environments. --- ## 6. Security, Key Isolation, & Rate Limiting - **Bring Your Own Key (BYOK):** Users provide personal Groq API keys (`gsk_...`), stored client-side in `localStorage`. Injected per-request via `X-Groq-Api-Key` headers. - **Token Budget Guardrails:** Critic PRM calls calibrated to `max_tokens=800` to prevent Groq free-tier 1000 OTPM rate limit exceptions (`429 rate_limit_exceeded`). - **Memory Stability:** Pre-initialized PyTorch and spaCy pipelines strictly isolated on CPU outside ZeroGPU calls, ensuring 0% OOM crashes.