webfind 0.5.0

Self-hosted native search engine for AI agents — pure CLI, zero cost
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
use std::path::Path;
use std::sync::Arc;

use anyhow::{Context, Result};
use spider::website::Website;

use super::device_profile::{SessionManager, StickySessions};
use super::embedder::Embedder;
use super::fetcher::{Fetcher, RotateUserAgent};
use super::proxy_pool::{ProxyPool, ProxyProtocol};
use super::search_engine::{InMemorySearchEngine, SearchEngine};
use crate::schema::content::StructuredContent;
use crate::storage::cache_store::{CacheEntry, CacheStore, ReCrawlPolicy, SimHash};

/// Crawler: spider for discovery + Fetcher for extraction + Indexer for storage.
pub struct Crawler {
    seed_url: String,
    max_pages: u32,
    delay_ms: u32,
    respect_robots: bool,
    subdomains: bool,
    cache: Option<CacheStore>,
    recrawl_policy: ReCrawlPolicy,
    skip_cached: bool,
    proxy_list: Vec<String>,
    proxy_cidr: Option<String>,
    proxy_protocol: ProxyProtocol,
    rotate_ua: bool,
    sticky_sessions: bool,
    rps: u32,
    dynamic_fallback: bool,
    dynamic_wait_ms: u64,
    embedder: Option<Arc<dyn Embedder>>,
}

/// Statistics from a crawl run.
#[derive(Debug, Default)]
pub struct CrawlStats {
    pub pages_discovered: usize,
    pub pages_fetched: usize,
    pub pages_skipped_cache: usize,
    pub pages_skipped_dup: usize,
    pub pages_indexed: usize,
    pub fetch_errors: usize,
    pub index_errors: usize,
}

impl Crawler {
    pub fn new(seed_url: &str, max_pages: u32, delay_ms: u32) -> Self {
        Self {
            seed_url: seed_url.to_string(),
            max_pages,
            delay_ms,
            respect_robots: true,
            subdomains: false,
            cache: None,
            recrawl_policy: ReCrawlPolicy::default(),
            skip_cached: false,
            proxy_list: vec![],
            proxy_cidr: None,
            proxy_protocol: ProxyProtocol::Http,
            rotate_ua: false,
            sticky_sessions: true,
            rps: 1,
            dynamic_fallback: false,
            dynamic_wait_ms: 2000,
            embedder: None,
        }
    }

    pub fn with_robots_txt(mut self, respect: bool) -> Self {
        self.respect_robots = respect;
        self
    }

    pub fn with_subdomains(mut self, allow: bool) -> Self {
        self.subdomains = allow;
        self
    }

    /// Configure proxy pool and User-Agent rotation.
    ///
    /// * `proxy_list` — explicit proxy URLs (http://, https://, socks5://).
    /// * `proxy_cidr` — generate random proxy endpoints inside this CIDR (e.g. "10.0.0.0/24").
    /// * `proxy_protocol` — protocol for generated proxies.
    /// * `rotate_ua` — generate random User-Agent per request.
    /// * `sticky_sessions` — keep same UA+proxy for a given domain.
    /// * `rps` — per-domain requests per second.
    pub fn with_human_mode(
        mut self,
        proxy_list: Vec<String>,
        proxy_cidr: Option<String>,
        proxy_protocol: ProxyProtocol,
        rotate_ua: bool,
        sticky_sessions: bool,
        rps: u32,
    ) -> Self {
        self.proxy_list = proxy_list;
        self.proxy_cidr = proxy_cidr;
        self.proxy_protocol = proxy_protocol;
        self.rotate_ua = rotate_ua;
        self.sticky_sessions = sticky_sessions;
        self.rps = rps.max(1);
        self
    }

    /// Enable Chromium-based dynamic rendering fallback.
    pub fn with_dynamic_fallback(mut self, wait_ms: u64) -> Self {
        self.dynamic_fallback = true;
        self.dynamic_wait_ms = wait_ms.max(500);
        self
    }

    /// Attach a dense-vector embedder to enable hybrid indexing.
    pub fn with_embedder(mut self, embedder: Option<Arc<dyn Embedder>>) -> Self {
        self.embedder = embedder;
        self
    }

