vectorlite 0.1.5

A high-performance, in-memory vector database optimized for AI agent workloads
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
//! # HTTP Server Module
//!
//! This module provides HTTP API endpoints for VectorLite, enabling easy integration
//! with AI agents and other services. The server uses Axum for high-performance
//! async request handling.
//!
//! # API Endpoints
//!
//! ## Health Check
//! - `GET /health` - Server health status
//!
//! ## Collection Management
//! - `GET /collections` - List all collections
//! - `POST /collections` - Create a new collection
//! - `DELETE /collections/{name}` - Delete a collection
//!
//! ## Vector Operations
//! - `POST /collections/{name}/text` - Add text (auto-generates embedding, optional metadata)
//! - `POST /collections/{name}/search/text` - Search by text
//! - `GET /collections/{name}/vectors/{id}` - Get vector by ID
//! - `DELETE /collections/{name}/vectors/{id}` - Delete vector by ID
//!
//! ## Persistence Operations
//! - `POST /collections/{name}/save` - Save collection to file
//! - `POST /collections/load` - Load collection from file
//!
//! ### Save Collection
//! ```bash
//! curl -X POST http://localhost:3001/collections/my_docs/save \
//!      -H 'Content-Type: application/json' \
//!      -d '{"file_path": "./my_docs.vlc"}'
//! ```
//!
//! ### Load Collection
//! ```bash
//! curl -X POST http://localhost:3001/collections/load \
//!      -H 'Content-Type: application/json' \
//!      -d '{"file_path": "./my_docs.vlc", "collection_name": "restored_docs"}'
//! ```
//!
//! # Examples
//!
//! ```rust,no_run
//! use vectorlite::{VectorLiteClient, EmbeddingGenerator, start_server};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let client = VectorLiteClient::new(Box::new(EmbeddingGenerator::new()?));
//!     start_server(client, "127.0.0.1", 3002).await?;
//!     Ok(())
//! }
//! ```

use axum::{
    extract::{Path, State},
    http::StatusCode,
    response::Json,
    routing::{get, post, delete},
    Router,
};
use std::path::PathBuf;
use serde::{Deserialize, Serialize};
use std::sync::{Arc, RwLock};
use tower_http::cors::CorsLayer;
use tower_http::trace::TraceLayer;
use tracing::{info, error};

use crate::{VectorLiteClient, SearchResult, SimilarityMetric, IndexType};
use crate::errors::{VectorLiteError, VectorLiteResult};

// Request/Response types
#[derive(Debug, Deserialize)]
pub struct CreateCollectionRequest {
    pub name: String,
    pub index_type: String, // "flat" or "hnsw"
}

#[derive(Debug, Serialize)]
pub struct CreateCollectionResponse {
    pub name: String,
}

#[derive(Debug, Deserialize)]
pub struct AddTextRequest {
    pub text: String,
    pub metadata: Option<serde_json::Value>,
}

#[derive(Debug, Serialize)]
pub struct AddTextResponse {
    pub id: Option<u64>,
}


#[derive(Debug, Deserialize)]
pub struct SearchTextRequest {
    pub query: String,
    pub k: Option<usize>,
    pub similarity_metric: Option<String>,
}


#[derive(Debug, Serialize)]
pub struct SearchResponse {
    pub results: Option<Vec<SearchResult>>,
}

#[derive(Debug, Serialize)]
pub struct CollectionInfoResponse {
    pub info: Option<crate::client::CollectionInfo>,
}

#[derive(Debug, Serialize)]
pub struct ListCollectionsResponse {
    pub collections: Vec<String>,
}

#[derive(Debug, Serialize)]
pub struct ErrorResponse {
    pub message: String,
}

#[derive(Debug, Deserialize)]
pub struct SaveCollectionRequest {
    pub file_path: String,
}

