dbagent-cli 0.2.2__tar.gz → 0.3.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dbagent_cli-0.3.1/PKG-INFO +198 -0
- dbagent_cli-0.3.1/README.md +174 -0
- {dbagent_cli-0.2.2 → dbagent_cli-0.3.1}/dbagent/__init__.py +1 -1
- dbagent_cli-0.3.1/dbagent/agent/confidence.py +86 -0
- dbagent_cli-0.3.1/dbagent/agent/conversation.py +82 -0
- dbagent_cli-0.3.1/dbagent/agent/doctor.py +161 -0
- dbagent_cli-0.3.1/dbagent/agent/error_classifier.py +77 -0
- dbagent_cli-0.3.1/dbagent/agent/pipeline.py +403 -0
- dbagent_cli-0.3.1/dbagent/agent/recommender.py +61 -0
- dbagent_cli-0.3.1/dbagent/cli.py +1556 -0
- {dbagent_cli-0.2.2 → dbagent_cli-0.3.1}/dbagent/config.py +6 -0
- {dbagent_cli-0.2.2 → dbagent_cli-0.3.1}/dbagent/connectors/base.py +11 -0
- {dbagent_cli-0.2.2 → dbagent_cli-0.3.1}/dbagent/connectors/relational.py +11 -0
- dbagent_cli-0.3.1/dbagent/execution/__init__.py +1 -0
- dbagent_cli-0.3.1/dbagent/execution/cache.py +70 -0
- dbagent_cli-0.3.1/dbagent/execution/changeset.py +188 -0
- dbagent_cli-0.3.1/dbagent/execution/cost.py +81 -0
- dbagent_cli-0.3.1/dbagent/execution/recovery.py +571 -0
- dbagent_cli-0.3.1/dbagent/execution/sandbox.py +96 -0
- dbagent_cli-0.3.1/dbagent/execution/streaming.py +65 -0
- dbagent_cli-0.3.1/dbagent/schema/cache.py +184 -0
- dbagent_cli-0.3.1/dbagent/schema/graph.py +248 -0
- dbagent_cli-0.3.1/dbagent/schema/retriever.py +114 -0
- dbagent_cli-0.3.1/dbagent/security/__init__.py +1 -0
- dbagent_cli-0.3.1/dbagent/security/audit.py +74 -0
- dbagent_cli-0.3.1/dbagent/security/environment.py +54 -0
- dbagent_cli-0.3.1/dbagent/security/masking.py +126 -0
- dbagent_cli-0.3.1/dbagent/security/modes.py +44 -0
- dbagent_cli-0.3.1/dbagent/security/rbac.py +49 -0
- dbagent_cli-0.3.1/dbagent/semantic/__init__.py +1 -0
- dbagent_cli-0.3.1/dbagent/semantic/dictionary.py +79 -0
- dbagent_cli-0.3.1/dbagent/semantic/templates.py +122 -0
- dbagent_cli-0.3.1/dbagent/sql/__init__.py +1 -0
- dbagent_cli-0.3.1/dbagent/sql/optimizer.py +63 -0
- dbagent_cli-0.3.1/dbagent/sql/validator.py +163 -0
- dbagent_cli-0.3.1/dbagent/ui/visualizer.py +96 -0
- dbagent_cli-0.3.1/dbagent_cli.egg-info/PKG-INFO +198 -0
- dbagent_cli-0.3.1/dbagent_cli.egg-info/SOURCES.txt +71 -0
- {dbagent_cli-0.2.2 → dbagent_cli-0.3.1}/pyproject.toml +2 -2
- dbagent_cli-0.3.1/tests/test_cache.py +91 -0
- {dbagent_cli-0.2.2 → dbagent_cli-0.3.1}/tests/test_cli.py +64 -0
- dbagent_cli-0.3.1/tests/test_doctor_and_viz.py +49 -0
- dbagent_cli-0.3.1/tests/test_graph.py +120 -0
- dbagent_cli-0.3.1/tests/test_recovery.py +265 -0
- dbagent_cli-0.3.1/tests/test_safety.py +50 -0
- dbagent_cli-0.3.1/tests/test_sandbox.py +67 -0
- dbagent_cli-0.3.1/tests/test_security.py +50 -0
- dbagent_cli-0.3.1/tests/test_semantic.py +46 -0
- dbagent_cli-0.2.2/PKG-INFO +0 -183
- dbagent_cli-0.2.2/README.md +0 -159
- dbagent_cli-0.2.2/dbagent/agent/pipeline.py +0 -285
- dbagent_cli-0.2.2/dbagent/cli.py +0 -1036
- dbagent_cli-0.2.2/dbagent_cli.egg-info/PKG-INFO +0 -183
- dbagent_cli-0.2.2/dbagent_cli.egg-info/SOURCES.txt +0 -35
- {dbagent_cli-0.2.2 → dbagent_cli-0.3.1}/dbagent/agent/generator.py +0 -0
- {dbagent_cli-0.2.2 → dbagent_cli-0.3.1}/dbagent/agent/validator.py +0 -0
- {dbagent_cli-0.2.2 → dbagent_cli-0.3.1}/dbagent/connectors/factory.py +0 -0
- {dbagent_cli-0.2.2 → dbagent_cli-0.3.1}/dbagent/connectors/mongo.py +0 -0
- {dbagent_cli-0.2.2 → dbagent_cli-0.3.1}/dbagent/llm/auto_setup.py +0 -0
- {dbagent_cli-0.2.2 → dbagent_cli-0.3.1}/dbagent/llm/base.py +0 -0
- {dbagent_cli-0.2.2 → dbagent_cli-0.3.1}/dbagent/llm/factory.py +0 -0
- {dbagent_cli-0.2.2 → dbagent_cli-0.3.1}/dbagent/llm/gemini_provider.py +0 -0
- {dbagent_cli-0.2.2 → dbagent_cli-0.3.1}/dbagent/llm/groq_provider.py +0 -0
- {dbagent_cli-0.2.2 → dbagent_cli-0.3.1}/dbagent/llm/mock_provider.py +0 -0
- {dbagent_cli-0.2.2 → dbagent_cli-0.3.1}/dbagent/llm/ollama_provider.py +0 -0
- {dbagent_cli-0.2.2 → dbagent_cli-0.3.1}/dbagent/llm/openrouter_provider.py +0 -0
- {dbagent_cli-0.2.2 → dbagent_cli-0.3.1}/dbagent/schema/formatter.py +0 -0
- {dbagent_cli-0.2.2 → dbagent_cli-0.3.1}/dbagent/schema/models.py +0 -0
- {dbagent_cli-0.2.2 → dbagent_cli-0.3.1}/dbagent/schema/selector.py +0 -0
- {dbagent_cli-0.2.2 → dbagent_cli-0.3.1}/dbagent/ui/console.py +0 -0
- {dbagent_cli-0.2.2 → dbagent_cli-0.3.1}/dbagent/ui/viewer.py +0 -0
- {dbagent_cli-0.2.2 → dbagent_cli-0.3.1}/dbagent_cli.egg-info/dependency_links.txt +0 -0
- {dbagent_cli-0.2.2 → dbagent_cli-0.3.1}/dbagent_cli.egg-info/entry_points.txt +0 -0
- {dbagent_cli-0.2.2 → dbagent_cli-0.3.1}/dbagent_cli.egg-info/requires.txt +0 -0
- {dbagent_cli-0.2.2 → dbagent_cli-0.3.1}/dbagent_cli.egg-info/top_level.txt +0 -0
- {dbagent_cli-0.2.2 → dbagent_cli-0.3.1}/setup.cfg +0 -0
- {dbagent_cli-0.2.2 → dbagent_cli-0.3.1}/tests/test_connectors.py +0 -0
- {dbagent_cli-0.2.2 → dbagent_cli-0.3.1}/tests/test_generator.py +0 -0
- {dbagent_cli-0.2.2 → dbagent_cli-0.3.1}/tests/test_schema.py +0 -0
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: dbagent-cli
|
|
3
|
+
Version: 0.3.1
|
|
4
|
+
Summary: Enterprise AI Database Copilot with Long-Term Change Recovery (CLI)
|
|
5
|
+
Author: Santhosh Gupta
|
|
6
|
+
Requires-Python: >=3.9
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
Requires-Dist: typer>=0.9.0
|
|
9
|
+
Requires-Dist: rich>=13.0.0
|
|
10
|
+
Requires-Dist: prompt-toolkit>=3.0.0
|
|
11
|
+
Requires-Dist: pydantic>=2.0.0
|
|
12
|
+
Requires-Dist: sqlalchemy>=2.0.0
|
|
13
|
+
Requires-Dist: requests>=2.28.0
|
|
14
|
+
Requires-Dist: httpx>=0.24.0
|
|
15
|
+
Requires-Dist: python-dotenv>=1.0.0
|
|
16
|
+
Provides-Extra: all
|
|
17
|
+
Requires-Dist: psycopg2-binary>=2.9.0; extra == "all"
|
|
18
|
+
Requires-Dist: pymysql>=1.0.0; extra == "all"
|
|
19
|
+
Requires-Dist: pymongo>=4.0.0; extra == "all"
|
|
20
|
+
Requires-Dist: duckdb>=0.9.0; extra == "all"
|
|
21
|
+
Provides-Extra: dev
|
|
22
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
23
|
+
Requires-Dist: pytest-asyncio>=0.20.0; extra == "dev"
|
|
24
|
+
|
|
25
|
+
# ⚡ DB-Agent
|
|
26
|
+
|
|
27
|
+
> **Enterprise AI Database Copilot & Autonomous Data Agent (CLI)**
|
|
28
|
+
> *Author: Santhosh Gupta | 100% Free, Standalone, Works Offline (Ollama) or with Cloud AI (Gemini / Groq / OpenRouter).*
|
|
29
|
+
|
|
30
|
+
[](https://pypi.org/project/dbagent-cli/)
|
|
31
|
+
[](https://pypi.org/project/dbagent-cli/)
|
|
32
|
+
[](https://opensource.org/licenses/MIT)
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## 🌟 What is DB-Agent?
|
|
37
|
+
|
|
38
|
+
**DB-Agent** is a production-grade AI Database Agent and Copilot. It empowers developers, DBAs, data analysts, and non-technical users to inspect schemas, discover complex multi-hop table relationships, generate and optimize SQL, execute queries with enterprise safety guardrails, and visualize data directly in the terminal.
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## 🚀 2-Minute Quickstart
|
|
43
|
+
|
|
44
|
+
### Step 1: Install from PyPI
|
|
45
|
+
```bash
|
|
46
|
+
pip install "dbagent-cli[all]"
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Step 2: 1-Click AI Setup
|
|
50
|
+
```bash
|
|
51
|
+
db-agent setup
|
|
52
|
+
```
|
|
53
|
+
Choose between:
|
|
54
|
+
1. **Local Offline AI (Ollama)**: Zero API keys, 100% private, runs entirely on your machine.
|
|
55
|
+
2. **Free Cloud AI (Gemini / Groq / OpenRouter)**: Ultra-fast cloud processing.
|
|
56
|
+
3. **Database Tools Only**: Standard DB introspection and CLI tools without AI.
|
|
57
|
+
|
|
58
|
+
### Step 3: Connect Database & Save Alias
|
|
59
|
+
```bash
|
|
60
|
+
db-agent connect postgresql://user:password@localhost:5432/mydb --alias my_db
|
|
61
|
+
```
|
|
62
|
+
*(Supports PostgreSQL, MySQL, SQLite, SQL Server, Oracle, DuckDB, and MongoDB).*
|
|
63
|
+
|
|
64
|
+
### Step 4: Ask Plain English Questions or Launch Chat!
|
|
65
|
+
```bash
|
|
66
|
+
# 1. Ask a question and get instant live results table + ASCII charts
|
|
67
|
+
db-agent ask "list top 10 users" --db my_db
|
|
68
|
+
|
|
69
|
+
# 2. Or start the interactive AI copilot shell
|
|
70
|
+
db-agent chat --db my_db
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
---
|
|
74
|
+
|
|
75
|
+
## 🛡️ Key Enterprise Features
|
|
76
|
+
|
|
77
|
+
### 1. 🧠 Relationship Graph & Smart Multi-Hop JOINs
|
|
78
|
+
Automatically discovers multi-hop join paths between disconnected tables without guessing arbitrary joins.
|
|
79
|
+
```bash
|
|
80
|
+
# Find shortest relationship path between two tables:
|
|
81
|
+
db-agent path users events --db my_db
|
|
82
|
+
|
|
83
|
+
# Explain join cardinality (1:1, 1:N) and recommended join syntax:
|
|
84
|
+
db-agent explain-join customers orders --db my_db
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### 2. ⚡ Persistent Schema Cache & Change Detection
|
|
88
|
+
Never runs expensive full database scans on every query. Local cache (`~/.dbagent/cache/`) detects schema diffs:
|
|
89
|
+
```bash
|
|
90
|
+
db-agent refresh-schema --db my_db
|
|
91
|
+
db-agent schema-status --db my_db
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### 3. 🔒 Safety Modes & Production Protection
|
|
95
|
+
Five operational safety modes enforce strict boundaries before SQL execution:
|
|
96
|
+
* **`SAFE`**: Only `SELECT`, `SHOW`, `DESCRIBE`, `EXPLAIN`, `WITH` allowed. All writes and DDL are strictly blocked.
|
|
97
|
+
* **`READ_ONLY`**: Enforced on `PRODUCTION` environments with 5s timeouts and row caps.
|
|
98
|
+
* **`CONFIRM`**: Reads auto-execute; write and DDL operations require interactive confirmation.
|
|
99
|
+
* **`SANDBOX`**: Writes executed inside uncommitted transactions with before/after diff preview.
|
|
100
|
+
* **`ADMIN`**: Full administrative access with audit tracking.
|
|
101
|
+
|
|
102
|
+
### 4. ⏪ Change Sets & Snapshot Rollback
|
|
103
|
+
Before executing modifying queries, DB-Agent captures a recovery snapshot and creates an auditable ChangeSet:
|
|
104
|
+
```bash
|
|
105
|
+
db-agent changes
|
|
106
|
+
db-agent changes-show CS-20260828-001
|
|
107
|
+
db-agent rollback CS-20260828-001 --db my_db
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### 5. 🏥 Database Doctor & Query Optimizer
|
|
111
|
+
Run comprehensive health checks on connectivity, latency, unindexed foreign keys, and slow queries:
|
|
112
|
+
```bash
|
|
113
|
+
# Database health diagnostics:
|
|
114
|
+
db-agent doctor --db my_db
|
|
115
|
+
|
|
116
|
+
# EXPLAIN plan analysis & index recommendations:
|
|
117
|
+
db-agent optimize "SELECT * FROM events WHERE camera_id = 42" --db my_db
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### 6. 📚 Business Semantic Dictionary & Query Templates
|
|
121
|
+
Teach DB-Agent your domain-specific metrics and definitions:
|
|
122
|
+
```bash
|
|
123
|
+
# Define a business metric:
|
|
124
|
+
db-agent define offline_camera "A camera with no heartbeat for > 2 minutes" --sql "last_heartbeat < NOW() - INTERVAL '2 mins'"
|
|
125
|
+
|
|
126
|
+
# List all definitions:
|
|
127
|
+
db-agent definitions
|
|
128
|
+
|
|
129
|
+
# Manage trusted SQL templates:
|
|
130
|
+
db-agent template list
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### 7. 🎭 Sensitive Data Masking & RBAC
|
|
134
|
+
Automatically classifies columns into PII, Secrets, and Financial data, masking confidential values in outputs (`s***@gmail.com`, `**********`, `****-****-****-1234`).
|
|
135
|
+
|
|
136
|
+
### 8. 📊 Automatic Visualization Detector
|
|
137
|
+
Detects when query results represent time series, category distributions, or single metrics, and renders ASCII/Unicode charts directly in your terminal.
|
|
138
|
+
|
|
139
|
+
---
|
|
140
|
+
|
|
141
|
+
## 📖 Complete CLI Command Reference
|
|
142
|
+
|
|
143
|
+
| Command | How to Use | Purpose |
|
|
144
|
+
|---|---|---|
|
|
145
|
+
| **`ask`** | `db-agent ask "list top 10 users" --db my_db` | Natural language to SQL -> executes & renders table. |
|
|
146
|
+
| **`chat`** | `db-agent chat --db my_db` | Interactive terminal copilot with multi-turn context. |
|
|
147
|
+
| **`doctor`** | `db-agent doctor --db my_db` | Comprehensive database and AI health diagnostic check. |
|
|
148
|
+
| **`path`** | `db-agent path users events --db my_db` | Shortest multi-hop foreign key join path discovery. |
|
|
149
|
+
| **`explain-join`** | `db-agent explain-join customers orders` | Explains join relationship, cardinality, and syntax. |
|
|
150
|
+
| **`optimize`** | `db-agent optimize "<SQL>" --db my_db` | Analyzes EXPLAIN plan and recommends indexes. |
|
|
151
|
+
| **`scan`** | `db-agent scan --db my_db --export db.md` | Full database schema introspection & Markdown export. |
|
|
152
|
+
| **`refresh-schema`**| `db-agent refresh-schema --db my_db` | Refreshes schema cache and reports detected diffs. |
|
|
153
|
+
| **`schema-status`** | `db-agent schema-status --db my_db` | Displays schema cache age, version, and health. |
|
|
154
|
+
| **`changes`** | `db-agent changes` | Lists recorded write ChangeSets. |
|
|
155
|
+
| **`changes-show`**| `db-agent changes-show CS-20260828-001` | Shows before/after snapshot of a ChangeSet. |
|
|
156
|
+
| **`rollback`** | `db-agent rollback CS-20260828-001` | Generates and executes inverse rollback SQL. |
|
|
157
|
+
| **`define`** | `db-agent define revenue "SUM(order.amount)"` | Saves a business definition into the semantic layer. |
|
|
158
|
+
| **`definitions`** | `db-agent definitions` | Lists all defined business terminology. |
|
|
159
|
+
| **`template`** | `db-agent template list` | Manages reusable, parameterized query templates. |
|
|
160
|
+
| **`audit`** | `db-agent audit` | Displays recent query execution audit log. |
|
|
161
|
+
| **`connect`** | `db-agent connect <url> --alias my_db` | Tests connection and saves profile alias. |
|
|
162
|
+
| **`use`** | `db-agent use gemini` | Switches default AI provider. |
|
|
163
|
+
| **`run`** | `db-agent run "SELECT * FROM users" --db my_db` | Executes raw SQL query with Rich table output. |
|
|
164
|
+
| **`generate`** | `db-agent generate "ETL script" --db my_db -o etl.py` | Generates Python ETL, Alembic migrations, or APIs. |
|
|
165
|
+
| **`setup`** | `db-agent setup` | 1-Click setup wizard for local or cloud AI. |
|
|
166
|
+
| **`config`** | `db-agent config` | Interactive AI API keys and preferences wizard. |
|
|
167
|
+
| **`profiles`** | `db-agent profiles` | Lists all saved database connection profiles. |
|
|
168
|
+
| **`models`** | `db-agent models` | Status of local Ollama models and cloud AI providers. |
|
|
169
|
+
| **`guide`** | `db-agent guide` | Displays full interactive command cheatsheet. |
|
|
170
|
+
|
|
171
|
+
---
|
|
172
|
+
|
|
173
|
+
## 💬 In-Chat REPL Commands (`db-agent chat`)
|
|
174
|
+
|
|
175
|
+
Inside the interactive chat shell, you can use built-in colon commands:
|
|
176
|
+
|
|
177
|
+
* **`:doctor`** — Run database health diagnostics.
|
|
178
|
+
* **`:path <t1> <t2>`** — Discover shortest join path between tables.
|
|
179
|
+
* **`:join <t1> <t2>`** — Explain join cardinality and recommended join syntax.
|
|
180
|
+
* **`:mode <SAFE|CONFIRM|SANDBOX|ADMIN>`** — Switch safety mode mid-session.
|
|
181
|
+
* **`:changes`** — List recent write ChangeSets.
|
|
182
|
+
* **`:tables`** — List all database tables.
|
|
183
|
+
* **`:table <name>`** — Inspect columns, types, primary keys, and sample data.
|
|
184
|
+
* **`:fk <name>`** — View incoming and outgoing foreign keys.
|
|
185
|
+
* **`:provider <name>`** — Switch AI provider (`ollama`, `gemini`, `groq`, `openrouter`).
|
|
186
|
+
* **`:model <name>`** — Switch active model name on the fly.
|
|
187
|
+
* **`:explain`** — Show generated SQL and query analysis for the last turn.
|
|
188
|
+
* **`:history`** — View query history for the session.
|
|
189
|
+
* **`:auto` / `:noauto`** — Toggle automatic execution of read queries.
|
|
190
|
+
* **`:run`** — Re-execute the last generated SQL query.
|
|
191
|
+
* **`:clear`** — Clear multi-turn conversational context.
|
|
192
|
+
* **`:export <file>`** — Save the last generated SQL to a file.
|
|
193
|
+
* **`:exit`** — Exit chat session.
|
|
194
|
+
|
|
195
|
+
---
|
|
196
|
+
|
|
197
|
+
## 📄 License
|
|
198
|
+
MIT License. Built with ❤️ by Santhosh Gupta.
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# ⚡ DB-Agent
|
|
2
|
+
|
|
3
|
+
> **Enterprise AI Database Copilot & Autonomous Data Agent (CLI)**
|
|
4
|
+
> *Author: Santhosh Gupta | 100% Free, Standalone, Works Offline (Ollama) or with Cloud AI (Gemini / Groq / OpenRouter).*
|
|
5
|
+
|
|
6
|
+
[](https://pypi.org/project/dbagent-cli/)
|
|
7
|
+
[](https://pypi.org/project/dbagent-cli/)
|
|
8
|
+
[](https://opensource.org/licenses/MIT)
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## 🌟 What is DB-Agent?
|
|
13
|
+
|
|
14
|
+
**DB-Agent** is a production-grade AI Database Agent and Copilot. It empowers developers, DBAs, data analysts, and non-technical users to inspect schemas, discover complex multi-hop table relationships, generate and optimize SQL, execute queries with enterprise safety guardrails, and visualize data directly in the terminal.
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## 🚀 2-Minute Quickstart
|
|
19
|
+
|
|
20
|
+
### Step 1: Install from PyPI
|
|
21
|
+
```bash
|
|
22
|
+
pip install "dbagent-cli[all]"
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### Step 2: 1-Click AI Setup
|
|
26
|
+
```bash
|
|
27
|
+
db-agent setup
|
|
28
|
+
```
|
|
29
|
+
Choose between:
|
|
30
|
+
1. **Local Offline AI (Ollama)**: Zero API keys, 100% private, runs entirely on your machine.
|
|
31
|
+
2. **Free Cloud AI (Gemini / Groq / OpenRouter)**: Ultra-fast cloud processing.
|
|
32
|
+
3. **Database Tools Only**: Standard DB introspection and CLI tools without AI.
|
|
33
|
+
|
|
34
|
+
### Step 3: Connect Database & Save Alias
|
|
35
|
+
```bash
|
|
36
|
+
db-agent connect postgresql://user:password@localhost:5432/mydb --alias my_db
|
|
37
|
+
```
|
|
38
|
+
*(Supports PostgreSQL, MySQL, SQLite, SQL Server, Oracle, DuckDB, and MongoDB).*
|
|
39
|
+
|
|
40
|
+
### Step 4: Ask Plain English Questions or Launch Chat!
|
|
41
|
+
```bash
|
|
42
|
+
# 1. Ask a question and get instant live results table + ASCII charts
|
|
43
|
+
db-agent ask "list top 10 users" --db my_db
|
|
44
|
+
|
|
45
|
+
# 2. Or start the interactive AI copilot shell
|
|
46
|
+
db-agent chat --db my_db
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
## 🛡️ Key Enterprise Features
|
|
52
|
+
|
|
53
|
+
### 1. 🧠 Relationship Graph & Smart Multi-Hop JOINs
|
|
54
|
+
Automatically discovers multi-hop join paths between disconnected tables without guessing arbitrary joins.
|
|
55
|
+
```bash
|
|
56
|
+
# Find shortest relationship path between two tables:
|
|
57
|
+
db-agent path users events --db my_db
|
|
58
|
+
|
|
59
|
+
# Explain join cardinality (1:1, 1:N) and recommended join syntax:
|
|
60
|
+
db-agent explain-join customers orders --db my_db
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### 2. ⚡ Persistent Schema Cache & Change Detection
|
|
64
|
+
Never runs expensive full database scans on every query. Local cache (`~/.dbagent/cache/`) detects schema diffs:
|
|
65
|
+
```bash
|
|
66
|
+
db-agent refresh-schema --db my_db
|
|
67
|
+
db-agent schema-status --db my_db
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### 3. 🔒 Safety Modes & Production Protection
|
|
71
|
+
Five operational safety modes enforce strict boundaries before SQL execution:
|
|
72
|
+
* **`SAFE`**: Only `SELECT`, `SHOW`, `DESCRIBE`, `EXPLAIN`, `WITH` allowed. All writes and DDL are strictly blocked.
|
|
73
|
+
* **`READ_ONLY`**: Enforced on `PRODUCTION` environments with 5s timeouts and row caps.
|
|
74
|
+
* **`CONFIRM`**: Reads auto-execute; write and DDL operations require interactive confirmation.
|
|
75
|
+
* **`SANDBOX`**: Writes executed inside uncommitted transactions with before/after diff preview.
|
|
76
|
+
* **`ADMIN`**: Full administrative access with audit tracking.
|
|
77
|
+
|
|
78
|
+
### 4. ⏪ Change Sets & Snapshot Rollback
|
|
79
|
+
Before executing modifying queries, DB-Agent captures a recovery snapshot and creates an auditable ChangeSet:
|
|
80
|
+
```bash
|
|
81
|
+
db-agent changes
|
|
82
|
+
db-agent changes-show CS-20260828-001
|
|
83
|
+
db-agent rollback CS-20260828-001 --db my_db
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### 5. 🏥 Database Doctor & Query Optimizer
|
|
87
|
+
Run comprehensive health checks on connectivity, latency, unindexed foreign keys, and slow queries:
|
|
88
|
+
```bash
|
|
89
|
+
# Database health diagnostics:
|
|
90
|
+
db-agent doctor --db my_db
|
|
91
|
+
|
|
92
|
+
# EXPLAIN plan analysis & index recommendations:
|
|
93
|
+
db-agent optimize "SELECT * FROM events WHERE camera_id = 42" --db my_db
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### 6. 📚 Business Semantic Dictionary & Query Templates
|
|
97
|
+
Teach DB-Agent your domain-specific metrics and definitions:
|
|
98
|
+
```bash
|
|
99
|
+
# Define a business metric:
|
|
100
|
+
db-agent define offline_camera "A camera with no heartbeat for > 2 minutes" --sql "last_heartbeat < NOW() - INTERVAL '2 mins'"
|
|
101
|
+
|
|
102
|
+
# List all definitions:
|
|
103
|
+
db-agent definitions
|
|
104
|
+
|
|
105
|
+
# Manage trusted SQL templates:
|
|
106
|
+
db-agent template list
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### 7. 🎭 Sensitive Data Masking & RBAC
|
|
110
|
+
Automatically classifies columns into PII, Secrets, and Financial data, masking confidential values in outputs (`s***@gmail.com`, `**********`, `****-****-****-1234`).
|
|
111
|
+
|
|
112
|
+
### 8. 📊 Automatic Visualization Detector
|
|
113
|
+
Detects when query results represent time series, category distributions, or single metrics, and renders ASCII/Unicode charts directly in your terminal.
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
## 📖 Complete CLI Command Reference
|
|
118
|
+
|
|
119
|
+
| Command | How to Use | Purpose |
|
|
120
|
+
|---|---|---|
|
|
121
|
+
| **`ask`** | `db-agent ask "list top 10 users" --db my_db` | Natural language to SQL -> executes & renders table. |
|
|
122
|
+
| **`chat`** | `db-agent chat --db my_db` | Interactive terminal copilot with multi-turn context. |
|
|
123
|
+
| **`doctor`** | `db-agent doctor --db my_db` | Comprehensive database and AI health diagnostic check. |
|
|
124
|
+
| **`path`** | `db-agent path users events --db my_db` | Shortest multi-hop foreign key join path discovery. |
|
|
125
|
+
| **`explain-join`** | `db-agent explain-join customers orders` | Explains join relationship, cardinality, and syntax. |
|
|
126
|
+
| **`optimize`** | `db-agent optimize "<SQL>" --db my_db` | Analyzes EXPLAIN plan and recommends indexes. |
|
|
127
|
+
| **`scan`** | `db-agent scan --db my_db --export db.md` | Full database schema introspection & Markdown export. |
|
|
128
|
+
| **`refresh-schema`**| `db-agent refresh-schema --db my_db` | Refreshes schema cache and reports detected diffs. |
|
|
129
|
+
| **`schema-status`** | `db-agent schema-status --db my_db` | Displays schema cache age, version, and health. |
|
|
130
|
+
| **`changes`** | `db-agent changes` | Lists recorded write ChangeSets. |
|
|
131
|
+
| **`changes-show`**| `db-agent changes-show CS-20260828-001` | Shows before/after snapshot of a ChangeSet. |
|
|
132
|
+
| **`rollback`** | `db-agent rollback CS-20260828-001` | Generates and executes inverse rollback SQL. |
|
|
133
|
+
| **`define`** | `db-agent define revenue "SUM(order.amount)"` | Saves a business definition into the semantic layer. |
|
|
134
|
+
| **`definitions`** | `db-agent definitions` | Lists all defined business terminology. |
|
|
135
|
+
| **`template`** | `db-agent template list` | Manages reusable, parameterized query templates. |
|
|
136
|
+
| **`audit`** | `db-agent audit` | Displays recent query execution audit log. |
|
|
137
|
+
| **`connect`** | `db-agent connect <url> --alias my_db` | Tests connection and saves profile alias. |
|
|
138
|
+
| **`use`** | `db-agent use gemini` | Switches default AI provider. |
|
|
139
|
+
| **`run`** | `db-agent run "SELECT * FROM users" --db my_db` | Executes raw SQL query with Rich table output. |
|
|
140
|
+
| **`generate`** | `db-agent generate "ETL script" --db my_db -o etl.py` | Generates Python ETL, Alembic migrations, or APIs. |
|
|
141
|
+
| **`setup`** | `db-agent setup` | 1-Click setup wizard for local or cloud AI. |
|
|
142
|
+
| **`config`** | `db-agent config` | Interactive AI API keys and preferences wizard. |
|
|
143
|
+
| **`profiles`** | `db-agent profiles` | Lists all saved database connection profiles. |
|
|
144
|
+
| **`models`** | `db-agent models` | Status of local Ollama models and cloud AI providers. |
|
|
145
|
+
| **`guide`** | `db-agent guide` | Displays full interactive command cheatsheet. |
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
## 💬 In-Chat REPL Commands (`db-agent chat`)
|
|
150
|
+
|
|
151
|
+
Inside the interactive chat shell, you can use built-in colon commands:
|
|
152
|
+
|
|
153
|
+
* **`:doctor`** — Run database health diagnostics.
|
|
154
|
+
* **`:path <t1> <t2>`** — Discover shortest join path between tables.
|
|
155
|
+
* **`:join <t1> <t2>`** — Explain join cardinality and recommended join syntax.
|
|
156
|
+
* **`:mode <SAFE|CONFIRM|SANDBOX|ADMIN>`** — Switch safety mode mid-session.
|
|
157
|
+
* **`:changes`** — List recent write ChangeSets.
|
|
158
|
+
* **`:tables`** — List all database tables.
|
|
159
|
+
* **`:table <name>`** — Inspect columns, types, primary keys, and sample data.
|
|
160
|
+
* **`:fk <name>`** — View incoming and outgoing foreign keys.
|
|
161
|
+
* **`:provider <name>`** — Switch AI provider (`ollama`, `gemini`, `groq`, `openrouter`).
|
|
162
|
+
* **`:model <name>`** — Switch active model name on the fly.
|
|
163
|
+
* **`:explain`** — Show generated SQL and query analysis for the last turn.
|
|
164
|
+
* **`:history`** — View query history for the session.
|
|
165
|
+
* **`:auto` / `:noauto`** — Toggle automatic execution of read queries.
|
|
166
|
+
* **`:run`** — Re-execute the last generated SQL query.
|
|
167
|
+
* **`:clear`** — Clear multi-turn conversational context.
|
|
168
|
+
* **`:export <file>`** — Save the last generated SQL to a file.
|
|
169
|
+
* **`:exit`** — Exit chat session.
|
|
170
|
+
|
|
171
|
+
---
|
|
172
|
+
|
|
173
|
+
## 📄 License
|
|
174
|
+
MIT License. Built with ❤️ by Santhosh Gupta.
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"""
|
|
2
|
+
AI Confidence Scoring Engine.
|
|
3
|
+
Computes confidence scores (0-100%) for generated SQL queries based on:
|
|
4
|
+
- Table name matching precision (exact vs fuzzy vs fallback)
|
|
5
|
+
- Foreign key relationship verification (verified FK path vs guessed join)
|
|
6
|
+
- Ambiguity indicators and business dictionary definitions
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from typing import Dict, Any, List, Optional
|
|
10
|
+
from dataclasses import dataclass, field
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@dataclass
|
|
14
|
+
class ConfidenceScore:
|
|
15
|
+
"""Confidence evaluation of an AI-generated query."""
|
|
16
|
+
score_percentage: int
|
|
17
|
+
level: str # HIGH (85-100%), MEDIUM (60-84%), LOW (<60%)
|
|
18
|
+
rationales: List[str] = field(default_factory=list)
|
|
19
|
+
caveats: List[str] = field(default_factory=list)
|
|
20
|
+
|
|
21
|
+
def format_summary(self) -> str:
|
|
22
|
+
color = "green" if self.score_percentage >= 85 else ("yellow" if self.score_percentage >= 60 else "red")
|
|
23
|
+
lines = [f"[{color}]Confidence: {self.score_percentage}% ({self.level})[/{color}]"]
|
|
24
|
+
for r in self.rationales:
|
|
25
|
+
lines.append(f" [dim]✓ {r}[/dim]")
|
|
26
|
+
for c in self.caveats:
|
|
27
|
+
lines.append(f" [yellow]⚠ {c}[/yellow]")
|
|
28
|
+
return "\n".join(lines)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class ConfidenceEvaluator:
|
|
32
|
+
"""
|
|
33
|
+
Evaluates confidence score for a generated SQL statement.
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
@classmethod
|
|
37
|
+
def evaluate(
|
|
38
|
+
self,
|
|
39
|
+
user_prompt: str,
|
|
40
|
+
sql: str,
|
|
41
|
+
exact_tables: List[str],
|
|
42
|
+
fuzzy_tables: List[str],
|
|
43
|
+
used_known_fk: bool = True,
|
|
44
|
+
has_business_definition: bool = False,
|
|
45
|
+
retries: int = 0,
|
|
46
|
+
) -> ConfidenceScore:
|
|
47
|
+
score = 100
|
|
48
|
+
rationales = []
|
|
49
|
+
caveats = []
|
|
50
|
+
|
|
51
|
+
# Table matching precision
|
|
52
|
+
if exact_tables:
|
|
53
|
+
rationales.append(f"Exact table match verified: {', '.join(exact_tables)}")
|
|
54
|
+
elif fuzzy_tables:
|
|
55
|
+
score -= 15
|
|
56
|
+
caveats.append(f"Fuzzy table name match: {', '.join(fuzzy_tables)}")
|
|
57
|
+
else:
|
|
58
|
+
score -= 30
|
|
59
|
+
caveats.append("No direct table name mentioned; inferred from schema keywords")
|
|
60
|
+
|
|
61
|
+
# Join relationship certainty
|
|
62
|
+
if "JOIN" in sql.upper():
|
|
63
|
+
if used_known_fk:
|
|
64
|
+
rationales.append("Verified foreign key relationship path used for JOINs")
|
|
65
|
+
else:
|
|
66
|
+
score -= 20
|
|
67
|
+
caveats.append("JOIN condition inferred without explicit foreign key constraint")
|
|
68
|
+
|
|
69
|
+
# Business definition
|
|
70
|
+
if has_business_definition:
|
|
71
|
+
rationales.append("Applied explicit business dictionary definition")
|
|
72
|
+
|
|
73
|
+
# Retries penalty
|
|
74
|
+
if retries > 0:
|
|
75
|
+
score -= (retries * 10)
|
|
76
|
+
caveats.append(f"Required {retries} automatic correction retry(ies)")
|
|
77
|
+
|
|
78
|
+
score = max(20, min(100, score))
|
|
79
|
+
level = "HIGH" if score >= 85 else ("MEDIUM" if score >= 60 else "LOW")
|
|
80
|
+
|
|
81
|
+
return ConfidenceScore(
|
|
82
|
+
score_percentage=score,
|
|
83
|
+
level=level,
|
|
84
|
+
rationales=rationales,
|
|
85
|
+
caveats=caveats,
|
|
86
|
+
)
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Multi-Turn Conversational Context Tracker.
|
|
3
|
+
Maintains active tables, filter predicates, entities, and sort orders across chat turns
|
|
4
|
+
so users can refine queries naturally (e.g. 'Which sites?', 'Only Hyderabad', 'Top 10?').
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from typing import Dict, Any, List, Optional, Set
|
|
8
|
+
from dataclasses import dataclass, field
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass
|
|
12
|
+
class ConversationTurn:
|
|
13
|
+
"""A single turn in the conversation."""
|
|
14
|
+
user_prompt: str
|
|
15
|
+
sql: str
|
|
16
|
+
tables: List[str] = field(default_factory=list)
|
|
17
|
+
filters: List[str] = field(default_factory=list)
|
|
18
|
+
entities: Dict[str, str] = field(default_factory=dict)
|
|
19
|
+
limit: Optional[int] = None
|
|
20
|
+
is_chat: bool = False
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class ConversationContext:
|
|
24
|
+
"""
|
|
25
|
+
Tracks state across interactive chat turns.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
def __init__(self, max_history: int = 10):
|
|
29
|
+
self.max_history = max_history
|
|
30
|
+
self.turns: List[ConversationTurn] = []
|
|
31
|
+
self.active_tables: Set[str] = set()
|
|
32
|
+
self.active_filters: List[str] = []
|
|
33
|
+
|
|
34
|
+
def add_turn(
|
|
35
|
+
self,
|
|
36
|
+
user_prompt: str,
|
|
37
|
+
sql: str,
|
|
38
|
+
tables: Optional[List[str]] = None,
|
|
39
|
+
filters: Optional[List[str]] = None,
|
|
40
|
+
is_chat: bool = False,
|
|
41
|
+
) -> None:
|
|
42
|
+
"""Records a completed turn and updates active context."""
|
|
43
|
+
turn = ConversationTurn(
|
|
44
|
+
user_prompt=user_prompt,
|
|
45
|
+
sql=sql,
|
|
46
|
+
tables=tables or [],
|
|
47
|
+
filters=filters or [],
|
|
48
|
+
is_chat=is_chat,
|
|
49
|
+
)
|
|
50
|
+
self.turns.append(turn)
|
|
51
|
+
if len(self.turns) > self.max_history:
|
|
52
|
+
self.turns.pop(0)
|
|
53
|
+
|
|
54
|
+
if tables and not is_chat:
|
|
55
|
+
self.active_tables.update(t.lower() for t in tables)
|
|
56
|
+
|
|
57
|
+
def get_last_turn(self) -> Optional[ConversationTurn]:
|
|
58
|
+
if self.turns:
|
|
59
|
+
return self.turns[-1]
|
|
60
|
+
return None
|
|
61
|
+
|
|
62
|
+
def format_context_for_prompt(self) -> str:
|
|
63
|
+
"""Formats recent context for inclusion in LLM prompt."""
|
|
64
|
+
if not self.turns:
|
|
65
|
+
return ""
|
|
66
|
+
|
|
67
|
+
recent_valid = [t for t in self.turns[-3:] if t.sql and not t.is_chat]
|
|
68
|
+
if not recent_valid:
|
|
69
|
+
return ""
|
|
70
|
+
|
|
71
|
+
lines = ["Previous Conversation Context (Use for follow-up refinements):"]
|
|
72
|
+
for i, t in enumerate(recent_valid, 1):
|
|
73
|
+
lines.append(f" {i}. User: \"{t.user_prompt}\"")
|
|
74
|
+
lines.append(f" Last SQL: `{t.sql.strip()}`")
|
|
75
|
+
|
|
76
|
+
return "\n".join(lines)
|
|
77
|
+
|
|
78
|
+
def clear(self) -> None:
|
|
79
|
+
"""Resets the conversation history."""
|
|
80
|
+
self.turns.clear()
|
|
81
|
+
self.active_tables.clear()
|
|
82
|
+
self.active_filters.clear()
|