@zleap-ai/sag-cli 0.2.0 → 0.3.0

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.
package/README.en.md ADDED
@@ -0,0 +1,214 @@
1
+ # SAG CLI
2
+
3
+ [简体中文](README.md) | English
4
+
5
+ `@zleap-ai/sag-cli` is a command-line client and diagnostics tool for SAG knowledge bases. From your terminal it lets you:
6
+
7
+ - Sign in to and validate a running SAG instance;
8
+ - Inspect sources and document processing status, and search knowledge directly;
9
+ - Verify the local Docker SAG knowledge-base MCP and wire it into Codex and Claude Code with a single command.
10
+
11
+ For release notes see [CHANGELOG.md](CHANGELOG.md).
12
+
13
+ ## Is this tool for you
14
+
15
+ - You already have a SAG instance (local Docker or remote HTTP) and want to query, diagnose, and expose it to coding agents from the terminal.
16
+ - You are developing or operating SAG and need diagnostics such as `doctor` and `mcp test`.
17
+ - You want to use SAG for retrieval inside Codex or Claude Code without hand-writing an MCP config.
18
+
19
+ ## Prerequisites
20
+
21
+ | Task | Requirement |
22
+ | ------------------------------------ | ------------------------------------------------------------------------- |
23
+ | Install and run the CLI | Node.js **≥ 20.19** |
24
+ | Sign in and search via the HTTP API | A reachable SAG origin (e.g. `http://localhost:8000`) plus a SAG JWT |
25
+ | Use the local Docker token-less path | Docker CLI, with a SAG API container running `sag_api.mcp.server` locally |
26
+ | Wire into Codex | Codex CLI (≥ 0.145) installed |
27
+ | Wire into Claude Code | Claude Code (≥ 2.1) installed |
28
+
29
+ **Where to get the JWT**: sign in to SAG Web → **Settings → Integrations** and copy the JWT. The CLI hides the input and prefers the OS credential store (macOS Keychain, Windows Credential Manager, Linux Secret Service). When that is unavailable the token stays only in the current process; use the `SAG_TOKEN` environment variable in automation.
30
+
31
+ ## Install
32
+
33
+ ```bash
34
+ npm install --global @zleap-ai/sag-cli
35
+ sag --help
36
+ sag version
37
+ ```
38
+
39
+ ## Quick start: pick the path that fits you
40
+
41
+ The CLI offers two independent paths. Pick one based on your situation.
42
+
43
+ ### Path A: Local Docker, no token required (recommended for local dev)
44
+
45
+ Requires a SAG API container running locally in Docker. The CLI discovers the container, talks to it over stdio MCP via `docker exec`, and never needs the JWT.
46
+
47
+ ```bash
48
+ # 1. Verify the local Docker SAG MCP works
49
+ sag mcp test
50
+
51
+ # 2. Wire it into an agent (either or both)
52
+ sag agent connect codex
53
+ sag agent connect claude-code
54
+
55
+ # 3. Check status any time
56
+ sag agent status
57
+ ```
58
+
59
+ Scope to a single source with `--source-id`:
60
+
61
+ ```bash
62
+ sag mcp test --source-id <source-id>
63
+ sag agent connect codex --source-id <source-id>
64
+ ```
65
+
66
+ Preview the plan with `--dry-run` before writing anything:
67
+
68
+ ```bash
69
+ sag agent connect codex --dry-run
70
+ ```
71
+
72
+ Undo an integration:
73
+
74
+ ```bash
75
+ sag agent disconnect codex
76
+ ```
77
+
78
+ ### Path B: HTTP API + JWT (across machines, or no Docker)
79
+
80
+ Authenticate and search against SAG's HTTP API directly.
81
+
82
+ ```bash
83
+ # 1. Register a SAG instance
84
+ sag profile add local http://localhost:8000
85
+ sag profile use local
86
+
87
+ # 2. Sign in (prompts for the JWT)
88
+ sag auth login
89
+ sag auth status
90
+
91
+ # 3. Health check and browse
92
+ sag doctor
93
+ sag source list
94
+ sag document status --source <source-id>
95
+
96
+ # 4. Search
97
+ sag search "how to wire MCP" --source <source-id> --top-k 5
98
+ ```
99
+
100
+ A profile stores only `scheme://host[:port]`. The `/api/v1` prefix is added by the CLI — do not put it in the URL yourself.
101
+
102
+ ## Use with the Skill
103
+
104
+ The Skill files (`skill/`) ship alongside this CLI. Copy them into your Agent's skills directory:
105
+
106
+ ```bash
107
+ # Claude Code
108
+ SKILL_SRC="$(npm root -g)/@zleap-ai/sag-cli"
109
+ cp -r "$SKILL_SRC/skill" ~/.claude/skills/sag-knowledge
110
+
111
+ # Codex
112
+ SKILL_SRC="$(npm root -g)/@zleap-ai/sag-cli"
113
+ cp -r "$SKILL_SRC/skill" ~/.codex/skills/sag-knowledge
114
+ ```
115
+
116
+ ## Command reference
117
+
118
+ ```text
119
+ sag version
120
+ sag auth login | status | logout
121
+ sag profile add | list | use | show | remove
122
+ sag doctor
123
+ sag source list | get | status
124
+ sag document list | get | status
125
+ sag search <query>
126
+ sag mcp test
127
+ sag agent list
128
+ sag agent connect <codex | claude-code>
129
+ sag agent status [codex | claude-code]
130
+ sag agent disconnect <codex | claude-code>
131
+ ```
132
+
133
+ Common examples:
134
+
135
+ ```bash
136
+ sag profile list --json
137
+ sag source get <source-id>
138
+ sag document list --source <source-id>
139
+ sag search "how to wire MCP" --source <source-id> --strategy multi
140
+ sag mcp test --container sag-api-1 --timeout 15000
141
+ sag agent connect claude-code --name sag-knowledge-local
142
+ ```
143
+
144
+ ## Global options
145
+
146
+ ```text
147
+ --profile <name> Select a profile
148
+ --url <origin> Override the SAG origin for one command
149
+ --json Emit stable JSON (schema: sag.cli.v1)
150
+ --quiet Print only the essential value
151
+ --yes Confirm a safe local configuration operation
152
+ ```
153
+
154
+ ## Environment variables and resolution order
155
+
156
+ Priority: **CLI flag > environment variable > current profile > local default probe**.
157
+
158
+ ```bash
159
+ SAG_URL=http://localhost:8000
160
+ SAG_TOKEN=<jwt>
161
+ SAG_PROFILE=local
162
+ ```
163
+
164
+ Profile storage locations:
165
+
166
+ | Platform | Path |
167
+ | -------- | ------------------------------------------------------------------------- |
168
+ | macOS | `~/Library/Application Support/sag-cli/config.yaml` |
169
+ | Linux | `$XDG_CONFIG_HOME/sag-cli/config.yaml` or `~/.config/sag-cli/config.yaml` |
170
+ | Windows | `%APPDATA%\sag-cli\config.yaml` |
171
+
172
+ Agent-integration managed state is kept in `managed-connections.yaml` (mode `0600`). Do not edit it by hand.
173
+
174
+ ## Agent integration safety
175
+
176
+ - SAG CLI **never** writes the JWT into agent configuration. The local Docker path needs no token at all.
177
+ - The default MCP entry name is `sag-knowledge-<profile>`, or `sag-knowledge-local` when no profile is active.
178
+ - The CLI only removes MCP entries it created whose fingerprint is unchanged. Same-name user entries and external edits block overwrite and delete.
179
+ - `--dry-run` shows the plan only; `--yes` skips confirmation but never skips conflict checks.
180
+
181
+ ## JSON output and exit codes
182
+
183
+ Under `--json`, both success and failure use a fixed schema:
184
+
185
+ ```json
186
+ { "schema": "sag.cli.v1", "ok": true, "data": {} }
187
+ ```
188
+
189
+ ```json
190
+ {
191
+ "schema": "sag.cli.v1",
192
+ "ok": false,
193
+ "error": { "code": "AUTH_REQUIRED", "message": "No SAG token is configured" }
194
+ }
195
+ ```
196
+
197
+ Tokens never appear in JSON, logs, or error output. Full error and exit codes live in the architecture docs.
198
+
199
+ ## Further reading
200
+
201
+ ### Using the CLI
202
+
203
+ - [CHANGELOG.md](CHANGELOG.md) — release notes
204
+ - Architecture and exit codes: [English](docs/architecture.md) / [简体中文](docs/architecture.zh-CN.md)
205
+ - [SKILL.md](SKILL.md) — teaches an Agent how to explore SAG efficiently
206
+
207
+ ### Developing and contributing
208
+
209
+ - Local development and debugging: [English](docs/development.md) / [简体中文](docs/development.zh-CN.md)
210
+ - Compatibility matrix: [English](docs/compatibility.md) / [简体中文](docs/compatibility.zh-CN.md)
211
+ - OAuth and MCP authorization: [English](docs/oauth.md) / [简体中文](docs/oauth.zh-CN.md)
212
+ - Release process: [English](docs/release.md) / [简体中文](docs/release.zh-CN.md)
213
+ - [CONTRIBUTING.md](CONTRIBUTING.md)
214
+ - [SECURITY.md](SECURITY.md)
package/README.md CHANGED
@@ -1,68 +1,133 @@
1
1
  # SAG CLI
