# sameer-ssr99/nyaya-ai architecture

> Architecture diagram of the GitHub repository sameer-ssr99/nyaya-ai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-09-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sameer-ssr99/nyaya-ai
- Repository: https://github.com/sameer-ssr99/nyaya-ai

## Overview

##### Project Type and Purpose
- Nyaya.ai is a full-stack web application: an AI-powered legal assistant specializing in Indian law.
- Frontend: Next.js (app router), React components, internationalization.
- Backend: Next.js API routes, Supabase (PostgreSQL + Auth) for database and authentication.
- External AI service: Puter.js AI loaded from CDN in the browser.

##### High-Level Architecture Components
- Client (Browser/mobile):
- Next.js pages/components (chat interface, document generator, lawyer directory, dashboard, stories, KYR content)
- Global CSS and shadcn/ui for styling
- Puter.js AI script loaded via CDN
- Supabase client library for auth and data queries
- API Layer (Next.js “app/api”):
- /api/chat, /api/chat/start
- /api/documents/enhance, /api/enhance-document
- /api/bookmarks
- Each route handles HTTP requests, applies business logic, calls AI or database
- Backend Services:
- Supabase:
– Auth (signup, login, session management via middleware.ts)
– Database tables for users, chats, documents, KYR articles, lawyers, case stories, bookmarks
- Database migration scripts in /scripts
- External AI Service:
- Puter.js AI (loaded in client, invoked by API routes via window.puter.ai.chat)
- Deployment & Hosting:
- Vercel (or similar) for Next.js app (serverless functions for API routes)
- Supabase hosted PostgreSQL

##### Relationships and Data Flows
- User → Frontend:
- Client sends UI requests (page navigation, form submissions)
- User interacts with chat page, document generator, lawyer directory
- Frontend → Next.js API:
- Fetch calls to endpoints: /api/chat, /api/documents/enhance, /api/bookmarks, etc.
- Supabase client used directly in server components for data fetching
- API → Supabase Database:
- CRUD operations for chats, documents, user profile, stories, KYR content
- API → External AI (Puter.js):
- Chat endpoint orchestrates AI prompts/responses
- Document enhancement endpoint passes user text to AI, receives enriched content
- Authentication Flow:
- User signs up/logs in via Supabase Auth
- Auth middleware protects API routes and pages
- JWT/session tokens stored in cookies/localStorage
- Internationalization:
- lib/i18n provides context and translations for multi-language support

