-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathGatewayClient.ts
More file actions
553 lines (505 loc) · 16.8 KB
/
Copy pathGatewayClient.ts
File metadata and controls
553 lines (505 loc) · 16.8 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
import NodeWebSocket from "ws";
import type { OpenclawDeviceIdentity } from "./deviceAuth.ts";
import { signOpenclawDeviceChallenge } from "./deviceAuth.ts";
import {
assertRequiredMethods,
extractHelloMethods,
extractHelloPayload,
formatGatewayError,
OPENCLAW_OPERATOR_SCOPES,
OPENCLAW_PROTOCOL_VERSION,
parseGatewayError,
parseGatewayFrame,
readString,
type GatewayFrame,
type OpenclawHelloAuth,
type OpenclawHelloPayload,
type ParsedGatewayError,
} from "./protocol.ts";
const WS_CONNECT_TIMEOUT_MS = 10_000;
const REQUEST_TIMEOUT_MS = 30_000;
export interface OpenclawGatewayClientOptions {
readonly url: string;
readonly identity: OpenclawDeviceIdentity;
readonly sharedSecret?: string;
readonly deviceToken?: string;
readonly deviceTokenRole?: string;
readonly deviceTokenScopes?: ReadonlyArray<string>;
readonly clientId: string;
readonly clientVersion: string;
readonly clientPlatform: string;
readonly clientMode: string;
readonly locale: string;
readonly userAgent: string;
readonly role?: string;
readonly scopes?: ReadonlyArray<string>;
readonly requiredMethods?: ReadonlyArray<string>;
}
export interface OpenclawGatewayConnectResult {
readonly hello: OpenclawHelloPayload | undefined;
readonly auth: OpenclawHelloAuth | undefined;
readonly methods: Set<string>;
readonly usedStoredDeviceToken: boolean;
}
interface PendingRequest {
readonly method: string;
readonly resolve: (payload: unknown) => void;
readonly reject: (error: unknown) => void;
readonly timeout: ReturnType<typeof setTimeout>;
}
interface PendingEventWaiter {
readonly eventName: string;
readonly resolve: (payload: Record<string, unknown> | undefined) => void;
readonly reject: (error: unknown) => void;
readonly timeout: ReturnType<typeof setTimeout>;
}
export class OpenclawGatewayClientError extends Error {
readonly gatewayError: ParsedGatewayError | undefined;
readonly socketCloseCode: number | undefined;
readonly socketCloseReason: string | undefined;
constructor(
message: string,
options?: {
readonly gatewayError?: ParsedGatewayError;
readonly socketCloseCode?: number;
readonly socketCloseReason?: string;
readonly cause?: unknown;
},
) {
super(message, options?.cause !== undefined ? { cause: options.cause } : undefined);
this.name = "OpenclawGatewayClientError";
this.gatewayError = options?.gatewayError;
this.socketCloseCode = options?.socketCloseCode;
this.socketCloseReason = options?.socketCloseReason;
}
}
function uniqueScopes(scopes: ReadonlyArray<string> | undefined): string[] {
const values = new Set<string>();
for (const scope of scopes ?? []) {
const trimmed = scope.trim();
if (trimmed.length > 0) {
values.add(trimmed);
}
}
return [...values];
}
function closeDetail(code: number | undefined, reason: string | undefined): string {
if (code === undefined) {
return "";
}
return reason && reason.length > 0 ? ` (code ${code}: ${reason})` : ` (code ${code})`;
}
function clientErrorOptions(input: {
readonly gatewayError: ParsedGatewayError | undefined;
readonly socketCloseCode: number | undefined;
readonly socketCloseReason: string | undefined;
readonly cause: unknown;
}) {
return {
...(input.gatewayError !== undefined ? { gatewayError: input.gatewayError } : {}),
...(input.socketCloseCode !== undefined ? { socketCloseCode: input.socketCloseCode } : {}),
...(input.socketCloseReason !== undefined
? { socketCloseReason: input.socketCloseReason }
: {}),
...(input.cause !== undefined ? { cause: input.cause } : {}),
};
}
export class OpenclawGatewayClient {
static async connect(options: OpenclawGatewayClientOptions): Promise<{
client: OpenclawGatewayClient;
connect: OpenclawGatewayConnectResult;
}> {
const client = new OpenclawGatewayClient(options);
try {
const connectResult = await client.connectInternal();
return { client, connect: connectResult };
} catch (error) {
await client.close();
throw error;
}
}
private readonly options: OpenclawGatewayClientOptions;
private ws: NodeWebSocket | null = null;
private nextRequestId = 1;
private closed = false;
private closeCode: number | undefined = undefined;
private closeReason: string | undefined = undefined;
private readonly pendingRequests = new Map<string, PendingRequest>();
private readonly pendingEventWaiters = new Set<PendingEventWaiter>();
private readonly bufferedEvents: GatewayFrame[] = [];
private readonly eventListeners = new Set<(event: GatewayFrame) => void>();
private readonly closeListeners = new Set<(error?: OpenclawGatewayClientError) => void>();
readonly methods = new Set<string>();
hello: OpenclawHelloPayload | undefined = undefined;
auth: OpenclawHelloAuth | undefined = undefined;
private constructor(options: OpenclawGatewayClientOptions) {
this.options = options;
}
onEvent(listener: (event: GatewayFrame) => void): () => void {
this.eventListeners.add(listener);
return () => {
this.eventListeners.delete(listener);
};
}
onClose(listener: (error?: OpenclawGatewayClientError) => void): () => void {
this.closeListeners.add(listener);
return () => {
this.closeListeners.delete(listener);
};
}
async request(method: string, params?: Record<string, unknown>, timeoutMs = REQUEST_TIMEOUT_MS) {
const socket = this.ws;
if (!socket || socket.readyState !== NodeWebSocket.OPEN) {
throw new OpenclawGatewayClientError(`WebSocket is not open for request '${method}'.`, {
...clientErrorOptions({
gatewayError: undefined,
socketCloseCode: this.closeCode,
socketCloseReason: this.closeReason,
cause: undefined,
}),
});
}
const id = String(this.nextRequestId++);
const payload = JSON.stringify({
type: "req",
id,
method,
...(params !== undefined ? { params } : {}),
});
return await new Promise<unknown>((resolve, reject) => {
const timeout = setTimeout(() => {
this.pendingRequests.delete(id);
reject(
new OpenclawGatewayClientError(
`Gateway request '${method}' timed out after ${timeoutMs}ms.`,
clientErrorOptions({
gatewayError: undefined,
socketCloseCode: this.closeCode,
socketCloseReason: this.closeReason,
cause: undefined,
}),
),
);
}, timeoutMs);
this.pendingRequests.set(id, {
method,
resolve,
reject,
timeout,
});
try {
socket.send(payload);
} catch (cause) {
clearTimeout(timeout);
this.pendingRequests.delete(id);
reject(
new OpenclawGatewayClientError(`Failed to send gateway request '${method}'.`, {
...clientErrorOptions({
gatewayError: undefined,
cause,
socketCloseCode: this.closeCode,
socketCloseReason: this.closeReason,
}),
}),
);
}
});
}
async waitForEvent(eventName: string, timeoutMs = REQUEST_TIMEOUT_MS) {
const bufferedIndex = this.bufferedEvents.findIndex(
(event) => event.type === "event" && event.event === eventName,
);
if (bufferedIndex >= 0) {
const [event] = this.bufferedEvents.splice(bufferedIndex, 1);
if (event) {
return this.framePayload(event);
}
}
return await new Promise<Record<string, unknown> | undefined>((resolve, reject) => {
const timeout = setTimeout(() => {
this.pendingEventWaiters.delete(waiter);
reject(
new OpenclawGatewayClientError(
`Gateway event '${eventName}' timed out after ${timeoutMs}ms.`,
clientErrorOptions({
gatewayError: undefined,
socketCloseCode: this.closeCode,
socketCloseReason: this.closeReason,
cause: undefined,
}),
),
);
}, timeoutMs);
const waiter: PendingEventWaiter = {
eventName,
resolve: (payload) => {
clearTimeout(timeout);
resolve(payload);
},
reject: (error) => {
clearTimeout(timeout);
reject(error);
},
timeout,
};
this.pendingEventWaiters.add(waiter);
});
}
async close(): Promise<void> {
this.closed = true;
const socket = this.ws;
this.ws = null;
if (!socket) {
return;
}
if (socket.readyState === NodeWebSocket.CLOSED || socket.readyState === NodeWebSocket.CLOSING) {
return;
}
await new Promise<void>((resolve) => {
socket.once("close", () => resolve());
socket.close();
});
}
private async connectInternal(): Promise<OpenclawGatewayConnectResult> {
const canUseStoredDeviceToken =
typeof this.options.deviceToken === "string" && this.options.deviceToken.length > 0;
try {
return await this.performConnectAttempt("shared");
} catch (error) {
const parsedError =
error instanceof OpenclawGatewayClientError ? error.gatewayError : undefined;
const shouldRetryWithDeviceToken =
canUseStoredDeviceToken &&
parsedError?.canRetryWithDeviceToken === true &&
this.options.sharedSecret !== undefined;
if (!shouldRetryWithDeviceToken) {
throw error;
}
await this.closeCurrentSocket();
return await this.performConnectAttempt("deviceToken");
}
}
private async performConnectAttempt(
authMode: "shared" | "deviceToken",
): Promise<OpenclawGatewayConnectResult> {
await this.openSocket();
const challenge = await this.waitForEvent("connect.challenge");
const nonce = readString(challenge?.nonce);
if (!nonce) {
throw new OpenclawGatewayClientError("Gateway challenge did not include a nonce.");
}
const signedAt =
typeof challenge?.ts === "number" && Number.isFinite(challenge.ts)
? challenge.ts
: Date.now();
const role = this.options.role ?? "operator";
const scopes =
authMode === "deviceToken" && uniqueScopes(this.options.deviceTokenScopes).length > 0
? uniqueScopes(this.options.deviceTokenScopes)
: uniqueScopes(this.options.scopes ?? OPENCLAW_OPERATOR_SCOPES);
const authToken =
authMode === "deviceToken"
? (this.options.deviceToken ?? "")
: (this.options.sharedSecret ?? "");
const signedDevice = signOpenclawDeviceChallenge(this.options.identity, {
clientId: this.options.clientId,
clientMode: this.options.clientMode,
role,
scopes,
token: authToken,
nonce,
signedAt,
});
const helloPayload = await this.request("connect", {
minProtocol: OPENCLAW_PROTOCOL_VERSION,
maxProtocol: OPENCLAW_PROTOCOL_VERSION,
client: {
id: this.options.clientId,
version: this.options.clientVersion,
platform: this.options.clientPlatform,
mode: this.options.clientMode,
},
role,
scopes,
caps: [],
commands: [],
permissions: {},
...(authMode === "shared" && authToken.length > 0 ? { auth: { token: authToken } } : {}),
...(authMode === "deviceToken" && authToken.length > 0
? { auth: { deviceToken: authToken } }
: {}),
locale: this.options.locale,
userAgent: this.options.userAgent,
device: signedDevice,
});
const hello = extractHelloPayload(helloPayload);
const methods = extractHelloMethods(hello);
if (this.options.requiredMethods && this.options.requiredMethods.length > 0) {
assertRequiredMethods(methods, this.options.requiredMethods);
}
this.hello = hello;
this.auth = hello?.auth;
this.methods.clear();
for (const method of methods) {
this.methods.add(method);
}
return {
hello,
auth: hello?.auth,
methods,
usedStoredDeviceToken: authMode === "deviceToken",
};
}
private framePayload(frame: GatewayFrame): Record<string, unknown> | undefined {
return typeof frame.payload === "object" && frame.payload !== null
? (frame.payload as Record<string, unknown>)
: undefined;
}
private async openSocket(): Promise<void> {
await this.closeCurrentSocket();
this.closeCode = undefined;
this.closeReason = undefined;
this.closed = false;
this.ws = await new Promise<NodeWebSocket>((resolve, reject) => {
const socket = new NodeWebSocket(this.options.url);
const timeout = setTimeout(() => {
socket.close();
reject(
new OpenclawGatewayClientError(
`WebSocket connection to ${this.options.url} timed out after ${WS_CONNECT_TIMEOUT_MS}ms.`,
),
);
}, WS_CONNECT_TIMEOUT_MS);
socket.on("open", () => {
clearTimeout(timeout);
resolve(socket);
});
socket.on("error", (cause) => {
clearTimeout(timeout);
reject(
new OpenclawGatewayClientError(
`WebSocket connection to ${this.options.url} failed: ${cause instanceof Error ? cause.message : String(cause)}`,
clientErrorOptions({
gatewayError: undefined,
socketCloseCode: undefined,
socketCloseReason: undefined,
cause,
}),
),
);
});
this.attachSocketHandlers(socket);
});
}
private attachSocketHandlers(socket: NodeWebSocket) {
socket.on("message", (data) => {
const frame = parseGatewayFrame(data);
if (!frame) {
return;
}
if (frame.type === "res" && frame.id !== undefined && frame.id !== null) {
const pending = this.pendingRequests.get(String(frame.id));
if (!pending) {
return;
}
clearTimeout(pending.timeout);
this.pendingRequests.delete(String(frame.id));
if (frame.ok === true) {
pending.resolve(frame.payload);
return;
}
const gatewayError = parseGatewayError(frame.error);
pending.reject(
new OpenclawGatewayClientError(
formatGatewayError(gatewayError),
clientErrorOptions({
gatewayError,
socketCloseCode: this.closeCode,
socketCloseReason: this.closeReason,
cause: undefined,
}),
),
);
return;
}
if (frame.type === "event" && typeof frame.event === "string") {
let matchedWaiter = false;
for (const waiter of this.pendingEventWaiters) {
if (waiter.eventName === frame.event) {
matchedWaiter = true;
this.pendingEventWaiters.delete(waiter);
waiter.resolve(this.framePayload(frame));
}
}
if (!matchedWaiter) {
this.bufferedEvents.push(frame);
}
}
for (const listener of this.eventListeners) {
listener(frame);
}
});
socket.on("close", (code, reasonBuffer) => {
this.closeCode = code;
const reason = reasonBuffer.toString("utf8");
this.closeReason = reason.length > 0 ? reason : undefined;
const error =
this.closed || (code === 1000 && !this.closeReason)
? undefined
: new OpenclawGatewayClientError(
`WebSocket closed before the gateway exchange completed${closeDetail(code, this.closeReason)}.`,
clientErrorOptions({
gatewayError: undefined,
socketCloseCode: code,
socketCloseReason: this.closeReason,
cause: undefined,
}),
);
for (const [, pending] of this.pendingRequests) {
clearTimeout(pending.timeout);
pending.reject(
error ??
new OpenclawGatewayClientError(`Gateway request '${pending.method}' was interrupted.`),
);
}
this.pendingRequests.clear();
for (const waiter of this.pendingEventWaiters) {
clearTimeout(waiter.timeout);
waiter.reject(
error ??
new OpenclawGatewayClientError(
`Gateway event '${waiter.eventName}' was interrupted.`,
clientErrorOptions({
gatewayError: undefined,
socketCloseCode: code,
socketCloseReason: this.closeReason,
cause: undefined,
}),
),
);
}
this.pendingEventWaiters.clear();
for (const listener of this.closeListeners) {
listener(error);
}
});
}
private async closeCurrentSocket() {
if (!this.ws) {
return;
}
const socket = this.ws;
this.ws = null;
await new Promise<void>((resolve) => {
if (
socket.readyState === NodeWebSocket.CLOSED ||
socket.readyState === NodeWebSocket.CLOSING
) {
resolve();
return;
}
socket.once("close", () => resolve());
socket.close();
});
}
}