@xbrowser/cli 1.34.0 → 1.36.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.
Files changed (63) hide show
  1. package/README.md +137 -80
  2. package/dist/{browser-ZWEJ663M.js → browser-IEZOLTHA.js} +3 -3
  3. package/dist/{browser-GXL6V2JW.js → browser-NHQDQRXB.js} +3 -2
  4. package/dist/{browser-X7LO42UI.js → browser-XELRNYBL.js} +3 -3
  5. package/dist/cdp-driver-CCG3OJUQ.js +49 -0
  6. package/dist/{cdp-driver-GCFEMRFJ.js → cdp-driver-HBMCD7NU.js} +2 -2
  7. package/dist/{cdp-driver-QOUSRPR2.js → cdp-driver-MJD7YIQI.js} +2 -2
  8. package/dist/{cdp-driver-4MNHDU2J.js → cdp-driver-YJU4GRSV.js} +196 -95
  9. package/dist/chunk-2IOFJ6A3.js +149 -0
  10. package/dist/chunk-3SZTCI33.js +147 -0
  11. package/dist/{chunk-VYTQLZAX.js → chunk-5UXRFNXG.js} +28 -2
  12. package/dist/{chunk-NAIHOVDZ.js → chunk-63526WET.js} +2 -8
  13. package/dist/{chunk-YIL44YDX.js → chunk-6DS3T6UK.js} +196 -95
  14. package/dist/{chunk-J4NOL5XX.js → chunk-7KVH6MQG.js} +1099 -204
  15. package/dist/{chunk-N7DFS4V7.js → chunk-ENTENDF5.js} +9 -3
  16. package/dist/{chunk-R57Z5KZX.js → chunk-GVJ2UY65.js} +1078 -185
  17. package/dist/{chunk-3E6VVMK2.js → chunk-GYGVHWG3.js} +272 -1460
  18. package/dist/{chunk-2KPI5GKP.js → chunk-IYCB2KJL.js} +2 -8
  19. package/dist/chunk-KSB4PMCY.js +1380 -0
  20. package/dist/{chunk-RMYKVZIZ.js → chunk-L6QJUB33.js} +28 -2
  21. package/dist/{chunk-TBJUSUKF.js → chunk-NBT2CE3H.js} +140 -96
  22. package/dist/{chunk-DZ6XEPAH.js → chunk-PGEZJ6V3.js} +9 -3
  23. package/dist/chunk-RGL2YTWQ.js +64 -0
  24. package/dist/chunk-RL5FFY2Q.js +150 -0
  25. package/dist/{chunk-43TEMG4U.js → chunk-SBMVECDS.js} +3 -1
  26. package/dist/{chunk-BND5O5KC.js → chunk-SQLICOPZ.js} +177 -240
  27. package/dist/{chunk-O5PASNI7.js → chunk-UUW5EGES.js} +139 -237
  28. package/dist/{chunk-SH6OTPXN.js → chunk-WGWPWNIA.js} +78 -17
  29. package/dist/chunk-YW4BIJQL.js +489 -0
  30. package/dist/{chunk-R7AG7ONI.js → chunk-ZCZELQCS.js} +196 -95
  31. package/dist/chunk-ZR4NHTUV.js +39 -0
  32. package/dist/cli.js +2284 -307
  33. package/dist/convert-JNOM6H6A.js +490 -0
  34. package/dist/{convert-R3XXYKC6.js → convert-K6TLQEPX.js} +5 -3
  35. package/dist/{daemon-client-7D6EZNOE.js → daemon-client-AKQK5IIB.js} +79 -18
  36. package/dist/{daemon-client-HEGAXWGK.js → daemon-client-BBPQWLUX.js} +7 -1
  37. package/dist/daemon-main.js +2305 -164
  38. package/dist/human-interaction-BGAZC3JR.js +12 -0
  39. package/dist/human-interaction-TPKPEE7A.js +11 -0
  40. package/dist/human-interaction-ZSD3IX6Q.js +12 -0
  41. package/dist/index.d.ts +251 -97
  42. package/dist/index.js +2929 -298
  43. package/dist/{launcher-TOLF23ET.js → launcher-AUJK5QQM.js} +1 -1
  44. package/dist/{launcher-ZYB32LSZ.js → launcher-QBAZ6F5L.js} +1 -1
  45. package/dist/mistake-ledger-2B4N54CO.js +29 -0
  46. package/dist/{recovery-ORHJSBDN.js → recovery-5AGY7CV3.js} +4 -5
  47. package/dist/{recovery-XDKN63O5.js → recovery-7NRRHLS4.js} +2 -2
  48. package/dist/{recovery-JFGV3DBX.js → recovery-TIBRFGRP.js} +2 -2
  49. package/dist/run-routes-64IBBVOX.js +24 -0
  50. package/dist/run-routes-FG4LIACU.js +26 -0
  51. package/dist/{server-5U56MQD2.js → server-K22LJBO2.js} +5 -4
  52. package/dist/{server-R4OYD7ZC.js → server-ZHZDJJMG.js} +5 -3
  53. package/dist/{session-recorder-RUNQOJ7T.js → session-recorder-CNTY5KXF.js} +1 -1
  54. package/dist/{session-recorder-N7VEKJVW.js → session-recorder-KMKSQWEP.js} +1 -1
  55. package/dist/{session-replayer-GHEFFNGV.js → session-replayer-4TCLQI76.js} +1 -1
  56. package/dist/viewer-routes-CEO7HWCB.js +11 -0
  57. package/package.json +3 -2
  58. package/dist/chunk-ETCO4SNK.js +0 -206
  59. package/dist/chunk-QCV2PI7U.js +0 -184
  60. package/dist/convert-LB3GJTLR.js +0 -207
  61. package/dist/human-interaction-ISMMOJOK.js +0 -8
  62. package/dist/human-interaction-STTZZZ5N.js +0 -8
  63. package/dist/human-interaction-UXFYV7DU.js +0 -8
