chrome-agent 0.15.0

Browser automation for AI agents. Single binary, zero deps, CDP direct to Chrome.
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
use std::io::Write as _;

use serde_json::{json, Value};
use tokio::io::{AsyncBufReadExt, BufReader};

use crate::browser::{self, BrowserOptions};
use crate::cdp::client::CdpClient;
use crate::commands;
use crate::pipe_dispatch::{
    dispatch_assert, dispatch_back, dispatch_batch, dispatch_check, dispatch_click,
    dispatch_console, dispatch_dblclick, dispatch_diff, dispatch_download, dispatch_drag,
    dispatch_emulate, dispatch_eval, dispatch_extract, dispatch_fill, dispatch_fill_and_submit,
    dispatch_fill_form, dispatch_forward, dispatch_frame, dispatch_goto,
    dispatch_history, dispatch_hover, dispatch_inspect,
    dispatch_navigate_and_read, dispatch_network, dispatch_pdf, dispatch_press,
    dispatch_read, dispatch_screenshot, dispatch_scroll, dispatch_select,
    dispatch_tabs, dispatch_text, dispatch_type, dispatch_upload,
    dispatch_wait, dispatch_webmcp_call, dispatch_webmcp_list, EmulationRecovery,
};
use crate::run_helpers::error_hint;
use crate::session::{self, SessionStore};
use crate::cli::Cli;

