# adimyth/ai-contract-review architecture

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

- Interactive diagram: https://gitdiagram.com/adimyth/ai-contract-review
- Repository: https://github.com/adimyth/ai-contract-review

## Overview

Contract Review AI lets a user upload a PDF/DOCX or choose a bundled sample, then streams a playbook-based legal pre-screening to the browser. The backend validates and extracts document text, runs structured AI extraction followed by clause analysis, and returns verdicts, clause findings, and auto-approved clauses over SSE. Users can also inspect the playbook and browse samples. The graph separates the two pipeline stages although both live in one module; unsampled implementation details are represented only where the README documents them.

Main entry points: [`backend/main.py`](https://github.com/adimyth/ai-contract-review/blob/main/backend/main.py) (API routes), [`backend/pipeline.py`](https://github.com/adimyth/ai-contract-review/blob/main/backend/pipeline.py) (Contract extraction), [`frontend/app.js`](https://github.com/adimyth/ai-contract-review/blob/main/frontend/app.js) (Review interface), [`backend/playbook.py`](https://github.com/adimyth/ai-contract-review/blob/main/backend/playbook.py) (Legal positions)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Browser experience"]
  node_web_app["Review interface<br/>[app.js]"]
  node_sample_files["Sample contracts"]
  node_results["Streaming results<br/>[app.js]"]
  node_playbook_view["Playbook display<br/>[app.js]"]
  node_samples_view["Sample browser<br/>[app.js]"]
end

subgraph group_backend["Review backend"]
  node_review_api["API routes<br/>[main.py]"]
  node_text_parser["Document text parser<br/>[parser.py]"]
end

subgraph group_review["Contract analysis"]
  node_extraction["Contract extraction<br/>[pipeline.py]"]
  node_analysis["Playbook analysis<br/>[pipeline.py]"]
  node_review_models["Review data models<br/>[models.py]"]
  node_playbook["Legal positions<br/>[playbook.py]"]
end

node_user(("Reviewer"))
node_anthropic{{"Anthropic API"}}
node_openai{{"OpenAI API"}}

node_user -->|"uploads or selects"| node_web_app
node_web_app -->|"submits review"| node_review_api
node_review_api -->|"extracts text"| node_text_parser
node_review_api -->|"starts review"| node_extraction
node_extraction -->|"passes clauses"| node_analysis
node_extraction -->|"returns extraction"| node_review_models
node_analysis -->|"reads positions"| node_playbook
node_extraction -->|"calls when selected"| node_anthropic
node_extraction -->|"calls when selected"| node_openai
node_analysis -->|"streams when selected"| node_anthropic
node_analysis -->|"streams when selected"| node_openai
node_analysis -->|"streams findings"| node_results
node_results -->|"renders review"| node_web_app
node_web_app -->|"loads samples"| node_samples_view
node_samples_view -->|"requests sample list"| node_review_api
node_review_api -->|"serves selected file"| node_sample_files
node_web_app -->|"loads rules"| node_playbook_view
node_playbook_view -->|"requests playbook"| node_review_api
node_review_api -->|"returns positions"| node_playbook

click node_web_app "https://github.com/adimyth/ai-contract-review/blob/main/frontend/app.js"
click node_sample_files "https://github.com/adimyth/ai-contract-review/tree/main/mock_contracts"
click node_review_api "https://github.com/adimyth/ai-contract-review/blob/main/backend/main.py"
click node_text_parser "https://github.com/adimyth/ai-contract-review/blob/main/backend/parser.py"
click node_extraction "https://github.com/adimyth/ai-contract-review/blob/main/backend/pipeline.py"
click node_analysis "https://github.com/adimyth/ai-contract-review/blob/main/backend/pipeline.py"
click node_review_models "https://github.com/adimyth/ai-contract-review/blob/main/backend/models.py"
click node_playbook "https://github.com/adimyth/ai-contract-review/blob/main/backend/playbook.py"
click node_results "https://github.com/adimyth/ai-contract-review/blob/main/frontend/app.js"
click node_playbook_view "https://github.com/adimyth/ai-contract-review/blob/main/frontend/app.js"
click node_samples_view "https://github.com/adimyth/ai-contract-review/blob/main/frontend/app.js"

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_web_app,node_sample_files,node_results,node_playbook_view,node_samples_view,node_user toneBlue
class node_review_api,node_text_parser toneAmber
class node_extraction,node_analysis,node_review_models,node_playbook,node_anthropic,node_openai toneMint
```

## Components

### Browser experience

- [Review interface](https://github.com/adimyth/ai-contract-review/blob/main/frontend/app.js): `frontend/app.js`
- [Sample contracts](https://github.com/adimyth/ai-contract-review/tree/main/mock_contracts): `mock_contracts`
- [Streaming results](https://github.com/adimyth/ai-contract-review/blob/main/frontend/app.js): `frontend/app.js`
- [Playbook display](https://github.com/adimyth/ai-contract-review/blob/main/frontend/app.js): `frontend/app.js`
- [Sample browser](https://github.com/adimyth/ai-contract-review/blob/main/frontend/app.js): `frontend/app.js`

### Review backend

- [API routes](https://github.com/adimyth/ai-contract-review/blob/main/backend/main.py): `backend/main.py`
- [Document text parser](https://github.com/adimyth/ai-contract-review/blob/main/backend/parser.py): `backend/parser.py`

### Contract analysis

- [Contract extraction](https://github.com/adimyth/ai-contract-review/blob/main/backend/pipeline.py): `backend/pipeline.py`
- [Playbook analysis](https://github.com/adimyth/ai-contract-review/blob/main/backend/pipeline.py): `backend/pipeline.py`
- [Review data models](https://github.com/adimyth/ai-contract-review/blob/main/backend/models.py): `backend/models.py`
- [Legal positions](https://github.com/adimyth/ai-contract-review/blob/main/backend/playbook.py): `backend/playbook.py`

### Other components

- **Reviewer**
- **Anthropic API**
- **OpenAI API**

## Connections

- Reviewer → Review interface: uploads or selects (evidence: `README.md`)
- Review interface → API routes: submits review (evidence: `frontend/app.js`)
- API routes → Document text parser: extracts text (evidence: `backend/main.py`)
- API routes → Contract extraction: starts review (evidence: `backend/main.py`)
- Contract extraction → Playbook analysis: passes clauses (evidence: `README.md`)
- Contract extraction → Review data models: returns extraction (evidence: `backend/pipeline.py`)
- Playbook analysis → Legal positions: reads positions (evidence: `backend/pipeline.py`)
- Contract extraction → Anthropic API: calls when selected (evidence: `backend/pipeline.py`)
- Contract extraction → OpenAI API: calls when selected (evidence: `backend/pipeline.py`)
- Playbook analysis → Anthropic API: streams when selected (evidence: `backend/pipeline.py`)
- Playbook analysis → OpenAI API: streams when selected (evidence: `backend/pipeline.py`)
- Playbook analysis → Streaming results: streams findings (evidence: `README.md`)
- Streaming results → Review interface: renders review (evidence: `frontend/app.js`)
- Review interface → Sample browser: loads samples (evidence: `frontend/app.js`)
- Sample browser → API routes: requests sample list (evidence: `README.md`)
- API routes → Sample contracts: serves selected file (evidence: `README.md`)
- Review interface → Playbook display: loads rules (evidence: `frontend/app.js`)
- Playbook display → API routes: requests playbook (evidence: `README.md`)
- API routes → Legal positions: returns positions (evidence: `backend/main.py`)
