Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
Merge branch 'main' into mert/new_everything
Magnus Müller0b6ebea431db
diff --cc browser_use/agent/service.pyindex 9f1074ee8,4eed78dd1..441c2a565--- a/browser_use/agent/service.py+++ b/browser_use/agent/service.py@@@ -58,23 -66,11 +68,18 @@@ from browser_use.browser.types import Bfrom browser_use.browser.views import BrowserStateSummaryfrom browser_use.controller.registry.views import ActionModelfrom browser_use.controller.service import Controller- from browser_use.dom.history_tree_processor.service import (- DOMHistoryElement,- HistoryTreeProcessor,- )+ from browser_use.dom.history_tree_processor.service import DOMHistoryElement, HistoryTreeProcessorfrom browser_use.exceptions import LLMException+from browser_use.filesystem.file_system import FileSystemfrom browser_use.telemetry.service import ProductTelemetry- from browser_use.telemetry.views import (- AgentTelemetryEvent,- )+ from browser_use.telemetry.views import AgentTelemetryEvent-from browser_use.utils import _log_pretty_path, get_browser_use_version, time_execution_async, time_execution_sync+from browser_use.utils import (+ _log_pretty_path,+ get_browser_use_version,+ handle_llm_error,+ time_execution_async,+ time_execution_sync,+)logger = logging.getLogger(__name__)@@@ -454,46 -457,9 +473,47 @@@ class Agent(Generic[Context])@propertydef browser_profile(self) -> BrowserProfile:+ assert self.browser_session is not None, 'BrowserSession is not set up'return self.browser_session.browser_profile+ def _set_file_system(self, file_system_path: str | None = None) -> None:+ # Initialize file system+ if file_system_path:+ self.file_system = FileSystem(file_system_path)+ self.file_system_path = file_system_path+ else:+ # create a temporary file system+ base_tmp = tempfile.gettempdir() # e.g., /tmp on Unix+ self.file_system_path = os.path.join(base_tmp, str(uuid.uuid4()))+ self.file_system = FileSystem(self.file_system_path)++ logger.info(f'💾 File system path: {self.file_system_path}')++ # if file system is set, add actions to the controller+ @self.controller.registry.action('Write content to file_name in file system, use only .md or .txt extensions.')+ async def write_file(file_name: str, content: str):+ result = await self.file_system.write_file(file_name, content)+ logger.info(f'💾 {result}')+ return ActionResult(extracted_content=result, include_in_memory=True, memory=result)++ @self.controller.registry.action('Append content to file_name in file system')+ async def append_file(file_name: str, content: str):+ result = await self.file_system.append_file(file_name, content)+ logger.info(f'💾 {result}')+ return ActionResult(extracted_content=result, include_in_memory=True, memory=result)++ @self.controller.registry.action('Read file_name from file system')+ async def read_file(file_name: str):+ result = await self.file_system.read_file(file_name)+ max_len = 50+ if len(result) > max_len:+ display_result = result[:max_len] + '\n...'+ else:+ display_result = result+ logger.info(f'💾 {display_result}')+ memory = result.split('\n')[-1]+ return ActionResult(extracted_content=result, include_in_memory=True, memory=memory, update_read_state=True)+def _set_message_context(self) -> str | None:if self.tool_calling_method == 'raw':# For raw tool calling, only include actions with no filters initiallydiff --cc browser_use/agent/views.pyindex c13ba740d,baf273bed..70ed75168--- a/browser_use/agent/views.py+++ b/browser_use/agent/views.py@@@ -109,10 -108,18 +109,21 @@@ class ActionResult(BaseModel)extracted_content: str | None = Noneerror: str | None = Noneinclude_in_memory: bool = False # whether to include in past messages as context or not+ memory: str | None = None # Memory of this action+ update_read_state: bool = False # Whether the extracted content should be used to update the read_state+ attachments: list[str] | None = None # Files to display in the done message+ @model_validator(mode='after')+ def validate_success_requires_done(self):+ """Ensure success=True can only be set when is_done=True"""+ if self.success is True and self.is_done is not True:+ raise ValueError(+ 'success=True can only be set when is_done=True. '+ 'For regular actions that succeed, leave success as None. '+ 'Use success=False only for actions that fail.'+ )+ return self+class StepMetadata(BaseModel):"""Metadata for a single step including timing and token information"""diff --cc browser_use/controller/registry/views.pyindex 63507c705,47b7774a5..84353a640--- a/browser_use/controller/registry/views.py+++ b/browser_use/controller/registry/views.py@@@ -5,8 -5,7 +5,8 @@@ from langchain_core.language_models.chafrom pydantic import BaseModel, ConfigDictfrom browser_use.browser import BrowserSession+ from browser_use.browser.types import Page+from browser_use.filesystem.file_system import FileSystemif TYPE_CHECKING:from browser_use.agent.service import Contextdiff --cc browser_use/controller/service.pyindex cb9c54a1d,806e701c6..07b4be7df--- a/browser_use/controller/service.py+++ b/browser_use/controller/service.py@@@ -1012,9 -927,8 +1014,9 @@@ Only use this for extracting info frombrowser_session: BrowserSession,#page_extraction_llm: BaseChatModel | None = None,- sensitive_data: dict[str, str] | None = None,+ sensitive_data: dict[str, str | dict[str, str]] | None = None,available_file_paths: list[str] | None = None,+ file_system: FileSystem | None = None,#context: Context | None = None,) -> ActionResult: