crw-cli 0.18.3

crw — Unified CLI for web scraping, crawling, search, and serving
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
//! Scrape subcommand — fetch a single URL and extract content.

use crate::teardown::CmdError;
use clap::{Args, ValueEnum};
use crw_core::config::{RendererConfig, RendererMode, StealthConfig};
use crw_core::types::{OutputFormat, ScrapeRequest};
use crw_crawl::single::scrape_url;
use crw_renderer::FallbackRenderer;
use std::collections::HashMap;
use std::sync::Arc;

#[derive(Clone, ValueEnum)]
pub enum Format {
    Markdown,
    Json,
    Html,
    Rawhtml,
    Text,
    Links,
}

#[derive(Args)]
pub struct ScrapeArgs {
    /// URL to scrape (http or https)
    pub url: String,

    /// Output format
    #[arg(short, long, value_enum, default_value = "markdown")]
    pub format: Format,

    /// Write output to file instead of stdout
    #[arg(short, long, value_name = "FILE")]
    pub output: Option<String>,

    /// Disable main content extraction (return full page content)
    #[arg(long)]
    pub raw: bool,

    /// Enable JavaScript rendering (auto-detects LightPanda/Chrome, or use CRW_CDP_URL)
    #[arg(long)]
    pub js: bool,

    /// Extract only elements matching this CSS selector
    #[arg(long, value_name = "SELECTOR")]
    pub css: Option<String>,

    /// Extract only elements matching this XPath expression
    #[arg(long, value_name = "EXPR")]
    pub xpath: Option<String>,

    /// HTTP, HTTPS, or SOCKS5 proxy URL (e.g. http://user:pass@host:port or socks5://user:pass@host:1080)
    #[arg(long, value_name = "URL")]
    pub proxy: Option<String>,

    /// Enable stealth mode (rotate user agents, inject browser headers)
    #[arg(long)]
    pub stealth: bool,

    /// Generate an AI summary of the page using the configured LLM.
    #[arg(long, conflicts_with = "extract")]
    pub summary: bool,

    /// Style/format hint for --summary (e.g. "in 3 bullet points", "as a haiku").
    #[arg(long, value_name = "TEXT", requires = "summary")]
    pub prompt: Option<String>,

    /// Extract structured data using a JSON Schema.
    /// Accepts inline JSON or @path/to/schema.json.
    #[arg(long, value_name = "SCHEMA")]
    pub extract: Option<String>,

    /// Override LLM provider for this request (anthropic, openai, deepseek, azure, openrouter).
    #[arg(long, value_name = "NAME")]
    pub llm_provider: Option<String>,

    /// Override LLM API key for this request.
    #[arg(long, value_name = "KEY")]
    pub llm_key: Option<String>,

    /// Override LLM model for this request.
    #[arg(long, value_name = "MODEL")]
    pub llm_model: Option<String>,

    /// Override LLM base URL (for OpenAI-compatible or Azure endpoints).
    #[arg(long, value_name = "URL")]
    pub llm_base_url: Option<String>,
}

