cbrowser 11.7.2 → 11.10.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/analysis/agent-ready-audit.d.ts.map +1 -1
- package/dist/analysis/agent-ready-audit.js +8 -3
- package/dist/analysis/agent-ready-audit.js.map +1 -1
- package/dist/analysis/natural-language.d.ts.map +1 -1
- package/dist/analysis/natural-language.js +12 -0
- package/dist/analysis/natural-language.js.map +1 -1
- package/dist/browser.d.ts +36 -1
- package/dist/browser.d.ts.map +1 -1
- package/dist/browser.js +302 -5
- package/dist/browser.js.map +1 -1
- package/dist/mcp-server-remote.d.ts.map +1 -1
- package/dist/mcp-server-remote.js +55 -3
- package/dist/mcp-server-remote.js.map +1 -1
- package/dist/mcp-server.d.ts.map +1 -1
- package/dist/mcp-server.js +55 -3
- package/dist/mcp-server.js.map +1 -1
- package/dist/types.d.ts +57 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
package/dist/browser.js
CHANGED
|
@@ -8,7 +8,7 @@ import { existsSync, readFileSync, writeFileSync, readdirSync, statSync, unlinkS
|
|
|
8
8
|
import { join } from "path";
|
|
9
9
|
import { mergeConfig, getPaths, ensureDirectories } from "./config.js";
|
|
10
10
|
import { BUILTIN_PERSONAS, getPersona } from "./personas.js";
|
|
11
|
-
import { DEVICE_PRESETS, LOCATION_PRESETS } from "./types.js";
|
|
11
|
+
import { DEVICE_PRESETS, LOCATION_PRESETS, CBrowserErrorCode } from "./types.js";
|
|
12
12
|
import { runCognitiveJourney, isApiKeyConfigured, } from "./cognitive/index.js";
|
|
13
13
|
import { SessionManager } from "./browser/session-manager.js";
|
|
14
14
|
import { SelectorCacheManager } from "./browser/selector-cache.js";
|
|
@@ -358,6 +358,292 @@ export class CBrowser {
|
|
|
358
358
|
return this.page;
|
|
359
359
|
}
|
|
360
360
|
// =========================================================================
|
|
361
|
+
// Browser Crash Recovery (v11.8.0)
|
|
362
|
+
// =========================================================================
|
|
363
|
+
/** Maximum attempts for crash recovery */
|
|
364
|
+
static MAX_RECOVERY_ATTEMPTS = 3;
|
|
365
|
+
/** Default timeout for health check in ms */
|
|
366
|
+
static HEALTH_CHECK_TIMEOUT = 5000;
|
|
367
|
+
/** Base retry delay in ms (exponential backoff) */
|
|
368
|
+
static BASE_RETRY_DELAY = 1000;
|
|
369
|
+
/**
|
|
370
|
+
* Check if the browser is healthy and responsive.
|
|
371
|
+
* This performs a lightweight operation to verify the browser process is alive.
|
|
372
|
+
*/
|
|
373
|
+
async isBrowserHealthy() {
|
|
374
|
+
const startTime = Date.now();
|
|
375
|
+
// No page or context means browser needs launch, not recovery
|
|
376
|
+
if (!this.page || !this.context) {
|
|
377
|
+
return {
|
|
378
|
+
healthy: false,
|
|
379
|
+
error: CBrowserErrorCode.BROWSER_DISCONNECTED,
|
|
380
|
+
message: "Browser not launched",
|
|
381
|
+
checkDurationMs: Date.now() - startTime,
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
try {
|
|
385
|
+
// Check if page is closed
|
|
386
|
+
if (this.page.isClosed()) {
|
|
387
|
+
return {
|
|
388
|
+
healthy: false,
|
|
389
|
+
error: CBrowserErrorCode.BROWSER_CRASHED,
|
|
390
|
+
message: "Page is closed unexpectedly",
|
|
391
|
+
checkDurationMs: Date.now() - startTime,
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
// Try a simple evaluate to verify browser is responsive
|
|
395
|
+
const healthCheck = await Promise.race([
|
|
396
|
+
this.page.evaluate(() => ({
|
|
397
|
+
url: window.location.href,
|
|
398
|
+
readyState: document.readyState,
|
|
399
|
+
timestamp: Date.now(),
|
|
400
|
+
})),
|
|
401
|
+
new Promise((_, reject) => setTimeout(() => reject(new Error("Health check timeout")), CBrowser.HEALTH_CHECK_TIMEOUT)),
|
|
402
|
+
]);
|
|
403
|
+
if (!healthCheck) {
|
|
404
|
+
return {
|
|
405
|
+
healthy: false,
|
|
406
|
+
error: CBrowserErrorCode.BROWSER_UNRESPONSIVE,
|
|
407
|
+
message: "Browser failed to respond within timeout",
|
|
408
|
+
checkDurationMs: Date.now() - startTime,
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
return {
|
|
412
|
+
healthy: true,
|
|
413
|
+
message: `Browser healthy, page at ${healthCheck.url}`,
|
|
414
|
+
checkDurationMs: Date.now() - startTime,
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
catch (error) {
|
|
418
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
419
|
+
// Detect specific crash patterns
|
|
420
|
+
if (errorMessage.includes("Target closed") || errorMessage.includes("Browser closed")) {
|
|
421
|
+
return {
|
|
422
|
+
healthy: false,
|
|
423
|
+
error: CBrowserErrorCode.BROWSER_CRASHED,
|
|
424
|
+
message: `Browser process crashed: ${errorMessage}`,
|
|
425
|
+
checkDurationMs: Date.now() - startTime,
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
if (errorMessage.includes("disconnected") || errorMessage.includes("Connection refused")) {
|
|
429
|
+
return {
|
|
430
|
+
healthy: false,
|
|
431
|
+
error: CBrowserErrorCode.BROWSER_DISCONNECTED,
|
|
432
|
+
message: `Browser disconnected: ${errorMessage}`,
|
|
433
|
+
checkDurationMs: Date.now() - startTime,
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
if (errorMessage.includes("timeout") || errorMessage.includes("Timeout")) {
|
|
437
|
+
return {
|
|
438
|
+
healthy: false,
|
|
439
|
+
error: CBrowserErrorCode.BROWSER_UNRESPONSIVE,
|
|
440
|
+
message: `Browser unresponsive: ${errorMessage}`,
|
|
441
|
+
checkDurationMs: Date.now() - startTime,
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
return {
|
|
445
|
+
healthy: false,
|
|
446
|
+
error: CBrowserErrorCode.BROWSER_CRASHED,
|
|
447
|
+
message: `Browser health check failed: ${errorMessage}`,
|
|
448
|
+
checkDurationMs: Date.now() - startTime,
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Attempt to recover from a browser crash by restarting the browser.
|
|
454
|
+
* Uses exponential backoff for retry attempts.
|
|
455
|
+
*/
|
|
456
|
+
async recoverBrowser(options) {
|
|
457
|
+
const startTime = Date.now();
|
|
458
|
+
const maxAttempts = options?.maxAttempts ?? CBrowser.MAX_RECOVERY_ATTEMPTS;
|
|
459
|
+
// First check if recovery is actually needed
|
|
460
|
+
const healthResult = await this.isBrowserHealthy();
|
|
461
|
+
if (healthResult.healthy) {
|
|
462
|
+
return {
|
|
463
|
+
success: true,
|
|
464
|
+
recoveryNeeded: false,
|
|
465
|
+
message: "Browser is already healthy, no recovery needed",
|
|
466
|
+
attempts: 0,
|
|
467
|
+
recoveryDurationMs: Date.now() - startTime,
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
if (this.config.verbose) {
|
|
471
|
+
console.log(`🔄 Browser crash detected: ${healthResult.message}`);
|
|
472
|
+
console.log(`🔄 Attempting recovery (max ${maxAttempts} attempts)...`);
|
|
473
|
+
}
|
|
474
|
+
// Get the last known URL before crash for restoration
|
|
475
|
+
const savedSession = this.loadSessionState();
|
|
476
|
+
const restoreUrl = options?.restoreUrl ?? savedSession?.url;
|
|
477
|
+
let lastError;
|
|
478
|
+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
479
|
+
try {
|
|
480
|
+
if (this.config.verbose) {
|
|
481
|
+
console.log(`🔄 Recovery attempt ${attempt}/${maxAttempts}...`);
|
|
482
|
+
}
|
|
483
|
+
// Force close any zombie processes
|
|
484
|
+
await this.forceClose();
|
|
485
|
+
// Wait with exponential backoff before retry
|
|
486
|
+
const delay = CBrowser.BASE_RETRY_DELAY * Math.pow(2, attempt - 1);
|
|
487
|
+
await new Promise((r) => setTimeout(r, delay));
|
|
488
|
+
// Relaunch browser
|
|
489
|
+
await this.launch();
|
|
490
|
+
// Restore previous URL if available
|
|
491
|
+
if (restoreUrl && restoreUrl !== "about:blank" && this.page) {
|
|
492
|
+
try {
|
|
493
|
+
await this.page.goto(restoreUrl, {
|
|
494
|
+
waitUntil: "domcontentloaded",
|
|
495
|
+
timeout: 15000,
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
catch (e) {
|
|
499
|
+
// URL restoration is best-effort, don't fail recovery
|
|
500
|
+
if (this.config.verbose) {
|
|
501
|
+
console.log(`⚠️ Could not restore URL: ${restoreUrl}`);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
// Verify recovery was successful
|
|
506
|
+
const postRecoveryHealth = await this.isBrowserHealthy();
|
|
507
|
+
if (postRecoveryHealth.healthy) {
|
|
508
|
+
if (this.config.verbose) {
|
|
509
|
+
console.log(`✅ Browser recovered successfully on attempt ${attempt}`);
|
|
510
|
+
}
|
|
511
|
+
return {
|
|
512
|
+
success: true,
|
|
513
|
+
recoveryNeeded: true,
|
|
514
|
+
message: `Browser recovered after ${attempt} attempt(s)`,
|
|
515
|
+
attempts: attempt,
|
|
516
|
+
recoveryDurationMs: Date.now() - startTime,
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
lastError = postRecoveryHealth.message;
|
|
520
|
+
}
|
|
521
|
+
catch (error) {
|
|
522
|
+
lastError = error instanceof Error ? error.message : String(error);
|
|
523
|
+
if (this.config.verbose) {
|
|
524
|
+
console.log(`❌ Recovery attempt ${attempt} failed: ${lastError}`);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
// All attempts failed
|
|
529
|
+
return {
|
|
530
|
+
success: false,
|
|
531
|
+
recoveryNeeded: true,
|
|
532
|
+
error: CBrowserErrorCode.BROWSER_RECOVERY_FAILED,
|
|
533
|
+
message: `Failed to recover browser after ${maxAttempts} attempts: ${lastError}`,
|
|
534
|
+
attempts: maxAttempts,
|
|
535
|
+
recoveryDurationMs: Date.now() - startTime,
|
|
536
|
+
retryAfterMs: CBrowser.BASE_RETRY_DELAY * Math.pow(2, maxAttempts),
|
|
537
|
+
};
|
|
538
|
+
}
|
|
539
|
+
/**
|
|
540
|
+
* Force close browser processes without normal cleanup.
|
|
541
|
+
* Used when browser is unresponsive and normal close() would hang.
|
|
542
|
+
*/
|
|
543
|
+
async forceClose() {
|
|
544
|
+
// Remove listeners to prevent memory leaks
|
|
545
|
+
if (this.page) {
|
|
546
|
+
try {
|
|
547
|
+
this.page.removeAllListeners();
|
|
548
|
+
}
|
|
549
|
+
catch {
|
|
550
|
+
// Ignore - page may be destroyed
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
// Force close context
|
|
554
|
+
if (this.context) {
|
|
555
|
+
try {
|
|
556
|
+
await Promise.race([
|
|
557
|
+
this.context.close(),
|
|
558
|
+
new Promise((r) => setTimeout(r, 2000)), // 2s timeout for close
|
|
559
|
+
]);
|
|
560
|
+
}
|
|
561
|
+
catch {
|
|
562
|
+
// Ignore - may already be closed
|
|
563
|
+
}
|
|
564
|
+
this.context = null;
|
|
565
|
+
this.page = null;
|
|
566
|
+
}
|
|
567
|
+
// Force close browser
|
|
568
|
+
if (this.browser) {
|
|
569
|
+
try {
|
|
570
|
+
await Promise.race([
|
|
571
|
+
this.browser.close(),
|
|
572
|
+
new Promise((r) => setTimeout(r, 2000)), // 2s timeout for close
|
|
573
|
+
]);
|
|
574
|
+
}
|
|
575
|
+
catch {
|
|
576
|
+
// Ignore - may already be closed
|
|
577
|
+
}
|
|
578
|
+
this.browser = null;
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
/**
|
|
582
|
+
* Execute an operation with automatic crash recovery.
|
|
583
|
+
* If the browser crashes during the operation, it will be restarted and the operation retried.
|
|
584
|
+
*
|
|
585
|
+
* @param operation - The async operation to execute
|
|
586
|
+
* @param operationName - Name of the operation for error messages
|
|
587
|
+
* @returns Result of the operation or throws if recovery fails
|
|
588
|
+
*/
|
|
589
|
+
async withCrashRecovery(operation, operationName = "operation") {
|
|
590
|
+
try {
|
|
591
|
+
return await operation();
|
|
592
|
+
}
|
|
593
|
+
catch (error) {
|
|
594
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
595
|
+
// Check if this looks like a browser crash
|
|
596
|
+
const crashIndicators = [
|
|
597
|
+
"Target closed",
|
|
598
|
+
"Browser closed",
|
|
599
|
+
"disconnected",
|
|
600
|
+
"Connection refused",
|
|
601
|
+
"Protocol error",
|
|
602
|
+
"browser has been closed",
|
|
603
|
+
"Execution context was destroyed",
|
|
604
|
+
];
|
|
605
|
+
const isCrash = crashIndicators.some((indicator) => errorMessage.toLowerCase().includes(indicator.toLowerCase()));
|
|
606
|
+
if (!isCrash) {
|
|
607
|
+
// Not a crash, re-throw original error
|
|
608
|
+
throw error;
|
|
609
|
+
}
|
|
610
|
+
if (this.config.verbose) {
|
|
611
|
+
console.log(`🔄 Browser crash detected during ${operationName}, attempting recovery...`);
|
|
612
|
+
}
|
|
613
|
+
// Attempt recovery
|
|
614
|
+
const recovery = await this.recoverBrowser();
|
|
615
|
+
if (!recovery.success) {
|
|
616
|
+
// Recovery failed, throw structured error
|
|
617
|
+
throw new Error(JSON.stringify({
|
|
618
|
+
error: "browser_crash",
|
|
619
|
+
errorCode: CBrowserErrorCode.BROWSER_RECOVERY_FAILED,
|
|
620
|
+
message: recovery.message,
|
|
621
|
+
recovering: false,
|
|
622
|
+
retryAfterMs: recovery.retryAfterMs ?? 5000,
|
|
623
|
+
operation: operationName,
|
|
624
|
+
}));
|
|
625
|
+
}
|
|
626
|
+
// Recovery succeeded, retry the operation once
|
|
627
|
+
if (this.config.verbose) {
|
|
628
|
+
console.log(`🔄 Retrying ${operationName} after recovery...`);
|
|
629
|
+
}
|
|
630
|
+
try {
|
|
631
|
+
return await operation();
|
|
632
|
+
}
|
|
633
|
+
catch (retryError) {
|
|
634
|
+
// Second failure after recovery
|
|
635
|
+
throw new Error(JSON.stringify({
|
|
636
|
+
error: "browser_crash",
|
|
637
|
+
errorCode: CBrowserErrorCode.BROWSER_CRASHED,
|
|
638
|
+
message: `Operation failed after recovery: ${retryError instanceof Error ? retryError.message : String(retryError)}`,
|
|
639
|
+
recovering: false,
|
|
640
|
+
retryAfterMs: 5000,
|
|
641
|
+
operation: operationName,
|
|
642
|
+
}));
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
// =========================================================================
|
|
361
647
|
// Network Mocking
|
|
362
648
|
// =========================================================================
|
|
363
649
|
/**
|
|
@@ -1915,10 +2201,12 @@ export class CBrowser {
|
|
|
1915
2201
|
// =========================================================================
|
|
1916
2202
|
/**
|
|
1917
2203
|
* Click with smart retry - automatically retries with alternative selectors on failure.
|
|
2204
|
+
* v11.8.0: Added confidence gating - only reports success if confidence >= minConfidence
|
|
1918
2205
|
*/
|
|
1919
2206
|
async smartClick(selector, options = {}) {
|
|
1920
2207
|
const maxRetries = options.maxRetries ?? 3;
|
|
1921
2208
|
const retryDelay = options.retryDelay ?? 1000;
|
|
2209
|
+
const minConfidence = options.minConfidence ?? 0.6; // v11.8.0: Confidence threshold for success
|
|
1922
2210
|
const attempts = [];
|
|
1923
2211
|
// Dismiss overlays first if requested
|
|
1924
2212
|
if (options.dismissOverlays) {
|
|
@@ -1957,14 +2245,23 @@ export class CBrowser {
|
|
|
1957
2245
|
screenshot: result.screenshot,
|
|
1958
2246
|
});
|
|
1959
2247
|
if (result.success) {
|
|
1960
|
-
//
|
|
1961
|
-
|
|
2248
|
+
// v11.8.0: Gate success on confidence threshold
|
|
2249
|
+
const meetsConfidence = alt.confidence >= minConfidence;
|
|
2250
|
+
if (meetsConfidence) {
|
|
2251
|
+
// Cache the working alternative for future use
|
|
2252
|
+
this.cacheAlternativeSelector(selector, alt.selector);
|
|
2253
|
+
}
|
|
1962
2254
|
return {
|
|
1963
|
-
success:
|
|
2255
|
+
success: meetsConfidence, // v11.8.0: Only success if confidence meets threshold
|
|
1964
2256
|
attempts,
|
|
1965
2257
|
finalSelector: alt.selector,
|
|
1966
|
-
message:
|
|
2258
|
+
message: meetsConfidence
|
|
2259
|
+
? `Clicked using alternative: ${alt.reason}`
|
|
2260
|
+
: `Clicked element with low confidence (${(alt.confidence * 100).toFixed(0)}%) - may not be the intended target`,
|
|
1967
2261
|
screenshot: result.screenshot,
|
|
2262
|
+
confidence: alt.confidence,
|
|
2263
|
+
healed: true,
|
|
2264
|
+
healReason: alt.reason,
|
|
1968
2265
|
};
|
|
1969
2266
|
}
|
|
1970
2267
|
}
|