# sheily899/querypilot-data-agent architecture

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

- Interactive diagram: https://gitdiagram.com/sheily899/querypilot-data-agent
- Repository: https://github.com/sheily899/querypilot-data-agent

## Overview

#### Purpose and entry points
QueryPilot is a local natural-language-to-SQL agent for a single structured database. It turns business questions and follow-ups into guarded, read-only SQL, executes it, and presents answers in a Vue workbench. Backend startup is `backend/run.py`, which serves the application assembled in `backend/app/main.py`; the browser client starts at `frontend/src/main.ts` and mounts `frontend/src/App.vue`.

#### API and application boundary
HTTP routes in `backend/app/api/routes.py` expose the query/session-facing backend contract. `backend/app/main.py` initializes the API application, while `backend/app/config.py` and `backend/app/database.py` provide environment-driven runtime configuration and database integration. The frontend calls this contract through `frontend/src/api.ts`, rendering typed responses defined in `frontend/src/types.ts`.

#### Query orchestration workflow
The core request pipeline is a stateful workflow:
- `backend/app/services/askdata_service.py` coordinates a user request.
- `backend/app/workflows/query_graph.py` defines the staged query graph.
- `backend/app/workflows/state.py` carries request, retrieval, SQL, execution, and response state across stages.
- `backend/app/workflows/result_builder.py` shapes the final client result.

This separates transport/API concerns from agent control flow and result presentation.

#### Retrieval and schema grounding
Before SQL generation, the agent retrieves relevant schema fields and relationship context. `backend/app/retrieval/service.py` orchestrates keyword/semantic retrieval, rank fusion, and reranking; `backend/app/retrieval/store.py` holds retrieval data; `backend/app/retrieval/graph.py` builds schema relationship graphs. Retrieved evidence is a boundary between broad database metadata and the constrained context supplied to generation.

#### Model-driven SQL agent and tools
`backend/app/querying/single_database_agent.py` implements the single-database agent loop, with query-domain types in `backend/app/querying/models.py`. It uses `backend/app/model_client.py` to call the configured external model endpoint and `backend/app/querying/response_generator.py` to form user-facing explanations. Local Model Context Protocol-style tool infrastructure resides in `backend/app/mcp_runtime/client.py`, `backend/app/mcp_runtime/server.py`, and tools such as `backend/app/mcp_runtime/tools/database_tools.py`; this is the controlled mechanism for model-initiated database/time operations.

#### Execution and security controls
`backend/app/querying/duckdb_engine.py` is the concrete local query execution layer. Security modules—`backend/app/security/access_control.py` and `backend/app/security/auth.py`—provide authorization-related boundaries, while the query path enforces the product’s read-only, single-statement, permitted-table, and dangerous-operation constraints before execution. The included demo-data path is represented by `backend/app/demo_data.py` and `backend/seed_demo_data.py`.

#### Conversation context and UI
Multi-turn behavior is managed independently of query planning: `backend/app/services/session_context.py` builds usable context, `backend/app/services/short_term_memory.py` maintains recent turns, and `backend/app/services/session_archive.py` persists/archive-oriented history. The Vue workbench in `frontend/src/App.vue` displays conversation and SQL/results; `frontend/src/components/ResultTableCard.vue` handles tabular result presentation/export-oriented UI. Runtime infrastructure is Python 3.11 with a model service configured via backend environment variables, plus a Vite/Vue frontend.

Main entry points: [`backend/app/workflows/query_graph.py`](https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/workflows/query_graph.py) (Staged query graph), [`backend/app/querying/single_database_agent.py`](https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/querying/single_database_agent.py) (Single-database SQL agent), [`backend/app/services/askdata_service.py`](https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/services/askdata_service.py) (Request coordinator), [`backend/app/retrieval/service.py`](https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/retrieval/service.py) (Evidence retrieval)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Vue workbench"]
  node_browser["Vue application<br/>Vue client<br/>[App.vue]"]
  node_frontend_api["API client<br/>TypeScript API<br/>[api.ts]"]
  node_result_table["Results table<br/>Vue component"]
end

subgraph group_api["Application API"]
  node_backend_entry["Backend launcher<br/>Python entry point<br/>[run.py]"]
  node_application["Application assembly<br/>Python app<br/>[main.py]"]
  node_routes["Query and session routes<br/>HTTP API<br/>[routes.py]"]
end