pub async fn run(mut args: ScrapeArgs) -> Result<(), CmdError> {
    // Local document short-circuit: when the positional arg is an existing file
    // on disk (not a URL), parse it directly. Only PDF is supported.
    if std::path::Path::new(&args.url).is_file() {
        return run_local_file(&args).await;
    }

    // Auto-prepend https:// if no scheme is provided
    if !args.url.contains("://") {
        args.url = format!("https://{}", args.url);
    }

    // First-run nudge for plain scrapes only. AI modes already prompt
    // interactively when there's no config, so we'd be doubling up.
    if !args.summary && args.extract.is_none() {
        maybe_show_first_run_hint();
    }

    let stealth_config = StealthConfig {
        enabled: args.stealth,
        inject_headers: args.stealth,
        ..Default::default()
    };

    // Load app config (config.toml) so we can pick up the user's LLM setup from `crw setup`.
    let app_config = crw_core::config::AppConfig::load().unwrap_or_default();
    let mut cli_extraction_cfg = app_config.extraction.clone();
    let env_cdp_url = std::env::var("CRW_CDP_URL").ok();

    // --extract: inline JSON or `@path/to/schema.json`.
    let extract_schema: Option<serde_json::Value> = match args.extract.as_deref() {
        Some(s) if s.starts_with('@') => {
            let path = &s[1..];
            match std::fs::read_to_string(path) {
                Ok(body) => match serde_json::from_str(&body) {
                    Ok(v) => Some(v),
                    Err(e) => {
                        eprintln!("error: invalid JSON in {path}: {e}");
                        return Err(CmdError::code_only(1));
                    }
                },
                Err(e) => {
                    eprintln!("error: failed to read {path}: {e}");
                    return Err(CmdError::code_only(1));
                }
            }
        }
        Some(s) => match serde_json::from_str(s) {
            Ok(v) => Some(v),
            Err(e) => {
                eprintln!(
                    "error: --extract is not valid JSON: {e}\n\
                     hint: use @path/to/schema.json for files"
                );
                return Err(CmdError::code_only(1));
            }
        },
        None => None,
    };

    let want_summary = args.summary;
    let want_extract = extract_schema.is_some();

    // Resolve effective LlmConfig: config-first, CLI overrides per-field.
    if want_summary || want_extract {
        let merged = match cli_extraction_cfg.llm.clone() {
            Some(mut cfg) => {
                if let Some(p) = args.llm_provider.clone() {
                    cfg.provider = p;
                }
                if let Some(k) = args.llm_key.clone() {
                    cfg.api_key = k;
                }
                if let Some(m) = args.llm_model.clone() {
                    cfg.model = m;
                }
                if args.llm_base_url.is_some() {
                    cfg.base_url = args.llm_base_url.clone();
                }
                Some(cfg)
            }
            None => {
                // No config — need at minimum provider + key + model on the CLI.
                match (
                    args.llm_provider.clone(),
                    args.llm_key.clone(),
                    args.llm_model.clone(),
                ) {
                    (Some(provider), Some(api_key), Some(model)) => {
                        Some(crw_core::config::LlmConfig {
                            provider,
                            api_key,
                            model,
                            base_url: args.llm_base_url.clone(),
                            ..Default::default()
                        })
                    }
                    _ => None,
                }
            }
        };
        let merged = match merged {
            Some(cfg) => Some(cfg),
            None => match run_inline_llm_setup().await {
                Ok(Some(cfg)) => Some(cfg),
                Ok(None) => {
                    eprintln!("Cancelled. --summary/--extract requires an LLM.");
                    return Err(CmdError::code_only(1));
                }
                Err(e) => {
                    eprintln!("error: LLM setup failed: {e}");
                    eprintln!(
                        "hint: run `crw setup` to configure manually, \
                         or pass --llm-provider/--llm-key/--llm-model."
                    );
                    return Err(CmdError::code_only(1));
                }
            },
        };
        cli_extraction_cfg.llm = merged;
    }

    // Request all formats we might need for the output.
    // When --summary/--extract is set, AI output formats are requested and `--format`
    // is ignored; we still include Markdown so phase 1 thinness detection works.
    let request_formats = if want_summary || want_extract {
        let mut v = vec![OutputFormat::Markdown];
        if want_summary {
            v.push(OutputFormat::Summary);
        }
        if want_extract {
            v.push(OutputFormat::Json);
        }
        v
    } else {
        match args.format {
            Format::Markdown => vec![OutputFormat::Markdown],
            Format::Json => vec![
                OutputFormat::Markdown,
                OutputFormat::Html,
                OutputFormat::Links,
            ],
            Format::Html => vec![OutputFormat::Html],
            Format::Rawhtml => vec![OutputFormat::RawHtml],
            Format::Text => vec![OutputFormat::PlainText],
            Format::Links => vec![OutputFormat::Links],
        }
    };

    // Two-phase fetch when `--js` is *not* explicitly set: try HTTP-only first
    // (no browser spawn cost), then escalate to a JS-capable renderer only if
    // the HTTP body extracted to a thin/empty document. This keeps plain-HTML
    // pages (example.com, news articles, blogs) fast while still automatically
    // rendering JS-heavy SPAs (React/Vue/etc.) that return an empty shell.
    //
    // When `--js` is set the user is explicitly opting into JS rendering, so
    // we skip phase 1 and spawn browsers up front.
    let force_js = args.js;

    let mut data: Option<crw_core::types::ScrapeData> = None;
    let mut keep_alive_guards: Vec<crw_renderer::browser::ManagedBrowser> = Vec::new();

    if !force_js {
        // Phase 1: HTTP-only. No browser spawn → no spawn cost on plain pages.
        let http_cfg = RendererConfig {
            mode: RendererMode::None,
            http_timeout_ms: Some(8_000),
            ..Default::default()
        };

        let req = build_request(
            args.url.clone(),
            request_formats.clone(),
            !args.raw,
            None, // render_js = None → auto, but with no JS renderer it stays HTTP
            args.css.clone(),
            args.xpath.clone(),
            args.proxy.clone(),
            args.stealth,
            args.prompt.clone(),
            extract_schema.clone(),
        );

        let http_renderer = match FallbackRenderer::new(
            &http_cfg,
            "crw/0.7.0",
            args.proxy.as_deref(),
            &stealth_config,
        ) {
            Ok(r) => Arc::new(r),
            Err(e) => {
                eprintln!("error: failed to build renderer: {e}");
                return Err(CmdError::code_only(1));
            }
        };

        let http_deadline = crw_core::Deadline::from_request_ms(8_000);
        match scrape_url(
            &req,
            &http_renderer,
            cli_extraction_cfg.llm.as_ref(),
            &cli_extraction_cfg,
            "crw/0.7.0",
            args.stealth,
            None,
            http_deadline,
        )
        .await
        {
            Ok(d) => {
                // Only auto-escalate for the "I just want the page" case
                // (default markdown format, no selectors). Filtered output
                // (--css/--xpath) or specific non-markdown formats are an
                // explicit user choice — measuring "thinness" of a 16-char
                // h1 extraction or a links-only response would always trip
                // the threshold and trigger pointless JS spawns.
                let can_escalate = args.css.is_none()
                    && args.xpath.is_none()
                    && !want_summary
                    && !want_extract
                    && matches!(args.format, Format::Markdown | Format::Json);
                let markdown_len = d.markdown.as_deref().map(str::len).unwrap_or(0);
                let html_text_len = d
                    .plain_text
                    .as_deref()
                    .map(str::len)
                    .unwrap_or_else(|| d.html.as_deref().map(str::len).unwrap_or(0));
                // Same threshold the renderer uses (`is_thin_markdown` < 100).
                // Also catch the "empty SPA shell" case where markdown is empty
                // but the raw HTML is also tiny.
                let is_thin = can_escalate && markdown_len < 100 && html_text_len < 400;
                if is_thin {
                    eprintln!(
                        "info: HTTP returned thin content ({markdown_len} chars markdown), \
                         escalating to JS renderer..."
                    );
                } else {
                    data = Some(d);
                }
            }
            Err(e) => {
                // HTTP-only failure → fall through to JS escalation below.
                eprintln!("info: HTTP fetch failed ({e}), trying JS renderer...");
            }
        }
    }

    // Phase 2 (or sole phase when --js): spawn browsers + run the full
    // HTTP → LightPanda → Chrome fallback chain.
    if data.is_none() {
        // CLI-tuned per-tier timeouts. Server defaults (30s each) assume a
        // long Tower envelope; for interactive CLI we want faster failover so
        // a hanging LightPanda still leaves enough budget for Chrome.
        let mut renderer_config = RendererConfig {
            http_timeout_ms: Some(8_000),
            lightpanda_timeout_ms: Some(12_000),
            chrome_timeout_ms: Some(25_000),
            ..Default::default()
        };

        if let Some(ws_url) = env_cdp_url {
            // Explicit CDP URL — honor it, skip spawn.
            renderer_config.lightpanda = Some(crw_core::config::CdpEndpoint { ws_url });
        } else {
            let browsers = crw_renderer::browser::spawn_all_headless().await;
            if browsers.is_empty() && force_js {
                eprintln!(
                    "warning: --js requested but no browser found. \
                     Install LightPanda or Chrome for JS rendering. \
                     Falling back to HTTP."
                );
            }
            for (guard, ws_url, kind) in browsers {
                match kind {
                    crw_renderer::browser::RendererKind::LightPanda => {
                        renderer_config.lightpanda = Some(crw_core::config::CdpEndpoint { ws_url });
                    }
                    crw_renderer::browser::RendererKind::Chrome => {
                        renderer_config.chrome = Some(crw_core::config::CdpEndpoint { ws_url });
                    }
                }
                keep_alive_guards.push(guard);
            }
            if keep_alive_guards.is_empty()
                && renderer_config.lightpanda.is_none()
                && renderer_config.chrome.is_none()
            {
                renderer_config.mode = RendererMode::None;
            }
        }

        let renderer = match FallbackRenderer::new(
            &renderer_config,
            "crw/0.7.0",
            args.proxy.as_deref(),
            &stealth_config,
        ) {
            Ok(r) => Arc::new(r),
            Err(e) => {
                eprintln!("error: failed to build renderer: {e}");
                return Err(CmdError::code_only(1));
            }
        };

        let req = build_request(
            args.url,
            request_formats,
            !args.raw,
            if force_js { Some(true) } else { None },
            args.css,
            args.xpath,
            args.proxy.clone(),
            args.stealth,
            args.prompt,
            extract_schema,
        );

        // Size the request deadline so the configured renderer ladder
        // (http + lightpanda + chrome + per-tier CDP overhead) can run
        // uncrushed. Mirrors the server's `auto_extend_deadline_for_ladder`.
        let cli_app_config = crw_core::config::AppConfig {
            renderer: renderer_config.clone(),
            request: crw_core::config::RequestConfig {
                deadline_ms_default: 8_000,
                auto_extend_deadline_for_ladder: true,
            },
            ..Default::default()
        };
        let deadline_ms = cli_app_config.effective_deadline_ms(req.deadline_ms, req.wait_for);
        let cli_deadline = crw_core::Deadline::from_request_ms(deadline_ms);

        match scrape_url(
            &req,
            &renderer,
            cli_extraction_cfg.llm.as_ref(),
            &cli_extraction_cfg,
            "crw/0.7.0",
            args.stealth,
            None,
            cli_deadline,
        )
        .await
        {
            Ok(d) => data = Some(d),
            Err(e) => {
                eprintln!("error: {e}");
                return Err(CmdError::code_only(1));
            }
        }
    }

    let data = data.expect("data must be populated by phase 1 or phase 2");
    // Drop guards only after extraction is done so the browser stays alive
    // through the whole fetch + parse pipeline.
    drop(keep_alive_guards);

    // AI output paths short-circuit `--format`. The backend populates
    // `data.summary` / `data.json` when those OutputFormats are requested.
    if want_summary {
        let summary = data.summary.clone().unwrap_or_default();
        match args.output {
            Some(path) => {
                if let Err(e) = std::fs::write(&path, &summary) {
                    eprintln!("error: failed to write to {path}: {e}");
                    return Err(CmdError::code_only(1));
                }
            }
            None => print!("{summary}"),
        }
        return Ok(());
    }
    if want_extract {
        let json = data
            .json
            .as_ref()
            .map(|v| serde_json::to_string_pretty(v).unwrap_or_default())
            .unwrap_or_default();
        match args.output {
            Some(path) => {
                if let Err(e) = std::fs::write(&path, &json) {
                    eprintln!("error: failed to write to {path}: {e}");
                    return Err(CmdError::code_only(1));
                }
            }
            None => println!("{json}"),
        }
        return Ok(());
    }

    let content = match args.format {
        Format::Markdown => data.markdown.unwrap_or_default(),
        Format::Json => match serde_json::to_string_pretty(&data) {
            Ok(s) => s,
            Err(e) => {
                eprintln!("error: failed to serialize JSON: {e}");
                return Err(CmdError::code_only(1));
            }
        },
        Format::Html => data.html.unwrap_or_default(),
        Format::Rawhtml => data.raw_html.unwrap_or_default(),
        Format::Text => data.plain_text.unwrap_or_default(),
        Format::Links => data.links.unwrap_or_default().join("\n"),
    };

    match args.output {
        Some(path) => {
            if let Err(e) = std::fs::write(&path, &content) {
                eprintln!("error: failed to write to {path}: {e}");
                return Err(CmdError::code_only(1));
            }
        }
        None => print!("{content}"),
    }
    Ok(())
}