2
2
 
3
- `@zleap-ai/sag-cli` 是运行中 SAG 实例的命令行客户端与诊断工具。除 Profile、
4
- 认证、知识状态和检索外,v0.2 还可以验证本机 Docker SAG 的知识库 MCP,并自动
5
- 接入 Codex 和 Claude Code。
3
+ 简体中文 | [English](README.en.md)
6
4
 
7
- ## 安装与运行
5
+ `@zleap-ai/sag-cli` 是 SAG 知识库的命令行客户端与诊断工具。它帮你在终端里:
8
6
 
9
- 需要 Node.js 20.19 或更高版本:
7
+ - 登录并验证一个正在运行的 SAG 实例;
8
+ - 查看信源、文档处理状态,直接检索知识内容;
9
+ - 验证本机 Docker SAG 的知识库 MCP,并把它一键接入 Codex 和 Claude Code。
10
+
11
+ 版本变更记录见 [CHANGELOG.md](CHANGELOG.md)。
12
+
13
+ ## 你适不适合用它
14
+
15
+ - 你手上已经有一个 SAG 实例(本机 Docker 或远程 HTTP),想在终端里查它、调它、把它接给编码 Agent。
16
+ - 你在开发或运维 SAG,需要 `doctor`、`mcp test` 这类诊断能力。
17
+ - 你只是想在 Codex 或 Claude Code 里用 SAG 做知识检索,又不想手写 MCP 配置。
18
+
19
+ ## 前置条件
20
+
21
+ | 你想做的事 | 需要准备 |
22
+ | --------------------------- | ----------------------------------------------------------- |
23
+ | 安装并运行 CLI | Node.js **≥ 20.19** |
24
+ | 用 HTTP API 登录、搜索 | 一个可访问的 SAG Origin(如 `http://localhost:8000`)+ JWT |
25
+ | 用本机 Docker 免 Token 路径 | Docker CLI,且本机运行着含 `sag_api.mcp.server` 的 SAG 容器 |
26
+ | 接入 Codex | 已安装 Codex CLI(≥ 0.145) |
27
+ | 接入 Claude Code | 已安装 Claude Code(≥ 2.1) |
28
+
29
+ **JWT 从哪拿**:登录 SAG Web → **Settings → Integrations**,复制其中的 JWT。CLI 会隐藏输入并优先保存到系统凭据存储(macOS Keychain、Windows Credential Manager、Linux Secret Service);不可用时只保留在当前进程,自动化环境请改用 `SAG_TOKEN` 环境变量。
30
+
31
+ ## 安装
10
32
 
