-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathengine.rs
More file actions
252 lines (218 loc) · 7.82 KB
/
Copy pathengine.rs
File metadata and controls
252 lines (218 loc) · 7.82 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
// Copyright (c) 2026 vectorless developers
// SPDX-License-Identifier: Apache-2.0
//! Engine Python wrapper — async compile/forget/list_documents.
use pyo3::prelude::*;
use pyo3_async_runtimes::tokio::future_into_py;
use std::sync::Arc;
use tokio::runtime::Runtime;
use ::vectorless_engine::{Engine, EngineBuilder, IngestInput};
use super::document::{PyDocument, PyDocumentInfo};
use super::error::VectorlessError;
use super::error::to_py_err;
use super::graph::PyDocumentGraph;
use super::metrics::PyMetricsReport;
// ============================================================
// Engine async helpers (named functions to avoid FnOnce HRTB issue)
// ============================================================
async fn run_compile(engine: Arc<Engine>, input: IngestInput) -> PyResult<PyDocumentInfo> {
let doc = engine.compile(input).await.map_err(to_py_err)?;
Ok(PyDocumentInfo { inner: doc })
}
async fn run_forget(engine: Arc<Engine>, doc_id: String) -> PyResult<()> {
engine.forget(&doc_id).await.map_err(to_py_err)
}
async fn run_list_documents(engine: Arc<Engine>) -> PyResult<Vec<PyDocumentInfo>> {
let docs = engine.list_documents().await.map_err(to_py_err)?;
Ok(docs
.into_iter()
.map(|d| PyDocumentInfo { inner: d })
.collect())
}
async fn run_exists(engine: Arc<Engine>, doc_id: String) -> PyResult<bool> {
engine.exists(&doc_id).await.map_err(to_py_err)
}
async fn run_clear(engine: Arc<Engine>) -> PyResult<usize> {
engine.clear().await.map_err(to_py_err)
}
async fn run_load_document(engine: Arc<Engine>, doc_id: String) -> PyResult<PyDocument> {
let doc = engine.load_document(&doc_id).await.map_err(to_py_err)?;
match doc {
Some(d) => {
let navigator = vectorless_primitives::DocumentNavigator::new(d);
Ok(PyDocument::from_navigator(navigator))
}
None => Err(PyErr::from(VectorlessError::new(
format!("Document not found: {doc_id}"),
"navigation",
))),
}
}
async fn run_list_document_ids(engine: Arc<Engine>) -> PyResult<Vec<String>> {
engine.list_document_ids().await.map_err(to_py_err)
}
async fn run_get_graph(engine: Arc<Engine>) -> PyResult<Option<PyDocumentGraph>> {
let graph = engine.get_graph().await.map_err(to_py_err)?;
Ok(graph.map(|g| PyDocumentGraph { inner: g }))
}
fn run_metrics_report(engine: Arc<Engine>) -> PyMetricsReport {
PyMetricsReport {
inner: engine.metrics_report(),
}
}
// ============================================================
// Engine
// ============================================================
/// The vectorless Document Understanding Engine.
///
/// All methods are **async** — use `await` to call them.
///
/// ```python
/// from vectorless import Engine
///
/// engine = Engine(api_key="sk-...", model="gpt-4o")
///
/// # Compile a document
/// doc = await engine.compile("./report.pdf")
/// print(doc.summary)
///
/// # List all understood documents
/// docs = await engine.list_documents()
///
/// # Forget a document
/// await engine.forget(doc.doc_id)
/// ```
#[pyclass(name = "Engine", skip_from_py_object)]
pub struct PyEngine {
inner: Arc<Engine>,
}
#[pymethods]
impl PyEngine {
/// Create a new Engine.
///
/// Args:
/// api_key: **Required**. LLM API key.
/// model: **Required**. LLM model name.
/// endpoint: Optional API endpoint.
/// config: Optional Config for advanced tuning.
///
/// Raises:
/// VectorlessError: If engine creation fails.
#[new]
#[pyo3(signature = (api_key=None, model=None, endpoint=None, config=None))]
fn new(
api_key: Option<String>,
model: Option<String>,
endpoint: Option<String>,
config: Option<PyRef<super::config::PyConfig>>,
) -> PyResult<Self> {
let rt = Runtime::new().map_err(|e| {
PyErr::from(VectorlessError::new(
format!("Failed to create tokio runtime: {}", e),
"config",
))
})?;
let rust_config = config.map(|c| c.inner.clone());
let engine = rt.block_on(async {
let mut builder = EngineBuilder::new();
if let Some(config) = rust_config {
builder = builder.with_config(config);
}
if let Some(m) = &model {
builder = builder.with_model(m);
}
if let Some(e) = &endpoint {
builder = builder.with_endpoint(e);
}
if let Some(key) = api_key {
builder = builder.with_key(key);
}
builder.build().await
});
let engine = engine.map_err(|e| {
PyErr::from(VectorlessError::new(
format!("Failed to create engine: {}", e),
"config",
))
})?;
Ok(Self {
inner: Arc::new(engine),
})
}
/// Compile a document — parse, analyze, and persist.
///
/// Args:
/// path: File path to the document (PDF or Markdown).
///
/// Returns:
/// DocumentInfo with doc_id, summary, structure, concepts.
///
/// Raises:
/// VectorlessError: If compilation fails.
fn compile<'py>(&self, py: Python<'py>, path: String) -> PyResult<Bound<'py, PyAny>> {
let engine = Arc::clone(&self.inner);
let input = IngestInput::Path(path.into());
future_into_py(py, run_compile(engine, input))
}
/// Remove a document by ID.
///
/// Args:
/// doc_id: The document ID to remove.
///
/// Raises:
/// VectorlessError: If removal fails.
fn forget<'py>(&self, py: Python<'py>, doc_id: String) -> PyResult<Bound<'py, PyAny>> {
let engine = Arc::clone(&self.inner);
future_into_py(py, run_forget(engine, doc_id))
}
/// List all understood documents.
///
/// Returns:
/// List of DocumentInfo objects with summary, structure, and concepts.
fn list_documents<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
let engine = Arc::clone(&self.inner);
future_into_py(py, run_list_documents(engine))
}
/// Check if a document exists.
fn exists<'py>(&self, py: Python<'py>, doc_id: String) -> PyResult<Bound<'py, PyAny>> {
let engine = Arc::clone(&self.inner);
future_into_py(py, run_exists(engine, doc_id))
}
/// Load a full navigable Document by ID.
///
/// Returns a Document with navigation methods (ls, cd, cat, grep, find, etc.).
///
/// Raises:
/// VectorlessError: If the document is not found or loading fails.
fn load_document<'py>(&self, py: Python<'py>, doc_id: String) -> PyResult<Bound<'py, PyAny>> {
let engine = Arc::clone(&self.inner);
future_into_py(py, run_load_document(engine, doc_id))
}
/// List all document IDs in the workspace.
fn list_document_ids<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
let engine = Arc::clone(&self.inner);
future_into_py(py, run_list_document_ids(engine))
}
/// Remove all documents.
///
/// Returns:
/// Number of documents removed.
fn clear<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
let engine = Arc::clone(&self.inner);
future_into_py(py, run_clear(engine))
}
/// Get the cross-document relationship graph.
///
/// Returns:
/// DocumentGraph if any documents exist, else None.
fn get_graph<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
let engine = Arc::clone(&self.inner);
future_into_py(py, run_get_graph(engine))
}
/// Generate a complete metrics report.
fn metrics_report(&self) -> PyMetricsReport {
run_metrics_report(Arc::clone(&self.inner))
}
fn __repr__(&self) -> String {
"Engine(...)".to_string()
}
}