/// Print a one-line nudge to run `crw setup` the first time someone scrapes
/// without a config.toml. Idempotent across runs via a dotfile sentinel so
/// long-time CLI-only users (who never want setup) don't get nagged.
fn maybe_show_first_run_hint() {
    let Some(cfg_path) = crw_core::config::user_config_path() else {
        return;
    };
    if cfg_path.exists() {
        return;
    }
    let sentinel = cfg_path.with_file_name(".first-run-hint-shown");
    if sentinel.exists() {
        return;
    }
    eprintln!();
    eprintln!(
        "  Tip: run `crw setup` to enable AI features (--summary, --extract) and SearXNG search."
    );
    eprintln!();
    if let Some(parent) = sentinel.parent() {
        let _ = std::fs::create_dir_all(parent);
    }
    let _ = std::fs::File::create(&sentinel);
}

/// Triggered when the user invokes `--summary` / `--extract` but no LLM is
/// configured. Asks once whether to set one up now; on yes, runs the same
/// interactive flow `crw setup` uses, writes the result to config.toml so it
/// sticks across runs, and returns the resolved `LlmConfig` to continue the
/// in-flight request.
async fn run_inline_llm_setup()
-> Result<Option<crw_core::config::LlmConfig>, crate::commands::setup::ui::SetupError> {
    use crate::commands::setup::config_file::{
        ExtractionSection, LlmSection, UserConfig, write_user_config,
    };
    use crate::commands::setup::{llm, ui};
    use dialoguer::{Confirm, theme::ColorfulTheme};

    ui::init_color(false);
    println!();
    println!(
        "  --summary / --extract requires an LLM, but none is configured in \
         ~/.config/crw/config.toml."
    );
    let confirm = Confirm::with_theme(&ColorfulTheme::default())
        .with_prompt("Configure one now?")
        .default(true)
        .interact()
        .map_err(ui::handle_dialoguer_error)?;
    if !confirm {
        return Ok(None);
    }

    let result = match llm::run().await? {
        Some(r) => r,
        None => return Ok(None), // user picked "Skip" in the provider list
    };

    // Persist to config.toml so the next `crw … --summary` just works.
    let user_cfg = UserConfig {
        client: None,
        search: None,
        extraction: Some(ExtractionSection {
            llm: Some(LlmSection {
                provider: Some(result.provider.config_value().to_string()),
                api_key: Some(result.api_key.clone()),
                model: Some(result.model.clone()),
                base_url: result.base_url.clone(),
                azure_api_version: result.azure_api_version.clone(),
            }),
        }),
    };
    match write_user_config(user_cfg) {
        Ok(path) => {
            ui::print_success(&format!("Saved to {}", path.display()));
        }
        Err(e) => {
            // Don't bail — we can still run this one request with what we have.
            eprintln!("warning: failed to save config.toml: {e}");
        }
    }

    Ok(Some(crw_core::config::LlmConfig {
        provider: result.provider.config_value().to_string(),
        api_key: result.api_key,
        model: result.model,
        base_url: result.base_url,
        azure_api_version: result.azure_api_version,
        ..Default::default()
    }))
}

