Skip to content

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

Split HistoryTreeProcessor in views and service

magmueller60fbbcae9303
diff --git a/browser_use/agent/message_manager/service.py b/browser_use/agent/message_manager/service.py
index d1ed36ceb..0263066e3 100644
--- a/browser_use/agent/message_manager/service.py
+++ b/browser_use/agent/message_manager/service.py
@@ -69,7 +69,7 @@ class MessageManager:
if r.error:
msg = HumanMessage(content=str(r.error)[-self.max_error_length :])
self._add_message_with_tokens(msg)
- result = None
+ result = None # if result in history, we dont want to add it again
# otherwise add state message and result to next message (which will not stay in memory)
state_message = AgentMessagePrompt(
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index d1bdf124e..be15c3fc9 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -30,7 +30,7 @@ from browser_use.agent.views import (
from browser_use.browser.views import BrowserState, BrowserStateHistory
from browser_use.controller.registry.views import ActionModel
from browser_use.controller.service import Controller
-from browser_use.dom.history_tree_processor import (
+from browser_use.dom.history_tree_processor.service import (
DOMHistoryElement,
HistoryTreeProcessor,
)
diff --git a/browser_use/agent/views.py b/browser_use/agent/views.py
index dd64c13a9..ba20045fb 100644
--- a/browser_use/agent/views.py
+++ b/browser_use/agent/views.py
@@ -10,7 +10,7 @@ from pydantic import BaseModel, ConfigDict, Field, ValidationError, create_model
from browser_use.browser.views import BrowserState, BrowserStateHistory
from browser_use.controller.registry.views import ActionModel
-from browser_use.dom.history_tree_processor import (
+from browser_use.dom.history_tree_processor.service import (
DOMElementNode,
DOMHistoryElement,
HistoryTreeProcessor,
diff --git a/browser_use/browser/views.py b/browser_use/browser/views.py
index 2953d564f..76a2de45c 100644
--- a/browser_use/browser/views.py
+++ b/browser_use/browser/views.py
@@ -3,7 +3,7 @@ from typing import Any, Optional
from pydantic import BaseModel
-from browser_use.dom.history_tree_processor import DOMHistoryElement
+from browser_use.dom.history_tree_processor.service import DOMHistoryElement
from browser_use.dom.views import DOMState
diff --git a/browser_use/dom/history_tree_processor.py b/browser_use/dom/history_tree_processor/service.py
similarity index 84%
rename from browser_use/dom/history_tree_processor.py
rename to browser_use/dom/history_tree_processor/service.py
index ee591ccce..32dd3e18b 100644
--- a/browser_use/dom/history_tree_processor.py
+++ b/browser_use/dom/history_tree_processor/service.py
@@ -2,40 +2,10 @@ import hashlib
from dataclasses import dataclass
from typing import Optional
+from browser_use.dom.history_tree_processor.view import DOMHistoryElement, HashedDomElement
from browser_use.dom.views import DOMElementNode
-@dataclass
-class HashedDomElement:
- """
- Hash of the dom element to be used as a unique identifier
- """
-
- branch_path_hash: str
- attributes_hash: str
- # text_hash: str
-
-
-@dataclass
-class DOMHistoryElement:
- tag_name: str
- xpath: str
- highlight_index: Optional[int]
- entire_parent_branch_path: list[str]
- attributes: dict[str, str]
- shadow_root: bool = False
-
- def to_dict(self) -> dict:
- return {
- 'tag_name': self.tag_name,
- 'xpath': self.xpath,
- 'highlight_index': self.highlight_index,
- 'entire_parent_branch_path': self.entire_parent_branch_path,
- 'attributes': self.attributes,
- 'shadow_root': self.shadow_root,
- }
-
-
class HistoryTreeProcessor:
""" "
Operations on the DOM elements
diff --git a/browser_use/dom/history_tree_processor/view.py b/browser_use/dom/history_tree_processor/view.py
new file mode 100644
index 000000000..a6604cf10
--- /dev/null
+++ b/browser_use/dom/history_tree_processor/view.py
@@ -0,0 +1,33 @@
+from dataclasses import dataclass
+from typing import Optional
+
+
+@dataclass
+class HashedDomElement:
+ """
+ Hash of the dom element to be used as a unique identifier
+ """
+
+ branch_path_hash: str
+ attributes_hash: str
+ # text_hash: str
+
+
+@dataclass
+class DOMHistoryElement:
+ tag_name: str
+ xpath: str
+ highlight_index: Optional[int]
+ entire_parent_branch_path: list[str]
+ attributes: dict[str, str]
+ shadow_root: bool = False
+
+ def to_dict(self) -> dict:
+ return {
+ 'tag_name': self.tag_name,
+ 'xpath': self.xpath,
+ 'highlight_index': self.highlight_index,
+ 'entire_parent_branch_path': self.entire_parent_branch_path,
+ 'attributes': self.attributes,
+ 'shadow_root': self.shadow_root,
+ }
diff --git a/browser_use/dom/views.py b/browser_use/dom/views.py
index 60b505819..910afa9af 100644
--- a/browser_use/dom/views.py
+++ b/browser_use/dom/views.py
@@ -1,6 +1,9 @@
from dataclasses import dataclass
+from functools import cached_property
from typing import TYPE_CHECKING, Dict, List, Optional
+from browser_use.dom.history_tree_processor.view import HashedDomElement
+
# Avoid circular import issues
if TYPE_CHECKING:
from .views import DOMElementNode
@@ -67,6 +70,14 @@ class DOMElementNode(DOMBaseNode):
return tag_str
+ @cached_property
+ def hash(self) -> HashedDomElement:
+ from browser_use.dom.history_tree_processor.service import (
+ HistoryTreeProcessor,
+ )
+
+ return HistoryTreeProcessor._hash_dom_element(self)
+
def get_all_text_till_next_clickable_element(self) -> str:
text_parts = []