min-agent 0.1.4 → 0.1.5

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/docs/API.md CHANGED
@@ -1,216 +1,403 @@
1
1
  # min-agent HTTP API
2
2
 
3
- 通过 `min-agent serve` 启动本地 HTTP 服务,以编程方式调用与 CLI `chat` 相同的代理能力(同一套 MCP、技能、规则、工具与模型配置)。
3
+ 通过 `min-agent serve` 启动本地 HTTP 服务,以编程方式调用与 CLI 相同的代理能力。
4
4
 
5
5
  ## 启动
6
6
 
7
7
  ```bash
8
8
  min-agent serve
9
9
  min-agent serve --host 127.0.0.1 --port 8787
10
- min-agent serve -p 3030
11
10
  ```
12
11
 
13
- 环境变量(可选):
12
+ 环境变量:
14
13
 
15
14
  | 变量 | 说明 |
16
15
  |------|------|
17
- | `MIN_AGENT_SERVE_HOST` | 默认监听地址,默认 `127.0.0.1` |
18
- | `MIN_AGENT_SERVE_PORT` | 默认端口,默认 `8787` |
19
- | `MIN_AGENT_SERVE_TOKEN` | 若设置,则所有除 `OPTIONS` 外的请求须带 `Authorization: Bearer <token>` |
20
- | `MIN_AGENT_SERVE_CORS` | 设为 `1` `true` 时添加 `Access-Control-Allow-Origin: *`(仅开发跨域时使用) |
21
-
22
- 启动后进程内 **`--yes` 等效**:危险 shell 等确认会自动通过,请勿把未鉴权服务暴露到公网。
16
+ | `MIN_AGENT_SERVE_HOST` | 监听地址(默认 `127.0.0.1`) |
17
+ | `MIN_AGENT_SERVE_PORT` | 端口(默认 `8787`) |
18
+ | `MIN_AGENT_SERVE_TOKEN` | 设置后所有请求须带 `Authorization: Bearer <token>` |
19
+ | `MIN_AGENT_SERVE_CORS` | 设为 `1` 启用 CORS |
23
20
 
24
21
  ---
25
22
 
26
- ## 通用约定
23
+ ## 端点一览
27
24
 
28
- - **Base URL**:`http://<host>:<port>`
29
- - **请求体**:JSON,`Content-Type: application/json`
30
- - **响应体**:JSON,除非注明为 SSE
31
- - **工作目录**:与启动 `serve` 时的 `process.cwd()` 一致(影响 `read`/`bash` 等工具路径)
32
- - **单例初始化**:进程启动时连接 MCP、扫描技能并加载规则;之后请求复用该状态。修改规则文件后可调用下方「重载规则」接口或重启进程
25
+ | 方法 | 路径 | 说明 |
26
+ |------|------|------|
27
+ | GET | `/health` | 存活检查 |
28
+ | GET | `/v1/meta` | 运行环境摘要 |
29
+ | GET | `/v1/models` | 模型列表 |
30
+ | GET | `/v1/context` | 上下文窗口信息 |
31
+ | GET | `/v1/project` | 项目扫描(code 模式) |
32
+ | POST | `/v1/chat` | 对话(支持流式) |
33
+ | POST | `/v1/paste` | 粘贴图片 + 文本对话 |
34
+ | POST | `/v1/code` | Code 模式对话(项目感知) |
35
+ | POST | `/v1/chat/compact` | 手动压缩会话 |
36
+ | POST | `/v1/chat/reload-instructions` | 重载规则 |
37
+ | GET | `/v1/sessions` | 列出历史会话 |
38
+ | DELETE | `/v1/sessions/:id` | 删除会话 |
39
+ | GET | `/v1/memory` | 列出记忆 |
40
+ | POST | `/v1/memory` | 添加记忆 |
41
+ | GET | `/v1/memory/search?q=xxx` | 搜索记忆 |
42
+ | DELETE | `/v1/memory/:index` | 删除记忆 |
43
+ | GET | `/v1/mcp` | MCP 服务器状态 |
44
+ | POST | `/v1/mcp` | 添加 MCP 服务器 |
45
+ | DELETE | `/v1/mcp/:name` | 删除 MCP 服务器 |
46
+ | GET | `/v1/skills` | 可用技能列表 |
47
+ | GET | `/v1/rules` | 已加载的规则 |
33
48
 
34
49
  ---
35
50
 
36
51
  ## `GET /health`
37
52
 
