oxibrowser 0.14.0

Headless browser engine with CDP support
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
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
//! OxiBrowser CLI 2.0 — headless browser for AI agents.
//!
//! Human is the default. `--json` opts into machine-readable output.
//!
//! 8 subcommands: fetch, extract, run, session, serve, search, describe, skill, version

use clap::{Parser, Subcommand};
use serde_json::Value;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tracing::info;

mod describe;
mod output;
mod search;
mod session;
mod skill;
mod validate;

/// OxiBrowser — headless browser for AI agents.
#[derive(Parser)]
#[command(name = "oxibrowser")]
#[command(version, about = "Headless browser for AI agents — single static binary, no Chromium")]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Fetch a URL and return content. Supports interaction before output.
    Fetch {
        /// URL to fetch.
        url: String,
        /// Output format: html, markdown, or text.
        #[arg(long, default_value = "markdown")]
        format: String,
        /// Output as JSON (for agents / scripting).
        #[arg(long)]
        json: bool,
        /// Truncate output at N bytes (JSON mode).
        #[arg(long)]
        max_bytes: Option<u64>,
        /// Comma-separated fields to include: url,title,status,markdown,html,text,content_type.
        #[arg(long)]
        fields: Option<String>,
        /// Page metadata only (headings, links_count, text_length).
        #[arg(long)]
        summary: bool,
        /// Evaluate JS expression after page load.
        #[arg(long)]
        eval: Option<String>,
        /// Click element matching CSS selector.
        #[arg(long)]
        click: Option<String>,
        /// Fill input (format: "selector:value").
        #[arg(long)]
        fill: Option<String>,
        /// Press key (Enter, Tab, Ctrl+C, etc.).
        #[arg(long)]
        press: Option<String>,
        /// Wait for CSS selector before output.
        #[arg(long)]
        wait: Option<String>,
        /// Wait timeout in ms.
        #[arg(long, default_value_t = 5000)]
        wait_timeout: u64,
        /// Extract text from selector instead of full content.
        #[arg(long)]
        extract: Option<String>,
        /// With --extract: return all matches.
        #[arg(long)]
        all: bool,
        /// Print HTTP headers to stderr.
        #[arg(long)]
        headers: bool,
        /// Timeout in seconds.
        #[arg(long, default_value_t = 30)]
        timeout: u64,
    },

    /// Extract structured data from a URL.
    Extract {
        /// URL to extract from.
        url: String,
        /// CSS selector to match elements.
        #[arg(long)]
        selector: Option<String>,
        /// Return all matches (not just first).
        #[arg(long)]
        all: bool,
        /// Comma-separated attributes to extract (text,href,data-*,src,...).
        #[arg(long, default_value = "text")]
        attrs: String,
        /// Extract all <a href> values.
        #[arg(long)]
        links: bool,
        /// Extract the <title> text.
        #[arg(long)]
        title: bool,
        /// Extract body text.
        #[arg(long)]
        text: bool,
        /// Extract page as markdown.
        #[arg(long)]
        markdown: bool,
        /// Truncate output at N bytes (JSON mode).
        #[arg(long)]
        max_bytes: Option<u64>,
        /// Output as JSON.
        #[arg(long)]
        json: bool,
        /// Timeout in seconds.
        #[arg(long, default_value_t = 30)]
        timeout: u64,
    },

    /// Run a YAML browser automation script.
    Run {
        /// Path to YAML script file or inline YAML.
        script: String,
        /// Timeout in seconds.
        #[arg(long, default_value_t = 60)]
        timeout: u64,
        /// No-op (run always outputs JSON).
        #[arg(long, hide = true)]
        json: bool,
    },

    /// Start interactive session (stdin/stdout JSON REPL).
    Session,

    /// Start CDP server for Puppeteer/Playwright.
    Serve {
        /// Host to bind to.
        #[arg(long, default_value = "127.0.0.1")]
        host: String,
        /// Port to listen on.
        #[arg(long, default_value_t = 9222)]
        port: u16,
        /// Cookie persistence file.
        #[arg(long)]
        cookie_file: Option<String>,
    },

    /// Print CLI schema as JSON (for agents).
    Describe {
        /// Specific command to describe.
        command: Option<String>,
        /// Minimal output (~200 tokens).
        #[arg(long)]
        compact: bool,
        /// No-op (describe always outputs JSON).
        #[arg(long, hide = true)]
        json: bool,
    },

    /// Print agent skill guide.
    Skill {
        /// Output as JSON.
        #[arg(long, hide = true)]
        json: bool,
    },

    /// Search the web or GitHub (lightweight HTTP, no browser needed).
    Search {
        /// Search query (all positional args are joined).
        query: Vec<String>,
        /// Search source: web, github, github-issues.
        #[arg(long, default_value = "web", value_parser = clap::builder::PossibleValuesParser::new(["web", "github", "github-issues"]))]
        source: String,
        /// Search engine(s) for web source: ddg, wiki, bing (comma-separated).
        #[arg(long, default_value = "ddg")]
        engine: String,
        /// Repository for github-issues (owner/repo).
        #[arg(long)]
        repo: Option<String>,
        /// GitHub personal access token (increases rate limit).
        #[arg(long)]
        token: Option<String>,
        /// Output as JSON.
        #[arg(long)]
        json: bool,
        /// Max results (max 30).
        #[arg(long, default_value_t = 10)]
        max_results: u32,
        /// Timeout in seconds.
        #[arg(long, default_value_t = 15)]
        timeout: u64,
    },

    /// Print version information.
    Version {
        /// Output as JSON.
        #[arg(long, hide = true)]
        json: bool,
    },
}

