agent-first-http 0.5.0

Give an AI agent any URL and get back a usable page — fetched directly, or rendered in a real browser when the page needs one — with a human able to take over the same browser for a login, captcha, or 2FA.
Documentation
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
//! Pointer/keyboard event replay via Input.dispatch*. Preserves
//! operator-supplied `performance.now()` timestamps as inter-event delays
//! so trajectory/dwell-time entropy from real input is kept end-to-end
//! (`architecture.md §9`).

use std::time::{Duration, Instant};

use axum::extract::ws::{Message, WebSocket};
use futures::{SinkExt, StreamExt};
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::host::ops_panel::screencast::resolve_page_target;
use crate::sdk::cdp::ws_client::Connection;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum OpsInputEvent {
    PointerMove {
        x: f32,
        y: f32,
        timestamp_ms: f64,
    },
    PointerDown {
        x: f32,
        y: f32,
        button: String,
        timestamp_ms: f64,
    },
    PointerUp {
        x: f32,
        y: f32,
        button: String,
        timestamp_ms: f64,
    },
    Wheel {
        x: f32,
        y: f32,
        dx: f32,
        dy: f32,
        timestamp_ms: f64,
    },
    KeyDown {
        key: String,
        code: String,
        modifiers: u32,
        timestamp_ms: f64,
    },
    KeyUp {
        key: String,
        code: String,
        modifiers: u32,
        timestamp_ms: f64,
    },
    /// A whole string pasted by the operator (clipboard → Input.insertText).
    /// Inserted at the focused element's caret in one shot — the operator's
    /// clipboard never reaches the target browser, so we relay the text
    /// itself rather than a Ctrl/⌘+V keystroke (which would paste the
    /// target's own, unrelated clipboard).
    InsertText {
        text: String,
        timestamp_ms: f64,
    },
}

impl OpsInputEvent {
    fn timestamp_ms(&self) -> f64 {
        match self {
            Self::PointerMove { timestamp_ms, .. }
            | Self::PointerDown { timestamp_ms, .. }
            | Self::PointerUp { timestamp_ms, .. }
            | Self::Wheel { timestamp_ms, .. }
            | Self::KeyDown { timestamp_ms, .. }
            | Self::KeyUp { timestamp_ms, .. }
            | Self::InsertText { timestamp_ms, .. } => *timestamp_ms,
        }
    }

    fn to_cdp(&self) -> (&'static str, Value) {
        match self {
            Self::PointerMove { x, y, .. } => (
                "Input.dispatchMouseEvent",
                serde_json::json!({
                    "type": "mouseMoved",
                    "x": x,
                    "y": y,
                    "button": "none",
                }),
            ),
            Self::PointerDown { x, y, button, .. } => (
                "Input.dispatchMouseEvent",
                serde_json::json!({
                    "type": "mousePressed",
                    "x": x,
                    "y": y,
                    "button": button,
                    "clickCount": 1,
                }),
            ),
            Self::PointerUp { x, y, button, .. } => (
                "Input.dispatchMouseEvent",
                serde_json::json!({
                    "type": "mouseReleased",
                    "x": x,
                    "y": y,
                    "button": button,
                    "clickCount": 1,
                }),
            ),
            Self::Wheel { x, y, dx, dy, .. } => (
                "Input.dispatchMouseEvent",
                serde_json::json!({
                    "type": "mouseWheel",
                    "x": x,
                    "y": y,
                    "deltaX": dx,
                    "deltaY": dy,
                }),
            ),
            Self::KeyDown {
                key,
                code,
                modifiers,
                ..
            } => {
                let mut params = serde_json::json!({
                    "type": "keyDown",
                    "key": key,
                    "code": code,
                    "modifiers": modifiers,
                    "text": one_char_text(key, *modifiers),
                });
                if let Some(vk) = virtual_key_code(key) {
                    params["windowsVirtualKeyCode"] = vk.into();
                    params["nativeVirtualKeyCode"] = vk.into();
                }
                ("Input.dispatchKeyEvent", params)
            }
            Self::KeyUp {
                key,
                code,
                modifiers,
                ..
            } => {
                let mut params = serde_json::json!({
                    "type": "keyUp",
                    "key": key,
                    "code": code,
                    "modifiers": modifiers,
                });
                if let Some(vk) = virtual_key_code(key) {
                    params["windowsVirtualKeyCode"] = vk.into();
                    params["nativeVirtualKeyCode"] = vk.into();
                }
                ("Input.dispatchKeyEvent", params)
            }
            Self::InsertText { text, .. } => (
                "Input.insertText",
                serde_json::json!({
                    "text": text,
                }),
            ),
        }
    }
}