subgraph group_workflow["Query workflow"]
  node_ask_service["Request coordinator<br/>application service<br/>[askdata_service.py]"]
  node_query_graph{{"Staged query graph<br/>agent workflow<br/>[query_graph.py]"}}
  node_workflow_state["Workflow state<br/>state model<br/>[state.py]"]
  node_result_builder["Client result builder<br/>response shaping<br/>[result_builder.py]"]
end

subgraph group_agent["Agent and execution"]
  node_sql_agent{{"Single-database SQL agent<br/>agent loop"}}
  node_model_client["Model client<br/>external model adapter<br/>[model_client.py]"]
  node_mcp_runtime["Controlled tool runtime<br/>MCP-style tools<br/>[server.py]"]
  node_query_engine[("DuckDB executor<br/>read-only SQL engine<br/>[duckdb_engine.py]")]
  node_database[("Local structured database")]
end

subgraph group_retrieval["Schema retrieval"]
  node_retrieval_service["Evidence retrieval<br/>retrieval orchestrator<br/>[service.py]"]
  node_retrieval_store[("Schema retrieval store<br/>metadata store<br/>[store.py]")]
  node_schema_graph["Schema relationships<br/>schema graph builder<br/>[graph.py]"]
end

subgraph group_context["Session context"]
  node_session_context["Conversation context<br/>context builder<br/>[session_context.py]"]
  node_short_memory["Recent-turn memory<br/>session memory"]
  node_session_archive[("Session archive<br/>history persistence<br/>[session_archive.py]")]
end

node_browser -->|"submits questions"| node_frontend_api
node_frontend_api -->|"HTTP"| node_routes
node_backend_entry -->|"starts"| node_application
node_application -->|"mounts"| node_routes
node_routes -->|"dispatches"| node_ask_service
node_ask_service -->|"runs"| node_query_graph
node_query_graph -->|"updates"| node_workflow_state
node_query_graph -->|"builds context"| node_session_context
node_session_context -->|"reads recent turns"| node_short_memory
node_session_context -->|"reads history"| node_session_archive
node_query_graph -->|"grounds request"| node_retrieval_service
node_retrieval_service -->|"retrieves metadata"| node_retrieval_store
node_retrieval_service -->|"uses relationships"| node_schema_graph
node_query_graph -->|"plans and generates"| node_sql_agent
node_retrieval_service -->|"constrained evidence"| node_sql_agent
node_sql_agent -->|"model calls"| node_model_client
node_sql_agent -->|"tool calls"| node_mcp_runtime
node_mcp_runtime -->|"database tool"| node_query_engine
node_sql_agent -->|"validated SQL"| node_query_engine
node_query_engine -->|"read-only queries"| node_database
node_query_graph -->|"builds response"| node_result_builder
node_result_builder -->|"client result"| node_routes
node_browser -->|"renders tabular results"| node_result_table
node_ask_service -->|"records turn"| node_short_memory
node_ask_service -->|"archives turn"| node_session_archive

