dbagent-cli 0.3.9__tar.gz → 0.4.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.
Files changed (77) hide show
  1. dbagent_cli-0.4.1/PKG-INFO +281 -0
  2. dbagent_cli-0.4.1/README.md +257 -0
  3. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/__init__.py +1 -1
  4. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/llm/gemini_provider.py +14 -12
  5. dbagent_cli-0.4.1/dbagent_cli.egg-info/PKG-INFO +281 -0
  6. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/pyproject.toml +2 -2
  7. dbagent_cli-0.3.9/PKG-INFO +0 -198
  8. dbagent_cli-0.3.9/README.md +0 -174
  9. dbagent_cli-0.3.9/dbagent_cli.egg-info/PKG-INFO +0 -198
  10. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/agent/confidence.py +0 -0
  11. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/agent/conversation.py +0 -0
  12. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/agent/doctor.py +0 -0
  13. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/agent/error_classifier.py +0 -0
  14. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/agent/generator.py +0 -0
  15. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/agent/pipeline.py +0 -0
  16. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/agent/recommender.py +0 -0
  17. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/agent/validator.py +0 -0
  18. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/cli.py +0 -0
  19. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/config.py +0 -0
  20. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/connectors/base.py +0 -0
  21. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/connectors/factory.py +0 -0
  22. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/connectors/mongo.py +0 -0
  23. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/connectors/relational.py +0 -0
  24. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/execution/__init__.py +0 -0
  25. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/execution/cache.py +0 -0
  26. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/execution/changeset.py +0 -0
  27. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/execution/cost.py +0 -0
  28. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/execution/recovery.py +0 -0
  29. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/execution/sandbox.py +0 -0
  30. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/execution/streaming.py +0 -0
  31. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/llm/auto_setup.py +0 -0
  32. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/llm/base.py +0 -0
  33. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/llm/factory.py +0 -0
  34. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/llm/groq_provider.py +0 -0
  35. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/llm/mock_provider.py +0 -0
  36. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/llm/ollama_provider.py +0 -0
  37. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/llm/openrouter_provider.py +0 -0
  38. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/schema/cache.py +0 -0
  39. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/schema/formatter.py +0 -0
  40. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/schema/graph.py +0 -0
  41. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/schema/models.py +0 -0
  42. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/schema/retriever.py +0 -0
  43. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/schema/selector.py +0 -0
  44. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/security/__init__.py +0 -0
  45. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/security/audit.py +0 -0
  46. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/security/environment.py +0 -0
  47. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/security/masking.py +0 -0
  48. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/security/modes.py +0 -0
  49. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/security/rbac.py +0 -0
  50. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/semantic/__init__.py +0 -0
  51. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/semantic/dictionary.py +0 -0
  52. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/semantic/templates.py +0 -0
  53. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/sql/__init__.py +0 -0
  54. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/sql/optimizer.py +0 -0
  55. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/sql/validator.py +0 -0
  56. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/ui/console.py +0 -0
  57. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/ui/grid_window.py +0 -0
  58. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/ui/viewer.py +0 -0
  59. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent/ui/visualizer.py +0 -0
  60. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent_cli.egg-info/SOURCES.txt +0 -0
  61. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent_cli.egg-info/dependency_links.txt +0 -0
  62. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent_cli.egg-info/entry_points.txt +0 -0
  63. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent_cli.egg-info/requires.txt +0 -0
  64. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/dbagent_cli.egg-info/top_level.txt +0 -0
  65. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/setup.cfg +0 -0
  66. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/tests/test_cache.py +0 -0
  67. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/tests/test_cli.py +0 -0
  68. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/tests/test_connectors.py +0 -0
  69. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/tests/test_doctor_and_viz.py +0 -0
  70. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/tests/test_generator.py +0 -0
  71. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/tests/test_graph.py +0 -0
  72. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/tests/test_recovery.py +0 -0
  73. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/tests/test_safety.py +0 -0
  74. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/tests/test_sandbox.py +0 -0
  75. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/tests/test_schema.py +0 -0
  76. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/tests/test_security.py +0 -0
  77. {dbagent_cli-0.3.9 → dbagent_cli-0.4.1}/tests/test_semantic.py +0 -0
