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
Refactor ActionResult to standardize the inclusion of extracted content, replacing update_only_read_state with include_extracted_content_only_once across multiple services. This change enhances clarity in memory management and ensures consistent handling of extracted content.
Magnus Müller90ae26316ea2
diff --git a/browser_use/agent/message_manager/service.py b/browser_use/agent/message_manager/service.pyindex a687e49a3..00e90cb6c 100644--- a/browser_use/agent/message_manager/service.py+++ b/browser_use/agent/message_manager/service.py@@ -381,14 +381,14 @@ My next action is to click on the iPhone link at index [4] to navigate to Apple'action_results = ''for idx, action_result in enumerate(result):- if action_result.update_only_read_state and action_result.extracted_content:+ if action_result.include_extracted_content_only_once and action_result.extracted_content:self.read_state_description += action_result.extracted_content + '\n'logger.debug(f'Added extracted_content to read_state_description: {action_result.extracted_content}')if action_result.long_term_memory:action_results += f'Action {idx + 1} Result: {action_result.long_term_memory}\n'logger.debug(f'Added long_term_memory to action_results: {action_result.long_term_memory}')- elif action_result.extracted_content and not action_result.update_only_read_state:+ elif action_result.extracted_content and not action_result.include_extracted_content_only_once:action_results += f'Action {idx + 1} Result: {action_result.extracted_content}\n'logger.debug(f'Added extracted_content to action_results: {action_result.extracted_content}')diff --git a/browser_use/agent/service.py b/browser_use/agent/service.pyindex 314557d9a..73212abed 100644--- a/browser_use/agent/service.py+++ b/browser_use/agent/service.py@@ -513,7 +513,10 @@ class Agent(Generic[Context]):logger.info(f'💾 {display_result}')memory = result.split('\n')[-1]return ActionResult(- extracted_content=result, include_in_memory=True, long_term_memory=memory, update_only_read_state=True+ extracted_content=result,+ include_in_memory=True,+ long_term_memory=memory,+ include_extracted_content_only_once=True,)def _set_message_context(self) -> str | None:diff --git a/browser_use/agent/views.py b/browser_use/agent/views.pyindex dd5a7117b..0f5dfeeff 100644--- a/browser_use/agent/views.py+++ b/browser_use/agent/views.py@@ -120,7 +120,7 @@ class ActionResult(BaseModel):# if update_only_read_state is True we add the extracted_content to the agent context only once for the next step# if update_only_read_state is False we add the extracted_content to the agent long term memory if no long_term_memory is providedextracted_content: str | None = None- update_only_read_state: bool = False # Whether the extracted content should be used to update the read_state+ include_extracted_content_only_once: bool = False # Whether the extracted content should be used to update the read_state# Deprecatedinclude_in_memory: bool = False # whether to include in extracted_content inside long_term_memorydiff --git a/browser_use/controller/service.py b/browser_use/controller/service.pyindex 2301b6452..388d01fd9 100644--- a/browser_use/controller/service.py+++ b/browser_use/controller/service.py@@ -435,12 +435,18 @@ Explain the content of the page and that the requested information is not availaoutput = await page_extraction_llm.ainvoke(template.format(query=query, page=content))output_text = output.contentextracted_content = f'Page Link: {page.url}\nQuery: {query}\nExtracted Content:\n{output_text}'- memory = f'Extracted content from {page.url} for query "{query}"'++ # if content is small include it to memory+ if len(extracted_content) < 1000:+ memory = extracted_content+ include_extracted_content_only_once = False+ else:+ memory = f'Extracted content from {page.url} for query "{query}"'+ include_extracted_content_only_once = Truelogger.info(f'📄 {memory}')return ActionResult(extracted_content=extracted_content,- include_in_memory=False,- update_only_read_state=True,+ include_extracted_content_only_once=include_extracted_content_only_once,long_term_memory=memory,)except Exception as e:@@ -472,7 +478,7 @@ Explain the content of the page and that the requested information is not availaextracted_content=msg,include_in_memory=False,long_term_memory='Retrieved accessibility tree',- update_only_read_state=True,+ include_extracted_content_only_once=True,)@self.registry.action(@@ -672,7 +678,7 @@ Explain the content of the page and that the requested information is not availaextracted_content=msg,include_in_memory=True,long_term_memory=f'Found dropdown options for index {index}.',- update_only_read_state=True,+ include_extracted_content_only_once=True,)else:msg = 'No options found in any frame for dropdown'@@ -1010,7 +1016,7 @@ Explain the content of the page and that the requested information is not availaextracted_content=extracted_tsv,include_in_memory=True,long_term_memory='Retrieved sheet contents',- update_only_read_state=True,+ include_extracted_content_only_once=True,)@self.registry.action('Google Sheets: Get the contents of a cell or range of cells', domains=['https://docs.google.com'])@@ -1026,7 +1032,7 @@ Explain the content of the page and that the requested information is not availaextracted_content=extracted_tsv,include_in_memory=True,long_term_memory=f'Retrieved contents from {cell_or_range}',- update_only_read_state=True,+ include_extracted_content_only_once=True,)@self.registry.action(diff --git a/eval/service.py b/eval/service.pyindex cd86a48a4..2e51e807f 100644--- a/eval/service.py+++ b/eval/service.py@@ -464,7 +464,9 @@ def create_controller_with_serp_search():# Convert to string for the agentserp_data_str = json.dumps(serp_data)- return ActionResult(extracted_content=serp_data_str, include_in_memory=False, update_only_read_state=True)+ return ActionResult(+ extracted_content=serp_data_str, include_in_memory=False, include_extracted_content_only_once=True+ )except Exception as e:logger.error(f'Error in SERP search: {type(e).__name__}: {e}')diff --git a/examples/custom-functions/advanced_search.py b/examples/custom-functions/advanced_search.pyindex fe50a87de..d22d9c95f 100644--- a/examples/custom-functions/advanced_search.py+++ b/examples/custom-functions/advanced_search.py@@ -63,7 +63,7 @@ async def search_web(query: str):# to stringorganic_str = json.dumps(organic)- return ActionResult(extracted_content=organic_str, include_in_memory=False, update_only_read_state=True)+ return ActionResult(extracted_content=organic_str, include_in_memory=False, include_extracted_content_only_once=True)names = [