opc-agent 4.1.0 → 4.1.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.
Files changed (245) hide show
  1. package/.github/ISSUE_TEMPLATE/bug_report.md +20 -20
  2. package/.github/ISSUE_TEMPLATE/feature_request.md +14 -14
  3. package/.github/PULL_REQUEST_TEMPLATE.md +13 -13
  4. package/CHANGELOG.md +48 -48
  5. package/CONTRIBUTING.md +36 -36
  6. package/README.zh-CN.md +497 -497
  7. package/dist/channels/wechat.js +6 -6
  8. package/dist/deploy/index.js +56 -56
  9. package/dist/studio/server.js +30 -1
  10. package/dist/studio-ui/index.html +230 -10
  11. package/dist/ui/components.js +105 -105
  12. package/examples/README.md +22 -22
  13. package/examples/basic-agent.ts +90 -90
  14. package/examples/brain-integration.ts +71 -71
  15. package/examples/multi-channel.ts +74 -74
  16. package/fix-sidebar.mjs +188 -188
  17. package/install.ps1 +154 -154
  18. package/install.sh +164 -164
  19. package/package.json +1 -1
  20. package/scripts/install.ps1 +31 -31
  21. package/scripts/install.sh +40 -40
  22. package/serve-studio.js +13 -13
  23. package/serve-test.js +25 -25
  24. package/src/channels/dingtalk.ts +46 -46
  25. package/src/channels/email.ts +351 -351
  26. package/src/channels/feishu.ts +349 -349
  27. package/src/channels/googlechat.ts +42 -42
  28. package/src/channels/imessage.ts +31 -31
  29. package/src/channels/irc.ts +82 -82
  30. package/src/channels/line.ts +32 -32
  31. package/src/channels/matrix.ts +33 -33
  32. package/src/channels/mattermost.ts +57 -57
  33. package/src/channels/msteams.ts +32 -32
  34. package/src/channels/nostr.ts +32 -32
  35. package/src/channels/qq.ts +33 -33
  36. package/src/channels/signal.ts +32 -32
  37. package/src/channels/sms.ts +33 -33
  38. package/src/channels/telegram.ts +616 -616
  39. package/src/channels/twitch.ts +65 -65
  40. package/src/channels/voice-call.ts +100 -100
  41. package/src/channels/websocket.ts +399 -399
  42. package/src/channels/wechat.ts +329 -329
  43. package/src/channels/whatsapp.ts +32 -32
  44. package/src/cli/chat.ts +99 -99
  45. package/src/cli/setup.ts +314 -314
  46. package/src/core/agent.ts +476 -476
  47. package/src/core/api-server.ts +277 -277
  48. package/src/core/audio.ts +98 -98
  49. package/src/core/collaboration.ts +275 -275
  50. package/src/core/context-discovery.ts +85 -85
  51. package/src/core/context-refs.ts +140 -140
  52. package/src/core/gateway.ts +106 -106
  53. package/src/core/heartbeat.ts +51 -51
  54. package/src/core/hooks.ts +105 -105
  55. package/src/core/ide-bridge.ts +133 -133
  56. package/src/core/node-network.ts +86 -86
  57. package/src/core/profiles.ts +122 -122
  58. package/src/core/scheduler.ts +187 -187
  59. package/src/core/session-manager.ts +137 -137
  60. package/src/core/subagent.ts +98 -98
  61. package/src/core/vision.ts +180 -180
  62. package/src/core/workflow-graph.ts +365 -365
  63. package/src/daemon.ts +96 -96
  64. package/src/deploy/index.ts +255 -255
  65. package/src/doctor.ts +156 -156
  66. package/src/eval/index.ts +211 -211
  67. package/src/eval/suites/basic.json +16 -16
  68. package/src/eval/suites/memory.json +12 -12
  69. package/src/eval/suites/safety.json +14 -14
  70. package/src/hub/brain-seed.ts +54 -54
  71. package/src/hub/client.ts +60 -60
  72. package/src/mcp/servers/calculator-mcp.ts +65 -65
  73. package/src/mcp/servers/crypto-mcp.ts +73 -73
  74. package/src/mcp/servers/database-mcp.ts +72 -72
  75. package/src/mcp/servers/datetime-mcp.ts +69 -69
  76. package/src/mcp/servers/filesystem.ts +66 -66
  77. package/src/mcp/servers/github-mcp.ts +58 -58
  78. package/src/mcp/servers/index.ts +63 -63
  79. package/src/mcp/servers/json-mcp.ts +102 -102
  80. package/src/mcp/servers/memory-mcp.ts +56 -56
  81. package/src/mcp/servers/regex-mcp.ts +53 -53
  82. package/src/mcp/servers/web-mcp.ts +49 -49
  83. package/src/memory/context-compressor.ts +189 -189
  84. package/src/memory/seed-loader.ts +212 -212
  85. package/src/memory/user-profiler.ts +215 -215
  86. package/src/plugins/content-filter.ts +23 -23
  87. package/src/plugins/logger.ts +18 -18
  88. package/src/plugins/rate-limiter.ts +38 -38
  89. package/src/protocols/a2a/client.ts +132 -132
  90. package/src/protocols/a2a/index.ts +8 -8
  91. package/src/protocols/a2a/server.ts +333 -333
  92. package/src/protocols/a2a/types.ts +88 -88
  93. package/src/protocols/a2a/utils.ts +50 -50
  94. package/src/protocols/agui/client.ts +83 -83
  95. package/src/protocols/agui/index.ts +4 -4
  96. package/src/protocols/agui/server.ts +218 -218
  97. package/src/protocols/agui/types.ts +153 -153
  98. package/src/protocols/index.ts +2 -2
  99. package/src/protocols/mcp/agent-tools.ts +134 -134
  100. package/src/protocols/mcp/index.ts +8 -8
  101. package/src/protocols/mcp/server.ts +262 -262
  102. package/src/protocols/mcp/types.ts +69 -69
  103. package/src/providers/index.ts +632 -632
  104. package/src/publish/index.ts +376 -376
  105. package/src/scheduler/cron-engine.ts +191 -191
  106. package/src/scheduler/index.ts +2 -2
  107. package/src/schema/oad.ts +217 -217
  108. package/src/security/approval.ts +131 -131
  109. package/src/security/approvals.ts +143 -143
  110. package/src/security/elevated.ts +105 -105
  111. package/src/security/guardrails.ts +248 -248
  112. package/src/security/index.ts +9 -9
  113. package/src/security/keys.ts +87 -87
  114. package/src/security/secrets.ts +129 -129
  115. package/src/skills/builtin/index.ts +408 -408
  116. package/src/skills/marketplace.ts +113 -113
  117. package/src/skills/types.ts +42 -42
  118. package/src/studio/server.ts +31 -1
  119. package/src/studio/templates-data.ts +178 -178
  120. package/src/studio-ui/index.html +230 -10
  121. package/src/telemetry/index.ts +324 -324
  122. package/src/tools/builtin/browser.ts +299 -299
  123. package/src/tools/builtin/datetime.ts +41 -41
  124. package/src/tools/builtin/file.ts +107 -107
  125. package/src/tools/builtin/home-assistant.ts +116 -116
  126. package/src/tools/builtin/rl-tools.ts +243 -243
  127. package/src/tools/builtin/shell.ts +43 -43
  128. package/src/tools/builtin/vision.ts +64 -64
  129. package/src/tools/builtin/web-search.ts +126 -126
  130. package/src/tools/builtin/web.ts +35 -35
  131. package/src/tools/document-processor.ts +213 -213
  132. package/src/tools/image-generator.ts +150 -150
  133. package/src/tools/integrations/calendar.ts +73 -73
  134. package/src/tools/integrations/code-exec.ts +39 -39
  135. package/src/tools/integrations/csv-analyzer.ts +92 -92
  136. package/src/tools/integrations/database.ts +44 -44
  137. package/src/tools/integrations/email-send.ts +76 -76
  138. package/src/tools/integrations/git-tool.ts +42 -42
  139. package/src/tools/integrations/github-tool.ts +76 -76
  140. package/src/tools/integrations/image-gen.ts +56 -56
  141. package/src/tools/integrations/index.ts +92 -92
  142. package/src/tools/integrations/jira.ts +83 -83
  143. package/src/tools/integrations/notion.ts +71 -71
  144. package/src/tools/integrations/npm-tool.ts +48 -48
  145. package/src/tools/integrations/pdf-reader.ts +58 -58
  146. package/src/tools/integrations/slack.ts +65 -65
  147. package/src/tools/integrations/summarizer.ts +49 -49
  148. package/src/tools/integrations/translator.ts +48 -48
  149. package/src/tools/integrations/trello.ts +60 -60
  150. package/src/tools/integrations/vector-search.ts +42 -42
  151. package/src/tools/integrations/web-scraper.ts +47 -47
  152. package/src/tools/integrations/web-search.ts +58 -58
  153. package/src/tools/integrations/webhook.ts +38 -38
  154. package/src/tools/mcp-client.ts +131 -131
  155. package/src/tools/web-scraper.ts +179 -179
  156. package/src/tools/web-search.ts +180 -180
  157. package/src/ui/components.ts +127 -127
  158. package/srv-out.txt +1 -1
  159. package/templates/ecommerce-assistant/README.md +45 -45
  160. package/templates/ecommerce-assistant/oad.yaml +47 -47
  161. package/templates/tech-support/README.md +43 -43
  162. package/templates/tech-support/oad.yaml +45 -45
  163. package/test-agent/Dockerfile +9 -9
  164. package/test-agent/README.md +50 -50
  165. package/test-agent/agent.yaml +23 -23
  166. package/test-agent/docker-compose.yml +11 -11
  167. package/test-agent/oad.yaml +31 -31
  168. package/test-agent/package-lock.json +1492 -1492
  169. package/test-agent/package.json +17 -17
  170. package/test-agent/src/index.ts +24 -24
  171. package/test-agent/src/skills/echo.ts +15 -15
  172. package/test-agent/tsconfig.json +24 -24
  173. package/test-full.js +43 -43
  174. package/test-sidebar.js +22 -22
  175. package/test-studio3.js +75 -75
  176. package/test-studio4.js +41 -41
  177. package/tests/a2a-protocol.test.ts +285 -285
  178. package/tests/agui-protocol.test.ts +246 -246
  179. package/tests/api-server.test.ts +148 -148
  180. package/tests/approvals.test.ts +89 -89
  181. package/tests/audio.test.ts +40 -40
  182. package/tests/brain-seed-extended.test.ts +490 -490
  183. package/tests/brain-seed.test.ts +239 -239
  184. package/tests/browser.test.ts +179 -179
  185. package/tests/channels/discord.test.ts +79 -79
  186. package/tests/channels/email.test.ts +148 -148
  187. package/tests/channels/feishu.test.ts +123 -123
  188. package/tests/channels/telegram.test.ts +129 -129
  189. package/tests/channels/websocket.test.ts +53 -53
  190. package/tests/channels/wechat.test.ts +170 -170
  191. package/tests/channels-extra.test.ts +45 -45
  192. package/tests/chat-cli.test.ts +160 -160
  193. package/tests/cli.test.ts +46 -46
  194. package/tests/context-compressor.test.ts +172 -172
  195. package/tests/context-refs.test.ts +121 -121
  196. package/tests/cron-engine.test.ts +101 -101
  197. package/tests/daemon.test.ts +135 -135
  198. package/tests/deepbrain-wire.test.ts +234 -234
  199. package/tests/deploy-and-dag.test.ts +196 -196
  200. package/tests/doctor.test.ts +38 -38
  201. package/tests/document-processor.test.ts +69 -69
  202. package/tests/e2e-nocode.test.ts +442 -442
  203. package/tests/elevated.test.ts +69 -69
  204. package/tests/eval.test.ts +173 -173
  205. package/tests/gateway.test.ts +63 -63
  206. package/tests/guardrails.test.ts +177 -177
  207. package/tests/home-assistant.test.ts +40 -40
  208. package/tests/hooks.test.ts +79 -79
  209. package/tests/ide-bridge.test.ts +38 -38
  210. package/tests/image-generator.test.ts +84 -84
  211. package/tests/init-role.test.ts +124 -124
  212. package/tests/integrations.test.ts +249 -249
  213. package/tests/mcp-client.test.ts +92 -92
  214. package/tests/mcp-server.test.ts +178 -178
  215. package/tests/mcp-servers.test.ts +260 -260
  216. package/tests/node-network.test.ts +74 -74
  217. package/tests/plugin-a2a-enhanced.test.ts +230 -230
  218. package/tests/profiles.test.ts +61 -61
  219. package/tests/publish.test.ts +231 -231
  220. package/tests/rl-tools.test.ts +93 -93
  221. package/tests/sandbox-manager.test.ts +46 -46
  222. package/tests/scheduler.test.ts +200 -200
  223. package/tests/secrets.test.ts +107 -107
  224. package/tests/security-enhanced.test.ts +233 -233
  225. package/tests/settings-api.test.ts +148 -148
  226. package/tests/setup.test.ts +73 -73
  227. package/tests/subagent.test.ts +193 -193
  228. package/tests/telegram-discord.test.ts +60 -60
  229. package/tests/telemetry.test.ts +186 -186
  230. package/tests/user-profiler.test.ts +169 -169
  231. package/tests/v090-features.test.ts +254 -254
  232. package/tests/vision.test.ts +61 -61
  233. package/tests/voice-call.test.ts +47 -47
  234. package/tests/voice-enhanced.test.ts +169 -169
  235. package/tests/voice-interaction.test.ts +38 -38
  236. package/tests/web-search.test.ts +155 -155
  237. package/tests/workflow-graph.test.ts +279 -279
  238. package/tutorial/customer-service-agent/README.md +612 -612
  239. package/tutorial/customer-service-agent/SOUL.md +26 -26
  240. package/tutorial/customer-service-agent/agent.yaml +63 -63
  241. package/tutorial/customer-service-agent/package.json +19 -19
  242. package/tutorial/customer-service-agent/src/index.ts +69 -69
  243. package/tutorial/customer-service-agent/src/skills/faq.ts +27 -27
  244. package/tutorial/customer-service-agent/src/skills/ticket.ts +22 -22
  245. package/tutorial/customer-service-agent/tsconfig.json +14 -14
