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
//! Async CDP WebSocket client.
//!
//! Each `Connection` owns one WebSocket to the host's `/cdp` endpoint.
//! Sent commands are tagged with monotonically increasing ids; replies and
//! events are demuxed by the reader task into pending-request channels and
//! a broadcast event stream respectively.

use std::collections::HashMap;
use std::sync::atomic::{AtomicI64, Ordering};
use std::sync::Arc;
use std::time::Duration;

use futures::{SinkExt, StreamExt};
use serde::Serialize;
use serde_json::Value;
use tokio::io::{AsyncRead, AsyncWrite};
use tokio::sync::{broadcast, mpsc, oneshot, Mutex};
use tokio::task::JoinHandle;
use tokio_tungstenite::tungstenite::{
    self,
    client::IntoClientRequest,
    handshake::client::generate_key,
    http::{header, Request, Uri},
};
use tokio_tungstenite::WebSocketStream;

use crate::sdk::endpoint::Endpoint;
use crate::shared::error::{Error, ErrorCode};

type ReplySender = oneshot::Sender<Result<Value, CdpRemoteError>>;
type PendingMap = Arc<Mutex<HashMap<i64, ReplySender>>>;

/// One CDP connection.
pub struct Connection {
    tx: mpsc::UnboundedSender<OutMsg>,
    pending: PendingMap,
    events_tx: broadcast::Sender<CdpEvent>,
    next_id: AtomicI64,
    _reader: JoinHandle<()>,
    _writer: JoinHandle<()>,
}

enum OutMsg {
    Text(String),
    Close,
}

#[derive(Debug, Clone)]
pub struct CdpEvent {
    pub method: String,
    pub session_id: Option<String>,
    pub params: Value,
}

#[derive(Debug, Clone)]
pub struct CdpRemoteError {
    pub code: i64,
    pub message: String,
}

impl std::fmt::Display for CdpRemoteError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "CDP error {}: {}", self.code, self.message)
    }
}

impl Connection {
    /// Open a CDP connection from a parsed endpoint.
    pub async fn connect_endpoint(endpoint: &Endpoint, token: Option<&str>) -> Result<Self, Error> {
        match endpoint {
            #[cfg(unix)]
            Endpoint::Unix { path } => Self::connect_unix(path, token).await,
            _ => Self::connect(&endpoint.cdp_ws_url(), token).await,
        }
    }

    /// Open a CDP connection to `ws://endpoint/cdp` (or `wss://`),
    /// attaching the optional bearer token via the `?token_secret=` query
    /// parameter (the `_secret` suffix lets AFDATA redaction scrub it).
    pub async fn connect(endpoint_ws_url: &str, token: Option<&str>) -> Result<Self, Error> {
        // Append ?token_secret= if needed.
        let url = match token {
            Some(t) => append_query_pairs(endpoint_ws_url, &[("token_secret", t)])?,
            None => endpoint_ws_url.to_string(),
        };
        let request = build_ws_request(&url, token)?;
        let uri: Uri = url
            .parse()
            .map_err(|e| Error::new(ErrorCode::InvalidEndpoint, format!("CDP url {url:?}: {e}")))?;
        let secure = uri
            .scheme_str()
            .is_some_and(|s| s.eq_ignore_ascii_case("wss"));
        if !secure {
            // Plaintext ws:// (all local CDP, and ws:// remote hosts): connect the
            // TCP stream directly and run the handshake with client_async. We
            // avoid connect_async because, with the rustls-tls-native-roots
            // feature, it builds a TLS connector and loads the OS root-cert store
            // even for ws:// — wasted work for every CDP connect, and stack-heavy
            // enough to overflow Windows' 1 MiB main-thread stack.
            let host = uri.host().ok_or_else(|| {
                Error::new(
                    ErrorCode::InvalidEndpoint,
                    format!("CDP url has no host: {url:?}"),
                )
            })?;
            let port = uri.port_u16().unwrap_or(80);
            let stream = tokio::net::TcpStream::connect((host, port))
                .await
                .map_err(|e| {
                    Error::new(
                        ErrorCode::HostUnreachable,
                        format!(
                            "CDP connect {}: {e}",
                            agent_first_data::redact_url_secrets(&url)
                        ),
                    )
                })?;
            let (ws, _resp) = tokio_tungstenite::client_async(request, stream)
                .await
                .map_err(|e| {
                    Error::new(
                        ErrorCode::HostUnreachable,
                        format!(
                            "CDP websocket {}: {e}",
                            agent_first_data::redact_url_secrets(&url)
                        ),
                    )
                })?;
            return Ok(Self::from_ws(ws));
        }
        // wss:// — keep the TLS-capable connector.
        let (ws, _resp) = tokio_tungstenite::connect_async(request)
            .await
            .map_err(|e| {
                // Redact the token before it lands in the error envelope.
                Error::new(
                    ErrorCode::HostUnreachable,
                    format!(
                        "CDP connect {}: {e}",
                        agent_first_data::redact_url_secrets(&url)
                    ),
                )
            })?;
        Ok(Self::from_ws(ws))
    }

