@mastra/agent-browser 0.5.5 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -2,6 +2,7 @@ import { DEFAULT_THREAD_ID, MastraBrowser, ScreencastStreamImpl, ThreadManager,
2
2
  import { BrowserManager } from "agent-browser";
3
3
  import { createTool } from "@mastra/core/tools";
4
4
  import { z } from "zod";
5
+ import { RequestContext } from "@mastra/core/request-context";
5
6
  // src/thread-manager.ts
6
7
  /**
7
8
  * AgentBrowserThreadManager - Thread scope management for AgentBrowser
@@ -20,11 +21,13 @@ var AgentBrowserThreadManager = class extends ThreadManager {
20
21
  browserConfig;
21
22
  resolveCdpUrl;
22
23
  onBrowserCreated;
24
+ onBrowserOpened;
23
25
  constructor(config) {
24
26
  super(config);
25
27
  this.browserConfig = config.browserConfig;
26
28
  this.resolveCdpUrl = config.resolveCdpUrl;
27
29
  this.onBrowserCreated = config.onBrowserCreated;
30
+ this.onBrowserOpened = config.onBrowserOpened;
28
31
  }
29
32
  /**
30
33
  * Get the page for a specific thread, creating session if needed.
@@ -64,6 +67,7 @@ var AgentBrowserThreadManager = class extends ThreadManager {
64
67
  session.manager = manager;
65
68
  this.threadManagers.set(threadId, manager);
66
69
  try {
70
+ await this.onBrowserOpened?.(threadId);
67
71
  if (savedState && savedState.tabs.length > 0) {
68
72
  this.logger?.debug?.(`Restoring browser state for thread ${threadId}: ${savedState.tabs.length} tabs`);
69
73
  await this.restoreBrowserState(manager, savedState);
@@ -314,7 +318,10 @@ const dragInputSchema = z.object({
314
318
  /**
315
319
  * browser_screenshot - Capture a screenshot of the current page
316
320
  */
317
- const screenshotInputSchema = z.object({ fullPage: z.boolean().optional().describe("Capture the full scrollable page instead of just the viewport (default: false)") });
321
+ const screenshotInputSchema = z.object({
322
+ fullPage: z.boolean().optional().describe("Capture the full scrollable page instead of just the viewport (default: false)"),
323
+ path: z.string().min(1).optional().describe("Also save the PNG to this path in your workspace, so you can treat it as a normal file (attach, commit, view). Missing folders are created.")
324
+ });
318
325
  /**
319
326
  * browser_evaluate - Execute JavaScript in the browser
320
327
  */
@@ -537,11 +544,40 @@ function createScreenshotTool(browser) {
537
544
  id: BROWSER_TOOLS.SCREENSHOT,
538
545
  description: "Capture a screenshot of the current viewport as a visible PNG (set fullPage: true for full-page capture). Use snapshot when you only need text or interactive elements — screenshots are expensive. Use this when you need to visually inspect the page, e.g. evaluating images, product photos, layout, design, or colors.",
539
546
  inputSchema: screenshotInputSchema,
540
- execute: async (input, { agent }) => {
547
+ execute: async (input, { agent, workspace, requestContext }) => {
541
548
  const threadId = agent?.threadId;
542
549
  browser.setCurrentThread(threadId);
543
550
  await browser.ensureReady();
544
- return await browser.screenshot(input, threadId);
551
+ const result = await browser.screenshot(input, threadId);
552
+ if (!input.path || !("base64" in result)) return result;
553
+ try {
554
+ const filesystem = typeof workspace?.resolveFilesystem === "function" ? await workspace.resolveFilesystem({ requestContext: requestContext ?? new RequestContext() }) : workspace?.filesystem;
555
+ if (!filesystem) return {
556
+ ...result,
557
+ saveError: "There is no workspace filesystem to save the screenshot into."
558
+ };
559
+ const blocked = await writeBlocked(workspace, filesystem.readOnly, {
560
+ path: input.path,
561
+ overwrite: true
562
+ }, requestContext);
563
+ if (blocked) return {
564
+ ...result,
565
+ saveError: blocked
566
+ };
567
+ await filesystem.writeFile(input.path, Buffer.from(result.base64, "base64"), {
568
+ recursive: true,
569
+ mimeType: "image/png"
570
+ });
571
+ return {
572
+ ...result,
573
+ path: input.path
574
+ };
575
+ } catch (error) {
576
+ return {
577
+ ...result,
578
+ saveError: error instanceof Error ? error.message : String(error)
579
+ };
580
+ }
545
581
  },
546
582
  toModelOutput(output) {
547
583
  const result = output;
@@ -554,15 +590,52 @@ function createScreenshotTool(browser) {
554
590
  };
555
591
  return {
556
592
  type: "content",
557
- value: [{
558
- type: "media",
559
- mediaType: "image/png",
560
- data: result.base64
561
- }]
593
+ value: [
594
+ {
595
+ type: "media",
596
+ mediaType: "image/png",
597
+ data: result.base64
598
+ },
599
+ ...result.path ? [{
600
+ type: "text",
601
+ text: `Saved to ${result.path}`
602
+ }] : [],
603
+ ...result.saveError ? [{
604
+ type: "text",
605
+ text: `Couldn't save the screenshot: ${result.saveError}`
606
+ }] : []
607
+ ]
562
608
  };
563
609
  }
564
610
  });
565
611
  }
