# huntisonn/datapilot architecture

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

- Interactive diagram: https://gitdiagram.com/huntisonn/datapilot
- Repository: https://github.com/huntisonn/datapilot

## Overview

#### Purpose and Entry Points
DataPilot is a natural-language analytics system: users connect data, ask questions, and receive validated, PII-redacted narrative and chart responses. The backend FastAPI entry point is `datapilot/api/main.py`; primary HTTP surfaces are dataset connection/upload and query execution in `datapilot/api/routes/dataset_routes.py` and `datapilot/api/routes/query_routes.py`. The React/Vite client starts at `frontend/src/main.tsx` and composes the product in `frontend/src/App.tsx`.

#### API, Session, and Dataset Boundary
The API is the boundary between browser requests and the agent/data pipeline. Dataset routes establish access to CSV, SQL, or NoSQL sources; query routes submit analytical questions. Request throttling is centralized in `datapilot/api/middleware/rate_limiter.py`. Conversation-scoped context is retained separately from data access in `datapilot/foundation/memory/conversation_memory.py`, enabling follow-up questions without exposing raw database mechanics to the UI.

#### Data Connectivity and Execution
A connector abstraction selects source-specific implementations through `datapilot/foundation/connectors/connector_factory.py`, with SQL/PostgreSQL, MongoDB, and CSV/DuckDB paths in `datapilot/foundation/connectors/sql_connector.py`, `datapilot/foundation/connectors/nosql_connector.py`, and `datapilot/foundation/connectors/csv_connector.py`. This is a read-oriented execution boundary: agents generate SQL or Mongo aggregation, while connectors execute against the configured source and return results.

#### Semantic Layer and Reuse
Before query generation, business language is mapped to physical schemas and derived metrics. The semantic model, metric registry, and automatic profiling live in `datapilot/foundation/semantic_layer/models.py`, `datapilot/foundation/semantic_layer/metric_registry.py`, and `datapilot/foundation/semantic_layer/auto_profiler.py`. Local embedding/synonym matching supports semantic resolution; caches reduce repeated semantic and query work via `datapilot/foundation/cache/embedding_cache.py` and `datapilot/foundation/cache/query_cache.py`.

#### Agent Pipeline and Orchestration
CrewAI-based orchestration is centered on `datapilot/crews/orchestrator.py` and `datapilot/crews/crew_factory.py`. Fast execution uses the compact phase pipeline in `datapilot/crews/phase1_crew.py`; deep analysis uses the broader financial crew in `datapilot/crews/financial_crew.py`. Core stages are routing, schema resolution, query building, optional change/comparison/breakdown/summary analysis, validation, privacy filtering, and narration. Agent responsibilities are concretely defined under `datapilot/agents/definitions/router_agent.py`, `datapilot/agents/definitions/query_builder_agent.py`, and `datapilot/agents/definitions/validator_agent.py`.

#### Trust and Presentation Boundary
Agents invoke bounded capabilities rather than directly owning infrastructure: semantic lookup, SQL execution, validation/statistics, PII detection, and visualization are in `datapilot/agents/tools/semantic_tools.py`, `datapilot/agents/tools/sql_tools.py`, and `datapilot/agents/tools/privacy_tools.py`. Validation precedes final redaction and narration; response contracts and formatting are produced through `datapilot/output/response_models.py` and `datapilot/output/formatters.py`. The UI renders conversational responses, source transparency, and charts through `frontend/src/components/chat/ChatInterface.tsx`, `frontend/src/components/chat/SourceTransparency.tsx`, and `frontend/src/components/chat/InsightTemplates.tsx`.

#### Runtime and External Services
Backend runtime is Python 3.11/FastAPI/Uvicorn, packaged by `datapilot/Dockerfile`; frontend is React 19, TypeScript, and Vite. External dependencies include OpenAI GPT-4o/GPT-4o-mini for agent reasoning and narration, PostgreSQL via Psycopg2, MongoDB via PyMongo, and local DuckDB/Pandas for uploaded files. Sentence Transformers provides local embeddings, limiting semantic matching’s dependence on external APIs.

