-
Notifications
You must be signed in to change notification settings - Fork 72
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 |
---|---|---|
|
@@ -23,14 +23,14 @@ jobs: | |
runs-on: ubuntu-latest | ||
steps: | ||
- name: Check out repository | ||
uses: actions/checkout@v3 | ||
uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Set up Python | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.10' | ||
python-version: '3.11' | ||
|
||
- name: Install dependencies | ||
run: | | ||
|
@@ -52,60 +52,12 @@ jobs: | |
# run: | | ||
# pytest | ||
|
||
- name: Calculate new version | ||
id: version | ||
- name: Get project version | ||
id: get_version | ||
run: | | ||
# Get current version from pyproject.toml | ||
CURRENT_VERSION=$(python -c "import tomllib; print(tomllib.load(open('pyproject.toml', 'rb'))['project']['version'])") | ||
echo "current_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT | ||
|
||
# Parse version components | ||
IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT_VERSION" | ||
|
||
# Calculate new version based on release type | ||
case "${{ github.event.inputs.release_type }}" in | ||
"major") | ||
NEW_MAJOR=$((MAJOR + 1)) | ||
NEW_MINOR=0 | ||
NEW_PATCH=0 | ||
;; | ||
"minor") | ||
NEW_MAJOR=$MAJOR | ||
NEW_MINOR=$((MINOR + 1)) | ||
NEW_PATCH=0 | ||
;; | ||
"patch") | ||
NEW_MAJOR=$MAJOR | ||
NEW_MINOR=$MINOR | ||
NEW_PATCH=$((PATCH + 1)) | ||
;; | ||
esac | ||
|
||
NEW_VERSION="${NEW_MAJOR}.${NEW_MINOR}.${NEW_PATCH}" | ||
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT | ||
echo "Bumping version from $CURRENT_VERSION to $NEW_VERSION" | ||
|
||
- name: Update version files | ||
run: | | ||
CURRENT_VERSION="${{ steps.version.outputs.current_version }}" | ||
NEW_VERSION="${{ steps.version.outputs.new_version }}" | ||
|
||
# Update pyproject.toml | ||
sed -i "s/version = \"$CURRENT_VERSION\"/version = \"$NEW_VERSION\"/" pyproject.toml | ||
|
||
# Update __init__.py | ||
sed -i "s/__version__ = \"$CURRENT_VERSION\"/__version__ = \"$NEW_VERSION\"/" stagehand/__init__.py | ||
|
||
echo "Updated version to $NEW_VERSION in pyproject.toml and __init__.py" | ||
|
||
- name: Commit version bump | ||
run: | | ||
git config --local user.email "[email protected]" | ||
git config --local user.name "GitHub Action" | ||
git add pyproject.toml stagehand/__init__.py | ||
git commit -m "Bump version to ${{ steps.version.outputs.new_version }}" | ||
git tag "v${{ steps.version.outputs.new_version }}" | ||
|
||
VERSION=$(python -c "import tomli; print(tomli.load(open('pyproject.toml', 'rb'))['project']['version'])") | ||
echo "version=$VERSION" >> $GITHUB_OUTPUT | ||
|
||
- name: Build package | ||
run: | | ||
python -m build | ||
|
@@ -117,15 +69,10 @@ jobs: | |
run: | | ||
twine upload dist/* | ||
|
||
- name: Push version bump | ||
run: | | ||
git push | ||
git push --tags | ||
|
||
- name: Create GitHub Release | ||
if: ${{ github.event.inputs.create_release == 'true' }} | ||
uses: softprops/action-gh-release@v1 | ||
with: | ||
tag_name: v${{ steps.version.outputs.new_version }} | ||
name: Release v${{ steps.version.outputs.new_version }} | ||
tag_name: v${{ steps.get_version.outputs.version }} | ||
name: Release v${{ steps.get_version.outputs.version }} | ||
generate_release_notes: true |
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 | ||
|
||
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", | ||
|
||
description="Whether to use the Stagehand API", | ||
) | ||
experimental: Optional[bool] = Field( | ||
False, | ||
alias="experimental", | ||
|
||
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 | ||
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