/// Run pipe mode: persistent CDP connection, reading JSON commands from stdin.
pub async fn run_pipe(cli: &Cli) -> Result<(), crate::BoxError> {
    let mut store = session::load_session()?;
    let want_headless = !cli.headed;
    let requested_proxy = browser::normalized_proxy_option(
        cli.connect.as_deref(),
        cli.proxy_server.as_deref(),
    )?;
    let requested_chrome_args = browser::normalized_chrome_args_option(
        cli.connect.as_deref(),
        &cli.chrome_args,
    )?;
    // Inherit a running named browser's proxy/chrome-args when the flag is omitted so a
    // relaunch never silently drops it (see run.rs for the full rationale).
    let effective_proxy = requested_proxy
        .or_else(|| store.browsers.get(&cli.browser).and_then(|b| b.proxy_server.clone()));
    let effective_chrome_args =
        crate::chrome_args::effective_chrome_args(&store, &cli.browser, &requested_chrome_args);

    let (conn, browser_client) = connect_browser(
        &mut store,
        cli,
        want_headless,
        effective_proxy.clone(),
        effective_chrome_args.clone(),
    )
    .await?;

    let http_endpoint = conn.http_endpoint.as_deref().ok_or(
        "No HTTP endpoint available. Cannot resolve page WebSocket URL.",
    )?;

    let target_id = {
        let browser_session = session::ensure_browser(
            &mut store,
            &cli.browser,
            &conn.ws_endpoint,
            conn.pid,
            want_headless,
            effective_proxy,
            effective_chrome_args,
        );
        crate::run_helpers::resolve_page_target(&browser_client, browser_session, &cli.page).await?
    };
    let _ = session::save_session(&mut store);

    let page_ws = browser::get_page_ws_url(http_endpoint, &target_id).await?;
    let client = CdpClient::connect(&page_ws).await?;
    // The caller's own answer to "how long am I willing to wait" also bounds every CDP
    // response, so a page promise that never settles fails instead of hanging forever.
    client.set_call_timeout(std::time::Duration::from_secs(cli.timeout));
    client.enable("Page").await?;

    // Console interceptor (stealth-safe)
    commands::console::inject(&client).await;

    if cli.stealth {
        crate::setup::apply_stealth(&client).await;
    } else {
        client.enable("Runtime").await?;
    }
    let dialog_policy = crate::setup::DialogPolicy::parse(&cli.dialog)?;
    client.spawn_dialog_handler(dialog_policy, cli.dialog_text.clone());
    let policy = report_policy(cli)?;
    // Do not fail before reading stdin when a stored device configuration no longer applies. The
    // recovery state reports that error for ordinary commands while still admitting
    // `emulate device` and `emulate reset`, the two commands that can repair the configuration.
    let mut emulation_recovery =
        EmulationRecovery::new(&client, &store, &cli.browser, &cli.page).await;

    // Main loop: read JSON commands from stdin
    let stdin = BufReader::new(tokio::io::stdin());
    let mut lines = stdin.lines();
    // What `macro record` distils at the end of the session. Slim entries, so this stays small
    // whatever the session did.
    let mut history: Vec<crate::macros_record::Observed> = Vec::new();

    loop {
        let Ok(Some(line)) = lines.next_line().await else {
            break;
        };
        let line = line.trim().to_string();
        if line.is_empty() { continue; }

        let cmd: Value = match serde_json::from_str(&line) {
            Ok(v) => v,
            Err(e) => { emit(&json!({"ok": false, "error": format!("Invalid JSON: {e}")})); continue; }
        };

        // A recording that never opened used to be silent: the response was ok:true and
        // stdout was indistinguishable from a session being written, so the agent finds
        // out at `replay` time that there is nothing to replay.
        let record_path = cmd.get("_record").and_then(Value::as_str).map(String::from);
        if let Some(ref path) = record_path
            && let Err(e) = commands::record::start_recording(path) {
                emit(&json!({"ok": false, "error": format!("{e}"), "hint": "Check the --record path's directory exists and is writable."}));
                continue;
            }

        // A macro is distilled from the session's own history, so `macro record` needs no
        // foresight: the agent finds out it succeeded, and only then asks for the path to be
        // kept. Answered before `dispatch` because it acts on the session rather than the page.
        if cmd.get("cmd").and_then(Value::as_str) == Some("macro") {
            let answer = crate::macros_cmd::dispatch_pipe(&cmd, &history)
                .unwrap_or_else(|e| json!({"ok": false, "error": e.to_string()}));
            emit(&answer);
            continue;
        }

        let mut response = if let Some(response) = emulation_recovery.refusal_for(&cmd) {
            response
        } else {
            dispatch(
                &client, &browser_client, &mut store,
                &cli.browser, &cli.page, &target_id, cli.timeout, cli.max_depth,
                policy,
                &cmd,
                &mut emulation_recovery,
            ).await
        };
        emulation_recovery.update_after(&cmd, &response);

        if let Some(ref path) = record_path
            && let Err(e) = commands::record::log_entry(path, &cmd, &response) {
                // The command itself ran; only the record of it was lost. Say so on the
                // response rather than failing an action that already happened.
                response["recording_error"] = json!(format!("{e}"));
            }

        // Slim on purpose (`macros_record::Observed`): a session keeps this for its whole life
        // and a full response carries snapshots. What is retained is what the whitelist and the
        // filter read, which is also why a session's history cannot leak a page's text.
        let snapshot = store
            .browsers
            .get(&cli.browser)
            .and_then(|b| b.pages.get(&cli.page))
            .and_then(|p| p.last_snapshot.clone());
        history.push(crate::macros_record::Observed::read_with_snapshot(
            &cmd,
            &response,
            snapshot.as_deref(),
        ));

        emit(&response);
    }

    let _ = session::save_session(&mut store);
    Ok(())
}

/// Replay a recorded session file, optionally substituting variables.
/// Everything a session needs to dispatch commands: the two clients, the store, the page.
///
/// `run_pipe`, `run_replay` and `macros_run` opened this identically, sixty lines each. One
/// copy, because a fourth entry point is a fourth chance for them to drift.
pub struct Session {
    pub store: SessionStore,
    pub browser_client: CdpClient,
    pub client: CdpClient,
    pub target_id: String,
    pub policy: crate::run_helpers::ReportPolicy,
}

