opc-agent 1.4.0 → 2.0.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/CHANGELOG.md +25 -0
- package/README.md +91 -32
- package/dist/channels/email.d.ts +32 -26
- package/dist/channels/email.js +239 -62
- package/dist/channels/feishu.d.ts +21 -6
- package/dist/channels/feishu.js +225 -126
- package/dist/channels/telegram.d.ts +30 -9
- package/dist/channels/telegram.js +125 -33
- package/dist/channels/websocket.d.ts +46 -3
- package/dist/channels/websocket.js +306 -37
- package/dist/channels/wechat.d.ts +33 -13
- package/dist/channels/wechat.js +229 -42
- package/dist/cli.js +1127 -19
- package/dist/core/a2a.d.ts +17 -0
- package/dist/core/a2a.js +43 -1
- package/dist/core/agent.d.ts +39 -0
- package/dist/core/agent.js +228 -3
- package/dist/core/runtime.d.ts +7 -0
- package/dist/core/runtime.js +205 -2
- package/dist/core/sandbox.d.ts +26 -0
- package/dist/core/sandbox.js +117 -0
- package/dist/core/scheduler.d.ts +52 -0
- package/dist/core/scheduler.js +168 -0
- package/dist/core/subagent.d.ts +28 -0
- package/dist/core/subagent.js +65 -0
- package/dist/core/workflow-graph.d.ts +93 -0
- package/dist/core/workflow-graph.js +247 -0
- package/dist/daemon.d.ts +3 -0
- package/dist/daemon.js +134 -0
- package/dist/doctor.d.ts +15 -0
- package/dist/doctor.js +183 -0
- package/dist/eval/index.d.ts +65 -0
- package/dist/eval/index.js +191 -0
- package/dist/index.d.ts +37 -6
- package/dist/index.js +75 -3
- package/dist/plugins/content-filter.d.ts +7 -0
- package/dist/plugins/content-filter.js +25 -0
- package/dist/plugins/index.d.ts +42 -0
- package/dist/plugins/index.js +108 -2
- package/dist/plugins/logger.d.ts +6 -0
- package/dist/plugins/logger.js +20 -0
- package/dist/plugins/rate-limiter.d.ts +7 -0
- package/dist/plugins/rate-limiter.js +35 -0
- package/dist/protocols/a2a/client.d.ts +25 -0
- package/dist/protocols/a2a/client.js +115 -0
- package/dist/protocols/a2a/index.d.ts +6 -0
- package/dist/protocols/a2a/index.js +12 -0
- package/dist/protocols/a2a/server.d.ts +41 -0
- package/dist/protocols/a2a/server.js +295 -0
- package/dist/protocols/a2a/types.d.ts +91 -0
- package/dist/protocols/a2a/types.js +15 -0
- package/dist/protocols/a2a/utils.d.ts +6 -0
- package/dist/protocols/a2a/utils.js +47 -0
- package/dist/protocols/agui/client.d.ts +10 -0
- package/dist/protocols/agui/client.js +75 -0
- package/dist/protocols/agui/index.d.ts +4 -0
- package/dist/protocols/agui/index.js +25 -0
- package/dist/protocols/agui/server.d.ts +37 -0
- package/dist/protocols/agui/server.js +191 -0
- package/dist/protocols/agui/types.d.ts +107 -0
- package/dist/protocols/agui/types.js +17 -0
- package/dist/protocols/index.d.ts +2 -0
- package/dist/protocols/index.js +19 -0
- package/dist/protocols/mcp/agent-tools.d.ts +11 -0
- package/dist/protocols/mcp/agent-tools.js +129 -0
- package/dist/protocols/mcp/index.d.ts +5 -0
- package/dist/protocols/mcp/index.js +11 -0
- package/dist/protocols/mcp/server.d.ts +31 -0
- package/dist/protocols/mcp/server.js +248 -0
- package/dist/protocols/mcp/types.d.ts +92 -0
- package/dist/protocols/mcp/types.js +17 -0
- package/dist/providers/index.d.ts +5 -1
- package/dist/providers/index.js +16 -9
- package/dist/publish/index.d.ts +45 -0
- package/dist/publish/index.js +350 -0
- package/dist/schema/oad.d.ts +859 -67
- package/dist/schema/oad.js +47 -3
- package/dist/security/approval.d.ts +36 -0
- package/dist/security/approval.js +113 -0
- package/dist/security/index.d.ts +4 -0
- package/dist/security/index.js +8 -0
- package/dist/security/keys.d.ts +16 -0
- package/dist/security/keys.js +117 -0
- package/dist/skills/auto-learn.d.ts +28 -0
- package/dist/skills/auto-learn.js +257 -0
- package/dist/studio/server.d.ts +63 -0
- package/dist/studio/server.js +625 -0
- package/dist/studio-ui/index.html +662 -0
- package/dist/telemetry/index.d.ts +93 -0
- package/dist/telemetry/index.js +285 -0
- package/dist/tools/builtin/datetime.d.ts +3 -0
- package/dist/tools/builtin/datetime.js +44 -0
- package/dist/tools/builtin/file.d.ts +3 -0
- package/dist/tools/builtin/file.js +151 -0
- package/dist/tools/builtin/index.d.ts +15 -0
- package/dist/tools/builtin/index.js +30 -0
- package/dist/tools/builtin/shell.d.ts +3 -0
- package/dist/tools/builtin/shell.js +43 -0
- package/dist/tools/builtin/web.d.ts +3 -0
- package/dist/tools/builtin/web.js +37 -0
- package/dist/tools/mcp-client.d.ts +24 -0
- package/dist/tools/mcp-client.js +119 -0
- package/package.json +5 -3
- package/scripts/install.ps1 +31 -0
- package/scripts/install.sh +40 -0
- package/src/channels/email.ts +351 -177
- package/src/channels/feishu.ts +349 -236
- package/src/channels/telegram.ts +212 -90
- package/src/channels/websocket.ts +399 -87
- package/src/channels/wechat.ts +329 -149
- package/src/cli.ts +1201 -20
- package/src/core/a2a.ts +60 -0
- package/src/core/agent.ts +420 -152
- package/src/core/runtime.ts +174 -0
- package/src/core/sandbox.ts +143 -0
- package/src/core/scheduler.ts +187 -0
- package/src/core/subagent.ts +98 -0
- package/src/core/workflow-graph.ts +365 -0
- package/src/daemon.ts +96 -0
- package/src/doctor.ts +156 -0
- package/src/eval/index.ts +211 -0
- package/src/eval/suites/basic.json +16 -0
- package/src/eval/suites/memory.json +12 -0
- package/src/eval/suites/safety.json +14 -0
- package/src/index.ts +65 -6
- package/src/plugins/content-filter.ts +23 -0
- package/src/plugins/index.ts +133 -2
- package/src/plugins/logger.ts +18 -0
- package/src/plugins/rate-limiter.ts +38 -0
- package/src/protocols/a2a/client.ts +132 -0
- package/src/protocols/a2a/index.ts +8 -0
- package/src/protocols/a2a/server.ts +333 -0
- package/src/protocols/a2a/types.ts +88 -0
- package/src/protocols/a2a/utils.ts +50 -0
- package/src/protocols/agui/client.ts +83 -0
- package/src/protocols/agui/index.ts +4 -0
- package/src/protocols/agui/server.ts +218 -0
- package/src/protocols/agui/types.ts +153 -0
- package/src/protocols/index.ts +2 -0
- package/src/protocols/mcp/agent-tools.ts +134 -0
- package/src/protocols/mcp/index.ts +8 -0
- package/src/protocols/mcp/server.ts +262 -0
- package/src/protocols/mcp/types.ts +69 -0
- package/src/providers/index.ts +354 -339
- package/src/publish/index.ts +376 -0
- package/src/schema/oad.ts +204 -154
- package/src/security/approval.ts +131 -0
- package/src/security/index.ts +3 -0
- package/src/security/keys.ts +87 -0
- package/src/skills/auto-learn.ts +262 -0
- package/src/studio/server.ts +629 -0
- package/src/studio-ui/index.html +662 -0
- package/src/telemetry/index.ts +324 -0
- package/src/tools/builtin/datetime.ts +41 -0
- package/src/tools/builtin/file.ts +107 -0
- package/src/tools/builtin/index.ts +28 -0
- package/src/tools/builtin/shell.ts +43 -0
- package/src/tools/builtin/web.ts +35 -0
- package/src/tools/mcp-client.ts +131 -0
- package/src/types/agent-workstation.d.ts +2 -0
- package/tests/a2a-protocol.test.ts +285 -0
- package/tests/agui-protocol.test.ts +246 -0
- package/tests/auto-learn.test.ts +105 -0
- package/tests/builtin-tools.test.ts +83 -0
- package/tests/channels/discord.test.ts +79 -0
- package/tests/channels/email.test.ts +148 -0
- package/tests/channels/feishu.test.ts +123 -0
- package/tests/channels/telegram.test.ts +129 -0
- package/tests/channels/websocket.test.ts +53 -0
- package/tests/channels/wechat.test.ts +170 -0
- package/tests/chat-cli.test.ts +160 -0
- package/tests/cli.test.ts +46 -0
- package/tests/daemon.test.ts +135 -0
- package/tests/deepbrain-wire.test.ts +234 -0
- package/tests/doctor.test.ts +38 -0
- package/tests/eval.test.ts +173 -0
- package/tests/init-role.test.ts +124 -0
- package/tests/mcp-client.test.ts +92 -0
- package/tests/mcp-server.test.ts +178 -0
- package/tests/plugin-a2a-enhanced.test.ts +230 -0
- package/tests/publish.test.ts +231 -0
- package/tests/scheduler.test.ts +200 -0
- package/tests/security-enhanced.test.ts +233 -0
- package/tests/skill-learner.test.ts +161 -0
- package/tests/studio.test.ts +229 -0
- package/tests/subagent.test.ts +193 -0
- package/tests/telegram-discord.test.ts +60 -0
- package/tests/telemetry.test.ts +186 -0
- package/tests/tools/builtin-extended.test.ts +138 -0
- package/tests/workflow-graph.test.ts +279 -0
- package/tutorial/customer-service-agent/README.md +612 -0
- package/tutorial/customer-service-agent/SOUL.md +26 -0
- package/tutorial/customer-service-agent/agent.yaml +63 -0
- package/tutorial/customer-service-agent/package.json +19 -0
- package/tutorial/customer-service-agent/src/index.ts +69 -0
- package/tutorial/customer-service-agent/src/skills/faq.ts +27 -0
- package/tutorial/customer-service-agent/src/skills/ticket.ts +22 -0
- package/tutorial/customer-service-agent/tsconfig.json +14 -0
|
@@ -1,87 +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
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
this.wss
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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
|
+
}
|