adp-openclaw 0.0.2
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/DESIGN.md +733 -0
- package/README.md +70 -0
- package/USAGE.md +910 -0
- package/WEBSOCKET_PROTOCOL.md +506 -0
- package/index.ts +17 -0
- package/openclaw.plugin.json +22 -0
- package/package.json +26 -0
- package/server/.claude/settings.local.json +16 -0
- package/server/go.mod +5 -0
- package/server/main.go +786 -0
- package/src/channel.ts +245 -0
- package/src/config-schema.ts +8 -0
- package/src/monitor.ts +325 -0
- package/src/runtime.ts +15 -0
package/USAGE.md
ADDED
|
@@ -0,0 +1,910 @@
|
|
|
1
|
+
# Simple Go Plugin - 完整使用说明
|
|
2
|
+
|
|
3
|
+
## 目录
|
|
4
|
+
- [简介](#简介)
|
|
5
|
+
- [系统架构](#系统架构)
|
|
6
|
+
- [环境要求](#环境要求)
|
|
7
|
+
- [安装步骤](#安装步骤)
|
|
8
|
+
- [配置说明](#配置说明)
|
|
9
|
+
- [启动服务](#启动服务)
|
|
10
|
+
- [测试验证](#测试验证)
|
|
11
|
+
- [API接口文档](#api接口文档)
|
|
12
|
+
- [故障排查](#故障排查)
|
|
13
|
+
- [开发指南](#开发指南)
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## 简介
|
|
18
|
+
|
|
19
|
+
Simple Go 是一个 OpenClaw 演示插件,展示如何通过 Go HTTP 后端集成自定义消息通道。该插件实现了完整的消息收发流程,支持多用户对话和会话管理。
|
|
20
|
+
|
|
21
|
+
### 主要特性
|
|
22
|
+
- ✅ Go HTTP 后端服务器
|
|
23
|
+
- ✅ API Token 认证
|
|
24
|
+
- ✅ 多客户端支持
|
|
25
|
+
- ✅ 多轮对话管理
|
|
26
|
+
- ✅ 会话持久化
|
|
27
|
+
- ✅ 负载均衡(最少消息队列优先)
|
|
28
|
+
- ✅ 实时消息轮询
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## 系统架构
|
|
33
|
+
|
|
34
|
+
```
|
|
35
|
+
┌─────────────┐ HTTP ┌─────────────┐
|
|
36
|
+
│ 外部用户 │ ────────────► │ Go Server │
|
|
37
|
+
│ │ /inbound │ (port 9876)│
|
|
38
|
+
└─────────────┘ └──────┬──────┘
|
|
39
|
+
│
|
|
40
|
+
│ /poll (1s)
|
|
41
|
+
▼
|
|
42
|
+
┌─────────────┐
|
|
43
|
+
│ OpenClaw │
|
|
44
|
+
│ Gateway │
|
|
45
|
+
└──────┬──────┘
|
|
46
|
+
│
|
|
47
|
+
▼
|
|
48
|
+
┌─────────────┐
|
|
49
|
+
│ AI Agent │
|
|
50
|
+
│ (Claude) │
|
|
51
|
+
└──────┬──────┘
|
|
52
|
+
│
|
|
53
|
+
│ /send
|
|
54
|
+
▼
|
|
55
|
+
┌─────────────┐
|
|
56
|
+
│ Go Server │
|
|
57
|
+
│ /outbox │
|
|
58
|
+
└─────────────┘
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### 组件说明
|
|
62
|
+
|
|
63
|
+
1. **Go HTTP Server** (`server/main.go`)
|
|
64
|
+
- 监听端口:9876
|
|
65
|
+
- 管理消息队列和会话
|
|
66
|
+
- 提供 REST API 接口
|
|
67
|
+
|
|
68
|
+
2. **OpenClaw Plugin** (`src/`)
|
|
69
|
+
- `channel.ts`: 插件配置和生命周期管理
|
|
70
|
+
- `monitor.ts`: 消息轮询和处理逻辑
|
|
71
|
+
- `runtime.ts`: 运行时状态管理
|
|
72
|
+
|
|
73
|
+
3. **OpenClaw Gateway**
|
|
74
|
+
- 管理插件生命周期
|
|
75
|
+
- 路由消息到 AI 代理
|
|
76
|
+
- 处理认证和会话
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
## 环境要求
|
|
81
|
+
|
|
82
|
+
### 必需软件
|
|
83
|
+
- **Go**: 1.22.0 或更高版本
|
|
84
|
+
- **Node.js**: 22.22.0 或更高版本
|
|
85
|
+
- **OpenClaw**: 2026.1.29 或更高版本
|
|
86
|
+
|
|
87
|
+
### 系统要求
|
|
88
|
+
- Linux/macOS/Windows
|
|
89
|
+
- 至少 512MB 可用内存
|
|
90
|
+
- 网络连接(用于 AI API 调用)
|
|
91
|
+
|
|
92
|
+
---
|
|
93
|
+
|
|
94
|
+
## 安装步骤
|
|
95
|
+
|
|
96
|
+
### 1. 安装 Go
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
# 如果未安装 Go
|
|
100
|
+
cd server
|
|
101
|
+
wget https://go.dev/dl/go1.22.0.linux-amd64.tar.gz
|
|
102
|
+
sudo tar -C /usr/local -xzf go1.22.0.linux-amd64.tar.gz
|
|
103
|
+
export PATH=$PATH:/usr/local/go/bin
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### 2. 安装依赖
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
# 安装 Node.js 依赖
|
|
110
|
+
cd /root/simplego
|
|
111
|
+
npm install
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### 3. 安装 OpenClaw 插件
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
# 插件已在 ~/simplego 目录,OpenClaw 会自动加载
|
|
118
|
+
openclaw plugins list | grep "Simple Go"
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
预期输出:
|
|
122
|
+
```
|
|
123
|
+
│ Simple Go │ simplego │ loaded │ ~/simplego/index.ts │ 0.0.1 │
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
## 配置说明
|
|
129
|
+
|
|
130
|
+
### 1. 启动 Go 服务器
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
cd /root/simplego/server
|
|
134
|
+
|
|
135
|
+
# 启动服务器(会自动生成 admin token)
|
|
136
|
+
go run main.go
|
|
137
|
+
|
|
138
|
+
# 或指定 admin token
|
|
139
|
+
go run main.go --admin-token "your-admin-token-here"
|
|
140
|
+
|
|
141
|
+
# 后台运行
|
|
142
|
+
nohup go run main.go --admin-token "your-admin-token" > /tmp/go-server.log 2>&1 &
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
服务器启动后会显示:
|
|
146
|
+
```
|
|
147
|
+
Generated admin token: <token>
|
|
148
|
+
Simple Go channel server starting on http://localhost:9876
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
**保存 admin token,后续需要使用!**
|
|
152
|
+
|
|
153
|
+
### 2. 注册 OpenClaw 客户端
|
|
154
|
+
|
|
155
|
+
使用 admin token 注册客户端:
|
|
156
|
+
|
|
157
|
+
```bash
|
|
158
|
+
curl -X POST http://localhost:9876/clients \
|
|
159
|
+
-H "Content-Type: application/json" \
|
|
160
|
+
-H "Authorization: Bearer <admin-token>" \
|
|
161
|
+
-d '{"name": "openclaw-main"}'
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
响应示例:
|
|
165
|
+
```json
|
|
166
|
+
{
|
|
167
|
+
"clientId": "client-666580ae8729e2b7",
|
|
168
|
+
"token": "92527c1a861bb50bd1acc47180ef317369e97529c7a5491b2bc67839992cfbb8",
|
|
169
|
+
"name": "openclaw-main"
|
|
170
|
+
}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
**保存 client token,配置插件时需要使用!**
|
|
174
|
+
|
|
175
|
+
### 3. 配置 OpenClaw 插件
|
|
176
|
+
|
|
177
|
+
```bash
|
|
178
|
+
# 方式 1: 使用环境变量
|
|
179
|
+
export SIMPLEGO_SERVER_URL="http://localhost:9876"
|
|
180
|
+
export SIMPLEGO_API_TOKEN="<client-token>"
|
|
181
|
+
|
|
182
|
+
# 方式 2: 使用配置文件
|
|
183
|
+
openclaw config set channels.simplego.enabled true
|
|
184
|
+
openclaw config set channels.simplego.serverUrl "http://localhost:9876"
|
|
185
|
+
openclaw config set channels.simplego.apiToken "<client-token>"
|
|
186
|
+
openclaw config set channels.simplego.pollIntervalMs 1000
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### 4. 配置 AI API Key
|
|
190
|
+
|
|
191
|
+
```bash
|
|
192
|
+
# 检查环境变量
|
|
193
|
+
echo $ANTHROPIC_AUTH_TOKEN
|
|
194
|
+
|
|
195
|
+
# 如果未设置,需要配置
|
|
196
|
+
export ANTHROPIC_AUTH_TOKEN="your-api-key"
|
|
197
|
+
export ANTHROPIC_BASE_URL="https://api.anthropic.com" # 可选
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
或者编辑 `~/.openclaw/agents/main/agent/models.json`:
|
|
201
|
+
|
|
202
|
+
```json
|
|
203
|
+
{
|
|
204
|
+
"providers": {
|
|
205
|
+
"anthropic": {
|
|
206
|
+
"baseUrl": "https://api.anthropic.com/v1",
|
|
207
|
+
"apiKey": "your-api-key",
|
|
208
|
+
"api": "anthropic",
|
|
209
|
+
"models": [
|
|
210
|
+
{
|
|
211
|
+
"id": "claude-opus-4-5-20251101",
|
|
212
|
+
"name": "Claude Opus 4.5",
|
|
213
|
+
"reasoning": true,
|
|
214
|
+
"input": ["text"],
|
|
215
|
+
"contextWindow": 200000,
|
|
216
|
+
"maxTokens": 8192
|
|
217
|
+
}
|
|
218
|
+
]
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
---
|
|
225
|
+
|
|
226
|
+
## 启动服务
|
|
227
|
+
|
|
228
|
+
### 1. 启动 Go 服务器
|
|
229
|
+
|
|
230
|
+
```bash
|
|
231
|
+
cd /root/simplego/server
|
|
232
|
+
nohup go run main.go --admin-token "17b86aef58c8a92d9152f7b1ccf3b10482b9b71ea3209bf6ff964189378e742e" > /tmp/go-server.log 2>&1 &
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
### 2. 启动 OpenClaw Gateway
|
|
236
|
+
|
|
237
|
+
```bash
|
|
238
|
+
# 启动网关
|
|
239
|
+
openclaw gateway start
|
|
240
|
+
|
|
241
|
+
# 检查状态
|
|
242
|
+
openclaw gateway status
|
|
243
|
+
|
|
244
|
+
# 查看日志
|
|
245
|
+
openclaw logs --follow
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
### 3. 验证服务状态
|
|
249
|
+
|
|
250
|
+
```bash
|
|
251
|
+
# 检查 Go 服务器
|
|
252
|
+
curl http://localhost:9876/health
|
|
253
|
+
|
|
254
|
+
# 检查插件状态
|
|
255
|
+
openclaw channels list
|
|
256
|
+
|
|
257
|
+
# 检查网关状态
|
|
258
|
+
openclaw health
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
预期输出:
|
|
262
|
+
```
|
|
263
|
+
Simple Go: configured
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
---
|
|
267
|
+
|
|
268
|
+
## 测试验证
|
|
269
|
+
|
|
270
|
+
### 1. 发送测试消息
|
|
271
|
+
|
|
272
|
+
```bash
|
|
273
|
+
# 发送消息
|
|
274
|
+
curl -X POST http://localhost:9876/inbound \
|
|
275
|
+
-H "Content-Type: application/json" \
|
|
276
|
+
-d '{"from": "testuser", "text": "Hello! What is 2 plus 2?"}'
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
响应示例:
|
|
280
|
+
```json
|
|
281
|
+
{
|
|
282
|
+
"id": "msg-1",
|
|
283
|
+
"conversationId": "conv-abc123",
|
|
284
|
+
"clientId": "client-666580ae8729e2b7",
|
|
285
|
+
"from": "testuser",
|
|
286
|
+
"to": "bot",
|
|
287
|
+
"text": "Hello! What is 2 plus 2?",
|
|
288
|
+
"timestamp": 1770213375101
|
|
289
|
+
}
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
### 2. 查看响应
|
|
293
|
+
|
|
294
|
+
```bash
|
|
295
|
+
# 等待几秒让 AI 处理
|
|
296
|
+
sleep 5
|
|
297
|
+
|
|
298
|
+
# 查看所有响应
|
|
299
|
+
curl http://localhost:9876/outbox | jq .
|
|
300
|
+
|
|
301
|
+
# 查看最新响应
|
|
302
|
+
curl http://localhost:9876/outbox | jq '.messages[-1]'
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
预期响应:
|
|
306
|
+
```json
|
|
307
|
+
{
|
|
308
|
+
"id": "msg-2",
|
|
309
|
+
"conversationId": "conv-abc123",
|
|
310
|
+
"clientId": "client-666580ae8729e2b7",
|
|
311
|
+
"from": "bot",
|
|
312
|
+
"to": "testuser",
|
|
313
|
+
"text": "2 plus 2 equals 4.",
|
|
314
|
+
"timestamp": 1770213378456
|
|
315
|
+
}
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
### 3. 查看会话历史
|
|
319
|
+
|
|
320
|
+
```bash
|
|
321
|
+
# 获取会话 ID(从上面的响应中)
|
|
322
|
+
CONV_ID="conv-abc123"
|
|
323
|
+
|
|
324
|
+
# 查看会话历史
|
|
325
|
+
curl -H "Authorization: Bearer <client-token>" \
|
|
326
|
+
http://localhost:9876/conversations/$CONV_ID | jq .
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
### 4. 测试多轮对话
|
|
330
|
+
|
|
331
|
+
```bash
|
|
332
|
+
# 第一轮
|
|
333
|
+
curl -X POST http://localhost:9876/inbound \
|
|
334
|
+
-H "Content-Type: application/json" \
|
|
335
|
+
-d '{"from": "alice", "text": "My name is Alice"}'
|
|
336
|
+
|
|
337
|
+
sleep 5
|
|
338
|
+
|
|
339
|
+
# 第二轮(同一用户)
|
|
340
|
+
curl -X POST http://localhost:9876/inbound \
|
|
341
|
+
-H "Content-Type: application/json" \
|
|
342
|
+
-d '{"from": "alice", "text": "What is my name?"}'
|
|
343
|
+
|
|
344
|
+
sleep 5
|
|
345
|
+
|
|
346
|
+
# 查看响应
|
|
347
|
+
curl http://localhost:9876/outbox | jq '.messages[-2:]'
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
---
|
|
351
|
+
|
|
352
|
+
## API接口文档
|
|
353
|
+
|
|
354
|
+
### 公共端点(无需认证)
|
|
355
|
+
|
|
356
|
+
#### 1. 健康检查
|
|
357
|
+
```
|
|
358
|
+
GET /health
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
响应:
|
|
362
|
+
```json
|
|
363
|
+
{
|
|
364
|
+
"ok": true,
|
|
365
|
+
"server": "simplego",
|
|
366
|
+
"clients": 1,
|
|
367
|
+
"conversations": 5
|
|
368
|
+
}
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
#### 2. 发送消息(支持多用户身份)
|
|
372
|
+
```
|
|
373
|
+
POST /inbound
|
|
374
|
+
Content-Type: application/json
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
**基础请求(简单模式)**:
|
|
378
|
+
```json
|
|
379
|
+
{
|
|
380
|
+
"from": "user_123",
|
|
381
|
+
"text": "你好,请帮我查询订单"
|
|
382
|
+
}
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
**完整请求(多用户/多租户模式,类似飞书)**:
|
|
386
|
+
```json
|
|
387
|
+
{
|
|
388
|
+
"userId": "user_123",
|
|
389
|
+
"query": "你好,请帮我查询订单",
|
|
390
|
+
"user": {
|
|
391
|
+
"userId": "user_123",
|
|
392
|
+
"username": "张三",
|
|
393
|
+
"avatar": "https://example.com/avatar.png",
|
|
394
|
+
"email": "zhangsan@company.com",
|
|
395
|
+
"tenantId": "tenant_abc",
|
|
396
|
+
"source": "web",
|
|
397
|
+
"extra": {
|
|
398
|
+
"department": "技术部",
|
|
399
|
+
"role": "developer"
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
**快捷请求(部分用户信息)**:
|
|
406
|
+
```json
|
|
407
|
+
{
|
|
408
|
+
"from": "user_123",
|
|
409
|
+
"text": "你好",
|
|
410
|
+
"username": "张三",
|
|
411
|
+
"tenantId": "tenant_abc",
|
|
412
|
+
"source": "mobile_app"
|
|
413
|
+
}
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
**字段说明**:
|
|
417
|
+
| 字段 | 必填 | 说明 |
|
|
418
|
+
|------|------|------|
|
|
419
|
+
| `from` / `userId` / `user.userId` | 是 | 用户唯一标识(三选一) |
|
|
420
|
+
| `text` / `query` | 是 | 用户查询内容(二选一) |
|
|
421
|
+
| `user` | 否 | 完整用户信息对象 |
|
|
422
|
+
| `user.username` | 否 | 用户显示名称 |
|
|
423
|
+
| `user.avatar` | 否 | 用户头像 URL |
|
|
424
|
+
| `user.email` | 否 | 用户邮箱 |
|
|
425
|
+
| `user.tenantId` | 否 | 租户/组织 ID(多租户场景) |
|
|
426
|
+
| `user.source` | 否 | 请求来源平台 |
|
|
427
|
+
| `user.extra` | 否 | 额外用户元数据 |
|
|
428
|
+
| `username` | 否 | 快捷用户名字段 |
|
|
429
|
+
| `tenantId` | 否 | 快捷租户 ID 字段 |
|
|
430
|
+
| `source` | 否 | 快捷来源字段 |
|
|
431
|
+
|
|
432
|
+
响应:
|
|
433
|
+
```json
|
|
434
|
+
{
|
|
435
|
+
"message": {
|
|
436
|
+
"id": "msg-1",
|
|
437
|
+
"conversationId": "conv-abc123",
|
|
438
|
+
"clientId": "client-xxx",
|
|
439
|
+
"from": "user_123",
|
|
440
|
+
"to": "bot",
|
|
441
|
+
"text": "你好,请帮我查询订单",
|
|
442
|
+
"timestamp": 1770213375101,
|
|
443
|
+
"user": {
|
|
444
|
+
"userId": "user_123",
|
|
445
|
+
"username": "张三",
|
|
446
|
+
"tenantId": "tenant_abc",
|
|
447
|
+
"source": "web"
|
|
448
|
+
}
|
|
449
|
+
},
|
|
450
|
+
"delivered": true,
|
|
451
|
+
"websocket": true,
|
|
452
|
+
"conversationId": "conv-abc123",
|
|
453
|
+
"userId": "user_123"
|
|
454
|
+
}
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
#### 3. 查看发送的消息
|
|
458
|
+
```
|
|
459
|
+
GET /outbox
|
|
460
|
+
```
|
|
461
|
+
|
|
462
|
+
响应:
|
|
463
|
+
```json
|
|
464
|
+
{
|
|
465
|
+
"messages": [
|
|
466
|
+
{
|
|
467
|
+
"id": "msg-2",
|
|
468
|
+
"conversationId": "conv-abc123",
|
|
469
|
+
"clientId": "client-xxx",
|
|
470
|
+
"from": "bot",
|
|
471
|
+
"to": "user_id",
|
|
472
|
+
"text": "response text",
|
|
473
|
+
"timestamp": 1770213378456
|
|
474
|
+
}
|
|
475
|
+
]
|
|
476
|
+
}
|
|
477
|
+
```
|
|
478
|
+
|
|
479
|
+
### 管理端点(需要 Admin Token)
|
|
480
|
+
|
|
481
|
+
#### 4. 注册客户端
|
|
482
|
+
```
|
|
483
|
+
POST /clients
|
|
484
|
+
Authorization: Bearer <admin-token>
|
|
485
|
+
Content-Type: application/json
|
|
486
|
+
|
|
487
|
+
{
|
|
488
|
+
"name": "client-name"
|
|
489
|
+
}
|
|
490
|
+
```
|
|
491
|
+
|
|
492
|
+
响应:
|
|
493
|
+
```json
|
|
494
|
+
{
|
|
495
|
+
"clientId": "client-xxx",
|
|
496
|
+
"token": "client-token-xxx",
|
|
497
|
+
"name": "client-name"
|
|
498
|
+
}
|
|
499
|
+
```
|
|
500
|
+
|
|
501
|
+
#### 5. 列出所有客户端
|
|
502
|
+
```
|
|
503
|
+
GET /clients
|
|
504
|
+
Authorization: Bearer <admin-token>
|
|
505
|
+
```
|
|
506
|
+
|
|
507
|
+
响应:
|
|
508
|
+
```json
|
|
509
|
+
{
|
|
510
|
+
"clients": [
|
|
511
|
+
{
|
|
512
|
+
"id": "client-xxx",
|
|
513
|
+
"name": "openclaw-main",
|
|
514
|
+
"lastSeenAt": 1770213375101
|
|
515
|
+
}
|
|
516
|
+
]
|
|
517
|
+
}
|
|
518
|
+
```
|
|
519
|
+
|
|
520
|
+
#### 6. 列出所有会话
|
|
521
|
+
```
|
|
522
|
+
GET /conversations
|
|
523
|
+
Authorization: Bearer <admin-token>
|
|
524
|
+
```
|
|
525
|
+
|
|
526
|
+
响应:
|
|
527
|
+
```json
|
|
528
|
+
{
|
|
529
|
+
"conversations": [
|
|
530
|
+
{
|
|
531
|
+
"id": "conv-abc123",
|
|
532
|
+
"userId": "alice",
|
|
533
|
+
"clientId": "client-xxx",
|
|
534
|
+
"messages": [...],
|
|
535
|
+
"createdAt": 1770213375101,
|
|
536
|
+
"updatedAt": 1770213378456
|
|
537
|
+
}
|
|
538
|
+
]
|
|
539
|
+
}
|
|
540
|
+
```
|
|
541
|
+
|
|
542
|
+
### 客户端端点(需要 Client Token)
|
|
543
|
+
|
|
544
|
+
#### 7. 轮询消息
|
|
545
|
+
```
|
|
546
|
+
GET /poll
|
|
547
|
+
Authorization: Bearer <client-token>
|
|
548
|
+
```
|
|
549
|
+
|
|
550
|
+
响应:
|
|
551
|
+
```json
|
|
552
|
+
{
|
|
553
|
+
"messages": [
|
|
554
|
+
{
|
|
555
|
+
"id": "msg-1",
|
|
556
|
+
"conversationId": "conv-abc123",
|
|
557
|
+
"from": "user_id",
|
|
558
|
+
"text": "message content",
|
|
559
|
+
"timestamp": 1770213375101
|
|
560
|
+
}
|
|
561
|
+
]
|
|
562
|
+
}
|
|
563
|
+
```
|
|
564
|
+
|
|
565
|
+
#### 8. 发送回复
|
|
566
|
+
```
|
|
567
|
+
POST /send
|
|
568
|
+
Authorization: Bearer <client-token>
|
|
569
|
+
Content-Type: application/json
|
|
570
|
+
|
|
571
|
+
{
|
|
572
|
+
"to": "user_id",
|
|
573
|
+
"text": "response text",
|
|
574
|
+
"conversationId": "conv-abc123"
|
|
575
|
+
}
|
|
576
|
+
```
|
|
577
|
+
|
|
578
|
+
响应:
|
|
579
|
+
```json
|
|
580
|
+
{
|
|
581
|
+
"ok": true,
|
|
582
|
+
"message": {
|
|
583
|
+
"id": "msg-2",
|
|
584
|
+
"conversationId": "conv-abc123",
|
|
585
|
+
"clientId": "client-xxx",
|
|
586
|
+
"from": "bot",
|
|
587
|
+
"to": "user_id",
|
|
588
|
+
"text": "response text",
|
|
589
|
+
"timestamp": 1770213378456
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
```
|
|
593
|
+
|
|
594
|
+
#### 9. 查看会话详情
|
|
595
|
+
```
|
|
596
|
+
GET /conversations/:id
|
|
597
|
+
Authorization: Bearer <client-token>
|
|
598
|
+
```
|
|
599
|
+
|
|
600
|
+
响应:
|
|
601
|
+
```json
|
|
602
|
+
{
|
|
603
|
+
"id": "conv-abc123",
|
|
604
|
+
"userId": "alice",
|
|
605
|
+
"clientId": "client-xxx",
|
|
606
|
+
"messages": [
|
|
607
|
+
{
|
|
608
|
+
"id": "msg-1",
|
|
609
|
+
"from": "alice",
|
|
610
|
+
"text": "Hello",
|
|
611
|
+
"timestamp": 1770213375101
|
|
612
|
+
},
|
|
613
|
+
{
|
|
614
|
+
"id": "msg-2",
|
|
615
|
+
"from": "bot",
|
|
616
|
+
"text": "Hi there!",
|
|
617
|
+
"timestamp": 1770213378456
|
|
618
|
+
}
|
|
619
|
+
],
|
|
620
|
+
"createdAt": 1770213375101,
|
|
621
|
+
"updatedAt": 1770213378456
|
|
622
|
+
}
|
|
623
|
+
```
|
|
624
|
+
|
|
625
|
+
---
|
|
626
|
+
|
|
627
|
+
## 故障排查
|
|
628
|
+
|
|
629
|
+
### 问题 1: Go 服务器无法启动
|
|
630
|
+
|
|
631
|
+
**症状:**
|
|
632
|
+
```
|
|
633
|
+
bind: address already in use
|
|
634
|
+
```
|
|
635
|
+
|
|
636
|
+
**解决方案:**
|
|
637
|
+
```bash
|
|
638
|
+
# 查找占用端口的进程
|
|
639
|
+
lsof -i :9876
|
|
640
|
+
|
|
641
|
+
# 杀死进程
|
|
642
|
+
kill -9 <PID>
|
|
643
|
+
|
|
644
|
+
# 或使用不同端口
|
|
645
|
+
go run main.go --port 9877
|
|
646
|
+
```
|
|
647
|
+
|
|
648
|
+
### 问题 2: 插件未加载
|
|
649
|
+
|
|
650
|
+
**症状:**
|
|
651
|
+
```
|
|
652
|
+
openclaw plugins list
|
|
653
|
+
# Simple Go 显示为 disabled
|
|
654
|
+
```
|
|
655
|
+
|
|
656
|
+
**解决方案:**
|
|
657
|
+
```bash
|
|
658
|
+
# 检查插件目录
|
|
659
|
+
ls -la ~/simplego/
|
|
660
|
+
|
|
661
|
+
# 检查 package.json
|
|
662
|
+
cat ~/simplego/package.json
|
|
663
|
+
|
|
664
|
+
# 重新加载插件
|
|
665
|
+
openclaw gateway restart
|
|
666
|
+
```
|
|
667
|
+
|
|
668
|
+
### 问题 3: 认证失败
|
|
669
|
+
|
|
670
|
+
**症状:**
|
|
671
|
+
```
|
|
672
|
+
[simplego] authentication failed - check apiToken
|
|
673
|
+
```
|
|
674
|
+
|
|
675
|
+
**解决方案:**
|
|
676
|
+
```bash
|
|
677
|
+
# 验证 token
|
|
678
|
+
curl -H "Authorization: Bearer <client-token>" \
|
|
679
|
+
http://localhost:9876/poll
|
|
680
|
+
|
|
681
|
+
# 重新注册客户端
|
|
682
|
+
curl -X POST http://localhost:9876/clients \
|
|
683
|
+
-H "Authorization: Bearer <admin-token>" \
|
|
684
|
+
-d '{"name": "openclaw-main"}'
|
|
685
|
+
|
|
686
|
+
# 更新配置
|
|
687
|
+
openclaw config set channels.simplego.apiToken "<new-token>"
|
|
688
|
+
openclaw gateway restart
|
|
689
|
+
```
|
|
690
|
+
|
|
691
|
+
### 问题 4: 消息未收到响应
|
|
692
|
+
|
|
693
|
+
**症状:**
|
|
694
|
+
消息发送成功,但 outbox 为空
|
|
695
|
+
|
|
696
|
+
**解决方案:**
|
|
697
|
+
```bash
|
|
698
|
+
# 1. 检查网关状态
|
|
699
|
+
openclaw gateway status
|
|
700
|
+
|
|
701
|
+
# 2. 查看日志
|
|
702
|
+
tail -f /tmp/openclaw/openclaw-$(date +%Y-%m-%d).log | grep simplego
|
|
703
|
+
|
|
704
|
+
# 3. 检查 API key
|
|
705
|
+
echo $ANTHROPIC_AUTH_TOKEN
|
|
706
|
+
|
|
707
|
+
# 4. 测试轮询
|
|
708
|
+
curl -H "Authorization: Bearer <client-token>" \
|
|
709
|
+
http://localhost:9876/poll
|
|
710
|
+
```
|
|
711
|
+
|
|
712
|
+
### 问题 5: API Key 错误
|
|
713
|
+
|
|
714
|
+
**症状:**
|
|
715
|
+
```
|
|
716
|
+
No API key found for provider "anthropic"
|
|
717
|
+
```
|
|
718
|
+
|
|
719
|
+
**解决方案:**
|
|
720
|
+
```bash
|
|
721
|
+
# 设置环境变量
|
|
722
|
+
export ANTHROPIC_AUTH_TOKEN="your-api-key"
|
|
723
|
+
|
|
724
|
+
# 或编辑配置文件
|
|
725
|
+
vim ~/.openclaw/agents/main/agent/models.json
|
|
726
|
+
|
|
727
|
+
# 重启网关
|
|
728
|
+
openclaw gateway restart
|
|
729
|
+
```
|
|
730
|
+
|
|
731
|
+
### 问题 6: 内存泄漏警告
|
|
732
|
+
|
|
733
|
+
**症状:**
|
|
734
|
+
```
|
|
735
|
+
MaxListenersExceededWarning: Possible EventTarget memory leak detected
|
|
736
|
+
```
|
|
737
|
+
|
|
738
|
+
**解决方案:**
|
|
739
|
+
```bash
|
|
740
|
+
# 重启网关
|
|
741
|
+
openclaw gateway stop
|
|
742
|
+
sleep 3
|
|
743
|
+
openclaw gateway start
|
|
744
|
+
```
|
|
745
|
+
|
|
746
|
+
---
|
|
747
|
+
|
|
748
|
+
## 开发指南
|
|
749
|
+
|
|
750
|
+
### 项目结构
|
|
751
|
+
|
|
752
|
+
```
|
|
753
|
+
simplego/
|
|
754
|
+
├── server/ # Go 后端服务器
|
|
755
|
+
│ ├── main.go # 主服务器代码
|
|
756
|
+
│ └── go.mod # Go 依赖
|
|
757
|
+
├── src/ # TypeScript 插件代码
|
|
758
|
+
│ ├── channel.ts # 插件配置
|
|
759
|
+
│ ├── monitor.ts # 消息轮询逻辑
|
|
760
|
+
│ ├── runtime.ts # 运行时管理
|
|
761
|
+
│ └── config-schema.ts # 配置模式
|
|
762
|
+
├── index.ts # 插件入口
|
|
763
|
+
├── package.json # Node.js 配置
|
|
764
|
+
├── openclaw.plugin.json # 插件元数据
|
|
765
|
+
├── README.md # 项目说明
|
|
766
|
+
├── DESIGN.md # 设计文档
|
|
767
|
+
└── USAGE.md # 本文档
|
|
768
|
+
```
|
|
769
|
+
|
|
770
|
+
### 修改轮询间隔
|
|
771
|
+
|
|
772
|
+
编辑配置:
|
|
773
|
+
```bash
|
|
774
|
+
openclaw config set channels.simplego.pollIntervalMs 2000 # 2秒
|
|
775
|
+
```
|
|
776
|
+
|
|
777
|
+
或修改 `src/channel.ts`:
|
|
778
|
+
```typescript
|
|
779
|
+
pollIntervalMs: section.pollIntervalMs ?? 2000, // 默认 2 秒
|
|
780
|
+
```
|
|
781
|
+
|
|
782
|
+
### 添加自定义字段
|
|
783
|
+
|
|
784
|
+
1. 修改 Go 服务器的 `Message` 结构:
|
|
785
|
+
```go
|
|
786
|
+
type Message struct {
|
|
787
|
+
ID string `json:"id"`
|
|
788
|
+
ConversationID string `json:"conversationId"`
|
|
789
|
+
ClientID string `json:"clientId"`
|
|
790
|
+
From string `json:"from"`
|
|
791
|
+
To string `json:"to"`
|
|
792
|
+
Text string `json:"text"`
|
|
793
|
+
Timestamp int64 `json:"timestamp"`
|
|
794
|
+
CustomField string `json:"customField"` // 新字段
|
|
795
|
+
}
|
|
796
|
+
```
|
|
797
|
+
|
|
798
|
+
2. 修改 `src/monitor.ts` 的类型定义:
|
|
799
|
+
```typescript
|
|
800
|
+
type InboundMessage = {
|
|
801
|
+
id: string;
|
|
802
|
+
conversationId: string;
|
|
803
|
+
from: string;
|
|
804
|
+
text: string;
|
|
805
|
+
timestamp: number;
|
|
806
|
+
customField?: string; // 新字段
|
|
807
|
+
};
|
|
808
|
+
```
|
|
809
|
+
|
|
810
|
+
3. 在 context 中传递:
|
|
811
|
+
```typescript
|
|
812
|
+
const ctx = runtime.channel.reply.finalizeInboundContext({
|
|
813
|
+
Body: msg.text,
|
|
814
|
+
// ... 其他字段
|
|
815
|
+
CustomField: msg.customField, // 传递新字段
|
|
816
|
+
});
|
|
817
|
+
```
|
|
818
|
+
|
|
819
|
+
### 调试技巧
|
|
820
|
+
|
|
821
|
+
1. **启用详细日志:**
|
|
822
|
+
```bash
|
|
823
|
+
# 查看实时日志
|
|
824
|
+
tail -f /tmp/openclaw/openclaw-$(date +%Y-%m-%d).log | grep simplego
|
|
825
|
+
|
|
826
|
+
# 查看 Go 服务器日志
|
|
827
|
+
tail -f /tmp/go-server.log
|
|
828
|
+
```
|
|
829
|
+
|
|
830
|
+
2. **测试单个组件:**
|
|
831
|
+
```bash
|
|
832
|
+
# 测试 Go 服务器
|
|
833
|
+
curl http://localhost:9876/health
|
|
834
|
+
|
|
835
|
+
# 测试插件轮询
|
|
836
|
+
curl -H "Authorization: Bearer <token>" http://localhost:9876/poll
|
|
837
|
+
|
|
838
|
+
# 测试消息发送
|
|
839
|
+
curl -X POST http://localhost:9876/inbound \
|
|
840
|
+
-H "Content-Type: application/json" \
|
|
841
|
+
-d '{"from": "test", "text": "debug message"}'
|
|
842
|
+
```
|
|
843
|
+
|
|
844
|
+
3. **使用 jq 格式化输出:**
|
|
845
|
+
```bash
|
|
846
|
+
curl http://localhost:9876/outbox | jq '.messages | map({from, to, text})'
|
|
847
|
+
```
|
|
848
|
+
|
|
849
|
+
### 性能优化
|
|
850
|
+
|
|
851
|
+
1. **调整轮询间隔:**
|
|
852
|
+
- 高频场景:500ms
|
|
853
|
+
- 正常场景:1000ms(默认)
|
|
854
|
+
- 低频场景:3000ms
|
|
855
|
+
|
|
856
|
+
2. **限制消息队列大小:**
|
|
857
|
+
修改 `server/main.go`:
|
|
858
|
+
```go
|
|
859
|
+
const maxQueueSize = 100
|
|
860
|
+
|
|
861
|
+
if len(s.inbound[clientID]) > maxQueueSize {
|
|
862
|
+
s.inbound[clientID] = s.inbound[clientID][1:]
|
|
863
|
+
}
|
|
864
|
+
```
|
|
865
|
+
|
|
866
|
+
3. **添加消息过期机制:**
|
|
867
|
+
```go
|
|
868
|
+
const messageExpiry = 5 * time.Minute
|
|
869
|
+
|
|
870
|
+
if time.Now().UnixMilli() - msg.Timestamp > messageExpiry.Milliseconds() {
|
|
871
|
+
continue // 跳过过期消息
|
|
872
|
+
}
|
|
873
|
+
```
|
|
874
|
+
|
|
875
|
+
---
|
|
876
|
+
|
|
877
|
+
## 常见问题 (FAQ)
|
|
878
|
+
|
|
879
|
+
### Q1: 支持多少并发用户?
|
|
880
|
+
A: 默认配置支持数百个并发用户。可以通过增加 Go 服务器实例和调整轮询间隔来扩展。
|
|
881
|
+
|
|
882
|
+
### Q2: 消息是否持久化?
|
|
883
|
+
A: 当前版本消息存储在内存中。生产环境建议添加数据库持久化。
|
|
884
|
+
|
|
885
|
+
### Q3: 如何实现消息加密?
|
|
886
|
+
A: 可以在发送前加密 `text` 字段,在接收后解密。建议使用 TLS/HTTPS 传输。
|
|
887
|
+
|
|
888
|
+
### Q4: 支持文件上传吗?
|
|
889
|
+
A: 当前版本仅支持文本。可以扩展 `Message` 结构添加 `mediaUrl` 字段。
|
|
890
|
+
|
|
891
|
+
### Q5: 如何实现消息撤回?
|
|
892
|
+
A: 需要添加新的 API 端点和消息状态管理。参考 `server/main.go` 添加 DELETE 端点。
|
|
893
|
+
|
|
894
|
+
---
|
|
895
|
+
|
|
896
|
+
## 许可证
|
|
897
|
+
|
|
898
|
+
本项目为演示用途,遵循 OpenClaw 项目许可证。
|
|
899
|
+
|
|
900
|
+
## 支持
|
|
901
|
+
|
|
902
|
+
- 文档:https://docs.openclaw.ai
|
|
903
|
+
- 问题反馈:https://github.com/anthropics/openclaw/issues
|
|
904
|
+
- 社区:https://discord.gg/openclaw
|
|
905
|
+
|
|
906
|
+
---
|
|
907
|
+
|
|
908
|
+
**最后更新:** 2026-02-04
|
|
909
|
+
**版本:** 0.0.1
|
|
910
|
+
**作者:** OpenClaw Team
|