dbagent-cli 0.4.0__tar.gz → 0.5.0__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.5.0/PKG-INFO +281 -0
- dbagent_cli-0.5.0/README.md +257 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/__init__.py +1 -1
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/agent/generator.py +14 -5
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/cli.py +120 -18
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/llm/gemini_provider.py +10 -2
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/ui/console.py +17 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/ui/grid_window.py +12 -3
- dbagent_cli-0.5.0/dbagent_cli.egg-info/PKG-INFO +281 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/pyproject.toml +2 -2
- dbagent_cli-0.4.0/PKG-INFO +0 -198
- dbagent_cli-0.4.0/README.md +0 -174
- dbagent_cli-0.4.0/dbagent_cli.egg-info/PKG-INFO +0 -198
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/agent/confidence.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/agent/conversation.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/agent/doctor.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/agent/error_classifier.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/agent/pipeline.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/agent/recommender.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/agent/validator.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/config.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/connectors/base.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/connectors/factory.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/connectors/mongo.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/connectors/relational.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/execution/__init__.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/execution/cache.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/execution/changeset.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/execution/cost.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/execution/recovery.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/execution/sandbox.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/execution/streaming.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/llm/auto_setup.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/llm/base.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/llm/factory.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/llm/groq_provider.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/llm/mock_provider.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/llm/ollama_provider.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/llm/openrouter_provider.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/schema/cache.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/schema/formatter.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/schema/graph.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/schema/models.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/schema/retriever.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/schema/selector.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/security/__init__.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/security/audit.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/security/environment.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/security/masking.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/security/modes.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/security/rbac.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/semantic/__init__.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/semantic/dictionary.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/semantic/templates.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/sql/__init__.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/sql/optimizer.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/sql/validator.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/ui/viewer.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent/ui/visualizer.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent_cli.egg-info/SOURCES.txt +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent_cli.egg-info/dependency_links.txt +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent_cli.egg-info/entry_points.txt +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent_cli.egg-info/requires.txt +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/dbagent_cli.egg-info/top_level.txt +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/setup.cfg +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/tests/test_cache.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/tests/test_cli.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/tests/test_connectors.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/tests/test_doctor_and_viz.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/tests/test_generator.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/tests/test_graph.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/tests/test_recovery.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/tests/test_safety.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/tests/test_sandbox.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/tests/test_schema.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/tests/test_security.py +0 -0
- {dbagent_cli-0.4.0 → dbagent_cli-0.5.0}/tests/test_semantic.py +0 -0
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: dbagent-cli
|
|
3
|
+
Version: 0.5.0
|
|
4
|
+
Summary: Enterprise AI Database Copilot, Schema Graph Introspector & Autonomous Recovery Agent (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 (`dbagent-cli`)
|
|
26
|
+
|
|
27
|
+
> **Enterprise AI Database Copilot, Schema Graph Introspector & Autonomous Recovery Agent**
|
|
28
|
+
> *Author: Santhosh Gupta | 100% Free, Standalone, Works 100% Offline (Ollama) or with High-Speed 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
|
+
[](https://pypi.org/project/dbagent-cli/)
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## 🌟 What is DB-Agent?
|
|
38
|
+
|
|
39
|
+
**DB-Agent** (`dbagent-cli`) is an enterprise-grade, privacy-first AI Database Copilot. Designed for developers, DBAs, data engineers, and analysts, it transforms plain English prompts into highly optimized, dialect-precise SQL, executes queries under strict safety guardrails, and renders crystal-clear data visualizations.
|
|
40
|
+
|
|
41
|
+
### 🚀 Key Highlights:
|
|
42
|
+
* **Instant On-Demand Introspection**: Targeted single-query metadata resolution (< 30ms) across databases with 300+ tables.
|
|
43
|
+
* **Interactive Data Grid Suite**: Native desktop GUI window (`:window`), browser virtual grid (`:web`), column paging (`:page`), and responsive zoom-out (`:wide`).
|
|
44
|
+
* **Long-Term Post-Commit Rollback**: Pluggable recovery engine that snapshots pre-write row states, detects concurrency conflicts, and allows safe revert hours or days after `COMMIT`.
|
|
45
|
+
* **Multi-Hop Join Discovery**: Automatically computes the shortest foreign-key join paths between disconnected tables.
|
|
46
|
+
* **Enterprise Safety Guardrails**: 5 operational modes (`SAFE`, `READ_ONLY`, `CONFIRM`, `SANDBOX`, `ADMIN`), PII masking, and destructive query protection.
|
|
47
|
+
* **Zero-Cost Local or Cloud AI**: Native support for **Google Gemini** (`gemini-3.6-flash`), **Local Ollama** (100% offline), **Groq**, and **OpenRouter**.
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
## 📦 Installation
|
|
52
|
+
|
|
53
|
+
Install `dbagent-cli` via pip:
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
# Recommended: Install with all database drivers
|
|
57
|
+
pip install --upgrade "dbagent-cli[all]"
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Optional Driver Extras:
|
|
61
|
+
```bash
|
|
62
|
+
pip install "dbagent-cli[postgres]" # PostgreSQL (psycopg2-binary, asyncpg)
|
|
63
|
+
pip install "dbagent-cli[mysql]" # MySQL & MariaDB (pymysql, cryptography)
|
|
64
|
+
pip install "dbagent-cli[mongo]" # MongoDB (pymongo)
|
|
65
|
+
pip install "dbagent-cli[duckdb]" # DuckDB (duckdb, duckdb-engine)
|
|
66
|
+
pip install "dbagent-cli[mssql]" # Microsoft SQL Server (pyodbc)
|
|
67
|
+
pip install "dbagent-cli[oracle]" # Oracle Database (oracledb)
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
## ⚡ 2-Minute Quickstart
|
|
73
|
+
|
|
74
|
+
### 1. Configure AI Provider
|
|
75
|
+
```bash
|
|
76
|
+
db-agent setup
|
|
77
|
+
```
|
|
78
|
+
Follow the interactive prompt to choose your preferred AI backend:
|
|
79
|
+
* **Google Gemini (Recommended Cloud)**: Ultra-fast inference with free tier (`gemini-3.6-flash`).
|
|
80
|
+
* **Local Ollama (Recommended Offline)**: 100% private, runs entirely on your GPU/CPU with 0 API keys.
|
|
81
|
+
* **Groq / OpenRouter**: Fast open-weights inference (Llama 3.3, Qwen 2.5 Coder).
|
|
82
|
+
|
|
83
|
+
### 2. Connect Database & Save Profile Alias
|
|
84
|
+
```bash
|
|
85
|
+
db-agent connect postgresql://user:pass@localhost:5432/mydb --alias my_db
|
|
86
|
+
```
|
|
87
|
+
*(Supports PostgreSQL, MySQL, SQLite, MSSQL, Oracle, DuckDB, and MongoDB).*
|
|
88
|
+
|
|
89
|
+
### 3. Ask Plain English Questions or Start Interactive Chat
|
|
90
|
+
```bash
|
|
91
|
+
# Ask a one-off question:
|
|
92
|
+
db-agent ask "list top 10 most recent active users" --db my_db
|
|
93
|
+
|
|
94
|
+
# Launch the interactive AI shell:
|
|
95
|
+
db-agent chat --db my_db
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
---
|
|
99
|
+
|
|
100
|
+
## 🪟 Interactive Viewing & Multi-Column Scroller Suite
|
|
101
|
+
|
|
102
|
+
When working with wide enterprise tables containing 20, 40, or 60+ columns, DB-Agent provides 5 flexible viewing modes:
|
|
103
|
+
|
|
104
|
+
| View Mode | In-Chat Command | Description |
|
|
105
|
+
|---|---|---|
|
|
106
|
+
| **Desktop GUI Grid** | `:window` / `:grid` | Opens a native desktop window with ↔️ horizontal & ↕️ vertical scrollbars, live search/filter, and CSV export. |
|
|
107
|
+
| **Browser Data Table** | `:web` / `:browser` | Generates a standalone virtual data grid and opens it in your default browser. |
|
|
108
|
+
| **Column Paging** | `:page <n>` | Browse wide columns in clean horizontal chunks (`:page 1`, `:page 2`, `:page 3`). |
|
|
109
|
+
| **Vertical Card View** | `:card` / `:v` | Displays every row as an expanded vertical record card with all columns cleanly listed. |
|
|
110
|
+
| **Responsive Zoom-Out** | `:wide` | Dynamically auto-scales column count to fill wide or zoomed-out (`Ctrl` + `-`) terminal windows. |
|
|
111
|
+
|
|
112
|
+
---
|
|
113
|
+
|
|
114
|
+
## 🛡️ Enterprise Long-Term Rollback & Recovery Engine
|
|
115
|
+
|
|
116
|
+
Unlike standard transaction rollbacks that only protect uncommitted sessions, DB-Agent includes a **Post-Commit Long-Term Recovery Engine**:
|
|
117
|
+
|
|
118
|
+
```text
|
|
119
|
+
User: Update all inactive users to ACTIVE where last_login < '2025-01-01'
|
|
120
|
+
AI: Modifying 42 rows. Capturing pre-write snapshot...
|
|
121
|
+
Change committed successfully.
|
|
122
|
+
Change ID: CS-20260828-001 (Recovery available for 30 days)
|
|
123
|
+
|
|
124
|
+
... 5 days later ...
|
|
125
|
+
|
|
126
|
+
User: db-agent rollback CS-20260828-001 --db my_db
|
|
127
|
+
AI: Verifying live data checksums... No concurrency conflicts detected.
|
|
128
|
+
Inverse UPDATE executed successfully. 42 rows restored!
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Recovery CLI Commands:
|
|
132
|
+
```bash
|
|
133
|
+
# List all recorded write ChangeSets:
|
|
134
|
+
db-agent changes
|
|
135
|
+
|
|
136
|
+
# Inspect before/after row snapshots of a change:
|
|
137
|
+
db-agent changes-show CS-20260828-001
|
|
138
|
+
|
|
139
|
+
# Preview rollback SQL without executing:
|
|
140
|
+
db-agent rollback CS-20260828-001 --preview
|
|
141
|
+
|
|
142
|
+
# Execute rollback with conflict safety:
|
|
143
|
+
db-agent rollback CS-20260828-001 --conflict-policy abort
|
|
144
|
+
|
|
145
|
+
# Roll back the most recent change:
|
|
146
|
+
db-agent rollback --last
|
|
147
|
+
|
|
148
|
+
# Manage recovery storage retention:
|
|
149
|
+
db-agent recovery status
|
|
150
|
+
db-agent recovery cleanup --older-than-days 30
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
---
|
|
154
|
+
|
|
155
|
+
## 🔒 Production Safety Guardrails & Operational Modes
|
|
156
|
+
|
|
157
|
+
DB-Agent provides 5 strictly enforced operational safety modes:
|
|
158
|
+
|
|
159
|
+
* **`SAFE`**: Only read-only queries (`SELECT`, `SHOW`, `DESCRIBE`, `EXPLAIN`, `WITH`) allowed. All writes and DDL are blocked.
|
|
160
|
+
* **`READ_ONLY`**: Enforced on `PRODUCTION` environments with query execution timeouts and row limits.
|
|
161
|
+
* **`CONFIRM`** *(Default)*: Read queries execute automatically; write (`UPDATE`, `INSERT`, `DELETE`) and DDL (`ALTER`, `DROP`) operations require user confirmation.
|
|
162
|
+
* **`SANDBOX`**: Executes writes within an uncommitted transaction to generate before/after diffs without modifying live data.
|
|
163
|
+
* **`ADMIN`**: Full administrative execution with audit tracking.
|
|
164
|
+
|
|
165
|
+
### Sensitive Data Masking:
|
|
166
|
+
Confidential fields (emails, passwords, API tokens, credit cards) are automatically masked in console outputs:
|
|
167
|
+
`a***@motivitylabs.com`, `**********`, `****-****-****-1234`.
|
|
168
|
+
|
|
169
|
+
---
|
|
170
|
+
|
|
171
|
+
## 🧠 Schema Graph & Multi-Hop JOIN Discovery
|
|
172
|
+
|
|
173
|
+
DB-Agent builds an in-memory directed graph of foreign-key relationships across your database schema:
|
|
174
|
+
|
|
175
|
+
```bash
|
|
176
|
+
# Discover shortest foreign-key path between disconnected tables:
|
|
177
|
+
db-agent path ivis_user ivis_camera_mapping --db my_db
|
|
178
|
+
|
|
179
|
+
# Explain join cardinality (1:1, 1:N) and recommended syntax:
|
|
180
|
+
db-agent explain-join customers orders --db my_db
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
---
|
|
184
|
+
|
|
185
|
+
## 🏥 Database Doctor & Query Diagnostics
|
|
186
|
+
|
|
187
|
+
Run comprehensive database health checks and EXPLAIN plan optimizations:
|
|
188
|
+
|
|
189
|
+
```bash
|
|
190
|
+
# Full connectivity, latency, index, and table health diagnostics:
|
|
191
|
+
db-agent doctor --db my_db
|
|
192
|
+
|
|
193
|
+
# Analyze query execution plan and recommend missing indexes:
|
|
194
|
+
db-agent optimize "SELECT * FROM ivis_user WHERE email = 'test@example.com'" --db my_db
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
---
|
|
198
|
+
|
|
199
|
+
## 📚 Business Semantic Dictionary
|
|
200
|
+
|
|
201
|
+
Teach DB-Agent your domain-specific metrics and shorthand:
|
|
202
|
+
|
|
203
|
+
```bash
|
|
204
|
+
# Define a business metric:
|
|
205
|
+
db-agent define active_user "A user who logged in within the last 30 days" --sql "last_login >= NOW() - INTERVAL '30 days'"
|
|
206
|
+
|
|
207
|
+
# List all saved business terminology:
|
|
208
|
+
db-agent definitions
|
|
209
|
+
|
|
210
|
+
# Manage parameterized query templates:
|
|
211
|
+
db-agent template list
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
---
|
|
215
|
+
|
|
216
|
+
## 📖 Complete CLI Command Reference
|
|
217
|
+
|
|
218
|
+
| Command | Usage | Description |
|
|
219
|
+
|---|---|---|
|
|
220
|
+
| **`ask`** | `db-agent ask "<prompt>" --db <alias>` | Translates natural language to SQL, executes query, and renders table. |
|
|
221
|
+
| **`chat`** | `db-agent chat --db <alias>` | Launches interactive multi-turn AI copilot shell. |
|
|
222
|
+
| **`doctor`** | `db-agent doctor --db <alias>` | Runs database health, connectivity, and index diagnostics. |
|
|
223
|
+
| **`path`** | `db-agent path <t1> <t2> --db <alias>` | Discovers shortest multi-hop foreign-key join path. |
|
|
224
|
+
| **`explain-join`** | `db-agent explain-join <t1> <t2>` | Explains join relationship, cardinality, and syntax. |
|
|
225
|
+
| **`optimize`** | `db-agent optimize "<SQL>" --db <alias>` | Analyzes EXPLAIN plan and recommends performance indexes. |
|
|
226
|
+
| **`scan`** | `db-agent scan --db <alias> -o schema.md` | Full database introspection and Markdown catalog export. |
|
|
227
|
+
| **`refresh-schema`** | `db-agent refresh-schema --db <alias>` | Refreshes schema cache and reports detected diffs. |
|
|
228
|
+
| **`schema-status`** | `db-agent schema-status --db <alias>` | Displays schema cache age, version, and health. |
|
|
229
|
+
| **`changes`** | `db-agent changes` | Lists recorded write ChangeSets. |
|
|
230
|
+
| **`changes-show`** | `db-agent changes-show <id>` | Shows before/after snapshot of a ChangeSet. |
|
|
231
|
+
| **`rollback`** | `db-agent rollback <id> [--preview] [--last]` | Generates and executes inverse rollback SQL. |
|
|
232
|
+
| **`recovery`** | `db-agent recovery [list\|status\|cleanup]` | Manages snapshot storage and retention policies. |
|
|
233
|
+
| **`define`** | `db-agent define <term> "<desc>" --sql "<sql>"` | Saves business terminology into semantic dictionary. |
|
|
234
|
+
| **`definitions`** | `db-agent definitions` | Lists all defined business terminology. |
|
|
235
|
+
| **`template`** | `db-agent template [list\|add\|run]` | Manages reusable, parameterized query templates. |
|
|
236
|
+
| **`audit`** | `db-agent audit` | Displays recent query execution audit log. |
|
|
237
|
+
| **`connect`** | `db-agent connect <url> --alias <name>` | Tests database connection and saves profile. |
|
|
238
|
+
| **`profiles`** | `db-agent profiles` | Lists all configured database profiles. |
|
|
239
|
+
| **`use`** | `db-agent use <provider>` | Switches active AI provider (`gemini`, `ollama`, `groq`, `openrouter`). |
|
|
240
|
+
| **`models`** | `db-agent models` | Status of local Ollama models and cloud AI providers. |
|
|
241
|
+
| **`setup`** | `db-agent setup` | 1-Click AI configuration setup wizard. |
|
|
242
|
+
| **`config`** | `db-agent config` | Interactive AI API keys and preferences wizard. |
|
|
243
|
+
| **`guide`** | `db-agent guide` | Displays full interactive command cheatsheet. |
|
|
244
|
+
|
|
245
|
+
---
|
|
246
|
+
|
|
247
|
+
## 💬 In-Chat Colon Commands (`db-agent chat`)
|
|
248
|
+
|
|
249
|
+
Inside the interactive chat REPL, use these colon commands:
|
|
250
|
+
|
|
251
|
+
```text
|
|
252
|
+
:window -> Open native desktop GUI data grid with horizontal & vertical scrollbars
|
|
253
|
+
:web -> Open interactive browser data table with virtual scrolling
|
|
254
|
+
:page <n> -> View column page <n> for wide tables (e.g. :page 1, :page 2)
|
|
255
|
+
:card -> View last result in expanded vertical card format (all columns)
|
|
256
|
+
:wide -> Render all columns across expanded / zoomed-out terminal
|
|
257
|
+
:scroll -> Interactive terminal column scroller
|
|
258
|
+
:tables -> List all tables in this database
|
|
259
|
+
:table <name> -> Inspect columns, PKs, types & sample data
|
|
260
|
+
:fk <name> -> View mapped foreign keys & related tables
|
|
261
|
+
:path <t1> <t2> -> Discover shortest join path between tables
|
|
262
|
+
:join <t1> <t2> -> Explain join cardinality & syntax
|
|
263
|
+
:doctor -> Run database health diagnostics
|
|
264
|
+
:mode <name> -> Switch mode (SAFE, READ_ONLY, CONFIRM, SANDBOX, ADMIN)
|
|
265
|
+
:changes -> List recent write ChangeSets
|
|
266
|
+
:provider <name> -> Switch AI provider (gemini, ollama, groq, openrouter)
|
|
267
|
+
:model <name> -> Switch active model name (e.g. gemini-3.6-flash)
|
|
268
|
+
:models -> Show active AI provider and model status
|
|
269
|
+
:explain -> Show generated SQL & query analysis for last turn
|
|
270
|
+
:history -> View history of queries in this session
|
|
271
|
+
:auto / :noauto -> Toggle automatic execution of read queries
|
|
272
|
+
:run -> Re-execute the last generated SQL query
|
|
273
|
+
:clear -> Clear conversation context
|
|
274
|
+
:export <file> -> Save the last generated SQL to a file
|
|
275
|
+
:exit -> Exit chat session
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
---
|
|
279
|
+
|
|
280
|
+
## 📄 License
|
|
281
|
+
MIT License. Built with ❤️ by **Santhosh Gupta**.
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
# ⚡ DB-Agent (`dbagent-cli`)
|
|
2
|
+
|
|
3
|
+
> **Enterprise AI Database Copilot, Schema Graph Introspector & Autonomous Recovery Agent**
|
|
4
|
+
> *Author: Santhosh Gupta | 100% Free, Standalone, Works 100% Offline (Ollama) or with High-Speed 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
|
+
[](https://pypi.org/project/dbagent-cli/)
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## 🌟 What is DB-Agent?
|
|
14
|
+
|
|
15
|
+
**DB-Agent** (`dbagent-cli`) is an enterprise-grade, privacy-first AI Database Copilot. Designed for developers, DBAs, data engineers, and analysts, it transforms plain English prompts into highly optimized, dialect-precise SQL, executes queries under strict safety guardrails, and renders crystal-clear data visualizations.
|
|
16
|
+
|
|
17
|
+
### 🚀 Key Highlights:
|
|
18
|
+
* **Instant On-Demand Introspection**: Targeted single-query metadata resolution (< 30ms) across databases with 300+ tables.
|
|
19
|
+
* **Interactive Data Grid Suite**: Native desktop GUI window (`:window`), browser virtual grid (`:web`), column paging (`:page`), and responsive zoom-out (`:wide`).
|
|
20
|
+
* **Long-Term Post-Commit Rollback**: Pluggable recovery engine that snapshots pre-write row states, detects concurrency conflicts, and allows safe revert hours or days after `COMMIT`.
|
|
21
|
+
* **Multi-Hop Join Discovery**: Automatically computes the shortest foreign-key join paths between disconnected tables.
|
|
22
|
+
* **Enterprise Safety Guardrails**: 5 operational modes (`SAFE`, `READ_ONLY`, `CONFIRM`, `SANDBOX`, `ADMIN`), PII masking, and destructive query protection.
|
|
23
|
+
* **Zero-Cost Local or Cloud AI**: Native support for **Google Gemini** (`gemini-3.6-flash`), **Local Ollama** (100% offline), **Groq**, and **OpenRouter**.
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
## 📦 Installation
|
|
28
|
+
|
|
29
|
+
Install `dbagent-cli` via pip:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
# Recommended: Install with all database drivers
|
|
33
|
+
pip install --upgrade "dbagent-cli[all]"
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### Optional Driver Extras:
|
|
37
|
+
```bash
|
|
38
|
+
pip install "dbagent-cli[postgres]" # PostgreSQL (psycopg2-binary, asyncpg)
|
|
39
|
+
pip install "dbagent-cli[mysql]" # MySQL & MariaDB (pymysql, cryptography)
|
|
40
|
+
pip install "dbagent-cli[mongo]" # MongoDB (pymongo)
|
|
41
|
+
pip install "dbagent-cli[duckdb]" # DuckDB (duckdb, duckdb-engine)
|
|
42
|
+
pip install "dbagent-cli[mssql]" # Microsoft SQL Server (pyodbc)
|
|
43
|
+
pip install "dbagent-cli[oracle]" # Oracle Database (oracledb)
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
## ⚡ 2-Minute Quickstart
|
|
49
|
+
|
|
50
|
+
### 1. Configure AI Provider
|
|
51
|
+
```bash
|
|
52
|
+
db-agent setup
|
|
53
|
+
```
|
|
54
|
+
Follow the interactive prompt to choose your preferred AI backend:
|
|
55
|
+
* **Google Gemini (Recommended Cloud)**: Ultra-fast inference with free tier (`gemini-3.6-flash`).
|
|
56
|
+
* **Local Ollama (Recommended Offline)**: 100% private, runs entirely on your GPU/CPU with 0 API keys.
|
|
57
|
+
* **Groq / OpenRouter**: Fast open-weights inference (Llama 3.3, Qwen 2.5 Coder).
|
|
58
|
+
|
|
59
|
+
### 2. Connect Database & Save Profile Alias
|
|
60
|
+
```bash
|
|
61
|
+
db-agent connect postgresql://user:pass@localhost:5432/mydb --alias my_db
|
|
62
|
+
```
|
|
63
|
+
*(Supports PostgreSQL, MySQL, SQLite, MSSQL, Oracle, DuckDB, and MongoDB).*
|
|
64
|
+
|
|
65
|
+
### 3. Ask Plain English Questions or Start Interactive Chat
|
|
66
|
+
```bash
|
|
67
|
+
# Ask a one-off question:
|
|
68
|
+
db-agent ask "list top 10 most recent active users" --db my_db
|
|
69
|
+
|
|
70
|
+
# Launch the interactive AI shell:
|
|
71
|
+
db-agent chat --db my_db
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
## 🪟 Interactive Viewing & Multi-Column Scroller Suite
|
|
77
|
+
|
|
78
|
+
When working with wide enterprise tables containing 20, 40, or 60+ columns, DB-Agent provides 5 flexible viewing modes:
|
|
79
|
+
|
|
80
|
+
| View Mode | In-Chat Command | Description |
|
|
81
|
+
|---|---|---|
|
|
82
|
+
| **Desktop GUI Grid** | `:window` / `:grid` | Opens a native desktop window with ↔️ horizontal & ↕️ vertical scrollbars, live search/filter, and CSV export. |
|
|
83
|
+
| **Browser Data Table** | `:web` / `:browser` | Generates a standalone virtual data grid and opens it in your default browser. |
|
|
84
|
+
| **Column Paging** | `:page <n>` | Browse wide columns in clean horizontal chunks (`:page 1`, `:page 2`, `:page 3`). |
|
|
85
|
+
| **Vertical Card View** | `:card` / `:v` | Displays every row as an expanded vertical record card with all columns cleanly listed. |
|
|
86
|
+
| **Responsive Zoom-Out** | `:wide` | Dynamically auto-scales column count to fill wide or zoomed-out (`Ctrl` + `-`) terminal windows. |
|
|
87
|
+
|
|
88
|
+
---
|
|
89
|
+
|
|
90
|
+
## 🛡️ Enterprise Long-Term Rollback & Recovery Engine
|
|
91
|
+
|
|
92
|
+
Unlike standard transaction rollbacks that only protect uncommitted sessions, DB-Agent includes a **Post-Commit Long-Term Recovery Engine**:
|
|
93
|
+
|
|
94
|
+
```text
|
|
95
|
+
User: Update all inactive users to ACTIVE where last_login < '2025-01-01'
|
|
96
|
+
AI: Modifying 42 rows. Capturing pre-write snapshot...
|
|
97
|
+
Change committed successfully.
|
|
98
|
+
Change ID: CS-20260828-001 (Recovery available for 30 days)
|
|
99
|
+
|
|
100
|
+
... 5 days later ...
|
|
101
|
+
|
|
102
|
+
User: db-agent rollback CS-20260828-001 --db my_db
|
|
103
|
+
AI: Verifying live data checksums... No concurrency conflicts detected.
|
|
104
|
+
Inverse UPDATE executed successfully. 42 rows restored!
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Recovery CLI Commands:
|
|
108
|
+
```bash
|
|
109
|
+
# List all recorded write ChangeSets:
|
|
110
|
+
db-agent changes
|
|
111
|
+
|
|
112
|
+
# Inspect before/after row snapshots of a change:
|
|
113
|
+
db-agent changes-show CS-20260828-001
|
|
114
|
+
|
|
115
|
+
# Preview rollback SQL without executing:
|
|
116
|
+
db-agent rollback CS-20260828-001 --preview
|
|
117
|
+
|
|
118
|
+
# Execute rollback with conflict safety:
|
|
119
|
+
db-agent rollback CS-20260828-001 --conflict-policy abort
|
|
120
|
+
|
|
121
|
+
# Roll back the most recent change:
|
|
122
|
+
db-agent rollback --last
|
|
123
|
+
|
|
124
|
+
# Manage recovery storage retention:
|
|
125
|
+
db-agent recovery status
|
|
126
|
+
db-agent recovery cleanup --older-than-days 30
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
---
|
|
130
|
+
|
|
131
|
+
## 🔒 Production Safety Guardrails & Operational Modes
|
|
132
|
+
|
|
133
|
+
DB-Agent provides 5 strictly enforced operational safety modes:
|
|
134
|
+
|
|
135
|
+
* **`SAFE`**: Only read-only queries (`SELECT`, `SHOW`, `DESCRIBE`, `EXPLAIN`, `WITH`) allowed. All writes and DDL are blocked.
|
|
136
|
+
* **`READ_ONLY`**: Enforced on `PRODUCTION` environments with query execution timeouts and row limits.
|
|
137
|
+
* **`CONFIRM`** *(Default)*: Read queries execute automatically; write (`UPDATE`, `INSERT`, `DELETE`) and DDL (`ALTER`, `DROP`) operations require user confirmation.
|
|
138
|
+
* **`SANDBOX`**: Executes writes within an uncommitted transaction to generate before/after diffs without modifying live data.
|
|
139
|
+
* **`ADMIN`**: Full administrative execution with audit tracking.
|
|
140
|
+
|
|
141
|
+
### Sensitive Data Masking:
|
|
142
|
+
Confidential fields (emails, passwords, API tokens, credit cards) are automatically masked in console outputs:
|
|
143
|
+
`a***@motivitylabs.com`, `**********`, `****-****-****-1234`.
|
|
144
|
+
|
|
145
|
+
---
|
|
146
|
+
|
|
147
|
+
## 🧠 Schema Graph & Multi-Hop JOIN Discovery
|
|
148
|
+
|
|
149
|
+
DB-Agent builds an in-memory directed graph of foreign-key relationships across your database schema:
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
# Discover shortest foreign-key path between disconnected tables:
|
|
153
|
+
db-agent path ivis_user ivis_camera_mapping --db my_db
|
|
154
|
+
|
|
155
|
+
# Explain join cardinality (1:1, 1:N) and recommended syntax:
|
|
156
|
+
db-agent explain-join customers orders --db my_db
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
---
|
|
160
|
+
|
|
161
|
+
## 🏥 Database Doctor & Query Diagnostics
|
|
162
|
+
|
|
163
|
+
Run comprehensive database health checks and EXPLAIN plan optimizations:
|
|
164
|
+
|
|
165
|
+
```bash
|
|
166
|
+
# Full connectivity, latency, index, and table health diagnostics:
|
|
167
|
+
db-agent doctor --db my_db
|
|
168
|
+
|
|
169
|
+
# Analyze query execution plan and recommend missing indexes:
|
|
170
|
+
db-agent optimize "SELECT * FROM ivis_user WHERE email = 'test@example.com'" --db my_db
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
---
|
|
174
|
+
|
|
175
|
+
## 📚 Business Semantic Dictionary
|
|
176
|
+
|
|
177
|
+
Teach DB-Agent your domain-specific metrics and shorthand:
|
|
178
|
+
|
|
179
|
+
```bash
|
|
180
|
+
# Define a business metric:
|
|
181
|
+
db-agent define active_user "A user who logged in within the last 30 days" --sql "last_login >= NOW() - INTERVAL '30 days'"
|
|
182
|
+
|
|
183
|
+
# List all saved business terminology:
|
|
184
|
+
db-agent definitions
|
|
185
|
+
|
|
186
|
+
# Manage parameterized query templates:
|
|
187
|
+
db-agent template list
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
---
|
|
191
|
+
|
|
192
|
+
## 📖 Complete CLI Command Reference
|
|
193
|
+
|
|
194
|
+
| Command | Usage | Description |
|
|
195
|
+
|---|---|---|
|
|
196
|
+
| **`ask`** | `db-agent ask "<prompt>" --db <alias>` | Translates natural language to SQL, executes query, and renders table. |
|
|
197
|
+
| **`chat`** | `db-agent chat --db <alias>` | Launches interactive multi-turn AI copilot shell. |
|
|
198
|
+
| **`doctor`** | `db-agent doctor --db <alias>` | Runs database health, connectivity, and index diagnostics. |
|
|
199
|
+
| **`path`** | `db-agent path <t1> <t2> --db <alias>` | Discovers shortest multi-hop foreign-key join path. |
|
|
200
|
+
| **`explain-join`** | `db-agent explain-join <t1> <t2>` | Explains join relationship, cardinality, and syntax. |
|
|
201
|
+
| **`optimize`** | `db-agent optimize "<SQL>" --db <alias>` | Analyzes EXPLAIN plan and recommends performance indexes. |
|
|
202
|
+
| **`scan`** | `db-agent scan --db <alias> -o schema.md` | Full database introspection and Markdown catalog export. |
|
|
203
|
+
| **`refresh-schema`** | `db-agent refresh-schema --db <alias>` | Refreshes schema cache and reports detected diffs. |
|
|
204
|
+
| **`schema-status`** | `db-agent schema-status --db <alias>` | Displays schema cache age, version, and health. |
|
|
205
|
+
| **`changes`** | `db-agent changes` | Lists recorded write ChangeSets. |
|
|
206
|
+
| **`changes-show`** | `db-agent changes-show <id>` | Shows before/after snapshot of a ChangeSet. |
|
|
207
|
+
| **`rollback`** | `db-agent rollback <id> [--preview] [--last]` | Generates and executes inverse rollback SQL. |
|
|
208
|
+
| **`recovery`** | `db-agent recovery [list\|status\|cleanup]` | Manages snapshot storage and retention policies. |
|
|
209
|
+
| **`define`** | `db-agent define <term> "<desc>" --sql "<sql>"` | Saves business terminology into semantic dictionary. |
|
|
210
|
+
| **`definitions`** | `db-agent definitions` | Lists all defined business terminology. |
|
|
211
|
+
| **`template`** | `db-agent template [list\|add\|run]` | Manages reusable, parameterized query templates. |
|
|
212
|
+
| **`audit`** | `db-agent audit` | Displays recent query execution audit log. |
|
|
213
|
+
| **`connect`** | `db-agent connect <url> --alias <name>` | Tests database connection and saves profile. |
|
|
214
|
+
| **`profiles`** | `db-agent profiles` | Lists all configured database profiles. |
|
|
215
|
+
| **`use`** | `db-agent use <provider>` | Switches active AI provider (`gemini`, `ollama`, `groq`, `openrouter`). |
|
|
216
|
+
| **`models`** | `db-agent models` | Status of local Ollama models and cloud AI providers. |
|
|
217
|
+
| **`setup`** | `db-agent setup` | 1-Click AI configuration setup wizard. |
|
|
218
|
+
| **`config`** | `db-agent config` | Interactive AI API keys and preferences wizard. |
|
|
219
|
+
| **`guide`** | `db-agent guide` | Displays full interactive command cheatsheet. |
|
|
220
|
+
|
|
221
|
+
---
|
|
222
|
+
|
|
223
|
+
## 💬 In-Chat Colon Commands (`db-agent chat`)
|
|
224
|
+
|
|
225
|
+
Inside the interactive chat REPL, use these colon commands:
|
|
226
|
+
|
|
227
|
+
```text
|
|
228
|
+
:window -> Open native desktop GUI data grid with horizontal & vertical scrollbars
|
|
229
|
+
:web -> Open interactive browser data table with virtual scrolling
|
|
230
|
+
:page <n> -> View column page <n> for wide tables (e.g. :page 1, :page 2)
|
|
231
|
+
:card -> View last result in expanded vertical card format (all columns)
|
|
232
|
+
:wide -> Render all columns across expanded / zoomed-out terminal
|
|
233
|
+
:scroll -> Interactive terminal column scroller
|
|
234
|
+
:tables -> List all tables in this database
|
|
235
|
+
:table <name> -> Inspect columns, PKs, types & sample data
|
|
236
|
+
:fk <name> -> View mapped foreign keys & related tables
|
|
237
|
+
:path <t1> <t2> -> Discover shortest join path between tables
|
|
238
|
+
:join <t1> <t2> -> Explain join cardinality & syntax
|
|
239
|
+
:doctor -> Run database health diagnostics
|
|
240
|
+
:mode <name> -> Switch mode (SAFE, READ_ONLY, CONFIRM, SANDBOX, ADMIN)
|
|
241
|
+
:changes -> List recent write ChangeSets
|
|
242
|
+
:provider <name> -> Switch AI provider (gemini, ollama, groq, openrouter)
|
|
243
|
+
:model <name> -> Switch active model name (e.g. gemini-3.6-flash)
|
|
244
|
+
:models -> Show active AI provider and model status
|
|
245
|
+
:explain -> Show generated SQL & query analysis for last turn
|
|
246
|
+
:history -> View history of queries in this session
|
|
247
|
+
:auto / :noauto -> Toggle automatic execution of read queries
|
|
248
|
+
:run -> Re-execute the last generated SQL query
|
|
249
|
+
:clear -> Clear conversation context
|
|
250
|
+
:export <file> -> Save the last generated SQL to a file
|
|
251
|
+
:exit -> Exit chat session
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
---
|
|
255
|
+
|
|
256
|
+
## 📄 License
|
|
257
|
+
MIT License. Built with ❤️ by **Santhosh Gupta**.
|
|
@@ -132,16 +132,25 @@ class ScriptGenerator:
|
|
|
132
132
|
if kw in prompt_lower:
|
|
133
133
|
return "ddl"
|
|
134
134
|
|
|
135
|
-
# Write indicators
|
|
136
|
-
|
|
135
|
+
# Write indicators (word-boundary matching to avoid false positives like "show dataset")
|
|
136
|
+
write_phrases = [
|
|
137
137
|
"insert", "add record", "add row", "add new",
|
|
138
|
-
"update", "
|
|
139
|
-
"delete", "remove", "drop record",
|
|
138
|
+
"update", "delete", "drop record",
|
|
140
139
|
"increment", "decrement",
|
|
141
140
|
]
|
|
142
|
-
for kw in
|
|
141
|
+
for kw in write_phrases:
|
|
143
142
|
if kw in prompt_lower:
|
|
144
143
|
return "write"
|
|
144
|
+
# Single-word write verbs need word boundary to avoid false positives
|
|
145
|
+
# e.g. "set" should not match "dataset", "change" should not match "show changes"
|
|
146
|
+
import re as _re
|
|
147
|
+
write_verbs = ["set", "change", "modify", "remove"]
|
|
148
|
+
for verb in write_verbs:
|
|
149
|
+
if _re.search(r'\b' + verb + r'\b', prompt_lower):
|
|
150
|
+
# Exclude when preceded by 'show', 'list', 'display', 'view', 'get'
|
|
151
|
+
read_context = _re.search(r'\b(?:show|list|display|view|get|count)\b.*\b' + verb, prompt_lower)
|
|
152
|
+
if not read_context:
|
|
153
|
+
return "write"
|
|
145
154
|
|
|
146
155
|
# Default: read
|
|
147
156
|
return "read"
|