@@ -0,0 +1,281 @@
1
+ Metadata-Version: 2.4
2
+ Name: dbagent-cli
3
+ Version: 0.4.1
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
+ [![PyPI Version](https://img.shields.io/pypi/v/dbagent-cli.svg)](https://pypi.org/project/dbagent-cli/)
31
+ [![Python Versions](https://img.shields.io/pypi/pyversions/dbagent-cli.svg)](https://pypi.org/project/dbagent-cli/)
32
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
33
+ [![Database Support](https://img.shields.io/badge/Databases-Postgres%20%7C%20MySQL%20%7C%20SQLite%20%7C%20MSSQL%20%7C%20Oracle%20%7C%20DuckDB%20%7C%20MongoDB-blue.svg)](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
+ [![PyPI Version](https://img.shields.io/pypi/v/dbagent-cli.svg)](https://pypi.org/project/dbagent-cli/)
7
+ [![Python Versions](https://img.shields.io/pypi/pyversions/dbagent-cli.svg)](https://pypi.org/project/dbagent-cli/)
8
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
9
+ [![Database Support](https://img.shields.io/badge/Databases-Postgres%20%7C%20MySQL%20%7C%20SQLite%20%7C%20MSSQL%20%7C%20Oracle%20%7C%20DuckDB%20%7C%20MongoDB-blue.svg)](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**.
@@ -2,5 +2,5 @@
2
2
  DB-Agent: Universal Database Introspection and Script Generation AI Agent (CLI).
3
3
  """
4
4
 
5
- __version__ = "0.3.9"
5
+ __version__ = "0.4.1"
6
6
  __author__ = "Santhosh Gupta"
@@ -1,6 +1,6 @@
1
1
  """
2
2
  Google Gemini Free Tier LLM Provider.
3
- Uses Gemini 2.5 Flash / Gemini 2.0 Flash / Gemini 1.5 Flash via direct HTTP API.
3
+ Uses gemini-3.6-flash (current primary), gemini-3.7-flash, gemini-3.5-flash, gemini-flash-latest.
4
4
  """
5
5
 
6
6
  import json
@@ -14,13 +14,15 @@ class GeminiProvider(BaseLLMProvider):
14
14
  """Google Gemini LLM provider (Free Tier available)."""
15
15
 
16
16
  FALLBACK_MODELS = [
17
+ "gemini-3.6-flash",
18
+ "gemini-3.7-flash",
19
+ "gemini-3.5-flash",
20
+ "gemini-flash-latest",
21
+ "gemini-pro-latest",
17
22
  "gemini-2.5-flash",
18
- "gemini-2.0-flash",
19
- "gemini-1.5-flash",
20
- "gemini-1.5-pro",
21
23
  ]
22
24
 
23
- def __init__(self, api_key: Optional[str] = None, default_model: str = "gemini-2.5-flash"):
25
+ def __init__(self, api_key: Optional[str] = None, default_model: str = "gemini-3.6-flash"):
24
26
  self.api_key = api_key or os.getenv("GEMINI_API_KEY")
25
27
  self.default_model = default_model
26
28
 
@@ -82,7 +84,7 @@ class GeminiProvider(BaseLLMProvider):
82
84
 
83
85
  try:
84
86
  if stream_callback:
85
- response = requests.post(endpoint, json=payload, stream=True, timeout=15)
87
+ response = requests.post(endpoint, json=payload, stream=True, timeout=10)
86
88
  if response.status_code == 200:
87
89
  full_text = []
88
90
  for line in response.iter_lines():
@@ -102,13 +104,13 @@ class GeminiProvider(BaseLLMProvider):
102
104
  except Exception:
103
105
  pass
104
106
  return "".join(full_text)
105
- elif response.status_code in (404, 400):
106
- last_error = f"Model {target_model} not available ({response.status_code})"
107
+ elif response.status_code in (404, 400, 503, 429):
108
+ last_error = f"Model {target_model} status {response.status_code}"
107
109
  continue
108
110
  else:
109
111
  raise RuntimeError(f"Gemini API error ({response.status_code}): {response.text}")
110
112
  else:
111
- response = requests.post(endpoint, json=payload, timeout=15)
113
+ response = requests.post(endpoint, json=payload, timeout=10)
112
114
  if response.status_code == 200:
113
115
  data = response.json()
114
116
  candidates = data.get("candidates", [])
@@ -116,13 +118,13 @@ class GeminiProvider(BaseLLMProvider):
116
118
  parts = candidates[0].get("content", {}).get("parts", [])
117
119
  return "".join(p.get("text", "") for p in parts)
118
120
  return ""
119
- elif response.status_code in (404, 400):
120
- last_error = f"Model {target_model} not available ({response.status_code})"
121
+ elif response.status_code in (404, 400, 503, 429):
122
+ last_error = f"Model {target_model} status {response.status_code}"
121
123
  continue
122
124
  else:
123
125
  raise RuntimeError(f"Gemini API error ({response.status_code}): {response.text}")
124
126
  except requests.exceptions.Timeout:
125
- last_error = f"Model {target_model} request timed out"
127
+ last_error = f"Model {target_model} timed out"
126
128
  continue
127
129
  except Exception as e:
128
130
  last_error = str(e)