-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathagent-client.js
More file actions
78 lines (65 loc) · 1.79 KB
/
Copy pathagent-client.js
File metadata and controls
78 lines (65 loc) · 1.79 KB
1
2
3
4
5
6
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* GeminiClient: Handles WebSocket communication
*/
class GeminiClient {
constructor(config) {
this.websocket = null;
this.onOpen = config.onOpen;
this.onMessage = config.onMessage;
this.onClose = config.onClose;
this.onError = config.onError;
}
connect(params = {}) {
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
let wsUrl = `${protocol}//${window.location.host}/ws`;
// Append query parameters if provided
const queryParts = [];
for (const [key, value] of Object.entries(params)) {
if (value != null && value !== '') {
queryParts.push(
`${encodeURIComponent(key)}=${encodeURIComponent(value)}`);
}
}
if (queryParts.length > 0) {
wsUrl += '?' + queryParts.join('&');
}
this.websocket = new WebSocket(wsUrl);
this.websocket.binaryType = 'arraybuffer';
this.websocket.onopen = () => {
if (this.onOpen) this.onOpen();
};
this.websocket.onmessage = (event) => {
if (this.onMessage) this.onMessage(event);
};
this.websocket.onclose = (event) => {
if (this.onClose) this.onClose(event);
};
this.websocket.onerror = (event) => {
if (this.onError) this.onError(event);
};
}
send(data) {
if (this.websocket && this.websocket.readyState === WebSocket.OPEN) {
this.websocket.send(data);
}
}
sendText(text) {
this.send(text);
}
sendImage(base64Data, mimeType = 'image/jpeg') {
this.send(JSON.stringify({
type: 'image',
mime_type: mimeType,
data: base64Data,
}));
}
disconnect() {
if (this.websocket) {
this.websocket.close();
this.websocket = null;
}
}
isConnected() {
return this.websocket && this.websocket.readyState === WebSocket.OPEN;
}
}