Skip to main content

zeph_memory/
vector_store.rs

1// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4use std::collections::HashMap;
5use std::future::Future;
6use std::pin::Pin;
7
8#[derive(Debug, thiserror::Error)]
9pub enum VectorStoreError {
10    #[error("connection error: {0}")]
11    Connection(String),
12    #[error("collection error: {0}")]
13    Collection(String),
14    #[error("upsert error: {0}")]
15    Upsert(String),
16    #[error("search error: {0}")]
17    Search(String),
18    #[error("delete error: {0}")]
19    Delete(String),
20    #[error("scroll error: {0}")]
21    Scroll(String),
22    #[error("serialization error: {0}")]
23    Serialization(String),
24}
25
26#[derive(Debug, Clone)]
27pub struct VectorPoint {
28    pub id: String,
29    pub vector: Vec<f32>,
30    pub payload: HashMap<String, serde_json::Value>,
31}
32
33#[derive(Debug, Clone, Default)]
34pub struct VectorFilter {
35    pub must: Vec<FieldCondition>,
36    pub must_not: Vec<FieldCondition>,
37}
38
39#[derive(Debug, Clone)]
40pub struct FieldCondition {
41    pub field: String,
42    pub value: FieldValue,
43}
44
45#[derive(Debug, Clone)]
46pub enum FieldValue {
47    Integer(i64),
48    Text(String),
49}
50
51#[derive(Debug, Clone)]
52pub struct ScoredVectorPoint {
53    pub id: String,
54    pub score: f32,
55    pub payload: HashMap<String, serde_json::Value>,
56}
57
58type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
59
60pub type ScrollResult = HashMap<String, HashMap<String, String>>;
61
62pub trait VectorStore: Send + Sync {
63    fn ensure_collection(
64        &self,
65        collection: &str,
66        vector_size: u64,
67    ) -> BoxFuture<'_, Result<(), VectorStoreError>>;
68
69    fn collection_exists(&self, collection: &str) -> BoxFuture<'_, Result<bool, VectorStoreError>>;
70
71    fn delete_collection(&self, collection: &str) -> BoxFuture<'_, Result<(), VectorStoreError>>;
72
73    fn upsert(
74        &self,
75        collection: &str,
76        points: Vec<VectorPoint>,
77    ) -> BoxFuture<'_, Result<(), VectorStoreError>>;
78
79    fn search(
80        &self,
81        collection: &str,
82        vector: Vec<f32>,
83        limit: u64,
84        filter: Option<VectorFilter>,
85    ) -> BoxFuture<'_, Result<Vec<ScoredVectorPoint>, VectorStoreError>>;
86
87    fn delete_by_ids(
88        &self,
89        collection: &str,
90        ids: Vec<String>,
91    ) -> BoxFuture<'_, Result<(), VectorStoreError>>;
92
93    fn scroll_all(
94        &self,
95        collection: &str,
96        key_field: &str,
97    ) -> BoxFuture<'_, Result<ScrollResult, VectorStoreError>>;
98
99    fn health_check(&self) -> BoxFuture<'_, Result<bool, VectorStoreError>>;
100}