-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.py
More file actions
228 lines (174 loc) · 5.74 KB
/
protocol.py
File metadata and controls
228 lines (174 loc) · 5.74 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
"""
v0 BrowserBackend Protocol - Minimal interface for browser-use integration.
This protocol defines the minimal interface required to:
- Take Sentience snapshots (DOM/geometry via extension)
- Compute viewport-coord clicks
- Scroll + re-snapshot + click
- Stabilize after action
No navigation API required (browser-use already handles navigation).
Design principle: Keep it so small that nothing can break.
"""
from typing import Any, Literal, Protocol, runtime_checkable
from pydantic import BaseModel
class ViewportInfo(BaseModel):
"""Viewport and scroll position information."""
width: int
height: int
scroll_x: float = 0.0
scroll_y: float = 0.0
content_width: float | None = None
content_height: float | None = None
class LayoutMetrics(BaseModel):
"""Page layout metrics from CDP Page.getLayoutMetrics."""
# Viewport dimensions
viewport_x: float = 0.0
viewport_y: float = 0.0
viewport_width: float = 0.0
viewport_height: float = 0.0
# Content dimensions (scrollable area)
content_width: float = 0.0
content_height: float = 0.0
# Device scale factor
device_scale_factor: float = 1.0
@runtime_checkable
class BrowserBackend(Protocol):
"""
Minimal backend protocol for v0 proof-of-concept.
This is enough to:
- Take Sentience snapshots (DOM/geometry via extension)
- Execute JavaScript for element interaction
- Perform mouse operations (move, click, scroll)
- Wait for page stability
Implementers:
- CDPBackendV0: For browser-use integration via CDP
- PlaywrightBackend: Wrapper around existing SentienceBrowser (future)
"""
async def refresh_page_info(self) -> ViewportInfo:
"""
Cache viewport + scroll offsets + url; cheap & safe to call often.
Returns:
ViewportInfo with current viewport state
"""
...
async def eval(self, expression: str) -> Any:
"""
Evaluate JavaScript expression in page context.
Uses CDP Runtime.evaluate with returnByValue=True.
Args:
expression: JavaScript expression to evaluate
Returns:
Result value (JSON-serializable)
"""
...
async def call(
self,
function_declaration: str,
args: list[Any] | None = None,
) -> Any:
"""
Call a JavaScript function with arguments.
Uses CDP Runtime.callFunctionOn for safe argument passing.
Safer than eval() for passing complex arguments.
Args:
function_declaration: JavaScript function body, e.g., "(x, y) => x + y"
args: Arguments to pass to the function
Returns:
Result value (JSON-serializable)
"""
...
async def get_layout_metrics(self) -> LayoutMetrics:
"""
Get page layout metrics.
Uses CDP Page.getLayoutMetrics to get viewport and content dimensions.
Returns:
LayoutMetrics with viewport and content size info
"""
...
async def screenshot_png(self) -> bytes:
"""
Capture viewport screenshot as PNG bytes.
Uses CDP Page.captureScreenshot.
Returns:
PNG image bytes
"""
...
async def screenshot_jpeg(self, quality: int | None = None) -> bytes:
"""
Capture viewport screenshot as JPEG bytes.
Args:
quality: Optional JPEG quality (1-100)
Returns:
JPEG image bytes
"""
...
async def mouse_move(self, x: float, y: float) -> None:
"""
Move mouse to viewport coordinates.
Uses CDP Input.dispatchMouseEvent with type="mouseMoved".
Args:
x: X coordinate in viewport
y: Y coordinate in viewport
"""
...
async def mouse_click(
self,
x: float,
y: float,
button: Literal["left", "right", "middle"] = "left",
click_count: int = 1,
) -> None:
"""
Click at viewport coordinates.
Uses CDP Input.dispatchMouseEvent with mousePressed + mouseReleased.
Args:
x: X coordinate in viewport
y: Y coordinate in viewport
button: Mouse button to click
click_count: Number of clicks (1 for single, 2 for double)
"""
...
async def wheel(
self,
delta_y: float,
x: float | None = None,
y: float | None = None,
) -> None:
"""
Scroll using mouse wheel.
Uses CDP Input.dispatchMouseEvent with type="mouseWheel".
Args:
delta_y: Scroll amount (positive = down, negative = up)
x: X coordinate for scroll (default: viewport center)
y: Y coordinate for scroll (default: viewport center)
"""
...
async def type_text(self, text: str) -> None:
"""
Type text using keyboard input.
Uses CDP Input.dispatchKeyEvent for each character.
Args:
text: Text to type
"""
...
async def wait_ready_state(
self,
state: Literal["interactive", "complete"] = "interactive",
timeout_ms: int = 15000,
) -> None:
"""
Wait for document.readyState to reach target state.
Uses polling instead of CDP events (no leak from unregistered listeners).
Args:
state: Target state ("interactive" or "complete")
timeout_ms: Maximum time to wait in milliseconds
Raises:
TimeoutError: If state not reached within timeout
"""
...
async def get_url(self) -> str:
"""
Get current page URL.
Returns:
Current page URL (window.location.href)
"""
...