Skip to content

Commit 86724f6

Browse files
authored
fix the getBrowser function (#633)
* fix the getBrowser function * fix log, prettier * throw error, remove logs * format * changeset * lint * fixed self recursing loop
1 parent 743f633 commit 86724f6

File tree

2 files changed

+48
-33
lines changed

2 files changed

+48
-33
lines changed

Diff for: .changeset/small-snakes-shave.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@browserbasehq/stagehand": patch
3+
---
4+
5+
Fix the getBrowser logic for redundant api calls and throw informed errors

Diff for: lib/index.ts

+43-33
Original file line numberDiff line numberDiff line change
@@ -78,24 +78,16 @@ async function getBrowser(
7878
): Promise<BrowserResult> {
7979
if (env === "BROWSERBASE") {
8080
if (!apiKey) {
81-
throw new Error(
82-
'BROWSERBASE_API_KEY is required to use the BROWSERBASE environment. Please set it in your .env or pass it in.'
81+
throw new MissingEnvironmentVariableError(
82+
"BROWSERBASE_API_KEY",
83+
"Browserbase",
8384
);
8485
}
85-
8686
if (!projectId) {
87-
logger({
88-
category: "init",
89-
message:
90-
"BROWSERBASE_PROJECT_ID is required for some Browserbase features that may not work without it.",
91-
level: 1,
92-
});
93-
}
94-
}
95-
96-
if (env === "BROWSERBASE") {
97-
if (!apiKey) {
98-
throw new StagehandError("BROWSERBASE_API_KEY is required.");
87+
throw new MissingEnvironmentVariableError(
88+
"BROWSERBASE_PROJECT_ID",
89+
"Browserbase",
90+
);
9991
}
10092

10193
let debugUrl: string | undefined = undefined;
@@ -110,17 +102,16 @@ async function getBrowser(
110102
if (browserbaseSessionID) {
111103
// Validate the session status
112104
try {
113-
const sessionStatus =
105+
const session =
114106
await browserbase.sessions.retrieve(browserbaseSessionID);
115107

116-
if (sessionStatus.status !== "RUNNING") {
108+
if (session.status !== "RUNNING") {
117109
throw new StagehandError(
118-
`Session ${browserbaseSessionID} is not running (status: ${sessionStatus.status})`,
110+
`Session ${browserbaseSessionID} is not running (status: ${session.status})`,
119111
);
120112
}
121113

122114
sessionId = browserbaseSessionID;
123-
const session = await browserbase.sessions.retrieve(sessionId);
124115
connectUrl = session.connectUrl;
125116

126117
logger({
@@ -191,6 +182,7 @@ async function getBrowser(
191182
}
192183

193184
const browser = await chromium.connectOverCDP(connectUrl);
185+
194186
const { debuggerUrl } = await browserbase.sessions.debug(sessionId);
195187

196188
debugUrl = debuggerUrl;
@@ -249,7 +241,7 @@ async function getBrowser(
249241
logger({
250242
category: "init",
251243
message: "connecting to local browser via CDP URL",
252-
level: 0,
244+
level: 1,
253245
auxiliary: {
254246
cdpUrl: {
255247
value: localBrowserLaunchOptions.cdpUrl,
@@ -386,16 +378,13 @@ async function applyStealthScripts(context: BrowserContext) {
386378
export class Stagehand {
387379
private stagehandPage!: StagehandPage;
388380
private stagehandContext!: StagehandContext;
389-
private intEnv: "LOCAL" | "BROWSERBASE";
390-
391381
public browserbaseSessionID?: string;
392382
public readonly domSettleTimeoutMs: number;
393383
public readonly debugDom: boolean;
394384
public readonly headless: boolean;
395385
public verbose: 0 | 1 | 2;
396386
public llmProvider: LLMProvider;
397387
public enableCaching: boolean;
398-
399388
private apiKey: string | undefined;
400389
private projectId: string | undefined;
401390
private externalLogger?: (logLine: LogLine) => void;
@@ -415,6 +404,7 @@ export class Stagehand {
415404
public readonly logInferenceToFile?: boolean;
416405
private stagehandLogger: StagehandLogger;
417406
private disablePino: boolean;
407+
private _env: "LOCAL" | "BROWSERBASE";
418408

419409
protected setActivePage(page: StagehandPage): void {
420410
this.stagehandPage = page;
@@ -530,17 +520,26 @@ export class Stagehand {
530520
this.llmProvider =
531521
llmProvider || new LLMProvider(this.logger, this.enableCaching);
532522

533-
this.intEnv = env;
534523
this.apiKey = apiKey ?? process.env.BROWSERBASE_API_KEY;
535524
this.projectId = projectId ?? process.env.BROWSERBASE_PROJECT_ID;
536525

537-
if (this.intEnv === "BROWSERBASE" && !this.apiKey) {
538-
throw new Error(
539-
'Stagehand is set to use "BROWSERBASE" but no BROWSERBASE_API_KEY was found. Please set it in your .env or pass it explicitly.'
540-
);
526+
// Store the environment value
527+
this._env = env ?? "BROWSERBASE";
528+
529+
if (this._env === "BROWSERBASE") {
530+
if (!this.apiKey) {
531+
throw new MissingEnvironmentVariableError(
532+
"BROWSERBASE_API_KEY",
533+
"Browserbase",
534+
);
535+
} else if (!this.projectId) {
536+
throw new MissingEnvironmentVariableError(
537+
"BROWSERBASE_PROJECT_ID",
538+
"Browserbase",
539+
);
540+
}
541541
}
542542

543-
544543
this.verbose = verbose ?? 0;
545544
// Update logger verbosity level
546545
this.stagehandLogger.setVerbosity(this.verbose);
@@ -623,10 +622,22 @@ export class Stagehand {
623622
}
624623

625624
public get env(): "LOCAL" | "BROWSERBASE" {
626-
if (this.intEnv === "BROWSERBASE" && this.apiKey && this.projectId) {
625+
if (this._env === "BROWSERBASE") {
626+
if (!this.apiKey) {
627+
throw new MissingEnvironmentVariableError(
628+
"BROWSERBASE_API_KEY",
629+
"Browserbase",
630+
);
631+
} else if (!this.projectId) {
632+
throw new MissingEnvironmentVariableError(
633+
"BROWSERBASE_PROJECT_ID",
634+
"Browserbase",
635+
);
636+
}
627637
return "BROWSERBASE";
638+
} else {
639+
return "LOCAL";
628640
}
629-
return "LOCAL";
630641
}
631642

632643
public get context(): EnhancedContext {
@@ -671,7 +682,7 @@ export class Stagehand {
671682
this.browserbaseSessionID = sessionId;
672683
}
673684

674-
const { context, debugUrl, sessionUrl, contextPath, sessionId, env } =
685+
const { context, debugUrl, sessionUrl, contextPath, sessionId } =
675686
await getBrowser(
676687
this.apiKey,
677688
this.projectId,
@@ -692,7 +703,6 @@ export class Stagehand {
692703
};
693704
return br;
694705
});
695-
this.intEnv = env;
696706
this.contextPath = contextPath;
697707

698708
this.stagehandContext = await StagehandContext.init(context, this);

0 commit comments

Comments
 (0)