vectorless 0.1.26

Hierarchical, reasoning-native document intelligence engine
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
// Copyright (c) 2026 vectorless developers
// SPDX-License-Identifier: Apache-2.0

//! Context building for retrieval results.
//!
//! This module provides utilities for building context strings
//! from retrieval results for LLM consumption.
//!
//! # Features
//!
//! - Multiple pruning strategies (token-based, relevance-based, diversity)
//! - Configurable token estimation (fast or accurate)
//! - Async support for large documents
//!
//! # Example
//!
//! ```rust,ignore
//! // Synchronous
//! let context = ContextBuilder::new()
//!     .with_max_tokens(4000)
//!     .with_pruning_strategy(PruningStrategy::Hybrid { min_relevance: 0.5 })
//!     .build(&results);
//!
//! // Asynchronous (for large documents)
//! let context = ContextBuilder::new()
//!     .with_max_tokens(4000)
//!     .build_async(&results).await?;
//! ```

use super::types::RetrievalResult;
use crate::document::{DocumentTree, NodeId};
use crate::utils::estimate_tokens;
use std::collections::HashSet;

/// Pruning strategy for context building.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PruningStrategy {
    /// Stop when token limit is reached (default).
    TokenLimit,
    /// Keep only results above relevance threshold.
    RelevanceThreshold(f32),
    /// Diversity-based: avoid redundant content.
    Diversity {
        /// Maximum keyword overlap ratio (0.0-1.0).
        max_overlap: f32,
    },
    /// Combined: token limit with relevance filtering.
    Hybrid {
        /// Minimum relevance score to include.
        min_relevance: f32,
    },
}

impl Default for PruningStrategy {
    fn default() -> Self {
        Self::TokenLimit
    }
}

/// Token estimation mode.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum TokenEstimation {
    /// Fast estimation: ~4 chars per token.
    #[default]
    Fast,
    /// Accurate estimation using tiktoken.
    Accurate,
}

/// Context builder for assembling retrieval results.
#[derive(Debug)]
pub struct ContextBuilder {
    /// Maximum tokens for the context.
    max_tokens: usize,

    /// Whether to include titles.
    include_titles: bool,

    /// Whether to include summaries.
    include_summaries: bool,

    /// Whether to include content.
    include_content: bool,

    /// Separator between sections.
    separator: String,

    /// Pruning strategy.
    pruning_strategy: PruningStrategy,

    /// Token estimation mode.
    token_estimation: TokenEstimation,

    /// Chunk size for async processing.
    async_chunk_size: usize,
}

impl Default for ContextBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl ContextBuilder {
    /// Create a new context builder.
    pub fn new() -> Self {
        Self {
            max_tokens: 4000,
            include_titles: true,
            include_summaries: true,
            include_content: true,
            separator: "\n\n---\n\n".to_string(),
            pruning_strategy: PruningStrategy::TokenLimit,
            token_estimation: TokenEstimation::Fast,
            async_chunk_size: 100,
        }
    }

    /// Set the maximum tokens.
    pub fn with_max_tokens(mut self, tokens: usize) -> Self {
        self.max_tokens = tokens;
        self
    }

    /// Set whether to include titles.
    pub fn with_titles(mut self, include: bool) -> Self {
        self.include_titles = include;
        self
    }

    /// Set whether to include summaries.
    pub fn with_summaries(mut self, include: bool) -> Self {
        self.include_summaries = include;
        self
    }

    /// Set whether to include content.
    pub fn with_content(mut self, include: bool) -> Self {
        self.include_content = include;
        self
    }

    /// Set the separator.
    pub fn with_separator(mut self, separator: impl Into<String>) -> Self {
        self.separator = separator.into();
        self
    }

    /// Set the pruning strategy.
    pub fn with_pruning_strategy(mut self, strategy: PruningStrategy) -> Self {
        self.pruning_strategy = strategy;
        self
    }

    /// Set token estimation mode.
    pub fn with_token_estimation(mut self, mode: TokenEstimation) -> Self {
        self.token_estimation = mode;
        self
    }

    /// Set chunk size for async processing.
    pub fn with_async_chunk_size(mut self, size: usize) -> Self {
        self.async_chunk_size = size;
        self
    }

