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
Filter out interessting information
magmueller947ef4d3ea14
diff --git a/browser_use/agent/message_manager/service.py b/browser_use/agent/message_manager/service.pyindex 2c65f7909..5b89a026f 100644--- a/browser_use/agent/message_manager/service.py+++ b/browser_use/agent/message_manager/service.py@@ -193,10 +193,34 @@ class MessageManager:def _add_message_with_tokens(self, message: BaseMessage) -> None:"""Add message with token count metadata"""++ # filter out sensitive data from the message+ if self.sensitive_data:+ message = self._filter_sensitive_data(message)+token_count = self._count_tokens(message)metadata = MessageMetadata(input_tokens=token_count)self.history.add_message(message, metadata)+ def _filter_sensitive_data(self, message: BaseMessage) -> BaseMessage:+ """Filter out sensitive data from the message"""++ def replace_sensitive(value: str) -> str:+ if not self.sensitive_data:+ return value+ for key, val in self.sensitive_data.items():+ value = value.replace(val, f'<secret>{key}</secret>')+ return value++ if isinstance(message.content, str):+ message.content = replace_sensitive(message.content)+ elif isinstance(message.content, list):+ for i, item in enumerate(message.content):+ if isinstance(item, dict) and 'text' in item:+ item['text'] = replace_sensitive(item['text'])+ message.content[i] = item+ return message+def _count_tokens(self, message: BaseMessage) -> int:"""Count tokens in a message using the model's tokenizer"""tokens = 0diff --git a/browser_use/controller/service.py b/browser_use/controller/service.pyindex 6c5a1e99a..722693fcf 100644--- a/browser_use/controller/service.py+++ b/browser_use/controller/service.py@@ -133,7 +133,7 @@ class Controller:element_node = state.selector_map[params.index]await browser._input_text_element_node(element_node, params.text)- msg = f'⌨️ Input text into index {params.index}'+ msg = f'⌨️ Input {params.text} into index {params.index}'logger.info(msg)logger.debug(f'Element xpath: {element_node.xpath}')return ActionResult(extracted_content=msg, include_in_memory=True)diff --git a/examples/features/sensitive_data.py b/examples/features/sensitive_data.pyindex 2e54fd4f9..9d3d8d02b 100644--- a/examples/features/sensitive_data.py+++ b/examples/features/sensitive_data.py@@ -14,7 +14,7 @@ llm = ChatOpenAI()# the llm will never see the values of your sensitve data, it will only see the keys. Internally we replace after the llm the keys with the values.sensitive_data = {'x_name': 'mamagnus00', 'x_password': '12345678'}-task = 'go to x and login with x_name and x_password'+task = 'go to x and login with x_name and x_password then find interesting posts and like them'agent = Agent(task=task, llm=llm, sensitive_data=sensitive_data)