@mastra/stagehand 0.0.0 → 0.1.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/CHANGELOG.md +81 -0
- package/LICENSE.md +30 -0
- package/dist/index.cjs +62 -266
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +50 -116
- package/dist/index.d.ts +50 -116
- package/dist/index.js +62 -266
- package/dist/index.js.map +1 -1
- package/package.json +16 -16
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Stagehand } from '@browserbasehq/stagehand';
|
|
2
|
-
import { ThreadManager, ThreadSession, ThreadManagerConfig,
|
|
2
|
+
import { ThreadManager, ThreadSession, ThreadManagerConfig, BrowserConfigBase, BrowserScope, CdpUrlProvider, MastraBrowser, BrowserToolError, BrowserState, BrowserTabState, ScreencastOptions, ScreencastStream, MouseEventParams, KeyboardEventParams } from '@mastra/core/browser';
|
|
3
3
|
import { Tool } from '@mastra/core/tools';
|
|
4
4
|
import { z } from 'zod';
|
|
5
5
|
|
|
@@ -108,11 +108,11 @@ declare const stagehandSchemas: {
|
|
|
108
108
|
};
|
|
109
109
|
|
|
110
110
|
/**
|
|
111
|
-
* StagehandThreadManager - Thread
|
|
111
|
+
* StagehandThreadManager - Thread scope management for StagehandBrowser
|
|
112
112
|
*
|
|
113
113
|
* Supports two scope modes:
|
|
114
|
-
* - '
|
|
115
|
-
* - '
|
|
114
|
+
* - 'shared': All threads share the same Stagehand instance and page
|
|
115
|
+
* - 'thread': Each thread gets its own Stagehand instance (separate browser)
|
|
116
116
|
*
|
|
117
117
|
* @see AgentBrowserThreadManager for the equivalent implementation.
|
|
118
118
|
*/
|
|
@@ -139,49 +139,23 @@ interface StagehandThreadManagerConfig extends ThreadManagerConfig {
|
|
|
139
139
|
* Thread manager for StagehandBrowser.
|
|
140
140
|
*
|
|
141
141
|
* Supports two scope modes:
|
|
142
|
-
* - '
|
|
143
|
-
* - '
|
|
142
|
+
* - 'shared': All threads share the shared Stagehand instance
|
|
143
|
+
* - 'thread': Each thread gets a dedicated Stagehand instance
|
|
144
144
|
*/
|
|
145
|
-
declare class StagehandThreadManager extends ThreadManager<
|
|
146
|
-
private sharedStagehand;
|
|
145
|
+
declare class StagehandThreadManager extends ThreadManager<V3> {
|
|
147
146
|
protected sessions: Map<string, StagehandThreadSession>;
|
|
148
147
|
private createStagehand?;
|
|
149
148
|
private onBrowserCreated?;
|
|
150
|
-
/** Map of thread ID to dedicated Stagehand instance (for 'thread' mode) */
|
|
151
|
-
private readonly threadStagehands;
|
|
152
149
|
constructor(config: StagehandThreadManagerConfig);
|
|
153
|
-
/**
|
|
154
|
-
* Set the shared Stagehand instance (called after browser launch).
|
|
155
|
-
*/
|
|
156
|
-
setStagehand(instance: V3): void;
|
|
157
|
-
/**
|
|
158
|
-
* Clear the shared Stagehand instance (called when browser disconnects).
|
|
159
|
-
*/
|
|
160
|
-
clearStagehand(): void;
|
|
161
150
|
/**
|
|
162
151
|
* Set the factory function for creating new Stagehand instances.
|
|
163
|
-
* Required for '
|
|
152
|
+
* Required for 'thread' scope mode.
|
|
164
153
|
*/
|
|
165
154
|
setCreateStagehand(factory: () => Promise<V3>): void;
|
|
166
155
|
/**
|
|
167
|
-
* Get the
|
|
168
|
-
*/
|
|
169
|
-
getSharedStagehand(): V3;
|
|
170
|
-
/**
|
|
171
|
-
* Get the Stagehand instance for a specific thread.
|
|
172
|
-
* In 'shared' mode, returns the shared instance.
|
|
173
|
-
* In 'thread' mode, returns the thread's dedicated instance.
|
|
174
|
-
*/
|
|
175
|
-
getStagehandForThread(threadId: string): V3 | undefined;
|
|
176
|
-
/**
|
|
177
|
-
* Get the Stagehand page for a thread.
|
|
178
|
-
* Returns the active page from the thread's Stagehand instance.
|
|
179
|
-
*/
|
|
180
|
-
getPageForThread(threadId: string): V3Page$1 | undefined;
|
|
181
|
-
/**
|
|
182
|
-
* Get the shared manager - returns the active page or the Stagehand instance.
|
|
156
|
+
* Get the page for a specific thread, creating session if needed.
|
|
183
157
|
*/
|
|
184
|
-
|
|
158
|
+
getPageForThread(threadId?: string): Promise<V3Page$1 | null>;
|
|
185
159
|
/**
|
|
186
160
|
* Create a new session for a thread.
|
|
187
161
|
*/
|
|
@@ -191,39 +165,18 @@ declare class StagehandThreadManager extends ThreadManager<V3Page$1 | V3> {
|
|
|
191
165
|
*/
|
|
192
166
|
private restoreBrowserState;
|
|
193
167
|
/**
|
|
194
|
-
*
|
|
195
|
-
* For 'thread' mode, no switching needed - each thread has its own instance.
|
|
196
|
-
* For 'shared' mode, nothing to switch.
|
|
197
|
-
*/
|
|
198
|
-
protected switchToSession(_session: StagehandThreadSession): Promise<void>;
|
|
199
|
-
/**
|
|
200
|
-
* Get the manager for a specific session.
|
|
168
|
+
* Get the manager (Stagehand instance) for a specific session.
|
|
201
169
|
*/
|
|
202
|
-
protected getManagerForSession(session: StagehandThreadSession):
|
|
170
|
+
protected getManagerForSession(session: StagehandThreadSession): V3;
|
|
203
171
|
/**
|
|
204
172
|
* Destroy a session and clean up resources.
|
|
205
173
|
*/
|
|
206
174
|
protected doDestroySession(session: StagehandThreadSession): Promise<void>;
|
|
207
175
|
/**
|
|
208
|
-
*
|
|
209
|
-
|
|
210
|
-
destroyAll(): Promise<void>;
|
|
211
|
-
/**
|
|
212
|
-
* Check if any thread Stagehands are still running.
|
|
176
|
+
* Destroy all sessions (called during browser close).
|
|
177
|
+
* doDestroySession handles closing individual Stagehand instances.
|
|
213
178
|
*/
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
* Clear all session tracking without closing browsers.
|
|
217
|
-
* Used when browsers have been externally closed and we just need to reset state.
|
|
218
|
-
*/
|
|
219
|
-
clearAllSessions(): void;
|
|
220
|
-
/**
|
|
221
|
-
* Clear a specific thread's session without closing the browser.
|
|
222
|
-
* Used when a thread's browser has been externally closed.
|
|
223
|
-
* Preserves the browser state for potential restoration.
|
|
224
|
-
* @param threadId - The thread ID to clear
|
|
225
|
-
*/
|
|
226
|
-
clearSession(threadId: string): void;
|
|
179
|
+
destroyAllSessions(): Promise<void>;
|
|
227
180
|
}
|
|
228
181
|
|
|
229
182
|
/**
|
|
@@ -239,9 +192,9 @@ type ModelConfiguration = string | {
|
|
|
239
192
|
baseURL?: string;
|
|
240
193
|
};
|
|
241
194
|
/**
|
|
242
|
-
*
|
|
195
|
+
* Stagehand-specific configuration fields.
|
|
243
196
|
*/
|
|
244
|
-
interface
|
|
197
|
+
interface StagehandConfigExtensions {
|
|
245
198
|
/**
|
|
246
199
|
* Environment to run the browser in
|
|
247
200
|
* - 'LOCAL': Run browser locally
|
|
@@ -286,6 +239,20 @@ interface StagehandBrowserConfig extends BrowserConfig {
|
|
|
286
239
|
*/
|
|
287
240
|
systemPrompt?: string;
|
|
288
241
|
}
|
|
242
|
+
/**
|
|
243
|
+
* Configuration for StagehandBrowser with compile-time enforcement of cdpUrl/scope compatibility.
|
|
244
|
+
*
|
|
245
|
+
* This type enforces that `cdpUrl` and `scope: 'thread'` cannot be used together:
|
|
246
|
+
* - When `cdpUrl` is provided, `scope` must be `'shared'` or omitted
|
|
247
|
+
* - When `scope: 'thread'` is used, `cdpUrl` must not be provided
|
|
248
|
+
*/
|
|
249
|
+
type StagehandBrowserConfig = (BrowserConfigBase & StagehandConfigExtensions & {
|
|
250
|
+
cdpUrl?: undefined;
|
|
251
|
+
scope?: BrowserScope;
|
|
252
|
+
}) | (BrowserConfigBase & StagehandConfigExtensions & {
|
|
253
|
+
cdpUrl: CdpUrlProvider;
|
|
254
|
+
scope?: 'shared';
|
|
255
|
+
});
|
|
289
256
|
/**
|
|
290
257
|
* Action returned from observe()
|
|
291
258
|
*/
|
|
@@ -345,31 +312,22 @@ type V3Page = NonNullable<ReturnType<NonNullable<Stagehand['context']>['activePa
|
|
|
345
312
|
* Unlike AgentBrowser which uses refs ([ref=e1]), StagehandBrowser uses
|
|
346
313
|
* natural language instructions for all interactions.
|
|
347
314
|
*
|
|
348
|
-
* Supports thread
|
|
349
|
-
* - '
|
|
350
|
-
* - '
|
|
315
|
+
* Supports thread scope via the scope config:
|
|
316
|
+
* - 'shared': All threads share the same Stagehand instance
|
|
317
|
+
* - 'thread': Each thread gets its own Stagehand instance (separate browser)
|
|
351
318
|
*/
|
|
352
319
|
declare class StagehandBrowser extends MastraBrowser {
|
|
353
320
|
readonly id: string;
|
|
354
321
|
readonly name = "StagehandBrowser";
|
|
355
322
|
readonly provider = "browserbase/stagehand";
|
|
356
|
-
|
|
323
|
+
/** Shared Stagehand instance (for 'shared' scope) - narrowed type from base class */
|
|
324
|
+
protected sharedManager: Stagehand | null;
|
|
357
325
|
private stagehandConfig;
|
|
358
326
|
/** Thread manager - narrowed type from base class */
|
|
359
327
|
protected threadManager: StagehandThreadManager;
|
|
360
|
-
/** Active screencast streams per thread (for reconnection on tab changes) */
|
|
361
|
-
private activeScreencastStreams;
|
|
362
328
|
/** Debounce timers per thread for tab change reconnection */
|
|
363
329
|
private tabChangeDebounceTimers;
|
|
364
|
-
/** Default key for shared scope */
|
|
365
|
-
private static readonly SHARED_STREAM_KEY;
|
|
366
330
|
constructor(config?: StagehandBrowserConfig);
|
|
367
|
-
/**
|
|
368
|
-
* Close a specific thread's browser session.
|
|
369
|
-
* For 'thread' scope, this closes only that thread's Stagehand instance.
|
|
370
|
-
* For 'shared' scope, this is a no-op (use close() to close the shared browser).
|
|
371
|
-
*/
|
|
372
|
-
closeThreadSession(threadId: string): Promise<void>;
|
|
373
331
|
/**
|
|
374
332
|
* Ensure browser is ready and thread session exists.
|
|
375
333
|
* For 'thread' scope, this creates a dedicated Stagehand instance for the thread.
|
|
@@ -382,37 +340,26 @@ declare class StagehandBrowser extends MastraBrowser {
|
|
|
382
340
|
private buildStagehandOptions;
|
|
383
341
|
/**
|
|
384
342
|
* Create a new Stagehand instance with the current config.
|
|
385
|
-
* Used by thread manager for '
|
|
343
|
+
* Used by thread manager for 'thread' scope.
|
|
386
344
|
*/
|
|
387
345
|
private createStagehandInstance;
|
|
388
346
|
protected doLaunch(): Promise<void>;
|
|
389
347
|
/**
|
|
390
|
-
* Set up close event listener for a Stagehand instance.
|
|
348
|
+
* Set up close event listener for a shared Stagehand instance.
|
|
391
349
|
* Listens to both context and page close events for robust detection.
|
|
392
350
|
*/
|
|
393
|
-
private
|
|
351
|
+
private setupCloseListenerForSharedScope;
|
|
394
352
|
/**
|
|
395
353
|
* Set up close event listener for a thread's Stagehand instance.
|
|
396
354
|
* Uses CDP Target.targetDestroyed events to detect when all pages are gone.
|
|
397
355
|
*/
|
|
398
356
|
private setupCloseListenerForThread;
|
|
399
|
-
/**
|
|
400
|
-
* Handle browser disconnection for a specific thread.
|
|
401
|
-
* Called when a thread's browser is closed externally.
|
|
402
|
-
*/
|
|
403
|
-
private handleThreadBrowserDisconnected;
|
|
404
357
|
protected doClose(): Promise<void>;
|
|
405
358
|
/**
|
|
406
359
|
* Check if the browser is still alive by verifying the context and pages exist.
|
|
407
360
|
* Called by base class ensureReady() to detect externally closed browsers.
|
|
408
361
|
*/
|
|
409
362
|
protected checkBrowserAlive(): Promise<boolean>;
|
|
410
|
-
/**
|
|
411
|
-
* Handle browser disconnection by clearing internal state.
|
|
412
|
-
* For 'thread' scope, only notifies the specific thread's callbacks.
|
|
413
|
-
* For 'shared' scope, notifies all callbacks.
|
|
414
|
-
*/
|
|
415
|
-
handleBrowserDisconnected(): void;
|
|
416
363
|
/**
|
|
417
364
|
* Create an error response from an exception.
|
|
418
365
|
* Extends base class to add Stagehand-specific error handling.
|
|
@@ -420,10 +367,10 @@ declare class StagehandBrowser extends MastraBrowser {
|
|
|
420
367
|
protected createErrorFromException(error: unknown, context: string): BrowserToolError;
|
|
421
368
|
/**
|
|
422
369
|
* Get the Stagehand instance for a thread, creating it if needed.
|
|
423
|
-
* For '
|
|
424
|
-
* For '
|
|
370
|
+
* For 'thread' scope, this creates a dedicated Stagehand instance.
|
|
371
|
+
* For 'shared' scope, returns the shared instance.
|
|
425
372
|
*/
|
|
426
|
-
|
|
373
|
+
getManagerForThread(threadId?: string): Promise<Stagehand | null>;
|
|
427
374
|
/**
|
|
428
375
|
* Require a Stagehand instance for the given or current thread.
|
|
429
376
|
* Throws if no instance is available.
|
|
@@ -432,15 +379,15 @@ declare class StagehandBrowser extends MastraBrowser {
|
|
|
432
379
|
*/
|
|
433
380
|
private requireStagehand;
|
|
434
381
|
/**
|
|
435
|
-
* Get the current page from Stagehand v3, respecting thread
|
|
382
|
+
* Get the current page from Stagehand v3, respecting thread scope.
|
|
436
383
|
* @param explicitThreadId - Optional thread ID to use instead of getCurrentThread()
|
|
437
384
|
* Use this to avoid race conditions in concurrent tool calls.
|
|
438
385
|
*/
|
|
439
386
|
private getPage;
|
|
440
387
|
/**
|
|
441
|
-
* Get the page for a
|
|
388
|
+
* Get the active page for a thread (implements abstract method from base class).
|
|
442
389
|
*/
|
|
443
|
-
|
|
390
|
+
protected getActivePage(threadId?: string): Promise<V3Page | null>;
|
|
444
391
|
/**
|
|
445
392
|
* Get a CDP session for a specific page.
|
|
446
393
|
*/
|
|
@@ -522,6 +469,11 @@ declare class StagehandBrowser extends MastraBrowser {
|
|
|
522
469
|
* Get the current browser state (all tabs and active tab index).
|
|
523
470
|
*/
|
|
524
471
|
getBrowserState(threadId?: string): Promise<BrowserState | null>;
|
|
472
|
+
/**
|
|
473
|
+
* Get browser state for a thread (implements abstract method from base class).
|
|
474
|
+
* Sync version that uses existing manager lookup without creating sessions.
|
|
475
|
+
*/
|
|
476
|
+
protected getBrowserStateForThread(threadId?: string): BrowserState | null;
|
|
525
477
|
/**
|
|
526
478
|
* Get browser state from a specific Stagehand instance.
|
|
527
479
|
*/
|
|
@@ -534,30 +486,12 @@ declare class StagehandBrowser extends MastraBrowser {
|
|
|
534
486
|
* Get the active tab index.
|
|
535
487
|
*/
|
|
536
488
|
getActiveTabIndex(threadId?: string): Promise<number>;
|
|
537
|
-
/**
|
|
538
|
-
* Update the browser state in the thread session.
|
|
539
|
-
* Called on navigation, tab open/close to keep state fresh.
|
|
540
|
-
*/
|
|
541
|
-
private updateSessionBrowserState;
|
|
542
|
-
/**
|
|
543
|
-
* Get the stream key for a thread (or shared key for shared scope).
|
|
544
|
-
*/
|
|
545
|
-
private getStreamKey;
|
|
546
489
|
startScreencast(options?: ScreencastOptions): Promise<ScreencastStream>;
|
|
547
490
|
/**
|
|
548
491
|
* Set up listeners to detect tab changes and reconnect the screencast.
|
|
549
492
|
* Uses CDP Target events since Stagehand doesn't expose page lifecycle events.
|
|
550
493
|
*/
|
|
551
494
|
private setupTabChangeDetection;
|
|
552
|
-
/**
|
|
553
|
-
* Reconnect the active screencast for a specific thread.
|
|
554
|
-
*/
|
|
555
|
-
private reconnectScreencastForThread;
|
|
556
|
-
/**
|
|
557
|
-
* Reconnect the active screencast for the current thread.
|
|
558
|
-
* Wrapper for reconnectScreencastForThread using getCurrentThread().
|
|
559
|
-
*/
|
|
560
|
-
private reconnectScreencast;
|
|
561
495
|
injectMouseEvent(event: MouseEventParams, threadId?: string): Promise<void>;
|
|
562
496
|
injectKeyboardEvent(event: KeyboardEventParams, threadId?: string): Promise<void>;
|
|
563
497
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Stagehand } from '@browserbasehq/stagehand';
|
|
2
|
-
import { ThreadManager, ThreadSession, ThreadManagerConfig,
|
|
2
|
+
import { ThreadManager, ThreadSession, ThreadManagerConfig, BrowserConfigBase, BrowserScope, CdpUrlProvider, MastraBrowser, BrowserToolError, BrowserState, BrowserTabState, ScreencastOptions, ScreencastStream, MouseEventParams, KeyboardEventParams } from '@mastra/core/browser';
|
|
3
3
|
import { Tool } from '@mastra/core/tools';
|
|
4
4
|
import { z } from 'zod';
|
|
5
5
|
|
|
@@ -108,11 +108,11 @@ declare const stagehandSchemas: {
|
|
|
108
108
|
};
|
|
109
109
|
|
|
110
110
|
/**
|
|
111
|
-
* StagehandThreadManager - Thread
|
|
111
|
+
* StagehandThreadManager - Thread scope management for StagehandBrowser
|
|
112
112
|
*
|
|
113
113
|
* Supports two scope modes:
|
|
114
|
-
* - '
|
|
115
|
-
* - '
|
|
114
|
+
* - 'shared': All threads share the same Stagehand instance and page
|
|
115
|
+
* - 'thread': Each thread gets its own Stagehand instance (separate browser)
|
|
116
116
|
*
|
|
117
117
|
* @see AgentBrowserThreadManager for the equivalent implementation.
|
|
118
118
|
*/
|
|
@@ -139,49 +139,23 @@ interface StagehandThreadManagerConfig extends ThreadManagerConfig {
|
|
|
139
139
|
* Thread manager for StagehandBrowser.
|
|
140
140
|
*
|
|
141
141
|
* Supports two scope modes:
|
|
142
|
-
* - '
|
|
143
|
-
* - '
|
|
142
|
+
* - 'shared': All threads share the shared Stagehand instance
|
|
143
|
+
* - 'thread': Each thread gets a dedicated Stagehand instance
|
|
144
144
|
*/
|
|
145
|
-
declare class StagehandThreadManager extends ThreadManager<
|
|
146
|
-
private sharedStagehand;
|
|
145
|
+
declare class StagehandThreadManager extends ThreadManager<V3> {
|
|
147
146
|
protected sessions: Map<string, StagehandThreadSession>;
|
|
148
147
|
private createStagehand?;
|
|
149
148
|
private onBrowserCreated?;
|
|
150
|
-
/** Map of thread ID to dedicated Stagehand instance (for 'thread' mode) */
|
|
151
|
-
private readonly threadStagehands;
|
|
152
149
|
constructor(config: StagehandThreadManagerConfig);
|
|
153
|
-
/**
|
|
154
|
-
* Set the shared Stagehand instance (called after browser launch).
|
|
155
|
-
*/
|
|
156
|
-
setStagehand(instance: V3): void;
|
|
157
|
-
/**
|
|
158
|
-
* Clear the shared Stagehand instance (called when browser disconnects).
|
|
159
|
-
*/
|
|
160
|
-
clearStagehand(): void;
|
|
161
150
|
/**
|
|
162
151
|
* Set the factory function for creating new Stagehand instances.
|
|
163
|
-
* Required for '
|
|
152
|
+
* Required for 'thread' scope mode.
|
|
164
153
|
*/
|
|
165
154
|
setCreateStagehand(factory: () => Promise<V3>): void;
|
|
166
155
|
/**
|
|
167
|
-
* Get the
|
|
168
|
-
*/
|
|
169
|
-
getSharedStagehand(): V3;
|
|
170
|
-
/**
|
|
171
|
-
* Get the Stagehand instance for a specific thread.
|
|
172
|
-
* In 'shared' mode, returns the shared instance.
|
|
173
|
-
* In 'thread' mode, returns the thread's dedicated instance.
|
|
174
|
-
*/
|
|
175
|
-
getStagehandForThread(threadId: string): V3 | undefined;
|
|
176
|
-
/**
|
|
177
|
-
* Get the Stagehand page for a thread.
|
|
178
|
-
* Returns the active page from the thread's Stagehand instance.
|
|
179
|
-
*/
|
|
180
|
-
getPageForThread(threadId: string): V3Page$1 | undefined;
|
|
181
|
-
/**
|
|
182
|
-
* Get the shared manager - returns the active page or the Stagehand instance.
|
|
156
|
+
* Get the page for a specific thread, creating session if needed.
|
|
183
157
|
*/
|
|
184
|
-
|
|
158
|
+
getPageForThread(threadId?: string): Promise<V3Page$1 | null>;
|
|
185
159
|
/**
|
|
186
160
|
* Create a new session for a thread.
|
|
187
161
|
*/
|
|
@@ -191,39 +165,18 @@ declare class StagehandThreadManager extends ThreadManager<V3Page$1 | V3> {
|
|
|
191
165
|
*/
|
|
192
166
|
private restoreBrowserState;
|
|
193
167
|
/**
|
|
194
|
-
*
|
|
195
|
-
* For 'thread' mode, no switching needed - each thread has its own instance.
|
|
196
|
-
* For 'shared' mode, nothing to switch.
|
|
197
|
-
*/
|
|
198
|
-
protected switchToSession(_session: StagehandThreadSession): Promise<void>;
|
|
199
|
-
/**
|
|
200
|
-
* Get the manager for a specific session.
|
|
168
|
+
* Get the manager (Stagehand instance) for a specific session.
|
|
201
169
|
*/
|
|
202
|
-
protected getManagerForSession(session: StagehandThreadSession):
|
|
170
|
+
protected getManagerForSession(session: StagehandThreadSession): V3;
|
|
203
171
|
/**
|
|
204
172
|
* Destroy a session and clean up resources.
|
|
205
173
|
*/
|
|
206
174
|
protected doDestroySession(session: StagehandThreadSession): Promise<void>;
|
|
207
175
|
/**
|
|
208
|
-
*
|
|
209
|
-
|
|
210
|
-
destroyAll(): Promise<void>;
|
|
211
|
-
/**
|
|
212
|
-
* Check if any thread Stagehands are still running.
|
|
176
|
+
* Destroy all sessions (called during browser close).
|
|
177
|
+
* doDestroySession handles closing individual Stagehand instances.
|
|
213
178
|
*/
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
* Clear all session tracking without closing browsers.
|
|
217
|
-
* Used when browsers have been externally closed and we just need to reset state.
|
|
218
|
-
*/
|
|
219
|
-
clearAllSessions(): void;
|
|
220
|
-
/**
|
|
221
|
-
* Clear a specific thread's session without closing the browser.
|
|
222
|
-
* Used when a thread's browser has been externally closed.
|
|
223
|
-
* Preserves the browser state for potential restoration.
|
|
224
|
-
* @param threadId - The thread ID to clear
|
|
225
|
-
*/
|
|
226
|
-
clearSession(threadId: string): void;
|
|
179
|
+
destroyAllSessions(): Promise<void>;
|
|
227
180
|
}
|
|
228
181
|
|
|
229
182
|
/**
|
|
@@ -239,9 +192,9 @@ type ModelConfiguration = string | {
|
|
|
239
192
|
baseURL?: string;
|
|
240
193
|
};
|
|
241
194
|
/**
|
|
242
|
-
*
|
|
195
|
+
* Stagehand-specific configuration fields.
|
|
243
196
|
*/
|
|
244
|
-
interface
|
|
197
|
+
interface StagehandConfigExtensions {
|
|
245
198
|
/**
|
|
246
199
|
* Environment to run the browser in
|
|
247
200
|
* - 'LOCAL': Run browser locally
|
|
@@ -286,6 +239,20 @@ interface StagehandBrowserConfig extends BrowserConfig {
|
|
|
286
239
|
*/
|
|
287
240
|
systemPrompt?: string;
|
|
288
241
|
}
|
|
242
|
+
/**
|
|
243
|
+
* Configuration for StagehandBrowser with compile-time enforcement of cdpUrl/scope compatibility.
|
|
244
|
+
*
|
|
245
|
+
* This type enforces that `cdpUrl` and `scope: 'thread'` cannot be used together:
|
|
246
|
+
* - When `cdpUrl` is provided, `scope` must be `'shared'` or omitted
|
|
247
|
+
* - When `scope: 'thread'` is used, `cdpUrl` must not be provided
|
|
248
|
+
*/
|
|
249
|
+
type StagehandBrowserConfig = (BrowserConfigBase & StagehandConfigExtensions & {
|
|
250
|
+
cdpUrl?: undefined;
|
|
251
|
+
scope?: BrowserScope;
|
|
252
|
+
}) | (BrowserConfigBase & StagehandConfigExtensions & {
|
|
253
|
+
cdpUrl: CdpUrlProvider;
|
|
254
|
+
scope?: 'shared';
|
|
255
|
+
});
|
|
289
256
|
/**
|
|
290
257
|
* Action returned from observe()
|
|
291
258
|
*/
|
|
@@ -345,31 +312,22 @@ type V3Page = NonNullable<ReturnType<NonNullable<Stagehand['context']>['activePa
|
|
|
345
312
|
* Unlike AgentBrowser which uses refs ([ref=e1]), StagehandBrowser uses
|
|
346
313
|
* natural language instructions for all interactions.
|
|
347
314
|
*
|
|
348
|
-
* Supports thread
|
|
349
|
-
* - '
|
|
350
|
-
* - '
|
|
315
|
+
* Supports thread scope via the scope config:
|
|
316
|
+
* - 'shared': All threads share the same Stagehand instance
|
|
317
|
+
* - 'thread': Each thread gets its own Stagehand instance (separate browser)
|
|
351
318
|
*/
|
|
352
319
|
declare class StagehandBrowser extends MastraBrowser {
|
|
353
320
|
readonly id: string;
|
|
354
321
|
readonly name = "StagehandBrowser";
|
|
355
322
|
readonly provider = "browserbase/stagehand";
|
|
356
|
-
|
|
323
|
+
/** Shared Stagehand instance (for 'shared' scope) - narrowed type from base class */
|
|
324
|
+
protected sharedManager: Stagehand | null;
|
|
357
325
|
private stagehandConfig;
|
|
358
326
|
/** Thread manager - narrowed type from base class */
|
|
359
327
|
protected threadManager: StagehandThreadManager;
|
|
360
|
-
/** Active screencast streams per thread (for reconnection on tab changes) */
|
|
361
|
-
private activeScreencastStreams;
|
|
362
328
|
/** Debounce timers per thread for tab change reconnection */
|
|
363
329
|
private tabChangeDebounceTimers;
|
|
364
|
-
/** Default key for shared scope */
|
|
365
|
-
private static readonly SHARED_STREAM_KEY;
|
|
366
330
|
constructor(config?: StagehandBrowserConfig);
|
|
367
|
-
/**
|
|
368
|
-
* Close a specific thread's browser session.
|
|
369
|
-
* For 'thread' scope, this closes only that thread's Stagehand instance.
|
|
370
|
-
* For 'shared' scope, this is a no-op (use close() to close the shared browser).
|
|
371
|
-
*/
|
|
372
|
-
closeThreadSession(threadId: string): Promise<void>;
|
|
373
331
|
/**
|
|
374
332
|
* Ensure browser is ready and thread session exists.
|
|
375
333
|
* For 'thread' scope, this creates a dedicated Stagehand instance for the thread.
|
|
@@ -382,37 +340,26 @@ declare class StagehandBrowser extends MastraBrowser {
|
|
|
382
340
|
private buildStagehandOptions;
|
|
383
341
|
/**
|
|
384
342
|
* Create a new Stagehand instance with the current config.
|
|
385
|
-
* Used by thread manager for '
|
|
343
|
+
* Used by thread manager for 'thread' scope.
|
|
386
344
|
*/
|
|
387
345
|
private createStagehandInstance;
|
|
388
346
|
protected doLaunch(): Promise<void>;
|
|
389
347
|
/**
|
|
390
|
-
* Set up close event listener for a Stagehand instance.
|
|
348
|
+
* Set up close event listener for a shared Stagehand instance.
|
|
391
349
|
* Listens to both context and page close events for robust detection.
|
|
392
350
|
*/
|
|
393
|
-
private
|
|
351
|
+
private setupCloseListenerForSharedScope;
|
|
394
352
|
/**
|
|
395
353
|
* Set up close event listener for a thread's Stagehand instance.
|
|
396
354
|
* Uses CDP Target.targetDestroyed events to detect when all pages are gone.
|
|
397
355
|
*/
|
|
398
356
|
private setupCloseListenerForThread;
|
|
399
|
-
/**
|
|
400
|
-
* Handle browser disconnection for a specific thread.
|
|
401
|
-
* Called when a thread's browser is closed externally.
|
|
402
|
-
*/
|
|
403
|
-
private handleThreadBrowserDisconnected;
|
|
404
357
|
protected doClose(): Promise<void>;
|
|
405
358
|
/**
|
|
406
359
|
* Check if the browser is still alive by verifying the context and pages exist.
|
|
407
360
|
* Called by base class ensureReady() to detect externally closed browsers.
|
|
408
361
|
*/
|
|
409
362
|
protected checkBrowserAlive(): Promise<boolean>;
|
|
410
|
-
/**
|
|
411
|
-
* Handle browser disconnection by clearing internal state.
|
|
412
|
-
* For 'thread' scope, only notifies the specific thread's callbacks.
|
|
413
|
-
* For 'shared' scope, notifies all callbacks.
|
|
414
|
-
*/
|
|
415
|
-
handleBrowserDisconnected(): void;
|
|
416
363
|
/**
|
|
417
364
|
* Create an error response from an exception.
|
|
418
365
|
* Extends base class to add Stagehand-specific error handling.
|
|
@@ -420,10 +367,10 @@ declare class StagehandBrowser extends MastraBrowser {
|
|
|
420
367
|
protected createErrorFromException(error: unknown, context: string): BrowserToolError;
|
|
421
368
|
/**
|
|
422
369
|
* Get the Stagehand instance for a thread, creating it if needed.
|
|
423
|
-
* For '
|
|
424
|
-
* For '
|
|
370
|
+
* For 'thread' scope, this creates a dedicated Stagehand instance.
|
|
371
|
+
* For 'shared' scope, returns the shared instance.
|
|
425
372
|
*/
|
|
426
|
-
|
|
373
|
+
getManagerForThread(threadId?: string): Promise<Stagehand | null>;
|
|
427
374
|
/**
|
|
428
375
|
* Require a Stagehand instance for the given or current thread.
|
|
429
376
|
* Throws if no instance is available.
|
|
@@ -432,15 +379,15 @@ declare class StagehandBrowser extends MastraBrowser {
|
|
|
432
379
|
*/
|
|
433
380
|
private requireStagehand;
|
|
434
381
|
/**
|
|
435
|
-
* Get the current page from Stagehand v3, respecting thread
|
|
382
|
+
* Get the current page from Stagehand v3, respecting thread scope.
|
|
436
383
|
* @param explicitThreadId - Optional thread ID to use instead of getCurrentThread()
|
|
437
384
|
* Use this to avoid race conditions in concurrent tool calls.
|
|
438
385
|
*/
|
|
439
386
|
private getPage;
|
|
440
387
|
/**
|
|
441
|
-
* Get the page for a
|
|
388
|
+
* Get the active page for a thread (implements abstract method from base class).
|
|
442
389
|
*/
|
|
443
|
-
|
|
390
|
+
protected getActivePage(threadId?: string): Promise<V3Page | null>;
|
|
444
391
|
/**
|
|
445
392
|
* Get a CDP session for a specific page.
|
|
446
393
|
*/
|
|
@@ -522,6 +469,11 @@ declare class StagehandBrowser extends MastraBrowser {
|
|
|
522
469
|
* Get the current browser state (all tabs and active tab index).
|
|
523
470
|
*/
|
|
524
471
|
getBrowserState(threadId?: string): Promise<BrowserState | null>;
|
|
472
|
+
/**
|
|
473
|
+
* Get browser state for a thread (implements abstract method from base class).
|
|
474
|
+
* Sync version that uses existing manager lookup without creating sessions.
|
|
475
|
+
*/
|
|
476
|
+
protected getBrowserStateForThread(threadId?: string): BrowserState | null;
|
|
525
477
|
/**
|
|
526
478
|
* Get browser state from a specific Stagehand instance.
|
|
527
479
|
*/
|
|
@@ -534,30 +486,12 @@ declare class StagehandBrowser extends MastraBrowser {
|
|
|
534
486
|
* Get the active tab index.
|
|
535
487
|
*/
|
|
536
488
|
getActiveTabIndex(threadId?: string): Promise<number>;
|
|
537
|
-
/**
|
|
538
|
-
* Update the browser state in the thread session.
|
|
539
|
-
* Called on navigation, tab open/close to keep state fresh.
|
|
540
|
-
*/
|
|
541
|
-
private updateSessionBrowserState;
|
|
542
|
-
/**
|
|
543
|
-
* Get the stream key for a thread (or shared key for shared scope).
|
|
544
|
-
*/
|
|
545
|
-
private getStreamKey;
|
|
546
489
|
startScreencast(options?: ScreencastOptions): Promise<ScreencastStream>;
|
|
547
490
|
/**
|
|
548
491
|
* Set up listeners to detect tab changes and reconnect the screencast.
|
|
549
492
|
* Uses CDP Target events since Stagehand doesn't expose page lifecycle events.
|
|
550
493
|
*/
|
|
551
494
|
private setupTabChangeDetection;
|
|
552
|
-
/**
|
|
553
|
-
* Reconnect the active screencast for a specific thread.
|
|
554
|
-
*/
|
|
555
|
-
private reconnectScreencastForThread;
|
|
556
|
-
/**
|
|
557
|
-
* Reconnect the active screencast for the current thread.
|
|
558
|
-
* Wrapper for reconnectScreencastForThread using getCurrentThread().
|
|
559
|
-
*/
|
|
560
|
-
private reconnectScreencast;
|
|
561
495
|
injectMouseEvent(event: MouseEventParams, threadId?: string): Promise<void>;
|
|
562
496
|
injectKeyboardEvent(event: KeyboardEventParams, threadId?: string): Promise<void>;
|
|
563
497
|
}
|