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
improve file system descriptions and memory
mertunsallfde5e09c6a2d
diff --git a/browser_use/filesystem/file_system.py b/browser_use/filesystem/file_system.pyindex b4562aa07..464579f59 100644--- a/browser_use/filesystem/file_system.py+++ b/browser_use/filesystem/file_system.py@@ -25,10 +25,17 @@ class FileSystem:self.todo_file = self.dir / 'todo.md'self.results_file.touch(exist_ok=True)self.todo_file.touch(exist_ok=True)+ self.extracted_content_count = 0def get_dir(self) -> Path:return self.dir+ async def save_extracted_content(self, content: str) -> str:+ extracted_content_file_name = f'extracted_content_{self.extracted_content_count}.md'+ result = await self.write_file(extracted_content_file_name, content)+ self.extracted_content_count += 1+ return result+def _is_valid_filename(self, file_name: str) -> bool:"""Check if filename matches the required pattern: name.extension"""pattern = r'^[a-zA-Z0-9_\-]+\.(txt|md)$'@@ -59,7 +66,7 @@ class FileSystem:with ThreadPoolExecutor() as executor:# Run file read in a thread to avoid blockingcontent = await asyncio.get_event_loop().run_in_executor(executor, lambda: path.read_text())- return f'Read from file {file_name}:\n{content}'+ return f'Read from file {file_name}.\n<content>\n{content}\n</content>'except Exception:return f"Error: Could not read file '{file_name}'."@@ -98,17 +105,89 @@ class FileSystem:return f"Error: Could not append to file '{file_name}'. {str(e)}"def describe(self) -> str:- """List all files with their line counts."""+ """List all files with their content information.++ Example output:+ <file>+ results.md - 42 lines+ <content>+ {preview_start}+ ... {n_lines} more lines ...+ {preview_end}+ </content>+ </file>+ """+ DISPLAY_CHARS = 400 # Total characters to display (split between start and end)description = ''+for f in self.dir.iterdir():- if f.is_file():- try:- num_lines = len(f.read_text().splitlines())- description += f'- {f.name} — {num_lines} lines\n'- except Exception:- description += f'- {f.name} — [error reading file]\n'-- return description+ # Only process files and skip todo.md+ if (not f.is_file()) or f.name == 'todo.md':+ continue++ try:+ content = f.read_text()++ # Handle empty files+ if not content:+ description += f'<file>\n{f.name} - [empty file]\n</file>\n\n'+ continue++ lines = content.splitlines()+ line_count = len(lines)++ # For small files, display the entire content+ whole_file_description = f'<file>\n{f.name} - {line_count} lines\n<content>\n{content}\n</content>\n</file>\n'+ if len(content) < int(1.5 * DISPLAY_CHARS):+ description += whole_file_description+ continue++ # For larger files, display start and end previews+ half_display_chars = DISPLAY_CHARS // 2++ # Get start preview+ start_preview = ''+ start_line_count = 0+ chars_count = 0+ for line in lines:+ if chars_count + len(line) + 1 > half_display_chars:+ break+ start_preview += line + '\n'+ chars_count += len(line) + 1+ start_line_count += 1++ # Get end preview+ end_preview = ''+ end_line_count = 0+ chars_count = 0+ for line in reversed(lines):+ if chars_count + len(line) + 1 > half_display_chars:+ break+ end_preview = line + '\n' + end_preview+ chars_count += len(line) + 1+ end_line_count += 1++ # Calculate lines in between+ middle_line_count = line_count - start_line_count - end_line_count+ if middle_line_count <= 0:+ # display the entire file+ description += whole_file_description+ continue++ start_preview = start_preview.strip('\n').rstrip()+ end_preview = end_preview.strip('\n').rstrip()++ # Format output+ description += f'<file>\n{f.name} - {line_count} lines\n<content>\n{start_preview}\n'+ description += f'... {middle_line_count} more lines ...\n'+ description += f'{end_preview}\n'+ description += '</content>\n</file>\n'++ except Exception as e:+ raise e+ description += f'<file>\n{f.name} - [error reading file]\n</file>\n\n'++ return description.strip('\n')def get_todo_contents(self) -> str:return self.todo_file.read_text()