    /// Enable URL cache with a re-crawl policy.
    ///
    /// `skip_cached=true` means URLs that are still fresh in the cache will not
    /// be extracted or indexed again (saves CPU/indexing work; note that the
    /// spider may still fetch the page depending on its own scheduler).
    pub fn with_cache(
        mut self,
        base: impl AsRef<Path>,
        policy: ReCrawlPolicy,
        skip_cached: bool,
    ) -> Result<Self> {
        self.cache = Some(CacheStore::open(base)?.with_policy(policy));
        self.recrawl_policy = policy;
        self.skip_cached = skip_cached;
        Ok(self)
    }

    /// Run the full crawl → fetch → index pipeline.
    pub async fn crawl_and_index(&self, _index_dir: &Path) -> Result<CrawlStats> {
        let mut stats = CrawlStats::default();

        // 1. Build spider
        let mut website = Website::new(&self.seed_url);
        website
            .with_limit(self.max_pages)
            .with_delay(self.delay_ms as u64)
            .with_respect_robots_txt(self.respect_robots)
            .with_subdomains(self.subdomains)
            .with_tld(false);

        // Subscribe to receive crawled pages
        let mut rx = website.subscribe(100);

        // 2. Start crawling in background
        let crawl_handle = tokio::spawn(async move {
            website.crawl().await;
            website.unsubscribe();
        });

        // 3. Build fetcher + indexer
        let proxy_pool = if let Some(ref cidr) = self.proxy_cidr {
            Some(ProxyPool::from_cidr(
                cidr,
                8080,
                self.proxy_protocol,
                10,
            )?)
        } else if !self.proxy_list.is_empty() {
            Some(ProxyPool::from_list(&self.proxy_list)?)
        } else {
            None
        };
        let session_manager = if self.rotate_ua || proxy_pool.is_some() {
            Some(SessionManager::new(if self.sticky_sessions {
                StickySessions::Sticky
            } else {
                StickySessions::PerRequest
            }))
        } else {
            None
        };
        let fetcher = if proxy_pool.is_some() || self.rotate_ua {
            Fetcher::new_human(
                proxy_pool,
                session_manager,
                if self.rotate_ua {
                    RotateUserAgent::Rotate
                } else {
                    RotateUserAgent::Fixed
                },
                self.rps,
            )
            .context("failed to create human-like fetcher")?
            .with_dynamic_fallback(self.dynamic_wait_ms)
        } else if self.dynamic_fallback {
            Fetcher::new()
                .context("failed to create fetcher")?
                .with_dynamic_fallback(self.dynamic_wait_ms)
        } else {
            Fetcher::new().context("failed to create fetcher")?
        };
        let indexer = self
            .embedder
            .as_ref()
            .map(|e| InMemorySearchEngine::with_embedder(e.clone()))
            .unwrap_or_default();

        // 4. Process pages as they come in
        let batch_size = 50;
        let mut batch: Vec<StructuredContent> = Vec::new();

        while let Ok(page) = rx.recv().await {
            stats.pages_discovered += 1;

            let url = page.get_url().to_string();
            let html = page.get_content().to_string();
            let status = page.status_code.as_u16();

            // Skip non-200 pages
            if status != 200 || html.is_empty() {
                stats.fetch_errors += 1;
                continue;
            }

            // Cache-aware extraction: skip recently-cached URLs when requested.
            if self.skip_cached
                && let Some(ref cache) = self.cache
                    && !cache.should_fetch(&url) {
                        stats.pages_skipped_cache += 1;
                        continue;
                    }

            // Extract structured content from the HTML we already have
            match fetcher.extract_from_html(
                &html,
                &url,
                &url,
                status,
                url.starts_with("https://"),
                0, // already fetched by spider
                None,
            ) {
                Ok(content) => {
                    stats.pages_fetched += 1;

                    // Skip pages with no meaningful content
                    if !content.is_valid_content {
                        continue;
                    }

                    // Near-duplicate detection against previously cached entries.
                    if let Some(ref cache) = self.cache
                        && let Some(existing) = cache.get(&url)
                            && let (Some(old), Some(new)) = (
                                existing.simhash,
                                Some(SimHash::compute(&content.content_text)),
                            )
                                && SimHash::hamming_distance(old, new) <= SimHash::NEAR_DUP_THRESHOLD {
                                    stats.pages_skipped_dup += 1;
                                    continue;
                                }

                    batch.push(content);

                    // Index in batches
                    if batch.len() >= batch_size {
                        let batch_count = batch.len();
                        match indexer.index_batch(&batch).await {
                            Ok(indexed) => {
                                stats.pages_indexed += indexed as usize;
                                self.persist_cache_entries(&batch);
                                eprintln!(
                                    "  indexed {} pages (total: {})",
                                    indexed, stats.pages_indexed
                                );
                            }
                            Err(e) => {
                                stats.index_errors += batch_count;
                                eprintln!("  index error: {}", e);
                            }
                        }
                        batch.clear();
                    }
                }
                Err(e) => {
                    stats.fetch_errors += 1;
                    eprintln!("  fetch error [{}]: {}", url, e);
                }
            }
        }

        // Index remaining batch
        if !batch.is_empty() {
            let batch_count = batch.len();
            match indexer.index_batch(&batch).await {
                Ok(indexed) => {
                    stats.pages_indexed += indexed as usize;
                    self.persist_cache_entries(&batch);
                }
                Err(e) => {
                    stats.index_errors += batch_count;
                    eprintln!("  final batch index error: {}", e);
                }
            }
        }

        // Wait for crawl to finish
        let _ = crawl_handle.await;

        Ok(stats)
    }

