web-search 0.5.0

A multi-provider web search aggregator with reranking 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
//! Web Search CLI and Server
//!
//! Usage:
//!   web-search <query> [options]       Search the web
//!   web-search --serve [--port <port>] Start as API server

use std::collections::HashMap;
use std::net::SocketAddr;

use axum::{
    extract::{Path, Query, State},
    http::StatusCode,
    response::Json,
    routing::get,
    Router,
};
use clap::{Parser, Subcommand};
use serde::{Deserialize, Serialize};
use tower_http::cors::CorsLayer;
use tracing_subscriber::EnvFilter;

use web_search::{
    get_provider_ids, get_registry, is_known_category, MergeOptions, MergeStrategy, RegistryEntry,
    SearchOptions, WebSearchEngine, CATEGORIES,
};

#[derive(Parser)]
#[command(name = "web-search")]
#[command(about = "Multi-provider web search aggregator")]
#[command(version)]
struct Cli {
    #[command(subcommand)]
    command: Option<Commands>,

    /// Search query (when not using subcommands)
    #[arg(trailing_var_arg = true)]
    query: Vec<String>,

    /// Providers to use (comma-separated)
    #[arg(long, value_delimiter = ',')]
    providers: Option<Vec<String>>,

    /// List every registered provider with its metadata and exit
    #[arg(long)]
    list_providers: bool,

    /// Maximum results per provider
    #[arg(short, long, default_value = "10")]
    limit: usize,

    /// Merge strategy (rrf, weighted, interleave)
    #[arg(long, default_value = "rrf")]
    strategy: String,

    /// Output format (text, json, urls)
    #[arg(short, long, default_value = "text")]
    format: String,

    /// Language code
    #[arg(long)]
    language: Option<String>,

    /// Region code
    #[arg(long)]
    region: Option<String>,

    /// Enable safe search
    #[arg(long)]
    safe: bool,

    /// Verbose output
    #[arg(short = 'v', long)]
    verbose: bool,
}

#[derive(Subcommand)]
enum Commands {
    /// Start as HTTP API server
    Serve {
        /// Port to listen on
        #[arg(short, long, default_value = "3000")]
        port: u16,
    },
}

#[derive(Clone)]
struct AppState {
    engine: std::sync::Arc<WebSearchEngine>,
}

#[derive(Debug, Deserialize)]
struct SearchQuery {
    q: Option<String>,
    query: Option<String>,
    providers: Option<String>,
    limit: Option<usize>,
    strategy: Option<String>,
    language: Option<String>,
    region: Option<String>,
    #[serde(rename = "safeSearch")]
    safe_search: Option<bool>,
    safe: Option<bool>,
}

#[derive(Debug, Serialize)]
struct SearchResponse {
    query: String,
    count: usize,
    options: SearchResponseOptions,
    results: Vec<web_search::providers::SearchResult>,
}

#[derive(Debug, Serialize)]
struct SearchResponseOptions {
    providers: Vec<String>,
    strategy: String,
    limit: usize,
}

#[derive(Debug, Serialize)]
struct ErrorResponse {
    error: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    message: Option<String>,
}

#[derive(Debug, Serialize)]
struct HealthResponse {
    status: String,
    providers: HashMap<String, web_search::search::ProviderStatus>,
}

async fn health_handler(State(state): State<AppState>) -> Json<HealthResponse> {
    Json(HealthResponse {
        status: "healthy".to_string(),
        providers: state.engine.get_provider_status().await,
    })
}

#[derive(Debug, Deserialize)]
struct CategoryQuery {
    category: Option<String>,
}

#[derive(Debug, Serialize)]
struct ProvidersResponse {
    categories: Vec<String>,
    count: usize,
    providers: HashMap<String, web_search::search::ProviderStatus>,
    registry: Vec<RegistryEntry>,
}

async fn providers_handler(
    State(state): State<AppState>,
    Query(params): Query<CategoryQuery>,
) -> Result<Json<ProvidersResponse>, (StatusCode, Json<ErrorResponse>)> {
    let category = params.category.filter(|c| !c.is_empty());
    if let Some(ref c) = category {
        if !is_known_category(c) {
            return Err((
                StatusCode::BAD_REQUEST,
                Json(ErrorResponse {
                    error: format!("Unknown category: {c}"),
                    message: None,
                }),
            ));
        }
    }

    let status = state.engine.get_provider_status().await;
    let registry: Vec<RegistryEntry> = get_registry()
        .into_iter()
        .filter(|e| category.as_deref().is_none_or(|c| e.category == c))
        .collect();

    let providers = match &category {
        Some(c) => status
            .into_iter()
            .filter(|(_, s)| s.category.as_deref() == Some(c.as_str()))
            .collect(),
        None => status,
    };

    Ok(Json(ProvidersResponse {
        categories: CATEGORIES.iter().map(|s| s.to_string()).collect(),
        count: registry.len(),
        providers,
        registry,
    }))
}

