Skip to content

Commit 4d23da1

Browse files
author
SentienceDEV
committed
add browser-use as optional extra
1 parent cbd09f7 commit 4d23da1

File tree

2 files changed

+94
-22
lines changed

2 files changed

+94
-22
lines changed

THIRD_PARTY_LICENSES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Third-Party Licenses
2+
3+
This product optionally depends on third-party open source software:
4+
5+
- **browser-use** (MIT License) — https://github.com/browser-use/browser-use

tests/test_sentience_context.py

Lines changed: 89 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,17 @@ class TestFormatSnapshotForLLM:
108108

109109
def test_basic_formatting(self) -> None:
110110
"""Test basic element formatting."""
111-
ctx = SentienceContext(top_element_selector=TopElementSelector(by_importance=10, from_dominant_group=5, by_position=5))
111+
ctx = SentienceContext(
112+
top_element_selector=TopElementSelector(
113+
by_importance=10, from_dominant_group=5, by_position=5
114+
)
115+
)
112116

113117
elements = [
114118
make_element(id=1, role="button", text="Click me", importance=80),
115-
make_element(id=2, role="link", text="Go home", importance=60, href="https://example.com"),
119+
make_element(
120+
id=2, role="link", text="Go home", importance=60, href="https://example.com"
121+
),
116122
]
117123
snap = make_snapshot(elements)
118124

@@ -130,7 +136,11 @@ def test_basic_formatting(self) -> None:
130136

131137
def test_is_primary_flag(self) -> None:
132138
"""Test is_primary flag is correctly extracted from visual_cues."""
133-
ctx = SentienceContext(top_element_selector=TopElementSelector(by_importance=10, from_dominant_group=5, by_position=5))
139+
ctx = SentienceContext(
140+
top_element_selector=TopElementSelector(
141+
by_importance=10, from_dominant_group=5, by_position=5
142+
)
143+
)
134144

