Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .agents/skills/xsql/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ description: >
- `stdout` is data; `stderr` is logs. Never parse stderr as result data.
- Validate responses by checking `ok`, `schema_version`, and `error.code` — not by string matching.
- On `ok: false`, read `error.details.hint`, `error.details.category`, and `error.details.retryable` before retrying (see `docs/error-contract.md`). Use `hint` as the next action; respect `retryable: false` for SQL/schema/permission fixes instead of blind retries.
- Assume read-only mode. Writes require both `unsafe_allow_write: true` in the selected profile and a runtime write opt-in: `--unsafe-allow-write` on `xsql query` / `xsql ai`, or `--unsafe-allow-write` when starting `xsql mcp server` (or `XSQL_MCP_UNSAFE_ALLOW_WRITE=true`). Only enable both factors when the user has explicitly requested writes.
- Assume read-only mode. Writes require both `unsafe_allow_write: true` in the selected profile and a runtime write opt-in: `--unsafe-allow-write` on `xsql query`, or `--unsafe-allow-write` when starting `xsql mcp server` (or `XSQL_MCP_UNSAFE_ALLOW_WRITE=true`). Only enable both factors when the user has explicitly requested writes.
- Never leak secrets, full DSNs, passwords, or private keys in output or summaries.
- `--ssh-skip-known-hosts-check` is a risky last resort — call out the security tradeoff if it's needed.

