in evals/elsuite/multistep_web_tasks/webarena/browser_env/processors.py [0:0]
def retrieve_viewport_info(self, info: BrowserState) -> None:
"""Add viewport related information to the DOMTree
1. add union bound, which is a union of all the bounds of the nodes in the subtree
This is only used when current_viewport_only is enabled since it is quite slow
TODO[robert1003]: improve
"""
tree = info["DOMTree"]
document = tree["documents"][0]
nodes = document["nodes"]
parent = nodes["parentIndex"]
node_names = nodes["nodeName"]
layout = document["layout"]
layout_node_cursor = layout["nodeIndex"]
bounds = layout["bounds"]
graph = defaultdict(lambda: [])
assert len(node_names) == len(parent)
for node_idx in range(len(node_names)):
parent_idx = parent[node_idx]
if parent_idx != -1:
graph[parent_idx].append(node_idx)
union_bounds: list[Optional[list[float]]] = [None for _ in bounds]
def valid_bbox(bound: Optional[list[float]]) -> bool:
if bound is None:
return False
# no width or height
if np.isclose(bound[2], 0):
return False
if np.isclose(bound[3], 0):
return False
return True
def add_union_bound(idx: int) -> Optional[list[float]]:
if idx in layout_node_cursor:
cursor = layout_node_cursor.index(idx)
node_bound = bounds[cursor].copy()
tree_bounds: list[Any] = [node_bound]
for child_idx in graph[idx]:
child_bound = add_union_bound(child_idx)
tree_bounds.append(child_bound.copy() if child_bound else None)
tree_bounds = [b for b in tree_bounds if valid_bbox(b)]
# convert to absolute coordinates
for i in range(len(tree_bounds)):
tree_bounds[i][2] = tree_bounds[i][0] + tree_bounds[i][2]
tree_bounds[i][3] = tree_bounds[i][1] + tree_bounds[i][3]
if len(tree_bounds) == 0:
assert not valid_bbox(node_bound)
node_union_bound = [0.0, 0.0, 0.0, 0.0]
else:
left_bound = min([b[0] for b in tree_bounds])
top_bound = min([b[1] for b in tree_bounds])
right_bound = max([b[2] for b in tree_bounds])
bottom_bound = max([b[3] for b in tree_bounds])
node_union_bound = [
left_bound,
top_bound,
right_bound - left_bound,
bottom_bound - top_bound,
]
# update the list
union_bounds[cursor] = node_union_bound
else:
node_union_bound = None
return node_union_bound
add_union_bound(0)
info["DOMTree"]["documents"][0]["layout"]["unionBounds"] = union_bounds