@mastra/agent-browser 0.5.5-alpha.0 → 0.6.0-alpha.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.cjs +103 -17
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +7 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +7 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +103 -17
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/dist/index.cjs
CHANGED
|
@@ -3,6 +3,7 @@ let _mastra_core_browser = require("@mastra/core/browser");
|
|
|
3
3
|
let agent_browser = require("agent-browser");
|
|
4
4
|
let _mastra_core_tools = require("@mastra/core/tools");
|
|
5
5
|
let zod = require("zod");
|
|
6
|
+
let _mastra_core_request_context = require("@mastra/core/request-context");
|
|
6
7
|
// src/thread-manager.ts
|
|
7
8
|
/**
|
|
8
9
|
* AgentBrowserThreadManager - Thread scope management for AgentBrowser
|
|
@@ -21,11 +22,13 @@ var AgentBrowserThreadManager = class extends _mastra_core_browser.ThreadManager
|
|
|
21
22
|
browserConfig;
|
|
22
23
|
resolveCdpUrl;
|
|
23
24
|
onBrowserCreated;
|
|
25
|
+
onBrowserOpened;
|
|
24
26
|
constructor(config) {
|
|
25
27
|
super(config);
|
|
26
28
|
this.browserConfig = config.browserConfig;
|
|
27
29
|
this.resolveCdpUrl = config.resolveCdpUrl;
|
|
28
30
|
this.onBrowserCreated = config.onBrowserCreated;
|
|
31
|
+
this.onBrowserOpened = config.onBrowserOpened;
|
|
29
32
|
}
|
|
30
33
|
/**
|
|
31
34
|
* Get the page for a specific thread, creating session if needed.
|
|
@@ -65,6 +68,7 @@ var AgentBrowserThreadManager = class extends _mastra_core_browser.ThreadManager
|
|
|
65
68
|
session.manager = manager;
|
|
66
69
|
this.threadManagers.set(threadId, manager);
|
|
67
70
|
try {
|
|
71
|
+
await this.onBrowserOpened?.(threadId);
|
|
68
72
|
if (savedState && savedState.tabs.length > 0) {
|
|
69
73
|
this.logger?.debug?.(`Restoring browser state for thread ${threadId}: ${savedState.tabs.length} tabs`);
|
|
70
74
|
await this.restoreBrowserState(manager, savedState);
|
|
@@ -315,7 +319,10 @@ const dragInputSchema = zod.z.object({
|
|
|
315
319
|
/**
|
|
316
320
|
* browser_screenshot - Capture a screenshot of the current page
|
|
317
321
|
*/
|
|
318
|
-
const screenshotInputSchema = zod.z.object({
|
|
322
|
+
const screenshotInputSchema = zod.z.object({
|
|
323
|
+
fullPage: zod.z.boolean().optional().describe("Capture the full scrollable page instead of just the viewport (default: false)"),
|
|
324
|
+
path: zod.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.")
|
|
325
|
+
});
|
|
319
326
|
/**
|
|
320
327
|
* browser_evaluate - Execute JavaScript in the browser
|
|
321
328
|
*/
|
|
@@ -538,11 +545,40 @@ function createScreenshotTool(browser) {
|
|
|
538
545
|
id: BROWSER_TOOLS.SCREENSHOT,
|
|
539
546
|
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.",
|
|
540
547
|
inputSchema: screenshotInputSchema,
|
|
541
|
-
execute: async (input, { agent }) => {
|
|
548
|
+
execute: async (input, { agent, workspace, requestContext }) => {
|
|
542
549
|
const threadId = agent?.threadId;
|
|
543
550
|
browser.setCurrentThread(threadId);
|
|
544
551
|
await browser.ensureReady();
|
|
545
|
-
|
|
552
|
+
const result = await browser.screenshot(input, threadId);
|
|
553
|
+
if (!input.path || !("base64" in result)) return result;
|
|
554
|
+
try {
|
|
555
|
+
const filesystem = typeof workspace?.resolveFilesystem === "function" ? await workspace.resolveFilesystem({ requestContext: requestContext ?? new _mastra_core_request_context.RequestContext() }) : workspace?.filesystem;
|
|
556
|
+
if (!filesystem) return {
|
|
557
|
+
...result,
|
|
558
|
+
saveError: "There is no workspace filesystem to save the screenshot into."
|
|
559
|
+
};
|
|
560
|
+
const blocked = await writeBlocked(workspace, filesystem.readOnly, {
|
|
561
|
+
path: input.path,
|
|
562
|
+
overwrite: true
|
|
563
|
+
}, requestContext);
|
|
564
|
+
if (blocked) return {
|
|
565
|
+
...result,
|
|
566
|
+
saveError: blocked
|
|
567
|
+
};
|
|
568
|
+
await filesystem.writeFile(input.path, Buffer.from(result.base64, "base64"), {
|
|
569
|
+
recursive: true,
|
|
570
|
+
mimeType: "image/png"
|
|
571
|
+
});
|
|
572
|
+
return {
|
|
573
|
+
...result,
|
|
574
|
+
path: input.path
|
|
575
|
+
};
|
|
576
|
+
} catch (error) {
|
|
577
|
+
return {
|
|
578
|
+
...result,
|
|
579
|
+
saveError: error instanceof Error ? error.message : String(error)
|
|
580
|
+
};
|
|
581
|
+
}
|
|
546
582
|
},
|
|
547
583
|
toModelOutput(output) {
|
|
548
584
|
const result = output;
|
|
@@ -555,15 +591,52 @@ function createScreenshotTool(browser) {
|
|
|
555
591
|
};
|
|
556
592
|
return {
|
|
557
593
|
type: "content",
|
|
558
|
-
value: [
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
594
|
+
value: [
|
|
595
|
+
{
|
|
596
|
+
type: "media",
|
|
597
|
+
mediaType: "image/png",
|
|
598
|
+
data: result.base64
|
|
599
|
+
},
|
|
600
|
+
...result.path ? [{
|
|
601
|
+
type: "text",
|
|
602
|
+
text: `Saved to ${result.path}`
|
|
603
|
+
}] : [],
|
|
604
|
+
...result.saveError ? [{
|
|
605
|
+
type: "text",
|
|
606
|
+
text: `Couldn't save the screenshot: ${result.saveError}`
|
|
607
|
+
}] : []
|
|
608
|
+
]
|
|
563
609
|
};
|
|
564
610
|
}
|
|
565
611
|
});
|
|
566
612
|
}
|
|
613
|
+
const WRITE_FILE_TOOL = "mastra_workspace_write_file";
|
|
614
|
+
/**
|
|
615
|
+
* Saving a screenshot writes a file, so it follows the workspace's write-file settings: it's refused
|
|
616
|
+
* when writing is turned off, the filesystem is read-only, or writes need approval (a screenshot
|
|
617
|
+
* can't ask for it).
|
|
618
|
+
*/
|
|
619
|
+
async function writeBlocked(workspace, readOnly, args, requestContext) {
|
|
620
|
+
if (readOnly) return "The workspace filesystem is read-only.";
|
|
621
|
+
const config = workspace.getToolsConfig?.();
|
|
622
|
+
const perTool = config?.[WRITE_FILE_TOOL];
|
|
623
|
+
const context = {
|
|
624
|
+
args,
|
|
625
|
+
requestContext: requestContext ? Object.fromEntries(requestContext.entries()) : {},
|
|
626
|
+
workspace
|
|
627
|
+
};
|
|
628
|
+
const resolve = async (value, fallback, safeDefault) => {
|
|
629
|
+
if (value === void 0) return fallback;
|
|
630
|
+
if (typeof value === "boolean") return value;
|
|
631
|
+
try {
|
|
632
|
+
return await value(context);
|
|
633
|
+
} catch {
|
|
634
|
+
return safeDefault;
|
|
635
|
+
}
|
|
636
|
+
};
|
|
637
|
+
if (!await resolve(perTool?.enabled ?? config?.enabled, true, false)) return "Writing files is turned off in this workspace.";
|
|
638
|
+
if (await resolve(perTool?.requireApproval ?? config?.requireApproval, false, true)) return "Writing files needs approval in this workspace, so the screenshot was not saved.";
|
|
639
|
+
}
|
|
567
640
|
// src/tools/scroll.ts
|
|
568
641
|
/**
|
|
569
642
|
* browser_scroll - Scroll the page or element
|
|
@@ -750,7 +823,8 @@ var AgentBrowser = class extends _mastra_core_browser.MastraBrowser {
|
|
|
750
823
|
},
|
|
751
824
|
onBrowserCreated: (manager, threadId) => {
|
|
752
825
|
this.setupCloseListenerForThread(manager, threadId, this.isRemoteThreadBrowser());
|
|
753
|
-
}
|
|
826
|
+
},
|
|
827
|
+
onBrowserOpened: (threadId) => this.addPendingCookies(threadId)
|
|
754
828
|
};
|
|
755
829
|
const createTm = config.createThreadManager ?? ((opts) => new AgentBrowserThreadManager(opts));
|
|
756
830
|
this.threadManager = createTm(threadManagerConfig);
|
|
@@ -781,7 +855,15 @@ var AgentBrowser = class extends _mastra_core_browser.MastraBrowser {
|
|
|
781
855
|
const existingManager = this.threadManager.getExistingManagerForThread(effectiveThreadId);
|
|
782
856
|
if (existingManager) return existingManager;
|
|
783
857
|
}
|
|
784
|
-
|
|
858
|
+
const manager = await this.threadManager.getManagerForThread(effectiveThreadId);
|
|
859
|
+
await this.addPendingCookies(effectiveThreadId);
|
|
860
|
+
return manager;
|
|
861
|
+
}
|
|
862
|
+
async addCookiesToOpenBrowser(cookies, threadId) {
|
|
863
|
+
const context = this.threadManager.getExistingManagerForThread(threadId)?.getContext();
|
|
864
|
+
if (!context) return false;
|
|
865
|
+
await context.addCookies(cookies);
|
|
866
|
+
return true;
|
|
785
867
|
}
|
|
786
868
|
async doLaunch() {
|
|
787
869
|
this.pendingCloseReasons.clear();
|
|
@@ -921,8 +1003,13 @@ var AgentBrowser = class extends _mastra_core_browser.MastraBrowser {
|
|
|
921
1003
|
async getPage(explicitThreadId) {
|
|
922
1004
|
const scope = this.getScope();
|
|
923
1005
|
const threadId = explicitThreadId ?? this.getCurrentThread();
|
|
924
|
-
if (scope === "thread")
|
|
1006
|
+
if (scope === "thread") {
|
|
1007
|
+
const manager = await this.threadManager.getManagerForThread(threadId);
|
|
1008
|
+
await this.addPendingCookies(threadId);
|
|
1009
|
+
return manager.getPage();
|
|
1010
|
+
}
|
|
925
1011
|
if (!this.sharedManager) throw new Error("Browser not launched");
|
|
1012
|
+
await this.addPendingCookies(threadId);
|
|
926
1013
|
return this.sharedManager.getPage();
|
|
927
1014
|
}
|
|
928
1015
|
/**
|
|
@@ -1435,13 +1522,12 @@ var AgentBrowser = class extends _mastra_core_browser.MastraBrowser {
|
|
|
1435
1522
|
success: true,
|
|
1436
1523
|
hint: `Element is now ${state}. Take a snapshot to continue.`
|
|
1437
1524
|
};
|
|
1438
|
-
} else {
|
|
1439
|
-
await (await this.getPage(threadId)).waitForTimeout(timeout);
|
|
1440
|
-
return {
|
|
1441
|
-
success: true,
|
|
1442
|
-
hint: "Wait complete. Take a snapshot to see current state."
|
|
1443
|
-
};
|
|
1444
1525
|
}
|
|
1526
|
+
await (await this.getPage(threadId)).waitForTimeout(timeout);
|
|
1527
|
+
return {
|
|
1528
|
+
success: true,
|
|
1529
|
+
hint: "Wait complete. Take a snapshot to see current state."
|
|
1530
|
+
};
|
|
1445
1531
|
} catch (error) {
|
|
1446
1532
|
return this.createErrorFromException(error, "Wait");
|
|
1447
1533
|
}
|