-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnapshot.py
More file actions
1027 lines (905 loc) · 36.7 KB
/
snapshot.py
File metadata and controls
1027 lines (905 loc) · 36.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Snapshot functionality - calls window.sentience.snapshot() or server-side API
"""
import asyncio
import json
import os
import time
from typing import Any, Optional
import requests
from .browser import AsyncSentienceBrowser, SentienceBrowser
from .browser_evaluator import BrowserEvaluator
from .constants import PREDICATE_API_URL
from .models import Snapshot, SnapshotOptions
from .sentience_methods import SentienceMethod
# Maximum payload size for API requests (10MB server limit)
MAX_PAYLOAD_BYTES = 10 * 1024 * 1024
class SnapshotGatewayError(RuntimeError):
"""
Structured error for server-side (gateway) snapshot failures.
Keeps HTTP status/URL/response details available to callers for better logging/debugging.
Subclasses RuntimeError for backward compatibility.
"""
def __init__(
self,
message: str,
*,
status_code: int | None = None,
url: str | None = None,
request_id: str | None = None,
response_text: str | None = None,
cause: Exception | None = None,
) -> None:
super().__init__(message)
self.status_code = status_code
self.url = url
self.request_id = request_id
self.response_text = response_text
# Note: callers should use `raise ... from cause` to preserve chaining.
_ = cause
@staticmethod
def _snip(s: str | None, n: int = 400) -> str | None:
if not s:
return None
t = str(s).replace("\n", " ").replace("\r", " ").strip()
return t[:n]
@classmethod
def from_httpx(cls, e: Exception) -> "SnapshotGatewayError":
status_code = None
url = None
request_id = None
body = None
try:
resp = getattr(e, "response", None)
if resp is not None:
status_code = getattr(resp, "status_code", None)
try:
url = str(getattr(resp, "url", None) or "")
except Exception:
url = None
try:
headers = getattr(resp, "headers", None) or {}
request_id = headers.get("x-request-id") or headers.get("x-trace-id")
except Exception:
request_id = None
try:
body = getattr(resp, "text", None)
except Exception:
body = None
req = getattr(e, "request", None)
if url is None and req is not None:
try:
url = str(getattr(req, "url", None) or "")
except Exception:
url = None
except Exception:
pass
msg = "Server-side snapshot API failed"
bits = []
if status_code is not None:
bits.append(f"status={status_code}")
if url:
bits.append(f"url={url}")
if request_id:
bits.append(f"request_id={request_id}")
body_snip = cls._snip(body)
if body_snip:
bits.append(f"body={body_snip}")
# If we don't have an HTTP status/response body, this is usually a transport error
# (timeout, DNS, connection reset). Preserve at least the exception type + message.
if status_code is None and not body_snip:
try:
err_s = cls._snip(str(e), 220)
except Exception:
err_s = None
bits.append(f"err_type={type(e).__name__}")
if err_s:
bits.append(f"err={err_s}")
else:
# Some transport errors (e.g. httpx.ReadError) can stringify to "".
# Include repr() so callers can still see the exception type/shape.
try:
bits.append(f"err_repr={cls._snip(repr(e), 220)}")
except Exception:
pass
if bits:
msg = f"{msg}: " + " ".join(bits)
msg = msg + ". Try using use_api=False to use local extension instead."
return cls(
msg,
status_code=int(status_code) if status_code is not None else None,
url=url,
request_id=str(request_id) if request_id else None,
response_text=body_snip,
cause=e,
)
@classmethod
def from_requests(cls, e: Exception) -> "SnapshotGatewayError":
status_code = None
url = None
request_id = None
body = None
try:
resp = getattr(e, "response", None)
if resp is not None:
status_code = getattr(resp, "status_code", None)
try:
url = str(getattr(resp, "url", None) or "")
except Exception:
url = None
try:
headers = getattr(resp, "headers", None) or {}
request_id = headers.get("x-request-id") or headers.get("x-trace-id")
except Exception:
request_id = None
try:
body = getattr(resp, "text", None)
except Exception:
body = None
except Exception:
pass
msg = "Server-side snapshot API failed"
bits = []
if status_code is not None:
bits.append(f"status={status_code}")
if url:
bits.append(f"url={url}")
if request_id:
bits.append(f"request_id={request_id}")
body_snip = cls._snip(body)
if body_snip:
bits.append(f"body={body_snip}")
if status_code is None and not body_snip:
try:
err_s = cls._snip(str(e), 220)
except Exception:
err_s = None
bits.append(f"err_type={type(e).__name__}")
if err_s:
bits.append(f"err={err_s}")
else:
try:
bits.append(f"err_repr={cls._snip(repr(e), 220)}")
except Exception:
pass
if bits:
msg = f"{msg}: " + " ".join(bits)
msg = msg + ". Try using use_api=False to use local extension instead."
return cls(
msg,
status_code=int(status_code) if status_code is not None else None,
url=url,
request_id=str(request_id) if request_id else None,
response_text=body_snip,
cause=e,
)
def _is_execution_context_destroyed_error(e: Exception) -> bool:
"""
Playwright can throw while a navigation is in-flight, invalidating the JS execution context.
Common symptoms:
- "Execution context was destroyed, most likely because of a navigation"
- "Cannot find context with specified id"
"""
msg = str(e).lower()
return (
"execution context was destroyed" in msg
or "most likely because of a navigation" in msg
or "cannot find context with specified id" in msg
)
async def _page_evaluate_with_nav_retry(
page: Any,
expression: str,
arg: Any = None,
*,
retries: int = 2,
settle_timeout_ms: int = 10000,
) -> Any:
"""
Evaluate JS with a small retry loop if the page is mid-navigation.
This prevents flaky crashes when callers snapshot right after triggering a navigation
(e.g., pressing Enter on Google).
"""
last_err: Exception | None = None
for attempt in range(retries + 1):
try:
if arg is None:
return await page.evaluate(expression)
return await page.evaluate(expression, arg)
except Exception as e:
last_err = e
if not _is_execution_context_destroyed_error(e) or attempt >= retries:
raise
try:
await page.wait_for_load_state("domcontentloaded", timeout=settle_timeout_ms)
except Exception:
pass
await asyncio.sleep(0.25)
raise last_err if last_err else RuntimeError("Page.evaluate failed")
async def _wait_for_function_with_nav_retry(
page: Any,
expression: str,
*,
timeout_ms: int,
retries: int = 2,
) -> None:
last_err: Exception | None = None
for attempt in range(retries + 1):
try:
await page.wait_for_function(expression, timeout=timeout_ms)
return
except Exception as e:
last_err = e
if not _is_execution_context_destroyed_error(e) or attempt >= retries:
raise
try:
await page.wait_for_load_state("domcontentloaded", timeout=timeout_ms)
except Exception:
pass
await asyncio.sleep(0.25)
raise last_err if last_err else RuntimeError("wait_for_function failed")
def _build_snapshot_payload(
raw_result: dict[str, Any],
options: SnapshotOptions,
) -> dict[str, Any]:
"""
Build payload dict for gateway snapshot API.
Shared helper used by both sync and async snapshot implementations.
"""
diagnostics = raw_result.get("diagnostics") or {}
client_metrics = None
client_diagnostics = None
try:
client_metrics = diagnostics.get("metrics")
except Exception:
client_metrics = None
try:
captcha = diagnostics.get("captcha")
requires_vision = diagnostics.get("requires_vision")
requires_vision_reason = diagnostics.get("requires_vision_reason")
if any(x is not None for x in [captcha, requires_vision, requires_vision_reason]):
client_diagnostics = {}
if captcha is not None:
client_diagnostics["captcha"] = captcha
if requires_vision is not None:
client_diagnostics["requires_vision"] = bool(requires_vision)
if requires_vision_reason is not None:
client_diagnostics["requires_vision_reason"] = str(requires_vision_reason)
except Exception:
client_diagnostics = None
return {
"raw_elements": raw_result.get("raw_elements", []),
"url": raw_result.get("url", ""),
"viewport": raw_result.get("viewport"),
"goal": options.goal,
"options": {
"limit": options.limit,
"filter": options.filter.model_dump() if options.filter else None,
},
"client_metrics": client_metrics,
"client_diagnostics": client_diagnostics,
}
def _validate_payload_size(payload_json: str) -> None:
"""
Validate payload size before sending to gateway.
Raises ValueError if payload exceeds server limit.
"""
payload_size = len(payload_json.encode("utf-8"))
if payload_size > MAX_PAYLOAD_BYTES:
raise ValueError(
f"Payload size ({payload_size / 1024 / 1024:.2f}MB) exceeds server limit "
f"({MAX_PAYLOAD_BYTES / 1024 / 1024:.0f}MB). "
f"Try reducing the number of elements on the page or filtering elements."
)
def _post_snapshot_to_gateway_sync(
payload: dict[str, Any],
api_key: str,
api_url: str = PREDICATE_API_URL,
*,
timeout_s: float | None = None,
) -> dict[str, Any]:
"""
Post snapshot payload to gateway (synchronous).
Used by sync snapshot() function.
"""
payload_json = json.dumps(payload)
_validate_payload_size(payload_json)
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
try:
timeout = 30 if timeout_s is None else float(timeout_s)
response = requests.post(
f"{api_url}/v1/snapshot",
data=payload_json,
headers=headers,
timeout=timeout,
)
response.raise_for_status()
return response.json()
except requests.exceptions.HTTPError as e:
raise SnapshotGatewayError.from_requests(e) from e
except requests.exceptions.RequestException as e:
# Network/timeouts/etc (no status code available)
raise SnapshotGatewayError.from_requests(e) from e
async def _post_snapshot_to_gateway_async(
payload: dict[str, Any],
api_key: str,
api_url: str = PREDICATE_API_URL,
*,
timeout_s: float | None = None,
) -> dict[str, Any]:
"""
Post snapshot payload to gateway (asynchronous).
Used by async backend snapshot() function.
"""
# Lazy import httpx - only needed for async API calls
import httpx
payload_json = json.dumps(payload)
_validate_payload_size(payload_json)
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
timeout = 30.0 if timeout_s is None else float(timeout_s)
async with httpx.AsyncClient(timeout=timeout) as client:
try:
response = await client.post(
f"{api_url}/v1/snapshot",
content=payload_json,
headers=headers,
)
response.raise_for_status()
return response.json()
except httpx.HTTPStatusError as e:
raise SnapshotGatewayError.from_httpx(e) from e
except httpx.RequestError as e:
raise SnapshotGatewayError.from_httpx(e) from e
except Exception as e:
# JSON decode or other unexpected issues — keep details if possible.
raise SnapshotGatewayError.from_httpx(e) from e
def _merge_api_result_with_local(
api_result: dict[str, Any],
raw_result: dict[str, Any],
) -> dict[str, Any]:
"""
Merge API result with local data (screenshot, etc.).
Shared helper used by both sync and async snapshot implementations.
"""
return {
"status": api_result.get("status", "success"),
"timestamp": api_result.get("timestamp"),
"url": api_result.get("url", raw_result.get("url", "")),
"viewport": api_result.get("viewport", raw_result.get("viewport")),
"elements": api_result.get("elements", []),
"screenshot": raw_result.get("screenshot"), # Keep local screenshot
"screenshot_format": raw_result.get("screenshot_format"),
"error": api_result.get("error"),
# Phase 2: Runtime stability/debug info
"diagnostics": api_result.get("diagnostics", raw_result.get("diagnostics")),
# Phase 2: Ordinal support - dominant group key from Gateway
"dominant_group_key": api_result.get("dominant_group_key"),
}
def _save_trace_to_file(raw_elements: list[dict[str, Any]], trace_path: str | None = None) -> None:
"""
Save raw_elements to a JSON file for benchmarking/training
Args:
raw_elements: Raw elements data from snapshot
trace_path: Path to save trace file. If None, uses "trace_{timestamp}.json"
"""
# Default filename if none provided
filename = trace_path or f"trace_{int(time.time())}.json"
# Ensure directory exists
directory = os.path.dirname(filename)
if directory:
os.makedirs(directory, exist_ok=True)
# Save the raw elements to JSON
with open(filename, "w") as f:
json.dump(raw_elements, f, indent=2)
print(f"[SDK] Trace saved to: {filename}")
def snapshot(
browser: SentienceBrowser,
options: SnapshotOptions | None = None,
) -> Snapshot:
"""
Take a snapshot of the current page
Args:
browser: SentienceBrowser instance
options: Snapshot options (screenshot, limit, filter, etc.)
If None, uses default options.
Returns:
Snapshot object
Example:
# Basic snapshot with defaults
snap = snapshot(browser)
# With options
snap = snapshot(browser, SnapshotOptions(
screenshot=True,
limit=100,
show_overlay=True
))
"""
# Use default options if none provided
if options is None:
options = SnapshotOptions()
# Resolve API key: predicate_api_key is canonical, sentience_api_key kept for compatibility.
# This allows browser-use users to pass api_key via options without SentienceBrowser.
effective_api_key = options.predicate_api_key or options.sentience_api_key or browser.api_key
# Determine if we should use server-side API
should_use_api = (
options.use_api if options.use_api is not None else (effective_api_key is not None)
)
if should_use_api and effective_api_key:
# Use server-side API (Pro/Enterprise tier)
return _snapshot_via_api(browser, options, effective_api_key)
else:
# Use local extension (Free tier)
return _snapshot_via_extension(browser, options)
def _snapshot_via_extension(
browser: SentienceBrowser,
options: SnapshotOptions,
) -> Snapshot:
"""Take snapshot using local extension (Free tier)"""
if not browser.page:
raise RuntimeError("Browser not started. Call browser.start() first.")
# CRITICAL: Wait for extension injection to complete (CSP-resistant architecture)
# The new architecture loads injected_api.js asynchronously, so window.sentience
# may not be immediately available after page load
BrowserEvaluator.wait_for_extension(browser.page, timeout_ms=5000)
# Build options dict for extension API (exclude save_trace/trace_path)
ext_options: dict[str, Any] = {}
if options.screenshot is not False:
# Serialize ScreenshotConfig to dict if it's a Pydantic model
if hasattr(options.screenshot, "model_dump"):
ext_options["screenshot"] = options.screenshot.model_dump()
else:
ext_options["screenshot"] = options.screenshot
if options.limit != 50:
ext_options["limit"] = options.limit
if options.filter is not None:
ext_options["filter"] = (
options.filter.model_dump() if hasattr(options.filter, "model_dump") else options.filter
)
# Call extension API
result = browser.page.evaluate(
"""
(options) => {
return window.sentience.snapshot(options);
}
""",
ext_options,
)
# Save trace if requested
if options.save_trace:
_save_trace_to_file(result.get("raw_elements", []), options.trace_path)
# Validate and parse with Pydantic
snapshot_obj = Snapshot(**result)
# Show visual overlay if requested
if options.show_overlay:
# Prefer processed semantic elements for overlay (have bbox/importance/visual_cues).
# raw_elements may not match the overlay renderer's expected shape.
elements_for_overlay = result.get("elements") or result.get("raw_elements") or []
if elements_for_overlay:
browser.page.evaluate(
"""
(elements) => {
if (window.sentience && window.sentience.showOverlay) {
window.sentience.showOverlay(elements, null);
}
}
""",
elements_for_overlay,
)
# Show grid overlay if requested
if options.show_grid:
# Get all grids (don't filter by grid_id here - we want to show all but highlight the target)
grids = snapshot_obj.get_grid_bounds(grid_id=None)
if grids:
# Convert GridInfo to dict for JavaScript
grid_dicts = [grid.model_dump() for grid in grids]
# Pass grid_id as targetGridId to highlight it in red
target_grid_id = options.grid_id if options.grid_id is not None else None
browser.page.evaluate(
"""
(grids, targetGridId) => {
if (window.sentience && window.sentience.showGrid) {
window.sentience.showGrid(grids, targetGridId);
} else {
console.warn('[SDK] showGrid not available in extension');
}
}
""",
grid_dicts,
target_grid_id,
)
return snapshot_obj
def _snapshot_via_api(
browser: SentienceBrowser,
options: SnapshotOptions,
api_key: str,
) -> Snapshot:
"""Take snapshot using server-side API (Pro/Enterprise tier)"""
if not browser.page:
raise RuntimeError("Browser not started. Call browser.start() first.")
# Use browser.api_url if set, otherwise default
api_url = browser.api_url or PREDICATE_API_URL
# CRITICAL: Wait for extension injection to complete (CSP-resistant architecture)
# Even for API mode, we need the extension to collect raw data locally
BrowserEvaluator.wait_for_extension(browser.page, timeout_ms=5000)
# Step 1: Get raw data from local extension (always happens locally)
raw_options: dict[str, Any] = {}
if options.screenshot is not False:
raw_options["screenshot"] = options.screenshot
# Important: also pass limit/filter to extension to keep raw_elements payload bounded.
# Without this, large pages (e.g. Amazon) can exceed gateway request size limits (HTTP 413).
if options.limit != 50:
raw_options["limit"] = options.limit
if options.filter is not None:
raw_options["filter"] = (
options.filter.model_dump() if hasattr(options.filter, "model_dump") else options.filter
)
raw_result = BrowserEvaluator.invoke(browser.page, SentienceMethod.SNAPSHOT, **raw_options)
# Save trace if requested (save raw data before API processing)
if options.save_trace:
_save_trace_to_file(raw_result.get("raw_elements", []), options.trace_path)
# Step 2: Send to server for smart ranking/filtering
# Use raw_elements (raw data) instead of elements (processed data)
# Server validates API key and applies proprietary ranking logic
payload = _build_snapshot_payload(raw_result, options)
try:
api_result = _post_snapshot_to_gateway_sync(
payload,
api_key,
api_url,
timeout_s=options.gateway_timeout_s,
)
# Merge API result with local data (screenshot, etc.)
snapshot_data = _merge_api_result_with_local(api_result, raw_result)
# Create snapshot object
snapshot_obj = Snapshot(**snapshot_data)
# Show visual overlay if requested (use API-ranked elements)
if options.show_overlay:
elements = api_result.get("elements", [])
if elements:
browser.page.evaluate(
"""
(elements) => {
if (window.sentience && window.sentience.showOverlay) {
window.sentience.showOverlay(elements, null);
}
}
""",
elements,
)
# Show grid overlay if requested
if options.show_grid:
# Get all grids (don't filter by grid_id here - we want to show all but highlight the target)
grids = snapshot_obj.get_grid_bounds(grid_id=None)
if grids:
grid_dicts = [grid.model_dump() for grid in grids]
# Pass grid_id as targetGridId to highlight it in red
target_grid_id = options.grid_id if options.grid_id is not None else None
browser.page.evaluate(
"""
(grids, targetGridId) => {
if (window.sentience && window.sentience.showGrid) {
window.sentience.showGrid(grids, targetGridId);
} else {
console.warn('[SDK] showGrid not available in extension');
}
}
""",
grid_dicts,
target_grid_id,
)
return snapshot_obj
except requests.exceptions.RequestException as e:
raise RuntimeError(f"API request failed: {e}") from e
# ========== Async Snapshot Functions ==========
async def snapshot_async(
browser: AsyncSentienceBrowser,
options: SnapshotOptions | None = None,
) -> Snapshot:
"""
Take a snapshot of the current page (async)
Args:
browser: AsyncSentienceBrowser instance
options: Snapshot options (screenshot, limit, filter, etc.)
If None, uses default options.
Returns:
Snapshot object
Example:
# Basic snapshot with defaults
snap = await snapshot_async(browser)
# With options
snap = await snapshot_async(browser, SnapshotOptions(
screenshot=True,
limit=100,
show_overlay=True
))
"""
# Use default options if none provided
if options is None:
options = SnapshotOptions()
# Resolve API key: predicate_api_key is canonical, sentience_api_key kept for compatibility.
# This allows browser-use users to pass api_key via options without SentienceBrowser.
effective_api_key = options.predicate_api_key or options.sentience_api_key or browser.api_key
# Determine if we should use server-side API
should_use_api = (
options.use_api if options.use_api is not None else (effective_api_key is not None)
)
if should_use_api and effective_api_key:
# Use server-side API (Pro/Enterprise tier)
return await _snapshot_via_api_async(browser, options, effective_api_key)
else:
# Use local extension (Free tier)
return await _snapshot_via_extension_async(browser, options)
async def _snapshot_via_extension_async(
browser: AsyncSentienceBrowser,
options: SnapshotOptions,
) -> Snapshot:
"""Take snapshot using local extension (Free tier) - async"""
if not browser.page:
raise RuntimeError("Browser not started. Call await browser.start() first.")
# Wait for extension injection to complete
try:
await _wait_for_function_with_nav_retry(
browser.page,
"typeof window.sentience !== 'undefined'",
timeout_ms=5000,
)
except Exception as e:
try:
diag = await _page_evaluate_with_nav_retry(
browser.page,
"""() => ({
sentience_defined: typeof window.sentience !== 'undefined',
extension_id: document.documentElement.dataset.sentienceExtensionId || 'not set',
url: window.location.href
})""",
)
except Exception:
diag = {"error": "Could not gather diagnostics"}
raise RuntimeError(
f"Sentience extension failed to inject window.sentience API. "
f"Is the extension loaded? Diagnostics: {diag}"
) from e
# Build options dict for extension API
ext_options: dict[str, Any] = {}
if options.screenshot is not False:
# Serialize ScreenshotConfig to dict if it's a Pydantic model
if hasattr(options.screenshot, "model_dump"):
ext_options["screenshot"] = options.screenshot.model_dump()
else:
ext_options["screenshot"] = options.screenshot
if options.limit != 50:
ext_options["limit"] = options.limit
if options.filter is not None:
ext_options["filter"] = (
options.filter.model_dump() if hasattr(options.filter, "model_dump") else options.filter
)
# Call extension API
result = await _page_evaluate_with_nav_retry(
browser.page,
"""
(options) => {
return window.sentience.snapshot(options);
}
""",
ext_options,
)
if result.get("error"):
print(f" Snapshot error: {result.get('error')}")
# Save trace if requested
if options.save_trace:
_save_trace_to_file(result.get("raw_elements", []), options.trace_path)
# Extract screenshot_format from data URL if not provided by extension
if result.get("screenshot") and not result.get("screenshot_format"):
screenshot_data_url = result.get("screenshot", "")
if screenshot_data_url.startswith("data:image/"):
# Extract format from "data:image/jpeg;base64,..." or "data:image/png;base64,..."
format_match = screenshot_data_url.split(";")[0].split("/")[-1]
if format_match in ["jpeg", "jpg", "png"]:
result["screenshot_format"] = "jpeg" if format_match in ["jpeg", "jpg"] else "png"
# Validate and parse with Pydantic
snapshot_obj = Snapshot(**result)
# Show visual overlay if requested
if options.show_overlay:
# Prefer processed semantic elements for overlay (have bbox/importance/visual_cues).
# raw_elements may not match the overlay renderer's expected shape.
elements_for_overlay = result.get("elements") or result.get("raw_elements") or []
if elements_for_overlay:
await _page_evaluate_with_nav_retry(
browser.page,
"""
(elements) => {
if (window.sentience && window.sentience.showOverlay) {
window.sentience.showOverlay(elements, null);
}
}
""",
elements_for_overlay,
)
# Show grid overlay if requested
if options.show_grid:
# Get all grids (don't filter by grid_id here - we want to show all but highlight the target)
grids = snapshot_obj.get_grid_bounds(grid_id=None)
if grids:
grid_dicts = [grid.model_dump() for grid in grids]
# Pass grid_id as targetGridId to highlight it in red
target_grid_id = options.grid_id if options.grid_id is not None else None
await _page_evaluate_with_nav_retry(
browser.page,
"""
(args) => {
const [grids, targetGridId] = args;
if (window.sentience && window.sentience.showGrid) {
window.sentience.showGrid(grids, targetGridId);
} else {
console.warn('[SDK] showGrid not available in extension');
}
}
""",
[grid_dicts, target_grid_id],
)
return snapshot_obj
async def _snapshot_via_api_async(
browser: AsyncSentienceBrowser,
options: SnapshotOptions,
api_key: str,
) -> Snapshot:
"""Take snapshot using server-side API (Pro/Enterprise tier) - async"""
if not browser.page:
raise RuntimeError("Browser not started. Call await browser.start() first.")
# Use browser.api_url if set, otherwise default
api_url = browser.api_url or PREDICATE_API_URL
# Wait for extension injection
try:
await _wait_for_function_with_nav_retry(
browser.page,
"typeof window.sentience !== 'undefined'",
timeout_ms=5000,
)
except Exception as e:
raise RuntimeError(
"Sentience extension failed to inject. Cannot collect raw data for API processing."
) from e
# Step 1: Get raw data from local extension (including screenshot)
raw_options: dict[str, Any] = {}
screenshot_requested = False
if options.screenshot is not False:
screenshot_requested = True
# Serialize ScreenshotConfig to dict if it's a Pydantic model
if hasattr(options.screenshot, "model_dump"):
raw_options["screenshot"] = options.screenshot.model_dump()
else:
raw_options["screenshot"] = options.screenshot
# Important: also pass limit/filter to extension to keep raw_elements payload bounded.
# Without this, large pages (e.g. Amazon) can exceed gateway request size limits (HTTP 413).
if options.limit != 50:
raw_options["limit"] = options.limit
if options.filter is not None:
raw_options["filter"] = (
options.filter.model_dump() if hasattr(options.filter, "model_dump") else options.filter
)
raw_result = await _page_evaluate_with_nav_retry(
browser.page,
"""
(options) => {
return window.sentience.snapshot(options);
}
""",
raw_options,
)
# Extract screenshot from raw result (extension captures it, but API doesn't return it)
screenshot_data_url = raw_result.get("screenshot")
screenshot_format = None
if screenshot_data_url:
# Extract format from data URL
if screenshot_data_url.startswith("data:image/"):
format_match = screenshot_data_url.split(";")[0].split("/")[-1]
if format_match in ["jpeg", "jpg", "png"]:
screenshot_format = "jpeg" if format_match in ["jpeg", "jpg"] else "png"
# Save trace if requested
if options.save_trace:
_save_trace_to_file(raw_result.get("raw_elements", []), options.trace_path)
# Step 2: Send to server for smart ranking/filtering
payload = {
"raw_elements": raw_result.get("raw_elements", []),
"url": raw_result.get("url", ""),
"viewport": raw_result.get("viewport"),
"goal": options.goal,
"options": {
"limit": options.limit,
"filter": options.filter.model_dump() if options.filter else None,
},
}
# Check payload size
payload_json = json.dumps(payload)
payload_size = len(payload_json.encode("utf-8"))
if payload_size > MAX_PAYLOAD_BYTES:
raise ValueError(
f"Payload size ({payload_size / 1024 / 1024:.2f}MB) exceeds server limit "
f"({MAX_PAYLOAD_BYTES / 1024 / 1024:.0f}MB). "
f"Try reducing the number of elements on the page or filtering elements."
)
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
try:
# Lazy import httpx - only needed for async API calls
import httpx
timeout = 30.0 if options.gateway_timeout_s is None else float(options.gateway_timeout_s)
async with httpx.AsyncClient(timeout=timeout) as client:
response = await client.post(
f"{api_url}/v1/snapshot",
content=payload_json,
headers=headers,
)
response.raise_for_status()
api_result = response.json()
# Extract screenshot format from data URL if not provided
if screenshot_data_url and not screenshot_format:
if screenshot_data_url.startswith("data:image/"):
format_match = screenshot_data_url.split(";")[0].split("/")[-1]
if format_match in ["jpeg", "jpg", "png"]:
screenshot_format = "jpeg" if format_match in ["jpeg", "jpg"] else "png"
# Merge API result with local data
snapshot_data = {
"status": api_result.get("status", "success"),
"timestamp": api_result.get("timestamp"),
"url": api_result.get("url", raw_result.get("url", "")),
"viewport": api_result.get("viewport", raw_result.get("viewport")),
"elements": api_result.get("elements", []),
"screenshot": screenshot_data_url, # Use the extracted screenshot
"screenshot_format": screenshot_format, # Use the extracted format
"error": api_result.get("error"),
}
# Create snapshot object
snapshot_obj = Snapshot(**snapshot_data)
# Show visual overlay if requested
if options.show_overlay:
elements = api_result.get("elements", [])
if elements:
await _page_evaluate_with_nav_retry(
browser.page,
"""
(elements) => {
if (window.sentience && window.sentience.showOverlay) {
window.sentience.showOverlay(elements, null);
}
}
""",
elements,
)
# Show grid overlay if requested
if options.show_grid:
# Get all grids (don't filter by grid_id here - we want to show all but highlight the target)
grids = snapshot_obj.get_grid_bounds(grid_id=None)