# cgmafia/ai-research-agent architecture

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

- Interactive diagram: https://gitdiagram.com/cgmafia/ai-research-agent
- Repository: https://github.com/cgmafia/ai-research-agent

## Overview

#### Purpose and Topology
This is a local Python research/lead-enrichment application. It accepts company website URLs, researches public website and LinkedIn information, and produces structured company/contact results. The system is split into a REST backend and a separate Streamlit user interface.

Paths: `backend/main.py`, `frontend/app.py`, `data/input.csv`.

#### Runtime Entry Points
- Start the API with Uvicorn targeting the FastAPI application in `backend/main.py`.
- Start the UI with Streamlit targeting `frontend/app.py`.
- The frontend is expected to act as the interactive client for backend research operations; the backend is also intended for programmatic API consumers.

Paths: `backend/main.py`, `frontend/app.py`.

#### Input and Job Boundary
The primary batch input is a CSV containing a `website` column. `data/input.csv` is the repository-provided input location/sample. Each website is an independent research target, allowing future parallelization or scheduling without changing the conceptual job model.

Paths: `data/input.csv`, `backend/main.py`.

#### Research and Extraction Pipeline
The backend is the orchestration boundary for the stated workflow:
1. Receive one or more website targets.
2. Crawl internal website pages.
3. Parse page content to identify public emails and phone numbers.
4. Discover/scrape relevant public LinkedIn profiles.
5. Enrich company attributes such as industry, size, and revenue.
6. Return or export normalized company-contact records.

The implementation is concentrated in `backend/main.py`; no separate crawler, persistence, or domain-module directories are present.

#### UI and Result Delivery
The Streamlit layer supplies the browser-facing workflow: submit/upload target data, invoke research, present structured results, and support CSV download/export. It should remain a consumer of API contracts rather than duplicate crawling or enrichment logic.

Paths: `frontend/app.py`, `backend/main.py`, `data/input.csv`.

#### External Services and Material Dependencies
The architecture depends on:
- Target company websites as crawl sources.
- Public LinkedIn-accessible data for profile discovery.
- Hunter.io for company enrichment.
- Google search capability for discovery, per declared dependencies.
- Playwright with Chromium for browser-based scraping where static HTTP parsing is insufficient.

FastAPI/Uvicorn provide API serving; Streamlit provides the local UI. Parsing/data processing dependencies include Requests, BeautifulSoup, lxml, Pandas, aiohttp, and tldextract. Dependencies are declared in `requirements.txt`.

Paths: `requirements.txt`, `backend/main.py`, `frontend/app.py`.

#### Operational Constraints
This is a local, two-process deployment with no container, database, queue, scheduler, or deployment infrastructure represented in the repository. Results are therefore expected to be request/batch-scoped or file-exported rather than durably persisted. Website anti-scraping behavior, robots policies, privacy law, LinkedIn terms, and Hunter.io credentials/rate limits are key external control boundaries.

Paths: `backend/main.py`, `frontend/app.py`, `requirements.txt`.

