@clawroom/sdk 0.5.25 → 0.5.27
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/client.d.ts +87 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +375 -0
- package/dist/client.js.map +1 -0
- package/{src/index.ts → dist/index.d.ts} +2 -16
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/machine-client.d.ts +78 -0
- package/dist/machine-client.d.ts.map +1 -0
- package/dist/machine-client.js +297 -0
- package/dist/machine-client.js.map +1 -0
- package/dist/protocol.d.ts +3 -0
- package/dist/protocol.d.ts.map +1 -0
- package/dist/protocol.js +2 -0
- package/dist/protocol.js.map +1 -0
- package/dist/ws-transport.d.ts +48 -0
- package/dist/ws-transport.d.ts.map +1 -0
- package/dist/ws-transport.js +188 -0
- package/dist/ws-transport.js.map +1 -0
- package/package.json +20 -5
- package/src/client.ts +0 -430
- package/src/machine-client.ts +0 -356
- package/src/protocol.ts +0 -22
- package/src/ws-transport.ts +0 -218
package/src/machine-client.ts
DELETED
|
@@ -1,356 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ClawRoom Machine Client — machine-level auth with multi-agent support.
|
|
3
|
-
* WebSocket primary for real-time push, HTTP polling as fallback.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import * as os from "node:os";
|
|
7
|
-
import type { AgentChatProfile, AgentWorkRef, ServerChatMessage, ServerTask } from "./protocol.js";
|
|
8
|
-
import { WsTransport } from "./ws-transport.js";
|
|
9
|
-
|
|
10
|
-
export type ClawroomMachineClientOptions = {
|
|
11
|
-
endpoint?: string;
|
|
12
|
-
apiKey: string;
|
|
13
|
-
hostname?: string;
|
|
14
|
-
capabilities?: string[];
|
|
15
|
-
wsUrl?: string;
|
|
16
|
-
log?: {
|
|
17
|
-
info?: (message: string, ...args: unknown[]) => void;
|
|
18
|
-
warn?: (message: string, ...args: unknown[]) => void;
|
|
19
|
-
error?: (message: string, ...args: unknown[]) => void;
|
|
20
|
-
};
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export type AgentReflectionPayload = {
|
|
24
|
-
scope: "chat" | "task";
|
|
25
|
-
status: string;
|
|
26
|
-
summary: string;
|
|
27
|
-
channelId?: string | null;
|
|
28
|
-
taskId?: string | null;
|
|
29
|
-
messageId?: string | null;
|
|
30
|
-
toolsUsed?: string[];
|
|
31
|
-
responseExcerpt?: string | null;
|
|
32
|
-
detail?: Record<string, unknown>;
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
type AgentWork = {
|
|
36
|
-
agentId: string;
|
|
37
|
-
agentName: string;
|
|
38
|
-
task: ServerTask | null;
|
|
39
|
-
chat: ServerChatMessage[] | null;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
const DEFAULT_AGENT_CHAT_PROFILE: AgentChatProfile = {
|
|
43
|
-
role: "No role defined",
|
|
44
|
-
systemPrompt: "No system prompt configured",
|
|
45
|
-
memory: "No memory recorded yet",
|
|
46
|
-
continuityPacket: "No continuity packet available yet",
|
|
47
|
-
};
|
|
48
|
-
const MAX_RECENT_CHAT_IDS = 1000;
|
|
49
|
-
|
|
50
|
-
type MachineHeartbeatResponse = {
|
|
51
|
-
machineId: string;
|
|
52
|
-
agents?: Array<{ id: string }>;
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
type MachinePollResponse = {
|
|
56
|
-
machineId: string;
|
|
57
|
-
agents: AgentWork[];
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
export class ClawroomMachineClient {
|
|
61
|
-
private options: ClawroomMachineClientOptions;
|
|
62
|
-
private heartbeatTimer: ReturnType<typeof setInterval> | null = null;
|
|
63
|
-
private pollTimer: ReturnType<typeof setInterval> | null = null;
|
|
64
|
-
private heartbeatInFlight = false;
|
|
65
|
-
private pollInFlight = false;
|
|
66
|
-
private taskHandler: ((agentId: string, task: AgentWork["task"]) => void) | null = null;
|
|
67
|
-
private chatHandler: ((agentId: string, messages: NonNullable<AgentWork["chat"]>) => void) | null = null;
|
|
68
|
-
private connectHandler: ((machineId: string) => void) | null = null;
|
|
69
|
-
private disconnectHandler: (() => void) | null = null;
|
|
70
|
-
private _connected = false;
|
|
71
|
-
private _stopped = false;
|
|
72
|
-
private wsTransport: WsTransport | null = null;
|
|
73
|
-
private recentChatIds = new Map<string, number>();
|
|
74
|
-
|
|
75
|
-
constructor(options: ClawroomMachineClientOptions) {
|
|
76
|
-
this.options = options;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
private get baseUrl(): string {
|
|
80
|
-
return this.options.endpoint ?? "http://localhost:3000/api/machines";
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
private get wsUrl(): string {
|
|
84
|
-
if (this.options.wsUrl) return this.options.wsUrl;
|
|
85
|
-
return this.baseUrl.replace(/\/api\/machines$/, "").replace(/^http/, "ws") + "/api/ws";
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
private async httpRequest(method: string, path: string, body?: unknown): Promise<unknown> {
|
|
89
|
-
const url = `${this.baseUrl}${path}`;
|
|
90
|
-
const res = await fetch(url, {
|
|
91
|
-
method,
|
|
92
|
-
headers: { "Content-Type": "application/json", Authorization: `Bearer ${this.options.apiKey}` },
|
|
93
|
-
body: body ? JSON.stringify(body) : undefined,
|
|
94
|
-
});
|
|
95
|
-
if (!res.ok) throw new Error(`HTTP ${res.status}: ${await res.text()}`);
|
|
96
|
-
const text = await res.text();
|
|
97
|
-
if (!text.trim()) return null;
|
|
98
|
-
try {
|
|
99
|
-
return JSON.parse(text) as unknown;
|
|
100
|
-
} catch (error) {
|
|
101
|
-
throw new Error(
|
|
102
|
-
`Invalid JSON from ${path}: ${error instanceof Error ? error.message : "unknown parse error"}`,
|
|
103
|
-
);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
onAgentTask(handler: (agentId: string, task: AgentWork["task"]) => void) { this.taskHandler = handler; }
|
|
108
|
-
onAgentChat(handler: (agentId: string, messages: NonNullable<AgentWork["chat"]>) => void) { this.chatHandler = handler; }
|
|
109
|
-
onConnected(handler: (machineId: string) => void) { this.connectHandler = handler; }
|
|
110
|
-
onDisconnected(handler: () => void) { this.disconnectHandler = handler; }
|
|
111
|
-
|
|
112
|
-
async sendAgentComplete(agentId: string, taskId: string, output: string, attachments?: Array<{ filename: string; mimeType: string; data: string }>, workRef?: AgentWorkRef) {
|
|
113
|
-
await this.httpRequest("POST", "/complete", { agentId, taskId, output, attachments, workRef });
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
async sendAgentFail(agentId: string, taskId: string, reason: string, workRef?: AgentWorkRef) {
|
|
117
|
-
await this.httpRequest("POST", "/fail", { agentId, taskId, reason, workRef });
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
async sendAgentChatReply(agentId: string, channelId: string, content: string, replyTo?: string, workRefs?: AgentWorkRef[]) {
|
|
121
|
-
await this.httpRequest("POST", "/chat-reply", { agentId, channelId, content, replyTo, workRefs });
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
async sendAgentTyping(agentId: string, channelId: string) {
|
|
125
|
-
await this.httpRequest("POST", "/typing", { agentId, channelId });
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
async sendAgentReflection(agentId: string, reflection: AgentReflectionPayload) {
|
|
129
|
-
await this.httpRequest("POST", "/reflections", { agentId, ...reflection });
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
get connected() { return this._connected; }
|
|
133
|
-
get stopped() { return this._stopped; }
|
|
134
|
-
|
|
135
|
-
private rememberChat(messageId?: string | null): boolean {
|
|
136
|
-
if (!messageId) return false;
|
|
137
|
-
const now = Date.now();
|
|
138
|
-
for (const [id, seenAt] of this.recentChatIds) {
|
|
139
|
-
if (now - seenAt > 10 * 60_000) this.recentChatIds.delete(id);
|
|
140
|
-
}
|
|
141
|
-
if (this.recentChatIds.has(messageId)) return false;
|
|
142
|
-
if (this.recentChatIds.size >= MAX_RECENT_CHAT_IDS) {
|
|
143
|
-
const oldestId = this.recentChatIds.keys().next().value;
|
|
144
|
-
if (oldestId) this.recentChatIds.delete(oldestId);
|
|
145
|
-
}
|
|
146
|
-
this.recentChatIds.set(messageId, now);
|
|
147
|
-
return true;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
private markConnected(machineId: string) {
|
|
151
|
-
if (!this._connected) {
|
|
152
|
-
this._connected = true;
|
|
153
|
-
this.options.log?.info?.("[machine] connected");
|
|
154
|
-
this.connectHandler?.(machineId);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
private markDisconnected() {
|
|
159
|
-
if (this._connected) {
|
|
160
|
-
this._connected = false;
|
|
161
|
-
this.options.log?.warn?.("[machine] disconnected");
|
|
162
|
-
this.disconnectHandler?.();
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
private async pollOnce() {
|
|
167
|
-
if (!this._connected || this.pollInFlight) return;
|
|
168
|
-
this.pollInFlight = true;
|
|
169
|
-
try {
|
|
170
|
-
const result = (await this.httpRequest("POST", "/poll", {})) as MachinePollResponse;
|
|
171
|
-
for (const agent of result.agents) {
|
|
172
|
-
if (agent.task && this.taskHandler) {
|
|
173
|
-
try {
|
|
174
|
-
this.taskHandler(agent.agentId, agent.task);
|
|
175
|
-
} catch (err) {
|
|
176
|
-
this.options.log?.warn?.(
|
|
177
|
-
`[machine] task dispatch failed for agent ${agent.agentId} task ${agent.task.taskId}: ${err}`,
|
|
178
|
-
);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
if (agent.chat && agent.chat.length > 0 && this.chatHandler) {
|
|
182
|
-
try {
|
|
183
|
-
const freshMessages = agent.chat.filter((message) => this.rememberChat(message.workId ?? message.messageId));
|
|
184
|
-
if (freshMessages.length > 0) {
|
|
185
|
-
this.chatHandler(agent.agentId, freshMessages);
|
|
186
|
-
}
|
|
187
|
-
} catch (err) {
|
|
188
|
-
this.options.log?.warn?.(`[machine] chat dispatch failed for agent ${agent.agentId}: ${err}`);
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
} catch (err) {
|
|
193
|
-
this.options.log?.warn?.(`[machine] poll error: ${err}`);
|
|
194
|
-
} finally {
|
|
195
|
-
this.pollInFlight = false;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
connect() {
|
|
200
|
-
this.stopHeartbeat();
|
|
201
|
-
this.stopPolling();
|
|
202
|
-
this.wsTransport?.disconnect();
|
|
203
|
-
this.wsTransport = null;
|
|
204
|
-
this._stopped = false;
|
|
205
|
-
const hostname = this.options.hostname ?? os.hostname();
|
|
206
|
-
const hbBody = {
|
|
207
|
-
hostname,
|
|
208
|
-
capabilities: this.options.capabilities,
|
|
209
|
-
platform: os.platform(),
|
|
210
|
-
architecture: os.arch(),
|
|
211
|
-
osRelease: os.release(),
|
|
212
|
-
};
|
|
213
|
-
const sendHeartbeat = async () => {
|
|
214
|
-
if (this.heartbeatInFlight) return;
|
|
215
|
-
this.heartbeatInFlight = true;
|
|
216
|
-
try {
|
|
217
|
-
const result = (await this.httpRequest("POST", "/heartbeat", hbBody)) as MachineHeartbeatResponse;
|
|
218
|
-
this.markConnected(result.machineId);
|
|
219
|
-
} catch (err) {
|
|
220
|
-
if (!this.wsTransport?.connected) {
|
|
221
|
-
this.markDisconnected();
|
|
222
|
-
} else {
|
|
223
|
-
this.options.log?.warn?.(`[machine] heartbeat failed while WS is still connected: ${err}`);
|
|
224
|
-
}
|
|
225
|
-
throw err;
|
|
226
|
-
} finally {
|
|
227
|
-
this.heartbeatInFlight = false;
|
|
228
|
-
}
|
|
229
|
-
};
|
|
230
|
-
|
|
231
|
-
// Heartbeat every 30s (always HTTP)
|
|
232
|
-
this.heartbeatTimer = setInterval(() => {
|
|
233
|
-
void sendHeartbeat().catch(() => {});
|
|
234
|
-
}, 30_000);
|
|
235
|
-
|
|
236
|
-
sendHeartbeat().catch((err) => this.options.log?.warn?.(`[machine] initial heartbeat failed: ${err}`));
|
|
237
|
-
|
|
238
|
-
// WebSocket transport
|
|
239
|
-
this.wsTransport = new WsTransport({
|
|
240
|
-
url: this.wsUrl,
|
|
241
|
-
token: this.options.apiKey,
|
|
242
|
-
log: { info: (m) => this.options.log?.info?.(m), warn: (m) => this.options.log?.warn?.(m) },
|
|
243
|
-
onConnected: () => {
|
|
244
|
-
this.options.log?.info?.("[machine] WebSocket connected");
|
|
245
|
-
},
|
|
246
|
-
onDisconnected: () => {
|
|
247
|
-
this.options.log?.info?.("[machine] WebSocket disconnected, HTTP polling active");
|
|
248
|
-
},
|
|
249
|
-
onMessage: (msg) => {
|
|
250
|
-
const message = getRecord(msg.message);
|
|
251
|
-
if (msg.type === "message") {
|
|
252
|
-
const targetAgentId = getOptionalString(msg.agentId) ?? getOptionalString(message?.agentId);
|
|
253
|
-
const messageId = getString(message?.id, getString(msg.messageId));
|
|
254
|
-
if (message && targetAgentId && this.chatHandler && this.rememberChat(messageId)) {
|
|
255
|
-
const agentProfile = resolveAgentChatProfile(msg.agentProfile, message?.agentProfile);
|
|
256
|
-
const messages = [{
|
|
257
|
-
workId: getString(message?.workId),
|
|
258
|
-
leaseToken: getString(message?.leaseToken),
|
|
259
|
-
messageId,
|
|
260
|
-
channelId: getString(message?.channelId),
|
|
261
|
-
content: getString(message?.content),
|
|
262
|
-
attachments: getAttachments(msg.attachments) ?? getAttachments(message?.attachments),
|
|
263
|
-
isMention: typeof msg.isMention === "boolean" ? msg.isMention : false,
|
|
264
|
-
wakeReason: getWakeReason(msg.wakeReason) ?? getWakeReason(message?.wakeReason),
|
|
265
|
-
triggerReason: getOptionalString(msg.triggerReason) ?? getOptionalString(message?.triggerReason),
|
|
266
|
-
context: getContext(msg.context),
|
|
267
|
-
agentProfile,
|
|
268
|
-
}];
|
|
269
|
-
this.chatHandler(targetAgentId, messages);
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
const taskAgentId = getOptionalString(msg.agentId);
|
|
273
|
-
if (msg.type === "task" && taskAgentId && this.taskHandler) {
|
|
274
|
-
this.taskHandler(taskAgentId, (msg.task as AgentWork["task"] | undefined) ?? null);
|
|
275
|
-
}
|
|
276
|
-
},
|
|
277
|
-
});
|
|
278
|
-
this.wsTransport.connect();
|
|
279
|
-
|
|
280
|
-
// Keep HTTP polling active even when WS is up.
|
|
281
|
-
// Machine WS chat delivery is best-effort; polling is the durable delivery path.
|
|
282
|
-
this.pollTimer = setInterval(async () => {
|
|
283
|
-
await this.pollOnce();
|
|
284
|
-
}, 10_000);
|
|
285
|
-
|
|
286
|
-
void this.pollOnce();
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
disconnect() {
|
|
290
|
-
this._stopped = true;
|
|
291
|
-
this._connected = false;
|
|
292
|
-
this.stopHeartbeat();
|
|
293
|
-
this.stopPolling();
|
|
294
|
-
this.wsTransport?.disconnect();
|
|
295
|
-
this.wsTransport = null;
|
|
296
|
-
this.recentChatIds.clear();
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
private stopHeartbeat() {
|
|
300
|
-
if (this.heartbeatTimer) {
|
|
301
|
-
clearInterval(this.heartbeatTimer);
|
|
302
|
-
this.heartbeatTimer = null;
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
private stopPolling() {
|
|
307
|
-
if (this.pollTimer) {
|
|
308
|
-
clearInterval(this.pollTimer);
|
|
309
|
-
this.pollTimer = null;
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
function getRecord(value: unknown): Record<string, unknown> | null {
|
|
315
|
-
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
|
316
|
-
return value as Record<string, unknown>;
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
function getString(value: unknown, fallback = ""): string {
|
|
320
|
-
return typeof value === "string" ? value : fallback;
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
function getOptionalString(value: unknown): string | undefined {
|
|
324
|
-
return typeof value === "string" ? value : undefined;
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
function getAttachments(value: unknown): ServerChatMessage["attachments"] | undefined {
|
|
328
|
-
return Array.isArray(value) ? value as ServerChatMessage["attachments"] : undefined;
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
function getContext(value: unknown): ServerChatMessage["context"] {
|
|
332
|
-
return Array.isArray(value) ? value as ServerChatMessage["context"] : [];
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
function getWakeReason(value: unknown): ServerChatMessage["wakeReason"] | undefined {
|
|
336
|
-
return typeof value === "string" ? value as ServerChatMessage["wakeReason"] : undefined;
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
function getAgentChatProfile(value: unknown): AgentChatProfile | null {
|
|
340
|
-
const record = getRecord(value);
|
|
341
|
-
if (!record) return null;
|
|
342
|
-
return {
|
|
343
|
-
role: getString(record.role, DEFAULT_AGENT_CHAT_PROFILE.role),
|
|
344
|
-
systemPrompt: getString(record.systemPrompt, DEFAULT_AGENT_CHAT_PROFILE.systemPrompt),
|
|
345
|
-
memory: getString(record.memory, DEFAULT_AGENT_CHAT_PROFILE.memory),
|
|
346
|
-
continuityPacket: getString(record.continuityPacket, DEFAULT_AGENT_CHAT_PROFILE.continuityPacket),
|
|
347
|
-
};
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
function resolveAgentChatProfile(...values: unknown[]): AgentChatProfile {
|
|
351
|
-
for (const value of values) {
|
|
352
|
-
const profile = getAgentChatProfile(value);
|
|
353
|
-
if (profile) return profile;
|
|
354
|
-
}
|
|
355
|
-
return DEFAULT_AGENT_CHAT_PROFILE;
|
|
356
|
-
}
|
package/src/protocol.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
export type {
|
|
2
|
-
AgentChatProfile,
|
|
3
|
-
ChatAttachmentRef,
|
|
4
|
-
AgentHeartbeat,
|
|
5
|
-
AgentResultFile,
|
|
6
|
-
AgentComplete,
|
|
7
|
-
AgentProgress,
|
|
8
|
-
AgentFail,
|
|
9
|
-
AgentChatReply,
|
|
10
|
-
AgentTyping,
|
|
11
|
-
AgentMessage,
|
|
12
|
-
ServerTask,
|
|
13
|
-
ServerChatMessage,
|
|
14
|
-
ServerMessage,
|
|
15
|
-
RuntimeDef,
|
|
16
|
-
} from "@clawroom/protocol";
|
|
17
|
-
|
|
18
|
-
export {
|
|
19
|
-
RUNTIMES,
|
|
20
|
-
BRIDGE_MANAGED_RUNTIME_IDS,
|
|
21
|
-
RUNTIME_MAP,
|
|
22
|
-
} from "@clawroom/protocol";
|
package/src/ws-transport.ts
DELETED
|
@@ -1,218 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shared WebSocket transport with auto-reconnect and fallback support.
|
|
3
|
-
* Used by both ClawroomClient and ClawroomMachineClient.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
export type WsLike = {
|
|
7
|
-
readyState: number;
|
|
8
|
-
on: (event: string, listener: (...args: unknown[]) => void) => void;
|
|
9
|
-
removeAllListeners?: (event?: string) => void;
|
|
10
|
-
send: (data: string) => void;
|
|
11
|
-
close: () => void;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
export type WsTransportOptions = {
|
|
15
|
-
url: string;
|
|
16
|
-
token: string;
|
|
17
|
-
onMessage: (msg: Record<string, unknown>) => void;
|
|
18
|
-
onConnected: () => void;
|
|
19
|
-
onDisconnected: () => void;
|
|
20
|
-
createSocket?: (url: string) => WsLike | Promise<WsLike>;
|
|
21
|
-
log?: {
|
|
22
|
-
info?: (message: string) => void;
|
|
23
|
-
warn?: (message: string) => void;
|
|
24
|
-
};
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
type WsModule = {
|
|
28
|
-
default: new (url: string) => WsLike;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
export class WsTransport {
|
|
32
|
-
private static readonly MAX_SEND_QUEUE = 500;
|
|
33
|
-
private ws: WsLike | null = null;
|
|
34
|
-
private options: WsTransportOptions;
|
|
35
|
-
private _connected = false;
|
|
36
|
-
private stopped = false;
|
|
37
|
-
private reconnectTimer: ReturnType<typeof setTimeout> | null = null;
|
|
38
|
-
private reconnectDelay = 1000;
|
|
39
|
-
private failCount = 0;
|
|
40
|
-
private sendQueue: string[] = [];
|
|
41
|
-
|
|
42
|
-
static readonly MAX_RECONNECT_DELAY = 60_000;
|
|
43
|
-
static readonly MAX_RECONNECT_FAILS = 10;
|
|
44
|
-
|
|
45
|
-
constructor(options: WsTransportOptions) {
|
|
46
|
-
this.options = options;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
get connected() { return this._connected; }
|
|
50
|
-
|
|
51
|
-
async connect() {
|
|
52
|
-
this.stopped = false;
|
|
53
|
-
if (this.reconnectTimer) {
|
|
54
|
-
clearTimeout(this.reconnectTimer);
|
|
55
|
-
this.reconnectTimer = null;
|
|
56
|
-
}
|
|
57
|
-
if (this.ws && (this.ws.readyState === 0 || this.ws.readyState === 1)) return;
|
|
58
|
-
|
|
59
|
-
if (this.ws) {
|
|
60
|
-
const staleSocket = this.ws;
|
|
61
|
-
this.ws = null;
|
|
62
|
-
this.closeSocket(staleSocket);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
try {
|
|
66
|
-
this.ws = await this.createSocket();
|
|
67
|
-
if (!this.ws) return;
|
|
68
|
-
} catch {
|
|
69
|
-
this._connected = false;
|
|
70
|
-
this.scheduleReconnect();
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const socket = this.ws;
|
|
75
|
-
|
|
76
|
-
socket.on("open", () => {
|
|
77
|
-
this.reconnectDelay = 1000;
|
|
78
|
-
socket.send(JSON.stringify({ type: "auth", token: this.options.token }));
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
socket.on("message", (raw) => {
|
|
82
|
-
if (!raw || typeof raw !== "object" || !("toString" in raw) || typeof raw.toString !== "function") return;
|
|
83
|
-
let msg: Record<string, unknown>;
|
|
84
|
-
try { msg = JSON.parse(raw.toString()) as Record<string, unknown>; } catch { return; }
|
|
85
|
-
|
|
86
|
-
if (msg.type === "auth_ok") {
|
|
87
|
-
this._connected = true;
|
|
88
|
-
this.failCount = 0;
|
|
89
|
-
this.flushSendQueue(socket);
|
|
90
|
-
this.options.onConnected();
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
if (msg.type === "auth_error") {
|
|
95
|
-
this._connected = false;
|
|
96
|
-
this.options.log?.warn?.(`WS auth failed: ${msg.error ?? "unknown"}`);
|
|
97
|
-
socket.close();
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
if (msg.type === "pong") return;
|
|
102
|
-
|
|
103
|
-
this.options.onMessage(msg);
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
socket.on("close", () => {
|
|
107
|
-
this.handleDisconnect(socket);
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
socket.on("error", () => {
|
|
111
|
-
this.handleDisconnect(socket);
|
|
112
|
-
try {
|
|
113
|
-
if (socket.readyState === 0 || socket.readyState === 1) socket.close();
|
|
114
|
-
} catch {
|
|
115
|
-
/* ignore close errors */
|
|
116
|
-
}
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
send(msg: Record<string, unknown>): boolean {
|
|
121
|
-
const payload = JSON.stringify(msg);
|
|
122
|
-
if (this._connected && this.ws?.readyState === 1) {
|
|
123
|
-
this.ws.send(payload);
|
|
124
|
-
return true;
|
|
125
|
-
}
|
|
126
|
-
if (!this.stopped) {
|
|
127
|
-
if (!this.sendQueue.includes(payload)) {
|
|
128
|
-
if (this.sendQueue.length >= WsTransport.MAX_SEND_QUEUE) {
|
|
129
|
-
this.sendQueue.shift();
|
|
130
|
-
this.options.log?.warn?.(`WS send queue full; dropping oldest queued message`);
|
|
131
|
-
}
|
|
132
|
-
this.sendQueue.push(payload);
|
|
133
|
-
}
|
|
134
|
-
this.options.log?.warn?.("WS send queued until the socket is ready");
|
|
135
|
-
}
|
|
136
|
-
return false;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
disconnect() {
|
|
140
|
-
this.stopped = true;
|
|
141
|
-
this._connected = false;
|
|
142
|
-
if (this.reconnectTimer) { clearTimeout(this.reconnectTimer); this.reconnectTimer = null; }
|
|
143
|
-
this.resetReconnectState();
|
|
144
|
-
this.sendQueue = [];
|
|
145
|
-
if (this.ws) {
|
|
146
|
-
const socket = this.ws;
|
|
147
|
-
this.ws = null;
|
|
148
|
-
this.closeSocket(socket);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
private handleDisconnect(socket: WsLike) {
|
|
153
|
-
if (this.ws !== socket) return;
|
|
154
|
-
this.ws = null;
|
|
155
|
-
const wasConnected = this._connected;
|
|
156
|
-
this._connected = false;
|
|
157
|
-
if (wasConnected) this.options.onDisconnected();
|
|
158
|
-
if (!this.stopped) this.scheduleReconnect();
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
private scheduleReconnect() {
|
|
162
|
-
if (this.stopped || this.reconnectTimer) return;
|
|
163
|
-
|
|
164
|
-
this.failCount++;
|
|
165
|
-
const failedAttempts = this.failCount;
|
|
166
|
-
if (failedAttempts > WsTransport.MAX_RECONNECT_FAILS) {
|
|
167
|
-
this._connected = false;
|
|
168
|
-
this.resetReconnectState();
|
|
169
|
-
this.options.log?.warn?.(`WS reconnect failed ${failedAttempts} times, giving up. HTTP polling active.`);
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
const jitter = Math.random() * 3000;
|
|
174
|
-
this.reconnectTimer = setTimeout(() => {
|
|
175
|
-
this.reconnectTimer = null;
|
|
176
|
-
this.connect();
|
|
177
|
-
}, Math.min(this.reconnectDelay + jitter, WsTransport.MAX_RECONNECT_DELAY));
|
|
178
|
-
this.reconnectDelay = Math.min(this.reconnectDelay * 2, WsTransport.MAX_RECONNECT_DELAY);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
private async createSocket(): Promise<WsLike | null> {
|
|
182
|
-
if (this.options.createSocket) {
|
|
183
|
-
return await this.options.createSocket(this.options.url);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
let WebSocket: WsModule["default"];
|
|
187
|
-
try {
|
|
188
|
-
WebSocket = (await import("ws") as WsModule).default;
|
|
189
|
-
} catch {
|
|
190
|
-
this.options.log?.info?.("ws module not available, skipping WebSocket");
|
|
191
|
-
return null;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
return new WebSocket(this.options.url);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
private closeSocket(socket: WsLike) {
|
|
198
|
-
try {
|
|
199
|
-
socket.removeAllListeners?.();
|
|
200
|
-
socket.close();
|
|
201
|
-
} catch {
|
|
202
|
-
/* ignore close errors */
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
private resetReconnectState() {
|
|
207
|
-
this.reconnectDelay = 1000;
|
|
208
|
-
this.failCount = 0;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
private flushSendQueue(socket: WsLike) {
|
|
212
|
-
while (this.ws === socket && this._connected && socket.readyState === 1 && this.sendQueue.length > 0) {
|
|
213
|
-
const payload = this.sendQueue.shift();
|
|
214
|
-
if (!payload) continue;
|
|
215
|
-
socket.send(payload);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
}
|