    /// Simple crawl-and-fetch (no indexing) — returns extracted content.
    pub async fn crawl_only(&self) -> Result<Vec<StructuredContent>> {
        let mut website = Website::new(&self.seed_url);
        website
            .with_limit(self.max_pages)
            .with_delay(self.delay_ms as u64)
            .with_respect_robots_txt(self.respect_robots)
            .with_subdomains(self.subdomains)
            .with_tld(false);

        let mut rx = website.subscribe(100);

        let crawl_handle = tokio::spawn(async move {
            website.crawl().await;
            website.unsubscribe();
        });

        let proxy_pool = if let Some(ref cidr) = self.proxy_cidr {
            Some(ProxyPool::from_cidr(
                cidr,
                8080,
                self.proxy_protocol,
                10,
            )?)
        } else if !self.proxy_list.is_empty() {
            Some(ProxyPool::from_list(&self.proxy_list)?)
        } else {
            None
        };
        let session_manager = if self.rotate_ua || proxy_pool.is_some() {
            Some(SessionManager::new(if self.sticky_sessions {
                StickySessions::Sticky
            } else {
                StickySessions::PerRequest
            }))
        } else {
            None
        };
        let fetcher = if proxy_pool.is_some() || self.rotate_ua {
            Fetcher::new_human(
                proxy_pool,
                session_manager,
                if self.rotate_ua {
                    RotateUserAgent::Rotate
                } else {
                    RotateUserAgent::Fixed
                },
                self.rps,
            )
            .context("failed to create human-like fetcher")?
            .with_dynamic_fallback(self.dynamic_wait_ms)
        } else if self.dynamic_fallback {
            Fetcher::new()
                .context("failed to create fetcher")?
                .with_dynamic_fallback(self.dynamic_wait_ms)
        } else {
            Fetcher::new().context("failed to create fetcher")?
        };
        let mut results = vec![];

        while let Ok(page) = rx.recv().await {
            let url = page.get_url().to_string();
            let html = page.get_content().to_string();
            let status = page.status_code.as_u16();

            if status != 200 || html.is_empty() {
                continue;
            }

            if let Some(ref cache) = self.cache
                && self.skip_cached && !cache.should_fetch(&url) {
                    continue;
                }

            if let Ok(content) = fetcher.extract_from_html(
                &html,
                &url,
                &url,
                status,
                url.starts_with("https://"),
                0,
                None,
            )
                && content.is_valid_content {
                    results.push(content);
                }
        }

        let _ = crawl_handle.await;
        Ok(results)
    }

    fn persist_cache_entries(&self, batch: &[StructuredContent]) {
        if let Some(ref cache) = self.cache {
            for content in batch {
                let entry = CacheEntry {
                    url: content.url.clone(),
                    content_hash: CacheEntry::hash_content(&content.content_text),
                    first_seen: content.fetched_at,
                    last_fetched: content.fetched_at,
                    fetch_count: 1,
                    last_status: content.status_code,
                    title: Some(content.title.clone()).filter(|t| !t.is_empty()),
                    simhash: Some(SimHash::compute(&content.content_text)),
                };
                if let Err(e) = cache.insert(entry) {
                    eprintln!("  cache write error: {}", e);
                }
            }
        }
    }
}