# m-tanay/criminal-law-chatbot architecture

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

- Interactive diagram: https://gitdiagram.com/m-tanay/criminal-law-chatbot
- Repository: https://github.com/m-tanay/criminal-law-chatbot

## Overview

This is a citation-grounded Australian criminal-law RAG assistant for Commonwealth and NSW legislation. A user asks in the React app; the app calls its chat API, the server proxy forwards to n8n, and the workflow retrieves statutory text, gates weak matches, drafts and checks a grounded answer, then returns sources with the response. Separate ingestion scripts build the shared Qdrant corpus. The README also describes a Control Hub for operations. Workflow internals are represented from README documentation; unsampled implementation details are not inferred.

Main entry points: [`ui/app/src/App.tsx`](https://github.com/m-tanay/criminal-law-chatbot/blob/main/ui/app/src/App.tsx) (Chat application), [`rag/lib/common.py`](https://github.com/m-tanay/criminal-law-chatbot/blob/main/rag/lib/common.py) (Chunk, embed, store), [`ui/app/src/components`](https://github.com/m-tanay/criminal-law-chatbot/tree/main/ui/app/src/components) (Chat components), [`ui/app/src/lib/api.ts`](https://github.com/m-tanay/criminal-law-chatbot/blob/main/ui/app/src/lib/api.ts) (Chat API client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Chat experience"]
  node_app["Chat application<br/>[App.tsx]"]
  node_chat_ui["Chat components"]
  node_citation_logic["Citation extraction<br/>[citations.ts]"]
  node_source_links["Source register links<br/>[legislation.ts]"]
  node_chat_api["Chat API client<br/>[api.ts]"]
  node_proxy["HTTP chat proxy<br/>[chatProxy.mjs]"]
end

subgraph group_answer["Answer workflow"]
  node_workflow["n8n chat workflow"]
  node_injection["Input guard"]
  node_embed_query["Query embedding"]
  node_retrieval["Vector retrieval"]
  node_score_gate["Match score gate"]
  node_draft["Grounded answer draft"]
  node_citation_guard["Groundedness guard"]
  node_audit["Audit and response"]
end

subgraph group_corpus["Legislation corpus"]
  node_qdrant[("Qdrant collection")]
  node_cth_ingest["Commonwealth ingestion<br/>[ingest_cth.py]"]
  node_nsw_ingest["NSW ingestion<br/>[ingest_nsw.py]"]
  node_nsw_scan["NSW corpus scan<br/>[scan_oalc_nsw.py]"]
  node_ingest_common["Chunk, embed, store<br/>[common.py]"]
end

subgraph group_operations["Operations"]
  node_control_hub["Control Hub<br/>[hub.py]"]
end

node_user(("Legal questioner"))
node_ollama{{"Ollama embeddings"}}
node_openrouter{{"OpenRouter model"}}
node_cth_register{{"Federal legislation source"}}
node_nsw_corpus{{"Open Australian Legal Corpus"}}

node_user -->|"asks"| node_app
node_app -->|"renders"| node_chat_ui
node_app -->|"sends query"| node_chat_api
node_chat_api -->|"posts chat"| node_proxy
node_proxy -->|"forwards request"| node_workflow
node_workflow -->|"sanitizes input"| node_injection
node_injection -->|"passes input"| node_embed_query
node_embed_query -->|"embeds query"| node_ollama
node_embed_query -->|"provides vector"| node_retrieval
node_retrieval -->|"searches top 8"| node_qdrant
node_retrieval -->|"returns matches"| node_score_gate
node_score_gate -->|"passes strong match"| node_draft
node_score_gate -->|"refuses weak match"| node_audit
node_draft -->|"requests draft"| node_openrouter
node_draft -->|"checks citations"| node_citation_guard
node_citation_guard -->|"passes grounded answer"| node_audit
node_audit -->|"returns answer and sources"| node_proxy
node_chat_api -->|"returns response"| node_app
node_app -->|"extracts citations"| node_citation_logic
node_chat_ui -->|"renders citations"| node_citation_logic
node_chat_ui -->|"links registers"| node_source_links
node_cth_register -->|"supplies legislation"| node_cth_ingest
node_nsw_corpus -->|"supplies corpus slice"| node_nsw_scan
node_nsw_scan -->|"prepares input"| node_nsw_ingest
node_cth_ingest -->|"uses helpers"| node_ingest_common
node_nsw_ingest -->|"uses helpers"| node_ingest_common
node_ingest_common -->|"embeds chunks"| node_ollama
node_ingest_common -->|"upserts chunks"| node_qdrant
node_control_hub -.->|"browses law database"| node_qdrant
node_control_hub -.->|"inspects runs and health"| node_workflow

click node_app "https://github.com/m-tanay/criminal-law-chatbot/blob/main/ui/app/src/App.tsx"
click node_chat_ui "https://github.com/m-tanay/criminal-law-chatbot/tree/main/ui/app/src/components"
click node_citation_logic "https://github.com/m-tanay/criminal-law-chatbot/blob/main/ui/app/src/lib/citations.ts"
click node_source_links "https://github.com/m-tanay/criminal-law-chatbot/blob/main/ui/app/src/lib/legislation.ts"
click node_chat_api "https://github.com/m-tanay/criminal-law-chatbot/blob/main/ui/app/src/lib/api.ts"
click node_proxy "https://github.com/m-tanay/criminal-law-chatbot/blob/main/ui/app/server/chatProxy.mjs"
click node_workflow "https://github.com/m-tanay/criminal-law-chatbot/blob/main/n8n/virtual-lawyer-clean-v1.workflow.json"
click node_injection "https://github.com/m-tanay/criminal-law-chatbot/blob/main/n8n/virtual-lawyer-clean-v1.workflow.json"
click node_embed_query "https://github.com/m-tanay/criminal-law-chatbot/blob/main/n8n/virtual-lawyer-clean-v1.workflow.json"
click node_retrieval "https://github.com/m-tanay/criminal-law-chatbot/blob/main/n8n/virtual-lawyer-clean-v1.workflow.json"
click node_score_gate "https://github.com/m-tanay/criminal-law-chatbot/blob/main/n8n/virtual-lawyer-clean-v1.workflow.json"
click node_draft "https://github.com/m-tanay/criminal-law-chatbot/blob/main/n8n/virtual-lawyer-clean-v1.workflow.json"
click node_citation_guard "https://github.com/m-tanay/criminal-law-chatbot/blob/main/n8n/virtual-lawyer-clean-v1.workflow.json"
click node_audit "https://github.com/m-tanay/criminal-law-chatbot/blob/main/n8n/virtual-lawyer-clean-v1.workflow.json"
click node_cth_ingest "https://github.com/m-tanay/criminal-law-chatbot/blob/main/rag/ingest/ingest_cth.py"
click node_nsw_ingest "https://github.com/m-tanay/criminal-law-chatbot/blob/main/rag/ingest/ingest_nsw.py"
click node_nsw_scan "https://github.com/m-tanay/criminal-law-chatbot/blob/main/rag/ingest/scan_oalc_nsw.py"
click node_ingest_common "https://github.com/m-tanay/criminal-law-chatbot/blob/main/rag/lib/common.py"
click node_control_hub "https://github.com/m-tanay/criminal-law-chatbot/blob/main/control-hub/hub.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_app,node_chat_ui,node_citation_logic,node_source_links,node_chat_api,node_proxy toneBlue
class node_workflow,node_injection,node_embed_query,node_retrieval,node_score_gate,node_draft,node_citation_guard,node_audit toneAmber
class node_qdrant,node_cth_ingest,node_nsw_ingest,node_nsw_scan,node_ingest_common,node_openrouter toneMint
class node_control_hub toneRose
class node_user,node_ollama,node_cth_register,node_nsw_corpus toneIndigo
```

## Components

### Chat experience

- [Chat application](https://github.com/m-tanay/criminal-law-chatbot/blob/main/ui/app/src/App.tsx): `ui/app/src/App.tsx`
- [Chat components](https://github.com/m-tanay/criminal-law-chatbot/tree/main/ui/app/src/components): `ui/app/src/components`
- [Citation extraction](https://github.com/m-tanay/criminal-law-chatbot/blob/main/ui/app/src/lib/citations.ts): `ui/app/src/lib/citations.ts`
- [Source register links](https://github.com/m-tanay/criminal-law-chatbot/blob/main/ui/app/src/lib/legislation.ts): `ui/app/src/lib/legislation.ts`
- [Chat API client](https://github.com/m-tanay/criminal-law-chatbot/blob/main/ui/app/src/lib/api.ts): `ui/app/src/lib/api.ts`
- [HTTP chat proxy](https://github.com/m-tanay/criminal-law-chatbot/blob/main/ui/app/server/chatProxy.mjs): `ui/app/server/chatProxy.mjs`

### Answer workflow

- [n8n chat workflow](https://github.com/m-tanay/criminal-law-chatbot/blob/main/n8n/virtual-lawyer-clean-v1.workflow.json): `n8n/virtual-lawyer-clean-v1.workflow.json`
- [Input guard](https://github.com/m-tanay/criminal-law-chatbot/blob/main/n8n/virtual-lawyer-clean-v1.workflow.json): `n8n/virtual-lawyer-clean-v1.workflow.json`
- [Query embedding](https://github.com/m-tanay/criminal-law-chatbot/blob/main/n8n/virtual-lawyer-clean-v1.workflow.json): `n8n/virtual-lawyer-clean-v1.workflow.json`
- [Vector retrieval](https://github.com/m-tanay/criminal-law-chatbot/blob/main/n8n/virtual-lawyer-clean-v1.workflow.json): `n8n/virtual-lawyer-clean-v1.workflow.json`
- [Match score gate](https://github.com/m-tanay/criminal-law-chatbot/blob/main/n8n/virtual-lawyer-clean-v1.workflow.json): `n8n/virtual-lawyer-clean-v1.workflow.json`
- [Grounded answer draft](https://github.com/m-tanay/criminal-law-chatbot/blob/main/n8n/virtual-lawyer-clean-v1.workflow.json): `n8n/virtual-lawyer-clean-v1.workflow.json`
- [Groundedness guard](https://github.com/m-tanay/criminal-law-chatbot/blob/main/n8n/virtual-lawyer-clean-v1.workflow.json): `n8n/virtual-lawyer-clean-v1.workflow.json`
- [Audit and response](https://github.com/m-tanay/criminal-law-chatbot/blob/main/n8n/virtual-lawyer-clean-v1.workflow.json): `n8n/virtual-lawyer-clean-v1.workflow.json`

### Legislation corpus

- **Qdrant collection**
- [Commonwealth ingestion](https://github.com/m-tanay/criminal-law-chatbot/blob/main/rag/ingest/ingest_cth.py): `rag/ingest/ingest_cth.py`
- [NSW ingestion](https://github.com/m-tanay/criminal-law-chatbot/blob/main/rag/ingest/ingest_nsw.py): `rag/ingest/ingest_nsw.py`
- [NSW corpus scan](https://github.com/m-tanay/criminal-law-chatbot/blob/main/rag/ingest/scan_oalc_nsw.py): `rag/ingest/scan_oalc_nsw.py`
- [Chunk, embed, store](https://github.com/m-tanay/criminal-law-chatbot/blob/main/rag/lib/common.py): `rag/lib/common.py`

### Operations

- [Control Hub](https://github.com/m-tanay/criminal-law-chatbot/blob/main/control-hub/hub.py): `control-hub/hub.py`

### Other components

- **Legal questioner**
- **Ollama embeddings**
- **OpenRouter model**
- **Federal legislation source**
- **Open Australian Legal Corpus**

## Connections

- Legal questioner → Chat application: asks (evidence: `README.md`)
- Chat application → Chat components: renders (evidence: `ui/app/src/App.tsx`)
- Chat application → Chat API client: sends query (evidence: `ui/app/src/App.tsx`)
- Chat API client → HTTP chat proxy: posts chat (evidence: `README.md`)
- HTTP chat proxy → n8n chat workflow: forwards request (evidence: `ui/app/server/chatProxy.mjs`)
- n8n chat workflow → Input guard: sanitizes input (evidence: `README.md`)
- Input guard → Query embedding: passes input (evidence: `README.md`)
- Query embedding → Ollama embeddings: embeds query (evidence: `README.md`)
- Query embedding → Vector retrieval: provides vector (evidence: `README.md`)
- Vector retrieval → Qdrant collection: searches top 8 (evidence: `README.md`)
- Vector retrieval → Match score gate: returns matches (evidence: `README.md`)
- Match score gate → Grounded answer draft: passes strong match (evidence: `README.md`)
- Match score gate → Audit and response: refuses weak match (evidence: `README.md`)
- Grounded answer draft → OpenRouter model: requests draft (evidence: `README.md`)
- Grounded answer draft → Groundedness guard: checks citations (evidence: `README.md`)
- Groundedness guard → Audit and response: passes grounded answer (evidence: `README.md`)
- Audit and response → HTTP chat proxy: returns answer and sources (evidence: `README.md`)
- Chat API client → Chat application: returns response (evidence: `ui/app/src/App.tsx`)
- Chat application → Citation extraction: extracts citations (evidence: `ui/app/src/App.tsx`)
- Chat components → Citation extraction: renders citations (evidence: `ui/app/src/components/MarkdownWithCitations.tsx`)
- Chat components → Source register links: links registers (evidence: `ui/app/src/components/SourcesPanel.tsx`)
- Federal legislation source → Commonwealth ingestion: supplies legislation (evidence: `rag/ingest/ingest_cth.py`)
- Open Australian Legal Corpus → NSW corpus scan: supplies corpus slice (evidence: `README.md`)
- NSW corpus scan → NSW ingestion: prepares input (evidence: `rag/ingest/ingest_nsw.py`)
- Commonwealth ingestion → Chunk, embed, store: uses helpers (evidence: `rag/ingest/ingest_cth.py`)
- NSW ingestion → Chunk, embed, store: uses helpers (evidence: `rag/ingest/ingest_nsw.py`)
- Chunk, embed, store → Ollama embeddings: embeds chunks (evidence: `rag/lib/common.py`)
- Chunk, embed, store → Qdrant collection: upserts chunks (evidence: `rag/lib/common.py`)
- Control Hub → Qdrant collection: browses law database (evidence: `README.md`)
- Control Hub → n8n chat workflow: inspects runs and health (evidence: `README.md`)