click node_browser "https://github.com/sheily899/querypilot-data-agent/blob/main/frontend/src/App.vue"
click node_frontend_api "https://github.com/sheily899/querypilot-data-agent/blob/main/frontend/src/api.ts"
click node_result_table "https://github.com/sheily899/querypilot-data-agent/blob/main/frontend/src/components/ResultTableCard.vue"
click node_backend_entry "https://github.com/sheily899/querypilot-data-agent/blob/main/backend/run.py"
click node_application "https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/main.py"
click node_routes "https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/api/routes.py"
click node_ask_service "https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/services/askdata_service.py"
click node_query_graph "https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/workflows/query_graph.py"
click node_workflow_state "https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/workflows/state.py"
click node_result_builder "https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/workflows/result_builder.py"
click node_retrieval_service "https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/retrieval/service.py"
click node_retrieval_store "https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/retrieval/store.py"
click node_schema_graph "https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/retrieval/graph.py"
click node_session_context "https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/services/session_context.py"
click node_short_memory "https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/services/short_term_memory.py"
click node_session_archive "https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/services/session_archive.py"
click node_sql_agent "https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/querying/single_database_agent.py"
click node_model_client "https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/model_client.py"
click node_mcp_runtime "https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/mcp_runtime/server.py"
click node_query_engine "https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/querying/duckdb_engine.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_browser,node_frontend_api,node_result_table toneBlue
class node_backend_entry,node_application,node_routes toneAmber
class node_ask_service,node_query_graph,node_workflow_state,node_result_builder toneMint
class node_sql_agent,node_model_client,node_mcp_runtime,node_query_engine,node_database toneRose
class node_retrieval_service,node_retrieval_store,node_schema_graph toneIndigo
class node_session_context,node_short_memory,node_session_archive toneTeal
```

## Components

### Vue workbench

- [Vue application](https://github.com/sheily899/querypilot-data-agent/blob/main/frontend/src/App.vue): Vue client · Conversation and query workbench. · `frontend/src/App.vue`
- [API client](https://github.com/sheily899/querypilot-data-agent/blob/main/frontend/src/api.ts): TypeScript API · `frontend/src/api.ts`
- [Results table](https://github.com/sheily899/querypilot-data-agent/blob/main/frontend/src/components/ResultTableCard.vue): Vue component · `frontend/src/components/ResultTableCard.vue`

### Application API

- [Backend launcher](https://github.com/sheily899/querypilot-data-agent/blob/main/backend/run.py): Python entry point · `backend/run.py`
- [Application assembly](https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/main.py): Python app · `backend/app/main.py`
- [Query and session routes](https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/api/routes.py): HTTP API · `backend/app/api/routes.py`

### Query workflow

- [Request coordinator](https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/services/askdata_service.py): application service · `backend/app/services/askdata_service.py`
- [Staged query graph](https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/workflows/query_graph.py): agent workflow · `backend/app/workflows/query_graph.py`
- [Workflow state](https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/workflows/state.py): state model · Carries retrieval, SQL, execution, and response state. · `backend/app/workflows/state.py`
- [Client result builder](https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/workflows/result_builder.py): response shaping · `backend/app/workflows/result_builder.py`

### Agent and execution

- [Single-database SQL agent](https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/querying/single_database_agent.py): agent loop · Generates guarded SQL using grounded evidence and tools. · `backend/app/querying/single_database_agent.py`
- [Model client](https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/model_client.py): external model adapter · `backend/app/model_client.py`
- [Controlled tool runtime](https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/mcp_runtime/server.py): MCP-style tools · Controls model-initiated database and time operations. · `backend/app/mcp_runtime/server.py`
- [DuckDB executor](https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/querying/duckdb_engine.py): read-only SQL engine · Executes validated single-database queries. · `backend/app/querying/duckdb_engine.py`
- **Local structured database**: database

### Schema retrieval

- [Evidence retrieval](https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/retrieval/service.py): retrieval orchestrator · Fuses keyword and semantic schema retrieval. · `backend/app/retrieval/service.py`
- [Schema retrieval store](https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/retrieval/store.py): metadata store · `backend/app/retrieval/store.py`
- [Schema relationships](https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/retrieval/graph.py): schema graph builder · `backend/app/retrieval/graph.py`

### Session context

- [Conversation context](https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/services/session_context.py): context builder · `backend/app/services/session_context.py`
- [Recent-turn memory](https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/services/short_term_memory.py): session memory · `backend/app/services/short_term_memory.py`
- [Session archive](https://github.com/sheily899/querypilot-data-agent/blob/main/backend/app/services/session_archive.py): history persistence · `backend/app/services/session_archive.py`

## Connections

- Vue application → API client: submits questions
- API client → Query and session routes: HTTP
- Backend launcher → Application assembly: starts
- Application assembly → Query and session routes: mounts
- Query and session routes → Request coordinator: dispatches
- Request coordinator → Staged query graph: runs
- Staged query graph → Workflow state: updates
- Staged query graph → Conversation context: builds context
- Conversation context → Recent-turn memory: reads recent turns
- Conversation context → Session archive: reads history
- Staged query graph → Evidence retrieval: grounds request
- Evidence retrieval → Schema retrieval store: retrieves metadata
- Evidence retrieval → Schema relationships: uses relationships
- Staged query graph → Single-database SQL agent: plans and generates
- Evidence retrieval → Single-database SQL agent: constrained evidence
- Single-database SQL agent → Model client: model calls
- Single-database SQL agent → Controlled tool runtime: tool calls
- Controlled tool runtime → DuckDB executor: database tool
- Single-database SQL agent → DuckDB executor: validated SQL
- DuckDB executor → Local structured database: read-only queries
- Staged query graph → Client result builder: builds response
- Client result builder → Query and session routes: client result
- Vue application → Results table: renders tabular results
- Request coordinator → Recent-turn memory: records turn
- Request coordinator → Session archive: archives turn
