|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import json |
| 5 | +import os |
| 6 | +import re |
| 7 | +from typing import Any |
| 8 | + |
| 9 | +from browserbase import Browserbase |
| 10 | +from bs4 import BeautifulSoup |
| 11 | +from langchain.tools import tool |
| 12 | +from stagehand import AsyncStagehand |
| 13 | + |
| 14 | +# Using the Browserbase Model Gateway, you only need to pass your Browserbase API key to use frontier models |
| 15 | +# Docs: https://docs.browserbase.com/platform/model-gateway/overview |
| 16 | + |
| 17 | +DEFAULT_STAGEHAND_MODEL = os.getenv( |
| 18 | + "STAGEHAND_MODEL", |
| 19 | + "google/gemini-3-flash-preview", |
| 20 | +) |
| 21 | +DEFAULT_STAGEHAND_AGENT_MODEL = os.getenv( |
| 22 | + "STAGEHAND_AGENT_MODEL", |
| 23 | + "anthropic/claude-sonnet-4-6", |
| 24 | +) |
| 25 | + |
| 26 | + |
| 27 | +def _require_env(name: str) -> str: |
| 28 | + value = os.getenv(name, "").strip() |
| 29 | + if not value: |
| 30 | + raise ValueError(f"Missing required environment variable: {name}") |
| 31 | + return value |
| 32 | + |
| 33 | + |
| 34 | +def _browserbase_client() -> Browserbase: |
| 35 | + return Browserbase(api_key=_require_env("BROWSERBASE_API_KEY")) |
| 36 | + |
| 37 | + |
| 38 | +def _normalize(value: Any) -> Any: |
| 39 | + if value is None or isinstance(value, (str, int, float, bool)): |
| 40 | + return value |
| 41 | + if isinstance(value, dict): |
| 42 | + return {str(key): _normalize(val) for key, val in value.items()} |
| 43 | + if isinstance(value, (list, tuple, set)): |
| 44 | + return [_normalize(item) for item in value] |
| 45 | + if hasattr(value, "model_dump"): |
| 46 | + return _normalize(value.model_dump()) |
| 47 | + if hasattr(value, "dict"): |
| 48 | + return _normalize(value.dict()) |
| 49 | + if hasattr(value, "__dict__"): |
| 50 | + public = { |
| 51 | + key: val |
| 52 | + for key, val in vars(value).items() |
| 53 | + if not key.startswith("_") and not callable(val) |
| 54 | + } |
| 55 | + if public: |
| 56 | + return _normalize(public) |
| 57 | + return str(value) |
| 58 | + |
| 59 | + |
| 60 | +def _json(value: Any) -> str: |
| 61 | + return json.dumps(_normalize(value), indent=2, default=str) |
| 62 | + |
| 63 | + |
| 64 | +def _html_to_text(html: str, max_chars: int) -> tuple[str, str]: |
| 65 | + soup = BeautifulSoup(html, "html.parser") |
| 66 | + title = soup.title.get_text(" ", strip=True) if soup.title else "" |
| 67 | + for tag in soup(["script", "style", "noscript"]): |
| 68 | + tag.decompose() |
| 69 | + body = soup.body or soup |
| 70 | + text = body.get_text("\n", strip=True) |
| 71 | + text = re.sub(r"\n{3,}", "\n\n", text) |
| 72 | + return title, text[:max_chars] |
| 73 | + |
| 74 | + |
| 75 | +def _stagehand_client() -> AsyncStagehand: |
| 76 | + return AsyncStagehand( |
| 77 | + browserbase_api_key=_require_env("BROWSERBASE_API_KEY"), |
| 78 | + ) |
| 79 | + |
| 80 | + |
| 81 | +def _run_async(coro: Any) -> Any: |
| 82 | + return asyncio.run(coro) |
| 83 | + |
| 84 | + |
| 85 | +@tool |
| 86 | +def browserbase_search(query: str, num_results: int = 5) -> str: |
| 87 | + """Search the web with Browserbase. Use this first for discovery before opening pages.""" |
| 88 | + bb = _browserbase_client() |
| 89 | + response = bb.search.web(query=query, num_results=max(1, min(num_results, 10))) |
| 90 | + results = [] |
| 91 | + for result in getattr(response, "results", []): |
| 92 | + results.append( |
| 93 | + { |
| 94 | + "title": getattr(result, "title", ""), |
| 95 | + "url": getattr(result, "url", ""), |
| 96 | + "author": getattr(result, "author", None), |
| 97 | + "published_date": ( |
| 98 | + getattr(result, "published_date", None) |
| 99 | + or getattr(result, "publishedDate", None) |
| 100 | + ), |
| 101 | + } |
| 102 | + ) |
| 103 | + return _json( |
| 104 | + { |
| 105 | + "query": query, |
| 106 | + "request_id": getattr(response, "request_id", None) |
| 107 | + or getattr(response, "requestId", None), |
| 108 | + "results": results, |
| 109 | + } |
| 110 | + ) |
| 111 | + |
| 112 | + |
| 113 | +@tool |
| 114 | +def browserbase_fetch(url: str, use_proxy: bool = False, max_chars: int = 12000) -> str: |
| 115 | + """Fetch page content without a browser session. Best for static pages and quick reads.""" |
| 116 | + bb = _browserbase_client() |
| 117 | + response = bb.fetch_api.create(url=url, proxies=use_proxy) |
| 118 | + content = getattr(response, "content", "") |
| 119 | + content_type = ( |
| 120 | + getattr(response, "content_type", None) |
| 121 | + or getattr(response, "contentType", "") |
| 122 | + or "" |
| 123 | + ).lower() |
| 124 | + |
| 125 | + title = "" |
| 126 | + text = str(content)[:max_chars] |
| 127 | + if "html" in content_type: |
| 128 | + title, text = _html_to_text(str(content), max_chars=max_chars) |
| 129 | + |
| 130 | + return _json( |
| 131 | + { |
| 132 | + "url": url, |
| 133 | + "status_code": getattr(response, "status_code", None) |
| 134 | + or getattr(response, "statusCode", None), |
| 135 | + "content_type": getattr(response, "content_type", None) |
| 136 | + or getattr(response, "contentType", None), |
| 137 | + "encoding": getattr(response, "encoding", None), |
| 138 | + "title": title, |
| 139 | + "text": text, |
| 140 | + } |
| 141 | + ) |
| 142 | + |
| 143 | + |
| 144 | +@tool |
| 145 | +def browserbase_rendered_extract(start_url: str, instruction: str) -> str: |
| 146 | + """Open a full Browserbase browser session and extract rendered content from a page with Stagehand.""" |
| 147 | + return _run_async(_browserbase_rendered_extract_async(start_url=start_url, instruction=instruction)) |
| 148 | + |
| 149 | + |
| 150 | +async def _browserbase_rendered_extract_async(start_url: str, instruction: str) -> str: |
| 151 | + client = _stagehand_client() |
| 152 | + start_resp = await client.sessions.start( |
| 153 | + model_name=DEFAULT_STAGEHAND_MODEL, |
| 154 | + ) |
| 155 | + session_id = start_resp.data.session_id |
| 156 | + |
| 157 | + try: |
| 158 | + await client.sessions.navigate( |
| 159 | + id=session_id, |
| 160 | + url=start_url, |
| 161 | + frame_id="", |
| 162 | + ) |
| 163 | + result = await client.sessions.extract( |
| 164 | + id=session_id, |
| 165 | + instruction=instruction, |
| 166 | + ) |
| 167 | + extracted = getattr(getattr(result, "data", None), "result", None) |
| 168 | + return _json( |
| 169 | + { |
| 170 | + "start_url": start_url, |
| 171 | + "session_id": session_id, |
| 172 | + "session_url": f"https://browserbase.com/sessions/{session_id}", |
| 173 | + "instruction": instruction, |
| 174 | + "result": _normalize(extracted), |
| 175 | + } |
| 176 | + ) |
| 177 | + finally: |
| 178 | + await client.sessions.end(id=session_id) |
| 179 | + |
| 180 | + |
| 181 | +@tool |
| 182 | +def browserbase_interactive_task(start_url: str, task: str) -> str: |
| 183 | + """Open a Browserbase-hosted Stagehand session and let a Stagehand agent execute a multi-step browser task.""" |
| 184 | + return _run_async(_browserbase_interactive_task_async(start_url=start_url, task=task)) |
| 185 | + |
| 186 | + |
| 187 | +async def _browserbase_interactive_task_async(start_url: str, task: str) -> str: |
| 188 | + client = _stagehand_client() |
| 189 | + start_resp = await client.sessions.start( |
| 190 | + model_name=DEFAULT_STAGEHAND_AGENT_MODEL, |
| 191 | + ) |
| 192 | + session_id = start_resp.data.session_id |
| 193 | + |
| 194 | + try: |
| 195 | + await client.sessions.navigate( |
| 196 | + id=session_id, |
| 197 | + url=start_url, |
| 198 | + frame_id="", |
| 199 | + ) |
| 200 | + result = await client.sessions.execute( |
| 201 | + id=session_id, |
| 202 | + execute_options={ |
| 203 | + "instruction": task, |
| 204 | + "max_steps": 20, |
| 205 | + }, |
| 206 | + agent_config={ |
| 207 | + "model": DEFAULT_STAGEHAND_AGENT_MODEL, |
| 208 | + "instructions": ( |
| 209 | + "You are executing a browser task on behalf of a LangChain tool. " |
| 210 | + "Be precise, avoid unnecessary actions, and stop once the requested task is complete." |
| 211 | + ), |
| 212 | + }, |
| 213 | + timeout=300.0, |
| 214 | + ) |
| 215 | + return _json(_normalize(result)) |
| 216 | + finally: |
| 217 | + await client.sessions.end(id=session_id) |
0 commit comments