11
33
  ```bash
12
- npx @zleap-ai/sag-cli version
13
34
  npm install --global @zleap-ai/sag-cli
14
35
  sag --help
36
+ sag version
15
37
  ```
16
38
 
17
- ## 最短使用路径
39
+ ## 快速上手:选一条适合你的路径
40
+
41
+ CLI 提供两条互相独立的路径,按你的场景选。
42
+
43
+ ### 路径 A:本机 Docker,免 Token(推荐给本地开发者)
18
44
 
19
- 只验证和接入本机 Docker SAG MCP,不需要从 SAG Web 复制 Token:
45
+ 前提是本机 Docker 已经在跑 SAG API 容器。CLI 会自动发现容器,通过 `docker exec` 走 stdio MCP,全程不需要 JWT。
20
46
 
21
47
  ```bash
48
+ # 1. 验证本机 Docker SAG MCP 可以工作
22
49
  sag mcp test
50
+
51
+ # 2. 把它接入你的 Agent(选一个或两个都接)
23
52
  sag agent connect codex
24
53
  sag agent connect claude-code
54
+
55
+ # 3. 随时看接入状态
25
56
  sag agent status
26
57
  ```
27
58
 
28
- 需要先安装 Docker CLI,并运行包含 `sag_api.mcp.server` 的 SAG API 容器。
29
- 接入某一个信源:
59
+ 只想验证 / 接入某一个信源时加 `--source-id`:
30
60
 