612
+ const WRITE_FILE_TOOL = "mastra_workspace_write_file";
613
+ /**
614
+ * Saving a screenshot writes a file, so it follows the workspace's write-file settings: it's refused
615
+ * when writing is turned off, the filesystem is read-only, or writes need approval (a screenshot
616
+ * can't ask for it).
617
+ */
618
+ async function writeBlocked(workspace, readOnly, args, requestContext) {
619
+ if (readOnly) return "The workspace filesystem is read-only.";
620
+ const config = workspace.getToolsConfig?.();
621
+ const perTool = config?.[WRITE_FILE_TOOL];
622
+ const context = {
623
+ args,
624
+ requestContext: requestContext ? Object.fromEntries(requestContext.entries()) : {},
625
+ workspace
626
+ };
627
+ const resolve = async (value, fallback, safeDefault) => {
628
+ if (value === void 0) return fallback;
629
+ if (typeof value === "boolean") return value;
630
+ try {
631
+ return await value(context);
632
+ } catch {
633
+ return safeDefault;
634
+ }
635
+ };
636
+ if (!await resolve(perTool?.enabled ?? config?.enabled, true, false)) return "Writing files is turned off in this workspace.";
637
+ if (await resolve(perTool?.requireApproval ?? config?.requireApproval, false, true)) return "Writing files needs approval in this workspace, so the screenshot was not saved.";
638
+ }
566
639
  // src/tools/scroll.ts
567
640
  /**
568
641
  * browser_scroll - Scroll the page or element
@@ -749,7 +822,8 @@ var AgentBrowser = class extends MastraBrowser {
749
822
  },
750
823
  onBrowserCreated: (manager, threadId) => {
751
824
  this.setupCloseListenerForThread(manager, threadId, this.isRemoteThreadBrowser());
752
- }
825
+ },
826
+ onBrowserOpened: (threadId) => this.addPendingCookies(threadId)
753
827
  };
754
828
  const createTm = config.createThreadManager ?? ((opts) => new AgentBrowserThreadManager(opts));
755
829
  this.threadManager = createTm(threadManagerConfig);
@@ -780,7 +854,15 @@ var AgentBrowser = class extends MastraBrowser {
780
854
  const existingManager = this.threadManager.getExistingManagerForThread(effectiveThreadId);
781
855
  if (existingManager) return existingManager;
782
856
  }
783
- return this.threadManager.getManagerForThread(effectiveThreadId);
857
+ const manager = await this.threadManager.getManagerForThread(effectiveThreadId);
858
+ await this.addPendingCookies(effectiveThreadId);
859
+ return manager;
860
+ }
861
+ async addCookiesToOpenBrowser(cookies, threadId) {
862
+ const context = this.threadManager.getExistingManagerForThread(threadId)?.getContext();
863
+ if (!context) return false;
864
+ await context.addCookies(cookies);
865
+ return true;
784
866
  }
785
867
  async doLaunch() {
786
868
  this.pendingCloseReasons.clear();
@@ -920,8 +1002,13 @@ var AgentBrowser = class extends MastraBrowser {
920
1002
  async getPage(explicitThreadId) {
921
1003
  const scope = this.getScope();
922
1004
  const threadId = explicitThreadId ?? this.getCurrentThread();
923
- if (scope === "thread") return this.threadManager.getPageForThread(threadId);
1005
+ if (scope === "thread") {
1006
+ const manager = await this.threadManager.getManagerForThread(threadId);
1007
+ await this.addPendingCookies(threadId);
1008
+ return manager.getPage();
1009
+ }
924
1010
  if (!this.sharedManager) throw new Error("Browser not launched");
1011
+ await this.addPendingCookies(threadId);
925
1012
  return this.sharedManager.getPage();
926
1013
  }
927
1014
  /**