/// If `key` is a single printable character, return it as the `text` field
/// for Input.dispatchKeyEvent so the page sees a real keypress. Otherwise
/// (Enter, Backspace, ArrowLeft, …) leave it out — CDP will synthesize the
/// right behavior from key/code.
///
/// When Ctrl or ⌘ is held the keystroke is a shortcut (select-all, reload, …),
/// not text, so we suppress `text` regardless of the key — otherwise chromium
/// types the bare letter instead of running the shortcut. Shift/Alt don't
/// count: Shift+a is still "A", and AltGr layouts produce real characters.
fn one_char_text(key: &str, modifiers: u32) -> Value {
    const CTRL: u32 = 2;
    const META: u32 = 4;
    if modifiers & (CTRL | META) != 0 {
        return Value::Null;
    }
    let mut chars = key.chars();
    let first = chars.next();
    let second = chars.next();
    match (first, second) {
        (Some(c), None) if !c.is_control() => Value::String(c.to_string()),
        _ => Value::Null,
    }
}

/// Windows virtual-key code for a DOM `key` value, or `None` when there isn't a
/// meaningful one. CDP needs this for any key whose effect is an *action*
/// rather than inserted text: Backspace/Delete/Enter/Tab/arrows won't edit, and
/// Ctrl/⌘+letter shortcuts won't fire, unless `windowsVirtualKeyCode` is set —
/// the `text` field alone only covers character insertion. Letters and digits
/// map to their ASCII-uppercase code (the VK code equals the uppercase ASCII
/// value); named editing/navigation keys use the fixed table below.
fn virtual_key_code(key: &str) -> Option<i64> {
    let named = match key {
        "Backspace" => 8,
        "Tab" => 9,
        "Enter" => 13,
        "Escape" => 27,
        " " | "Spacebar" => 32,
        "PageUp" => 33,
        "PageDown" => 34,
        "End" => 35,
        "Home" => 36,
        "ArrowLeft" => 37,
        "ArrowUp" => 38,
        "ArrowRight" => 39,
        "ArrowDown" => 40,
        "Insert" => 45,
        "Delete" => 46,
        _ => 0,
    };
    if named != 0 {
        return Some(named);
    }
    // Single ASCII letter/digit: VK code is the uppercase ASCII value.
    let mut chars = key.chars();
    match (chars.next(), chars.next()) {
        (Some(c), None) if c.is_ascii_alphanumeric() => Some(c.to_ascii_uppercase() as i64),
        _ => None,
    }
}

/// Run the input-replay loop: accept JSON events, schedule them to
/// preserve inter-event timing, and dispatch via CDP.
pub async fn run(client_ws: WebSocket, browser_ws_url: &str) {
    let conn = match Connection::connect(browser_ws_url, None).await {
        Ok(c) => c,
        Err(_) => {
            let _ = close_with_error(client_ws, "browser connect failed").await;
            return;
        }
    };
    let Some(target_id) = resolve_page_target(&conn).await else {
        let _ = close_with_error(client_ws, "no page target available").await;
        return;
    };
    let attach = match conn
        .send(
            "Target.attachToTarget",
            &serde_json::json!({"targetId": target_id, "flatten": true}),
            None,
        )
        .await
    {
        Ok(v) => v,
        Err(_) => {
            let _ = close_with_error(client_ws, "attach failed").await;
            return;
        }
    };
    let Some(session_id) = attach["sessionId"].as_str().map(str::to_string) else {
        let _ = close_with_error(client_ws, "no session id").await;
        return;
    };

    replay_loop(client_ws, conn, session_id).await;
}

