@mastra/browser-viewer 0.0.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/README.md +101 -0
- package/dist/index.cjs +796 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +335 -0
- package/dist/index.d.ts +335 -0
- package/dist/index.js +768 -0
- package/dist/index.js.map +1 -0
- package/package.json +65 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
import { BrowserConfigBase, ThreadManager, ThreadManagerConfig, ThreadSession, MastraBrowser, BrowserState, ScreencastOptions, ScreencastStream, MouseEventParams, KeyboardEventParams } from '@mastra/core/browser';
|
|
2
|
+
import { Tool } from '@mastra/core/tools';
|
|
3
|
+
import { Browser, Page, CDPSession, BrowserContext, BrowserServer } from 'playwright-core';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Types for @mastra/browser-viewer
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Supported CLI providers that can be used with BrowserViewer.
|
|
11
|
+
*/
|
|
12
|
+
type CLIProvider = 'agent-browser' | 'browser-use' | 'browse-cli';
|
|
13
|
+
/**
|
|
14
|
+
* Configuration for BrowserViewer.
|
|
15
|
+
*/
|
|
16
|
+
interface BrowserViewerConfig extends BrowserConfigBase {
|
|
17
|
+
/**
|
|
18
|
+
* Which CLI the agent will use for browser automation.
|
|
19
|
+
* The CLI connects to Mastra's Chrome via the CDP URL.
|
|
20
|
+
*/
|
|
21
|
+
cli: CLIProvider;
|
|
22
|
+
/**
|
|
23
|
+
* Port for Chrome's remote debugging protocol.
|
|
24
|
+
* Only used when launching Chrome (not when connecting via cdpUrl).
|
|
25
|
+
*
|
|
26
|
+
* @default 0 (auto-assign available port)
|
|
27
|
+
*/
|
|
28
|
+
cdpPort?: number;
|
|
29
|
+
/**
|
|
30
|
+
* Path to Chrome user data directory (profile).
|
|
31
|
+
* Persists cookies, localStorage, extensions, etc.
|
|
32
|
+
*/
|
|
33
|
+
userDataDir?: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* BrowserViewerThreadManager - Thread scope management for BrowserViewer
|
|
38
|
+
*
|
|
39
|
+
* Manages thread-scoped browser sessions using Playwright to launch
|
|
40
|
+
* separate Chrome instances per thread.
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Extended session info for BrowserViewer.
|
|
45
|
+
*/
|
|
46
|
+
interface BrowserViewerSession extends ThreadSession {
|
|
47
|
+
/**
|
|
48
|
+
* Playwright browser server (owns the Chrome process).
|
|
49
|
+
* Null for external CDP connections where we don't own the browser process.
|
|
50
|
+
*/
|
|
51
|
+
browserServer: BrowserServer | null;
|
|
52
|
+
/** Playwright browser instance (connected to server) */
|
|
53
|
+
browser: Browser;
|
|
54
|
+
/** Browser context */
|
|
55
|
+
context: BrowserContext;
|
|
56
|
+
/** CDP session for the active page */
|
|
57
|
+
cdpSession: CDPSession | null;
|
|
58
|
+
/** CDP WebSocket URL (null if discovery failed) */
|
|
59
|
+
cdpUrl: string | null;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Configuration for BrowserViewerThreadManager.
|
|
63
|
+
*/
|
|
64
|
+
interface BrowserViewerThreadManagerConfig extends ThreadManagerConfig {
|
|
65
|
+
/** Browser configuration */
|
|
66
|
+
browserConfig: BrowserViewerConfig;
|
|
67
|
+
/** Callback when a browser is created for a thread */
|
|
68
|
+
onBrowserCreated?: (browser: Browser, threadId: string, cdpUrl: string | null) => void;
|
|
69
|
+
/** Callback when a browser is closed for a thread */
|
|
70
|
+
onBrowserClosed?: (threadId: string) => void;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Thread manager implementation for BrowserViewer.
|
|
74
|
+
*
|
|
75
|
+
* Supports two scope modes:
|
|
76
|
+
* - 'shared': All threads share one Chrome instance
|
|
77
|
+
* - 'thread': Each thread gets a dedicated Chrome instance
|
|
78
|
+
*/
|
|
79
|
+
declare class BrowserViewerThreadManager extends ThreadManager<Browser> {
|
|
80
|
+
private readonly browserConfig;
|
|
81
|
+
private readonly onBrowserCreated?;
|
|
82
|
+
private readonly onBrowserClosed?;
|
|
83
|
+
/** Map of thread ID to session info (for 'thread' scope) */
|
|
84
|
+
private readonly threadSessions;
|
|
85
|
+
/** Shared session info (for 'shared' scope) */
|
|
86
|
+
private sharedSession;
|
|
87
|
+
/** Cached CDP sessions for input injection, keyed by threadId */
|
|
88
|
+
private inputCdpSessions;
|
|
89
|
+
constructor(config: BrowserViewerThreadManagerConfig);
|
|
90
|
+
/**
|
|
91
|
+
* Check if a thread should use the shared session slot.
|
|
92
|
+
* In shared scope, all threads use the shared session.
|
|
93
|
+
* In thread scope, DEFAULT_THREAD_ID also uses the shared session.
|
|
94
|
+
*/
|
|
95
|
+
private usesSharedSlot;
|
|
96
|
+
/**
|
|
97
|
+
* Get the viewer session for a thread, using consistent routing.
|
|
98
|
+
* Handles both shared and thread-scoped sessions.
|
|
99
|
+
*/
|
|
100
|
+
private getViewerSession;
|
|
101
|
+
/**
|
|
102
|
+
* Store a session in the appropriate slot based on scope.
|
|
103
|
+
* Consolidates session storage logic used by createSession, createSharedSession,
|
|
104
|
+
* createSharedSessionFromCdp, and connectToExternalCdp.
|
|
105
|
+
*/
|
|
106
|
+
private storeSession;
|
|
107
|
+
/**
|
|
108
|
+
* Clear a session from the appropriate slot based on scope.
|
|
109
|
+
* Must be called BEFORE async cleanup operations to prevent double callbacks
|
|
110
|
+
* from disconnect handlers.
|
|
111
|
+
*/
|
|
112
|
+
private clearSessionState;
|
|
113
|
+
/**
|
|
114
|
+
* Clean up a session's resources (CDP session, browser, server).
|
|
115
|
+
* Consolidates cleanup logic used by closeThreadBrowser, closeSharedBrowser,
|
|
116
|
+
* and doDestroySession.
|
|
117
|
+
*
|
|
118
|
+
* @param session - The session to clean up
|
|
119
|
+
* @param threadId - The thread ID (for onBrowserClosed callback)
|
|
120
|
+
*/
|
|
121
|
+
private cleanupSession;
|
|
122
|
+
/**
|
|
123
|
+
* Launch a new browser instance and return the components.
|
|
124
|
+
* Consolidates the launch logic shared by createSession and createSharedSession.
|
|
125
|
+
*
|
|
126
|
+
* @param threadId - Thread ID for logging and disconnect handler
|
|
127
|
+
*/
|
|
128
|
+
private launchBrowser;
|
|
129
|
+
/**
|
|
130
|
+
* Get CDP URL for a specific thread.
|
|
131
|
+
*/
|
|
132
|
+
getCdpUrlForThread(threadId?: string): string | null;
|
|
133
|
+
/**
|
|
134
|
+
* Get the active page for a thread.
|
|
135
|
+
*/
|
|
136
|
+
getActivePageForThread(threadId?: string): Promise<Page | null>;
|
|
137
|
+
/**
|
|
138
|
+
* Resolve the active page from a browser context.
|
|
139
|
+
* Uses last page (most recently opened) with fallback to first page.
|
|
140
|
+
*/
|
|
141
|
+
private resolveActivePage;
|
|
142
|
+
/**
|
|
143
|
+
* Get or create a CDP session for the active page in a thread.
|
|
144
|
+
*
|
|
145
|
+
* CDP sessions are page-scoped, so we create a fresh one for the currently active page
|
|
146
|
+
* rather than caching one that may point to a closed or inactive page.
|
|
147
|
+
*/
|
|
148
|
+
getCdpSessionForThread(threadId?: string): Promise<CDPSession | null>;
|
|
149
|
+
/**
|
|
150
|
+
* Get the browser context for a thread.
|
|
151
|
+
*/
|
|
152
|
+
getContextForThread(threadId?: string): BrowserContext | null;
|
|
153
|
+
/**
|
|
154
|
+
* Create a fresh CDP session for the active page (not cached).
|
|
155
|
+
* Used by screencast which needs fresh sessions on tab switches.
|
|
156
|
+
*/
|
|
157
|
+
createFreshCdpSession(threadId?: string): Promise<CDPSession | null>;
|
|
158
|
+
/**
|
|
159
|
+
* Create a new session for a thread.
|
|
160
|
+
*/
|
|
161
|
+
protected createSession(threadId: string): Promise<BrowserViewerSession>;
|
|
162
|
+
/**
|
|
163
|
+
* Discover the actual CDP WebSocket URL from Chrome's DevToolsActivePort file.
|
|
164
|
+
*
|
|
165
|
+
* Playwright's BrowserServer exposes _userDataDirForTest which points to Chrome's
|
|
166
|
+
* user data directory. Chrome writes a DevToolsActivePort file there containing:
|
|
167
|
+
* Line 1: The debugging port number
|
|
168
|
+
* Line 2: The browser WebSocket path (e.g., /devtools/browser/<guid>)
|
|
169
|
+
*
|
|
170
|
+
* This gives us the real CDP URL that external tools like agent-browser can connect to.
|
|
171
|
+
* Returns null if discovery fails - callers should handle this case.
|
|
172
|
+
*/
|
|
173
|
+
private discoverCdpUrl;
|
|
174
|
+
/**
|
|
175
|
+
* Create a shared session by connecting to an existing browser via CDP URL.
|
|
176
|
+
* Used when BrowserViewer is configured with a cdpUrl to connect to an external browser.
|
|
177
|
+
*/
|
|
178
|
+
createSharedSessionFromCdp(cdpUrl: string): Promise<void>;
|
|
179
|
+
/**
|
|
180
|
+
* Create a shared session (for 'shared' scope).
|
|
181
|
+
*/
|
|
182
|
+
createSharedSession(): Promise<void>;
|
|
183
|
+
/**
|
|
184
|
+
* Handle browser disconnection for a thread.
|
|
185
|
+
*/
|
|
186
|
+
private handleBrowserDisconnected;
|
|
187
|
+
/**
|
|
188
|
+
* Connect to an external browser via CDP URL for screencast.
|
|
189
|
+
*
|
|
190
|
+
* This is used when an agent is using their own external CDP (e.g., browser-use cloud).
|
|
191
|
+
* We connect Playwright to the external browser to enable screencast without launching
|
|
192
|
+
* our own browser.
|
|
193
|
+
*
|
|
194
|
+
* @param cdpUrl - The external CDP WebSocket URL (wss://... or ws://...)
|
|
195
|
+
* @param threadId - Thread ID to associate the session with
|
|
196
|
+
*/
|
|
197
|
+
connectToExternalCdp(cdpUrl: string, threadId: string): Promise<BrowserViewerSession>;
|
|
198
|
+
/**
|
|
199
|
+
* Connect to a browser via CDP URL and create a session.
|
|
200
|
+
* Shared implementation for createSharedSessionFromCdp and connectToExternalCdp.
|
|
201
|
+
*/
|
|
202
|
+
private connectToCdp;
|
|
203
|
+
/**
|
|
204
|
+
* Close a specific thread's browser.
|
|
205
|
+
*/
|
|
206
|
+
closeThreadBrowser(threadId: string): Promise<void>;
|
|
207
|
+
/**
|
|
208
|
+
* Close the shared browser.
|
|
209
|
+
*/
|
|
210
|
+
closeSharedBrowser(): Promise<void>;
|
|
211
|
+
/**
|
|
212
|
+
* Close all browsers.
|
|
213
|
+
*/
|
|
214
|
+
closeAll(): Promise<void>;
|
|
215
|
+
/**
|
|
216
|
+
* Get the manager for a session.
|
|
217
|
+
* Required by base class.
|
|
218
|
+
*/
|
|
219
|
+
protected getManagerForSession(session: ThreadSession): Browser;
|
|
220
|
+
/**
|
|
221
|
+
* Get the shared manager.
|
|
222
|
+
* Required by base class.
|
|
223
|
+
*/
|
|
224
|
+
protected getSharedManager(): Browser;
|
|
225
|
+
/**
|
|
226
|
+
* Destroy a session and clean up resources.
|
|
227
|
+
* Required by base class.
|
|
228
|
+
*/
|
|
229
|
+
protected doDestroySession(session: ThreadSession): Promise<void>;
|
|
230
|
+
/**
|
|
231
|
+
* Check if browser is running for a thread.
|
|
232
|
+
*/
|
|
233
|
+
isBrowserRunning(threadId?: string): boolean;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* BrowserViewer - Playwright-managed Chrome for CLI providers
|
|
238
|
+
*
|
|
239
|
+
* Launches Chrome via Playwright and exposes the CDP URL for CLI tools
|
|
240
|
+
* (agent-browser, browser-use, browse-cli) to connect as secondary clients.
|
|
241
|
+
*
|
|
242
|
+
* This gives us:
|
|
243
|
+
* - Direct page-level CDP sessions (fixes screencast sessionId issues)
|
|
244
|
+
* - Full browser lifecycle control
|
|
245
|
+
* - Predictable CDP URL for CLI injection
|
|
246
|
+
* - Thread-scoped browser isolation
|
|
247
|
+
*/
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* BrowserViewer - CLI provider with Playwright-managed Chrome
|
|
251
|
+
*
|
|
252
|
+
* Use this with Workspace to enable browser automation via CLI tools.
|
|
253
|
+
* The agent uses skills + workspace_execute_command to drive the CLI,
|
|
254
|
+
* while Mastra handles screencast, input injection, and lifecycle.
|
|
255
|
+
*
|
|
256
|
+
* @example
|
|
257
|
+
* ```ts
|
|
258
|
+
* import { Workspace } from '@mastra/core';
|
|
259
|
+
* import { BrowserViewer } from '@mastra/browser-viewer';
|
|
260
|
+
*
|
|
261
|
+
* const workspace = new Workspace({
|
|
262
|
+
* browser: new BrowserViewer({
|
|
263
|
+
* cli: 'agent-browser',
|
|
264
|
+
* headless: false,
|
|
265
|
+
* }),
|
|
266
|
+
* });
|
|
267
|
+
* ```
|
|
268
|
+
*/
|
|
269
|
+
declare class BrowserViewer extends MastraBrowser {
|
|
270
|
+
readonly id: string;
|
|
271
|
+
readonly name = "BrowserViewer";
|
|
272
|
+
readonly provider = "browser-viewer";
|
|
273
|
+
readonly providerType: "cli";
|
|
274
|
+
/** Which CLI the agent uses */
|
|
275
|
+
readonly cli: CLIProvider;
|
|
276
|
+
/** Viewer-specific config (stored for reference) */
|
|
277
|
+
readonly viewerConfig: BrowserViewerConfig;
|
|
278
|
+
/** Thread manager for browser sessions */
|
|
279
|
+
protected threadManager: BrowserViewerThreadManager;
|
|
280
|
+
constructor(config: BrowserViewerConfig);
|
|
281
|
+
/**
|
|
282
|
+
* Get the CDP WebSocket URL for CLI tools to connect.
|
|
283
|
+
* For thread scope, returns the CDP URL for the specified thread.
|
|
284
|
+
* For shared scope, returns the single shared CDP URL.
|
|
285
|
+
*
|
|
286
|
+
* @param threadId - Thread identifier (optional, uses current thread if not specified)
|
|
287
|
+
* @returns CDP URL or null if browser not running for that thread
|
|
288
|
+
*/
|
|
289
|
+
getCdpUrl(threadId?: string): string | null;
|
|
290
|
+
protected doLaunch(): Promise<void>;
|
|
291
|
+
protected doClose(): Promise<void>;
|
|
292
|
+
/**
|
|
293
|
+
* Connect to an existing browser via CDP URL.
|
|
294
|
+
*/
|
|
295
|
+
private connectToExisting;
|
|
296
|
+
/**
|
|
297
|
+
* Ensure browser is ready for the current thread.
|
|
298
|
+
* For thread scope, creates a new browser if needed.
|
|
299
|
+
*/
|
|
300
|
+
ensureReady(): Promise<void>;
|
|
301
|
+
/**
|
|
302
|
+
* Check if browser is running (for current thread in thread scope).
|
|
303
|
+
*/
|
|
304
|
+
isBrowserRunning(threadId?: string): boolean;
|
|
305
|
+
/**
|
|
306
|
+
* Launch browser, optionally for a specific thread.
|
|
307
|
+
* For thread scope, creates a browser for that thread.
|
|
308
|
+
* For shared scope, launches the single shared browser.
|
|
309
|
+
*/
|
|
310
|
+
launch(threadId?: string): Promise<void>;
|
|
311
|
+
/**
|
|
312
|
+
* Handle browser disconnection.
|
|
313
|
+
* Overrides base class method.
|
|
314
|
+
*/
|
|
315
|
+
handleBrowserDisconnected(): void;
|
|
316
|
+
/**
|
|
317
|
+
* Connect to an external browser via CDP URL for screencast.
|
|
318
|
+
*
|
|
319
|
+
* Use this when an agent is using their own external CDP (e.g., browser-use cloud).
|
|
320
|
+
* Connects Playwright to the external browser to enable screencast without launching
|
|
321
|
+
* our own browser.
|
|
322
|
+
*
|
|
323
|
+
* @param cdpUrl - The external CDP WebSocket URL (wss://... or ws://...)
|
|
324
|
+
* @param threadId - Thread ID to associate the session with
|
|
325
|
+
*/
|
|
326
|
+
connectToExternalCdp(cdpUrl: string, threadId?: string): Promise<void>;
|
|
327
|
+
protected getActivePage(threadId?: string): Promise<Page | null>;
|
|
328
|
+
protected getBrowserStateForThread(threadId?: string): BrowserState | null;
|
|
329
|
+
startScreencast(options?: ScreencastOptions): Promise<ScreencastStream>;
|
|
330
|
+
injectMouseEvent(params: MouseEventParams, threadId?: string): Promise<void>;
|
|
331
|
+
injectKeyboardEvent(params: KeyboardEventParams, threadId?: string): Promise<void>;
|
|
332
|
+
getTools(): Record<string, Tool>;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
export { BrowserViewer, type BrowserViewerConfig, BrowserViewerThreadManager, type BrowserViewerThreadManagerConfig, type CLIProvider };
|