38
- 存活检查。
53
+ ```json
54
+ { "ok": true, "service": "min-agent", "version": "0.1.0" }
55
+ ```
56
+
57
+ ---
39
58
 
40
- **响应 200**
59
+ ## `GET /v1/meta`
60
+
61
+ ```json
62
+ { "version": "0.1.0", "cwd": "/path/to/project", "instructions_chars": 1234 }
63
+ ```
64
+
65
+ ---
66
+
67
+ ## `GET /v1/models`
68
+
69
+ ```json
70
+ { "default_model": "gpt-4o", "models": ["gpt-4o", "gpt-4o-mini", "..."] }
71
+ ```
72
+
73
+ ---
74
+
75
+ ## `GET /v1/context`
76
+
77
+ 返回当前模型的上下文窗口大小(自动检测)。
78
+
79
+ ```json
80
+ { "context_window": 128000, "model": "gpt-4o" }
81
+ ```
82
+
83
+ ---
84
+
85
+ ## `GET /v1/project`
86
+
87
+ 扫描当前工作目录的项目信息(与 `min-agent code` 启动时相同)。
41
88
 
42
89
  ```json
43
90
  {
44
- "ok": true,
45
- "service": "min-agent",
46
- "version": "0.1.0"
91
+ "project": {
92
+ "directory": "/path/to/project",
93
+ "isGitRepo": true,
94
+ "branch": "main",
95
+ "languages": ["TypeScript"],
96
+ "framework": "Next.js",
97
+ "packageManager": "bun",
98
+ "entryFiles": ["src/index.ts"],
99
+ "configFiles": ["package.json", "tsconfig.json"],
100
+ "summary": "..."
101
+ }
47
102
  }
48
103
  ```
49
104
 
50
105
  ---
51
106
 
52
- ## `GET /v1/meta`
107
+ ## `POST /v1/chat`
108
+
109
+ ### 请求体
53
110
 
54
- 运行环境摘要。
111
+ | 字段 | 类型 | 说明 |
112
+ |------|------|------|
113
+ | `message` | string | 单轮用户消息 |
114
+ | `messages` | array | 多轮 ModelMessage[](与 `message` 二选一) |
115
+ | `model` | string | 覆盖默认模型 |
116
+ | `stream` | boolean | SSE 流式返回 |
117
+ | `session_id` | string | 恢复历史会话并追加 |
118
+ | `images` | string[] | 本地图片路径 |
55
119
 
56
- **响应 200**
120
+ ### 非流式响应
57
121
 
58
122
  ```json
59
123
  {
60
- "version": "0.1.0",
61
- "cwd": "/path/to/project",
62
- "instructions_chars": 1234
124
+ "messages": [...],
125
+ "assistant": { "role": "assistant", "content": "..." },
126
+ "tool_calls": [{ "name": "bash", "input": {...} }],
127
+ "tool_results": [{ "name": "bash", "output": "..." }],
128
+ "session_id": "abc123",
129
+ "step_count": 2,
130
+ "usage": { "inputTokens": 1200, "outputTokens": 300 },
131
+ "has_error": false,
132
+ "aborted": false
63
133
  }
64
134
  ```
65
135
 
136
+ ### 流式 SSE 事件
137
+
138
+ | type | 字段 | 说明 |
139
+ |------|------|------|
140
+ | `assistant` | text | 正文增量 |
141
+ | `thinking` | text | 思考片段 |
142
+ | `tool_call` | name, input | 工具调用 |
143
+ | `tool_result` | name, output | 工具返回 |
144
+ | `compaction` | line | 压缩进度 |
145
+ | `error` | message | 错误 |
146
+ | `done` | step_count, usage, messages, session_id | 结束 |
147
+ | `fatal` | message | 致命错误 |
148
+
66
149
  ---
67
150
 
68
- ## `GET /v1/models`
151
+ ## `POST /v1/chat/compact`
152
+
153
+ 手动压缩指定会话的上下文。
154
+
155
+ ```json
156
+ // 请求
157
+ { "session_id": "abc123" }
158
+
159
+ // 响应
160
+ { "ok": true, "compacted": true, "message_count": 5 }
161
+ ```
162
+
163
+ ---
164
+
165
+ ## `POST /v1/chat/reload-instructions`
166
+
167
+ 重新加载规则文件(不重启 MCP)。
168
+
169
+ ```json
170
+ { "ok": true, "instructions_chars": 1234 }
171
+ ```
172
+
173
+ ---
69
174
 
