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
now the model can display files to the users
mertunsall1e549d72f3e7
diff --git a/browser_use/controller/service.py b/browser_use/controller/service.pyindex cac1eaed9..f98bbaa5d 100644--- a/browser_use/controller/service.py+++ b/browser_use/controller/service.py@@ -77,16 +77,26 @@ class Controller(Generic[Context]):else:@self.registry.action(- 'Complete task - with return text and if the task is finished (success=True) or not yet completely finished (success=False), because last step is reached',+ 'Complete task - provide a summary of results for the user. Set success=True if task completed successfully, false otherwise. Text should be your response to the user summarizing results. Include files you would like to display to the user in files_to_display.',param_model=DoneAction,)- async def done(params: DoneAction):+ async def done(params: DoneAction, file_system: FileSystem):+ user_message = params.text+len_text = len(params.text)len_max_memory = 100memory = f'Task completed: {params.success} - {params.text[:len_max_memory]}'if len_text > len_max_memory:memory += f' - {len_text - len_max_memory} more characters'- return ActionResult(is_done=True, success=params.success, extracted_content=params.text, memory=memory)++ if params.files_to_display:+ user_message += '\n\nBelow are the files I saved for you:'+ for file_name in params.files_to_display:+ file_content = file_system.display_file(file_name)+ if file_content:+ user_message += f'\n\n{file_name}:\n{file_content}'++ return ActionResult(is_done=True, success=params.success, extracted_content=user_message, memory=memory)# Basic Navigation Actions@self.registry.action(diff --git a/browser_use/controller/views.py b/browser_use/controller/views.pyindex 8bfe79f84..0a46e513d 100644--- a/browser_use/controller/views.py+++ b/browser_use/controller/views.py@@ -24,6 +24,7 @@ class InputTextAction(BaseModel):class DoneAction(BaseModel):text: strsuccess: bool+ files_to_display: list[str] | None = []class SwitchTabAction(BaseModel):diff --git a/browser_use/filesystem/file_system.py b/browser_use/filesystem/file_system.pyindex c8ab3a49b..99fd99906 100644--- a/browser_use/filesystem/file_system.py+++ b/browser_use/filesystem/file_system.py@@ -31,6 +31,18 @@ class FileSystem:pattern = r'^[a-zA-Z0-9_\-]+\.(txt|md)$'return bool(re.match(pattern, file_name))+ def display_file(self, file_name: str) -> str | None:+ if not self._is_valid_filename(file_name):+ return None++ path = self.dir / file_name+ if not path.exists():+ return None+ try:+ return path.read_text()+ except Exception:+ return None+async def read_file(self, file_name: str) -> str:if not self._is_valid_filename(file_name):return INVALID_FILENAME_ERROR_MESSAGE@@ -73,11 +85,7 @@ class FileSystem:# Create a new executor for this operationwith ThreadPoolExecutor() as executor:# Run file append in a thread to avoid blocking- def append_to_file(file_path, data):- with file_path.open('a') as f:- f.write(data)-- await asyncio.get_event_loop().run_in_executor(executor, append_to_file, path, content)+ await asyncio.get_event_loop().run_in_executor(executor, lambda p=path, c=content: p.write_text(c, append=True))return f'Data appended to {file_name} successfully.'except Exception:return f"Error: Could not append to file '{file_name}'."