Skip to main content

vectorless/client/
types.rs

1// Copyright (c) 2026 vectorless developers
2// SPDX-License-Identifier: Apache-2.0
3
4//! Client type definitions.
5
6use serde::{Deserialize, Serialize};
7use std::path::PathBuf;
8
9use crate::core::DocumentTree;
10use crate::document::DocumentFormat;
11
12/// An indexed document with its tree structure and metadata.
13#[derive(Debug, Clone)]
14pub struct IndexedDocument {
15    /// Unique document identifier.
16    pub id: String,
17
18    /// Document format.
19    pub format: DocumentFormat,
20
21    /// Document name/title.
22    pub name: String,
23
24    /// Document description (generated by LLM).
25    pub description: Option<String>,
26
27    /// Source file path.
28    pub source_path: Option<PathBuf>,
29
30    /// Page count (for PDFs).
31    pub page_count: Option<usize>,
32
33    /// Line count (for text files).
34    pub line_count: Option<usize>,
35
36    /// The document tree structure.
37    pub tree: Option<DocumentTree>,
38
39    /// Per-page content (for PDFs).
40    pub pages: Vec<PageContent>,
41}
42
43impl IndexedDocument {
44    /// Create a new indexed document.
45    pub fn new(id: impl Into<String>, format: DocumentFormat) -> Self {
46        Self {
47            id: id.into(),
48            format,
49            name: String::new(),
50            description: None,
51            source_path: None,
52            page_count: None,
53            line_count: None,
54            tree: None,
55            pages: Vec::new(),
56        }
57    }
58
59    /// Set the document name.
60    pub fn with_name(mut self, name: impl Into<String>) -> Self {
61        self.name = name.into();
62        self
63    }
64
65    /// Set the document description.
66    pub fn with_description(mut self, desc: impl Into<String>) -> Self {
67        self.description = Some(desc.into());
68        self
69    }
70
71    /// Set the source path.
72    pub fn with_source_path(mut self, path: impl Into<PathBuf>) -> Self {
73        self.source_path = Some(path.into());
74        self
75    }
76
77    /// Set the page count.
78    pub fn with_page_count(mut self, count: usize) -> Self {
79        self.page_count = Some(count);
80        self
81    }
82
83    /// Set the line count.
84    pub fn with_line_count(mut self, count: usize) -> Self {
85        self.line_count = Some(count);
86        self
87    }
88
89    /// Set the document tree.
90    pub fn with_tree(mut self, tree: DocumentTree) -> Self {
91        self.tree = Some(tree);
92        self
93    }
94
95    /// Add a page content.
96    pub fn add_page(&mut self, page: usize, content: impl Into<String>) {
97        self.pages.push(PageContent {
98            page,
99            content: content.into(),
100        });
101    }
102
103    /// Check if the tree is loaded.
104    pub fn is_loaded(&self) -> bool {
105        self.tree.is_some()
106    }
107}
108
109/// Content for a single page.
110#[derive(Debug, Clone, Serialize, Deserialize)]
111pub struct PageContent {
112    /// Page number (1-based).
113    pub page: usize,
114
115    /// Page text content.
116    pub content: String,
117}
118
119/// Document indexing mode.
120#[derive(Debug, Clone, Copy, PartialEq, Eq)]
121pub enum IndexMode {
122    /// Automatically detect format from file extension.
123    Auto,
124
125    /// Force PDF parsing.
126    Pdf,
127
128    /// Force Markdown parsing.
129    Markdown,
130
131    /// Force HTML parsing.
132    Html,
133}
134
135impl Default for IndexMode {
136    fn default() -> Self {
137        Self::Auto
138    }
139}
140
141/// Options for indexing a document.
142#[derive(Debug, Clone)]
143pub struct IndexOptions {
144    /// Indexing mode.
145    pub mode: IndexMode,
146
147    /// Whether to generate summaries using LLM.
148    pub generate_summaries: bool,
149
150    /// Whether to include node text in the tree.
151    pub include_text: bool,
152
153    /// Whether to generate node IDs.
154    pub generate_ids: bool,
155
156    /// Whether to generate document description.
157    pub generate_description: bool,
158}
159
160impl Default for IndexOptions {
161    fn default() -> Self {
162        Self {
163            mode: IndexMode::Auto,
164            generate_summaries: false, // Disabled by default, requires API key
165            include_text: true,
166            generate_ids: true,
167            generate_description: false,
168        }
169    }
170}
171
172impl IndexOptions {
173    /// Create new index options with defaults.
174    pub fn new() -> Self {
175        Self::default()
176    }
177
178    /// Enable summary generation.
179    pub fn with_summaries(mut self) -> Self {
180        self.generate_summaries = true;
181        self
182    }
183
184    /// Enable document description generation.
185    pub fn with_description(mut self) -> Self {
186        self.generate_description = true;
187        self
188    }
189}
190
191/// Result of a document query.
192#[derive(Debug, Clone)]
193pub struct QueryResult {
194    /// The document ID.
195    pub doc_id: String,
196
197    /// Matching node IDs.
198    pub node_ids: Vec<String>,
199
200    /// Retrieved content.
201    pub content: String,
202
203    /// Relevance score.
204    pub score: f32,
205}
206
207/// Document info for listing.
208#[derive(Debug, Clone, Serialize, Deserialize)]
209pub struct DocumentInfo {
210    /// Document ID.
211    pub id: String,
212
213    /// Document name.
214    pub name: String,
215
216    /// Document format.
217    pub format: String,
218
219    /// Document description.
220    pub description: Option<String>,
221
222    /// Page count (for PDFs).
223    pub page_count: Option<usize>,
224
225    /// Line count (for text files).
226    pub line_count: Option<usize>,
227}