mirror of
https://github.com/browser-use/browser-use
synced 2026-05-06 17:52:15 +02:00
commit92d1732a40Merge:fa4ff47aefcd2b33Author: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Wed Oct 8 08:18:35 2025 -0700 Merge main commitfa4ff47ae4Author: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Wed Oct 8 08:03:04 2025 -0700 Laminar info mode commit8625aa9ba9Author: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Wed Oct 8 08:02:54 2025 -0700 Default to flash for browser use llm commit69a517ad7cAuthor: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Wed Oct 8 08:02:44 2025 -0700 Update example commitc91467c51dAuthor: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Wed Oct 8 08:02:15 2025 -0700 Update example commit4e2b3f1306Author: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Wed Oct 8 07:54:05 2025 -0700 Linter commite8d6936a47Author: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Wed Oct 8 07:53:43 2025 -0700 Example commit961d6fc451Author: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Wed Oct 8 07:52:23 2025 -0700 Update default links commit0b2853859bAuthor: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Wed Oct 8 07:44:59 2025 -0700 Default fast fasle commit9d67594b63Author: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Tue Oct 7 22:01:24 2025 -0700 Simplify commitf5712bdec0Author: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Tue Oct 7 21:45:00 2025 -0700 Parsing json commit503af624ddMerge:ef7fa1e40d2522b1Author: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Tue Oct 7 21:04:31 2025 -0700 Merge branch 'flat-output' of https://github.com/browser-use/browser-use into flat-output commitef7fa1e401Author: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Tue Oct 7 21:04:28 2025 -0700 Add prompt description handling for browser-use cloud service - Enhanced the Agent class to generate and pass prompt descriptions to the LLM when using the browser-use provider. - Updated the ChatBrowserUse class to accept an optional prompt_description parameter in the ainvoke method. - Modified the tracked_ainvoke function in TokenCost to forward additional keyword arguments. - Adjusted example model to initialize Laminar for tracing. commit0d2522b1f9Author: Gregor Žunič <36313686+gregpr07@users.noreply.github.com> Date: Tue Oct 7 20:23:39 2025 -0700 updated default base url commit0a2e6d6520Author: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Tue Oct 7 18:50:56 2025 -0700 Hide api key commit96bba9692bAuthor: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Tue Oct 7 18:49:05 2025 -0700 Add env var loading and validation to ChatBrowserUse, update example commitc2e57a3babAuthor: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Tue Oct 7 18:36:31 2025 -0700 Linter commit3d4cc1dbecAuthor: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Tue Oct 7 16:28:10 2025 -0700 Rename commit14ab0b75feAuthor: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Tue Oct 7 16:21:52 2025 -0700 Rename super_fast to fast parameter in ChatBrowserUse commit7fab3c47d3Author: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Tue Oct 7 16:08:10 2025 -0700 Update system prompt commit2066e77b98Author: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Tue Oct 7 16:07:51 2025 -0700 Del claude file commit297b767e91Author: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Tue Oct 7 16:01:50 2025 -0700 Revert flash system prompt to JSON format - Reverted system_prompt_flash.md to main (JSON format) - Added examples/models/browser_use_cloud.py for llm-use cloud service - llm-use service will inject unstructured format on the server side commit99af55f8e9Author: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Tue Oct 7 15:58:40 2025 -0700 Example commit657ff0d352Author: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Tue Oct 7 15:54:23 2025 -0700 Remove unused commita42123d8e5Author: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Tue Oct 7 15:49:54 2025 -0700 ignore commitde4fe19020Author: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Tue Oct 7 15:48:01 2025 -0700 Example commit27e5ab9947Author: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Tue Oct 7 14:29:01 2025 -0700 Add llm-use cloud client example - ChatLLMUseCloud: Browser-use compatible client for llm-use service - Implements BaseChatModel protocol with proper typing - Includes credit checking before/after tasks - Error handling for 401/402/500 responses - Converts messages between browser-use and HTTP API formats - Returns ChatInvokeCompletion with usage stats Usage: export LLM_USE_URL='https://your-app.railway.app' export LLM_USE_API_KEY='your-key' python examples/llm_use_cloud_client.py commita47a448005Author: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Tue Oct 7 13:03:59 2025 -0700 Enhance response parsing in ChatBrowserUse to handle structured data from the API, including debug logging for received data keys. commite79c7dc633Author: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Mon Oct 6 19:39:29 2025 -0700 Include browser-use llm commit584d977154Author: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Mon Oct 6 17:55:31 2025 -0700 Linter commitb0ac87076aMerge:1183b8fe0076775fAuthor: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Mon Oct 6 15:01:37 2025 -0700 merge main commit1183b8fe6bAuthor: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Sun Oct 5 16:26:34 2025 -0700 Fix done output action model commit852449b1bcAuthor: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Sun Oct 5 16:23:31 2025 -0700 More debug info commita57ba25d36Author: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Sun Oct 5 16:07:15 2025 -0700 Multiline parser commit2153dbe6aaAuthor: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Sun Oct 5 15:50:14 2025 -0700 Fix custom output commitec5c08ba0fAuthor: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Sun Oct 5 15:36:54 2025 -0700 Fix files in output commit7444048e8fAuthor: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Sun Oct 5 15:28:09 2025 -0700 Update codebase with unstructured output commita8f05b4611Author: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Sun Oct 5 14:55:20 2025 -0700 Save systemprompt commitd7d112c569Author: Magnus Müller <67061560+MagMueller@users.noreply.github.com> Date: Sun Oct 5 14:51:24 2025 -0700 new parser
175 lines
5.6 KiB
Python
175 lines
5.6 KiB
Python
from collections.abc import Callable
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
from pydantic import BaseModel, ConfigDict
|
|
|
|
from browser_use.browser import BrowserSession
|
|
from browser_use.filesystem.file_system import FileSystem
|
|
from browser_use.llm.base import BaseChatModel
|
|
|
|
if TYPE_CHECKING:
|
|
pass
|
|
|
|
|
|
class RegisteredAction(BaseModel):
|
|
"""Model for a registered action"""
|
|
|
|
name: str
|
|
description: str
|
|
function: Callable
|
|
param_model: type[BaseModel]
|
|
|
|
# filters: provide specific domains to determine whether the action should be available on the given URL or not
|
|
domains: list[str] | None = None # e.g. ['*.google.com', 'www.bing.com', 'yahoo.*]
|
|
|
|
model_config = ConfigDict(arbitrary_types_allowed=True)
|
|
|
|
def prompt_description(self) -> str:
|
|
"""Get a description of the action for the prompt in unstructured format"""
|
|
schema = self.param_model.model_json_schema()
|
|
params = []
|
|
|
|
if 'properties' in schema:
|
|
for param_name, param_info in schema['properties'].items():
|
|
# Build parameter description
|
|
param_desc = param_name
|
|
|
|
# Add type information if available
|
|
if 'type' in param_info:
|
|
param_type = param_info['type']
|
|
param_desc += f'={param_type}'
|
|
|
|
# Add description as comment if available
|
|
if 'description' in param_info:
|
|
param_desc += f' ({param_info["description"]})'
|
|
|
|
params.append(param_desc)
|
|
|
|
# Format: action_name: Description. (param1=type, param2=type, ...)
|
|
if params:
|
|
return f'{self.name}: {self.description}. ({", ".join(params)})'
|
|
else:
|
|
return f'{self.name}: {self.description}'
|
|
|
|
|
|
class ActionModel(BaseModel):
|
|
"""Base model for dynamically created action models"""
|
|
|
|
# this will have all the registered actions, e.g.
|
|
# click_element = param_model = ClickElementParams
|
|
# done = param_model = None
|
|
#
|
|
model_config = ConfigDict(arbitrary_types_allowed=True, extra='forbid')
|
|
|
|
def get_index(self) -> int | None:
|
|
"""Get the index of the action"""
|
|
# {'clicked_element': {'index':5}}
|
|
params = self.model_dump(exclude_unset=True).values()
|
|
if not params:
|
|
return None
|
|
for param in params:
|
|
if param is not None and 'index' in param:
|
|
return param['index']
|
|
return None
|
|
|
|
def set_index(self, index: int):
|
|
"""Overwrite the index of the action"""
|
|
# Get the action name and params
|
|
action_data = self.model_dump(exclude_unset=True)
|
|
action_name = next(iter(action_data.keys()))
|
|
action_params = getattr(self, action_name)
|
|
|
|
# Update the index directly on the model
|
|
if hasattr(action_params, 'index'):
|
|
action_params.index = index
|
|
|
|
|
|
class ActionRegistry(BaseModel):
|
|
"""Model representing the action registry"""
|
|
|
|
actions: dict[str, RegisteredAction] = {}
|
|
|
|
@staticmethod
|
|
def _match_domains(domains: list[str] | None, url: str) -> bool:
|
|
"""
|
|
Match a list of domain glob patterns against a URL.
|
|
|
|
Args:
|
|
domains: A list of domain patterns that can include glob patterns (* wildcard)
|
|
url: The URL to match against
|
|
|
|
Returns:
|
|
True if the URL's domain matches the pattern, False otherwise
|
|
"""
|
|
|
|
if domains is None or not url:
|
|
return True
|
|
|
|
# Use the centralized URL matching logic from utils
|
|
from browser_use.utils import match_url_with_domain_pattern
|
|
|
|
for domain_pattern in domains:
|
|
if match_url_with_domain_pattern(url, domain_pattern):
|
|
return True
|
|
return False
|
|
|
|
def get_prompt_description(self, page_url: str | None = None) -> str:
|
|
"""Get a description of all actions for the prompt
|
|
|
|
Args:
|
|
page_url: If provided, filter actions by URL using domain filters.
|
|
|
|
Returns:
|
|
A string description of available actions.
|
|
- If page is None: return only actions with no page_filter and no domains (for system prompt)
|
|
- If page is provided: return only filtered actions that match the current page (excluding unfiltered actions)
|
|
"""
|
|
if page_url is None:
|
|
# For system prompt (no URL provided), include only actions with no filters
|
|
return '\n'.join(action.prompt_description() for action in self.actions.values() if action.domains is None)
|
|
|
|
# only include filtered actions for the current page URL
|
|
filtered_actions = []
|
|
for action in self.actions.values():
|
|
if not action.domains:
|
|
# skip actions with no filters, they are already included in the system prompt
|
|
continue
|
|
|
|
# Check domain filter
|
|
if self._match_domains(action.domains, page_url):
|
|
filtered_actions.append(action)
|
|
|
|
return '\n'.join(action.prompt_description() for action in filtered_actions)
|
|
|
|
|
|
class SpecialActionParameters(BaseModel):
|
|
"""Model defining all special parameters that can be injected into actions"""
|
|
|
|
model_config = ConfigDict(arbitrary_types_allowed=True)
|
|
|
|
# optional user-provided context object passed down from Agent(context=...)
|
|
# e.g. can contain anything, external db connections, file handles, queues, runtime config objects, etc.
|
|
# that you might want to be able to access quickly from within many of your actions
|
|
# browser-use code doesn't use this at all, we just pass it down to your actions for convenience
|
|
context: Any | None = None
|
|
|
|
# browser-use session object, can be used to create new tabs, navigate, access CDP
|
|
browser_session: BrowserSession | None = None
|
|
|
|
# Current page URL for filtering and context
|
|
page_url: str | None = None
|
|
|
|
# CDP client for direct Chrome DevTools Protocol access
|
|
cdp_client: Any | None = None # CDPClient type from cdp_use
|
|
|
|
# extra injected config if the action asks for these arg names
|
|
page_extraction_llm: BaseChatModel | None = None
|
|
file_system: FileSystem | None = None
|
|
available_file_paths: list[str] | None = None
|
|
has_sensitive_data: bool = False
|
|
|
|
@classmethod
|
|
def get_browser_requiring_params(cls) -> set[str]:
|
|
"""Get parameter names that require browser_session"""
|
|
return {'browser_session', 'cdp_client', 'page_url'}
|