-
Notifications
You must be signed in to change notification settings - Fork 109
enable env browserbase for multi-region #100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| from typing import Any, Optional | ||
|
|
||
| from browserbase import Browserbase | ||
| from browserbase.types import SessionCreateParams as BrowserbaseSessionCreateParams | ||
| from playwright.async_api import ( | ||
| Browser, | ||
| BrowserContext, | ||
|
|
@@ -40,12 +41,31 @@ async def connect_browserbase_browser( | |
| # Connect to remote browser via Browserbase SDK and CDP | ||
| bb = Browserbase(api_key=browserbase_api_key) | ||
| try: | ||
| session = bb.sessions.retrieve(session_id) | ||
| if session.status != "RUNNING": | ||
| raise RuntimeError( | ||
| f"Browserbase session {session_id} is not running (status: {session.status})" | ||
| if session_id: | ||
| session = bb.sessions.retrieve(session_id) | ||
| if session.status != "RUNNING": | ||
| raise RuntimeError( | ||
| f"Browserbase session {session_id} is not running (status: {session.status})" | ||
| ) | ||
| else: | ||
| browserbase_session_create_params = ( | ||
| BrowserbaseSessionCreateParams( | ||
| project_id=stagehand_instance.browserbase_project_id, | ||
| browser_settings={ | ||
| "viewport": { | ||
| "width": 1024, | ||
| "height": 768, | ||
| }, | ||
| }, | ||
| ) | ||
| if not stagehand_instance.browserbase_session_create_params | ||
| else stagehand_instance.browserbase_session_create_params | ||
| ) | ||
| session = bb.sessions.create(**browserbase_session_create_params) | ||
| if not session.id: | ||
| raise Exception("Could not create Browserbase session") | ||
| connect_url = session.connectUrl | ||
| stagehand_instance.session_id = session.id | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move up |
||
| except Exception as e: | ||
| logger.error(f"Error retrieving or validating Browserbase session: {str(e)}") | ||
| raise | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -96,6 +96,16 @@ class StagehandConfig(BaseModel): | |
| alias="localBrowserLaunchOptions", | ||
| description="Local browser launch options", | ||
| ) | ||
| use_api: Optional[bool] = Field( | ||
| True, | ||
| alias="useAPI", | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. None |
||
| description="Whether to use the Stagehand API", | ||
| ) | ||
| experimental: Optional[bool] = Field( | ||
| False, | ||
| alias="experimental", | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. none |
||
| description="Whether to use experimental features", | ||
| ) | ||
|
|
||
| model_config = ConfigDict(populate_by_name=True) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -168,13 +168,23 @@ def __init__( | |
| self._playwright_page: Optional[PlaywrightPage] = None | ||
| self.page: Optional[StagehandPage] = None | ||
| self.context: Optional[StagehandContext] = None | ||
| self.use_api = self.config.use_api | ||
| self.experimental = self.config.experimental | ||
| if self.experimental: | ||
| self.use_api = False | ||
| if ( | ||
| self.browserbase_session_create_params | ||
| and self.browserbase_session_create_params.get("region") | ||
| and self.browserbase_session_create_params.get("region") != "us-west-2" | ||
| ): | ||
| self.use_api = False | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice |
||
|
|
||
| self._initialized = False # Flag to track if init() has run | ||
| self._closed = False # Flag to track if resources have been closed | ||
|
|
||
| # Setup LLM client if LOCAL mode | ||
| self.llm = None | ||
| if self.env == "LOCAL": | ||
| if not self.use_api: | ||
| self.llm = LLMClient( | ||
| stagehand_logger=self.logger, | ||
| api_key=self.model_api_key, | ||
|
|
@@ -385,15 +395,16 @@ async def init(self): | |
|
|
||
| if self.env == "BROWSERBASE": | ||
| # Create session if we don't have one | ||
| if not self.session_id: | ||
| await self._create_session() # Uses self._client and api_url | ||
| self.logger.debug( | ||
| f"Created new Browserbase session via Stagehand server: {self.session_id}" | ||
| ) | ||
| else: | ||
| self.logger.debug( | ||
| f"Using existing Browserbase session: {self.session_id}" | ||
| ) | ||
| if self.use_api: | ||
| if not self.session_id: | ||
| await self._create_session() # Uses self._client and api_url | ||
| self.logger.debug( | ||
| f"Created new Browserbase session via Stagehand server: {self.session_id}" | ||
| ) | ||
| else: | ||
| self.logger.debug( | ||
| f"Using existing Browserbase session: {self.session_id}" | ||
| ) | ||
|
|
||
| # Connect to remote browser | ||
| try: | ||
|
|
@@ -470,8 +481,8 @@ async def close(self): | |
|
|
||
| self.logger.debug("Closing resources...") | ||
|
|
||
| if self.env == "BROWSERBASE": | ||
| # --- BROWSERBASE Cleanup --- | ||
| if self.use_api: | ||
| # --- BROWSERBASE Cleanup (API) --- | ||
| # End the session on the server if we have a session ID | ||
| if self.session_id and self._client: # Check if client was initialized | ||
| try: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this be a param?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is just the default config if none is passed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it should go inside browserbase_session_create_params anyway