min-agent 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) min-agent contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,251 @@
1
+ # min-agent
2
+
3
+ 轻量级 AI Coding Agent,支持工具调用(读写文件、Shell、搜索)、MCP 与 Skill。
4
+ 兼容 OpenAI 风格 API(OpenAI、Ollama、vLLM、MiniMax、DeepSeek 等)。
5
+
6
+ ## 安装
7
+
8
+ ```bash
9
+ npm install -g min-agent
10
+ ```
11
+
12
+ ## 快速开始
13
+
14
+ ```bash
15
+ # 1) 交互式配置 provider / api key / 默认模型
16
+ min-agent setup
17
+
18
+ # 2) 单轮问答
19
+ min-agent chat "hello"
20
+ min-agent chat "read package.json and tell me the version"
21
+ min-agent chat "帮我搜索一下最新的 AI 新闻"
22
+
23
+ # 3) 进入多轮对话
24
+ min-agent chat
25
+ ```
26
+
27
+ ## 首次运行会自动初始化
28
+
29
+ 在当前项目目录自动确保以下结构存在:
30
+
31
+ - `.min-agent/skills/`
32
+ - `.min-agent/mcp.json`(默认 `{ "mcpServers": {} }`)
33
+
34
+ 用户全局技能目录 `~/.agents/skills/` 不会自动创建;若你自行创建并放入 `SKILL.md`,启动时会被扫描加载(见下文「Skill 安装」)。
35
+
36
+ ## CLI 命令
37
+
38
+ | Command | Description |
39
+ |---------|-------------|
40
+ | `min-agent setup` | 交互配置 API URL、Key、默认模型 |
41
+ | `min-agent chat <message>` | 单轮消息 |
42
+ | `min-agent chat` | 多轮交互聊天 |
43
+ | `min-agent chat -m <model> <message>` | 指定模型发送消息 |
44
+ | `min-agent models` | 拉取并列出可用模型 |
45
+ | `min-agent mcp add <name> <cmd...>` | 添加本地 MCP(stdio,默认先验证) |
46
+ | `min-agent mcp add <name> --url <url> [--sse] [--token …]` | 添加远程 MCP(HTTP) |
47
+ | `min-agent mcp remove <name>` | 删除 MCP 服务 |
48
+ | `min-agent mcp list` | 列出 MCP 服务 |
49
+ | `min-agent mcp check` | 检查所有 MCP 服务可用性 |
50
+ | `min-agent skills list` | 列出已发现 skills |
51
+ | `min-agent rules` | 查看已加载规则来源 |
52
+ | `min-agent rules edit` | 编辑全局规则文件 |
53
+ | `min-agent history` | 查看会话历史 |
54
+ | `min-agent memory` | 查看记忆 |
55
+ | `min-agent memory add <text>` | 新增记忆 |
56
+ | `min-agent memory search <query>` | 检索记忆 |
57
+ | `min-agent memory delete <index>` | 删除记忆 |
58
+
59
+ ## 交互聊天 Slash 命令(`min-agent chat` 内)
60
+
61
+ | Slash Command | Description |
62
+ |---------------|-------------|
63
+ | `/clear` | 清空当前会话消息 |
64
+ | `/compact` | 强制压缩上下文 |
65
+ | `/model [name]` | 查看或设置默认模型 |
66
+ | `/models` | 拉取并列出 provider 模型 |
67
+ | `/memory [text]` | 查看记忆或保存记忆 |
68
+ | `/skills` | 列出当前已发现 skills |
69
+ | `/mcp` | 查看 MCP 服务与连接状态 |
70
+ | `/tokens` | 显示上下文 token 估算 |
71
+ | `/help` | 显示帮助 |
72
+ | `/exit` | 退出聊天 |
73
+
74
+ ## 配置文件与加载规则
75
+
76
+ ### Provider 配置
77
+
78
+ 保存在 `~/.min-agent/config.json`:
79
+
80
+ ```json
81
+ {
82
+ "provider": {
83
+ "type": "openai-compatible",
84
+ "baseURL": "https://api.openai.com/v1",
85
+ "apiKey": "sk-...",
86
+ "defaultModel": "gpt-4o"
87
+ }
88
+ }
89
+ ```
90
+
91
+ 说明:
92
+
93
+ - `setup` 时根据填写信息自动拉取模型列表。
94
+ - Ollama 支持填 `http://host:11434` 或 `http://host:11434/v1`,会自动兼容。
95
+
96
+ ### MCP 配置
97
+
98
+ 项目级配置:`.min-agent/mcp.json`
99
+
100
+ ### 规则(Instructions)加载顺序
101
+
102
+ 1. 全局规则:`~/.min-agent/rules.md`
103
+ 2. 项目规则(从当前目录向上查找首个命中):
104
+ - `./AGENTS.md`
105
+ - `./RULES.md`
106
+ - `./CLAUDE.md`
107
+ - `./.min-agent/AGENTS.md`
108
+ 3. `~/.min-agent/config.json` 的 `instructions` 字段中声明的本地文件与远程 URL
109
+
110
+ ## Built-in Tools
111
+
112
+ | Tool | Description |
113
+ |------|-------------|
114
+ | `bash` | 执行 shell 命令 |
115
+ | `read` | 读取文件 |
116
+ | `write` | 写入/创建文件 |
117
+ | `edit` | 文本替换编辑 |
118
+ | `glob` | 按模式查找文件 |
119
+ | `grep` | 按内容搜索 |
120
+ | `web_search` | 联网搜索(SearXNG) |
121
+ | `web_fetch` | 抓取网页内容(含 Firecrawl SPA 兜底) |
122
+
123
+ ## 工具输出与截断
124
+
125
+ 内置工具与 MCP 返回的文本若超过约 **2000 行**或 **50KB(UTF-8)**,会将**全文**保存到 `~/.min-agent/tool-output/tool-*.txt`,并在返回内容中给出**预览 + 文件路径**;请用 `read`(`startLine`/`endLine`)或 `grep` 分段查看。
126
+
127
+ - **`bash`**:使用流式收集;内存中累计超过 **16MB** 会终止进程并提示改用重定向到文件。预览默认保留**输出尾部**(便于看日志)。
128
+ - **其它文本类工具**(`read`、`grep`、`web_fetch`、`web_search`、`glob`、MCP):超长时预览默认保留**开头**(`head`),全文仍在上述目录。
129
+
130
+ 该目录下旧文件会按修改时间做简单清理(约保留 7 天)。
131
+
132
+ ## Skill 安装
133
+
134
+ 会在以下目录递归查找 `**/SKILL.md`(**靠后的路径覆盖同名 skill**,即项目内优先于全局):
135
+
136
+ 1. `~/.agents/skills/`(用户全局,跨项目共享)
137
+ 2. `<cwd>/.min-agent/skills/`
138
+ 3. `<cwd>/.agent-demo/skills/`
139
+ 4. `<cwd>/.opencode/skills/`
140
+ 5. `<cwd>/.claude/skills/`
141
+
142
+ 创建目录与文件示例:
143
+
144
+ ```text
145
+ ~/.agents/skills/<your-skill>/SKILL.md
146
+ .min-agent/skills/<your-skill>/SKILL.md
147
+ ```
148
+
149
+ `SKILL.md` 最小格式:
150
+
151
+ ```markdown
152
+ ---
153
+ name: my-skill
154
+ description: What this skill does
155
+ ---
156
+
157
+ # Instructions for the agent...
158
+ ```
159
+
160
+ 可用命令:
161
+
162
+ ```bash
163
+ min-agent skills list
164
+ ```
165
+
166
+ ## MCP 安装
167
+
168
+ 配置在项目目录 `.min-agent/mcp.json`。
169
+
170
+ ### 本地 MCP(stdio)
171
+
172
+ ```bash
173
+ min-agent mcp add filesystem npx -y @modelcontextprotocol/server-filesystem /tmp
174
+ min-agent mcp add memory npx -y @modelcontextprotocol/server-memory
175
+ min-agent mcp add custom --skip-check node ./my-mcp-server.js
176
+ ```
177
+
178
+ ### 远程 MCP(HTTP)
179
+
180
+ 通过 `url` 连接远端 MCP,使用 `@modelcontextprotocol/sdk` 的 **Streamable HTTP**;默认 `remoteTransport` 为 `auto`:先尝试 Streamable HTTP,失败再回退 **SSE**(兼容旧版 HTTP+SSE)。
181
+
182
+ ```bash
183
+ min-agent mcp add myremote --url https://api.example.com/mcp
184
+ min-agent mcp add myremote --url https://api.example.com/mcp --token "$YOUR_BEARER_TOKEN"
185
+ min-agent mcp add legacy --url https://old.example.com/sse --sse
186
+ min-agent mcp add legacy --url https://old.example.com/sse --ext/event-stream
187
+ min-agent mcp list
188
+ min-agent mcp check
189
+ ```
190
+
191
+ 手写配置示例:
192
+
193
+ ```json
194
+ {
195
+ "mcpServers": {
196
+ "remote-demo": {
197
+ "url": "https://example.com/mcp",
198
+ "remoteTransport": "auto",
199
+ "token": "optional-bearer-token",
200
+ "headers": { "X-Custom-Header": "value" },
201
+ "enabled": true
202
+ }
203
+ }
204
+ }
205
+ ```
206
+
207
+ - `token`:可选;若未设置 `Authorization`,会自动添加 `Bearer <token>`。
208
+ - `remoteTransport`:`auto` | `streamable-http` | `sse`;CLI 中 `--sse` 等价于 `sse`。
209
+
210
+ 说明:
211
+
212
+ - `mcp add` 默认会先验证连通性;验证失败不会写入配置。
213
+ - 离线或私有场景可使用 `--skip-check` 跳过添加时验证。
214
+ - `mcp check` 会检查配置中所有服务(含远程),若有失败会返回非零退出码。
215
+
216
+ 聊天内可用:
217
+
218
+ ```text
219
+ /mcp
220
+ ```
221
+
222
+ ## 常见问题(Troubleshooting)
223
+
224
+ ### 1) `AI_APICallError: Not Found`
225
+
226
+ 常见原因是 provider `baseURL` 不匹配(例如 Ollama 未使用 `/v1` 路径)。
227
+ 建议先执行:
228
+
229
+ ```bash
230
+ min-agent setup
231
+ min-agent models
232
+ ```
233
+
234
+ 确认可以正常拉取模型列表。
235
+
236
+ ### 2) `models` 拉取为空
237
+
238
+ - 检查 `apiKey`、`baseURL` 是否正确
239
+ - 检查网络或服务端是否可达
240
+ - 检查服务是否实现 `/models`(Ollama 已做 `/v1/models` 兼容重试)
241
+
242
+ ### 3) MCP 添加后不可用
243
+
244
+ - 先 `min-agent mcp list` 看是否已配置
245
+ - 在聊天内 `/mcp` 看连接状态(connected/disconnected/disabled)
246
+ - **本地 stdio**:检查命令是否可执行(如 `npx` 是否可用)
247
+ - **远程 URL**:确认地址为 `http(s)://`,必要时加 `--sse` 强制使用旧版 SSE 传输,或用 `mcp check` 查看具体错误信息
248
+
249
+ ## License
250
+
251
+ MIT
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env bun
2
+ import "../src/cli.ts"
package/docs/API.md ADDED
@@ -0,0 +1,216 @@
1
+ # min-agent HTTP API
2
+
3
+ 通过 `min-agent serve` 启动本地 HTTP 服务,以编程方式调用与 CLI `chat` 相同的代理能力(同一套 MCP、技能、规则、工具与模型配置)。
4
+
5
+ ## 启动
6
+
7
+ ```bash
8
+ min-agent serve
9
+ min-agent serve --host 127.0.0.1 --port 8787
10
+ min-agent serve -p 3030
11
+ ```
12
+
13
+ 环境变量(可选):
14
+
15
+ | 变量 | 说明 |
16
+ |------|------|
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 等确认会自动通过,请勿把未鉴权服务暴露到公网。
23
+
24
+ ---
25
+
26
+ ## 通用约定
27
+
28
+ - **Base URL**:`http://<host>:<port>`
29
+ - **请求体**:JSON,`Content-Type: application/json`
30
+ - **响应体**:JSON,除非注明为 SSE
31
+ - **工作目录**:与启动 `serve` 时的 `process.cwd()` 一致(影响 `read`/`bash` 等工具路径)
32
+ - **单例初始化**:进程启动时连接 MCP、扫描技能并加载规则;之后请求复用该状态。修改规则文件后可调用下方「重载规则」接口或重启进程
33
+
34
+ ---
35
+
36
+ ## `GET /health`
37
+
38
+ 存活检查。
39
+
40
+ **响应 200**
41
+
42
+ ```json
43
+ {
44
+ "ok": true,
45
+ "service": "min-agent",
46
+ "version": "0.1.0"
47
+ }
48
+ ```
49
+
50
+ ---
51
+
52
+ ## `GET /v1/meta`
53
+
54
+ 运行环境摘要。
55
+
56
+ **响应 200**
57
+
58
+ ```json
59
+ {
60
+ "version": "0.1.0",
61
+ "cwd": "/path/to/project",
62
+ "instructions_chars": 1234
63
+ }
64
+ ```
65
+
66
+ ---
67
+
68
+ ## `GET /v1/models`
69
+
70
+ 从当前配置的 OpenAI 兼容提供商拉取模型列表(与 CLI `min-agent models` 同源)。
71
+
72
+ **响应 200**
73
+
74
+ ```json
75
+ {
76
+ "default_model": "gpt-4.1",
77
+ "models": ["gpt-4.1", "gpt-4o-mini", "..."]
78
+ }
79
+ ```
80
+
81
+ ---
82
+
83
+ ## `POST /v1/chat/reload-instructions`
84
+
85
+ 重新从磁盘加载 `AGENTS.md`、规则与 `config.json` 中的 instructions 片段(不重启 MCP)。
86
+
87
+ **响应 200**
88
+
89
+ ```json
90
+ {
91
+ "ok": true,
92
+ "instructions_chars": 1234
93
+ }
94
+ ```
95
+
96
+ ---
97
+
98
+ ## `POST /v1/chat`
99
+
100
+ 执行一轮或多轮对话(与 CLI 使用相同的 `streamText` + 工具集)。
101
+
102
+ ### 请求体字段
103
+
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` 一起使用,用于构造多模态用户消息 |
112
+
113
+ ### 会话续写 `session_id`
114
+
115
+ - 请求体必须包含 **`message`**(新用户发言)
116
+ - 服务端加载该会话已有 `messages`,追加新用户消息后调用模型,并在本轮结束后 **写回同一 `session_id`**
117
+
118
+ ### 非流式 `stream: false`(默认)
119
+
120
+ **响应 200**
121
+
122
+ ```json
123
+ {
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
133
+ }
134
+ ```
135
+
136
+ - `messages`:已包含本轮助手回复(就地追加,与 CLI 行为一致)
137
+ - `tool_results` 中单条输出过长时会被截断并附带说明后缀
138
+ - 未使用 `session_id` 时 `session_id` 字段为 `undefined`/省略
139
+
140
+ ### 流式 `stream: true`
141
+
142
+ - **Headers**:`Content-Type: text/event-stream`
143
+ - **Body**:SSE,每条事件为一行 `data: <json>\n\n`
144
+
145
+ 事件 `type` 取值:
146
+
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` | 服务异常,随后关闭 |
157
+
158
+ 客户端断开连接时会 **Abort** 正在进行的生成;若已有部分正文,仍可能写入 `messages` 中的 assistant(与 CLI Ctrl+C 行为对齐)。
159
+
160
+ ### 错误 HTTP 状态
161
+
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` | 未配置提供商等内部错误 |
170
+
171
+ ---
172
+
173
+ ## `OPTIONS *`
174
+
175
+ 当启用 `MIN_AGENT_SERVE_CORS` 时,用于浏览器预检;返回 `204`。
176
+
177
+ ---
178
+
179
+ ## 示例
180
+
181
+ ### curl:单轮非流式
182
+
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"}'
187
+ ```
188
+
189
+ ### curl:SSE 流式
190
+
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}'
195
+ ```
196
+
197
+ ### 带鉴权
198
+
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"
204
+ ```
205
+
206
+ ---
207
+
208
+ ## 与 OpenCode / 其他客户端的对比说明
209
+
210
+ 本 API **不是** OpenAI Chat Completions 的完全兼容实现;字段与事件名为 min-agent 专用。若需对接现有 OpenAI SDK,请在网关层做映射。
211
+
212
+ ---
213
+
214
+ ## 版本
215
+
216
+ 文档与实现随仓库版本迭代;`GET /health` 中的 `version` 来自项目根目录 `package.json`。
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "min-agent",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "description": "Minimal AI coding agent with tool use, MCP, and skills support",
6
+ "license": "MIT",
7
+ "bin": {
8
+ "min-agent": "./bin/min-agent.js"
9
+ },
10
+ "engines": {
11
+ "bun": ">=1.0.0"
12
+ },
13
+ "files": [
14
+ "bin",
15
+ "src",
16
+ "docs",
17
+ "tsconfig.json",
18
+ "README.md",
19
+ "LICENSE"
20
+ ],
21
+ "publishConfig": {
22
+ "access": "public"
23
+ },
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git+https://gitee.com/lemon8510/min-agent.git"
27
+ },
28
+ "bugs": {
29
+ "url": "https://gitee.com/lemon8510/min-agent/issues"
30
+ },
31
+ "homepage": "https://gitee.com/lemon8510/min-agent",
32
+ "scripts": {
33
+ "dev": "bun run src/cli.ts",
34
+ "build": "tsc",
35
+ "typecheck": "tsc --noEmit",
36
+ "prepublishOnly": "npm run typecheck"
37
+ },
38
+ "keywords": [
39
+ "ai",
40
+ "agent",
41
+ "cli",
42
+ "coding",
43
+ "mcp",
44
+ "llm",
45
+ "openai-compatible",
46
+ "coding-agent",
47
+ "http-api"
48
+ ],
49
+ "dependencies": {
50
+ "@ai-sdk/openai": "3.0.53",
51
+ "@ai-sdk/provider": "3.0.8",
52
+ "@modelcontextprotocol/sdk": "1.27.1",
53
+ "ai": "6.0.168",
54
+ "glob": "13.0.5"
55
+ },
56
+ "devDependencies": {
57
+ "@types/node": "22.13.9",
58
+ "tsx": "4.19.4",
59
+ "typescript": "5.8.2"
60
+ }
61
+ }