@@ -1,399 +1,399 @@
1
- import type { Message } from '../core/types';
2
- import { BaseChannel } from './index';
3
- import { WebSocketServer, type WebSocket } from 'ws';
4
-
5
- /**
6
- * WebSocket Channel — v1.1.0
7
- *
8
- * Enhanced with:
9
- * - Room support (multiple clients in a room)
10
- * - Heartbeat/ping-pong to detect disconnected clients
11
- * - Reconnection handling (session persistence)
12
- * - Binary message support
13
- * - Connection authentication (optional token in query string)
14
- */
15
-
16
- export interface WebSocketChannelConfig {
17
- port?: number;
18
- /** Heartbeat interval in ms (default: 30000) */
19
- heartbeatInterval?: number;
20
- /** Valid auth tokens (if empty, no auth required) */
21
- authTokens?: string[];
22
- /** Max clients per room (default: 100) */
23
- maxClientsPerRoom?: number;
24
- }
25
-
26
- interface ClientInfo {
27
- ws: WebSocket;
28
- sessionId: string;
29
- rooms: Set<string>;
30
- isAlive: boolean;
31
- authenticated: boolean;
32
- connectedAt: number;
33
- lastMessageAt: number;
34
- }
35
-
36
- export class WebSocketChannel extends BaseChannel {
37
- readonly type = 'websocket';
38
- private wss: WebSocketServer | null = null;
39
- private config: Required<WebSocketChannelConfig>;
40
- private clients: Map<string, ClientInfo> = new Map(); // sessionId -> ClientInfo
41
- private rooms: Map<string, Set<string>> = new Map(); // roomId -> Set<sessionId>
42
- private heartbeatTimer: ReturnType<typeof setInterval> | null = null;
43
-
44
- constructor(configOrPort: number | WebSocketChannelConfig = 3002) {
45
- super();
46
- if (typeof configOrPort === 'number') {
47
- this.config = { port: configOrPort, heartbeatInterval: 30000, authTokens: [], maxClientsPerRoom: 100 };
48
- } else {
49
- this.config = {
50
- port: configOrPort.port ?? 3002,
51
- heartbeatInterval: configOrPort.heartbeatInterval ?? 30000,
52
- authTokens: configOrPort.authTokens ?? [],
53
- maxClientsPerRoom: configOrPort.maxClientsPerRoom ?? 100,
54
- };
55
- }
56
- }
57
-
58
- async start(): Promise<void> {
59
- return new Promise((resolve) => {
60
- this.wss = new WebSocketServer({ port: this.config.port });
61
-
62
- this.wss.on('connection', (ws, req) => {
63
- const url = new URL(req.url ?? '/', `http://localhost:${this.config.port}`);
64
- const token = url.searchParams.get('token');
65
- const sessionId = url.searchParams.get('sessionId') ?? `ws_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
66
-
67
- // Authentication check
68
- if (this.config.authTokens.length > 0) {
69
- if (!token || !this.config.authTokens.includes(token)) {
70
- ws.close(4001, 'Unauthorized');
71
- return;
72
- }
73
- }
74
-
75
- // Handle reconnection: if sessionId already exists, replace the connection
76
- const existing = this.clients.get(sessionId);
77
- if (existing) {
78
- try { existing.ws.close(4000, 'Replaced by new connection'); } catch {}
79
- }
80
-
81
- const clientInfo: ClientInfo = {
82
- ws,
83
- sessionId,
84
- rooms: existing?.rooms ?? new Set(),
85
- isAlive: true,
86
- authenticated: true,
87
- connectedAt: Date.now(),
88
- lastMessageAt: Date.now(),
89
- };
90
- this.clients.set(sessionId, clientInfo);
91
-
92
- // Re-register in rooms after reconnect
93
- for (const roomId of clientInfo.rooms) {
94
- const room = this.rooms.get(roomId);
95
- if (room) room.add(sessionId);
96
- }
97
-
98
- ws.on('pong', () => {
99
- clientInfo.isAlive = true;
100
- });
101
-
102
- ws.on('message', async (data, isBinary) => {
103
- clientInfo.lastMessageAt = Date.now();
104
- clientInfo.isAlive = true;
105
-
106
- if (isBinary) {
107
- await this.handleBinaryMessage(clientInfo, data as Buffer);
108
- return;
109
- }
110
-
111
- try {
112
- const parsed = JSON.parse(data.toString());
113
- await this.handleTextMessage(clientInfo, parsed);
114
- } catch {
115
- ws.send(JSON.stringify({ error: 'Invalid message format' }));
116
- }
117
- });
118
-
119
- ws.on('close', () => {
120
- // Don't immediately remove - allow reconnection window
121
- const info = this.clients.get(sessionId);
122
- if (info && info.ws === ws) {
123
- // Mark as disconnected but keep for potential reconnection
124
- setTimeout(() => {
125
- const current = this.clients.get(sessionId);
126
- if (current && current.ws === ws) {
127
- this.removeClient(sessionId);
128
- }
129
- }, 60000); // 60s reconnection window
130
- }
131
- });
132
-
133
- ws.send(JSON.stringify({
134
- type: 'connected',
135
- sessionId,
136
- timestamp: Date.now(),
137
- }));
138
- });
139
-
140
- // Start heartbeat
141
- this.heartbeatTimer = setInterval(() => {
142
- for (const [sessionId, info] of this.clients) {
143
- if (!info.isAlive) {
144
- try { info.ws.terminate(); } catch {}
145
- this.removeClient(sessionId);
146
- continue;
147
- }
148
- info.isAlive = false;
149
- try { info.ws.ping(); } catch {}
150
- }
151
- }, this.config.heartbeatInterval);
152
-
153
- this.wss.on('listening', () => {
154
- console.log(`[WebSocketChannel] Listening on port ${this.config.port}`);
155
- resolve();
156
- });
157
- });
158
- }
159
-
160
- async stop(): Promise<void> {
161
- if (this.heartbeatTimer) {
162
- clearInterval(this.heartbeatTimer);
163
- this.heartbeatTimer = null;
164
- }
165
-
166
- for (const [, info] of this.clients) {
167
- try { info.ws.close(); } catch {}
168
- }
169
- this.clients.clear();
170
- this.rooms.clear();
171
-
172
- return new Promise((resolve, reject) => {
173
- if (!this.wss) return resolve();
174
- this.wss.close((err) => (err ? reject(err) : resolve()));
175
- });
176
- }
177
-
178
- /** Handle text (JSON) messages */
179
- private async handleTextMessage(client: ClientInfo, parsed: any): Promise<void> {
180
- const type = parsed.type ?? 'message';
181
-
182
- switch (type) {
183
- case 'join': {
184
- const roomId = parsed.room;
185
- if (!roomId) {
186
- client.ws.send(JSON.stringify({ error: 'room is required for join' }));
187
- return;
188
- }
189
- this.joinRoom(client.sessionId, roomId);
190
- client.ws.send(JSON.stringify({ type: 'joined', room: roomId, members: this.getRoomMembers(roomId).length }));
191
- return;
192
- }
193
-
194
- case 'leave': {
195
- const roomId = parsed.room;
196
- if (roomId) {
197
- this.leaveRoom(client.sessionId, roomId);
198
- client.ws.send(JSON.stringify({ type: 'left', room: roomId }));
199
- }
200
- return;
201
- }
202
-
203
- case 'room_message': {
204
- const roomId = parsed.room;
205
- const content = parsed.content ?? parsed.message;
206
- if (roomId && content) {
207
- this.broadcastToRoom(roomId, {
208
- type: 'room_message',
209
- room: roomId,
210
- from: client.sessionId,
211
- content,
212
- timestamp: Date.now(),
213
- }, client.sessionId);
214
- }
215
- return;
216
- }
217
-
218
- case 'ping': {
219
- client.ws.send(JSON.stringify({ type: 'pong', timestamp: Date.now() }));
220
- return;
221
- }
222
-
223
- default: {
224
- // Regular chat message
225
- if (!this.handler) return;
226
-
227
- const msg: Message = {
228
- id: parsed.id ?? `ws_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`,
229
- role: 'user',
230
- content: parsed.content ?? parsed.message ?? JSON.stringify(parsed),
231
- timestamp: Date.now(),
232
- metadata: {
233
- sessionId: client.sessionId,
234
- platform: 'websocket',
235
- room: parsed.room,
236
- },
237
- };
238
-
239
- const response = await this.handler(msg);
240
- client.ws.send(JSON.stringify({
241
- id: response.id,
242
- content: response.content,
243
- timestamp: response.timestamp,
244
- }));
245
- }
246
- }
247
- }
248
-
249
- /** Handle binary messages */
250
- private async handleBinaryMessage(client: ClientInfo, data: Buffer): Promise<void> {
251
- // Emit binary data with metadata
252
- client.ws.send(JSON.stringify({
253
- type: 'binary_ack',
254
- size: data.length,
255
- timestamp: Date.now(),
256
- }));
257
-
258
- // If handler exists, pass as base64
259
- if (this.handler) {
260
- const msg: Message = {
261
- id: `ws_bin_${Date.now()}`,
262
- role: 'user',
263
- content: `[binary:${data.length} bytes]`,
264
- timestamp: Date.now(),
265
- metadata: {
266
- sessionId: client.sessionId,
267
- platform: 'websocket',
268
- binary: true,
269
- binaryData: data.toString('base64'),
270
- },
271
- };
272
- const response = await this.handler(msg);
273
- client.ws.send(JSON.stringify({
274
- id: response.id,
275
- content: response.content,
276
- timestamp: response.timestamp,
277
- }));
278
- }
279
- }
280
-
281
- /** Join a room */
282
- joinRoom(sessionId: string, roomId: string): boolean {
283
- const client = this.clients.get(sessionId);
284
- if (!client) return false;
285
-
286
- if (!this.rooms.has(roomId)) {
287
- this.rooms.set(roomId, new Set());
288
- }
289
-
290
- const room = this.rooms.get(roomId)!;
291
- if (room.size >= this.config.maxClientsPerRoom) {
292
- client.ws.send(JSON.stringify({ error: 'Room is full', room: roomId }));
293
- return false;
294
- }
295
-
296
- room.add(sessionId);
297
- client.rooms.add(roomId);
298
-
299
- // Notify other room members
300
- this.broadcastToRoom(roomId, {
301
- type: 'member_joined',
302
- room: roomId,
303
- sessionId,
304
- members: room.size,
305
- timestamp: Date.now(),
306
- }, sessionId);
307
-
308
- return true;
309
- }
310
-
311
- /** Leave a room */
312
- leaveRoom(sessionId: string, roomId: string): void {
313
- const client = this.clients.get(sessionId);
314
- if (client) client.rooms.delete(roomId);
315
-
316
- const room = this.rooms.get(roomId);
317
- if (room) {
318
- room.delete(sessionId);
319
- if (room.size === 0) {
320
- this.rooms.delete(roomId);
321
- } else {
322
- this.broadcastToRoom(roomId, {
323
- type: 'member_left',
324
- room: roomId,
325
- sessionId,
326
- members: room.size,
327
- timestamp: Date.now(),
328
- });
329
- }
330
- }
331
- }
332
-
333
- /** Remove client completely */
334
- private removeClient(sessionId: string): void {
335
- const client = this.clients.get(sessionId);
336
- if (!client) return;
337
-
338
- for (const roomId of client.rooms) {
339
- this.leaveRoom(sessionId, roomId);
340
- }
341
- this.clients.delete(sessionId);
342
- }
343
-
344
- /** Get room member session IDs */
345
- getRoomMembers(roomId: string): string[] {
346
- return [...(this.rooms.get(roomId) ?? [])];
347
- }
348
-
349
- /** Get all rooms */
350
- getRooms(): string[] {
351
- return [...this.rooms.keys()];
352
- }
353
-
354
- /** Broadcast to all clients */
355
- broadcast(content: string): void {
356
- const msg = JSON.stringify({ type: 'broadcast', content, timestamp: Date.now() });
357
- for (const [, info] of this.clients) {
358
- if (info.ws.readyState === 1) {
359
- info.ws.send(msg);
360
- }
361
- }
362
- }
363
-
364
- /** Broadcast to all clients in a room */
365
- broadcastToRoom(roomId: string, data: any, excludeSessionId?: string): void {
366
- const room = this.rooms.get(roomId);
367
- if (!room) return;
368
-
369
- const msg = typeof data === 'string' ? data : JSON.stringify(data);
370
- for (const sessionId of room) {
371
- if (sessionId === excludeSessionId) continue;
372
- const client = this.clients.get(sessionId);
373
- if (client && client.ws.readyState === 1) {
374
- client.ws.send(msg);
375
- }
376
- }
377
- }
378
-
379
- /** Send to specific session */
380
- sendToSession(sessionId: string, data: any): boolean {
381
- const client = this.clients.get(sessionId);
382
- if (!client || client.ws.readyState !== 1) return false;
383
- client.ws.send(typeof data === 'string' ? data : JSON.stringify(data));
384
- return true;
385
- }
386
-
387
- /** Get connection stats */
388
- getStats(): { clients: number; rooms: number; roomDetails: Record<string, number> } {
389
- const roomDetails: Record<string, number> = {};
390
- for (const [roomId, members] of this.rooms) {
391
- roomDetails[roomId] = members.size;
392
- }
393
- return {
394
- clients: this.clients.size,
395
- rooms: this.rooms.size,
396
- roomDetails,
397
- };
398
- }
399
- }
1
+ import type { Message } from '../core/types';
2
+ import { BaseChannel } from './index';
3
+ import { WebSocketServer, type WebSocket } from 'ws';
4
+
5
+ /**
6
+ * WebSocket Channel — v1.1.0
7
+ *
8
+ * Enhanced with:
9
+ * - Room support (multiple clients in a room)
10
+ * - Heartbeat/ping-pong to detect disconnected clients
11
+ * - Reconnection handling (session persistence)
12
+ * - Binary message support
13
+ * - Connection authentication (optional token in query string)
14
+ */
15
+
16
+ export interface WebSocketChannelConfig {
17
+ port?: number;
18
+ /** Heartbeat interval in ms (default: 30000) */
19
+ heartbeatInterval?: number;
20
+ /** Valid auth tokens (if empty, no auth required) */
21
+ authTokens?: string[];
22
+ /** Max clients per room (default: 100) */
23
+ maxClientsPerRoom?: number;
24
+ }
25
+
26
+ interface ClientInfo {
27
+ ws: WebSocket;
28
+ sessionId: string;
29
+ rooms: Set<string>;
30
+ isAlive: boolean;
31
+ authenticated: boolean;
32
+ connectedAt: number;
33
+ lastMessageAt: number;
34
+ }
35
+
36
+ export class WebSocketChannel extends BaseChannel {
37
+ readonly type = 'websocket';
38
+ private wss: WebSocketServer | null = null;
39
+ private config: Required<WebSocketChannelConfig>;
40
+ private clients: Map<string, ClientInfo> = new Map(); // sessionId -> ClientInfo
41
+ private rooms: Map<string, Set<string>> = new Map(); // roomId -> Set<sessionId>
42
+ private heartbeatTimer: ReturnType<typeof setInterval> | null = null;
43
+
44
+ constructor(configOrPort: number | WebSocketChannelConfig = 3002) {
45
+ super();
46
+ if (typeof configOrPort === 'number') {
47
+ this.config = { port: configOrPort, heartbeatInterval: 30000, authTokens: [], maxClientsPerRoom: 100 };
48
+ } else {
49
+ this.config = {
50
+ port: configOrPort.port ?? 3002,
51
+ heartbeatInterval: configOrPort.heartbeatInterval ?? 30000,
52
+ authTokens: configOrPort.authTokens ?? [],
53
+ maxClientsPerRoom: configOrPort.maxClientsPerRoom ?? 100,
54
+ };
55
+ }
56
+ }
57
+
58
+ async start(): Promise<void> {
59
+ return new Promise((resolve) => {
60
+ this.wss = new WebSocketServer({ port: this.config.port });
61
+
62
+ this.wss.on('connection', (ws, req) => {
63
+ const url = new URL(req.url ?? '/', `http://localhost:${this.config.port}`);
64
+ const token = url.searchParams.get('token');
65
+ const sessionId = url.searchParams.get('sessionId') ?? `ws_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
66
+
67
+ // Authentication check
68
+ if (this.config.authTokens.length > 0) {
69
+ if (!token || !this.config.authTokens.includes(token)) {
70
+ ws.close(4001, 'Unauthorized');
71
+ return;
72
+ }
73
+ }
74
+
75
+ // Handle reconnection: if sessionId already exists, replace the connection
76
+ const existing = this.clients.get(sessionId);
77
+ if (existing) {
78
+ try { existing.ws.close(4000, 'Replaced by new connection'); } catch {}
79
+ }
80
+
81
+ const clientInfo: ClientInfo = {
82
+ ws,
83
+ sessionId,
84
+ rooms: existing?.rooms ?? new Set(),
85
+ isAlive: true,
86
+ authenticated: true,
87
+ connectedAt: Date.now(),
88
+ lastMessageAt: Date.now(),
89
+ };
90
+ this.clients.set(sessionId, clientInfo);
91
+
92
+ // Re-register in rooms after reconnect
93
+ for (const roomId of clientInfo.rooms) {
94
+ const room = this.rooms.get(roomId);
95
+ if (room) room.add(sessionId);
96
+ }
97
+
98
+ ws.on('pong', () => {
99
+ clientInfo.isAlive = true;
100
+ });
101
+
102
+ ws.on('message', async (data, isBinary) => {
103
+ clientInfo.lastMessageAt = Date.now();
104
+ clientInfo.isAlive = true;
105
+
106
+ if (isBinary) {
107
+ await this.handleBinaryMessage(clientInfo, data as Buffer);
108
+ return;
109
+ }
110
+
111
+ try {
112
+ const parsed = JSON.parse(data.toString());
113
+ await this.handleTextMessage(clientInfo, parsed);
114
+ } catch {
115
+ ws.send(JSON.stringify({ error: 'Invalid message format' }));
116
+ }
117
+ });
118
+
119
+ ws.on('close', () => {
120
+ // Don't immediately remove - allow reconnection window
121
+ const info = this.clients.get(sessionId);
122
+ if (info && info.ws === ws) {
123
+ // Mark as disconnected but keep for potential reconnection
124
+ setTimeout(() => {
125
+ const current = this.clients.get(sessionId);
126
+ if (current && current.ws === ws) {
127
+ this.removeClient(sessionId);
128
+ }
129
+ }, 60000); // 60s reconnection window
130
+ }
131
+ });
132
+
133
+ ws.send(JSON.stringify({
134
+ type: 'connected',
135
+ sessionId,
136
+ timestamp: Date.now(),
137
+ }));
138
+ });
139
+
140
+ // Start heartbeat
141
+ this.heartbeatTimer = setInterval(() => {
142
+ for (const [sessionId, info] of this.clients) {
143
+ if (!info.isAlive) {
144
+ try { info.ws.terminate(); } catch {}
145
+ this.removeClient(sessionId);
146
+ continue;
147
+ }
148
+ info.isAlive = false;
149
+ try { info.ws.ping(); } catch {}
150
+ }
151
+ }, this.config.heartbeatInterval);
152
+
153
+ this.wss.on('listening', () => {
154
+ console.log(`[WebSocketChannel] Listening on port ${this.config.port}`);
155
+ resolve();
156
+ });
157
+ });
158
+ }
159
+
160
+ async stop(): Promise<void> {
161
+ if (this.heartbeatTimer) {
162
+ clearInterval(this.heartbeatTimer);
163
+ this.heartbeatTimer = null;
164
+ }
165
+
166
+ for (const [, info] of this.clients) {
167
+ try { info.ws.close(); } catch {}
168
+ }
169
+ this.clients.clear();
170
+ this.rooms.clear();
171
+
172
+ return new Promise((resolve, reject) => {
173
+ if (!this.wss) return resolve();
174
+ this.wss.close((err) => (err ? reject(err) : resolve()));
175
+ });
176
+ }
177
+
178
+ /** Handle text (JSON) messages */
179
+ private async handleTextMessage(client: ClientInfo, parsed: any): Promise<void> {
180
+ const type = parsed.type ?? 'message';
181
+
182
+ switch (type) {
183
+ case 'join': {
184
+ const roomId = parsed.room;
185
+ if (!roomId) {
186
+ client.ws.send(JSON.stringify({ error: 'room is required for join' }));
187
+ return;
188
+ }
189
+ this.joinRoom(client.sessionId, roomId);
190
+ client.ws.send(JSON.stringify({ type: 'joined', room: roomId, members: this.getRoomMembers(roomId).length }));
191
+ return;
192
+ }
193
+
194
+ case 'leave': {
195
+ const roomId = parsed.room;
196
+ if (roomId) {
197
+ this.leaveRoom(client.sessionId, roomId);
198
+ client.ws.send(JSON.stringify({ type: 'left', room: roomId }));
199
+ }
200
+ return;
201
+ }
202
+
203
+ case 'room_message': {
204
+ const roomId = parsed.room;
205
+ const content = parsed.content ?? parsed.message;
206
+ if (roomId && content) {
207
+ this.broadcastToRoom(roomId, {
208
+ type: 'room_message',
209
+ room: roomId,
210
+ from: client.sessionId,
211
+ content,
212
+ timestamp: Date.now(),
213
+ }, client.sessionId);
214
+ }
215
+ return;
216
+ }
217
+
218
+ case 'ping': {
219
+ client.ws.send(JSON.stringify({ type: 'pong', timestamp: Date.now() }));
220
+ return;
221
+ }
222
+
223
+ default: {
224
+ // Regular chat message
225
+ if (!this.handler) return;
226
+
227
+ const msg: Message = {
228
+ id: parsed.id ?? `ws_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`,
229
+ role: 'user',
230
+ content: parsed.content ?? parsed.message ?? JSON.stringify(parsed),
231
+ timestamp: Date.now(),
232
+ metadata: {
233
+ sessionId: client.sessionId,
234
+ platform: 'websocket',
235
+ room: parsed.room,
236
+ },
237
+ };
238
+
239
+ const response = await this.handler(msg);
240
+ client.ws.send(JSON.stringify({
241
+ id: response.id,
242
+ content: response.content,
243
+ timestamp: response.timestamp,
244
+ }));
245
+ }
246
+ }
247
+ }
248
+
249
+ /** Handle binary messages */
250
+ private async handleBinaryMessage(client: ClientInfo, data: Buffer): Promise<void> {
251
+ // Emit binary data with metadata
252
+ client.ws.send(JSON.stringify({
253
+ type: 'binary_ack',
254
+ size: data.length,
255
+ timestamp: Date.now(),
256
+ }));
257
+
258
+ // If handler exists, pass as base64
259
+ if (this.handler) {
260
+ const msg: Message = {
261
+ id: `ws_bin_${Date.now()}`,
262
+ role: 'user',
263
+ content: `[binary:${data.length} bytes]`,
264
+ timestamp: Date.now(),
265
+ metadata: {
266
+ sessionId: client.sessionId,
267
+ platform: 'websocket',
268
+ binary: true,
269
+ binaryData: data.toString('base64'),
270
+ },
271
+ };
272
+ const response = await this.handler(msg);
273
+ client.ws.send(JSON.stringify({
274
+ id: response.id,
275
+ content: response.content,
276
+ timestamp: response.timestamp,
277
+ }));
278
+ }
279
+ }
280
+
281
+ /** Join a room */
282
+ joinRoom(sessionId: string, roomId: string): boolean {
283
+ const client = this.clients.get(sessionId);
284
+ if (!client) return false;
285
+
286
+ if (!this.rooms.has(roomId)) {
287
+ this.rooms.set(roomId, new Set());
288
+ }
289
+
290
+ const room = this.rooms.get(roomId)!;
291
+ if (room.size >= this.config.maxClientsPerRoom) {
292
+ client.ws.send(JSON.stringify({ error: 'Room is full', room: roomId }));
293
+ return false;
294
+ }
295
+
296
+ room.add(sessionId);
297
+ client.rooms.add(roomId);
298
+
299
+ // Notify other room members
300
+ this.broadcastToRoom(roomId, {
301
+ type: 'member_joined',
302
+ room: roomId,
303
+ sessionId,
304
+ members: room.size,
305
+ timestamp: Date.now(),
306
+ }, sessionId);
307
+
308
+ return true;
309
+ }
310
+
311
+ /** Leave a room */
312
+ leaveRoom(sessionId: string, roomId: string): void {
313
+ const client = this.clients.get(sessionId);
314
+ if (client) client.rooms.delete(roomId);
315
+
316
+ const room = this.rooms.get(roomId);
317
+ if (room) {
318
+ room.delete(sessionId);
319
+ if (room.size === 0) {
320
+ this.rooms.delete(roomId);
321
+ } else {
322
+ this.broadcastToRoom(roomId, {
323
+ type: 'member_left',
324
+ room: roomId,
325
+ sessionId,
326
+ members: room.size,
327
+ timestamp: Date.now(),
328
+ });
329
+ }
330
+ }
331
+ }
332
+
333
+ /** Remove client completely */
334
+ private removeClient(sessionId: string): void {
335
+ const client = this.clients.get(sessionId);
336
+ if (!client) return;
337
+
338
+ for (const roomId of client.rooms) {
339
+ this.leaveRoom(sessionId, roomId);
340
+ }
341
+ this.clients.delete(sessionId);
342
+ }
343
+
344
+ /** Get room member session IDs */
345
+ getRoomMembers(roomId: string): string[] {
346
+ return [...(this.rooms.get(roomId) ?? [])];
347
+ }
348
+
349
+ /** Get all rooms */
350
+ getRooms(): string[] {
351
+ return [...this.rooms.keys()];
352
+ }
353
+
354
+ /** Broadcast to all clients */
355
+ broadcast(content: string): void {
356
+ const msg = JSON.stringify({ type: 'broadcast', content, timestamp: Date.now() });
357
+ for (const [, info] of this.clients) {
358
+ if (info.ws.readyState === 1) {
359
+ info.ws.send(msg);
360
+ }
361
+ }
362
+ }
363
+
364
+ /** Broadcast to all clients in a room */
365
+ broadcastToRoom(roomId: string, data: any, excludeSessionId?: string): void {
366
+ const room = this.rooms.get(roomId);
367
+ if (!room) return;
368
+
369
+ const msg = typeof data === 'string' ? data : JSON.stringify(data);
370
+ for (const sessionId of room) {
371
+ if (sessionId === excludeSessionId) continue;
372
+ const client = this.clients.get(sessionId);
373
+ if (client && client.ws.readyState === 1) {
374
+ client.ws.send(msg);
375
+ }
376
+ }
377
+ }
378
+
379
+ /** Send to specific session */
380
+ sendToSession(sessionId: string, data: any): boolean {
381
+ const client = this.clients.get(sessionId);
382
+ if (!client || client.ws.readyState !== 1) return false;
383
+ client.ws.send(typeof data === 'string' ? data : JSON.stringify(data));
384
+ return true;
385
+ }
386
+
387
+ /** Get connection stats */
388
+ getStats(): { clients: number; rooms: number; roomDetails: Record<string, number> } {
389
+ const roomDetails: Record<string, number> = {};
390
+ for (const [roomId, members] of this.rooms) {
391
+ roomDetails[roomId] = members.size;
392
+ }
393
+ return {
394
+ clients: this.clients.size,
395
+ rooms: this.rooms.size,
396
+ roomDetails,
397
+ };
398
+ }
399
+ }