@@ -1,152 +1,6 @@
1
- import {
2
- getCaptchaConfig
3
- } from "./chunk-ZTHE5RBZ.js";
4
-
5
1
  // src/human-interaction.ts
6
2
  import { execFile } from "child_process";
7
3
 
8
- // src/screencast.ts
9
- var ScreencastCapturer = class {
10
- interval;
11
- quality;
12
- type;
13
- maxWidth;
14
- maxHeight;
15
- isCapturing = false;
16
- frameCallback = null;
17
- // CDP Cast state
18
- cdpSession = null;
19
- sessionId = "";
20
- // Fallback polling state
21
- fallbackTimer = null;
22
- constructor(options = {}) {
23
- this.interval = options.interval || 100;
24
- this.quality = options.quality || 60;
25
- this.type = options.type || "jpeg";
26
- this.maxWidth = options.width || 1920;
27
- this.maxHeight = options.height || 1080;
28
- }
29
- /**
30
- * Start screencast capture using CDP Page.startScreencast.
31
- *
32
- * If CDP session creation fails (non-Chromium, restricted access),
33
- * automatically falls back to `page.screenshot()` polling.
34
- */
35
- async startCapture(page, sessionId, onFrame) {
36
- if (this.isCapturing) {
37
- throw new Error("Screencast is already capturing");
38
- }
39
- this.isCapturing = true;
40
- this.frameCallback = onFrame;
41
- this.sessionId = sessionId;
42
- try {
43
- const cdp = await page.context().newCDPSession(page);
44
- this.cdpSession = cdp;
45
- cdp.on("Page.screencastFrame", async (params) => {
46
- const cb = this.frameCallback;
47
- if (!cb) return;
48
- cdp.send("Page.screencastFrameAck", { sessionId: params.sessionId }).catch(() => {
49
- });
50
- const logicalViewport = page.viewportSize();
51
- const viewport = logicalViewport ?? {
52
- width: params.metadata?.deviceWidth || this.maxWidth,
53
- height: params.metadata?.deviceHeight || this.maxHeight
54
- };
55
- cb({
56
- id: crypto.randomUUID(),
57
- sessionId: this.sessionId,
58
- timestamp: Date.now(),
59
- data: Buffer.from(params.data, "base64"),
60
- url: page.url(),
61
- viewport
62
- });
63
- });
64
- await cdp.send("Page.startScreencast", {
65
- format: this.type === "png" ? "png" : "jpeg",
66
- quality: this.type === "jpeg" ? this.quality : void 0,
67
- maxWidth: this.maxWidth,
68
- maxHeight: this.maxHeight
69
- });
70
- } catch {
71
- this.cdpSession = null;
72
- this.startFallbackPolling(page, sessionId);
73
- }
74
- }
75
- /**
76
- * Fallback: periodic page.screenshot() polling when CDP Cast is unavailable.
77
- */
78
- startFallbackPolling(page, sessionId) {
79
- const captureLoop = async () => {
80
- if (!this.frameCallback) return;
81
- try {
82
- const frame = await this.captureFrame(page, sessionId);
83
- this.frameCallback(frame);
84
- } catch {
85
- }
86
- };
87
- captureLoop();
88
- this.fallbackTimer = setInterval(captureLoop, this.interval);
89
- }
90
- /**
91
- * Capture a single screenshot frame from the page (fallback mode).
92
- */
93
- async captureFrame(page, sessionId) {
94
- let viewport = page.viewportSize();
95
- if (!viewport) {
96
- try {
97
- viewport = await page.evaluate(() => ({ width: window.innerWidth, height: window.innerHeight }));
98
- } catch {
99
- viewport = { width: 1920, height: 1080 };
100
- }
101
- }
102
- const screenshot = await page.screenshot({
103
- type: this.type,
104
- quality: this.type === "jpeg" ? this.quality : void 0
105
- });
106
- return {
107
- id: crypto.randomUUID(),
108
- sessionId,
109
- timestamp: Date.now(),
110
- data: screenshot,
111
- url: page.url(),
112
- viewport: viewport || { width: 0, height: 0 }
113
- };
114
- }
115
- /**
116
- * Stop the current screencast capture.
117
- */
118
- async stopCapture() {
119
- if (this.cdpSession) {
120
- try {
121
- this.cdpSession.off("Page.screencastFrame", () => {
122
- });
123
- await this.cdpSession.send("Page.stopScreencast");
124
- } catch {
125
- }
126
- try {
127
- await this.cdpSession.detach();
128
- } catch {
129
- }
130
- this.cdpSession = null;
131
- }
132
- if (this.fallbackTimer) {
133
- clearInterval(this.fallbackTimer);
134
- this.fallbackTimer = null;
135
- }
136
- this.isCapturing = false;
137
- this.frameCallback = null;
138
- }
139
- isActive() {
140
- return this.isCapturing;
141
- }
142
- setInterval(interval) {
143
- this.interval = interval;
144
- }
145
- setQuality(quality) {
146
- this.quality = quality;
147
- }
148
- };
149
-
150
4
  // src/captcha-detector.ts