    #[cfg(unix)]
    async fn connect_unix(path: &std::path::Path, token: Option<&str>) -> Result<Self, Error> {
        let url = match token {
            Some(t) => append_query_pairs("ws://localhost/cdp", &[("token_secret", t)])?,
            None => "ws://localhost/cdp".to_string(),
        };
        let request = build_ws_request(&url, token)?;
        let stream = tokio::net::UnixStream::connect(path).await.map_err(|e| {
            Error::new(
                ErrorCode::HostUnreachable,
                format!("CDP connect unix:{}: {e}", path.display()),
            )
        })?;
        let (ws, _resp) = tokio_tungstenite::client_async(request, stream)
            .await
            .map_err(|e| {
                Error::new(
                    ErrorCode::HostUnreachable,
                    format!("CDP websocket over unix:{}: {e}", path.display()),
                )
            })?;
        Ok(Self::from_ws(ws))
    }

    fn from_ws<S>(ws: WebSocketStream<S>) -> Self
    where
        S: AsyncRead + AsyncWrite + Unpin + Send + 'static,
    {
        let (mut sink, mut stream) = ws.split();

        let pending: PendingMap = Arc::new(Mutex::new(HashMap::new()));
        let (events_tx, _events_rx) = broadcast::channel::<CdpEvent>(256);
        let (tx, mut rx) = mpsc::unbounded_channel::<OutMsg>();

        let pending_w = pending.clone();
        let events_w = events_tx.clone();
        let reader = tokio::spawn(async move {
            while let Some(Ok(msg)) = stream.next().await {
                match msg {
                    tungstenite::Message::Text(t) => {
                        if let Ok(v) = serde_json::from_str::<Value>(t.as_str()) {
                            dispatch(v, &pending_w, &events_w).await;
                        }
                    }
                    tungstenite::Message::Binary(_)
                    | tungstenite::Message::Ping(_)
                    | tungstenite::Message::Pong(_) => {}
                    tungstenite::Message::Close(_) | tungstenite::Message::Frame(_) => break,
                }
            }
            // On close, fail all pending requests so callers stop waiting.
            let mut map = pending_w.lock().await;
            for (_, sender) in map.drain() {
                let _ = sender.send(Err(CdpRemoteError {
                    code: -1,
                    message: "CDP connection closed".into(),
                }));
            }
        });

        let writer = tokio::spawn(async move {
            while let Some(out) = rx.recv().await {
                let msg = match out {
                    OutMsg::Text(t) => tungstenite::Message::Text(t.as_str().into()),
                    OutMsg::Close => {
                        let _ = sink.send(tungstenite::Message::Close(None)).await;
                        break;
                    }
                };
                if sink.send(msg).await.is_err() {
                    break;
                }
            }
        });

        Self {
            tx,
            pending,
            events_tx,
            next_id: AtomicI64::new(1),
            _reader: reader,
            _writer: writer,
        }
    }

    /// Subscribe to events for the lifetime of this connection.
    pub fn subscribe(&self) -> broadcast::Receiver<CdpEvent> {
        self.events_tx.subscribe()
    }

    /// Send a CDP command and await the result. `session_id` is `Some` when
    /// the call is scoped to a flattened session (after Target.attachToTarget).
    pub async fn send<P: Serialize>(
        &self,
        method: &str,
        params: &P,
        session_id: Option<&str>,
    ) -> Result<Value, Error> {
        let id = self.next_id.fetch_add(1, Ordering::SeqCst);
        let body = match session_id {
            Some(sid) => serde_json::json!({
                "id": id,
                "method": method,
                "params": params,
                "sessionId": sid,
            }),
            None => serde_json::json!({
                "id": id,
                "method": method,
                "params": params,
            }),
        };
        let serialized = serde_json::to_string(&body).map_err(|e| {
            Error::new(
                ErrorCode::InternalError,
                format!("CDP send: serialize {method}: {e}"),
            )
        })?;
        let (resp_tx, resp_rx) = oneshot::channel();
        self.pending.lock().await.insert(id, resp_tx);
        self.tx
            .send(OutMsg::Text(serialized))
            .map_err(|_| Error::new(ErrorCode::CdpUnavailable, "CDP writer closed before send"))?;
        let value = resp_rx
            .await
            .map_err(|_| Error::new(ErrorCode::CdpUnavailable, "CDP reader closed"))?
            .map_err(|e| Error::new(ErrorCode::CdpError, e.to_string()))?;
        Ok(value)
    }

