vectorless 0.1.24

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
// Copyright (c) 2026 vectorless developers
// SPDX-License-Identifier: Apache-2.0

//! Public API types for the client module.
//!
//! This module contains all types exposed in the public API.

use serde::{Deserialize, Serialize};
use std::path::PathBuf;

use crate::document::DocumentTree;
use crate::index::parse::DocumentFormat;
use crate::metrics::IndexMetrics;

// ============================================================
// Document Types
// ============================================================

/// An indexed document with its tree structure and metadata.
#[derive(Debug, Clone)]
pub struct IndexedDocument {
    /// Unique document identifier.
    pub id: String,

    /// Document format.
    pub format: DocumentFormat,

    /// Document name/title.
    pub name: String,

    /// Document description (generated by LLM).
    pub description: Option<String>,

    /// Source file path.
    pub source_path: Option<PathBuf>,

    /// Page count (for PDFs).
    pub page_count: Option<usize>,

    /// Line count (for text files).
    pub line_count: Option<usize>,

    /// The document tree structure.
    pub tree: Option<DocumentTree>,

    /// Per-page content (for PDFs).
    pub pages: Vec<PageContent>,

    /// Indexing pipeline metrics.
    pub metrics: Option<IndexMetrics>,

    /// Pre-computed reasoning index for retrieval acceleration.
    pub reasoning_index: Option<crate::document::ReasoningIndex>,
}

impl IndexedDocument {
    /// Create a new indexed document.
    pub fn new(id: impl Into<String>, format: DocumentFormat) -> Self {
        Self {
            id: id.into(),
            format,
            name: String::new(),
            description: None,
            source_path: None,
            page_count: None,
            line_count: None,
            tree: None,
            pages: Vec::new(),
            metrics: None,
            reasoning_index: None,
        }
    }

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

    /// Set the document description.
    pub fn with_description(mut self, desc: impl Into<String>) -> Self {
        self.description = Some(desc.into());
        self
    }

    /// Set the source path.
    pub fn with_source_path(mut self, path: impl Into<PathBuf>) -> Self {
        self.source_path = Some(path.into());
        self
    }

    /// Set the page count.
    pub fn with_page_count(mut self, count: usize) -> Self {
        self.page_count = Some(count);
        self
    }

    /// Set the line count.
    pub fn with_line_count(mut self, count: usize) -> Self {
        self.line_count = Some(count);
        self
    }

    /// Set the document tree.
    pub fn with_tree(mut self, tree: DocumentTree) -> Self {
        self.tree = Some(tree);
        self
    }

    /// Set the indexing metrics.
    pub fn with_metrics(mut self, metrics: IndexMetrics) -> Self {
        self.metrics = Some(metrics);
        self
    }

    /// Add a page content.
    pub fn add_page(&mut self, page: usize, content: impl Into<String>) {
        self.pages.push(PageContent {
            page,
            content: content.into(),
        });
    }

    /// Check if the tree is loaded.
    pub fn is_loaded(&self) -> bool {
        self.tree.is_some()
    }
}

/// Content for a single page.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PageContent {
    /// Page number (1-based).
    pub page: usize,

    /// Page text content.
    pub content: String,
}

// ============================================================
// Partial Success
// ============================================================

/// A failed item in a batch operation.
#[derive(Debug, Clone)]
pub struct FailedItem {
    /// Source description (file path, content name, or doc ID).
    pub source: String,
    /// Error message.
    pub error: String,
}

impl FailedItem {
    /// Create a new failed item.
    pub fn new(source: impl Into<String>, error: impl Into<String>) -> Self {
        Self {
            source: source.into(),
            error: error.into(),
        }
    }
}

// ============================================================
// Index Types
// ============================================================

/// Document indexing behavior mode.
///
/// Controls how the indexer handles existing documents and re-indexing.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum IndexMode {
    /// Default mode - skip if already indexed.
    ///
    /// If a document with the same source has already been indexed,
    /// the operation is skipped and the existing document ID is returned.
    #[default]
    Default,

    /// Force re-indexing.
    ///
    /// Always re-index the document, even if it has been indexed before.
    /// A new document ID is generated.
    Force,

    /// Incremental mode - only re-index changed files.
    ///
    /// Re-index only if the file has been modified since the last index.
    /// For content/bytes sources, this behaves like [`IndexMode::Default`].
    Incremental,
}

/// Options for indexing a document.
#[derive(Debug, Clone)]
pub struct IndexOptions {
    /// Indexing mode.
    pub mode: IndexMode,

    /// Whether to generate summaries using LLM.
    pub generate_summaries: bool,

    /// Whether to include node text in the tree.
    pub include_text: bool,

    /// Whether to generate node IDs.
    pub generate_ids: bool,

