# ajaysai157/legal_document_analyzer architecture

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

- Interactive diagram: https://gitdiagram.com/ajaysai157/legal_document_analyzer
- Repository: https://github.com/ajaysai157/legal_document_analyzer

## Overview

The product is a legal and policy document analyzer. Its primary workflow is upload and parsing, clause and cross-clause analysis, risk scoring, optional human review, and report generation; users can also ask grounded questions about a document. The graph preserves the documented stages and major UI capabilities. Some connections rely on the README or SOURCE INDEX because not all router and service implementations were excerpted.

Main entry points: [`app/services/analysis_graph.py`](https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/services/analysis_graph.py) (Analysis workflow), [`app/main.py`](https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/main.py) (FastAPI app), [`frontend/src/api/client.ts`](https://github.com/ajaysai157/legal_document_analyzer/blob/main/frontend/src/api/client.ts) (Typed API client), [`app/db/models.py`](https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/db/models.py) (Relational store)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["User interface"]
  node_upload["Upload page<br/>[UploadPage.tsx]"]
  node_workspace["Document workspace<br/>[WorkspacePage.tsx]"]
  node_review_ui["Review page<br/>[ReviewPage.tsx]"]
  node_report_ui["Report page<br/>[ReportPage.tsx]"]
  node_doc_view["Document viewer<br/>[DocumentViewer.tsx]"]
  node_chat_ui["Chat panel<br/>[ChatPanel.tsx]"]
  node_report_view["Report summary<br/>[ReportSummary.tsx]"]
  node_review_card["Review card"]
  node_api_client["Typed API client<br/>[client.ts]"]
end

subgraph group_api["API and workflow"]
  node_app["FastAPI app<br/>[main.py]"]
  node_documents["Document endpoints<br/>[documents.py]"]
  node_clauses_findings["Clause and finding endpoints<br/>[clauses.py]"]
  node_review["Review endpoints<br/>[review.py]"]
  node_report["Report endpoints<br/>[report.py]"]
  node_chat["Chat endpoints<br/>[chat.py]"]
end

subgraph group_analysis["Analysis services"]
  node_parser["Document parser<br/>[parser.py]"]
  node_graph{{"Analysis workflow<br/>[analysis_graph.py]"}}
  node_clause_analysis["Clause analysis<br/>[clause_analyzer.py]"]
  node_cross_clause["Cross-clause reasoning<br/>[cross_clause.py]"]
  node_risk["Risk scoring<br/>[risk_scoring.py]"]
  node_retriever["Clause retrieval<br/>[retriever.py]"]
end

subgraph group_data["Persistence and models"]
  node_database[("Relational store<br/>[models.py]")]
  node_schemas["Domain schemas<br/>[clause.py]"]
  node_config["Application settings<br/>[config.py]"]
  node_exceptions["Domain exceptions<br/>[exceptions.py]"]
end

subgraph group_integration["Model providers"]
  node_llm_factory["LLM client factory<br/>[llm_client.py]"]
  node_llm_provider["Configured LLM provider"]
end

node_user(("Document reviewer"))

node_user -->|"uploads document"| node_upload
node_upload -->|"submits file"| node_api_client
node_workspace -->|"loads document data"| node_api_client
node_review_ui -->|"submits review"| node_api_client
node_report_ui -->|"requests report"| node_api_client
node_workspace -->|"renders document"| node_doc_view
node_workspace -->|"opens chat"| node_chat_ui
node_report_ui -->|"renders summary"| node_report_view
node_review_ui -->|"renders queue item"| node_review_card
node_api_client -->|"sends API requests"| node_app
node_app -->|"routes requests"| node_documents
node_app -->|"routes requests"| node_clauses_findings
node_app -->|"routes requests"| node_review
node_app -->|"routes requests"| node_report
node_app -->|"routes requests"| node_chat
node_documents -->|"parses upload"| node_parser
node_documents -->|"starts analysis"| node_graph
node_graph -->|"analyzes clauses"| node_clause_analysis
node_graph -->|"checks relationships"| node_cross_clause
node_graph -->|"calculates score"| node_risk
node_clause_analysis -->|"requests analysis model"| node_llm_factory
node_cross_clause -->|"requests reasoning model"| node_llm_factory
node_llm_factory -->|"creates model client"| node_llm_provider
node_review -->|"resumes workflow"| node_graph
node_chat -->|"retrieves clauses"| node_retriever
node_chat -->|"requests answer model"| node_llm_factory
node_documents -->|"stores document data"| node_database
node_graph -->|"persists analysis"| node_database
node_report -->|"reads report inputs"| node_database
node_chat -->|"reads document data"| node_database
node_review -->|"updates review state"| node_database
node_graph -->|"uses clause types"| node_schemas
node_database -->|"configures connection"| node_config
node_documents -->|"raises upload errors"| node_exceptions
node_report -->|"requests report model"| node_llm_factory
node_chat_ui -->|"sends question"| node_api_client

click node_upload "https://github.com/ajaysai157/legal_document_analyzer/blob/main/frontend/src/pages/UploadPage.tsx"
click node_workspace "https://github.com/ajaysai157/legal_document_analyzer/blob/main/frontend/src/pages/WorkspacePage.tsx"
click node_review_ui "https://github.com/ajaysai157/legal_document_analyzer/blob/main/frontend/src/pages/ReviewPage.tsx"
click node_report_ui "https://github.com/ajaysai157/legal_document_analyzer/blob/main/frontend/src/pages/ReportPage.tsx"
click node_doc_view "https://github.com/ajaysai157/legal_document_analyzer/blob/main/frontend/src/components/DocumentViewer.tsx"
click node_chat_ui "https://github.com/ajaysai157/legal_document_analyzer/blob/main/frontend/src/components/ChatPanel.tsx"
click node_report_view "https://github.com/ajaysai157/legal_document_analyzer/blob/main/frontend/src/components/ReportSummary.tsx"
click node_review_card "https://github.com/ajaysai157/legal_document_analyzer/blob/main/frontend/src/components/ReviewQueueCard.tsx"
click node_api_client "https://github.com/ajaysai157/legal_document_analyzer/blob/main/frontend/src/api/client.ts"
click node_app "https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/main.py"
click node_documents "https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/routers/documents.py"
click node_clauses_findings "https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/routers/clauses.py"
click node_review "https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/routers/review.py"
click node_report "https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/routers/report.py"
click node_chat "https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/routers/chat.py"
click node_parser "https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/services/parser.py"
click node_graph "https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/services/analysis_graph.py"
click node_clause_analysis "https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/services/clause_analyzer.py"
click node_cross_clause "https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/services/cross_clause.py"
click node_risk "https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/services/risk_scoring.py"
click node_retriever "https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/services/retriever.py"
click node_llm_factory "https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/services/llm_client.py"
click node_database "https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/db/models.py"
click node_schemas "https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/models/clause.py"
click node_config "https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/config.py"
click node_exceptions "https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/core/exceptions.py"

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_upload,node_workspace,node_review_ui,node_report_ui,node_doc_view,node_chat_ui,node_report_view,node_review_card,node_api_client,node_user toneBlue
class node_app,node_documents,node_clauses_findings,node_review,node_report,node_chat toneAmber
class node_parser,node_graph,node_clause_analysis,node_cross_clause,node_risk,node_retriever toneMint
class node_database,node_schemas,node_config,node_exceptions toneRose
class node_llm_factory,node_llm_provider toneIndigo
```

## Components

### User interface

- [Upload page](https://github.com/ajaysai157/legal_document_analyzer/blob/main/frontend/src/pages/UploadPage.tsx): `frontend/src/pages/UploadPage.tsx`
- [Document workspace](https://github.com/ajaysai157/legal_document_analyzer/blob/main/frontend/src/pages/WorkspacePage.tsx): `frontend/src/pages/WorkspacePage.tsx`
- [Review page](https://github.com/ajaysai157/legal_document_analyzer/blob/main/frontend/src/pages/ReviewPage.tsx): `frontend/src/pages/ReviewPage.tsx`
- [Report page](https://github.com/ajaysai157/legal_document_analyzer/blob/main/frontend/src/pages/ReportPage.tsx): `frontend/src/pages/ReportPage.tsx`
- [Document viewer](https://github.com/ajaysai157/legal_document_analyzer/blob/main/frontend/src/components/DocumentViewer.tsx): `frontend/src/components/DocumentViewer.tsx`
- [Chat panel](https://github.com/ajaysai157/legal_document_analyzer/blob/main/frontend/src/components/ChatPanel.tsx): `frontend/src/components/ChatPanel.tsx`
- [Report summary](https://github.com/ajaysai157/legal_document_analyzer/blob/main/frontend/src/components/ReportSummary.tsx): `frontend/src/components/ReportSummary.tsx`
- [Review card](https://github.com/ajaysai157/legal_document_analyzer/blob/main/frontend/src/components/ReviewQueueCard.tsx): `frontend/src/components/ReviewQueueCard.tsx`
- [Typed API client](https://github.com/ajaysai157/legal_document_analyzer/blob/main/frontend/src/api/client.ts): `frontend/src/api/client.ts`

### API and workflow

- [FastAPI app](https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/main.py): `app/main.py`
- [Document endpoints](https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/routers/documents.py): `app/routers/documents.py`
- [Clause and finding endpoints](https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/routers/clauses.py): `app/routers/clauses.py`
- [Review endpoints](https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/routers/review.py): `app/routers/review.py`
- [Report endpoints](https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/routers/report.py): `app/routers/report.py`
- [Chat endpoints](https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/routers/chat.py): `app/routers/chat.py`

### Analysis services

- [Document parser](https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/services/parser.py): `app/services/parser.py`
- [Analysis workflow](https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/services/analysis_graph.py): `app/services/analysis_graph.py`
- [Clause analysis](https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/services/clause_analyzer.py): `app/services/clause_analyzer.py`
- [Cross-clause reasoning](https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/services/cross_clause.py): `app/services/cross_clause.py`
- [Risk scoring](https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/services/risk_scoring.py): `app/services/risk_scoring.py`
- [Clause retrieval](https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/services/retriever.py): `app/services/retriever.py`

### Persistence and models

- [Relational store](https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/db/models.py): `app/db/models.py`
- [Domain schemas](https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/models/clause.py): `app/models/clause.py`
- [Application settings](https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/config.py): `app/config.py`
- [Domain exceptions](https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/core/exceptions.py): `app/core/exceptions.py`

### Model providers

- [LLM client factory](https://github.com/ajaysai157/legal_document_analyzer/blob/main/app/services/llm_client.py): `app/services/llm_client.py`
- **Configured LLM provider**

### Other components

- **Document reviewer**

## Connections

- Document reviewer → Upload page: uploads document (evidence: `README.md`)
- Upload page → Typed API client: submits file (evidence: `frontend/src/pages/UploadPage.tsx`)
- Document workspace → Typed API client: loads document data (evidence: `frontend/src/pages/WorkspacePage.tsx`)
- Review page → Typed API client: submits review (evidence: `frontend/src/pages/ReviewPage.tsx`)
- Report page → Typed API client: requests report (evidence: `frontend/src/pages/ReportPage.tsx`)
- Document workspace → Document viewer: renders document (evidence: `frontend/src/pages/WorkspacePage.tsx`)
- Document workspace → Chat panel: opens chat (evidence: `frontend/src/pages/WorkspacePage.tsx`)
- Report page → Report summary: renders summary (evidence: `frontend/src/pages/ReportPage.tsx`)
- Review page → Review card: renders queue item (evidence: `frontend/src/pages/ReviewPage.tsx`)
- Typed API client → FastAPI app: sends API requests (evidence: `README.md`)
- FastAPI app → Document endpoints: routes requests (evidence: `app/main.py`)
- FastAPI app → Clause and finding endpoints: routes requests (evidence: `app/main.py`)
- FastAPI app → Review endpoints: routes requests (evidence: `app/main.py`)
- FastAPI app → Report endpoints: routes requests (evidence: `app/main.py`)
- FastAPI app → Chat endpoints: routes requests (evidence: `app/main.py`)
- Document endpoints → Document parser: parses upload (evidence: `app/routers/documents.py`)
- Document endpoints → Analysis workflow: starts analysis (evidence: `app/routers/documents.py`)
- Analysis workflow → Clause analysis: analyzes clauses (evidence: `app/services/analysis_graph.py`)
- Analysis workflow → Cross-clause reasoning: checks relationships (evidence: `app/services/analysis_graph.py`)
- Analysis workflow → Risk scoring: calculates score (evidence: `app/services/analysis_graph.py`)
- Clause analysis → LLM client factory: requests analysis model (evidence: `app/services/clause_analyzer.py`)
- Cross-clause reasoning → LLM client factory: requests reasoning model (evidence: `app/services/cross_clause.py`)
- LLM client factory → Configured LLM provider: creates model client (evidence: `app/services/llm_client.py`)
- Review endpoints → Analysis workflow: resumes workflow (evidence: `app/routers/review.py`)
- Chat endpoints → Clause retrieval: retrieves clauses (evidence: `app/routers/chat.py`)
- Chat endpoints → LLM client factory: requests answer model (evidence: `app/routers/chat.py`)
- Document endpoints → Relational store: stores document data (evidence: `app/routers/documents.py`)
- Analysis workflow → Relational store: persists analysis (evidence: `app/services/analysis_graph.py`)
- Report endpoints → Relational store: reads report inputs (evidence: `app/routers/report.py`)
- Chat endpoints → Relational store: reads document data (evidence: `app/routers/chat.py`)
- Review endpoints → Relational store: updates review state (evidence: `app/routers/review.py`)
- Analysis workflow → Domain schemas: uses clause types (evidence: `app/services/analysis_graph.py`)
- Relational store → Application settings: configures connection (evidence: `app/db/database.py`)
- Document endpoints → Domain exceptions: raises upload errors (evidence: `app/routers/documents.py`)
- Report endpoints → LLM client factory: requests report model (evidence: `app/routers/report.py`)
- Chat panel → Typed API client: sends question (evidence: `frontend/src/components/ChatPanel.tsx`)
