Skip to content

Commit

Permalink
Make BB Session ID public within Stagehand class (#298)
Browse files Browse the repository at this point in the history
* Add session ID to init result

* add to readme

* changeset

* index ts
  • Loading branch information
kamath authored Dec 13, 2024
1 parent b902192 commit 55f0cd2
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/wicked-rockets-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@browserbasehq/stagehand": minor
---

Add sessionId to public params
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ This constructor is used to create an instance of Stagehand.
- A `Promise` that resolves to an object containing:
- `debugUrl`: a `string` representing the URL for live debugging. This is only available when using a Browserbase browser.
- `sessionUrl`: a `string` representing the session URL. This is only available when using a Browserbase browser.
- `sessionId`: a `string` representing the session ID. This is only available when using a Browserbase browser.

- **Example:**
```javascript
Expand Down
43 changes: 24 additions & 19 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ async function getBrowser(

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

return { browser, context, debugUrl, sessionUrl };
return { browser, context, debugUrl, sessionUrl, sessionId };
} else {
logger({
category: "init",
Expand Down Expand Up @@ -307,6 +307,8 @@ export class Stagehand {
private llmClient: LLMClient;
public page: Page;
public context: BrowserContext;
public browserbaseSessionID?: string;

private env: "LOCAL" | "BROWSERBASE";
private apiKey: string | undefined;
private projectId: string | undefined;
Expand Down Expand Up @@ -377,23 +379,25 @@ export class Stagehand {
"Passing parameters to init() is deprecated and will be removed in the next major version. Use constructor options instead.",
);
}
const { context, debugUrl, sessionUrl, contextPath } = await getBrowser(
this.apiKey,
this.projectId,
this.env,
this.headless,
this.logger,
this.browserbaseSessionCreateParams,
this.browserbaseResumeSessionID,
).catch((e) => {
console.error("Error in init:", e);
const br: BrowserResult = {
context: undefined,
debugUrl: undefined,
sessionUrl: undefined,
};
return br;
});
const { context, debugUrl, sessionUrl, contextPath, sessionId } =
await getBrowser(
this.apiKey,
this.projectId,
this.env,
this.headless,
this.logger,
this.browserbaseSessionCreateParams,
this.browserbaseResumeSessionID,
).catch((e) => {
console.error("Error in init:", e);
const br: BrowserResult = {
context: undefined,
debugUrl: undefined,
sessionUrl: undefined,
sessionId: undefined,
};
return br;
});
this.contextPath = contextPath;
this.context = context;
this.page = context.pages()[0];
Expand Down Expand Up @@ -455,8 +459,9 @@ export class Stagehand {
verbose: this.verbose,
llmClient: this.llmClient,
});
this.browserbaseSessionID = sessionId;

return { debugUrl, sessionUrl };
return { debugUrl, sessionUrl, sessionId };
}

/** @deprecated initFromPage is deprecated and will be removed in the next major version. */
Expand Down
1 change: 1 addition & 0 deletions types/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export interface BrowserResult {
debugUrl?: string;
sessionUrl?: string;
contextPath?: string;
sessionId?: string;
}
6 changes: 1 addition & 5 deletions types/stagehand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ export interface ConstructorParams {
modelClientOptions?: ClientOptions;
}

export interface InitResult {
debugUrl: string;
sessionUrl: string;
}

export interface InitOptions {
/** @deprecated Pass this into the Stagehand constructor instead. This will be removed in the next major version. */
modelName?: AvailableModel;
Expand All @@ -39,6 +34,7 @@ export interface InitOptions {
export interface InitResult {
debugUrl: string;
sessionUrl: string;
sessionId: string;
}

export interface InitFromPageOptions {
Expand Down

0 comments on commit 55f0cd2

Please # to comment.