151
5
  var CAPTCHA_SELECTORS = [
152
6
  { selector: 'iframe[src*="recaptcha"]', type: "recaptcha", confidence: "high" },
@@ -317,28 +171,90 @@ var WebhookNotifier = class {
317
171
  }
318
172
  };
319
173
 
174
+ // src/config.ts
175
+ import { homedir, tmpdir } from "os";
176
+ import { join } from "path";
177
+ import { loadConfig as coreLoadConfig, saveConfig as coreSaveConfig } from "@dyyz1993/xcli-core";
178
+ function getConfigSource() {
179
+ return { configDir: join(homedir() || tmpdir(), ".xbrowser") };
180
+ }
181
+ function loadConfig() {
182
+ return coreLoadConfig(getConfigSource());
183
+ }
184
+ function getConfigValue(key) {
185
+ const parts = key.split(".");
186
+ let obj = loadConfig();
187
+ for (const part of parts) {
188
+ if (obj && typeof obj === "object") {
189
+ obj = obj[part];
190
+ } else {
191
+ return void 0;
192
+ }
193
+ }
194
+ return obj;
195
+ }
196
+ function getCaptchaConfig() {
197
+ const config = loadConfig();
198
+ return {
199
+ notifyUrl: process.env.XBROWSER_NOTIFY_URL || config.captcha?.notifyUrl,
200
+ autoOpen: process.env.XBROWSER_AUTO_OPEN === "true" || config.captcha?.autoOpen === true,
201
+ timeout: parseInt(process.env.XBROWSER_CAPTCHA_TIMEOUT || "") || config.captcha?.timeout || 120,
202
+ previewPort: parseInt(process.env.XBROWSER_PREVIEW_PORT || "") || config.preview?.port || 9223
203
+ };
204
+ }
205
+ function resolveScreenshotsDir() {
206
+ const configured = getConfigValue("screenshots.dir");
207
+ if (typeof configured === "string" && configured.trim()) return configured.trim();
208
+ return join(tmpdir(), "xbrowser", "screenshots");
209
+ }
210
+
320
211
  // src/human-interaction.ts