    /// Whether to generate document description.
    pub generate_description: bool,
}

impl Default for IndexOptions {
    fn default() -> Self {
        Self {
            mode: IndexMode::Default,
            generate_summaries: true,
            include_text: true,
            generate_ids: true,
            generate_description: false,
        }
    }
}

impl IndexOptions {
    /// Create new index options with defaults.
    pub fn new() -> Self {
        Self::default()
    }

    /// Enable summary generation.
    pub fn with_summaries(mut self) -> Self {
        self.generate_summaries = true;
        self
    }

    /// Enable document description generation.
    pub fn with_description(mut self) -> Self {
        self.generate_description = true;
        self
    }

    /// Set the indexing mode.
    ///
    /// # Modes
    ///
    /// - [`IndexMode::Default`] - Skip if already indexed
    /// - [`IndexMode::Force`] - Always re-index
    /// - [`IndexMode::Incremental`] - Only re-index changed files
    pub fn with_mode(mut self, mode: IndexMode) -> Self {
        self.mode = mode;
        self
    }
}

// ============================================================
// Index Result Types
// ============================================================

/// Result of a document indexing operation.
#[derive(Debug, Clone)]
pub struct IndexResult {
    /// Successfully indexed items.
    pub items: Vec<IndexItem>,

    /// Items that failed to index (partial success).
    pub failed: Vec<FailedItem>,
}

impl IndexResult {
    /// Create a new index result.
    pub fn new(items: Vec<IndexItem>) -> Self {
        Self {
            items,
            failed: Vec::new(),
        }
    }

    /// Create with both successes and failures.
    pub fn with_partial(items: Vec<IndexItem>, failed: Vec<FailedItem>) -> Self {
        Self { items, failed }
    }

    /// Get the single document ID (convenience for single-document indexing).
    pub fn doc_id(&self) -> Option<&str> {
        if self.items.len() == 1 {
            Some(&self.items[0].doc_id)
        } else {
            None
        }
    }

    /// Check if the result is empty.
    pub fn is_empty(&self) -> bool {
        self.items.is_empty()
    }

    /// Get the number of indexed items.
    pub fn len(&self) -> usize {
        self.items.len()
    }

    /// Whether any items failed.
    pub fn has_failures(&self) -> bool {
        !self.failed.is_empty()
    }

    /// Total number of sources (success + failed).
    pub fn total(&self) -> usize {
        self.items.len() + self.failed.len()
    }
}

/// A single indexed document item.
#[derive(Debug, Clone)]
pub struct IndexItem {
    /// The unique document ID.
    pub doc_id: String,
    /// The document name.
    pub name: String,
    /// The document format.
    pub format: DocumentFormat,
    /// Document description (from root summary).
    pub description: Option<String>,
    /// Page count (for PDFs).
    pub page_count: Option<usize>,
    /// Indexing pipeline metrics (timing, LLM usage, node stats).
    pub metrics: Option<IndexMetrics>,
}

impl IndexItem {
    /// Create a new index item.
    pub fn new(
        doc_id: impl Into<String>,
        name: impl Into<String>,
        format: DocumentFormat,
        description: Option<String>,
        page_count: Option<usize>,
    ) -> Self {
        Self {
            doc_id: doc_id.into(),
            name: name.into(),
            format,
            description,
            page_count,
            metrics: None,
        }
    }

    /// Set the indexing metrics.
    pub fn with_metrics(mut self, metrics: IndexMetrics) -> Self {
        self.metrics = Some(metrics);
        self
    }

    /// Set the indexing metrics (optional).
    pub fn with_metrics_opt(mut self, metrics: Option<IndexMetrics>) -> Self {
        self.metrics = metrics;
        self
    }
}

// ============================================================
// Query Types
// ============================================================

/// A single document's query result.
#[derive(Debug, Clone)]
pub struct QueryResultItem {
    /// The document ID.
    pub doc_id: String,

    /// Matching node IDs.
    pub node_ids: Vec<String>,

    /// Retrieved content.
    pub content: String,

    /// Relevance score.
    pub score: f32,
}

/// Result of a document query.
///
/// Contains results from one or more documents. For single-document queries,
/// `items` has one entry. For multi-document or workspace queries, it has
/// one entry per document that matched.
#[derive(Debug, Clone)]
pub struct QueryResult {
    /// Query results per document.
    pub items: Vec<QueryResultItem>,

    /// Documents that failed during multi-doc query.
    pub failed: Vec<FailedItem>,
}

impl QueryResult {
    /// Create a new query result (empty).
    pub fn new() -> Self {
        Self {
            items: Vec::new(),
            failed: Vec::new(),
        }
    }

    /// Create a query result with a single item.
    pub fn from_single(item: QueryResultItem) -> Self {
        Self {
            items: vec![item],
            failed: Vec::new(),
        }
    }