// ---------------------------------------------------------------------------
// Output decision: --json → agent, otherwise → human.
// ---------------------------------------------------------------------------

/// Whether to use JSON output. Only true when --json is explicitly set.
fn use_json(explicit_json: bool) -> bool {
    explicit_json
}

// ---------------------------------------------------------------------------
// Entry
// ---------------------------------------------------------------------------

#[tokio::main]
async fn main() {
    tracing_subscriber::fmt()
        .with_writer(std::io::stderr)
        .with_env_filter(
            tracing_subscriber::EnvFilter::try_from_default_env()
                .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("warn")),
        )
        .init();

    let cli = Cli::parse();

    let exit_code = match cli.command {
        Commands::Fetch {
            url, format, json, max_bytes, fields, summary, eval,
            click, fill, press, wait, wait_timeout, extract, all, headers, timeout,
        } => {
            run_fetch(
                &url, &format, json, max_bytes, fields.as_deref(), summary,
                eval.as_deref(), click.as_deref(), fill.as_deref(), press.as_deref(),
                wait.as_deref(), wait_timeout, extract.as_deref(), all, headers, timeout,
            ).await
        }
        Commands::Extract {
            url, selector, all, attrs, links, title, text, markdown,
            max_bytes, json, timeout,
        } => {
            run_extract(
                &url, selector.as_deref(), all, &attrs,
                links, title, text, markdown, max_bytes, json, timeout,
            ).await
        }
        Commands::Run { script, timeout, .. } => run_script(&script, timeout).await,
        Commands::Session => session::run_session().await,
        Commands::Serve { host, port, cookie_file } => {
            run_serve(&host, port, cookie_file.as_deref()).await
        }
        Commands::Search { query, source, engine, repo, token, json, max_results, timeout } => {
            run_search(&query, &source, &engine, repo.as_deref(), token.as_deref(), json, max_results, timeout).await
        }
        Commands::Describe { command, compact, .. } => run_describe(command.as_deref(), compact),
        Commands::Skill { json } => {
            if json {
                let resp = output::CliResponse::success(serde_json::json!({"skill": skill::skill_text()}));
                resp.print_json();
                0
            } else {
                print!("{}", skill::skill_text());
                0
            }
        }
        Commands::Version { json } => {
            if json {
                let resp = output::CliResponse::success(serde_json::json!({"version": env!("CARGO_PKG_VERSION"), "name": "oxibrowser"}));
                resp.print_json();
                0
            } else {
                println!("oxibrowser {}", env!("CARGO_PKG_VERSION"));
                0
            }
        }
    };

    if exit_code != 0 {
        std::process::exit(exit_code);
    }
}

// ---------------------------------------------------------------------------
// Error output — human vs JSON
// ---------------------------------------------------------------------------

/// Print an error and return the exit code.
fn print_error(msg: &str, error_code: &str, json: bool) -> i32 {
    let code = match error_code {
        "INVALID_URL" | "INVALID_SELECTOR" | "INPUT_VALIDATION" | "PATH_TRAVERSAL" | "SSRF_BLOCKED" => 2,
        "TIMEOUT" => 3,
        "NETWORK_ERROR" | "HTTP_ERROR" => 4,
        _ => 1,
    };

    if json {
        let resp = output::CliResponse::error(msg, error_code);
        resp.print_json();
    } else {
        eprintln!("Error: {msg}");
    }
    code
}