async fn replay_loop(client_ws: WebSocket, conn: Connection, session_id: String) {
    let (_tx, mut rx) = client_ws.split();
    let mut first_event_ts: Option<f64> = None;
    let mut relay_start = Instant::now();

    while let Some(Ok(msg)) = rx.next().await {
        let payload = match msg {
            Message::Text(t) => t.to_string(),
            Message::Close(_) => break,
            _ => continue,
        };
        let event: OpsInputEvent = match serde_json::from_str(&payload) {
            Ok(e) => e,
            Err(_) => continue,
        };

        if first_event_ts.is_none() {
            first_event_ts = Some(event.timestamp_ms());
            relay_start = Instant::now();
        }
        let target_offset_ms = event.timestamp_ms() - first_event_ts.unwrap_or(0.0);
        let elapsed_ms = relay_start.elapsed().as_millis() as f64;
        if target_offset_ms > elapsed_ms {
            let sleep_ms = (target_offset_ms - elapsed_ms) as u64;
            tokio::time::sleep(Duration::from_millis(sleep_ms)).await;
        }

        let (method, params) = event.to_cdp();
        let _ = conn.send(method, &params, Some(&session_id)).await;
    }

    let _ = conn
        .send(
            "Target.detachFromTarget",
            &serde_json::json!({"sessionId": session_id}),
            None,
        )
        .await;
    conn.close();
}