70
- 从当前配置的 OpenAI 兼容提供商拉取模型列表(与 CLI `min-agent models` 同源)。
175
+ ## `GET /v1/sessions`
71
176
 
72
- **响应 200**
177
+ 列出所有保存的会话。
73
178
 
74
179
  ```json
75
180
  {
76
- "default_model": "gpt-4.1",
77
- "models": ["gpt-4.1", "gpt-4o-mini", "..."]
181
+ "sessions": [
182
+ { "id": "abc123", "title": "Fix login bug", "created": "...", "updated": "...", "messageCount": 12 }
183
+ ]
78
184
  }
79
185
  ```
80
186
 
81
187
  ---
82
188
 
83
- ## `POST /v1/chat/reload-instructions`
189
+ ## `DELETE /v1/sessions/:id`
190
+
191
+ 删除指定会话。
192
+
193
+ ```json
194
+ { "ok": true, "deleted": "abc123" }
195
+ ```
196
+
197
+ ---
84
198
 
85
- 重新从磁盘加载 `AGENTS.md`、规则与 `config.json` 中的 instructions 片段(不重启 MCP)。
199
+ ## `GET /v1/memory`
86
200
 
87
- **响应 200**
201
+ 列出所有记忆。
88
202
 
89
203
  ```json
90
204
  {
91
- "ok": true,
92
- "instructions_chars": 1234
205
+ "memories": [
206
+ { "content": "用户喜欢中文回复", "tags": ["偏好"], "created": "2025-01-01T00:00:00Z" }
207
+ ]
93
208
  }
94
209
  ```
95
210
 
96
211
  ---
97
212
 
98
- ## `POST /v1/chat`
213
+ ## `POST /v1/memory`
99
214
 
100
- 执行一轮或多轮对话(与 CLI 使用相同的 `streamText` + 工具集)。
215
+ 添加记忆。
101
216
 
102
- ### 请求体字段
217
+ ```json
218
+ // 请求
219
+ { "content": "项目使用 bun 作为包管理器", "tags": ["project"] }
103
220
 
104
- | 字段 | 类型 | 说明 |
105
- |------|------|------|
106
- | `message` | `string` | 单轮:用户消息文本。与 `messages` 二选一(会话续写见下) |
107
- | `messages` | `array` | 多轮:完整 `ModelMessage[]`(与 AI SDK 一致:`role` + `content`)。最后一轮通常为用户消息 |
108
- | `model` | `string` | 可选,覆盖默认模型 |
109
- | `stream` | `boolean` | 为 `true` 时使用 **SSE** 流式返回 |
110
- | `session_id` | `string` | 可选,从 `~/.min-agent/sessions/<id>.json` 恢复历史,并追加本条 `message` 为新用户轮 |
111
- | `images` | `string[]` | 可选,本地图片路径(相对 cwd 或绝对路径),须与顶层 `message` 一起使用,用于构造多模态用户消息 |
221
+ // 响应
222
+ { "ok": true, "memory": { "content": "...", "tags": [...], "created": "..." } }
223
+ ```
112
224
 
113
- ### 会话续写 `session_id`
225
+ ---
114
226
 
115
- - 请求体必须包含 **`message`**(新用户发言)
116
- - 服务端加载该会话已有 `messages`,追加新用户消息后调用模型,并在本轮结束后 **写回同一 `session_id`**
227
+ ## `GET /v1/memory/search?q=bun`
117
228
 
118
- ### 非流式 `stream: false`(默认)
229
+ 搜索记忆。
119
230
 
120
- **响应 200**
231
+ ```json
232
+ { "results": [{ "content": "...", "tags": [...], "created": "...", "index": 0 }] }
233
+ ```
234
+
235
+ ---
236
+
237
+ ## `DELETE /v1/memory/:index`
238
+
239
+ 删除记忆(1-based index)。
240
+
241
+ ```json
242
+ { "ok": true, "deleted": 1 }
243
+ ```
244
+
245
+ ---
246
+
247
+ ## `GET /v1/mcp`
248
+
249
+ MCP 服务器连接状态。
121
250
 
