agent-browser 0.18.0 → 0.20.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 (82) hide show
  1. package/README.md +150 -122
  2. package/bin/agent-browser-darwin-arm64 +0 -0
  3. package/bin/agent-browser-darwin-x64 +0 -0
  4. package/bin/agent-browser-linux-arm64 +0 -0
  5. package/bin/agent-browser-linux-x64 +0 -0
  6. package/bin/agent-browser-win32-x64.exe +0 -0
  7. package/package.json +6 -40
  8. package/scripts/postinstall.js +12 -15
  9. package/skills/agent-browser/SKILL.md +37 -32
  10. package/skills/electron/SKILL.md +5 -5
  11. package/dist/action-policy.d.ts +0 -14
  12. package/dist/action-policy.d.ts.map +0 -1
  13. package/dist/action-policy.js +0 -253
  14. package/dist/action-policy.js.map +0 -1
  15. package/dist/actions.d.ts +0 -18
  16. package/dist/actions.d.ts.map +0 -1
  17. package/dist/actions.js +0 -2120
  18. package/dist/actions.js.map +0 -1
  19. package/dist/auth-cli.d.ts +0 -2
  20. package/dist/auth-cli.d.ts.map +0 -1
  21. package/dist/auth-cli.js +0 -97
  22. package/dist/auth-cli.js.map +0 -1
  23. package/dist/auth-vault.d.ts +0 -36
  24. package/dist/auth-vault.d.ts.map +0 -1
  25. package/dist/auth-vault.js +0 -125
  26. package/dist/auth-vault.js.map +0 -1
  27. package/dist/browser.d.ts +0 -592
  28. package/dist/browser.d.ts.map +0 -1
  29. package/dist/browser.js +0 -2190
  30. package/dist/browser.js.map +0 -1
  31. package/dist/confirmation.d.ts +0 -8
  32. package/dist/confirmation.d.ts.map +0 -1
  33. package/dist/confirmation.js +0 -30
  34. package/dist/confirmation.js.map +0 -1
  35. package/dist/daemon.d.ts +0 -71
  36. package/dist/daemon.d.ts.map +0 -1
  37. package/dist/daemon.js +0 -671
  38. package/dist/daemon.js.map +0 -1
  39. package/dist/diff.d.ts +0 -18
  40. package/dist/diff.d.ts.map +0 -1
  41. package/dist/diff.js +0 -271
  42. package/dist/diff.js.map +0 -1
  43. package/dist/domain-filter.d.ts +0 -28
  44. package/dist/domain-filter.d.ts.map +0 -1
  45. package/dist/domain-filter.js +0 -149
  46. package/dist/domain-filter.js.map +0 -1
  47. package/dist/encryption.d.ts +0 -73
  48. package/dist/encryption.d.ts.map +0 -1
  49. package/dist/encryption.js +0 -171
  50. package/dist/encryption.js.map +0 -1
  51. package/dist/inspect-server.d.ts +0 -26
  52. package/dist/inspect-server.d.ts.map +0 -1
  53. package/dist/inspect-server.js +0 -218
  54. package/dist/inspect-server.js.map +0 -1
  55. package/dist/ios-actions.d.ts +0 -11
  56. package/dist/ios-actions.d.ts.map +0 -1
  57. package/dist/ios-actions.js +0 -228
  58. package/dist/ios-actions.js.map +0 -1
  59. package/dist/ios-manager.d.ts +0 -266
  60. package/dist/ios-manager.d.ts.map +0 -1
  61. package/dist/ios-manager.js +0 -1073
  62. package/dist/ios-manager.js.map +0 -1
  63. package/dist/protocol.d.ts +0 -28
  64. package/dist/protocol.d.ts.map +0 -1
  65. package/dist/protocol.js +0 -986
  66. package/dist/protocol.js.map +0 -1
  67. package/dist/snapshot.d.ts +0 -67
  68. package/dist/snapshot.d.ts.map +0 -1
  69. package/dist/snapshot.js +0 -514
  70. package/dist/snapshot.js.map +0 -1
  71. package/dist/state-utils.d.ts +0 -77
  72. package/dist/state-utils.d.ts.map +0 -1
  73. package/dist/state-utils.js +0 -178
  74. package/dist/state-utils.js.map +0 -1
  75. package/dist/stream-server.d.ts +0 -117
  76. package/dist/stream-server.d.ts.map +0 -1
  77. package/dist/stream-server.js +0 -309
  78. package/dist/stream-server.js.map +0 -1
  79. package/dist/types.d.ts +0 -925
  80. package/dist/types.d.ts.map +0 -1
  81. package/dist/types.js +0 -2
  82. package/dist/types.js.map +0 -1
