# vivenx/ai-agent-for-leasing architecture

> Architecture diagram of the GitHub repository vivenx/ai-agent-for-leasing, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/vivenx/ai-agent-for-leasing
- Repository: https://github.com/vivenx/ai-agent-for-leasing

## Overview

1. **Top-level shape**
   - The repo is a single Python service split into two layers: a FastAPI HTTP application in `api/` and a reusable domain package in `leasing_analyzer/`.
   - The `api` layer is only the delivery boundary: it serves the UI, exposes REST endpoints, and delegates all business work to the package.

2. **HTTP/UI boundary**
   - `api/main.py` is the main runtime entrypoint.
   - It serves:
     - `GET /` for the local web UI from `api/templates/index.html`
     - static assets from `api/static/`
     - `GET /health`
     - analysis endpoints under `/api/*`
   - The frontend is a lightweight browser client (`script.js`, `style.css`) that calls the backend; there is no separate SPA framework.

3. **Core analysis pipeline**
   - `leasing_analyzer/services/pipeline.py` is the orchestration layer for the main “analyze an item” flow.
   - It wires together search, fetching, parsing, market aggregation, and optional AI assistance.
   - This is the central subsystem to place in the diagram because most requests eventually pass through it.

4. **Search and page acquisition**
   - `leasing_analyzer/services/search.py` builds search queries and collects candidate result URLs, likely via Serper/Google.
   - `leasing_analyzer/services/fetcher.py` retrieves target pages using Selenium, which makes browser availability part of the runtime architecture.
   - This layer depends on external websites and browser automation, so it is the main operational bottleneck and failure point for market analysis.

5. **Parsing and normalization**
   - `leasing_analyzer/parsing/` contains site-specific and generic parsers:
     - `avito.py` for Avito-specific logic
     - `basic.py` and `base.py` for generic parsing contracts
     - `content_cleaner.py` and `helpers.py` for text cleanup and normalization
   - The parser layer converts raw fetched HTML/text into structured offer data that the market layer can aggregate.

6. **Market computation and reporting**
   - `leasing_analyzer/services/market.py` computes the market picture from parsed offers: ranges, medians, comparisons, and deviation from client price.
   - `leasing_analyzer/services/output.py` shapes the final response payload returned by the API.
   - `leasing_analyzer/services/specs.py` appears to hold item/specification extraction or comparison helpers used during report assembly.

7. **AI and external model clients**
   - `leasing_analyzer/clients/` isolates outbound model/API integrations:
     - `gigachat.py` for GigaChat
     - `sonar.py` for Perplexity Sonar
     - `ai_analyzer.py` as a higher-level AI abstraction
   - These clients are optional dependencies used for text interpretation, characteristics extraction, analog comparison, and deep analysis.
   - They are separated from the pipeline so the rest of the system can run with partial AI capability.

8. **Document analysis subsystem**
   - `leasing_analyzer/document/extractors.py` handles file-text extraction for `.txt`, `.docx`, and `.pdf`.
   - `leasing_analyzer/document/service.py` converts uploaded documents into the same downstream analysis flow used by the text API.
   - This creates an important boundary: document ingestion first, then normalization into item name, price, and characteristics, then market analysis.

9. **Session memory and context**
   - `leasing_analyzer/memory/` provides a persistent session-memory feature keyed by `sessionId`.
   - `models.py`, `repository.py`, and `service.py` indicate a simple persistence abstraction, backed by SQLite according to the README.
   - This memory layer is used to retain short conversational context across multiple requests without coupling it to the HTTP layer.

10. **Cross-cutting core utilities**
    - `leasing_analyzer/core/` contains shared infrastructure:
      - `config.py` for environment-driven settings
      - `logging.py` for log setup
      - `rate_limit.py` for throttling external calls
      - `sessions.py` for session handling
      - `models.py`, `utils.py`, and `audit.py` for shared structures and helpers
    - These modules are not domain logic; they support reliability, observability, and request-level controls.

11. **End-to-end request flow**
    - For `POST /api/describe`, the flow is: HTTP request → pipeline → search → fetcher → parsers → market computation → optional AI/Sonar augmentation → response assembly.
    - For `POST /api/analyze-document`, the flow is: file upload → extractor → GigaChat-based text interpretation → same market pipeline → response assembly.
    - Memory can be consulted and updated at the start/end of these flows when `sessionId` is present.