122
251
  ```json
123
252
  {
124
- "messages": [ ... ],
125
- "assistant": { "role": "assistant", "content": "..." },
126
- "tool_calls": [{ "name": "bash", "input": { "command": "ls" } }],
127
- "tool_results": [{ "name": "bash", "output": "..." }],
128
- "session_id": "abc123",
129
- "step_count": 2,
130
- "usage": { "inputTokens": 1200, "outputTokens": 300, "totalTokens": 1500 },
131
- "has_error": false,
132
- "aborted": false
253
+ "servers": {
254
+ "filesystem": { "connected": true, "tools": ["read_file", "write_file", "list_dir"] },
255
+ "fetch": { "connected": false, "tools": [] }
256
+ }
133
257
  }
134
258
  ```
135
259
 
136
- - `messages`:已包含本轮助手回复(就地追加,与 CLI 行为一致)
137
- - `tool_results` 中单条输出过长时会被截断并附带说明后缀
138
- - 未使用 `session_id` 时 `session_id` 字段为 `undefined`/省略
260
+ ---
139
261
 
140
- ### 流式 `stream: true`
262
+ ## `GET /v1/skills`
141
263
 
142
- - **Headers**:`Content-Type: text/event-stream`
143
- - **Body**:SSE,每条事件为一行 `data: <json>\n\n`
264
+ 可用技能列表。
144
265
 
145
- 事件 `type` 取值:
266
+ ```json
267
+ {
268
+ "skills": [
269
+ { "name": "git-workflow", "description": "Git workflow guide", "location": "/path/to/SKILL.md" }
270
+ ]
271
+ }
272
+ ```
146
273
 
147
- | `type` | 字段 | 说明 |
148
- |--------|------|------|
149
- | `assistant` | `text` | 助手正文增量(已剥离思考块,与入库内容一致) |
150
- | `thinking` | `text` | 模型泄漏的思考片段(若存在) |
151
- | `tool_call` | `name`, `input` | 工具调用开始 |
152
- | `tool_result` | `name`, `output` | 工具返回(可能截断) |
153
- | `compaction` | `line` | 上下文压缩进度说明 |
154
- | `error` | `message` | 流内模型/鉴权类错误事件 |
155
- | `done` | `step_count`, `usage`, `has_error`, `aborted`, `messages`, `session_id?` | 本轮结束;之后连接关闭 |
156
- | `fatal` | `message` | 服务异常,随后关闭 |
274
+ ---
157
275
 
158
- 客户端断开连接时会 **Abort** 正在进行的生成;若已有部分正文,仍可能写入 `messages` 中的 assistant(与 CLI Ctrl+C 行为对齐)。
276
+ ## `GET /v1/rules`
159
277
 
160
- ### 错误 HTTP 状态
278
+ 已加载的规则来源。
161
279
 
162
- | 状态 | 说明 |
163
- |------|------|
164
- | `400` | JSON 无效或缺少 `message`/`messages` |
165
- | `401` | 配置了 `MIN_AGENT_SERVE_TOKEN` 但未携带合法 Bearer |
166
- | `404` | `session_id` 不存在 |
167
- | `413` | 请求体超过约 2MB |
168
- | `415` | 非 `application/json` |
169
- | `500` | 未配置提供商等内部错误 |
280
+ ```json
281
+ {
282
+ "count": 2,
283
+ "rules": [
284
+ { "source": "~/.min-agent/rules.md", "chars": 456 },
285
+ { "source": "/project/AGENTS.md", "chars": 1200 }
286
+ ]
287
+ }
288
+ ```
170
289
 
171
290
  ---
172
291
 
173
- ## `OPTIONS *`
292
+ ## `POST /v1/paste`
293
+
294
+ 发送剪贴板图片 + 文本进行多模态对话。
295
+
296
+ ```json
297
+ // 请求
298
+ {
299
+ "image_base64": "<base64 encoded image>",
300
+ "mime_type": "image/png",
301
+ "message": "这张图片里是什么?",
302
+ "model": "gpt-4o",
303
+ "session_id": "abc123",
304
+ "stream": true
305
+ }
306
+ ```
307
+
308
+ | 字段 | 类型 | 说明 |
309
+ |------|------|------|
310
+ | `image_base64` | string | **必填** Base64 编码的图片数据 |
311
+ | `mime_type` | string | 图片 MIME 类型(默认 `image/png`) |
312
+ | `message` | string | 文本 prompt(默认 "What's in this image?") |
313
+ | `model` | string | 覆盖模型 |
314
+ | `session_id` | string | 追加到已有会话 |
315
+ | `stream` | boolean | SSE 流式 |
174
316
 
175
- 当启用 `MIN_AGENT_SERVE_CORS` 时,用于浏览器预检;返回 `204`。
317
+ 响应格式与 `/v1/chat` 相同。
176
318
 
