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
Handle sensitive data in agent state and handle user request being visible in agent history in case of update
mertunsall927b286d71eb
diff --git a/browser_use/agent/message_manager/service.py b/browser_use/agent/message_manager/service.pyindex 4f9ed97e6..77d0bdb07 100644--- a/browser_use/agent/message_manager/service.py+++ b/browser_use/agent/message_manager/service.py@@ -209,8 +209,9 @@ class MessageManager:self.state = stateself.system_prompt = system_messageself.file_system = file_system- self.agent_history_description = ''+ self.agent_history_description = f'Agent initialized with USER REQUEST: {task}\n'self.read_state_description = ''+ self.sensitive_data_description = ''# Only initialize messages if state is emptyif len(self.state.history.messages) == 0:self._init_messages()@@ -360,7 +361,7 @@ My next action is to click on the iPhone link at index [4] to navigate to Apple'def add_new_task(self, new_task: str) -> None:self.task = new_task- self.agent_history_description += f'\nUser updated USER REQUEST to:\n{new_task}\n'+ self.agent_history_description += f'\nUser updated USER REQUEST to: {new_task}\n'def _update_agent_history_description(self,@@ -405,10 +406,10 @@ Next goal: {model_output.current_state.next_goal}else:self.read_state_description += '\nMAKE SURE TO SAVE THIS INFORMATION INTO A FILE OR TO MEMORY IF YOU NEED IT LATER.'- def add_sensitive_data(self, current_page_url) -> None:+ def _get_sensitive_data_description(self, current_page_url) -> str:sensitive_data = self.settings.sensitive_dataif not sensitive_data:- return+ return ''# Collect placeholders for sensitive dataplaceholders = set()@@ -424,8 +425,9 @@ Next goal: {model_output.current_state.next_goal}if placeholders:info = f'Here are placeholders for sensitive data: {list(placeholders)}'info += '\nTo use them, write <secret>the placeholder name</secret>'- info_message = HumanMessage(content=info)- self._add_message_with_tokens(info_message, message_type='init')+ return info++ return ''@time_execution_sync('--add_state_message')def add_state_message(@@ -436,10 +438,13 @@ Next goal: {model_output.current_state.next_goal}step_info: AgentStepInfo | None = None,use_vision=True,page_filtered_actions: str | None = None,+ sensitive_data=None,) -> None:"""Add browser state as human message"""self._update_agent_history_description(model_output, result, step_info)+ if sensitive_data:+ self.sensitive_data_description = self._get_sensitive_data_description(browser_state_summary.url)# otherwise add state message and result to next message (which will not stay in memory)assert browser_state_summarystate_message = AgentMessagePrompt(@@ -451,6 +456,7 @@ Next goal: {model_output.current_state.next_goal}include_attributes=self.settings.include_attributes,step_info=step_info,page_filtered_actions=page_filtered_actions,+ sensitive_data=self.sensitive_data_description,).get_user_message(use_vision)self._add_message_with_tokens(state_message)diff --git a/browser_use/agent/prompts.py b/browser_use/agent/prompts.pyindex b02cb32ef..733f3e9c2 100644--- a/browser_use/agent/prompts.py+++ b/browser_use/agent/prompts.py@@ -72,6 +72,7 @@ class AgentMessagePrompt:step_info: Optional['AgentStepInfo'] = None,page_filtered_actions: str | None = None,max_clickable_elements_length: int = 40000,+ sensitive_data: str | None = None,):self.browser_state: 'BrowserStateSummary' = browser_state_summaryself.file_system: 'FileSystem' | None = file_system@@ -82,6 +83,7 @@ class AgentMessagePrompt:self.step_info = step_infoself.page_filtered_actions: str | None = page_filtered_actionsself.max_clickable_elements_length: int = max_clickable_elements_length+ self.sensitive_data: str | None = sensitive_dataassert self.browser_statedef _get_browser_state_description(self) -> str:@@ -142,10 +144,11 @@ Interactive elements from top layer of the current page inside the viewport{trun<todo_contents>{todo_contents}</todo_contents>-<step_info>-{step_info_description}-</step_info>"""+ if self.sensitive_data:+ agent_state += f'<sensitive_data>\n{self.sensitive_data}\n</sensitive_data>\n'++ agent_state += f'<step_info>\n{step_info_description}\n</step_info>\n'return agent_statedef get_user_message(self, use_vision: bool = True) -> HumanMessage:diff --git a/browser_use/agent/service.py b/browser_use/agent/service.pyindex f39bc2d51..9f1074ee8 100644--- a/browser_use/agent/service.py+++ b/browser_use/agent/service.py@@ -879,9 +879,6 @@ class Agent(Generic[Context]):# Get page-specific filtered actionspage_filtered_actions = self.controller.registry.get_prompt_description(current_page)- if self.sensitive_data:- self._message_manager.add_sensitive_data(current_page.url)-# If there are page-specific actions, add them as a special message for this step onlyif page_filtered_actions:page_action_message = f'For this page, these additional actions are available:\n{page_filtered_actions}'@@ -911,6 +908,7 @@ class Agent(Generic[Context]):step_info=step_info,use_vision=self.settings.use_vision,page_filtered_actions=page_filtered_actions if page_filtered_actions else None,+ sensitive_data=self.sensitive_data,)# Run planner at specified intervals if planner is configured