    /// Estimate tokens for a string.
    fn estimate_tokens(&self, text: &str) -> usize {
        match self.token_estimation {
            TokenEstimation::Fast => text.len() / 4,
            TokenEstimation::Accurate => estimate_tokens(text),
        }
    }

    /// Build context from retrieval results (synchronous).
    pub fn build(&self, results: &[RetrievalResult]) -> String {
        match self.pruning_strategy {
            PruningStrategy::TokenLimit => self.build_token_limit(results),
            PruningStrategy::RelevanceThreshold(min) => self.build_relevance(results, min),
            PruningStrategy::Diversity { max_overlap } => {
                self.build_diversity(results, max_overlap)
            }
            PruningStrategy::Hybrid { min_relevance } => self.build_hybrid(results, min_relevance),
        }
    }

    /// Build context asynchronously for large documents.
    ///
    /// Processes results in chunks to avoid blocking.
    pub async fn build_async(&self, results: &[RetrievalResult]) -> String {
        // For small result sets, just use sync
        if results.len() < self.async_chunk_size {
            return self.build(results);
        }

        // Process in chunks with yield points
        let mut sections = Vec::new();
        let mut estimated_tokens = 0;
        let separator_tokens = self.estimate_tokens(&self.separator);
        let mut included_keywords: HashSet<String> = HashSet::new();

        for (i, chunk) in results.chunks(self.async_chunk_size).enumerate() {
            // Yield to the runtime every chunk
            if i > 0 {
                tokio::task::yield_now().await;
            }

            for result in chunk {
                // Apply pruning strategy
                match self.pruning_strategy {
                    PruningStrategy::RelevanceThreshold(min) => {
                        if result.score < min {
                            continue;
                        }
                    }
                    PruningStrategy::Diversity { max_overlap } => {
                        let keywords = self.extract_keywords(result);
                        if self.calculate_overlap(&keywords, &included_keywords) > max_overlap {
                            continue;
                        }
                        included_keywords.extend(keywords);
                    }
                    PruningStrategy::Hybrid { min_relevance } => {
                        if result.score < min_relevance {
                            continue;
                        }
                    }
                    PruningStrategy::TokenLimit => {}
                }

                let section = self.format_section(result);
                let section_tokens = self.estimate_tokens(&section);

                if estimated_tokens + section_tokens + separator_tokens > self.max_tokens {
                    break;
                }

                estimated_tokens += section_tokens + separator_tokens;
                sections.push(section);
            }

            // Early exit if we've hit the token limit
            if estimated_tokens >= self.max_tokens {
                break;
            }
        }

        sections.join(&self.separator)
    }

    /// Build with simple token limit.
    fn build_token_limit(&self, results: &[RetrievalResult]) -> String {
        let mut sections = Vec::new();
        let mut estimated_tokens = 0;
        let separator_tokens = self.estimate_tokens(&self.separator);

        for result in results {
            let section = self.format_section(result);
            let section_tokens = self.estimate_tokens(&section);

            if estimated_tokens + section_tokens + separator_tokens > self.max_tokens {
                break;
            }

            estimated_tokens += section_tokens + separator_tokens;
            sections.push(section);
        }

        sections.join(&self.separator)
    }

    /// Build with relevance threshold.
    fn build_relevance(&self, results: &[RetrievalResult], min_score: f32) -> String {
        let mut sections = Vec::new();
        let mut estimated_tokens = 0;
        let separator_tokens = self.estimate_tokens(&self.separator);

        for result in results {
            if result.score < min_score {
                continue;
            }

            let section = self.format_section(result);
            let section_tokens = self.estimate_tokens(&section);

            if estimated_tokens + section_tokens + separator_tokens > self.max_tokens {
                break;
            }

            estimated_tokens += section_tokens + separator_tokens;
            sections.push(section);
        }

        sections.join(&self.separator)
    }

    /// Build with diversity-based pruning.
    fn build_diversity(&self, results: &[RetrievalResult], max_overlap: f32) -> String {
        let mut sections = Vec::new();
        let mut estimated_tokens = 0;
        let separator_tokens = self.estimate_tokens(&self.separator);
        let mut included_keywords: HashSet<String> = HashSet::new();

        for result in results {
            let keywords = self.extract_keywords(result);

            if self.calculate_overlap(&keywords, &included_keywords) > max_overlap {
                continue;
            }

            let section = self.format_section(result);
            let section_tokens = self.estimate_tokens(&section);

            if estimated_tokens + section_tokens + separator_tokens > self.max_tokens {
                break;
            }

            estimated_tokens += section_tokens + separator_tokens;
            included_keywords.extend(keywords);
            sections.push(section);
        }

        sections.join(&self.separator)
    }

