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
feat: enhance file handling capabilities with JSON and CSV support
mertunsall550a2a255216
diff --git a/browser_use/controller/service.py b/browser_use/controller/service.pyindex 919b61b60..8f14bfa7d 100644--- a/browser_use/controller/service.py+++ b/browser_use/controller/service.py@@ -566,7 +566,7 @@ Explain the content of the page and that the requested information is not availareturn ActionResult(error=msg, include_in_memory=True)# File System Actions- @self.registry.action('Write content to file_name in file system, use only .md or .txt extensions.')+ @self.registry.action('Write content to file_name in file system. Allowed extensions are .md, .txt, .json, .csv.')async def write_file(file_name: str, content: str, file_system: FileSystem):result = await file_system.write_file(file_name, content)logger.info(f'💾 {result}')@@ -581,13 +581,9 @@ Explain the content of the page and that the requested information is not availa@self.registry.action('Read file_name from file system')async def read_file(file_name: str, available_file_paths: list[str], file_system: FileSystem):if available_file_paths and file_name in available_file_paths:- import anyio-- async with await anyio.open_file(file_name, 'r') as f:- content = await f.read()- result = f'Read from file {file_name}.\n<content>\n{content}\n</content>'+ result = await file_system.read_file(file_name, external_file=True)else:- result = file_system.read_file(file_name)+ result = await file_system.read_file(file_name)MAX_MEMORY_SIZE = 1000if len(result) > MAX_MEMORY_SIZE:diff --git a/browser_use/filesystem/file_system.py b/browser_use/filesystem/file_system.pyindex eb823e9fd..6a72e5a6e 100644--- a/browser_use/filesystem/file_system.py+++ b/browser_use/filesystem/file_system.py@@ -94,6 +94,22 @@ class TxtFile(BaseFile):return 'txt'+class JsonFile(BaseFile):+ """JSON file implementation"""++ @property+ def extension(self) -> str:+ return 'json'+++class CsvFile(BaseFile):+ """CSV file implementation"""++ @property+ def extension(self) -> str:+ return 'csv'++class FileSystemState(BaseModel):"""Serializable state of the file system"""@@ -120,6 +136,8 @@ class FileSystem:self._file_types: dict[str, type[BaseFile]] = {'md': MarkdownFile,'txt': TxtFile,+ 'json': JsonFile,+ 'csv': CsvFile,}self.files = {}@@ -188,8 +206,37 @@ class FileSystem:return file_obj.read()- def read_file(self, full_filename: str) -> str:+ async def read_file(self, full_filename: str, external_file: bool = False) -> str:"""Read file content using file-specific read method and return appropriate message to LLM"""+ if external_file:+ try:+ _, extension = self._parse_filename(full_filename)+ if extension in ['md', 'txt', 'json', 'csv']:+ import anyio++ async with await anyio.open_file(full_filename, 'r') as f:+ content = await f.read()+ return f'Read from file {full_filename}.\n<content>\n{content}\n</content>'+ elif extension == 'pdf':+ import pypdf++ reader = pypdf.PdfReader(full_filename)+ num_pages = len(reader.pages)+ MAX_PDF_PAGES = 5+ extra_pages = num_pages - MAX_PDF_PAGES+ extracted_text = ''+ for page in reader.pages[:MAX_PDF_PAGES]:+ extracted_text += page.extract_text()+ return f'Read from file {full_filename}.\n<content>\n{extracted_text}\n{extra_pages} more pages...</content>'+ else:+ return f'Error: Cannot read file {full_filename} as {extension} extension is not supported.'+ except FileNotFoundError:+ return f"Error: File '{full_filename}' not found."+ except PermissionError:+ return f"Error: Permission denied to read file '{full_filename}'."+ except Exception as e:+ return f"Error: Could not read external file '{full_filename}'. {str(e)}"+if not self._is_valid_filename(full_filename):return INVALID_FILENAME_ERROR_MESSAGEdiff --git a/pyproject.toml b/pyproject.tomlindex a3dd52848..e86f881eb 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -39,6 +39,7 @@ dependencies = ["google-api-python-client>=2.174.0","google-auth>=2.40.3","google-auth-oauthlib>=1.2.2",+ "pypdf>=5.7.0",]# google-api-core: only used for Google LLM APIs# pyperclip: only used for examples that use copy/paste