vectorless 0.1.21

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

//! Document indexing client.
//!
//! This module provides document indexing operations including
//! format detection, parsing, and tree building.
//!
//! # Example
//!
//! ```rust,ignore
//! use vectorless::client::{IndexerClient, IndexContext};
//!
//! let indexer = IndexerClient::new(executor);
//!
//! let result = indexer
//!     .index(IndexContext::from_path("./document.md"))
//!     .await?;
//!
//! println!("Indexed: {} ({} nodes)", result.id, result.tree.as_ref().map(|t| t.node_count()).unwrap_or(0));
//! ```

use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};

use tracing::info;
use uuid::Uuid;

use crate::error::{Error, Result};
use crate::index::{IndexInput, IndexMode, PipelineExecutor, PipelineOptions, SummaryStrategy};
use crate::parser::DocumentFormat;
use crate::storage::{DocumentMeta, PersistedDocument};

use super::events::{EventEmitter, IndexEvent};
use super::index_context::{IndexContext, IndexSource};
use super::types::{IndexOptions, IndexedDocument};

/// Document indexing client.
///
/// Provides operations for parsing and indexing documents.
pub(crate) struct IndexerClient {
    /// Pipeline executor.
    executor: Arc<Mutex<PipelineExecutor>>,

    /// Event emitter.
    events: EventEmitter,

    /// Configuration.
    config: IndexerConfig,
}

/// Indexer configuration.
#[derive(Debug, Clone)]
pub struct IndexerConfig {
    /// Minimum content tokens required to generate a summary.
    pub min_summary_tokens: usize,

    /// Whether to generate IDs by default.
    pub generate_ids: bool,

    /// Whether to generate descriptions by default.
    pub generate_descriptions: bool,
}

impl Default for IndexerConfig {
    fn default() -> Self {
        Self {
            min_summary_tokens: 20,
            generate_ids: true,
            generate_descriptions: false,
        }
    }
}

impl IndexerClient {
    /// Create a new indexer client.
    pub fn new(executor: PipelineExecutor) -> Self {
        Self {
            executor: Arc::new(Mutex::new(executor)),
            events: EventEmitter::new(),
            config: IndexerConfig::default(),
        }
    }

    /// Create with event emitter.
    pub fn with_events(mut self, events: EventEmitter) -> Self {
        self.events = events;
        self
    }

    /// Create with configuration.
    pub fn with_config(mut self, config: IndexerConfig) -> Self {
        self.config = config;
        self
    }

    /// Create from an existing executor Arc.
    pub(crate) fn from_arc(
        executor: Arc<Mutex<PipelineExecutor>>,
        events: EventEmitter,
        config: IndexerConfig,
    ) -> Self {
        Self {
            executor,
            events,
            config,
        }
    }

    /// Index a document from an index context.
    ///
    /// This is the main entry point for indexing documents. The context
    /// specifies the source (path, content, or bytes) and options.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The file does not exist (for path sources)
    /// - The file format is not supported
    /// - The pipeline execution fails
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use vectorless::client::{IndexerClient, IndexContext};
    /// use vectorless::parser::DocumentFormat;
    ///
    /// // From file path
    /// let doc = indexer.index(IndexContext::from_path("./doc.md")).await?;
    ///
    /// // From HTML content
    /// let html = "<html><body><h1>Title</h1></body></html>";
    /// let doc = indexer.index(
    ///     IndexContext::from_content(html, DocumentFormat::Html)
    ///         .with_name("webpage")
    /// ).await?;
    /// ```
    pub async fn index(&self, ctx: IndexContext) -> Result<IndexedDocument> {
        match &ctx.source {
            IndexSource::Path(path) => self.index_from_path(path, &ctx).await,
            IndexSource::Content { data, format } => {
                self.index_from_content(data, *format, &ctx).await
            }
            IndexSource::Bytes { data, format } => self.index_from_bytes(data, *format, &ctx).await,
        }
    }

    /// Index from a file path.
    async fn index_from_path(&self, path: &Path, ctx: &IndexContext) -> Result<IndexedDocument> {
        let path = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());

        if !path.exists() {
            return Err(Error::Parse(format!("File not found: {}", path.display())));
        }

        // Emit start event
        self.events.emit_index(IndexEvent::Started {
            path: path.display().to_string(),
        });

