converge-provider 3.7.0

LLM provider implementations for Converge
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
// Copyright 2024-2026 Reflective Labs
// SPDX-License-Identifier: MIT
// See LICENSE file in the project root for full license information.

//! `LanceDB` embedded vector store implementation.
//!
//! `LanceDB` is an embedded vector database built on Apache Arrow, providing
//! fast vector similarity search without requiring a separate server process.
//!
//! # Example
//!
//! ```ignore
//! use converge_provider::vector::LanceStore;
//! use converge_core::capability::{VectorRecall, VectorRecord, VectorQuery};
//!
//! let store = LanceStore::new("/tmp/vectors", "embeddings", 1024)?;
//!
//! store.upsert(&VectorRecord {
//!     id: "doc-1".into(),
//!     vector: vec![0.1; 1024],
//!     payload: serde_json::json!({"title": "Hello"}),
//! })?;
//!
//! let matches = store.query(&VectorQuery::new(vec![0.1; 1024], 10))?;
//! ```

use arrow_array::{
    Array, ArrayRef, FixedSizeListArray, Float32Array, RecordBatch, RecordBatchIterator,
    StringArray,
};
use arrow_schema::{DataType, Field, Schema as ArrowSchema};
use converge_core::capability::{
    CapabilityError, VectorMatch, VectorQuery, VectorRecall, VectorRecord,
};
use futures::TryStreamExt;
use lancedb::query::{ExecutableQuery, QueryBase};
use std::sync::{Arc, RwLock};
use tracing::debug;

/// `LanceDB` embedded vector store.
///
/// This store uses `LanceDB` for fast vector similarity search with an
/// embedded database (no separate server required).
///
/// # Thread Safety
///
/// This store is thread-safe and can be shared across threads.
///
/// # Storage
///
/// Data is stored on disk at the specified path. The store creates
/// a single table for all vectors.
pub struct LanceStore {
    /// `LanceDB` connection.
    db: lancedb::Connection,
    /// Cached table reference.
    table: RwLock<Option<lancedb::Table>>,
    /// Table name.
    table_name: String,
    /// Vector dimensions (must match all inserted vectors).
    vector_dim: usize,
    /// Tokio runtime handle for async operations.
    runtime: tokio::runtime::Handle,
    /// Owned runtime (kept alive when we create our own).
    _owned_runtime: Option<tokio::runtime::Runtime>,
}

impl LanceStore {
    /// Creates a new `LanceDB` vector store.
    ///
    /// # Arguments
    ///
    /// * `path` - Directory path for the database files
    /// * `table_name` - Name of the table to store vectors
    /// * `vector_dim` - Dimensionality of vectors (e.g., 1024 for BGE-large)
    ///
    /// # Errors
    ///
    /// Returns error if the database cannot be opened.
    pub fn new(
        path: impl Into<String>,
        table_name: impl Into<String>,
        vector_dim: usize,
    ) -> Result<Self, CapabilityError> {
        let path = path.into();
        let table_name = table_name.into();

        // Get or create a tokio runtime handle
        let (runtime, owned_runtime) = if let Ok(handle) = tokio::runtime::Handle::try_current() {
            (handle, None)
        } else {
            // No runtime exists, create one and keep it alive
            let rt = tokio::runtime::Runtime::new().expect("Failed to create tokio runtime");
            let handle = rt.handle().clone();
            (handle, Some(rt))
        };

        // Connect to LanceDB
        let db = runtime
            .block_on(lancedb::connect(&path).execute())
            .map_err(|e| CapabilityError::store(format!("Failed to connect to LanceDB: {e}")))?;

        debug!(path = %path, table = %table_name, dim = vector_dim, "Connected to LanceDB");

        Ok(Self {
            db,
            table: RwLock::new(None),
            table_name,
            vector_dim,
            runtime,
            _owned_runtime: owned_runtime,
        })
    }