#[derive(Debug, Serialize)]
pub struct SaveCollectionResponse {
    pub file_path: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct LoadCollectionRequest {
    pub file_path: String,
    pub collection_name: Option<String>, // Optional: if not provided, uses name from file
}

#[derive(Debug, Serialize)]
pub struct LoadCollectionResponse {
    pub collection_name: Option<String>,
}

// App state
pub type AppState = Arc<RwLock<VectorLiteClient>>;

// Helper functions
fn parse_index_type(index_type: &str) -> VectorLiteResult<IndexType> {
    match index_type.to_lowercase().as_str() {
        "flat" => Ok(IndexType::Flat),
        "hnsw" => Ok(IndexType::HNSW),
        _ => Err(VectorLiteError::InvalidIndexType { index_type: index_type.to_string() }),
    }
}

fn parse_similarity_metric(metric: &str) -> VectorLiteResult<SimilarityMetric> {
    match metric.to_lowercase().as_str() {
        "cosine" => Ok(SimilarityMetric::Cosine),
        "euclidean" => Ok(SimilarityMetric::Euclidean),
        "manhattan" => Ok(SimilarityMetric::Manhattan),
        "dotproduct" => Ok(SimilarityMetric::DotProduct),
        _ => Err(VectorLiteError::InvalidSimilarityMetric { metric: metric.to_string() }),
    }
}

// Handlers
async fn health_check() -> Json<serde_json::Value> {
    Json(serde_json::json!({
        "status": "healthy",
        "service": "vectorlite"
    }))
}

async fn list_collections(
    State(state): State<AppState>,
) -> Result<Json<ListCollectionsResponse>, StatusCode> {
    let client = state.read().map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
    let collections = client.list_collections();
    Ok(Json(ListCollectionsResponse {
        collections,
    }))
}

async fn create_collection(
    State(state): State<AppState>,
    Json(payload): Json<CreateCollectionRequest>,
) -> Result<Json<CreateCollectionResponse>, StatusCode> {
    let index_type = match parse_index_type(&payload.index_type) {
        Ok(t) => t,
        Err(e) => {
            return Err(e.status_code());
        }
    };

    let mut client = state.write().map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
    match client.create_collection(&payload.name, index_type) {
        Ok(_) => {
            info!("Created collection: {}", payload.name);
            Ok(Json(CreateCollectionResponse {
                name: payload.name,
            }))
        }
        Err(e) => {
            error!("Failed to create collection '{}': {}", payload.name, e);
            Err(e.status_code())
        }
    }
}

async fn get_collection_info(
    State(state): State<AppState>,
    Path(collection_name): Path<String>,
) -> Result<Json<CollectionInfoResponse>, StatusCode> {
    let client = state.read().map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
    match client.get_collection_info(&collection_name) {
        Ok(info) => Ok(Json(CollectionInfoResponse {
            info: Some(info),
        })),
        Err(e) => Err(e.status_code()),
    }
}

async fn delete_collection(
    State(state): State<AppState>,
    Path(collection_name): Path<String>,
) -> Result<Json<CreateCollectionResponse>, StatusCode> {
    let mut client = state.write().map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
    match client.delete_collection(&collection_name) {
        Ok(_) => {
            info!("Deleted collection: {}", collection_name);
            Ok(Json(CreateCollectionResponse {
                name: collection_name,
            }))
        }
        Err(e) => Err(e.status_code()),
    }
}

async fn add_text(
    State(state): State<AppState>,
    Path(collection_name): Path<String>,
    Json(payload): Json<AddTextRequest>,
) -> Result<Json<AddTextResponse>, StatusCode> {
    let client = state.read().map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
    match client.add_text_to_collection(&collection_name, &payload.text, payload.metadata) {
        Ok(id) => {
            info!("Added text to collection '{}' with ID: {}", collection_name, id);
            Ok(Json(AddTextResponse {
                id: Some(id),
            }))
        }
        Err(e) => {
            Err(e.status_code())
        }
    }
}



async fn search_text(
    State(state): State<AppState>,
    Path(collection_name): Path<String>,
    Json(payload): Json<SearchTextRequest>,
) -> Result<Json<SearchResponse>, StatusCode> {
    let k = payload.k.unwrap_or(10);
    let similarity_metric = match payload.similarity_metric {
        Some(metric) => match parse_similarity_metric(&metric) {
            Ok(m) => m,
            Err(e) => {
                return Err(e.status_code());
            }
        },
        None => SimilarityMetric::Cosine,
    };

    let client = state.read().map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
    match client.search_text_in_collection(&collection_name, &payload.query, k, similarity_metric) {
        Ok(results) => {
            info!("Search completed for collection '{}' with {} results", collection_name, results.len());
            Ok(Json(SearchResponse {
                results: Some(results),
            }))
        }
        Err(e) => Err(e.status_code()),
    }
}


async fn get_vector(
    State(state): State<AppState>,
    Path((collection_name, vector_id)): Path<(String, u64)>,
) -> Result<Json<serde_json::Value>, StatusCode> {
    let client = state.read().map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
    match client.get_vector_from_collection(&collection_name, vector_id) {
        Ok(Some(vector)) => {
            Ok(Json(serde_json::json!({
                "vector": vector
            })))
        }
        Ok(None) => {
            Err(StatusCode::NOT_FOUND)
        }
        Err(e) => {
            Err(e.status_code())
        }
    }
}

async fn delete_vector(
    State(state): State<AppState>,
    Path((collection_name, vector_id)): Path<(String, u64)>,
) -> Result<Json<serde_json::Value>, StatusCode> {
    let client = state.read().map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
    match client.delete_from_collection(&collection_name, vector_id) {
        Ok(_) => {
            info!("Deleted vector {} from collection '{}'", vector_id, collection_name);
            Ok(Json(serde_json::json!({})))
        }
        Err(e) => {
            Err(e.status_code())
        }
    }
}

async fn save_collection(
    State(state): State<AppState>,
    Path(collection_name): Path<String>,
    Json(payload): Json<SaveCollectionRequest>,
) -> Result<Json<SaveCollectionResponse>, StatusCode> {
    let client = state.read().map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
    
    // Get the collection
    let collection = match client.get_collection(&collection_name) {
        Some(collection) => collection,
        None => {
            return Err(StatusCode::NOT_FOUND);
        }
    };

    // Convert file path to PathBuf
    let file_path = PathBuf::from(&payload.file_path);
    
    // Save the collection
    match collection.save_to_file(&file_path) {
        Ok(_) => {
            info!("Saved collection '{}' to file: {}", collection_name, payload.file_path);
            Ok(Json(SaveCollectionResponse {
                file_path: Some(payload.file_path),
            }))
        }
        Err(_) => {
            Err(StatusCode::INTERNAL_SERVER_ERROR)
        }
    }
}

async fn load_collection(
    State(state): State<AppState>,
    Json(payload): Json<LoadCollectionRequest>,
) -> Result<Json<LoadCollectionResponse>, StatusCode> {
    // Convert file path to PathBuf
    let file_path = PathBuf::from(&payload.file_path);
    
    // Load the collection from file
    let collection = match crate::Collection::load_from_file(&file_path) {
        Ok(collection) => collection,
        Err(e) => {
            // Check if it's a file not found error
            if let crate::persistence::PersistenceError::Io(io_err) = &e
                && io_err.kind() == std::io::ErrorKind::NotFound {
                return Err(VectorLiteError::FileNotFound(format!("File not found: {}", payload.file_path)).status_code());
            }
            return Err(VectorLiteError::from(e).status_code());
        }
    };

    // Determine the collection name to use
    let collection_name = payload.collection_name.unwrap_or_else(|| collection.name().to_string());
    
    // Add the collection to the client
    let mut client = state.write().map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
    
    // Check if collection already exists
    if client.has_collection(&collection_name) {
        return Err(StatusCode::CONFLICT);
    }

    // Extract the index from the loaded collection
    let index = {
        let index_guard = collection.index_read().map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
        (*index_guard).clone()
    };
    
    // Create a new collection with the loaded data
    let new_collection = crate::Collection::new(collection_name.clone(), index);
    
    // Add the collection to the client
    if client.add_collection(new_collection).is_err() {
        return Err(StatusCode::INTERNAL_SERVER_ERROR);
    }
    
    info!("Loaded collection '{}' from file: {}", collection_name, payload.file_path);
    Ok(Json(LoadCollectionResponse {
        collection_name: Some(collection_name),
    }))
}

pub fn create_app(state: AppState) -> Router {
    Router::new()
        .route("/health", get(health_check))
        .route("/collections", get(list_collections))
        .route("/collections", post(create_collection))
        .route("/collections/:name", get(get_collection_info))
        .route("/collections/:name", delete(delete_collection))
        .route("/collections/:name/text", post(add_text))
        .route("/collections/:name/search/text", post(search_text))
        .route("/collections/:name/vectors/:id", get(get_vector))
        .route("/collections/:name/vectors/:id", delete(delete_vector))
        .route("/collections/:name/save", post(save_collection))
        .route("/collections/load", post(load_collection))
        .layer(CorsLayer::permissive())
        .layer(TraceLayer::new_for_http())
        .with_state(state)
}

pub async fn start_server(
    client: VectorLiteClient,
    host: &str,
    port: u16,
) -> Result<(), Box<dyn std::error::Error>> {
    let app = create_app(Arc::new(RwLock::new(client)));
    
    let listener = tokio::net::TcpListener::bind(format!("{}:{}", host, port)).await?;
    info!("VectorLite server starting on {}:{}", host, port);
    
    axum::serve(listener, app).await?;
    
    Ok(())
}