@ceki/sdk 1.9.0

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/index.js ADDED
@@ -0,0 +1,1993 @@
1
+ // src/client.ts
2
+ import WebSocket from "ws";
3
+
4
+ // src/config.ts
5
+ var defaults = {
6
+ apiUrl: "https://api.ceki.me",
7
+ relayUrl: "wss://browser.ceki.me/ws/agent",
8
+ chatUrl: "https://chat.ceki.me/api/chat"
9
+ };
10
+ function resolveConfig(opts) {
11
+ return {
12
+ apiUrl: opts?.apiUrl ?? process.env.CEKI_API_URL ?? defaults.apiUrl,
13
+ relayUrl: opts?.relayUrl ?? process.env.CEKI_RELAY_URL ?? defaults.relayUrl,
14
+ chatUrl: opts?.chatUrl ?? process.env.CEKI_CHAT_URL ?? defaults.chatUrl,
15
+ basicAuth: opts?.basicAuth ?? (process.env.CEKI_BASIC_AUTH_USER && process.env.CEKI_BASIC_AUTH_PASS ? [process.env.CEKI_BASIC_AUTH_USER, process.env.CEKI_BASIC_AUTH_PASS] : void 0),
16
+ reconnect: opts?.reconnect ?? true
17
+ };
18
+ }
19
+
20
+ // src/errors.ts
21
+ var CekiBrowserError = class extends Error {
22
+ constructor(message) {
23
+ super(message);
24
+ this.name = "CekiBrowserError";
25
+ }
26
+ };
27
+ var AuthError = class extends CekiBrowserError {
28
+ constructor(message = "Authentication failed") {
29
+ super(message);
30
+ this.name = "AuthError";
31
+ }
32
+ };
33
+ var SessionNotFound = class extends CekiBrowserError {
34
+ constructor(message = "Session not found") {
35
+ super(message);
36
+ this.name = "SessionNotFound";
37
+ }
38
+ };
39
+ var SessionExpired = class extends SessionNotFound {
40
+ constructor(message = "Session expired") {
41
+ super(message);
42
+ this.name = "SessionExpired";
43
+ }
44
+ };
45
+ var NotOwner = class extends CekiBrowserError {
46
+ constructor(message = "Not session owner") {
47
+ super(message);
48
+ this.name = "NotOwner";
49
+ }
50
+ };
51
+ var TransportError = class extends CekiBrowserError {
52
+ constructor(message = "Transport error") {
53
+ super(message);
54
+ this.name = "TransportError";
55
+ }
56
+ };
57
+ var TimeoutError = class extends CekiBrowserError {
58
+ constructor(message = "Operation timed out") {
59
+ super(message);
60
+ this.name = "TimeoutError";
61
+ }
62
+ };
63
+ var SessionEnded = class extends CekiBrowserError {
64
+ reason;
65
+ constructor(reason) {
66
+ super(`Session ended: ${reason}`);
67
+ this.name = "SessionEnded";
68
+ this.reason = reason;
69
+ }
70
+ };
71
+ var InsufficientFunds = class extends CekiBrowserError {
72
+ constructor(message = "Insufficient funds") {
73
+ super(message);
74
+ this.name = "InsufficientFunds";
75
+ }
76
+ };
77
+ var RateLimitExceeded = class extends CekiBrowserError {
78
+ retryAfter;
79
+ constructor(retryAfter = 0, message = "Rate limit exceeded") {
80
+ super(message);
81
+ this.name = "RateLimitExceeded";
82
+ this.retryAfter = retryAfter;
83
+ }
84
+ };
85
+ var ConnectionLost = class extends CekiBrowserError {
86
+ constructor(message = "Connection lost") {
87
+ super(message);
88
+ this.name = "ConnectionLost";
89
+ }
90
+ };
91
+ var ProviderOffline = class extends CekiBrowserError {
92
+ constructor(message = "Provider offline") {
93
+ super(message);
94
+ this.name = "ProviderOffline";
95
+ }
96
+ };
97
+ var ProviderDisconnected = class extends CekiBrowserError {
98
+ constructor(message = "Provider disconnected") {
99
+ super(message);
100
+ this.name = "ProviderDisconnected";
101
+ }
102
+ };
103
+ var CdpUnrecoverable = class extends CekiBrowserError {
104
+ lastError;
105
+ constructor(lastError) {
106
+ super(`CDP unrecoverable: ${lastError}`);
107
+ this.name = "CdpUnrecoverable";
108
+ this.lastError = lastError;
109
+ }
110
+ };
111
+ var CaptchaError = class extends CekiBrowserError {
112
+ constructor(message = "Captcha error") {
113
+ super(message);
114
+ this.name = "CaptchaError";
115
+ }
116
+ };
117
+ var CaptchaTimeoutError = class extends CaptchaError {
118
+ phase;
119
+ constructor(phase) {
120
+ super(`Captcha timeout: ${phase}`);
121
+ this.name = "CaptchaTimeoutError";
122
+ this.phase = phase;
123
+ }
124
+ };
125
+ var ChatSendFailed = class extends CekiBrowserError {
126
+ status;
127
+ messageText;
128
+ constructor(status, messageText) {
129
+ super(`Chat send failed (${status})`);
130
+ this.name = "ChatSendFailed";
131
+ this.status = status;
132
+ this.messageText = messageText;
133
+ }
134
+ };
135
+
136
+ // src/chat.ts
137
+ import * as crypto from "crypto";
138
+ import * as fs from "fs";
139
+ import * as path from "path";
140
+ var MAX_IMAGE_SIZE = 5 * 1024 * 1024;
141
+ function detectMime(buf) {
142
+ if (buf.length >= 4 && buf[0] === 137 && buf[1] === 80 && buf[2] === 78 && buf[3] === 71) {
143
+ return { mime: "image/png", ext: "png" };
144
+ }
145
+ if (buf.length >= 3 && buf[0] === 255 && buf[1] === 216 && buf[2] === 255) {
146
+ return { mime: "image/jpeg", ext: "jpg" };
147
+ }
148
+ if (buf.length >= 12 && buf[0] === 82 && buf[1] === 73 && buf[2] === 70 && buf[3] === 70 && buf[8] === 87 && buf[9] === 69 && buf[10] === 66 && buf[11] === 80) {
149
+ return { mime: "image/webp", ext: "webp" };
150
+ }
151
+ return { mime: "application/octet-stream", ext: "bin" };
152
+ }
153
+ function randomHex(len) {
154
+ return crypto.randomBytes(len / 2).toString("hex");
155
+ }
156
+ var BrowserChat = class {
157
+ _browser;
158
+ _topicId;
159
+ _messageHandlers = [];
160
+ _readHandlers = [];
161
+ _pendingSends = /* @__PURE__ */ new Map();
162
+ /** @internal */
163
+ _actionCallbacks = /* @__PURE__ */ new Map();
164
+ constructor(browser) {
165
+ this._browser = browser;
166
+ this._topicId = browser.chatTopicId;
167
+ }
168
+ get topicId() {
169
+ return this._topicId;
170
+ }
171
+ async send(text) {
172
+ const clientMsgId = randomHex(32);
173
+ const msg = {
174
+ type: "chat.send",
175
+ session_id: this._browser.sessionId,
176
+ client_msg_id: clientMsgId,
177
+ text
178
+ };
179
+ return new Promise((resolve, reject) => {
180
+ const timer = setTimeout(() => {
181
+ this._pendingSends.delete(clientMsgId);
182
+ reject(new TimeoutError("Chat send timed out"));
183
+ }, 15e3);
184
+ this._pendingSends.set(clientMsgId, { resolve, reject, timer });
185
+ this._browser._sendRaw(msg);
186
+ });
187
+ }
188
+ async sendImage(source, text) {
189
+ let buf;
190
+ let filename;
191
+ if (typeof source === "string") {
192
+ buf = fs.readFileSync(source);
193
+ filename = path.basename(source);
194
+ } else {
195
+ buf = Buffer.isBuffer(source) ? source : Buffer.from(source);
196
+ filename = "image";
197
+ }
198
+ if (buf.length > MAX_IMAGE_SIZE) {
199
+ throw new Error(`Image too large: ${buf.length} bytes (max ${MAX_IMAGE_SIZE})`);
200
+ }
201
+ const { mime, ext } = detectMime(buf);
202
+ if (!filename.includes(".")) {
203
+ filename = `${filename}.${ext}`;
204
+ }
205
+ const clientMsgId = randomHex(32);
206
+ const data_b64 = buf.toString("base64");
207
+ const msg = {
208
+ type: "chat.send_image",
209
+ session_id: this._browser.sessionId,
210
+ client_msg_id: clientMsgId,
211
+ filename,
212
+ mime,
213
+ data_b64
214
+ };
215
+ if (text) msg.text = text;
216
+ return new Promise((resolve, reject) => {
217
+ const timer = setTimeout(() => {
218
+ this._pendingSends.delete(clientMsgId);
219
+ reject(new TimeoutError("Chat sendImage timed out"));
220
+ }, 15e3);
221
+ this._pendingSends.set(clientMsgId, { resolve, reject, timer });
222
+ this._browser._sendRaw(msg);
223
+ });
224
+ }
225
+ onMessage(cb) {
226
+ this._messageHandlers.push(cb);
227
+ }
228
+ onRead(cb) {
229
+ this._readHandlers.push(cb);
230
+ }
231
+ async history(opts) {
232
+ if (!this._topicId) return [];
233
+ const params = new URLSearchParams();
234
+ params.set("topic_id", this._topicId);
235
+ if (opts?.limit != null) params.set("limit", String(opts.limit));
236
+ if (opts?.beforeId) params.set("before", opts.beforeId);
237
+ if (opts?.since) params.set("since", opts.since);
238
+ const url = `${this._browser._chatUrl}/messages?${params.toString()}`;
239
+ const headers = {
240
+ "Authorization": `Bearer ${this._browser._apiKey}`
241
+ };
242
+ const basicAuth = this._browser._basicAuth;
243
+ if (basicAuth) {
244
+ const encoded = Buffer.from(`${basicAuth[0]}:${basicAuth[1]}`).toString("base64");
245
+ headers["X-Basic-Auth"] = `Basic ${encoded}`;
246
+ }
247
+ const resp = await fetch(url, { headers });
248
+ if (!resp.ok) {
249
+ throw new Error(`Chat history request failed: ${resp.status} ${resp.statusText}`);
250
+ }
251
+ const body = await resp.json();
252
+ const messages = body.messages ?? body.data ?? body;
253
+ if (!Array.isArray(messages)) return [];
254
+ return messages.map(parseChatMessage);
255
+ }
256
+ /** @internal */
257
+ _onMessage(payload) {
258
+ const msgData = payload.message ?? payload;
259
+ if (msgData.type === "action" && msgData.action) {
260
+ const action = msgData.action;
261
+ const eventId = Number(action.event_id);
262
+ if (eventId && this._actionCallbacks.has(eventId)) {
263
+ this._actionCallbacks.get(eventId)(action);
264
+ }
265
+ }
266
+ const msg = parseChatMessage(msgData);
267
+ for (const h of this._messageHandlers) {
268
+ try {
269
+ const result = h(msg);
270
+ if (result && typeof result.catch === "function") {
271
+ result.catch(() => {
272
+ });
273
+ }
274
+ } catch {
275
+ }
276
+ }
277
+ }
278
+ /** @internal */
279
+ _onRead(payload) {
280
+ const receipt = {
281
+ topic_id: String(payload.topic_id ?? this._topicId ?? ""),
282
+ last_read_message_id: String(payload.last_read_message_id ?? ""),
283
+ read_at: Number(payload.read_at ?? 0)
284
+ };
285
+ for (const h of this._readHandlers) {
286
+ try {
287
+ const result = h(receipt);
288
+ if (result && typeof result.catch === "function") {
289
+ result.catch(() => {
290
+ });
291
+ }
292
+ } catch {
293
+ }
294
+ }
295
+ }
296
+ /** @internal */
297
+ _onSendAck(msg) {
298
+ const clientMsgId = String(msg.client_msg_id ?? "");
299
+ const pending = this._pendingSends.get(clientMsgId);
300
+ if (!pending) return;
301
+ clearTimeout(pending.timer);
302
+ this._pendingSends.delete(clientMsgId);
303
+ pending.resolve({
304
+ messageId: String(msg.message_id ?? ""),
305
+ sentAt: String(msg.sent_at ?? "")
306
+ });
307
+ }
308
+ /** @internal */
309
+ _onSendError(msg) {
310
+ const clientMsgId = String(msg.client_msg_id ?? "");
311
+ const pending = this._pendingSends.get(clientMsgId);
312
+ if (!pending) return;
313
+ clearTimeout(pending.timer);
314
+ this._pendingSends.delete(clientMsgId);
315
+ pending.reject(new ChatSendFailed(
316
+ Number(msg.status ?? 0),
317
+ String(msg.message ?? "")
318
+ ));
319
+ }
320
+ };
321
+ function parseChatMessage(data) {
322
+ const action = data.action;
323
+ return {
324
+ id: String(data.id ?? data._id ?? data.message_id ?? ""),
325
+ topic_id: String(data.topic_id ?? ""),
326
+ sender_id: data.sender_id != null ? Number(data.sender_id) : null,
327
+ text: data.text != null ? String(data.text) : null,
328
+ media: data.media ?? null,
329
+ type: String(data.type ?? "text"),
330
+ created_at: String(data.created_at ?? ""),
331
+ edited_at: data.edited_at != null ? String(data.edited_at) : null,
332
+ deleted_at: data.deleted_at != null ? String(data.deleted_at) : null,
333
+ action: action ? {
334
+ kind: String(action.kind ?? ""),
335
+ event_id: Number(action.event_id ?? 0),
336
+ data: action.data ?? void 0
337
+ } : null
338
+ };
339
+ }
340
+
341
+ // src/profile.ts
342
+ var BrowserProfile = class {
343
+ _browser;
344
+ constructor(browser) {
345
+ this._browser = browser;
346
+ }
347
+ async export(opts) {
348
+ let fingerprint = null;
349
+ try {
350
+ const fpResult = await this._browser.send({ method: "Browser.getFingerprint" });
351
+ fingerprint = fpResult ?? null;
352
+ } catch {
353
+ fingerprint = null;
354
+ }
355
+ let cookies = [];
356
+ try {
357
+ const cookieResult = await this._browser.send({ method: "Network.getCookies" });
358
+ const allCookies = cookieResult?.cookies ?? [];
359
+ if (opts?.domains && opts.domains.length > 0) {
360
+ const domainSet = new Set(opts.domains.map((d) => d.toLowerCase()));
361
+ cookies = allCookies.filter((c) => {
362
+ const cookieDomain = String(c.domain ?? "").toLowerCase().replace(/^\./, "");
363
+ return domainSet.has(cookieDomain) || opts.domains.some(
364
+ (d) => cookieDomain.endsWith("." + d.toLowerCase()) || cookieDomain === d.toLowerCase()
365
+ );
366
+ });
367
+ } else {
368
+ cookies = allCookies;
369
+ }
370
+ } catch {
371
+ cookies = [];
372
+ }
373
+ let localStorage = {};
374
+ try {
375
+ const lsResult = await this._browser.send({
376
+ method: "Runtime.evaluate",
377
+ params: { expression: "JSON.stringify(localStorage)", returnByValue: true }
378
+ });
379
+ const resultObj = lsResult?.result;
380
+ if (resultObj?.value) {
381
+ localStorage = JSON.parse(String(resultObj.value));
382
+ }
383
+ } catch {
384
+ localStorage = {};
385
+ }
386
+ let sessionStorage = {};
387
+ if (opts?.includeSessionStorage !== false) {
388
+ try {
389
+ const ssResult = await this._browser.send({
390
+ method: "Runtime.evaluate",
391
+ params: { expression: "JSON.stringify(sessionStorage)", returnByValue: true }
392
+ });
393
+ const resultObj = ssResult?.result;
394
+ if (resultObj?.value) {
395
+ sessionStorage = JSON.parse(String(resultObj.value));
396
+ }
397
+ } catch {
398
+ sessionStorage = {};
399
+ }
400
+ }
401
+ let origin = "";
402
+ try {
403
+ const originResult = await this._browser.send({
404
+ method: "Runtime.evaluate",
405
+ params: { expression: "location.origin", returnByValue: true }
406
+ });
407
+ const resultObj = originResult?.result;
408
+ if (resultObj?.value) {
409
+ origin = String(resultObj.value);
410
+ }
411
+ } catch {
412
+ origin = "";
413
+ }
414
+ return {
415
+ schema_version: 2,
416
+ fingerprint,
417
+ origin,
418
+ cookies,
419
+ localStorage,
420
+ sessionStorage
421
+ };
422
+ }
423
+ async import(profile) {
424
+ if (profile.schema_version !== 1 && profile.schema_version !== 2) {
425
+ throw new Error(`Unsupported profile schema_version: ${profile.schema_version}`);
426
+ }
427
+ if (profile.cookies && profile.cookies.length > 0) {
428
+ await this._browser.send({
429
+ method: "Network.setCookies",
430
+ params: { cookies: profile.cookies }
431
+ });
432
+ }
433
+ if (profile.localStorage && Object.keys(profile.localStorage).length > 0) {
434
+ const entries = JSON.stringify(profile.localStorage);
435
+ await this._browser.send({
436
+ method: "Runtime.evaluate",
437
+ params: {
438
+ expression: `(function(){var d=${entries};for(var k in d)localStorage.setItem(k,d[k])})()`,
439
+ returnByValue: true
440
+ }
441
+ });
442
+ }
443
+ if (profile.sessionStorage && Object.keys(profile.sessionStorage).length > 0) {
444
+ const entries = JSON.stringify(profile.sessionStorage);
445
+ await this._browser.send({
446
+ method: "Runtime.evaluate",
447
+ params: {
448
+ expression: `(function(){var d=${entries};for(var k in d)sessionStorage.setItem(k,d[k])})()`,
449
+ returnByValue: true
450
+ }
451
+ });
452
+ }
453
+ }
454
+ };
455
+
456
+ // src/state.ts
457
+ import * as fs2 from "fs";
458
+ import * as path2 from "path";
459
+ import * as os from "os";
460
+ var STATE_DIR = path2.join(os.homedir(), ".ceki", "sessions");
461
+ function statePath(sid) {
462
+ return path2.join(STATE_DIR, `${sid}.json`);
463
+ }
464
+ function loadSession(sid) {
465
+ try {
466
+ const raw = fs2.readFileSync(statePath(sid), "utf-8");
467
+ return JSON.parse(raw);
468
+ } catch {
469
+ return null;
470
+ }
471
+ }
472
+ function saveSession(sid, data) {
473
+ fs2.mkdirSync(STATE_DIR, { recursive: true });
474
+ const payload = { ...data, updated_at: (/* @__PURE__ */ new Date()).toISOString() };
475
+ fs2.writeFileSync(statePath(sid), JSON.stringify(payload, null, 2), "utf-8");
476
+ }
477
+ function getLastSeenTs(sid) {
478
+ const data = loadSession(sid);
479
+ if (!data) return null;
480
+ return data.last_seen_ts ?? null;
481
+ }
482
+ function updateLastSeenTs(sid, ts) {
483
+ const data = loadSession(sid) ?? {};
484
+ data.last_seen_ts = ts;
485
+ saveSession(sid, data);
486
+ }
487
+
488
+ // src/humanize/keymap.ts
489
+ var STATIC_MAP = {
490
+ " ": { code: "Space", key: " ", vk: 32, needsShift: false },
491
+ "\n": { code: "Enter", key: "Enter", vk: 13, needsShift: false },
492
+ " ": { code: "Tab", key: "Tab", vk: 9, needsShift: false },
493
+ // Shifted digits
494
+ "!": { code: "Digit1", key: "!", vk: 49, needsShift: true },
495
+ "@": { code: "Digit2", key: "@", vk: 50, needsShift: true },
496
+ "#": { code: "Digit3", key: "#", vk: 51, needsShift: true },
497
+ "$": { code: "Digit4", key: "$", vk: 52, needsShift: true },
498
+ "%": { code: "Digit5", key: "%", vk: 53, needsShift: true },
499
+ "^": { code: "Digit6", key: "^", vk: 54, needsShift: true },
500
+ "&": { code: "Digit7", key: "&", vk: 55, needsShift: true },
501
+ "*": { code: "Digit8", key: "*", vk: 56, needsShift: true },
502
+ "(": { code: "Digit9", key: "(", vk: 57, needsShift: true },
503
+ ")": { code: "Digit0", key: ")", vk: 48, needsShift: true },
504
+ // Punctuation (unshifted)
505
+ "-": { code: "Minus", key: "-", vk: 189, needsShift: false },
506
+ "=": { code: "Equal", key: "=", vk: 187, needsShift: false },
507
+ "[": { code: "BracketLeft", key: "[", vk: 219, needsShift: false },
508
+ "]": { code: "BracketRight", key: "]", vk: 221, needsShift: false },
509
+ "\\": { code: "Backslash", key: "\\", vk: 220, needsShift: false },
510
+ ";": { code: "Semicolon", key: ";", vk: 186, needsShift: false },
511
+ "'": { code: "Quote", key: "'", vk: 222, needsShift: false },
512
+ ",": { code: "Comma", key: ",", vk: 188, needsShift: false },
513
+ ".": { code: "Period", key: ".", vk: 190, needsShift: false },
514
+ "/": { code: "Slash", key: "/", vk: 191, needsShift: false },
515
+ "`": { code: "Backquote", key: "`", vk: 192, needsShift: false },
516
+ // Punctuation (shifted)
517
+ "_": { code: "Minus", key: "_", vk: 189, needsShift: true },
518
+ "+": { code: "Equal", key: "+", vk: 187, needsShift: true },
519
+ "{": { code: "BracketLeft", key: "{", vk: 219, needsShift: true },
520
+ "}": { code: "BracketRight", key: "}", vk: 221, needsShift: true },
521
+ "|": { code: "Backslash", key: "|", vk: 220, needsShift: true },
522
+ ":": { code: "Semicolon", key: ":", vk: 186, needsShift: true },
523
+ '"': { code: "Quote", key: '"', vk: 222, needsShift: true },
524
+ "<": { code: "Comma", key: "<", vk: 188, needsShift: true },
525
+ ">": { code: "Period", key: ">", vk: 190, needsShift: true },
526
+ "?": { code: "Slash", key: "?", vk: 191, needsShift: true },
527
+ "~": { code: "Backquote", key: "~", vk: 192, needsShift: true }
528
+ };
529
+ function keymapForChar(char) {
530
+ if (char >= "a" && char <= "z") {
531
+ const upper = char.toUpperCase();
532
+ return {
533
+ code: `Key${upper}`,
534
+ key: char,
535
+ vk: upper.charCodeAt(0),
536
+ needsShift: false
537
+ };
538
+ }
539
+ if (char >= "A" && char <= "Z") {
540
+ return {
541
+ code: `Key${char}`,
542
+ key: char,
543
+ vk: char.charCodeAt(0),
544
+ needsShift: true
545
+ };
546
+ }
547
+ if (char >= "0" && char <= "9") {
548
+ return {
549
+ code: `Digit${char}`,
550
+ key: char,
551
+ vk: char.charCodeAt(0),
552
+ needsShift: false
553
+ };
554
+ }
555
+ const mapping = STATIC_MAP[char];
556
+ if (mapping) return mapping;
557
+ return null;
558
+ }
559
+
560
+ // src/browser.ts
561
+ var Browser = class {
562
+ sessionId;
563
+ browserId;
564
+ scheduleId;
565
+ chatTopicId;
566
+ browserInfo;
567
+ providerUserId;
568
+ /** @internal */
569
+ _eventId;
570
+ chat;
571
+ profile;
572
+ /** @internal */
573
+ _client;
574
+ /** @internal */
575
+ _humanizer = null;
576
+ /** @internal */
577
+ _lastPointer = null;
578
+ /** @internal */
579
+ _lastSeenTs = null;
580
+ /** @internal */
581
+ _cdpCounter = 1;
582
+ /** @internal */
583
+ _pendingCdp = /* @__PURE__ */ new Map();
584
+ /** @internal */
585
+ _ended;
586
+ /** @internal */
587
+ _endedReason = null;
588
+ /** @internal */
589
+ _resolveEnded;
590
+ _eventHandlers = [];
591
+ _tabHandlers = [];
592
+ _disconnectHandlers = [];
593
+ _reconnectHandlers = [];
594
+ _userEventHandlers = [];
595
+ /** @internal */
596
+ get _apiKey() {
597
+ return this._client._apiKey;
598
+ }
599
+ /** @internal */
600
+ get _chatUrl() {
601
+ return this._client._chatUrl;
602
+ }
603
+ /** @internal */
604
+ get _basicAuth() {
605
+ return this._client._basicAuth;
606
+ }
607
+ constructor(client, match, humanizer) {
608
+ this._client = client;
609
+ this.sessionId = match.session_id;
610
+ this.browserId = match.schedule_id;
611
+ this.scheduleId = match.schedule_id;
612
+ this.chatTopicId = match.chat_topic_id ?? null;
613
+ this.browserInfo = match.browser_info ?? {};
614
+ this.providerUserId = match.provider_user_id ?? null;
615
+ this._eventId = match.event_id ?? null;
616
+ this._humanizer = humanizer ?? null;
617
+ this.chat = new BrowserChat(this);
618
+ this.profile = new BrowserProfile(this);
619
+ this._ended = new Promise((resolve) => {
620
+ this._resolveEnded = resolve;
621
+ });
622
+ this._lastSeenTs = getLastSeenTs(this.sessionId);
623
+ saveSession(this.sessionId, {
624
+ session_id: this.sessionId,
625
+ chat_topic_id: this.chatTopicId,
626
+ schedule_id: this.scheduleId,
627
+ last_seen_ts: this._lastSeenTs
628
+ });
629
+ }
630
+ /** @internal — send raw message via client WS */
631
+ _sendRaw(msg) {
632
+ this._client._wsSend(msg);
633
+ }
634
+ async send(cdp, timeout = 3e4) {
635
+ if (this._endedReason) {
636
+ throw new SessionEnded(this._endedReason);
637
+ }
638
+ const id = this._cdpCounter++;
639
+ const msg = {
640
+ type: "cdp",
641
+ session_id: this.sessionId,
642
+ id,
643
+ method: cdp.method,
644
+ params: cdp.params ?? {}
645
+ };
646
+ return new Promise((resolve, reject) => {
647
+ const timer = setTimeout(() => {
648
+ this._pendingCdp.delete(id);
649
+ reject(new TimeoutError(`CDP ${cdp.method} timed out after ${timeout}ms`));
650
+ }, timeout);
651
+ this._pendingCdp.set(id, { resolve, reject, timer });
652
+ this._sendRaw(msg);
653
+ });
654
+ }
655
+ async navigate(url, timeout = 3e4) {
656
+ if (this._humanizer) await this._humanizer.before("navigate");
657
+ const result = await this.send({ method: "Page.navigate", params: { url } }, timeout);
658
+ if (this._humanizer) await this._humanizer.after("navigate");
659
+ return {
660
+ url: String(result?.url ?? url),
661
+ frameId: result?.frameId ? String(result.frameId) : void 0
662
+ };
663
+ }
664
+ async click(x, y) {
665
+ if (this._humanizer) await this._humanizer.before("click");
666
+ await this.send({
667
+ method: "Input.dispatchMouseEvent",
668
+ params: { type: "mousePressed", x, y, button: "left", clickCount: 1 }
669
+ });
670
+ await this.send({
671
+ method: "Input.dispatchMouseEvent",
672
+ params: { type: "mouseReleased", x, y, button: "left", clickCount: 1 }
673
+ });
674
+ this._lastPointer = [x, y];
675
+ if (this._humanizer) await this._humanizer.after("click");
676
+ }
677
+ async _sendKeystroke(char) {
678
+ const mapping = keymapForChar(char);
679
+ if (!mapping) {
680
+ await this.send({ method: "Input.insertText", params: { text: char } });
681
+ return;
682
+ }
683
+ const { code, key, vk, needsShift } = mapping;
684
+ if (needsShift) {
685
+ await this.send({
686
+ method: "Input.dispatchKeyEvent",
687
+ params: { type: "keyDown", key: "Shift", code: "ShiftLeft", windowsVirtualKeyCode: 16, nativeVirtualKeyCode: 16 }
688
+ });
689
+ }
690
+ await this.send({
691
+ method: "Input.dispatchKeyEvent",
692
+ params: {
693
+ type: "keyDown",
694
+ key,
695
+ code,
696
+ text: char,
697
+ unmodifiedText: needsShift ? char.toLowerCase() : char,
698
+ windowsVirtualKeyCode: vk,
699
+ nativeVirtualKeyCode: vk,
700
+ ...needsShift ? { modifiers: 8 } : {}
701
+ }
702
+ });
703
+ await this.send({
704
+ method: "Input.dispatchKeyEvent",
705
+ params: {
706
+ type: "keyUp",
707
+ key,
708
+ code,
709
+ windowsVirtualKeyCode: vk,
710
+ nativeVirtualKeyCode: vk,
711
+ ...needsShift ? { modifiers: 8 } : {}
712
+ }
713
+ });
714
+ if (needsShift) {
715
+ await this.send({
716
+ method: "Input.dispatchKeyEvent",
717
+ params: { type: "keyUp", key: "Shift", code: "ShiftLeft", windowsVirtualKeyCode: 16, nativeVirtualKeyCode: 16 }
718
+ });
719
+ }
720
+ }
721
+ async type(text) {
722
+ if (this._humanizer) {
723
+ await this._humanizer.before("type");
724
+ if (this._lastPointer) {
725
+ const [px, py] = this._lastPointer;
726
+ await this.send({
727
+ method: "Input.dispatchMouseEvent",
728
+ params: { type: "mousePressed", x: px, y: py, button: "left", clickCount: 1 }
729
+ });
730
+ await this.send({
731
+ method: "Input.dispatchMouseEvent",
732
+ params: { type: "mouseReleased", x: px, y: py, button: "left", clickCount: 1 }
733
+ });
734
+ }
735
+ for (const char of text) {
736
+ await this._sendKeystroke(char);
737
+ const delay = this._humanizer.typeDelay();
738
+ if (delay > 0) {
739
+ await new Promise((r) => setTimeout(r, delay));
740
+ }
741
+ }
742
+ await this._humanizer.after("type");
743
+ } else {
744
+ for (const char of text) {
745
+ await this._sendKeystroke(char);
746
+ }
747
+ }
748
+ }
749
+ async scroll(opts) {
750
+ const x = opts?.x ?? 0;
751
+ const y = opts?.y ?? 0;
752
+ const deltaX = opts?.deltaX ?? 0;
753
+ const deltaY = opts?.deltaY ?? -300;
754
+ if (this._humanizer) await this._humanizer.before("scroll");
755
+ await this.send({
756
+ method: "Input.dispatchMouseEvent",
757
+ params: { type: "mouseWheel", x, y, deltaX, deltaY }
758
+ });
759
+ this._lastPointer = [x, y];
760
+ if (this._humanizer) await this._humanizer.after("scroll");
761
+ }
762
+ async screenshot(opts) {
763
+ const format = opts?.format ?? "base64";
764
+ const fullPage = opts?.fullPage ?? false;
765
+ let clip;
766
+ if (fullPage) {
767
+ const metrics = await this.send({ method: "Page.getLayoutMetrics" });
768
+ const contentSize = metrics?.contentSize;
769
+ if (contentSize) {
770
+ const width = Number(contentSize.width ?? 1920);
771
+ const height = Math.min(Number(contentSize.height ?? 1080), 16384);
772
+ clip = { x: 0, y: 0, width, height, scale: 1 };
773
+ }
774
+ }
775
+ const params = { format: "png" };
776
+ if (clip) params.clip = clip;
777
+ const result = await this.send({ method: "Page.captureScreenshot", params });
778
+ const data = String(result?.data ?? "");
779
+ if (format === "png") {
780
+ return Buffer.from(data, "base64");
781
+ }
782
+ return { data };
783
+ }
784
+ async snapshot() {
785
+ const [ssResult, chatMessages] = await Promise.all([
786
+ this.screenshot({ format: "base64" }),
787
+ this.chat.history({ since: this._lastSeenTs ?? void 0 })
788
+ ]);
789
+ const screenshotData = ssResult.data;
790
+ if (chatMessages.length > 0) {
791
+ const lastMsg = chatMessages[chatMessages.length - 1];
792
+ this._lastSeenTs = lastMsg.created_at;
793
+ updateLastSeenTs(this.sessionId, this._lastSeenTs);
794
+ }
795
+ return {
796
+ screenshot: screenshotData,
797
+ chat: chatMessages,
798
+ ts: /* @__PURE__ */ new Date()
799
+ };
800
+ }
801
+ async upload(selector, source, filename) {
802
+ let buf;
803
+ let resolvedFilename;
804
+ if (typeof source === "string") {
805
+ const fs4 = await import("fs");
806
+ const path3 = await import("path");
807
+ buf = fs4.readFileSync(source);
808
+ resolvedFilename = filename ?? path3.basename(source);
809
+ } else {
810
+ buf = Buffer.isBuffer(source) ? source : Buffer.from(source);
811
+ resolvedFilename = filename ?? "file";
812
+ }
813
+ const b64 = buf.toString("base64");
814
+ const size = buf.length;
815
+ const expression = `
816
+ (function() {
817
+ var input = document.querySelector(${JSON.stringify(selector)});
818
+ if (!input) return JSON.stringify({ok: false, error: 'Element not found'});
819
+ var b64 = ${JSON.stringify(b64)};
820
+ var binary = atob(b64);
821
+ var bytes = new Uint8Array(binary.length);
822
+ for (var i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
823
+ var file = new File([bytes], ${JSON.stringify(resolvedFilename)}, {type: 'application/octet-stream'});
824
+ var dt = new DataTransfer();
825
+ dt.items.add(file);
826
+ input.files = dt.files;
827
+ input.dispatchEvent(new Event('change', {bubbles: true}));
828
+ return JSON.stringify({ok: true, filename: ${JSON.stringify(resolvedFilename)}, size: ${size}});
829
+ })()
830
+ `.trim();
831
+ const result = await this.send({
832
+ method: "Runtime.evaluate",
833
+ params: { expression, returnByValue: true }
834
+ });
835
+ const resultObj = result?.result;
836
+ if (resultObj?.value) {
837
+ return JSON.parse(String(resultObj.value));
838
+ }
839
+ return { ok: true, filename: resolvedFilename, size };
840
+ }
841
+ async switchTab() {
842
+ this._sendRaw({ type: "switch_tab", session_id: this.sessionId });
843
+ }
844
+ async configure(opts) {
845
+ const msg = {
846
+ type: "session.configure",
847
+ session_id: this.sessionId
848
+ };
849
+ if (opts.maskingMode !== void 0) msg.masking_mode = opts.maskingMode;
850
+ if (opts.fingerprint !== void 0) msg.fingerprint = opts.fingerprint;
851
+ this._sendRaw(msg);
852
+ }
853
+ async close(timeout = 1e4) {
854
+ if (this._endedReason) return;
855
+ this._sendRaw({
856
+ type: "session.end",
857
+ session_id: this.sessionId,
858
+ reason: "user_stop"
859
+ });
860
+ await Promise.race([
861
+ this._ended,
862
+ new Promise(
863
+ (_, reject) => setTimeout(() => reject(new TimeoutError("Close timed out")), timeout)
864
+ )
865
+ ]).catch(() => {
866
+ });
867
+ this._cleanup();
868
+ }
869
+ async release(timeout) {
870
+ return this.close(timeout);
871
+ }
872
+ async waitUntilEnded() {
873
+ return this._ended;
874
+ }
875
+ onEvent(cb) {
876
+ this._eventHandlers.push(cb);
877
+ }
878
+ onTabOpened(cb) {
879
+ this._tabHandlers.push(cb);
880
+ }
881
+ onProviderDisconnected(cb) {
882
+ this._disconnectHandlers.push(cb);
883
+ }
884
+ onProviderReconnected(cb) {
885
+ this._reconnectHandlers.push(cb);
886
+ }
887
+ onUserEvent(cb) {
888
+ this._userEventHandlers.push(cb);
889
+ }
890
+ // --- Captcha / human action ---
891
+ _apiHeaders() {
892
+ const headers = {
893
+ "Authorization": `Bearer ${this._apiKey}`,
894
+ "Content-Type": "application/json"
895
+ };
896
+ const basicAuth = this._basicAuth;
897
+ if (basicAuth) {
898
+ const encoded = Buffer.from(`${basicAuth[0]}:${basicAuth[1]}`).toString("base64");
899
+ headers["X-Basic-Auth"] = `Basic ${encoded}`;
900
+ }
901
+ return headers;
902
+ }
903
+ async requestCaptcha(opts) {
904
+ let acceptanceTimeout = opts?.acceptanceTimeout ?? 60;
905
+ let completionTimeout = opts?.completionTimeout ?? 120;
906
+ const autoAccept = opts?.autoAccept ?? true;
907
+ if (acceptanceTimeout < 30) throw new Error("acceptanceTimeout must be >= 30 seconds");
908
+ if (completionTimeout < 30) throw new Error("completionTimeout must be >= 30 seconds");
909
+ acceptanceTimeout = Math.min(acceptanceTimeout, 300);
910
+ completionTimeout = Math.min(completionTimeout, 600);
911
+ const { id: childEventId } = await this._createCaptchaEvent(acceptanceTimeout, completionTimeout);
912
+ const completionDeadline = Date.now() + completionTimeout * 1e3;
913
+ const buffer = [];
914
+ let waiter = null;
915
+ this.chat._actionCallbacks.set(childEventId, (action) => {
916
+ if (waiter) {
917
+ const w = waiter;
918
+ waiter = null;
919
+ w(action);
920
+ } else {
921
+ buffer.push(action);
922
+ }
923
+ });
924
+ const nextAction = (timeoutMs) => {
925
+ if (buffer.length > 0) return Promise.resolve(buffer.shift());
926
+ return new Promise((resolve, reject) => {
927
+ const timer = setTimeout(() => {
928
+ waiter = null;
929
+ reject(new Error("timeout"));
930
+ }, timeoutMs);
931
+ waiter = (action) => {
932
+ clearTimeout(timer);
933
+ resolve(action);
934
+ };
935
+ });
936
+ };
937
+ const cleanup = () => {
938
+ this.chat._actionCallbacks.delete(childEventId);
939
+ waiter = null;
940
+ };
941
+ const makeResult = async (data, solved) => {
942
+ const correctionId = data.correction_id != null ? Number(data.correction_id) : null;
943
+ const proofMessageId = data.proof_message_id != null ? String(data.proof_message_id) : null;
944
+ let voted = false;
945
+ const result = {
946
+ solved,
947
+ proofMessageId,
948
+ cancelReason: null,
949
+ childEventId,
950
+ correctionId,
951
+ acceptWork: async () => {
952
+ if (voted) return;
953
+ if (!correctionId) throw new CaptchaError("no correction_id \u2014 provider has not proposed completion");
954
+ voted = true;
955
+ await fetch(`${this._client._apiUrl}/api/agent/kal/event/${childEventId}/vote`, {
956
+ method: "POST",
957
+ headers: this._apiHeaders(),
958
+ body: JSON.stringify({ ids: [correctionId], vote: true })
959
+ });
960
+ },
961
+ rejectWork: async (reason) => {
962
+ if (voted) return;
963
+ if (!correctionId) throw new CaptchaError("no correction_id \u2014 provider has not proposed completion");
964
+ voted = true;
965
+ const body = { ids: [correctionId], vote: false };
966
+ if (reason) body.reason = reason;
967
+ await fetch(`${this._client._apiUrl}/api/agent/kal/event/${childEventId}/vote`, {
968
+ method: "POST",
969
+ headers: this._apiHeaders(),
970
+ body: JSON.stringify(body)
971
+ });
972
+ }
973
+ };
974
+ if (autoAccept && solved && correctionId) {
975
+ await new Promise((r) => setTimeout(r, 2e3));
976
+ await result.acceptWork();
977
+ }
978
+ return result;
979
+ };
980
+ let accepted = false;
981
+ try {
982
+ const acceptDeadline = Date.now() + acceptanceTimeout * 1e3;
983
+ while (true) {
984
+ const remaining = acceptDeadline - Date.now();
985
+ if (remaining <= 0) throw new Error("timeout");
986
+ const action = await nextAction(remaining);
987
+ const kind = String(action.kind ?? "");
988
+ const data = action.data ?? {};
989
+ if (kind === "human_action_accepted") {
990
+ accepted = true;
991
+ break;
992
+ }
993
+ if (kind === "human_action_completed") {
994
+ cleanup();
995
+ return await makeResult(data, true);
996
+ }
997
+ if (kind === "human_action_failed" || kind === "human_action_declined" || kind === "human_action_withdrew") {
998
+ cleanup();
999
+ return {
1000
+ solved: false,
1001
+ proofMessageId: null,
1002
+ cancelReason: kind.replace("human_action_", ""),
1003
+ childEventId,
1004
+ correctionId: null,
1005
+ acceptWork: async () => {
1006
+ },
1007
+ rejectWork: async () => {
1008
+ }
1009
+ };
1010
+ }
1011
+ }
1012
+ while (true) {
1013
+ const remaining = completionDeadline - Date.now();
1014
+ if (remaining <= 0) throw new Error("timeout");
1015
+ const action = await nextAction(remaining);
1016
+ const kind = String(action.kind ?? "");
1017
+ const data = action.data ?? {};
1018
+ if (kind === "human_action_completed") {
1019
+ cleanup();
1020
+ return await makeResult(data, true);
1021
+ }
1022
+ if (kind === "human_action_failed" || kind === "human_action_withdrew") {
1023
+ cleanup();
1024
+ return {
1025
+ solved: false,
1026
+ proofMessageId: null,
1027
+ cancelReason: kind.replace("human_action_", ""),
1028
+ childEventId,
1029
+ correctionId: null,
1030
+ acceptWork: async () => {
1031
+ },
1032
+ rejectWork: async () => {
1033
+ }
1034
+ };
1035
+ }
1036
+ }
1037
+ } catch {
1038
+ cleanup();
1039
+ const phase = accepted ? "completion" : "acceptance";
1040
+ await this._expireCaptchaEvent(childEventId);
1041
+ throw new CaptchaTimeoutError(phase);
1042
+ }
1043
+ }
1044
+ async _createCaptchaEvent(acceptanceTimeout, completionTimeout) {
1045
+ const body = {
1046
+ acceptance_deadline_at: Math.floor(acceptanceTimeout),
1047
+ completion_deadline_at: Math.floor(completionTimeout)
1048
+ };
1049
+ const resp = await fetch(`${this._client._apiUrl}/api/agent/sessions/${this._eventId}/captcha-request`, {
1050
+ method: "POST",
1051
+ headers: this._apiHeaders(),
1052
+ body: JSON.stringify(body)
1053
+ });
1054
+ if (!resp.ok) {
1055
+ throw new Error(`Captcha request failed: ${resp.status}`);
1056
+ }
1057
+ const result = await resp.json();
1058
+ if (!result.id) throw new Error("Captcha request did not return an id");
1059
+ return { id: result.id, amount: result.amount };
1060
+ }
1061
+ async _expireCaptchaEvent(childEventId) {
1062
+ try {
1063
+ await fetch(`${this._client._apiUrl}/api/agent/kal/event/${childEventId}`, {
1064
+ method: "PATCH",
1065
+ headers: this._apiHeaders(),
1066
+ body: JSON.stringify({ status_id: 777 })
1067
+ });
1068
+ } catch {
1069
+ }
1070
+ }
1071
+ // --- Internal handlers called by Client dispatch ---
1072
+ /** @internal */
1073
+ _onCdpResponse(msg) {
1074
+ const id = Number(msg.id);
1075
+ const pending = this._pendingCdp.get(id);
1076
+ if (!pending) return;
1077
+ clearTimeout(pending.timer);
1078
+ this._pendingCdp.delete(id);
1079
+ if (msg.ok === false) {
1080
+ const error = msg.error;
1081
+ pending.reject(new Error(String(error?.message ?? error?.code ?? "CDP error")));
1082
+ } else {
1083
+ pending.resolve(msg.result ?? null);
1084
+ }
1085
+ }
1086
+ /** @internal */
1087
+ _onCdpEvent(msg) {
1088
+ const method = String(msg.method ?? "");
1089
+ const params = msg.params ?? {};
1090
+ for (const h of this._eventHandlers) {
1091
+ try {
1092
+ h(method, params);
1093
+ } catch {
1094
+ }
1095
+ }
1096
+ }
1097
+ /** @internal */
1098
+ _onTabOpened(msg) {
1099
+ const url = String(msg.url ?? "");
1100
+ for (const h of this._tabHandlers) {
1101
+ try {
1102
+ h(url);
1103
+ } catch {
1104
+ }
1105
+ }
1106
+ }
1107
+ /** @internal */
1108
+ _onSessionEnded(msg) {
1109
+ const reason = String(msg.reason ?? "unknown");
1110
+ this._endedReason = reason;
1111
+ this._resolveEnded(reason);
1112
+ this._rejectAllPending(new SessionEnded(reason));
1113
+ this._cleanup();
1114
+ }
1115
+ /** @internal */
1116
+ _onProviderDisconnected() {
1117
+ for (const h of this._disconnectHandlers) {
1118
+ try {
1119
+ h();
1120
+ } catch {
1121
+ }
1122
+ }
1123
+ }
1124
+ /** @internal */
1125
+ _onProviderReconnected() {
1126
+ for (const h of this._reconnectHandlers) {
1127
+ try {
1128
+ h();
1129
+ } catch {
1130
+ }
1131
+ }
1132
+ }
1133
+ /** @internal */
1134
+ _onError(msg) {
1135
+ const reason = String(msg.reason ?? msg.message ?? "unknown error");
1136
+ this._endedReason = reason;
1137
+ this._resolveEnded(reason);
1138
+ this._rejectAllPending(new SessionEnded(reason));
1139
+ this._cleanup();
1140
+ }
1141
+ /** @internal */
1142
+ _onUserEvents(msg) {
1143
+ const events = msg.events ?? [];
1144
+ for (const h of this._userEventHandlers) {
1145
+ try {
1146
+ h(events);
1147
+ } catch {
1148
+ }
1149
+ }
1150
+ }
1151
+ /** @internal */
1152
+ _onChatMessage(payload) {
1153
+ this.chat._onMessage(payload);
1154
+ }
1155
+ /** @internal */
1156
+ _onChatRead(payload) {
1157
+ this.chat._onRead(payload);
1158
+ }
1159
+ /** @internal */
1160
+ _onChatSendAck(msg) {
1161
+ this.chat._onSendAck(msg);
1162
+ }
1163
+ /** @internal */
1164
+ _onChatSendError(msg) {
1165
+ this.chat._onSendError(msg);
1166
+ }
1167
+ _rejectAllPending(err) {
1168
+ for (const [id, pending] of this._pendingCdp) {
1169
+ clearTimeout(pending.timer);
1170
+ pending.reject(err);
1171
+ }
1172
+ this._pendingCdp.clear();
1173
+ }
1174
+ _cleanup() {
1175
+ this._client._activeBrowsers.delete(this.sessionId);
1176
+ }
1177
+ };
1178
+
1179
+ // src/humanize/humanizer.ts
1180
+ function seededRandom(seed) {
1181
+ if (seed === null || seed === void 0) {
1182
+ return Math.random;
1183
+ }
1184
+ let s = seed;
1185
+ return () => {
1186
+ s = s * 1664525 + 1013904223 & 4294967295;
1187
+ return (s >>> 0) / 4294967295;
1188
+ };
1189
+ }
1190
+ function gaussianRandom(rng, mean, sigma) {
1191
+ let u1, u2;
1192
+ do {
1193
+ u1 = rng();
1194
+ } while (u1 === 0);
1195
+ u2 = rng();
1196
+ const z = Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);
1197
+ return mean + z * sigma;
1198
+ }
1199
+ var Humanizer = class {
1200
+ profile;
1201
+ _rng;
1202
+ constructor(profile) {
1203
+ this.profile = profile;
1204
+ this._rng = seededRandom(profile.raw.rng_seed ?? null);
1205
+ }
1206
+ async before(action) {
1207
+ const [lo, hi] = this.profile.getRange(action, "pre");
1208
+ if (lo === 0 && hi === 0) return;
1209
+ const delay = lo + this._rng() * (hi - lo);
1210
+ await sleep(delay);
1211
+ }
1212
+ async after(action) {
1213
+ const [lo, hi] = this.profile.getRange(action, "post");
1214
+ if (lo === 0 && hi === 0) return;
1215
+ const delay = lo + this._rng() * (hi - lo);
1216
+ await sleep(delay);
1217
+ }
1218
+ typeDelay() {
1219
+ const typing = this.profile.raw.typing ?? {};
1220
+ const wpm = typing.wpm ?? 110;
1221
+ const jitter = typing.jitter ?? 0.35;
1222
+ const thinkProb = typing.thinking_pause_prob ?? 0;
1223
+ const thinkMs = typing.thinking_pause_ms ?? [300, 1200];
1224
+ const meanInterval = 6e4 / (wpm * 5);
1225
+ const sigma = meanInterval * jitter;
1226
+ let delay = gaussianRandom(this._rng, meanInterval, sigma);
1227
+ delay = Math.max(delay, 20);
1228
+ if (thinkProb > 0 && this._rng() < thinkProb) {
1229
+ delay += thinkMs[0] + this._rng() * (thinkMs[1] - thinkMs[0]);
1230
+ }
1231
+ return delay;
1232
+ }
1233
+ };
1234
+ function sleep(ms) {
1235
+ return new Promise((resolve) => setTimeout(resolve, ms));
1236
+ }
1237
+
1238
+ // src/humanize/profile.ts
1239
+ import * as fs3 from "fs";
1240
+ var PRESETS = {
1241
+ natural: {
1242
+ version: 1,
1243
+ name: "natural",
1244
+ typing: { wpm: 110, jitter: 0.35, thinking_pause_prob: 0.012, thinking_pause_ms: [300, 1200], typo_prob: 0 },
1245
+ pre_action_ms: { click: [80, 350], type: [120, 500], scroll: [50, 250], navigate: [0, 0], screenshot: [0, 0] },
1246
+ post_action_ms: { click: [150, 800], type: [150, 800], scroll: [200, 900], navigate: [400, 1800], screenshot: [0, 0] },
1247
+ mouse: { move_before_click: false, trajectory: "off" },
1248
+ rng_seed: null
1249
+ },
1250
+ careful: {
1251
+ version: 1,
1252
+ name: "careful",
1253
+ typing: { wpm: 80, jitter: 0.4, thinking_pause_prob: 0.025, thinking_pause_ms: [400, 1800], typo_prob: 0 },
1254
+ pre_action_ms: { click: [200, 600], type: [250, 800], scroll: [100, 400], navigate: [0, 0], screenshot: [0, 0] },
1255
+ post_action_ms: { click: [400, 1500], type: [300, 1200], scroll: [300, 1200], navigate: [800, 3e3], screenshot: [0, 0] },
1256
+ mouse: { move_before_click: false, trajectory: "off" },
1257
+ rng_seed: null
1258
+ }
1259
+ };
1260
+ var DEFAULTS = {
1261
+ version: 1,
1262
+ name: "custom",
1263
+ typing: {
1264
+ wpm: 110,
1265
+ jitter: 0.35,
1266
+ thinking_pause_prob: 0.012,
1267
+ thinking_pause_ms: [300, 1200],
1268
+ typo_prob: 0
1269
+ },
1270
+ pre_action_ms: {
1271
+ click: [80, 350],
1272
+ type: [120, 500],
1273
+ scroll: [50, 250],
1274
+ navigate: [0, 0],
1275
+ screenshot: [0, 0]
1276
+ },
1277
+ post_action_ms: {
1278
+ click: [150, 800],
1279
+ type: [150, 800],
1280
+ scroll: [200, 900],
1281
+ navigate: [400, 1800],
1282
+ screenshot: [0, 0]
1283
+ },
1284
+ mouse: {
1285
+ move_before_click: false,
1286
+ trajectory: "off"
1287
+ },
1288
+ rng_seed: null
1289
+ };
1290
+ function deepMerge(base, override) {
1291
+ const result = { ...base };
1292
+ for (const [key, value] of Object.entries(override)) {
1293
+ if (key in result && typeof result[key] === "object" && result[key] !== null && !Array.isArray(result[key]) && typeof value === "object" && value !== null && !Array.isArray(value)) {
1294
+ result[key] = deepMerge(
1295
+ result[key],
1296
+ value
1297
+ );
1298
+ } else {
1299
+ result[key] = value;
1300
+ }
1301
+ }
1302
+ return result;
1303
+ }
1304
+ var HumanProfile = class _HumanProfile {
1305
+ name;
1306
+ raw;
1307
+ constructor(name, raw) {
1308
+ this.name = name;
1309
+ this.raw = raw;
1310
+ }
1311
+ static fromDict(d) {
1312
+ const merged = deepMerge(
1313
+ DEFAULTS,
1314
+ d
1315
+ );
1316
+ const name = d.name ?? "custom";
1317
+ merged.name = name;
1318
+ return new _HumanProfile(name, merged);
1319
+ }
1320
+ static load(filePath) {
1321
+ const raw = fs3.readFileSync(filePath, "utf-8");
1322
+ const data = JSON.parse(raw);
1323
+ return _HumanProfile.fromDict(data);
1324
+ }
1325
+ static loadPreset(name) {
1326
+ const preset = PRESETS[name];
1327
+ if (!preset) {
1328
+ throw new Error(`Preset '${name}' not found. Available: ${Object.keys(PRESETS).join(", ")}`);
1329
+ }
1330
+ return _HumanProfile.fromDict(preset);
1331
+ }
1332
+ getRange(action, phase) {
1333
+ const key = `${phase}_action_ms`;
1334
+ const mapping = this.raw[key];
1335
+ const pair = mapping?.[action];
1336
+ if (Array.isArray(pair) && pair.length === 2) {
1337
+ return [pair[0], pair[1]];
1338
+ }
1339
+ return [0, 0];
1340
+ }
1341
+ typingInterval() {
1342
+ const wpm = this.raw.typing?.wpm ?? 110;
1343
+ return 6e4 / (wpm * 5);
1344
+ }
1345
+ toDict() {
1346
+ return JSON.parse(JSON.stringify(this.raw));
1347
+ }
1348
+ toJSON(indent = 2) {
1349
+ return JSON.stringify(this.raw, null, indent);
1350
+ }
1351
+ };
1352
+
1353
+ // src/client.ts
1354
+ var BACKOFF_SCHEDULE = [1, 2, 4, 8, 16, 32, 60];
1355
+ var MAX_RECONNECT_ATTEMPTS = 10;
1356
+ var PING_INTERVAL = 3e4;
1357
+ var PONG_TIMEOUT = 9e4;
1358
+ var Client = class _Client {
1359
+ /** @internal */
1360
+ _apiKey;
1361
+ /** @internal */
1362
+ _chatUrl;
1363
+ /** @internal */
1364
+ _basicAuth;
1365
+ /** @internal */
1366
+ _activeBrowsers = /* @__PURE__ */ new Map();
1367
+ _ws = null;
1368
+ /** @internal */
1369
+ _apiUrl;
1370
+ _relayUrl;
1371
+ _reconnect;
1372
+ _reconnectAttempt = 0;
1373
+ _reconnecting = false;
1374
+ _closed = false;
1375
+ _pingTimer = null;
1376
+ _pongTimer = null;
1377
+ _lastPongAt = 0;
1378
+ _pendingRents = /* @__PURE__ */ new Map();
1379
+ // keyed by `rent:<scheduleId>` or eventId
1380
+ _pendingResumes = /* @__PURE__ */ new Map();
1381
+ // keyed by sessionId
1382
+ _connectResolve = null;
1383
+ _connectReject = null;
1384
+ constructor(apiKey, opts) {
1385
+ const cfg = resolveConfig(opts);
1386
+ this._apiKey = apiKey;
1387
+ this._apiUrl = cfg.apiUrl;
1388
+ this._relayUrl = cfg.relayUrl;
1389
+ this._chatUrl = cfg.chatUrl;
1390
+ this._basicAuth = cfg.basicAuth;
1391
+ this._reconnect = cfg.reconnect;
1392
+ }
1393
+ /** Factory: create client and connect */
1394
+ static async create(apiKey, opts) {
1395
+ const client = new _Client(apiKey, opts);
1396
+ await client._connect();
1397
+ return client;
1398
+ }
1399
+ /** @internal */
1400
+ _wsSend(msg) {
1401
+ if (!this._ws || this._ws.readyState !== WebSocket.OPEN) {
1402
+ throw new ConnectionLost("WebSocket not connected");
1403
+ }
1404
+ this._ws.send(JSON.stringify(msg));
1405
+ }
1406
+ async search(filters, limit) {
1407
+ const params = new URLSearchParams();
1408
+ if (limit != null) params.set("limit", String(limit));
1409
+ if (filters) {
1410
+ for (const [key, value] of Object.entries(filters)) {
1411
+ if (value != null) params.set(key, String(value));
1412
+ }
1413
+ }
1414
+ const url = `${this._apiUrl}/api/browsers/search?${params.toString()}`;
1415
+ const headers = {
1416
+ "Authorization": `Bearer ${this._apiKey}`
1417
+ };
1418
+ if (this._basicAuth) {
1419
+ const encoded = Buffer.from(`${this._basicAuth[0]}:${this._basicAuth[1]}`).toString("base64");
1420
+ headers["X-Basic-Auth"] = `Basic ${encoded}`;
1421
+ }
1422
+ const resp = await fetch(url, { headers });
1423
+ if (!resp.ok) {
1424
+ throw new TransportError(`Search request failed: ${resp.status} ${resp.statusText}`);
1425
+ }
1426
+ const body = await resp.json();
1427
+ const data = body.data ?? body;
1428
+ return Array.isArray(data) ? data : [];
1429
+ }
1430
+ async listSessions(opts) {
1431
+ const active = opts?.active ?? true;
1432
+ const limit = opts?.limit ?? 50;
1433
+ const params = new URLSearchParams({
1434
+ active: active ? "1" : "0",
1435
+ limit: String(limit)
1436
+ });
1437
+ const url = `${this._apiUrl}/api/agent/sessions?${params.toString()}`;
1438
+ const headers = {
1439
+ "Authorization": `Bearer ${this._apiKey}`
1440
+ };
1441
+ if (this._basicAuth) {
1442
+ const encoded = Buffer.from(`${this._basicAuth[0]}:${this._basicAuth[1]}`).toString("base64");
1443
+ headers["X-Basic-Auth"] = `Basic ${encoded}`;
1444
+ }
1445
+ const resp = await fetch(url, { headers });
1446
+ if (!resp.ok) {
1447
+ throw new TransportError(`listSessions request failed: ${resp.status} ${resp.statusText}`);
1448
+ }
1449
+ const body = await resp.json();
1450
+ const items = body.data ?? body;
1451
+ return Array.isArray(items) ? items : [];
1452
+ }
1453
+ async myBrowsers() {
1454
+ const url = `${this._apiUrl}/api/agent/browsers`;
1455
+ const headers = {
1456
+ "Authorization": `Bearer ${this._apiKey}`
1457
+ };
1458
+ if (this._basicAuth) {
1459
+ const encoded = Buffer.from(`${this._basicAuth[0]}:${this._basicAuth[1]}`).toString("base64");
1460
+ headers["X-Basic-Auth"] = `Basic ${encoded}`;
1461
+ }
1462
+ const resp = await fetch(url, { headers });
1463
+ if (!resp.ok) {
1464
+ throw new TransportError(`myBrowsers request failed: ${resp.status} ${resp.statusText}`);
1465
+ }
1466
+ const body = await resp.json();
1467
+ const items = body.browsers ?? body.data ?? body;
1468
+ return Array.isArray(items) ? items : [];
1469
+ }
1470
+ async rent(scheduleId, opts) {
1471
+ const rentMsg = { type: "rent", browser_id: scheduleId };
1472
+ if (opts?.mode) rentMsg.mode = opts.mode;
1473
+ this._wsSend(rentMsg);
1474
+ const key = `rent:${scheduleId}`;
1475
+ return new Promise((resolve, reject) => {
1476
+ const timer = setTimeout(() => {
1477
+ this._pendingRents.delete(key);
1478
+ reject(new TimeoutError("Rent timed out after 90s"));
1479
+ }, 9e4);
1480
+ this._pendingRents.set(key, {
1481
+ scheduleId,
1482
+ eventId: null,
1483
+ opts,
1484
+ resolve: (match) => {
1485
+ const humanizer = this._resolveHumanizer(opts);
1486
+ const browser = new Browser(this, match, humanizer);
1487
+ this._activeBrowsers.set(browser.sessionId, browser);
1488
+ if (opts?.maskingMode) {
1489
+ browser.configure({ maskingMode: true }).catch(() => {
1490
+ });
1491
+ }
1492
+ if (opts?.fingerprint) {
1493
+ browser.configure({ fingerprint: opts.fingerprint }).catch(() => {
1494
+ });
1495
+ }
1496
+ resolve(browser);
1497
+ },
1498
+ reject,
1499
+ timer
1500
+ });
1501
+ });
1502
+ }
1503
+ async resume(sessionId, opts) {
1504
+ this._wsSend({ type: "resume", session_id: sessionId });
1505
+ return new Promise((resolve, reject) => {
1506
+ const timer = setTimeout(() => {
1507
+ this._pendingResumes.delete(sessionId);
1508
+ reject(new TimeoutError("Resume timed out after 10s"));
1509
+ }, 1e4);
1510
+ this._pendingResumes.set(sessionId, {
1511
+ sessionId,
1512
+ opts,
1513
+ resolve: (match) => {
1514
+ const humanizer = this._resolveHumanizer(opts);
1515
+ const browser = new Browser(this, match, humanizer);
1516
+ this._activeBrowsers.set(browser.sessionId, browser);
1517
+ resolve(browser);
1518
+ },
1519
+ reject,
1520
+ timer
1521
+ });
1522
+ });
1523
+ }
1524
+ async close() {
1525
+ this._closed = true;
1526
+ const closePromises = [];
1527
+ for (const browser of this._activeBrowsers.values()) {
1528
+ closePromises.push(browser.close().catch(() => {
1529
+ }));
1530
+ }
1531
+ await Promise.allSettled(closePromises);
1532
+ for (const [key, pending] of this._pendingRents) {
1533
+ clearTimeout(pending.timer);
1534
+ pending.reject(new Error("Client closed"));
1535
+ }
1536
+ this._pendingRents.clear();
1537
+ for (const [key, pending] of this._pendingResumes) {
1538
+ clearTimeout(pending.timer);
1539
+ pending.reject(new Error("Client closed"));
1540
+ }
1541
+ this._pendingResumes.clear();
1542
+ this._stopHeartbeat();
1543
+ this._closeWs();
1544
+ }
1545
+ async disconnect() {
1546
+ this._closed = true;
1547
+ this._activeBrowsers.clear();
1548
+ this._pendingRents.clear();
1549
+ this._pendingResumes.clear();
1550
+ this._stopHeartbeat();
1551
+ this._closeWs();
1552
+ }
1553
+ // --- Private methods ---
1554
+ async _connect() {
1555
+ return new Promise((resolve, reject) => {
1556
+ this._connectResolve = resolve;
1557
+ this._connectReject = reject;
1558
+ this._openWs();
1559
+ });
1560
+ }
1561
+ _openWs() {
1562
+ const protocols = [`bearer.${this._apiKey}`];
1563
+ this._ws = new WebSocket(this._relayUrl, protocols);
1564
+ this._ws.on("open", () => {
1565
+ this._reconnectAttempt = 0;
1566
+ this._reconnecting = false;
1567
+ this._lastPongAt = Date.now();
1568
+ this._startHeartbeat();
1569
+ if (this._connectResolve) {
1570
+ this._connectResolve();
1571
+ this._connectResolve = null;
1572
+ this._connectReject = null;
1573
+ }
1574
+ });
1575
+ this._ws.on("message", (data) => {
1576
+ this._handleMessage(data);
1577
+ });
1578
+ this._ws.on("close", (code, reason) => {
1579
+ this._stopHeartbeat();
1580
+ const reasonStr = reason.toString();
1581
+ if (code === 4401 || code === 4403) {
1582
+ const err = new AuthError(reasonStr || `Auth failed (${code})`);
1583
+ if (this._connectReject) {
1584
+ this._connectReject(err);
1585
+ this._connectResolve = null;
1586
+ this._connectReject = null;
1587
+ }
1588
+ return;
1589
+ }
1590
+ if (!this._closed && this._reconnect) {
1591
+ this._scheduleReconnect();
1592
+ }
1593
+ });
1594
+ this._ws.on("error", (err) => {
1595
+ if (this._connectReject) {
1596
+ this._connectReject(new TransportError(err.message));
1597
+ this._connectResolve = null;
1598
+ this._connectReject = null;
1599
+ }
1600
+ });
1601
+ }
1602
+ _closeWs() {
1603
+ if (this._ws) {
1604
+ try {
1605
+ this._ws.removeAllListeners();
1606
+ this._ws.close();
1607
+ } catch {
1608
+ }
1609
+ this._ws = null;
1610
+ }
1611
+ }
1612
+ _scheduleReconnect() {
1613
+ if (this._closed || this._reconnecting) return;
1614
+ if (this._reconnectAttempt >= MAX_RECONNECT_ATTEMPTS) {
1615
+ const err = new ConnectionLost("Max reconnection attempts exceeded");
1616
+ this._rejectAllPending(err);
1617
+ return;
1618
+ }
1619
+ this._reconnecting = true;
1620
+ const backoffIdx = Math.min(this._reconnectAttempt, BACKOFF_SCHEDULE.length - 1);
1621
+ const delay = BACKOFF_SCHEDULE[backoffIdx] * 1e3;
1622
+ this._reconnectAttempt++;
1623
+ setTimeout(() => {
1624
+ if (this._closed) return;
1625
+ this._closeWs();
1626
+ this._connectResolve = () => {
1627
+ for (const browser of this._activeBrowsers.values()) {
1628
+ this._wsSend({ type: "resume", session_id: browser.sessionId });
1629
+ }
1630
+ };
1631
+ this._connectReject = () => {
1632
+ this._reconnecting = false;
1633
+ this._scheduleReconnect();
1634
+ };
1635
+ this._openWs();
1636
+ }, delay);
1637
+ }
1638
+ _startHeartbeat() {
1639
+ this._stopHeartbeat();
1640
+ this._pingTimer = setInterval(() => {
1641
+ try {
1642
+ this._wsSend({ type: "ping" });
1643
+ } catch {
1644
+ }
1645
+ }, PING_INTERVAL);
1646
+ this._pongTimer = setTimeout(() => {
1647
+ this._checkPongTimeout();
1648
+ }, PONG_TIMEOUT);
1649
+ }
1650
+ _stopHeartbeat() {
1651
+ if (this._pingTimer) {
1652
+ clearInterval(this._pingTimer);
1653
+ this._pingTimer = null;
1654
+ }
1655
+ if (this._pongTimer) {
1656
+ clearTimeout(this._pongTimer);
1657
+ this._pongTimer = null;
1658
+ }
1659
+ }
1660
+ _checkPongTimeout() {
1661
+ if (this._closed) return;
1662
+ const elapsed = Date.now() - this._lastPongAt;
1663
+ if (elapsed >= PONG_TIMEOUT) {
1664
+ this._closeWs();
1665
+ if (this._reconnect) {
1666
+ this._scheduleReconnect();
1667
+ }
1668
+ } else {
1669
+ this._pongTimer = setTimeout(() => {
1670
+ this._checkPongTimeout();
1671
+ }, PONG_TIMEOUT - elapsed);
1672
+ }
1673
+ }
1674
+ _handleMessage(data) {
1675
+ let msg;
1676
+ try {
1677
+ msg = JSON.parse(data.toString());
1678
+ } catch {
1679
+ return;
1680
+ }
1681
+ const type = String(msg.type ?? "");
1682
+ const sessionId = msg.session_id ? String(msg.session_id) : null;
1683
+ switch (type) {
1684
+ case "pong":
1685
+ this._lastPongAt = Date.now();
1686
+ break;
1687
+ case "rent_pending":
1688
+ this._onRentPending(msg);
1689
+ break;
1690
+ case "match":
1691
+ this._onMatch(msg);
1692
+ break;
1693
+ case "rent.error":
1694
+ this._onRentError(msg);
1695
+ break;
1696
+ case "resume_ok":
1697
+ this._onResumeOk(msg);
1698
+ break;
1699
+ case "resume_failed":
1700
+ this._onResumeFailed(msg);
1701
+ break;
1702
+ case "cdp_response":
1703
+ if (sessionId) {
1704
+ const browser = this._activeBrowsers.get(sessionId);
1705
+ browser?._onCdpResponse(msg);
1706
+ }
1707
+ break;
1708
+ case "cdp_event":
1709
+ if (sessionId) {
1710
+ const browser = this._activeBrowsers.get(sessionId);
1711
+ browser?._onCdpEvent(msg);
1712
+ }
1713
+ break;
1714
+ case "tab_opened":
1715
+ if (sessionId) {
1716
+ const browser = this._activeBrowsers.get(sessionId);
1717
+ browser?._onTabOpened(msg);
1718
+ }
1719
+ break;
1720
+ case "session.ended":
1721
+ if (sessionId) {
1722
+ const browser = this._activeBrowsers.get(sessionId);
1723
+ browser?._onSessionEnded(msg);
1724
+ }
1725
+ break;
1726
+ case "session.provider_disconnected":
1727
+ if (sessionId) {
1728
+ const browser = this._activeBrowsers.get(sessionId);
1729
+ browser?._onProviderDisconnected();
1730
+ }
1731
+ break;
1732
+ case "session.provider_reconnected":
1733
+ if (sessionId) {
1734
+ const browser = this._activeBrowsers.get(sessionId);
1735
+ browser?._onProviderReconnected();
1736
+ }
1737
+ break;
1738
+ case "user_events":
1739
+ if (sessionId) {
1740
+ const browser = this._activeBrowsers.get(sessionId);
1741
+ browser?._onUserEvents(msg);
1742
+ }
1743
+ break;
1744
+ case "chat.message":
1745
+ if (sessionId) {
1746
+ const browser = this._activeBrowsers.get(sessionId);
1747
+ browser?._onChatMessage(msg.payload ?? msg);
1748
+ }
1749
+ break;
1750
+ case "chat.read":
1751
+ if (sessionId) {
1752
+ const browser = this._activeBrowsers.get(sessionId);
1753
+ browser?._onChatRead(msg.payload ?? msg);
1754
+ }
1755
+ break;
1756
+ case "chat.send_ack":
1757
+ if (sessionId) {
1758
+ const browser = this._activeBrowsers.get(sessionId);
1759
+ browser?._onChatSendAck(msg);
1760
+ }
1761
+ break;
1762
+ case "chat.error":
1763
+ if (sessionId) {
1764
+ const browser = this._activeBrowsers.get(sessionId);
1765
+ browser?._onChatSendError(msg);
1766
+ }
1767
+ break;
1768
+ case "error":
1769
+ this._onError(msg);
1770
+ break;
1771
+ }
1772
+ }
1773
+ _onRentPending(msg) {
1774
+ const eventId = String(msg.event_id ?? "");
1775
+ for (const [key, pending] of this._pendingRents) {
1776
+ if (!pending.eventId) {
1777
+ pending.eventId = eventId;
1778
+ this._pendingRents.delete(key);
1779
+ this._pendingRents.set(`event:${eventId}`, pending);
1780
+ break;
1781
+ }
1782
+ }
1783
+ }
1784
+ _onMatch(msg) {
1785
+ const eventId = String(msg.event_id ?? "");
1786
+ const scheduleId = Number(msg.schedule_id ?? 0);
1787
+ const sessionId = String(msg.session_id ?? "");
1788
+ if (msg.requires_ack) {
1789
+ try {
1790
+ this._wsSend({ type: "match_ack", session_id: sessionId });
1791
+ } catch {
1792
+ }
1793
+ }
1794
+ let pending = this._pendingRents.get(`event:${eventId}`);
1795
+ if (!pending) {
1796
+ pending = this._pendingRents.get(`rent:${scheduleId}`);
1797
+ }
1798
+ if (pending) {
1799
+ clearTimeout(pending.timer);
1800
+ const key = pending.eventId ? `event:${pending.eventId}` : `rent:${pending.scheduleId}`;
1801
+ this._pendingRents.delete(key);
1802
+ const match = {
1803
+ session_id: sessionId,
1804
+ schedule_id: scheduleId,
1805
+ event_id: eventId || null,
1806
+ chat_topic_id: msg.chat_topic_id ? String(msg.chat_topic_id) : null,
1807
+ provider_user_id: msg.provider_user_id != null ? Number(msg.provider_user_id) : null,
1808
+ started_at: Date.now(),
1809
+ browser_info: msg.browser_info ?? {}
1810
+ };
1811
+ pending.resolve(match);
1812
+ }
1813
+ }
1814
+ _onRentError(msg) {
1815
+ const eventId = String(msg.event_id ?? "");
1816
+ const code = String(msg.code ?? "");
1817
+ const message = String(msg.message ?? "");
1818
+ let pending = this._pendingRents.get(`event:${eventId}`);
1819
+ if (!pending) {
1820
+ for (const [key, p] of this._pendingRents) {
1821
+ pending = p;
1822
+ this._pendingRents.delete(key);
1823
+ break;
1824
+ }
1825
+ } else {
1826
+ this._pendingRents.delete(`event:${eventId}`);
1827
+ }
1828
+ if (pending) {
1829
+ clearTimeout(pending.timer);
1830
+ if (code === "provider_offline") {
1831
+ pending.reject(new ProviderOffline(message));
1832
+ } else {
1833
+ pending.reject(new TransportError(message || `Rent error: ${code}`));
1834
+ }
1835
+ }
1836
+ }
1837
+ _onResumeOk(msg) {
1838
+ const sessionId = String(msg.session_id ?? "");
1839
+ const pending = this._pendingResumes.get(sessionId);
1840
+ if (!pending) return;
1841
+ clearTimeout(pending.timer);
1842
+ this._pendingResumes.delete(sessionId);
1843
+ const match = {
1844
+ session_id: sessionId,
1845
+ event_id: msg.event_id != null ? String(msg.event_id) : null,
1846
+ schedule_id: Number(msg.schedule_id ?? 0),
1847
+ chat_topic_id: msg.chat_topic_id ? String(msg.chat_topic_id) : null,
1848
+ provider_user_id: msg.provider_user_id != null ? Number(msg.provider_user_id) : null,
1849
+ started_at: Date.now(),
1850
+ browser_info: msg.browser_info ?? {}
1851
+ };
1852
+ pending.resolve(match);
1853
+ }
1854
+ _onResumeFailed(msg) {
1855
+ const sessionId = String(msg.session_id ?? "");
1856
+ const reason = String(msg.reason ?? "");
1857
+ const pending = this._pendingResumes.get(sessionId);
1858
+ if (!pending) return;
1859
+ clearTimeout(pending.timer);
1860
+ this._pendingResumes.delete(sessionId);
1861
+ switch (reason) {
1862
+ case "expired":
1863
+ pending.reject(new SessionExpired());
1864
+ break;
1865
+ case "not_owner":
1866
+ pending.reject(new NotOwner());
1867
+ break;
1868
+ case "not_found":
1869
+ pending.reject(new SessionNotFound());
1870
+ break;
1871
+ default:
1872
+ pending.reject(new SessionNotFound(reason));
1873
+ }
1874
+ }
1875
+ _onError(msg) {
1876
+ const code = Number(msg.code ?? 0);
1877
+ const reason = String(msg.reason ?? msg.message ?? "");
1878
+ const sessionId = msg.session_id ? String(msg.session_id) : null;
1879
+ if (sessionId) {
1880
+ const browser = this._activeBrowsers.get(sessionId);
1881
+ if (browser) {
1882
+ switch (code) {
1883
+ case -1011:
1884
+ browser._onSessionEnded({ reason: "heartbeat_timeout" });
1885
+ return;
1886
+ case -1012:
1887
+ browser._onError({ reason: "insufficient_funds" });
1888
+ return;
1889
+ case -1013:
1890
+ browser._onError({ reason: "rate_limit_exceeded" });
1891
+ return;
1892
+ case -1015:
1893
+ browser._onSessionEnded({ reason: "provider_declined" });
1894
+ return;
1895
+ case -1018:
1896
+ browser._onSessionEnded({ reason: "killed" });
1897
+ return;
1898
+ case -1050:
1899
+ browser._onError({ reason: `cdp_unrecoverable: ${reason}` });
1900
+ return;
1901
+ default:
1902
+ browser._onError(msg);
1903
+ return;
1904
+ }
1905
+ }
1906
+ }
1907
+ let err;
1908
+ switch (code) {
1909
+ case -1012:
1910
+ err = new InsufficientFunds(reason);
1911
+ this._rejectAllPending(err);
1912
+ break;
1913
+ case -1013:
1914
+ err = new RateLimitExceeded(0, reason);
1915
+ this._rejectAllPending(err);
1916
+ break;
1917
+ case -1015:
1918
+ err = new ProviderOffline(reason);
1919
+ this._rejectFirstPendingRent(err);
1920
+ break;
1921
+ default:
1922
+ err = new CekiBrowserError(reason || `relay error ${code}`);
1923
+ this._rejectFirstPendingRent(err);
1924
+ break;
1925
+ }
1926
+ }
1927
+ _rejectFirstPendingRent(err) {
1928
+ const eventId = this._pendingRents.keys().next().value;
1929
+ if (eventId != null) {
1930
+ const pending = this._pendingRents.get(eventId);
1931
+ clearTimeout(pending.timer);
1932
+ this._pendingRents.delete(eventId);
1933
+ pending.reject(err);
1934
+ }
1935
+ }
1936
+ _rejectAllPending(err) {
1937
+ for (const [key, pending] of this._pendingRents) {
1938
+ clearTimeout(pending.timer);
1939
+ pending.reject(err);
1940
+ }
1941
+ this._pendingRents.clear();
1942
+ for (const [key, pending] of this._pendingResumes) {
1943
+ clearTimeout(pending.timer);
1944
+ pending.reject(err);
1945
+ }
1946
+ this._pendingResumes.clear();
1947
+ }
1948
+ _resolveHumanizer(opts) {
1949
+ if (process.env.CEKI_HUMAN_DISABLE === "1") return null;
1950
+ const human = opts?.human;
1951
+ if (human === null || human === void 0) {
1952
+ const envPreset = process.env.CEKI_HUMAN_PROFILE;
1953
+ const envPath = process.env.CEKI_HUMAN_PROFILE_PATH;
1954
+ if (envPath) return new Humanizer(HumanProfile.load(envPath));
1955
+ if (envPreset) return new Humanizer(HumanProfile.loadPreset(envPreset));
1956
+ return new Humanizer(HumanProfile.loadPreset("natural"));
1957
+ }
1958
+ if (human === "natural" || human === "careful") {
1959
+ return new Humanizer(HumanProfile.loadPreset(human));
1960
+ }
1961
+ return null;
1962
+ }
1963
+ };
1964
+ async function connect(apiKey, opts) {
1965
+ return Client.create(apiKey, opts);
1966
+ }
1967
+ export {
1968
+ AuthError,
1969
+ Browser,
1970
+ BrowserChat,
1971
+ BrowserProfile,
1972
+ CaptchaError,
1973
+ CaptchaTimeoutError,
1974
+ CdpUnrecoverable,
1975
+ CekiBrowserError,
1976
+ ChatSendFailed,
1977
+ Client,
1978
+ ConnectionLost,
1979
+ HumanProfile,
1980
+ Humanizer,
1981
+ InsufficientFunds,
1982
+ NotOwner,
1983
+ ProviderDisconnected,
1984
+ ProviderOffline,
1985
+ RateLimitExceeded,
1986
+ SessionEnded,
1987
+ SessionExpired,
1988
+ SessionNotFound,
1989
+ TimeoutError,
1990
+ TransportError,
1991
+ connect
1992
+ };
1993
+ //# sourceMappingURL=index.js.map