async fn close_with_error(mut client_ws: WebSocket, reason: &str) -> Result<(), axum::Error> {
    let body = serde_json::json!({
        "code": "ops_error",
        "channel": "input",
        "error": reason,
    });
    let _ = client_ws.send(Message::Text(body.to_string().into())).await;
    client_ws.close().await
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn pointer_move_round_trips() {
        let ev = OpsInputEvent::PointerMove {
            x: 12.5,
            y: 34.0,
            timestamp_ms: 1_234.5,
        };
        let json = serde_json::to_string(&ev).unwrap_or_default();
        let back: OpsInputEvent = serde_json::from_str(&json).unwrap();
        match back {
            OpsInputEvent::PointerMove { x, y, .. } => {
                assert!((x - 12.5).abs() < 1e-3);
                assert!((y - 34.0).abs() < 1e-3);
            }
            _ => panic!("wrong variant"),
        }
    }

    #[test]
    fn pointer_down_converts_to_mousepressed() {
        let ev = OpsInputEvent::PointerDown {
            x: 10.0,
            y: 20.0,
            button: "left".into(),
            timestamp_ms: 0.0,
        };
        let (method, params) = ev.to_cdp();
        assert_eq!(method, "Input.dispatchMouseEvent");
        assert_eq!(params["type"], "mousePressed");
        assert_eq!(params["x"], 10.0);
        assert_eq!(params["button"], "left");
    }

    #[test]
    fn keydown_with_printable_key_gets_text_field() {
        let ev = OpsInputEvent::KeyDown {
            key: "a".into(),
            code: "KeyA".into(),
            modifiers: 0,
            timestamp_ms: 0.0,
        };
        let (_, params) = ev.to_cdp();
        assert_eq!(params["text"], "a");
    }

    #[test]
    fn keydown_with_special_key_has_no_text() {
        let ev = OpsInputEvent::KeyDown {
            key: "Enter".into(),
            code: "Enter".into(),
            modifiers: 0,
            timestamp_ms: 0.0,
        };
        let (_, params) = ev.to_cdp();
        assert!(params["text"].is_null(), "{params}");
    }

    #[test]
    fn keydown_printable_with_ctrl_has_no_text() {
        // Ctrl+A is select-all, not typing "a" — `text` must be suppressed so
        // chromium runs the shortcut instead of inserting the letter.
        let ev = OpsInputEvent::KeyDown {
            key: "a".into(),
            code: "KeyA".into(),
            modifiers: 2, // Ctrl
            timestamp_ms: 0.0,
        };
        let (_, params) = ev.to_cdp();
        assert!(params["text"].is_null(), "{params}");
    }

    #[test]
    fn keydown_printable_with_meta_has_no_text() {
        let ev = OpsInputEvent::KeyDown {
            key: "r".into(),
            code: "KeyR".into(),
            modifiers: 4, // Meta/⌘
            timestamp_ms: 0.0,
        };
        let (_, params) = ev.to_cdp();
        assert!(params["text"].is_null(), "{params}");
    }

    #[test]
    fn keydown_printable_with_shift_keeps_text() {
        // Shift is not a shortcut modifier — Shift+a still produces text.
        let ev = OpsInputEvent::KeyDown {
            key: "A".into(),
            code: "KeyA".into(),
            modifiers: 8, // Shift
            timestamp_ms: 0.0,
        };
        let (_, params) = ev.to_cdp();
        assert_eq!(params["text"], "A");
    }

    #[test]
    fn backspace_carries_virtual_key_code() {
        // Without windowsVirtualKeyCode chromium ignores Backspace, so deletes
        // do nothing — the bug this guards against.
        let ev = OpsInputEvent::KeyDown {
            key: "Backspace".into(),
            code: "Backspace".into(),
            modifiers: 0,
            timestamp_ms: 0.0,
        };
        let (_, params) = ev.to_cdp();
        assert_eq!(params["windowsVirtualKeyCode"], 8);
        assert_eq!(params["nativeVirtualKeyCode"], 8);
        assert!(params["text"].is_null(), "{params}");
    }

    #[test]
    fn enter_and_arrows_carry_virtual_key_code() {
        for (key, vk) in [("Enter", 13), ("ArrowLeft", 37), ("Delete", 46)] {
            let ev = OpsInputEvent::KeyDown {
                key: key.into(),
                code: key.into(),
                modifiers: 0,
                timestamp_ms: 0.0,
            };
            let (_, params) = ev.to_cdp();
            assert_eq!(params["windowsVirtualKeyCode"], vk, "{key}");
        }
    }

    #[test]
    fn letter_carries_uppercase_virtual_key_code() {
        // Ctrl+A needs VK 65 to trigger select-all even though text is dropped.
        let ev = OpsInputEvent::KeyDown {
            key: "a".into(),
            code: "KeyA".into(),
            modifiers: 2, // Ctrl
            timestamp_ms: 0.0,
        };
        let (_, params) = ev.to_cdp();
        assert_eq!(params["windowsVirtualKeyCode"], 65);
        assert!(params["text"].is_null(), "{params}");
    }

    #[test]
    fn keyup_also_carries_virtual_key_code() {
        let ev = OpsInputEvent::KeyUp {
            key: "Backspace".into(),
            code: "Backspace".into(),
            modifiers: 0,
            timestamp_ms: 0.0,
        };
        let (_, params) = ev.to_cdp();
        assert_eq!(params["type"], "keyUp");
        assert_eq!(params["windowsVirtualKeyCode"], 8);
    }

    #[test]
    fn insert_text_converts_to_input_inserttext() {
        let ev = OpsInputEvent::InsertText {
            text: "SuperSecretPassword!".into(),
            timestamp_ms: 0.0,
        };
        let (method, params) = ev.to_cdp();
        assert_eq!(method, "Input.insertText");
        assert_eq!(params["text"], "SuperSecretPassword!");
    }

    #[test]
    fn insert_text_round_trips_from_snake_case_tag() {
        let json = r#"{"type":"insert_text","text":"héllo","timestamp_ms":5.0}"#;
        let back: OpsInputEvent = serde_json::from_str(json).unwrap();
        match back {
            OpsInputEvent::InsertText { text, .. } => assert_eq!(text, "héllo"),
            _ => panic!("wrong variant"),
        }
    }
}