From 55f0cd2fe7976e800833ec6e41e9af62d88d09d5 Mon Sep 17 00:00:00 2001 From: Anirudh Kamath Date: Thu, 12 Dec 2024 16:28:10 -0800 Subject: [PATCH] Make BB Session ID public within Stagehand class (#298) * Add session ID to init result * add to readme * changeset * index ts --- .changeset/wicked-rockets-beg.md | 5 ++++ README.md | 1 + lib/index.ts | 43 ++++++++++++++++++-------------- types/browser.ts | 1 + types/stagehand.ts | 6 +---- 5 files changed, 32 insertions(+), 24 deletions(-) create mode 100644 .changeset/wicked-rockets-beg.md diff --git a/.changeset/wicked-rockets-beg.md b/.changeset/wicked-rockets-beg.md new file mode 100644 index 00000000..4aa39f68 --- /dev/null +++ b/.changeset/wicked-rockets-beg.md @@ -0,0 +1,5 @@ +--- +"@browserbasehq/stagehand": minor +--- + +Add sessionId to public params diff --git a/README.md b/README.md index fefb5ce4..f1f847c6 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/index.ts b/lib/index.ts index 3540b320..224c2436 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -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", @@ -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; @@ -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]; @@ -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. */ diff --git a/types/browser.ts b/types/browser.ts index 940b5189..9796fa60 100644 --- a/types/browser.ts +++ b/types/browser.ts @@ -6,4 +6,5 @@ export interface BrowserResult { debugUrl?: string; sessionUrl?: string; contextPath?: string; + sessionId?: string; } diff --git a/types/stagehand.ts b/types/stagehand.ts index 422c42e7..58396573 100644 --- a/types/stagehand.ts +++ b/types/stagehand.ts @@ -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; @@ -39,6 +34,7 @@ export interface InitOptions { export interface InitResult { debugUrl: string; sessionUrl: string; + sessionId: string; } export interface InitFromPageOptions {