package/dist/browser.js DELETED
@@ -1,2190 +0,0 @@
1
- import { chromium, firefox, webkit, devices, } from 'playwright-core';
2
- import path from 'node:path';
3
- import os from 'node:os';
4
- import { existsSync, mkdirSync, rmSync, readFileSync, statSync } from 'node:fs';
5
- import { writeFile, mkdir } from 'node:fs/promises';
6
- import { getEnhancedSnapshot, parseRef } from './snapshot.js';
7
- import { safeHeaderMerge } from './state-utils.js';
8
- import { isDomainAllowed, installDomainFilter, parseDomainList } from './domain-filter.js';
9
- import { getEncryptionKey, isEncryptedPayload, decryptData, ENCRYPTION_KEY_ENV, } from './state-utils.js';
10
- /**
11
- * Returns the default Playwright timeout in milliseconds for standard operations.
12
- * Can be overridden via the AGENT_BROWSER_DEFAULT_TIMEOUT environment variable.
13
- * Default is 25s, which is below the CLI's 30s IPC read timeout to ensure
14
- * Playwright errors are returned before the CLI gives up with EAGAIN.
15
- * Recording contexts use a shorter fixed timeout (10s) and are not affected.
16
- */
17
- export function getDefaultTimeout() {
18
- const envValue = process.env.AGENT_BROWSER_DEFAULT_TIMEOUT;
19
- if (envValue) {
20
- const parsed = parseInt(envValue, 10);
21
- if (!isNaN(parsed) && parsed >= 1000) {
22
- return parsed;
23
- }
24
- }
25
- return 25000;
26
- }
27
- /**
28
- * Manages the Playwright browser lifecycle with multiple tabs/windows
29
- */
30
- export class BrowserManager {
31
- browser = null;
32
- cdpEndpoint = null; // stores port number or full URL
33
- resolvedWsUrl = null;
34
- isPersistentContext = false;
35
- browserbaseSessionId = null;
36
- browserbaseApiKey = null;
37
- browserUseSessionId = null;
38
- browserUseApiKey = null;
39
- kernelSessionId = null;
40
- kernelApiKey = null;
41
- contexts = [];
42
- pages = [];
43
- activePageIndex = 0;
44
- activeFrame = null;
45
- dialogHandler = null;
46
- trackedRequests = [];
47
- routes = new Map();
48
- consoleMessages = [];
49
- pageErrors = [];
50
- isRecordingHar = false;
51
- refMap = {};
52
- lastSnapshot = '';
53
- scopedHeaderRoutes = new Map();
54
- colorScheme = null;
55
- downloadPath = null;
56
- allowedDomains = [];
57
- inspectServer = null;
58
- stopInspectServer() {
59
- if (this.inspectServer) {
60
- this.inspectServer.stop();
61
- this.inspectServer = null;
62
- }
63
- }
64
- setInspectServer(server) {
65
- this.stopInspectServer();
66
- this.inspectServer = server;
67
- }
68
- /**
69
- * Set the persistent color scheme preference.
70
- * Applied automatically to all new pages and contexts.
71
- */
72
- setColorScheme(scheme) {
73
- this.colorScheme = scheme;
74
- }
75
- // CDP session for screencast and input injection
76
- cdpSession = null;
77
- screencastActive = false;
78
- screencastSessionId = 0;
79
- frameCallback = null;
80
- screencastFrameHandler = null;
81
- // Video recording (Playwright native)
82
- recordingContext = null;
83
- recordingPage = null;
84
- recordingOutputPath = '';
85
- recordingTempDir = '';
86
- launchWarnings = [];
87
- /**
88
- * Get and clear launch warnings (e.g., decryption failures)
89
- */
90
- getAndClearWarnings() {
91
- const warnings = this.launchWarnings;
92
- this.launchWarnings = [];
93
- return warnings;
94
- }
95
- // CDP profiling state
96
- static MAX_PROFILE_EVENTS = 5_000_000;
97
- profilingActive = false;
98
- profileChunks = [];
99
- profileEventsDropped = false;
100
- profileCompleteResolver = null;
101
- profileDataHandler = null;
102
- profileCompleteHandler = null;
103
- /**
104
- * Check if browser is launched
105
- */
106
- isLaunched() {
107
- return this.browser !== null || this.isPersistentContext;
108
- }
109
- getCdpUrl() {
110
- if (this.resolvedWsUrl)
111
- return this.resolvedWsUrl;
112
- if (this.cdpEndpoint?.startsWith('ws://') || this.cdpEndpoint?.startsWith('wss://')) {
113
- return this.cdpEndpoint;
114
- }
115
- try {
116
- return this.browser?.wsEndpoint?.() ?? null;
117
- }
118
- catch {
119
- return null;
120
- }
121
- }
122
- /**
123
- * Get enhanced snapshot with refs and cache the ref map
124
- */
125
- async getSnapshot(options) {
126
- const page = this.getPage();
127
- const snapshot = await getEnhancedSnapshot(page, options);
128
- this.refMap = snapshot.refs;
129
- this.lastSnapshot = snapshot.tree;
130
- return snapshot;
131
- }
132
- /**
133
- * Get the last snapshot tree text (empty string if no snapshot has been taken)
134
- */
135
- getLastSnapshot() {
136
- return this.lastSnapshot;
137
- }
138
- /**
139
- * Update the stored snapshot (used by diff to keep the baseline current)
140
- */
141
- setLastSnapshot(snapshot) {
142
- this.lastSnapshot = snapshot;
143
- }
144
- /**
145
- * Get the cached ref map from last snapshot
146
- */
147
- getRefMap() {
148
- return this.refMap;
149
- }
150
- /**
151
- * Get a locator from a ref (e.g., "e1", "@e1", "ref=e1")
152
- * Returns null if ref doesn't exist or is invalid
153
- */
154
- getLocatorFromRef(refArg) {
155
- const ref = parseRef(refArg);
156
- if (!ref)
157
- return null;
158
- const refData = this.refMap[ref];
159
- if (!refData)
160
- return null;
161
- const page = this.getPage();
162
- // Check if this is a cursor-interactive element (uses CSS selector, not ARIA role)
163
- // These have pseudo-roles 'clickable' or 'focusable' and a CSS selector
164
- if (refData.role === 'clickable' || refData.role === 'focusable') {
165
- // The selector is a CSS selector, use it directly
166
- return page.locator(refData.selector);
167
- }
168
- // Build locator with exact: true to avoid substring matches
169
- let locator = page.getByRole(refData.role, {
170
- name: refData.name,
171
- exact: true,
172
- });
173
- // If an nth index is stored (for disambiguation), use it
174
- if (refData.nth !== undefined) {
175
- locator = locator.nth(refData.nth);
176
- }
177
- return locator;
178
- }
179
- /**
180
- * Check if a selector looks like a ref
181
- */
182
- isRef(selector) {
183
- return parseRef(selector) !== null;
184
- }
185
- /**
186
- * Install the domain filter on a context if an allowlist is configured.
187
- * Should be called before any pages navigate on the context.
188
- */
189
- async ensureDomainFilter(context) {
190
- if (this.allowedDomains.length > 0) {
191
- await installDomainFilter(context, this.allowedDomains);
192
- }
193
- }
194
- /**
195
- * After installing the domain filter, verify existing pages are on allowed
196
- * domains. Pages that pre-date the filter (e.g. CDP/cloud connect) may have
197
- * already navigated to disallowed domains. Navigate them to about:blank.
198
- */
199
- async sanitizeExistingPages(pages) {
200
- if (this.allowedDomains.length === 0)
201
- return;
202
- for (const page of pages) {
203
- const url = page.url();
204
- if (!url || url === 'about:blank')
205
- continue;
206
- try {
207
- const hostname = new URL(url).hostname.toLowerCase();
208
- if (!isDomainAllowed(hostname, this.allowedDomains)) {
209
- await page.goto('about:blank');
210
- }
211
- }
212
- catch {
213
- await page.goto('about:blank').catch(() => { });
214
- }
215
- }
216
- }
217
- /**
218
- * Check if a URL is allowed by the domain allowlist.
219
- * Throws if the URL's domain is blocked. No-op if no allowlist is set.
220
- * Blocks non-http(s) schemes and unparseable URLs by default.
221
- */
222
- checkDomainAllowed(url) {
223
- if (this.allowedDomains.length === 0)
224
- return;
225
- if (!url.startsWith('http://') && !url.startsWith('https://')) {
226
- throw new Error(`Navigation blocked: non-http(s) scheme in URL "${url}"`);
227
- }
228
- let hostname;
229
- try {
230
- hostname = new URL(url).hostname.toLowerCase();
231
- }
232
- catch {
233
- throw new Error(`Navigation blocked: unable to parse URL "${url}"`);
234
- }
235
- if (!isDomainAllowed(hostname, this.allowedDomains)) {
236
- throw new Error(`Navigation blocked: ${hostname} is not in the allowed domains list`);
237
- }
238
- }
239
- /**
240
- * Get locator - supports both refs and regular selectors
241
- */
242
- getLocator(selectorOrRef) {
243
- // Check if it's a ref first
244
- const locator = this.getLocatorFromRef(selectorOrRef);
245
- if (locator)
246
- return locator;
247
- // Otherwise treat as regular selector
248
- const page = this.getPage();
249
- return page.locator(selectorOrRef);
250
- }
251
- /**
252
- * Check if the browser has any usable pages
253
- */
254
- hasPages() {
255
- return this.pages.length > 0;
256
- }
257
- /**
258
- * Ensure at least one page exists. If the browser is launched but all pages
259
- * were closed (stale session), creates a new page on the existing context.
260
- * No-op if pages already exist.
261
- */
262
- async ensurePage() {
263
- if (this.pages.length > 0)
264
- return;
265
- if (!this.browser && !this.isPersistentContext)
266
- return;
267
- // Use the last existing context, or create a new one
268
- let context;
269
- if (this.contexts.length > 0) {
270
- context = this.contexts[this.contexts.length - 1];
271
- }
272
- else if (this.browser) {
273
- context = await this.browser.newContext({
274
- ...(this.colorScheme && { colorScheme: this.colorScheme }),
275
- });
276
- context.setDefaultTimeout(getDefaultTimeout());
277
- this.contexts.push(context);
278
- this.setupContextTracking(context);
279
- await this.ensureDomainFilter(context);
280
- }
281
- else {
282
- return;
283
- }
284
- const page = await context.newPage();
285
- if (!this.pages.includes(page)) {
286
- this.pages.push(page);
287
- this.setupPageTracking(page);
288
- }
289
- this.activePageIndex = this.pages.length - 1;
290
- }
291
- /**
292
- * Get the current active page, throws if not launched
293
- */
294
- getPage() {
295
- if (this.pages.length === 0) {
296
- throw new Error('Browser not launched. Call launch first.');
297
- }
298
- return this.pages[this.activePageIndex];
299
- }
300
- /**
301
- * Get the current frame (or page's main frame if no frame is selected)
302
- */
303
- getFrame() {
304
- if (this.activeFrame) {
305
- return this.activeFrame;
306
- }
307
- return this.getPage().mainFrame();
308
- }
309
- /**
310
- * Switch to a frame by selector, name, or URL
311
- */
312
- async switchToFrame(options) {
313
- const page = this.getPage();
314
- if (options.selector) {
315
- const frameElement = await page.$(options.selector);
316
- if (!frameElement) {
317
- throw new Error(`Frame not found: ${options.selector}`);
318
- }
319
- const frame = await frameElement.contentFrame();
320
- if (!frame) {
321
- throw new Error(`Element is not a frame: ${options.selector}`);
322
- }
323
- this.activeFrame = frame;
324
- }
325
- else if (options.name) {
326
- const frame = page.frame({ name: options.name });
327
- if (!frame) {
328
- throw new Error(`Frame not found with name: ${options.name}`);
329
- }
330
- this.activeFrame = frame;
331
- }
332
- else if (options.url) {
333
- const frame = page.frame({ url: options.url });
334
- if (!frame) {
335
- throw new Error(`Frame not found with URL: ${options.url}`);
336
- }
337
- this.activeFrame = frame;
338
- }
339
- }
340
- /**
341
- * Switch back to main frame
342
- */
343
- switchToMainFrame() {
344
- this.activeFrame = null;
345
- }
346
- /**
347
- * Set up dialog handler
348
- */
349
- setDialogHandler(response, promptText) {
350
- const page = this.getPage();
351
- // Remove existing handler if any
352
- if (this.dialogHandler) {
353
- page.removeListener('dialog', this.dialogHandler);
354
- }
355
- this.dialogHandler = async (dialog) => {
356
- if (response === 'accept') {
357
- await dialog.accept(promptText);
358
- }
359
- else {
360
- await dialog.dismiss();
361
- }
362
- };
363
- page.on('dialog', this.dialogHandler);
364
- }
365
- /**
366
- * Clear dialog handler
367
- */
368
- clearDialogHandler() {
369
- if (this.dialogHandler) {
370
- const page = this.getPage();
371
- page.removeListener('dialog', this.dialogHandler);
372
- this.dialogHandler = null;
373
- }
374
- }
375
- /**
376
- * Start tracking requests
377
- */
378
- startRequestTracking() {
379
- const page = this.getPage();
380
- page.on('request', (request) => {
381
- this.trackedRequests.push({
382
- url: request.url(),
383
- method: request.method(),
384
- headers: request.headers(),
385
- timestamp: Date.now(),
386
- resourceType: request.resourceType(),
387
- });
388
- });
389
- }
390
- /**
391
- * Get tracked requests
392
- */
393
- getRequests(filter) {
394
- if (filter) {
395
- return this.trackedRequests.filter((r) => r.url.includes(filter));
396
- }
397
- return this.trackedRequests;
398
- }
399
- /**
400
- * Clear tracked requests
401
- */
402
- clearRequests() {
403
- this.trackedRequests = [];
404
- }
405
- /**
406
- * Add a route to intercept requests
407
- */
408
- async addRoute(url, options) {
409
- const page = this.getPage();
410
- const handler = async (route) => {
411
- if (options.abort) {
412
- await route.abort();
413
- }
414
- else if (options.response) {
415
- await route.fulfill({
416
- status: options.response.status ?? 200,
417
- body: options.response.body ?? '',
418
- contentType: options.response.contentType ?? 'text/plain',
419
- headers: options.response.headers,
420
- });
421
- }
422
- else {
423
- await route.continue();
424
- }
425
- };
426
- this.routes.set(url, handler);
427
- await page.route(url, handler);
428
- }
429
- /**
430
- * Remove a route
431
- */
432
- async removeRoute(url) {
433
- const page = this.getPage();
434
- if (url) {
435
- const handler = this.routes.get(url);
436
- if (handler) {
437
- await page.unroute(url, handler);
438
- this.routes.delete(url);
439
- }
440
- }
441
- else {
442
- // Remove all routes
443
- for (const [routeUrl, handler] of this.routes) {
444
- await page.unroute(routeUrl, handler);
445
- }
446
- this.routes.clear();
447
- }
448
- }
449
- /**
450
- * Set geolocation
451
- */
452
- async setGeolocation(latitude, longitude, accuracy) {
453
- const context = this.contexts[0];
454
- if (context) {
455
- await context.setGeolocation({ latitude, longitude, accuracy });
456
- }
457
- }
458
- /**
459
- * Set permissions
460
- */
461
- async setPermissions(permissions, grant) {
462
- const context = this.contexts[0];
463
- if (context) {
464
- if (grant) {
465
- await context.grantPermissions(permissions);
466
- }
467
- else {
468
- await context.clearPermissions();
469
- }
470
- }
471
- }
472
- /**
473
- * Set viewport
474
- */
475
- async setViewport(width, height) {
476
- const page = this.getPage();
477
- await page.setViewportSize({ width, height });
478
- }
479
- /**
480
- * Set device scale factor (devicePixelRatio) via CDP
481
- * This sets window.devicePixelRatio which affects how the page renders and responds to media queries
482
- *
483
- * Note: When using CDP to set deviceScaleFactor, screenshots will be at logical pixel dimensions
484
- * (viewport size), not physical pixel dimensions (viewport × scale). This is a Playwright limitation
485
- * when using CDP emulation on existing contexts. For true HiDPI screenshots with physical pixels,
486
- * deviceScaleFactor must be set at context creation time.
487
- *
488
- * Must be called after setViewport to work correctly
489
- */
490
- async setDeviceScaleFactor(deviceScaleFactor, width, height, mobile = false) {
491
- const cdp = await this.getCDPSession();
492
- await cdp.send('Emulation.setDeviceMetricsOverride', {
493
- width,
494
- height,
495
- deviceScaleFactor,
496
- mobile,
497
- });
498
- }
499
- /**
500
- * Clear device metrics override to restore default devicePixelRatio
501
- */
502
- async clearDeviceMetricsOverride() {
503
- const cdp = await this.getCDPSession();
504
- await cdp.send('Emulation.clearDeviceMetricsOverride');
505
- }
506
- /**
507
- * Get device descriptor
508
- */
509
- getDevice(deviceName) {
510
- return devices[deviceName];
511
- }
512
- /**
513
- * List available devices
514
- */
515
- listDevices() {
516
- return Object.keys(devices);
517
- }
518
- /**
519
- * Start console message tracking
520
- */
521
- startConsoleTracking() {
522
- const page = this.getPage();
523
- page.on('console', (msg) => {
524
- this.consoleMessages.push({
525
- type: msg.type(),
526
- text: msg.text(),
527
- timestamp: Date.now(),
528
- });
529
- });
530
- }
531
- /**
532
- * Get console messages
533
- */
534
- getConsoleMessages() {
535
- return this.consoleMessages;
536
- }
537
- /**
538
- * Clear console messages
539
- */
540
- clearConsoleMessages() {
541
- this.consoleMessages = [];
542
- }
543
- /**
544
- * Start error tracking
545
- */
546
- startErrorTracking() {
547
- const page = this.getPage();
548
- page.on('pageerror', (error) => {
549
- this.pageErrors.push({
550
- message: error.message,
551
- timestamp: Date.now(),
552
- });
553
- });
554
- }
555
- /**
556
- * Get page errors
557
- */
558
- getPageErrors() {
559
- return this.pageErrors;
560
- }
561
- /**
562
- * Clear page errors
563
- */
564
- clearPageErrors() {
565
- this.pageErrors = [];
566
- }
567
- /**
568
- * Start HAR recording
569
- */
570
- async startHarRecording() {
571
- // HAR is started at context level, flag for tracking
572
- this.isRecordingHar = true;
573
- }
574
- /**
575
- * Check if HAR recording
576
- */
577
- isHarRecording() {
578
- return this.isRecordingHar;
579
- }
580
- /**
581
- * Set offline mode
582
- */
583
- async setOffline(offline) {
584
- const context = this.contexts[0];
585
- if (context) {
586
- await context.setOffline(offline);
587
- }
588
- }
589
- /**
590
- * Set extra HTTP headers (global - all requests)
591
- */
592
- async setExtraHeaders(headers) {
593
- const context = this.contexts[0];
594
- if (context) {
595
- await context.setExtraHTTPHeaders(headers);
596
- }
597
- }
598
- /**
599
- * Set scoped HTTP headers (only for requests matching the origin)
600
- * Uses route interception to add headers only to matching requests
601
- */
602
- async setScopedHeaders(origin, headers) {
603
- const page = this.getPage();
604
- // Build URL pattern from origin (e.g., "api.example.com" -> "**://api.example.com/**")
605
- // Handle both full URLs and just hostnames
606
- let urlPattern;
607
- try {
608
- const url = new URL(origin.startsWith('http') ? origin : `https://${origin}`);
609
- // Match any protocol, the host, and any path
610
- urlPattern = `**://${url.host}/**`;
611
- }
612
- catch {
613
- // If parsing fails, treat as hostname pattern
614
- urlPattern = `**://${origin}/**`;
615
- }
616
- // Remove existing route for this origin if any
617
- const existingHandler = this.scopedHeaderRoutes.get(urlPattern);
618
- if (existingHandler) {
619
- await page.unroute(urlPattern, existingHandler);
620
- }
621
- // Create handler that adds headers to matching requests
622
- const handler = async (route) => {
623
- const requestHeaders = route.request().headers();
624
- await route.continue({
625
- headers: safeHeaderMerge(requestHeaders, headers),
626
- });
627
- };
628
- // Store and register the route
629
- this.scopedHeaderRoutes.set(urlPattern, handler);
630
- await page.route(urlPattern, handler);
631
- }
632
- /**
633
- * Clear scoped headers for an origin (or all if no origin specified)
634
- */
635
- async clearScopedHeaders(origin) {
636
- const page = this.getPage();
637
- if (origin) {
638
- let urlPattern;
639
- try {
640
- const url = new URL(origin.startsWith('http') ? origin : `https://${origin}`);
641
- urlPattern = `**://${url.host}/**`;
642
- }
643
- catch {
644
- urlPattern = `**://${origin}/**`;
645
- }
646
- const handler = this.scopedHeaderRoutes.get(urlPattern);
647
- if (handler) {
648
- await page.unroute(urlPattern, handler);
649
- this.scopedHeaderRoutes.delete(urlPattern);
650
- }
651
- }
652
- else {
653
- // Clear all scoped header routes
654
- for (const [pattern, handler] of this.scopedHeaderRoutes) {
655
- await page.unroute(pattern, handler);
656
- }
657
- this.scopedHeaderRoutes.clear();
658
- }
659
- }
660
- /**
661
- * Start tracing
662
- */
663
- async startTracing(options) {
664
- const context = this.contexts[0];
665
- if (context) {
666
- await context.tracing.start({
667
- screenshots: options.screenshots ?? true,
668
- snapshots: options.snapshots ?? true,
669
- });
670
- }
671
- }
672
- /**
673
- * Stop tracing and save
674
- */
675
- async stopTracing(path) {
676
- const context = this.contexts[0];
677
- if (context) {
678
- await context.tracing.stop(path ? { path } : undefined);
679
- }
680
- }
681
- /**
682
- * Get the current browser context (first context)
683
- */
684
- getContext() {
685
- return this.contexts[0] ?? null;
686
- }
687
- /**
688
- * Save storage state (cookies, localStorage, etc.)
689
- */
690
- async saveStorageState(path) {
691
- const context = this.contexts[0];
692
- if (context) {
693
- await context.storageState({ path });
694
- }
695
- }
696
- /**
697
- * Get all pages
698
- */
699
- getPages() {
700
- return this.pages;
701
- }
702
- /**
703
- * Get current page index
704
- */
705
- getActiveIndex() {
706
- return this.activePageIndex;
707
- }
708
- /**
709
- * Get the current browser instance
710
- */
711
- getBrowser() {
712
- return this.browser;
713
- }
714
- /**
715
- * Check if an existing CDP connection is still alive
716
- * by verifying we can access browser contexts and that at least one has pages
717
- */
718
- isCdpConnectionAlive() {
719
- if (!this.browser)
720
- return false;
721
- try {
722
- const contexts = this.browser.contexts();
723
- if (contexts.length === 0)
724
- return false;
725
- return contexts.some((context) => context.pages().length > 0);
726
- }
727
- catch {
728
- return false;
729
- }
730
- }
731
- /**
732
- * Check if CDP connection needs to be re-established
733
- */
734
- needsCdpReconnect(cdpEndpoint) {
735
- if (!this.browser?.isConnected())
736
- return true;
737
- if (this.cdpEndpoint !== cdpEndpoint)
738
- return true;
739
- if (!this.isCdpConnectionAlive())
740
- return true;
741
- return false;
742
- }
743
- /**
744
- * Close a Browserbase session via API
745
- */
746
- async closeBrowserbaseSession(sessionId, apiKey) {
747
- const response = await fetch(`https://api.browserbase.com/v1/sessions/${sessionId}`, {
748
- method: 'POST',
749
- headers: {
750
- 'Content-Type': 'application/json',
751
- 'X-BB-API-Key': apiKey,
752
- },
753
- body: JSON.stringify({ status: 'REQUEST_RELEASE' }),
754
- });
755
- if (!response.ok) {
756
- throw new Error(`Failed to close Browserbase session: ${response.statusText}`);
757
- }
758
- }
759
- /**
760
- * Close a Browser Use session via API
761
- */
762
- async closeBrowserUseSession(sessionId, apiKey) {
763
- const response = await fetch(`https://api.browser-use.com/api/v2/browsers/${sessionId}`, {
764
- method: 'PATCH',
765
- headers: {
766
- 'Content-Type': 'application/json',
767
- 'X-Browser-Use-API-Key': apiKey,
768
- },
769
- body: JSON.stringify({ action: 'stop' }),
770
- });
771
- if (!response.ok) {
772
- throw new Error(`Failed to close Browser Use session: ${response.statusText}`);
773
- }
774
- }
775
- /**
776
- * Close a Kernel session via API
777
- */
778
- async closeKernelSession(sessionId, apiKey) {
779
- const headers = {};
780
- if (apiKey) {
781
- headers['Authorization'] = `Bearer ${apiKey}`;
782
- }
783
- const response = await fetch(`https://api.onkernel.com/browsers/${sessionId}`, {
784
- method: 'DELETE',
785
- headers,
786
- });
787
- if (!response.ok) {
788
- throw new Error(`Failed to close Kernel session: ${response.statusText}`);
789
- }
790
- }
791
- /**
792
- * Connect to Browserbase remote browser via CDP.
793
- * Requires BROWSERBASE_API_KEY environment variable.
794
- */
795
- async connectToBrowserbase() {
796
- const browserbaseApiKey = process.env.BROWSERBASE_API_KEY;
797
- if (!browserbaseApiKey) {
798
- throw new Error('BROWSERBASE_API_KEY is required when using browserbase as a provider');
799
- }
800
- const response = await fetch('https://api.browserbase.com/v1/sessions', {
801
- method: 'POST',
802
- headers: {
803
- 'X-BB-API-Key': browserbaseApiKey,
804
- },
805
- });
806
- if (!response.ok) {
807
- throw new Error(`Failed to create Browserbase session: ${response.statusText}`);
808
- }
809
- const session = (await response.json());
810
- const browser = await chromium.connectOverCDP(session.connectUrl).catch(() => {
811
- throw new Error('Failed to connect to Browserbase session via CDP');
812
- });
813
- try {
814
- const contexts = browser.contexts();
815
- if (contexts.length === 0) {
816
- throw new Error('No browser context found in Browserbase session');
817
- }
818
- const context = contexts[0];
819
- const pages = context.pages();
820
- const page = pages[0] ?? (await context.newPage());
821
- this.browserbaseSessionId = session.id;
822
- this.browserbaseApiKey = browserbaseApiKey;
823
- this.browser = browser;
824
- context.setDefaultTimeout(getDefaultTimeout());
825
- this.contexts.push(context);
826
- this.setupContextTracking(context);
827
- await this.ensureDomainFilter(context);
828
- await this.sanitizeExistingPages([page]);
829
- this.pages.push(page);
830
- this.activePageIndex = 0;
831
- this.setupPageTracking(page);
832
- }
833
- catch (error) {
834
- await this.closeBrowserbaseSession(session.id, browserbaseApiKey).catch((sessionError) => {
835
- console.error('Failed to close Browserbase session during cleanup:', sessionError);
836
- });
837
- throw error;
838
- }
839
- }
840
- /**
841
- * Find or create a Kernel profile by name.
842
- * Returns the profile object if successful.
843
- */
844
- async findOrCreateKernelProfile(profileName, apiKey) {
845
- const headers = {};
846
- if (apiKey) {
847
- headers['Authorization'] = `Bearer ${apiKey}`;
848
- }
849
- // First, try to get the existing profile
850
- const getResponse = await fetch(`https://api.onkernel.com/profiles/${encodeURIComponent(profileName)}`, {
851
- method: 'GET',
852
- headers,
853
- });
854
- if (getResponse.ok) {
855
- // Profile exists, return it
856
- return { name: profileName };
857
- }
858
- if (getResponse.status !== 404) {
859
- throw new Error(`Failed to check Kernel profile: ${getResponse.statusText}`);
860
- }
861
- // Profile doesn't exist, create it
862
- const createResponse = await fetch('https://api.onkernel.com/profiles', {
863
- method: 'POST',
864
- headers: {
865
- 'Content-Type': 'application/json',
866
- ...headers,
867
- },
868
- body: JSON.stringify({ name: profileName }),
869
- });
870
- if (!createResponse.ok) {
871
- throw new Error(`Failed to create Kernel profile: ${createResponse.statusText}`);
872
- }
873
- return { name: profileName };
874
- }
875
- /**
876
- * Connect to Kernel remote browser via CDP.
877
- * Uses KERNEL_API_KEY environment variable for authentication when set.
878
- * When running inside environments with external credential injection
879
- * (e.g. Vercel Sandbox credentials brokering), the API key can be omitted
880
- * and auth headers will be injected at the network layer.
881
- */
882
- async connectToKernel() {
883
- const kernelApiKey = process.env.KERNEL_API_KEY;
884
- // Find or create profile if KERNEL_PROFILE_NAME is set
885
- const profileName = process.env.KERNEL_PROFILE_NAME;
886
- let profileConfig;
887
- if (profileName) {
888
- await this.findOrCreateKernelProfile(profileName, kernelApiKey);
889
- profileConfig = {
890
- profile: {
891
- name: profileName,
892
- save_changes: true, // Save cookies/state back to the profile when session ends
893
- },
894
- };
895
- }
896
- const headers = {
897
- 'Content-Type': 'application/json',
898
- };
899
- if (kernelApiKey) {
900
- headers['Authorization'] = `Bearer ${kernelApiKey}`;
901
- }
902
- const response = await fetch('https://api.onkernel.com/browsers', {
903
- method: 'POST',
904
- headers,
905
- body: JSON.stringify({
906
- // Kernel browsers are headful by default with stealth mode available
907
- // The user can configure these via environment variables if needed
908
- headless: process.env.KERNEL_HEADLESS?.toLowerCase() === 'true',
909
- stealth: process.env.KERNEL_STEALTH?.toLowerCase() !== 'false', // Default to stealth mode
910
- timeout_seconds: parseInt(process.env.KERNEL_TIMEOUT_SECONDS || '300', 10),
911
- // Load and save to a profile if specified
912
- ...profileConfig,
913
- }),
914
- });
915
- if (!response.ok) {
916
- throw new Error(`Failed to create Kernel session: ${response.statusText}`);
917
- }
918
- let session;
919
- try {
920
- session = (await response.json());
921
- }
922
- catch (error) {
923
- throw new Error(`Failed to parse Kernel session response: ${error instanceof Error ? error.message : String(error)}`);
924
- }
925
- if (!session.session_id || !session.cdp_ws_url) {
926
- throw new Error(`Invalid Kernel session response: missing ${!session.session_id ? 'session_id' : 'cdp_ws_url'}`);
927
- }
928
- const browser = await chromium.connectOverCDP(session.cdp_ws_url).catch(() => {
929
- throw new Error('Failed to connect to Kernel session via CDP');
930
- });
931
- try {
932
- const contexts = browser.contexts();
933
- let context;
934
- let page;
935
- // Kernel browsers launch with a default context and page
936
- if (contexts.length === 0) {
937
- context = await browser.newContext();
938
- page = await context.newPage();
939
- }
940
- else {
941
- context = contexts[0];
942
- const pages = context.pages();
943
- page = pages[0] ?? (await context.newPage());
944
- }
945
- this.kernelSessionId = session.session_id;
946
- this.kernelApiKey = kernelApiKey ?? null;
947
- this.browser = browser;
948
- context.setDefaultTimeout(getDefaultTimeout());
949
- this.contexts.push(context);
950
- this.setupContextTracking(context);
951
- await this.ensureDomainFilter(context);
952
- await this.sanitizeExistingPages([page]);
953
- this.pages.push(page);
954
- this.activePageIndex = 0;
955
- this.setupPageTracking(page);
956
- }
957
- catch (error) {
958
- await this.closeKernelSession(session.session_id, kernelApiKey).catch((sessionError) => {
959
- console.error('Failed to close Kernel session during cleanup:', sessionError);
960
- });
961
- throw error;
962
- }
963
- }
964
- /**
965
- * Connect to Browser Use remote browser via CDP.
966
- * Requires BROWSER_USE_API_KEY environment variable.
967
- */
968
- async connectToBrowserUse() {
969
- const browserUseApiKey = process.env.BROWSER_USE_API_KEY;
970
- if (!browserUseApiKey) {
971
- throw new Error('BROWSER_USE_API_KEY is required when using browseruse as a provider');
972
- }
973
- const response = await fetch('https://api.browser-use.com/api/v2/browsers', {
974
- method: 'POST',
975
- headers: {
976
- 'Content-Type': 'application/json',
977
- 'X-Browser-Use-API-Key': browserUseApiKey,
978
- },
979
- body: JSON.stringify({}),
980
- });
981
- if (!response.ok) {
982
- throw new Error(`Failed to create Browser Use session: ${response.statusText}`);
983
- }
984
- let session;
985
- try {
986
- session = (await response.json());
987
- }
988
- catch (error) {
989
- throw new Error(`Failed to parse Browser Use session response: ${error instanceof Error ? error.message : String(error)}`);
990
- }
991
- if (!session.id || !session.cdpUrl) {
992
- throw new Error(`Invalid Browser Use session response: missing ${!session.id ? 'id' : 'cdpUrl'}`);
993
- }
994
- const browser = await chromium.connectOverCDP(session.cdpUrl).catch(() => {
995
- throw new Error('Failed to connect to Browser Use session via CDP');
996
- });
997
- try {
998
- const contexts = browser.contexts();
999
- let context;
1000
- let page;
1001
- if (contexts.length === 0) {
1002
- context = await browser.newContext();
1003
- page = await context.newPage();
1004
- }
1005
- else {
1006
- context = contexts[0];
1007
- const pages = context.pages();
1008
- page = pages[0] ?? (await context.newPage());
1009
- }
1010
- this.browserUseSessionId = session.id;
1011
- this.browserUseApiKey = browserUseApiKey;
1012
- this.browser = browser;
1013
- context.setDefaultTimeout(getDefaultTimeout());
1014
- this.contexts.push(context);
1015
- this.setupContextTracking(context);
1016
- await this.ensureDomainFilter(context);
1017
- await this.sanitizeExistingPages([page]);
1018
- this.pages.push(page);
1019
- this.activePageIndex = 0;
1020
- this.setupPageTracking(page);
1021
- }
1022
- catch (error) {
1023
- await this.closeBrowserUseSession(session.id, browserUseApiKey).catch((sessionError) => {
1024
- console.error('Failed to close Browser Use session during cleanup:', sessionError);
1025
- });
1026
- throw error;
1027
- }
1028
- }
1029
- /**
1030
- * Launch the browser with the specified options
1031
- * If already launched, this is a no-op (browser stays open)
1032
- */
1033
- async launch(options) {
1034
- // Determine CDP endpoint: prefer cdpUrl over cdpPort for flexibility
1035
- const cdpEndpoint = options.cdpUrl ?? (options.cdpPort ? String(options.cdpPort) : undefined);
1036
- const hasExtensions = !!options.extensions?.length;
1037
- const hasProfile = !!options.profile;
1038
- const hasStorageState = !!options.storageState;
1039
- if (hasExtensions && cdpEndpoint) {
1040
- throw new Error('Extensions cannot be used with CDP connection');
1041
- }
1042
- if (hasProfile && cdpEndpoint) {
1043
- throw new Error('Profile cannot be used with CDP connection');
1044
- }
1045
- if (hasStorageState && hasProfile) {
1046
- throw new Error('Storage state cannot be used with profile (profile is already persistent storage)');
1047
- }
1048
- if (hasStorageState && hasExtensions) {
1049
- throw new Error('Storage state cannot be used with extensions (extensions require persistent context)');
1050
- }
1051
- if (this.isLaunched()) {
1052
- const needsRelaunch = (!cdpEndpoint && !options.autoConnect && this.cdpEndpoint !== null) ||
1053
- (!!cdpEndpoint && this.needsCdpReconnect(cdpEndpoint)) ||
1054
- (!!options.autoConnect && !this.isCdpConnectionAlive());
1055
- if (needsRelaunch) {
1056
- await this.close();
1057
- }
1058
- else if (options.autoConnect && this.isCdpConnectionAlive()) {
1059
- // Already connected via auto-connect, no need to reconnect
1060
- return;
1061
- }
1062
- else {
1063
- return;
1064
- }
1065
- }
1066
- if (options.colorScheme) {
1067
- this.colorScheme = options.colorScheme;
1068
- }
1069
- if (options.downloadPath) {
1070
- this.downloadPath = options.downloadPath;
1071
- }
1072
- if (options.allowedDomains && options.allowedDomains.length > 0) {
1073
- this.allowedDomains = options.allowedDomains.map((d) => d.toLowerCase());
1074
- }
1075
- else {
1076
- const envDomains = process.env.AGENT_BROWSER_ALLOWED_DOMAINS;
1077
- if (envDomains) {
1078
- this.allowedDomains = parseDomainList(envDomains);
1079
- }
1080
- }
1081
- if (this.downloadPath && (cdpEndpoint || options.autoConnect)) {
1082
- const warning = "--download-path is ignored when connecting via CDP or auto-connect (downloads use the remote browser's configuration)";
1083
- this.launchWarnings.push(warning);
1084
- console.error(`[WARN] ${warning}`);
1085
- }
1086
- if (cdpEndpoint) {
1087
- await this.connectViaCDP(cdpEndpoint);
1088
- return;
1089
- }
1090
- if (options.autoConnect) {
1091
- await this.autoConnectViaCDP();
1092
- return;
1093
- }
1094
- // Cloud browser providers require explicit opt-in via -p flag or AGENT_BROWSER_PROVIDER env var
1095
- // -p flag takes precedence over env var
1096
- const provider = options.provider ?? process.env.AGENT_BROWSER_PROVIDER;
1097
- if (this.downloadPath && provider) {
1098
- const warning = "--download-path is ignored when using a cloud provider (downloads use the remote browser's configuration)";
1099
- this.launchWarnings.push(warning);
1100
- console.error(`[WARN] ${warning}`);
1101
- }
1102
- if (provider === 'browserbase') {
1103
- await this.connectToBrowserbase();
1104
- return;
1105
- }
1106
- if (provider === 'browseruse') {
1107
- await this.connectToBrowserUse();
1108
- return;
1109
- }
1110
- // Kernel: requires explicit opt-in via -p kernel flag or AGENT_BROWSER_PROVIDER=kernel
1111
- if (provider === 'kernel') {
1112
- await this.connectToKernel();
1113
- return;
1114
- }
1115
- if (this.downloadPath) {
1116
- const resolved = path.resolve(this.downloadPath);
1117
- const stat = statSync(resolved, { throwIfNoEntry: false });
1118
- if (stat && !stat.isDirectory()) {
1119
- throw new Error(`Download path is not a directory: ${resolved}`);
1120
- }
1121
- if (!stat) {
1122
- try {
1123
- mkdirSync(resolved, { recursive: true });
1124
- }
1125
- catch (e) {
1126
- const msg = e instanceof Error ? e.message : String(e);
1127
- throw new Error(`Cannot create download directory '${resolved}': ${msg}`);
1128
- }
1129
- }
1130
- this.downloadPath = resolved;
1131
- }
1132
- const browserType = options.browser ?? 'chromium';
1133
- if (hasExtensions && browserType !== 'chromium') {
1134
- throw new Error('Extensions are only supported in Chromium');
1135
- }
1136
- // allowFileAccess is only supported in Chromium
1137
- if (options.allowFileAccess && browserType !== 'chromium') {
1138
- throw new Error('allowFileAccess is only supported in Chromium');
1139
- }
1140
- const launcher = browserType === 'firefox' ? firefox : browserType === 'webkit' ? webkit : chromium;
1141
- // Build base args array with file access flags if enabled
1142
- // --allow-file-access-from-files: allows file:// URLs to read other file:// URLs via XHR/fetch
1143
- // --allow-file-access: allows the browser to access local files in general
1144
- const fileAccessArgs = options.allowFileAccess
1145
- ? ['--allow-file-access-from-files', '--allow-file-access']
1146
- : [];
1147
- const baseArgs = options.args
1148
- ? [...fileAccessArgs, ...options.args]
1149
- : fileAccessArgs.length > 0
1150
- ? fileAccessArgs
1151
- : undefined;
1152
- // Auto-detect args that control window size and disable viewport emulation
1153
- // so Playwright doesn't override the browser's own sizing behavior
1154
- const hasWindowSizeArgs = baseArgs?.some((arg) => arg === '--start-maximized' || arg.startsWith('--window-size='));
1155
- const viewport = options.viewport !== undefined
1156
- ? options.viewport
1157
- : hasWindowSizeArgs
1158
- ? null
1159
- : { width: 1280, height: 720 };
1160
- let context;
1161
- if (hasExtensions) {
1162
- // Extensions require persistent context in a temp directory
1163
- const extPaths = options.extensions.join(',');
1164
- const session = process.env.AGENT_BROWSER_SESSION || 'default';
1165
- // Combine extension args with custom args and file access args
1166
- const extArgs = [`--disable-extensions-except=${extPaths}`, `--load-extension=${extPaths}`];
1167
- const allArgs = baseArgs ? [...extArgs, ...baseArgs] : extArgs;
1168
- context = await launcher.launchPersistentContext(path.join(os.tmpdir(), `agent-browser-ext-${session}`), {
1169
- headless: options.headless ?? true,
1170
- executablePath: options.executablePath,
1171
- args: allArgs,
1172
- viewport,
1173
- extraHTTPHeaders: options.headers,
1174
- userAgent: options.userAgent,
1175
- ...(options.proxy && { proxy: options.proxy }),
1176
- ignoreHTTPSErrors: options.ignoreHTTPSErrors ?? false,
1177
- ...(this.colorScheme && { colorScheme: this.colorScheme }),
1178
- ...(this.downloadPath && { downloadsPath: this.downloadPath }),
1179
- });
1180
- this.isPersistentContext = true;
1181
- }
1182
- else if (hasProfile) {
1183
- // Profile uses persistent context for durable cookies/storage
1184
- // Expand ~ to home directory since it won't be shell-expanded
1185
- const profilePath = options.profile.replace(/^~\//, os.homedir() + '/');
1186
- context = await launcher.launchPersistentContext(profilePath, {
1187
- headless: options.headless ?? true,
1188
- executablePath: options.executablePath,
1189
- args: baseArgs,
1190
- viewport,
1191
- extraHTTPHeaders: options.headers,
1192
- userAgent: options.userAgent,
1193
- ...(options.proxy && { proxy: options.proxy }),
1194
- ignoreHTTPSErrors: options.ignoreHTTPSErrors ?? false,
1195
- ...(this.colorScheme && { colorScheme: this.colorScheme }),
1196
- ...(this.downloadPath && { downloadsPath: this.downloadPath }),
1197
- });
1198
- this.isPersistentContext = true;
1199
- }
1200
- else {
1201
- // Regular ephemeral browser
1202
- this.browser = await launcher.launch({
1203
- headless: options.headless ?? true,
1204
- executablePath: options.executablePath,
1205
- args: baseArgs,
1206
- ...(this.downloadPath && { downloadsPath: this.downloadPath }),
1207
- });
1208
- this.cdpEndpoint = null;
1209
- this.resolvedWsUrl = null;
1210
- // Check for auto-load state file (supports encrypted files)
1211
- let storageState = options.storageState ? options.storageState : undefined;
1212
- if (!storageState && options.autoStateFilePath) {
1213
- try {
1214
- const fs = await import('fs');
1215
- if (fs.existsSync(options.autoStateFilePath)) {
1216
- const content = fs.readFileSync(options.autoStateFilePath, 'utf8');
1217
- const parsed = JSON.parse(content);
1218
- if (isEncryptedPayload(parsed)) {
1219
- const key = getEncryptionKey();
1220
- if (key) {
1221
- try {
1222
- const decrypted = decryptData(parsed, key);
1223
- storageState = JSON.parse(decrypted);
1224
- if (process.env.AGENT_BROWSER_DEBUG === '1') {
1225
- console.error(`[DEBUG] Auto-loading session state (decrypted): ${options.autoStateFilePath}`);
1226
- }
1227
- }
1228
- catch (decryptErr) {
1229
- const warning = 'Failed to decrypt state file - wrong encryption key? Starting fresh.';
1230
- this.launchWarnings.push(warning);
1231
- console.error(`[WARN] ${warning}`);
1232
- if (process.env.AGENT_BROWSER_DEBUG === '1') {
1233
- console.error(`[DEBUG] Decryption error:`, decryptErr);
1234
- }
1235
- }
1236
- }
1237
- else {
1238
- const warning = `State file is encrypted but ${ENCRYPTION_KEY_ENV} not set - starting fresh`;
1239
- this.launchWarnings.push(warning);
1240
- console.error(`[WARN] ${warning}`);
1241
- }
1242
- }
1243
- else {
1244
- storageState = options.autoStateFilePath;
1245
- if (process.env.AGENT_BROWSER_DEBUG === '1') {
1246
- console.error(`[DEBUG] Auto-loading session state: ${options.autoStateFilePath}`);
1247
- }
1248
- }
1249
- }
1250
- }
1251
- catch (err) {
1252
- if (process.env.AGENT_BROWSER_DEBUG === '1') {
1253
- console.error(`[DEBUG] Failed to load state file, starting fresh:`, err);
1254
- }
1255
- }
1256
- }
1257
- context = await this.browser.newContext({
1258
- viewport,
1259
- extraHTTPHeaders: options.headers,
1260
- userAgent: options.userAgent,
1261
- storageState,
1262
- ...(options.proxy && { proxy: options.proxy }),
1263
- ignoreHTTPSErrors: options.ignoreHTTPSErrors ?? false,
1264
- ...(this.colorScheme && { colorScheme: this.colorScheme }),
1265
- });
1266
- }
1267
- context.setDefaultTimeout(getDefaultTimeout());
1268
- this.contexts.push(context);
1269
- this.setupContextTracking(context);
1270
- await this.ensureDomainFilter(context);
1271
- const page = context.pages()[0] ?? (await context.newPage());
1272
- await this.sanitizeExistingPages([page]);
1273
- // Only add if not already tracked (setupContextTracking may have already added it via 'page' event)
1274
- if (!this.pages.includes(page)) {
1275
- this.pages.push(page);
1276
- this.setupPageTracking(page);
1277
- }
1278
- this.activePageIndex = this.pages.length > 0 ? this.pages.length - 1 : 0;
1279
- }
1280
- /**
1281
- * Connect to a running browser via CDP (Chrome DevTools Protocol)
1282
- * @param cdpEndpoint Either a port number (as string) or a full WebSocket URL (ws:// or wss://)
1283
- */
1284
- async connectViaCDP(cdpEndpoint, options) {
1285
- if (!cdpEndpoint) {
1286
- throw new Error('CDP endpoint is required for CDP connection');
1287
- }
1288
- // Determine the connection URL:
1289
- // - If it starts with ws://, wss://, http://, or https://, use it directly
1290
- // - If it's a numeric string (e.g., "9222"), treat as port for localhost
1291
- // - Otherwise, treat it as a port number for localhost
1292
- let cdpUrl;
1293
- if (cdpEndpoint.startsWith('ws://') ||
1294
- cdpEndpoint.startsWith('wss://') ||
1295
- cdpEndpoint.startsWith('http://') ||
1296
- cdpEndpoint.startsWith('https://')) {
1297
- cdpUrl = cdpEndpoint;
1298
- }
1299
- else if (/^\d+$/.test(cdpEndpoint)) {
1300
- // Numeric string - treat as port number (handles JSON serialization quirks)
1301
- cdpUrl = `http://127.0.0.1:${cdpEndpoint}`;
1302
- }
1303
- else {
1304
- // Unknown format - still try as port for backward compatibility
1305
- cdpUrl = `http://127.0.0.1:${cdpEndpoint}`;
1306
- }
1307
- const browser = await chromium
1308
- .connectOverCDP(cdpUrl, { timeout: options?.timeout })
1309
- .catch(() => {
1310
- throw new Error(`Failed to connect via CDP to ${cdpUrl}. ` +
1311
- (cdpUrl.includes('127.0.0.1')
1312
- ? `Make sure the app is running with --remote-debugging-port=${cdpEndpoint}`
1313
- : 'Make sure the remote browser is accessible and the URL is correct.'));
1314
- });
1315
- // Validate and set up state, cleaning up browser connection if anything fails
1316
- try {
1317
- const contexts = browser.contexts();
1318
- if (contexts.length === 0) {
1319
- throw new Error('No browser context found. Make sure the app has an open window.');
1320
- }
1321
- // Filter out pages with empty URLs, which can cause Playwright to hang
1322
- const allPages = contexts.flatMap((context) => context.pages()).filter((page) => page.url());
1323
- if (allPages.length === 0) {
1324
- throw new Error('No page found. Make sure the app has loaded content.');
1325
- }
1326
- // All validation passed - commit state
1327
- this.browser = browser;
1328
- this.cdpEndpoint = cdpEndpoint;
1329
- let resolvedWs = null;
1330
- try {
1331
- resolvedWs = browser.wsEndpoint?.() ?? null;
1332
- }
1333
- catch (err) {
1334
- console.error('[inspect] wsEndpoint() failed:', err);
1335
- }
1336
- if (!resolvedWs && (cdpUrl.startsWith('http://') || cdpUrl.startsWith('https://'))) {
1337
- try {
1338
- const resp = await fetch(`${cdpUrl}/json/version`);
1339
- const info = await resp.json();
1340
- resolvedWs = info.webSocketDebuggerUrl ?? null;
1341
- }
1342
- catch (err) {
1343
- console.error('[inspect] /json/version fetch failed:', err);
1344
- }
1345
- }
1346
- this.resolvedWsUrl = resolvedWs;
1347
- for (const context of contexts) {
1348
- context.setDefaultTimeout(getDefaultTimeout());
1349
- this.contexts.push(context);
1350
- this.setupContextTracking(context);
1351
- await this.ensureDomainFilter(context);
1352
- }
1353
- await this.sanitizeExistingPages(allPages);
1354
- for (const page of allPages) {
1355
- this.pages.push(page);
1356
- this.setupPageTracking(page);
1357
- }
1358
- this.activePageIndex = 0;
1359
- }
1360
- catch (error) {
1361
- // Clean up browser connection if validation or setup failed
1362
- await browser.close().catch(() => { });
1363
- throw error;
1364
- }
1365
- }
1366
- /**
1367
- * Get Chrome's default user data directory paths for the current platform.
1368
- * Returns an array of candidate paths to check (stable, then beta/canary).
1369
- */
1370
- getChromeUserDataDirs() {
1371
- const home = os.homedir();
1372
- const platform = os.platform();
1373
- if (platform === 'darwin') {
1374
- return [
1375
- path.join(home, 'Library', 'Application Support', 'Google', 'Chrome'),
1376
- path.join(home, 'Library', 'Application Support', 'Google', 'Chrome Canary'),
1377
- path.join(home, 'Library', 'Application Support', 'Chromium'),
1378
- ];
1379
- }
1380
- else if (platform === 'win32') {
1381
- const localAppData = process.env.LOCALAPPDATA ?? path.join(home, 'AppData', 'Local');
1382
- return [
1383
- path.join(localAppData, 'Google', 'Chrome', 'User Data'),
1384
- path.join(localAppData, 'Google', 'Chrome SxS', 'User Data'),
1385
- path.join(localAppData, 'Chromium', 'User Data'),
1386
- ];
1387
- }
1388
- else {
1389
- // Linux
1390
- return [
1391
- path.join(home, '.config', 'google-chrome'),
1392
- path.join(home, '.config', 'google-chrome-unstable'),
1393
- path.join(home, '.config', 'chromium'),
1394
- ];
1395
- }
1396
- }
1397
- /**
1398
- * Try to read the DevToolsActivePort file from a Chrome user data directory.
1399
- * Returns { port, wsPath } if found, or null if not available.
1400
- */
1401
- readDevToolsActivePort(userDataDir) {
1402
- const filePath = path.join(userDataDir, 'DevToolsActivePort');
1403
- try {
1404
- if (!existsSync(filePath))
1405
- return null;
1406
- const content = readFileSync(filePath, 'utf-8').trim();
1407
- const lines = content.split('\n');
1408
- if (lines.length < 2)
1409
- return null;
1410
- const port = parseInt(lines[0].trim(), 10);
1411
- const wsPath = lines[1].trim();
1412
- if (isNaN(port) || port <= 0 || port > 65535)
1413
- return null;
1414
- if (!wsPath)
1415
- return null;
1416
- return { port, wsPath };
1417
- }
1418
- catch {
1419
- return null;
1420
- }
1421
- }
1422
- /**
1423
- * Try to discover a Chrome CDP endpoint by querying an HTTP debug port.
1424
- * Returns the WebSocket debugger URL if available.
1425
- */
1426
- async probeDebugPort(port) {
1427
- try {
1428
- const response = await fetch(`http://127.0.0.1:${port}/json/version`, {
1429
- signal: AbortSignal.timeout(2000),
1430
- });
1431
- if (!response.ok)
1432
- return null;
1433
- const data = (await response.json());
1434
- return data.webSocketDebuggerUrl ?? null;
1435
- }
1436
- catch {
1437
- return null;
1438
- }
1439
- }
1440
- /**
1441
- * Auto-discover and connect to a running Chrome/Chromium instance.
1442
- *
1443
- * Discovery strategy:
1444
- * 1. Read DevToolsActivePort from Chrome's default user data directories
1445
- * 2. If found, connect using the port and WebSocket path from that file
1446
- * 3. If not found, probe common debugging ports (9222, 9229)
1447
- * 4. If a port responds, connect via CDP
1448
- */
1449
- async autoConnectViaCDP() {
1450
- // Strategy 1: Check DevToolsActivePort files
1451
- const userDataDirs = this.getChromeUserDataDirs();
1452
- for (const dir of userDataDirs) {
1453
- const activePort = this.readDevToolsActivePort(dir);
1454
- if (activePort) {
1455
- // Try HTTP discovery first (works with --remote-debugging-port mode)
1456
- const wsUrl = await this.probeDebugPort(activePort.port);
1457
- if (wsUrl) {
1458
- await this.connectViaCDP(wsUrl);
1459
- return;
1460
- }
1461
- // HTTP probe failed -- Chrome M144+ chrome://inspect remote debugging uses a
1462
- // WebSocket-only server with no HTTP endpoints. Connect using the WebSocket
1463
- // path read directly from DevToolsActivePort.
1464
- const directWsUrl = `ws://127.0.0.1:${activePort.port}${activePort.wsPath}`;
1465
- try {
1466
- if (process.env.AGENT_BROWSER_DEBUG === '1') {
1467
- console.error(`[DEBUG] HTTP probe failed on port ${activePort.port}, ` +
1468
- `attempting direct WebSocket connection to ${directWsUrl}`);
1469
- }
1470
- await this.connectViaCDP(directWsUrl, { timeout: 60_000 });
1471
- return;
1472
- }
1473
- catch {
1474
- // Direct WebSocket also failed, try next directory
1475
- }
1476
- }
1477
- }
1478
- // Strategy 2: Probe common debugging ports
1479
- const commonPorts = [9222, 9229];
1480
- for (const port of commonPorts) {
1481
- const wsUrl = await this.probeDebugPort(port);
1482
- if (wsUrl) {
1483
- await this.connectViaCDP(wsUrl);
1484
- return;
1485
- }
1486
- }
1487
- // Nothing found
1488
- const platform = os.platform();
1489
- let hint;
1490
- if (platform === 'darwin') {
1491
- hint =
1492
- 'Start Chrome with: /Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome --remote-debugging-port=9222\n' +
1493
- 'Or enable remote debugging in Chrome 144+ at chrome://inspect/#remote-debugging';
1494
- }
1495
- else if (platform === 'win32') {
1496
- hint =
1497
- 'Start Chrome with: chrome.exe --remote-debugging-port=9222\n' +
1498
- 'Or enable remote debugging in Chrome 144+ at chrome://inspect/#remote-debugging';
1499
- }
1500
- else {
1501
- hint =
1502
- 'Start Chrome with: google-chrome --remote-debugging-port=9222\n' +
1503
- 'Or enable remote debugging in Chrome 144+ at chrome://inspect/#remote-debugging';
1504
- }
1505
- throw new Error(`No running Chrome instance with remote debugging found.\n${hint}`);
1506
- }
1507
- /**
1508
- * Set up console, error, and close tracking for a page
1509
- */
1510
- setupPageTracking(page) {
1511
- if (this.colorScheme) {
1512
- page.emulateMedia({ colorScheme: this.colorScheme }).catch(() => { });
1513
- }
1514
- page.on('console', (msg) => {
1515
- this.consoleMessages.push({
1516
- type: msg.type(),
1517
- text: msg.text(),
1518
- timestamp: Date.now(),
1519
- });
1520
- });
1521
- page.on('pageerror', (error) => {
1522
- this.pageErrors.push({
1523
- message: error.message,
1524
- timestamp: Date.now(),
1525
- });
1526
- });
1527
- page.on('close', () => {
1528
- const index = this.pages.indexOf(page);
1529
- if (index !== -1) {
1530
- this.pages.splice(index, 1);
1531
- if (this.activePageIndex >= this.pages.length) {
1532
- this.activePageIndex = Math.max(0, this.pages.length - 1);
1533
- }
1534
- }
1535
- });
1536
- }
1537
- /**
1538
- * Set up tracking for new pages in a context (for CDP connections and popups/new tabs)
1539
- * This handles pages created externally (e.g., via target="_blank" links, window.open)
1540
- */
1541
- setupContextTracking(context) {
1542
- context.on('page', (page) => {
1543
- // Only add if not already tracked (avoids duplicates when newTab() creates pages)
1544
- if (!this.pages.includes(page)) {
1545
- this.pages.push(page);
1546
- this.setupPageTracking(page);
1547
- }
1548
- // Auto-switch to the newly opened tab so subsequent commands target it.
1549
- // For tabs created via newTab()/newWindow(), this is redundant (they set activePageIndex after),
1550
- // but for externally opened tabs (window.open, target="_blank"), this ensures the active tab
1551
- // stays in sync with the browser.
1552
- const newIndex = this.pages.indexOf(page);
1553
- if (newIndex !== -1 && newIndex !== this.activePageIndex) {
1554
- this.activePageIndex = newIndex;
1555
- // Invalidate CDP session since the active page changed
1556
- this.invalidateCDPSession().catch(() => { });
1557
- }
1558
- });
1559
- }
1560
- /**
1561
- * Create a new tab in the current context
1562
- */
1563
- async newTab() {
1564
- if (!this.isLaunched() || this.contexts.length === 0) {
1565
- throw new Error('Browser not launched');
1566
- }
1567
- // Invalidate CDP session since we're switching to a new page
1568
- await this.invalidateCDPSession();
1569
- const context = this.contexts[0]; // Use first context for tabs
1570
- const page = await context.newPage();
1571
- // Only add if not already tracked (setupContextTracking may have already added it via 'page' event)
1572
- if (!this.pages.includes(page)) {
1573
- this.pages.push(page);
1574
- this.setupPageTracking(page);
1575
- }
1576
- this.activePageIndex = this.pages.length - 1;
1577
- return { index: this.activePageIndex, total: this.pages.length };
1578
- }
1579
- /**
1580
- * Create a new window (new context)
1581
- */
1582
- async newWindow(viewport) {
1583
- if (!this.browser) {
1584
- throw new Error(this.isPersistentContext
1585
- ? 'newWindow is not supported in extension (persistent context) mode'
1586
- : 'Browser not launched');
1587
- }
1588
- const context = await this.browser.newContext({
1589
- viewport: viewport === undefined ? { width: 1280, height: 720 } : viewport,
1590
- ...(this.colorScheme && { colorScheme: this.colorScheme }),
1591
- });
1592
- context.setDefaultTimeout(getDefaultTimeout());
1593
- this.contexts.push(context);
1594
- this.setupContextTracking(context);
1595
- await this.ensureDomainFilter(context);
1596
- const page = await context.newPage();
1597
- // Only add if not already tracked (setupContextTracking may have already added it via 'page' event)
1598
- if (!this.pages.includes(page)) {
1599
- this.pages.push(page);
1600
- this.setupPageTracking(page);
1601
- }
1602
- this.activePageIndex = this.pages.length - 1;
1603
- return { index: this.activePageIndex, total: this.pages.length };
1604
- }
1605
- /**
1606
- * Invalidate the current CDP session (must be called before switching pages)
1607
- * This ensures screencast and input injection work correctly after tab switch
1608
- */
1609
- async invalidateCDPSession() {
1610
- // Stop screencast if active (it's tied to the current page's CDP session)
1611
- if (this.screencastActive) {
1612
- await this.stopScreencast();
1613
- }
1614
- // Detach and clear the CDP session
1615
- if (this.cdpSession) {
1616
- await this.cdpSession.detach().catch(() => { });
1617
- this.cdpSession = null;
1618
- }
1619
- }
1620
- /**
1621
- * Switch to a specific tab/page by index
1622
- */
1623
- async switchTo(index) {
1624
- if (index < 0 || index >= this.pages.length) {
1625
- throw new Error(`Invalid tab index: ${index}. Available: 0-${this.pages.length - 1}`);
1626
- }
1627
- // Invalidate CDP session before switching (it's page-specific)
1628
- if (index !== this.activePageIndex) {
1629
- await this.invalidateCDPSession();
1630
- }
1631
- this.activePageIndex = index;
1632
- const page = this.pages[index];
1633
- return {
1634
- index: this.activePageIndex,
1635
- url: page.url(),
1636
- title: '', // Title requires async, will be fetched separately
1637
- };
1638
- }
1639
- /**
1640
- * Close a specific tab/page
1641
- */
1642
- async closeTab(index) {
1643
- const targetIndex = index ?? this.activePageIndex;
1644
- if (targetIndex < 0 || targetIndex >= this.pages.length) {
1645
- throw new Error(`Invalid tab index: ${targetIndex}`);
1646
- }
1647
- if (this.pages.length === 1) {
1648
- throw new Error('Cannot close the last tab. Use "close" to close the browser.');
1649
- }
1650
- // If closing the active tab, invalidate CDP session first
1651
- if (targetIndex === this.activePageIndex) {
1652
- await this.invalidateCDPSession();
1653
- }
1654
- const page = this.pages[targetIndex];
1655
- await page.close();
1656
- this.pages.splice(targetIndex, 1);
1657
- // Adjust active index if needed
1658
- if (this.activePageIndex >= this.pages.length) {
1659
- this.activePageIndex = this.pages.length - 1;
1660
- }
1661
- else if (this.activePageIndex > targetIndex) {
1662
- this.activePageIndex--;
1663
- }
1664
- return { closed: targetIndex, remaining: this.pages.length };
1665
- }
1666
- /**
1667
- * List all tabs with their info
1668
- */
1669
- async listTabs() {
1670
- const tabs = await Promise.all(this.pages.map(async (page, index) => ({
1671
- index,
1672
- url: page.url(),
1673
- title: await page.title().catch(() => ''),
1674
- active: index === this.activePageIndex,
1675
- })));
1676
- return tabs;
1677
- }
1678
- /**
1679
- * Get or create a CDP session for the current page
1680
- * Only works with Chromium-based browsers
1681
- */
1682
- async getCDPSession() {
1683
- if (this.cdpSession) {
1684
- return this.cdpSession;
1685
- }
1686
- const page = this.getPage();
1687
- const context = page.context();
1688
- // Create a new CDP session attached to the page
1689
- this.cdpSession = await context.newCDPSession(page);
1690
- return this.cdpSession;
1691
- }
1692
- /**
1693
- * Check if screencast is currently active
1694
- */
1695
- isScreencasting() {
1696
- return this.screencastActive;
1697
- }
1698
- /**
1699
- * Start screencast - streams viewport frames via CDP
1700
- * @param callback Function called for each frame
1701
- * @param options Screencast options
1702
- */
1703
- async startScreencast(callback, options) {
1704
- if (this.screencastActive) {
1705
- throw new Error('Screencast already active');
1706
- }
1707
- const cdp = await this.getCDPSession();
1708
- this.frameCallback = callback;
1709
- this.screencastActive = true;
1710
- // Create and store the frame handler so we can remove it later
1711
- this.screencastFrameHandler = async (params) => {
1712
- const frame = {
1713
- data: params.data,
1714
- metadata: params.metadata,
1715
- sessionId: params.sessionId,
1716
- };
1717
- // Acknowledge the frame to receive the next one
1718
- await cdp.send('Page.screencastFrameAck', { sessionId: params.sessionId });
1719
- // Call the callback with the frame
1720
- if (this.frameCallback) {
1721
- this.frameCallback(frame);
1722
- }
1723
- };
1724
- // Listen for screencast frames
1725
- cdp.on('Page.screencastFrame', this.screencastFrameHandler);
1726
- // Start the screencast
1727
- await cdp.send('Page.startScreencast', {
1728
- format: options?.format ?? 'jpeg',
1729
- quality: options?.quality ?? 80,
1730
- maxWidth: options?.maxWidth ?? 1280,
1731
- maxHeight: options?.maxHeight ?? 720,
1732
- everyNthFrame: options?.everyNthFrame ?? 1,
1733
- });
1734
- }
1735
- /**
1736
- * Stop screencast
1737
- */
1738
- async stopScreencast() {
1739
- if (!this.screencastActive) {
1740
- return;
1741
- }
1742
- try {
1743
- const cdp = await this.getCDPSession();
1744
- await cdp.send('Page.stopScreencast');
1745
- // Remove the event listener to prevent accumulation
1746
- if (this.screencastFrameHandler) {
1747
- cdp.off('Page.screencastFrame', this.screencastFrameHandler);
1748
- }
1749
- }
1750
- catch {
1751
- // Ignore errors when stopping
1752
- }
1753
- this.screencastActive = false;
1754
- this.frameCallback = null;
1755
- this.screencastFrameHandler = null;
1756
- }
1757
- /**
1758
- * Check if profiling is currently active
1759
- */
1760
- isProfilingActive() {
1761
- return this.profilingActive;
1762
- }
1763
- /**
1764
- * Start CDP profiling (Tracing)
1765
- */
1766
- async startProfiling(options) {
1767
- if (this.profilingActive) {
1768
- throw new Error('Profiling already active');
1769
- }
1770
- const cdp = await this.getCDPSession();
1771
- const dataHandler = (params) => {
1772
- if (params.value) {
1773
- for (const evt of params.value) {
1774
- if (this.profileChunks.length >= BrowserManager.MAX_PROFILE_EVENTS) {
1775
- if (!this.profileEventsDropped) {
1776
- this.profileEventsDropped = true;
1777
- console.warn(`Profiling: exceeded ${BrowserManager.MAX_PROFILE_EVENTS} events, dropping further data`);
1778
- }
1779
- return;
1780
- }
1781
- this.profileChunks.push(evt);
1782
- }
1783
- }
1784
- };
1785
- const completeHandler = () => {
1786
- if (this.profileCompleteResolver) {
1787
- this.profileCompleteResolver();
1788
- }
1789
- };
1790
- cdp.on('Tracing.dataCollected', dataHandler);
1791
- cdp.on('Tracing.tracingComplete', completeHandler);
1792
- const categories = options?.categories ?? [
1793
- 'devtools.timeline',
1794
- 'disabled-by-default-devtools.timeline',
1795
- 'disabled-by-default-devtools.timeline.frame',
1796
- 'disabled-by-default-devtools.timeline.stack',
1797
- 'v8.execute',
1798
- 'disabled-by-default-v8.cpu_profiler',
1799
- 'disabled-by-default-v8.cpu_profiler.hires',
1800
- 'v8',
1801
- 'disabled-by-default-v8.runtime_stats',
1802
- 'blink',
1803
- 'blink.user_timing',
1804
- 'latencyInfo',
1805
- 'renderer.scheduler',
1806
- 'sequence_manager',
1807
- 'toplevel',
1808
- ];
1809
- try {
1810
- await cdp.send('Tracing.start', {
1811
- traceConfig: {
1812
- includedCategories: categories,
1813
- enableSampling: true,
1814
- },
1815
- transferMode: 'ReportEvents',
1816
- });
1817
- }
1818
- catch (error) {
1819
- cdp.off('Tracing.dataCollected', dataHandler);
1820
- cdp.off('Tracing.tracingComplete', completeHandler);
1821
- throw error;
1822
- }
1823
- // Only commit state after the CDP call succeeds
1824
- this.profilingActive = true;
1825
- this.profileChunks = [];
1826
- this.profileEventsDropped = false;
1827
- this.profileDataHandler = dataHandler;
1828
- this.profileCompleteHandler = completeHandler;
1829
- }
1830
- /**
1831
- * Stop CDP profiling and save to file
1832
- */
1833
- async stopProfiling(outputPath) {
1834
- if (!this.profilingActive) {
1835
- throw new Error('No profiling session active');
1836
- }
1837
- const cdp = await this.getCDPSession();
1838
- const TRACE_TIMEOUT_MS = 30_000;
1839
- const completePromise = new Promise((resolve, reject) => {
1840
- const timer = setTimeout(() => reject(new Error('Profiling data collection timed out')), TRACE_TIMEOUT_MS);
1841
- this.profileCompleteResolver = () => {
1842
- clearTimeout(timer);
1843
- resolve();
1844
- };
1845
- });
1846
- await cdp.send('Tracing.end');
1847
- let chunks;
1848
- try {
1849
- await completePromise;
1850
- chunks = this.profileChunks;
1851
- }
1852
- finally {
1853
- if (this.profileDataHandler) {
1854
- cdp.off('Tracing.dataCollected', this.profileDataHandler);
1855
- }
1856
- if (this.profileCompleteHandler) {
1857
- cdp.off('Tracing.tracingComplete', this.profileCompleteHandler);
1858
- }
1859
- this.profilingActive = false;
1860
- this.profileChunks = [];
1861
- this.profileEventsDropped = false;
1862
- this.profileCompleteResolver = null;
1863
- this.profileDataHandler = null;
1864
- this.profileCompleteHandler = null;
1865
- }
1866
- const clockDomain = process.platform === 'linux'
1867
- ? 'LINUX_CLOCK_MONOTONIC'
1868
- : process.platform === 'darwin'
1869
- ? 'MAC_MACH_ABSOLUTE_TIME'
1870
- : undefined;
1871
- const traceData = {
1872
- traceEvents: chunks,
1873
- };
1874
- if (clockDomain) {
1875
- traceData.metadata = { 'clock-domain': clockDomain };
1876
- }
1877
- const dir = path.dirname(outputPath);
1878
- await mkdir(dir, { recursive: true });
1879
- await writeFile(outputPath, JSON.stringify(traceData));
1880
- const eventCount = chunks.length;
1881
- return { path: outputPath, eventCount };
1882
- }
1883
- /**
1884
- * Inject a mouse event via CDP
1885
- */
1886
- async injectMouseEvent(params) {
1887
- const cdp = await this.getCDPSession();
1888
- const cdpButton = params.button === 'left'
1889
- ? 'left'
1890
- : params.button === 'right'
1891
- ? 'right'
1892
- : params.button === 'middle'
1893
- ? 'middle'
1894
- : 'none';
1895
- await cdp.send('Input.dispatchMouseEvent', {
1896
- type: params.type,
1897
- x: params.x,
1898
- y: params.y,
1899
- button: cdpButton,
1900
- clickCount: params.clickCount ?? 1,
1901
- deltaX: params.deltaX ?? 0,
1902
- deltaY: params.deltaY ?? 0,
1903
- modifiers: params.modifiers ?? 0,
1904
- });
1905
- }
1906
- /**
1907
- * Inject a keyboard event via CDP
1908
- */
1909
- async injectKeyboardEvent(params) {
1910
- const cdp = await this.getCDPSession();
1911
- await cdp.send('Input.dispatchKeyEvent', {
1912
- type: params.type,
1913
- key: params.key,
1914
- code: params.code,
1915
- text: params.text,
1916
- modifiers: params.modifiers ?? 0,
1917
- });
1918
- }
1919
- /**
1920
- * Inject touch event via CDP (for mobile emulation)
1921
- */
1922
- async injectTouchEvent(params) {
1923
- const cdp = await this.getCDPSession();
1924
- await cdp.send('Input.dispatchTouchEvent', {
1925
- type: params.type,
1926
- touchPoints: params.touchPoints.map((tp, i) => ({
1927
- x: tp.x,
1928
- y: tp.y,
1929
- id: tp.id ?? i,
1930
- })),
1931
- modifiers: params.modifiers ?? 0,
1932
- });
1933
- }
1934
- /**
1935
- * Check if video recording is currently active
1936
- */
1937
- isRecording() {
1938
- return this.recordingContext !== null;
1939
- }
1940
- /**
1941
- * Start recording to a video file using Playwright's native video recording.
1942
- * Creates a fresh browser context with video recording enabled.
1943
- * Automatically captures current URL and transfers cookies/storage if no URL provided.
1944
- *
1945
- * @param outputPath - Path to the output video file (will be .webm)
1946
- * @param url - Optional URL to navigate to (defaults to current page URL)
1947
- */
1948
- async startRecording(outputPath, url) {
1949
- if (this.recordingContext) {
1950
- throw new Error("Recording already in progress. Run 'record stop' first, or use 'record restart' to stop and start a new recording.");
1951
- }
1952
- if (!this.browser) {
1953
- throw new Error('Browser not launched. Call launch first.');
1954
- }
1955
- // Check if output file already exists
1956
- if (existsSync(outputPath)) {
1957
- throw new Error(`Output file already exists: ${outputPath}`);
1958
- }
1959
- // Validate output path is .webm (Playwright native format)
1960
- if (!outputPath.endsWith('.webm')) {
1961
- throw new Error('Playwright native recording only supports WebM format. Please use a .webm extension.');
1962
- }
1963
- // Auto-capture current URL if none provided
1964
- const currentPage = this.pages.length > 0 ? this.pages[this.activePageIndex] : null;
1965
- const currentContext = this.contexts.length > 0 ? this.contexts[0] : null;
1966
- if (!url && currentPage) {
1967
- const currentUrl = currentPage.url();
1968
- if (currentUrl && currentUrl !== 'about:blank') {
1969
- url = currentUrl;
1970
- }
1971
- }
1972
- // Capture state from current context (cookies + storage)
1973
- let storageState;
1974
- if (currentContext) {
1975
- try {
1976
- storageState = await currentContext.storageState();
1977
- }
1978
- catch {
1979
- // Ignore errors - context might be closed or invalid
1980
- }
1981
- }
1982
- // Create a temp directory for video recording
1983
- const session = process.env.AGENT_BROWSER_SESSION || 'default';
1984
- this.recordingTempDir = path.join(os.tmpdir(), `agent-browser-recording-${session}-${Date.now()}`);
1985
- mkdirSync(this.recordingTempDir, { recursive: true });
1986
- this.recordingOutputPath = outputPath;
1987
- // Reuse the active page viewport when available so recording matches the current layout.
1988
- const viewport = currentPage?.viewportSize() ?? { width: 1280, height: 720 };
1989
- this.recordingContext = await this.browser.newContext({
1990
- viewport,
1991
- recordVideo: {
1992
- dir: this.recordingTempDir,
1993
- size: viewport,
1994
- },
1995
- storageState,
1996
- });
1997
- this.recordingContext.setDefaultTimeout(10000);
1998
- // Create a page in the recording context
1999
- this.recordingPage = await this.recordingContext.newPage();
2000
- // Add the recording context and page to our managed lists
2001
- this.contexts.push(this.recordingContext);
2002
- this.pages.push(this.recordingPage);
2003
- this.activePageIndex = this.pages.length - 1;
2004
- // Set up page tracking
2005
- this.setupPageTracking(this.recordingPage);
2006
- // Invalidate CDP session since we switched pages
2007
- await this.invalidateCDPSession();
2008
- // Navigate to URL if provided or captured
2009
- if (url) {
2010
- await this.recordingPage.goto(url, { waitUntil: 'load' });
2011
- }
2012
- }
2013
- /**
2014
- * Stop recording and save the video file
2015
- * @returns Recording result with path
2016
- */
2017
- async stopRecording() {
2018
- if (!this.recordingContext || !this.recordingPage) {
2019
- return { path: '', frames: 0, error: 'No recording in progress' };
2020
- }
2021
- const outputPath = this.recordingOutputPath;
2022
- try {
2023
- // Get the video object before closing the page
2024
- const video = this.recordingPage.video();
2025
- // Remove recording page/context from our managed lists before closing
2026
- const pageIndex = this.pages.indexOf(this.recordingPage);
2027
- if (pageIndex !== -1) {
2028
- this.pages.splice(pageIndex, 1);
2029
- }
2030
- const contextIndex = this.contexts.indexOf(this.recordingContext);
2031
- if (contextIndex !== -1) {
2032
- this.contexts.splice(contextIndex, 1);
2033
- }
2034
- // Close the page to finalize the video
2035
- await this.recordingPage.close();
2036
- // Save the video to the desired output path
2037
- if (video) {
2038
- await video.saveAs(outputPath);
2039
- }
2040
- // Clean up temp directory
2041
- if (this.recordingTempDir) {
2042
- rmSync(this.recordingTempDir, { recursive: true, force: true });
2043
- }
2044
- // Close the recording context
2045
- await this.recordingContext.close();
2046
- // Reset recording state
2047
- this.recordingContext = null;
2048
- this.recordingPage = null;
2049
- this.recordingOutputPath = '';
2050
- this.recordingTempDir = '';
2051
- // Adjust active page index
2052
- if (this.pages.length > 0) {
2053
- this.activePageIndex = Math.min(this.activePageIndex, this.pages.length - 1);
2054
- }
2055
- else {
2056
- this.activePageIndex = 0;
2057
- }
2058
- // Invalidate CDP session since we may have switched pages
2059
- await this.invalidateCDPSession();
2060
- return { path: outputPath, frames: 0 }; // Playwright doesn't expose frame count
2061
- }
2062
- catch (error) {
2063
- // Clean up temp directory on error
2064
- if (this.recordingTempDir) {
2065
- rmSync(this.recordingTempDir, { recursive: true, force: true });
2066
- }
2067
- // Reset state on error
2068
- this.recordingContext = null;
2069
- this.recordingPage = null;
2070
- this.recordingOutputPath = '';
2071
- this.recordingTempDir = '';
2072
- const message = error instanceof Error ? error.message : String(error);
2073
- return { path: outputPath, frames: 0, error: message };
2074
- }
2075
- }
2076
- /**
2077
- * Restart recording - stops current recording (if any) and starts a new one.
2078
- * Convenience method that combines stopRecording and startRecording.
2079
- *
2080
- * @param outputPath - Path to the output video file (must be .webm)
2081
- * @param url - Optional URL to navigate to (defaults to current page URL)
2082
- * @returns Result from stopping the previous recording (if any)
2083
- */
2084
- async restartRecording(outputPath, url) {
2085
- let previousPath;
2086
- let stopped = false;
2087
- // Stop current recording if active
2088
- if (this.recordingContext) {
2089
- const result = await this.stopRecording();
2090
- previousPath = result.path;
2091
- stopped = true;
2092
- }
2093
- // Start new recording
2094
- await this.startRecording(outputPath, url);
2095
- return { previousPath, stopped };
2096
- }
2097
- /**
2098
- * Close the browser and clean up
2099
- */
2100
- async close() {
2101
- this.stopInspectServer();
2102
- // Stop recording if active (saves video)
2103
- if (this.recordingContext) {
2104
- await this.stopRecording();
2105
- }
2106
- // Stop screencast if active
2107
- if (this.screencastActive) {
2108
- await this.stopScreencast();
2109
- }
2110
- // Clean up profiling state if active (without saving)
2111
- if (this.profilingActive) {
2112
- const cdp = this.cdpSession;
2113
- if (cdp) {
2114
- if (this.profileDataHandler) {
2115
- cdp.off('Tracing.dataCollected', this.profileDataHandler);
2116
- }
2117
- if (this.profileCompleteHandler) {
2118
- cdp.off('Tracing.tracingComplete', this.profileCompleteHandler);
2119
- }
2120
- await cdp.send('Tracing.end').catch(() => { });
2121
- }
2122
- this.profilingActive = false;
2123
- this.profileChunks = [];
2124
- this.profileEventsDropped = false;
2125
- this.profileCompleteResolver = null;
2126
- this.profileDataHandler = null;
2127
- this.profileCompleteHandler = null;
2128
- }
2129
- // Clean up CDP session
2130
- if (this.cdpSession) {
2131
- await this.cdpSession.detach().catch(() => { });
2132
- this.cdpSession = null;
2133
- }
2134
- if (this.browserbaseSessionId && this.browserbaseApiKey) {
2135
- await this.closeBrowserbaseSession(this.browserbaseSessionId, this.browserbaseApiKey).catch((error) => {
2136
- console.error('Failed to close Browserbase session:', error);
2137
- });
2138
- this.browser = null;
2139
- }
2140
- else if (this.browserUseSessionId && this.browserUseApiKey) {
2141
- await this.closeBrowserUseSession(this.browserUseSessionId, this.browserUseApiKey).catch((error) => {
2142
- console.error('Failed to close Browser Use session:', error);
2143
- });
2144
- this.browser = null;
2145
- }
2146
- else if (this.kernelSessionId) {
2147
- await this.closeKernelSession(this.kernelSessionId, this.kernelApiKey ?? undefined).catch((error) => {
2148
- console.error('Failed to close Kernel session:', error);
2149
- });
2150
- this.browser = null;
2151
- }
2152
- else if (this.cdpEndpoint !== null) {
2153
- // CDP: only disconnect, don't close external app's pages
2154
- if (this.browser) {
2155
- await this.browser.close().catch(() => { });
2156
- this.browser = null;
2157
- }
2158
- }
2159
- else {
2160
- // Regular browser: close everything
2161
- for (const page of this.pages) {
2162
- await page.close().catch(() => { });
2163
- }
2164
- for (const context of this.contexts) {
2165
- await context.close().catch(() => { });
2166
- }
2167
- if (this.browser) {
2168
- await this.browser.close().catch(() => { });
2169
- this.browser = null;
2170
- }
2171
- }
2172
- this.pages = [];
2173
- this.contexts = [];
2174
- this.cdpEndpoint = null;
2175
- this.resolvedWsUrl = null;
2176
- this.browserbaseSessionId = null;
2177
- this.browserbaseApiKey = null;
2178
- this.browserUseSessionId = null;
2179
- this.browserUseApiKey = null;
2180
- this.kernelSessionId = null;
2181
- this.kernelApiKey = null;
2182
- this.isPersistentContext = false;
2183
- this.activePageIndex = 0;
2184
- this.colorScheme = null;
2185
- this.refMap = {};
2186
- this.lastSnapshot = '';
2187
- this.frameCallback = null;
2188
- }
2189
- }
2190
- //# sourceMappingURL=browser.js.map