-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcdp_backend.py
More file actions
407 lines (349 loc) · 12.9 KB
/
cdp_backend.py
File metadata and controls
407 lines (349 loc) · 12.9 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
"""
CDP Backend implementation for browser-use integration.
This module provides CDPBackendV0, which implements BrowserBackend protocol
using Chrome DevTools Protocol (CDP) commands.
Usage with browser-use:
from browser_use import BrowserSession
from sentience.backends import CDPBackendV0
from sentience.backends.browser_use_adapter import BrowserUseAdapter
session = BrowserSession(...)
await session.start()
adapter = BrowserUseAdapter(session)
backend = await adapter.create_backend()
# Now use backend for Sentience operations
viewport = await backend.refresh_page_info()
await backend.mouse_click(100, 200)
"""
import asyncio
import base64
import time
from typing import Any, Literal, Protocol, runtime_checkable
from .protocol import BrowserBackend, LayoutMetrics, ViewportInfo
@runtime_checkable
class CDPTransport(Protocol):
"""
Protocol for CDP transport layer.
This abstracts the actual CDP communication, allowing different
implementations (browser-use, Playwright CDP, raw WebSocket).
"""
async def send(self, method: str, params: dict | None = None) -> dict:
"""
Send a CDP command and return the result.
Args:
method: CDP method name, e.g., "Runtime.evaluate"
params: Method parameters
Returns:
CDP response dict
"""
...
class CDPBackendV0:
"""
CDP-based implementation of BrowserBackend.
This backend uses CDP commands to interact with the browser,
making it compatible with browser-use's CDP client.
"""
def __init__(self, transport: CDPTransport) -> None:
"""
Initialize CDP backend.
Args:
transport: CDP transport for sending commands
"""
self._transport = transport
self._cached_viewport: ViewportInfo | None = None
self._execution_context_id: int | None = None
async def _get_execution_context(self) -> int:
"""Get or create execution context ID for Runtime.callFunctionOn."""
if self._execution_context_id is not None:
return self._execution_context_id
# Enable Runtime domain if not already enabled
try:
await self._transport.send("Runtime.enable")
except Exception:
pass # May already be enabled
# Get the main frame's execution context
result = await self._transport.send(
"Runtime.evaluate",
{
"expression": "1",
"returnByValue": True,
},
)
# Extract context ID from the result
if "executionContextId" in result:
self._execution_context_id = result["executionContextId"]
else:
# Fallback: use context ID 1 (main frame)
self._execution_context_id = 1
return self._execution_context_id
async def refresh_page_info(self) -> ViewportInfo:
"""Cache viewport + scroll offsets; cheap & safe to call often."""
result = await self.eval(
"""(() => ({
width: window.innerWidth,
height: window.innerHeight,
scroll_x: window.scrollX,
scroll_y: window.scrollY,
content_width: document.documentElement.scrollWidth,
content_height: document.documentElement.scrollHeight
}))()"""
)
self._cached_viewport = ViewportInfo(
width=result.get("width", 0),
height=result.get("height", 0),
scroll_x=result.get("scroll_x", 0),
scroll_y=result.get("scroll_y", 0),
content_width=result.get("content_width"),
content_height=result.get("content_height"),
)
return self._cached_viewport
async def eval(self, expression: str) -> Any:
"""Evaluate JavaScript expression using Runtime.evaluate."""
result = await self._transport.send(
"Runtime.evaluate",
{
"expression": expression,
"returnByValue": True,
"awaitPromise": True,
},
)
# Check for exceptions
if "exceptionDetails" in result:
exc = result["exceptionDetails"]
text = exc.get("text", "Unknown error")
raise RuntimeError(f"JavaScript evaluation failed: {text}")
# Extract value from result
if "result" in result:
res = result["result"]
if res.get("type") == "undefined":
return None
return res.get("value")
return None
async def call(
self,
function_declaration: str,
args: list[Any] | None = None,
) -> Any:
"""Call JavaScript function using Runtime.callFunctionOn."""
# Build call arguments
call_args = []
if args:
for arg in args:
if arg is None:
call_args.append({"value": None})
elif isinstance(arg, bool):
call_args.append({"value": arg})
elif isinstance(arg, (int, float)):
call_args.append({"value": arg})
elif isinstance(arg, str):
call_args.append({"value": arg})
elif isinstance(arg, dict):
call_args.append({"value": arg})
elif isinstance(arg, list):
call_args.append({"value": arg})
else:
# Serialize complex objects to JSON
call_args.append({"value": str(arg)})
# We need an object ID to call function on
# Use globalThis (window) as the target
global_result = await self._transport.send(
"Runtime.evaluate",
{
"expression": "globalThis",
"returnByValue": False,
},
)
object_id = global_result.get("result", {}).get("objectId")
if not object_id:
# Fallback: evaluate the function directly
if args:
args_json = ", ".join(repr(a) if isinstance(a, str) else str(a) for a in args)
expression = f"({function_declaration})({args_json})"
else:
expression = f"({function_declaration})()"
return await self.eval(expression)
result = await self._transport.send(
"Runtime.callFunctionOn",
{
"functionDeclaration": function_declaration,
"objectId": object_id,
"arguments": call_args,
"returnByValue": True,
"awaitPromise": True,
},
)
# Check for exceptions
if "exceptionDetails" in result:
exc = result["exceptionDetails"]
text = exc.get("text", "Unknown error")
raise RuntimeError(f"JavaScript call failed: {text}")
# Extract value from result
if "result" in result:
res = result["result"]
if res.get("type") == "undefined":
return None
return res.get("value")
return None
async def get_layout_metrics(self) -> LayoutMetrics:
"""Get page layout metrics using Page.getLayoutMetrics."""
result = await self._transport.send("Page.getLayoutMetrics")
# Extract metrics from result
layout_viewport = result.get("layoutViewport", {})
content_size = result.get("contentSize", {})
visual_viewport = result.get("visualViewport", {})
return LayoutMetrics(
viewport_x=visual_viewport.get("pageX", 0),
viewport_y=visual_viewport.get("pageY", 0),
viewport_width=visual_viewport.get(
"clientWidth", layout_viewport.get("clientWidth", 0)
),
viewport_height=visual_viewport.get(
"clientHeight", layout_viewport.get("clientHeight", 0)
),
content_width=content_size.get("width", 0),
content_height=content_size.get("height", 0),
device_scale_factor=visual_viewport.get("scale", 1.0),
)
async def screenshot_png(self) -> bytes:
"""Capture viewport screenshot as PNG bytes."""
result = await self._transport.send(
"Page.captureScreenshot",
{
"format": "png",
"captureBeyondViewport": False,
},
)
data = result.get("data", "")
return base64.b64decode(data)
async def screenshot_jpeg(self, quality: int | None = None) -> bytes:
"""Capture viewport screenshot as JPEG bytes."""
q = 80 if quality is None else max(1, min(int(quality), 100))
result = await self._transport.send(
"Page.captureScreenshot",
{
"format": "jpeg",
"quality": q,
"captureBeyondViewport": False,
},
)
data = result.get("data", "")
return base64.b64decode(data)
async def mouse_move(self, x: float, y: float) -> None:
"""Move mouse to viewport coordinates."""
await self._transport.send(
"Input.dispatchMouseEvent",
{
"type": "mouseMoved",
"x": x,
"y": y,
},
)
async def mouse_click(
self,
x: float,
y: float,
button: Literal["left", "right", "middle"] = "left",
click_count: int = 1,
) -> None:
"""Click at viewport coordinates."""
# Mouse down
await self._transport.send(
"Input.dispatchMouseEvent",
{
"type": "mousePressed",
"x": x,
"y": y,
"button": button,
"clickCount": click_count,
},
)
# Small delay between press and release
await asyncio.sleep(0.05)
# Mouse up
await self._transport.send(
"Input.dispatchMouseEvent",
{
"type": "mouseReleased",
"x": x,
"y": y,
"button": button,
"clickCount": click_count,
},
)
async def wheel(
self,
delta_y: float,
x: float | None = None,
y: float | None = None,
) -> None:
"""Scroll using mouse wheel."""
# Get viewport center if coordinates not provided
if x is None or y is None:
if self._cached_viewport is None:
await self.refresh_page_info()
assert self._cached_viewport is not None
x = x if x is not None else self._cached_viewport.width / 2
y = y if y is not None else self._cached_viewport.height / 2
await self._transport.send(
"Input.dispatchMouseEvent",
{
"type": "mouseWheel",
"x": x,
"y": y,
"deltaX": 0,
"deltaY": delta_y,
},
)
async def type_text(self, text: str) -> None:
"""Type text using keyboard input."""
for char in text:
# Key down
await self._transport.send(
"Input.dispatchKeyEvent",
{
"type": "keyDown",
"text": char,
},
)
# Char event (for text input)
await self._transport.send(
"Input.dispatchKeyEvent",
{
"type": "char",
"text": char,
},
)
# Key up
await self._transport.send(
"Input.dispatchKeyEvent",
{
"type": "keyUp",
"text": char,
},
)
# Small delay between characters
await asyncio.sleep(0.01)
async def wait_ready_state(
self,
state: Literal["interactive", "complete"] = "interactive",
timeout_ms: int = 15000,
) -> None:
"""Wait for document.readyState using polling."""
start = time.monotonic()
timeout_sec = timeout_ms / 1000.0
# Map state to acceptable states
acceptable_states = {"complete"} if state == "complete" else {"interactive", "complete"}
while True:
elapsed = time.monotonic() - start
if elapsed >= timeout_sec:
raise TimeoutError(
f"Timed out waiting for document.readyState='{state}' " f"after {timeout_ms}ms"
)
current_state = await self.eval("document.readyState")
if current_state in acceptable_states:
return
# Poll every 100ms
await asyncio.sleep(0.1)
async def get_url(self) -> str:
"""Get current page URL."""
result = await self.eval("window.location.href")
return result if result else ""