hiloop-sdk 0.4.2 → 0.5.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/dist/ws.d.ts +1 -2
- package/dist/ws.js +32 -16
- package/package.json +1 -1
package/dist/ws.d.ts
CHANGED
|
@@ -33,6 +33,7 @@ export declare class HiloopWsClient {
|
|
|
33
33
|
private closed;
|
|
34
34
|
private crypto;
|
|
35
35
|
private initPromise;
|
|
36
|
+
private seenMessageIds;
|
|
36
37
|
constructor(options: HiloopWsClientOptions);
|
|
37
38
|
/** Initialize encryption context (derives keypair from API key, fetches space key). */
|
|
38
39
|
private ensureCrypto;
|
|
@@ -55,8 +56,6 @@ export declare class HiloopWsClient {
|
|
|
55
56
|
onAny(handler: WsEventHandler): () => void;
|
|
56
57
|
/** Remove all listeners for an event type. */
|
|
57
58
|
off(eventType: string): void;
|
|
58
|
-
/** Auto-decrypt a session message's encryptedContent field. */
|
|
59
|
-
decryptSessionMessage(encryptedContent: string): Promise<string | null>;
|
|
60
59
|
private send;
|
|
61
60
|
private emit;
|
|
62
61
|
private scheduleReconnect;
|
package/dist/ws.js
CHANGED
|
@@ -22,6 +22,7 @@ export class HiloopWsClient {
|
|
|
22
22
|
this.closed = false;
|
|
23
23
|
this.crypto = null;
|
|
24
24
|
this.initPromise = null;
|
|
25
|
+
this.seenMessageIds = new Set();
|
|
25
26
|
this.options = options;
|
|
26
27
|
}
|
|
27
28
|
/** Initialize encryption context (derives keypair from API key, fetches space key). */
|
|
@@ -77,18 +78,38 @@ export class HiloopWsClient {
|
|
|
77
78
|
this.reconnectAttempts = 0;
|
|
78
79
|
};
|
|
79
80
|
this.ws.onmessage = (event) => {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
81
|
+
(async () => {
|
|
82
|
+
try {
|
|
83
|
+
const data = JSON.parse(event.data);
|
|
84
|
+
if (data.type === "connected") {
|
|
85
|
+
resolve();
|
|
86
|
+
}
|
|
87
|
+
// Skip metadata-only message.new (agents get the richer session.message.new instead)
|
|
88
|
+
if (data.type === "message.new") {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
// Deduplicate messages delivered via multiple topics
|
|
92
|
+
if (data.messageId) {
|
|
93
|
+
if (this.seenMessageIds.has(data.messageId))
|
|
94
|
+
return;
|
|
95
|
+
this.seenMessageIds.add(data.messageId);
|
|
96
|
+
// Keep set bounded
|
|
97
|
+
if (this.seenMessageIds.size > 1000) {
|
|
98
|
+
const first = this.seenMessageIds.values().next().value;
|
|
99
|
+
if (first)
|
|
100
|
+
this.seenMessageIds.delete(first);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
// Auto-decrypt session messages
|
|
104
|
+
if (data.type === "session.message.new" && data.encryptedContent && this.crypto) {
|
|
105
|
+
const plain = await this.crypto.decryptSessionMessage(data.encryptedContent);
|
|
106
|
+
if (plain !== null)
|
|
107
|
+
data.content = plain;
|
|
108
|
+
}
|
|
109
|
+
this.emit(data.type, data);
|
|
88
110
|
}
|
|
89
|
-
|
|
90
|
-
}
|
|
91
|
-
catch { /* ignore malformed */ }
|
|
111
|
+
catch { /* ignore malformed */ }
|
|
112
|
+
})();
|
|
92
113
|
};
|
|
93
114
|
this.ws.onclose = () => {
|
|
94
115
|
if (!this.closed && (this.options.autoReconnect ?? true)) {
|
|
@@ -151,11 +172,6 @@ export class HiloopWsClient {
|
|
|
151
172
|
off(eventType) {
|
|
152
173
|
this.handlers.delete(eventType);
|
|
153
174
|
}
|
|
154
|
-
/** Auto-decrypt a session message's encryptedContent field. */
|
|
155
|
-
async decryptSessionMessage(encryptedContent) {
|
|
156
|
-
await this.ensureCrypto();
|
|
157
|
-
return this.crypto.decryptSessionMessage(encryptedContent);
|
|
158
|
-
}
|
|
159
175
|
// -- Private ----------------------------------------------------------------
|
|
160
176
|
send(data) {
|
|
161
177
|
if (this.ws?.readyState === WebSocket.OPEN) {
|