/ tools / browser_providers / browserbase.py
browserbase.py
  1  """Browserbase cloud browser provider (direct credentials only)."""
  2  
  3  import logging
  4  import os
  5  import uuid
  6  from typing import Any, Dict, Optional
  7  
  8  import requests
  9  
 10  from tools.browser_providers.base import CloudBrowserProvider
 11  
 12  logger = logging.getLogger(__name__)
 13  
 14  
 15  class BrowserbaseProvider(CloudBrowserProvider):
 16      """Browserbase (https://browserbase.com) cloud browser backend.
 17  
 18      This provider requires direct BROWSERBASE_API_KEY and BROWSERBASE_PROJECT_ID
 19      credentials.  Managed Nous gateway support has been removed — the Nous
 20      subscription now routes through Browser Use instead.
 21      """
 22  
 23      def provider_name(self) -> str:
 24          return "Browserbase"
 25  
 26      def is_configured(self) -> bool:
 27          return self._get_config_or_none() is not None
 28  
 29      # ------------------------------------------------------------------
 30      # Session lifecycle
 31      # ------------------------------------------------------------------
 32  
 33      def _get_config_or_none(self) -> Optional[Dict[str, Any]]:
 34          api_key = os.environ.get("BROWSERBASE_API_KEY")
 35          project_id = os.environ.get("BROWSERBASE_PROJECT_ID")
 36          if api_key and project_id:
 37              return {
 38                  "api_key": api_key,
 39                  "project_id": project_id,
 40                  "base_url": os.environ.get("BROWSERBASE_BASE_URL", "https://api.browserbase.com").rstrip("/"),
 41              }
 42          return None
 43  
 44      def _get_config(self) -> Dict[str, Any]:
 45          config = self._get_config_or_none()
 46          if config is None:
 47              raise ValueError(
 48                  "Browserbase requires BROWSERBASE_API_KEY and BROWSERBASE_PROJECT_ID "
 49                  "environment variables."
 50              )
 51          return config
 52  
 53      def create_session(self, task_id: str) -> Dict[str, object]:
 54          config = self._get_config()
 55  
 56          # Optional env-var knobs
 57          enable_proxies = os.environ.get("BROWSERBASE_PROXIES", "true").lower() != "false"
 58          enable_advanced_stealth = os.environ.get("BROWSERBASE_ADVANCED_STEALTH", "false").lower() == "true"
 59          enable_keep_alive = os.environ.get("BROWSERBASE_KEEP_ALIVE", "true").lower() != "false"
 60          custom_timeout_ms = os.environ.get("BROWSERBASE_SESSION_TIMEOUT")
 61  
 62          features_enabled = {
 63              "basic_stealth": True,
 64              "proxies": False,
 65              "advanced_stealth": False,
 66              "keep_alive": False,
 67              "custom_timeout": False,
 68          }
 69  
 70          session_config: Dict[str, object] = {"projectId": config["project_id"]}
 71  
 72          if enable_keep_alive:
 73              session_config["keepAlive"] = True
 74  
 75          if custom_timeout_ms:
 76              try:
 77                  timeout_val = int(custom_timeout_ms)
 78                  if timeout_val > 0:
 79                      session_config["timeout"] = timeout_val
 80              except ValueError:
 81                  logger.warning("Invalid BROWSERBASE_SESSION_TIMEOUT value: %s", custom_timeout_ms)
 82  
 83          if enable_proxies:
 84              session_config["proxies"] = True
 85  
 86          if enable_advanced_stealth:
 87              session_config["browserSettings"] = {"advancedStealth": True}
 88  
 89          # --- Create session via API ---
 90          headers = {
 91              "Content-Type": "application/json",
 92              "X-BB-API-Key": config["api_key"],
 93          }
 94  
 95          response = requests.post(
 96              f"{config['base_url']}/v1/sessions",
 97              headers=headers,
 98              json=session_config,
 99              timeout=30,
100          )
101  
102          proxies_fallback = False
103          keepalive_fallback = False
104  
105          # Handle 402 — paid features unavailable
106          if response.status_code == 402:
107              if enable_keep_alive:
108                  keepalive_fallback = True
109                  logger.warning(
110                      "keepAlive may require paid plan (402), retrying without it. "
111                      "Sessions may timeout during long operations."
112                  )
113                  session_config.pop("keepAlive", None)
114                  response = requests.post(
115                      f"{config['base_url']}/v1/sessions",
116                      headers=headers,
117                      json=session_config,
118                      timeout=30,
119                  )
120  
121              if response.status_code == 402 and enable_proxies:
122                  proxies_fallback = True
123                  logger.warning(
124                      "Proxies unavailable (402), retrying without proxies. "
125                      "Bot detection may be less effective."
126                  )
127                  session_config.pop("proxies", None)
128                  response = requests.post(
129                      f"{config['base_url']}/v1/sessions",
130                      headers=headers,
131                      json=session_config,
132                      timeout=30,
133                  )
134  
135          if not response.ok:
136              raise RuntimeError(
137                  f"Failed to create Browserbase session: "
138                  f"{response.status_code} {response.text}"
139              )
140  
141          session_data = response.json()
142          session_name = f"hermes_{task_id}_{uuid.uuid4().hex[:8]}"
143  
144          if enable_proxies and not proxies_fallback:
145              features_enabled["proxies"] = True
146          if enable_advanced_stealth:
147              features_enabled["advanced_stealth"] = True
148          if enable_keep_alive and not keepalive_fallback:
149              features_enabled["keep_alive"] = True
150          if custom_timeout_ms and "timeout" in session_config:
151              features_enabled["custom_timeout"] = True
152  
153          feature_str = ", ".join(k for k, v in features_enabled.items() if v)
154          logger.info("Created Browserbase session %s with features: %s", session_name, feature_str)
155  
156          return {
157              "session_name": session_name,
158              "bb_session_id": session_data["id"],
159              "cdp_url": session_data["connectUrl"],
160              "features": features_enabled,
161          }
162  
163      def close_session(self, session_id: str) -> bool:
164          try:
165              config = self._get_config()
166          except ValueError:
167              logger.warning("Cannot close Browserbase session %s — missing credentials", session_id)
168              return False
169  
170          try:
171              response = requests.post(
172                  f"{config['base_url']}/v1/sessions/{session_id}",
173                  headers={
174                      "X-BB-API-Key": config["api_key"],
175                      "Content-Type": "application/json",
176                  },
177                  json={
178                      "projectId": config["project_id"],
179                      "status": "REQUEST_RELEASE",
180                  },
181                  timeout=10,
182              )
183              if response.status_code in (200, 201, 204):
184                  logger.debug("Successfully closed Browserbase session %s", session_id)
185                  return True
186              else:
187                  logger.warning(
188                      "Failed to close session %s: HTTP %s - %s",
189                      session_id,
190                      response.status_code,
191                      response.text[:200],
192                  )
193                  return False
194          except Exception as e:
195              logger.error("Exception closing Browserbase session %s: %s", session_id, e)
196              return False
197  
198      def emergency_cleanup(self, session_id: str) -> None:
199          config = self._get_config_or_none()
200          if config is None:
201              logger.warning("Cannot emergency-cleanup Browserbase session %s — missing credentials", session_id)
202              return
203          try:
204              requests.post(
205                  f"{config['base_url']}/v1/sessions/{session_id}",
206                  headers={
207                      "X-BB-API-Key": config["api_key"],
208                      "Content-Type": "application/json",
209                  },
210                  json={
211                      "projectId": config["project_id"],
212                      "status": "REQUEST_RELEASE",
213                  },
214                  timeout=5,
215              )
216          except Exception as e:
217              logger.debug("Emergency cleanup failed for Browserbase session %s: %s", session_id, e)