adp-openclaw 0.0.2 → 0.0.4

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.
@@ -1,506 +0,0 @@
1
- # ADP-OpenCraw WebSocket 协议文档
2
-
3
- ## 📋 概述
4
-
5
- 本文档描述了 adp-opencraw 项目中插件端(TypeScript)与 Go Server 之间的 WebSocket 通信协议和 Key 验证机制。
6
-
7
- ## 🏗️ 架构概览
8
-
9
- ```
10
- ┌─────────────────┐ ┌──────────────────┐
11
- │ │ WebSocket (双向通信) │ │
12
- │ 插件端 │ ◀════════════════════▶ │ Go Server │
13
- │ (TypeScript) │ │ │
14
- │ │ │ │
15
- └─────────────────┘ └──────────────────┘
16
- │ │
17
- │ │
18
- ▼ ▼
19
- ┌───────────┐ ┌─────────────┐
20
- │ OpenClaw │ │ 用户消息 │
21
- │ Runtime │ │ HTTP接口 │
22
- └───────────┘ └─────────────┘
23
- ```
24
-
25
- ---
26
-
27
- ## 🔐 Key 验证流程
28
-
29
- ### 1. Token 类型
30
-
31
- | Token 类型 | 用途 | 生成方式 | 长度 |
32
- |-----------|------|---------|------|
33
- | **Admin Token** | 管理员操作(注册客户端) | 启动时生成或 `--admin-token` 指定 | 64 字符 hex |
34
- | **Client Token** | 客户端 WebSocket 认证 | 注册客户端时自动生成 | 64 字符 hex |
35
-
36
- ### 2. Token 生成算法
37
-
38
- ```go
39
- // 生成 32 字节随机数,转换为 64 字符的十六进制字符串
40
- func generateToken() string {
41
- bytes := make([]byte, 32)
42
- rand.Read(bytes)
43
- return hex.EncodeToString(bytes)
44
- }
45
- ```
46
-
47
- ### 3. WebSocket 认证流程
48
-
49
- ```
50
- ┌──────────┐ ┌──────────┐
51
- │ Client │ │ Server │
52
- └────┬─────┘ └────┬─────┘
53
- │ │
54
- │ 1. 建立 WebSocket 连接 │
55
- │ ─────────────────────────────────────▶ │
56
- │ │
57
- │ 2. 发送 Auth 消息 (30秒超时) │
58
- │ ─────────────────────────────────────▶ │
59
- │ { │
60
- │ type: "auth", │
61
- │ payload: { │
62
- │ token: "client_api_token", │
63
- │ nonce: "random_nonce", │ 3. 验证 Token
64
- │ signature: "hmac_sha256" │ 验证签名
65
- │ } │
66
- │ } │
67
- │ │
68
- │ 4. 返回认证结果 │
69
- │ ◀───────────────────────────────────── │
70
- │ { │
71
- │ type: "auth_result", │
72
- │ payload: { │
73
- │ success: true, │
74
- │ clientId: "client-xxx", │
75
- │ message: "Authentication ok" │
76
- │ } │
77
- │ } │
78
- │ │
79
- │ 5. 开始双向消息通信 │
80
- │ ◀════════════════════════════════════▶ │
81
- │ │
82
- ```
83
-
84
- ### 4. 签名验证(可选增强安全)
85
-
86
- ```typescript
87
- // 客户端生成签名
88
- function generateSignature(token: string, nonce: string): string {
89
- return crypto.createHash("sha256")
90
- .update(`${token}:${nonce}`)
91
- .digest("hex");
92
- }
93
-
94
- // 示例
95
- const token = "92527c1a...";
96
- const nonce = "abc123def456";
97
- const signature = sha256(token + ":" + nonce);
98
- ```
99
-
100
- ```go
101
- // 服务端验证签名
102
- func verifySignature(token, nonce, signature string) bool {
103
- h := sha256.New()
104
- h.Write([]byte(token + ":" + nonce))
105
- expected := hex.EncodeToString(h.Sum(nil))
106
- return expected == signature
107
- }
108
- ```
109
-
110
- ---
111
-
112
- ## 📡 WebSocket 消息协议
113
-
114
- ### 基础消息格式
115
-
116
- 所有 WebSocket 消息都遵循统一的 JSON 格式:
117
-
118
- ```typescript
119
- interface WSMessage {
120
- type: string; // 消息类型
121
- requestId?: string; // 请求ID(用于关联请求/响应)
122
- payload?: any; // 消息载荷
123
- timestamp: number; // Unix毫秒时间戳
124
- }
125
- ```
126
-
127
- ### 消息类型一览
128
-
129
- | 类型 | 方向 | 说明 |
130
- |------|------|------|
131
- | `auth` | Client → Server | 认证请求 |
132
- | `auth_result` | Server → Client | 认证结果 |
133
- | `ping` | Client → Server | 心跳请求 |
134
- | `pong` | Server → Client | 心跳响应 |
135
- | `inbound` | Server → Client | 用户消息推送 |
136
- | `outbound` | Client → Server | Bot 回复消息 |
137
- | `ack` | Server → Client | 消息确认 |
138
- | `error` | Server → Client | 错误消息 |
139
- | `conv_history` | Client → Server | 请求会话历史 |
140
- | `conv_response` | Server → Client | 会话历史响应 |
141
-
142
- ---
143
-
144
- ### 详细消息格式
145
-
146
- #### 1. 认证请求 (auth)
147
-
148
- **方向**: Client → Server
149
-
150
- ```json
151
- {
152
- "type": "auth",
153
- "requestId": "req-1706951234567-abc123",
154
- "payload": {
155
- "token": "92527c1a861bb50bd1acc47180ef317369e97529c7a5491b2bc67839992cfbb8",
156
- "nonce": "a1b2c3d4e5f6",
157
- "signature": "sha256_hash_of_token_colon_nonce"
158
- },
159
- "timestamp": 1706951234567
160
- }
161
- ```
162
-
163
- | 字段 | 类型 | 必填 | 说明 |
164
- |------|------|------|------|
165
- | `token` | string | ✅ | 客户端 API Token |
166
- | `nonce` | string | ❌ | 随机数(用于签名) |
167
- | `signature` | string | ❌ | HMAC-SHA256 签名 |
168
-
169
- #### 2. 认证结果 (auth_result)
170
-
171
- **方向**: Server → Client
172
-
173
- ```json
174
- {
175
- "type": "auth_result",
176
- "requestId": "req-1706951234567-abc123",
177
- "payload": {
178
- "success": true,
179
- "clientId": "client-abc123def456",
180
- "message": "Authentication successful"
181
- },
182
- "timestamp": 1706951234890
183
- }
184
- ```
185
-
186
- | 字段 | 类型 | 说明 |
187
- |------|------|------|
188
- | `success` | boolean | 认证是否成功 |
189
- | `clientId` | string | 分配的客户端ID |
190
- | `message` | string | 状态消息 |
191
-
192
- #### 3. 心跳 (ping/pong)
193
-
194
- **ping** (Client → Server):
195
- ```json
196
- {
197
- "type": "ping",
198
- "requestId": "req-1706951234567-xyz",
199
- "timestamp": 1706951234567
200
- }
201
- ```
202
-
203
- **pong** (Server → Client):
204
- ```json
205
- {
206
- "type": "pong",
207
- "requestId": "req-1706951234567-xyz",
208
- "timestamp": 1706951234890
209
- }
210
- ```
211
-
212
- > 💡 建议心跳间隔:25-30 秒
213
-
214
- #### 4. 用户消息推送 (inbound)
215
-
216
- **方向**: Server → Client
217
-
218
- 当有用户发送消息时,服务器实时推送给对应客户端:
219
-
220
- ```json
221
- {
222
- "type": "inbound",
223
- "payload": {
224
- "id": "msg-1",
225
- "conversationId": "conv-abc123def456",
226
- "clientId": "client-xyz789",
227
- "from": "user123",
228
- "to": "bot",
229
- "text": "Hello, how are you?",
230
- "timestamp": 1706951234567
231
- },
232
- "timestamp": 1706951234567
233
- }
234
- ```
235
-
236
- #### 5. Bot 回复消息 (outbound)
237
-
238
- **方向**: Client → Server
239
-
240
- ```json
241
- {
242
- "type": "outbound",
243
- "requestId": "req-1706951235000-reply",
244
- "payload": {
245
- "to": "user123",
246
- "text": "I'm doing great! How can I help you today?",
247
- "conversationId": "conv-abc123def456"
248
- },
249
- "timestamp": 1706951235000
250
- }
251
- ```
252
-
253
- #### 6. 消息确认 (ack)
254
-
255
- **方向**: Server → Client
256
-
257
- ```json
258
- {
259
- "type": "ack",
260
- "requestId": "req-1706951235000-reply",
261
- "payload": {
262
- "ok": true,
263
- "message": {
264
- "id": "msg-2",
265
- "conversationId": "conv-abc123def456",
266
- "clientId": "client-xyz789",
267
- "from": "bot",
268
- "to": "user123",
269
- "text": "I'm doing great! How can I help you today?",
270
- "timestamp": 1706951235123
271
- }
272
- },
273
- "timestamp": 1706951235123
274
- }
275
- ```
276
-
277
- #### 7. 错误消息 (error)
278
-
279
- **方向**: Server → Client
280
-
281
- ```json
282
- {
283
- "type": "error",
284
- "requestId": "req-xxx",
285
- "payload": {
286
- "error": "invalid_payload",
287
- "message": "Missing required field: conversationId"
288
- },
289
- "timestamp": 1706951235000
290
- }
291
- ```
292
-
293
- 常见错误码:
294
-
295
- | 错误码 | 说明 |
296
- |--------|------|
297
- | `auth_timeout` | 认证超时(30秒内未发送auth) |
298
- | `invalid_auth` | 首条消息不是auth类型 |
299
- | `invalid_token` | Token 无效 |
300
- | `invalid_signature` | 签名验证失败 |
301
- | `invalid_payload` | 消息载荷格式错误 |
302
- | `not_found` | 资源不存在 |
303
-
304
- #### 8. 会话历史请求/响应
305
-
306
- **请求** (Client → Server):
307
- ```json
308
- {
309
- "type": "conv_history",
310
- "requestId": "req-1706951236000-hist",
311
- "payload": {
312
- "conversationId": "conv-abc123def456"
313
- },
314
- "timestamp": 1706951236000
315
- }
316
- ```
317
-
318
- **响应** (Server → Client):
319
- ```json
320
- {
321
- "type": "conv_response",
322
- "requestId": "req-1706951236000-hist",
323
- "payload": {
324
- "id": "conv-abc123def456",
325
- "userId": "user123",
326
- "clientId": "client-xyz789",
327
- "messages": [
328
- {"id": "msg-1", "from": "user123", "text": "Hello", ...},
329
- {"id": "msg-2", "from": "bot", "text": "Hi there!", ...}
330
- ],
331
- "createdAt": 1706951234000,
332
- "updatedAt": 1706951235123
333
- },
334
- "timestamp": 1706951236123
335
- }
336
- ```
337
-
338
- ---
339
-
340
- ## 📊 数据结构
341
-
342
- ### Message(消息)
343
-
344
- ```typescript
345
- interface Message {
346
- id: string; // 消息ID,格式: "msg-{序号}"
347
- conversationId: string; // 会话ID,格式: "conv-{随机hex}"
348
- clientId: string; // 客户端ID,格式: "client-{随机hex}"
349
- from: string; // 发送者
350
- to: string; // 接收者
351
- text: string; // 消息内容
352
- timestamp: number; // Unix毫秒时间戳
353
- }
354
- ```
355
-
356
- ### Conversation(会话)
357
-
358
- ```typescript
359
- interface Conversation {
360
- id: string; // 会话ID
361
- userId: string; // 用户ID
362
- clientId: string; // 分配的客户端ID
363
- messages: Message[];
364
- createdAt: number;
365
- updatedAt: number;
366
- }
367
- ```
368
-
369
- ---
370
-
371
- ## 🌐 HTTP API(保留)
372
-
373
- 以下 HTTP 接口仍然可用:
374
-
375
- ### 公共端点
376
-
377
- | 方法 | 路径 | 说明 |
378
- |------|------|------|
379
- | GET | `/health` | 健康检查 |
380
- | POST | `/inbound` | 模拟用户发送消息(会通过 WS 推送) |
381
- | GET | `/outbox` | 查看已发送的消息 |
382
-
383
- ### 管理端点(需 Admin Token)
384
-
385
- | 方法 | 路径 | 说明 |
386
- |------|------|------|
387
- | POST | `/clients` | 注册新客户端 |
388
- | GET | `/clients` | 列出所有客户端 |
389
- | GET | `/conversations` | 列出所有会话 |
390
-
391
- ---
392
-
393
- ## 🔄 完整通信流程
394
-
395
- ```
396
- ┌─────────────┐ POST /inbound ┌──────────────┐ WebSocket ┌─────────────┐
397
- │ 用户端 │ ──────────────────────▶ │ Go Server │ ═══════════════════▶ │ 插件端 │
398
- │ (浏览器) │ │ │ │ (OpenClaw) │
399
- └─────────────┘ │ │ │ │
400
- │ 实时推送 │ │ │
401
- │ inbound │ │ 处理消息 │
402
- │ │ ◀═══════════════════ │ │
403
- │ │ outbound │ │
404
- │ │ │ │
405
- │ 返回 ack │ ═══════════════════▶ │ │
406
- └──────────────┘ └─────────────┘
407
- ```
408
-
409
- ---
410
-
411
- ## 🛠️ 配置示例
412
-
413
- ### 插件端配置
414
-
415
- ```typescript
416
- {
417
- serverUrl: "http://localhost:9876", // 会自动转换为 ws://localhost:9876/ws
418
- apiToken: "your_client_token_here",
419
- pollIntervalMs: 3000 // 作为重连延迟使用
420
- }
421
- ```
422
-
423
- ### 环境变量
424
-
425
- ```bash
426
- SIMPLEGO_SERVER_URL=http://localhost:9876
427
- SIMPLEGO_API_TOKEN=your_client_token
428
- ```
429
-
430
- ---
431
-
432
- ## 🚀 启动服务
433
-
434
- ### Go Server
435
-
436
- ```bash
437
- cd server
438
-
439
- # 安装依赖
440
- go mod tidy
441
-
442
- # 启动服务
443
- go run main.go -port 9876 -admin-token "your_admin_token"
444
-
445
- # 如果不指定 admin-token,会自动生成并打印到控制台
446
- ```
447
-
448
- ### 注册客户端
449
-
450
- ```bash
451
- # 使用 admin token 注册新客户端
452
- curl -X POST http://localhost:9876/clients \
453
- -H "Authorization: Bearer YOUR_ADMIN_TOKEN" \
454
- -H "Content-Type: application/json" \
455
- -d '{"name": "OpenClaw-Client-1"}'
456
-
457
- # 返回示例:
458
- # {
459
- # "clientId": "client-abc123",
460
- # "token": "92527c1a861bb50bd1acc47180ef317369e97529c7a5491b2bc67839992cfbb8",
461
- # "name": "OpenClaw-Client-1"
462
- # }
463
- ```
464
-
465
- ---
466
-
467
- ## 📝 最佳实践
468
-
469
- ### 1. 连接管理
470
-
471
- - 连接断开后自动重连(建议延迟 3-5 秒)
472
- - 使用心跳保持连接活跃(25-30 秒间隔)
473
- - 处理认证超时(30 秒内必须完成认证)
474
-
475
- ### 2. 安全建议
476
-
477
- - 生产环境使用 WSS(WebSocket Secure)
478
- - 启用签名验证增强安全性
479
- - 定期轮换 Token
480
-
481
- ### 3. 错误处理
482
-
483
- - 监听 `error` 类型消息
484
- - 使用 `requestId` 关联请求和响应
485
- - 记录所有错误日志用于排查
486
-
487
- ---
488
-
489
- ## 📋 检查清单
490
-
491
- - [ ] Go Server 启动并打印 admin token
492
- - [ ] 使用 admin token 注册客户端获取 client token
493
- - [ ] 插件配置正确的 serverUrl 和 apiToken
494
- - [ ] WebSocket 连接成功建立
495
- - [ ] 认证流程通过
496
- - [ ] 心跳保持连接活跃
497
- - [ ] 消息收发正常
498
-
499
- ---
500
-
501
- ## 📚 版本历史
502
-
503
- | 版本 | 日期 | 变更 |
504
- |------|------|------|
505
- | 0.0.2 | 2026-02-05 | 升级为 WebSocket 协议 |
506
- | 0.0.1 | - | 初始 HTTP 轮询版本 |
@@ -1,16 +0,0 @@
1
- {
2
- "permissions": {
3
- "allow": [
4
- "Bash(go run:*)",
5
- "Bash(apt-get:*)",
6
- "Bash(apt-get install:*)",
7
- "Bash(yum install:*)",
8
- "Bash(go version:*)",
9
- "Bash(curl -OL https://go.dev/dl/go1.22.0.linux-amd64.tar.gz)",
10
- "Bash(sudo rm -rf /usr/local/go)",
11
- "Bash(sudo tar -C /usr/local -xzf go1.22.0.linux-amd64.tar.gz)",
12
- "Bash(export PATH=$PATH:/usr/local/go/bin)",
13
- "Bash(~/.bashrc)"
14
- ]
15
- }
16
- }
package/server/go.mod DELETED
@@ -1,5 +0,0 @@
1
- module simplego
2
-
3
- go 1.21
4
-
5
- require github.com/gorilla/websocket v1.5.1