212
+ var DEFAULT_HUMAN_INTERACTION_SESSION = "default";
213
+ var activeWaitsByServer = /* @__PURE__ */ new WeakMap();
214
+ function acquireWaitSlot(server, sessionName) {
215
+ let sessions = activeWaitsByServer.get(server);
216
+ if (!sessions) {
217
+ sessions = /* @__PURE__ */ new Map();
218
+ activeWaitsByServer.set(server, sessions);
219
+ }
220
+ if (sessions.has(sessionName)) return false;
221
+ sessions.set(sessionName, "starting");
222
+ return true;
223
+ }
224
+ function releaseWaitSlot(server, sessionName) {
225
+ activeWaitsByServer.get(server)?.delete(sessionName);
226
+ }
227
+ function isHumanWaitActive(server, sessionName) {
228
+ return activeWaitsByServer.get(server)?.get(sessionName) === "waiting";
229
+ }
321
230
  var HumanInteractionManager = class {
322
231
  wsServer;
323
232
  page;
324
- capturer;
233
+ sessionName;
325
234
  webhook;
326
235
  autoOpen;
327
- constructor(wsServer, page) {
236
+ constructor(wsServer, page, sessionName) {
328
237
  this.wsServer = wsServer;
329
238
  this.page = page;
330
- this.capturer = new ScreencastCapturer();
239
+ this.sessionName = sessionName && sessionName.trim() ? sessionName.trim() : DEFAULT_HUMAN_INTERACTION_SESSION;
331
240
  const cfg = getCaptchaConfig();
332
241
  this.webhook = new WebhookNotifier(cfg.notifyUrl);
333
242
  this.autoOpen = cfg.autoOpen;
334
- this.wsServer.registerSession("default", page);
243
+ this.wsServer.registerSession(this.sessionName, page);
244
+ }
245
+ /** The named session this manager interacts with. */
246
+ get session() {
247
+ return this.sessionName;
248
+ }
249
+ buildPreviewUrl() {
250
+ return `http://localhost:${this.wsServer.getPort()}/preview/${encodeURIComponent(this.sessionName)}`;
335
251
  }
336
252
  async sendWebhook(event, overrides = {}) {
337
253
  await this.webhook.notify({
338
254
  event,
339
255
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
340
256
  url: this.page.url(),
341
- previewUrl: `http://localhost:${this.wsServer.getPort()}`,
257
+ previewUrl: this.buildPreviewUrl(),
342
258
  ...overrides
343
259
  });
344
260
  }
@@ -355,11 +271,16 @@ var HumanInteractionManager = class {
355
271
  /**
356
272
  * Wait for a human to solve a CAPTCHA or complete an interaction.
357
273
  *
358
- * Starts screencast streaming, sends webhook and broadcast notifications,
359
- * then polls for CAPTCHA resolution or waits for a manual solve signal.
274
+ * Sends session-scoped notifications and waits for CAPTCHA resolution
275
+ * or a manual solve signal from a viewer bound to this session.
276
+ *
277
+ * Only one wait may be in flight per session per WSServer. The wait
278
+ * slot, listener, and timers are cleaned
279
+ * up on every exit path (auto-detected / preview / timeout / error).
360
280
  *
361
281
  * @param options - Configuration for timeout, auto-detection, and reason text.
362
282
  * @returns Result indicating whether the CAPTCHA was solved and by what method.
283
+ * @throws If a wait is already in progress for this session.
363
284
  */
364
285
  async waitForHuman(options = {}) {
365
286
  const {
@@ -368,102 +289,118 @@ var HumanInteractionManager = class {
368
289
  autoDetect = true,
369
290
  detectInterval = 2e3
370
291
  } = options;
371
- const captcha = await CaptchaDetector.detect(this.page);
372
- const captchaInfo = captcha.detected ? captcha : void 0;
373
- await this.capturer.startCapture(this.page, "default", (frame) => {
374
- this.wsServer.broadcast({
375
- type: "screenshot",
376
- data: {
377
- sessionId: frame.sessionId,
378
- id: frame.id,
379
- timestamp: frame.timestamp,
380
- data: frame.data,
381
- url: frame.url,
382
- viewport: frame.viewport
383
- }
384
- });
385
- });
386
- const previewUrl = `http://localhost:${this.wsServer.getPort()}`;
387
- await this.sendWebhook("captcha-detected", {
388
- reason: captchaInfo ? `${captchaInfo.type ?? "unknown"} CAPTCHA detected` : reason,
389
- timeout,
390
- targetUrl: this.page.url()
391
- });
392
- this.wsServer.broadcast({
393
- type: "captcha-detected",
394
- sessionId: "default",
395
- url: this.page.url(),
396
- reason: captchaInfo ? `${captchaInfo.type ?? "unknown"} CAPTCHA detected` : reason,
397
- timeout
398
- });
399
- this.tryAutoOpen(previewUrl);
400
- console.log("");
401
- console.log("\u26A0\uFE0F \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550");
402
- console.log(`\u26A0\uFE0F ${captchaInfo ? (captchaInfo.type ?? "UNKNOWN").toUpperCase() + " CAPTCHA" : "INTERACTION"} REQUIRED`);
403
- console.log(`\u26A0\uFE0F URL: ${this.page.url()}`);
404
- console.log(`\u26A0\uFE0F `);
405
- console.log(`\u26A0\uFE0F Solve via:`);
406
- console.log(`\u26A0\uFE0F \u{1F4FA} Preview: ${previewUrl}`);
407
- console.log(`\u26A0\uFE0F \u{1F310} Direct: ${this.page.url()}`);
408
- console.log(`\u26A0\uFE0F \u23ED\uFE0F Skip`);
409
- console.log(`\u26A0\uFE0F \u274C Abort`);
410
- console.log(`\u26A0\uFE0F `);
411
- console.log(`\u26A0\uFE0F \u23F3 Waiting... (${timeout}s timeout)`);
412
- console.log("\u26A0\uFE0F \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550");
413
- console.log("");
414
- return new Promise((resolve) => {
415
- let resolved = false;
416
- let pollTimer = null;
417
- let timeoutTimer = null;
418
- const cleanup = () => {
419
- if (pollTimer) clearInterval(pollTimer);
420
- if (timeoutTimer) clearTimeout(timeoutTimer);
421
- this.wsServer.removeListener("human-solved", onHumanSolved);
422
- this.capturer.stopCapture();
423
- };
424
- if (autoDetect && captchaInfo) {
425
- pollTimer = setInterval(async () => {
426
- if (resolved) return;
427
- try {
428
- const solved = await CaptchaDetector.isSolved(this.page, captchaInfo.selector);
429
- if (solved) {
430
- resolved = true;
431
- cleanup();
432
- this.wsServer.broadcast({ type: "resolved", sessionId: "default" });
433
- console.log("\u2705 CAPTCHA auto-detected as solved!");
434
- this.sendWebhook("captcha-resolved", { reason: "auto-detected" });
435
- resolve({ solved: true, method: "auto-detected" });
436
- }
437
- } catch {
438
- }
439
- }, detectInterval);
292
+ if (!acquireWaitSlot(this.wsServer, this.sessionName)) {
293
+ throw new Error(
294
+ `waitForHuman already in progress for session "${this.sessionName}" \u2014 only one human-interaction wait per session is allowed`
295
+ );
296
+ }
297
+ let slotReleased = false;
298
+ const releaseSlot = () => {
299
+ if (!slotReleased) {
300
+ slotReleased = true;
301
+ releaseWaitSlot(this.wsServer, this.sessionName);
440
302
  }
441
- const onHumanSolved = () => {
442
- if (!resolved) {
443
- resolved = true;
444
- cleanup();
445
- this.wsServer.broadcast({ type: "resolved", sessionId: "default" });
446
- console.log("\u2705 CAPTCHA solved via preview!");
447
- this.sendWebhook("captcha-resolved", { reason: "preview" });
448
- resolve({ solved: true, method: "preview" });
303
+ };
304
+ try {
305
+ const captcha = await CaptchaDetector.detect(this.page);
306
+ const captchaInfo = captcha.detected ? captcha : void 0;
307
+ const previewUrl = this.buildPreviewUrl();
308
+ const waitReason = captchaInfo ? `${captchaInfo.type ?? "unknown"} CAPTCHA detected` : reason;
309
+ console.log("");
310
+ console.log("\u26A0\uFE0F \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550");
311
+ console.log(`\u26A0\uFE0F ${captchaInfo ? (captchaInfo.type ?? "UNKNOWN").toUpperCase() + " CAPTCHA" : "INTERACTION"} REQUIRED (session: ${this.sessionName})`);
312
+ console.log(`\u26A0\uFE0F URL: ${this.page.url()}`);
313
+ console.log(`\u26A0\uFE0F `);
314
+ console.log(`\u26A0\uFE0F Solve via:`);
315
+ console.log(`\u26A0\uFE0F \u{1F4FA} Preview: ${previewUrl}`);
316
+ console.log(`\u26A0\uFE0F \u{1F310} Direct: ${this.page.url()}`);
317
+ console.log(`\u26A0\uFE0F \u23ED\uFE0F Skip`);
318
+ console.log(`\u26A0\uFE0F \u274C Abort`);
319
+ console.log(`\u26A0\uFE0F `);
320
+ console.log(`\u26A0\uFE0F \u23F3 Waiting... (${timeout}s timeout)`);
321
+ console.log("\u26A0\uFE0F \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550");
322
+ console.log("");
323
+ return await new Promise((resolve) => {
324
+ let resolved = false;
325
+ let pollTimer = null;
326
+ let timeoutTimer = null;
327
+ const cleanup = () => {
328
+ if (pollTimer) clearInterval(pollTimer);
329
+ if (timeoutTimer) clearTimeout(timeoutTimer);
330
+ this.wsServer.removeListener("human-solved", onHumanSolved);
331
+ releaseSlot();
332
+ };
333
+ if (autoDetect && captchaInfo) {
334
+ pollTimer = setInterval(async () => {
335
+ if (resolved) return;
336
+ try {
337
+ const solved = await CaptchaDetector.isSolved(this.page, captchaInfo.selector);
338
+ if (solved && !resolved) {
339
+ resolved = true;
340
+ cleanup();
341
+ this.wsServer.broadcastToSession(this.sessionName, {
342
+ type: "resolved",
343
+ sessionId: this.sessionName
344
+ });
345
+ console.log("\u2705 CAPTCHA auto-detected as solved!");
346
+ this.sendWebhook("captcha-resolved", { reason: "auto-detected" });
347
+ resolve({ solved: true, method: "auto-detected" });
348
+ }
349
+ } catch {
350
+ }
351
+ }, detectInterval);
449
352
  }
450
- };
451
- this.wsServer.on("human-solved", onHumanSolved);
452
- if (timeout > 0) {
453
- timeoutTimer = setTimeout(() => {
353
+ const onHumanSolved = (payload) => {
354
+ if (!payload || payload.sessionId !== this.sessionName) return;
454
355
  if (!resolved) {
455
356
  resolved = true;
456
357
  cleanup();
457
- console.log("\u23F0 Timeout - skipping");
458
- this.sendWebhook("captcha-resolved", { reason: "timeout" });
459
- resolve({ solved: false, method: "timeout" });
358
+ this.wsServer.broadcastToSession(this.sessionName, {
359
+ type: "resolved",
360
+ sessionId: this.sessionName
361
+ });
362
+ console.log("\u2705 CAPTCHA solved via preview!");
363
+ this.sendWebhook("captcha-resolved", { reason: "preview" });
364
+ resolve({ solved: true, method: "preview" });
460
365
  }
461
- }, timeout * 1e3);
462
- }
463
- });
366
+ };
367
+ this.wsServer.on("human-solved", onHumanSolved);
368
+ if (timeout > 0) {
369
+ timeoutTimer = setTimeout(() => {
370
+ if (!resolved) {
371
+ resolved = true;
372
+ cleanup();
373
+ this.wsServer.broadcastToSession(this.sessionName, {
374
+ type: "handoff-timeout",
375
+ sessionId: this.sessionName
376
+ });
377
+ console.log("\u23F0 Timeout - skipping");
378
+ this.sendWebhook("captcha-resolved", { reason: "timeout" });
379
+ resolve({ solved: false, method: "timeout" });
380
+ }
381
+ }, timeout * 1e3);
382
+ }
383
+ activeWaitsByServer.get(this.wsServer)?.set(this.sessionName, "waiting");
384
+ this.wsServer.broadcastToSession(this.sessionName, {
385
+ type: "captcha-detected",
386
+ sessionId: this.sessionName,
387
+ url: this.page.url(),
388
+ reason: waitReason,
389
+ timeout
390
+ });
391
+ void this.sendWebhook("captcha-detected", { reason: waitReason, timeout, targetUrl: this.page.url() });
392
+ this.tryAutoOpen(previewUrl);
393
+ });
394
+ } catch (err) {
395
+ releaseSlot();
396
+ throw err;
397
+ }
464
398
  }
465
399
  };
466
400
 
467
401
  export {
402
+ resolveScreenshotsDir,
403
+ DEFAULT_HUMAN_INTERACTION_SESSION,
404
+ isHumanWaitActive,
468
405
  HumanInteractionManager
469
406
  };