/// Parse a local document file (PDF) directly, bypassing the network fetch.
/// Supports markdown/json/text/links output plus `--summary`/`--extract` when a
/// server-side LLM is configured (via `crw setup`).
async fn run_local_file(args: &ScrapeArgs) -> Result<(), CmdError> {
    let path = args.url.clone();
    let bytes = match std::fs::read(&path) {
        Ok(b) => b,
        Err(e) => {
            eprintln!("error: failed to read {path}: {e}");
            return Err(CmdError::code_only(1));
        }
    };

    let is_pdf = path.to_ascii_lowercase().ends_with(".pdf")
        || bytes
            .strip_prefix(&[0xEF, 0xBB, 0xBF])
            .unwrap_or(&bytes)
            .trim_ascii_start()
            .starts_with(b"%PDF-");
    if !is_pdf {
        eprintln!("error: local file parsing currently supports only PDF files (got {path})");
        return Err(CmdError::code_only(1));
    }

    let want_summary = args.summary;
    let want_extract = args.extract.is_some();

    // --extract schema (inline JSON or @path), mirroring the URL path.
    let extract_schema: Option<serde_json::Value> = match args.extract.as_deref() {
        Some(s) if s.starts_with('@') => match std::fs::read_to_string(&s[1..]) {
            Ok(body) => serde_json::from_str(&body).ok(),
            Err(e) => {
                eprintln!("error: failed to read {}: {e}", &s[1..]);
                return Err(CmdError::code_only(1));
            }
        },
        Some(s) => match serde_json::from_str(s) {
            Ok(v) => Some(v),
            Err(e) => {
                eprintln!("error: --extract is not valid JSON: {e}");
                return Err(CmdError::code_only(1));
            }
        },
        None => None,
    };

    let app_config = crw_core::config::AppConfig::load().unwrap_or_default();

    let formats = if want_summary || want_extract {
        let mut v = vec![OutputFormat::Markdown];
        if want_summary {
            v.push(OutputFormat::Summary);
        }
        if want_extract {
            v.push(OutputFormat::Json);
        }
        v
    } else {
        match args.format {
            Format::Markdown => vec![OutputFormat::Markdown],
            Format::Json => vec![OutputFormat::Markdown],
            Format::Html | Format::Rawhtml => vec![OutputFormat::Markdown],
            Format::Text => vec![OutputFormat::PlainText],
            Format::Links => vec![OutputFormat::Links],
        }
    };

    let req = ScrapeRequest {
        formats,
        json_schema: extract_schema,
        summary_prompt: args.prompt.clone(),
        ..Default::default()
    };

    let source = crw_crawl::pdf::PdfSource {
        source_url: format!("file://{path}"),
        status_code: 200,
        elapsed_ms: 0,
        source_filename: std::path::Path::new(&path)
            .file_name()
            .map(|s| s.to_string_lossy().into_owned()),
    };

    let mut data = match crw_crawl::pdf::convert_pdf_bytes(bytes, &req, source).await {
        Ok(d) => d,
        Err(e) => {
            eprintln!("error: PDF conversion failed: {e}");
            return Err(CmdError::code_only(1));
        }
    };

    if (want_summary || want_extract)
        && let Err(e) =
            crw_crawl::pdf::apply_llm_formats(&mut data, &req, app_config.extraction.llm.as_ref())
                .await
    {
        eprintln!("error: {e}");
        eprintln!("hint: run `crw setup` to configure an LLM for --summary/--extract.");
        return Err(CmdError::code_only(1));
    }

    for w in &data.warnings {
        eprintln!("warning: {w}");
    }

    let content = if want_summary {
        data.summary.unwrap_or_default()
    } else if want_extract {
        data.json
            .as_ref()
            .map(|v| serde_json::to_string_pretty(v).unwrap_or_default())
            .unwrap_or_default()
    } else {
        match args.format {
            Format::Markdown => data.markdown.unwrap_or_default(),
            Format::Json => serde_json::to_string_pretty(&data).unwrap_or_default(),
            Format::Html | Format::Rawhtml => {
                eprintln!("warning: HTML output is unavailable for PDF; returning markdown");
                data.markdown.unwrap_or_default()
            }
            Format::Text => data.plain_text.unwrap_or_default(),
            Format::Links => data.links.unwrap_or_default().join("\n"),
        }
    };

    match &args.output {
        Some(p) => {
            if let Err(e) = std::fs::write(p, &content) {
                eprintln!("error: failed to write to {p}: {e}");
                return Err(CmdError::code_only(1));
            }
        }
        None => println!("{content}"),
    }
    Ok(())
}

