-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathresults.rs
More file actions
351 lines (296 loc) · 7.98 KB
/
Copy pathresults.rs
File metadata and controls
351 lines (296 loc) · 7.98 KB
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
// Copyright (c) 2026 vectorless developers
// SPDX-License-Identifier: Apache-2.0
//! Query and index result Python wrappers.
use pyo3::prelude::*;
use ::vectorless::IndexMetrics;
use ::vectorless::{FailedItem, IndexItem, IndexResult, QueryResult, QueryResultItem};
// ============================================================
// QueryResultItem
// ============================================================
/// A single document's query result.
#[pyclass(name = "QueryResultItem")]
pub struct PyQueryResultItem {
pub(crate) inner: QueryResultItem,
}
#[pymethods]
impl PyQueryResultItem {
/// The document ID.
#[getter]
fn doc_id(&self) -> &str {
&self.inner.doc_id
}
/// The retrieved content.
#[getter]
fn content(&self) -> &str {
&self.inner.content
}
/// Relevance score (0.0 to 1.0).
#[getter]
fn score(&self) -> f32 {
self.inner.score
}
/// Node IDs that matched.
#[getter]
fn node_ids(&self) -> Vec<String> {
self.inner.node_ids.clone()
}
fn __repr__(&self) -> String {
format!(
"QueryResultItem(doc_id='{}', score={:.2}, content_len={})",
self.inner.doc_id,
self.inner.score,
self.inner.content.len()
)
}
}
// ============================================================
// FailedItem
// ============================================================
/// A failed item in a batch operation.
#[pyclass(name = "FailedItem")]
pub struct PyFailedItem {
pub(crate) inner: FailedItem,
}
#[pymethods]
impl PyFailedItem {
/// Source description.
#[getter]
fn source(&self) -> &str {
&self.inner.source
}
/// Error message.
#[getter]
fn error(&self) -> &str {
&self.inner.error
}
fn __repr__(&self) -> String {
format!(
"FailedItem(source='{}', error='{}')",
self.inner.source, self.inner.error
)
}
}
// ============================================================
// QueryResult
// ============================================================
/// Result of a document query.
#[pyclass(name = "QueryResult")]
pub struct PyQueryResult {
pub(crate) inner: QueryResult,
}
#[pymethods]
impl PyQueryResult {
/// Result items (one per document).
#[getter]
fn items(&self) -> Vec<PyQueryResultItem> {
self.inner
.items
.iter()
.map(|i| PyQueryResultItem { inner: i.clone() })
.collect()
}
/// Get the first (single-doc) result item.
fn single(&self) -> Option<PyQueryResultItem> {
self.inner
.single()
.map(|i| PyQueryResultItem { inner: i.clone() })
}
/// Number of result items.
fn __len__(&self) -> usize {
self.inner.len()
}
/// Whether any documents failed.
fn has_failures(&self) -> bool {
self.inner.has_failures()
}
/// Failed items.
#[getter]
fn failed(&self) -> Vec<PyFailedItem> {
self.inner
.failed
.iter()
.map(|f| PyFailedItem { inner: f.clone() })
.collect()
}
fn __repr__(&self) -> String {
format!(
"QueryResult(items={}, failed={})",
self.inner.len(),
self.inner.failed.len()
)
}
}
// ============================================================
// IndexMetrics
// ============================================================
/// Indexing pipeline metrics.
#[pyclass(name = "IndexMetrics")]
pub struct PyIndexMetrics {
pub(crate) inner: IndexMetrics,
}
#[pymethods]
impl PyIndexMetrics {
/// Total indexing time (ms).
#[getter]
fn total_time_ms(&self) -> u64 {
self.inner.total_time_ms()
}
/// Parse stage duration (ms).
#[getter]
fn parse_time_ms(&self) -> u64 {
self.inner.parse_time_ms
}
/// Build stage duration (ms).
#[getter]
fn build_time_ms(&self) -> u64 {
self.inner.build_time_ms
}
/// Enhance (summary) stage duration (ms).
#[getter]
fn enhance_time_ms(&self) -> u64 {
self.inner.enhance_time_ms
}
/// Number of nodes processed.
#[getter]
fn nodes_processed(&self) -> usize {
self.inner.nodes_processed
}
/// Number of summaries successfully generated.
#[getter]
fn summaries_generated(&self) -> usize {
self.inner.summaries_generated
}
/// Number of summaries that failed to generate.
#[getter]
fn summaries_failed(&self) -> usize {
self.inner.summaries_failed
}
/// Number of LLM calls made.
#[getter]
fn llm_calls(&self) -> usize {
self.inner.llm_calls
}
/// Total tokens generated by LLM.
#[getter]
fn total_tokens_generated(&self) -> usize {
self.inner.total_tokens_generated
}
/// Number of topics in reasoning index.
#[getter]
fn topics_indexed(&self) -> usize {
self.inner.topics_indexed
}
/// Number of keywords in reasoning index.
#[getter]
fn keywords_indexed(&self) -> usize {
self.inner.keywords_indexed
}
fn __repr__(&self) -> String {
format!(
"IndexMetrics(total={}ms, summaries={}, failed={}, llm_calls={})",
self.inner.total_time_ms(),
self.inner.summaries_generated,
self.inner.summaries_failed,
self.inner.llm_calls,
)
}
}
// ============================================================
// IndexItem / IndexResult
// ============================================================
/// A single indexed document item.
#[pyclass(name = "IndexItem")]
pub struct PyIndexItem {
pub(crate) inner: IndexItem,
}
#[pymethods]
impl PyIndexItem {
#[getter]
fn doc_id(&self) -> &str {
&self.inner.doc_id
}
#[getter]
fn name(&self) -> &str {
&self.inner.name
}
#[getter]
fn format(&self) -> String {
format!("{:?}", self.inner.format).to_lowercase()
}
#[getter]
fn description(&self) -> Option<&str> {
self.inner.description.as_deref()
}
#[getter]
fn source_path(&self) -> Option<&str> {
self.inner.source_path.as_deref()
}
#[getter]
fn page_count(&self) -> Option<usize> {
self.inner.page_count
}
/// Indexing pipeline metrics (timing, LLM usage, etc.).
#[getter]
fn metrics(&self) -> Option<PyIndexMetrics> {
self.inner
.metrics
.as_ref()
.map(|m| PyIndexMetrics { inner: m.clone() })
}
fn __repr__(&self) -> String {
format!(
"IndexItem(doc_id='{}', name='{}')",
self.inner.doc_id, self.inner.name
)
}
}
/// Result of a document indexing operation.
#[pyclass(name = "IndexResult")]
pub struct PyIndexResult {
pub(crate) inner: IndexResult,
}
#[pymethods]
impl PyIndexResult {
/// The document ID (convenience for single-document indexing).
#[getter]
fn doc_id(&self) -> Option<String> {
self.inner.doc_id().map(|s| s.to_string())
}
/// All indexed items.
#[getter]
fn items(&self) -> Vec<PyIndexItem> {
self.inner
.items
.iter()
.map(|i| PyIndexItem { inner: i.clone() })
.collect()
}
/// Failed items.
#[getter]
fn failed(&self) -> Vec<PyFailedItem> {
self.inner
.failed
.iter()
.map(|f| PyFailedItem { inner: f.clone() })
.collect()
}
/// Whether any items failed.
fn has_failures(&self) -> bool {
self.inner.has_failures()
}
/// Total number of items (successful + failed).
fn total(&self) -> usize {
self.inner.total()
}
fn __len__(&self) -> usize {
self.inner.len()
}
fn __repr__(&self) -> String {
format!(
"IndexResult(doc_id={:?}, count={}, failed={})",
self.inner.doc_id(),
self.inner.items.len(),
self.inner.failed.len()
)
}
}