##### Architectural Patterns and Principles
- Next.js App Router for file-based routing (separates pages and API routes).
- Client-side AI integration (Puter.js) with serverless API orchestration.
- Supabase as BaaS: unified Auth + Database layer.
- Separation of concerns:
- UI layer (components/)
- Data/services layer (lib/supabase, lib/utils)
- API layer (app/api)
- Scripts for database schema
- Modular, reusable React components (shadcn/ui patterns).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Browser UI Layer
    subgraph "Browser (UI)" 
        style Browser fill:#E3F2FD,stroke:#42A5F5
        subgraph "Chat Interface" 
            CI1["Chat List Page"]:::ui
            CI2["Conversation Page"]:::ui
            CI3["Chat Interface Component"]:::ui
            CI4["Chat History Component"]:::ui
        end
        subgraph "Document Generator" 
            DG1["Template Selector Page"]:::ui
            DG2["Document Generator Component"]:::ui
            DG3["Templates Component"]:::ui
            DG4["History Page"]:::ui
        end
        subgraph "Lawyer Directory" 
            LD1["Directory Page"]:::ui
            LD2["Profile Page"]:::ui
            LD3["Consult Booking Page"]:::ui
            LD4["Directory Component"]:::ui
            LD5["Profile Component"]:::ui
            LD6["Consultation Component"]:::ui
            LD7["Lawyer Chat Interface"]:::ui
        end
        subgraph "KYR Content" 
            K1["Categories Page"]:::ui
            K2["Category View"]:::ui
            K3["Article View"]:::ui
            K4["Browse Component"]:::ui
            K5["Category Component"]:::ui
            K6["Article Component"]:::ui
        end
        subgraph "Case Stories" 
            S1["Stories Page"]:::ui
            S2["Story Detail Page"]:::ui
            S3["Share Story Page"]:::ui
            S4["Directory Component"]:::ui
            S5["Story View Component"]:::ui
            S6["Share Form Component"]:::ui
        end
        subgraph "User Dashboard" 
            U1["Dashboard Home"]:::ui
            U2["Chats Tab"]:::ui
            U3["History Tab"]:::ui
            U4["Profile Tab"]:::ui
            U5["Settings Tab"]:::ui
            U6["Dashboard Component"]:::ui
            U7["Profile Dropdown"]:::ui
            U8["Settings Component"]:::ui
        end
        JS["Puter.js AI Script"]:::ai
        SC["Supabase Client Library"]:::api
    end

    %% Next.js Frontend Server
    subgraph "Next.js Frontend" 
        style Front fill:#E8F5E9,stroke:#66BB6A
        LR["app/layout.tsx"]:::frontend
        PG["app/page.tsx"]:::frontend
        ML["middleware.ts"]:::backend
        IB["lib/supabase/middleware.ts"]:::backend
        I18C["lib/i18n/context.tsx"]:::frontend
        I18T["lib/i18n/translations.ts"]:::frontend
        GC["app/globals.css"]:::frontend
        GS["styles/globals.css"]:::frontend
        UIlib["components/ui/**/*.tsx"]:::ui
    end

    %% Next.js API Layer
    subgraph "Next.js API (Serverless Functions)" 
        style API fill:#FFF3E0,stroke:#FFA726
        subgraph "Chat API"
            CA1["/api/chat"]:::api
            CA2["/api/chat/start"]:::api
        end
        subgraph "Document API"
            DA1["/api/documents/enhance"]:::api
            DA2["/api/enhance-document"]:::api
        end
        BA["/api/bookmarks"]:::api
    end

    %% Supabase Layer
    subgraph "Supabase (Auth & DB)" 
        style DB fill:#FFF9C4,stroke:#FFEE58
        Auth["Auth Service"]:::db
        DBTables["Database Tables"]:::db
        MScripts["Migration Scripts"]:::db
    end

    %% External AI Service
    AIS["Puter.js AI Service (CDN)"]:::ai

    %% Deployment
    Vercel["Vercel Hosting"]:::infra

    %% Flows
    %% Chat Flow
    CI2 -->|"calls /api/chat"| CA1
    CA1 -->|"invokes AI"| AIS
    AIS -->|"response"| CA1
    CA1 -->|"stores/retrieves"| DBTables
    CA1 --> CI2

    %% Document Flow
    DG2 -->|"calls /api/documents/enhance"| DA1
    DA1 -->|"invokes AI"| AIS
    AIS --> DA1
    DA1 --> DBTables
    DA1 --> DG2

    %% Bookmark Flow
    CI2 -->|"calls /api/bookmarks"| BA
    BA --> DBTables
    BA --> CI2

    %% Auth Flow
    SC --> Auth
    CI1 -->|"Supabase Auth"| Auth
    Auth --> CI1
    ML --> Auth
    IB --> Auth

    %% Content Fetch Flow
    LR --> SC
    PG --> SC
    I18C --> PG
    I18T --> PG

    %% Deployment Arrows
    LR --> Vercel
    CA1 --> Vercel
    DA1 --> Vercel
    BA --> Vercel

    %% Click Events
    click LR "https://github.com/sameer-ssr99/nyaya-ai/blob/main/app/layout.tsx"
    click PG "https://github.com/sameer-ssr99/nyaya-ai/blob/main/app/page.tsx"
    click ML "https://github.com/sameer-ssr99/nyaya-ai/blob/main/middleware.ts"
    click IB "https://github.com/sameer-ssr99/nyaya-ai/blob/main/lib/supabase/middleware.ts"
    click I18C "https://github.com/sameer-ssr99/nyaya-ai/blob/main/lib/i18n/context.tsx"
    click I18T "https://github.com/sameer-ssr99/nyaya-ai/blob/main/lib/i18n/translations.ts"
    click CA1 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/app/api/chat/route.ts"
    click CA2 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/app/api/chat/start/route.ts"
    click DA1 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/app/api/documents/enhance/route.ts"
    click DA2 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/app/api/enhance-document/route.ts"
    click BA "https://github.com/sameer-ssr99/nyaya-ai/blob/main/app/api/bookmarks/route.ts"
    click CI1 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/app/chat/page.tsx"
    click CI2 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/app/chat/[conversationId]/page.tsx"
    click CI3 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/components/chat-interface.tsx"
    click CI4 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/components/chat-history.tsx"
    click DG1 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/app/documents/generate/[template]/page.tsx"
    click DG2 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/components/document-generator.tsx"
    click DG3 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/components/document-templates.tsx"
    click DG4 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/app/documents/history/page.tsx"
    click LD1 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/app/lawyers/page.tsx"
    click LD2 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/app/lawyers/[id]/page.tsx"
    click LD3 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/app/lawyers/[id]/consult/page.tsx"
    click LD4 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/components/lawyer-directory.tsx"
    click LD5 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/components/lawyer-profile.tsx"
    click LD6 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/components/consultation-booking.tsx"
    click LD7 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/components/lawyer-chat-interface.tsx"
    click K1 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/app/kyr/page.tsx"
    click K2 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/app/kyr/[category]/page.tsx"
    click K3 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/app/kyr/[category]/[article]/page.tsx"
    click K4 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/components/kyr-browse.tsx"
    click K5 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/components/kyr-category-view.tsx"
    click K6 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/components/kyr-article-view.tsx"
    click S1 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/app/stories/page.tsx"
    click S2 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/app/stories/[id]/page.tsx"
    click S3 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/app/stories/share/page.tsx"
    click S4 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/components/stories-directory.tsx"
    click S5 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/components/story-view.tsx"
    click S6 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/components/share-story-form.tsx"
    click U1 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/app/dashboard/page.tsx"
    click U2 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/app/dashboard/chats/page.tsx"
    click U3 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/app/dashboard/history/page.tsx"
    click U4 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/app/dashboard/profile/page.tsx"
    click U5 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/app/dashboard/settings/page.tsx"
    click U6 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/components/user-dashboard.tsx"
    click U7 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/components/user-profile-dropdown.tsx"
    click U8 "https://github.com/sameer-ssr99/nyaya-ai/blob/main/components/user-settings.tsx"
    click MScripts "https://github.com/sameer-ssr99/nyaya-ai/blob/main/scripts/*.sql"

    %% Styles
    classDef ui fill:#E3F2FD,stroke:#42A5F5
    classDef frontend fill:#E8F5E9,stroke:#66BB6A
    classDef api fill:#FFF3E0,stroke:#FFA726
    classDef backend fill:#E0F7FA,stroke:#00ACC1
    classDef db fill:#FFF9C4,stroke:#FFEE58
    classDef ai fill:#F3E5F5,stroke:#AB47BC
    classDef infra fill:#ECEFF1,stroke:#90A4AE
```