    /// Wait for a CDP event matching `predicate` (true = matches).
    /// Returns `cdp_timeout` if `timeout` elapses first.
    pub async fn wait_event<F>(
        &self,
        timeout: Duration,
        mut predicate: F,
    ) -> Result<CdpEvent, Error>
    where
        F: FnMut(&CdpEvent) -> bool,
    {
        let mut rx = self.events_tx.subscribe();
        let deadline = tokio::time::Instant::now() + timeout;
        loop {
            let remaining = deadline.saturating_duration_since(tokio::time::Instant::now());
            if remaining.is_zero() {
                return Err(Error::new(ErrorCode::CdpTimeout, "wait_event: timed out"));
            }
            match tokio::time::timeout(remaining, rx.recv()).await {
                Ok(Ok(ev)) if predicate(&ev) => return Ok(ev),
                Ok(Ok(_)) => continue,
                Ok(Err(broadcast::error::RecvError::Lagged(_))) => continue,
                Ok(Err(broadcast::error::RecvError::Closed)) => {
                    return Err(Error::new(
                        ErrorCode::CdpUnavailable,
                        "wait_event: events channel closed",
                    ));
                }
                Err(_) => {
                    return Err(Error::new(ErrorCode::CdpTimeout, "wait_event: timed out"));
                }
            }
        }
    }

    pub fn close(&self) {
        let _ = self.tx.send(OutMsg::Close);
    }
}

impl Drop for Connection {
    fn drop(&mut self) {
        let _ = self.tx.send(OutMsg::Close);
    }
}

async fn dispatch(msg: Value, pending: &PendingMap, events: &broadcast::Sender<CdpEvent>) {
    if let Some(id) = msg.get("id").and_then(|v| v.as_i64()) {
        let mut map = pending.lock().await;
        if let Some(sender) = map.remove(&id) {
            if let Some(err) = msg.get("error") {
                let code = err.get("code").and_then(|v| v.as_i64()).unwrap_or(-1);
                let message = err
                    .get("message")
                    .and_then(|v| v.as_str())
                    .unwrap_or("")
                    .to_string();
                let _ = sender.send(Err(CdpRemoteError { code, message }));
            } else {
                let result = msg.get("result").cloned().unwrap_or(Value::Null);
                let _ = sender.send(Ok(result));
            }
        }
    } else if let Some(method) = msg.get("method").and_then(|v| v.as_str()) {
        let params = msg.get("params").cloned().unwrap_or(Value::Null);
        let session_id = msg
            .get("sessionId")
            .and_then(|v| v.as_str())
            .map(str::to_string);
        let _ = events.send(CdpEvent {
            method: method.to_string(),
            session_id,
            params,
        });
    }
}

fn append_query_pairs(url: &str, pairs: &[(&str, &str)]) -> Result<String, Error> {
    let mut parsed = url::Url::parse(url)
        .map_err(|e| Error::new(ErrorCode::InvalidEndpoint, format!("CDP url {url:?}: {e}")))?;
    {
        let mut query = parsed.query_pairs_mut();
        for (key, value) in pairs {
            query.append_pair(key, value);
        }
    }
    Ok(parsed.to_string())
}

fn build_ws_request(url: &str, _token: Option<&str>) -> Result<Request<()>, Error> {
    // tokio_tungstenite::connect_async accepts &str directly via IntoClientRequest,
    // but we go through the explicit Request type so we can attach headers later.
    let uri: Uri = url
        .parse()
        .map_err(|e| Error::new(ErrorCode::InvalidEndpoint, format!("CDP url {url:?}: {e}")))?;
    let host = uri.authority().map(|a| a.as_str()).unwrap_or("localhost");
    let req = Request::builder()
        .method("GET")
        .uri(url)
        .header(header::HOST, host)
        .header(header::CONNECTION, "Upgrade")
        .header(header::UPGRADE, "websocket")
        .header(header::SEC_WEBSOCKET_VERSION, "13")
        .header(header::SEC_WEBSOCKET_KEY, generate_key())
        .body(())
        .map_err(|e| Error::new(ErrorCode::InternalError, format!("CDP build request: {e}")))?;
    // _token is already baked into the URL by the caller; bearer header
    // would also be acceptable but axum's WebSocketUpgrade ignores it for
    // the upgrade handshake.
    req.into_client_request().map_err(|e| {
        Error::new(
            ErrorCode::InternalError,
            format!("CDP into_client_request: {e}"),
        )
    })
}

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

    #[test]
    fn query_pairs_are_percent_encoded() {
        let url = append_query_pairs("ws://localhost:9222/cdp", &[("token", "a+b&c%20")]).unwrap();
        assert_eq!(url, "ws://localhost:9222/cdp?token=a%2Bb%26c%2520");
    }

    // The bearer travels as `?token_secret=`; AFDATA redaction must scrub it
    // before the failed-connect URL reaches the error envelope.
    #[tokio::test]
    async fn connect_error_redacts_token_secret() {
        // Port 1 refuses fast, so we exercise the map_err path deterministically.
        let err = Connection::connect("ws://127.0.0.1:1/cdp", Some("supersecret"))
            .await
            .err()
            .expect("connect to closed port must fail");
        let msg = err.to_string();
        assert!(
            msg.contains("token_secret=***"),
            "token not redacted: {msg}"
        );
        assert!(!msg.contains("supersecret"), "raw token leaked: {msg}");
    }
}