    /// Build with hybrid strategy (relevance + token limit).
    fn build_hybrid(&self, results: &[RetrievalResult], min_relevance: f32) -> String {
        let mut sections = Vec::new();
        let mut estimated_tokens = 0;
        let separator_tokens = self.estimate_tokens(&self.separator);

        for result in results {
            if result.score < min_relevance {
                continue;
            }

            let section = self.format_section(result);
            let section_tokens = self.estimate_tokens(&section);

            if estimated_tokens + section_tokens + separator_tokens > self.max_tokens {
                break;
            }

            estimated_tokens += section_tokens + separator_tokens;
            sections.push(section);
        }

        sections.join(&self.separator)
    }

    /// Extract keywords from a result for diversity checking.
    fn extract_keywords(&self, result: &RetrievalResult) -> Vec<String> {
        let mut words = Vec::new();

        // Collect from title
        words.extend(
            result
                .title
                .to_lowercase()
                .split_whitespace()
                .filter(|w| w.len() > 3)
                .map(|w| w.to_string()),
        );

        // Collect from summary
        if let Some(summary) = &result.summary {
            words.extend(
                summary
                    .to_lowercase()
                    .split_whitespace()
                    .filter(|w| w.len() > 3)
                    .map(|w| w.to_string()),
            );
        }

        // Limit keywords
        words.truncate(20);
        words
    }

    /// Calculate overlap between keyword sets.
    fn calculate_overlap(&self, new_keywords: &[String], existing: &HashSet<String>) -> f32 {
        if new_keywords.is_empty() || existing.is_empty() {
            return 0.0;
        }

        let matches = new_keywords
            .iter()
            .filter(|k| existing.contains(*k))
            .count();

        matches as f32 / new_keywords.len() as f32
    }

    /// Build context from a document tree starting at a node (synchronous).
    pub fn build_from_tree(
        &self,
        tree: &DocumentTree,
        node_id: NodeId,
        max_depth: usize,
    ) -> String {
        let mut sections = Vec::new();
        self.collect_sections(tree, node_id, 0, max_depth, &mut sections);
        sections.join(&self.separator)
    }

    /// Build context from a document tree asynchronously.
    pub async fn build_from_tree_async(
        &self,
        tree: &DocumentTree,
        node_id: NodeId,
        max_depth: usize,
    ) -> String {
        let mut sections = Vec::new();
        self.collect_sections_async(tree, node_id, 0, max_depth, &mut sections)
            .await;
        sections.join(&self.separator)
    }

    fn collect_sections(
        &self,
        tree: &DocumentTree,
        node_id: NodeId,
        current_depth: usize,
        max_depth: usize,
        sections: &mut Vec<String>,
    ) {
        if current_depth > max_depth {
            return;
        }

        if let Some(node) = tree.get(node_id) {
            let section = self.format_node_section(node, current_depth);
            if !section.is_empty() {
                sections.push(section);
            }

            for child_id in tree.children_iter(node_id) {
                self.collect_sections(tree, child_id, current_depth + 1, max_depth, sections);
            }
        }
    }

    async fn collect_sections_async(
        &self,
        tree: &DocumentTree,
        node_id: NodeId,
        current_depth: usize,
        max_depth: usize,
        sections: &mut Vec<String>,
    ) {
        if current_depth > max_depth {
            return;
        }

        // Yield every few levels to avoid blocking
        if current_depth > 0 && current_depth.is_multiple_of(3) {
            tokio::task::yield_now().await;
        }

        if let Some(node) = tree.get(node_id) {
            let section = self.format_node_section(node, current_depth);
            if !section.is_empty() {
                sections.push(section);
            }

            for child_id in tree.children_iter(node_id) {
                Box::pin(self.collect_sections_async(
                    tree,
                    child_id,
                    current_depth + 1,
                    max_depth,
                    sections,
                ))
                .await;
            }
        }
    }