12. **Operational dependencies and boundaries**
    - The service depends on several external systems that materially shape the architecture:
      - Serper/search API for discovery
      - GigaChat for primary AI extraction
      - Perplexity/Sonar for analog reasoning
      - Selenium + Chrome for market-page retrieval
      - SQLite for local memory persistence
    - Because these are external and failure-prone, the architecture is intentionally layered so each integration is isolated behind a client/service boundary.

13. **Tests and validation**
    - The test suite is small and focused, with examples around the GigaChat client and seller deduplication.
    - `verify_env.py` is part of operational validation, checking that the required environment is present before running the service.
    - This suggests the repo is oriented more toward integration-heavy runtime behavior than pure unit-test coverage.

14. **Architecture summary for a diagram**
    - Draw it as: browser UI and API clients on the left, FastAPI entrypoint in the center, orchestration services beneath, and external integrations on the right/bottom.
    - The two main domain flows are “text item analysis” and “document analysis,” both converging on the same market-analysis core.
    - Memory, logging, config, and rate limiting are shared support services spanning both flows.

Main entry points: [`leasing_analyzer/services/pipeline.py`](https://github.com/vivenx/ai-agent-for-leasing/blob/main/leasing_analyzer/services/pipeline.py) (Analysis pipeline), [`api/main.py`](https://github.com/vivenx/ai-agent-for-leasing/blob/main/api/main.py) (FastAPI app), [`leasing_analyzer/document/service.py`](https://github.com/vivenx/ai-agent-for-leasing/blob/main/leasing_analyzer/document/service.py) (Doc service), [`leasing_analyzer/clients/ai_analyzer.py`](https://github.com/vivenx/ai-agent-for-leasing/blob/main/leasing_analyzer/clients/ai_analyzer.py) (AI analyzer)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ui["UI & API"]
  node_api_main["FastAPI app<br/>http entrypoint<br/>[main.py]"]
  node_ui_bundle["Browser UI<br/>static frontend<br/>[index.html]"]
end

subgraph group_domain["Domain Core"]
  node_pipeline["Analysis pipeline<br/>orchestrator<br/>[pipeline.py]"]
  node_search["Search service<br/>discovery<br/>[search.py]"]
  node_fetcher["Page fetcher<br/>browser fetch<br/>[fetcher.py]"]
  node_parsing["Parsers<br/>normalization"]
  node_market["Market engine<br/>aggregation<br/>[market.py]"]
  node_output["Response builder<br/>payload shaping<br/>[output.py]"]
  node_specs["Specs helper<br/>comparison helper<br/>[specs.py]"]
  node_doc_service["Doc service<br/>document flow<br/>[service.py]"]
  node_extractors["File extractors<br/>document parsing<br/>[extractors.py]"]
  node_ai_analyzer["AI analyzer<br/>ai abstraction<br/>[ai_analyzer.py]"]
  node_memory[("Session memory<br/>persistent context<br/>[service.py]")]
  node_core["Core services<br/>shared infra<br/>[config.py]"]
end

subgraph group_external["External Systems"]
  node_gigachat["GigaChat client<br/>model client<br/>[gigachat.py]"]
  node_sonar["Sonar client<br/>model client<br/>[sonar.py]"]
  node_sqlite[("SQLite store<br/>local persistence")]
  node_browser["Selenium/Chrome<br/>runtime dependency"]
  node_search_api["Search API<br/>external discovery"]
end

node_ui_bundle -->|"calls"| node_api_main
node_api_main -->|"routes text analyze"| node_pipeline
node_api_main -->|"routes document analyze"| node_doc_service
node_api_main -->|"session context"| node_memory
node_pipeline -->|"discover"| node_search
node_pipeline -->|"fetch"| node_fetcher
node_pipeline -->|"normalize"| node_parsing
node_pipeline -->|"aggregate"| node_market
node_pipeline -.->|"augment"| node_ai_analyzer
node_pipeline -->|"assemble"| node_output
node_pipeline -->|"compare specs"| node_specs
node_doc_service -->|"extract text"| node_extractors
node_doc_service -.->|"interpret text"| node_gigachat
node_doc_service -->|"handoff"| node_pipeline
node_ai_analyzer -->|"primary model"| node_gigachat
node_ai_analyzer -.->|"fallback reasoning"| node_sonar
node_fetcher -->|"drives"| node_browser
node_search -->|"queries"| node_search_api
node_memory -->|"persists"| node_sqlite
node_api_main -->|"uses"| node_core
node_pipeline -->|"uses"| node_core

click node_api_main "https://github.com/vivenx/ai-agent-for-leasing/blob/main/api/main.py"
click node_ui_bundle "https://github.com/vivenx/ai-agent-for-leasing/blob/main/api/templates/index.html"
click node_pipeline "https://github.com/vivenx/ai-agent-for-leasing/blob/main/leasing_analyzer/services/pipeline.py"
click node_search "https://github.com/vivenx/ai-agent-for-leasing/blob/main/leasing_analyzer/services/search.py"
click node_fetcher "https://github.com/vivenx/ai-agent-for-leasing/blob/main/leasing_analyzer/services/fetcher.py"
click node_parsing "https://github.com/vivenx/ai-agent-for-leasing/tree/main/leasing_analyzer/parsing"
click node_market "https://github.com/vivenx/ai-agent-for-leasing/blob/main/leasing_analyzer/services/market.py"
click node_output "https://github.com/vivenx/ai-agent-for-leasing/blob/main/leasing_analyzer/services/output.py"
click node_specs "https://github.com/vivenx/ai-agent-for-leasing/blob/main/leasing_analyzer/services/specs.py"
click node_doc_service "https://github.com/vivenx/ai-agent-for-leasing/blob/main/leasing_analyzer/document/service.py"
click node_extractors "https://github.com/vivenx/ai-agent-for-leasing/blob/main/leasing_analyzer/document/extractors.py"
click node_ai_analyzer "https://github.com/vivenx/ai-agent-for-leasing/blob/main/leasing_analyzer/clients/ai_analyzer.py"
click node_gigachat "https://github.com/vivenx/ai-agent-for-leasing/blob/main/leasing_analyzer/clients/gigachat.py"
click node_sonar "https://github.com/vivenx/ai-agent-for-leasing/blob/main/leasing_analyzer/clients/sonar.py"
click node_memory "https://github.com/vivenx/ai-agent-for-leasing/blob/main/leasing_analyzer/memory/service.py"
click node_core "https://github.com/vivenx/ai-agent-for-leasing/blob/main/leasing_analyzer/core/config.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_api_main,node_ui_bundle toneBlue
class node_pipeline,node_search,node_fetcher,node_parsing,node_market,node_output,node_specs,node_doc_service,node_extractors,node_ai_analyzer,node_memory,node_core toneAmber
class node_gigachat,node_sonar,node_sqlite,node_browser,node_search_api toneMint
```

## Components

### UI & API

HTTP entrypoint, browser UI, and request handlers

- [FastAPI app](https://github.com/vivenx/ai-agent-for-leasing/blob/main/api/main.py): http entrypoint · Main runtime entrypoint serving UI, health, and analysis endpoints · `api/main.py`
- [Browser UI](https://github.com/vivenx/ai-agent-for-leasing/blob/main/api/templates/index.html): static frontend · Lightweight browser client and static assets · `api/templates/index.html`

### Domain Core

Reusable analysis, parsing, memory, and support services

- [Analysis pipeline](https://github.com/vivenx/ai-agent-for-leasing/blob/main/leasing_analyzer/services/pipeline.py): orchestrator · Coordinates search, fetch, parsing, market analysis, and AI augmentation · `leasing_analyzer/services/pipeline.py`
- [Search service](https://github.com/vivenx/ai-agent-for-leasing/blob/main/leasing_analyzer/services/search.py): discovery · Builds queries and collects candidate result URLs · `leasing_analyzer/services/search.py`
- [Page fetcher](https://github.com/vivenx/ai-agent-for-leasing/blob/main/leasing_analyzer/services/fetcher.py): browser fetch · Retrieves target pages through Selenium-driven browsing · `leasing_analyzer/services/fetcher.py`
- [Parsers](https://github.com/vivenx/ai-agent-for-leasing/tree/main/leasing_analyzer/parsing): normalization · Site-specific and generic parsers that turn raw pages into structured offers · `leasing_analyzer/parsing`
- [Market engine](https://github.com/vivenx/ai-agent-for-leasing/blob/main/leasing_analyzer/services/market.py): aggregation · Computes ranges, medians, comparisons, and deviation from client price · `leasing_analyzer/services/market.py`
- [Response builder](https://github.com/vivenx/ai-agent-for-leasing/blob/main/leasing_analyzer/services/output.py): payload shaping · Assembles the final API response payload · `leasing_analyzer/services/output.py`
- [Specs helper](https://github.com/vivenx/ai-agent-for-leasing/blob/main/leasing_analyzer/services/specs.py): comparison helper · Item/spec extraction and comparison helpers used during report assembly · `leasing_analyzer/services/specs.py`
- [Doc service](https://github.com/vivenx/ai-agent-for-leasing/blob/main/leasing_analyzer/document/service.py): document flow · Converts uploaded documents into the downstream analysis flow · `leasing_analyzer/document/service.py`
- [File extractors](https://github.com/vivenx/ai-agent-for-leasing/blob/main/leasing_analyzer/document/extractors.py): document parsing · Extracts text from txt, docx, and pdf uploads · `leasing_analyzer/document/extractors.py`
- [AI analyzer](https://github.com/vivenx/ai-agent-for-leasing/blob/main/leasing_analyzer/clients/ai_analyzer.py): ai abstraction · Higher-level AI layer for text interpretation and extraction · `leasing_analyzer/clients/ai_analyzer.py`
- [Session memory](https://github.com/vivenx/ai-agent-for-leasing/blob/main/leasing_analyzer/memory/service.py): persistent context · Stores short conversational context keyed by sessionId · `leasing_analyzer/memory/service.py`
- [Core services](https://github.com/vivenx/ai-agent-for-leasing/blob/main/leasing_analyzer/core/config.py): shared infra · Config, logging, rate limiting, sessions, audit, and shared utilities · `leasing_analyzer/core/config.py`

### External Systems

Third-party services and runtime dependencies

- [GigaChat client](https://github.com/vivenx/ai-agent-for-leasing/blob/main/leasing_analyzer/clients/gigachat.py): model client · Primary outbound client for AI-assisted extraction · `leasing_analyzer/clients/gigachat.py`
- [Sonar client](https://github.com/vivenx/ai-agent-for-leasing/blob/main/leasing_analyzer/clients/sonar.py): model client · Perplexity Sonar integration for analog reasoning · `leasing_analyzer/clients/sonar.py`
- **SQLite store**: local persistence · Local backing store for session memory
- **Selenium/Chrome**: runtime dependency · Browser automation runtime used for page retrieval
- **Search API**: external discovery · External search provider used to find candidate pages

## Connections

- Browser UI → FastAPI app: calls · Browser client sends requests to the FastAPI backend
- FastAPI app → Analysis pipeline: routes text analyze · HTTP analysis endpoints delegate to the main pipeline
- FastAPI app → Doc service: routes document analyze · Uploaded documents are handed to the document flow
- FastAPI app → Session memory: session context · Optional session state is loaded and updated around requests
- Analysis pipeline → Search service: discover · Finds candidate listing URLs and queries
- Analysis pipeline → Page fetcher: fetch · Retrieves candidate pages for analysis
- Analysis pipeline → Parsers: normalize · Parses raw content into structured offers
- Analysis pipeline → Market engine: aggregate · Computes market statistics and comparisons
- Analysis pipeline → AI analyzer: augment · Optional AI-assisted interpretation and extraction
- Analysis pipeline → Response builder: assemble · Builds the final API payload
- Analysis pipeline → Specs helper: compare specs · Uses item specification helpers during report assembly
- Doc service → File extractors: extract text · Reads text from uploaded files before analysis
- Doc service → GigaChat client: interpret text · Uses GigaChat for document understanding and extraction
- Doc service → Analysis pipeline: handoff · Converges back into the shared analysis pipeline
- AI analyzer → GigaChat client: primary model · Wraps the GigaChat client for AI operations
- AI analyzer → Sonar client: fallback reasoning · Uses Sonar for additional model-based reasoning
- Page fetcher → Selenium/Chrome: drives · Requires Selenium/Chrome to retrieve pages
- Search service → Search API: queries · Search discovery relies on an external search provider
- Session memory → SQLite store: persists · Session state is stored locally in SQLite
- FastAPI app → Core services: uses · HTTP layer relies on shared config, logging, rate limits, and session helpers
- Analysis pipeline → Core services: uses · Core services support orchestration and external-call controls
