agent-search 0.4.6

Unified multi-provider search CLI for AI agents — 12 providers, 14 modes, one binary
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
mod cache;
mod classify;
mod cli;
mod config;
mod context;
mod engine;
mod errors;
mod logging;
mod output;
mod providers;
mod types;

use clap::Parser;
use cli::{Cli, Commands, ConfigAction};
use config::{config_check, config_set, config_show, load_config};
use context::AppContext;
use output::OutputFormat;
use std::sync::Arc;
use tokio::net::lookup_host;

#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

#[tokio::main]
async fn main() {
    // 1. Pre-emptive DNS resolution (starts immediately in background)
    // Priming the OS DNS cache for the most likely API domains.
    tokio::spawn(async {
        let domains = [
            "api.parallel.ai:443",
            "api.search.brave.com:443",
            "google.serper.dev:443",
            "api.exa.ai:443",
            "api.jina.ai:443",
            "api.tavily.com:443",
            "api.perplexity.ai:443",
        ];
        for domain in domains {
            let _ = lookup_host(domain).await;
        }
    });

    // 2. Start loading config in parallel with CLI parsing
    let config_handle = tokio::task::spawn_blocking(load_config);

    // 3. CLI Parsing (fast, but we want to overlap it with I/O)
    let cli = Cli::parse();
    let format = OutputFormat::detect(cli.json);

    // 4. Wait for config
    let config = match config_handle.await.unwrap() {
        Ok(c) => c,
        Err(e) => {
            eprintln!("Config error: {e}");
            std::process::exit(1);
        }
    };

    let ctx = Arc::new(AppContext::new(config));

    // 5. Pre-emptive TLS Handshake (Unconventional: Race to the wire)
    // If this is a search command, start Warming up TLS sessions for the "Big 3".
    let is_search = cli.command.is_none() || matches!(cli.command, Some(Commands::Search(_)));
    if is_search && !cli.last {
        let ctx_c = ctx.clone();
        tokio::spawn(async move {
            let urls = [
                "https://api.search.brave.com/res/v1/web/search",
                "https://google.serper.dev/search",
                "https://api.exa.ai/search",
            ];
            for url in urls {
                // Send a HEAD request to trigger TLS handshake + connection pooling.
                // We don't care about the result, just want the connection established.
                let _ = ctx_c.client.head(url).send().await;
            }
        });
    }

    let exit_code = match run(cli, &format, ctx).await {
        Ok(code) => code,
        Err(e) => {
            match format {
                OutputFormat::Json => output::json::render_error(&e),
                OutputFormat::Table => eprintln!("Error: {e}"),
            }
            e.exit_code()
        }
    };

    std::process::exit(exit_code);
}