    fn format_node_section(&self, node: &crate::document::TreeNode, depth: usize) -> String {
        let mut section = String::new();

        if self.include_titles {
            let indent = "  ".repeat(depth);
            section.push_str(&format!("{}# {}\n", indent, node.title));
        }

        if self.include_summaries && !node.summary.is_empty() {
            section.push_str(&format!("Summary: {}\n", node.summary));
        }

        if self.include_content && !node.content.is_empty() {
            section.push_str(&format!("\n{}\n", node.content));
        }

        section
    }

    fn format_section(&self, result: &RetrievalResult) -> String {
        let mut section = String::new();

        if self.include_titles {
            section.push_str(&format!("## {}\n", result.title));
        }

        if self.include_summaries {
            if let Some(summary) = &result.summary {
                section.push_str(&format!("Summary: {}\n", summary));
            }
        }

        if self.include_content {
            if let Some(content) = &result.content {
                section.push_str(&format!("\n{}\n", content));
            }
        }

        section
    }
}

/// Format retrieval results for LLM consumption.
pub fn format_for_llm(results: &[RetrievalResult], max_tokens: usize) -> String {
    ContextBuilder::new()
        .with_max_tokens(max_tokens)
        .build(results)
}

/// Format retrieval results asynchronously.
pub async fn format_for_llm_async(results: &[RetrievalResult], max_tokens: usize) -> String {
    ContextBuilder::new()
        .with_max_tokens(max_tokens)
        .build_async(results)
        .await
}

/// Format a document tree for LLM consumption.
pub fn format_tree_for_llm(tree: &DocumentTree, max_depth: usize, max_tokens: usize) -> String {
    ContextBuilder::new()
        .with_max_tokens(max_tokens)
        .build_from_tree(tree, tree.root(), max_depth)
}

/// Format a document tree asynchronously.
pub async fn format_tree_for_llm_async(
    tree: &DocumentTree,
    max_depth: usize,
    max_tokens: usize,
) -> String {
    ContextBuilder::new()
        .with_max_tokens(max_tokens)
        .build_from_tree_async(tree, tree.root(), max_depth)
        .await
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_context_builder() {
        let results = vec![
            RetrievalResult::new("Section 1").with_content("Content 1"),
            RetrievalResult::new("Section 2").with_content("Content 2"),
        ];

        let context = ContextBuilder::new().with_max_tokens(1000).build(&results);

        assert!(context.contains("Section 1"));
        assert!(context.contains("Content 1"));
    }

    #[test]
    fn test_pruning_strategy_relevance() {
        let results = vec![
            RetrievalResult::new("High relevance").with_score(0.9),
            RetrievalResult::new("Low relevance").with_score(0.1),
        ];

        let context = ContextBuilder::new()
            .with_max_tokens(1000)
            .with_pruning_strategy(PruningStrategy::RelevanceThreshold(0.5))
            .build(&results);

        assert!(context.contains("High relevance"));
        assert!(!context.contains("Low relevance"));
    }

    #[test]
    fn test_token_estimation_modes() {
        let fast_builder = ContextBuilder::new().with_token_estimation(TokenEstimation::Fast);
        let accurate_builder =
            ContextBuilder::new().with_token_estimation(TokenEstimation::Accurate);

        let fast_tokens = fast_builder.estimate_tokens("Hello world test");
        let accurate_tokens = accurate_builder.estimate_tokens("Hello world test");

        assert!(fast_tokens > 0);
        assert!(accurate_tokens > 0);
    }

    #[test]
    fn test_diversity_pruning() {
        let results = vec![
            RetrievalResult::new("Unique topic alpha").with_score(0.9),
            RetrievalResult::new("Unique topic alpha beta").with_score(0.8), // Similar
            RetrievalResult::new("Different gamma delta").with_score(0.7),
        ];

        let context = ContextBuilder::new()
            .with_max_tokens(1000)
            .with_pruning_strategy(PruningStrategy::Diversity { max_overlap: 0.3 })
            .build(&results);

        // Should include first and third, skip second (too similar to first)
        assert!(context.contains("alpha"));
        assert!(context.contains("gamma"));
    }

    #[tokio::test]
    async fn test_async_build() {
        let results: Vec<_> = (0..200)
            .map(|i| {
                RetrievalResult::new(&format!("Section {}", i))
                    .with_content(&format!("Content {}", i))
            })
            .collect();

        let context = ContextBuilder::new()
            .with_max_tokens(10000)
            .build_async(&results)
            .await;

        assert!(!context.is_empty());
    }
}