#[allow(clippy::too_many_arguments)]
fn build_request(
    url: String,
    formats: Vec<OutputFormat>,
    only_main_content: bool,
    render_js: Option<bool>,
    css: Option<String>,
    xpath: Option<String>,
    proxy: Option<String>,
    stealth: bool,
    summary_prompt: Option<String>,
    extract_schema: Option<serde_json::Value>,
) -> ScrapeRequest {
    let extract = extract_schema
        .clone()
        .map(|s| crw_core::types::ExtractOptions { schema: Some(s) });
    ScrapeRequest {
        url,
        formats,
        only_main_content,
        render_js,
        wait_for: None,
        include_tags: vec![],
        exclude_tags: vec![],
        json_schema: extract_schema,
        headers: HashMap::new(),
        css_selector: css,
        xpath,
        chunk_strategy: None,
        query: None,
        filter_mode: None,
        top_k: None,
        proxy,
        proxy_list: Vec::new(),
        proxy_rotation: None,
        country: None,
        stealth: if stealth { Some(true) } else { None },
        actions: None,
        extract,
        llm_api_key: None,
        llm_provider: None,
        llm_model: None,
        base_url: None,
        summary_prompt,
        max_content_chars: None,
        renderer: None,
        deadline_ms: None,
        debug: None,
        change_tracking: None,
        goal: None,
        judge_enabled: None,
        parsers: None,
        screenshot_full_page: false,
    }
}