Main entry points: [`datapilot/api/routes/query_routes.py`](https://github.com/huntisonn/datapilot/blob/main/datapilot/api/routes/query_routes.py) (Query execution), [`datapilot/agents/definitions/router_agent.py`](https://github.com/huntisonn/datapilot/blob/main/datapilot/agents/definitions/router_agent.py) (Routing, build, validate), [`datapilot/api/middleware/rate_limiter.py`](https://github.com/huntisonn/datapilot/blob/main/datapilot/api/middleware/rate_limiter.py) (Request throttling), [`datapilot/crews/crew_factory.py`](https://github.com/huntisonn/datapilot/blob/main/datapilot/crews/crew_factory.py) (Crew selection)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["React client"]
  node_web_app["Product shell<br/>React/Vite<br/>[App.tsx]"]
  node_chat_ui["Chat and insights<br/>UI components<br/>[ChatInterface.tsx]"]
end

subgraph group_api["FastAPI boundary"]
  node_api_main["API runtime<br/>FastAPI<br/>[main.py]"]
  node_rate_limiter["Request throttling<br/>middleware<br/>[rate_limiter.py]"]
  node_dataset_api["Dataset access<br/>HTTP routes<br/>[dataset_routes.py]"]
  node_query_api["Query execution<br/>HTTP routes<br/>[query_routes.py]"]
  node_conversation_memory[("Conversation context<br/>session memory")]
end

subgraph group_intelligence["Analytics pipeline"]
  node_orchestrator{{"Crew orchestrator<br/>CrewAI orchestration<br/>[orchestrator.py]"}}
  node_crew_factory["Crew selection<br/>crew factory<br/>[crew_factory.py]"]
  node_phase_crew["Fast analysis crew<br/>phase pipeline<br/>[phase1_crew.py]"]
  node_financial_crew["Deep analysis crew<br/>financial pipeline<br/>[financial_crew.py]"]
  node_agent_roles["Routing, build, validate<br/>agent definitions<br/>[router_agent.py]"]
  node_semantic_layer[("Semantic model and metrics<br/>semantic layer<br/>[models.py]")]
  node_semantic_cache["Semantic and query cache<br/>local cache<br/>[embedding_cache.py]"]
  node_bounded_tools["Bounded agent tools<br/>tool boundary<br/>[semantic_tools.py]"]
  node_response_output["Validated response<br/>output contracts<br/>[response_models.py]"]
end

subgraph group_data["Data and services"]
  node_connector_factory["Connector selection<br/>connector factory"]
  node_connected_sources[("Connected data sources<br/>PostgreSQL, MongoDB, CSV<br/>[sql_connector.py]")]
  node_llm_service{{"OpenAI reasoning<br/>external LLM"}}
end

node_web_app -->|"composes"| node_chat_ui
node_chat_ui -->|"HTTP requests"| node_api_main
node_api_main -->|"applies"| node_rate_limiter
node_rate_limiter -->|"dataset requests"| node_dataset_api
node_rate_limiter -->|"question requests"| node_query_api
node_dataset_api -->|"connects source"| node_connector_factory
node_query_api -->|"loads and stores context"| node_conversation_memory
node_query_api -->|"submits question"| node_orchestrator
node_orchestrator -->|"selects execution mode"| node_crew_factory
node_crew_factory -->|"fast path"| node_phase_crew
node_crew_factory -->|"deep path"| node_financial_crew
node_phase_crew -->|"runs stages"| node_agent_roles
node_financial_crew -->|"runs stages"| node_agent_roles
node_agent_roles -->|"resolves business terms"| node_semantic_layer
node_semantic_layer -->|"reuses embeddings and queries"| node_semantic_cache
node_agent_roles -->|"invokes capabilities"| node_bounded_tools
node_bounded_tools -->|"executes generated query"| node_connector_factory
node_connector_factory -->|"reads"| node_connected_sources
node_agent_roles -->|"reasoning and narration"| node_llm_service
node_bounded_tools -->|"validate then redact"| node_response_output
node_response_output -->|"returns response"| node_query_api
node_query_api -->|"answer, sources, charts"| node_chat_ui

click node_web_app "https://github.com/huntisonn/datapilot/blob/main/frontend/src/App.tsx"
click node_chat_ui "https://github.com/huntisonn/datapilot/blob/main/frontend/src/components/chat/ChatInterface.tsx"
click node_api_main "https://github.com/huntisonn/datapilot/blob/main/datapilot/api/main.py"
click node_rate_limiter "https://github.com/huntisonn/datapilot/blob/main/datapilot/api/middleware/rate_limiter.py"
click node_dataset_api "https://github.com/huntisonn/datapilot/blob/main/datapilot/api/routes/dataset_routes.py"
click node_query_api "https://github.com/huntisonn/datapilot/blob/main/datapilot/api/routes/query_routes.py"
click node_conversation_memory "https://github.com/huntisonn/datapilot/blob/main/datapilot/foundation/memory/conversation_memory.py"
click node_orchestrator "https://github.com/huntisonn/datapilot/blob/main/datapilot/crews/orchestrator.py"
click node_crew_factory "https://github.com/huntisonn/datapilot/blob/main/datapilot/crews/crew_factory.py"
click node_phase_crew "https://github.com/huntisonn/datapilot/blob/main/datapilot/crews/phase1_crew.py"
click node_financial_crew "https://github.com/huntisonn/datapilot/blob/main/datapilot/crews/financial_crew.py"
click node_agent_roles "https://github.com/huntisonn/datapilot/blob/main/datapilot/agents/definitions/router_agent.py"
click node_semantic_layer "https://github.com/huntisonn/datapilot/blob/main/datapilot/foundation/semantic_layer/models.py"
click node_semantic_cache "https://github.com/huntisonn/datapilot/blob/main/datapilot/foundation/cache/embedding_cache.py"
click node_bounded_tools "https://github.com/huntisonn/datapilot/blob/main/datapilot/agents/tools/semantic_tools.py"
click node_response_output "https://github.com/huntisonn/datapilot/blob/main/datapilot/output/response_models.py"
click node_connector_factory "https://github.com/huntisonn/datapilot/blob/main/datapilot/foundation/connectors/connector_factory.py"
click node_connected_sources "https://github.com/huntisonn/datapilot/blob/main/datapilot/foundation/connectors/sql_connector.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_web_app,node_chat_ui toneBlue
class node_api_main,node_rate_limiter,node_dataset_api,node_query_api,node_conversation_memory toneAmber
class node_orchestrator,node_crew_factory,node_phase_crew,node_financial_crew,node_agent_roles,node_semantic_layer,node_semantic_cache,node_bounded_tools,node_response_output toneMint
class node_connector_factory,node_connected_sources,node_llm_service toneRose
```

## Components

### React client

- [Product shell](https://github.com/huntisonn/datapilot/blob/main/frontend/src/App.tsx): React/Vite · `frontend/src/App.tsx`
- [Chat and insights](https://github.com/huntisonn/datapilot/blob/main/frontend/src/components/chat/ChatInterface.tsx): UI components · Renders answers, sources, and charts. · `frontend/src/components/chat/ChatInterface.tsx`

### FastAPI boundary

- [API runtime](https://github.com/huntisonn/datapilot/blob/main/datapilot/api/main.py): FastAPI · `datapilot/api/main.py`
- [Request throttling](https://github.com/huntisonn/datapilot/blob/main/datapilot/api/middleware/rate_limiter.py): middleware · `datapilot/api/middleware/rate_limiter.py`
- [Dataset access](https://github.com/huntisonn/datapilot/blob/main/datapilot/api/routes/dataset_routes.py): HTTP routes · Connects uploaded and configured data sources. · `datapilot/api/routes/dataset_routes.py`
- [Query execution](https://github.com/huntisonn/datapilot/blob/main/datapilot/api/routes/query_routes.py): HTTP routes · `datapilot/api/routes/query_routes.py`
- [Conversation context](https://github.com/huntisonn/datapilot/blob/main/datapilot/foundation/memory/conversation_memory.py): session memory · `datapilot/foundation/memory/conversation_memory.py`

### Analytics pipeline

- [Crew orchestrator](https://github.com/huntisonn/datapilot/blob/main/datapilot/crews/orchestrator.py): CrewAI orchestration · Selects fast or deep analytical execution. · `datapilot/crews/orchestrator.py`
- [Crew selection](https://github.com/huntisonn/datapilot/blob/main/datapilot/crews/crew_factory.py): crew factory · `datapilot/crews/crew_factory.py`
- [Fast analysis crew](https://github.com/huntisonn/datapilot/blob/main/datapilot/crews/phase1_crew.py): phase pipeline · Runs compact routing-to-narration flow. · `datapilot/crews/phase1_crew.py`
- [Deep analysis crew](https://github.com/huntisonn/datapilot/blob/main/datapilot/crews/financial_crew.py): financial pipeline · Supports broader comparison and breakdown analysis. · `datapilot/crews/financial_crew.py`
- [Routing, build, validate](https://github.com/huntisonn/datapilot/blob/main/datapilot/agents/definitions/router_agent.py): agent definitions · Routes questions, builds queries, and validates results. · `datapilot/agents/definitions/router_agent.py`
- [Semantic model and metrics](https://github.com/huntisonn/datapilot/blob/main/datapilot/foundation/semantic_layer/models.py): semantic layer · Maps business language to schemas and derived metrics. · `datapilot/foundation/semantic_layer/models.py`
- [Semantic and query cache](https://github.com/huntisonn/datapilot/blob/main/datapilot/foundation/cache/embedding_cache.py): local cache · `datapilot/foundation/cache/embedding_cache.py`
- [Bounded agent tools](https://github.com/huntisonn/datapilot/blob/main/datapilot/agents/tools/semantic_tools.py): tool boundary · Provides semantic lookup, execution, validation, and PII detection. · `datapilot/agents/tools/semantic_tools.py`
- [Validated response](https://github.com/huntisonn/datapilot/blob/main/datapilot/output/response_models.py): output contracts · Formats redacted narratives and chart responses. · `datapilot/output/response_models.py`

### Data and services

- [Connector selection](https://github.com/huntisonn/datapilot/blob/main/datapilot/foundation/connectors/connector_factory.py): connector factory · Chooses SQL, MongoDB, or CSV execution path. · `datapilot/foundation/connectors/connector_factory.py`
- [Connected data sources](https://github.com/huntisonn/datapilot/blob/main/datapilot/foundation/connectors/sql_connector.py): PostgreSQL, MongoDB, CSV · Read-oriented source systems and uploaded-file execution. · `datapilot/foundation/connectors/sql_connector.py`
- **OpenAI reasoning**: external LLM · GPT-4o and GPT-4o-mini reasoning and narration.

## Connections

- Product shell → Chat and insights: composes
- Chat and insights → API runtime: HTTP requests
- API runtime → Request throttling: applies
- Request throttling → Dataset access: dataset requests
- Request throttling → Query execution: question requests
- Dataset access → Connector selection: connects source
- Query execution → Conversation context: loads and stores context
- Query execution → Crew orchestrator: submits question
- Crew orchestrator → Crew selection: selects execution mode
- Crew selection → Fast analysis crew: fast path
- Crew selection → Deep analysis crew: deep path
- Fast analysis crew → Routing, build, validate: runs stages
- Deep analysis crew → Routing, build, validate: runs stages
- Routing, build, validate → Semantic model and metrics: resolves business terms
- Semantic model and metrics → Semantic and query cache: reuses embeddings and queries
- Routing, build, validate → Bounded agent tools: invokes capabilities
- Bounded agent tools → Connector selection: executes generated query
- Connector selection → Connected data sources: reads
- Routing, build, validate → OpenAI reasoning: reasoning and narration
- Bounded agent tools → Validated response: validate then redact
- Validated response → Query execution: returns response
- Query execution → Chat and insights: answer, sources, charts
