Skip to content

Commit 55f0cd2

Browse files
authored
Make BB Session ID public within Stagehand class (#298)
* Add session ID to init result * add to readme * changeset * index ts
1 parent b902192 commit 55f0cd2

File tree

5 files changed

+32
-24
lines changed

5 files changed

+32
-24
lines changed

.changeset/wicked-rockets-beg.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@browserbasehq/stagehand": minor
3+
---
4+
5+
Add sessionId to public params

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ This constructor is used to create an instance of Stagehand.
198198
- A `Promise` that resolves to an object containing:
199199
- `debugUrl`: a `string` representing the URL for live debugging. This is only available when using a Browserbase browser.
200200
- `sessionUrl`: a `string` representing the session URL. This is only available when using a Browserbase browser.
201+
- `sessionId`: a `string` representing the session ID. This is only available when using a Browserbase browser.
201202

202203
- **Example:**
203204
```javascript

lib/index.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ async function getBrowser(
195195

196196
const context = browser.contexts()[0];
197197

198-
return { browser, context, debugUrl, sessionUrl };
198+
return { browser, context, debugUrl, sessionUrl, sessionId };
199199
} else {
200200
logger({
201201
category: "init",
@@ -307,6 +307,8 @@ export class Stagehand {
307307
private llmClient: LLMClient;
308308
public page: Page;
309309
public context: BrowserContext;
310+
public browserbaseSessionID?: string;
311+
310312
private env: "LOCAL" | "BROWSERBASE";
311313
private apiKey: string | undefined;
312314
private projectId: string | undefined;
@@ -377,23 +379,25 @@ export class Stagehand {
377379
"Passing parameters to init() is deprecated and will be removed in the next major version. Use constructor options instead.",
378380
);
379381
}
380-
const { context, debugUrl, sessionUrl, contextPath } = await getBrowser(
381-
this.apiKey,
382-
this.projectId,
383-
this.env,
384-
this.headless,
385-
this.logger,
386-
this.browserbaseSessionCreateParams,
387-
this.browserbaseResumeSessionID,
388-
).catch((e) => {
389-
console.error("Error in init:", e);
390-
const br: BrowserResult = {
391-
context: undefined,
392-
debugUrl: undefined,
393-
sessionUrl: undefined,
394-
};
395-
return br;
396-
});
382+
const { context, debugUrl, sessionUrl, contextPath, sessionId } =
383+
await getBrowser(
384+
this.apiKey,
385+
this.projectId,
386+
this.env,
387+
this.headless,
388+
this.logger,
389+
this.browserbaseSessionCreateParams,
390+
this.browserbaseResumeSessionID,
391+
).catch((e) => {
392+
console.error("Error in init:", e);
393+
const br: BrowserResult = {
394+
context: undefined,
395+
debugUrl: undefined,
396+
sessionUrl: undefined,
397+
sessionId: undefined,
398+
};
399+
return br;
400+
});
397401
this.contextPath = contextPath;
398402
this.context = context;
399403
this.page = context.pages()[0];
@@ -455,8 +459,9 @@ export class Stagehand {
455459
verbose: this.verbose,
456460
llmClient: this.llmClient,
457461
});
462+
this.browserbaseSessionID = sessionId;
458463

459-
return { debugUrl, sessionUrl };
464+
return { debugUrl, sessionUrl, sessionId };
460465
}
461466

462467
/** @deprecated initFromPage is deprecated and will be removed in the next major version. */

types/browser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ export interface BrowserResult {
66
debugUrl?: string;
77
sessionUrl?: string;
88
contextPath?: string;
9+
sessionId?: string;
910
}

types/stagehand.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ export interface ConstructorParams {
2222
modelClientOptions?: ClientOptions;
2323
}
2424

25-
export interface InitResult {
26-
debugUrl: string;
27-
sessionUrl: string;
28-
}
29-
3025
export interface InitOptions {
3126
/** @deprecated Pass this into the Stagehand constructor instead. This will be removed in the next major version. */
3227
modelName?: AvailableModel;
@@ -39,6 +34,7 @@ export interface InitOptions {
3934
export interface InitResult {
4035
debugUrl: string;
4136
sessionUrl: string;
37+
sessionId: string;
4238
}
4339

4440
export interface InitFromPageOptions {

0 commit comments

Comments
 (0)