# heart-breaker-beep/lexagent architecture

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

- Interactive diagram: https://gitdiagram.com/heart-breaker-beep/lexagent
- Repository: https://github.com/heart-breaker-beep/lexagent

## Overview

This is a Chinese-language legal assistant offering chat-based legal consultation, law retrieval, contract review, risk and jurisdiction analysis, limitation-period calculations, document drafting, web search, and evidence/document handling. The principal workflow is user chat through the FastAPI chat endpoint into a LangGraph agent, which can retrieve legal material and invoke MCP tools before streaming a response. Supporting subsystems provide retrieval and vector storage, contract analysis, persistent conversation and memory state, and a separate MCP tool server. Several links reflect explicit imports or documentation; unsampled implementation details are not inferred.

Main entry points: [`api/chat.py`](https://github.com/heart-breaker-beep/lexagent/blob/main/api/chat.py) (Chat endpoint), [`agent/nodes.py`](https://github.com/heart-breaker-beep/lexagent/blob/main/agent/nodes.py) (Agent nodes), [`main.py`](https://github.com/heart-breaker-beep/lexagent/blob/main/main.py) (FastAPI application), [`agent/graph.py`](https://github.com/heart-breaker-beep/lexagent/blob/main/agent/graph.py) (Agent graph)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Application entry"]
  node_main["FastAPI application<br/>[main.py]"]
  node_chat["Chat endpoint<br/>[chat.py]"]
  node_other_api["Other API routes"]
end

subgraph group_agent["Agent workflow"]
  node_graph["Agent graph<br/>[graph.py]"]
  node_agent_nodes["Agent nodes<br/>[nodes.py]"]
  node_llm["LLM gateway<br/>[llm.py]"]
  node_mcp_client["MCP client<br/>[mcp_client.py]"]
end

subgraph group_legal["Legal capabilities"]
  node_mcp_server["MCP tool server<br/>[server.py]"]
  node_legal_tools["Legal MCP tools"]
  node_contract_workflow["Contract analysis<br/>[workflow.py]"]
  node_legal_analysis["Legal analysis<br/>[legal_analysis.py]"]
  node_evidence["Evidence handling<br/>[evidence_video.py]"]
end

subgraph group_retrieval["Legal knowledge"]
  node_retriever["Hybrid retrieval<br/>[hybrid.py]"]
  node_vectorstore["Vector store interface<br/>[base.py]"]
  node_law_store[("Law vector store<br/>[chroma_store.py]")]
  node_indexer["Law indexing"]
end

subgraph group_state["Conversation state"]
  node_checkpoints[("Thread checkpoints<br/>[checkpoint.py]")]
  node_memory[("Conversation memory<br/>[memory.py]")]
  node_compaction["Context compaction"]
end

node_user(("User"))
node_external_llm{{"LLM provider"}}

node_user -->|"sends message"| node_chat
node_main -->|"registers route"| node_chat
node_main -->|"registers routes"| node_other_api
node_main -->|"builds graph"| node_graph
node_chat -.->|"streams run"| node_graph
node_graph -->|"dispatches nodes"| node_agent_nodes
node_agent_nodes -->|"calls model"| node_llm
node_llm -->|"sends requests"| node_external_llm
node_agent_nodes -->|"uses analysis"| node_legal_analysis
node_agent_nodes -.->|"uses formatter and schema"| node_contract_workflow
node_agent_nodes -.->|"uses MCP tools"| node_mcp_client
node_mcp_client -->|"starts subprocess"| node_mcp_server
node_mcp_server -->|"registers tools"| node_legal_tools
node_legal_tools -->|"retrieves laws"| node_retriever
node_retriever -->|"uses interface"| node_vectorstore
node_law_store -->|"implements interface"| node_vectorstore
node_chat -->|"reads thread state"| node_checkpoints
node_chat -->|"extracts memory"| node_memory
node_chat -->|"reports compaction"| node_compaction
node_chat -->|"uses evidence service"| node_evidence
node_indexer -.->|"writes law vectors"| node_law_store

click node_main "https://github.com/heart-breaker-beep/lexagent/blob/main/main.py"
click node_chat "https://github.com/heart-breaker-beep/lexagent/blob/main/api/chat.py"
click node_other_api "https://github.com/heart-breaker-beep/lexagent/tree/main/api"
click node_graph "https://github.com/heart-breaker-beep/lexagent/blob/main/agent/graph.py"
click node_agent_nodes "https://github.com/heart-breaker-beep/lexagent/blob/main/agent/nodes.py"
click node_llm "https://github.com/heart-breaker-beep/lexagent/blob/main/services/llm.py"
click node_mcp_client "https://github.com/heart-breaker-beep/lexagent/blob/main/services/mcp_client.py"
click node_mcp_server "https://github.com/heart-breaker-beep/lexagent/blob/main/mcp_server/server.py"
click node_legal_tools "https://github.com/heart-breaker-beep/lexagent/tree/main/mcp_server/tools"
click node_contract_workflow "https://github.com/heart-breaker-beep/lexagent/blob/main/services/contract_agent/workflow.py"
click node_legal_analysis "https://github.com/heart-breaker-beep/lexagent/blob/main/services/legal_analysis.py"
click node_evidence "https://github.com/heart-breaker-beep/lexagent/blob/main/services/evidence_video.py"
click node_retriever "https://github.com/heart-breaker-beep/lexagent/blob/main/services/retriever/hybrid.py"
click node_vectorstore "https://github.com/heart-breaker-beep/lexagent/blob/main/services/vectorstore/base.py"
click node_law_store "https://github.com/heart-breaker-beep/lexagent/blob/main/services/vectorstore/chroma_store.py"
click node_indexer "https://github.com/heart-breaker-beep/lexagent/tree/main/services/indexer"
click node_checkpoints "https://github.com/heart-breaker-beep/lexagent/blob/main/services/checkpoint.py"
click node_memory "https://github.com/heart-breaker-beep/lexagent/blob/main/services/memory.py"
click node_compaction "https://github.com/heart-breaker-beep/lexagent/blob/main/services/context_compaction.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_main,node_chat,node_other_api,node_user toneBlue
class node_graph,node_agent_nodes,node_llm,node_mcp_client toneAmber
class node_mcp_server,node_legal_tools,node_contract_workflow,node_legal_analysis,node_evidence toneMint
class node_retriever,node_vectorstore,node_law_store,node_indexer toneRose
class node_checkpoints,node_memory,node_compaction,node_external_llm toneIndigo
```

## Components

### Application entry

- [FastAPI application](https://github.com/heart-breaker-beep/lexagent/blob/main/main.py): `main.py`
- [Chat endpoint](https://github.com/heart-breaker-beep/lexagent/blob/main/api/chat.py): `api/chat.py`
- [Other API routes](https://github.com/heart-breaker-beep/lexagent/tree/main/api): `api`

### Agent workflow

- [Agent graph](https://github.com/heart-breaker-beep/lexagent/blob/main/agent/graph.py): `agent/graph.py`
- [Agent nodes](https://github.com/heart-breaker-beep/lexagent/blob/main/agent/nodes.py): `agent/nodes.py`
- [LLM gateway](https://github.com/heart-breaker-beep/lexagent/blob/main/services/llm.py): `services/llm.py`
- [MCP client](https://github.com/heart-breaker-beep/lexagent/blob/main/services/mcp_client.py): `services/mcp_client.py`

### Legal capabilities

- [MCP tool server](https://github.com/heart-breaker-beep/lexagent/blob/main/mcp_server/server.py): `mcp_server/server.py`
- [Legal MCP tools](https://github.com/heart-breaker-beep/lexagent/tree/main/mcp_server/tools): `mcp_server/tools`
- [Contract analysis](https://github.com/heart-breaker-beep/lexagent/blob/main/services/contract_agent/workflow.py): `services/contract_agent/workflow.py`
- [Legal analysis](https://github.com/heart-breaker-beep/lexagent/blob/main/services/legal_analysis.py): `services/legal_analysis.py`
- [Evidence handling](https://github.com/heart-breaker-beep/lexagent/blob/main/services/evidence_video.py): `services/evidence_video.py`

### Legal knowledge

- [Hybrid retrieval](https://github.com/heart-breaker-beep/lexagent/blob/main/services/retriever/hybrid.py): `services/retriever/hybrid.py`
- [Vector store interface](https://github.com/heart-breaker-beep/lexagent/blob/main/services/vectorstore/base.py): `services/vectorstore/base.py`
- [Law vector store](https://github.com/heart-breaker-beep/lexagent/blob/main/services/vectorstore/chroma_store.py): `services/vectorstore/chroma_store.py`
- [Law indexing](https://github.com/heart-breaker-beep/lexagent/tree/main/services/indexer): `services/indexer`

### Conversation state

- [Thread checkpoints](https://github.com/heart-breaker-beep/lexagent/blob/main/services/checkpoint.py): `services/checkpoint.py`
- [Conversation memory](https://github.com/heart-breaker-beep/lexagent/blob/main/services/memory.py): `services/memory.py`
- [Context compaction](https://github.com/heart-breaker-beep/lexagent/blob/main/services/context_compaction.py): `services/context_compaction.py`

### Other components

- **User**
- **LLM provider**

## Connections

- User → Chat endpoint: sends message
- FastAPI application → Chat endpoint: registers route (evidence: `main.py`)
- FastAPI application → Other API routes: registers routes (evidence: `main.py`)
- FastAPI application → Agent graph: builds graph (evidence: `main.py`)
- Chat endpoint → Agent graph: streams run
- Agent graph → Agent nodes: dispatches nodes (evidence: `agent/graph.py`)
- Agent nodes → LLM gateway: calls model (evidence: `agent/nodes.py`)
- LLM gateway → LLM provider: sends requests (evidence: `services/llm.py`)
- Agent nodes → Legal analysis: uses analysis (evidence: `agent/nodes.py`)
- Agent nodes → Contract analysis: uses formatter and schema
- Agent nodes → MCP client: uses MCP tools
- MCP client → MCP tool server: starts subprocess (evidence: `services/mcp_client.py`)
- MCP tool server → Legal MCP tools: registers tools (evidence: `mcp_server/server.py`)
- Legal MCP tools → Hybrid retrieval: retrieves laws (evidence: `mcp_server/tools/review.py`)
- Hybrid retrieval → Vector store interface: uses interface (evidence: `services/retriever/__init__.py`)
- Law vector store → Vector store interface: implements interface (evidence: `services/vectorstore/chroma_store.py`)
- Chat endpoint → Thread checkpoints: reads thread state (evidence: `api/chat.py`)
- Chat endpoint → Conversation memory: extracts memory (evidence: `api/chat.py`)
- Chat endpoint → Context compaction: reports compaction (evidence: `api/chat.py`)
- Chat endpoint → Evidence handling: uses evidence service (evidence: `api/chat.py`)
- Law indexing → Law vector store: writes law vectors
