|
9 | 9 | from typing import TYPE_CHECKING, Any, Optional |
10 | 10 |
|
11 | 11 | from .actions import click, click_async, press, press_async, type_text, type_text_async |
| 12 | +from .agent_config import AgentConfig |
12 | 13 | from .base_agent import BaseAgent, BaseAgentAsync |
13 | 14 | from .browser import AsyncSentienceBrowser, SentienceBrowser |
14 | 15 | from .llm_provider import LLMProvider, LLMResponse |
|
25 | 26 | from .snapshot import snapshot, snapshot_async |
26 | 27 |
|
27 | 28 | if TYPE_CHECKING: |
28 | | - from .agent_config import AgentConfig |
29 | 29 | from .tracing import Tracer |
30 | 30 |
|
31 | 31 |
|
@@ -78,7 +78,10 @@ def __init__( |
78 | 78 | self.default_snapshot_limit = default_snapshot_limit |
79 | 79 | self.verbose = verbose |
80 | 80 | self.tracer = tracer |
81 | | - self.config = config |
| 81 | + self.config = config or AgentConfig() |
| 82 | + |
| 83 | + # Screenshot sequence counter |
| 84 | + self._screenshot_sequence = 0 |
82 | 85 |
|
83 | 86 | # Execution history |
84 | 87 | self.history: list[dict[str, Any]] = [] |
@@ -150,12 +153,42 @@ def act( # noqa: C901 |
150 | 153 | if snap_opts.goal is None: |
151 | 154 | snap_opts.goal = goal |
152 | 155 |
|
| 156 | + # Apply AgentConfig screenshot settings if not overridden by snapshot_options |
| 157 | + if snapshot_options is None and self.config: |
| 158 | + if self.config.capture_screenshots: |
| 159 | + # Create ScreenshotConfig from AgentConfig |
| 160 | + snap_opts.screenshot = ScreenshotConfig( |
| 161 | + format=self.config.screenshot_format, |
| 162 | + quality=( |
| 163 | + self.config.screenshot_quality |
| 164 | + if self.config.screenshot_format == "jpeg" |
| 165 | + else None |
| 166 | + ), |
| 167 | + ) |
| 168 | + else: |
| 169 | + snap_opts.screenshot = False |
| 170 | + |
153 | 171 | # Call snapshot with options object (matches TypeScript API) |
154 | 172 | snap = snapshot(self.browser, snap_opts) |
155 | 173 |
|
156 | 174 | if snap.status != "success": |
157 | 175 | raise RuntimeError(f"Snapshot failed: {snap.error}") |
158 | 176 |
|
| 177 | + # Store screenshot if captured |
| 178 | + if snap.screenshot and self.tracer: |
| 179 | + self._screenshot_sequence += 1 |
| 180 | + seq = self._screenshot_sequence |
| 181 | + |
| 182 | + # Store screenshot in CloudTraceSink if available |
| 183 | + if hasattr(self.tracer.sink, "store_screenshot"): |
| 184 | + self.tracer.sink.store_screenshot( |
| 185 | + sequence=seq, |
| 186 | + screenshot_data=snap.screenshot, |
| 187 | + format=snap.screenshot_format |
| 188 | + or (self.config.screenshot_format if self.config else "jpeg"), |
| 189 | + step_id=step_id, |
| 190 | + ) |
| 191 | + |
159 | 192 | # Apply element filtering based on goal |
160 | 193 | filtered_elements = self.filter_elements(snap, goal) |
161 | 194 |
|
@@ -721,7 +754,10 @@ def __init__( |
721 | 754 | self.default_snapshot_limit = default_snapshot_limit |
722 | 755 | self.verbose = verbose |
723 | 756 | self.tracer = tracer |
724 | | - self.config = config |
| 757 | + self.config = config or AgentConfig() |
| 758 | + |
| 759 | + # Screenshot sequence counter |
| 760 | + self._screenshot_sequence = 0 |
725 | 761 |
|
726 | 762 | # Execution history |
727 | 763 | self.history: list[dict[str, Any]] = [] |
@@ -790,12 +826,42 @@ async def act( # noqa: C901 |
790 | 826 | if snap_opts.goal is None: |
791 | 827 | snap_opts.goal = goal |
792 | 828 |
|
| 829 | + # Apply AgentConfig screenshot settings if not overridden by snapshot_options |
| 830 | + if snapshot_options is None and self.config: |
| 831 | + if self.config.capture_screenshots: |
| 832 | + # Create ScreenshotConfig from AgentConfig |
| 833 | + snap_opts.screenshot = ScreenshotConfig( |
| 834 | + format=self.config.screenshot_format, |
| 835 | + quality=( |
| 836 | + self.config.screenshot_quality |
| 837 | + if self.config.screenshot_format == "jpeg" |
| 838 | + else None |
| 839 | + ), |
| 840 | + ) |
| 841 | + else: |
| 842 | + snap_opts.screenshot = False |
| 843 | + |
793 | 844 | # Call snapshot with options object (matches TypeScript API) |
794 | 845 | snap = await snapshot_async(self.browser, snap_opts) |
795 | 846 |
|
796 | 847 | if snap.status != "success": |
797 | 848 | raise RuntimeError(f"Snapshot failed: {snap.error}") |
798 | 849 |
|
| 850 | + # Store screenshot if captured |
| 851 | + if snap.screenshot and self.tracer: |
| 852 | + self._screenshot_sequence += 1 |
| 853 | + seq = self._screenshot_sequence |
| 854 | + |
| 855 | + # Store screenshot in CloudTraceSink if available |
| 856 | + if hasattr(self.tracer.sink, "store_screenshot"): |
| 857 | + self.tracer.sink.store_screenshot( |
| 858 | + sequence=seq, |
| 859 | + screenshot_data=snap.screenshot, |
| 860 | + format=snap.screenshot_format |
| 861 | + or (self.config.screenshot_format if self.config else "jpeg"), |
| 862 | + step_id=step_id, |
| 863 | + ) |
| 864 | + |
799 | 865 | # Apply element filtering based on goal |
800 | 866 | filtered_elements = self.filter_elements(snap, goal) |
801 | 867 |
|
|
0 commit comments