pub async fn open_session(cli: &Cli) -> Result<Session, crate::BoxError> {
    let mut store = session::load_session()?;
    let want_headless = !cli.headed;
    let requested_proxy =
        browser::normalized_proxy_option(cli.connect.as_deref(), cli.proxy_server.as_deref())?;
    let requested_chrome_args =
        browser::normalized_chrome_args_option(cli.connect.as_deref(), &cli.chrome_args)?;
    let effective_proxy = requested_proxy
        .or_else(|| store.browsers.get(&cli.browser).and_then(|b| b.proxy_server.clone()));
    let effective_chrome_args =
        crate::chrome_args::effective_chrome_args(&store, &cli.browser, &requested_chrome_args);

    let (conn, browser_client) = connect_browser(
        &mut store,
        cli,
        want_headless,
        effective_proxy.clone(),
        effective_chrome_args.clone(),
    )
    .await?;

    let http_endpoint = conn
        .http_endpoint
        .as_deref()
        .ok_or("No HTTP endpoint available. Cannot resolve page WebSocket URL.")?;
    let target_id = {
        let browser_session = session::ensure_browser(
            &mut store,
            &cli.browser,
            &conn.ws_endpoint,
            conn.pid,
            want_headless,
            effective_proxy,
            effective_chrome_args,
        );
        crate::run_helpers::resolve_page_target(&browser_client, browser_session, &cli.page).await?
    };
    let _ = session::save_session(&mut store);

    let page_ws = browser::get_page_ws_url(http_endpoint, &target_id).await?;
    let client = CdpClient::connect(&page_ws).await?;
    client.set_call_timeout(std::time::Duration::from_secs(cli.timeout));
    client.enable("Page").await?;
    commands::console::inject(&client).await;
    if cli.stealth {
        crate::setup::apply_stealth(&client).await;
    } else {
        client.enable("Runtime").await?;
    }
    let dialog_policy = crate::setup::DialogPolicy::parse(&cli.dialog)?;
    client.spawn_dialog_handler(dialog_policy, cli.dialog_text.clone());
    let policy = report_policy(cli)?;
    Ok(Session { store, browser_client, client, target_id, policy })
}

/// Dispatch one command on an open session. `pub` for `macros_run`, which needs exactly the
/// same execution semantics as pipe and batch — a macro step IS a pipe command.
pub async fn dispatch_on(
    session: &mut Session,
    cli: &Cli,
    cmd: &Value,
    emulation_recovery: &mut EmulationRecovery,
) -> Value {
    if let Some(response) = emulation_recovery.refusal_for(cmd) {
        return response;
    }
    let response = dispatch(
        &session.client,
        &session.browser_client,
        &mut session.store,
        &cli.browser,
        &cli.page,
        &session.target_id,
        cli.timeout,
        cli.max_depth,
        session.policy,
        cmd,
        emulation_recovery,
    )
    .await;
    emulation_recovery.update_after(cmd, &response);
    response
}

