Skip to content

Agent Zero AI framework

playwright headless fix

frdelaadc111b3121
diff --git a/docker/run/fs/ins/install_playwright.sh b/docker/run/fs/ins/install_playwright.sh
index 2c46dc1b0..3c9b9eb1d 100644
--- a/docker/run/fs/ins/install_playwright.sh
+++ b/docker/run/fs/ins/install_playwright.sh
@@ -6,11 +6,14 @@
# install playwright if not installed (should be from requirements.txt)
uv pip install playwright
+# set PW installation path to /a0/tmp/playwright
+export PLAYWRIGHT_BROWSERS_PATH=/a0/tmp/playwright
+
# install chromium with dependencies
# for kali-based
# if [ "$@" = "hacking" ]; then
apt-get install -y fonts-unifont libnss3 libnspr4 libatk1.0-0 libatspi2.0-0 libxcomposite1 libxdamage1 libatk-bridge2.0-0 libcups2
- playwright install chromium-headless-shell
+ playwright install chromium --only-shell
# else
# # for debian based
# playwright install --with-deps chromium
diff --git a/python/helpers/browser.py b/python/helpers/browser.py
index 6f71e5c7b..c560f5c86 100644
--- a/python/helpers/browser.py
+++ b/python/helpers/browser.py
@@ -1,385 +1,385 @@
-import asyncio
-import re
-from bs4 import BeautifulSoup
-from playwright.async_api import (
- async_playwright,
- Browser as PlaywrightBrowser,
- Page,
- Frame,
- BrowserContext,
-)
-
-from python.helpers import files
-
-
-class NoPageError(Exception):
- pass
-
-
-class Browser:
-
- load_timeout = 10000
- interact_timeout = 3000
- selector_name = "data-a0sel3ct0r"
-
- def __init__(self, headless=True):
- self.browser: PlaywrightBrowser = None # type: ignore
- self.context: BrowserContext = None # type: ignore
- self.page: Page = None # type: ignore
- self._playwright = None
- self.headless = headless
- self.contexts = {}
- self.last_selector = ""
- self.page_loaded = False
- self.navigation_count = 0
-
- async def __aenter__(self):
- await self.start()
- return self
-
- async def __aexit__(self, exc_type, exc_val, exc_tb):
- await self.close()
-
- async def start(self):
- """Start browser session"""
- self._playwright = await async_playwright().start()
- if not self.browser:
- self.browser = await self._playwright.chromium.launch(
- headless=self.headless, args=["--disable-http2"]
- )
- if not self.context:
- self.context = await self.browser.new_context(
- user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.6422.141 Safari/537.36"
- )
-
- self.page = await self.context.new_page()
- await self.page.set_viewport_size({"width": 1200, "height": 1200})
-
- # Inject the JavaScript to modify the attachShadow method
- js_override = files.read_file("lib/browser/init_override.js")
- await self.page.add_init_script(js_override)
-
- # Setup frame handling
- async def inject_script_into_frames(frame):
- try:
- await self.wait_tick()
- if not frame.is_detached():
- async with asyncio.timeout(0.25):
- await frame.evaluate(js_override)
- print(f"Injected script into frame: {frame.url[:100]}")
- except Exception as e:
- # Frame might have been detached during injection, which is normal
- print(
- f"Could not inject into frame (possibly detached): {str(e)[:100]}"
- )
-
- self.page.on(
- "frameattached",
- lambda frame: asyncio.ensure_future(inject_script_into_frames(frame)),
- )
-
- # Handle page navigation events
- async def handle_navigation(frame):
- if frame == self.page.main_frame:
- print(f"Page navigated to: {frame.url[:100]}")
- self.page_loaded = False
- self.navigation_count += 1
-
- async def handle_load(dummy):
- print("Page load completed")
- self.page_loaded = True
-
- async def handle_request(request):
- if (
- request.is_navigation_request()
- and request.frame == self.page.main_frame
- ):
- print(f"Navigation started to: {request.url[:100]}")
- self.page_loaded = False
- self.navigation_count += 1
-
- self.page.on("request", handle_request)
- self.page.on("framenavigated", handle_navigation)
- self.page.on("load", handle_load)
-
- async def close(self):
- """Close browser session"""
- if self.browser:
- await self.browser.close()
- if self._playwright:
- await self._playwright.stop()
-
- async def open(self, url: str):
- """Open a URL in the browser"""
- self.last_selector = ""
- self.contexts = {}
- if self.page:
- await self.page.close()
- await self.start()
- try:
- await self.page.goto(
- url, wait_until="networkidle", timeout=Browser.load_timeout
- )
- except TimeoutError as e:
- pass
- except Exception as e:
- print(f"Error opening page: {e}")
- raise e
- await self.wait_tick()
-
- async def get_full_dom(self) -> str:
- """Get full DOM with unique selectors"""
- await self._check_page()
- js_code = files.read_file("lib/browser/extract_dom.js")
-
- # Get all frames
- self.contexts = {}
- frame_contents = {}
-
- # Extract content from each frame
- i = -1
- for frame in self.page.frames:
- try:
- if frame.url: # and frame != self.page.main_frame:
- i += 1
- frame_mark = self._num_to_alpha(i)
-
- # Check if frame is still valid
- await self.wait_tick()
- if not frame.is_detached():
- try:
- # short timeout to identify and skip unresponsive frames
- async with asyncio.timeout(0.25):
- await frame.evaluate("window.location.href")
- except TimeoutError as e:
- print(f"Skipping unresponsive frame: {frame.url}")
- continue
-
- await frame.wait_for_load_state(
- "domcontentloaded", timeout=1000
- )
-
- async with asyncio.timeout(1):
- content = await frame.evaluate(
- js_code, [frame_mark, self.selector_name]
- )
- self.contexts[frame_mark] = frame
- frame_contents[frame.url] = content
- else:
- print(f"Warning: Frame was detached: {frame.url}")
- except Exception as e:
- print(f"Error extracting from frame {frame.url}: {e}")
-
- # # Get main frame content
- # main_mark = self._num_to_alpha(0)
- # main_content = ""
- # try:
- # async with asyncio.timeout(1):
- # main_content = await self.page.evaluate(js_code, [main_mark, self.selector_name])
- # self.contexts[main_mark] = self.page
- # except Exception as e:
- # print(f"Error when extracting from main frame: {e}")
-
- # Replace iframe placeholders with actual content
- # for url, content in frame_contents.items():
- # placeholder = f'<iframe src="{url}"'
- # main_content = main_content.replace(placeholder, f'{placeholder}>\n<!-- IFrame Content Start -->\n{content}\n<!-- IFrame Content End -->\n</iframe')
-
- # return main_content + "".join(frame_contents.values())
- return "".join(frame_contents.values())
-
- def strip_html_dom(self, html_content: str) -> str:
- """Clean and strip HTML content"""
- if not html_content:
- return ""
-
- soup = BeautifulSoup(html_content, "html.parser")
-
- for tag in soup.find_all(
- ["br", "hr", "style", "script", "noscript", "meta", "link", "svg"]
- ):
- tag.decompose()
-
- for tag in soup.find_all(True):
- if tag.attrs and "invisible" in tag.attrs:
- tag.decompose()
-
- for tag in soup.find_all(True):
- allowed_attrs = [
- self.selector_name,
- "aria-label",
- "placeholder",
- "name",
- "value",
- "type",
- ]
- attrs = {
- "selector" if key == self.selector_name else key: tag.attrs[key]
- for key in allowed_attrs
- if key in tag.attrs and tag.attrs[key]
- }
- tag.attrs = attrs
-
- def remove_empty(tag_name: str) -> None:
- for tag in soup.find_all(tag_name):
- if not tag.attrs:
- tag.unwrap()
-
- remove_empty("span")
- remove_empty("p")
- remove_empty("strong")
-
- return soup.prettify(formatter="minimal")
-
- def process_html_with_selectors(self, html_content: str) -> str:
- """Process HTML content and add selectors to interactive elements"""
- if not html_content:
- return ""
-
- html_content = re.sub(r"\s+", " ", html_content)
- soup = BeautifulSoup(html_content, "html.parser")
-
- structural_tags = [
- "html",
- "head",
- "body",
- "div",
- "span",
- "section",
- "main",
- "article",
- "header",
- "footer",
- "nav",
- "ul",
- "ol",
- "li",
- "tr",
- "td",
- "th",
- ]
- for tag in structural_tags:
- for element in soup.find_all(tag):
- element.unwrap()
-
- out = str(soup).strip()
- out = re.sub(r">\s*<", "><", out)
- out = re.sub(r'aria-label="', 'label="', out)
-
- # out = re.sub(r'selector="(\d+[a-zA-Z]+)"', r'selector=\1', out)
- return out
-
- async def get_clean_dom(self) -> str:
- """Get clean DOM with selectors"""
- full_dom = await self.get_full_dom()
- clean_dom = self.strip_html_dom(full_dom)
- return self.process_html_with_selectors(clean_dom)
-
- async def click(self, selector: str):
- await self._check_page()
- ctx, selector = self._parse_selector(selector)
- self.last_selector = selector
- # js_code = files.read_file("lib/browser/click.js")
- # result = await self.page.evaluate(js_code, [selector])
- # if not result:
- result = await ctx.hover(selector, force=True, timeout=Browser.interact_timeout)
- await self.wait_tick()
- result = await ctx.click(selector, force=True, timeout=Browser.interact_timeout)
- await self.wait_tick()
-
- # await self.page.wait_for_load_state("networkidle")
- return result
-
- async def press(self, key: str):
- await self._check_page()
- if self.last_selector:
- await self.page.press(
- self.last_selector, key, timeout=Browser.interact_timeout
- )
- else:
- await self.page.keyboard.press(key)
-
- async def fill(self, selector: str, text: str):
- await self._check_page()
- ctx, selector = self._parse_selector(selector)
- self.last_selector = selector
- try:
- await self.click(selector)
- except Exception as e:
- pass
- await ctx.fill(selector, text, force=True, timeout=Browser.interact_timeout)
- await self.wait_tick()
-
- async def execute(self, js_code: str):
- await self._check_page()
- result = await self.page.evaluate(js_code)
- return result
-
- async def screenshot(self, path: str, full_page=False):
- await self._check_page()
- await self.page.screenshot(path=path, full_page=full_page)
-
- def _parse_selector(self, selector: str) -> tuple[Page | Frame, str]:
- try:
- ctx = self.page
- # Check if selector is our UID, return
- if re.match(r"^\d+[a-zA-Z]+$", selector):
- alpha_part = "".join(filter(str.isalpha, selector))
- ctx = self.contexts[alpha_part]
- selector = f"[{self.selector_name}='{selector}']"
- return (ctx, selector)
- except Exception as e:
- raise Exception(f"Error evaluating selector: {selector}")
-
- async def _check_page(self):
- for _ in range(2):
- try:
- await self.wait_tick()
- self.page = self.context.pages[0]
- if not self.page:
- raise NoPageError(
- "No page is open in the browser. Please open a URL first."
- )
- # await self.page.wait_for_load_state("networkidle",)
- async with asyncio.timeout(self.load_timeout / 1000):
- if not self.page_loaded:
- while not self.page_loaded:
- await asyncio.sleep(0.1)
- await self.wait_tick()
- return
- except TimeoutError as e:
- self.page_loaded = True
- return
- except NoPageError as e:
- raise e
- except Exception as e:
- print(f"Error checking page: {e}")
-
- def _num_to_alpha(self, num: int) -> str:
- if num < 0:
- return ""
-
- result = ""
- while num >= 0:
- result = chr(num % 26 + 97) + result
- num = num // 26 - 1
-
- return result
-
- async def wait_tick(self):
- if self.page:
- await self.page.evaluate("window.location.href")
-
- async def wait(self, seconds: float = 1.0):
- await asyncio.sleep(seconds)
- await self.wait_tick()
-
- async def wait_for_action(self):
- nav_count = self.navigation_count
- for _ in range(5):
- await self._check_page()
- if nav_count != self.navigation_count:
- print("Navigation detected")
- await asyncio.sleep(1)
- return
- await asyncio.sleep(0.1)
+# import asyncio
+# import re
+# from bs4 import BeautifulSoup
+# from playwright.async_api import (
+# async_playwright,
+# Browser as PlaywrightBrowser,
+# Page,
+# Frame,
+# BrowserContext,
+# )
+
+# from python.helpers import files
+
+
+# class NoPageError(Exception):
+# pass
+
+
+# class Browser:
+
+# load_timeout = 10000
+# interact_timeout = 3000
+# selector_name = "data-a0sel3ct0r"
+
+# def __init__(self, headless=True):
+# self.browser: PlaywrightBrowser = None # type: ignore
+# self.context: BrowserContext = None # type: ignore
+# self.page: Page = None # type: ignore
+# self._playwright = None
+# self.headless = headless
+# self.contexts = {}
+# self.last_selector = ""
+# self.page_loaded = False
+# self.navigation_count = 0
+
+# async def __aenter__(self):
+# await self.start()
+# return self
+
+# async def __aexit__(self, exc_type, exc_val, exc_tb):
+# await self.close()
+
+# async def start(self):
+# """Start browser session"""
+# self._playwright = await async_playwright().start()
+# if not self.browser:
+# self.browser = await self._playwright.chromium.launch(
+# headless=self.headless, args=["--disable-http2"]
+# )
+# if not self.context:
+# self.context = await self.browser.new_context(
+# user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.6422.141 Safari/537.36"
+# )
+
+# self.page = await self.context.new_page()
+# await self.page.set_viewport_size({"width": 1200, "height": 1200})
+
+# # Inject the JavaScript to modify the attachShadow method
+# js_override = files.read_file("lib/browser/init_override.js")
+# await self.page.add_init_script(js_override)
+
+# # Setup frame handling
+# async def inject_script_into_frames(frame):
+# try:
+# await self.wait_tick()
+# if not frame.is_detached():
+# async with asyncio.timeout(0.25):
+# await frame.evaluate(js_override)
+# print(f"Injected script into frame: {frame.url[:100]}")
+# except Exception as e:
+# # Frame might have been detached during injection, which is normal
+# print(
+# f"Could not inject into frame (possibly detached): {str(e)[:100]}"
+# )
+
+# self.page.on(
+# "frameattached",
+# lambda frame: asyncio.ensure_future(inject_script_into_frames(frame)),
+# )
+
+# # Handle page navigation events
+# async def handle_navigation(frame):
+# if frame == self.page.main_frame:
+# print(f"Page navigated to: {frame.url[:100]}")
+# self.page_loaded = False
+# self.navigation_count += 1
+
+# async def handle_load(dummy):
+# print("Page load completed")
+# self.page_loaded = True
+
+# async def handle_request(request):
+# if (
+# request.is_navigation_request()
+# and request.frame == self.page.main_frame
+# ):
+# print(f"Navigation started to: {request.url[:100]}")
+# self.page_loaded = False
+# self.navigation_count += 1
+
+# self.page.on("request", handle_request)
+# self.page.on("framenavigated", handle_navigation)
+# self.page.on("load", handle_load)
+
+# async def close(self):
+# """Close browser session"""
+# if self.browser:
+# await self.browser.close()
+# if self._playwright:
+# await self._playwright.stop()
+
+# async def open(self, url: str):
+# """Open a URL in the browser"""
+# self.last_selector = ""
+# self.contexts = {}
+# if self.page:
+# await self.page.close()
+# await self.start()
+# try:
+# await self.page.goto(
+# url, wait_until="networkidle", timeout=Browser.load_timeout
+# )
+# except TimeoutError as e:
+# pass
+# except Exception as e:
+# print(f"Error opening page: {e}")
+# raise e
+# await self.wait_tick()
+
+# async def get_full_dom(self) -> str:
+# """Get full DOM with unique selectors"""
+# await self._check_page()
+# js_code = files.read_file("lib/browser/extract_dom.js")
+
+# # Get all frames
+# self.contexts = {}
+# frame_contents = {}
+
+# # Extract content from each frame
+# i = -1
+# for frame in self.page.frames:
+# try:
+# if frame.url: # and frame != self.page.main_frame:
+# i += 1
+# frame_mark = self._num_to_alpha(i)
+
+# # Check if frame is still valid
+# await self.wait_tick()
+# if not frame.is_detached():
+# try:
+# # short timeout to identify and skip unresponsive frames
+# async with asyncio.timeout(0.25):
+# await frame.evaluate("window.location.href")
+# except TimeoutError as e:
+# print(f"Skipping unresponsive frame: {frame.url}")
+# continue
+
+# await frame.wait_for_load_state(
+# "domcontentloaded", timeout=1000
+# )
+
+# async with asyncio.timeout(1):
+# content = await frame.evaluate(
+# js_code, [frame_mark, self.selector_name]
+# )
+# self.contexts[frame_mark] = frame
+# frame_contents[frame.url] = content
+# else:
+# print(f"Warning: Frame was detached: {frame.url}")
+# except Exception as e:
+# print(f"Error extracting from frame {frame.url}: {e}")
+
+# # # Get main frame content
+# # main_mark = self._num_to_alpha(0)
+# # main_content = ""
+# # try:
+# # async with asyncio.timeout(1):
+# # main_content = await self.page.evaluate(js_code, [main_mark, self.selector_name])
+# # self.contexts[main_mark] = self.page
+# # except Exception as e:
+# # print(f"Error when extracting from main frame: {e}")
+
+# # Replace iframe placeholders with actual content
+# # for url, content in frame_contents.items():
+# # placeholder = f'<iframe src="{url}"'
+# # main_content = main_content.replace(placeholder, f'{placeholder}>\n<!-- IFrame Content Start -->\n{content}\n<!-- IFrame Content End -->\n</iframe')
+
+# # return main_content + "".join(frame_contents.values())
+# return "".join(frame_contents.values())
+
+# def strip_html_dom(self, html_content: str) -> str:
+# """Clean and strip HTML content"""
+# if not html_content:
+# return ""
+
+# soup = BeautifulSoup(html_content, "html.parser")
+
+# for tag in soup.find_all(
+# ["br", "hr", "style", "script", "noscript", "meta", "link", "svg"]
+# ):
+# tag.decompose()
+
+# for tag in soup.find_all(True):
+# if tag.attrs and "invisible" in tag.attrs:
+# tag.decompose()
+
+# for tag in soup.find_all(True):
+# allowed_attrs = [
+# self.selector_name,
+# "aria-label",
+# "placeholder",
+# "name",
+# "value",
+# "type",
+# ]
+# attrs = {
+# "selector" if key == self.selector_name else key: tag.attrs[key]
+# for key in allowed_attrs
+# if key in tag.attrs and tag.attrs[key]
+# }
+# tag.attrs = attrs
+
+# def remove_empty(tag_name: str) -> None:
+# for tag in soup.find_all(tag_name):
+# if not tag.attrs:
+# tag.unwrap()
+
+# remove_empty("span")
+# remove_empty("p")
+# remove_empty("strong")
+
+# return soup.prettify(formatter="minimal")
+
+# def process_html_with_selectors(self, html_content: str) -> str:
+# """Process HTML content and add selectors to interactive elements"""
+# if not html_content:
+# return ""
+
+# html_content = re.sub(r"\s+", " ", html_content)
+# soup = BeautifulSoup(html_content, "html.parser")
+
+# structural_tags = [
+# "html",
+# "head",
+# "body",
+# "div",
+# "span",
+# "section",
+# "main",
+# "article",
+# "header",
+# "footer",
+# "nav",
+# "ul",
+# "ol",
+# "li",
+# "tr",
+# "td",
+# "th",
+# ]
+# for tag in structural_tags:
+# for element in soup.find_all(tag):
+# element.unwrap()
+
+# out = str(soup).strip()
+# out = re.sub(r">\s*<", "><", out)
+# out = re.sub(r'aria-label="', 'label="', out)
+
+# # out = re.sub(r'selector="(\d+[a-zA-Z]+)"', r'selector=\1', out)
+# return out
+
+# async def get_clean_dom(self) -> str:
+# """Get clean DOM with selectors"""
+# full_dom = await self.get_full_dom()
+# clean_dom = self.strip_html_dom(full_dom)
+# return self.process_html_with_selectors(clean_dom)
+
+# async def click(self, selector: str):
+# await self._check_page()
+# ctx, selector = self._parse_selector(selector)
+# self.last_selector = selector
+# # js_code = files.read_file("lib/browser/click.js")
+# # result = await self.page.evaluate(js_code, [selector])
+# # if not result:
+# result = await ctx.hover(selector, force=True, timeout=Browser.interact_timeout)
+# await self.wait_tick()
+# result = await ctx.click(selector, force=True, timeout=Browser.interact_timeout)
+# await self.wait_tick()
+
+# # await self.page.wait_for_load_state("networkidle")
+# return result
+
+# async def press(self, key: str):
+# await self._check_page()
+# if self.last_selector:
+# await self.page.press(
+# self.last_selector, key, timeout=Browser.interact_timeout
+# )
+# else:
+# await self.page.keyboard.press(key)
+
+# async def fill(self, selector: str, text: str):
+# await self._check_page()
+# ctx, selector = self._parse_selector(selector)
+# self.last_selector = selector
+# try:
+# await self.click(selector)
+# except Exception as e:
+# pass
+# await ctx.fill(selector, text, force=True, timeout=Browser.interact_timeout)
+# await self.wait_tick()
+
+# async def execute(self, js_code: str):
+# await self._check_page()
+# result = await self.page.evaluate(js_code)
+# return result
+
+# async def screenshot(self, path: str, full_page=False):
+# await self._check_page()
+# await self.page.screenshot(path=path, full_page=full_page)
+
+# def _parse_selector(self, selector: str) -> tuple[Page | Frame, str]:
+# try:
+# ctx = self.page
+# # Check if selector is our UID, return
+# if re.match(r"^\d+[a-zA-Z]+$", selector):
+# alpha_part = "".join(filter(str.isalpha, selector))
+# ctx = self.contexts[alpha_part]
+# selector = f"[{self.selector_name}='{selector}']"
+# return (ctx, selector)
+# except Exception as e:
+# raise Exception(f"Error evaluating selector: {selector}")
+
+# async def _check_page(self):
+# for _ in range(2):
+# try:
+# await self.wait_tick()
+# self.page = self.context.pages[0]
+# if not self.page:
+# raise NoPageError(
+# "No page is open in the browser. Please open a URL first."
+# )
+# # await self.page.wait_for_load_state("networkidle",)
+# async with asyncio.timeout(self.load_timeout / 1000):
+# if not self.page_loaded:
+# while not self.page_loaded:
+# await asyncio.sleep(0.1)
+# await self.wait_tick()
+# return
+# except TimeoutError as e:
+# self.page_loaded = True
+# return
+# except NoPageError as e:
+# raise e
+# except Exception as e:
+# print(f"Error checking page: {e}")
+
+# def _num_to_alpha(self, num: int) -> str:
+# if num < 0:
+# return ""
+
+# result = ""
+# while num >= 0:
+# result = chr(num % 26 + 97) + result
+# num = num // 26 - 1
+
+# return result
+
+# async def wait_tick(self):
+# if self.page:
+# await self.page.evaluate("window.location.href")
+
+# async def wait(self, seconds: float = 1.0):
+# await asyncio.sleep(seconds)
+# await self.wait_tick()
+
+# async def wait_for_action(self):
+# nav_count = self.navigation_count
+# for _ in range(5):
+# await self._check_page()
+# if nav_count != self.navigation_count:
+# print("Navigation detected")
+# await asyncio.sleep(1)
+# return
+# await asyncio.sleep(0.1)
diff --git a/python/helpers/playwright.py b/python/helpers/playwright.py
new file mode 100644
index 000000000..34f851ab6
--- /dev/null
+++ b/python/helpers/playwright.py
@@ -0,0 +1,32 @@
+
+from pathlib import Path
+import subprocess
+from python.helpers import files
+
+
+# this helper ensures that playwright is installed in /lib/playwright
+# should work for both docker and local installation
+
+def get_playwright_binary():
+ pw_cache = Path(get_playwright_cache_dir())
+ headless_shell = next(pw_cache.glob("chromium_headless_shell-*/chrome-*/headless_shell"), None)
+ return headless_shell
+
+def get_playwright_cache_dir():
+ return files.get_abs_path("tmp/playwright")
+
+def ensure_playwright_binary():
+ bin = get_playwright_binary()
+ if not bin:
+ cache = get_playwright_cache_dir()
+ import os
+ env = os.environ.copy()
+ env["PLAYWRIGHT_BROWSERS_PATH"] = cache
+ subprocess.check_call(
+ ["playwright", "install", "chromium", "--only-shell"],
+ env=env
+ )
+ bin = get_playwright_binary()
+ if not bin:
+ raise Exception("Playwright binary not found after installation")
+ return bin
\ No newline at end of file
diff --git a/python/tools/browser_agent.py b/python/tools/browser_agent.py
index 7960cfae9..a009bc467 100644
--- a/python/tools/browser_agent.py
+++ b/python/tools/browser_agent.py
@@ -3,13 +3,15 @@ import json
import time
from typing import Optional
from agent import Agent, InterventionException
+from pathlib import Path
+
import models
from python.helpers.tool import Tool, Response
from python.helpers import files, defer, persist_chat, strings
from python.helpers.browser_use import browser_use
from python.helpers.print_style import PrintStyle
-
+from python.helpers.playwright import ensure_playwright_binary
from python.extensions.message_loop_start._10_iteration_no import get_iter_no
from pydantic import BaseModel
import uuid
@@ -36,19 +38,23 @@ class State:
if self.browser_session:
return
+ # for some reason we need to provide exact path to headless shell, otherwise it looks for headed browser
+ pw_binary = ensure_playwright_binary()
+
self.browser_session = browser_use.BrowserSession(
browser_profile=browser_use.BrowserProfile(
headless=True,
disable_security=True,
chromium_sandbox=False,
accept_downloads=True,
+ executable_path=pw_binary,
keep_alive=True,
minimum_wait_page_load_time=1.0,
wait_for_network_idle_page_load_time=2.0,
maximum_wait_page_load_time=10.0,
- screen={'width': 1024, 'height': 1024},
- viewport={'width': 1024, 'height': 1024},
- args=['--headless=new'],
+ screen={"width": 1024, "height": 1024},
+ viewport={"width": 1024, "height": 1024},
+ args=["--headless=new"],
)
)
@@ -79,6 +85,7 @@ class State:
if self.browser_session:
try:
import asyncio
+
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(self.browser_session.close())
@@ -105,9 +112,7 @@ class State:
@controller.registry.action("Complete task", param_model=DoneResult)
async def complete_task(params: DoneResult):
result = browser_use.ActionResult(
- is_done=True,
- success=True,
- extracted_content=params.model_dump_json()
+ is_done=True, success=True, extracted_content=params.model_dump_json()
)
return result
@@ -123,9 +128,12 @@ class State:
browser_session=self.browser_session,
llm=model,
use_vision=self.agent.config.browser_model.vision,
- extend_system_message=self.agent.read_prompt("prompts/browser_agent.system.md"),
+ extend_system_message=self.agent.read_prompt(
+ "prompts/browser_agent.system.md"
+ ),
controller=controller,
enable_memory=False, # Disable memory to avoid state conflicts
+ # available_file_paths=[],
)
self.iter_no = get_iter_no(self.agent)
@@ -150,10 +158,13 @@ class State:
if self.iter_no != get_iter_no(self.agent):
raise InterventionException("Task cancelled")
return await func(*args, **kwargs)
+
return wrapper
- if self.browser_session and hasattr(self.browser_session, 'remove_highlights'):
- self.browser_session.remove_highlights = override_hook(self.browser_session.remove_highlights)
+ if self.browser_session and hasattr(self.browser_session, "remove_highlights"):
+ self.browser_session.remove_highlights = override_hook(
+ self.browser_session.remove_highlights
+ )
async def get_page(self):
if self.use_agent and self.browser_session:
@@ -167,7 +178,9 @@ class State:
async def get_selector_map(self):
"""Get the selector map for the current page state."""
if self.use_agent:
- await self.use_agent.browser_session.get_state_summary(cache_clickable_elements_hashes=True)
+ await self.use_agent.browser_session.get_state_summary(
+ cache_clickable_elements_hashes=True
+ )
return await self.use_agent.browser_session.get_selector_map()
return {}
@@ -187,14 +200,16 @@ class BrowserAgent(Tool):
while not task.is_ready():
# Check for timeout to prevent infinite waiting
if time.time() - start_time > timeout_seconds:
- PrintStyle().warning(f"Browser agent task timeout after {timeout_seconds} seconds, forcing completion")
+ PrintStyle().warning(
+ f"Browser agent task timeout after {timeout_seconds} seconds, forcing completion"
+ )
self.state.kill_task()
break
await self.agent.handle_intervention()
await asyncio.sleep(1)
- try:
- if task.is_ready(): # otherwise get_update hangs
+ try:
+ if task.is_ready(): # otherwise get_update hangs
break
update = await self.get_update()
log = update.get("log")
@@ -228,15 +243,23 @@ class BrowserAgent(Tool):
answer_data = DirtyJson.parse_string(answer)
answer_text = strings.dict_to_text(answer_data) # type: ignore
else:
- answer_text = str(answer) if answer else "Task completed successfully"
+ answer_text = (
+ str(answer) if answer else "Task completed successfully"
+ )
except Exception as e:
- answer_text = str(answer) if answer else f"Task completed with parse error: {str(e)}"
+ answer_text = (
+ str(answer)
+ if answer
+ else f"Task completed with parse error: {str(e)}"
+ )
else:
# Task hit max_steps without calling done()
urls = result.urls()
current_url = urls[-1] if urls else "unknown"
- answer_text = (f"Task reached step limit without completion. Last page: {current_url}. "
- f"The browser agent may need clearer instructions on when to finish.")
+ answer_text = (
+ f"Task reached step limit without completion. Last page: {current_url}. "
+ f"The browser agent may need clearer instructions on when to finish."
+ )
self.log.update(answer=answer_text)
return Response(message=answer_text, break_loop=False)
@@ -265,7 +288,6 @@ class BrowserAgent(Tool):
await agent.wait_if_paused()
log = []
-
# for message in ua.message_manager.get_messages():
# if message.type == "system":
@@ -291,9 +313,11 @@ class BrowserAgent(Tool):
# for res in hist.result:
# log.append(res.extracted_content)
log = ua.state.history.extracted_content()
-
-
- result["log"] = log
+ short_log = []
+ for item in log:
+ first_line = str(item).split("\n", 1)[0][:200]
+ short_log.append(first_line)
+ result["log"] = short_log
path = files.get_abs_path(
persist_chat.get_chat_folder_path(agent.context.id),
diff --git a/requirements.txt b/requirements.txt
index 1b1de90ea..760ca4373 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,6 +1,5 @@
a2wsgi==1.10.8
ansio==0.0.1
-beautifulsoup4==4.13.4
browser-use==0.2.5
docker==7.1.0
duckduckgo-search==6.1.12