erio-context-store 0.1.0

Context and vector storage for Erio
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
//! Context store backed by `LanceDB`.

use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};

use arrow_array::types::Float32Type;
use arrow_array::{FixedSizeListArray, RecordBatch, RecordBatchIterator, StringArray};
use arrow_schema::{DataType, Field, Schema};
use futures::TryStreamExt;
use lancedb::query::{ExecutableQuery, QueryBase};

use crate::config::{ContextConfig, SearchResult, StorageStats};
use crate::error::ContextStoreError;

static ID_COUNTER: AtomicU64 = AtomicU64::new(0);

fn generate_id() -> String {
    let count = ID_COUNTER.fetch_add(1, Ordering::Relaxed);
    let nanos = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_nanos();
    format!("{nanos:x}-{count:x}")
}

pub struct ContextStore {
    table: lancedb::Table,
    embedding: Arc<dyn erio_embedding::EmbeddingEngine>,
}

impl ContextStore {
    pub async fn new(
        config: ContextConfig,
        embedding: Arc<dyn erio_embedding::EmbeddingEngine>,
    ) -> Result<Self, ContextStoreError> {
        let dims = embedding.dimensions();
        let schema = Arc::new(Schema::new(vec![
            Field::new("id", DataType::Utf8, false),
            Field::new("content", DataType::Utf8, false),
            Field::new("metadata", DataType::Utf8, false),
            Field::new(
                "vector",
                DataType::FixedSizeList(
                    Arc::new(Field::new("item", DataType::Float32, true)),
                    i32::try_from(dims).map_err(|e| {
                        ContextStoreError::InvalidInput(format!("bad dimensions: {e}"))
                    })?,
                ),
                false,
            ),
        ]));

        let db = lancedb::connect(config.path.to_string_lossy().as_ref())
            .execute()
            .await
            .map_err(|e| ContextStoreError::Storage(e.to_string()))?;

        let table = db
            .create_empty_table("context", schema)
            .execute()
            .await
            .map_err(|e| ContextStoreError::Storage(e.to_string()))?;

        Ok(Self { table, embedding })
    }

    pub async fn add(
        &self,
        content: &str,
        metadata: serde_json::Value,
    ) -> Result<String, ContextStoreError> {
        if content.is_empty() {
            return Err(ContextStoreError::InvalidInput(
                "content must not be empty".into(),
            ));
        }

        let id = generate_id();
        let vector = self
            .embedding
            .embed(content)
            .await
            .map_err(|e| ContextStoreError::Embedding(e.to_string()))?;

        let dims = i32::try_from(self.embedding.dimensions())
            .map_err(|e| ContextStoreError::InvalidInput(format!("bad dimensions: {e}")))?;

        let schema = self
            .table
            .schema()
            .await
            .map_err(|e| ContextStoreError::Storage(e.to_string()))?;

        let batch = RecordBatch::try_new(
            schema,
            vec![
                Arc::new(StringArray::from(vec![id.as_str()])),
                Arc::new(StringArray::from(vec![content])),
                Arc::new(StringArray::from(vec![metadata.to_string().as_str()])),
                Arc::new(
                    FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
                        vec![Some(vector.into_iter().map(Some).collect::<Vec<_>>())],
                        dims,
                    ),
                ),
            ],
        )
        .map_err(|e| ContextStoreError::Storage(e.to_string()))?;

        let batches = RecordBatchIterator::new(
            vec![Ok(batch)],
            self.table
                .schema()
                .await
                .map_err(|e| ContextStoreError::Storage(e.to_string()))?,
        );

        self.table
            .add(batches)
            .execute()
            .await
            .map_err(|e| ContextStoreError::Storage(e.to_string()))?;