pub async fn run_replay(
    cli: &Cli, file: &str, vars: Option<&[String]>,
) -> Result<(), crate::BoxError> {
    let content = std::fs::read_to_string(file)
        .map_err(|e| format!("Cannot read replay file '{file}': {e}"))?;

    let replacements: Vec<(&str, &str)> = vars
        .unwrap_or(&[]).iter().filter_map(|pair| pair.split_once('=')).collect();

    let mut store = session::load_session()?;
    let want_headless = !cli.headed;
    let requested_proxy = browser::normalized_proxy_option(
        cli.connect.as_deref(),
        cli.proxy_server.as_deref(),
    )?;
    let requested_chrome_args = browser::normalized_chrome_args_option(
        cli.connect.as_deref(),
        &cli.chrome_args,
    )?;
    let effective_proxy = requested_proxy
        .or_else(|| store.browsers.get(&cli.browser).and_then(|b| b.proxy_server.clone()));
    let effective_chrome_args =
        crate::chrome_args::effective_chrome_args(&store, &cli.browser, &requested_chrome_args);
    let (conn, browser_client) = connect_browser(
        &mut store,
        cli,
        want_headless,
        effective_proxy.clone(),
        effective_chrome_args.clone(),
    )
    .await?;

    let http_endpoint = conn.http_endpoint.as_deref().ok_or("No HTTP endpoint available.")?;
    let target_id = {
        let browser_session = session::ensure_browser(
            &mut store,
            &cli.browser,
            &conn.ws_endpoint,
            conn.pid,
            want_headless,
            effective_proxy,
            effective_chrome_args,
        );
        crate::run_helpers::resolve_page_target(&browser_client, browser_session, &cli.page).await?
    };
    let _ = session::save_session(&mut store);

    let page_ws = browser::get_page_ws_url(http_endpoint, &target_id).await?;
    let client = CdpClient::connect(&page_ws).await?;
    // The caller's own answer to "how long am I willing to wait" also bounds every CDP
    // response, so a page promise that never settles fails instead of hanging forever.
    client.set_call_timeout(std::time::Duration::from_secs(cli.timeout));
    client.enable("Page").await?;
    commands::console::inject(&client).await;
    if cli.stealth { crate::setup::apply_stealth(&client).await; }
    else { client.enable("Runtime").await?; }
    let dialog_policy = crate::setup::DialogPolicy::parse(&cli.dialog)?;
    client.spawn_dialog_handler(dialog_policy, cli.dialog_text.clone());
    // A recording may begin with the device/reset command that repairs its stored configuration.
    // Replay therefore uses the same recovery state as a live pipe.
    let mut emulation_recovery =
        EmulationRecovery::new(&client, &store, &cli.browser, &cli.page).await;
    let policy = report_policy(cli)?;

    for line in content.lines() {
        let line = line.trim();
        if line.is_empty() || line.starts_with('#') { continue; }
        let mut resolved = line.to_string();
        for (key, val) in &replacements {
            resolved = resolved.replace(&format!("{{{{{key}}}}}"), val);
        }

        let parsed: Value = serde_json::from_str(&resolved)
            .map_err(|e| format!("Invalid JSON in replay: {e}"))?;

        let cmd = if parsed.get("cmd").is_some_and(Value::is_object) && parsed.get("response").is_some() {
            parsed.get("cmd").cloned().unwrap_or_default()
        } else { parsed };

        let response = if let Some(response) = emulation_recovery.refusal_for(&cmd) {
            response
        } else {
            dispatch(
                &client, &browser_client, &mut store,
                &cli.browser, &cli.page, &target_id, cli.timeout, cli.max_depth,
                policy,
                &cmd,
                &mut emulation_recovery,
            ).await
        };
        emulation_recovery.update_after(&cmd, &response);

        emit(&response);
    }

    let _ = session::save_session(&mut store);
    Ok(())
}

// ---------------------------------------------------------------------------
// Dispatch
// ---------------------------------------------------------------------------

