Skip to content

Commit ae5c490

Browse files
authored
Merge pull request #86 from SentienceAPI/refix_snapshot
refactor snapshot function, to be consistent with snapshot in typescript
2 parents 4b7fee1 + 2482735 commit ae5c490

File tree

5 files changed

+27
-49
lines changed

5 files changed

+27
-49
lines changed

sentience/agent.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -149,23 +149,8 @@ def act( # noqa: C901
149149
if snap_opts.goal is None:
150150
snap_opts.goal = goal
151151

152-
# Convert screenshot config to dict if needed
153-
screenshot_param = snap_opts.screenshot
154-
if isinstance(snap_opts.screenshot, ScreenshotConfig):
155-
screenshot_param = {
156-
"format": snap_opts.screenshot.format,
157-
"quality": snap_opts.screenshot.quality,
158-
}
159-
160-
# Call snapshot with converted parameters
161-
snap = snapshot(
162-
self.browser,
163-
screenshot=screenshot_param,
164-
limit=snap_opts.limit,
165-
filter=snap_opts.filter.model_dump() if snap_opts.filter else None,
166-
use_api=snap_opts.use_api,
167-
goal=snap_opts.goal, # Pass goal to snapshot
168-
)
152+
# Call snapshot with options object (matches TypeScript API)
153+
snap = snapshot(self.browser, snap_opts)
169154

170155
if snap.status != "success":
171156
raise RuntimeError(f"Snapshot failed: {snap.error}")

sentience/conversational_agent.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from .agent import SentienceAgent
1111
from .browser import SentienceBrowser
1212
from .llm_provider import LLMProvider
13-
from .models import Snapshot
13+
from .models import Snapshot, SnapshotOptions
1414
from .snapshot import snapshot
1515

1616

@@ -274,7 +274,7 @@ def _execute_step(self, step: dict[str, Any]) -> dict[str, Any]:
274274
elif action == "EXTRACT_INFO":
275275
info_type = params["info_type"]
276276
# Get current page snapshot and extract info
277-
snap = snapshot(self.browser, limit=50)
277+
snap = snapshot(self.browser, SnapshotOptions(limit=50))
278278

279279
# Use LLM to extract specific information
280280
extracted = self._extract_information(snap, info_type)
@@ -361,7 +361,7 @@ def _verify_condition(self, condition: str) -> bool:
361361
True if condition is met, False otherwise
362362
"""
363363
try:
364-
snap = snapshot(self.browser, limit=30)
364+
snap = snapshot(self.browser, SnapshotOptions(limit=30))
365365

366366
# Build context
367367
elements_text = "\n".join([f"{el.role}: {el.text}" for el in snap.elements[:20]])

sentience/snapshot.py

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import json
66
import os
77
import time
8-
from typing import Any
8+
from typing import Any, Optional
99

1010
import requests
1111

@@ -41,41 +41,33 @@ def _save_trace_to_file(raw_elements: list[dict[str, Any]], trace_path: str | No
4141

4242
def snapshot(
4343
browser: SentienceBrowser,
44-
screenshot: bool | None = None,
45-
limit: int | None = None,
46-
filter: dict[str, Any] | None = None,
47-
use_api: bool | None = None,
48-
save_trace: bool = False,
49-
trace_path: str | None = None,
50-
show_overlay: bool = False,
44+
options: Optional[SnapshotOptions] = None,
5145
) -> Snapshot:
5246
"""
5347
Take a snapshot of the current page
5448
5549
Args:
5650
browser: SentienceBrowser instance
57-
screenshot: Whether to capture screenshot (bool or dict with format/quality)
58-
limit: Limit number of elements returned
59-
filter: Filter options (min_area, allowed_roles, min_z_index)
60-
use_api: Force use of server-side API if True, local extension if False.
61-
If None, uses API if api_key is set, otherwise uses local extension.
62-
save_trace: Whether to save raw_elements to JSON for benchmarking/training
63-
trace_path: Path to save trace file. If None, uses "trace_{timestamp}.json"
64-
show_overlay: Show visual overlay highlighting elements in browser
51+
options: Snapshot options (screenshot, limit, filter, etc.)
52+
If None, uses default options.
6553
6654
Returns:
6755
Snapshot object
56+
57+
Example:
58+
# Basic snapshot with defaults
59+
snap = snapshot(browser)
60+
61+
# With options
62+
snap = snapshot(browser, SnapshotOptions(
63+
screenshot=True,
64+
limit=100,
65+
show_overlay=True
66+
))
6867
"""
69-
# Build SnapshotOptions from individual parameters
70-
options = SnapshotOptions(
71-
screenshot=screenshot if screenshot is not None else False,
72-
limit=limit if limit is not None else 50,
73-
filter=filter,
74-
use_api=use_api,
75-
save_trace=save_trace,
76-
trace_path=trace_path,
77-
show_overlay=show_overlay,
78-
)
68+
# Use default options if none provided
69+
if options is None:
70+
options = SnapshotOptions()
7971

8072
# Determine if we should use server-side API
8173
should_use_api = (

sentience/wait.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import time
66

77
from .browser import SentienceBrowser
8-
from .models import WaitResult
8+
from .models import WaitResult, SnapshotOptions
99
from .query import find
1010
from .snapshot import snapshot
1111

@@ -46,7 +46,7 @@ def wait_for(
4646

4747
while time.time() - start_time < timeout:
4848
# Take snapshot (may be local extension or remote API)
49-
snap = snapshot(browser, use_api=use_api)
49+
snap = snapshot(browser, SnapshotOptions(use_api=use_api))
5050

5151
# Try to find element
5252
element = find(snap, selector)

tests/test_snapshot.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pytest
66

77
from sentience import SentienceBrowser, snapshot
8+
from sentience.models import SnapshotOptions
89

910

1011
@pytest.mark.requires_extension
@@ -110,7 +111,7 @@ def test_snapshot_with_goal():
110111
browser.page.wait_for_load_state("networkidle")
111112

112113
# Test snapshot with goal
113-
snap = snapshot(browser, goal="Find the main heading")
114+
snap = snapshot(browser, SnapshotOptions(goal="Find the main heading"))
114115

115116
assert snap.status == "success"
116117
assert snap.url == "https://example.com/"

0 commit comments

Comments
 (0)