ai-helper-agent 1.14.8 → 1.14.9
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.md +198 -198
- package/README.zh-CN.md +372 -0
- package/package.json +2 -2
- package/README.en.md +0 -372
package/README.md
CHANGED
|
@@ -1,117 +1,117 @@
|
|
|
1
|
-
> [English](./README.
|
|
1
|
+
> [English](./README.md) | [中文](./README.zh-CN.md)
|
|
2
2
|
|
|
3
3
|
# AI Helper Agent
|
|
4
4
|
|
|
5
|
-
AI Helper
|
|
5
|
+
AI Helper Agent — a local proxy service that provides the [AI Helper Chrome Extension](https://github.com/xiweicheng/ai-helper) with local file read/write, system command execution, Skill system, and MCP protocol extension capabilities.
|
|
6
6
|
|
|
7
|
-
##
|
|
7
|
+
## Installation
|
|
8
8
|
|
|
9
9
|
```bash
|
|
10
10
|
npm install -g ai-helper-agent
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
Requires Node.js >= 18.0.0.
|
|
14
14
|
|
|
15
|
-
##
|
|
15
|
+
## Quick Start
|
|
16
16
|
|
|
17
17
|
```bash
|
|
18
|
-
#
|
|
18
|
+
# Start in foreground (real-time logs in terminal, ideal for debugging)
|
|
19
19
|
ai-helper-agent start
|
|
20
20
|
|
|
21
|
-
#
|
|
21
|
+
# Start in background (daemon mode, terminal returns immediately)
|
|
22
22
|
ai-helper-agent start --background
|
|
23
23
|
ai-helper-agent start -b
|
|
24
24
|
|
|
25
|
-
#
|
|
25
|
+
# Specify work directory and port
|
|
26
26
|
ai-helper-agent start --workdir /path/to/your/project --port 18911
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
After startup, the terminal will display a 4-digit pairing code. Enter it in the "Agent" tab of the Chrome extension settings page to complete pairing.
|
|
30
30
|
|
|
31
|
-
## CLI
|
|
31
|
+
## CLI Commands
|
|
32
32
|
|
|
33
|
-
|
|
|
34
|
-
|
|
35
|
-
| `start` |
|
|
36
|
-
| `start --background` / `start -b` |
|
|
37
|
-
| `stop` |
|
|
38
|
-
| `restart` |
|
|
39
|
-
| `status` |
|
|
40
|
-
| `paircode` |
|
|
41
|
-
| `config` |
|
|
42
|
-
| `help` |
|
|
43
|
-
| `--version` / `-v` |
|
|
33
|
+
| Command | Description |
|
|
34
|
+
|---------|-------------|
|
|
35
|
+
| `start` | Start the agent service (foreground, with real-time logs) |
|
|
36
|
+
| `start --background` / `start -b` | Start in background (daemon mode) |
|
|
37
|
+
| `stop` | Stop the running agent |
|
|
38
|
+
| `restart` | Restart the agent service (supports -b for background restart) |
|
|
39
|
+
| `status` | Check running status |
|
|
40
|
+
| `paircode` | Show pairing code |
|
|
41
|
+
| `config` | View current configuration |
|
|
42
|
+
| `help` | Show help information |
|
|
43
|
+
| `--version` / `-v` | Show version number |
|
|
44
44
|
|
|
45
|
-
###
|
|
45
|
+
### Startup Options
|
|
46
46
|
|
|
47
47
|
```
|
|
48
|
-
--background, -b
|
|
49
|
-
--port
|
|
50
|
-
--host
|
|
51
|
-
--workdir
|
|
48
|
+
--background, -b Run in background (daemon mode)
|
|
49
|
+
--port <port> Listen port, default 18910
|
|
50
|
+
--host <address> Listen address, default 127.0.0.1
|
|
51
|
+
--workdir <dir> Work directory (file read/write restricted to this scope)
|
|
52
52
|
```
|
|
53
53
|
|
|
54
|
-
###
|
|
54
|
+
### Foreground vs Background
|
|
55
55
|
|
|
56
|
-
|
|
|
57
|
-
|
|
58
|
-
|
|
|
59
|
-
|
|
|
56
|
+
| Mode | Command | Terminal Behavior | Use Case |
|
|
57
|
+
|------|---------|-------------------|----------|
|
|
58
|
+
| Foreground | `start` | Blocks terminal, shows real-time logs | Development, debugging |
|
|
59
|
+
| Background | `start --background` | Returns immediately, no log output | Production, daily use |
|
|
60
60
|
|
|
61
|
-
|
|
61
|
+
When running in foreground, all file read/write, command execution, and security interception logs are output in real-time in a formatted manner:
|
|
62
62
|
|
|
63
63
|
```
|
|
64
|
-
[12:30:01] [INFO] [
|
|
65
|
-
[12:30:05] [INFO] [
|
|
66
|
-
[12:30:12] [INFO] [
|
|
67
|
-
[12:31:00] [WARN] [
|
|
64
|
+
[12:30:01] [INFO] [File:read] path=/project/src/index.js size=2048
|
|
65
|
+
[12:30:05] [INFO] [Command:started] command="npm test" cwd=/project execId=a1b2c3d4
|
|
66
|
+
[12:30:12] [INFO] [Command:completed] execId=a1b2c3d4 exitCode=0 killed=false
|
|
67
|
+
[12:31:00] [WARN] [Security:exec_denied] command="rm -rf /" reason=High-risk command blocked
|
|
68
68
|
```
|
|
69
69
|
|
|
70
|
-
###
|
|
70
|
+
### Process Management
|
|
71
71
|
|
|
72
|
-
|
|
72
|
+
The agent manages process lifecycle via a PID file (`~/.ai-helper-agent/agent.pid`):
|
|
73
73
|
|
|
74
|
-
-
|
|
75
|
-
- `stop`
|
|
76
|
-
-
|
|
74
|
+
- Automatically writes PID file on startup
|
|
75
|
+
- `stop` command prefers graceful shutdown via API, falls back to killing process via PID file
|
|
76
|
+
- Automatically cleans up PID file on normal shutdown
|
|
77
77
|
|
|
78
|
-
##
|
|
78
|
+
## Security
|
|
79
79
|
|
|
80
|
-
###
|
|
80
|
+
### File Sandbox
|
|
81
81
|
|
|
82
|
-
|
|
82
|
+
All file operations are restricted to directories listed in the `allowedPaths` whitelist. `realpath` resolution prevents symlink bypasses, ensuring physical paths are safe.
|
|
83
83
|
|
|
84
|
-
###
|
|
84
|
+
### Command Tiered Control
|
|
85
85
|
|
|
86
|
-
|
|
86
|
+
**Blacklist** — Blocked directly, cannot be bypassed:
|
|
87
87
|
|
|
88
|
-
|
|
|
89
|
-
|
|
90
|
-
|
|
|
91
|
-
|
|
|
92
|
-
|
|
|
93
|
-
| Shell
|
|
88
|
+
| Type | Examples |
|
|
89
|
+
|------|----------|
|
|
90
|
+
| Disk destruction | `rm -rf /`, `mkfs.*`, `dd if=... of=/dev/...` |
|
|
91
|
+
| System file overwrite | `> /etc/passwd`, `> /etc/shadow` |
|
|
92
|
+
| Malicious pipe execution | `curl ... \| bash`, `git clone ... \| sh` |
|
|
93
|
+
| Shell injection | Backticks, `$()`, `${}` command substitution |
|
|
94
94
|
| Fork bomb | `:(){ :\|:& };:` |
|
|
95
95
|
|
|
96
|
-
|
|
96
|
+
**Greylist** — Requires user confirmation before execution:
|
|
97
97
|
|
|
98
|
-
|
|
|
99
|
-
|
|
100
|
-
| `sudo ...` |
|
|
101
|
-
| `npm install -g ...` |
|
|
102
|
-
| `pip install/uninstall` | Python
|
|
103
|
-
| `chmod -R 777` |
|
|
104
|
-
| `rm -rf ...` |
|
|
105
|
-
| `git push --force` |
|
|
106
|
-
| `shutdown/reboot` |
|
|
98
|
+
| Command Pattern | Reason |
|
|
99
|
+
|-----------------|--------|
|
|
100
|
+
| `sudo ...` | Requires admin privileges |
|
|
101
|
+
| `npm install -g ...` | Global package installation |
|
|
102
|
+
| `pip install/uninstall` | Python package management |
|
|
103
|
+
| `chmod -R 777` | Recursive permission modification |
|
|
104
|
+
| `rm -rf ...` | Recursive forced deletion |
|
|
105
|
+
| `git push --force` | Force push |
|
|
106
|
+
| `shutdown/reboot` | System shutdown/restart |
|
|
107
107
|
|
|
108
|
-
###
|
|
108
|
+
### Authentication
|
|
109
109
|
|
|
110
|
-
|
|
110
|
+
Pairing is completed by entering the 4-digit pairing code displayed in the terminal into the extension. The pairing code rotates every 30 seconds. After successful pairing, an HMAC token is used for subsequent request authentication, stored in `~/.ai-helper-agent/pairings.json`.
|
|
111
111
|
|
|
112
|
-
##
|
|
112
|
+
## Configuration
|
|
113
113
|
|
|
114
|
-
|
|
114
|
+
Configuration file path: `~/.ai-helper-agent/config.json`
|
|
115
115
|
|
|
116
116
|
```json
|
|
117
117
|
{
|
|
@@ -125,209 +125,209 @@ Agent 通过 PID 文件(`~/.ai-helper-agent/agent.pid`)管理进程生命周
|
|
|
125
125
|
}
|
|
126
126
|
```
|
|
127
127
|
|
|
128
|
-
|
|
|
129
|
-
|
|
130
|
-
| `port` |
|
|
131
|
-
| `host` |
|
|
132
|
-
| `workdir` |
|
|
133
|
-
| `allowedPaths` |
|
|
134
|
-
| `pairCodeTTL` |
|
|
135
|
-
| `commandTimeout` |
|
|
136
|
-
| `fileMaxSize` |
|
|
128
|
+
| Field | Description | Default |
|
|
129
|
+
|-------|-------------|---------|
|
|
130
|
+
| `port` | Listen port | 18910 |
|
|
131
|
+
| `host` | Listen address | 127.0.0.1 |
|
|
132
|
+
| `workdir` | Default work directory | Current directory at startup |
|
|
133
|
+
| `allowedPaths` | Additional allowed directory list | `[]` |
|
|
134
|
+
| `pairCodeTTL` | Pairing code validity (seconds) | 30 |
|
|
135
|
+
| `commandTimeout` | Command execution timeout (ms) | 300000 (5 min) |
|
|
136
|
+
| `fileMaxSize` | Max file read/write size (bytes) | 52428800 (50 MB) |
|
|
137
137
|
|
|
138
|
-
##
|
|
138
|
+
## File Search
|
|
139
139
|
|
|
140
|
-
|
|
140
|
+
The agent prefers native system search tools, with automatic fallback to Node.js implementation when unavailable:
|
|
141
141
|
|
|
142
|
-
|
|
|
143
|
-
|
|
144
|
-
| `fd` |
|
|
145
|
-
| `rg` (ripgrep) |
|
|
142
|
+
| Engine | Purpose | Detection Command |
|
|
143
|
+
|--------|---------|-------------------|
|
|
144
|
+
| `fd` | Filename search (fast) | `fd --version` |
|
|
145
|
+
| `rg` (ripgrep) | File content search (fast) | `rg --version` |
|
|
146
146
|
|
|
147
|
-
|
|
147
|
+
The status endpoint reports currently available search tools:
|
|
148
148
|
|
|
149
149
|
```json
|
|
150
150
|
{ "searchTools": { "fd": true, "rg": true } }
|
|
151
151
|
```
|
|
152
152
|
|
|
153
|
-
##
|
|
153
|
+
## Audit Logs
|
|
154
154
|
|
|
155
|
-
|
|
155
|
+
All operations are automatically recorded in audit logs, located in `~/.ai-helper-agent/logs/`.
|
|
156
156
|
|
|
157
|
-
###
|
|
157
|
+
### Dual Channel Output
|
|
158
158
|
|
|
159
|
-
-
|
|
160
|
-
-
|
|
159
|
+
- **Terminal output** (foreground mode): Formatted human-readable output to stderr
|
|
160
|
+
- **File output**: JSON Lines format, written to log files
|
|
161
161
|
|
|
162
|
-
|
|
162
|
+
Both channels work simultaneously without interfering with each other.
|
|
163
163
|
|
|
164
|
-
###
|
|
164
|
+
### Log Format
|
|
165
165
|
|
|
166
|
-
JSON Lines
|
|
166
|
+
JSON Lines format, one record per line, files named by date `agent-YYYY-MM-DD.log`.
|
|
167
167
|
|
|
168
168
|
```json
|
|
169
169
|
{"timestamp":"2026-01-15T10:30:00.123Z","level":"info","category":"fs","action":"read","path":"/home/user/project/src/index.js","size":2048}
|
|
170
170
|
{"timestamp":"2026-01-15T10:30:05.456Z","level":"info","category":"exec","action":"completed","command":"npm test","cwd":"/home/user/project","execId":"a1b2c3d4","exitCode":0,"killed":false,"stdoutLen":1024,"stderrLen":0}
|
|
171
|
-
{"timestamp":"2026-01-15T10:31:00.789Z","level":"warn","category":"security","action":"exec_denied","command":"rm -rf /","reason":"
|
|
171
|
+
{"timestamp":"2026-01-15T10:31:00.789Z","level":"warn","category":"security","action":"exec_denied","command":"rm -rf /","reason":"High-risk command blocked"}
|
|
172
172
|
```
|
|
173
173
|
|
|
174
|
-
###
|
|
174
|
+
### Log Categories
|
|
175
175
|
|
|
176
|
-
|
|
|
177
|
-
|
|
178
|
-
| `auth` |
|
|
179
|
-
| `fs` |
|
|
180
|
-
| `exec` |
|
|
181
|
-
| `security` |
|
|
182
|
-
| `system` |
|
|
176
|
+
| Category | Description | Included Actions |
|
|
177
|
+
|----------|-------------|-----------------|
|
|
178
|
+
| `auth` | Authentication events | Pairing success/failure |
|
|
179
|
+
| `fs` | File operations | read, write, list, delete, search_files, search_content |
|
|
180
|
+
| `exec` | Command execution | started, completed, stopped, error |
|
|
181
|
+
| `security` | Security events | deny, confirm, auth failure, path breach interception |
|
|
182
|
+
| `system` | System events | server_start, server_stop, shutdown, server_error, uncaught_exception, unhandled_rejection |
|
|
183
183
|
|
|
184
|
-
###
|
|
184
|
+
### Log Query API
|
|
185
185
|
|
|
186
186
|
```
|
|
187
187
|
GET /api/logs?date=2026-01-15&category=security&limit=50&offset=0
|
|
188
188
|
GET /api/logs/dates
|
|
189
189
|
```
|
|
190
190
|
|
|
191
|
-
- `date` -
|
|
192
|
-
- `category` -
|
|
193
|
-
- `limit` -
|
|
194
|
-
- `offset` -
|
|
191
|
+
- `date` - Date filter (YYYY-MM-DD), defaults to today
|
|
192
|
+
- `category` - Category filter, returns all if omitted
|
|
193
|
+
- `limit` - Max number of records returned, defaults to 200
|
|
194
|
+
- `offset` - Pagination offset
|
|
195
195
|
|
|
196
|
-
|
|
196
|
+
Results are returned in reverse chronological order (newest first).
|
|
197
197
|
|
|
198
|
-
###
|
|
198
|
+
### Auto Cleanup
|
|
199
199
|
|
|
200
|
-
-
|
|
201
|
-
-
|
|
202
|
-
-
|
|
200
|
+
- Maximum 30 log files retained
|
|
201
|
+
- Single files exceeding 10 MB are automatically deleted
|
|
202
|
+
- Cleanup check triggered on every log write
|
|
203
203
|
|
|
204
|
-
## API
|
|
204
|
+
## API Endpoints
|
|
205
205
|
|
|
206
|
-
###
|
|
206
|
+
### No Authentication Required
|
|
207
207
|
|
|
208
|
-
|
|
|
209
|
-
|
|
210
|
-
| POST | `/api/pair` |
|
|
211
|
-
| GET | `/api/status` |
|
|
212
|
-
| POST | `/api/shutdown` |
|
|
208
|
+
| Method | Path | Description |
|
|
209
|
+
|--------|------|-------------|
|
|
210
|
+
| POST | `/api/pair` | Pairing authentication |
|
|
211
|
+
| GET | `/api/status` | Health check (version, platform info, search tool availability) |
|
|
212
|
+
| POST | `/api/shutdown` | Shut down the agent service (local access only) |
|
|
213
213
|
|
|
214
|
-
###
|
|
214
|
+
### Authentication Required (Bearer Token)
|
|
215
215
|
|
|
216
|
-
|
|
|
217
|
-
|
|
218
|
-
| POST | `/api/fs/read` |
|
|
219
|
-
| POST | `/api/fs/write` |
|
|
220
|
-
| POST | `/api/fs/list` |
|
|
221
|
-
| POST | `/api/fs/delete` |
|
|
222
|
-
| POST | `/api/fs/search_files` |
|
|
223
|
-
| POST | `/api/fs/search_content` |
|
|
224
|
-
| POST | `/api/exec` |
|
|
225
|
-
| POST | `/api/exec/stop` |
|
|
226
|
-
| GET | `/api/exec/running` |
|
|
227
|
-
| GET | `/api/status/detail` |
|
|
228
|
-
| GET | `/api/logs` |
|
|
229
|
-
| GET | `/api/logs/dates` |
|
|
216
|
+
| Method | Path | Description |
|
|
217
|
+
|--------|------|-------------|
|
|
218
|
+
| POST | `/api/fs/read` | Read file content |
|
|
219
|
+
| POST | `/api/fs/write` | Write file |
|
|
220
|
+
| POST | `/api/fs/list` | List directory |
|
|
221
|
+
| POST | `/api/fs/delete` | Delete file/directory |
|
|
222
|
+
| POST | `/api/fs/search_files` | Search files by name pattern (glob) |
|
|
223
|
+
| POST | `/api/fs/search_content` | Search file content (rg preferred, Node.js fallback) |
|
|
224
|
+
| POST | `/api/exec` | Execute system command |
|
|
225
|
+
| POST | `/api/exec/stop` | Stop command execution |
|
|
226
|
+
| GET | `/api/exec/running` | List running processes |
|
|
227
|
+
| GET | `/api/status/detail` | Detailed info (work directory, pairing code, search tools) |
|
|
228
|
+
| GET | `/api/logs` | Query audit logs (supports date/category/limit/offset params) |
|
|
229
|
+
| GET | `/api/logs/dates` | Get available log date list |
|
|
230
230
|
|
|
231
231
|
### WebSocket
|
|
232
232
|
|
|
233
|
-
|
|
|
234
|
-
|
|
235
|
-
| `ws://127.0.0.1:18910/ws/exec/:execId` |
|
|
233
|
+
| Path | Description |
|
|
234
|
+
|------|-------------|
|
|
235
|
+
| `ws://127.0.0.1:18910/ws/exec/:execId` | Real-time command execution output stream |
|
|
236
236
|
|
|
237
|
-
##
|
|
237
|
+
## Robustness
|
|
238
238
|
|
|
239
|
-
|
|
239
|
+
The agent has built-in multi-layer exception protection to prevent a single error from crashing the entire service:
|
|
240
240
|
|
|
241
|
-
-
|
|
242
|
-
- **URL
|
|
243
|
-
-
|
|
244
|
-
-
|
|
245
|
-
-
|
|
246
|
-
-
|
|
241
|
+
- **Request-level protection**: Each HTTP request handler is wrapped with exception catching, returns 500 instead of crashing
|
|
242
|
+
- **URL parsing protection**: Malformed URLs don't crash the process, returns 400
|
|
243
|
+
- **Server-level protection**: Server errors like port conflicts are handled gracefully
|
|
244
|
+
- **Global fallback**: `uncaughtException` and `unhandledRejection` global handlers, logs errors but doesn't exit the process
|
|
245
|
+
- **File I/O protection**: Config file read/write failures don't affect service operation
|
|
246
|
+
- **Process management protection**: `SIGTERM`/`SIGKILL` sending has try-catch protection for already-exited processes
|
|
247
247
|
|
|
248
|
-
## Skill
|
|
248
|
+
## Skill System
|
|
249
249
|
|
|
250
|
-
|
|
250
|
+
The agent has a built-in Skill system that allows codifying workflows into reusable skills.
|
|
251
251
|
|
|
252
|
-
### Skill
|
|
252
|
+
### Skill Types
|
|
253
253
|
|
|
254
|
-
|
|
|
255
|
-
|
|
256
|
-
| **Workflow Skill** | JSON/YAML |
|
|
257
|
-
| **Agent Skill** | SKILL.md | AI
|
|
254
|
+
| Type | Definition Format | Execution | Purpose |
|
|
255
|
+
|------|-------------------|-----------|---------|
|
|
256
|
+
| **Workflow Skill** | JSON/YAML | Direct execution | Automated workflows, step-by-step |
|
|
257
|
+
| **Agent Skill** | SKILL.md | AI autonomous invocation | Knowledge codification, triggered in conversation |
|
|
258
258
|
|
|
259
|
-
### Skill
|
|
259
|
+
### Skill Directory Structure
|
|
260
260
|
|
|
261
|
-
|
|
261
|
+
All skills are stored in `~/.ai-helper-agent/skills/`:
|
|
262
262
|
|
|
263
263
|
```
|
|
264
264
|
~/.ai-helper-agent/skills/
|
|
265
|
-
├── workflow-skill.json # Workflow Skill
|
|
266
|
-
├── another-skill.yaml # Workflow Skill
|
|
267
|
-
└── agent-skill/ # Agent Skill
|
|
268
|
-
├── SKILL.md #
|
|
269
|
-
└── _meta.json #
|
|
265
|
+
├── workflow-skill.json # Workflow Skill (JSON format)
|
|
266
|
+
├── another-skill.yaml # Workflow Skill (YAML format)
|
|
267
|
+
└── agent-skill/ # Agent Skill (directory form)
|
|
268
|
+
├── SKILL.md # Skill definition file
|
|
269
|
+
└── _meta.json # Metadata (optional)
|
|
270
270
|
```
|
|
271
271
|
|
|
272
|
-
### SKILL.md
|
|
272
|
+
### SKILL.md Format
|
|
273
273
|
|
|
274
274
|
```markdown
|
|
275
275
|
---
|
|
276
276
|
name: <skill-name>
|
|
277
|
-
description: "
|
|
277
|
+
description: "<Brief description including: (1) what the skill does, (2) when to trigger>"
|
|
278
278
|
enabled: true
|
|
279
279
|
---
|
|
280
280
|
|
|
281
|
-
#
|
|
281
|
+
# <Skill Title>
|
|
282
282
|
|
|
283
283
|
## When to Use This Skill
|
|
284
284
|
|
|
285
|
-
-
|
|
286
|
-
-
|
|
285
|
+
- Trigger condition 1
|
|
286
|
+
- Trigger condition 2
|
|
287
287
|
|
|
288
288
|
## Core Capabilities
|
|
289
289
|
|
|
290
|
-
-
|
|
291
|
-
-
|
|
290
|
+
- Capability 1
|
|
291
|
+
- Capability 2
|
|
292
292
|
|
|
293
293
|
## Usage
|
|
294
294
|
|
|
295
295
|
### Step-by-Step
|
|
296
296
|
|
|
297
|
-
1.
|
|
298
|
-
2.
|
|
297
|
+
1. Step 1
|
|
298
|
+
2. Step 2
|
|
299
299
|
|
|
300
300
|
## Examples
|
|
301
301
|
|
|
302
|
-
[
|
|
302
|
+
[Concrete examples]
|
|
303
303
|
|
|
304
304
|
## Source
|
|
305
305
|
|
|
306
|
-
|
|
306
|
+
Codified from conversation, created: YYYY-MM-DD
|
|
307
307
|
```
|
|
308
308
|
|
|
309
|
-
###
|
|
309
|
+
### Built-in Skills
|
|
310
310
|
|
|
311
|
-
- **skill-creator
|
|
311
|
+
- **skill-creator**: Meta-skill for creating and updating other skills from conversations
|
|
312
312
|
|
|
313
313
|
### Skill API
|
|
314
314
|
|
|
315
|
-
|
|
|
316
|
-
|
|
317
|
-
| GET | `/api/skill/list` |
|
|
318
|
-
| GET | `/api/skill/:name` |
|
|
319
|
-
| POST | `/api/skill/import` |
|
|
320
|
-
| POST | `/api/skill/:name/toggle` |
|
|
321
|
-
| DELETE | `/api/skill/:name` |
|
|
322
|
-
| POST | `/api/skill/:name/run` |
|
|
315
|
+
| Method | Path | Description |
|
|
316
|
+
|--------|------|-------------|
|
|
317
|
+
| GET | `/api/skill/list` | Get all skills list |
|
|
318
|
+
| GET | `/api/skill/:name` | Get a single skill's full definition |
|
|
319
|
+
| POST | `/api/skill/import` | Import a new skill |
|
|
320
|
+
| POST | `/api/skill/:name/toggle` | Toggle enable/disable status |
|
|
321
|
+
| DELETE | `/api/skill/:name` | Delete a skill |
|
|
322
|
+
| POST | `/api/skill/:name/run` | Execute a workflow skill |
|
|
323
323
|
|
|
324
|
-
## MCP
|
|
324
|
+
## MCP Protocol Extension
|
|
325
325
|
|
|
326
|
-
|
|
326
|
+
Supports Model Context Protocol (MCP) to extend third-party tool capabilities.
|
|
327
327
|
|
|
328
|
-
### MCP Server
|
|
328
|
+
### MCP Server Configuration
|
|
329
329
|
|
|
330
|
-
|
|
330
|
+
Configuration file path: `~/.ai-helper-agent/config.json`
|
|
331
331
|
|
|
332
332
|
```json
|
|
333
333
|
{
|
|
@@ -344,28 +344,28 @@ enabled: true
|
|
|
344
344
|
|
|
345
345
|
### MCP API
|
|
346
346
|
|
|
347
|
-
|
|
|
348
|
-
|
|
349
|
-
| GET | `/api/mcp/status` |
|
|
350
|
-
| POST | `/api/mcp/:serverId/connect` |
|
|
351
|
-
| POST | `/api/mcp/:serverId/disconnect` |
|
|
352
|
-
| GET | `/api/mcp/tools` |
|
|
347
|
+
| Method | Path | Description |
|
|
348
|
+
|--------|------|-------------|
|
|
349
|
+
| GET | `/api/mcp/status` | Get all MCP server statuses |
|
|
350
|
+
| POST | `/api/mcp/:serverId/connect` | Connect to specified MCP server |
|
|
351
|
+
| POST | `/api/mcp/:serverId/disconnect` | Disconnect |
|
|
352
|
+
| GET | `/api/mcp/tools` | Get all MCP tools list |
|
|
353
353
|
|
|
354
|
-
###
|
|
354
|
+
### How It Works
|
|
355
355
|
|
|
356
|
-
1. Agent
|
|
357
|
-
2.
|
|
358
|
-
3.
|
|
359
|
-
4.
|
|
360
|
-
5.
|
|
356
|
+
1. Agent auto-connects all enabled MCP servers on startup
|
|
357
|
+
2. Establishes JSON-RPC 2.0 communication via stdio
|
|
358
|
+
3. Auto-discover tools provided by MCP servers
|
|
359
|
+
4. Tool call requests are forwarded to MCP servers via the agent
|
|
360
|
+
5. Tool results are returned to the extension
|
|
361
361
|
|
|
362
|
-
##
|
|
362
|
+
## Tech Stack
|
|
363
363
|
|
|
364
364
|
- Node.js >= 18
|
|
365
|
-
-
|
|
366
|
-
- `ws`
|
|
367
|
-
-
|
|
368
|
-
-
|
|
365
|
+
- Native `http` module (HTTP server)
|
|
366
|
+
- `ws` library (WebSocket server)
|
|
367
|
+
- Zero external framework dependencies
|
|
368
|
+
- Optional dependencies: `fd`, `rg` (ripgrep) — for accelerated file search
|
|
369
369
|
|
|
370
370
|
## License
|
|
371
371
|
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
> [English](./README.md) | [中文](./README.zh-CN.md)
|
|
2
|
+
|
|
3
|
+
# AI Helper Agent
|
|
4
|
+
|
|
5
|
+
AI Helper 代理服务,为 [AI Helper Chrome 扩展](https://github.com/xiweicheng/ai-helper) 提供本地文件读写、系统命令执行、Skill 技能系统和 MCP 协议扩展能力。
|
|
6
|
+
|
|
7
|
+
## 安装
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install -g ai-helper-agent
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
要求 Node.js >= 18.0.0。
|
|
14
|
+
|
|
15
|
+
## 快速开始
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
# 前台启动(终端显示实时运行日志,适合调试)
|
|
19
|
+
ai-helper-agent start
|
|
20
|
+
|
|
21
|
+
# 后台启动(守护进程模式,终端立即返回)
|
|
22
|
+
ai-helper-agent start --background
|
|
23
|
+
ai-helper-agent start -b
|
|
24
|
+
|
|
25
|
+
# 指定工作目录和端口
|
|
26
|
+
ai-helper-agent start --workdir /path/to/your/project --port 18911
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
启动后终端会显示 4 位配对码,在 Chrome 扩展设置页「代理」标签中填入即可完成配对。
|
|
30
|
+
|
|
31
|
+
## CLI 命令
|
|
32
|
+
|
|
33
|
+
| 命令 | 说明 |
|
|
34
|
+
|------|------|
|
|
35
|
+
| `start` | 启动代理服务(前台运行,显示实时日志) |
|
|
36
|
+
| `start --background` / `start -b` | 后台启动(守护进程模式) |
|
|
37
|
+
| `stop` | 停止正在运行的 Agent |
|
|
38
|
+
| `restart` | 重启代理服务(支持 -b 后台重启) |
|
|
39
|
+
| `status` | 查看运行状态 |
|
|
40
|
+
| `paircode` | 查看配对码提示 |
|
|
41
|
+
| `config` | 查看当前配置 |
|
|
42
|
+
| `help` | 显示帮助信息 |
|
|
43
|
+
| `--version` / `-v` | 显示版本号 |
|
|
44
|
+
|
|
45
|
+
### 启动选项
|
|
46
|
+
|
|
47
|
+
```
|
|
48
|
+
--background, -b 后台运行(守护进程模式)
|
|
49
|
+
--port <端口> 监听端口,默认 18910
|
|
50
|
+
--host <地址> 监听地址,默认 127.0.0.1
|
|
51
|
+
--workdir <目录> 工作目录(文件读写限制在此范围内)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### 前台 vs 后台运行
|
|
55
|
+
|
|
56
|
+
| 模式 | 命令 | 终端行为 | 适用场景 |
|
|
57
|
+
|------|------|----------|----------|
|
|
58
|
+
| 前台 | `start` | 阻塞终端,实时显示运行日志 | 开发调试、问题排查 |
|
|
59
|
+
| 后台 | `start --background` | 立即返回,终端无日志输出 | 生产环境、日常使用 |
|
|
60
|
+
|
|
61
|
+
前台运行时,所有文件读写、命令执行、安全拦截等操作日志会以格式化形式实时输出到终端:
|
|
62
|
+
|
|
63
|
+
```
|
|
64
|
+
[12:30:01] [INFO] [文件:read] path=/project/src/index.js size=2048
|
|
65
|
+
[12:30:05] [INFO] [命令:started] command="npm test" cwd=/project execId=a1b2c3d4
|
|
66
|
+
[12:30:12] [INFO] [命令:completed] execId=a1b2c3d4 exitCode=0 killed=false
|
|
67
|
+
[12:31:00] [WARN] [安全:exec_denied] command="rm -rf /" reason=高危命令被拦截
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### 进程管理
|
|
71
|
+
|
|
72
|
+
Agent 通过 PID 文件(`~/.ai-helper-agent/agent.pid`)管理进程生命周期:
|
|
73
|
+
|
|
74
|
+
- 启动时自动写入 PID 文件
|
|
75
|
+
- `stop` 命令优先通过 API 优雅关闭,失败则通过 PID 文件 kill 进程
|
|
76
|
+
- 正常关闭时自动清理 PID 文件
|
|
77
|
+
|
|
78
|
+
## 安全机制
|
|
79
|
+
|
|
80
|
+
### 文件沙箱
|
|
81
|
+
|
|
82
|
+
所有文件操作限制在配置的 `allowedPaths` 白名单目录内。通过 `realpath` 解析防止符号链接绕过,确保物理路径安全。
|
|
83
|
+
|
|
84
|
+
### 命令分级管控
|
|
85
|
+
|
|
86
|
+
**黑名单** — 直接拦截,不可绕过:
|
|
87
|
+
|
|
88
|
+
| 类型 | 示例 |
|
|
89
|
+
|------|------|
|
|
90
|
+
| 磁盘破坏 | `rm -rf /`、`mkfs.*`、`dd if=... of=/dev/...` |
|
|
91
|
+
| 系统文件覆盖 | `> /etc/passwd`、`> /etc/shadow` |
|
|
92
|
+
| 恶意管道执行 | `curl ... \| bash`、`git clone ... \| sh` |
|
|
93
|
+
| Shell 注入 | 反引号、`$()`、`${}` 命令替换 |
|
|
94
|
+
| Fork bomb | `:(){ :\|:& };:` |
|
|
95
|
+
|
|
96
|
+
**灰名单** — 需要用户确认后才执行:
|
|
97
|
+
|
|
98
|
+
| 命令模式 | 原因 |
|
|
99
|
+
|----------|------|
|
|
100
|
+
| `sudo ...` | 需要管理员权限 |
|
|
101
|
+
| `npm install -g ...` | 全局安装包 |
|
|
102
|
+
| `pip install/uninstall` | Python 包管理 |
|
|
103
|
+
| `chmod -R 777` | 递归修改权限 |
|
|
104
|
+
| `rm -rf ...` | 递归强制删除 |
|
|
105
|
+
| `git push --force` | 强制推送 |
|
|
106
|
+
| `shutdown/reboot` | 关机/重启 |
|
|
107
|
+
|
|
108
|
+
### 认证机制
|
|
109
|
+
|
|
110
|
+
首次配对时用户在扩展中输入终端显示的 4 位配对码完成配对。配对码每 30 秒轮换。配对成功后使用 HMAC token 做后续请求认证,存储在 `~/.ai-helper-agent/pairings.json`。
|
|
111
|
+
|
|
112
|
+
## 配置
|
|
113
|
+
|
|
114
|
+
配置文件路径:`~/.ai-helper-agent/config.json`
|
|
115
|
+
|
|
116
|
+
```json
|
|
117
|
+
{
|
|
118
|
+
"port": 18910,
|
|
119
|
+
"host": "127.0.0.1",
|
|
120
|
+
"workdir": "/path/to/project",
|
|
121
|
+
"allowedPaths": [],
|
|
122
|
+
"pairCodeTTL": 30,
|
|
123
|
+
"commandTimeout": 300000,
|
|
124
|
+
"fileMaxSize": 52428800
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
| 字段 | 说明 | 默认值 |
|
|
129
|
+
|------|------|--------|
|
|
130
|
+
| `port` | 监听端口 | 18910 |
|
|
131
|
+
| `host` | 监听地址 | 127.0.0.1 |
|
|
132
|
+
| `workdir` | 默认工作目录 | 启动时当前目录 |
|
|
133
|
+
| `allowedPaths` | 额外允许访问的目录列表 | `[]` |
|
|
134
|
+
| `pairCodeTTL` | 配对码有效期(秒) | 30 |
|
|
135
|
+
| `commandTimeout` | 命令执行超时(毫秒) | 300000(5分钟) |
|
|
136
|
+
| `fileMaxSize` | 文件读写最大字节数 | 52428800(50MB) |
|
|
137
|
+
|
|
138
|
+
## 文件搜索
|
|
139
|
+
|
|
140
|
+
Agent 优先使用系统原生搜索工具,不可用时自动回退到 Node.js 实现:
|
|
141
|
+
|
|
142
|
+
| 引擎 | 用途 | 检测命令 |
|
|
143
|
+
|------|------|----------|
|
|
144
|
+
| `fd` | 文件名搜索(快速) | `fd --version` |
|
|
145
|
+
| `rg` (ripgrep) | 文件内容搜索(快速) | `rg --version` |
|
|
146
|
+
|
|
147
|
+
状态接口返回当前可用的搜索工具:
|
|
148
|
+
|
|
149
|
+
```json
|
|
150
|
+
{ "searchTools": { "fd": true, "rg": true } }
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## 审计日志
|
|
154
|
+
|
|
155
|
+
所有操作自动记录到审计日志中,日志文件位于 `~/.ai-helper-agent/logs/`。
|
|
156
|
+
|
|
157
|
+
### 双通道输出
|
|
158
|
+
|
|
159
|
+
- **终端输出**(前台模式):格式化可读格式,输出到 stderr
|
|
160
|
+
- **文件输出**:JSON Lines 格式,写入日志文件
|
|
161
|
+
|
|
162
|
+
两种通道同时工作,互不影响。
|
|
163
|
+
|
|
164
|
+
### 日志格式
|
|
165
|
+
|
|
166
|
+
JSON Lines 格式,每行一条记录,文件按日命名 `agent-YYYY-MM-DD.log`。
|
|
167
|
+
|
|
168
|
+
```json
|
|
169
|
+
{"timestamp":"2026-01-15T10:30:00.123Z","level":"info","category":"fs","action":"read","path":"/home/user/project/src/index.js","size":2048}
|
|
170
|
+
{"timestamp":"2026-01-15T10:30:05.456Z","level":"info","category":"exec","action":"completed","command":"npm test","cwd":"/home/user/project","execId":"a1b2c3d4","exitCode":0,"killed":false,"stdoutLen":1024,"stderrLen":0}
|
|
171
|
+
{"timestamp":"2026-01-15T10:31:00.789Z","level":"warn","category":"security","action":"exec_denied","command":"rm -rf /","reason":"高危命令被拦截"}
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### 日志分类
|
|
175
|
+
|
|
176
|
+
| category | 说明 | 包含的操作 |
|
|
177
|
+
|----------|------|-----------|
|
|
178
|
+
| `auth` | 认证事件 | 配对成功/失败 |
|
|
179
|
+
| `fs` | 文件操作 | read, write, list, delete, search_files, search_content |
|
|
180
|
+
| `exec` | 命令执行 | started, completed, stopped, error |
|
|
181
|
+
| `security` | 安全事件 | deny, confirm, auth 失败, 路径越权拦截 |
|
|
182
|
+
| `system` | 系统事件 | server_start, server_stop, shutdown, server_error, uncaught_exception, unhandled_rejection |
|
|
183
|
+
|
|
184
|
+
### 日志查询 API
|
|
185
|
+
|
|
186
|
+
```
|
|
187
|
+
GET /api/logs?date=2026-01-15&category=security&limit=50&offset=0
|
|
188
|
+
GET /api/logs/dates
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
- `date` - 日期筛选 (YYYY-MM-DD),不传默认今天
|
|
192
|
+
- `category` - 分类筛选,不传返回全部
|
|
193
|
+
- `limit` - 返回条数上限,默认 200
|
|
194
|
+
- `offset` - 分页偏移
|
|
195
|
+
|
|
196
|
+
返回按时间倒序排列(最新的在前)。
|
|
197
|
+
|
|
198
|
+
### 自动清理
|
|
199
|
+
|
|
200
|
+
- 最多保留 30 个日志文件
|
|
201
|
+
- 单文件超过 10MB 自动删除
|
|
202
|
+
- 每次写入日志时触发检查
|
|
203
|
+
|
|
204
|
+
## API 端点
|
|
205
|
+
|
|
206
|
+
### 无需认证
|
|
207
|
+
|
|
208
|
+
| 方法 | 路径 | 说明 |
|
|
209
|
+
|------|------|------|
|
|
210
|
+
| POST | `/api/pair` | 配对认证 |
|
|
211
|
+
| GET | `/api/status` | 健康检查(版本号、平台信息、搜索工具可用性) |
|
|
212
|
+
| POST | `/api/shutdown` | 关闭代理服务(仅限本地访问) |
|
|
213
|
+
|
|
214
|
+
### 需要认证(Bearer Token)
|
|
215
|
+
|
|
216
|
+
| 方法 | 路径 | 说明 |
|
|
217
|
+
|------|------|------|
|
|
218
|
+
| POST | `/api/fs/read` | 读取文件内容 |
|
|
219
|
+
| POST | `/api/fs/write` | 写入文件 |
|
|
220
|
+
| POST | `/api/fs/list` | 列出目录 |
|
|
221
|
+
| POST | `/api/fs/delete` | 删除文件/目录 |
|
|
222
|
+
| POST | `/api/fs/search_files` | 按文件名模式搜索(glob) |
|
|
223
|
+
| POST | `/api/fs/search_content` | 搜索文件内容(rg 优先,Node.js 回退) |
|
|
224
|
+
| POST | `/api/exec` | 执行系统命令 |
|
|
225
|
+
| POST | `/api/exec/stop` | 停止命令执行 |
|
|
226
|
+
| GET | `/api/exec/running` | 运行中的进程列表 |
|
|
227
|
+
| GET | `/api/status/detail` | 详细信息(含工作目录、配对码、搜索工具) |
|
|
228
|
+
| GET | `/api/logs` | 查询审计日志(支持 date/category/limit/offset 参数) |
|
|
229
|
+
| GET | `/api/logs/dates` | 获取可用日志日期列表 |
|
|
230
|
+
|
|
231
|
+
### WebSocket
|
|
232
|
+
|
|
233
|
+
| 路径 | 说明 |
|
|
234
|
+
|------|------|
|
|
235
|
+
| `ws://127.0.0.1:18910/ws/exec/:execId` | 命令执行实时输出流 |
|
|
236
|
+
|
|
237
|
+
## 健壮性
|
|
238
|
+
|
|
239
|
+
Agent 内置多层异常保护,防止单个错误导致整个服务崩溃:
|
|
240
|
+
|
|
241
|
+
- **请求级保护**:每个 HTTP 请求处理外层包裹异常捕获,异常时返回 500 而非崩溃
|
|
242
|
+
- **URL 解析保护**:畸形 URL 不会导致进程崩溃,返回 400
|
|
243
|
+
- **服务器级保护**:端口占用等服务器错误有专门处理,优雅报错退出
|
|
244
|
+
- **全局兜底**:`uncaughtException` 和 `unhandledRejection` 全局捕获,记录错误日志但不退出进程
|
|
245
|
+
- **文件 I/O 保护**:配置文件读写失败不影响服务运行
|
|
246
|
+
- **进程管理保护**:`SIGTERM`/`SIGKILL` 发送对已退出进程有 try-catch 保护
|
|
247
|
+
|
|
248
|
+
## Skill 系统
|
|
249
|
+
|
|
250
|
+
Agent 服务内置 Skill 系统,支持将操作流程沉淀为可复用的技能。
|
|
251
|
+
|
|
252
|
+
### Skill 类型
|
|
253
|
+
|
|
254
|
+
| 类型 | 定义格式 | 执行方式 | 用途 |
|
|
255
|
+
|------|----------|----------|------|
|
|
256
|
+
| **Workflow Skill** | JSON/YAML | 直接执行 | 自动化流程,按步骤执行 |
|
|
257
|
+
| **Agent Skill** | SKILL.md | AI 自主调用 | 知识沉淀,在对话中触发 |
|
|
258
|
+
|
|
259
|
+
### Skill 目录结构
|
|
260
|
+
|
|
261
|
+
所有 Skill 存储在 `~/.ai-helper-agent/skills/` 目录下:
|
|
262
|
+
|
|
263
|
+
```
|
|
264
|
+
~/.ai-helper-agent/skills/
|
|
265
|
+
├── workflow-skill.json # Workflow Skill(JSON 格式)
|
|
266
|
+
├── another-skill.yaml # Workflow Skill(YAML 格式)
|
|
267
|
+
└── agent-skill/ # Agent Skill(目录形式)
|
|
268
|
+
├── SKILL.md # 技能定义文件
|
|
269
|
+
└── _meta.json # 元数据(可选)
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
### SKILL.md 格式
|
|
273
|
+
|
|
274
|
+
```markdown
|
|
275
|
+
---
|
|
276
|
+
name: <skill-name>
|
|
277
|
+
description: "<简明描述,包含:(1) 技能做什么,(2) 何时触发调用>"
|
|
278
|
+
enabled: true
|
|
279
|
+
---
|
|
280
|
+
|
|
281
|
+
# <技能标题>
|
|
282
|
+
|
|
283
|
+
## When to Use This Skill
|
|
284
|
+
|
|
285
|
+
- 触发条件1
|
|
286
|
+
- 触发条件2
|
|
287
|
+
|
|
288
|
+
## Core Capabilities
|
|
289
|
+
|
|
290
|
+
- 能力1
|
|
291
|
+
- 能力2
|
|
292
|
+
|
|
293
|
+
## Usage
|
|
294
|
+
|
|
295
|
+
### Step-by-Step
|
|
296
|
+
|
|
297
|
+
1. 步骤1
|
|
298
|
+
2. 步骤2
|
|
299
|
+
|
|
300
|
+
## Examples
|
|
301
|
+
|
|
302
|
+
[具体示例]
|
|
303
|
+
|
|
304
|
+
## Source
|
|
305
|
+
|
|
306
|
+
从对话中沉淀,创建日期:YYYY-MM-DD
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
### 内置技能
|
|
310
|
+
|
|
311
|
+
- **skill-creator**:元技能,用于从对话中创建和更新其他技能
|
|
312
|
+
|
|
313
|
+
### Skill API
|
|
314
|
+
|
|
315
|
+
| 方法 | 路径 | 说明 |
|
|
316
|
+
|------|------|------|
|
|
317
|
+
| GET | `/api/skill/list` | 获取所有 Skill 列表 |
|
|
318
|
+
| GET | `/api/skill/:name` | 获取单个 Skill 完整定义 |
|
|
319
|
+
| POST | `/api/skill/import` | 导入新 Skill |
|
|
320
|
+
| POST | `/api/skill/:name/toggle` | 切换启用/停用状态 |
|
|
321
|
+
| DELETE | `/api/skill/:name` | 删除 Skill |
|
|
322
|
+
| POST | `/api/skill/:name/run` | 执行 Workflow Skill |
|
|
323
|
+
|
|
324
|
+
## MCP 协议扩展
|
|
325
|
+
|
|
326
|
+
支持 Model Context Protocol(MCP),扩展第三方工具能力。
|
|
327
|
+
|
|
328
|
+
### MCP Server 配置
|
|
329
|
+
|
|
330
|
+
配置文件路径:`~/.ai-helper-agent/config.json`
|
|
331
|
+
|
|
332
|
+
```json
|
|
333
|
+
{
|
|
334
|
+
"mcpServers": [
|
|
335
|
+
{
|
|
336
|
+
"id": "my-mcp-server",
|
|
337
|
+
"name": "My MCP Server",
|
|
338
|
+
"command": ["python", "-m", "my_mcp_server"],
|
|
339
|
+
"enabled": true
|
|
340
|
+
}
|
|
341
|
+
]
|
|
342
|
+
}
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
### MCP API
|
|
346
|
+
|
|
347
|
+
| 方法 | 路径 | 说明 |
|
|
348
|
+
|------|------|------|
|
|
349
|
+
| GET | `/api/mcp/status` | 获取所有 MCP Server 状态 |
|
|
350
|
+
| POST | `/api/mcp/:serverId/connect` | 连接指定 MCP Server |
|
|
351
|
+
| POST | `/api/mcp/:serverId/disconnect` | 断开连接 |
|
|
352
|
+
| GET | `/api/mcp/tools` | 获取所有 MCP 工具列表 |
|
|
353
|
+
|
|
354
|
+
### 工作原理
|
|
355
|
+
|
|
356
|
+
1. Agent 启动时自动连接所有启用的 MCP Server
|
|
357
|
+
2. 通过 stdio 建立 JSON-RPC 2.0 通信
|
|
358
|
+
3. 自动发现 MCP Server 提供的工具
|
|
359
|
+
4. 工具调用请求通过 Agent 转发到 MCP Server
|
|
360
|
+
5. 工具结果返回给扩展
|
|
361
|
+
|
|
362
|
+
## 技术栈
|
|
363
|
+
|
|
364
|
+
- Node.js >= 18
|
|
365
|
+
- 原生 `http` 模块(HTTP 服务)
|
|
366
|
+
- `ws` 库(WebSocket 服务)
|
|
367
|
+
- 零外部框架依赖
|
|
368
|
+
- 可选依赖:`fd`、`rg`(ripgrep)— 用于加速文件搜索
|
|
369
|
+
|
|
370
|
+
## License
|
|
371
|
+
|
|
372
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ai-helper-agent",
|
|
3
|
-
"version": "1.14.
|
|
3
|
+
"version": "1.14.9",
|
|
4
4
|
"description": "AI Helper Agent - Local file access and system command execution proxy for Chrome extension / AI Helper 代理服务,为 Chrome 扩展提供本地文件读写和系统命令执行",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/server.js",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"bin/",
|
|
13
13
|
"src/",
|
|
14
14
|
"README.md",
|
|
15
|
-
"README.
|
|
15
|
+
"README.zh-CN.md"
|
|
16
16
|
],
|
|
17
17
|
"scripts": {
|
|
18
18
|
"start": "node bin/agent.js start",
|
package/README.en.md
DELETED
|
@@ -1,372 +0,0 @@
|
|
|
1
|
-
> [English](./README.en.md) | [中文](./README.md)
|
|
2
|
-
|
|
3
|
-
# AI Helper Agent
|
|
4
|
-
|
|
5
|
-
AI Helper Agent — a local proxy service that provides the [AI Helper Chrome Extension](https://github.com/xiweicheng/ai-helper) with local file read/write, system command execution, Skill system, and MCP protocol extension capabilities.
|
|
6
|
-
|
|
7
|
-
## Installation
|
|
8
|
-
|
|
9
|
-
```bash
|
|
10
|
-
npm install -g ai-helper-agent
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
Requires Node.js >= 18.0.0.
|
|
14
|
-
|
|
15
|
-
## Quick Start
|
|
16
|
-
|
|
17
|
-
```bash
|
|
18
|
-
# Start in foreground (real-time logs in terminal, ideal for debugging)
|
|
19
|
-
ai-helper-agent start
|
|
20
|
-
|
|
21
|
-
# Start in background (daemon mode, terminal returns immediately)
|
|
22
|
-
ai-helper-agent start --background
|
|
23
|
-
ai-helper-agent start -b
|
|
24
|
-
|
|
25
|
-
# Specify work directory and port
|
|
26
|
-
ai-helper-agent start --workdir /path/to/your/project --port 18911
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
After startup, the terminal will display a 4-digit pairing code. Enter it in the "Agent" tab of the Chrome extension settings page to complete pairing.
|
|
30
|
-
|
|
31
|
-
## CLI Commands
|
|
32
|
-
|
|
33
|
-
| Command | Description |
|
|
34
|
-
|---------|-------------|
|
|
35
|
-
| `start` | Start the agent service (foreground, with real-time logs) |
|
|
36
|
-
| `start --background` / `start -b` | Start in background (daemon mode) |
|
|
37
|
-
| `stop` | Stop the running agent |
|
|
38
|
-
| `restart` | Restart the agent service (supports -b for background restart) |
|
|
39
|
-
| `status` | Check running status |
|
|
40
|
-
| `paircode` | Show pairing code |
|
|
41
|
-
| `config` | View current configuration |
|
|
42
|
-
| `help` | Show help information |
|
|
43
|
-
| `--version` / `-v` | Show version number |
|
|
44
|
-
|
|
45
|
-
### Startup Options
|
|
46
|
-
|
|
47
|
-
```
|
|
48
|
-
--background, -b Run in background (daemon mode)
|
|
49
|
-
--port <port> Listen port, default 18910
|
|
50
|
-
--host <address> Listen address, default 127.0.0.1
|
|
51
|
-
--workdir <dir> Work directory (file read/write restricted to this scope)
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
### Foreground vs Background
|
|
55
|
-
|
|
56
|
-
| Mode | Command | Terminal Behavior | Use Case |
|
|
57
|
-
|------|---------|-------------------|----------|
|
|
58
|
-
| Foreground | `start` | Blocks terminal, shows real-time logs | Development, debugging |
|
|
59
|
-
| Background | `start --background` | Returns immediately, no log output | Production, daily use |
|
|
60
|
-
|
|
61
|
-
When running in foreground, all file read/write, command execution, and security interception logs are output in real-time in a formatted manner:
|
|
62
|
-
|
|
63
|
-
```
|
|
64
|
-
[12:30:01] [INFO] [File:read] path=/project/src/index.js size=2048
|
|
65
|
-
[12:30:05] [INFO] [Command:started] command="npm test" cwd=/project execId=a1b2c3d4
|
|
66
|
-
[12:30:12] [INFO] [Command:completed] execId=a1b2c3d4 exitCode=0 killed=false
|
|
67
|
-
[12:31:00] [WARN] [Security:exec_denied] command="rm -rf /" reason=High-risk command blocked
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
### Process Management
|
|
71
|
-
|
|
72
|
-
The agent manages process lifecycle via a PID file (`~/.ai-helper-agent/agent.pid`):
|
|
73
|
-
|
|
74
|
-
- Automatically writes PID file on startup
|
|
75
|
-
- `stop` command prefers graceful shutdown via API, falls back to killing process via PID file
|
|
76
|
-
- Automatically cleans up PID file on normal shutdown
|
|
77
|
-
|
|
78
|
-
## Security
|
|
79
|
-
|
|
80
|
-
### File Sandbox
|
|
81
|
-
|
|
82
|
-
All file operations are restricted to directories listed in the `allowedPaths` whitelist. `realpath` resolution prevents symlink bypasses, ensuring physical paths are safe.
|
|
83
|
-
|
|
84
|
-
### Command Tiered Control
|
|
85
|
-
|
|
86
|
-
**Blacklist** — Blocked directly, cannot be bypassed:
|
|
87
|
-
|
|
88
|
-
| Type | Examples |
|
|
89
|
-
|------|----------|
|
|
90
|
-
| Disk destruction | `rm -rf /`, `mkfs.*`, `dd if=... of=/dev/...` |
|
|
91
|
-
| System file overwrite | `> /etc/passwd`, `> /etc/shadow` |
|
|
92
|
-
| Malicious pipe execution | `curl ... \| bash`, `git clone ... \| sh` |
|
|
93
|
-
| Shell injection | Backticks, `$()`, `${}` command substitution |
|
|
94
|
-
| Fork bomb | `:(){ :\|:& };:` |
|
|
95
|
-
|
|
96
|
-
**Greylist** — Requires user confirmation before execution:
|
|
97
|
-
|
|
98
|
-
| Command Pattern | Reason |
|
|
99
|
-
|-----------------|--------|
|
|
100
|
-
| `sudo ...` | Requires admin privileges |
|
|
101
|
-
| `npm install -g ...` | Global package installation |
|
|
102
|
-
| `pip install/uninstall` | Python package management |
|
|
103
|
-
| `chmod -R 777` | Recursive permission modification |
|
|
104
|
-
| `rm -rf ...` | Recursive forced deletion |
|
|
105
|
-
| `git push --force` | Force push |
|
|
106
|
-
| `shutdown/reboot` | System shutdown/restart |
|
|
107
|
-
|
|
108
|
-
### Authentication
|
|
109
|
-
|
|
110
|
-
Pairing is completed by entering the 4-digit pairing code displayed in the terminal into the extension. The pairing code rotates every 30 seconds. After successful pairing, an HMAC token is used for subsequent request authentication, stored in `~/.ai-helper-agent/pairings.json`.
|
|
111
|
-
|
|
112
|
-
## Configuration
|
|
113
|
-
|
|
114
|
-
Configuration file path: `~/.ai-helper-agent/config.json`
|
|
115
|
-
|
|
116
|
-
```json
|
|
117
|
-
{
|
|
118
|
-
"port": 18910,
|
|
119
|
-
"host": "127.0.0.1",
|
|
120
|
-
"workdir": "/path/to/project",
|
|
121
|
-
"allowedPaths": [],
|
|
122
|
-
"pairCodeTTL": 30,
|
|
123
|
-
"commandTimeout": 300000,
|
|
124
|
-
"fileMaxSize": 52428800
|
|
125
|
-
}
|
|
126
|
-
```
|
|
127
|
-
|
|
128
|
-
| Field | Description | Default |
|
|
129
|
-
|-------|-------------|---------|
|
|
130
|
-
| `port` | Listen port | 18910 |
|
|
131
|
-
| `host` | Listen address | 127.0.0.1 |
|
|
132
|
-
| `workdir` | Default work directory | Current directory at startup |
|
|
133
|
-
| `allowedPaths` | Additional allowed directory list | `[]` |
|
|
134
|
-
| `pairCodeTTL` | Pairing code validity (seconds) | 30 |
|
|
135
|
-
| `commandTimeout` | Command execution timeout (ms) | 300000 (5 min) |
|
|
136
|
-
| `fileMaxSize` | Max file read/write size (bytes) | 52428800 (50 MB) |
|
|
137
|
-
|
|
138
|
-
## File Search
|
|
139
|
-
|
|
140
|
-
The agent prefers native system search tools, with automatic fallback to Node.js implementation when unavailable:
|
|
141
|
-
|
|
142
|
-
| Engine | Purpose | Detection Command |
|
|
143
|
-
|--------|---------|-------------------|
|
|
144
|
-
| `fd` | Filename search (fast) | `fd --version` |
|
|
145
|
-
| `rg` (ripgrep) | File content search (fast) | `rg --version` |
|
|
146
|
-
|
|
147
|
-
The status endpoint reports currently available search tools:
|
|
148
|
-
|
|
149
|
-
```json
|
|
150
|
-
{ "searchTools": { "fd": true, "rg": true } }
|
|
151
|
-
```
|
|
152
|
-
|
|
153
|
-
## Audit Logs
|
|
154
|
-
|
|
155
|
-
All operations are automatically recorded in audit logs, located in `~/.ai-helper-agent/logs/`.
|
|
156
|
-
|
|
157
|
-
### Dual Channel Output
|
|
158
|
-
|
|
159
|
-
- **Terminal output** (foreground mode): Formatted human-readable output to stderr
|
|
160
|
-
- **File output**: JSON Lines format, written to log files
|
|
161
|
-
|
|
162
|
-
Both channels work simultaneously without interfering with each other.
|
|
163
|
-
|
|
164
|
-
### Log Format
|
|
165
|
-
|
|
166
|
-
JSON Lines format, one record per line, files named by date `agent-YYYY-MM-DD.log`.
|
|
167
|
-
|
|
168
|
-
```json
|
|
169
|
-
{"timestamp":"2026-01-15T10:30:00.123Z","level":"info","category":"fs","action":"read","path":"/home/user/project/src/index.js","size":2048}
|
|
170
|
-
{"timestamp":"2026-01-15T10:30:05.456Z","level":"info","category":"exec","action":"completed","command":"npm test","cwd":"/home/user/project","execId":"a1b2c3d4","exitCode":0,"killed":false,"stdoutLen":1024,"stderrLen":0}
|
|
171
|
-
{"timestamp":"2026-01-15T10:31:00.789Z","level":"warn","category":"security","action":"exec_denied","command":"rm -rf /","reason":"High-risk command blocked"}
|
|
172
|
-
```
|
|
173
|
-
|
|
174
|
-
### Log Categories
|
|
175
|
-
|
|
176
|
-
| Category | Description | Included Actions |
|
|
177
|
-
|----------|-------------|-----------------|
|
|
178
|
-
| `auth` | Authentication events | Pairing success/failure |
|
|
179
|
-
| `fs` | File operations | read, write, list, delete, search_files, search_content |
|
|
180
|
-
| `exec` | Command execution | started, completed, stopped, error |
|
|
181
|
-
| `security` | Security events | deny, confirm, auth failure, path breach interception |
|
|
182
|
-
| `system` | System events | server_start, server_stop, shutdown, server_error, uncaught_exception, unhandled_rejection |
|
|
183
|
-
|
|
184
|
-
### Log Query API
|
|
185
|
-
|
|
186
|
-
```
|
|
187
|
-
GET /api/logs?date=2026-01-15&category=security&limit=50&offset=0
|
|
188
|
-
GET /api/logs/dates
|
|
189
|
-
```
|
|
190
|
-
|
|
191
|
-
- `date` - Date filter (YYYY-MM-DD), defaults to today
|
|
192
|
-
- `category` - Category filter, returns all if omitted
|
|
193
|
-
- `limit` - Max number of records returned, defaults to 200
|
|
194
|
-
- `offset` - Pagination offset
|
|
195
|
-
|
|
196
|
-
Results are returned in reverse chronological order (newest first).
|
|
197
|
-
|
|
198
|
-
### Auto Cleanup
|
|
199
|
-
|
|
200
|
-
- Maximum 30 log files retained
|
|
201
|
-
- Single files exceeding 10 MB are automatically deleted
|
|
202
|
-
- Cleanup check triggered on every log write
|
|
203
|
-
|
|
204
|
-
## API Endpoints
|
|
205
|
-
|
|
206
|
-
### No Authentication Required
|
|
207
|
-
|
|
208
|
-
| Method | Path | Description |
|
|
209
|
-
|--------|------|-------------|
|
|
210
|
-
| POST | `/api/pair` | Pairing authentication |
|
|
211
|
-
| GET | `/api/status` | Health check (version, platform info, search tool availability) |
|
|
212
|
-
| POST | `/api/shutdown` | Shut down the agent service (local access only) |
|
|
213
|
-
|
|
214
|
-
### Authentication Required (Bearer Token)
|
|
215
|
-
|
|
216
|
-
| Method | Path | Description |
|
|
217
|
-
|--------|------|-------------|
|
|
218
|
-
| POST | `/api/fs/read` | Read file content |
|
|
219
|
-
| POST | `/api/fs/write` | Write file |
|
|
220
|
-
| POST | `/api/fs/list` | List directory |
|
|
221
|
-
| POST | `/api/fs/delete` | Delete file/directory |
|
|
222
|
-
| POST | `/api/fs/search_files` | Search files by name pattern (glob) |
|
|
223
|
-
| POST | `/api/fs/search_content` | Search file content (rg preferred, Node.js fallback) |
|
|
224
|
-
| POST | `/api/exec` | Execute system command |
|
|
225
|
-
| POST | `/api/exec/stop` | Stop command execution |
|
|
226
|
-
| GET | `/api/exec/running` | List running processes |
|
|
227
|
-
| GET | `/api/status/detail` | Detailed info (work directory, pairing code, search tools) |
|
|
228
|
-
| GET | `/api/logs` | Query audit logs (supports date/category/limit/offset params) |
|
|
229
|
-
| GET | `/api/logs/dates` | Get available log date list |
|
|
230
|
-
|
|
231
|
-
### WebSocket
|
|
232
|
-
|
|
233
|
-
| Path | Description |
|
|
234
|
-
|------|-------------|
|
|
235
|
-
| `ws://127.0.0.1:18910/ws/exec/:execId` | Real-time command execution output stream |
|
|
236
|
-
|
|
237
|
-
## Robustness
|
|
238
|
-
|
|
239
|
-
The agent has built-in multi-layer exception protection to prevent a single error from crashing the entire service:
|
|
240
|
-
|
|
241
|
-
- **Request-level protection**: Each HTTP request handler is wrapped with exception catching, returns 500 instead of crashing
|
|
242
|
-
- **URL parsing protection**: Malformed URLs don't crash the process, returns 400
|
|
243
|
-
- **Server-level protection**: Server errors like port conflicts are handled gracefully
|
|
244
|
-
- **Global fallback**: `uncaughtException` and `unhandledRejection` global handlers, logs errors but doesn't exit the process
|
|
245
|
-
- **File I/O protection**: Config file read/write failures don't affect service operation
|
|
246
|
-
- **Process management protection**: `SIGTERM`/`SIGKILL` sending has try-catch protection for already-exited processes
|
|
247
|
-
|
|
248
|
-
## Skill System
|
|
249
|
-
|
|
250
|
-
The agent has a built-in Skill system that allows codifying workflows into reusable skills.
|
|
251
|
-
|
|
252
|
-
### Skill Types
|
|
253
|
-
|
|
254
|
-
| Type | Definition Format | Execution | Purpose |
|
|
255
|
-
|------|-------------------|-----------|---------|
|
|
256
|
-
| **Workflow Skill** | JSON/YAML | Direct execution | Automated workflows, step-by-step |
|
|
257
|
-
| **Agent Skill** | SKILL.md | AI autonomous invocation | Knowledge codification, triggered in conversation |
|
|
258
|
-
|
|
259
|
-
### Skill Directory Structure
|
|
260
|
-
|
|
261
|
-
All skills are stored in `~/.ai-helper-agent/skills/`:
|
|
262
|
-
|
|
263
|
-
```
|
|
264
|
-
~/.ai-helper-agent/skills/
|
|
265
|
-
├── workflow-skill.json # Workflow Skill (JSON format)
|
|
266
|
-
├── another-skill.yaml # Workflow Skill (YAML format)
|
|
267
|
-
└── agent-skill/ # Agent Skill (directory form)
|
|
268
|
-
├── SKILL.md # Skill definition file
|
|
269
|
-
└── _meta.json # Metadata (optional)
|
|
270
|
-
```
|
|
271
|
-
|
|
272
|
-
### SKILL.md Format
|
|
273
|
-
|
|
274
|
-
```markdown
|
|
275
|
-
---
|
|
276
|
-
name: <skill-name>
|
|
277
|
-
description: "<Brief description including: (1) what the skill does, (2) when to trigger>"
|
|
278
|
-
enabled: true
|
|
279
|
-
---
|
|
280
|
-
|
|
281
|
-
# <Skill Title>
|
|
282
|
-
|
|
283
|
-
## When to Use This Skill
|
|
284
|
-
|
|
285
|
-
- Trigger condition 1
|
|
286
|
-
- Trigger condition 2
|
|
287
|
-
|
|
288
|
-
## Core Capabilities
|
|
289
|
-
|
|
290
|
-
- Capability 1
|
|
291
|
-
- Capability 2
|
|
292
|
-
|
|
293
|
-
## Usage
|
|
294
|
-
|
|
295
|
-
### Step-by-Step
|
|
296
|
-
|
|
297
|
-
1. Step 1
|
|
298
|
-
2. Step 2
|
|
299
|
-
|
|
300
|
-
## Examples
|
|
301
|
-
|
|
302
|
-
[Concrete examples]
|
|
303
|
-
|
|
304
|
-
## Source
|
|
305
|
-
|
|
306
|
-
Codified from conversation, created: YYYY-MM-DD
|
|
307
|
-
```
|
|
308
|
-
|
|
309
|
-
### Built-in Skills
|
|
310
|
-
|
|
311
|
-
- **skill-creator**: Meta-skill for creating and updating other skills from conversations
|
|
312
|
-
|
|
313
|
-
### Skill API
|
|
314
|
-
|
|
315
|
-
| Method | Path | Description |
|
|
316
|
-
|--------|------|-------------|
|
|
317
|
-
| GET | `/api/skill/list` | Get all skills list |
|
|
318
|
-
| GET | `/api/skill/:name` | Get a single skill's full definition |
|
|
319
|
-
| POST | `/api/skill/import` | Import a new skill |
|
|
320
|
-
| POST | `/api/skill/:name/toggle` | Toggle enable/disable status |
|
|
321
|
-
| DELETE | `/api/skill/:name` | Delete a skill |
|
|
322
|
-
| POST | `/api/skill/:name/run` | Execute a workflow skill |
|
|
323
|
-
|
|
324
|
-
## MCP Protocol Extension
|
|
325
|
-
|
|
326
|
-
Supports Model Context Protocol (MCP) to extend third-party tool capabilities.
|
|
327
|
-
|
|
328
|
-
### MCP Server Configuration
|
|
329
|
-
|
|
330
|
-
Configuration file path: `~/.ai-helper-agent/config.json`
|
|
331
|
-
|
|
332
|
-
```json
|
|
333
|
-
{
|
|
334
|
-
"mcpServers": [
|
|
335
|
-
{
|
|
336
|
-
"id": "my-mcp-server",
|
|
337
|
-
"name": "My MCP Server",
|
|
338
|
-
"command": ["python", "-m", "my_mcp_server"],
|
|
339
|
-
"enabled": true
|
|
340
|
-
}
|
|
341
|
-
]
|
|
342
|
-
}
|
|
343
|
-
```
|
|
344
|
-
|
|
345
|
-
### MCP API
|
|
346
|
-
|
|
347
|
-
| Method | Path | Description |
|
|
348
|
-
|--------|------|-------------|
|
|
349
|
-
| GET | `/api/mcp/status` | Get all MCP server statuses |
|
|
350
|
-
| POST | `/api/mcp/:serverId/connect` | Connect to specified MCP server |
|
|
351
|
-
| POST | `/api/mcp/:serverId/disconnect` | Disconnect |
|
|
352
|
-
| GET | `/api/mcp/tools` | Get all MCP tools list |
|
|
353
|
-
|
|
354
|
-
### How It Works
|
|
355
|
-
|
|
356
|
-
1. Agent auto-connects all enabled MCP servers on startup
|
|
357
|
-
2. Establishes JSON-RPC 2.0 communication via stdio
|
|
358
|
-
3. Auto-discover tools provided by MCP servers
|
|
359
|
-
4. Tool call requests are forwarded to MCP servers via the agent
|
|
360
|
-
5. Tool results are returned to the extension
|
|
361
|
-
|
|
362
|
-
## Tech Stack
|
|
363
|
-
|
|
364
|
-
- Node.js >= 18
|
|
365
|
-
- Native `http` module (HTTP server)
|
|
366
|
-
- `ws` library (WebSocket server)
|
|
367
|
-
- Zero external framework dependencies
|
|
368
|
-
- Optional dependencies: `fd`, `rg` (ripgrep) — for accelerated file search
|
|
369
|
-
|
|
370
|
-
## License
|
|
371
|
-
|
|
372
|
-
MIT
|