|
1 | | -import { Browser, Page } from "playwright-core"; |
2 | | -import { AvailableModel, Stagehand } from "@browserbasehq/stagehand"; |
| 1 | +import { Page } from "playwright-core"; |
| 2 | +import { BrowserContext } from "@browserbasehq/stagehand"; |
3 | 3 | import type { Config } from "../config.js"; |
4 | 4 | import type { Cookie } from "playwright-core"; |
5 | | - |
6 | | -export type BrowserSession = { |
7 | | - browser: Browser; |
8 | | - page: Page; |
9 | | - sessionId: string; |
10 | | - stagehand: Stagehand; |
11 | | -}; |
| 5 | +import { createStagehandInstance } from "./stagehandStore.js"; |
| 6 | +import type { BrowserSession } from "./types/types.js"; |
12 | 7 |
|
13 | 8 | // Global state for managing browser sessions |
14 | 9 | const browsers = new Map<string, BrowserSession>(); |
@@ -50,7 +45,7 @@ export function getActiveSessionId(): string { |
50 | 45 | * @param cookies Array of cookies to add |
51 | 46 | */ |
52 | 47 | export async function addCookiesToContext( |
53 | | - context: any, |
| 48 | + context: BrowserContext, |
54 | 49 | cookies: Cookie[], |
55 | 50 | ): Promise<void> { |
56 | 51 | if (!cookies || cookies.length === 0) { |
@@ -92,40 +87,14 @@ export async function createNewBrowserSession( |
92 | 87 | `[SessionManager] ${resumeSessionId ? "Resuming" : "Creating"} Stagehand session ${newSessionId}...\n`, |
93 | 88 | ); |
94 | 89 |
|
95 | | - // Create and initialize Stagehand instance |
96 | | - const stagehand = new Stagehand({ |
97 | | - env: "BROWSERBASE", |
98 | | - apiKey: config.browserbaseApiKey, |
99 | | - projectId: config.browserbaseProjectId, |
100 | | - modelName: (config.modelName || |
101 | | - "google/gemini-2.0-flash") as AvailableModel, |
102 | | - modelClientOptions: { |
103 | | - apiKey: process.env.GEMINI_API_KEY, // TODO: change this in prod to just use our key |
104 | | - }, |
105 | | - ...(resumeSessionId && { browserbaseSessionID: resumeSessionId }), |
106 | | - browserbaseSessionCreateParams: { |
107 | | - projectId: config.browserbaseProjectId!, |
108 | | - proxies: config.proxies, |
109 | | - browserSettings: { |
110 | | - viewport: { |
111 | | - width: config.viewPort?.browserWidth ?? 1024, |
112 | | - height: config.viewPort?.browserHeight ?? 768, |
113 | | - }, |
114 | | - context: config.context?.contextId |
115 | | - ? { |
116 | | - id: config.context?.contextId, |
117 | | - persist: config.context?.persist ?? true, |
118 | | - } |
119 | | - : undefined, |
120 | | - advancedStealth: config.advancedStealth ?? undefined, |
121 | | - }, |
| 90 | + // Create and initialize Stagehand instance using shared function |
| 91 | + const stagehand = await createStagehandInstance( |
| 92 | + config, |
| 93 | + { |
| 94 | + ...(resumeSessionId && { browserbaseSessionID: resumeSessionId }), |
122 | 95 | }, |
123 | | - logger: (logLine) => { |
124 | | - console.error(`Stagehand[${newSessionId}]: ${logLine.message}`); |
125 | | - }, |
126 | | - }); |
127 | | - |
128 | | - await stagehand.init(); |
| 96 | + newSessionId, |
| 97 | + ); |
129 | 98 |
|
130 | 99 | // Get the page and browser from Stagehand |
131 | 100 | const page = stagehand.page as unknown as Page; |
@@ -171,7 +140,10 @@ export async function createNewBrowserSession( |
171 | 140 | Array.isArray(config.cookies) && |
172 | 141 | config.cookies.length > 0 |
173 | 142 | ) { |
174 | | - await addCookiesToContext(page.context(), config.cookies); |
| 143 | + await addCookiesToContext( |
| 144 | + page.context() as BrowserContext, |
| 145 | + config.cookies, |
| 146 | + ); |
175 | 147 | } |
176 | 148 |
|
177 | 149 | const sessionObj: BrowserSession = { |
@@ -306,7 +278,7 @@ export async function getSession( |
306 | 278 | if (sessionId === defaultSessionId && createIfMissing) { |
307 | 279 | try { |
308 | 280 | return await ensureDefaultSessionInternal(config); |
309 | | - } catch (error) { |
| 281 | + } catch { |
310 | 282 | process.stderr.write( |
311 | 283 | `[SessionManager] Failed to get default session due to error in ensureDefaultSessionInternal for ${sessionId}. See previous messages for details.\n`, |
312 | 284 | ); |
@@ -385,7 +357,7 @@ export async function closeAllSessions(): Promise<void> { |
385 | 357 | } |
386 | 358 | try { |
387 | 359 | await Promise.all(closePromises); |
388 | | - } catch (_e) { |
| 360 | + } catch { |
389 | 361 | // Individual errors are caught and logged by closeBrowserGracefully |
390 | 362 | process.stderr.write( |
391 | 363 | `[SessionManager] WARN - Some errors occurred during batch session closing. See individual messages.\n`, |
|
0 commit comments