Skip to content

Commit b38f9d9

Browse files
Merge branch 'bpsaog' of github.com:joaopauloschuler/beyond-python-smolagents into bpsaog
2 parents 55d8471 + aebf030 commit b38f9d9

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

src/smolagents/bp_tools_browser.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,46 @@
1414
prompt_toolkit's asyncio.run() on the main thread.
1515
"""
1616

17+
import os
1718
import queue
1819
import threading
1920

2021
from .tools import Tool
2122

2223

24+
def _check_chromium_installed():
25+
"""Check if Chromium browser is installed for Playwright.
26+
27+
Returns the path to the Chromium executable if found, None otherwise.
28+
"""
29+
# Get the Playwright cache directory
30+
cache_dir = os.path.expanduser("~/.cache/ms-playwright")
31+
32+
if not os.path.exists(cache_dir):
33+
return None
34+
35+
# Look for chromium directories (they start with 'chromium-')
36+
try:
37+
chromium_dirs = [d for d in os.listdir(cache_dir) if d.startswith('chromium-')]
38+
if not chromium_dirs:
39+
return None
40+
41+
# Check if the chrome executable exists in any of the chromium directories
42+
for chromium_dir in chromium_dirs:
43+
chrome_path = os.path.join(cache_dir, chromium_dir, 'chrome-linux', 'chrome')
44+
if os.path.exists(chrome_path):
45+
return chrome_path
46+
47+
# Also check for chrome-linux64 (newer versions)
48+
chrome_path = os.path.join(cache_dir, chromium_dir, 'chrome-linux64', 'chrome')
49+
if os.path.exists(chrome_path):
50+
return chrome_path
51+
52+
return None
53+
except Exception:
54+
return None
55+
56+
2357
class BrowserManager:
2458
"""Manages a headed Chromium browser in a dedicated thread.
2559
@@ -45,6 +79,21 @@ def _ensure_thread(self):
4579
def _run_loop(self):
4680
"""Background thread: owns Playwright, browser, and page."""
4781
from playwright.sync_api import sync_playwright
82+
83+
# Check if Chromium is installed before trying to launch
84+
chromium_path = _check_chromium_installed()
85+
if chromium_path is None:
86+
error_msg = (
87+
"Chromium browser is not installed for Playwright.\n"
88+
"Please run the following command to install it:\n"
89+
" playwright install chromium\n"
90+
"Or if you're using a virtual environment:\n"
91+
" python -m playwright install chromium"
92+
)
93+
self._res_q.put((False, RuntimeError(error_msg)))
94+
self._ready.set()
95+
return
96+
4897
pw = sync_playwright().start()
4998
browser = pw.chromium.launch(headless=False, slow_mo=300)
5099
page = browser.new_page()

0 commit comments

Comments
 (0)