177
319
  ---
178
320
 
179
- ## 示例
321
+ ## `POST /v1/code`
180
322
 
181
- ### curl:单轮非流式
323
+ Code 模式对话(与 `min-agent code` 相同的项目感知 prompt 和工具集)。
182
324
 
183
- ```bash
184
- curl -sS http://127.0.0.1:8787/v1/chat \
185
- -H "Content-Type: application/json" \
186
- -d '{"message":"List files in current directory","model":"gpt-4o-mini"}'
325
+ 请求体与 `/v1/chat` 相同,响应额外包含 `mode: "code"` 和 `project` 字段。
326
+
327
+ ```json
328
+ // 请求
329
+ { "message": "add error handling to the login function", "stream": true }
330
+
331
+ // 响应(非流式)额外字段
332
+ { "mode": "code", "project": { "languages": ["TypeScript"], "framework": "Next.js", ... }, ... }
187
333
  ```
188
334
 
189
- ### curl:SSE 流式
335
+ ---
190
336
 
191
- ```bash
192
- curl -sS -N http://127.0.0.1:8787/v1/chat \
193
- -H "Content-Type: application/json" \
194
- -d '{"message":"Say hi in one sentence","stream":true}'
337
+ ## `POST /v1/mcp`
338
+
339
+ 添加 MCP 服务器。
340
+
341
+ ```json
342
+ // 本地 stdio
343
+ { "name": "filesystem", "command": ["npx", "-y", "@modelcontextprotocol/server-filesystem", "/tmp"] }
344
+
345
+ // 远程 HTTP
346
+ { "name": "remote", "url": "https://mcp.example.com", "token": "xxx" }
195
347
  ```
196
348
 
197
- ### 带鉴权
349
+ 响应:`{ "ok": true, "name": "filesystem" }`
198
350
 
199
- ```bash
200
- export MIN_AGENT_SERVE_TOKEN=secret
201
- min-agent serve &
202
- curl -sS http://127.0.0.1:8787/health \
203
- -H "Authorization: Bearer secret"
351
+ 注意:添加后需重启 serve 进程才能连接新服务器。
352
+
353
+ ---
354
+
355
+ ## `DELETE /v1/mcp/:name`
356
+
357
+ 删除 MCP 服务器配置。
358
+
359
+ ```json
360
+ { "ok": true, "deleted": "filesystem" }
204
361
  ```
205
362
 
206
363
  ---
207
364
 
208
- ## 与 OpenCode / 其他客户端的对比说明
365
+ ## 错误码
209
366
 
210
- API **不是** OpenAI Chat Completions 的完全兼容实现;字段与事件名为 min-agent 专用。若需对接现有 OpenAI SDK,请在网关层做映射。
367
+ | 状态 | 说明 |
368
+ |------|------|
369
+ | 400 | 请求参数错误 |
370
+ | 401 | 未授权 |
371
+ | 404 | 资源不存在 |
372
+ | 413 | 请求体过大(>2MB) |
373
+ | 415 | Content-Type 不是 JSON |
374
+ | 500 | 内部错误 |
211
375
 
212
376
  ---
213
377
 
214
- ## 版本
378
+ ## 示例
379
+
380
+ ```bash
381
+ # 单轮对话
382
+ curl http://127.0.0.1:8787/v1/chat \
383
+ -H "Content-Type: application/json" \
384
+ -d '{"message":"list files"}'
215
385
 
216
- 文档与实现随仓库版本迭代;`GET /health` 中的 `version` 来自项目根目录 `package.json`。
386
+ # 流式
387
+ curl -N http://127.0.0.1:8787/v1/chat \
388
+ -H "Content-Type: application/json" \
389
+ -d '{"message":"say hi","stream":true}'
390
+
391
+ # 添加记忆
392
+ curl -X POST http://127.0.0.1:8787/v1/memory \
393
+ -H "Content-Type: application/json" \
394
+ -d '{"content":"prefer TypeScript","tags":["preference"]}'
395
+
396
+ # 项目扫描
397
+ curl http://127.0.0.1:8787/v1/project
398
+
399
+ # 压缩会话
400
+ curl -X POST http://127.0.0.1:8787/v1/chat/compact \
401
+ -H "Content-Type: application/json" \
402
+ -d '{"session_id":"abc123"}'
403
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "min-agent",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "type": "module",
5
5
  "description": "Minimal AI coding agent with tool use, MCP, and skills support",
6
6
  "license": "MIT",