@mastra/agent-browser 0.0.0-studio-deploy-20260404182525 → 0.0.0-studio-cli-20260504022012

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -283,11 +283,29 @@ declare const browserSchemas: {
283
283
  }, z.core.$strip>;
284
284
  };
285
285
 
286
+ /**
287
+ * AgentBrowser-specific configuration extensions.
288
+ */
289
+ interface AgentBrowserConfigExtensions {
290
+ /**
291
+ * Path to a Playwright storage state file (JSON) containing cookies and localStorage.
292
+ * This is a lighter-weight alternative to `profile` — it only persists
293
+ * authentication state, not the full browser profile.
294
+ *
295
+ * You can export storage state from a Playwright session and reuse it later.
296
+ *
297
+ * @example
298
+ * ```ts
299
+ * { storageState: './auth-state.json' }
300
+ * ```
301
+ */
302
+ storageState?: string;
303
+ }
286
304
  /**
287
305
  * Configuration options for AgentBrowser.
288
- * Type alias for BaseBrowserConfig.
306
+ * Extends the base BrowserConfig with agent-browser specific options.
289
307
  */
290
- type BrowserConfig = BrowserConfig$1;
308
+ type BrowserConfig = BrowserConfig$1 & AgentBrowserConfigExtensions;
291
309
 
292
310
  /**
293
311
  * AgentBrowserThreadManager - Thread scope management for AgentBrowser
@@ -365,6 +383,8 @@ declare class AgentBrowser extends MastraBrowser {
365
383
  /** Shared browser manager instance (for 'shared' scope) - narrowed type from base class */
366
384
  protected sharedManager: BrowserManager | null;
367
385
  private defaultTimeout;
386
+ /** Pending PID lookups — awaited in disconnect handlers to avoid racing. */
387
+ private pidLookups;
368
388
  /** Thread manager - narrowed type from base class */
369
389
  protected threadManager: AgentBrowserThreadManager;
370
390
  constructor(config?: BrowserConfig);