Expand Down
4 changes: 2 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
## 3. 架构与目录约束(不要越界)
推荐结构(实现时请遵循):
- `cmd/xsql`:CLI 入口(只做参数解析、调用 app、输出)
- `internal/app`:应用编排(核心入口,供未来 MCP/TUI/Web 复用)
- `internal/app`:应用编排(核心入口,供 MCP/Web 等复用)
- `internal/config`:配置加载/合并/校验 + profile
- `internal/secret`:keyring/加密/明文兼容
- `internal/db`:driver registry + 执行引擎
Expand Down Expand Up @@ -71,7 +71,7 @@
- 目标数据库:MySQL + PostgreSQL。
- 可扩展性:通过 driver registry 扩展新 DB。
- **默认只读**:防止误操作,默认启用双重只读保护(SQL 静态分析 + 数据库事务级只读)。
- **CLI 写入双重授权**:profile 必须配置 `unsafe_allow_write: true`,且当前 `query` / `ai` 命令必须同时携带 `--unsafe-allow-write`;任一条件缺失都保持只读。
- **CLI 写入双重授权**:profile 必须配置 `unsafe_allow_write: true`,且当前 `query` 命令必须同时携带 `--unsafe-allow-write`;任一条件缺失都保持只读。
- **AI 调用标记**:Codex 发起的 xsql CLI 调用必须携带 `--attr source=codex-cli`,建议同时追加 `agent`、`env`、`team`、`task` 等 attr;其他 AI 客户端应使用自己的稳定 source 值,且不得覆盖 source。
- 只读拦截必须返回明确错误码(退出码=4)。

Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ Note: Read-only mode by default. Writes require both profile `unsafe_allow_write

| Command | Description |
|---------|-------------|
| `xsql ai [PROMPT]` | Start interactive AI assistant mode (TUI) |
| `xsql query <SQL>` | Execute SQL queries (read-only by default; 1000-row default cap, `--max-rows`) |
| `xsql schema dump` | Export database schema (tables, columns, indexes, foreign keys) |
| `xsql profile list` | List all profiles |
Expand Down
1 change: 0 additions & 1 deletion README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ xsql query "<SQL>" -p <profile> -f json --attr source=codex-cli --attr agent=cod

| 命令 | 说明 |
|------|------|
| `xsql ai [PROMPT]` | 启动全屏交互式 AI 终端模式(TUI) |
| `xsql query <SQL>` | 执行 SQL 查询(默认只读;默认最多 1000 行,`--max-rows` 可调) |
| `xsql schema dump` | 导出数据库结构(表、列、索引、外键) |
| `xsql profile list` | 列出所有 profile |
Expand Down
98 changes: 0 additions & 98 deletions cmd/xsql/ai.go

This file was deleted.

174 changes: 0 additions & 174 deletions cmd/xsql/ai_test.go

This file was deleted.

1 change: 0 additions & 1 deletion cmd/xsql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ func run() int {
root.AddCommand(NewServeCommand(&w))
root.AddCommand(NewWebCommand(&w))
root.AddCommand(NewStatsCommand(&w))
root.AddCommand(NewAICommand())

// Execute and handle errors
if err := root.Execute(); err != nil {
Expand Down
6 changes: 0 additions & 6 deletions cmd/xsql/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ func NewRootCommand() *cobra.Command {
CLIFormatSet: formatSet,
EnvProfile: os.Getenv("XSQL_PROFILE"),
EnvFormat: os.Getenv("XSQL_FORMAT"),
EnvAIModel: os.Getenv("XSQL_AI_MODEL"),
EnvAIBaseURL: os.Getenv("XSQL_AI_BASE_URL"),
EnvAIAPIKey: os.Getenv("XSQL_AI_API_KEY"),
WorkDir: os.Getenv("XSQL_WORKDIR"),
HomeDir: os.Getenv("XSQL_HOMEDIR"),
})
Expand Down Expand Up @@ -95,9 +92,6 @@ func NewRootCommand() *cobra.Command {
CLIFormatSet: formatSet,
EnvProfile: os.Getenv("XSQL_PROFILE"),
EnvFormat: os.Getenv("XSQL_FORMAT"),
EnvAIModel: os.Getenv("XSQL_AI_MODEL"),
EnvAIBaseURL: os.Getenv("XSQL_AI_BASE_URL"),
EnvAIAPIKey: os.Getenv("XSQL_AI_API_KEY"),
WorkDir: os.Getenv("XSQL_WORKDIR"),
HomeDir: os.Getenv("XSQL_HOMEDIR"),
})
Expand Down
47 changes: 1 addition & 46 deletions docs/ai.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,49 +112,4 @@ xsql web

Web UI 复用 xsql 的 profile、SSH、只读策略和结构化错误契约,但其 HTTP API 面向浏览器,不等同于 MCP 协议。

## AI TUI 交互模式 (xsql ai)
`xsql ai` 为主程序的子命令,提供交互式 AI 终端模式。用户只需在终端以自然语言发问,AI 结合当前数据库 Schema 结构自动构建对应的 SQL 查询,并在 TUI 中提供交互预览与安全执行:

```bash
# 启动交互式 TUI
xsql ai --profile dev --attr source=codex-cli --attr agent=codex --attr env=dev --attr task=interactive-analysis
```

`xsql ai` 默认保持只读。只有所选 profile 配置了 `unsafe_allow_write: true`,且本次启动同时携带 `--unsafe-allow-write` 时,TUI 才进入 READ-WRITE 模式;切换 profile 后会重新校验这两个条件。

### LLM 集成与 Tool Call 机制
`xsql` 使用 OpenAI 官方 SDK (`github.com/openai/openai-go`) 与大模型交互,基于标准的 **ReAct Agent Loop 循环推理**,支持 4 大核心 Tools 调度:
1. **`execute_sql(sql: string, explanation: string)`**: 数据库 SQL 查询工具(执行成功后宿主自动渲染内嵌交互表格)。
2. **`execute_javascript(js_code: string, explanation: string)`**: 基于 `goja` 沙箱的本地 JS 数据聚合计算工具(必须遵循 ES5 语法)。
3. **`export_data(dataset_id: string, format: string, filepath: string, explanation: string)`**: 原始数据集转储工具,仅支持 `csv` 和 `json` 格式(触发人机交互二次确认,路径支持 `~/` 展开)。
4. **`export_report(content: string, filepath: string, explanation: string)`**: 富文本 Markdown 分析报告导出工具(触发人机交互二次确认,路径支持 `~/` 展开)。

#### ReAct Agent Loop 准则
- **循环驱动**:Agent 会在单次交互中循环执行 Tools,直到不再产生 Tool Call。
- **自愈重试机制**:当模型返回的 Tool Call 参数格式不符合 JSON Schema 时,Agent 循环自动捕获并将格式错误反馈给模型,触发自动修复与重试(最大 2 次)。
- **最终回答不变性**:交互轮次的最终输出必定是 AI 总结出的自然语言 / Markdown 格式分析报告。
- **工具折叠与容器内嵌**:所有的中间 Tool Call 默认以单行 Pill 收起折叠(内嵌表格与指标数据),界面保持极简清爽。

#### 有界数据回传与 Session 数据集召回 (Session DataStore)
- 每次查询成功的结果在本地分配标号(`res1`, `res2`, ...)。
- 大模型上下文包含数据集的轻量 Catalog 目录结构(字段名与行数),不会自动加入完整查询结果。
- 本地 JavaScript 的派生结果会以最多 4096 个字符的摘要回传给模型,用于生成最终分析;超出部分会截断并明确标记。
- AI 可通过 `execute_javascript` 生成纯 Go 沙箱 (`goja`) 执行的代码,在本地对 `res1`, `res2` 等数据集做跨表 Join、占比统计与数据清洗,并通过 `export_data` 安全导出为 CSV/JSON,或通过 `export_report` 导出完整分析报告。

### 快捷键操作

#### SQL & 导出确认状态 (Approval Mode)
- `Enter`: 确认并安全执行当前生成预览的 SQL 或同意文件导出
- `Esc`: 取消当前 SQL 生成建议或拒绝文件导出

#### 通用与表格/工具操作 (General & Tool Operations)
- `Enter`: 提交自然语言需求给 AI
- `Ctrl+O`: 折叠/展开当前选中的 Tool Call 详情(内嵌表格与指标)
- `Ctrl+P`: 切换数据库 Profile
- `Tab`: 在会话历史中的多个 Tool Call 组件之间切换焦点
- `Ctrl+E`: 切换表格单行展开视图(Expand Vertical View)
- `←` / `→`: 横向平滑滚动查看当前焦点表格的隐藏列
- `PgUp` / `PgDn`: 向上/向下翻页查看当前焦点表格的数据
- `Shift+Tab`: 一键切换 **自动执行 (AUTO-EXECUTE)** 与 **手动批准 (MANUAL-APPROVE)** 模式
- `Esc`: 清空输入框
- `Ctrl+C` 连按两次:退出 AI 模式
> **说明**:交互式 `xsql ai` TUI 已在后续版本中移除。请使用 `xsql query`、`xsql schema dump`、MCP Server 或 Web UI 完成数据库分析工作流。
Loading
Loading