        // Generate document ID
        let doc_id = Uuid::new_v4().to_string();

        // Detect format from extension
        let format = self.detect_format_from_path(&path)?;
        self.events
            .emit_index(IndexEvent::FormatDetected { format });

        info!("Indexing {:?} document: {}", format, path.display());

        // Build pipeline options
        let pipeline_options = self.build_pipeline_options(&ctx.options, format);

        // Create pipeline input and execute
        let input = IndexInput::file(&path);
        let result = {
            let mut executor = self
                .executor
                .lock()
                .map_err(|_| Error::Other("Pipeline executor lock poisoned".to_string()))?;
            executor.execute(input, pipeline_options).await?
        };

        self.build_indexed_document(doc_id, result, format, ctx.name.as_deref(), Some(&path))
    }

    /// Index from content string.
    async fn index_from_content(
        &self,
        content: &str,
        format: DocumentFormat,
        ctx: &IndexContext,
    ) -> Result<IndexedDocument> {
        // Emit start event
        self.events.emit_index(IndexEvent::Started {
            path: ctx.name.clone().unwrap_or_else(|| "content".to_string()),
        });

        let doc_id = Uuid::new_v4().to_string();
        self.events
            .emit_index(IndexEvent::FormatDetected { format });

        info!("Indexing {:?} document from content", format);

        let pipeline_options = self.build_pipeline_options(&ctx.options, format);

        let input = IndexInput::content(content);
        let result = {
            let mut executor = self
                .executor
                .lock()
                .map_err(|_| Error::Other("Pipeline executor lock poisoned".to_string()))?;
            executor.execute(input, pipeline_options).await?
        };

        self.build_indexed_document(doc_id, result, format, ctx.name.as_deref(), None)
    }

    /// Index from binary data.
    async fn index_from_bytes(
        &self,
        bytes: &[u8],
        format: DocumentFormat,
        ctx: &IndexContext,
    ) -> Result<IndexedDocument> {
        // Emit start event
        self.events.emit_index(IndexEvent::Started {
            path: ctx.name.clone().unwrap_or_else(|| "bytes".to_string()),
        });

        let doc_id = Uuid::new_v4().to_string();
        self.events
            .emit_index(IndexEvent::FormatDetected { format });

        info!(
            "Indexing {:?} document from bytes ({} bytes)",
            format,
            bytes.len()
        );

        let pipeline_options = self.build_pipeline_options(&ctx.options, format);

        let input = IndexInput::bytes(bytes);
        let result = {
            let mut executor = self
                .executor
                .lock()
                .map_err(|_| Error::Other("Pipeline executor lock poisoned".to_string()))?;
            executor.execute(input, pipeline_options).await?
        };

        self.build_indexed_document(doc_id, result, format, ctx.name.as_deref(), None)
    }

    /// Build pipeline options from client options.
    fn build_pipeline_options(
        &self,
        options: &IndexOptions,
        format: DocumentFormat,
    ) -> PipelineOptions {
        println!("[DEBUG] Building pipeline options for format: {:?} with options: {:?}", format, options);

        PipelineOptions {
            mode: match format {
                DocumentFormat::Markdown => IndexMode::Markdown,
                DocumentFormat::Pdf => IndexMode::Pdf,
                DocumentFormat::Html => IndexMode::Html,
                DocumentFormat::Docx => IndexMode::Docx,
            },
            generate_ids: options.generate_ids,
            summary_strategy: if options.generate_summaries {
                // SummaryStrategy::selective(self.config.min_summary_tokens, false)
                SummaryStrategy::full()
            } else {
                SummaryStrategy::none()
            },
            generate_description: options.generate_description,
            ..Default::default()
        }
    }

    /// Build indexed document from pipeline result.
    fn build_indexed_document(
        &self,
        doc_id: String,
        result: crate::index::IndexResult,
        format: DocumentFormat,
        name: Option<&str>,
        path: Option<&Path>,
    ) -> Result<IndexedDocument> {
        let tree = result
            .tree
            .ok_or_else(|| Error::Parse("Document tree not generated".to_string()))?;

        let node_count = tree.node_count();
        self.events.emit_index(IndexEvent::TreeBuilt { node_count });

        let doc_name = name
            .map(str::to_string)
            .or_else(|| {
                path.and_then(|p| p.file_stem())
                    .map(|s| s.to_string_lossy().to_string())
            })
            .unwrap_or_else(|| result.name.clone());

        let mut doc = IndexedDocument::new(&doc_id, format)
            .with_name(&doc_name)
            .with_tree(tree);

        if let Some(p) = path {
            doc = doc.with_source_path(p);
        }

        if let Some(desc) = &result.description {
            doc = doc.with_description(desc);
        }

        if let Some(page_count) = result.page_count {
            doc = doc.with_page_count(page_count);
        }

        info!("Indexing complete: {} ({} nodes)", doc_id, node_count);
        self.events.emit_index(IndexEvent::Complete { doc_id });

        Ok(doc)
    }

    /// Detect document format from file extension.
    fn detect_format_from_path(&self, path: &Path) -> Result<DocumentFormat> {
        let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
        DocumentFormat::from_extension(ext)
            .ok_or_else(|| Error::Parse(format!("Unsupported format: {}", ext)))
    }

    /// Validate a document before indexing.
    ///
    /// # Errors
    ///
    /// Returns an error if the file doesn't exist or is not readable.
    pub fn validate(&self, path: impl AsRef<Path>) -> Result<ValidationResult> {
        let path = path.as_ref();

        if !path.exists() {
            return Ok(ValidationResult {
                valid: false,
                errors: vec![format!("File not found: {}", path.display())],
                warnings: vec![],
                format: None,
                estimated_size: 0,
            });
        }

        let metadata = std::fs::metadata(path)
            .map_err(|e| Error::Parse(format!("Cannot read file metadata: {}", e)))?;

        let estimated_size = metadata.len() as usize;
        let mut warnings = Vec::new();

        // Check file size
        if estimated_size > 100 * 1024 * 1024 {
            warnings.push("Large file (>100MB) may take longer to index".to_string());
        }

        // Detect format
        let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
        let format = DocumentFormat::from_extension(ext);

        if format.is_none() {
            return Ok(ValidationResult {
                valid: false,
                errors: vec![format!("Unsupported format: {}", ext)],
                warnings,
                format: None,
                estimated_size,
            });
        }

        Ok(ValidationResult {
            valid: true,
            errors: vec![],
            warnings,
            format,
            estimated_size,
        })
    }

    /// Convert IndexedDocument to PersistedDocument for storage.
    pub fn to_persisted(&self, doc: IndexedDocument) -> PersistedDocument {
        let meta = DocumentMeta::new(&doc.id, &doc.name, doc.format.extension())
            .with_source_path(
                doc.source_path
                    .as_ref()
                    .map(|p| p.to_string_lossy().to_string())
                    .unwrap_or_default(),
            )
            .with_description(doc.description.clone().unwrap_or_default());

        let mut persisted =
            PersistedDocument::new(meta, doc.tree.expect("IndexedDocument must have a tree"));

        for page in doc.pages {
            persisted.add_page(page.page, &page.content);
        }

        persisted
    }

    /// Get the underlying executor Arc (for advanced use).
    pub(crate) fn inner(&self) -> Arc<Mutex<PipelineExecutor>> {
        Arc::clone(&self.executor)
    }
}

impl Clone for IndexerClient {
    fn clone(&self) -> Self {
        Self {
            executor: Arc::clone(&self.executor),
            events: self.events.clone(),
            config: self.config.clone(),
        }
    }
}

/// Document validation result.
#[derive(Debug, Clone)]
pub(crate) struct ValidationResult {
    /// Whether the document is valid for indexing.
    pub valid: bool,

    /// Validation errors (prevents indexing).
    pub errors: Vec<String>,

    /// Validation warnings (non-blocking).
    pub warnings: Vec<String>,

    /// Detected document format.
    pub format: Option<DocumentFormat>,

    /// Estimated file size in bytes.
    pub estimated_size: usize,
}

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

    #[test]
    fn test_indexer_client_creation() {
        let executor = PipelineExecutor::new();
        let client = IndexerClient::new(executor);
        assert_eq!(client.config.min_summary_tokens, 20);
    }

    #[test]
    fn test_validate_missing_file() {
        let executor = PipelineExecutor::new();
        let client = IndexerClient::new(executor);

        let result = client.validate("./nonexistent.md").unwrap();
        assert!(!result.valid);
        assert!(!result.errors.is_empty());
    }
}