31
61
  ```bash
32
62
  sag mcp test --source-id <source-id>
33
63
  sag agent connect codex --source-id <source-id>
34
64
  ```
35
65
 
36
- 原有 HTTP API 与知识检索路径:
66
+ 不放心可以先 `--dry-run` 看计划:
67
+
68
+ ```bash
69
+ sag agent connect codex --dry-run
70
+ ```
71
+
72
+ 想撤掉接入:
37
73
 
38
74
  ```bash
75
+ sag agent disconnect codex
76
+ ```
77
+
78
+ ### 路径 B:HTTP API + JWT(跨机器、或本机没有 Docker)
79
+
80
+ 用 SAG 的 HTTP API 做认证、查询、检索。
81
+
82
+ ```bash
83
+ # 1. 记录一个 SAG 实例
39
84
  sag profile add local http://localhost:8000
85
+ sag profile use local
86
+
87
+ # 2. 登录(会提示输入 JWT)
40
88
  sag auth login
89
+ sag auth status
90
+
91
+ # 3. 体检 + 看信源
41
92
  sag doctor
42
93
  sag source list
43
94
  sag document status --source <source-id>
44
- sag search "查询内容"
95
+
96
+ # 4. 检索
97
+ sag search "MCP 如何接入" --source <source-id> --top-k 5
45
98
  ```
46
99
 
47
- `auth login` 会隐藏输入 SAG JWT,验证成功后优先保存到 macOS Keychain、
48
- Windows Credential Manager 或 Linux Secret Service。无法使用系统凭据存储时,
49
- Token 只保留在当前进程;自动化环境应使用 `SAG_TOKEN`。
100
+ Profile URL 只保存 `scheme://host[:port]`。API 前缀 `/api/v1` CLI 自动补齐,不要自己写。
101
+
102
+ ## 搭配 Skill 使用
50
103
 
51
- ## 命令
104
+ Skill 文件(`skill/`)随本 CLI 一起发布。安装 CLI 后复制到 Agent 的 skills 目录:
105
+
106
+ ```bash
107
+ # Claude Code
108
+ SKILL_SRC="$(npm root -g)/@zleap-ai/sag-cli"
109
+ cp -r "$SKILL_SRC/skill" ~/.claude/skills/sag-knowledge
110
+
111
+ # Codex
112
+ SKILL_SRC="$(npm root -g)/@zleap-ai/sag-cli"
113
+ cp -r "$SKILL_SRC/skill" ~/.codex/skills/sag-knowledge
114
+ ```
115
+
116
+ ## 命令一览
52
117
 