    /// Creates a new store with a provided runtime handle.
    ///
    /// Use this when you have an existing tokio runtime.
    pub fn with_runtime(
        path: impl Into<String>,
        table_name: impl Into<String>,
        vector_dim: usize,
        runtime: tokio::runtime::Handle,
    ) -> Result<Self, CapabilityError> {
        let path = path.into();
        let table_name = table_name.into();

        let db = runtime
            .block_on(lancedb::connect(&path).execute())
            .map_err(|e| CapabilityError::store(format!("Failed to connect to LanceDB: {e}")))?;

        Ok(Self {
            db,
            table: RwLock::new(None),
            table_name,
            vector_dim,
            runtime,
            _owned_runtime: None,
        })
    }

    /// Returns the vector dimensionality.
    #[must_use]
    pub fn vector_dim(&self) -> usize {
        self.vector_dim
    }

    /// Returns the table name.
    #[must_use]
    pub fn table_name(&self) -> &str {
        &self.table_name
    }

    /// Gets or creates the table.
    fn get_or_create_table(&self) -> Result<lancedb::Table, CapabilityError> {
        // Check if we have a cached table
        {
            let guard = self.table.read().expect("Lock poisoned");
            if let Some(ref table) = *guard {
                return Ok(table.clone());
            }
        }

        // Need to create or open the table
        let mut guard = self.table.write().expect("Lock poisoned");

        // Double-check after acquiring write lock
        if let Some(ref table) = *guard {
            return Ok(table.clone());
        }

        // Check if table exists
        let table_names: Vec<String> = self
            .runtime
            .block_on(self.db.table_names().execute())
            .map_err(|e| CapabilityError::store(format!("Failed to list tables: {e}")))?;

        let table = if table_names.contains(&self.table_name) {
            // Open existing table
            self.runtime
                .block_on(self.db.open_table(&self.table_name).execute())
                .map_err(|e| CapabilityError::store(format!("Failed to open table: {e}")))?
        } else {
            // Create new table with schema
            let schema = self.create_arrow_schema();
            let empty_batch = self.create_empty_batch(&schema)?;
            let batches = RecordBatchIterator::new(vec![Ok(empty_batch)], Arc::clone(&schema));

            self.runtime
                .block_on(
                    self.db
                        .create_table(&self.table_name, Box::new(batches))
                        .execute(),
                )
                .map_err(|e| CapabilityError::store(format!("Failed to create table: {e}")))?
        };

        *guard = Some(table.clone());
        Ok(table)
    }

    /// Creates the Arrow schema for the table.
    fn create_arrow_schema(&self) -> Arc<ArrowSchema> {
        let vector_field = Field::new("item", DataType::Float32, true);
        Arc::new(ArrowSchema::new(vec![
            Field::new("id", DataType::Utf8, false),
            Field::new(
                "vector",
                DataType::FixedSizeList(Arc::new(vector_field), self.vector_dim as i32),
                false,
            ),
            Field::new("payload", DataType::Utf8, true),
        ]))
    }

    /// Creates an empty batch for table initialization.
    fn create_empty_batch(
        &self,
        schema: &Arc<ArrowSchema>,
    ) -> Result<RecordBatch, CapabilityError> {
        let ids: Vec<&str> = vec![];
        let id_array = StringArray::from(ids.clone());

        let vector_field = Arc::new(Field::new("item", DataType::Float32, true));
        let empty_values = Float32Array::from(Vec::<f32>::new());
        let vector_array = FixedSizeListArray::try_new(
            vector_field,
            self.vector_dim as i32,
            Arc::new(empty_values),
            None,
        )
        .map_err(|e| CapabilityError::store(format!("Failed to create vector array: {e}")))?;

        let payload_array = StringArray::from(ids);

        RecordBatch::try_new(
            Arc::clone(schema),
            vec![
                Arc::new(id_array),
                Arc::new(vector_array),
                Arc::new(payload_array),
            ],
        )
        .map_err(|e| CapabilityError::store(format!("Failed to create batch: {e}")))
    }