async fn run(cli: Cli, format: &OutputFormat, ctx: Arc<AppContext>) -> Result<i32, errors::SearchError> {
    // Handle bare `search "query"` without subcommand
    let command = if let Some(cmd) = cli.command {
        cmd
    } else if cli.last {
        Commands::Search(cli::SearchArgs {
            query: String::new(),
            mode: types::Mode::Auto,
            count: None,
            providers: None,
            domain: None,
            exclude_domain: None,
            freshness: None,
        })
    } else if !cli.query_words.is_empty() {
        let query = cli.query_words.join(" ");
        Commands::Search(cli::SearchArgs {
            query,
            mode: types::Mode::Auto,
            count: None,
            providers: None,
            domain: None,
            exclude_domain: None,
            freshness: None,
        })
    } else {
        use clap::CommandFactory;
        Cli::command().print_help().ok();
        println!();
        return Ok(0);
    };

    match command {
        Commands::Search(mut args) => {
            // --x flag: force X/Twitter search via xAI Grok
            if cli.x_only {
                args.mode = types::Mode::Social;
                args.providers = Some(vec!["xai".to_string()]);
            }

            if cli.last {
                if let Some(cached) = cache::load_last() {
                    match *format {
                        OutputFormat::Json => output::json::render(&cached),
                        OutputFormat::Table => output::table::render(&cached),
                    }
                    return Ok(0);
                } else {
                    let err = errors::SearchError::Config("No cached results found. Run a search first.".into());
                    match *format {
                        OutputFormat::Json => output::json::render_error(&err),
                        OutputFormat::Table => eprintln!("No cached results found. Run a search first."),
                    }
                    return Ok(1);
                }
            }

            // Validate provider names early
            if let Some(ref providers) = args.providers {
                const KNOWN: &[&str] = &[
                    "parallel", "brave", "serper", "exa", "jina", "firecrawl", "tavily",
                    "serpapi", "perplexity", "browserless", "stealth", "xai",
                ];
                for p in providers {
                    if !KNOWN.iter().any(|k| k.eq_ignore_ascii_case(p)) {
                        let err = errors::SearchError::Config(format!(
                            "Unknown provider '{}'. Valid: {}", p, KNOWN.join(", ")
                        ));
                        match *format {
                            OutputFormat::Json => output::json::render_error(&err),
                            OutputFormat::Table => eprintln!("Error: {err}"),
                        }
                        return Ok(err.exit_code());
                    }
                }
            }

            let count = args.count.unwrap_or(ctx.config.settings.count);
            let opts = types::SearchOpts {
                include_domains: args.domain.unwrap_or_default(),
                exclude_domains: args.exclude_domain.unwrap_or_default(),
                freshness: args.freshness,
            };

            // Check query cache (5min TTL) — skip if filters or provider selection is active
            let mode_str = args.mode.to_string();
            if args.providers.is_none()
                && opts.include_domains.is_empty()
                && opts.exclude_domains.is_empty()
                && opts.freshness.is_none()
            {
                if let Some(cached) = cache::load_query(&args.query, &mode_str) {
                    match *format {
                        OutputFormat::Json => output::json::render(&cached),
                        OutputFormat::Table => output::table::render(&cached),
                    }
                    return Ok(0);
                }
            }

            // Show spinner for human output
            let spinner = if matches!(*format, OutputFormat::Table) && !cli.quiet {
                let sp = indicatif::ProgressBar::new_spinner();
                sp.set_style(
                    indicatif::ProgressStyle::default_spinner()
                        .tick_strings(&["   ", ".  ", ".. ", "...", " ..", "  .", "   "])
                        .template("  {spinner:.cyan} searching {msg}")
                        .unwrap(),
                );
                let provider_hint = args
                    .providers
                    .as_ref()
                    .map(|p| format!(" via {}", p.join(", ")))
                    .unwrap_or_default();
                sp.set_message(format!(
                    "\"{}\" [{}{}]",
                    args.query,
                    args.mode,
                    provider_hint
                ));
                sp.enable_steady_tick(std::time::Duration::from_millis(100));
                Some(sp)
            } else {
                None
            };

            let response =
                engine::run(ctx, &args.query, args.mode, count, &args.providers, &opts).await;

            if let Some(sp) = spinner {
                sp.finish_and_clear();
            }

            let response = response?;

            cache::save_last(&response);
            cache::save_query(&args.query, &mode_str, &response);
            logging::log_search(&response);

            match *format {
                OutputFormat::Json => output::json::render(&response),
                OutputFormat::Table => output::table::render(&response),
            }

            // Exit non-zero when all providers failed (semantic exit codes)
            if response.status == "all_providers_failed" {
                Ok(1)
            } else {
                Ok(0)
            }
        }

        Commands::Config { action } => {
            match action {
                ConfigAction::Show => {
                    if matches!(*format, OutputFormat::Json) {
                        let configured: Vec<&str> = [
                            ("brave", !ctx.config.keys.brave.is_empty()),
                            ("serper", !ctx.config.keys.serper.is_empty()),
                            ("exa", !ctx.config.keys.exa.is_empty()),
                            ("jina", !ctx.config.keys.jina.is_empty()),
                            ("firecrawl", !ctx.config.keys.firecrawl.is_empty()),
                            ("tavily", !ctx.config.keys.tavily.is_empty()),
                            ("serpapi", !ctx.config.keys.serpapi.is_empty()),
                            ("perplexity", !ctx.config.keys.perplexity.is_empty()),
                            ("browserless", !ctx.config.keys.browserless.is_empty()),
                            ("xai", !ctx.config.keys.xai.is_empty()),
                        ].iter().filter(|(_, v)| *v).map(|(k, _)| *k).collect();
                        let info = serde_json::json!({
                            "version": "1",
                            "status": "success",
                            "config_path": config::config_path().to_string_lossy(),
                            "settings": {
                                "timeout": ctx.config.settings.timeout,
                                "count": ctx.config.settings.count,
                            },
                            "providers_configured": configured,
                        });
                        output::json::render_value(&info);
                    } else {
                        config_show(&ctx.config);
                    }
                }
                ConfigAction::Set { key, value } => {
                    config_set(&key, &value)?;
                    if matches!(*format, OutputFormat::Json) {
                        output::json::render_value(&serde_json::json!({
                            "version": "1",
                            "status": "success",
                            "key": key,
                            "message": format!("Set {key}"),
                        }));
                    } else {
                        eprintln!("Set {key}");
                    }
                }
                ConfigAction::Check => {
                    if matches!(*format, OutputFormat::Json) {
                        let all_providers = providers::build_providers(&ctx);
                        let all: Vec<(&str, bool)> = all_providers
                            .iter()
                            .map(|p| (p.name(), p.is_configured()))
                            .collect();
                        let configured: Vec<&str> = all.iter().filter(|(_, v)| *v).map(|(k, _)| *k).collect();
                        let unconfigured: Vec<&str> = all.iter().filter(|(_, v)| !v).map(|(k, _)| *k).collect();
                        let total = all.len();
                        output::json::render_value(&serde_json::json!({
                            "version": "1",
                            "status": "success",
                            "configured_count": configured.len(),
                            "total_count": total,
                            "configured": configured,
                            "unconfigured": unconfigured,
                        }));
                    } else {
                        config_check(&ctx.config);
                    }
                }
            }
            Ok(0)
        }

        Commands::AgentInfo => {
            let all = providers::build_providers(&ctx);
            let providers_info: Vec<serde_json::Value> = all
                .iter()
                .map(|p| {
                    serde_json::json!({
                        "name": p.name(),
                        "configured": p.is_configured(),
                        "capabilities": p.capabilities(),
                        "env_keys": p.env_keys(),
                    })
                })
                .collect();

            let info = serde_json::json!({
                "name": "search",
                "version": env!("CARGO_PKG_VERSION"),
                "commands": ["search", "config show", "config set", "config check", "agent-info", "providers", "update"],
                "modes": ["auto", "general", "news", "academic", "people", "deep", "extract", "similar", "scrape", "scholar", "patents", "images", "places", "social"],
                "providers": providers_info,
                "global_flags": ["--json", "--quiet", "--last", "--x"],
                "env_prefix": "SEARCH_",
                "config_path": config::config_path().to_string_lossy(),
                "output_formats": ["json", "table"],
                "auto_json_when_piped": true,
            });

            output::json::render_value(&info);
            Ok(0)
        }

        Commands::Providers => {
            let all = providers::build_providers(&ctx);
            let provider_info: Vec<(String, bool, Vec<String>)> = all
                .iter()
                .map(|p| {
                    (
                        p.name().to_string(),
                        p.is_configured(),
                        p.capabilities().iter().map(|s| s.to_string()).collect(),
                    )
                })
                .collect();

            match *format {
                OutputFormat::Json => {
                    let json: Vec<serde_json::Value> = provider_info
                        .iter()
                        .map(|(name, configured, caps)| {
                            serde_json::json!({
                                "name": name,
                                "configured": configured,
                                "capabilities": caps,
                            })
                        })
                        .collect();
                    output::json::render_value(&serde_json::json!({
                        "version": "1",
                        "status": "success",
                        "providers": json,
                    }));
                }
                OutputFormat::Table => {
                    output::table::render_providers(&provider_info);
                }
            }
            Ok(0)
        }

        Commands::Update { check } => {
            let current = env!("CARGO_PKG_VERSION");
            if check {
                match self_update::backends::github::Update::configure()
                    .repo_owner("199-biotechnologies")
                    .repo_name("search-cli")
                    .bin_name("search")
                    .current_version(current)
                    .build()
                {
                    Ok(updater) => match updater.get_latest_release() {
                        Ok(release) => {
                            let up_to_date = release.version == current;
                            if matches!(*format, OutputFormat::Json) {
                                output::json::render_value(&serde_json::json!({
                                    "version": "1",
                                    "status": "success",
                                    "current_version": current,
                                    "latest_version": release.version,
                                    "update_available": !up_to_date,
                                }));
                            } else if !up_to_date {
                                eprintln!("Current version: {current}");
                                eprintln!("New version available: {}", release.version);
                                eprintln!("Run `search update` to install");
                            } else {
                                eprintln!("Already up to date (v{current})");
                            }
                        }
                        Err(e) => {
                            if matches!(*format, OutputFormat::Json) {
                                let err = errors::SearchError::Api {
                                    provider: "github",
                                    code: "update_check_failed",
                                    message: e.to_string(),
                                };
                                output::json::render_error(&err);
                            } else {
                                eprintln!("Could not check for updates: {e}");
                            }
                            return Ok(1);
                        }
                    },
                    Err(e) => {
                        if matches!(*format, OutputFormat::Json) {
                            let err = errors::SearchError::Config(format!("Update check failed: {e}"));
                            output::json::render_error(&err);
                        } else {
                            eprintln!("Update check failed: {e}");
                        }
                        return Ok(1);
                    }
                }
            } else {
                eprintln!("Updating search from v{current}...");
                match self_update::backends::github::Update::configure()
                    .repo_owner("199-biotechnologies")
                    .repo_name("search-cli")
                    .bin_name("search")
                    .current_version(current)
                    .build()
                    .and_then(|u| u.update())
                {
                    Ok(status) => {
                        if matches!(*format, OutputFormat::Json) {
                            output::json::render_value(&serde_json::json!({
                                "version": "1",
                                "status": "success",
                                "updated": status.updated(),
                                "version_installed": status.version(),
                            }));
                        } else if status.updated() {
                            eprintln!("Updated to v{}", status.version());
                        } else {
                            eprintln!("Already up to date (v{current})");
                        }
                    }
                    Err(e) => {
                        if matches!(*format, OutputFormat::Json) {
                            let err = errors::SearchError::Config(format!("Update failed: {e}"));
                            output::json::render_error(&err);
                        } else {
                            eprintln!("Update failed: {e}");
                            eprintln!("You can update manually: cargo install agent-search");
                        }
                        return Ok(1);
                    }
                }
            }
            Ok(0)
        }
    }
}