mirror of
https://github.com/browser-use/browser-use.git
synced 2026-10-02 04:04:36 +08:00
fix(agent): preserve downloaded file path order
This commit is contained in:
@@ -676,11 +676,16 @@ class Agent(Generic[Context, AgentStructuredOutput]):
|
||||
if not self.has_downloads_path:
|
||||
return
|
||||
|
||||
current_files = set(self.available_file_paths or [])
|
||||
new_files = set(downloads) - current_files
|
||||
available_files = self.available_file_paths or []
|
||||
seen_files = set(available_files)
|
||||
new_files = []
|
||||
for download in downloads:
|
||||
if download not in seen_files:
|
||||
seen_files.add(download)
|
||||
new_files.append(download)
|
||||
|
||||
if new_files:
|
||||
self.available_file_paths = list(current_files | new_files)
|
||||
self.available_file_paths = [*available_files, *new_files]
|
||||
|
||||
self.logger.info(
|
||||
f'📁 Added {len(new_files)} downloaded files to available_file_paths (total: {len(self.available_file_paths)} files)'
|
||||
@@ -688,7 +693,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):
|
||||
for file_path in new_files:
|
||||
self.logger.info(f'📄 New file available: {file_path}')
|
||||
else:
|
||||
self.logger.debug(f'📁 No new downloads detected (tracking {len(current_files)} files)')
|
||||
self.logger.debug(f'📁 No new downloads detected (tracking {len(available_files)} files)')
|
||||
|
||||
def _set_file_system(self, file_system_path: str | None = None) -> None:
|
||||
# Check for conflicting parameters
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
from browser_use.agent.service import Agent
|
||||
from browser_use.browser import BrowserProfile, BrowserSession
|
||||
from tests.ci.conftest import create_mock_llm
|
||||
|
||||
|
||||
def test_update_available_file_paths_preserves_input_and_download_order(tmp_path):
|
||||
session = BrowserSession(browser_profile=BrowserProfile(downloads_path=tmp_path))
|
||||
agent = Agent(
|
||||
task='Use downloaded files.',
|
||||
llm=create_mock_llm(),
|
||||
browser_session=session,
|
||||
available_file_paths=['/tmp/input-b.txt', '/tmp/input-a.txt'],
|
||||
)
|
||||
|
||||
agent._update_available_file_paths(['/tmp/download-b.txt', '/tmp/input-a.txt', '/tmp/download-a.txt', '/tmp/download-b.txt'])
|
||||
|
||||
assert agent.available_file_paths == [
|
||||
'/tmp/input-b.txt',
|
||||
'/tmp/input-a.txt',
|
||||
'/tmp/download-b.txt',
|
||||
'/tmp/download-a.txt',
|
||||
]
|
||||
Reference in New Issue
Block a user