#[allow(clippy::too_many_arguments)]
async fn dispatch(
    client: &CdpClient, browser_client: &CdpClient, store: &mut SessionStore,
    browser_name: &str, page_name: &str, target_id: &str,
    timeout: u64, global_max_depth: Option<usize>,
    report: crate::run_helpers::ReportPolicy, cmd: &Value,
    emulation_recovery: &mut EmulationRecovery,
) -> Value {
    let cmd_name = cmd.get("cmd").and_then(Value::as_str).unwrap_or("");
    // Same contract as the CLI: an action says what it changed. Capture the baseline first,
    // because a command run with `inspect` refreshes it itself.
    let baseline = if report.changes && crate::pipe_dispatch::mutates_page(cmd_name) {
        store
            .browsers
            .get(browser_name)
            .and_then(|b| b.pages.get(page_name))
            .map(|p| {
                (
                    p.last_snapshot.clone(),
                    p.last_snapshot_frame.clone().zip(p.last_snapshot_loader.clone()),
                )
            })
    } else {
        None
    };

    let mut value = {
    let result: Result<Value, crate::BoxError> = match cmd_name {
        "goto" => dispatch_goto(client, store, browser_name, page_name, target_id, timeout, global_max_depth, cmd).await,
        "click" => dispatch_click(client, store, browser_name, page_name, target_id, global_max_depth, report, cmd).await,
        "fill" => dispatch_fill(client, store, browser_name, page_name, target_id, global_max_depth, cmd).await,
        "inspect" => dispatch_inspect(client, store, browser_name, page_name, target_id, cmd).await,
        "eval" => dispatch_eval(client, cmd).await,
        "read" => dispatch_read(client, cmd).await,
        "text" => dispatch_text(client, store, browser_name, page_name, cmd).await,
        "screenshot" => dispatch_screenshot(client, store, browser_name, page_name, cmd).await,
        "pdf" => dispatch_pdf(client, cmd).await,
        "download" => dispatch_download(client, store, browser_name, page_name, timeout, report, cmd).await,
        "wait" => dispatch_wait(client, timeout, cmd).await,
        "back" => dispatch_back(client).await,
        "forward" => dispatch_forward(client).await,
        "scroll" => dispatch_scroll(client, store, browser_name, page_name, cmd).await,
        "type" => dispatch_type(client, cmd).await,
        "press" => dispatch_press(client, cmd).await,
        "fill-form" | "fill_form" | "fillform" => dispatch_fill_form(client, store, browser_name, page_name, target_id, global_max_depth, cmd).await,
        "dblclick" => dispatch_dblclick(client, store, browser_name, page_name, target_id, global_max_depth, report, cmd).await,
        "select" => dispatch_select(client, store, browser_name, page_name, target_id, global_max_depth, cmd).await,
        "check" => dispatch_check(client, store, browser_name, page_name, report, cmd).await,
        "uncheck" => {
            let mut cmd_with_desired = cmd.clone();
            if let Some(m) = cmd_with_desired.as_object_mut() {
                m.insert("desired".into(), Value::Bool(false));
            }
            dispatch_check(client, store, browser_name, page_name, report, &cmd_with_desired).await
        }
        "upload" => dispatch_upload(client, store, browser_name, page_name, cmd).await,
        "drag" => dispatch_drag(client, store, browser_name, page_name, cmd).await,
        "hover" => dispatch_hover(client, store, browser_name, page_name, cmd).await,
        "tabs" => dispatch_tabs(browser_client, store).await,
        "network" => dispatch_network(client, cmd).await,
        "console" => dispatch_console(client, cmd).await,
        "diff" => dispatch_diff(client, store, browser_name, page_name, target_id).await,
        "extract" => dispatch_extract(client, cmd).await,
        "navigate_and_read" | "navigate-and-read" => dispatch_navigate_and_read(client, store, browser_name, page_name, target_id, timeout, cmd).await,
        "fill_and_submit" | "fill-and-submit" => dispatch_fill_and_submit(client, timeout, cmd).await,
        "history" => dispatch_history(cmd),
        "frame" => dispatch_frame(client, cmd).await,
        "emulate" => dispatch_emulate(client, store, browser_name, page_name, cmd).await,
        "assert" => dispatch_assert(client, store, browser_name, page_name, cmd).await,
        "webmcp_list" | "webmcp-list" => dispatch_webmcp_list(client).await,
        "webmcp_call" | "webmcp-call" => dispatch_webmcp_call(client, cmd).await,
        "batch" => dispatch_batch(client, browser_client, store, browser_name, page_name, target_id, timeout, global_max_depth, report, cmd, emulation_recovery).await,
        "" => Err("Missing \"cmd\" field".into()),
        other => Err(format!("Unknown command: {other}").into()),
    };

    // `result` must not outlive this block: BoxError is not Send, and an await with it
    // still in scope would make every caller's future non-Send.
    match result {
        Ok(v) => v,
        Err(e) => {
            // A refusal carries what it measured — the receiver, the aim point, the branch —
            // and flattening it to its Display would drop all of it on the one path where
            // nothing was dispatched and the caller has to re-plan.
            if let Some(refused) = crate::hit_test::refusal_in(&e) {
                return refused.to_json(browser_name);
            }
            let msg = e.to_string();
            let mut obj = json!({"ok": false, "error": msg});
            if let Some(h) = error_hint(&msg, browser_name) { obj["hint"] = json!(h); }
            return obj;
        }
    }
    };
    // `--verdict off` is a decision, not an observation. Saying so costs two fields and no
    // page read, and it is the difference between "I did not look" and "nothing moved".
    if !report.changes && crate::pipe_dispatch::mutates_page(cmd_name) {
        // The hit test still ran: it is part of aiming the action, not part of the report.
        // An intercepted click says so even here, where the page was never re-read.
        crate::pipe_report::attach_verdict_for(
            client,
            &mut value,
            crate::verdict::Observation::ReportingDisabled,
        );
    }
    if let Some((old_text, old_url)) = baseline {
        crate::pipe_dispatch::attach_change_report(
            client, store, browser_name, page_name, target_id, report, old_text.as_deref(),
            old_url, &mut value,
        )
        .await;
    }
    value
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// The global reporting flags, parsed once for the session rather than per command.
fn report_policy(cli: &Cli) -> Result<crate::run_helpers::ReportPolicy, crate::BoxError> {
    Ok(crate::run_helpers::ReportPolicy {
        changes: cli.verdict == "auto",
        budget: cli.budget,
        on_intercept: crate::hit_test::OnIntercept::parse(&cli.on_intercept)?,
    })
}

fn emit(value: &Value) {
    let line = serde_json::to_string(value).unwrap_or_default();
    let stdout = std::io::stdout();
    let mut handle = stdout.lock();
    let _ = writeln!(handle, "{line}");
    let _ = handle.flush();
}

async fn connect_browser(
    store: &mut SessionStore,
    cli: &Cli,
    want_headless: bool,
    effective_proxy: Option<String>,
    effective_chrome_args: Vec<String>,
) -> Result<(browser::BrowserConnection, CdpClient), crate::BoxError> {
    if let Some(existing) = store.browsers.get(&cli.browser) {
        let mode_matches = existing.headless == want_headless;
        let ws = &existing.ws_endpoint;
        let http = browser::extract_http_from_ws(ws);

        if mode_matches {
            if let Ok(client) = CdpClient::connect(ws).await {
                session::ensure_proxy_compatible(existing, effective_proxy.as_deref())?;
                session::ensure_chrome_args_compatible(existing, &effective_chrome_args)?;
                let conn = browser::BrowserConnection {
                    ws_endpoint: ws.clone(), http_endpoint: Some(http), pid: existing.pid,
                };
                client.set_call_timeout(std::time::Duration::from_secs(cli.timeout));
                return Ok((conn, client));
            }
        } else if let Some(pid) = existing.pid {
            #[cfg(unix)]
            {
                let _ = std::process::Command::new("kill")
                    .arg(pid.to_string())
                    .stdout(std::process::Stdio::null())
                    .stderr(std::process::Stdio::null())
                    .status();
            }
        }
        store.browsers.remove(&cli.browser);
    }

    let opts = BrowserOptions {
        name: cli.browser.clone(), headless: want_headless,
        ignore_https_errors: cli.ignore_https_errors, stealth: cli.stealth,
        connect: cli.connect.clone(), proxy_server: effective_proxy,
        copy_cookies: cli.copy_cookies,
        chrome_args: effective_chrome_args,
    };
    let conn = browser::resolve_browser(&opts).await?;
    let client = CdpClient::connect(&conn.ws_endpoint).await?;
    // Browser-level Target.* calls obey the caller's --timeout like page calls do.
    client.set_call_timeout(std::time::Duration::from_secs(cli.timeout));
    Ok((conn, client))
}