# elvismutinda/intelaw architecture

> Architecture diagram of the GitHub repository elvismutinda/intelaw, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/elvismutinda/intelaw
- Repository: https://github.com/elvismutinda/intelaw

## Overview

Intelaw is a Kenyan-law research and document-assistance product: users can create document-backed or case-law chats, ask legal questions, and generate drafts; the repository also includes account, subscription, and admin features. The main runtime is a Next.js client with authenticated API routes and database-backed chat history, alongside a separate FastAPI service and legal-material scraping tools. The graph shows only relationships visible in sampled code or the source index; unsampled settings and product areas are represented without guessed call paths.

Main entry points: [`client/src/app/api/caselaws/route.ts`](https://github.com/elvismutinda/intelaw/blob/main/client/src/app/api/caselaws/route.ts) (Case-law chat API), [`client/auth.ts`](https://github.com/elvismutinda/intelaw/blob/main/client/auth.ts) (Authentication), [`client/src/app/api/chat/route.ts`](https://github.com/elvismutinda/intelaw/blob/main/client/src/app/api/chat/route.ts) (Document chat API), [`client/src/app/api/create-chat/route.ts`](https://github.com/elvismutinda/intelaw/blob/main/client/src/app/api/create-chat/route.ts) (Document chat setup)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_web["Web Experience"]
  node_landing["Landing and legal pages<br/>[page.tsx]"]
  node_chat_ui["Research chat UI<br/>[Chat.tsx]"]
  node_case_ui["Case-law browser<br/>[page.tsx]"]
  node_draft_ui["Draft workspace<br/>[page.tsx]"]
  node_settings["Account settings<br/>[actions.ts]"]
end

subgraph group_research["Legal Research"]
  node_chat_api["Document chat API<br/>[route.ts]"]
  node_case_api["Case-law chat API<br/>[route.ts]"]
  node_chat_create["Document chat setup<br/>[route.ts]"]
  node_case_messages["Case chat history API<br/>[route.ts]"]
  node_history["Chat history API<br/>[route.ts]"]
  node_context["Context retrieval<br/>[context.ts]"]
  node_db[("Application database<br/>[schema.ts]")]
  node_cloudinary["PDF file service<br/>[cloudinary.ts]"]
  node_neon_load["PDF indexing<br/>[neon.ts]"]
end

subgraph group_identity["Accounts and Admin"]
  node_admin["Admin reporting<br/>[page.tsx]"]
  node_auth["Authentication<br/>[auth.ts]"]
  node_auth_pages["Sign-in and registration<br/>[page.tsx]"]
end

subgraph group_models["AI Services"]
  node_llm_api["Draft chat embedding API<br/>[main.py]"]
  node_draft_engine["Draft generation<br/>[draft.py]"]
  node_hf_model["Hugging Face chat<br/>[hf_model.py]"]
  node_local_model["Local model runner<br/>[local_model.py]"]
end

subgraph group_ingestion["Legal Material Ingestion"]
  node_scraper_entry["Scraper entry point<br/>[scraper.py]"]
  node_case_links["Case link collection"]
  node_case_download["Case download and metadata"]
  node_klr_cases["KLR case collection"]
  node_klr_acts["KLR acts collection"]
end

node_user(("Legal user"))
node_hf_service["Hugging Face inference"]
node_kenyalaw["Kenya Law website"]

node_user -->|"browses"| node_landing
node_user -.->|"signs in"| node_auth_pages
node_auth_pages -.->|"authenticates"| node_auth
node_user -.->|"asks questions"| node_chat_ui
node_chat_ui -.->|"submits chat"| node_chat_api
node_chat_api -->|"checks session"| node_auth
node_chat_api -->|"gets context"| node_context
node_chat_api -->|"reads and saves"| node_db
node_chat_api -->|"streams completion"| node_hf_service
node_user -.->|"opens case"| node_case_ui
node_case_ui -.->|"asks case question"| node_case_api
node_case_api -->|"checks session"| node_auth
node_case_api -->|"gets case context"| node_context
node_case_api -->|"reads and saves"| node_db
node_case_api -->|"streams completion"| node_hf_service
node_case_api -->|"loads case text"| node_klr_cases
node_user -.->|"requests draft"| node_draft_ui
node_user -.->|"manages account"| node_settings
node_user -.->|"administers"| node_admin
node_chat_create -->|"checks session"| node_auth
node_chat_create -->|"indexes PDF"| node_neon_load
node_chat_create -->|"resolves file URL"| node_cloudinary
node_chat_create -->|"creates chat"| node_db
node_case_messages -->|"checks session"| node_auth
node_case_messages -->|"reads messages"| node_db
node_history -->|"checks session"| node_auth
node_user -.->|"requests draft or chat"| node_llm_api
node_llm_api -->|"generates draft"| node_draft_engine
node_llm_api -->|"runs chat"| node_hf_model
node_draft_engine -->|"runs model"| node_local_model
node_hf_model -->|"sends inference"| node_hf_service
node_scraper_entry -->|"uses links"| node_case_links
node_scraper_entry -->|"downloads cases"| node_case_download
node_case_links -.->|"collects links"| node_kenyalaw
node_case_download -.->|"downloads materials"| node_kenyalaw

click node_landing "https://github.com/elvismutinda/intelaw/blob/main/client/src/app/(nondashboard)/landing/page.tsx"
click node_chat_ui "https://github.com/elvismutinda/intelaw/blob/main/client/src/components/chat/Chat.tsx"
click node_case_ui "https://github.com/elvismutinda/intelaw/blob/main/client/src/app/app/caselaws/%5Bid%5D/page.tsx"
click node_draft_ui "https://github.com/elvismutinda/intelaw/blob/main/client/src/app/app/draft/%5Bslug%5D/page.tsx"
click node_settings "https://github.com/elvismutinda/intelaw/blob/main/client/src/app/app/settings/actions.ts"
click node_admin "https://github.com/elvismutinda/intelaw/blob/main/client/src/app/admin/page.tsx"
click node_auth "https://github.com/elvismutinda/intelaw/blob/main/client/auth.ts"
click node_auth_pages "https://github.com/elvismutinda/intelaw/blob/main/client/src/app/(auth)/login/page.tsx"
click node_chat_api "https://github.com/elvismutinda/intelaw/blob/main/client/src/app/api/chat/route.ts"
click node_case_api "https://github.com/elvismutinda/intelaw/blob/main/client/src/app/api/caselaws/route.ts"
click node_chat_create "https://github.com/elvismutinda/intelaw/blob/main/client/src/app/api/create-chat/route.ts"
click node_case_messages "https://github.com/elvismutinda/intelaw/blob/main/client/src/app/api/case-messages/route.ts"
click node_history "https://github.com/elvismutinda/intelaw/blob/main/client/src/app/api/history/route.ts"
click node_context "https://github.com/elvismutinda/intelaw/blob/main/client/src/lib/context.ts"
click node_db "https://github.com/elvismutinda/intelaw/blob/main/client/src/lib/db/schema.ts"
click node_cloudinary "https://github.com/elvismutinda/intelaw/blob/main/client/src/lib/cloudinary.ts"
click node_neon_load "https://github.com/elvismutinda/intelaw/blob/main/client/src/lib/neon.ts"
click node_llm_api "https://github.com/elvismutinda/intelaw/blob/main/local-llm/main.py"
click node_draft_engine "https://github.com/elvismutinda/intelaw/blob/main/local-llm/draft.py"
click node_hf_model "https://github.com/elvismutinda/intelaw/blob/main/local-llm/hf_model.py"
click node_local_model "https://github.com/elvismutinda/intelaw/blob/main/local-llm/local_model.py"
click node_scraper_entry "https://github.com/elvismutinda/intelaw/blob/main/scraping/scraper.py"
click node_case_links "https://github.com/elvismutinda/intelaw/blob/main/scraping/case_laws/case_link_scraper.py"
click node_case_download "https://github.com/elvismutinda/intelaw/blob/main/scraping/case_laws/case_law_downloader.py"
click node_klr_cases "https://github.com/elvismutinda/intelaw/tree/main/scraping/new_klr/case_laws"
click node_klr_acts "https://github.com/elvismutinda/intelaw/tree/main/scraping/new_klr/acts"

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_landing,node_chat_ui,node_case_ui,node_draft_ui,node_settings,node_user toneBlue
class node_chat_api,node_case_api,node_chat_create,node_case_messages,node_history,node_context,node_db,node_cloudinary,node_neon_load toneAmber
class node_admin,node_auth,node_auth_pages toneMint
class node_llm_api,node_draft_engine,node_hf_model,node_local_model toneRose
class node_scraper_entry,node_case_links,node_case_download,node_klr_cases,node_klr_acts,node_hf_service,node_kenyalaw toneIndigo
```

## Components

### Web Experience

- [Landing and legal pages](https://github.com/elvismutinda/intelaw/blob/main/client/src/app/(nondashboard)/landing/page.tsx): `client/src/app/(nondashboard)/landing/page.tsx`
- [Research chat UI](https://github.com/elvismutinda/intelaw/blob/main/client/src/components/chat/Chat.tsx): `client/src/components/chat/Chat.tsx`
- [Case-law browser](https://github.com/elvismutinda/intelaw/blob/main/client/src/app/app/caselaws/%5Bid%5D/page.tsx): `client/src/app/app/caselaws/[id]/page.tsx`
- [Draft workspace](https://github.com/elvismutinda/intelaw/blob/main/client/src/app/app/draft/%5Bslug%5D/page.tsx): `client/src/app/app/draft/[slug]/page.tsx`
- [Account settings](https://github.com/elvismutinda/intelaw/blob/main/client/src/app/app/settings/actions.ts): `client/src/app/app/settings/actions.ts`

### Legal Research

- [Document chat API](https://github.com/elvismutinda/intelaw/blob/main/client/src/app/api/chat/route.ts): `client/src/app/api/chat/route.ts`
- [Case-law chat API](https://github.com/elvismutinda/intelaw/blob/main/client/src/app/api/caselaws/route.ts): `client/src/app/api/caselaws/route.ts`
- [Document chat setup](https://github.com/elvismutinda/intelaw/blob/main/client/src/app/api/create-chat/route.ts): `client/src/app/api/create-chat/route.ts`
- [Case chat history API](https://github.com/elvismutinda/intelaw/blob/main/client/src/app/api/case-messages/route.ts): `client/src/app/api/case-messages/route.ts`
- [Chat history API](https://github.com/elvismutinda/intelaw/blob/main/client/src/app/api/history/route.ts): `client/src/app/api/history/route.ts`
- [Context retrieval](https://github.com/elvismutinda/intelaw/blob/main/client/src/lib/context.ts): `client/src/lib/context.ts`
- [Application database](https://github.com/elvismutinda/intelaw/blob/main/client/src/lib/db/schema.ts): `client/src/lib/db/schema.ts`
- [PDF file service](https://github.com/elvismutinda/intelaw/blob/main/client/src/lib/cloudinary.ts): `client/src/lib/cloudinary.ts`
- [PDF indexing](https://github.com/elvismutinda/intelaw/blob/main/client/src/lib/neon.ts): `client/src/lib/neon.ts`

### Accounts and Admin

- [Admin reporting](https://github.com/elvismutinda/intelaw/blob/main/client/src/app/admin/page.tsx): `client/src/app/admin/page.tsx`
- [Authentication](https://github.com/elvismutinda/intelaw/blob/main/client/auth.ts): `client/auth.ts`
- [Sign-in and registration](https://github.com/elvismutinda/intelaw/blob/main/client/src/app/(auth)/login/page.tsx): `client/src/app/(auth)/login/page.tsx`

### AI Services

- [Draft chat embedding API](https://github.com/elvismutinda/intelaw/blob/main/local-llm/main.py): `local-llm/main.py`
- [Draft generation](https://github.com/elvismutinda/intelaw/blob/main/local-llm/draft.py): `local-llm/draft.py`
- [Hugging Face chat](https://github.com/elvismutinda/intelaw/blob/main/local-llm/hf_model.py): `local-llm/hf_model.py`
- [Local model runner](https://github.com/elvismutinda/intelaw/blob/main/local-llm/local_model.py): `local-llm/local_model.py`

### Legal Material Ingestion

- [Scraper entry point](https://github.com/elvismutinda/intelaw/blob/main/scraping/scraper.py): `scraping/scraper.py`
- [Case link collection](https://github.com/elvismutinda/intelaw/blob/main/scraping/case_laws/case_link_scraper.py): `scraping/case_laws/case_link_scraper.py`
- [Case download and metadata](https://github.com/elvismutinda/intelaw/blob/main/scraping/case_laws/case_law_downloader.py): `scraping/case_laws/case_law_downloader.py`
- [KLR case collection](https://github.com/elvismutinda/intelaw/tree/main/scraping/new_klr/case_laws): `scraping/new_klr/case_laws`
- [KLR acts collection](https://github.com/elvismutinda/intelaw/tree/main/scraping/new_klr/acts): `scraping/new_klr/acts`

### Other components

- **Legal user**
- **Hugging Face inference**
- **Kenya Law website**

## Connections

- Legal user → Landing and legal pages: browses
- Legal user → Sign-in and registration: signs in
- Sign-in and registration → Authentication: authenticates
- Legal user → Research chat UI: asks questions
- Research chat UI → Document chat API: submits chat
- Document chat API → Authentication: checks session (evidence: `client/src/app/api/chat/route.ts`)
- Document chat API → Context retrieval: gets context (evidence: `client/src/app/api/chat/route.ts`)
- Document chat API → Application database: reads and saves (evidence: `client/src/app/api/chat/route.ts`)
- Document chat API → Hugging Face inference: streams completion (evidence: `client/src/app/api/chat/route.ts`)
- Legal user → Case-law browser: opens case
- Case-law browser → Case-law chat API: asks case question
- Case-law chat API → Authentication: checks session (evidence: `client/src/app/api/caselaws/route.ts`)
- Case-law chat API → Context retrieval: gets case context (evidence: `client/src/app/api/caselaws/route.ts`)
- Case-law chat API → Application database: reads and saves (evidence: `client/src/app/api/caselaws/route.ts`)
- Case-law chat API → Hugging Face inference: streams completion (evidence: `client/src/app/api/caselaws/route.ts`)
- Case-law chat API → KLR case collection: loads case text (evidence: `client/src/app/api/caselaws/route.ts`)
- Legal user → Draft workspace: requests draft
- Legal user → Account settings: manages account
- Legal user → Admin reporting: administers
- Document chat setup → Authentication: checks session (evidence: `client/src/app/api/create-chat/route.ts`)
- Document chat setup → PDF indexing: indexes PDF (evidence: `client/src/app/api/create-chat/route.ts`)
- Document chat setup → PDF file service: resolves file URL (evidence: `client/src/app/api/create-chat/route.ts`)
- Document chat setup → Application database: creates chat (evidence: `client/src/app/api/create-chat/route.ts`)
- Case chat history API → Authentication: checks session (evidence: `client/src/app/api/case-messages/route.ts`)
- Case chat history API → Application database: reads messages (evidence: `client/src/app/api/case-messages/route.ts`)
- Chat history API → Authentication: checks session (evidence: `client/src/app/api/history/route.ts`)
- Legal user → Draft chat embedding API: requests draft or chat
- Draft chat embedding API → Draft generation: generates draft (evidence: `local-llm/main.py`)
- Draft chat embedding API → Hugging Face chat: runs chat (evidence: `local-llm/main.py`)
- Draft generation → Local model runner: runs model (evidence: `local-llm/draft.py`)
- Hugging Face chat → Hugging Face inference: sends inference (evidence: `local-llm/hf_model.py`)
- Scraper entry point → Case link collection: uses links (evidence: `scraping/scraper.py`)
- Scraper entry point → Case download and metadata: downloads cases (evidence: `scraping/scraper.py`)
- Case link collection → Kenya Law website: collects links
- Case download and metadata → Kenya Law website: downloads materials
