Skip to content

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

bug fixes + lint

Laith Weinberger7a1b76a5f0f7
diff --git a/browser_use/filesystem/file_system.py b/browser_use/filesystem/file_system.py
index 322ece5ef..0fbe39ea4 100644
--- a/browser_use/filesystem/file_system.py
+++ b/browser_use/filesystem/file_system.py
@@ -542,12 +542,19 @@ class FileSystem:
text = page_texts[page_num - 1][1]
if not text.strip():
continue
- page_content = f'--- Page {page_num} ---\n{text}'
- if chars_used + len(page_content) > MAX_CHARS:
- continue # skip this page, try next
+ page_header = f'--- Page {page_num} ---\n'
+ remaining = MAX_CHARS - chars_used
+ if remaining <= len(page_header):
+ break # no room left for any content
+ page_content = page_header + text
+ if len(page_content) > remaining:
+ # Truncate page to fit remaining budget
+ page_content = page_content[: remaining - 20] + '\n[...truncated]'
content_parts.append((page_num, page_content))
chars_used += len(page_content)
pages_included.append(page_num)
+ if chars_used >= MAX_CHARS:
+ break
# Sort included pages by page number for readability
content_parts.sort(key=lambda x: x[0])
diff --git a/browser_use/tools/service.py b/browser_use/tools/service.py
index fd4750959..3dbbfb6f1 100644
--- a/browser_use/tools/service.py
+++ b/browser_use/tools/service.py
@@ -4,6 +4,8 @@ import logging
import os
from typing import Generic, TypeVar
+import anyio
+
try:
from lmnr import Laminar # type: ignore
except ImportError:
@@ -1196,21 +1198,25 @@ Context: {context}"""
for match in regex.finditer(content):
start = max(0, match.start() - context_chars)
end = min(len(content), match.end() + context_chars)
- matches.append({
- 'position': match.start(),
- 'snippet': content[start:end],
- })
+ matches.append(
+ {
+ 'position': match.start(),
+ 'snippet': content[start:end],
+ }
+ )
return matches
def chunk_content(content: str, chunk_size: int = 2000) -> list[dict]:
"""Split content into chunks with positions."""
chunks = []
for i in range(0, len(content), chunk_size):
- chunks.append({
- 'start': i,
- 'end': min(i + chunk_size, len(content)),
- 'text': content[i:i + chunk_size],
- })
+ chunks.append(
+ {
+ 'start': i,
+ 'end': min(i + chunk_size, len(content)),
+ 'text': content[i : i + chunk_size],
+ }
+ )
return chunks
try:
@@ -1334,8 +1340,8 @@ Context: {context}"""
else:
# Text file
- with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
- content = f.read()
+ async with await anyio.open_file(file_path, 'r', encoding='utf-8', errors='ignore') as f:
+ content = await f.read()
# Check if content fits in budget
if len(content) <= max_chars:
@@ -1439,10 +1445,12 @@ Context: {context}"""
start = max(0, match.start() - context_chars)
end = min(len(text), match.end() + context_chars)
snippet = text[start:end].replace('\n', ' ')
- matches.append({
- 'position': match.start(),
- 'snippet': snippet,
- })
+ matches.append(
+ {
+ 'position': match.start(),
+ 'snippet': snippet,
+ }
+ )
return matches
if source.lower() == 'page':
@@ -1496,13 +1504,13 @@ Context: {context}"""
elif not os.path.isabs(file_path):
return ActionResult(
extracted_content=f'Error: File path must be absolute or in available_file_paths: {file_path}',
- long_term_memory=f'Failed to search: invalid path',
+ long_term_memory='Failed to search: invalid path',
)
if not os.path.exists(file_path):
return ActionResult(
extracted_content=f'Error: File not found: {file_path}',
- long_term_memory=f'Failed to search: file not found',
+ long_term_memory='Failed to search: file not found',
)
# Determine file type and search accordingly
@@ -1540,8 +1548,8 @@ Context: {context}"""
else:
# Text file search
- with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
- content = f.read()
+ async with await anyio.open_file(file_path, 'r', encoding='utf-8', errors='ignore') as f:
+ content = await f.read()
lines = content.split('\n')
matches = []
@@ -1549,10 +1557,12 @@ Context: {context}"""
for match in pattern.finditer(line):
start = max(0, match.start() - 50)
end = min(len(line), match.end() + 50)
- matches.append({
- 'line': line_num,
- 'snippet': line[start:end],
- })
+ matches.append(
+ {
+ 'line': line_num,
+ 'snippet': line[start:end],
+ }
+ )
if not matches:
result_text = f'No matches found for "{query}" in {file_path}'
diff --git a/browser_use/tools/views.py b/browser_use/tools/views.py
index ad4bd0b7b..a6da8ab97 100644
--- a/browser_use/tools/views.py
+++ b/browser_use/tools/views.py
@@ -128,7 +128,6 @@ class ReadContentAction(BaseModel):
context: str = Field(default='', description='Additional context about the task')
-
class GetDropdownOptionsAction(BaseModel):
index: int