Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

move filesystem controllers

mertunsall9edffbdd102f
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index 739b50c56..40cde26f7 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -15,7 +15,6 @@ from pathlib import Path
from threading import Thread
from typing import Any, Generic, TypeVar
-import anyio
from dotenv import load_dotenv
load_dotenv()
@@ -493,48 +492,6 @@ class Agent(Generic[Context]):
logger.info(f'💾 File system path: {self.file_system_path}')
- # if file system is set, add actions to the controller
- @self.controller.registry.action('Write content to file_name in file system, use only .md or .txt extensions.')
- async def write_file(file_name: str, content: str):
- result = await self.file_system.write_file(file_name, content)
- logger.info(f'💾 {result}')
- return ActionResult(extracted_content=result, include_in_memory=True, long_term_memory=result)
-
- @self.controller.registry.action('Append content to file_name in file system')
- async def append_file(file_name: str, content: str):
- result = await self.file_system.append_file(file_name, content)
- logger.info(f'💾 {result}')
- return ActionResult(extracted_content=result, include_in_memory=True, long_term_memory=result)
-
- @self.controller.registry.action('Read file_name from file system')
- async def read_file(file_name: str, available_file_paths: list[str]):
- if file_name in available_file_paths:
- 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>'
- else:
- result = await self.file_system.read_file(file_name)
-
- MAX_MEMORY_SIZE = 1000
- if len(result) > MAX_MEMORY_SIZE:
- lines = result.splitlines()
- display = ''
- for line in lines:
- if len(display) + len(line) < MAX_MEMORY_SIZE:
- display += line + '\n'
- else:
- break
- memory = f'{display}{len(lines) - len(display)} more lines...'
- else:
- memory = result
- logger.info(f'💾 {memory}')
- return ActionResult(
- extracted_content=result,
- include_in_memory=True,
- long_term_memory=memory,
- include_extracted_content_only_once=True,
- )
-
def _set_message_context(self) -> str | None:
if self.tool_calling_method == 'raw':
# For raw tool calling, only include actions with no filters initially
diff --git a/browser_use/controller/service.py b/browser_use/controller/service.py
index b1e13b35d..1d20df52f 100644
--- a/browser_use/controller/service.py
+++ b/browser_use/controller/service.py
@@ -633,6 +633,50 @@ Explain the content of the page and that the requested information is not availa
logger.error(msg)
return 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.')
+ 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}')
+ return ActionResult(extracted_content=result, include_in_memory=True, long_term_memory=result)
+
+ @self.registry.action('Append content to file_name in file system')
+ async def append_file(file_name: str, content: str, file_system: FileSystem):
+ result = await file_system.append_file(file_name, content)
+ logger.info(f'💾 {result}')
+ return ActionResult(extracted_content=result, include_in_memory=True, long_term_memory=result)
+
+ @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>'
+ else:
+ result = await file_system.read_file(file_name)
+
+ MAX_MEMORY_SIZE = 1000
+ if len(result) > MAX_MEMORY_SIZE:
+ lines = result.splitlines()
+ display = ''
+ for line in lines:
+ if len(display) + len(line) < MAX_MEMORY_SIZE:
+ display += line + '\n'
+ else:
+ break
+ memory = f'{display}{len(lines) - len(display)} more lines...'
+ else:
+ memory = result
+ logger.info(f'💾 {memory}')
+ return ActionResult(
+ extracted_content=result,
+ include_in_memory=True,
+ long_term_memory=memory,
+ include_extracted_content_only_once=True,
+ )
+
@self.registry.action(
description='Get all options from a native dropdown',
)