// ---------------------------------------------------------------------------
// fetch
// ---------------------------------------------------------------------------

#[allow(clippy::too_many_arguments)]
async fn run_fetch(
    url: &str, format: &str, json: bool, max_bytes: Option<u64>,
    fields: Option<&str>, summary: bool, eval: Option<&str>,
    click: Option<&str>, fill: Option<&str>, press: Option<&str>,
    wait: Option<&str>, wait_timeout: u64, extract_sel: Option<&str>,
    all: bool, headers: bool, timeout: u64,
) -> i32 {
    let start = Instant::now();
    let json = use_json(json);

    // Validate
    if let Some(e) = validate_fetch_inputs(url, click, fill, wait, extract_sel, eval) {
        return print_error(&e.error.unwrap_or_default(), &e.error_code.unwrap_or_default(), json);
    }

    let needs_tab = click.is_some() || fill.is_some() || press.is_some()
        || wait.is_some() || eval.is_some();

    let config = oxibrowser_core::BrowserConfig::headless();
    let browser = match oxibrowser_core::Browser::new(config).await {
        Ok(b) => b,
        Err(e) => return print_error(&format!("browser init failed: {e}"), "RUNTIME_ERROR", json),
    };

    let result = if needs_tab {
        fetch_with_tab(
            start, &browser, url, format, json, max_bytes, fields, summary,
            eval, click, fill, press, wait, wait_timeout, extract_sel, all, headers, timeout,
        ).await
    } else {
        fetch_direct(
            start, &browser, url, format, json, max_bytes, fields, summary,
            extract_sel, all, headers,
        ).await
    };

    browser.close().await.ok();

    match result {
        Ok(()) => 0,
        Err(FetchError { msg, code }) => print_error(&msg, &code, json),
    }
}

struct FetchError {
    msg: String,
    code: String,
}

impl From<oxibrowser_core::error::CoreError> for FetchError {
    fn from(e: oxibrowser_core::error::CoreError) -> Self {
        FetchError {
            msg: format!("{e}"),
            code: output::core_error_code(&e).to_string(),
        }
    }
}