135145
elements = [
136146
make_element(
@@ -166,7 +176,13 @@ def test_role_link_when_href(self) -> None:
166176
ctx = SentienceContext()
167177

168178
elements = [
169-
make_element(id=1, role="button", text="Button with href", importance=80, href="https://example.com"),
179+
make_element(
180+
id=1,
181+
role="button",
182+
text="Button with href",
183+
importance=80,
184+
href="https://example.com",
185+
),
170186
]
171187
snap = make_snapshot(elements)
172188

@@ -225,11 +241,15 @@ def test_generic_role_fallback(self) -> None:
225241

226242
def test_dominant_group_flag(self) -> None:
227243
"""Test DG flag is set correctly for dominant group elements."""
228-
ctx = SentienceContext(top_element_selector=TopElementSelector(by_importance=10, from_dominant_group=5))
244+
ctx = SentienceContext(
245+
top_element_selector=TopElementSelector(by_importance=10, from_dominant_group=5)
246+
)
229247

230248
elements = [
231249
make_element(id=1, role="link", text="In DG", importance=80, in_dominant_group=True),
232-
make_element(id=2, role="link", text="Not in DG", importance=70, in_dominant_group=False),
250+
make_element(
251+
id=2, role="link", text="Not in DG", importance=70, in_dominant_group=False
252+
),
233253
]
234254
snap = make_snapshot(elements)
235255

@@ -250,10 +270,23 @@ def test_rank_in_group_computation(self) -> None:
250270
)
251271

252272
elements = [
253-
make_element(id=1, role="link", text="Third", importance=70, doc_y=300.0, in_dominant_group=True),
254-
make_element(id=2, role="link", text="First", importance=80, doc_y=100.0, in_dominant_group=True),
255-
make_element(id=3, role="link", text="Second", importance=90, doc_y=200.0, in_dominant_group=True),
256-
make_element(id=4, role="button", text="Not in DG", importance=95, doc_y=50.0, in_dominant_group=False),
273+
make_element(
274+
id=1, role="link", text="Third", importance=70, doc_y=300.0, in_dominant_group=True
275+
),
276+
make_element(
277+
id=2, role="link", text="First", importance=80, doc_y=100.0, in_dominant_group=True
278+
),
279+
make_element(
280+
id=3, role="link", text="Second", importance=90, doc_y=200.0, in_dominant_group=True
281+
),
282+
make_element(
283+
id=4,
284+
role="button",
285+
text="Not in DG",
286+
importance=95,
287+
doc_y=50.0,
288+
in_dominant_group=False,
289+
),
257290
]
258291
snap = make_snapshot(elements)
259292

@@ -284,7 +317,9 @@ def test_href_compression(self) -> None:
284317
ctx = SentienceContext(top_element_selector=TopElementSelector(by_importance=10))
285318

286319
elements = [
287-
make_element(id=1, role="link", text="GitHub", importance=80, href="https://github.com/user/repo"),
320+
make_element(
321+
id=1, role="link", text="GitHub", importance=80, href="https://github.com/user/repo"
322+
),
288323
make_element(id=2, role="link", text="Local", importance=70, href="/api/items/123"),
289324
]
290325
snap = make_snapshot(elements)
@@ -308,7 +343,9 @@ def test_full_url_extracts_domain(self) -> None:
308343
ctx = SentienceContext()
309344

310345
# Note: _compress_href truncates to 10 chars
311-
assert ctx._compress_href("https://news.ycombinator.com/item?id=123") == "ycombinato" # truncated
346+
assert (
347+
ctx._compress_href("https://news.ycombinator.com/item?id=123") == "ycombinato"
348+
) # truncated
312349
assert ctx._compress_href("https://github.com/user/repo") == "github"
313350
assert ctx._compress_href("https://www.example.com/page") == "example"
314351

@@ -339,7 +376,11 @@ class TestElementSelection:
339376

340377
def test_top_by_importance(self) -> None:
341378
"""Test elements are selected by importance."""
342-
ctx = SentienceContext(top_element_selector=TopElementSelector(by_importance=2, from_dominant_group=0, by_position=0))
379+
ctx = SentienceContext(
380+
top_element_selector=TopElementSelector(
381+
by_importance=2, from_dominant_group=0, by_position=0
382+
)
383+
)
343384

344385
elements = [
345386
make_element(id=1, role="button", importance=50),
@@ -362,7 +403,11 @@ def test_top_by_importance(self) -> None:
362403

363404
def test_top_from_dominant_group(self) -> None:
364405
"""Test elements from dominant group are included."""
365-
ctx = SentienceContext(top_element_selector=TopElementSelector(by_importance=1, from_dominant_group=2, by_position=0))
406+
ctx = SentienceContext(
407+
top_element_selector=TopElementSelector(
408+
by_importance=1, from_dominant_group=2, by_position=0
409+
)
410+
)
366411

367412
elements = [
368413
make_element(id=1, role="button", importance=100), # Top by importance
@@ -386,7 +431,11 @@ def test_top_from_dominant_group(self) -> None:
386431

387432
def test_top_by_position(self) -> None:
388433
"""Test elements at top of page (lowest doc_y) are included."""
389-
ctx = SentienceContext(top_element_selector=TopElementSelector(by_importance=0, from_dominant_group=0, by_position=2))
434+
ctx = SentienceContext(
435+
top_element_selector=TopElementSelector(
436+
by_importance=0, from_dominant_group=0, by_position=2
437+
)
438+
)
390439

391440
elements = [
392441
make_element(id=1, role="button", importance=50, doc_y=500.0),
@@ -408,13 +457,26 @@ def test_top_by_position(self) -> None:
408457

409458
def test_deduplication(self) -> None:
410459
"""Test elements are not duplicated when selected by multiple criteria."""
411-
ctx = SentienceContext(top_element_selector=TopElementSelector(by_importance=2, from_dominant_group=2, by_position=2))
460+
ctx = SentienceContext(
461+
top_element_selector=TopElementSelector(
462+
by_importance=2, from_dominant_group=2, by_position=2
463+
)
464+
)
412465

413466
# Element 1 qualifies for all three criteria
414467
elements = [
415-
make_element(id=1, role="button", importance=100, doc_y=50.0, in_dominant_group=True, group_index=0),
468+
make_element(
469+
id=1,
470+
role="button",
471+
importance=100,
472+
doc_y=50.0,
473+
in_dominant_group=True,
474+
group_index=0,
475+
),
416476
make_element(id=2, role="button", importance=80, doc_y=100.0),
417-
make_element(id=3, role="link", importance=30, doc_y=200.0, in_dominant_group=True, group_index=1),
477+
make_element(
478+
id=3, role="link", importance=30, doc_y=200.0, in_dominant_group=True, group_index=1
479+
),
418480
]
419481
snap = make_snapshot(elements)
420482

@@ -435,17 +497,22 @@ async def test_build_returns_context_state(self) -> None:
435497
ctx = SentienceContext()
436498

437499
# Create mock snapshot
438-
mock_snap = make_snapshot([
439-
make_element(id=1, role="button", text="Click", importance=80),
440-
])
500+
mock_snap = make_snapshot(
501+
[
502+
make_element(id=1, role="button", text="Click", importance=80),
503+
]
504+
)
441505

442506
# Mock at the import location within the build() method
443507
mock_adapter = MagicMock()
444508
mock_adapter.create_backend = AsyncMock(return_value=MagicMock())
445509

446-
with patch.object(ctx, "_format_snapshot_for_llm", return_value="1|button|Click|80|0|0|-|0|"):
510+
with patch.object(
511+
ctx, "_format_snapshot_for_llm", return_value="1|button|Click|80|0|0|-|0|"
512+
):
447513
# Patch the imports that happen inside build()
448514
import sentience.backends.sentience_context as ctx_module
515+
449516
original_build = ctx.build
450517

451518
async def patched_build(browser_session, **kwargs):

0 commit comments

Comments
 (0)