-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcdp.rs
More file actions
422 lines (381 loc) · 16.5 KB
/
Copy pathcdp.rs
File metadata and controls
422 lines (381 loc) · 16.5 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
/*! CDP adapter for the Rust `browser-connection` MCP server.
CHANGE: drive Chromium directly through CDP from Rust instead of shelling out to an external upstream MCP command.
WHY: issue #347 requires docker-git's noVNC-visible browser and MCP browser tools to be one session.
QUOTE(ТЗ): "заменить использование команды playright на нашу кастомную имплементацию"
REF: https://github.com/ProverCoderAI/docker-git/issues/347
SOURCE: n/a
FORMAT THEOREM: tool(cdp_url, args) -> CDP command on the same endpoint that noVNC exposes.
PURITY: SHELL
EFFECT: HTTP probes via curl and WebSocket CDP commands.
INVARIANT: no tool starts a second browser; every command targets the configured CDP endpoint.
*/
use anyhow::{anyhow, Context, Result};
use serde_json::{json, Value};
use std::process::{Command, Stdio};
use std::time::{Duration, Instant};
use tungstenite::{connect, Message};
#[derive(Debug, Clone)]
pub struct CdpClient {
endpoint: String,
}
impl CdpClient {
pub fn new(endpoint: impl Into<String>) -> Self {
Self {
endpoint: endpoint.into(),
}
}
pub fn endpoint(&self) -> &str {
&self.endpoint
}
pub fn navigate(&self, url: &str) -> Result<String> {
require_non_empty(url, "url")?;
self.command("Page.navigate", json!({ "url": url }))?;
self.wait_for_navigation_ready()?;
Ok(format!("Navigated to {url}"))
}
// CHANGE: wait for browser_navigate to leave the page in a DOM-readable state.
// WHY: MCP clients commonly call browser_evaluate immediately after navigate; without this wait,
// Runtime.evaluate can race while document.body is still null and produce flaky smoke failures.
// QUOTE(ТЗ): "проверил работате ли поднятие MCP Playright вместе с noVNC протоколом"
// REF: proc_47c63ffc3276 JSONDecodeError after browser_evaluate saw document.body == null
// SOURCE: Chrome DevTools Protocol Runtime.evaluate + document.readyState
// FORMAT THEOREM: navigate(url) returns Ok -> document.body exists ∧ readyState ∈ {interactive, complete}
// PURITY: SHELL
// EFFECT: polls CDP Runtime.evaluate for up to 10 seconds after Page.navigate.
// INVARIANT: no additional browser is started; polling uses the same CDP endpoint and page target.
fn wait_for_navigation_ready(&self) -> Result<()> {
let deadline = Instant::now() + Duration::from_secs(10);
let mut last_error = None;
let mut last_status = Value::Null;
while Instant::now() < deadline {
match self.evaluate_value(NAVIGATION_READY_EXPRESSION) {
Ok(status) => {
if navigation_ready(&status) {
return Ok(());
}
last_status = status;
}
Err(error) => {
last_error = Some(error.to_string());
}
}
std::thread::sleep(Duration::from_millis(50));
}
Err(anyhow!(
"page did not become DOM-ready after navigation; last status: {last_status}; last error: {}",
last_error.unwrap_or_else(|| "none".to_string())
))
}
pub fn evaluate(&self, expression: &str) -> Result<String> {
let value = self.evaluate_value(expression)?;
serde_json::to_string_pretty(&value).context("failed to render evaluation result")
}
pub fn snapshot(&self) -> Result<String> {
let value = self.evaluate_value(
r#"(() => {
const text = (document.body && document.body.innerText || document.documentElement.innerText || '').trim();
const interactive = Array.from(document.querySelectorAll('a,button,input,textarea,select,[role="button"],[tabindex]'))
.slice(0, 80)
.map((el, index) => ({
ref: `@e${index + 1}`,
tag: el.tagName.toLowerCase(),
role: el.getAttribute('role') || '',
text: (el.innerText || el.value || el.getAttribute('aria-label') || el.getAttribute('title') || el.getAttribute('href') || '').trim().slice(0, 160),
selector: el.id ? `#${CSS.escape(el.id)}` : el.name ? `${el.tagName.toLowerCase()}[name="${CSS.escape(el.name)}"]` : el.tagName.toLowerCase()
}));
return { url: location.href, title: document.title, text: text.slice(0, 4000), interactive };
})()"#,
)?;
let title = value
.get("title")
.and_then(Value::as_str)
.unwrap_or("(untitled)");
let url = value.get("url").and_then(Value::as_str).unwrap_or("");
let text = value.get("text").and_then(Value::as_str).unwrap_or("");
let mut output = format!("Title: {title}\nURL: {url}\n\n{text}");
if let Some(items) = value.get("interactive").and_then(Value::as_array) {
if !items.is_empty() {
output.push_str("\n\nInteractive elements:\n");
for item in items {
let reference = item.get("ref").and_then(Value::as_str).unwrap_or("@e?");
let tag = item.get("tag").and_then(Value::as_str).unwrap_or("element");
let label = item.get("text").and_then(Value::as_str).unwrap_or("");
let selector = item.get("selector").and_then(Value::as_str).unwrap_or(tag);
output.push_str(&format!(
"{reference} {tag} \"{label}\" selector={selector}\n"
));
}
}
}
Ok(output)
}
pub fn click(&self, selector: &str) -> Result<String> {
require_non_empty(selector, "selector")?;
let selector_json = serde_json::to_string(selector).context("failed to quote selector")?;
let expression = format!(
r#"(() => {{
const selector = {selector_json};
const el = document.querySelector(selector);
if (!el) throw new Error(`No element matches selector: ${{selector}}`);
el.scrollIntoView({{block: 'center', inline: 'center'}});
el.click();
return {{ clicked: true, selector, text: (el.innerText || el.value || el.getAttribute('aria-label') || '').trim() }};
}})()"#
);
self.evaluate(&expression)
}
pub fn type_text(&self, selector: &str, text: &str) -> Result<String> {
require_non_empty(selector, "selector")?;
let selector_json = serde_json::to_string(selector).context("failed to quote selector")?;
let text_json = serde_json::to_string(text).context("failed to quote text")?;
let expression = format!(
r#"(() => {{
const selector = {selector_json};
const text = {text_json};
const el = document.querySelector(selector);
if (!el) throw new Error(`No element matches selector: ${{selector}}`);
el.focus();
if ('value' in el) {{
el.value = text;
el.dispatchEvent(new Event('input', {{ bubbles: true }}));
el.dispatchEvent(new Event('change', {{ bubbles: true }}));
}} else {{
el.textContent = text;
el.dispatchEvent(new InputEvent('input', {{ bubbles: true, inputType: 'insertText', data: text }}));
}}
return {{ typed: true, selector, textLength: text.length }};
}})()"#
);
self.evaluate(&expression)
}
pub fn press_key(&self, key: &str) -> Result<String> {
require_non_empty(key, "key")?;
let text = if key.chars().count() == 1 { key } else { "" };
let params = json!({ "type": "keyDown", "key": key, "text": text });
self.command("Input.dispatchKeyEvent", params)?;
self.command(
"Input.dispatchKeyEvent",
json!({ "type": "keyUp", "key": key }),
)?;
Ok(format!("Pressed key {key}"))
}
pub fn screenshot(&self, full_page: bool) -> Result<String> {
let result = self.command(
"Page.captureScreenshot",
json!({ "format": "png", "captureBeyondViewport": full_page }),
)?;
let data = result
.get("data")
.and_then(Value::as_str)
.ok_or_else(|| anyhow!("CDP screenshot response did not include data"))?;
Ok(format!("data:image/png;base64,{data}"))
}
fn evaluate_value(&self, expression: &str) -> Result<Value> {
require_non_empty(expression, "expression")?;
let result = self.command(
"Runtime.evaluate",
json!({
"expression": expression,
"returnByValue": true,
"awaitPromise": true,
"userGesture": true
}),
)?;
if let Some(details) = result.get("exceptionDetails") {
return Err(anyhow!("CDP Runtime.evaluate exception: {details}"));
}
let remote = result
.get("result")
.ok_or_else(|| anyhow!("CDP Runtime.evaluate response did not include result"))?;
if let Some(value) = remote.get("value") {
return Ok(value.clone());
}
if let Some(description) = remote.get("description").and_then(Value::as_str) {
return Ok(Value::String(description.to_string()));
}
Ok(remote.clone())
}
fn command(&self, method: &str, params: Value) -> Result<Value> {
let websocket_url = self.page_websocket_url()?;
send_cdp_command(&websocket_url, method, params)
}
fn page_websocket_url(&self) -> Result<String> {
let list = self.curl_json("GET", "/json/list")?;
if let Some(targets) = list.as_array() {
if let Some(url) = targets.iter().find_map(page_target_websocket_url) {
return rewrite_websocket_url(&self.endpoint, url);
}
}
let created = self.curl_json("PUT", "/json/new?about:blank")?;
let url = created
.get("webSocketDebuggerUrl")
.and_then(Value::as_str)
.ok_or_else(|| anyhow!("CDP /json/new did not return a page websocket URL"))?;
rewrite_websocket_url(&self.endpoint, url)
}
fn curl_json(&self, method: &str, path: &str) -> Result<Value> {
let url = format!("{}{}", self.endpoint.trim_end_matches('/'), path);
let output = Command::new("curl")
.args([
"-sSf",
"--connect-timeout",
"2",
"--max-time",
"10",
"-X",
method,
"-H",
"Host: 127.0.0.1:9222",
&url,
])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
.with_context(|| format!("failed to execute curl for CDP endpoint {url}"))?;
if !output.status.success() {
return Err(anyhow!(
"CDP HTTP request {method} {url} failed with status {:?}: {}",
output.status.code(),
String::from_utf8_lossy(&output.stderr)
));
}
serde_json::from_slice(&output.stdout)
.with_context(|| format!("CDP HTTP response from {url} was not JSON"))
}
}
const NAVIGATION_READY_EXPRESSION: &str = r#"(() => ({
readyState: document.readyState,
hasBody: document.body !== null
}))()"#;
fn navigation_ready(status: &Value) -> bool {
let ready_state = status
.get("readyState")
.and_then(Value::as_str)
.unwrap_or_default();
let has_body = status
.get("hasBody")
.and_then(Value::as_bool)
.unwrap_or(false);
has_body && matches!(ready_state, "interactive" | "complete")
}
fn page_target_websocket_url(target: &Value) -> Option<&str> {
let target_type = target.get("type").and_then(Value::as_str)?;
if target_type != "page" {
return None;
}
target.get("webSocketDebuggerUrl").and_then(Value::as_str)
}
// CHANGE: rewrite Chrome's self-reported websocket host to the configured CDP endpoint.
// WHY: Chrome usually reports ws://127.0.0.1:9222 even when docker-git exposes it via the stable :9223 proxy.
// QUOTE(ТЗ): "CDP port 9223 ... single session"
// REF: issue-347
// SOURCE: n/a
// FORMAT THEOREM: endpoint=http://h:p ∧ original=ws://x/devtools/y -> ws://h:p/devtools/y
// PURITY: CORE
// INVARIANT: MCP CDP WebSocket traffic uses the same endpoint that noVNC/CDP health checks verify.
// COMPLEXITY: O(n)/O(n), n = endpoint length + original length.
fn rewrite_websocket_url(endpoint: &str, original: &str) -> Result<String> {
let scheme = if endpoint.trim_start().starts_with("https://") {
"wss"
} else {
"ws"
};
let authority = endpoint
.trim()
.trim_end_matches('/')
.strip_prefix("http://")
.or_else(|| {
endpoint
.trim()
.trim_end_matches('/')
.strip_prefix("https://")
})
.unwrap_or_else(|| endpoint.trim().trim_end_matches('/'));
let path = original
.find("/devtools/")
.map(|index| &original[index..])
.ok_or_else(|| anyhow!("CDP websocket URL did not include /devtools/: {original}"))?;
Ok(format!("{scheme}://{authority}{path}"))
}
fn send_cdp_command(websocket_url: &str, method: &str, params: Value) -> Result<Value> {
let (mut socket, _) = connect(websocket_url)
.with_context(|| format!("failed to connect to CDP websocket {websocket_url}"))?;
let request = json!({ "id": 1, "method": method, "params": params });
socket
.send(Message::Text(request.to_string()))
.with_context(|| format!("failed to send CDP command {method}"))?;
loop {
let message = socket
.read()
.with_context(|| format!("failed to read CDP response for {method}"))?;
match message {
Message::Text(text) => {
let value: Value = serde_json::from_str(text.as_ref())
.with_context(|| format!("CDP websocket response for {method} was not JSON"))?;
if value.get("id").and_then(Value::as_u64) == Some(1) {
if let Some(error) = value.get("error") {
return Err(anyhow!("CDP command {method} failed: {error}"));
}
return Ok(value.get("result").cloned().unwrap_or(Value::Null));
}
}
Message::Close(_) => {
return Err(anyhow!("CDP websocket closed before {method} response"))
}
Message::Ping(payload) => {
socket
.send(Message::Pong(payload))
.context("failed to answer CDP websocket ping")?;
}
_ => {}
}
std::thread::sleep(Duration::from_millis(1));
}
}
fn require_non_empty(value: &str, name: &str) -> Result<()> {
if value.trim().is_empty() {
return Err(anyhow!("{name} must not be empty"));
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::{navigation_ready, rewrite_websocket_url};
use serde_json::json;
#[test]
fn navigation_ready_requires_loaded_document_body() {
assert!(!navigation_ready(
&json!({ "readyState": "loading", "hasBody": false })
));
assert!(!navigation_ready(
&json!({ "readyState": "complete", "hasBody": false })
));
assert!(navigation_ready(
&json!({ "readyState": "interactive", "hasBody": true })
));
assert!(navigation_ready(
&json!({ "readyState": "complete", "hasBody": true })
));
}
#[test]
fn rewrites_chrome_9222_websocket_to_stable_9223_endpoint() {
let rewritten = rewrite_websocket_url(
"http://127.0.0.1:9223",
"ws://127.0.0.1:9222/devtools/page/ABC",
)
.expect("websocket URL rewrites");
assert_eq!(rewritten, "ws://127.0.0.1:9223/devtools/page/ABC");
}
#[test]
fn rewrites_https_endpoint_to_wss() {
let rewritten = rewrite_websocket_url(
"https://browser.example.test:9443/",
"ws://127.0.0.1:9222/devtools/browser/XYZ",
)
.expect("websocket URL rewrites");
assert_eq!(
rewritten,
"wss://browser.example.test:9443/devtools/browser/XYZ"
);
}
}