/// Direct fetch: no interaction needed.
#[allow(clippy::too_many_arguments)]
async fn fetch_direct(start: Instant, 
    browser: &oxibrowser_core::Browser,
    url: &str,
    format: &str,
    json: bool,
    max_bytes: Option<u64>,
    fields: Option<&str>,
    summary: bool,
    extract_sel: Option<&str>,
    all: bool,
    headers: bool,
) -> Result<(), FetchError> {
    let session = browser.new_page(url).await.map_err(FetchError::from)?;
    let guard = session.read().await;
    let page = guard.page().ok_or_else(|| FetchError {
        msg: "no page loaded".into(),
        code: "PAGE_NOT_LOADED".into(),
    })?;

    if headers {
        eprintln!("HTTP {}", page.status());
        eprintln!("Content-Type: {}", page.content_type());
    }

    // Summary — always JSON (structured metadata)
    if summary {
        let data = output::build_summary(page);
        if json {
            let resp = output::CliResponse::success_with_meta(data, None, start.elapsed().as_millis() as u64);
            resp.print_json();
        } else {
            // Human: print summary as key-value
            let obj = data.as_object().unwrap();
            if let Some(v) = obj.get("url").and_then(|v| v.as_str()) {
                eprintln!("URL: {v}");
            }
            if let Some(v) = obj.get("title").and_then(|v| v.as_str()) {
                eprintln!("Title: {v}");
            }
            eprintln!("Status: {}", obj.get("status").unwrap());
            if let Some(h) = obj.get("headings").and_then(|v| v.as_array()) {
                eprintln!("Headings: {}", h.len());
                for h in h {
                    if let Some(s) = h.as_str() { eprintln!("  - {s}"); }
                }
            }
            eprintln!("Links: {}", obj.get("links_count").unwrap());
            eprintln!("Forms: {}", obj.get("forms_count").unwrap());
            eprintln!("Images: {}", obj.get("images_count").unwrap());
            eprintln!("Text length: {}", obj.get("text_length").unwrap());
        }
        return Ok(());
    }

    // Extract — human gets text, agent gets JSON
    if let Some(sel) = extract_sel {
        let doc = page.root_frame().document();
        if all {
            let texts: Vec<String> = doc
                .query_selector_all(sel)
                .iter()
                .filter_map(|id| doc.text_content(*id))
                .map(|t| t.trim().to_string())
                .filter(|t| !t.is_empty())
                .collect();
            if json {
                let resp = output::CliResponse::success(serde_json::json!({
                    "selector": sel, "count": texts.len(), "items": texts
                }));
                resp.print_json();
            } else {
                for t in &texts { println!("{t}"); }
            }
        } else {
            let text = doc.query_text(sel).map(|t| t.trim().to_string()).unwrap_or_default();
            if json {
                let resp = output::CliResponse::success(serde_json::json!({
                    "selector": sel, "match": text
                }));
                resp.print_json();
            } else {
                println!("{text}");
            }
        }
        return Ok(());
    }

    // Full content
    let body = match format {
        "markdown" | "md" => page.to_markdown(),
        "text" => {
            // textContent has no line breaks (no CSS layout).
            // Use markdown → strip formatting for readable plain text.
            let md = page.to_markdown();
            // Strip markdown syntax: # headings, **bold**, [links](url), etc.
            let text = md
                .lines()
                .map(|line| {
                    let l = line.trim();
                    // Strip heading markers
                    let l = l.strip_prefix('#').map(|s| s.trim()).unwrap_or(l);
                    let l = l.strip_prefix('#').map(|s| s.trim()).unwrap_or(l);
                    // Strip bold/italic markers
                    let l = l.replace("**", "").replace("__", "");
                    let l = l.replace("* ", "");
                    // Convert [text](url) to just text
                    let l = regex_strip_link(&l);
                    l
                })
                .filter(|l| !l.is_empty())
                .collect::<Vec<_>>()
                .join("\n");
            text
        }
        _ => page.content().to_string(),
    };

    if json {
        let mut data = serde_json::json!({
            "url": page.url().to_string(),
            "title": page.title().unwrap_or("").to_string(),
            "status": page.status(),
            "content_type": page.content_type().to_string(),
        });
        let key = match format {
            "markdown" | "md" => "markdown",
            "text" => "text",
            _ => "html",
        };
        data.as_object_mut().unwrap().insert(key.into(), Value::String(body));
        if let Some(mb) = max_bytes {
            output::truncate_fields(&mut data, mb);
        }
        if let Some(f) = fields {
            output::filter_fields(&mut data, &output::parse_fields(f));
        }
        output::CliResponse::success_with_meta(data, None, start.elapsed().as_millis() as u64).print_json();
    } else {
        print!("{body}");
    }
    Ok(())
}