    /// Converts records to a `RecordBatch`.
    fn records_to_batch(&self, records: &[VectorRecord]) -> Result<RecordBatch, CapabilityError> {
        if records.is_empty() {
            return Err(CapabilityError::invalid_input("No records to insert"));
        }

        // Validate vector dimensions
        for record in records {
            if record.vector.len() != self.vector_dim {
                return Err(CapabilityError::invalid_input(format!(
                    "Vector dimension mismatch: expected {}, got {}",
                    self.vector_dim,
                    record.vector.len()
                )));
            }
        }

        let schema = self.create_arrow_schema();

        // Build arrays
        let ids: Vec<&str> = records.iter().map(|r| r.id.as_str()).collect();
        let id_array = StringArray::from(ids);

        // Flatten all vectors into a single array
        let all_values: Vec<f32> = records
            .iter()
            .flat_map(|r| r.vector.iter().copied())
            .collect();
        let values_array = Float32Array::from(all_values);

        let vector_field = Arc::new(Field::new("item", DataType::Float32, true));
        let vector_array = FixedSizeListArray::try_new(
            vector_field,
            self.vector_dim as i32,
            Arc::new(values_array),
            None,
        )
        .map_err(|e| CapabilityError::store(format!("Failed to create vector array: {e}")))?;

        // Serialize payloads to JSON strings
        let payloads: Vec<String> = records
            .iter()
            .map(|r| serde_json::to_string(&r.payload).unwrap_or_else(|_| "{}".to_string()))
            .collect();
        let payload_array = StringArray::from(
            payloads
                .iter()
                .map(std::string::String::as_str)
                .collect::<Vec<_>>(),
        );

        RecordBatch::try_new(
            schema,
            vec![
                Arc::new(id_array),
                Arc::new(vector_array),
                Arc::new(payload_array),
            ],
        )
        .map_err(|e| CapabilityError::store(format!("Failed to create batch: {e}")))
    }
}