Main entry points: [`backend/main.py`](https://github.com/cgmafia/ai-research-agent/blob/master/backend/main.py) (FastAPI application), [`frontend/app.py`](https://github.com/cgmafia/ai-research-agent/blob/master/frontend/app.py) (Streamlit UI), [`data/input.csv`](https://github.com/cgmafia/ai-research-agent/blob/master/data/input.csv) (Website input CSV), [`requirements.txt`](https://github.com/cgmafia/ai-research-agent/blob/master/requirements.txt) (Dependency manifest)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_local["Local processes"]
  node_input_csv["Website input CSV<br/>batch input<br/>[input.csv]"]
  node_frontend["Streamlit UI<br/>interactive client<br/>[app.py]"]
  node_dependencies["Dependency manifest<br/>Python dependencies<br/>[requirements.txt]"]
end

subgraph group_backend["Research API pipeline"]
  node_api["FastAPI application<br/>REST API<br/>[main.py]"]
  node_job_targets["Research targets<br/>job model<br/>[main.py]"]
  node_website_crawler["Internal page crawler<br/>crawl stage<br/>[main.py]"]
  node_contact_extractor["Contact extractor<br/>parse stage<br/>[main.py]"]
  node_linkedin_discovery["LinkedIn discovery<br/>profile stage<br/>[main.py]"]
  node_company_enrichment["Company enrichment<br/>enrichment stage<br/>[main.py]"]
  node_normalized_results["Company-contact records<br/>result model<br/>[main.py]"]
end

subgraph group_external["External sources and services"]
  node_target_websites{{"Company websites<br/>crawl source"}}
  node_linkedin{{"Public LinkedIn data<br/>profile source"}}
  node_hunter{{"Hunter.io<br/>enrichment API"}}
  node_google_search{{"Google search<br/>discovery service"}}
  node_playwright["Playwright + Chromium<br/>browser scraper<br/>[requirements.txt]"]
end

node_input_csv -->|"upload/select"| node_frontend
node_input_csv -->|"batch targets"| node_api
node_frontend -->|"research requests"| node_api
node_api -->|"creates"| node_job_targets
node_job_targets -->|"website URLs"| node_website_crawler
node_website_crawler -->|"crawl pages"| node_target_websites
node_website_crawler -.->|"dynamic pages"| node_playwright
node_website_crawler -->|"page content"| node_contact_extractor
node_job_targets -->|"company context"| node_linkedin_discovery
node_linkedin_discovery -->|"discover profiles"| node_linkedin
node_linkedin_discovery -->|"search discovery"| node_google_search
node_job_targets -->|"company identity"| node_company_enrichment
node_company_enrichment -->|"lookup"| node_hunter
node_contact_extractor -->|"contacts"| node_normalized_results
node_linkedin_discovery -->|"profiles"| node_normalized_results
node_company_enrichment -->|"company attributes"| node_normalized_results
node_normalized_results -->|"response payload"| node_api
node_api -->|"structured results"| node_frontend
node_dependencies -.->|"runtime libraries"| node_api
node_dependencies -.->|"UI runtime"| node_frontend

click node_input_csv "https://github.com/cgmafia/ai-research-agent/blob/master/data/input.csv"
click node_frontend "https://github.com/cgmafia/ai-research-agent/blob/master/frontend/app.py"
click node_api "https://github.com/cgmafia/ai-research-agent/blob/master/backend/main.py"
click node_job_targets "https://github.com/cgmafia/ai-research-agent/blob/master/backend/main.py"
click node_website_crawler "https://github.com/cgmafia/ai-research-agent/blob/master/backend/main.py"
click node_contact_extractor "https://github.com/cgmafia/ai-research-agent/blob/master/backend/main.py"
click node_linkedin_discovery "https://github.com/cgmafia/ai-research-agent/blob/master/backend/main.py"
click node_company_enrichment "https://github.com/cgmafia/ai-research-agent/blob/master/backend/main.py"
click node_normalized_results "https://github.com/cgmafia/ai-research-agent/blob/master/backend/main.py"
click node_playwright "https://github.com/cgmafia/ai-research-agent/blob/master/requirements.txt"
click node_dependencies "https://github.com/cgmafia/ai-research-agent/blob/master/requirements.txt"

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_input_csv,node_frontend,node_dependencies toneBlue
class node_api,node_job_targets,node_website_crawler,node_contact_extractor,node_linkedin_discovery,node_company_enrichment,node_normalized_results toneAmber
class node_target_websites,node_linkedin,node_hunter,node_google_search,node_playwright toneMint
```

## Components

### Local processes

Run separately on a developer machine.

- [Website input CSV](https://github.com/cgmafia/ai-research-agent/blob/master/data/input.csv): batch input · Contains the website column for independent research targets. · `data/input.csv`
- [Streamlit UI](https://github.com/cgmafia/ai-research-agent/blob/master/frontend/app.py): interactive client · Submits targets, displays results, and exports CSV. · `frontend/app.py`
- [Dependency manifest](https://github.com/cgmafia/ai-research-agent/blob/master/requirements.txt): Python dependencies · Declares FastAPI, Streamlit, parsing, HTTP, and browser tooling. · `requirements.txt`

### Research API pipeline

Workflow implementation is concentrated in backend/main.py.

- [FastAPI application](https://github.com/cgmafia/ai-research-agent/blob/master/backend/main.py): REST API · Uvicorn-served boundary for UI and programmatic consumers. · `backend/main.py`
- [Research targets](https://github.com/cgmafia/ai-research-agent/blob/master/backend/main.py): job model · One or more website URLs per request or batch. · `backend/main.py`
- [Internal page crawler](https://github.com/cgmafia/ai-research-agent/blob/master/backend/main.py): crawl stage · Collects relevant pages from each target website. · `backend/main.py`
- [Contact extractor](https://github.com/cgmafia/ai-research-agent/blob/master/backend/main.py): parse stage · Finds public emails and phone numbers in page content. · `backend/main.py`
- [LinkedIn discovery](https://github.com/cgmafia/ai-research-agent/blob/master/backend/main.py): profile stage · Finds and scrapes relevant public profiles. · `backend/main.py`
- [Company enrichment](https://github.com/cgmafia/ai-research-agent/blob/master/backend/main.py): enrichment stage · Adds industry, size, revenue, and related attributes. · `backend/main.py`
- [Company-contact records](https://github.com/cgmafia/ai-research-agent/blob/master/backend/main.py): result model · Normalized request-scoped research output. · `backend/main.py`

### External sources and services

Public-data access and third-party limits are external boundaries.

- **Company websites**: crawl source
- **Public LinkedIn data**: profile source · Subject to platform access constraints.
- **Hunter.io**: enrichment API · Credentials and rate limits govern access.
- **Google search**: discovery service
- [Playwright + Chromium](https://github.com/cgmafia/ai-research-agent/blob/master/requirements.txt): browser scraper · Used where static HTTP parsing is insufficient. · `requirements.txt`

## Connections

- Website input CSV → Streamlit UI: upload/select
- Website input CSV → FastAPI application: batch targets
- Streamlit UI → FastAPI application: research requests
- FastAPI application → Research targets: creates
- Research targets → Internal page crawler: website URLs
- Internal page crawler → Company websites: crawl pages
- Internal page crawler → Playwright + Chromium: dynamic pages
- Internal page crawler → Contact extractor: page content
- Research targets → LinkedIn discovery: company context
- LinkedIn discovery → Public LinkedIn data: discover profiles
- LinkedIn discovery → Google search: search discovery
- Research targets → Company enrichment: company identity
- Company enrichment → Hunter.io: lookup
- Contact extractor → Company-contact records: contacts
- LinkedIn discovery → Company-contact records: profiles
- Company enrichment → Company-contact records: company attributes
- Company-contact records → FastAPI application: response payload
- FastAPI application → Streamlit UI: structured results
- Dependency manifest → FastAPI application: runtime libraries
- Dependency manifest → Streamlit UI: UI runtime