/// Tab-based fetch: for interaction and JS eval.
#[allow(clippy::too_many_arguments)]
async fn fetch_with_tab(start: Instant, 
    browser: &oxibrowser_core::Browser,
    url: &str,
    format: &str,
    json: bool,
    max_bytes: Option<u64>,
    fields: Option<&str>,
    summary: bool,
    eval: Option<&str>,
    click: Option<&str>,
    fill: Option<&str>,
    press: Option<&str>,
    wait: Option<&str>,
    wait_timeout: u64,
    extract_sel: Option<&str>,
    all: bool,
    headers: bool,
    timeout: u64,
) -> Result<(), FetchError> {
    let tab = browser.new_tab().await.map_err(FetchError::from)?;

    let nav_result = tokio::time::timeout(Duration::from_secs(timeout), tab.goto(url)).await;
    match nav_result {
        Ok(Ok(nav)) => {
            if headers {
                eprintln!("HTTP {}", nav.status);
                eprintln!("URL: {}", nav.url);
                eprintln!("Title: {}", nav.title);
            }
        }
        Ok(Err(e)) => return Err(FetchError::from(e)),
        Err(_) => return Err(FetchError {
            msg: format!("timed out after {timeout}s"),
            code: "TIMEOUT".into(),
        }),
    }

    // Interaction: wait → fill → click → press
    if let Some(sel) = wait {
        tab.wait_for(sel, wait_timeout).await.map_err(FetchError::from)?;
    }
    if let Some(spec) = fill {
        let (sel, val) = spec.split_once(':').ok_or_else(|| FetchError {
            msg: "--fill must be selector:value".into(),
            code: "INPUT_VALIDATION".into(),
        })?;
        tab.fill(sel, val).await.map_err(FetchError::from)?;
    }
    if let Some(sel) = click {
        tab.click(sel).await.map_err(FetchError::from)?;
    }
    if let Some(keys) = press {
        tab.press(keys).await.map_err(FetchError::from)?;
    }

    // Eval
    if let Some(expr) = eval {
        let value = tab.evaluate(expr).await.map_err(FetchError::from)?;
        if json {
            output::CliResponse::success(serde_json::json!({"value": value})).print_json();
        } else {
            match value {
                Value::String(s) => println!("{s}"),
                Value::Null => {}
                other => println!("{other}"),
            }
        }
        return Ok(());
    }

    // Summary
    if summary {
        let content = tab.content().await.map_err(FetchError::from)?;
        let data = serde_json::json!({
            "url": content.url, "title": content.title,
            "status": content.status, "text_length": content.markdown.len(),
        });
        if json {
            output::CliResponse::success_with_meta(data, None, start.elapsed().as_millis() as u64).print_json();
        } else {
            eprintln!("URL: {}", content.url);
            eprintln!("Title: {}", content.title);
            eprintln!("Status: {}", content.status);
            eprintln!("Text length: {}", content.markdown.len());
        }
        return Ok(());
    }

    // Extract
    if let Some(sel) = extract_sel {
        let matches = tab.query_all(sel).await.map_err(FetchError::from)?;
        if all {
            let items: Vec<String> = matches.into_iter()
                .map(|t| t.trim().to_string())
                .filter(|t| !t.is_empty())
                .collect();
            if json {
                output::CliResponse::success(serde_json::json!({
                    "selector": sel, "count": items.len(), "items": items
                })).print_json();
            } else {
                for t in &items { println!("{t}"); }
            }
        } else {
            let text = matches.first().map(|t| t.trim().to_string()).unwrap_or_default();
            if json {
                output::CliResponse::success(serde_json::json!({
                    "selector": sel, "match": text
                })).print_json();
            } else {
                println!("{text}");
            }
        }
        return Ok(());
    }

    // Full content
    let content = tab.content().await.map_err(FetchError::from)?;
    if json {
        let mut data = match format {
            "markdown" | "md" => serde_json::json!({
                "url": content.url, "title": content.title,
                "status": content.status, "markdown": content.markdown,
            }),
            "text" => {
                let body = content.markdown.split_whitespace().collect::<Vec<_>>().join(" ");
                serde_json::json!({
                    "url": content.url, "title": content.title,
                    "status": content.status, "text": body,
                })
            },
            _ => serde_json::json!({
                "url": content.url, "title": content.title,
                "status": content.status, "html": content.html,
            }),
        };
        if let Some(mb) = max_bytes {
            output::truncate_fields(&mut data, mb);
        }
        if let Some(f) = fields {
            output::filter_fields(&mut data, &output::parse_fields(f));
        }
        output::CliResponse::success_with_meta(data, None, start.elapsed().as_millis() as u64).print_json();
    } else {
        match format {
            "html" => print!("{}", content.html),
            "text" => {
                let body = content.markdown.lines()
                    .map(|l| l.trim())
                    .filter(|l| !l.is_empty())
                    .collect::<Vec<_>>()
                    .join("\n");
                println!("{body}");
            }
            _ => print!("{}", content.markdown),
        }
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// extract
// ---------------------------------------------------------------------------

#[allow(clippy::too_many_arguments)]
async fn run_extract(
    url: &str, selector: Option<&str>, all: bool, attrs: &str,
    links: bool, title: bool, text: bool, markdown: bool,
    max_bytes: Option<u64>, json: bool, timeout: u64,
) -> i32 {
    let json = use_json(json);
    let start = Instant::now();

    // Validate
    if let Err(e) = validate::validate_url(url) {
        return print_error(&e.to_string(), e.error_code(), json);
    }
    if let Some(sel) = selector {
        if let Err(e) = validate::validate_selector(sel) {
            return print_error(&e.to_string(), e.error_code(), json);
        }
    }

    let requested_attrs: Vec<&str> = output::parse_fields(attrs);

    let config = oxibrowser_core::BrowserConfig::headless();
    let browser = match oxibrowser_core::Browser::new(config).await {
        Ok(b) => b,
        Err(e) => return print_error(&format!("browser init failed: {e}"), "RUNTIME_ERROR", json),
    };

    let session_result =
        tokio::time::timeout(Duration::from_secs(timeout), browser.new_page(url)).await;

    let session = match session_result {
        Ok(Ok(s)) => s,
        Ok(Err(e)) => {
            browser.close().await.ok();
            return print_error(&format!("{e}"), output::core_error_code(&e), json);
        }
        Err(_) => {
            browser.close().await.ok();
            return print_error(&format!("timed out after {timeout}s"), "TIMEOUT", json);
        }
    };

    let guard = session.read().await;
    let page = match guard.page() {
        Some(p) => p,
        None => {
            browser.close().await.ok();
            return print_error("no page loaded", "PAGE_NOT_LOADED", json);
        }
    };

    let doc = page.root_frame().document();
    let mut json_map = serde_json::Map::new();

    if title {
        json_map.insert("title".into(), Value::String(page.title().unwrap_or("").to_string()));
    }
    if links {
        let hrefs: Vec<Value> = doc.query_selector_all("a[href]")
            .iter()
            .filter_map(|id| doc.get_node(*id).and_then(|n| n.href().map(|h| Value::String(h.to_string()))))
            .collect();
        json_map.insert("links".into(), Value::Array(hrefs));
    }
    if text {
        json_map.insert("text".into(), Value::String(doc.query_text("body").unwrap_or_default()));
    }
    if markdown {
        json_map.insert("markdown".into(), Value::String(page.to_markdown()));
    }

    if let Some(sel) = selector {
        let ids = doc.query_selector_all(sel);
        if all {
            let items: Vec<Value> = ids.iter().filter_map(|id| {
                let mut item = serde_json::Map::new();
                for &attr in &requested_attrs {
                    let val = if attr == "text" {
                        doc.text_content(*id).map(|t| t.trim().to_string()).unwrap_or_default()
                    } else {
                        doc.get_node(*id).and_then(|n| n.get_attribute(attr).map(|v| v.to_string())).unwrap_or_default()
                    };
                    item.insert(attr.into(), Value::String(val));
                }
                if !item.is_empty() { Some(Value::Object(item)) } else { None }
            }).collect();
            json_map.insert("selector".into(), Value::String(sel.into()));
            json_map.insert("count".into(), Value::Number(serde_json::Number::from(items.len())));
            json_map.insert("items".into(), Value::Array(items));
        } else {
            let mut item = serde_json::Map::new();
            if let Some(id) = ids.first() {
                for &attr in &requested_attrs {
                    let val = if attr == "text" {
                        doc.text_content(*id).map(|t| t.trim().to_string()).unwrap_or_default()
                    } else {
                        doc.get_node(*id).and_then(|n| n.get_attribute(attr).map(|v| v.to_string())).unwrap_or_default()
                    };
                    item.insert(attr.into(), Value::String(val));
                }
            }
            json_map.insert("selector".into(), Value::String(sel.into()));
            json_map.insert("match".into(), Value::Object(item));
        }
    }

    // Default: title + text
    if !title && !links && !text && !markdown && selector.is_none() {
        json_map.insert("title".into(), Value::String(page.title().unwrap_or("").to_string()));
        json_map.insert("text".into(), Value::String(doc.query_text("body").unwrap_or_default()));
    }

    drop(guard);
    browser.close().await.ok();

    let mut data = Value::Object(json_map);
    if let Some(mb) = max_bytes {
        output::truncate_fields(&mut data, mb);
    }

    if json {
        output::CliResponse::success_with_meta(data, None, start.elapsed().as_millis() as u64).print_json();
    } else {
        print_extract_human(&data);
    }
    0
}

/// Print extract data in human-friendly format.
fn print_extract_human(data: &Value) {
    let obj = match data.as_object() {
        Some(o) => o,
        None => { println!("{data}"); return; }
    };

    // Title
    if let Some(title) = obj.get("title").and_then(|v| v.as_str()) {
        if !title.is_empty() { println!("Title: {title}"); }
    }
    // Blank line after title for visual separation
    if obj.contains_key("title") && (obj.contains_key("text") || obj.contains_key("items") || obj.contains_key("links")) {
        println!();
    }
    // Links: one per line
    if let Some(links) = obj.get("links").and_then(|v| v.as_array()) {
        for link in links {
            if let Some(s) = link.as_str() { println!("{s}"); }
        }
    }
    // Selector items
    if let Some(items) = obj.get("items").and_then(|v| v.as_array()) {
        for item in items {
            if let Some(s) = item.as_str() {
                println!("{s}");
            } else {
                let vals: Vec<&str> = item.as_object()
                    .map(|o| o.values().filter_map(|v| v.as_str()).collect())
                    .unwrap_or_default();
                println!("{}", vals.join("\t"));
            }
        }
    }
    // Single match
    if let Some(m) = obj.get("match") {
        if let Some(s) = m.as_str() {
            println!("{s}");
        } else {
            let vals: Vec<&str> = m.as_object()
                .map(|o| o.values().filter_map(|v| v.as_str()).collect())
                .unwrap_or_default();
            println!("{}", vals.join("\t"));
        }
    }
    // Body text
    if let Some(text) = obj.get("text").and_then(|v| v.as_str()) {
        if !text.is_empty() {
            for line in text.lines() {
                println!("{line}");
            }
        }
    }
    // Markdown
    if let Some(md) = obj.get("markdown").and_then(|v| v.as_str()) {
        if !md.is_empty() { print!("{md}"); }
    }
}

// ---------------------------------------------------------------------------
// run (YAML script)
// ---------------------------------------------------------------------------

async fn run_script(script_path_or_yaml: &str, timeout: u64) -> i32 {
    let script_config = if std::path::Path::new(script_path_or_yaml).exists() {
        match std::fs::read_to_string(script_path_or_yaml) {
            Ok(content) => match oxibrowser_core::script::parse_script(&content) {
                Ok(cfg) => cfg,
                Err(e) => { eprintln!("Error: parse error: {e}"); return 1; }
            },
            Err(e) => { eprintln!("Error: cannot read script: {e}"); return 1; }
        }
    } else {
        match oxibrowser_core::script::parse_script(script_path_or_yaml) {
            Ok(cfg) => cfg,
            Err(e) => { eprintln!("Error: parse error: {e}"); return 1; }
        }
    };

    let mut browser_config = oxibrowser_core::BrowserConfig::headless();
    browser_config.enable_ssrf_filter = false;
    let browser = match oxibrowser_core::Browser::new(browser_config).await {
        Ok(b) => b,
        Err(e) => { eprintln!("Error: browser init failed: {e}"); return 1; }
    };

    let tab = match browser.new_tab().await {
        Ok(t) => t,
        Err(e) => { eprintln!("Error: tab creation failed: {e}"); return 1; }
    };

    let mut runner = oxibrowser_core::script::ScriptRunner::new(&tab);
    let script_result = match tokio::time::timeout(
        Duration::from_secs(timeout),
        runner.run_config(&script_config),
    ).await {
        Ok(Ok(r)) => r,
        Ok(Err(e)) => { browser.close().await.ok(); eprintln!("Error: {e}"); return 1; }
        Err(_) => { browser.close().await.ok(); eprintln!("Error: timed out after {timeout}s"); return 3; }
    };
    browser.close().await.ok();
    let elapsed = script_result.duration_ms;
    let data = serde_json::to_value(&script_result).unwrap_or_default();
    output::CliResponse::success_with_meta(data, None, elapsed).print_json();
    0
}

// ---------------------------------------------------------------------------
// session — see session/mod.rs
// ---------------------------------------------------------------------------

// ---------------------------------------------------------------------------
// serve (CDP server)
// ---------------------------------------------------------------------------

async fn run_serve(host: &str, port: u16, cookie_file: Option<&str>) -> i32 {
    let addr: SocketAddr = match format!("{host}:{port}").parse() {
        Ok(a) => a,
        Err(e) => { eprintln!("Error: invalid address: {e}"); return 2; }
    };

    info!(addr = %addr, "starting CDP server");

    let mut config = oxibrowser_core::BrowserConfig::headless();
    if let Some(path) = cookie_file {
        config.cookie_file = Some(std::path::PathBuf::from(path));
    }
    config.enable_ssrf_filter = false;

    let browser = match oxibrowser_core::Browser::new(config).await {
        Ok(b) => b,
        Err(e) => { eprintln!("Error: browser init failed: {e}"); return 1; }
    };
    let browser = Arc::new(browser);

    let server = Arc::new(oxibrowser_cdp::CdpServer::new(addr, browser.clone()));
    let bound_addr = match server.start().await {
        Ok(a) => a,
        Err(e) => { eprintln!("Error: server bind failed: {e}"); return 4; }
    };

    info!(addr = %bound_addr, "CDP server ready");
    println!("OxiBrowser CDP server listening on {bound_addr}");
    println!("  DevTools: http://{bound_addr}/json/version");
    println!("  WebSocket: ws://{bound_addr}/ws");

    tokio::signal::ctrl_c().await.ok();
    info!("shutting down");

    server.shutdown();
    browser.close().await.ok();
    0
}

// ---------------------------------------------------------------------------
// search
// ---------------------------------------------------------------------------

async fn run_search(
    query_parts: &[String],
    source: &str,
    engine: &str,
    repo: Option<&str>,
    token: Option<&str>,
    json: bool,
    max_results: u32,
    timeout: u64,
) -> i32 {
    let start = std::time::Instant::now();
    let json = crate::output::should_output_json(json);

    let query = query_parts.join(" ");
    let query = query.trim();
    if query.is_empty() {
        return print_error("empty search query", "INPUT_VALIDATION", json);
    }

    let result = search::dispatch(
        query, source, engine, repo, token,
        max_results as usize,
        timeout,
    ).await;

    match result {
        Ok(output) => {
            let elapsed = start.elapsed().as_millis() as u64;
            if json {
                let data = serde_json::to_value(&output).unwrap_or_default();
                let resp = output::CliResponse::success_with_search_meta(
                    data, elapsed, &output.source, &output.engine,
                );
                resp.print_json()
            } else {
                search::format_human(&output);
                0
            }
        }
        Err(e) => print_error(&e.to_string(), "SEARCH_ERROR", json),
    }
}

// ---------------------------------------------------------------------------
// describe
// ---------------------------------------------------------------------------

fn run_describe(command: Option<&str>, compact: bool) -> i32 {
    // describe is agent-only — always JSON
    let response = match command {
        Some(cmd) => describe::describe_command(cmd),
        None => describe::describe_all(compact),
    };
    response.print_json()
}

// ---------------------------------------------------------------------------
// Text formatting helpers
// ---------------------------------------------------------------------------

/// Strip markdown link syntax: [text](url) → text
fn regex_strip_link(s: &str) -> String {
    let bytes = s.as_bytes();
    let mut result = String::with_capacity(s.len());
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b'[' {
            // Find matching ](
            if let Some(close) = bytes[i..].iter().position(|&b| b == b']') {
                let close_idx = i + close;
                if close_idx + 1 < bytes.len() && bytes[close_idx + 1] == b'(' {
                    // Find closing )
                    if let Some(paren) = bytes[close_idx + 2..].iter().position(|&b| b == b')') {
                        // Extract text between [ and ]
                        let text = &s[i + 1..close_idx];
                        result.push_str(text);
                        i = close_idx + 2 + paren + 1;
                        continue;
                    }
                }
            }
        }
        result.push(bytes[i] as char);
        i += 1;
    }
    result
}

// ---------------------------------------------------------------------------
// Validation helper
// ---------------------------------------------------------------------------

fn validate_fetch_inputs(
    url: &str, click: Option<&str>, fill: Option<&str>,
    wait: Option<&str>, extract: Option<&str>, eval: Option<&str>,
) -> Option<output::CliResponse> {
    if let Err(e) = validate::validate_url(url) {
        return Some(output::CliResponse::from_validation(e));
    }
    if let Some(sel) = click {
        if let Err(e) = validate::validate_selector(sel) {
            return Some(output::CliResponse::from_validation(e));
        }
    }
    if let Some(spec) = fill {
        if !spec.contains(':') {
            return Some(output::CliResponse::error(
                "--fill must be in the format selector:value",
                "INPUT_VALIDATION",
            ));
        }
    }
    if let Some(sel) = wait {
        if let Err(e) = validate::validate_selector(sel) {
            return Some(output::CliResponse::from_validation(e));
        }
    }
    if let Some(sel) = extract {
        if let Err(e) = validate::validate_selector(sel) {
            return Some(output::CliResponse::from_validation(e));
        }
    }
    if let Some(expr) = eval {
        if let Err(e) = validate::validate_expression(expr) {
            return Some(output::CliResponse::from_validation(e));
        }
    }
    None
}