@@ -454,6 +474,14 @@ declare class AgentBrowser extends MastraBrowser {
454
474
  * Get the active tab index.
455
475
  */
456
476
  getActiveTabIndex(threadId?: string): Promise<number>;
477
+ /**
478
+ * Export the current browser session's storage state (cookies, localStorage) to a JSON file.
479
+ * This can later be loaded via the `storageState` config option to restore the session.
480
+ *
481
+ * @param path - File path to save the storage state JSON
482
+ * @param threadId - Optional thread ID (defaults to current thread)
483
+ */
484
+ exportStorageState(path: string, threadId?: string): Promise<void>;
457
485
  goto(input: GotoInput, threadId?: string): Promise<{
458
486
  success: true;
459
487
  url: string;
@@ -550,6 +578,17 @@ declare class AgentBrowser extends MastraBrowser {
550
578
  injectKeyboardEvent(event: KeyboardEventParams, threadId?: string): Promise<void>;
551
579
  }
552
580
 
581
+ /**
582
+ * Get the browser process PID from a BrowserManager instance via CDP.
583
+ *
584
+ * Playwright doesn't expose the browser process PID directly, so we use CDP's
585
+ * SystemInfo.getProcessInfo to get it. This works for both regular browser
586
+ * launches and persistent contexts (profiles).
587
+ *
588
+ * Returns undefined if the PID can't be retrieved (e.g., browser not running).
589
+ */
590
+ declare function getBrowserPid(manager: BrowserManager): Promise<number | undefined>;
591
+
553
592
  /**
554
593
  * Browser Tool Constants
555
594
  */
@@ -585,4 +624,4 @@ type BrowserToolName = (typeof BROWSER_TOOLS)[keyof typeof BROWSER_TOOLS];
585
624
  */
586
625
  declare function createAgentBrowserTools(browser: AgentBrowser): Record<string, Tool<any, any>>;
587
626
 
588
- export { AgentBrowser, BROWSER_TOOLS, type BackInput, type BrowserConfig, type BrowserToolName, type ClickInput, type CloseInput, type DialogInput, type DragInput, type EvaluateInput, type GotoInput, type HoverInput, type PressInput, type ScrollInput, type SelectInput, type SnapshotInput, type TabsInput, type TypeInput, type WaitInput, backInputSchema, browserSchemas, clickInputSchema, closeInputSchema, createAgentBrowserTools, dialogInputSchema, dragInputSchema, evaluateInputSchema, gotoInputSchema, hoverInputSchema, pressInputSchema, scrollInputSchema, selectInputSchema, snapshotInputSchema, tabsInputSchema, typeInputSchema, waitInputSchema };
627
+ export { AgentBrowser, BROWSER_TOOLS, type BackInput, type BrowserConfig, type BrowserToolName, type ClickInput, type CloseInput, type DialogInput, type DragInput, type EvaluateInput, type GotoInput, type HoverInput, type PressInput, type ScrollInput, type SelectInput, type SnapshotInput, type TabsInput, type TypeInput, type WaitInput, backInputSchema, browserSchemas, clickInputSchema, closeInputSchema, createAgentBrowserTools, dialogInputSchema, dragInputSchema, evaluateInputSchema, getBrowserPid, gotoInputSchema, hoverInputSchema, pressInputSchema, scrollInputSchema, selectInputSchema, snapshotInputSchema, tabsInputSchema, typeInputSchema, waitInputSchema };
package/dist/index.d.ts CHANGED
@@ -283,11 +283,29 @@ declare const browserSchemas: {
283
283
  }, z.core.$strip>;
284
284
  };
285
285
 
286
+ /**
287
+ * AgentBrowser-specific configuration extensions.
288
+ */
289
+ interface AgentBrowserConfigExtensions {
290
+ /**
291
+ * Path to a Playwright storage state file (JSON) containing cookies and localStorage.
292
+ * This is a lighter-weight alternative to `profile` — it only persists
293
+ * authentication state, not the full browser profile.
294
+ *
295
+ * You can export storage state from a Playwright session and reuse it later.
296
+ *
297
+ * @example
298
+ * ```ts
299
+ * { storageState: './auth-state.json' }
300
+ * ```
301
+ */
302
+ storageState?: string;
303
+ }
286
304
  /**
287
305
  * Configuration options for AgentBrowser.
288
- * Type alias for BaseBrowserConfig.
306
+ * Extends the base BrowserConfig with agent-browser specific options.
289
307
  */
290
- type BrowserConfig = BrowserConfig$1;
308
+ type BrowserConfig = BrowserConfig$1 & AgentBrowserConfigExtensions;
291
309
 
292
310
  /**
293
311
  * AgentBrowserThreadManager - Thread scope management for AgentBrowser
@@ -365,6 +383,8 @@ declare class AgentBrowser extends MastraBrowser {
365
383
  /** Shared browser manager instance (for 'shared' scope) - narrowed type from base class */
366
384
  protected sharedManager: BrowserManager | null;
367
385
  private defaultTimeout;
386
+ /** Pending PID lookups — awaited in disconnect handlers to avoid racing. */
387
+ private pidLookups;
368
388
  /** Thread manager - narrowed type from base class */
369
389
  protected threadManager: AgentBrowserThreadManager;
370
390
  constructor(config?: BrowserConfig);
@@ -454,6 +474,14 @@ declare class AgentBrowser extends MastraBrowser {
454
474
  * Get the active tab index.
455
475
  */
456
476
  getActiveTabIndex(threadId?: string): Promise<number>;
477
+ /**
478
+ * Export the current browser session's storage state (cookies, localStorage) to a JSON file.
479
+ * This can later be loaded via the `storageState` config option to restore the session.
480
+ *
481
+ * @param path - File path to save the storage state JSON
482
+ * @param threadId - Optional thread ID (defaults to current thread)
483
+ */
484
+ exportStorageState(path: string, threadId?: string): Promise<void>;
457
485
  goto(input: GotoInput, threadId?: string): Promise<{
458
486
  success: true;
459
487
  url: string;
@@ -550,6 +578,17 @@ declare class AgentBrowser extends MastraBrowser {
550
578
  injectKeyboardEvent(event: KeyboardEventParams, threadId?: string): Promise<void>;
551
579
  }
552
580
 
581
+ /**
582
+ * Get the browser process PID from a BrowserManager instance via CDP.
583
+ *
584
+ * Playwright doesn't expose the browser process PID directly, so we use CDP's
585
+ * SystemInfo.getProcessInfo to get it. This works for both regular browser
586
+ * launches and persistent contexts (profiles).
587
+ *
588
+ * Returns undefined if the PID can't be retrieved (e.g., browser not running).
589
+ */
590
+ declare function getBrowserPid(manager: BrowserManager): Promise<number | undefined>;
591
+
553
592
  /**
554
593
  * Browser Tool Constants
555
594
  */
@@ -585,4 +624,4 @@ type BrowserToolName = (typeof BROWSER_TOOLS)[keyof typeof BROWSER_TOOLS];
585
624
  */
586
625
  declare function createAgentBrowserTools(browser: AgentBrowser): Record<string, Tool<any, any>>;
587
626
 
588
- export { AgentBrowser, BROWSER_TOOLS, type BackInput, type BrowserConfig, type BrowserToolName, type ClickInput, type CloseInput, type DialogInput, type DragInput, type EvaluateInput, type GotoInput, type HoverInput, type PressInput, type ScrollInput, type SelectInput, type SnapshotInput, type TabsInput, type TypeInput, type WaitInput, backInputSchema, browserSchemas, clickInputSchema, closeInputSchema, createAgentBrowserTools, dialogInputSchema, dragInputSchema, evaluateInputSchema, gotoInputSchema, hoverInputSchema, pressInputSchema, scrollInputSchema, selectInputSchema, snapshotInputSchema, tabsInputSchema, typeInputSchema, waitInputSchema };
627
+ export { AgentBrowser, BROWSER_TOOLS, type BackInput, type BrowserConfig, type BrowserToolName, type ClickInput, type CloseInput, type DialogInput, type DragInput, type EvaluateInput, type GotoInput, type HoverInput, type PressInput, type ScrollInput, type SelectInput, type SnapshotInput, type TabsInput, type TypeInput, type WaitInput, backInputSchema, browserSchemas, clickInputSchema, closeInputSchema, createAgentBrowserTools, dialogInputSchema, dragInputSchema, evaluateInputSchema, getBrowserPid, gotoInputSchema, hoverInputSchema, pressInputSchema, scrollInputSchema, selectInputSchema, snapshotInputSchema, tabsInputSchema, typeInputSchema, waitInputSchema };
package/dist/index.js CHANGED
@@ -34,8 +34,11 @@ var AgentBrowserThreadManager = class extends ThreadManager {
34
34
  if (this.scope === "thread") {
35
35
  const manager = new BrowserManager();
36
36
  const launchOptions = {
37
- headless: this.browserConfig.headless ?? true,
38
- viewport: this.browserConfig.viewport
37
+ headless: this.browserConfig.headless,
38
+ viewport: this.browserConfig.viewport,
39
+ profile: this.browserConfig.profile,
40
+ executablePath: this.browserConfig.executablePath,
41
+ storageState: this.browserConfig.storageState
39
42
  };
40
43
  if (this.browserConfig.cdpUrl && this.resolveCdpUrl) {
41
44
  launchOptions.cdpUrl = await this.resolveCdpUrl(this.browserConfig.cdpUrl);
@@ -216,7 +219,9 @@ var dragInputSchema = z.object({
216
219
  }
217
220
  });
218
221
  var evaluateInputSchema = z.object({
219
- script: z.string().describe("JavaScript code to execute"),
222
+ script: z.string().describe(
223
+ "JavaScript expression to evaluate in the browser and return the result. Do not use `return` \u2014 write a bare expression like `document.title` or `1 + 1`. For async code, wrap in an async IIFE: `(async () => { ... })()`."
224
+ ),
220
225
  arg: z.unknown().optional().describe("Argument to pass to the script (JSON-serializable)")
221
226
  });
222
227
  var browserSchemas = {
@@ -489,12 +494,36 @@ function createAgentBrowserTools(browser) {
489
494
  };
490
495
  }
491
496
 
497
+ // src/utils.ts
498
+ async function getBrowserPid(manager) {
499
+ try {
500
+ let browser = manager.getBrowser();
501
+ if (!browser) {
502
+ const ctx = manager.getContext();
503
+ browser = ctx?.browser?.() ?? null;
504
+ }
505
+ if (!browser) return void 0;
506
+ const cdp = await browser.newBrowserCDPSession();
507
+ try {
508
+ const info = await cdp.send("SystemInfo.getProcessInfo");
509
+ const browserProcess = info.processInfo?.find((p) => p.type === "browser");
510
+ return browserProcess?.id;
511
+ } finally {
512
+ await cdp.detach().catch(() => void 0);
513
+ }
514
+ } catch {
515
+ return void 0;
516
+ }
517
+ }
518
+
492
519
  // src/agent-browser.ts
493
520
  var AgentBrowser = class extends MastraBrowser {
494
521
  id;
495
522
  name = "AgentBrowser";
496
523
  provider = "vercel-labs/agent-browser";
497
524
  defaultTimeout = 3e4;
525
+ /** Pending PID lookups — awaited in disconnect handlers to avoid racing. */
526
+ pidLookups = /* @__PURE__ */ new Set();
498
527
  constructor(config = {}) {
499
528
  super(config);
500
529
  this.id = `agent-browser-${Date.now()}`;
@@ -504,7 +533,7 @@ var AgentBrowser = class extends MastraBrowser {
504
533
  const effectiveScope = config.cdpUrl ? config.scope ?? "shared" : config.scope ?? "thread";
505
534
  this.threadManager = new AgentBrowserThreadManager({
506
535
  scope: effectiveScope,
507
- browserConfig: config,
536
+ browserConfig: { ...config, headless: this.headless },
508
537
  resolveCdpUrl: this.resolveCdpUrl.bind(this),
509
538
  logger: this.logger,
510
539
  // When a new thread session is created, notify listeners so screencast can start
@@ -532,11 +561,11 @@ var AgentBrowser = class extends MastraBrowser {
532
561
  const scope = this.threadManager.getScope();
533
562
  const threadId = this.getCurrentThread();
534
563
  const existingSession = this.threadManager.hasSession(threadId);
535
- if (scope === "thread" && threadId !== DEFAULT_THREAD_ID && !existingSession) {
564
+ if (scope === "thread" && !existingSession) {
536
565
  await this.getManagerForThread(threadId);
537
566
  }
538
567
  await super.ensureReady();
539
- if (scope === "thread" && threadId !== DEFAULT_THREAD_ID && existingSession) {
568
+ if (scope === "thread" && existingSession) {
540
569
  await this.getManagerForThread(threadId);
541
570
  }
542
571
  }
@@ -568,8 +597,11 @@ var AgentBrowser = class extends MastraBrowser {
568
597
  this.sharedManager = new BrowserManager();
569
598
  const localConfig = this.config;
570
599
  const launchOptions = {
571
- headless: localConfig.headless ?? true,
572
- viewport: localConfig.viewport
600
+ headless: this.headless,
601
+ viewport: localConfig.viewport,
602
+ profile: localConfig.profile,
603
+ executablePath: localConfig.executablePath,
604
+ storageState: localConfig.storageState
573
605
  };
574
606
  if (localConfig.cdpUrl) {
575
607
  launchOptions.cdpUrl = await this.resolveCdpUrl(localConfig.cdpUrl);
@@ -584,11 +616,15 @@ var AgentBrowser = class extends MastraBrowser {
584
616
  */
585
617
  setupCloseListenerForSharedScope(manager) {
586
618
  try {
619
+ const pidLookup = getBrowserPid(manager).then((pid) => {
620
+ if (pid && this.sharedManager === manager) this.sharedBrowserPid = pid;
621
+ }).finally(() => this.pidLookups.delete(pidLookup));
622
+ this.pidLookups.add(pidLookup);
587
623
  let disconnectHandled = false;
588
624
  const handleDisconnect = () => {
589
625
  if (disconnectHandled) return;
590
626
  disconnectHandled = true;
591
- this.handleBrowserDisconnected();
627
+ void pidLookup.catch(() => void 0).then(() => this.handleBrowserDisconnected());
592
628
  };
593
629
  const context = manager.getContext();
594
630
  if (context) {
@@ -607,6 +643,8 @@ var AgentBrowser = class extends MastraBrowser {
607
643
  }
608
644
  }
609
645
  async doClose() {
646
+ await Promise.allSettled([...this.pidLookups]);
647
+ this.pidLookups.clear();
610
648
  await this.threadManager.destroyAllSessions();
611
649
  this.setCurrentThread(void 0);
612
650
  const scope = this.threadManager.getScope();
@@ -690,11 +728,17 @@ var AgentBrowser = class extends MastraBrowser {
690
728
  */
691
729
  setupCloseListenerForThread(manager, threadId) {
692
730
  try {
731
+ const pidLookup = getBrowserPid(manager).then((pid) => {
732
+ if (pid && this.threadManager?.getExistingManagerForThread(threadId) === manager) {
733
+ this.threadBrowserPids.set(threadId, pid);
734
+ }
735
+ }).finally(() => this.pidLookups.delete(pidLookup));
736
+ this.pidLookups.add(pidLookup);
693
737
  let disconnectHandled = false;
694
738
  const handleDisconnect = () => {
695
739
  if (disconnectHandled) return;
696
740
  disconnectHandled = true;
697
- this.handleThreadBrowserDisconnected(threadId);
741
+ void pidLookup.catch(() => void 0).then(() => this.handleThreadBrowserDisconnected(threadId));
698
742
  };
699
743
  const context = manager.getContext();
700
744
  if (context) {
@@ -884,6 +928,25 @@ var AgentBrowser = class extends MastraBrowser {
884
928
  return 0;
885
929
  }
886
930
  }
931
+ /**
932
+ * Export the current browser session's storage state (cookies, localStorage) to a JSON file.
933
+ * This can later be loaded via the `storageState` config option to restore the session.
934
+ *
935
+ * @param path - File path to save the storage state JSON
936
+ * @param threadId - Optional thread ID (defaults to current thread)
937
+ */
938
+ async exportStorageState(path, threadId) {
939
+ const effectiveThreadId = threadId ?? this.getCurrentThread();
940
+ const manager = this.threadManager.getExistingManagerForThread(effectiveThreadId);
941
+ if (!manager) {
942
+ throw new Error("No browser is running. Launch a browser first before exporting storage state.");
943
+ }
944
+ const context = manager.getContext();
945
+ if (!context) {
946
+ throw new Error("Browser context not available");
947
+ }
948
+ await context.storageState({ path });
949
+ }
887
950
  // ---------------------------------------------------------------------------
888
951
  // 1. browser_goto - Navigate to URL
889
952
  // ---------------------------------------------------------------------------
@@ -1400,8 +1463,7 @@ var AgentBrowser = class extends MastraBrowser {
1400
1463
  async evaluate(input, threadId) {
1401
1464
  try {
1402
1465
  const page = await this.getPage(threadId);
1403
- const wrappedScript = `(async () => { ${input.script} })()`;
1404
- const result = await page.evaluate(wrappedScript);
1466
+ const result = await page.evaluate(input.script);
1405
1467
  return {
1406
1468
  success: true,
1407
1469
  result,
@@ -1552,6 +1614,6 @@ var AgentBrowser = class extends MastraBrowser {
1552
1614
  }
1553
1615
  };
1554
1616
 
1555
- export { AgentBrowser, BROWSER_TOOLS, backInputSchema, browserSchemas, clickInputSchema, closeInputSchema, createAgentBrowserTools, dialogInputSchema, dragInputSchema, evaluateInputSchema, gotoInputSchema, hoverInputSchema, pressInputSchema, scrollInputSchema, selectInputSchema, snapshotInputSchema, tabsInputSchema, typeInputSchema, waitInputSchema };
1617
+ export { AgentBrowser, BROWSER_TOOLS, backInputSchema, browserSchemas, clickInputSchema, closeInputSchema, createAgentBrowserTools, dialogInputSchema, dragInputSchema, evaluateInputSchema, getBrowserPid, gotoInputSchema, hoverInputSchema, pressInputSchema, scrollInputSchema, selectInputSchema, snapshotInputSchema, tabsInputSchema, typeInputSchema, waitInputSchema };
1556
1618
  //# sourceMappingURL=index.js.map
1557
1619
  //# sourceMappingURL=index.js.map