    /// Create with both successes and failures.
    pub fn with_partial(items: Vec<QueryResultItem>, failed: Vec<FailedItem>) -> Self {
        Self { items, failed }
    }

    /// Check if the result is empty.
    pub fn is_empty(&self) -> bool {
        self.items.is_empty()
    }

    /// Get the number of result items.
    pub fn len(&self) -> usize {
        self.items.len()
    }

    /// Get the first (single-doc) result item, if any.
    pub fn single(&self) -> Option<&QueryResultItem> {
        self.items.first()
    }

    /// Whether any documents failed.
    pub fn has_failures(&self) -> bool {
        !self.failed.is_empty()
    }
}

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

// ============================================================
// Document Info Types
// ============================================================

/// Document info for listing.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DocumentInfo {
    /// Document ID.
    pub id: String,

    /// Document name.
    pub name: String,

    /// Document format.
    pub format: String,

    /// Document description.
    pub description: Option<String>,

    /// Page count (for PDFs).
    pub page_count: Option<usize>,

    /// Line count (for text files).
    pub line_count: Option<usize>,
}

impl DocumentInfo {
    /// Create a new document info.
    pub fn new(id: impl Into<String>, name: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            name: name.into(),
            format: String::new(),
            description: None,
            page_count: None,
            line_count: None,
        }
    }

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

// ============================================================
// Error Types
// ============================================================

/// Client error types.
#[derive(Debug, Clone, thiserror::Error)]
pub enum ClientError {
    /// Document not found.
    #[error("Document not found: {0}")]
    NotFound(String),

    /// Invalid operation.
    #[error("Invalid operation: {0}")]
    InvalidOperation(String),

    /// Configuration error.
    #[error("Configuration error: {0}")]
    Config(String),

    /// Timeout error.
    #[error("Operation timed out")]
    Timeout,
}

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

    #[test]
    fn test_indexed_document() {
        let doc = IndexedDocument::new("doc-1", DocumentFormat::Markdown)
            .with_name("Test Document")
            .with_description("A test document");

        assert_eq!(doc.id, "doc-1");
        assert_eq!(doc.name, "Test Document");
        assert!(doc.tree.is_none());
    }

    #[test]
    fn test_index_options() {
        let options = IndexOptions::new()
            .with_summaries()
            .with_mode(IndexMode::Force);

        assert!(options.generate_summaries);
        assert_eq!(options.mode, IndexMode::Force);
    }

    #[test]
    fn test_query_result() {
        let result = QueryResult::new();
        assert!(result.is_empty());
        assert_eq!(result.len(), 0);
    }

    #[test]
    fn test_query_result_single() {
        let item = QueryResultItem {
            doc_id: "doc-1".into(),
            node_ids: vec!["n1".into()],
            content: "content".into(),
            score: 0.9,
        };
        let result = QueryResult::from_single(item);
        assert!(!result.is_empty());
        assert_eq!(result.len(), 1);
        assert!(result.single().is_some());
        assert_eq!(result.single().unwrap().doc_id, "doc-1");
    }

    #[test]
    fn test_document_info() {
        let info = DocumentInfo::new("doc-1", "Test").with_format("markdown");

        assert_eq!(info.id, "doc-1");
        assert_eq!(info.format, "markdown");
    }

    #[test]
    fn test_index_result() {
        let item = IndexItem::new("doc-1", "Test", DocumentFormat::Markdown, None, None);
        let result = IndexResult::new(vec![item]);

        assert_eq!(result.doc_id(), Some("doc-1"));
        assert_eq!(result.len(), 1);
        assert!(!result.is_empty());
    }

    #[test]
    fn test_index_result_empty() {
        let result = IndexResult::new(vec![]);
        assert!(result.is_empty());
        assert_eq!(result.doc_id(), None);
    }

    #[test]
    fn test_index_result_multiple() {
        let items = vec![
            IndexItem::new("doc-1", "A", DocumentFormat::Markdown, None, None),
            IndexItem::new("doc-2", "B", DocumentFormat::Pdf, None, None),
        ];
        let result = IndexResult::new(items);
        assert_eq!(result.len(), 2);
        assert_eq!(result.doc_id(), None);
    }

    #[test]
    fn test_partial_success() {
        let items = vec![IndexItem::new(
            "doc-1",
            "A",
            DocumentFormat::Markdown,
            None,
            None,
        )];
        let failed = vec![FailedItem::new("missing.pdf", "File not found")];
        let result = IndexResult::with_partial(items, failed);

        assert_eq!(result.len(), 1);
        assert!(result.has_failures());
        assert_eq!(result.total(), 2);
        assert_eq!(result.failed[0].source, "missing.pdf");
    }
}