Skip to content

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

Fix done true only when final task done

magmuellerf3cf42edd5a9
diff --git a/src/agent/service.py b/src/agent/service.py
index b34de5df8..a95ea8573 100644
--- a/src/agent/service.py
+++ b/src/agent/service.py
@@ -44,20 +44,16 @@ class AgentService:
elif action.go_back:
self.browser.go_back()
elif action.done:
- # TODO: implement
- # self.browser.done()
- pass
+ return AgentActionResult(done=True)
elif action.click_element:
self.browser.click_element_by_index(action.click_element.id)
elif action.input_text:
self.browser.input_text_by_index(action.input_text.id, action.input_text.text)
elif action.extract_page_content:
content = self.browser.extract_page_content()
- return AgentActionResult(done=True, extracted_content=content)
+ return AgentActionResult(done=False, extracted_content=content)
else:
return AgentActionResult(done=False)
- return AgentActionResult(done=True)
-
except Exception as e:
return AgentActionResult(done=False, error=str(e))
diff --git a/src/planning/service.py b/src/planning/service.py
index 6d87f5957..8d434f225 100644
--- a/src/planning/service.py
+++ b/src/planning/service.py
@@ -42,7 +42,7 @@ class PlaningService:
self.messages: list[BaseMessage] = [system_prompt, first_message]
async def step(self) -> tuple[PlanningAgentAction, AgentActionResult]:
- state = self.agent.get_current_state()
+ state = self.agent.get_current_state(screenshot=self.use_vision)
action = await self.get_next_action(state)
if action.ask_human and action.ask_human.question:
@@ -77,13 +77,13 @@ class PlaningService:
input_messages = self.messages + [new_message]
structured_llm = self.llm.with_structured_output(PlanningAgentAction)
- print(state)
+ # print(state)
input('continue? ...')
response: PlanningAgentAction = await structured_llm.ainvoke(input_messages) # type: ignore
- print('response', response)
+ # print('response', response)
# if store_conversation:
# # save conversation
diff --git a/src/tests/test_kayak_search.py b/src/tests/test_kayak_search.py
index 42119e076..f5c09d912 100644
--- a/src/tests/test_kayak_search.py
+++ b/src/tests/test_kayak_search.py
@@ -1,3 +1,4 @@
+import datetime
import os
import pytest
@@ -7,45 +8,43 @@ from src.agent.service import AgentService
from src.planning.service import PlaningService
-# run with pytest src/tests/test_kayak_search.py -v -s
-# @pytest.mark.skip
-async def test_kayak_flight_search():
- task = 'go to kayak.com andfind a flight from Bali to Kirgistan on 2024-11-25 for 2 people one way.'
- timestamp = 'kayak_com_flight_search'
-
- agent = AgentService()
- model = ChatOpenAI(model='gpt-4o')
- planning_service = PlaningService(task, model, agent)
-
- vision = True
- print('starting')
+def setup_run_folder(timestamp_prefix: str) -> str:
+ timestamp = f'{timestamp_prefix}_{datetime.datetime.now().strftime("%Y-%m-%d")}'
run_folder = f'temp/{timestamp}'
if not os.path.exists(run_folder):
os.makedirs(run_folder)
-
else:
# Remove run folder if it exists
for file in os.listdir(run_folder):
os.remove(os.path.join(run_folder, file))
os.rmdir(run_folder)
os.makedirs(run_folder)
+ return run_folder
+
+
+# run with pytest src/tests/test_kayak_search.py -v -s
+# @pytest.mark.skip
+async def test_kayak_flight_search():
+ task = 'go to kayak.com andfind a flight from Bali to Kirgistan on 2024-11-25 for 2 people one way.'
+ run_folder = setup_run_folder('kayak_com_flight_search2')
+
+ agent = AgentService()
+ model = ChatOpenAI(model='gpt-4o')
+ planning_service = PlaningService(task, model, agent, use_vision=True)
print('\n' + '=' * 50)
print('🚀 Starting flight search task')
print('=' * 50)
- url_history = []
- # output = AgentActionResult(done)
-
try:
max_steps = 50
for i in range(max_steps):
print(f'\n📍 Step {i+1}')
action, result = await planning_service.step()
- print('action:', action)
- print('result:', result)
+ print('action:\n', action)
+ print('result:\n', result)
# current_state = agent.get_current_state()
# save_formatted_html(
# current_state.interactable_elements, f'{run_folder}/current_state_{i}.html'
@@ -74,8 +73,9 @@ async def test_kayak_flight_search():
# output: ActionResult = actions.execute_action(action, current_state.selector_map)
# # check if output is exactly True (boolean)
- # if output.done:
- # break
+ if result.done:
+ print('\n✅ Task completed successfully')
+ break
# time.sleep(0.5)
except KeyboardInterrupt: