10
10
11
11
import httpx
12
12
from browserbase import Browserbase
13
+ from browserbase .types import SessionCreateParams as BrowserbaseSessionCreateParams
13
14
from dotenv import load_dotenv
14
15
from playwright .async_api import (
15
16
BrowserContext ,
18
19
)
19
20
from playwright .async_api import Page as PlaywrightPage
20
21
21
- from .base import StagehandBase
22
22
from .config import StagehandConfig
23
23
from .context import StagehandContext
24
24
from .llm import LLMClient
25
25
from .metrics import StagehandFunctionName , StagehandMetrics
26
26
from .page import StagehandPage
27
- from .utils import StagehandLogger , convert_dict_keys_to_camel_case
27
+ from .utils import StagehandLogger , convert_dict_keys_to_camel_case , default_log_handler
28
28
29
29
load_dotenv ()
30
30
31
31
32
- class Stagehand ( StagehandBase ) :
32
+ class Stagehand :
33
33
"""
34
34
Python client for interacting with a running Stagehand server and Browserbase remote headless browser.
35
35
@@ -42,6 +42,7 @@ class Stagehand(StagehandBase):
42
42
43
43
def __init__ (
44
44
self ,
45
+ * ,
45
46
config : Optional [StagehandConfig ] = None ,
46
47
server_url : Optional [str ] = None ,
47
48
session_id : Optional [str ] = None ,
@@ -62,6 +63,7 @@ def __init__(
62
63
use_rich_logging : bool = True ,
63
64
env : Literal ["BROWSERBASE" , "LOCAL" ] = None ,
64
65
local_browser_launch_options : Optional [dict [str , Any ]] = None ,
66
+ browserbase_session_create_params : Optional [BrowserbaseSessionCreateParams ] = None ,
65
67
):
66
68
"""
67
69
Initialize the Stagehand client.
@@ -89,31 +91,81 @@ def __init__(
89
91
local_browser_launch_options (Optional[dict[str, Any]]): Options for launching the local browser context
90
92
when env="LOCAL". See Playwright's launch_persistent_context documentation.
91
93
Common keys: 'headless', 'user_data_dir', 'downloads_path', 'viewport', 'locale', 'proxy', 'args', 'cdp_url'.
94
+ browserbase_session_create_params (Optional[BrowserbaseSessionCreateParams]): Params for Browserbase session creation.
92
95
"""
93
- super ().__init__ (
94
- config = config ,
95
- server_url = server_url ,
96
- session_id = session_id ,
97
- browserbase_api_key = browserbase_api_key ,
98
- browserbase_project_id = browserbase_project_id ,
99
- model_api_key = model_api_key ,
100
- on_log = on_log ,
101
- verbose = verbose ,
102
- model_name = model_name ,
103
- dom_settle_timeout_ms = dom_settle_timeout_ms ,
104
- timeout_settings = timeout_settings ,
105
- stream_response = stream_response ,
106
- model_client_options = model_client_options ,
107
- self_heal = self_heal ,
108
- wait_for_captcha_solves = wait_for_captcha_solves ,
109
- system_prompt = system_prompt ,
96
+ # Initialize configuration from config object or individual parameters
97
+ self .server_url = server_url or os .getenv ("STAGEHAND_SERVER_URL" )
98
+
99
+ if config :
100
+ self .browserbase_api_key = (
101
+ config .api_key
102
+ or browserbase_api_key
103
+ or os .getenv ("BROWSERBASE_API_KEY" )
104
+ )
105
+ self .browserbase_project_id = (
106
+ config .project_id
107
+ or browserbase_project_id
108
+ or os .getenv ("BROWSERBASE_PROJECT_ID" )
109
+ )
110
+ self .session_id = config .browserbase_session_id or session_id
111
+ self .model_name = config .model_name or model_name
112
+ self .dom_settle_timeout_ms = (
113
+ config .dom_settle_timeout_ms or dom_settle_timeout_ms
114
+ )
115
+ self .self_heal = (
116
+ config .self_heal if config .self_heal is not None else self_heal
117
+ )
118
+ self .wait_for_captcha_solves = (
119
+ config .wait_for_captcha_solves
120
+ if config .wait_for_captcha_solves is not None
121
+ else wait_for_captcha_solves
122
+ )
123
+ self .system_prompt = config .system_prompt or system_prompt
124
+ self .browserbase_session_create_params = (
125
+ config .browserbase_session_create_params
126
+ or browserbase_session_create_params
127
+ )
128
+ self .verbose = config .verbose if config .verbose is not None else verbose
129
+ else :
130
+ self .browserbase_api_key = browserbase_api_key or os .getenv (
131
+ "BROWSERBASE_API_KEY"
132
+ )
133
+ self .browserbase_project_id = browserbase_project_id or os .getenv (
134
+ "BROWSERBASE_PROJECT_ID"
135
+ )
136
+ self .session_id = session_id
137
+ self .model_name = model_name
138
+ self .dom_settle_timeout_ms = dom_settle_timeout_ms
139
+ self .self_heal = self_heal
140
+ self .wait_for_captcha_solves = wait_for_captcha_solves
141
+ self .system_prompt = system_prompt
142
+ self .browserbase_session_create_params = browserbase_session_create_params
143
+ self .verbose = verbose
144
+
145
+ # Handle model-related settings directly
146
+ self .model_api_key = model_api_key or os .getenv ("MODEL_API_KEY" )
147
+ self .model_client_options = model_client_options or {}
148
+ if self .model_api_key and "apiKey" not in self .model_client_options :
149
+ self .model_client_options ["apiKey" ] = self .model_api_key
150
+
151
+ # Handle streaming response setting directly
152
+ self .streamed_response = (
153
+ stream_response if stream_response is not None else True
154
+ )
155
+
156
+ self .on_log = on_log or default_log_handler
157
+ self .timeout_settings = timeout_settings or httpx .Timeout (
158
+ connect = 180.0 ,
159
+ read = 180.0 ,
160
+ write = 180.0 ,
161
+ pool = 180.0 ,
110
162
)
111
163
112
164
self .env = env .upper () if env else "BROWSERBASE"
113
165
self .local_browser_launch_options = (
114
166
getattr (config , "local_browser_launch_options" , {})
115
167
if config
116
- else local_browser_launch_options
168
+ else local_browser_launch_options if local_browser_launch_options else {}
117
169
)
118
170
self ._local_user_data_dir_temp : Optional [Path ] = (
119
171
None # To store path if created temporarily
@@ -127,6 +179,11 @@ def __init__(
127
179
if self .env not in ["BROWSERBASE" , "LOCAL" ]:
128
180
raise ValueError ("env must be either 'BROWSERBASE' or 'LOCAL'" )
129
181
182
+ # Initialize the centralized logger with the specified verbosity
183
+ self .logger = StagehandLogger (
184
+ verbose = self .verbose , external_logger = on_log , use_rich = use_rich_logging
185
+ )
186
+
130
187
# If using BROWSERBASE, session_id or creation params are needed
131
188
if self .env == "BROWSERBASE" :
132
189
if not self .session_id :
@@ -155,18 +212,7 @@ def __init__(
155
212
"browserbase_project_id is required for BROWSERBASE env with existing session_id (or set BROWSERBASE_PROJECT_ID in env)."
156
213
)
157
214
158
- # Initialize the centralized logger with the specified verbosity
159
- self .logger = StagehandLogger (
160
- verbose = self .verbose , external_logger = on_log , use_rich = use_rich_logging
161
- )
162
-
163
215
self .httpx_client = httpx_client
164
- self .timeout_settings = timeout_settings or httpx .Timeout (
165
- connect = 180.0 ,
166
- read = 180.0 ,
167
- write = 180.0 ,
168
- pool = 180.0 ,
169
- )
170
216
self ._client : Optional [httpx .AsyncClient ] = (
171
217
None # Used for server communication in BROWSERBASE
172
218
)
0 commit comments