#[derive(Debug, Serialize)]
struct CategoriesResponse {
    categories: HashMap<String, Vec<String>>,
}

async fn categories_handler() -> Json<CategoriesResponse> {
    let mut categories = HashMap::new();
    for category in CATEGORIES {
        categories.insert(category.to_string(), get_provider_ids(Some(category)));
    }
    Json(CategoriesResponse { categories })
}

async fn search_handler(
    State(state): State<AppState>,
    Query(params): Query<SearchQuery>,
) -> Result<Json<SearchResponse>, (StatusCode, Json<ErrorResponse>)> {
    let query = params.q.or(params.query).ok_or_else(|| {
        (
            StatusCode::BAD_REQUEST,
            Json(ErrorResponse {
                error: "Missing required parameter: q or query".to_string(),
                message: None,
            }),
        )
    })?;

    let providers = params
        .providers
        .map(|p| p.split(',').map(|s| s.trim().to_string()).collect());
    let limit = params.limit.unwrap_or(10);
    let strategy = match params.strategy.as_deref() {
        Some("weighted") => MergeStrategy::Weighted,
        Some("interleave") => MergeStrategy::Interleave,
        _ => MergeStrategy::Rrf,
    };
    let safe_search = params.safe_search.or(params.safe);

    let search_options = SearchOptions {
        limit: Some(limit),
        language: params.language,
        region: params.region,
        safe_search,
    };

    let merge_options = MergeOptions {
        strategy,
        weights: HashMap::new(),
        rrf_k: None,
        remove_duplicates: true,
    };

    let results = state
        .engine
        .search_with_options(
            &query,
            search_options,
            providers.clone(),
            Some(merge_options),
        )
        .await
        .map_err(|e| {
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(ErrorResponse {
                    error: "Search failed".to_string(),
                    message: Some(e.to_string()),
                }),
            )
        })?;

    let strategy_str = match strategy {
        MergeStrategy::Rrf => "rrf",
        MergeStrategy::Weighted => "weighted",
        MergeStrategy::Interleave => "interleave",
    };

    Ok(Json(SearchResponse {
        query,
        count: results.len(),
        options: SearchResponseOptions {
            providers: providers.unwrap_or_else(|| state.engine.get_available_providers()),
            strategy: strategy_str.to_string(),
            limit,
        },
        results,
    }))
}

async fn search_provider_handler(
    State(state): State<AppState>,
    Path(provider): Path<String>,
    Query(params): Query<SearchQuery>,
) -> Result<Json<serde_json::Value>, (StatusCode, Json<ErrorResponse>)> {
    let query = params.q.or(params.query).ok_or_else(|| {
        (
            StatusCode::BAD_REQUEST,
            Json(ErrorResponse {
                error: "Missing required parameter: q or query".to_string(),
                message: None,
            }),
        )
    })?;

    let search_options = SearchOptions {
        limit: params.limit,
        language: params.language,
        region: params.region,
        safe_search: params.safe_search.or(params.safe),
    };

    let results = state
        .engine
        .search_single(&query, &provider, search_options)
        .await
        .map_err(|e| {
            let status = if e.to_string().contains("Unknown provider") {
                StatusCode::BAD_REQUEST
            } else {
                StatusCode::INTERNAL_SERVER_ERROR
            };
            (
                status,
                Json(ErrorResponse {
                    error: e.to_string(),
                    message: None,
                }),
            )
        })?;

    Ok(Json(serde_json::json!({
        "query": query,
        "provider": provider,
        "count": results.len(),
        "results": results,
    })))
}

async fn start_server(port: u16) -> Result<(), Box<dyn std::error::Error>> {
    let state = AppState {
        engine: std::sync::Arc::new(WebSearchEngine::new()),
    };

    let app = Router::new()
        .route("/health", get(health_handler))
        .route("/providers", get(providers_handler))
        .route("/categories", get(categories_handler))
        .route("/search", get(search_handler))
        .route("/search/{provider}", get(search_provider_handler))
        .layer(CorsLayer::permissive())
        .with_state(state);

    let addr = SocketAddr::from(([0, 0, 0, 0], port));
    println!("Web Search API listening on http://localhost:{}", port);
    println!();
    println!("Available endpoints:");
    println!("  GET  /search?q=<query>        - Search all providers");
    println!("  GET  /search/:provider?q=<query> - Search single provider");
    println!("  GET  /providers               - List available providers");
    println!("  GET  /categories              - List provider categories");
    println!("  GET  /health                  - Health check");
    println!();
    println!("Press Ctrl+C to stop the server");

    let listener = tokio::net::TcpListener::bind(addr).await?;
    axum::serve(listener, app).await?;

    Ok(())
}

