@henryxiaoyang/wechat-access-unqclawed 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/websocket.md ADDED
@@ -0,0 +1,273 @@
1
+ agentwsserver WebSocket 接口文档
2
+ 目录
3
+ 1.概述
4
+ 2.连接
5
+ 3.数据协议 (AGP Envelope)
6
+ 4.下行消息 (服务端 → 客户端)
7
+ 5.上行消息 (客户端 → 服务端)
8
+ 6.通用数据结构
9
+ 7.时序示意
10
+
11
+ 概述
12
+ 为独立 APP 提供 WebSocket 双向通信能力。
13
+ WebSocket 服务 — 运行于 :8080 端口,处理客户端的 WebSocket 长连接
14
+ 数据协议 — 使用 AGP (Agent Gateway Protocol) 统一消息信封
15
+ 消息传输 — 所有消息均为 WebSocket Text 帧,内容为 JSON
16
+
17
+ 连接
18
+ 地址
19
+ ws://21.0.62.97:8080/?token={token}
20
+ Query 参数
21
+ 参数 类型 必填 说明
22
+ token string 否 鉴权 token(当前未校验,后续启用)
23
+ 连接行为
24
+ 握手成功后服务端注册连接,同一 guid 的旧连接会被踢下线
25
+ 空闲超时 5 分钟,超时无消息收发将断开
26
+ 连接断开后服务端自动清理路由注册
27
+ 错误场景
28
+ 场景 行为
29
+ 缺少 guid 或 user_id 握手拒绝,WebSocket 连接不会建立
30
+ URL 解析失败 握手拒绝
31
+
32
+ 数据协议 (AGP Envelope)
33
+ Envelope 结构
34
+ 所有 WebSocket 消息(上行和下行)均使用统一的 AGP 信封格式:
35
+ {
36
+ "msg_id": "string",
37
+ "guid": "string",
38
+ "user_id": "string",
39
+ "method": "string",
40
+ "payload": {}
41
+ }
42
+ 字段 类型 必填 说明
43
+ msg_id string 是 全局唯一消息 ID(UUID),用于幂等去重
44
+ guid string 是 设备 GUID
45
+ user_id string 是 用户账户 ID
46
+ method string 是 消息类型,见下方枚举
47
+ payload object 是 消息载荷(JSON 对象,根据 method 类型而异)
48
+ Method 枚举
49
+ method 方向 说明
50
+ session.prompt 服务端 → 客户端 下发用户指令
51
+ session.cancel 服务端 → 客户端 取消 Prompt Turn
52
+ session.update 客户端 → 服务端 流式中间更新
53
+ session.promptResponse 客户端 → 服务端 最终结果
54
+
55
+ 下行消息 (服务端 → 客户端)
56
+ session.prompt — 下发用户指令
57
+ {
58
+ "msg_id": "550e8400-e29b-41d4-a716-446655440000",
59
+ "guid": "device_001",
60
+ "user_id": "user_123",
61
+ "method": "session.prompt",
62
+ "payload": {
63
+ "session_id": "550e8400-e29b-41d4-a716-446655440000",
64
+ "prompt_id": "550e8400-e29b-41d4-a716-446655440001",
65
+ "agent_app": "openclaw",
66
+ "content": [
67
+ { "type": "text", "text": "帮我查一下今天的天气" }
68
+ ]
69
+ }
70
+ }
71
+ payload 字段:
72
+ 字段 类型 必填 说明
73
+ session_id string 是 所属 Session ID
74
+ prompt_id string 是 本次 Turn 唯一 ID
75
+ agent_app string 是 目标 AI 应用标识,客户端据此路由到本地 AI 应用
76
+ content ContentBlock[] 是 用户指令内容(数组)
77
+ session.cancel — 取消 Prompt Turn
78
+ {
79
+ "msg_id": "550e8400-e29b-41d4-a716-446655440001",
80
+ "guid": "device_001",
81
+ "user_id": "user_123",
82
+ "method": "session.cancel",
83
+ "payload": {
84
+ "session_id": "550e8400-e29b-41d4-a716-446655440000",
85
+ "prompt_id": "550e8400-e29b-41d4-a716-446655440001",
86
+ "agent_app": "openclaw"
87
+ }
88
+ }
89
+ payload 字段:
90
+ 字段 类型 必填 说明
91
+ session_id string 是 所属 Session ID
92
+ prompt_id string 是 要取消的 Turn ID
93
+ agent_app string 是 目标 AI 应用标识
94
+
95
+ 上行消息 (客户端 → 服务端)
96
+ session.update — 流式中间更新
97
+ 客户端在处理 session.prompt 期间,通过此消息上报中间进度。可多次发送。
98
+ update_type 枚举
99
+ update_type 说明 使用字段
100
+ message_chunk 增量文本/内容(Agent 消息片段) content
101
+ tool_call AI 正在调用工具 tool_call
102
+ tool_call_update 工具执行状态变更 tool_call
103
+ payload 字段
104
+ 字段 类型 必填 说明
105
+ session_id string 是 所属 Session ID
106
+ prompt_id string 是 所属 Turn ID
107
+ update_type string 是 更新类型,取值见上方枚举
108
+ content ContentBlock 条件 update_type=message_chunk 时使用,单个对象(非数组)
109
+ tool_call ToolCall 条件 update_type=tool_call 或 tool_call_update 时使用
110
+ 注意: content 字段为单个 ContentBlock 对象,不是数组。与 session.promptResponse 的 content 数组不同。
111
+ 示例 — message_chunk(增量文本)
112
+ {
113
+ "msg_id": "550e8400-e29b-41d4-a716-446655440002",
114
+ "guid": "device_001",
115
+ "user_id": "user_123",
116
+ "method": "session.update",
117
+ "payload": {
118
+ "session_id": "550e8400-e29b-41d4-a716-446655440000",
119
+ "prompt_id": "550e8400-e29b-41d4-a716-446655440001",
120
+ "update_type": "message_chunk",
121
+ "content": {
122
+ "type": "text",
123
+ "text": "正在思考中...第一步是..."
124
+ }
125
+ }
126
+ }
127
+ 示例 — tool_call(工具调用)
128
+ {
129
+ "msg_id": "550e8400-e29b-41d4-a716-446655440003",
130
+ "guid": "device_001",
131
+ "user_id": "user_123",
132
+ "method": "session.update",
133
+ "payload": {
134
+ "session_id": "550e8400-e29b-41d4-a716-446655440000",
135
+ "prompt_id": "550e8400-e29b-41d4-a716-446655440001",
136
+ "update_type": "tool_call",
137
+ "tool_call": {
138
+ "tool_call_id": "tc-001",
139
+ "title": "扫描临时文件",
140
+ "kind": "execute",
141
+ "status": "pending"
142
+ }
143
+ }
144
+ }
145
+ 示例 — tool_call_update(工具状态更新)
146
+ {
147
+ "msg_id": "550e8400-e29b-41d4-a716-446655440004",
148
+ "guid": "device_001",
149
+ "user_id": "user_123",
150
+ "method": "session.update",
151
+ "payload": {
152
+ "session_id": "550e8400-e29b-41d4-a716-446655440000",
153
+ "prompt_id": "550e8400-e29b-41d4-a716-446655440001",
154
+ "update_type": "tool_call_update",
155
+ "tool_call": {
156
+ "tool_call_id": "tc-001",
157
+ "status": "completed",
158
+ "content": [{ "type": "text", "text": "发现临时文件 2.3GB" }]
159
+ }
160
+ }
161
+ }
162
+
163
+ session.promptResponse — 最终结果
164
+ 客户端完成 session.prompt 处理后,上报最终结果。每个 prompt_id 只接受一次最终响应,重复的 msg_id 会被去重。
165
+ {
166
+ "msg_id": "550e8400-e29b-41d4-a716-446655440005",
167
+ "guid": "device_001",
168
+ "user_id": "user_123",
169
+ "method": "session.promptResponse",
170
+ "payload": {
171
+ "session_id": "550e8400-e29b-41d4-a716-446655440000",
172
+ "prompt_id": "550e8400-e29b-41d4-a716-446655440001",
173
+ "stop_reason": "end_turn",
174
+ "content": [
175
+ { "type": "text", "text": "今天北京晴,气温 15°C" }
176
+ ]
177
+ }
178
+ }
179
+ payload 字段:
180
+ 字段 类型 必填 说明
181
+ session_id string 是 所属 Session ID
182
+ prompt_id string 是 所属 Turn ID
183
+ stop_reason string 是 停止原因
184
+ content ContentBlock[] 否 最终结果内容(数组)
185
+ error string 否 错误描述(stop_reason 为 error / refusal 时附带)
186
+ stop_reason 枚举:
187
+ 值 说明
188
+ end_turn 正常完成
189
+ cancelled 被取消
190
+ refusal AI 应用拒绝执行
191
+ error 技术错误
192
+ 错误响应示例
193
+ {
194
+ "msg_id": "550e8400-e29b-41d4-a716-446655440006",
195
+ "guid": "device_001",
196
+ "user_id": "user_123",
197
+ "method": "session.promptResponse",
198
+ "payload": {
199
+ "session_id": "550e8400-e29b-41d4-a716-446655440000",
200
+ "prompt_id": "550e8400-e29b-41d4-a716-446655440001",
201
+ "stop_reason": "error",
202
+ "error": "AI 应用执行超时"
203
+ }
204
+ }
205
+
206
+ 通用数据结构
207
+ ContentBlock — 内容块
208
+ {
209
+ "type": "text",
210
+ "text": "文本内容"
211
+ }
212
+ 字段 类型 必填 说明
213
+ type string 是 内容类型,当前仅支持 "text"
214
+ text string 是 type=text 时必填
215
+ ToolCall — 工具调用
216
+ {
217
+ "tool_call_id": "tc-001",
218
+ "title": "扫描临时文件",
219
+ "kind": "execute",
220
+ "status": "in_progress",
221
+ "content": [{ "type": "text", "text": "发现临时文件 2.3GB" }],
222
+ "locations": [{ "path": "/tmp" }]
223
+ }
224
+ 字段 类型 必填 说明
225
+ tool_call_id string 是 工具调用唯一 ID
226
+ title string 否 工具调用标题(展示用)
227
+ kind string 否 工具类型
228
+ status string 是 工具调用状态
229
+ content ContentBlock[] 否 工具调用结果内容
230
+ locations Location[] 否 工具操作路径
231
+ kind 枚举:
232
+ 值 说明
233
+ read 读取
234
+ edit 编辑
235
+ delete 删除
236
+ execute 执行
237
+ search 搜索
238
+ fetch 获取
239
+ think 思考
240
+ other 其他
241
+ status 枚举:
242
+ 值 说明
243
+ pending 等待中
244
+ in_progress 执行中
245
+ completed 已完成
246
+ failed 失败
247
+ Location — 路径
248
+ { "path": "/tmp" }
249
+ 字段 类型 说明
250
+ path string 操作路径
251
+
252
+ 时序示意
253
+ 正常流程
254
+ 客户端 (APP) 服务端
255
+ | |
256
+ |--- WS 握手 (guid/user_id) ----->|
257
+ |<-- 101 Switching Protocols -----| 连接建立
258
+ | |
259
+ |<-- session.prompt (WS Text) ----| 下发指令
260
+ | |
261
+ |--- session.update (WS Text) --->| 流式上报(可多次)
262
+ |--- session.update (WS Text) --->|
263
+ | |
264
+ |--- promptResponse (WS Text) --->| 最终结果
265
+ | |
266
+ |--- 断开 / 超时 ---------------->| 连接清理
267
+ 取消流程
268
+ 客户端 (APP) 服务端
269
+ | |
270
+ | (正在处理 session.prompt) |
271
+ |<-- session.cancel (WS Text) ----| 服务端取消
272
+ | |
273
+ |--- promptResponse (WS Text) --->| stop_reason: "cancelled"