Skip to main content

common/
types.rs

1use serde::{Deserialize, Serialize};
2
3/// Unique identifier for a vector
4pub type VectorId = String;
5
6/// Namespace identifier
7pub type NamespaceId = String;
8
9/// A vector with associated metadata
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct Vector {
12    pub id: VectorId,
13    pub values: Vec<f32>,
14    #[serde(skip_serializing_if = "Option::is_none")]
15    pub metadata: Option<serde_json::Value>,
16    /// TTL in seconds (optional, for upsert requests)
17    #[serde(skip_serializing_if = "Option::is_none")]
18    pub ttl_seconds: Option<u64>,
19    /// Unix timestamp when this vector expires (internal use)
20    #[serde(skip_serializing_if = "Option::is_none")]
21    pub expires_at: Option<u64>,
22}
23
24impl Vector {
25    /// Check if this vector has expired
26    pub fn is_expired(&self) -> bool {
27        if let Some(expires_at) = self.expires_at {
28            let now = std::time::SystemTime::now()
29                .duration_since(std::time::UNIX_EPOCH)
30                .unwrap_or_default()
31                .as_secs();
32            now >= expires_at
33        } else {
34            false
35        }
36    }
37
38    /// Check if this vector has expired against a pre-captured timestamp.
39    /// Prefer this over `is_expired()` inside loops to avoid N syscalls.
40    #[inline]
41    pub fn is_expired_at(&self, now_secs: u64) -> bool {
42        self.expires_at.is_some_and(|exp| now_secs >= exp)
43    }
44
45    /// Calculate and set expires_at from ttl_seconds
46    pub fn apply_ttl(&mut self) {
47        if let Some(ttl) = self.ttl_seconds {
48            let now = std::time::SystemTime::now()
49                .duration_since(std::time::UNIX_EPOCH)
50                .unwrap_or_default()
51                .as_secs();
52            self.expires_at = Some(now + ttl);
53        }
54    }
55
56    /// Get remaining TTL in seconds (None if no expiration or expired)
57    pub fn remaining_ttl(&self) -> Option<u64> {
58        self.expires_at.and_then(|expires_at| {
59            let now = std::time::SystemTime::now()
60                .duration_since(std::time::UNIX_EPOCH)
61                .unwrap_or_default()
62                .as_secs();
63            if now < expires_at {
64                Some(expires_at - now)
65            } else {
66                None
67            }
68        })
69    }
70}
71
72/// Request to upsert vectors
73#[derive(Debug, Deserialize)]
74pub struct UpsertRequest {
75    pub vectors: Vec<Vector>,
76}
77
78/// Response from upsert operation
79#[derive(Debug, Serialize, Deserialize)]
80pub struct UpsertResponse {
81    pub upserted_count: usize,
82}
83
84/// Column-based upsert request (Turbopuffer-inspired)
85/// All arrays must have equal length. Use null for missing values.
86#[derive(Debug, Deserialize)]
87pub struct ColumnUpsertRequest {
88    /// Array of document IDs (required)
89    pub ids: Vec<VectorId>,
90    /// Array of vectors (required for vector namespaces)
91    pub vectors: Vec<Vec<f32>>,
92    /// Additional attributes as columns (optional)
93    /// Each key is an attribute name, value is array of attribute values
94    #[serde(default)]
95    pub attributes: std::collections::HashMap<String, Vec<serde_json::Value>>,
96    /// TTL in seconds for all vectors (optional)
97    #[serde(default)]
98    pub ttl_seconds: Option<u64>,
99    /// Expected dimension (optional, for validation)
100    #[serde(default)]
101    pub dimension: Option<usize>,
102}
103
104impl ColumnUpsertRequest {
105    /// Convert column format to row format (Vec<Vector>)
106    pub fn to_vectors(&self) -> Result<Vec<Vector>, String> {
107        let count = self.ids.len();
108
109        // Validate all arrays have same length
110        if self.vectors.len() != count {
111            return Err(format!(
112                "vectors array length ({}) doesn't match ids array length ({})",
113                self.vectors.len(),
114                count
115            ));
116        }
117
118        for (attr_name, attr_values) in &self.attributes {
119            if attr_values.len() != count {
120                return Err(format!(
121                    "attribute '{}' array length ({}) doesn't match ids array length ({})",
122                    attr_name,
123                    attr_values.len(),
124                    count
125                ));
126            }
127        }
128
129        // Validate vector dimensions
130        // Use explicit dimension if provided, otherwise derive from first vector
131        let expected_dim = if let Some(dim) = self.dimension {
132            Some(dim)
133        } else {
134            self.vectors.first().map(|v| v.len())
135        };
136
137        if let Some(expected) = expected_dim {
138            for (i, vec) in self.vectors.iter().enumerate() {
139                if vec.len() != expected {
140                    return Err(format!(
141                        "vectors[{}] has dimension {} but expected {}",
142                        i,
143                        vec.len(),
144                        expected
145                    ));
146                }
147            }
148        }
149
150        // Convert to row format
151        let mut vectors = Vec::with_capacity(count);
152        for i in 0..count {
153            // Build metadata from attributes
154            let metadata = if self.attributes.is_empty() {
155                None
156            } else {
157                let mut meta = serde_json::Map::new();
158                for (attr_name, attr_values) in &self.attributes {
159                    let value = &attr_values[i];
160                    if !value.is_null() {
161                        meta.insert(attr_name.clone(), value.clone());
162                    }
163                }
164                if meta.is_empty() {
165                    None
166                } else {
167                    Some(serde_json::Value::Object(meta))
168                }
169            };
170
171            let mut vector = Vector {
172                id: self.ids[i].clone(),
173                values: self.vectors[i].clone(),
174                metadata,
175                ttl_seconds: self.ttl_seconds,
176                expires_at: None,
177            };
178            vector.apply_ttl();
179            vectors.push(vector);
180        }
181
182        Ok(vectors)
183    }
184}
185
186/// Distance metric for vector comparison
187#[derive(Debug, Clone, Copy, Default, Deserialize, Serialize, PartialEq, Eq)]
188#[serde(rename_all = "snake_case")]
189pub enum DistanceMetric {
190    #[default]
191    Cosine,
192    Euclidean,
193    DotProduct,
194}
195
196/// Read consistency level for queries (Turbopuffer-inspired)
197///
198/// Controls the trade-off between read latency and data freshness.
199/// - `Strong`: Read from primary only, ensures latest data (higher latency)
200/// - `Eventual`: Read from any replica, may return stale data (lower latency)
201/// - `BoundedStaleness`: Allow reads from replicas within staleness threshold
202#[derive(Debug, Clone, Copy, Default, Deserialize, Serialize, PartialEq, Eq)]
203#[serde(rename_all = "snake_case")]
204pub enum ReadConsistency {
205    /// Read from primary replica only - ensures latest data
206    Strong,
207    /// Read from any available replica - faster but may be stale
208    #[default]
209    Eventual,
210    /// Allow staleness up to specified milliseconds
211    #[serde(rename = "bounded_staleness")]
212    BoundedStaleness,
213}
214
215/// Configuration for bounded staleness reads
216#[derive(Debug, Clone, Copy, Default, Deserialize, Serialize, PartialEq, Eq)]
217pub struct StalenessConfig {
218    /// Maximum acceptable staleness in milliseconds
219    #[serde(default = "default_max_staleness_ms")]
220    pub max_staleness_ms: u64,
221}
222
223fn default_max_staleness_ms() -> u64 {
224    5000 // 5 seconds default
225}
226
227/// Query request for vector search
228#[derive(Debug, Deserialize)]
229pub struct QueryRequest {
230    pub vector: Vec<f32>,
231    #[serde(default = "default_top_k")]
232    pub top_k: usize,
233    #[serde(default)]
234    pub distance_metric: DistanceMetric,
235    #[serde(default = "default_true")]
236    pub include_metadata: bool,
237    #[serde(default)]
238    pub include_vectors: bool,
239    /// Optional metadata filter
240    #[serde(default)]
241    pub filter: Option<FilterExpression>,
242    /// Cursor for pagination (from previous response's next_cursor)
243    #[serde(default)]
244    pub cursor: Option<String>,
245    /// Read consistency level (Turbopuffer-inspired)
246    /// Controls trade-off between latency and data freshness
247    #[serde(default)]
248    pub consistency: ReadConsistency,
249    /// Staleness configuration for bounded_staleness consistency
250    #[serde(default)]
251    pub staleness_config: Option<StalenessConfig>,
252}
253
254fn default_top_k() -> usize {
255    10
256}
257
258fn default_true() -> bool {
259    true
260}
261
262/// Single search result
263#[derive(Debug, Serialize, Deserialize)]
264pub struct SearchResult {
265    pub id: VectorId,
266    pub score: f32,
267    #[serde(skip_serializing_if = "Option::is_none")]
268    pub metadata: Option<serde_json::Value>,
269    #[serde(skip_serializing_if = "Option::is_none")]
270    pub vector: Option<Vec<f32>>,
271}
272
273/// Query response
274#[derive(Debug, Serialize, Deserialize)]
275pub struct QueryResponse {
276    pub results: Vec<SearchResult>,
277    /// Cursor for fetching next page of results
278    #[serde(skip_serializing_if = "Option::is_none")]
279    pub next_cursor: Option<String>,
280    /// Whether there are more results available
281    #[serde(skip_serializing_if = "Option::is_none")]
282    pub has_more: Option<bool>,
283    /// Server-side search time in milliseconds
284    #[serde(default)]
285    pub search_time_ms: u64,
286}
287
288// ============================================================================
289// Cursor-based pagination types
290// ============================================================================
291
292/// Internal cursor state for pagination
293#[derive(Debug, Clone, Serialize, Deserialize)]
294pub struct PaginationCursor {
295    /// Last seen score for cursor-based pagination
296    pub last_score: f32,
297    /// Last seen ID for tie-breaking
298    pub last_id: String,
299}
300
301impl PaginationCursor {
302    /// Create a new pagination cursor
303    pub fn new(last_score: f32, last_id: String) -> Self {
304        Self {
305            last_score,
306            last_id,
307        }
308    }
309
310    /// Encode cursor to base64 string
311    pub fn encode(&self) -> String {
312        use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine as _};
313        let json = serde_json::to_string(self).unwrap_or_default();
314        URL_SAFE_NO_PAD.encode(json.as_bytes())
315    }
316
317    /// Decode cursor from base64 string
318    pub fn decode(cursor: &str) -> Option<Self> {
319        use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine as _};
320        let bytes = URL_SAFE_NO_PAD.decode(cursor).ok()?;
321        let json = String::from_utf8(bytes).ok()?;
322        serde_json::from_str(&json).ok()
323    }
324}
325
326/// Delete request
327#[derive(Debug, Deserialize)]
328pub struct DeleteRequest {
329    pub ids: Vec<VectorId>,
330}
331
332/// Delete response
333#[derive(Debug, Serialize)]
334pub struct DeleteResponse {
335    pub deleted_count: usize,
336}
337
338// ============================================================================
339// Batch query types
340// ============================================================================
341
342/// A single query within a batch request
343#[derive(Debug, Clone, Deserialize)]
344pub struct BatchQueryItem {
345    /// Unique identifier for this query within the batch
346    #[serde(default)]
347    pub id: Option<String>,
348    /// The query vector
349    pub vector: Vec<f32>,
350    /// Number of results to return
351    #[serde(default = "default_batch_top_k")]
352    pub top_k: u32,
353    /// Optional filter expression
354    #[serde(default)]
355    pub filter: Option<FilterExpression>,
356    /// Whether to include metadata in results
357    #[serde(default)]
358    pub include_metadata: bool,
359    /// Read consistency level (Turbopuffer-inspired)
360    #[serde(default)]
361    pub consistency: ReadConsistency,
362    /// Staleness configuration for bounded_staleness consistency
363    #[serde(default)]
364    pub staleness_config: Option<StalenessConfig>,
365}
366
367fn default_batch_top_k() -> u32 {
368    10
369}
370
371/// Batch query request - execute multiple queries in parallel
372#[derive(Debug, Deserialize)]
373pub struct BatchQueryRequest {
374    /// List of queries to execute
375    pub queries: Vec<BatchQueryItem>,
376}
377
378/// Results for a single query within a batch
379#[derive(Debug, Serialize)]
380pub struct BatchQueryResult {
381    /// The query identifier (if provided in request)
382    #[serde(skip_serializing_if = "Option::is_none")]
383    pub id: Option<String>,
384    /// Query results (empty if an error occurred)
385    pub results: Vec<SearchResult>,
386    /// Query execution time in milliseconds
387    pub latency_ms: f64,
388    /// Error message if this individual query failed
389    #[serde(skip_serializing_if = "Option::is_none")]
390    pub error: Option<String>,
391}
392
393/// Batch query response
394#[derive(Debug, Serialize)]
395pub struct BatchQueryResponse {
396    /// Results for each query in the batch
397    pub results: Vec<BatchQueryResult>,
398    /// Total execution time in milliseconds
399    pub total_latency_ms: f64,
400    /// Number of queries executed
401    pub query_count: usize,
402}
403
404// ============================================================================
405// Multi-vector search types
406// ============================================================================
407
408/// Request for multi-vector search with positive and negative vectors
409#[derive(Debug, Deserialize)]
410pub struct MultiVectorSearchRequest {
411    /// Positive vectors to search towards (required, at least one)
412    pub positive_vectors: Vec<Vec<f32>>,
413    /// Weights for positive vectors (optional, defaults to equal weights)
414    #[serde(default)]
415    pub positive_weights: Option<Vec<f32>>,
416    /// Negative vectors to search away from (optional)
417    #[serde(default)]
418    pub negative_vectors: Option<Vec<Vec<f32>>>,
419    /// Weights for negative vectors (optional, defaults to equal weights)
420    #[serde(default)]
421    pub negative_weights: Option<Vec<f32>>,
422    /// Number of results to return
423    #[serde(default = "default_top_k")]
424    pub top_k: usize,
425    /// Distance metric to use
426    #[serde(default)]
427    pub distance_metric: DistanceMetric,
428    /// Minimum score threshold
429    #[serde(default)]
430    pub score_threshold: Option<f32>,
431    /// Enable MMR (Maximal Marginal Relevance) for diversity
432    #[serde(default)]
433    pub enable_mmr: bool,
434    /// Lambda parameter for MMR (0 = max diversity, 1 = max relevance)
435    #[serde(default = "default_mmr_lambda")]
436    pub mmr_lambda: f32,
437    /// Include metadata in results
438    #[serde(default = "default_true")]
439    pub include_metadata: bool,
440    /// Include vectors in results
441    #[serde(default)]
442    pub include_vectors: bool,
443    /// Optional metadata filter
444    #[serde(default)]
445    pub filter: Option<FilterExpression>,
446    /// Read consistency level (Turbopuffer-inspired)
447    #[serde(default)]
448    pub consistency: ReadConsistency,
449    /// Staleness configuration for bounded_staleness consistency
450    #[serde(default)]
451    pub staleness_config: Option<StalenessConfig>,
452}
453
454fn default_mmr_lambda() -> f32 {
455    0.5
456}
457
458/// Single result from multi-vector search
459#[derive(Debug, Serialize, Deserialize)]
460pub struct MultiVectorSearchResult {
461    pub id: VectorId,
462    /// Similarity score
463    pub score: f32,
464    /// MMR score (if MMR enabled)
465    #[serde(skip_serializing_if = "Option::is_none")]
466    pub mmr_score: Option<f32>,
467    /// Original rank before reranking
468    #[serde(skip_serializing_if = "Option::is_none")]
469    pub original_rank: Option<usize>,
470    #[serde(skip_serializing_if = "Option::is_none")]
471    pub metadata: Option<serde_json::Value>,
472    #[serde(skip_serializing_if = "Option::is_none")]
473    pub vector: Option<Vec<f32>>,
474}
475
476/// Response from multi-vector search
477#[derive(Debug, Serialize, Deserialize)]
478pub struct MultiVectorSearchResponse {
479    pub results: Vec<MultiVectorSearchResult>,
480    /// The computed query vector (weighted combination of positive - negative)
481    #[serde(skip_serializing_if = "Option::is_none")]
482    pub computed_query_vector: Option<Vec<f32>>,
483}
484
485// ============================================================================
486// Full-text search types
487// ============================================================================
488
489/// Request to index a document for full-text search
490#[derive(Debug, Serialize, Deserialize)]
491pub struct IndexDocumentRequest {
492    pub id: String,
493    pub text: String,
494    #[serde(skip_serializing_if = "Option::is_none")]
495    pub metadata: Option<serde_json::Value>,
496}
497
498/// Request to index multiple documents
499#[derive(Debug, Deserialize)]
500pub struct IndexDocumentsRequest {
501    pub documents: Vec<IndexDocumentRequest>,
502}
503
504/// Response from indexing operation
505#[derive(Debug, Serialize, Deserialize)]
506pub struct IndexDocumentsResponse {
507    pub indexed_count: usize,
508}
509
510/// Request to search for documents
511#[derive(Debug, Deserialize)]
512pub struct FullTextSearchRequest {
513    pub query: String,
514    #[serde(default = "default_top_k")]
515    pub top_k: usize,
516    /// Optional metadata filter
517    #[serde(default)]
518    pub filter: Option<FilterExpression>,
519}
520
521/// Single full-text search result
522#[derive(Debug, Serialize, Deserialize)]
523pub struct FullTextSearchResult {
524    pub id: String,
525    pub score: f32,
526    #[serde(skip_serializing_if = "Option::is_none")]
527    pub metadata: Option<serde_json::Value>,
528}
529
530/// Full-text search response
531#[derive(Debug, Serialize, Deserialize)]
532pub struct FullTextSearchResponse {
533    pub results: Vec<FullTextSearchResult>,
534    /// Server-side search time in milliseconds
535    #[serde(default)]
536    pub search_time_ms: u64,
537}
538
539/// Request to delete documents from full-text index
540#[derive(Debug, Deserialize)]
541pub struct DeleteDocumentsRequest {
542    pub ids: Vec<String>,
543}
544
545/// Response from deleting documents
546#[derive(Debug, Serialize)]
547pub struct DeleteDocumentsResponse {
548    pub deleted_count: usize,
549}
550
551/// Full-text index statistics
552#[derive(Debug, Serialize)]
553pub struct FullTextIndexStats {
554    pub document_count: u32,
555    pub unique_terms: usize,
556    pub avg_doc_length: f32,
557}
558
559// ============================================================================
560// Hybrid search types (vector + full-text)
561// ============================================================================
562
563/// Hybrid search request combining vector similarity and full-text search
564#[derive(Debug, Deserialize)]
565pub struct HybridSearchRequest {
566    /// Query vector for similarity search. Optional — if omitted, falls back to fulltext-only BM25
567    /// (equivalent to vector_weight=0.0).
568    #[serde(default)]
569    pub vector: Option<Vec<f32>>,
570    /// Text query for full-text search
571    pub text: String,
572    /// Number of results to return
573    #[serde(default = "default_top_k")]
574    pub top_k: usize,
575    /// Weight for vector search score (0.0 to 1.0)
576    /// Text search weight is (1.0 - vector_weight)
577    #[serde(default = "default_vector_weight")]
578    pub vector_weight: f32,
579    /// Distance metric for vector search
580    #[serde(default)]
581    pub distance_metric: DistanceMetric,
582    /// Include metadata in results
583    #[serde(default = "default_true")]
584    pub include_metadata: bool,
585    /// Include vectors in results
586    #[serde(default)]
587    pub include_vectors: bool,
588    /// Optional metadata filter
589    #[serde(default)]
590    pub filter: Option<FilterExpression>,
591}
592
593fn default_vector_weight() -> f32 {
594    0.5 // Equal weight by default
595}
596
597/// Single hybrid search result
598#[derive(Debug, Serialize, Deserialize)]
599pub struct HybridSearchResult {
600    pub id: String,
601    /// Combined score
602    pub score: f32,
603    /// Vector similarity score (normalized 0-1)
604    pub vector_score: f32,
605    /// Text search BM25 score (normalized 0-1)
606    pub text_score: f32,
607    #[serde(skip_serializing_if = "Option::is_none")]
608    pub metadata: Option<serde_json::Value>,
609    #[serde(skip_serializing_if = "Option::is_none")]
610    pub vector: Option<Vec<f32>>,
611}
612
613/// Hybrid search response
614#[derive(Debug, Serialize, Deserialize)]
615pub struct HybridSearchResponse {
616    pub results: Vec<HybridSearchResult>,
617    /// Server-side search time in milliseconds
618    #[serde(default)]
619    pub search_time_ms: u64,
620}
621
622// ============================================================================
623// Filter types for metadata filtering
624// ============================================================================
625
626/// A filter value that can be compared against metadata fields
627#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
628#[serde(untagged)]
629pub enum FilterValue {
630    String(String),
631    Number(f64),
632    Integer(i64),
633    Boolean(bool),
634    StringArray(Vec<String>),
635    NumberArray(Vec<f64>),
636}
637
638impl FilterValue {
639    /// Try to get as f64 for numeric comparisons
640    pub fn as_f64(&self) -> Option<f64> {
641        match self {
642            FilterValue::Number(n) => Some(*n),
643            FilterValue::Integer(i) => Some(*i as f64),
644            _ => None,
645        }
646    }
647
648    /// Try to get as string
649    pub fn as_str(&self) -> Option<&str> {
650        match self {
651            FilterValue::String(s) => Some(s.as_str()),
652            _ => None,
653        }
654    }
655
656    /// Try to get as bool
657    pub fn as_bool(&self) -> Option<bool> {
658        match self {
659            FilterValue::Boolean(b) => Some(*b),
660            _ => None,
661        }
662    }
663}
664
665/// Comparison operators for filter conditions
666#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
667#[serde(rename_all = "snake_case")]
668pub enum FilterCondition {
669    /// Equal to
670    #[serde(rename = "$eq")]
671    Eq(FilterValue),
672    /// Not equal to
673    #[serde(rename = "$ne")]
674    Ne(FilterValue),
675    /// Greater than
676    #[serde(rename = "$gt")]
677    Gt(FilterValue),
678    /// Greater than or equal to
679    #[serde(rename = "$gte")]
680    Gte(FilterValue),
681    /// Less than
682    #[serde(rename = "$lt")]
683    Lt(FilterValue),
684    /// Less than or equal to
685    #[serde(rename = "$lte")]
686    Lte(FilterValue),
687    /// In array
688    #[serde(rename = "$in")]
689    In(Vec<FilterValue>),
690    /// Not in array
691    #[serde(rename = "$nin")]
692    NotIn(Vec<FilterValue>),
693    /// Field exists
694    #[serde(rename = "$exists")]
695    Exists(bool),
696    // =========================================================================
697    // Enhanced string operators (Turbopuffer-inspired)
698    // =========================================================================
699    /// Contains substring (case-sensitive)
700    #[serde(rename = "$contains")]
701    Contains(String),
702    /// Contains substring (case-insensitive)
703    #[serde(rename = "$icontains")]
704    IContains(String),
705    /// Starts with prefix
706    #[serde(rename = "$startsWith")]
707    StartsWith(String),
708    /// Ends with suffix
709    #[serde(rename = "$endsWith")]
710    EndsWith(String),
711    /// Glob pattern matching (supports * and ? wildcards)
712    #[serde(rename = "$glob")]
713    Glob(String),
714    /// Regular expression matching
715    #[serde(rename = "$regex")]
716    Regex(String),
717    // =========================================================================
718    // Array operators
719    // =========================================================================
720    /// Array field contains a value (checks if a JSON array field includes the given element)
721    #[serde(rename = "$arrayContains")]
722    ArrayContains(FilterValue),
723    /// Array field contains all specified values
724    #[serde(rename = "$arrayContainsAll")]
725    ArrayContainsAll(Vec<FilterValue>),
726    /// Array field contains any of the specified values
727    #[serde(rename = "$arrayContainsAny")]
728    ArrayContainsAny(Vec<FilterValue>),
729}
730
731/// A filter expression that can be a single field condition or a logical combinator
732#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
733#[serde(untagged)]
734pub enum FilterExpression {
735    /// Logical AND of multiple expressions
736    And {
737        #[serde(rename = "$and")]
738        conditions: Vec<FilterExpression>,
739    },
740    /// Logical OR of multiple expressions
741    Or {
742        #[serde(rename = "$or")]
743        conditions: Vec<FilterExpression>,
744    },
745    /// Single field condition
746    Field {
747        #[serde(flatten)]
748        field: std::collections::HashMap<String, FilterCondition>,
749    },
750}
751
752// ============================================================================
753// Namespace quota types
754// ============================================================================
755
756/// Quota configuration for a namespace
757#[derive(Debug, Clone, Serialize, Deserialize, Default)]
758pub struct QuotaConfig {
759    /// Maximum number of vectors allowed (None = unlimited)
760    #[serde(skip_serializing_if = "Option::is_none")]
761    pub max_vectors: Option<u64>,
762    /// Maximum storage size in bytes (None = unlimited)
763    #[serde(skip_serializing_if = "Option::is_none")]
764    pub max_storage_bytes: Option<u64>,
765    /// Maximum dimensions per vector (None = unlimited)
766    #[serde(skip_serializing_if = "Option::is_none")]
767    pub max_dimensions: Option<usize>,
768    /// Maximum metadata size per vector in bytes (None = unlimited)
769    #[serde(skip_serializing_if = "Option::is_none")]
770    pub max_metadata_bytes: Option<usize>,
771    /// Whether to enforce quotas (soft limit = warn only, hard = reject)
772    #[serde(default)]
773    pub enforcement: QuotaEnforcement,
774}
775
776/// Quota enforcement mode
777#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default, PartialEq, Eq)]
778#[serde(rename_all = "snake_case")]
779pub enum QuotaEnforcement {
780    /// No enforcement, just tracking
781    None,
782    /// Log warnings when quota exceeded but allow operations
783    Soft,
784    /// Reject operations that would exceed quota
785    #[default]
786    Hard,
787}
788
789/// Current quota usage for a namespace
790#[derive(Debug, Clone, Serialize, Deserialize, Default)]
791pub struct QuotaUsage {
792    /// Current number of vectors
793    pub vector_count: u64,
794    /// Current storage size in bytes (estimated)
795    pub storage_bytes: u64,
796    /// Average vector dimensions
797    pub avg_dimensions: Option<usize>,
798    /// Average metadata size in bytes
799    pub avg_metadata_bytes: Option<usize>,
800    /// Last updated timestamp (Unix epoch)
801    pub last_updated: u64,
802}
803
804impl QuotaUsage {
805    /// Create new usage with current timestamp
806    pub fn new(vector_count: u64, storage_bytes: u64) -> Self {
807        let now = std::time::SystemTime::now()
808            .duration_since(std::time::UNIX_EPOCH)
809            .unwrap_or_default()
810            .as_secs();
811        Self {
812            vector_count,
813            storage_bytes,
814            avg_dimensions: None,
815            avg_metadata_bytes: None,
816            last_updated: now,
817        }
818    }
819
820    /// Update the timestamp to now
821    pub fn touch(&mut self) {
822        self.last_updated = std::time::SystemTime::now()
823            .duration_since(std::time::UNIX_EPOCH)
824            .unwrap_or_default()
825            .as_secs();
826    }
827}
828
829/// Combined quota status showing config and current usage
830#[derive(Debug, Clone, Serialize, Deserialize)]
831pub struct QuotaStatus {
832    /// Namespace name
833    pub namespace: String,
834    /// Quota configuration
835    pub config: QuotaConfig,
836    /// Current usage
837    pub usage: QuotaUsage,
838    /// Percentage of vector quota used (0-100, None if unlimited)
839    #[serde(skip_serializing_if = "Option::is_none")]
840    pub vector_usage_percent: Option<f32>,
841    /// Percentage of storage quota used (0-100, None if unlimited)
842    #[serde(skip_serializing_if = "Option::is_none")]
843    pub storage_usage_percent: Option<f32>,
844    /// Whether any quota is exceeded
845    pub is_exceeded: bool,
846    /// List of exceeded quota types
847    #[serde(skip_serializing_if = "Vec::is_empty")]
848    pub exceeded_quotas: Vec<String>,
849}
850
851impl QuotaStatus {
852    /// Create a new quota status from config and usage
853    pub fn new(namespace: String, config: QuotaConfig, usage: QuotaUsage) -> Self {
854        let vector_usage_percent = config
855            .max_vectors
856            .map(|max| (usage.vector_count as f32 / max as f32) * 100.0);
857
858        let storage_usage_percent = config
859            .max_storage_bytes
860            .map(|max| (usage.storage_bytes as f32 / max as f32) * 100.0);
861
862        let mut exceeded_quotas = Vec::new();
863
864        if let Some(max) = config.max_vectors {
865            if usage.vector_count > max {
866                exceeded_quotas.push("max_vectors".to_string());
867            }
868        }
869
870        if let Some(max) = config.max_storage_bytes {
871            if usage.storage_bytes > max {
872                exceeded_quotas.push("max_storage_bytes".to_string());
873            }
874        }
875
876        let is_exceeded = !exceeded_quotas.is_empty();
877
878        Self {
879            namespace,
880            config,
881            usage,
882            vector_usage_percent,
883            storage_usage_percent,
884            is_exceeded,
885            exceeded_quotas,
886        }
887    }
888}
889
890/// Request to set quota for a namespace
891#[derive(Debug, Deserialize)]
892pub struct SetQuotaRequest {
893    /// Quota configuration to apply
894    pub config: QuotaConfig,
895}
896
897/// Response from setting quota
898#[derive(Debug, Serialize)]
899pub struct SetQuotaResponse {
900    /// Whether the operation succeeded
901    pub success: bool,
902    /// Namespace name
903    pub namespace: String,
904    /// Applied quota configuration
905    pub config: QuotaConfig,
906    /// Status message
907    pub message: String,
908}
909
910/// Quota check result
911#[derive(Debug, Clone, Serialize)]
912pub struct QuotaCheckResult {
913    /// Whether the operation is allowed
914    pub allowed: bool,
915    /// Reason if not allowed
916    #[serde(skip_serializing_if = "Option::is_none")]
917    pub reason: Option<String>,
918    /// Current usage
919    pub usage: QuotaUsage,
920    /// Quota that would be exceeded
921    #[serde(skip_serializing_if = "Option::is_none")]
922    pub exceeded_quota: Option<String>,
923}
924
925/// Response listing all namespace quotas
926#[derive(Debug, Serialize)]
927pub struct QuotaListResponse {
928    /// List of quota statuses per namespace
929    pub quotas: Vec<QuotaStatus>,
930    /// Total number of namespaces with quotas
931    pub total: u64,
932    /// Default quota configuration (if set)
933    #[serde(skip_serializing_if = "Option::is_none")]
934    pub default_config: Option<QuotaConfig>,
935}
936
937/// Response for default quota query
938#[derive(Debug, Serialize)]
939pub struct DefaultQuotaResponse {
940    /// Default quota configuration (None if not set)
941    pub config: Option<QuotaConfig>,
942}
943
944/// Request to set default quota configuration
945#[derive(Debug, Deserialize)]
946pub struct SetDefaultQuotaRequest {
947    /// Default quota configuration (None to remove)
948    pub config: Option<QuotaConfig>,
949}
950
951/// Request to check if an operation would exceed quota
952#[derive(Debug, Deserialize)]
953pub struct QuotaCheckRequest {
954    /// Vector IDs to check (simulated vectors)
955    pub vector_ids: Vec<String>,
956    /// Dimension of vectors (for size estimation)
957    #[serde(default)]
958    pub dimensions: Option<usize>,
959    /// Estimated metadata size per vector
960    #[serde(default)]
961    pub metadata_bytes: Option<usize>,
962}
963
964// ============================================================================
965// Export API Types (Turbopuffer-inspired)
966// ============================================================================
967
968/// Request to export vectors from a namespace with pagination
969#[derive(Debug, Deserialize)]
970pub struct ExportRequest {
971    /// Maximum number of vectors to return per page (default: 1000, max: 10000)
972    #[serde(default = "default_export_top_k")]
973    pub top_k: usize,
974    /// Cursor for pagination - the last vector ID from previous page
975    #[serde(skip_serializing_if = "Option::is_none")]
976    pub cursor: Option<String>,
977    /// Whether to include vector values in the response (default: true)
978    #[serde(default = "default_true")]
979    pub include_vectors: bool,
980    /// Whether to include metadata in the response (default: true)
981    #[serde(default = "default_true")]
982    pub include_metadata: bool,
983}
984
985fn default_export_top_k() -> usize {
986    1000
987}
988
989impl Default for ExportRequest {
990    fn default() -> Self {
991        Self {
992            top_k: 1000,
993            cursor: None,
994            include_vectors: true,
995            include_metadata: true,
996        }
997    }
998}
999
1000/// A single exported vector record
1001#[derive(Debug, Clone, Serialize, Deserialize)]
1002pub struct ExportedVector {
1003    /// Vector ID
1004    pub id: String,
1005    /// Vector values (optional based on include_vectors)
1006    #[serde(skip_serializing_if = "Option::is_none")]
1007    pub values: Option<Vec<f32>>,
1008    /// Metadata (optional based on include_metadata)
1009    #[serde(skip_serializing_if = "Option::is_none")]
1010    pub metadata: Option<serde_json::Value>,
1011    /// TTL in seconds if set
1012    #[serde(skip_serializing_if = "Option::is_none")]
1013    pub ttl_seconds: Option<u64>,
1014}
1015
1016impl From<&Vector> for ExportedVector {
1017    fn from(v: &Vector) -> Self {
1018        Self {
1019            id: v.id.clone(),
1020            values: Some(v.values.clone()),
1021            metadata: v.metadata.clone(),
1022            ttl_seconds: v.ttl_seconds,
1023        }
1024    }
1025}
1026
1027/// Response from export operation
1028#[derive(Debug, Serialize)]
1029pub struct ExportResponse {
1030    /// Exported vectors for this page
1031    pub vectors: Vec<ExportedVector>,
1032    /// Cursor for next page (None if this is the last page)
1033    #[serde(skip_serializing_if = "Option::is_none")]
1034    pub next_cursor: Option<String>,
1035    /// Total vectors in namespace (for progress tracking)
1036    pub total_count: usize,
1037    /// Number of vectors returned in this page
1038    pub returned_count: usize,
1039}
1040
1041// ============================================================================
1042// Unified Query API with rank_by (Turbopuffer-inspired)
1043// ============================================================================
1044
1045/// Ranking function for unified query API
1046/// Supports vector search (ANN/kNN), full-text BM25, and attribute ordering
1047#[derive(Debug, Clone, Serialize, Deserialize)]
1048#[serde(untagged)]
1049pub enum RankBy {
1050    /// Vector search: ["vector_field", "ANN"|"kNN", [query_vector]]
1051    /// or simplified: ["ANN", [query_vector]] for default "vector" field
1052    VectorSearch {
1053        field: String,
1054        method: VectorSearchMethod,
1055        query_vector: Vec<f32>,
1056    },
1057    /// Full-text BM25 search: ["text_field", "BM25", "query string"]
1058    FullTextSearch {
1059        field: String,
1060        method: String, // Always "BM25"
1061        query: String,
1062    },
1063    /// Attribute ordering: ["field_name", "asc"|"desc"]
1064    AttributeOrder {
1065        field: String,
1066        direction: SortDirection,
1067    },
1068    /// Sum of multiple ranking functions: ["Sum", [...rankings]]
1069    Sum(Vec<RankBy>),
1070    /// Max of multiple ranking functions: ["Max", [...rankings]]
1071    Max(Vec<RankBy>),
1072    /// Product with weight: ["Product", weight, ranking]
1073    Product { weight: f32, ranking: Box<RankBy> },
1074}
1075
1076/// Vector search method
1077#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
1078pub enum VectorSearchMethod {
1079    /// Approximate Nearest Neighbor (fast, default)
1080    #[default]
1081    ANN,
1082    /// Exact k-Nearest Neighbor (exhaustive, requires filters)
1083    #[serde(rename = "kNN")]
1084    KNN,
1085}
1086
1087/// Sort direction for attribute ordering
1088#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
1089#[serde(rename_all = "lowercase")]
1090#[derive(Default)]
1091pub enum SortDirection {
1092    Asc,
1093    #[default]
1094    Desc,
1095}
1096
1097/// Unified query request with rank_by parameter (Turbopuffer-inspired)
1098#[derive(Debug, Deserialize)]
1099pub struct UnifiedQueryRequest {
1100    /// How to rank documents (required unless using aggregations)
1101    pub rank_by: RankByInput,
1102    /// Number of results to return
1103    #[serde(default = "default_top_k")]
1104    pub top_k: usize,
1105    /// Optional metadata filter
1106    #[serde(default)]
1107    pub filter: Option<FilterExpression>,
1108    /// Include metadata in results
1109    #[serde(default = "default_true")]
1110    pub include_metadata: bool,
1111    /// Include vectors in results
1112    #[serde(default)]
1113    pub include_vectors: bool,
1114    /// Distance metric for vector search (default: cosine)
1115    #[serde(default)]
1116    pub distance_metric: DistanceMetric,
1117}
1118
1119/// Input format for rank_by that handles JSON array syntax
1120/// Examples:
1121/// - ["vector", "ANN", [0.1, 0.2, 0.3]]
1122/// - ["text", "BM25", "search query"]
1123/// - ["timestamp", "desc"]
1124/// - ["Sum", [["title", "BM25", "query"], ["content", "BM25", "query"]]]
1125/// - ["Product", 2.0, ["title", "BM25", "query"]]
1126#[derive(Debug, Clone, Serialize, Deserialize)]
1127#[serde(from = "serde_json::Value")]
1128pub struct RankByInput(pub RankBy);
1129
1130impl From<serde_json::Value> for RankByInput {
1131    fn from(value: serde_json::Value) -> Self {
1132        RankByInput(parse_rank_by(&value).unwrap_or_else(|| {
1133            // Default fallback - shouldn't happen with valid input
1134            RankBy::AttributeOrder {
1135                field: "id".to_string(),
1136                direction: SortDirection::Asc,
1137            }
1138        }))
1139    }
1140}
1141
1142/// Parse rank_by JSON array into RankBy enum
1143fn parse_rank_by(value: &serde_json::Value) -> Option<RankBy> {
1144    let arr = value.as_array()?;
1145    if arr.is_empty() {
1146        return None;
1147    }
1148
1149    let first = arr.first()?.as_str()?;
1150
1151    match first {
1152        // Combination operators
1153        "Sum" => {
1154            let rankings = arr.get(1)?.as_array()?;
1155            let parsed: Option<Vec<RankBy>> = rankings.iter().map(parse_rank_by).collect();
1156            Some(RankBy::Sum(parsed?))
1157        }
1158        "Max" => {
1159            let rankings = arr.get(1)?.as_array()?;
1160            let parsed: Option<Vec<RankBy>> = rankings.iter().map(parse_rank_by).collect();
1161            Some(RankBy::Max(parsed?))
1162        }
1163        "Product" => {
1164            let weight = arr.get(1)?.as_f64()? as f32;
1165            let ranking = parse_rank_by(arr.get(2)?)?;
1166            Some(RankBy::Product {
1167                weight,
1168                ranking: Box::new(ranking),
1169            })
1170        }
1171        // Vector search shorthand: ["ANN", [vector]] or ["kNN", [vector]]
1172        "ANN" => {
1173            let query_vector = parse_vector_array(arr.get(1)?)?;
1174            Some(RankBy::VectorSearch {
1175                field: "vector".to_string(),
1176                method: VectorSearchMethod::ANN,
1177                query_vector,
1178            })
1179        }
1180        "kNN" => {
1181            let query_vector = parse_vector_array(arr.get(1)?)?;
1182            Some(RankBy::VectorSearch {
1183                field: "vector".to_string(),
1184                method: VectorSearchMethod::KNN,
1185                query_vector,
1186            })
1187        }
1188        // Field-based operations
1189        field => {
1190            let second = arr.get(1)?;
1191
1192            // Check if second element is a method string
1193            if let Some(method_str) = second.as_str() {
1194                match method_str {
1195                    "ANN" => {
1196                        let query_vector = parse_vector_array(arr.get(2)?)?;
1197                        Some(RankBy::VectorSearch {
1198                            field: field.to_string(),
1199                            method: VectorSearchMethod::ANN,
1200                            query_vector,
1201                        })
1202                    }
1203                    "kNN" => {
1204                        let query_vector = parse_vector_array(arr.get(2)?)?;
1205                        Some(RankBy::VectorSearch {
1206                            field: field.to_string(),
1207                            method: VectorSearchMethod::KNN,
1208                            query_vector,
1209                        })
1210                    }
1211                    "BM25" => {
1212                        let query = arr.get(2)?.as_str()?;
1213                        Some(RankBy::FullTextSearch {
1214                            field: field.to_string(),
1215                            method: "BM25".to_string(),
1216                            query: query.to_string(),
1217                        })
1218                    }
1219                    "asc" => Some(RankBy::AttributeOrder {
1220                        field: field.to_string(),
1221                        direction: SortDirection::Asc,
1222                    }),
1223                    "desc" => Some(RankBy::AttributeOrder {
1224                        field: field.to_string(),
1225                        direction: SortDirection::Desc,
1226                    }),
1227                    _ => None,
1228                }
1229            } else {
1230                None
1231            }
1232        }
1233    }
1234}
1235
1236/// Parse a JSON value into a vector of f32
1237fn parse_vector_array(value: &serde_json::Value) -> Option<Vec<f32>> {
1238    let arr = value.as_array()?;
1239    arr.iter().map(|v| v.as_f64().map(|n| n as f32)).collect()
1240}
1241
1242/// Unified query response with $dist scoring
1243#[derive(Debug, Serialize, Deserialize)]
1244pub struct UnifiedQueryResponse {
1245    /// Search results ordered by rank_by score
1246    pub results: Vec<UnifiedSearchResult>,
1247    /// Cursor for pagination (if more results available)
1248    #[serde(skip_serializing_if = "Option::is_none")]
1249    pub next_cursor: Option<String>,
1250}
1251
1252/// Single result from unified query
1253#[derive(Debug, Serialize, Deserialize)]
1254pub struct UnifiedSearchResult {
1255    /// Vector/document ID
1256    pub id: String,
1257    /// Ranking score (distance for vector search, BM25 score for text)
1258    /// Named $dist for Turbopuffer compatibility
1259    #[serde(rename = "$dist", skip_serializing_if = "Option::is_none")]
1260    pub dist: Option<f32>,
1261    /// Metadata if requested
1262    #[serde(skip_serializing_if = "Option::is_none")]
1263    pub metadata: Option<serde_json::Value>,
1264    /// Vector values if requested
1265    #[serde(skip_serializing_if = "Option::is_none")]
1266    pub vector: Option<Vec<f32>>,
1267}
1268
1269// ============================================================================
1270// Aggregation types (Turbopuffer-inspired)
1271// ============================================================================
1272
1273/// Aggregate function for computing values across documents
1274#[derive(Debug, Clone, Serialize, Deserialize)]
1275pub enum AggregateFunction {
1276    /// Count matching documents: ["Count"]
1277    Count,
1278    /// Sum numeric attribute values: ["Sum", "attribute_name"]
1279    Sum { field: String },
1280    /// Average numeric attribute values: ["Avg", "attribute_name"]
1281    Avg { field: String },
1282    /// Minimum numeric attribute value: ["Min", "attribute_name"]
1283    Min { field: String },
1284    /// Maximum numeric attribute value: ["Max", "attribute_name"]
1285    Max { field: String },
1286}
1287
1288/// Wrapper for parsing aggregate function from JSON array
1289#[derive(Debug, Clone, Serialize, Deserialize)]
1290#[serde(from = "serde_json::Value")]
1291pub struct AggregateFunctionInput(pub AggregateFunction);
1292
1293impl From<serde_json::Value> for AggregateFunctionInput {
1294    fn from(value: serde_json::Value) -> Self {
1295        parse_aggregate_function(&value)
1296            .map(AggregateFunctionInput)
1297            .unwrap_or_else(|| {
1298                // Default to count if parsing fails
1299                AggregateFunctionInput(AggregateFunction::Count)
1300            })
1301    }
1302}
1303
1304/// Parse aggregate function from JSON array
1305fn parse_aggregate_function(value: &serde_json::Value) -> Option<AggregateFunction> {
1306    let arr = value.as_array()?;
1307    if arr.is_empty() {
1308        return None;
1309    }
1310
1311    let func_name = arr.first()?.as_str()?;
1312
1313    match func_name {
1314        "Count" => Some(AggregateFunction::Count),
1315        "Sum" => {
1316            let field = arr.get(1)?.as_str()?;
1317            Some(AggregateFunction::Sum {
1318                field: field.to_string(),
1319            })
1320        }
1321        "Avg" => {
1322            let field = arr.get(1)?.as_str()?;
1323            Some(AggregateFunction::Avg {
1324                field: field.to_string(),
1325            })
1326        }
1327        "Min" => {
1328            let field = arr.get(1)?.as_str()?;
1329            Some(AggregateFunction::Min {
1330                field: field.to_string(),
1331            })
1332        }
1333        "Max" => {
1334            let field = arr.get(1)?.as_str()?;
1335            Some(AggregateFunction::Max {
1336                field: field.to_string(),
1337            })
1338        }
1339        _ => None,
1340    }
1341}
1342
1343/// Request for aggregation query (Turbopuffer-inspired)
1344#[derive(Debug, Deserialize)]
1345pub struct AggregationRequest {
1346    /// Named aggregations to compute
1347    /// Example: {"my_count": ["Count"], "total_score": ["Sum", "score"]}
1348    pub aggregate_by: std::collections::HashMap<String, AggregateFunctionInput>,
1349    /// Fields to group results by (optional)
1350    /// Example: ["category", "status"]
1351    #[serde(default)]
1352    pub group_by: Vec<String>,
1353    /// Filter to apply before aggregation
1354    #[serde(default)]
1355    pub filter: Option<FilterExpression>,
1356    /// Maximum number of groups to return (default: 100)
1357    #[serde(default = "default_agg_limit")]
1358    pub limit: usize,
1359}
1360
1361fn default_agg_limit() -> usize {
1362    100
1363}
1364
1365/// Response for aggregation query
1366#[derive(Debug, Serialize, Deserialize)]
1367pub struct AggregationResponse {
1368    /// Aggregation results (without grouping)
1369    #[serde(skip_serializing_if = "Option::is_none")]
1370    pub aggregations: Option<std::collections::HashMap<String, serde_json::Value>>,
1371    /// Grouped aggregation results (with group_by)
1372    #[serde(skip_serializing_if = "Option::is_none")]
1373    pub aggregation_groups: Option<Vec<AggregationGroup>>,
1374}
1375
1376/// Single group in aggregation results
1377#[derive(Debug, Serialize, Deserialize)]
1378pub struct AggregationGroup {
1379    /// Group key values (flattened into object)
1380    #[serde(flatten)]
1381    pub group_key: std::collections::HashMap<String, serde_json::Value>,
1382    /// Aggregation results for this group
1383    #[serde(flatten)]
1384    pub aggregations: std::collections::HashMap<String, serde_json::Value>,
1385}
1386
1387// =============================================================================
1388// TEXT-BASED API TYPES (Embedded Inference)
1389// =============================================================================
1390
1391/// A text document with metadata for text-based upsert
1392#[derive(Debug, Clone, Serialize, Deserialize)]
1393pub struct TextDocument {
1394    /// Unique identifier for this document
1395    pub id: VectorId,
1396    /// The text content to be embedded
1397    pub text: String,
1398    /// Optional metadata to store with the vector
1399    #[serde(skip_serializing_if = "Option::is_none")]
1400    pub metadata: Option<serde_json::Value>,
1401    /// TTL in seconds (optional)
1402    #[serde(skip_serializing_if = "Option::is_none")]
1403    pub ttl_seconds: Option<u64>,
1404}
1405
1406/// Request to upsert text documents (auto-embedded)
1407#[derive(Debug, Deserialize)]
1408pub struct TextUpsertRequest {
1409    /// Text documents to embed and store
1410    pub documents: Vec<TextDocument>,
1411    /// Embedding model to use (default: `minilm`).
1412    #[serde(default)]
1413    pub model: Option<EmbeddingModelType>,
1414}
1415
1416/// Response from text upsert operation
1417#[derive(Debug, Serialize, Deserialize)]
1418pub struct TextUpsertResponse {
1419    /// Number of documents successfully upserted
1420    pub upserted_count: usize,
1421    /// Number of tokens processed for embedding
1422    pub tokens_processed: usize,
1423    /// Embedding model used
1424    pub model: EmbeddingModelType,
1425    /// Time taken for embedding generation (ms)
1426    pub embedding_time_ms: u64,
1427}
1428
1429/// Request for text-based query (auto-embedded)
1430#[derive(Debug, Deserialize)]
1431pub struct TextQueryRequest {
1432    /// The query text to search for
1433    pub text: String,
1434    /// Number of results to return
1435    #[serde(default = "default_top_k")]
1436    pub top_k: usize,
1437    /// Optional filter to apply
1438    #[serde(default)]
1439    pub filter: Option<FilterExpression>,
1440    /// Whether to include vectors in response
1441    #[serde(default)]
1442    pub include_vectors: bool,
1443    /// Whether to include the original text in response (if stored in metadata)
1444    #[serde(default = "default_true")]
1445    pub include_text: bool,
1446    /// Embedding model to use (must match upsert model; default: `minilm`).
1447    #[serde(default)]
1448    pub model: Option<EmbeddingModelType>,
1449}
1450
1451/// Response from text-based query
1452#[derive(Debug, Serialize, Deserialize)]
1453pub struct TextQueryResponse {
1454    /// Search results with similarity scores
1455    pub results: Vec<TextSearchResult>,
1456    /// Embedding model used
1457    pub model: EmbeddingModelType,
1458    /// Time taken for embedding generation (ms)
1459    pub embedding_time_ms: u64,
1460    /// Time taken for search (ms)
1461    pub search_time_ms: u64,
1462}
1463
1464/// Single result from text search
1465#[derive(Debug, Serialize, Deserialize)]
1466pub struct TextSearchResult {
1467    /// Document ID
1468    pub id: VectorId,
1469    /// Similarity score (higher is better)
1470    pub score: f32,
1471    /// Original text (if include_text=true and stored in metadata)
1472    #[serde(skip_serializing_if = "Option::is_none")]
1473    pub text: Option<String>,
1474    /// Associated metadata
1475    #[serde(skip_serializing_if = "Option::is_none")]
1476    pub metadata: Option<serde_json::Value>,
1477    /// Vector values (if include_vectors=true)
1478    #[serde(skip_serializing_if = "Option::is_none")]
1479    pub vector: Option<Vec<f32>>,
1480}
1481
1482/// Batch text query request
1483#[derive(Debug, Deserialize)]
1484pub struct BatchTextQueryRequest {
1485    /// Multiple query texts
1486    pub queries: Vec<String>,
1487    /// Number of results per query
1488    #[serde(default = "default_top_k")]
1489    pub top_k: usize,
1490    /// Optional filter to apply to all queries
1491    #[serde(default)]
1492    pub filter: Option<FilterExpression>,
1493    /// Whether to include vectors in response
1494    #[serde(default)]
1495    pub include_vectors: bool,
1496    /// Embedding model to use (default: `minilm`).
1497    #[serde(default)]
1498    pub model: Option<EmbeddingModelType>,
1499}
1500
1501/// Response from batch text query
1502#[derive(Debug, Serialize, Deserialize)]
1503pub struct BatchTextQueryResponse {
1504    /// Results for each query
1505    pub results: Vec<Vec<TextSearchResult>>,
1506    /// Embedding model used
1507    pub model: EmbeddingModelType,
1508    /// Total time for embedding generation (ms)
1509    pub embedding_time_ms: u64,
1510    /// Total time for search (ms)
1511    pub search_time_ms: u64,
1512}
1513
1514/// Available embedding models.
1515///
1516/// Replaces the previous `model: String` field — callers now supply a
1517/// typed enum value, eliminating runtime string-mismatch bugs.
1518///
1519/// JSON serialisation uses lowercase identifiers:
1520/// `"bge-large"`, `"minilm"`, `"bge-small"`, `"e5-small"`.
1521#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
1522pub enum EmbeddingModelType {
1523    /// BAAI/bge-large-en-v1.5 — highest quality, 1024 dimensions (default)
1524    #[default]
1525    #[serde(rename = "bge-large")]
1526    BgeLarge,
1527    /// all-MiniLM-L6-v2 — fast and memory-efficient, 384 dimensions
1528    #[serde(rename = "minilm")]
1529    MiniLM,
1530    /// BAAI/bge-small-en-v1.5 — balanced quality and speed, 384 dimensions
1531    #[serde(rename = "bge-small")]
1532    BgeSmall,
1533    /// intfloat/e5-small-v2 — quality-focused, 384 dimensions
1534    #[serde(rename = "e5-small")]
1535    E5Small,
1536}
1537
1538impl EmbeddingModelType {
1539    /// Embedding vector dimension for this model.
1540    pub fn dimension(&self) -> usize {
1541        match self {
1542            EmbeddingModelType::BgeLarge => 1024,
1543            EmbeddingModelType::MiniLM => 384,
1544            EmbeddingModelType::BgeSmall => 384,
1545            EmbeddingModelType::E5Small => 384,
1546        }
1547    }
1548}
1549
1550impl std::fmt::Display for EmbeddingModelType {
1551    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1552        match self {
1553            EmbeddingModelType::BgeLarge => write!(f, "bge-large"),
1554            EmbeddingModelType::MiniLM => write!(f, "minilm"),
1555            EmbeddingModelType::BgeSmall => write!(f, "bge-small"),
1556            EmbeddingModelType::E5Small => write!(f, "e5-small"),
1557        }
1558    }
1559}
1560
1561// ============================================================================
1562// Dakera Memory Types — AI Agent Memory Platform
1563// ============================================================================
1564
1565/// Type of memory stored by an agent
1566#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
1567#[serde(rename_all = "snake_case")]
1568#[derive(Default)]
1569pub enum MemoryType {
1570    /// Personal experiences and events
1571    #[default]
1572    Episodic,
1573    /// Facts and general knowledge
1574    Semantic,
1575    /// How-to knowledge and skills
1576    Procedural,
1577    /// Short-term, temporary context
1578    Working,
1579}
1580
1581impl std::fmt::Display for MemoryType {
1582    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1583        match self {
1584            MemoryType::Episodic => write!(f, "episodic"),
1585            MemoryType::Semantic => write!(f, "semantic"),
1586            MemoryType::Procedural => write!(f, "procedural"),
1587            MemoryType::Working => write!(f, "working"),
1588        }
1589    }
1590}
1591
1592/// A memory stored by an AI agent
1593#[derive(Debug, Clone, Serialize, Deserialize)]
1594pub struct Memory {
1595    pub id: String,
1596    #[serde(default)]
1597    pub memory_type: MemoryType,
1598    pub content: String,
1599    pub agent_id: String,
1600    #[serde(skip_serializing_if = "Option::is_none")]
1601    pub session_id: Option<String>,
1602    #[serde(default = "default_importance")]
1603    pub importance: f32,
1604    #[serde(default)]
1605    pub tags: Vec<String>,
1606    #[serde(skip_serializing_if = "Option::is_none")]
1607    pub metadata: Option<serde_json::Value>,
1608    pub created_at: u64,
1609    pub last_accessed_at: u64,
1610    #[serde(default)]
1611    pub access_count: u32,
1612    #[serde(skip_serializing_if = "Option::is_none")]
1613    pub ttl_seconds: Option<u64>,
1614    #[serde(skip_serializing_if = "Option::is_none")]
1615    pub expires_at: Option<u64>,
1616}
1617
1618fn default_importance() -> f32 {
1619    0.5
1620}
1621
1622impl Memory {
1623    /// Create a new memory with current timestamps
1624    pub fn new(id: String, content: String, agent_id: String, memory_type: MemoryType) -> Self {
1625        let now = std::time::SystemTime::now()
1626            .duration_since(std::time::UNIX_EPOCH)
1627            .unwrap_or_default()
1628            .as_secs();
1629        Self {
1630            id,
1631            memory_type,
1632            content,
1633            agent_id,
1634            session_id: None,
1635            importance: 0.5,
1636            tags: Vec::new(),
1637            metadata: None,
1638            created_at: now,
1639            last_accessed_at: now,
1640            access_count: 0,
1641            ttl_seconds: None,
1642            expires_at: None,
1643        }
1644    }
1645
1646    /// Check if this memory has expired
1647    pub fn is_expired(&self) -> bool {
1648        if let Some(expires_at) = self.expires_at {
1649            let now = std::time::SystemTime::now()
1650                .duration_since(std::time::UNIX_EPOCH)
1651                .unwrap_or_default()
1652                .as_secs();
1653            now >= expires_at
1654        } else {
1655            false
1656        }
1657    }
1658
1659    /// Pack memory fields into metadata for Vector storage
1660    pub fn to_vector_metadata(&self) -> serde_json::Value {
1661        let mut meta = serde_json::Map::new();
1662        meta.insert("_dakera_type".to_string(), serde_json::json!("memory"));
1663        meta.insert(
1664            "memory_type".to_string(),
1665            serde_json::json!(self.memory_type),
1666        );
1667        meta.insert("content".to_string(), serde_json::json!(self.content));
1668        meta.insert("agent_id".to_string(), serde_json::json!(self.agent_id));
1669        if let Some(ref sid) = self.session_id {
1670            meta.insert("session_id".to_string(), serde_json::json!(sid));
1671        }
1672        meta.insert("importance".to_string(), serde_json::json!(self.importance));
1673        meta.insert("tags".to_string(), serde_json::json!(self.tags));
1674        meta.insert("created_at".to_string(), serde_json::json!(self.created_at));
1675        meta.insert(
1676            "last_accessed_at".to_string(),
1677            serde_json::json!(self.last_accessed_at),
1678        );
1679        meta.insert(
1680            "access_count".to_string(),
1681            serde_json::json!(self.access_count),
1682        );
1683        if let Some(ref ttl) = self.ttl_seconds {
1684            meta.insert("ttl_seconds".to_string(), serde_json::json!(ttl));
1685        }
1686        if let Some(ref expires) = self.expires_at {
1687            meta.insert("expires_at".to_string(), serde_json::json!(expires));
1688        }
1689        if let Some(ref user_meta) = self.metadata {
1690            meta.insert("user_metadata".to_string(), user_meta.clone());
1691        }
1692        serde_json::Value::Object(meta)
1693    }
1694
1695    /// Convert a Memory to a Vector (for storage layer)
1696    pub fn to_vector(&self, embedding: Vec<f32>) -> Vector {
1697        let mut v = Vector {
1698            id: self.id.clone(),
1699            values: embedding,
1700            metadata: Some(self.to_vector_metadata()),
1701            ttl_seconds: self.ttl_seconds,
1702            expires_at: self.expires_at,
1703        };
1704        v.apply_ttl();
1705        v
1706    }
1707
1708    /// Reconstruct a Memory from a Vector's metadata
1709    pub fn from_vector(vector: &Vector) -> Option<Self> {
1710        let meta = vector.metadata.as_ref()?.as_object()?;
1711        let entry_type = meta.get("_dakera_type")?.as_str()?;
1712        if entry_type != "memory" {
1713            return None;
1714        }
1715
1716        Some(Memory {
1717            id: vector.id.clone(),
1718            memory_type: serde_json::from_value(meta.get("memory_type")?.clone())
1719                .unwrap_or_default(),
1720            content: meta.get("content")?.as_str()?.to_string(),
1721            agent_id: meta.get("agent_id")?.as_str()?.to_string(),
1722            session_id: meta
1723                .get("session_id")
1724                .and_then(|v| v.as_str())
1725                .map(String::from),
1726            importance: meta
1727                .get("importance")
1728                .and_then(|v| v.as_f64())
1729                .unwrap_or(0.5) as f32,
1730            tags: meta
1731                .get("tags")
1732                .and_then(|v| serde_json::from_value(v.clone()).ok())
1733                .unwrap_or_default(),
1734            metadata: meta.get("user_metadata").cloned(),
1735            created_at: meta.get("created_at").and_then(|v| v.as_u64()).unwrap_or(0),
1736            last_accessed_at: meta
1737                .get("last_accessed_at")
1738                .and_then(|v| v.as_u64())
1739                .unwrap_or(0),
1740            access_count: meta
1741                .get("access_count")
1742                .and_then(|v| v.as_u64())
1743                .unwrap_or(0) as u32,
1744            ttl_seconds: vector.ttl_seconds,
1745            expires_at: vector.expires_at,
1746        })
1747    }
1748}
1749
1750/// An agent session
1751#[derive(Debug, Clone, Serialize, Deserialize)]
1752pub struct Session {
1753    pub id: String,
1754    pub agent_id: String,
1755    pub started_at: u64,
1756    #[serde(skip_serializing_if = "Option::is_none")]
1757    pub ended_at: Option<u64>,
1758    #[serde(skip_serializing_if = "Option::is_none")]
1759    pub summary: Option<String>,
1760    #[serde(skip_serializing_if = "Option::is_none")]
1761    pub metadata: Option<serde_json::Value>,
1762    /// Cached count of memories in this session (updated on store/forget)
1763    #[serde(default)]
1764    pub memory_count: usize,
1765}
1766
1767impl Session {
1768    pub fn new(id: String, agent_id: String) -> Self {
1769        let now = std::time::SystemTime::now()
1770            .duration_since(std::time::UNIX_EPOCH)
1771            .unwrap_or_default()
1772            .as_secs();
1773        Self {
1774            id,
1775            agent_id,
1776            started_at: now,
1777            ended_at: None,
1778            summary: None,
1779            metadata: None,
1780            memory_count: 0,
1781        }
1782    }
1783
1784    /// Pack session into metadata for Vector storage
1785    pub fn to_vector_metadata(&self) -> serde_json::Value {
1786        let mut meta = serde_json::Map::new();
1787        meta.insert("_dakera_type".to_string(), serde_json::json!("session"));
1788        meta.insert("agent_id".to_string(), serde_json::json!(self.agent_id));
1789        meta.insert("started_at".to_string(), serde_json::json!(self.started_at));
1790        if let Some(ref ended) = self.ended_at {
1791            meta.insert("ended_at".to_string(), serde_json::json!(ended));
1792        }
1793        if let Some(ref summary) = self.summary {
1794            meta.insert("summary".to_string(), serde_json::json!(summary));
1795        }
1796        if let Some(ref user_meta) = self.metadata {
1797            meta.insert("user_metadata".to_string(), user_meta.clone());
1798        }
1799        meta.insert(
1800            "memory_count".to_string(),
1801            serde_json::json!(self.memory_count),
1802        );
1803        serde_json::Value::Object(meta)
1804    }
1805
1806    /// Convert to a Vector for storage (use summary or agent_id as embedding source)
1807    pub fn to_vector(&self, embedding: Vec<f32>) -> Vector {
1808        Vector {
1809            id: self.id.clone(),
1810            values: embedding,
1811            metadata: Some(self.to_vector_metadata()),
1812            ttl_seconds: None,
1813            expires_at: None,
1814        }
1815    }
1816
1817    /// Reconstruct a Session from a Vector's metadata
1818    pub fn from_vector(vector: &Vector) -> Option<Self> {
1819        let meta = vector.metadata.as_ref()?.as_object()?;
1820        let entry_type = meta.get("_dakera_type")?.as_str()?;
1821        if entry_type != "session" {
1822            return None;
1823        }
1824
1825        Some(Session {
1826            id: vector.id.clone(),
1827            agent_id: meta.get("agent_id")?.as_str()?.to_string(),
1828            started_at: meta.get("started_at").and_then(|v| v.as_u64()).unwrap_or(0),
1829            ended_at: meta.get("ended_at").and_then(|v| v.as_u64()),
1830            summary: meta
1831                .get("summary")
1832                .and_then(|v| v.as_str())
1833                .map(String::from),
1834            metadata: meta.get("user_metadata").cloned(),
1835            memory_count: meta
1836                .get("memory_count")
1837                .and_then(|v| v.as_u64())
1838                .unwrap_or(0) as usize,
1839        })
1840    }
1841}
1842
1843/// Strategy for importance decay
1844#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
1845#[serde(rename_all = "snake_case")]
1846#[derive(Default)]
1847pub enum DecayStrategy {
1848    #[default]
1849    Exponential,
1850    Linear,
1851    StepFunction,
1852    /// Power-law decay: I(t) = I₀ / (1 + k·t)^α — natural for episodic memories
1853    PowerLaw,
1854    /// Logarithmic decay: I(t) = I₀ · (1 − log₂(1 + t/h)) — slow for semantic knowledge
1855    Logarithmic,
1856    /// Flat (no decay) — for procedural/skill memories
1857    Flat,
1858}
1859
1860/// Configuration for importance decay
1861#[derive(Debug, Clone, Serialize, Deserialize)]
1862pub struct DecayConfig {
1863    #[serde(default)]
1864    pub strategy: DecayStrategy,
1865    #[serde(default = "default_half_life_hours")]
1866    pub half_life_hours: f64,
1867    #[serde(default = "default_min_importance")]
1868    pub min_importance: f32,
1869}
1870
1871fn default_half_life_hours() -> f64 {
1872    168.0 // 1 week
1873}
1874
1875fn default_min_importance() -> f32 {
1876    0.01
1877}
1878
1879impl Default for DecayConfig {
1880    fn default() -> Self {
1881        Self {
1882            strategy: DecayStrategy::default(),
1883            half_life_hours: default_half_life_hours(),
1884            min_importance: default_min_importance(),
1885        }
1886    }
1887}
1888
1889// ============================================================================
1890// Dakera Memory Request/Response Types
1891// ============================================================================
1892
1893/// Request to store a memory
1894#[derive(Debug, Deserialize)]
1895pub struct StoreMemoryRequest {
1896    pub content: String,
1897    pub agent_id: String,
1898    #[serde(default)]
1899    pub memory_type: MemoryType,
1900    #[serde(skip_serializing_if = "Option::is_none")]
1901    pub session_id: Option<String>,
1902    #[serde(default = "default_importance")]
1903    pub importance: f32,
1904    #[serde(default)]
1905    pub tags: Vec<String>,
1906    #[serde(skip_serializing_if = "Option::is_none")]
1907    pub metadata: Option<serde_json::Value>,
1908    #[serde(skip_serializing_if = "Option::is_none")]
1909    pub ttl_seconds: Option<u64>,
1910    /// Optional explicit expiry Unix timestamp (seconds).
1911    /// If provided, takes precedence over ttl_seconds.
1912    /// On expiry the memory is hard-deleted by the decay engine, bypassing
1913    /// importance scoring.
1914    #[serde(skip_serializing_if = "Option::is_none")]
1915    pub expires_at: Option<u64>,
1916    /// Optional custom ID (auto-generated if not provided)
1917    #[serde(skip_serializing_if = "Option::is_none")]
1918    pub id: Option<String>,
1919}
1920
1921/// Response from storing a memory
1922#[derive(Debug, Serialize)]
1923pub struct StoreMemoryResponse {
1924    pub memory: Memory,
1925    pub embedding_time_ms: u64,
1926}
1927
1928/// CE-12: Routing mode for smart query dispatch.
1929///
1930/// Controls which retrieval backend(s) are used when recalling or searching memories.
1931#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
1932#[serde(rename_all = "lowercase")]
1933pub enum RoutingMode {
1934    /// Automatically select the best backend based on query characteristics (default).
1935    #[default]
1936    Auto,
1937    /// Force pure vector-similarity search (always embeds the query).
1938    Vector,
1939    /// Force pure BM25 full-text search (no embedding inference).
1940    Bm25,
1941    /// Force hybrid search: combine vector + BM25 with adaptive weighting.
1942    Hybrid,
1943}
1944
1945/// CE-14: Fusion strategy for hybrid search results.
1946///
1947/// Selects how vector-similarity and BM25 scores are combined when
1948/// `routing=hybrid` (or auto-classified as hybrid).
1949#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
1950#[serde(rename_all = "lowercase")]
1951pub enum FusionStrategy {
1952    /// Reciprocal Rank Fusion — Cormack et al., SIGIR 2009.
1953    /// Formula: score(d) = Σ 1 / (k + rank_r(d)), k=60.
1954    /// NOTE: RRF penalises memories that appear in only one retriever — catastrophic for
1955    /// temporal recall (A/B v0.11.1: 29.2% temporal vs 42.7% MinMax). Use MinMax instead.
1956    Rrf,
1957    /// Weighted min-max normalisation: combines BM25 and vector scores independently.
1958    /// Default since v0.11.2 — A/B benchmark shows +6.3pp overall, +13.5pp temporal vs RRF.
1959    #[default]
1960    MinMax,
1961}
1962
1963/// Request to recall memories by semantic query
1964#[derive(Debug, Deserialize)]
1965pub struct RecallRequest {
1966    pub query: String,
1967    pub agent_id: String,
1968    #[serde(default = "default_top_k")]
1969    pub top_k: usize,
1970    #[serde(default)]
1971    pub memory_type: Option<MemoryType>,
1972    #[serde(default)]
1973    pub session_id: Option<String>,
1974    #[serde(default)]
1975    pub tags: Option<Vec<String>>,
1976    #[serde(default)]
1977    pub min_importance: Option<f32>,
1978    /// Include importance-weighted re-ranking (default: true)
1979    #[serde(default = "default_true")]
1980    pub importance_weighted: bool,
1981    /// COG-2: traverse KG depth-1 from recalled memories and include associatively linked memories
1982    #[serde(default)]
1983    pub include_associated: bool,
1984    /// COG-2: max number of associated memories to return (default: 10, max: 10)
1985    #[serde(default)]
1986    pub associated_memories_cap: Option<usize>,
1987    /// CE-7: only include memories created at or after this ISO-8601 timestamp (e.g. "2024-01-01T00:00:00Z")
1988    #[serde(default)]
1989    pub since: Option<String>,
1990    /// CE-7: only include memories created at or before this ISO-8601 timestamp (e.g. "2024-12-31T23:59:59Z")
1991    #[serde(default)]
1992    pub until: Option<String>,
1993    /// KG-3: KG traversal depth for associative recall (1–3, default 1).
1994    /// Requires `include_associated: true`. Depth 1 = direct neighbours only (COG-2 behaviour).
1995    #[serde(default)]
1996    pub associated_memories_depth: Option<u8>,
1997    /// KG-3: minimum edge weight to traverse (0.0–1.0, default 0.0 = all edges).
1998    /// Requires `include_associated: true`.
1999    #[serde(default)]
2000    pub associated_memories_min_weight: Option<f32>,
2001    /// CE-12: retrieval routing mode.
2002    /// `auto` (default) classifies the query heuristically; `vector`/`bm25`/`hybrid`
2003    /// force a specific backend.
2004    #[serde(default)]
2005    pub routing: RoutingMode,
2006    /// CE-13: apply cross-encoder reranking after ANN candidate retrieval.
2007    /// Fetches `top_k * 3` candidates and rescores with `bge-reranker-base`.
2008    /// Default: `true` (improves recall precision significantly).
2009    #[serde(default = "default_true")]
2010    pub rerank: bool,
2011    /// CE-14: fusion strategy when routing=Hybrid.
2012    /// `rrf` (default) uses Reciprocal Rank Fusion; `minmax` uses weighted min-max normalization.
2013    #[serde(default)]
2014    pub fusion: FusionStrategy,
2015    /// CE-17: explicit vector weight for Hybrid routing (0.0–1.0).
2016    /// When set, overrides the adaptive heuristic from QueryClassifier.
2017    /// Omit to use adaptive defaults (recommended for most callers).
2018    #[serde(default)]
2019    pub vector_weight: Option<f32>,
2020    /// CE-23/CE-49: pseudo-relevance feedback iteration count.
2021    /// When `iterations >= 2`, a second pass is run with the original query augmented
2022    /// by entity/date terms from pass-1 results, merged via RRF (k=60).
2023    /// Default: 1 (single-pass). Max: 3. Ignored for pure vector routing.
2024    /// BM25 routing: auto-enables PRF for temporal queries (CE-35).
2025    /// Hybrid routing: PRF fires for temporal queries (auto) or when iterations >= 2 (CE-49).
2026    #[serde(default)]
2027    pub iterations: Option<u8>,
2028    /// v0.11.0 Phase 2: after main recall, fetch session-adjacent memories within ±5 min
2029    /// of each top result as context enrichment. Default: true.
2030    /// Set to false to disable neighborhood expansion (reduces latency, lower recall).
2031    #[serde(default = "default_true")]
2032    pub neighborhood: bool,
2033}
2034
2035/// Single recall result
2036#[derive(Debug, Serialize, Deserialize)]
2037pub struct RecallResult {
2038    pub memory: Memory,
2039    pub score: f32,
2040    /// Score after importance-weighted re-ranking
2041    #[serde(skip_serializing_if = "Option::is_none")]
2042    pub weighted_score: Option<f32>,
2043    /// Always-on multi-signal smart score (vector + importance + recency + frequency)
2044    #[serde(skip_serializing_if = "Option::is_none")]
2045    pub smart_score: Option<f32>,
2046    /// KG-3: traversal depth at which this memory was found (only set on associated_memories entries).
2047    /// 1 = direct neighbour of a primary result, 2 = two hops, 3 = three hops.
2048    #[serde(skip_serializing_if = "Option::is_none")]
2049    pub depth: Option<u8>,
2050}
2051
2052/// Response from recall
2053#[derive(Debug, Serialize)]
2054pub struct RecallResponse {
2055    pub memories: Vec<RecallResult>,
2056    pub query_embedding_time_ms: u64,
2057    pub search_time_ms: u64,
2058    /// COG-2: memories linked to recalled memories via KG depth-1 traversal.
2059    /// Only populated when `include_associated: true` in the request.
2060    #[serde(skip_serializing_if = "Option::is_none")]
2061    pub associated_memories: Option<Vec<RecallResult>>,
2062}
2063
2064/// Request to forget (delete) memories
2065#[derive(Debug, Deserialize)]
2066pub struct ForgetRequest {
2067    pub agent_id: String,
2068    #[serde(default)]
2069    pub memory_ids: Option<Vec<String>>,
2070    #[serde(default)]
2071    pub memory_type: Option<MemoryType>,
2072    #[serde(default)]
2073    pub session_id: Option<String>,
2074    #[serde(default)]
2075    pub tags: Option<Vec<String>>,
2076    /// Delete memories below this importance threshold
2077    #[serde(default)]
2078    pub below_importance: Option<f32>,
2079}
2080
2081/// Response from forget
2082#[derive(Debug, Serialize)]
2083pub struct ForgetResponse {
2084    pub deleted_count: usize,
2085}
2086
2087/// Request to update a memory
2088#[derive(Debug, Deserialize)]
2089pub struct UpdateMemoryRequest {
2090    #[serde(default)]
2091    pub content: Option<String>,
2092    #[serde(default)]
2093    pub importance: Option<f32>,
2094    #[serde(default)]
2095    pub tags: Option<Vec<String>>,
2096    #[serde(default)]
2097    pub metadata: Option<serde_json::Value>,
2098    #[serde(default)]
2099    pub memory_type: Option<MemoryType>,
2100}
2101
2102/// Request to update importance of a memory
2103#[derive(Debug, Deserialize)]
2104pub struct UpdateImportanceRequest {
2105    pub memory_id: String,
2106    pub importance: f32,
2107    pub agent_id: String,
2108}
2109
2110/// Request to consolidate related memories
2111#[derive(Debug, Deserialize)]
2112pub struct ConsolidateRequest {
2113    pub agent_id: String,
2114    /// Memory IDs to consolidate (if empty, auto-detect similar memories)
2115    #[serde(default)]
2116    pub memory_ids: Option<Vec<String>>,
2117    /// Similarity threshold for auto-detection (default: 0.85)
2118    #[serde(default = "default_consolidation_threshold")]
2119    pub threshold: f32,
2120    /// Type for the consolidated memory
2121    #[serde(default)]
2122    pub target_type: Option<MemoryType>,
2123}
2124
2125fn default_consolidation_threshold() -> f32 {
2126    0.85
2127}
2128
2129/// Response from consolidation
2130#[derive(Debug, Serialize)]
2131pub struct ConsolidateResponse {
2132    pub consolidated_memory: Memory,
2133    pub source_memory_ids: Vec<String>,
2134    pub memories_removed: usize,
2135}
2136
2137/// Feedback signal for active learning
2138#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
2139#[serde(rename_all = "lowercase")]
2140pub enum FeedbackSignal {
2141    /// Boost importance (×1.15, capped at 1.0). INT-1 canonical name.
2142    Upvote,
2143    /// Penalise importance (×0.85, floor 0.0). INT-1 canonical name.
2144    Downvote,
2145    /// Mark as irrelevant — sets `decay_flag=true`, no immediate importance change.
2146    Flag,
2147    /// Backward-compatible alias for `upvote`.
2148    Positive,
2149    /// Backward-compatible alias for `downvote`.
2150    Negative,
2151}
2152
2153/// One recorded feedback event stored in memory metadata (feedback_history).
2154#[derive(Debug, Clone, Serialize, Deserialize)]
2155pub struct FeedbackHistoryEntry {
2156    pub signal: FeedbackSignal,
2157    pub timestamp: u64,
2158    pub old_importance: f32,
2159    pub new_importance: f32,
2160}
2161
2162/// Request to provide feedback on a recalled memory (legacy — body contains memory_id)
2163#[derive(Debug, Deserialize)]
2164pub struct FeedbackRequest {
2165    pub agent_id: String,
2166    pub memory_id: String,
2167    pub signal: FeedbackSignal,
2168}
2169
2170/// Request for `POST /v1/memories/{id}/feedback` (INT-1 — memory_id in path)
2171#[derive(Debug, Deserialize)]
2172pub struct MemoryFeedbackRequest {
2173    pub agent_id: String,
2174    pub signal: FeedbackSignal,
2175}
2176
2177/// Response from feedback
2178#[derive(Debug, Serialize)]
2179pub struct FeedbackResponse {
2180    pub memory_id: String,
2181    pub new_importance: f32,
2182    pub signal: FeedbackSignal,
2183}
2184
2185/// Response from `GET /v1/memories/{id}/feedback`
2186#[derive(Debug, Serialize)]
2187pub struct FeedbackHistoryResponse {
2188    pub memory_id: String,
2189    pub entries: Vec<FeedbackHistoryEntry>,
2190}
2191
2192/// Response from `GET /v1/agents/{id}/feedback/summary`
2193#[derive(Debug, Serialize)]
2194pub struct AgentFeedbackSummary {
2195    pub agent_id: String,
2196    pub upvotes: u64,
2197    pub downvotes: u64,
2198    pub flags: u64,
2199    pub total_feedback: u64,
2200    /// Weighted-average importance across all non-expired memories (0.0–1.0).
2201    pub health_score: f32,
2202}
2203
2204/// Request for `PATCH /v1/memories/{id}/importance` (INT-1 — memory_id in path)
2205#[derive(Debug, Deserialize)]
2206pub struct MemoryImportancePatchRequest {
2207    pub agent_id: String,
2208    pub importance: f32,
2209}
2210
2211/// Query params for `GET /v1/feedback/health`
2212#[derive(Debug, Deserialize)]
2213pub struct FeedbackHealthQuery {
2214    pub agent_id: String,
2215}
2216
2217/// Response from `GET /v1/feedback/health`
2218#[derive(Debug, Serialize)]
2219pub struct FeedbackHealthResponse {
2220    pub agent_id: String,
2221    /// Mean importance of all non-expired memories (0.0–1.0). Higher = healthier.
2222    pub health_score: f32,
2223    pub memory_count: usize,
2224    pub avg_importance: f32,
2225}
2226
2227/// Request for advanced memory search
2228#[derive(Debug, Deserialize)]
2229pub struct SearchMemoriesRequest {
2230    pub agent_id: String,
2231    #[serde(default)]
2232    pub query: Option<String>,
2233    #[serde(default)]
2234    pub memory_type: Option<MemoryType>,
2235    #[serde(default)]
2236    pub session_id: Option<String>,
2237    #[serde(default)]
2238    pub tags: Option<Vec<String>>,
2239    #[serde(default)]
2240    pub min_importance: Option<f32>,
2241    #[serde(default)]
2242    pub max_importance: Option<f32>,
2243    #[serde(default)]
2244    pub created_after: Option<u64>,
2245    #[serde(default)]
2246    pub created_before: Option<u64>,
2247    #[serde(default = "default_top_k")]
2248    pub top_k: usize,
2249    #[serde(default)]
2250    pub sort_by: Option<MemorySortField>,
2251    /// CE-12: retrieval routing mode (auto-detected when not specified).
2252    #[serde(default)]
2253    pub routing: RoutingMode,
2254    /// CE-13: apply cross-encoder reranking on vector/hybrid query results.
2255    /// Default: `false` (search is typically used for browsing, not precision recall).
2256    #[serde(default)]
2257    pub rerank: bool,
2258}
2259
2260/// Fields to sort memories by
2261#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
2262#[serde(rename_all = "snake_case")]
2263pub enum MemorySortField {
2264    CreatedAt,
2265    LastAccessedAt,
2266    Importance,
2267    AccessCount,
2268}
2269
2270/// Response from memory search
2271#[derive(Debug, Serialize)]
2272pub struct SearchMemoriesResponse {
2273    pub memories: Vec<RecallResult>,
2274    pub total_count: usize,
2275}
2276
2277// ============================================================================
2278// Dakera Session Request/Response Types
2279// ============================================================================
2280
2281/// Request to start a session
2282#[derive(Debug, Deserialize)]
2283pub struct SessionStartRequest {
2284    pub agent_id: String,
2285    #[serde(skip_serializing_if = "Option::is_none")]
2286    pub metadata: Option<serde_json::Value>,
2287    /// Optional custom session ID
2288    #[serde(skip_serializing_if = "Option::is_none")]
2289    pub id: Option<String>,
2290}
2291
2292/// Response from starting a session
2293#[derive(Debug, Serialize)]
2294pub struct SessionStartResponse {
2295    pub session: Session,
2296}
2297
2298/// Request to end a session
2299#[derive(Debug, Deserialize)]
2300pub struct SessionEndRequest {
2301    #[serde(default)]
2302    pub summary: Option<String>,
2303    /// Auto-generate summary from session memories
2304    #[serde(default)]
2305    pub auto_summarize: bool,
2306}
2307
2308/// Response from ending a session
2309#[derive(Debug, Serialize)]
2310pub struct SessionEndResponse {
2311    pub session: Session,
2312    pub memory_count: usize,
2313}
2314
2315/// Response listing sessions
2316#[derive(Debug, Serialize)]
2317pub struct ListSessionsResponse {
2318    pub sessions: Vec<Session>,
2319    pub total: usize,
2320}
2321
2322/// Response for session memories
2323#[derive(Debug, Serialize)]
2324pub struct SessionMemoriesResponse {
2325    pub session: Session,
2326    pub memories: Vec<Memory>,
2327    /// Total number of memories in this session (before pagination)
2328    #[serde(skip_serializing_if = "Option::is_none")]
2329    pub total: Option<usize>,
2330}
2331
2332// ============================================================================
2333// Dakera Agent & Knowledge Types
2334// ============================================================================
2335
2336/// Lightweight agent summary for batch listing (uses count() not get_all)
2337#[derive(Debug, Serialize, Deserialize, Clone)]
2338pub struct AgentSummary {
2339    pub agent_id: String,
2340    pub memory_count: usize,
2341    pub session_count: usize,
2342    pub active_sessions: usize,
2343}
2344
2345/// Agent memory statistics
2346#[derive(Debug, Serialize)]
2347pub struct AgentStats {
2348    pub agent_id: String,
2349    pub total_memories: usize,
2350    pub memories_by_type: std::collections::HashMap<String, usize>,
2351    pub total_sessions: usize,
2352    pub active_sessions: usize,
2353    pub avg_importance: f32,
2354    pub oldest_memory_at: Option<u64>,
2355    pub newest_memory_at: Option<u64>,
2356}
2357
2358/// Response from `GET /v1/agents/{agent_id}/wake-up` (DAK-1690).
2359///
2360/// Returns the highest-scored memories for an agent using a pure metadata
2361/// sort (`importance × recency_weight`). No embedding inference is performed,
2362/// making this suitable for fast agent startup context loading.
2363#[derive(Debug, Serialize)]
2364pub struct WakeUpResponse {
2365    pub agent_id: String,
2366    /// Top-N memories sorted by `importance × recency_weight` descending.
2367    pub memories: Vec<Memory>,
2368    /// Total memories available before the top_n cap was applied.
2369    pub total_available: usize,
2370}
2371
2372/// Request for knowledge graph traversal
2373#[derive(Debug, Deserialize)]
2374pub struct KnowledgeGraphRequest {
2375    pub agent_id: String,
2376    pub memory_id: String,
2377    #[serde(default = "default_graph_depth")]
2378    pub depth: usize,
2379    #[serde(default = "default_graph_min_similarity")]
2380    pub min_similarity: f32,
2381}
2382
2383fn default_graph_depth() -> usize {
2384    2
2385}
2386
2387fn default_graph_min_similarity() -> f32 {
2388    0.7
2389}
2390
2391/// Knowledge graph node
2392#[derive(Debug, Serialize)]
2393pub struct KnowledgeGraphNode {
2394    pub memory: Memory,
2395    pub similarity: f32,
2396    pub related: Vec<KnowledgeGraphEdge>,
2397}
2398
2399/// Knowledge graph edge
2400#[derive(Debug, Serialize)]
2401pub struct KnowledgeGraphEdge {
2402    pub memory_id: String,
2403    pub similarity: f32,
2404    pub shared_tags: Vec<String>,
2405}
2406
2407/// Response from knowledge graph query
2408#[derive(Debug, Serialize)]
2409pub struct KnowledgeGraphResponse {
2410    pub root: KnowledgeGraphNode,
2411    pub total_nodes: usize,
2412}
2413
2414// ============================================================================
2415// Full Knowledge Graph Types (Global Network Topology)
2416// ============================================================================
2417
2418fn default_full_graph_max_nodes() -> usize {
2419    200
2420}
2421
2422fn default_full_graph_min_similarity() -> f32 {
2423    0.50
2424}
2425
2426fn default_full_graph_cluster_threshold() -> f32 {
2427    0.60
2428}
2429
2430fn default_full_graph_max_edges_per_node() -> usize {
2431    8
2432}
2433
2434/// Request for full knowledge graph (all memories, pairwise similarity)
2435#[derive(Debug, Deserialize)]
2436pub struct FullKnowledgeGraphRequest {
2437    pub agent_id: String,
2438    #[serde(default = "default_full_graph_max_nodes")]
2439    pub max_nodes: usize,
2440    #[serde(default = "default_full_graph_min_similarity")]
2441    pub min_similarity: f32,
2442    #[serde(default = "default_full_graph_cluster_threshold")]
2443    pub cluster_threshold: f32,
2444    #[serde(default = "default_full_graph_max_edges_per_node")]
2445    pub max_edges_per_node: usize,
2446}
2447
2448/// A node in the full knowledge graph
2449#[derive(Debug, Serialize)]
2450pub struct FullGraphNode {
2451    pub id: String,
2452    pub content: String,
2453    pub memory_type: String,
2454    pub importance: f32,
2455    pub tags: Vec<String>,
2456    pub created_at: Option<String>,
2457    pub cluster_id: usize,
2458    pub centrality: f32,
2459}
2460
2461/// An edge in the full knowledge graph
2462#[derive(Debug, Serialize)]
2463pub struct FullGraphEdge {
2464    pub source: String,
2465    pub target: String,
2466    pub similarity: f32,
2467    pub shared_tags: Vec<String>,
2468}
2469
2470/// A cluster of related memories
2471#[derive(Debug, Serialize)]
2472pub struct GraphCluster {
2473    pub id: usize,
2474    pub node_count: usize,
2475    pub top_tags: Vec<String>,
2476    pub avg_importance: f32,
2477}
2478
2479/// Statistics about the full knowledge graph
2480#[derive(Debug, Serialize)]
2481pub struct GraphStats {
2482    pub total_memories: usize,
2483    pub included_memories: usize,
2484    pub total_edges: usize,
2485    pub cluster_count: usize,
2486    pub density: f32,
2487    pub hub_memory_id: Option<String>,
2488}
2489
2490/// Response from full knowledge graph query
2491#[derive(Debug, Serialize)]
2492pub struct FullKnowledgeGraphResponse {
2493    pub nodes: Vec<FullGraphNode>,
2494    pub edges: Vec<FullGraphEdge>,
2495    pub clusters: Vec<GraphCluster>,
2496    pub stats: GraphStats,
2497}
2498
2499/// Request to summarize memories
2500#[derive(Debug, Deserialize)]
2501pub struct SummarizeRequest {
2502    pub agent_id: String,
2503    pub memory_ids: Vec<String>,
2504    #[serde(default)]
2505    pub target_type: Option<MemoryType>,
2506}
2507
2508/// Response from summarization
2509#[derive(Debug, Serialize)]
2510pub struct SummarizeResponse {
2511    pub summary_memory: Memory,
2512    pub source_count: usize,
2513}
2514
2515/// Request to deduplicate memories
2516#[derive(Debug, Deserialize)]
2517pub struct DeduplicateRequest {
2518    pub agent_id: String,
2519    #[serde(default = "default_dedup_threshold")]
2520    pub threshold: f32,
2521    #[serde(default)]
2522    pub memory_type: Option<MemoryType>,
2523    /// Dry run — report duplicates without merging
2524    #[serde(default)]
2525    pub dry_run: bool,
2526}
2527
2528fn default_dedup_threshold() -> f32 {
2529    0.92
2530}
2531
2532/// A group of duplicate memories
2533#[derive(Debug, Serialize)]
2534pub struct DuplicateGroup {
2535    pub canonical_id: String,
2536    pub duplicate_ids: Vec<String>,
2537    pub avg_similarity: f32,
2538}
2539
2540/// Response from deduplication
2541#[derive(Debug, Serialize)]
2542pub struct DeduplicateResponse {
2543    pub groups: Vec<DuplicateGroup>,
2544    pub duplicates_found: usize,
2545    pub duplicates_merged: usize,
2546}
2547
2548// ============================================================================
2549// Cross-Agent Memory Network Types (DASH-A)
2550// ============================================================================
2551
2552fn default_cross_agent_min_similarity() -> f32 {
2553    0.3
2554}
2555
2556fn default_cross_agent_max_nodes_per_agent() -> usize {
2557    50
2558}
2559
2560fn default_cross_agent_max_cross_edges() -> usize {
2561    200
2562}
2563
2564/// Request for cross-agent memory network graph
2565#[derive(Debug, Deserialize)]
2566pub struct CrossAgentNetworkRequest {
2567    /// Specific agent IDs to include (None = all agents)
2568    #[serde(default)]
2569    pub agent_ids: Option<Vec<String>>,
2570    /// Minimum cosine similarity for a cross-agent edge (default 0.3)
2571    #[serde(default = "default_cross_agent_min_similarity")]
2572    pub min_similarity: f32,
2573    /// Maximum memories per agent to include (top N by importance, default 50)
2574    #[serde(default = "default_cross_agent_max_nodes_per_agent")]
2575    pub max_nodes_per_agent: usize,
2576    /// Minimum importance score for a memory to be included (default 0.0)
2577    #[serde(default)]
2578    pub min_importance: f32,
2579    /// Maximum cross-agent edges to return (default 200)
2580    #[serde(default = "default_cross_agent_max_cross_edges")]
2581    pub max_cross_edges: usize,
2582}
2583
2584/// Summary info for an agent in the cross-agent network
2585#[derive(Debug, Serialize)]
2586pub struct AgentNetworkInfo {
2587    pub agent_id: String,
2588    pub memory_count: usize,
2589    pub avg_importance: f32,
2590}
2591
2592/// A memory node in the cross-agent network (includes agent_id)
2593#[derive(Debug, Serialize)]
2594pub struct AgentNetworkNode {
2595    pub id: String,
2596    pub agent_id: String,
2597    pub content: String,
2598    pub importance: f32,
2599    pub tags: Vec<String>,
2600    pub memory_type: String,
2601    pub created_at: u64,
2602}
2603
2604/// An edge between memories from two different agents
2605#[derive(Debug, Serialize)]
2606pub struct AgentNetworkEdge {
2607    pub source: String,
2608    pub target: String,
2609    pub source_agent: String,
2610    pub target_agent: String,
2611    pub similarity: f32,
2612}
2613
2614/// Statistics for the cross-agent network
2615#[derive(Debug, Serialize)]
2616pub struct AgentNetworkStats {
2617    pub total_agents: usize,
2618    pub total_nodes: usize,
2619    pub total_cross_edges: usize,
2620    pub density: f32,
2621}
2622
2623/// Response from cross-agent network query
2624#[derive(Debug, Serialize)]
2625pub struct CrossAgentNetworkResponse {
2626    pub node_count: usize,
2627    pub agents: Vec<AgentNetworkInfo>,
2628    pub nodes: Vec<AgentNetworkNode>,
2629    pub edges: Vec<AgentNetworkEdge>,
2630    pub stats: AgentNetworkStats,
2631}
2632
2633// ---------------------------------------------------------------------------
2634// CE-2: Batch recall / forget types
2635// ---------------------------------------------------------------------------
2636
2637/// Filter predicates for batch memory operations.
2638///
2639/// At least one field must be set for forget operations (safety guard).
2640#[derive(Debug, Deserialize, Default)]
2641pub struct BatchMemoryFilter {
2642    /// Restrict to memories that carry **all** listed tags.
2643    #[serde(default)]
2644    pub tags: Option<Vec<String>>,
2645    /// Minimum importance (inclusive).
2646    #[serde(default)]
2647    pub min_importance: Option<f32>,
2648    /// Maximum importance (inclusive).
2649    #[serde(default)]
2650    pub max_importance: Option<f32>,
2651    /// Only memories created at or after this Unix timestamp (seconds).
2652    #[serde(default)]
2653    pub created_after: Option<u64>,
2654    /// Only memories created before or at this Unix timestamp (seconds).
2655    #[serde(default)]
2656    pub created_before: Option<u64>,
2657    /// Restrict to a specific memory type.
2658    #[serde(default)]
2659    pub memory_type: Option<MemoryType>,
2660    /// Restrict to memories from a specific session.
2661    #[serde(default)]
2662    pub session_id: Option<String>,
2663}
2664
2665impl BatchMemoryFilter {
2666    /// Returns `true` if the filter has at least one constraint set.
2667    pub fn has_any(&self) -> bool {
2668        self.tags.is_some()
2669            || self.min_importance.is_some()
2670            || self.max_importance.is_some()
2671            || self.created_after.is_some()
2672            || self.created_before.is_some()
2673            || self.memory_type.is_some()
2674            || self.session_id.is_some()
2675    }
2676
2677    /// Returns `true` if the given memory matches all active filter predicates.
2678    pub fn matches(&self, memory: &Memory) -> bool {
2679        if let Some(ref tags) = self.tags {
2680            if !tags.is_empty() && !tags.iter().all(|t| memory.tags.contains(t)) {
2681                return false;
2682            }
2683        }
2684        if let Some(min) = self.min_importance {
2685            if memory.importance < min {
2686                return false;
2687            }
2688        }
2689        if let Some(max) = self.max_importance {
2690            if memory.importance > max {
2691                return false;
2692            }
2693        }
2694        if let Some(after) = self.created_after {
2695            if memory.created_at < after {
2696                return false;
2697            }
2698        }
2699        if let Some(before) = self.created_before {
2700            if memory.created_at > before {
2701                return false;
2702            }
2703        }
2704        if let Some(ref mt) = self.memory_type {
2705            if memory.memory_type != *mt {
2706                return false;
2707            }
2708        }
2709        if let Some(ref sid) = self.session_id {
2710            if memory.session_id.as_ref() != Some(sid) {
2711                return false;
2712            }
2713        }
2714        true
2715    }
2716}
2717
2718/// Request for `POST /v1/memories/recall/batch`
2719#[derive(Debug, Deserialize)]
2720pub struct BatchRecallRequest {
2721    /// Agent whose memory namespace to search.
2722    pub agent_id: String,
2723    /// Filter predicates to apply.
2724    #[serde(default)]
2725    pub filter: BatchMemoryFilter,
2726    /// Maximum number of results to return (default: 100).
2727    #[serde(default = "default_batch_limit")]
2728    pub limit: usize,
2729}
2730
2731fn default_batch_limit() -> usize {
2732    100
2733}
2734
2735/// Response from `POST /v1/memories/recall/batch`
2736#[derive(Debug, Serialize)]
2737pub struct BatchRecallResponse {
2738    pub memories: Vec<Memory>,
2739    pub total: usize,
2740    pub filtered: usize,
2741}
2742
2743/// Request for `DELETE /v1/memories/forget/batch`
2744#[derive(Debug, Deserialize)]
2745pub struct BatchForgetRequest {
2746    /// Agent whose memory namespace to purge from.
2747    pub agent_id: String,
2748    /// Filter predicates — **at least one must be set** (safety guard).
2749    pub filter: BatchMemoryFilter,
2750}
2751
2752/// Response from `DELETE /v1/memories/forget/batch`
2753#[derive(Debug, Serialize)]
2754pub struct BatchForgetResponse {
2755    pub deleted_count: usize,
2756}
2757
2758// ─────────────────────────────────────────────────────────────────────────────
2759// CE-4 — Entity extraction types
2760// ─────────────────────────────────────────────────────────────────────────────
2761
2762/// Request to update entity extraction config for a namespace.
2763/// `PATCH /v1/namespaces/{namespace}/config`
2764#[derive(Debug, Deserialize)]
2765pub struct NamespaceEntityConfigRequest {
2766    /// Enable or disable entity extraction for this namespace.
2767    pub extract_entities: bool,
2768    /// Entity types to extract via GLiNER (e.g. ["person","org","location"]).
2769    /// If empty and extract_entities=true, only the rule-based pre-pass runs.
2770    #[serde(default)]
2771    pub entity_types: Vec<String>,
2772}
2773
2774/// Response from `PATCH /v1/namespaces/{namespace}/config`
2775#[derive(Debug, Serialize, Deserialize)]
2776pub struct NamespaceEntityConfigResponse {
2777    pub namespace: String,
2778    pub extract_entities: bool,
2779    pub entity_types: Vec<String>,
2780}
2781
2782/// Request to extract entities from content without storing.
2783/// `POST /v1/memories/extract`
2784#[derive(Debug, Deserialize)]
2785pub struct ExtractEntitiesRequest {
2786    /// Text content to extract entities from.
2787    pub content: String,
2788    /// Entity types for GLiNER inference (optional).
2789    /// If omitted, only the rule-based pre-pass runs.
2790    #[serde(default)]
2791    pub entity_types: Vec<String>,
2792}
2793
2794/// A single extracted entity (shared with inference crate — mirrored here for API types).
2795#[derive(Debug, Clone, Serialize, Deserialize)]
2796pub struct EntityResult {
2797    pub entity_type: String,
2798    pub value: String,
2799    pub score: f32,
2800    pub start: usize,
2801    pub end: usize,
2802    /// Canonical tag form: `entity:<type>:<value>`
2803    pub tag: String,
2804}
2805
2806/// Response from `POST /v1/memories/extract` and `GET /v1/memories/{id}/entities`
2807#[derive(Debug, Serialize)]
2808pub struct ExtractEntitiesResponse {
2809    pub entities: Vec<EntityResult>,
2810    pub count: usize,
2811}
2812
2813// ============================================================================
2814// CE-5: Memory Knowledge Graph — request / response types
2815// ============================================================================
2816
2817/// GET /v1/memories/:id/graph
2818#[derive(Debug, Deserialize)]
2819pub struct GraphTraverseQuery {
2820    /// BFS depth limit (default 3, max 5).
2821    #[serde(default = "default_ce5_graph_depth")]
2822    pub depth: u32,
2823}
2824
2825fn default_ce5_graph_depth() -> u32 {
2826    3
2827}
2828
2829/// GET /v1/memories/:id/path
2830#[derive(Debug, Deserialize)]
2831pub struct GraphPathQuery {
2832    /// Target memory ID.
2833    pub to: String,
2834}
2835
2836/// POST /v1/memories/:id/links — create an explicit edge
2837#[derive(Debug, Deserialize)]
2838pub struct MemoryLinkRequest {
2839    /// The other memory ID to link to.
2840    pub target_id: String,
2841    /// Optional human-readable label (stored as `linked_by` edge).
2842    #[serde(skip_serializing_if = "Option::is_none")]
2843    pub label: Option<String>,
2844    /// Agent ID (for authorization).
2845    pub agent_id: String,
2846}
2847
2848/// Response from graph traversal.
2849#[derive(Debug, Serialize)]
2850pub struct GraphTraverseResponse {
2851    pub root_id: String,
2852    pub depth: u32,
2853    pub node_count: usize,
2854    pub nodes: Vec<GraphNodeResponse>,
2855}
2856
2857/// A single node in a graph traversal response.
2858#[derive(Debug, Serialize)]
2859pub struct GraphNodeResponse {
2860    pub memory_id: String,
2861    pub depth: u32,
2862    pub edges: Vec<GraphEdgeResponse>,
2863}
2864
2865/// A single edge in a graph response.
2866#[derive(Debug, Serialize)]
2867pub struct GraphEdgeResponse {
2868    pub from_id: String,
2869    pub to_id: String,
2870    pub edge_type: String,
2871    pub weight: f32,
2872    pub created_at: u64,
2873}
2874
2875/// Response from shortest-path query.
2876#[derive(Debug, Serialize)]
2877pub struct GraphPathResponse {
2878    pub from_id: String,
2879    pub to_id: String,
2880    /// Ordered list of memory IDs along the shortest path (inclusive).
2881    pub path: Vec<String>,
2882    pub hop_count: usize,
2883}
2884
2885/// Response from explicit link creation.
2886#[derive(Debug, Serialize)]
2887pub struct MemoryLinkResponse {
2888    pub from_id: String,
2889    pub to_id: String,
2890    pub edge_type: String,
2891}
2892
2893/// Response from agent graph export.
2894#[derive(Debug, Serialize)]
2895pub struct GraphExportResponse {
2896    pub agent_id: String,
2897    pub namespace: String,
2898    pub node_count: usize,
2899    pub edge_count: usize,
2900    pub edges: Vec<GraphEdgeResponse>,
2901}
2902
2903// ============================================================================
2904// KG-2: Graph Query & Export — request / response types
2905// ============================================================================
2906
2907/// GET /v1/knowledge/query — JSON DSL for graph filtering/traversal
2908#[derive(Debug, Deserialize)]
2909pub struct KgQueryParams {
2910    /// Agent ID whose graph to query (required).
2911    pub agent_id: String,
2912    /// Optional root memory ID — if set, performs BFS from this node first.
2913    #[serde(default)]
2914    pub root_id: Option<String>,
2915    /// Filter edges by type (comma-separated, e.g. "related_to,shares_entity").
2916    #[serde(default)]
2917    pub edge_type: Option<String>,
2918    /// Minimum edge weight (0.0–1.0).
2919    #[serde(default)]
2920    pub min_weight: Option<f32>,
2921    /// BFS depth when root_id is set (1–5, default 3).
2922    #[serde(default = "default_kg_depth")]
2923    pub max_depth: u32,
2924    /// Maximum number of edges to return (default 100, max 1000).
2925    #[serde(default = "default_kg_limit")]
2926    pub limit: usize,
2927}
2928
2929fn default_kg_depth() -> u32 {
2930    3
2931}
2932
2933fn default_kg_limit() -> usize {
2934    100
2935}
2936
2937/// Response from GET /v1/knowledge/query
2938#[derive(Debug, Serialize)]
2939pub struct KgQueryResponse {
2940    pub agent_id: String,
2941    pub node_count: usize,
2942    pub edge_count: usize,
2943    pub edges: Vec<GraphEdgeResponse>,
2944}
2945
2946/// GET /v1/knowledge/path — shortest path between two memory IDs
2947#[derive(Debug, Deserialize)]
2948pub struct KgPathParams {
2949    /// Agent ID for authorization.
2950    pub agent_id: String,
2951    /// Source memory ID.
2952    pub from: String,
2953    /// Target memory ID.
2954    pub to: String,
2955}
2956
2957/// Response from GET /v1/knowledge/path
2958#[derive(Debug, Serialize)]
2959pub struct KgPathResponse {
2960    pub agent_id: String,
2961    pub from_id: String,
2962    pub to_id: String,
2963    pub hop_count: usize,
2964    pub path: Vec<String>,
2965}
2966
2967/// GET /v1/knowledge/export — export graph as JSON or GraphML
2968#[derive(Debug, Deserialize)]
2969pub struct KgExportParams {
2970    /// Agent ID whose graph to export.
2971    pub agent_id: String,
2972    /// Export format: "json" (default) or "graphml".
2973    #[serde(default = "default_kg_format")]
2974    pub format: String,
2975}
2976
2977fn default_kg_format() -> String {
2978    "json".to_string()
2979}
2980
2981/// Response from GET /v1/knowledge/export (format=json)
2982#[derive(Debug, Serialize)]
2983pub struct KgExportJsonResponse {
2984    pub agent_id: String,
2985    pub format: String,
2986    pub node_count: usize,
2987    pub edge_count: usize,
2988    pub edges: Vec<GraphEdgeResponse>,
2989}
2990
2991// ============================================================================
2992// COG-1: Cognitive Memory Lifecycle — per-namespace memory policy
2993// ============================================================================
2994
2995fn default_working_ttl() -> Option<u64> {
2996    Some(14_400) // 4 hours
2997}
2998fn default_episodic_ttl() -> Option<u64> {
2999    Some(2_592_000) // 30 days
3000}
3001fn default_semantic_ttl() -> Option<u64> {
3002    Some(31_536_000) // 365 days
3003}
3004fn default_procedural_ttl() -> Option<u64> {
3005    Some(63_072_000) // 730 days
3006}
3007fn default_working_decay() -> DecayStrategy {
3008    DecayStrategy::Exponential
3009}
3010fn default_episodic_decay() -> DecayStrategy {
3011    DecayStrategy::PowerLaw
3012}
3013fn default_semantic_decay() -> DecayStrategy {
3014    DecayStrategy::Logarithmic
3015}
3016fn default_procedural_decay() -> DecayStrategy {
3017    DecayStrategy::Flat
3018}
3019fn default_sr_factor() -> f64 {
3020    1.0
3021}
3022fn default_sr_base_interval() -> u64 {
3023    86_400 // 1 day
3024}
3025fn default_consolidation_enabled() -> bool {
3026    false
3027}
3028fn default_policy_consolidation_threshold() -> f32 {
3029    0.92
3030}
3031fn default_consolidation_interval_hours() -> u32 {
3032    24
3033}
3034fn default_store_dedup_threshold() -> f32 {
3035    0.95
3036}
3037
3038/// Per-namespace memory lifecycle policy (COG-1).
3039///
3040/// Controls type-specific TTLs, decay curves, and spaced repetition behaviour.
3041/// All fields have sensible defaults; only override what you need.
3042#[derive(Debug, Clone, Serialize, Deserialize)]
3043pub struct MemoryPolicy {
3044    // ── Differential TTLs ────────────────────────────────────────────────────
3045    /// Default TTL for `working` memories in seconds (default: 4 h = 14 400 s).
3046    #[serde(
3047        default = "default_working_ttl",
3048        skip_serializing_if = "Option::is_none"
3049    )]
3050    pub working_ttl_seconds: Option<u64>,
3051    /// Default TTL for `episodic` memories in seconds (default: 30 d = 2 592 000 s).
3052    #[serde(
3053        default = "default_episodic_ttl",
3054        skip_serializing_if = "Option::is_none"
3055    )]
3056    pub episodic_ttl_seconds: Option<u64>,
3057    /// Default TTL for `semantic` memories in seconds (default: 365 d = 31 536 000 s).
3058    #[serde(
3059        default = "default_semantic_ttl",
3060        skip_serializing_if = "Option::is_none"
3061    )]
3062    pub semantic_ttl_seconds: Option<u64>,
3063    /// Default TTL for `procedural` memories in seconds (default: 730 d = 63 072 000 s).
3064    #[serde(
3065        default = "default_procedural_ttl",
3066        skip_serializing_if = "Option::is_none"
3067    )]
3068    pub procedural_ttl_seconds: Option<u64>,
3069
3070    // ── Decay curves ─────────────────────────────────────────────────────────
3071    /// Decay strategy for `working` memories (default: exponential).
3072    #[serde(default = "default_working_decay")]
3073    pub working_decay: DecayStrategy,
3074    /// Decay strategy for `episodic` memories (default: power_law).
3075    #[serde(default = "default_episodic_decay")]
3076    pub episodic_decay: DecayStrategy,
3077    /// Decay strategy for `semantic` memories (default: logarithmic).
3078    #[serde(default = "default_semantic_decay")]
3079    pub semantic_decay: DecayStrategy,
3080    /// Decay strategy for `procedural` memories (default: flat — no decay).
3081    #[serde(default = "default_procedural_decay")]
3082    pub procedural_decay: DecayStrategy,
3083
3084    // ── Spaced repetition ────────────────────────────────────────────────────
3085    /// Multiplier applied to the TTL extension on each recall.
3086    /// Extension = `access_count × sr_factor × sr_base_interval_seconds`.
3087    /// Set to 0.0 to disable spaced repetition. (default: 1.0)
3088    #[serde(default = "default_sr_factor")]
3089    pub spaced_repetition_factor: f64,
3090    /// Base interval in seconds for spaced repetition TTL extension (default: 86 400 = 1 day).
3091    #[serde(default = "default_sr_base_interval")]
3092    pub spaced_repetition_base_interval_seconds: u64,
3093
3094    // ── COG-3: Proactive consolidation ───────────────────────────────────────
3095    /// Enable background deduplication of semantically similar memories (default: false).
3096    #[serde(default = "default_consolidation_enabled")]
3097    pub consolidation_enabled: bool,
3098    /// Cosine-similarity threshold for merging memories (default: 0.92, range 0.85–0.99).
3099    #[serde(default = "default_policy_consolidation_threshold")]
3100    pub consolidation_threshold: f32,
3101    /// How often the background consolidation job runs, in hours (default: 24).
3102    #[serde(default = "default_consolidation_interval_hours")]
3103    pub consolidation_interval_hours: u32,
3104    /// Total number of memories merged since namespace creation (read-only).
3105    #[serde(default)]
3106    pub consolidated_count: u64,
3107
3108    // ── SEC-5: Per-namespace rate limiting ───────────────────────────────────
3109    /// Master rate-limit switch (default: false — opt-in to avoid breaking existing clients).
3110    /// Set to `true` to enforce `rate_limit_stores_per_minute` / `rate_limit_recalls_per_minute`.
3111    #[serde(default)]
3112    pub rate_limit_enabled: bool,
3113    /// Maximum `POST /v1/memory/store` operations per minute for this namespace.
3114    /// `None` = unlimited. Only enforced when `rate_limit_enabled = true`.
3115    #[serde(default, skip_serializing_if = "Option::is_none")]
3116    pub rate_limit_stores_per_minute: Option<u32>,
3117    /// Maximum `POST /v1/memory/recall` operations per minute for this namespace.
3118    /// `None` = unlimited. Only enforced when `rate_limit_enabled = true`.
3119    #[serde(default, skip_serializing_if = "Option::is_none")]
3120    pub rate_limit_recalls_per_minute: Option<u32>,
3121
3122    // ── CE-10a: Store-time deduplication ─────────────────────────────────────
3123    /// Enable near-duplicate detection on every `store` call (default: false).
3124    ///
3125    /// When enabled, a quick vector-search (top-1) runs after embedding; if the
3126    /// nearest neighbour has cosine similarity ≥ 0.95 the new store is rejected
3127    /// and the existing memory ID is returned instead.  Adds one ANN query to
3128    /// every store operation — keep disabled for high-throughput namespaces.
3129    #[serde(default)]
3130    pub dedup_on_store: bool,
3131    /// Similarity threshold for store-time deduplication (default: 0.95).
3132    #[serde(default = "default_store_dedup_threshold")]
3133    pub dedup_threshold: f32,
3134}
3135
3136impl Default for MemoryPolicy {
3137    fn default() -> Self {
3138        Self {
3139            working_ttl_seconds: default_working_ttl(),
3140            episodic_ttl_seconds: default_episodic_ttl(),
3141            semantic_ttl_seconds: default_semantic_ttl(),
3142            procedural_ttl_seconds: default_procedural_ttl(),
3143            working_decay: default_working_decay(),
3144            episodic_decay: default_episodic_decay(),
3145            semantic_decay: default_semantic_decay(),
3146            procedural_decay: default_procedural_decay(),
3147            spaced_repetition_factor: default_sr_factor(),
3148            spaced_repetition_base_interval_seconds: default_sr_base_interval(),
3149            consolidation_enabled: default_consolidation_enabled(),
3150            consolidation_threshold: default_policy_consolidation_threshold(),
3151            consolidation_interval_hours: default_consolidation_interval_hours(),
3152            consolidated_count: 0,
3153            rate_limit_enabled: false,
3154            rate_limit_stores_per_minute: None,
3155            rate_limit_recalls_per_minute: None,
3156            dedup_on_store: false,
3157            dedup_threshold: default_store_dedup_threshold(),
3158        }
3159    }
3160}
3161
3162impl MemoryPolicy {
3163    /// Return the configured TTL for the given memory type, in seconds.
3164    pub fn ttl_for_type(&self, memory_type: &MemoryType) -> Option<u64> {
3165        match memory_type {
3166            MemoryType::Working => self.working_ttl_seconds,
3167            MemoryType::Episodic => self.episodic_ttl_seconds,
3168            MemoryType::Semantic => self.semantic_ttl_seconds,
3169            MemoryType::Procedural => self.procedural_ttl_seconds,
3170        }
3171    }
3172
3173    /// Return the configured decay strategy for the given memory type.
3174    pub fn decay_for_type(&self, memory_type: &MemoryType) -> DecayStrategy {
3175        match memory_type {
3176            MemoryType::Working => self.working_decay,
3177            MemoryType::Episodic => self.episodic_decay,
3178            MemoryType::Semantic => self.semantic_decay,
3179            MemoryType::Procedural => self.procedural_decay,
3180        }
3181    }
3182
3183    /// Compute the spaced repetition TTL extension in seconds.
3184    ///
3185    /// `extension = access_count × sr_factor × sr_base_interval_seconds`
3186    pub fn spaced_repetition_extension(&self, access_count: u32) -> u64 {
3187        if self.spaced_repetition_factor <= 0.0 {
3188            return 0;
3189        }
3190        let ext = access_count as f64
3191            * self.spaced_repetition_factor
3192            * self.spaced_repetition_base_interval_seconds as f64;
3193        ext.round() as u64
3194    }
3195}