impl VectorRecall for LanceStore {
    fn name(&self) -> &'static str {
        "lancedb"
    }

    fn upsert(&self, record: &VectorRecord) -> Result<(), CapabilityError> {
        self.upsert_batch(std::slice::from_ref(record))
    }

    fn upsert_batch(&self, records: &[VectorRecord]) -> Result<(), CapabilityError> {
        if records.is_empty() {
            return Ok(());
        }

        let table = self.get_or_create_table()?;
        let batch = self.records_to_batch(records)?;

        // For upsert, we use merge_insert
        let schema = batch.schema();

        self.runtime
            .block_on(async {
                let batches = RecordBatchIterator::new(vec![Ok(batch)], schema);
                let mut op = table.merge_insert(&["id"]);
                op.when_matched_update_all(None);
                op.when_not_matched_insert_all();
                op.execute(Box::new(batches)).await
            })
            .map_err(|e| CapabilityError::store(format!("Failed to upsert: {e}")))?;

        debug!(count = records.len(), "Upserted records to LanceDB");
        Ok(())
    }

    fn query(&self, query: &VectorQuery) -> Result<Vec<VectorMatch>, CapabilityError> {
        if query.vector.len() != self.vector_dim {
            return Err(CapabilityError::invalid_input(format!(
                "Query vector dimension mismatch: expected {}, got {}",
                self.vector_dim,
                query.vector.len()
            )));
        }

        let table = self.get_or_create_table()?;

        let results: Vec<RecordBatch> = self.runtime.block_on(async {
            table
                .query()
                .nearest_to(query.vector.as_slice())
                .map_err(|e| CapabilityError::store(format!("Query setup failed: {e}")))?
                .limit(query.top_k)
                .execute()
                .await
                .map_err(|e| CapabilityError::store(format!("Query failed: {e}")))?
                .try_collect::<Vec<RecordBatch>>()
                .await
                .map_err(|e| CapabilityError::store(format!("Failed to collect results: {e}")))
        })?;

        let mut matches = Vec::new();

        for batch in results {
            let id_col: &StringArray = batch
                .column_by_name("id")
                .and_then(|c: &ArrayRef| c.as_any().downcast_ref::<StringArray>())
                .ok_or_else(|| CapabilityError::store("Missing id column"))?;

            let score_col: &Float32Array = batch
                .column_by_name("_distance")
                .and_then(|c: &ArrayRef| c.as_any().downcast_ref::<Float32Array>())
                .ok_or_else(|| CapabilityError::store("Missing _distance column"))?;

            let payload_col: Option<&StringArray> = batch
                .column_by_name("payload")
                .and_then(|c: &ArrayRef| c.as_any().downcast_ref::<StringArray>());

            for i in 0..batch.num_rows() {
                let id = id_col.value(i).to_string();
                // LanceDB returns distance, convert to similarity (1 - distance for L2, or use directly for cosine)
                let distance = f64::from(score_col.value(i));
                // For cosine distance, similarity = 1 - distance
                let score = 1.0 - distance;

                // Apply min_score filter
                if let Some(min_score) = query.min_score
                    && score < min_score
                {
                    continue;
                }

                let payload = payload_col
                    .map(|col: &StringArray| col.value(i))
                    .and_then(|s| serde_json::from_str(s).ok())
                    .unwrap_or(serde_json::Value::Null);

                matches.push(VectorMatch { id, score, payload });
            }
        }

        // Sort by score descending (highest similarity first)
        matches.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        matches.truncate(query.top_k);

        debug!(count = matches.len(), "Query returned matches");
        Ok(matches)
    }

    fn delete(&self, id: &str) -> Result<(), CapabilityError> {
        let table = self.get_or_create_table()?;

        self.runtime
            .block_on(table.delete(&format!("id = '{id}'")))
            .map_err(|e| CapabilityError::store(format!("Failed to delete: {e}")))?;

        debug!(id = %id, "Deleted record from LanceDB");
        Ok(())
    }

    fn clear(&self) -> Result<(), CapabilityError> {
        // Drop and recreate the table
        let table_names: Vec<String> = self
            .runtime
            .block_on(self.db.table_names().execute())
            .map_err(|e| CapabilityError::store(format!("Failed to list tables: {e}")))?;

        if table_names.contains(&self.table_name) {
            self.runtime
                .block_on(self.db.drop_table(&self.table_name, &[]))
                .map_err(|e| CapabilityError::store(format!("Failed to drop table: {e}")))?;
        }

        // Clear cached table reference
        {
            let mut guard = self.table.write().expect("Lock poisoned");
            *guard = None;
        }

        debug!(table = %self.table_name, "Cleared LanceDB table");
        Ok(())
    }

    fn count(&self) -> Result<usize, CapabilityError> {
        let table = self.get_or_create_table()?;

        let count = self
            .runtime
            .block_on(table.count_rows(None))
            .map_err(|e| CapabilityError::store(format!("Failed to count rows: {e}")))?;

        Ok(count)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;
    use tempfile::TempDir;

    fn create_test_store() -> (LanceStore, TempDir) {
        let temp_dir = TempDir::new().unwrap();
        let store = LanceStore::new(temp_dir.path().to_str().unwrap(), "test_vectors", 3).unwrap();
        (store, temp_dir)
    }

    #[test]
    fn test_store_creation() {
        let (store, _dir) = create_test_store();
        assert_eq!(store.name(), "lancedb");
        assert_eq!(store.vector_dim(), 3);
        assert_eq!(store.table_name(), "test_vectors");
    }

    #[test]
    fn test_upsert_and_count() {
        let (store, _dir) = create_test_store();

        store
            .upsert(&VectorRecord {
                id: "doc-1".into(),
                vector: vec![1.0, 0.0, 0.0],
                payload: json!({"title": "Document 1"}),
            })
            .unwrap();

        assert_eq!(store.count().unwrap(), 1);

        store
            .upsert(&VectorRecord {
                id: "doc-2".into(),
                vector: vec![0.0, 1.0, 0.0],
                payload: json!({"title": "Document 2"}),
            })
            .unwrap();

        assert_eq!(store.count().unwrap(), 2);
    }

    #[test]
    fn test_upsert_overwrites() {
        let (store, _dir) = create_test_store();

        store
            .upsert(&VectorRecord {
                id: "doc-1".into(),
                vector: vec![1.0, 0.0, 0.0],
                payload: json!({"version": 1}),
            })
            .unwrap();

        store
            .upsert(&VectorRecord {
                id: "doc-1".into(),
                vector: vec![0.0, 1.0, 0.0],
                payload: json!({"version": 2}),
            })
            .unwrap();

        // Should still be 1 record (upsert, not insert)
        assert_eq!(store.count().unwrap(), 1);
    }

    #[test]
    fn test_query() {
        let (store, _dir) = create_test_store();

        store
            .upsert_batch(&[
                VectorRecord {
                    id: "doc-1".into(),
                    vector: vec![1.0, 0.0, 0.0],
                    payload: json!({"title": "Doc 1"}),
                },
                VectorRecord {
                    id: "doc-2".into(),
                    vector: vec![0.9, 0.1, 0.0],
                    payload: json!({"title": "Doc 2"}),
                },
                VectorRecord {
                    id: "doc-3".into(),
                    vector: vec![0.0, 1.0, 0.0],
                    payload: json!({"title": "Doc 3"}),
                },
            ])
            .unwrap();

        let matches = store
            .query(&VectorQuery::new(vec![1.0, 0.0, 0.0], 2))
            .unwrap();

        assert_eq!(matches.len(), 2);
        // First match should be doc-1 (exact match)
        assert_eq!(matches[0].id, "doc-1");
        // Second should be doc-2 (close match)
        assert_eq!(matches[1].id, "doc-2");
    }

    #[test]
    fn test_query_with_min_score() {
        let (store, _dir) = create_test_store();

        store
            .upsert_batch(&[
                VectorRecord {
                    id: "close".into(),
                    vector: vec![0.95, 0.05, 0.0],
                    payload: json!({}),
                },
                VectorRecord {
                    id: "far".into(),
                    vector: vec![0.0, 0.0, 1.0],
                    payload: json!({}),
                },
            ])
            .unwrap();

        let matches = store
            .query(&VectorQuery::new(vec![1.0, 0.0, 0.0], 10).with_min_score(0.5))
            .unwrap();

        // Only the close match should pass the threshold
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].id, "close");
    }

    #[test]
    fn test_delete() {
        let (store, _dir) = create_test_store();

        store
            .upsert_batch(&[
                VectorRecord {
                    id: "doc-1".into(),
                    vector: vec![1.0, 0.0, 0.0],
                    payload: json!({}),
                },
                VectorRecord {
                    id: "doc-2".into(),
                    vector: vec![0.0, 1.0, 0.0],
                    payload: json!({}),
                },
            ])
            .unwrap();

        assert_eq!(store.count().unwrap(), 2);

        store.delete("doc-1").unwrap();
        assert_eq!(store.count().unwrap(), 1);
    }

    #[test]
    fn test_clear() {
        let (store, _dir) = create_test_store();

        store
            .upsert_batch(&[
                VectorRecord {
                    id: "doc-1".into(),
                    vector: vec![1.0, 0.0, 0.0],
                    payload: json!({}),
                },
                VectorRecord {
                    id: "doc-2".into(),
                    vector: vec![0.0, 1.0, 0.0],
                    payload: json!({}),
                },
            ])
            .unwrap();

        assert_eq!(store.count().unwrap(), 2);

        store.clear().unwrap();
        assert_eq!(store.count().unwrap(), 0);
    }

    #[test]
    fn test_dimension_mismatch() {
        let (store, _dir) = create_test_store();

        let result = store.upsert(&VectorRecord {
            id: "bad".into(),
            vector: vec![1.0, 0.0], // Wrong dimension (2 instead of 3)
            payload: json!({}),
        });

        assert!(result.is_err());
    }
}