async fn perform_search(cli: &Cli) -> Result<(), Box<dyn std::error::Error>> {
    let query = cli.query.join(" ");
    if query.is_empty() {
        eprintln!("Error: Missing search query");
        eprintln!("Run with --help for usage information");
        std::process::exit(1);
    }

    let engine = WebSearchEngine::new();

    let strategy = match cli.strategy.as_str() {
        "weighted" => MergeStrategy::Weighted,
        "interleave" => MergeStrategy::Interleave,
        _ => MergeStrategy::Rrf,
    };

    let search_options = SearchOptions {
        limit: Some(cli.limit),
        language: cli.language.clone(),
        region: cli.region.clone(),
        safe_search: if cli.safe { Some(true) } else { None },
    };

    let merge_options = MergeOptions {
        strategy,
        weights: HashMap::new(),
        rrf_k: None,
        remove_duplicates: true,
    };

    if cli.verbose {
        eprintln!("Searching for: \"{}\"", query);
        eprintln!(
            "Providers: {}",
            cli.providers
                .as_ref()
                .map(|p| p.join(", "))
                .unwrap_or_else(|| "all".to_string())
        );
        eprintln!("Strategy: {}", cli.strategy);
        eprintln!();
    }

    let results = engine
        .search_with_options(
            &query,
            search_options,
            cli.providers.clone(),
            Some(merge_options),
        )
        .await?;

    match cli.format.as_str() {
        "json" => {
            let response = serde_json::json!({
                "query": query,
                "count": results.len(),
                "results": results,
            });
            println!("{}", serde_json::to_string_pretty(&response)?);
        }
        "urls" => {
            for result in &results {
                println!("{}", result.url);
            }
        }
        _ => {
            if results.is_empty() {
                println!("No results found.");
                return Ok(());
            }

            println!("Found {} results for \"{}\":\n", results.len(), query);

            for result in &results {
                println!("{}. {}", result.rank, result.title);
                println!("   {}", result.url);
                if !result.snippet.is_empty() {
                    let snippet = if result.snippet.len() > 150 {
                        format!("{}...", &result.snippet[..150])
                    } else {
                        result.snippet.clone()
                    };
                    println!("   {}", snippet);
                }
                let sources = result
                    .sources
                    .as_ref()
                    .map(|s| s.join(", "))
                    .unwrap_or_else(|| result.source.clone());
                println!("   [{}]", sources);
                println!();
            }
        }
    }

    Ok(())
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    tracing_subscriber::fmt()
        .with_env_filter(EnvFilter::from_default_env())
        .init();

    let cli = Cli::parse();

    if cli.list_providers {
        // Write the rendered listing through a single fallible write so a
        // downstream reader that closes early (e.g. `web-search --list-providers
        // | head`) results in a clean exit instead of a panic. Rust ignores
        // SIGPIPE by default and surfaces the closed pipe as an EPIPE error;
        // `println!` would turn that into "failed printing to stdout: Broken
        // pipe" and abort with exit code 101. Treating BrokenPipe as success
        // keeps piped usage (and the release smoke test) green.
        return match write_stdout(&render_providers()) {
            Ok(()) => Ok(()),
            Err(error) if error.kind() == std::io::ErrorKind::BrokenPipe => Ok(()),
            Err(error) => Err(error.into()),
        };
    }

    match &cli.command {
        Some(Commands::Serve { port }) => {
            start_server(*port).await?;
        }
        None => {
            perform_search(&cli).await?;
        }
    }

    Ok(())
}

/// Render the full provider registry grouped by category into a single string.
///
/// Building the listing as a string (rather than emitting it with `println!`
/// line by line) lets the caller perform one fallible write to stdout and treat
/// a closed pipe (`| head`) as a clean exit instead of a panic.
fn render_providers() -> String {
    use std::fmt::Write as _;
    let registry = get_registry();
    let mut out = String::new();
    let _ = writeln!(out, "Registered providers ({} total):\n", registry.len());
    for category in CATEGORIES {
        let entries: Vec<&RegistryEntry> =
            registry.iter().filter(|e| e.category == category).collect();
        let _ = writeln!(out, "{} ({}):", category, entries.len());
        for e in entries {
            let default = if e.default_for_category {
                " [default]"
            } else {
                ""
            };
            let cors = if e.cors_readable { ", cors" } else { "" };
            let _ = writeln!(
                out,
                "  {:<16} {} ({}{}){}",
                e.id, e.label, e.access, cors, default
            );
        }
        let _ = writeln!(out);
    }
    out
}

/// Write text to stdout in one shot, propagating the I/O error (including
/// `BrokenPipe`) to the caller instead of panicking the way `println!` does.
fn write_stdout(text: &str) -> std::io::Result<()> {
    use std::io::Write as _;
    let stdout = std::io::stdout();
    let mut handle = stdout.lock();
    handle.write_all(text.as_bytes())?;
    handle.flush()
}