53
118
  ```text
54
119
  sag version
55
- sag auth login|status|logout
56
- sag profile add|list|use|show|remove
120
+ sag auth login | status | logout
121
+ sag profile add | list | use | show | remove
57
122
  sag doctor
58
- sag source list|get|status
59
- sag document list|get|status
123
+ sag source list | get | status
124
+ sag document list | get | status
60
125
  sag search <query>
61
126
  sag mcp test
62
127
  sag agent list
63
- sag agent connect <codex|claude-code>
64
- sag agent status [codex|claude-code]
65
- sag agent disconnect <codex|claude-code>
128
+ sag agent connect <codex | claude-code>
129
+ sag agent status [codex | claude-code]
130
+ sag agent disconnect <codex | claude-code>
66
131
  ```
67
132
 
68
133
  常用示例:
@@ -70,30 +135,25 @@ sag agent disconnect <codex|claude-code>
70
135
  ```bash
71
136
  sag profile list --json
72
137
  sag source get <source-id>
73
- sag source status [source-id]
74
138
  sag document list --source <source-id>
75
- sag document get <document-id> --source <source-id>
76
- sag document status [document-id] --source <source-id>
77
- sag search "MCP 如何接入" --source <source-id> --top-k 5 --strategy multi
139
+ sag search "MCP 如何接入" --source <source-id> --strategy multi
78
140
  sag mcp test --container sag-api-1 --timeout 15000
79
- sag agent connect codex --dry-run
80
141
  sag agent connect claude-code --name sag-knowledge-local
81
- sag agent disconnect codex
82
142
  ```
83
143
 
84
- 全局参数:
144
+ ## 全局参数
85
145
 
86
146
  ```text
87
147
  --profile <name> 选择 Profile
88
148
  --url <origin> 临时指定 SAG Origin
89
- --json 输出稳定 JSON
149
+ --json 输出稳定 JSON(schema: sag.cli.v1)
90
150
  --quiet 只输出核心值
91
151
  --yes 确认安全的本地配置操作
92
152
  ```
93
153
 
94
- ## 环境变量
154
+ ## 环境变量与配置优先级
95
155
 
96
- 配置优先级为:命令行参数 > 环境变量 > 当前 Profile > 本地默认探测。
156
+ 优先级:**命令行参数 > 环境变量 > 当前 Profile > 本地默认探测**。
97
157
 
98
158
  ```bash
99
159
  SAG_URL=http://localhost:8000
@@ -101,29 +161,26 @@ SAG_TOKEN=<jwt>
101
161
  SAG_PROFILE=local
102
162
  ```
103
163
 
104
- Profile URL 只保存 `scheme://host[:port]`。CLI 自动为 API 添加 `/api/v1`,
105
- 不要求用户手工填写接口前缀。
164
+ Profile 配置存放位置:
106
165
 
107
- ## Agent MCP 接入行为
166
+ | 平台 | 路径 |
167
+ | ------- | ------------------------------------------------------------------------- |
168
+ | macOS | `~/Library/Application Support/sag-cli/config.yaml` |
169
+ | Linux | `$XDG_CONFIG_HOME/sag-cli/config.yaml` 或 `~/.config/sag-cli/config.yaml` |
170
+ | Windows | `%APPDATA%\sag-cli\config.yaml` |
108
171
 
109
- CLI 自动发现 Compose `api` 服务,优先选择项目名为 `sag` 的健康容器,并验证
110
- 八个 SAG 只读 MCP 工具和 `list_sources` 调用。实际保存到 Agent 的命令是:
172
+ Agent 接入的受管状态另存于 `managed-connections.yaml`(权限 `0600`),不要手工编辑。
111
173
 