        Ok(id)
    }

    pub async fn search(
        &self,
        query: &str,
        k: usize,
        filter: Option<String>,
    ) -> Result<Vec<SearchResult>, ContextStoreError> {
        let count = self
            .table
            .count_rows(None)
            .await
            .map_err(|e| ContextStoreError::Storage(e.to_string()))?;
        if count == 0 {
            return Ok(vec![]);
        }

        let vector = self
            .embedding
            .embed(query)
            .await
            .map_err(|e| ContextStoreError::Embedding(e.to_string()))?;

        let mut builder = self
            .table
            .query()
            .nearest_to(vector)
            .map_err(|e| ContextStoreError::Storage(e.to_string()))?
            .limit(k);

        if let Some(f) = filter {
            builder = builder.only_if(f);
        }

        let batches: Vec<RecordBatch> = builder
            .execute()
            .await
            .map_err(|e| ContextStoreError::Storage(e.to_string()))?
            .try_collect()
            .await
            .map_err(|e| ContextStoreError::Storage(e.to_string()))?;

        let mut results = Vec::new();
        for batch in &batches {
            let content_col = batch
                .column_by_name("content")
                .and_then(|c| c.as_any().downcast_ref::<StringArray>())
                .ok_or_else(|| ContextStoreError::Storage("missing content column".into()))?;

            let metadata_col = batch
                .column_by_name("metadata")
                .and_then(|c| c.as_any().downcast_ref::<StringArray>())
                .ok_or_else(|| ContextStoreError::Storage("missing metadata column".into()))?;

            let distance_col = batch
                .column_by_name("_distance")
                .and_then(|c| c.as_any().downcast_ref::<arrow_array::Float32Array>())
                .ok_or_else(|| ContextStoreError::Storage("missing _distance column".into()))?;

            for i in 0..batch.num_rows() {
                let meta_str = metadata_col.value(i);
                let metadata: serde_json::Value =
                    serde_json::from_str(meta_str).unwrap_or_default();

                results.push(SearchResult {
                    content: content_col.value(i).to_string(),
                    score: 1.0 - distance_col.value(i),
                    metadata,
                });
            }
        }

        Ok(results)
    }

    pub async fn stats(&self) -> Result<StorageStats, ContextStoreError> {
        let count = self
            .table
            .count_rows(None)
            .await
            .map_err(|e| ContextStoreError::Storage(e.to_string()))?;
        Ok(StorageStats {
            document_count: count,
        })
    }

    pub async fn delete(&self, id: &str) -> Result<(), ContextStoreError> {
        let escaped = id.replace('\'', "''");
        let filter = format!("id = '{escaped}'");

        let count_before = self
            .table
            .count_rows(Some(filter.clone()))
            .await
            .map_err(|e| ContextStoreError::Storage(e.to_string()))?;

        if count_before == 0 {
            return Err(ContextStoreError::NotFound(id.to_string()));
        }

        self.table
            .delete(&filter)
            .await
            .map_err(|e| ContextStoreError::Storage(e.to_string()))?;

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::ContextConfig;
    use crate::config::HnswConfig;
    use erio_embedding::EmbeddingError;
    use std::sync::Arc;

    struct FakeEmbedding;

    #[async_trait::async_trait]
    impl erio_embedding::EmbeddingEngine for FakeEmbedding {
        #[allow(clippy::unnecessary_literal_bound)]
        fn name(&self) -> &str {
            "fake"
        }

        fn dimensions(&self) -> usize {
            3
        }

        async fn embed(&self, text: &str) -> Result<Vec<f32>, EmbeddingError> {
            let bytes = text.as_bytes();
            Ok(vec![
                f32::from(*bytes.first().unwrap_or(&0)),
                f32::from(*bytes.get(1).unwrap_or(&0)),
                f32::from(*bytes.get(2).unwrap_or(&0)),
            ])
        }

        async fn embed_batch(&self, texts: &[&str]) -> Result<Vec<Vec<f32>>, EmbeddingError> {
            let mut results = Vec::with_capacity(texts.len());
            for text in texts {
                results.push(self.embed(text).await?);
            }
            Ok(results)
        }
    }

    fn test_config(dir: &tempfile::TempDir) -> ContextConfig {
        ContextConfig {
            path: dir.path().to_path_buf(),
            index: HnswConfig::default(),
        }
    }

    fn fake_embedding() -> Arc<dyn erio_embedding::EmbeddingEngine> {
        Arc::new(FakeEmbedding)
    }

    // === Construction ===

    #[tokio::test]
    async fn new_creates_empty_store() {
        let dir = tempfile::tempdir().unwrap();
        let store = ContextStore::new(test_config(&dir), fake_embedding())
            .await
            .unwrap();
        let stats = store.stats().await.unwrap();
        assert_eq!(stats.document_count, 0);
    }

    // === Add ===

    #[tokio::test]
    async fn add_returns_document_id() {
        let dir = tempfile::tempdir().unwrap();
        let store = ContextStore::new(test_config(&dir), fake_embedding())
            .await
            .unwrap();
        let id = store
            .add("hello world", serde_json::json!({}))
            .await
            .unwrap();
        assert!(!id.is_empty());
    }

    #[tokio::test]
    async fn add_increments_document_count() {
        let dir = tempfile::tempdir().unwrap();
        let store = ContextStore::new(test_config(&dir), fake_embedding())
            .await
            .unwrap();
        store.add("first", serde_json::json!({})).await.unwrap();
        store.add("second", serde_json::json!({})).await.unwrap();
        let stats = store.stats().await.unwrap();
        assert_eq!(stats.document_count, 2);
    }

    #[tokio::test]
    async fn add_rejects_empty_content() {
        let dir = tempfile::tempdir().unwrap();
        let store = ContextStore::new(test_config(&dir), fake_embedding())
            .await
            .unwrap();
        let result = store.add("", serde_json::json!({})).await;
        assert!(result.is_err());
    }

    // === Search ===

    #[tokio::test]
    async fn search_returns_matching_results() {
        let dir = tempfile::tempdir().unwrap();
        let store = ContextStore::new(test_config(&dir), fake_embedding())
            .await
            .unwrap();
        store
            .add("hello world", serde_json::json!({}))
            .await
            .unwrap();
        store
            .add("hello there", serde_json::json!({}))
            .await
            .unwrap();
        store.add("goodbye", serde_json::json!({})).await.unwrap();

        let results = store.search("hello", 2, None).await.unwrap();
        assert_eq!(results.len(), 2);
    }

    #[tokio::test]
    async fn search_returns_results_with_scores() {
        let dir = tempfile::tempdir().unwrap();
        let store = ContextStore::new(test_config(&dir), fake_embedding())
            .await
            .unwrap();
        store.add("hello", serde_json::json!({})).await.unwrap();

        let results = store.search("hello", 1, None).await.unwrap();
        assert_eq!(results.len(), 1);
        assert!(results[0].score >= 0.0);
    }

    #[tokio::test]
    async fn search_respects_k_limit() {
        let dir = tempfile::tempdir().unwrap();
        let store = ContextStore::new(test_config(&dir), fake_embedding())
            .await
            .unwrap();
        for i in 0..10 {
            store
                .add(&format!("document {i}"), serde_json::json!({}))
                .await
                .unwrap();
        }

        let results = store.search("document", 3, None).await.unwrap();
        assert_eq!(results.len(), 3);
    }

    #[tokio::test]
    async fn search_on_empty_store_returns_empty() {
        let dir = tempfile::tempdir().unwrap();
        let store = ContextStore::new(test_config(&dir), fake_embedding())
            .await
            .unwrap();
        let results = store.search("anything", 5, None).await.unwrap();
        assert!(results.is_empty());
    }

    // === Delete ===

    #[tokio::test]
    async fn delete_removes_document() {
        let dir = tempfile::tempdir().unwrap();
        let store = ContextStore::new(test_config(&dir), fake_embedding())
            .await
            .unwrap();
        let id = store
            .add("to be deleted", serde_json::json!({}))
            .await
            .unwrap();

        store.delete(&id).await.unwrap();

        let stats = store.stats().await.unwrap();
        assert_eq!(stats.document_count, 0);
    }

    #[tokio::test]
    async fn delete_nonexistent_returns_error() {
        let dir = tempfile::tempdir().unwrap();
        let store = ContextStore::new(test_config(&dir), fake_embedding())
            .await
            .unwrap();
        let result = store.delete("nonexistent_id").await;
        assert!(result.is_err());
    }

    // === Metadata Filtering ===

    #[tokio::test]
    async fn search_with_metadata_filter_narrows_results() {
        let dir = tempfile::tempdir().unwrap();
        let store = ContextStore::new(test_config(&dir), fake_embedding())
            .await
            .unwrap();
        store
            .add("hello alpha", serde_json::json!({"source": "docs"}))
            .await
            .unwrap();
        store
            .add("hello beta", serde_json::json!({"source": "code"}))
            .await
            .unwrap();
        store
            .add("hello gamma", serde_json::json!({"source": "docs"}))
            .await
            .unwrap();

        let filter = "metadata LIKE '%\"source\":\"docs\"%'".to_string();
        let results = store.search("hello", 10, Some(filter)).await.unwrap();
        assert_eq!(results.len(), 2);
        for r in &results {
            assert_eq!(r.metadata["source"], "docs");
        }
    }

    #[tokio::test]
    async fn search_filter_returns_empty_when_nothing_matches() {
        let dir = tempfile::tempdir().unwrap();
        let store = ContextStore::new(test_config(&dir), fake_embedding())
            .await
            .unwrap();
        store
            .add("hello", serde_json::json!({"source": "docs"}))
            .await
            .unwrap();

        let filter = "metadata LIKE '%\"source\":\"nonexistent\"%'".to_string();
        let results = store.search("hello", 10, Some(filter)).await.unwrap();
        assert!(results.is_empty());
    }

    #[tokio::test]
    async fn search_preserves_metadata_in_results() {
        let dir = tempfile::tempdir().unwrap();
        let store = ContextStore::new(test_config(&dir), fake_embedding())
            .await
            .unwrap();
        let meta = serde_json::json!({"source": "test", "priority": 5});
        store.add("hello world", meta.clone()).await.unwrap();

        let results = store.search("hello", 1, None).await.unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].metadata["source"], "test");
        assert_eq!(results[0].metadata["priority"], 5);
    }
}