@kenhkl/agent-moss 0.2.1 → 0.3.1
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 +119 -372
- package/dist/cli.js +896 -150
- package/dist/cli.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,422 +1,169 @@
|
|
|
1
|
-
# AgentMoss
|
|
1
|
+
# AgentMoss (TypeScript)
|
|
2
2
|
|
|
3
|
-
AgentMoss
|
|
3
|
+
AgentMoss TypeScript 实现 — 可被任意 AI Agent 调用的独立通用安全分析服务。三层防御引擎(启发式 → 逻辑规则 → LLM 语义分析),两级快速放行白名单,fail-closed 安全原则。
|
|
4
4
|
|
|
5
|
-
>
|
|
5
|
+
> 当前版本:**v0.2.1** | npm: `@kenhkl/agent-moss`
|
|
6
6
|
|
|
7
|
-
##
|
|
8
|
-
|
|
9
|
-
- [架构](#架构)
|
|
10
|
-
- [快速开始](#快速开始)
|
|
11
|
-
- [LLM 配置](#llm-配置)
|
|
12
|
-
- [openEuler 部署](#openeuler-部署)
|
|
13
|
-
- [API 参考](#api-参考)
|
|
14
|
-
- [配置](#配置)
|
|
15
|
-
- [项目结构](#项目结构)
|
|
16
|
-
- [测试](#测试)
|
|
17
|
-
|
|
18
|
-
## 架构
|
|
19
|
-
|
|
20
|
-
```
|
|
21
|
-
┌───────────────────────────────────────────┐
|
|
22
|
-
│ 调用入口 (HTTP / Unix Socket) │
|
|
23
|
-
│ POST /api/v1/analyze │
|
|
24
|
-
│ GET /api/v1/health │
|
|
25
|
-
├───────────────────────────────────────────┤
|
|
26
|
-
│ ObservableAdapter │
|
|
27
|
-
│ AgentOS 可观测服务 syscall → 标准格式 │
|
|
28
|
-
├───────────────────────────────────────────┤
|
|
29
|
-
│ OS Profile 选择 │
|
|
30
|
-
│ ┌──────────┐ ┌──────────┐ │
|
|
31
|
-
│ │ Linux │ │ Windows │ │
|
|
32
|
-
│ │ Profile │ │ Profile │ │
|
|
33
|
-
│ └──────────┘ └──────────┘ │
|
|
34
|
-
├───────────────────────────────────────────┤
|
|
35
|
-
│ 安全分析引擎 (三层防御) │
|
|
36
|
-
│ │
|
|
37
|
-
│ 层1: 启发式静态检测 (毫秒级) │
|
|
38
|
-
│ ├── 用户规则匹配 │
|
|
39
|
-
│ ├── 危险命令正则 (OS Profile 驱动) │
|
|
40
|
-
│ └── Prompt 注入检测 │
|
|
41
|
-
│ │
|
|
42
|
-
│ 层2: 逻辑规则检测 (毫秒级) │
|
|
43
|
-
│ ├── read_before_write 原则 │
|
|
44
|
-
│ ├── 意图一致性检测 │
|
|
45
|
-
│ ├── 敏感路径访问 (OS Profile 驱动) │
|
|
46
|
-
│ └── 危险操作模式 │
|
|
47
|
-
│ │
|
|
48
|
-
│ 层3: LLM + Skill 深度分析 │
|
|
49
|
-
│ ├── Skill 规则匹配 (12 个 Skill) │
|
|
50
|
-
│ ├── 脚本内容预扫描 │
|
|
51
|
-
│ └── LLM 语义安全判断 │
|
|
52
|
-
└───────────────────────────────────────────┘
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
### 设计原则
|
|
56
|
-
|
|
57
|
-
- **适配在 OS 层,不在 Agent 层** — 不同 Agent 最终都会调用操作系统的 syscall,这是确定性的
|
|
58
|
-
- **标准化 I/O 契约** — 任何 Agent 遵循 API 格式即可接入,无需关心内部实现
|
|
59
|
-
- **fail-closed 安全原则** — 不确定时默认拒绝
|
|
60
|
-
|
|
61
|
-
## 快速开始
|
|
62
|
-
|
|
63
|
-
### 安装
|
|
7
|
+
## 安装
|
|
64
8
|
|
|
65
9
|
```bash
|
|
66
|
-
|
|
10
|
+
npm install @kenhkl/agent-moss
|
|
67
11
|
```
|
|
68
12
|
|
|
69
|
-
|
|
13
|
+
从源码构建:
|
|
70
14
|
|
|
71
15
|
```bash
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
16
|
+
cd ts
|
|
17
|
+
npm install
|
|
18
|
+
npm run build
|
|
75
19
|
```
|
|
76
20
|
|
|
77
|
-
|
|
21
|
+
## CLI 使用
|
|
78
22
|
|
|
79
23
|
```bash
|
|
80
|
-
# 生成输入模板
|
|
81
|
-
agent-moss init -o input.json
|
|
24
|
+
# 生成输入模板
|
|
25
|
+
npx agent-moss init -o input.json
|
|
82
26
|
|
|
83
27
|
# 运行安全分析
|
|
84
|
-
agent-moss analyze input.json
|
|
85
|
-
|
|
86
|
-
# 启动 HTTP 服务 (TCP)
|
|
87
|
-
agent-moss server --port 9090
|
|
88
|
-
|
|
89
|
-
# 启动 Unix Domain Socket 服务(同机部署推荐)
|
|
90
|
-
agent-moss server --mode socket --socket /var/run/agent_moss/agent_moss.sock
|
|
91
|
-
|
|
92
|
-
# 指定配置文件
|
|
93
|
-
agent-moss server --mode socket --config /etc/agent_moss/agent_moss.yaml
|
|
94
|
-
```
|
|
95
|
-
|
|
96
|
-
### API 调用
|
|
97
|
-
|
|
98
|
-
**HTTP TCP 方式:**
|
|
99
|
-
|
|
100
|
-
```bash
|
|
101
|
-
curl -X POST http://127.0.0.1:9090/api/v1/analyze \
|
|
102
|
-
-H 'Content-Type: application/json' \
|
|
103
|
-
-d '{
|
|
104
|
-
"session_id": "test-001",
|
|
105
|
-
"prompt_session": "列出系统文件",
|
|
106
|
-
"action_history": [],
|
|
107
|
-
"a_next": {
|
|
108
|
-
"action_type": "bash",
|
|
109
|
-
"action_detail": "ls -la /tmp"
|
|
110
|
-
},
|
|
111
|
-
"reason": "查看临时目录",
|
|
112
|
-
"os_type": "",
|
|
113
|
-
"cwd": "/home/user"
|
|
114
|
-
}'
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
**Unix Domain Socket 方式(同机低延迟):**
|
|
118
|
-
|
|
119
|
-
```bash
|
|
120
|
-
curl --unix-socket /var/run/agent_moss/agent_moss.sock \
|
|
121
|
-
-X POST http://localhost/api/v1/analyze \
|
|
122
|
-
-H 'Content-Type: application/json' \
|
|
123
|
-
-d '{
|
|
124
|
-
"session_id": "test-001",
|
|
125
|
-
"a_next": {
|
|
126
|
-
"action_type": "bash",
|
|
127
|
-
"action_detail": "ls -la /tmp"
|
|
128
|
-
}
|
|
129
|
-
}'
|
|
130
|
-
```
|
|
131
|
-
|
|
132
|
-
**响应示例 (Allow)**:
|
|
133
|
-
|
|
134
|
-
```json
|
|
135
|
-
{
|
|
136
|
-
"decision": "Allow",
|
|
137
|
-
"risk_level": "low",
|
|
138
|
-
"risk_type": "",
|
|
139
|
-
"violated_layers": [],
|
|
140
|
-
"confidence": 100,
|
|
141
|
-
"analysis_duration_ms": 10.8
|
|
142
|
-
}
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
**响应示例 (Deny)**:
|
|
146
|
-
|
|
147
|
-
```json
|
|
148
|
-
{
|
|
149
|
-
"decision": "Deny",
|
|
150
|
-
"reason": "检测到递归强制删除关键路径 (rm -rf /...)",
|
|
151
|
-
"risk_level": "critical",
|
|
152
|
-
"risk_type": "script_execution",
|
|
153
|
-
"violated_layers": ["1.1"],
|
|
154
|
-
"confidence": 95,
|
|
155
|
-
"analysis_duration_ms": 1.2
|
|
156
|
-
}
|
|
157
|
-
```
|
|
158
|
-
|
|
159
|
-
## LLM 配置
|
|
160
|
-
|
|
161
|
-
AgentMoss 的三层防御中,层3(LLM 语义分析)是可选的。层1+层2 的静态规则可以独立运行。
|
|
162
|
-
|
|
163
|
-
### 启用 LLM
|
|
164
|
-
|
|
165
|
-
**方式 1:环境变量(推荐)**
|
|
166
|
-
|
|
167
|
-
```bash
|
|
168
|
-
export AGENT_MOSS_LLM_API_KEY="sk-your-key"
|
|
169
|
-
# 可选:自定义模型和 API 端点
|
|
170
|
-
export AGENT_MOSS_LLM_MODEL="glm-5.0"
|
|
171
|
-
export AGENT_MOSS_LLM_BASE_URL="https://api.nextapi.store/v1"
|
|
172
|
-
```
|
|
173
|
-
|
|
174
|
-
**方式 2:YAML 配置文件**
|
|
28
|
+
npx agent-moss analyze input.json
|
|
175
29
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
```yaml
|
|
179
|
-
llm:
|
|
180
|
-
provider: "zhipu"
|
|
181
|
-
model: "glm-5.0"
|
|
182
|
-
base_url: "https://api.nextapi.store/v1"
|
|
183
|
-
api_key_env: "AGENT_MOSS_LLM_API_KEY"
|
|
184
|
-
temperature: 0.1
|
|
185
|
-
max_tokens: 4096
|
|
186
|
-
|
|
187
|
-
security:
|
|
188
|
-
llm_analysis:
|
|
189
|
-
enabled: true
|
|
190
|
-
```
|
|
191
|
-
|
|
192
|
-
### 禁用 LLM(仅静态规则)
|
|
193
|
-
|
|
194
|
-
```bash
|
|
195
|
-
export AGENT_MOSS_DISABLE_LLM=1
|
|
196
|
-
```
|
|
197
|
-
|
|
198
|
-
### 支持的 LLM Provider
|
|
199
|
-
|
|
200
|
-
| Provider | 说明 |
|
|
201
|
-
|----------|------|
|
|
202
|
-
| OpenAI | api.openai.com |
|
|
203
|
-
| DeepSeek | api.deepseek.com |
|
|
204
|
-
| 智谱 (GLM) | open.bigmodel.cn |
|
|
205
|
-
| OpenRouter | openrouter.ai |
|
|
206
|
-
| Groq | api.groq.com |
|
|
207
|
-
| 任何 OpenAI 兼容 API | 设置 `base_url` 即可 |
|
|
208
|
-
|
|
209
|
-
## openEuler 部署
|
|
210
|
-
|
|
211
|
-
### 前提
|
|
212
|
-
|
|
213
|
-
- openEuler 22.03 LTS+
|
|
214
|
-
- Python 3.10+
|
|
215
|
-
- root 权限
|
|
216
|
-
|
|
217
|
-
### 一键安装
|
|
218
|
-
|
|
219
|
-
```bash
|
|
220
|
-
sudo dnf install -y python3 python3-pip python3-devel
|
|
221
|
-
git clone <repo-url> agent_moss
|
|
222
|
-
cd agent_moss
|
|
223
|
-
sudo bash scripts/install.sh
|
|
30
|
+
# 启动 HTTP 服务(默认端口 9090)
|
|
31
|
+
npx agent-moss server --port 9090
|
|
224
32
|
```
|
|
225
33
|
|
|
226
|
-
|
|
34
|
+
## 编程调用
|
|
227
35
|
|
|
228
|
-
```
|
|
229
|
-
|
|
230
|
-
echo "AGENT_MOSS_LLM_API_KEY=sk-your-key" | sudo tee /etc/agent_moss/agent_moss.env
|
|
231
|
-
sudo chmod 600 /etc/agent_moss/agent_moss.env
|
|
36
|
+
```typescript
|
|
37
|
+
import { createApp } from '@kenhkl/agent-moss';
|
|
232
38
|
|
|
233
|
-
|
|
234
|
-
|
|
39
|
+
const app = createApp();
|
|
40
|
+
// 通过 Hono app.fetch 在 Node.js / Bun / Electron 中调用
|
|
235
41
|
```
|
|
236
42
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
```bash
|
|
240
|
-
# HTTP TCP 模式(默认,适合跨机/调试)
|
|
241
|
-
sudo systemctl enable --now agent_moss
|
|
43
|
+
或直接 HTTP 调用:
|
|
242
44
|
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
45
|
+
```typescript
|
|
46
|
+
const resp = await fetch('http://127.0.0.1:9090/api/v1/analyze', {
|
|
47
|
+
method: 'POST',
|
|
48
|
+
headers: { 'Content-Type': 'application/json' },
|
|
49
|
+
body: JSON.stringify({
|
|
50
|
+
session_id: 'sess-001',
|
|
51
|
+
a_next: { action_type: 'bash', action_detail: 'ls -la' },
|
|
52
|
+
os_type: 'linux',
|
|
53
|
+
cwd: '/home/user',
|
|
54
|
+
}),
|
|
55
|
+
});
|
|
56
|
+
const result = await resp.json();
|
|
57
|
+
// → { decision: 'Allow'|'Deny', risk_level: 'low'|'...', ... }
|
|
246
58
|
```
|
|
247
59
|
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
```bash
|
|
251
|
-
sudo systemctl enable --now agent_moss
|
|
252
|
-
|
|
253
|
-
# HTTP 模式验证
|
|
254
|
-
curl http://127.0.0.1:9090/api/v1/health
|
|
255
|
-
# {"status":"healthy","version":"0.1.0"}
|
|
60
|
+
## 架构
|
|
256
61
|
|
|
257
|
-
# Socket 模式验证
|
|
258
|
-
curl --unix-socket /var/run/agent_moss/agent_moss.sock \
|
|
259
|
-
http://localhost/api/v1/health
|
|
260
62
|
```
|
|
63
|
+
快速放行白名单
|
|
64
|
+
├── 完全安全 (15 bash 模式 + 6 action types) → 跳过 L2+L3
|
|
65
|
+
└── 只读敏感 (15 bash 模式 + 8 action types) → 跳过 L3
|
|
261
66
|
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
| `systemctl start agent_moss` | 启动 |
|
|
267
|
-
| `systemctl stop agent_moss` | 停止 |
|
|
268
|
-
| `systemctl restart agent_moss` | 重启 |
|
|
269
|
-
| `systemctl status agent_moss` | 查看状态 |
|
|
270
|
-
| `journalctl -u agent_moss -f` | 查看日志 |
|
|
271
|
-
| `systemctl disable agent_moss` | 取消开机启动 |
|
|
67
|
+
层1: 启发式静态检测 (<1ms)
|
|
68
|
+
├── 危险命令正则: 30 个模式(rm 分级检测: 5 条严格→宽松规则)
|
|
69
|
+
├── 注入关键词: 56 个中英双语关键词(9 类: 指令覆盖/角色劫持/命令注入/...)
|
|
70
|
+
└── 自定义规则: AGENT_MOSS_CUSTOM_RULES 环境变量注入
|
|
272
71
|
|
|
273
|
-
|
|
72
|
+
层2: 逻辑规则检测 (<1ms)
|
|
73
|
+
├── Rule 1: read-before-write(有历史但无读取 → flag)
|
|
74
|
+
├── Rule 2: 意图偏离(读取意图 vs 删除动作)
|
|
75
|
+
├── Rule 3: 敏感路径访问(20 条路径,4 级风险: critical/high/medium/low)
|
|
76
|
+
├── Rule 4: 危险操作(通配符删除 + 重定向覆盖 /etc/ /boot/ /proc/)
|
|
77
|
+
├── Rule 5: 提权检测(sudo/su/chmod u+s/setuid)
|
|
78
|
+
└── Rule 6: 横向移动(ssh/scp/rsync,排除 localhost)
|
|
274
79
|
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
| 方法 | 路径 | 说明 |
|
|
283
|
-
|------|------|------|
|
|
284
|
-
| `GET` | `/api/v1/health` | 健康检查 |
|
|
285
|
-
| `POST` | `/api/v1/analyze` | 安全分析 |
|
|
286
|
-
|
|
287
|
-
### POST /api/v1/analyze
|
|
288
|
-
|
|
289
|
-
**请求体 (v2)**:
|
|
290
|
-
|
|
291
|
-
```json
|
|
292
|
-
{
|
|
293
|
-
"session_id": "会话ID (必填)",
|
|
294
|
-
"prompt_session": "原始任务描述 (可选)",
|
|
295
|
-
"action_history": [{"action_type": "...", "action_detail": "..."}],
|
|
296
|
-
"a_next": {
|
|
297
|
-
"action_type": "bash",
|
|
298
|
-
"action_detail": "cat /etc/passwd"
|
|
299
|
-
},
|
|
300
|
-
"reason": "执行理由 (可选)",
|
|
301
|
-
"os_type": "",
|
|
302
|
-
"cwd": "/home/user/project",
|
|
303
|
-
"metadata": {"agent_id": "...", "sandbox_id": "..."}
|
|
304
|
-
}
|
|
80
|
+
层3: LLM + Skill 深度分析 (2-5s)
|
|
81
|
+
├── 脚本预扫描: 读取 .sh 文件(最多 500 行),16 个可疑模式
|
|
82
|
+
├── Skill 匹配: 12 个 Skill,关键词加权评分,返回 top 3
|
|
83
|
+
├── LLM 缓存: 200 条 FIFO,避免重复调用
|
|
84
|
+
├── Provider 识别: 15+ Provider,自动注入 HTTP Header
|
|
85
|
+
├── LLM 调用: 支持重试(默认 2 次重试 = 3 次尝试)
|
|
86
|
+
└── fail-closed: 失败时拒绝(confidence=60, source='llm_failure')
|
|
305
87
|
```
|
|
306
88
|
|
|
307
|
-
|
|
308
|
-
|------|------|------|------|
|
|
309
|
-
| `session_id` | string | 是 | 会话唯一标识 |
|
|
310
|
-
| `prompt_session` | string | 否 | 原始任务描述 |
|
|
311
|
-
| `action_history` | array | 否 | 历史动作序列 |
|
|
312
|
-
| `a_next.action_type` | string | 是 | 动作类型 (bash/file_read/file_write 等) |
|
|
313
|
-
| `a_next.action_detail` | string | 是 | 命令/动作详情 |
|
|
314
|
-
| `reason` | string | 否 | 执行理由 |
|
|
315
|
-
| `os_type` | string | 否 | **v2 新增** — `"linux"` / `"windows"`,留空自动检测 |
|
|
316
|
-
| `cwd` | string | 否 | **v2 新增** — 当前工作目录 |
|
|
317
|
-
| `metadata` | object | 否 | 扩展元数据 |
|
|
318
|
-
|
|
319
|
-
**响应字段**:
|
|
320
|
-
|
|
321
|
-
| 字段 | 类型 | 说明 |
|
|
322
|
-
|------|------|------|
|
|
323
|
-
| `decision` | string | `Allow` / `Deny` |
|
|
324
|
-
| `reason` | string | 决策原因 |
|
|
325
|
-
| `risk_level` | string | `low` / `medium` / `high` / `critical` |
|
|
326
|
-
| `risk_type` | string | 风险类别 (file_access, script_execution, data_exfiltration, ...) |
|
|
327
|
-
| `violated_layers` | array | 触发的检测层,如 `["1.1", "1.2"]` |
|
|
328
|
-
| `violated_policy` | string | 违反的具体条款 (Deny 时) |
|
|
329
|
-
| `policy` | string | Cerberus TOML 策略 (Allow 时) |
|
|
330
|
-
| `confidence` | int | 置信度 0-100 |
|
|
331
|
-
| `analysis_duration_ms` | float | 分析耗时(毫秒) |
|
|
332
|
-
|
|
333
|
-
## 配置
|
|
334
|
-
|
|
335
|
-
优先级:环境变量 > YAML 配置文件 > 内置默认值
|
|
336
|
-
|
|
337
|
-
### 环境变量
|
|
89
|
+
## 环境变量
|
|
338
90
|
|
|
339
91
|
| 变量 | 说明 | 默认值 |
|
|
340
92
|
|------|------|--------|
|
|
341
|
-
| `
|
|
342
|
-
| `AGENT_MOSS_LLM_API_KEY` | LLM API Key | 未设置 |
|
|
93
|
+
| `AGENT_MOSS_LLM_API_KEY` | LLM API Key(回退 `OPENAI_API_KEY`) | 未设置 |
|
|
343
94
|
| `AGENT_MOSS_LLM_MODEL` | LLM 模型名称 | `gpt-4o` |
|
|
344
95
|
| `AGENT_MOSS_LLM_BASE_URL` | LLM API 端点 | `https://api.openai.com/v1` |
|
|
96
|
+
| `AGENT_MOSS_LLM_TEMPERATURE` | 采样温度 | `0.1` |
|
|
97
|
+
| `AGENT_MOSS_LLM_MAX_TOKENS` | 最大输出 token 数 | `4096` |
|
|
345
98
|
| `AGENT_MOSS_LLM_TIMEOUT` | LLM 超时(秒) | `300` |
|
|
346
|
-
| `
|
|
347
|
-
| `
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
参考 `config/agent_moss.yaml`。
|
|
352
|
-
|
|
353
|
-
### v2 新增配置
|
|
99
|
+
| `AGENT_MOSS_LLM_RETRIES` | LLM 调用重试次数 | `2`(共 3 次尝试) |
|
|
100
|
+
| `AGENT_MOSS_DISABLE_LLM` | 设为 `1` 禁用层3 LLM | 未设置 |
|
|
101
|
+
| `AGENT_MOSS_DISABLE_HEURISTIC` | 设为 `1` 禁用层1 启发式 | 未设置 |
|
|
102
|
+
| `AGENT_MOSS_DISABLE_LOGIC_RULES` | 设为 `1` 禁用层2 逻辑规则 | 未设置 |
|
|
103
|
+
| `AGENT_MOSS_CUSTOM_RULES` | 自定义规则 JSON 数组 | `[]` |
|
|
354
104
|
|
|
355
|
-
|
|
356
|
-
# OS Profile 自动选择
|
|
357
|
-
os_profile:
|
|
358
|
-
type: "" # 留空自动检测,可手动指定 linux / windows
|
|
105
|
+
### 自定义规则
|
|
359
106
|
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
107
|
+
```bash
|
|
108
|
+
export AGENT_MOSS_CUSTOM_RULES='[
|
|
109
|
+
{"pattern": "kubectl delete namespace", "action": "Deny", "severity": "critical"},
|
|
110
|
+
{"pattern": "docker rm -f", "action": "Deny", "severity": "high"}
|
|
111
|
+
]'
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## 支持的 Provider
|
|
115
|
+
|
|
116
|
+
| Provider | URL 匹配 | Provider Hint | Header 注入 |
|
|
117
|
+
|----------|---------|---------------|-------------|
|
|
118
|
+
| OpenAI | `api.openai.com` | `openai` | — |
|
|
119
|
+
| Anthropic | `anthropic.com` | `claude` / `anthropic` | — |
|
|
120
|
+
| Google | `generativelanguage.googleapis.com` | `google` / `gemini` | — |
|
|
121
|
+
| DeepSeek | `api.deepseek.com` | `deepseek` | — |
|
|
122
|
+
| 智谱 (GLM) | `open.bigmodel.cn` | `zhipu` / `glm-cn` | — |
|
|
123
|
+
| 智谱 Coding Plan | `api.z.ai` | `zai-coding-plan` | — |
|
|
124
|
+
| OpenRouter | `openrouter.ai` | `openrouter` | Referer + Title |
|
|
125
|
+
| Groq | `api.groq.com` | `groq` | — |
|
|
126
|
+
| Mistral | `api.mistral.ai` | `mistral` | — |
|
|
127
|
+
| Together | `api.together.xyz` | `together` | — |
|
|
128
|
+
| xAI | `api.x.ai` | `xai` / `xai-grok` | Referer |
|
|
129
|
+
| MiniMax | `api.minimaxi.com` | `minimax` | — |
|
|
130
|
+
| GitCode | `api-ai.gitcode.com` | `gitcode` | — |
|
|
131
|
+
| Ollama | `:11434` | `ollama` | — |
|
|
132
|
+
| 任意兼容 | — | `other` / `custom` / `local` | — |
|
|
365
133
|
|
|
366
134
|
## 项目结构
|
|
367
135
|
|
|
368
136
|
```
|
|
369
|
-
|
|
370
|
-
├──
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
├──
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
│ ├── app.py # FastAPI 应用 + 多模式启动
|
|
385
|
-
│ ├── socket_server.py # [v2 新增] Unix Domain Socket
|
|
386
|
-
│ ├── routes.py # API 路由
|
|
387
|
-
│ └── models.py # Pydantic 请求/响应模型
|
|
388
|
-
├── adapters/ # 适配器层
|
|
389
|
-
│ └── observable.py # AgentOS 可观测服务数据适配器
|
|
390
|
-
├── infra/ # 基础设施
|
|
391
|
-
│ ├── config.py # 配置管理
|
|
392
|
-
│ ├── llm_client.py # LLM 客户端 (OpenAI SDK)
|
|
393
|
-
│ └── ...
|
|
394
|
-
├── skills/ # 安全 Skill 规则 (12 个 Markdown)
|
|
395
|
-
├── rules/ # 用户自定义规则
|
|
396
|
-
├── templates/ # Prompt 模板
|
|
397
|
-
└── cli.py # 命令行工具
|
|
398
|
-
config/
|
|
399
|
-
└── agent_moss.yaml # YAML 配置模板
|
|
400
|
-
docs/
|
|
401
|
-
├── design.md # 基础架构设计
|
|
402
|
-
├── agent_moss_adapter_design_v2.md # v2 适配方案设计
|
|
403
|
-
└── agent_collector_design_v1.md # Agent 数据采集服务设计
|
|
404
|
-
tests/
|
|
405
|
-
├── conftest.py # 共享 fixtures
|
|
406
|
-
├── test_all_cases.py # 全量测试 (48 个用例)
|
|
407
|
-
└── cases/ # 测试用例 JSON
|
|
137
|
+
ts/src/
|
|
138
|
+
├── cli.ts # CLI 入口 (init/analyze/server)
|
|
139
|
+
├── config.ts # 全局配置 (环境变量 + Provider 识别 + Header 注入)
|
|
140
|
+
├── models.ts # Zod 数据模型
|
|
141
|
+
├── routes.ts # Hono API 路由 (POST /analyze, GET /health)
|
|
142
|
+
├── server.ts # Hono HTTP 服务 (CORS + logger)
|
|
143
|
+
└── engine/
|
|
144
|
+
├── types.ts # 核心类型 (SecurityJudgment, LLMConfig, ...)
|
|
145
|
+
├── coordinator.ts # 三层协调器 + 两级快速放行白名单
|
|
146
|
+
├── heuristic.ts # 层1: 30 危险模式 + 56 注入关键词
|
|
147
|
+
├── logic-rules.ts # 层2: 6 条规则 + 20 敏感路径
|
|
148
|
+
├── llm-analyzer.ts # 层3: LLM 分析 (缓存 + 重试 + fail-closed)
|
|
149
|
+
├── script-analyzer.ts # 脚本内容预扫描 (18 个可疑模式)
|
|
150
|
+
├── skill-loader.ts # Skill 加载与关键词匹配 (12 个 Skill)
|
|
151
|
+
└── template-loader.ts # Prompt 模板加载 (3 个模板)
|
|
408
152
|
```
|
|
409
153
|
|
|
410
|
-
##
|
|
154
|
+
## 依赖
|
|
411
155
|
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
156
|
+
| 包 | 说明 |
|
|
157
|
+
|---|---|
|
|
158
|
+
| `hono` | HTTP 框架 |
|
|
159
|
+
| `@hono/node-server` | Node.js HTTP 适配 |
|
|
160
|
+
| `openai` | LLM API 客户端 |
|
|
161
|
+
| `zod` | 数据校验 |
|
|
162
|
+
| `yaml` | YAML 解析 |
|
|
415
163
|
|
|
416
|
-
|
|
417
|
-
AGENT_MOSS_LLM_API_KEY="sk-xxx" python3 -m pytest tests/ -v
|
|
164
|
+
## 构建
|
|
418
165
|
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
166
|
+
```bash
|
|
167
|
+
npm run build # tsup 打包到 dist/
|
|
168
|
+
npm run dev # tsx 热重载开发 (端口 9090)
|
|
422
169
|
```
|