112
- ```text
113
- docker exec -i <container> python -m sag_api.mcp.server
114
- ```
115
-
116
- 默认 MCP 名称为 `sag-knowledge-<profile>`;没有 Profile 时为
117
- `sag-knowledge-local`。`--dry-run` 只展示计划,`--yes` 跳过确认但不会跳过
118
- 冲突和安全检查。
174
+ ## Agent 接入的安全约束
119
175
 
120
- SAG CLI 不会把 JWT 写入 Agent 配置。它只删除自己创建且指纹未变化的 MCP
121
- 条目;检测到同名用户配置或外部修改时会拒绝覆盖或删除。受管状态保存在平台配置
122
- 目录的 `managed-connections.yaml` 中,权限为 `0600`。
176
+ - SAG CLI **不会** JWT 写入 Agent 配置。本机 Docker 路径无需 Token。
177
+ - 默认 MCP 名称为 `sag-knowledge-<profile>`,无 Profile 时为 `sag-knowledge-local`。
178
+ - 只删除自己创建、指纹未变化的 MCP 条目;检测到同名用户配置或外部改动会拒绝覆盖。
179
+ - `--dry-run` 只展示计划;`--yes` 跳过确认但不跳过冲突检查。
123
180
 
124
- ## JSON 契约
181
+ ## JSON 输出与退出码
125
182
 
126
- 成功与失败都包含固定 Schema:
183
+ `--json` 下成功与失败使用固定 Schema:
127
184
 
128
185
  ```json
129
186
  { "schema": "sag.cli.v1", "ok": true, "data": {} }
@@ -137,24 +194,21 @@ SAG CLI 不会把 JWT 写入 Agent 配置。它只删除自己创建且指纹未
137
194
  }
138
195
  ```
139
196
 
140
- JSON、日志和错误输出不会包含 Token。退出码和错误分类见架构说明:
141
- [中文](docs/architecture.zh-CN.md) / [English](docs/architecture.md)。
197
+ Token 不会出现在 JSON、日志或错误输出里。完整错误码与退出码见架构文档。
142
198
 
143
- ## 开发
199
+ ## 深入阅读
144
200
 
145
- ```bash
146
- npm install
147
- npm run dev -- --help
148
- npm run check
149
- ```
201
+ ### 使用指南
150
202
 
151
- 本地运行、认证、知识库搜索、断点调试、测试和 Docker MCP 验证见开发指南:
152
- [中文](docs/development.zh-CN.md) / [English](docs/development.md)
203
+ - [CHANGELOG.md](CHANGELOG.md) 版本变更记录
204
+ - 架构与退出码:[中文](docs/architecture.zh-CN.md) / [English](docs/architecture.md)
205
+ - [SKILL.md](SKILL.md) — 教 Agent 怎么高效探索 SAG 知识库
153
206
 
154
- 贡献规则见 [CONTRIBUTING.md](CONTRIBUTING.md)。其他文档:
207
+ ### 开发与贡献
155
208
 
156
- - 发布:[中文](docs/release.zh-CN.md) / [English](docs/release.md)
157
- - 兼容性:[中文](docs/compatibility.zh-CN.md) /
158
- [English](docs/compatibility.md)
159
- - OAuth 与 MCP 授权:[中文](docs/oauth.zh-CN.md) /
160
- [English](docs/oauth.md)
209
+ - 本地开发与调试:[中文](docs/development.zh-CN.md) / [English](docs/development.md)
210
+ - 兼容性矩阵:[中文](docs/compatibility.zh-CN.md) / [English](docs/compatibility.md)
211
+ - OAuth 与 MCP 授权:[中文](docs/oauth.zh-CN.md) / [English](docs/oauth.md)
212
+ - 发布流程:[中文](docs/release.zh-CN.md) / [English](docs/release.md)
213
+ - [CONTRIBUTING.md](CONTRIBUTING.md)
214
+ - [SECURITY.md](SECURITY.md)