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
719/// A filter expression that can be a single field condition or a logical combinator
720#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
721#[serde(untagged)]
722pub enum FilterExpression {
723    /// Logical AND of multiple expressions
724    And {
725        #[serde(rename = "$and")]
726        conditions: Vec<FilterExpression>,
727    },
728    /// Logical OR of multiple expressions
729    Or {
730        #[serde(rename = "$or")]
731        conditions: Vec<FilterExpression>,
732    },
733    /// Single field condition
734    Field {
735        #[serde(flatten)]
736        field: std::collections::HashMap<String, FilterCondition>,
737    },
738}
739
740// ============================================================================
741// Namespace quota types
742// ============================================================================
743
744/// Quota configuration for a namespace
745#[derive(Debug, Clone, Serialize, Deserialize, Default)]
746pub struct QuotaConfig {
747    /// Maximum number of vectors allowed (None = unlimited)
748    #[serde(skip_serializing_if = "Option::is_none")]
749    pub max_vectors: Option<u64>,
750    /// Maximum storage size in bytes (None = unlimited)
751    #[serde(skip_serializing_if = "Option::is_none")]
752    pub max_storage_bytes: Option<u64>,
753    /// Maximum dimensions per vector (None = unlimited)
754    #[serde(skip_serializing_if = "Option::is_none")]
755    pub max_dimensions: Option<usize>,
756    /// Maximum metadata size per vector in bytes (None = unlimited)
757    #[serde(skip_serializing_if = "Option::is_none")]
758    pub max_metadata_bytes: Option<usize>,
759    /// Whether to enforce quotas (soft limit = warn only, hard = reject)
760    #[serde(default)]
761    pub enforcement: QuotaEnforcement,
762}
763
764/// Quota enforcement mode
765#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default, PartialEq, Eq)]
766#[serde(rename_all = "snake_case")]
767pub enum QuotaEnforcement {
768    /// No enforcement, just tracking
769    None,
770    /// Log warnings when quota exceeded but allow operations
771    Soft,
772    /// Reject operations that would exceed quota
773    #[default]
774    Hard,
775}
776
777/// Current quota usage for a namespace
778#[derive(Debug, Clone, Serialize, Deserialize, Default)]
779pub struct QuotaUsage {
780    /// Current number of vectors
781    pub vector_count: u64,
782    /// Current storage size in bytes (estimated)
783    pub storage_bytes: u64,
784    /// Average vector dimensions
785    pub avg_dimensions: Option<usize>,
786    /// Average metadata size in bytes
787    pub avg_metadata_bytes: Option<usize>,
788    /// Last updated timestamp (Unix epoch)
789    pub last_updated: u64,
790}
791
792impl QuotaUsage {
793    /// Create new usage with current timestamp
794    pub fn new(vector_count: u64, storage_bytes: u64) -> Self {
795        let now = std::time::SystemTime::now()
796            .duration_since(std::time::UNIX_EPOCH)
797            .unwrap_or_default()
798            .as_secs();
799        Self {
800            vector_count,
801            storage_bytes,
802            avg_dimensions: None,
803            avg_metadata_bytes: None,
804            last_updated: now,
805        }
806    }
807
808    /// Update the timestamp to now
809    pub fn touch(&mut self) {
810        self.last_updated = std::time::SystemTime::now()
811            .duration_since(std::time::UNIX_EPOCH)
812            .unwrap_or_default()
813            .as_secs();
814    }
815}
816
817/// Combined quota status showing config and current usage
818#[derive(Debug, Clone, Serialize, Deserialize)]
819pub struct QuotaStatus {
820    /// Namespace name
821    pub namespace: String,
822    /// Quota configuration
823    pub config: QuotaConfig,
824    /// Current usage
825    pub usage: QuotaUsage,
826    /// Percentage of vector quota used (0-100, None if unlimited)
827    #[serde(skip_serializing_if = "Option::is_none")]
828    pub vector_usage_percent: Option<f32>,
829    /// Percentage of storage quota used (0-100, None if unlimited)
830    #[serde(skip_serializing_if = "Option::is_none")]
831    pub storage_usage_percent: Option<f32>,
832    /// Whether any quota is exceeded
833    pub is_exceeded: bool,
834    /// List of exceeded quota types
835    #[serde(skip_serializing_if = "Vec::is_empty")]
836    pub exceeded_quotas: Vec<String>,
837}
838
839impl QuotaStatus {
840    /// Create a new quota status from config and usage
841    pub fn new(namespace: String, config: QuotaConfig, usage: QuotaUsage) -> Self {
842        let vector_usage_percent = config
843            .max_vectors
844            .map(|max| (usage.vector_count as f32 / max as f32) * 100.0);
845
846        let storage_usage_percent = config
847            .max_storage_bytes
848            .map(|max| (usage.storage_bytes as f32 / max as f32) * 100.0);
849
850        let mut exceeded_quotas = Vec::new();
851
852        if let Some(max) = config.max_vectors {
853            if usage.vector_count > max {
854                exceeded_quotas.push("max_vectors".to_string());
855            }
856        }
857
858        if let Some(max) = config.max_storage_bytes {
859            if usage.storage_bytes > max {
860                exceeded_quotas.push("max_storage_bytes".to_string());
861            }
862        }
863
864        let is_exceeded = !exceeded_quotas.is_empty();
865
866        Self {
867            namespace,
868            config,
869            usage,
870            vector_usage_percent,
871            storage_usage_percent,
872            is_exceeded,
873            exceeded_quotas,
874        }
875    }
876}
877
878/// Request to set quota for a namespace
879#[derive(Debug, Deserialize)]
880pub struct SetQuotaRequest {
881    /// Quota configuration to apply
882    pub config: QuotaConfig,
883}
884
885/// Response from setting quota
886#[derive(Debug, Serialize)]
887pub struct SetQuotaResponse {
888    /// Whether the operation succeeded
889    pub success: bool,
890    /// Namespace name
891    pub namespace: String,
892    /// Applied quota configuration
893    pub config: QuotaConfig,
894    /// Status message
895    pub message: String,
896}
897
898/// Quota check result
899#[derive(Debug, Clone, Serialize)]
900pub struct QuotaCheckResult {
901    /// Whether the operation is allowed
902    pub allowed: bool,
903    /// Reason if not allowed
904    #[serde(skip_serializing_if = "Option::is_none")]
905    pub reason: Option<String>,
906    /// Current usage
907    pub usage: QuotaUsage,
908    /// Quota that would be exceeded
909    #[serde(skip_serializing_if = "Option::is_none")]
910    pub exceeded_quota: Option<String>,
911}
912
913/// Response listing all namespace quotas
914#[derive(Debug, Serialize)]
915pub struct QuotaListResponse {
916    /// List of quota statuses per namespace
917    pub quotas: Vec<QuotaStatus>,
918    /// Total number of namespaces with quotas
919    pub total: u64,
920    /// Default quota configuration (if set)
921    #[serde(skip_serializing_if = "Option::is_none")]
922    pub default_config: Option<QuotaConfig>,
923}
924
925/// Response for default quota query
926#[derive(Debug, Serialize)]
927pub struct DefaultQuotaResponse {
928    /// Default quota configuration (None if not set)
929    pub config: Option<QuotaConfig>,
930}
931
932/// Request to set default quota configuration
933#[derive(Debug, Deserialize)]
934pub struct SetDefaultQuotaRequest {
935    /// Default quota configuration (None to remove)
936    pub config: Option<QuotaConfig>,
937}
938
939/// Request to check if an operation would exceed quota
940#[derive(Debug, Deserialize)]
941pub struct QuotaCheckRequest {
942    /// Vector IDs to check (simulated vectors)
943    pub vector_ids: Vec<String>,
944    /// Dimension of vectors (for size estimation)
945    #[serde(default)]
946    pub dimensions: Option<usize>,
947    /// Estimated metadata size per vector
948    #[serde(default)]
949    pub metadata_bytes: Option<usize>,
950}
951
952// ============================================================================
953// Export API Types (Turbopuffer-inspired)
954// ============================================================================
955
956/// Request to export vectors from a namespace with pagination
957#[derive(Debug, Deserialize)]
958pub struct ExportRequest {
959    /// Maximum number of vectors to return per page (default: 1000, max: 10000)
960    #[serde(default = "default_export_top_k")]
961    pub top_k: usize,
962    /// Cursor for pagination - the last vector ID from previous page
963    #[serde(skip_serializing_if = "Option::is_none")]
964    pub cursor: Option<String>,
965    /// Whether to include vector values in the response (default: true)
966    #[serde(default = "default_true")]
967    pub include_vectors: bool,
968    /// Whether to include metadata in the response (default: true)
969    #[serde(default = "default_true")]
970    pub include_metadata: bool,
971}
972
973fn default_export_top_k() -> usize {
974    1000
975}
976
977impl Default for ExportRequest {
978    fn default() -> Self {
979        Self {
980            top_k: 1000,
981            cursor: None,
982            include_vectors: true,
983            include_metadata: true,
984        }
985    }
986}
987
988/// A single exported vector record
989#[derive(Debug, Clone, Serialize, Deserialize)]
990pub struct ExportedVector {
991    /// Vector ID
992    pub id: String,
993    /// Vector values (optional based on include_vectors)
994    #[serde(skip_serializing_if = "Option::is_none")]
995    pub values: Option<Vec<f32>>,
996    /// Metadata (optional based on include_metadata)
997    #[serde(skip_serializing_if = "Option::is_none")]
998    pub metadata: Option<serde_json::Value>,
999    /// TTL in seconds if set
1000    #[serde(skip_serializing_if = "Option::is_none")]
1001    pub ttl_seconds: Option<u64>,
1002}
1003
1004impl From<&Vector> for ExportedVector {
1005    fn from(v: &Vector) -> Self {
1006        Self {
1007            id: v.id.clone(),
1008            values: Some(v.values.clone()),
1009            metadata: v.metadata.clone(),
1010            ttl_seconds: v.ttl_seconds,
1011        }
1012    }
1013}
1014
1015/// Response from export operation
1016#[derive(Debug, Serialize)]
1017pub struct ExportResponse {
1018    /// Exported vectors for this page
1019    pub vectors: Vec<ExportedVector>,
1020    /// Cursor for next page (None if this is the last page)
1021    #[serde(skip_serializing_if = "Option::is_none")]
1022    pub next_cursor: Option<String>,
1023    /// Total vectors in namespace (for progress tracking)
1024    pub total_count: usize,
1025    /// Number of vectors returned in this page
1026    pub returned_count: usize,
1027}
1028
1029// ============================================================================
1030// Unified Query API with rank_by (Turbopuffer-inspired)
1031// ============================================================================
1032
1033/// Ranking function for unified query API
1034/// Supports vector search (ANN/kNN), full-text BM25, and attribute ordering
1035#[derive(Debug, Clone, Serialize, Deserialize)]
1036#[serde(untagged)]
1037pub enum RankBy {
1038    /// Vector search: ["vector_field", "ANN"|"kNN", [query_vector]]
1039    /// or simplified: ["ANN", [query_vector]] for default "vector" field
1040    VectorSearch {
1041        field: String,
1042        method: VectorSearchMethod,
1043        query_vector: Vec<f32>,
1044    },
1045    /// Full-text BM25 search: ["text_field", "BM25", "query string"]
1046    FullTextSearch {
1047        field: String,
1048        method: String, // Always "BM25"
1049        query: String,
1050    },
1051    /// Attribute ordering: ["field_name", "asc"|"desc"]
1052    AttributeOrder {
1053        field: String,
1054        direction: SortDirection,
1055    },
1056    /// Sum of multiple ranking functions: ["Sum", [...rankings]]
1057    Sum(Vec<RankBy>),
1058    /// Max of multiple ranking functions: ["Max", [...rankings]]
1059    Max(Vec<RankBy>),
1060    /// Product with weight: ["Product", weight, ranking]
1061    Product { weight: f32, ranking: Box<RankBy> },
1062}
1063
1064/// Vector search method
1065#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
1066pub enum VectorSearchMethod {
1067    /// Approximate Nearest Neighbor (fast, default)
1068    #[default]
1069    ANN,
1070    /// Exact k-Nearest Neighbor (exhaustive, requires filters)
1071    #[serde(rename = "kNN")]
1072    KNN,
1073}
1074
1075/// Sort direction for attribute ordering
1076#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
1077#[serde(rename_all = "lowercase")]
1078#[derive(Default)]
1079pub enum SortDirection {
1080    Asc,
1081    #[default]
1082    Desc,
1083}
1084
1085/// Unified query request with rank_by parameter (Turbopuffer-inspired)
1086#[derive(Debug, Deserialize)]
1087pub struct UnifiedQueryRequest {
1088    /// How to rank documents (required unless using aggregations)
1089    pub rank_by: RankByInput,
1090    /// Number of results to return
1091    #[serde(default = "default_top_k")]
1092    pub top_k: usize,
1093    /// Optional metadata filter
1094    #[serde(default)]
1095    pub filter: Option<FilterExpression>,
1096    /// Include metadata in results
1097    #[serde(default = "default_true")]
1098    pub include_metadata: bool,
1099    /// Include vectors in results
1100    #[serde(default)]
1101    pub include_vectors: bool,
1102    /// Distance metric for vector search (default: cosine)
1103    #[serde(default)]
1104    pub distance_metric: DistanceMetric,
1105}
1106
1107/// Input format for rank_by that handles JSON array syntax
1108/// Examples:
1109/// - ["vector", "ANN", [0.1, 0.2, 0.3]]
1110/// - ["text", "BM25", "search query"]
1111/// - ["timestamp", "desc"]
1112/// - ["Sum", [["title", "BM25", "query"], ["content", "BM25", "query"]]]
1113/// - ["Product", 2.0, ["title", "BM25", "query"]]
1114#[derive(Debug, Clone, Serialize, Deserialize)]
1115#[serde(from = "serde_json::Value")]
1116pub struct RankByInput(pub RankBy);
1117
1118impl From<serde_json::Value> for RankByInput {
1119    fn from(value: serde_json::Value) -> Self {
1120        RankByInput(parse_rank_by(&value).unwrap_or_else(|| {
1121            // Default fallback - shouldn't happen with valid input
1122            RankBy::AttributeOrder {
1123                field: "id".to_string(),
1124                direction: SortDirection::Asc,
1125            }
1126        }))
1127    }
1128}
1129
1130/// Parse rank_by JSON array into RankBy enum
1131fn parse_rank_by(value: &serde_json::Value) -> Option<RankBy> {
1132    let arr = value.as_array()?;
1133    if arr.is_empty() {
1134        return None;
1135    }
1136
1137    let first = arr.first()?.as_str()?;
1138
1139    match first {
1140        // Combination operators
1141        "Sum" => {
1142            let rankings = arr.get(1)?.as_array()?;
1143            let parsed: Option<Vec<RankBy>> = rankings.iter().map(parse_rank_by).collect();
1144            Some(RankBy::Sum(parsed?))
1145        }
1146        "Max" => {
1147            let rankings = arr.get(1)?.as_array()?;
1148            let parsed: Option<Vec<RankBy>> = rankings.iter().map(parse_rank_by).collect();
1149            Some(RankBy::Max(parsed?))
1150        }
1151        "Product" => {
1152            let weight = arr.get(1)?.as_f64()? as f32;
1153            let ranking = parse_rank_by(arr.get(2)?)?;
1154            Some(RankBy::Product {
1155                weight,
1156                ranking: Box::new(ranking),
1157            })
1158        }
1159        // Vector search shorthand: ["ANN", [vector]] or ["kNN", [vector]]
1160        "ANN" => {
1161            let query_vector = parse_vector_array(arr.get(1)?)?;
1162            Some(RankBy::VectorSearch {
1163                field: "vector".to_string(),
1164                method: VectorSearchMethod::ANN,
1165                query_vector,
1166            })
1167        }
1168        "kNN" => {
1169            let query_vector = parse_vector_array(arr.get(1)?)?;
1170            Some(RankBy::VectorSearch {
1171                field: "vector".to_string(),
1172                method: VectorSearchMethod::KNN,
1173                query_vector,
1174            })
1175        }
1176        // Field-based operations
1177        field => {
1178            let second = arr.get(1)?;
1179
1180            // Check if second element is a method string
1181            if let Some(method_str) = second.as_str() {
1182                match method_str {
1183                    "ANN" => {
1184                        let query_vector = parse_vector_array(arr.get(2)?)?;
1185                        Some(RankBy::VectorSearch {
1186                            field: field.to_string(),
1187                            method: VectorSearchMethod::ANN,
1188                            query_vector,
1189                        })
1190                    }
1191                    "kNN" => {
1192                        let query_vector = parse_vector_array(arr.get(2)?)?;
1193                        Some(RankBy::VectorSearch {
1194                            field: field.to_string(),
1195                            method: VectorSearchMethod::KNN,
1196                            query_vector,
1197                        })
1198                    }
1199                    "BM25" => {
1200                        let query = arr.get(2)?.as_str()?;
1201                        Some(RankBy::FullTextSearch {
1202                            field: field.to_string(),
1203                            method: "BM25".to_string(),
1204                            query: query.to_string(),
1205                        })
1206                    }
1207                    "asc" => Some(RankBy::AttributeOrder {
1208                        field: field.to_string(),
1209                        direction: SortDirection::Asc,
1210                    }),
1211                    "desc" => Some(RankBy::AttributeOrder {
1212                        field: field.to_string(),
1213                        direction: SortDirection::Desc,
1214                    }),
1215                    _ => None,
1216                }
1217            } else {
1218                None
1219            }
1220        }
1221    }
1222}
1223
1224/// Parse a JSON value into a vector of f32
1225fn parse_vector_array(value: &serde_json::Value) -> Option<Vec<f32>> {
1226    let arr = value.as_array()?;
1227    arr.iter().map(|v| v.as_f64().map(|n| n as f32)).collect()
1228}
1229
1230/// Unified query response with $dist scoring
1231#[derive(Debug, Serialize, Deserialize)]
1232pub struct UnifiedQueryResponse {
1233    /// Search results ordered by rank_by score
1234    pub results: Vec<UnifiedSearchResult>,
1235    /// Cursor for pagination (if more results available)
1236    #[serde(skip_serializing_if = "Option::is_none")]
1237    pub next_cursor: Option<String>,
1238}
1239
1240/// Single result from unified query
1241#[derive(Debug, Serialize, Deserialize)]
1242pub struct UnifiedSearchResult {
1243    /// Vector/document ID
1244    pub id: String,
1245    /// Ranking score (distance for vector search, BM25 score for text)
1246    /// Named $dist for Turbopuffer compatibility
1247    #[serde(rename = "$dist", skip_serializing_if = "Option::is_none")]
1248    pub dist: Option<f32>,
1249    /// Metadata if requested
1250    #[serde(skip_serializing_if = "Option::is_none")]
1251    pub metadata: Option<serde_json::Value>,
1252    /// Vector values if requested
1253    #[serde(skip_serializing_if = "Option::is_none")]
1254    pub vector: Option<Vec<f32>>,
1255}
1256
1257// ============================================================================
1258// Aggregation types (Turbopuffer-inspired)
1259// ============================================================================
1260
1261/// Aggregate function for computing values across documents
1262#[derive(Debug, Clone, Serialize, Deserialize)]
1263pub enum AggregateFunction {
1264    /// Count matching documents: ["Count"]
1265    Count,
1266    /// Sum numeric attribute values: ["Sum", "attribute_name"]
1267    Sum { field: String },
1268    /// Average numeric attribute values: ["Avg", "attribute_name"]
1269    Avg { field: String },
1270    /// Minimum numeric attribute value: ["Min", "attribute_name"]
1271    Min { field: String },
1272    /// Maximum numeric attribute value: ["Max", "attribute_name"]
1273    Max { field: String },
1274}
1275
1276/// Wrapper for parsing aggregate function from JSON array
1277#[derive(Debug, Clone, Serialize, Deserialize)]
1278#[serde(from = "serde_json::Value")]
1279pub struct AggregateFunctionInput(pub AggregateFunction);
1280
1281impl From<serde_json::Value> for AggregateFunctionInput {
1282    fn from(value: serde_json::Value) -> Self {
1283        parse_aggregate_function(&value)
1284            .map(AggregateFunctionInput)
1285            .unwrap_or_else(|| {
1286                // Default to count if parsing fails
1287                AggregateFunctionInput(AggregateFunction::Count)
1288            })
1289    }
1290}
1291
1292/// Parse aggregate function from JSON array
1293fn parse_aggregate_function(value: &serde_json::Value) -> Option<AggregateFunction> {
1294    let arr = value.as_array()?;
1295    if arr.is_empty() {
1296        return None;
1297    }
1298
1299    let func_name = arr.first()?.as_str()?;
1300
1301    match func_name {
1302        "Count" => Some(AggregateFunction::Count),
1303        "Sum" => {
1304            let field = arr.get(1)?.as_str()?;
1305            Some(AggregateFunction::Sum {
1306                field: field.to_string(),
1307            })
1308        }
1309        "Avg" => {
1310            let field = arr.get(1)?.as_str()?;
1311            Some(AggregateFunction::Avg {
1312                field: field.to_string(),
1313            })
1314        }
1315        "Min" => {
1316            let field = arr.get(1)?.as_str()?;
1317            Some(AggregateFunction::Min {
1318                field: field.to_string(),
1319            })
1320        }
1321        "Max" => {
1322            let field = arr.get(1)?.as_str()?;
1323            Some(AggregateFunction::Max {
1324                field: field.to_string(),
1325            })
1326        }
1327        _ => None,
1328    }
1329}
1330
1331/// Request for aggregation query (Turbopuffer-inspired)
1332#[derive(Debug, Deserialize)]
1333pub struct AggregationRequest {
1334    /// Named aggregations to compute
1335    /// Example: {"my_count": ["Count"], "total_score": ["Sum", "score"]}
1336    pub aggregate_by: std::collections::HashMap<String, AggregateFunctionInput>,
1337    /// Fields to group results by (optional)
1338    /// Example: ["category", "status"]
1339    #[serde(default)]
1340    pub group_by: Vec<String>,
1341    /// Filter to apply before aggregation
1342    #[serde(default)]
1343    pub filter: Option<FilterExpression>,
1344    /// Maximum number of groups to return (default: 100)
1345    #[serde(default = "default_agg_limit")]
1346    pub limit: usize,
1347}
1348
1349fn default_agg_limit() -> usize {
1350    100
1351}
1352
1353/// Response for aggregation query
1354#[derive(Debug, Serialize, Deserialize)]
1355pub struct AggregationResponse {
1356    /// Aggregation results (without grouping)
1357    #[serde(skip_serializing_if = "Option::is_none")]
1358    pub aggregations: Option<std::collections::HashMap<String, serde_json::Value>>,
1359    /// Grouped aggregation results (with group_by)
1360    #[serde(skip_serializing_if = "Option::is_none")]
1361    pub aggregation_groups: Option<Vec<AggregationGroup>>,
1362}
1363
1364/// Single group in aggregation results
1365#[derive(Debug, Serialize, Deserialize)]
1366pub struct AggregationGroup {
1367    /// Group key values (flattened into object)
1368    #[serde(flatten)]
1369    pub group_key: std::collections::HashMap<String, serde_json::Value>,
1370    /// Aggregation results for this group
1371    #[serde(flatten)]
1372    pub aggregations: std::collections::HashMap<String, serde_json::Value>,
1373}
1374
1375// =============================================================================
1376// TEXT-BASED API TYPES (Embedded Inference)
1377// =============================================================================
1378
1379/// A text document with metadata for text-based upsert
1380#[derive(Debug, Clone, Serialize, Deserialize)]
1381pub struct TextDocument {
1382    /// Unique identifier for this document
1383    pub id: VectorId,
1384    /// The text content to be embedded
1385    pub text: String,
1386    /// Optional metadata to store with the vector
1387    #[serde(skip_serializing_if = "Option::is_none")]
1388    pub metadata: Option<serde_json::Value>,
1389    /// TTL in seconds (optional)
1390    #[serde(skip_serializing_if = "Option::is_none")]
1391    pub ttl_seconds: Option<u64>,
1392}
1393
1394/// Request to upsert text documents (auto-embedded)
1395#[derive(Debug, Deserialize)]
1396pub struct TextUpsertRequest {
1397    /// Text documents to embed and store
1398    pub documents: Vec<TextDocument>,
1399    /// Embedding model to use (default: `minilm`).
1400    #[serde(default)]
1401    pub model: Option<EmbeddingModelType>,
1402}
1403
1404/// Response from text upsert operation
1405#[derive(Debug, Serialize, Deserialize)]
1406pub struct TextUpsertResponse {
1407    /// Number of documents successfully upserted
1408    pub upserted_count: usize,
1409    /// Number of tokens processed for embedding
1410    pub tokens_processed: usize,
1411    /// Embedding model used
1412    pub model: EmbeddingModelType,
1413    /// Time taken for embedding generation (ms)
1414    pub embedding_time_ms: u64,
1415}
1416
1417/// Request for text-based query (auto-embedded)
1418#[derive(Debug, Deserialize)]
1419pub struct TextQueryRequest {
1420    /// The query text to search for
1421    pub text: String,
1422    /// Number of results to return
1423    #[serde(default = "default_top_k")]
1424    pub top_k: usize,
1425    /// Optional filter to apply
1426    #[serde(default)]
1427    pub filter: Option<FilterExpression>,
1428    /// Whether to include vectors in response
1429    #[serde(default)]
1430    pub include_vectors: bool,
1431    /// Whether to include the original text in response (if stored in metadata)
1432    #[serde(default = "default_true")]
1433    pub include_text: bool,
1434    /// Embedding model to use (must match upsert model; default: `minilm`).
1435    #[serde(default)]
1436    pub model: Option<EmbeddingModelType>,
1437}
1438
1439/// Response from text-based query
1440#[derive(Debug, Serialize, Deserialize)]
1441pub struct TextQueryResponse {
1442    /// Search results with similarity scores
1443    pub results: Vec<TextSearchResult>,
1444    /// Embedding model used
1445    pub model: EmbeddingModelType,
1446    /// Time taken for embedding generation (ms)
1447    pub embedding_time_ms: u64,
1448    /// Time taken for search (ms)
1449    pub search_time_ms: u64,
1450}
1451
1452/// Single result from text search
1453#[derive(Debug, Serialize, Deserialize)]
1454pub struct TextSearchResult {
1455    /// Document ID
1456    pub id: VectorId,
1457    /// Similarity score (higher is better)
1458    pub score: f32,
1459    /// Original text (if include_text=true and stored in metadata)
1460    #[serde(skip_serializing_if = "Option::is_none")]
1461    pub text: Option<String>,
1462    /// Associated metadata
1463    #[serde(skip_serializing_if = "Option::is_none")]
1464    pub metadata: Option<serde_json::Value>,
1465    /// Vector values (if include_vectors=true)
1466    #[serde(skip_serializing_if = "Option::is_none")]
1467    pub vector: Option<Vec<f32>>,
1468}
1469
1470/// Batch text query request
1471#[derive(Debug, Deserialize)]
1472pub struct BatchTextQueryRequest {
1473    /// Multiple query texts
1474    pub queries: Vec<String>,
1475    /// Number of results per query
1476    #[serde(default = "default_top_k")]
1477    pub top_k: usize,
1478    /// Optional filter to apply to all queries
1479    #[serde(default)]
1480    pub filter: Option<FilterExpression>,
1481    /// Whether to include vectors in response
1482    #[serde(default)]
1483    pub include_vectors: bool,
1484    /// Embedding model to use (default: `minilm`).
1485    #[serde(default)]
1486    pub model: Option<EmbeddingModelType>,
1487}
1488
1489/// Response from batch text query
1490#[derive(Debug, Serialize, Deserialize)]
1491pub struct BatchTextQueryResponse {
1492    /// Results for each query
1493    pub results: Vec<Vec<TextSearchResult>>,
1494    /// Embedding model used
1495    pub model: EmbeddingModelType,
1496    /// Total time for embedding generation (ms)
1497    pub embedding_time_ms: u64,
1498    /// Total time for search (ms)
1499    pub search_time_ms: u64,
1500}
1501
1502/// Available embedding models.
1503///
1504/// Replaces the previous `model: String` field — callers now supply a
1505/// typed enum value, eliminating runtime string-mismatch bugs.
1506///
1507/// JSON serialisation uses lowercase identifiers:
1508/// `"minilm"`, `"bge-small"`, `"e5-small"`.
1509#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
1510pub enum EmbeddingModelType {
1511    /// all-MiniLM-L6-v2 — fast and memory-efficient, 384 dimensions
1512    #[default]
1513    #[serde(rename = "minilm")]
1514    MiniLM,
1515    /// BAAI/bge-small-en-v1.5 — balanced quality and speed, 384 dimensions
1516    #[serde(rename = "bge-small")]
1517    BgeSmall,
1518    /// intfloat/e5-small-v2 — quality-focused, 384 dimensions
1519    #[serde(rename = "e5-small")]
1520    E5Small,
1521}
1522
1523impl EmbeddingModelType {
1524    /// Embedding vector dimension for this model.
1525    pub fn dimension(&self) -> usize {
1526        match self {
1527            EmbeddingModelType::MiniLM => 384,
1528            EmbeddingModelType::BgeSmall => 384,
1529            EmbeddingModelType::E5Small => 384,
1530        }
1531    }
1532}
1533
1534impl std::fmt::Display for EmbeddingModelType {
1535    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1536        match self {
1537            EmbeddingModelType::MiniLM => write!(f, "minilm"),
1538            EmbeddingModelType::BgeSmall => write!(f, "bge-small"),
1539            EmbeddingModelType::E5Small => write!(f, "e5-small"),
1540        }
1541    }
1542}
1543
1544// ============================================================================
1545// Dakera Memory Types — AI Agent Memory Platform
1546// ============================================================================
1547
1548/// Type of memory stored by an agent
1549#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
1550#[serde(rename_all = "snake_case")]
1551#[derive(Default)]
1552pub enum MemoryType {
1553    /// Personal experiences and events
1554    #[default]
1555    Episodic,
1556    /// Facts and general knowledge
1557    Semantic,
1558    /// How-to knowledge and skills
1559    Procedural,
1560    /// Short-term, temporary context
1561    Working,
1562}
1563
1564impl std::fmt::Display for MemoryType {
1565    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1566        match self {
1567            MemoryType::Episodic => write!(f, "episodic"),
1568            MemoryType::Semantic => write!(f, "semantic"),
1569            MemoryType::Procedural => write!(f, "procedural"),
1570            MemoryType::Working => write!(f, "working"),
1571        }
1572    }
1573}
1574
1575/// A memory stored by an AI agent
1576#[derive(Debug, Clone, Serialize, Deserialize)]
1577pub struct Memory {
1578    pub id: String,
1579    #[serde(default)]
1580    pub memory_type: MemoryType,
1581    pub content: String,
1582    pub agent_id: String,
1583    #[serde(skip_serializing_if = "Option::is_none")]
1584    pub session_id: Option<String>,
1585    #[serde(default = "default_importance")]
1586    pub importance: f32,
1587    #[serde(default)]
1588    pub tags: Vec<String>,
1589    #[serde(skip_serializing_if = "Option::is_none")]
1590    pub metadata: Option<serde_json::Value>,
1591    pub created_at: u64,
1592    pub last_accessed_at: u64,
1593    #[serde(default)]
1594    pub access_count: u32,
1595    #[serde(skip_serializing_if = "Option::is_none")]
1596    pub ttl_seconds: Option<u64>,
1597    #[serde(skip_serializing_if = "Option::is_none")]
1598    pub expires_at: Option<u64>,
1599}
1600
1601fn default_importance() -> f32 {
1602    0.5
1603}
1604
1605impl Memory {
1606    /// Create a new memory with current timestamps
1607    pub fn new(id: String, content: String, agent_id: String, memory_type: MemoryType) -> Self {
1608        let now = std::time::SystemTime::now()
1609            .duration_since(std::time::UNIX_EPOCH)
1610            .unwrap_or_default()
1611            .as_secs();
1612        Self {
1613            id,
1614            memory_type,
1615            content,
1616            agent_id,
1617            session_id: None,
1618            importance: 0.5,
1619            tags: Vec::new(),
1620            metadata: None,
1621            created_at: now,
1622            last_accessed_at: now,
1623            access_count: 0,
1624            ttl_seconds: None,
1625            expires_at: None,
1626        }
1627    }
1628
1629    /// Check if this memory has expired
1630    pub fn is_expired(&self) -> bool {
1631        if let Some(expires_at) = self.expires_at {
1632            let now = std::time::SystemTime::now()
1633                .duration_since(std::time::UNIX_EPOCH)
1634                .unwrap_or_default()
1635                .as_secs();
1636            now >= expires_at
1637        } else {
1638            false
1639        }
1640    }
1641
1642    /// Pack memory fields into metadata for Vector storage
1643    pub fn to_vector_metadata(&self) -> serde_json::Value {
1644        let mut meta = serde_json::Map::new();
1645        meta.insert("_dakera_type".to_string(), serde_json::json!("memory"));
1646        meta.insert(
1647            "memory_type".to_string(),
1648            serde_json::json!(self.memory_type),
1649        );
1650        meta.insert("content".to_string(), serde_json::json!(self.content));
1651        meta.insert("agent_id".to_string(), serde_json::json!(self.agent_id));
1652        if let Some(ref sid) = self.session_id {
1653            meta.insert("session_id".to_string(), serde_json::json!(sid));
1654        }
1655        meta.insert("importance".to_string(), serde_json::json!(self.importance));
1656        meta.insert("tags".to_string(), serde_json::json!(self.tags));
1657        meta.insert("created_at".to_string(), serde_json::json!(self.created_at));
1658        meta.insert(
1659            "last_accessed_at".to_string(),
1660            serde_json::json!(self.last_accessed_at),
1661        );
1662        meta.insert(
1663            "access_count".to_string(),
1664            serde_json::json!(self.access_count),
1665        );
1666        if let Some(ref ttl) = self.ttl_seconds {
1667            meta.insert("ttl_seconds".to_string(), serde_json::json!(ttl));
1668        }
1669        if let Some(ref expires) = self.expires_at {
1670            meta.insert("expires_at".to_string(), serde_json::json!(expires));
1671        }
1672        if let Some(ref user_meta) = self.metadata {
1673            meta.insert("user_metadata".to_string(), user_meta.clone());
1674        }
1675        serde_json::Value::Object(meta)
1676    }
1677
1678    /// Convert a Memory to a Vector (for storage layer)
1679    pub fn to_vector(&self, embedding: Vec<f32>) -> Vector {
1680        let mut v = Vector {
1681            id: self.id.clone(),
1682            values: embedding,
1683            metadata: Some(self.to_vector_metadata()),
1684            ttl_seconds: self.ttl_seconds,
1685            expires_at: self.expires_at,
1686        };
1687        v.apply_ttl();
1688        v
1689    }
1690
1691    /// Reconstruct a Memory from a Vector's metadata
1692    pub fn from_vector(vector: &Vector) -> Option<Self> {
1693        let meta = vector.metadata.as_ref()?.as_object()?;
1694        let entry_type = meta.get("_dakera_type")?.as_str()?;
1695        if entry_type != "memory" {
1696            return None;
1697        }
1698
1699        Some(Memory {
1700            id: vector.id.clone(),
1701            memory_type: serde_json::from_value(meta.get("memory_type")?.clone())
1702                .unwrap_or_default(),
1703            content: meta.get("content")?.as_str()?.to_string(),
1704            agent_id: meta.get("agent_id")?.as_str()?.to_string(),
1705            session_id: meta
1706                .get("session_id")
1707                .and_then(|v| v.as_str())
1708                .map(String::from),
1709            importance: meta
1710                .get("importance")
1711                .and_then(|v| v.as_f64())
1712                .unwrap_or(0.5) as f32,
1713            tags: meta
1714                .get("tags")
1715                .and_then(|v| serde_json::from_value(v.clone()).ok())
1716                .unwrap_or_default(),
1717            metadata: meta.get("user_metadata").cloned(),
1718            created_at: meta.get("created_at").and_then(|v| v.as_u64()).unwrap_or(0),
1719            last_accessed_at: meta
1720                .get("last_accessed_at")
1721                .and_then(|v| v.as_u64())
1722                .unwrap_or(0),
1723            access_count: meta
1724                .get("access_count")
1725                .and_then(|v| v.as_u64())
1726                .unwrap_or(0) as u32,
1727            ttl_seconds: vector.ttl_seconds,
1728            expires_at: vector.expires_at,
1729        })
1730    }
1731}
1732
1733/// An agent session
1734#[derive(Debug, Clone, Serialize, Deserialize)]
1735pub struct Session {
1736    pub id: String,
1737    pub agent_id: String,
1738    pub started_at: u64,
1739    #[serde(skip_serializing_if = "Option::is_none")]
1740    pub ended_at: Option<u64>,
1741    #[serde(skip_serializing_if = "Option::is_none")]
1742    pub summary: Option<String>,
1743    #[serde(skip_serializing_if = "Option::is_none")]
1744    pub metadata: Option<serde_json::Value>,
1745    /// Cached count of memories in this session (updated on store/forget)
1746    #[serde(default)]
1747    pub memory_count: usize,
1748}
1749
1750impl Session {
1751    pub fn new(id: String, agent_id: String) -> Self {
1752        let now = std::time::SystemTime::now()
1753            .duration_since(std::time::UNIX_EPOCH)
1754            .unwrap_or_default()
1755            .as_secs();
1756        Self {
1757            id,
1758            agent_id,
1759            started_at: now,
1760            ended_at: None,
1761            summary: None,
1762            metadata: None,
1763            memory_count: 0,
1764        }
1765    }
1766
1767    /// Pack session into metadata for Vector storage
1768    pub fn to_vector_metadata(&self) -> serde_json::Value {
1769        let mut meta = serde_json::Map::new();
1770        meta.insert("_dakera_type".to_string(), serde_json::json!("session"));
1771        meta.insert("agent_id".to_string(), serde_json::json!(self.agent_id));
1772        meta.insert("started_at".to_string(), serde_json::json!(self.started_at));
1773        if let Some(ref ended) = self.ended_at {
1774            meta.insert("ended_at".to_string(), serde_json::json!(ended));
1775        }
1776        if let Some(ref summary) = self.summary {
1777            meta.insert("summary".to_string(), serde_json::json!(summary));
1778        }
1779        if let Some(ref user_meta) = self.metadata {
1780            meta.insert("user_metadata".to_string(), user_meta.clone());
1781        }
1782        meta.insert(
1783            "memory_count".to_string(),
1784            serde_json::json!(self.memory_count),
1785        );
1786        serde_json::Value::Object(meta)
1787    }
1788
1789    /// Convert to a Vector for storage (use summary or agent_id as embedding source)
1790    pub fn to_vector(&self, embedding: Vec<f32>) -> Vector {
1791        Vector {
1792            id: self.id.clone(),
1793            values: embedding,
1794            metadata: Some(self.to_vector_metadata()),
1795            ttl_seconds: None,
1796            expires_at: None,
1797        }
1798    }
1799
1800    /// Reconstruct a Session from a Vector's metadata
1801    pub fn from_vector(vector: &Vector) -> Option<Self> {
1802        let meta = vector.metadata.as_ref()?.as_object()?;
1803        let entry_type = meta.get("_dakera_type")?.as_str()?;
1804        if entry_type != "session" {
1805            return None;
1806        }
1807
1808        Some(Session {
1809            id: vector.id.clone(),
1810            agent_id: meta.get("agent_id")?.as_str()?.to_string(),
1811            started_at: meta.get("started_at").and_then(|v| v.as_u64()).unwrap_or(0),
1812            ended_at: meta.get("ended_at").and_then(|v| v.as_u64()),
1813            summary: meta
1814                .get("summary")
1815                .and_then(|v| v.as_str())
1816                .map(String::from),
1817            metadata: meta.get("user_metadata").cloned(),
1818            memory_count: meta
1819                .get("memory_count")
1820                .and_then(|v| v.as_u64())
1821                .unwrap_or(0) as usize,
1822        })
1823    }
1824}
1825
1826/// Strategy for importance decay
1827#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
1828#[serde(rename_all = "snake_case")]
1829#[derive(Default)]
1830pub enum DecayStrategy {
1831    #[default]
1832    Exponential,
1833    Linear,
1834    StepFunction,
1835    /// Power-law decay: I(t) = I₀ / (1 + k·t)^α — natural for episodic memories
1836    PowerLaw,
1837    /// Logarithmic decay: I(t) = I₀ · (1 − log₂(1 + t/h)) — slow for semantic knowledge
1838    Logarithmic,
1839    /// Flat (no decay) — for procedural/skill memories
1840    Flat,
1841}
1842
1843/// Configuration for importance decay
1844#[derive(Debug, Clone, Serialize, Deserialize)]
1845pub struct DecayConfig {
1846    #[serde(default)]
1847    pub strategy: DecayStrategy,
1848    #[serde(default = "default_half_life_hours")]
1849    pub half_life_hours: f64,
1850    #[serde(default = "default_min_importance")]
1851    pub min_importance: f32,
1852}
1853
1854fn default_half_life_hours() -> f64 {
1855    168.0 // 1 week
1856}
1857
1858fn default_min_importance() -> f32 {
1859    0.01
1860}
1861
1862impl Default for DecayConfig {
1863    fn default() -> Self {
1864        Self {
1865            strategy: DecayStrategy::default(),
1866            half_life_hours: default_half_life_hours(),
1867            min_importance: default_min_importance(),
1868        }
1869    }
1870}
1871
1872// ============================================================================
1873// Dakera Memory Request/Response Types
1874// ============================================================================
1875
1876/// Request to store a memory
1877#[derive(Debug, Deserialize)]
1878pub struct StoreMemoryRequest {
1879    pub content: String,
1880    pub agent_id: String,
1881    #[serde(default)]
1882    pub memory_type: MemoryType,
1883    #[serde(skip_serializing_if = "Option::is_none")]
1884    pub session_id: Option<String>,
1885    #[serde(default = "default_importance")]
1886    pub importance: f32,
1887    #[serde(default)]
1888    pub tags: Vec<String>,
1889    #[serde(skip_serializing_if = "Option::is_none")]
1890    pub metadata: Option<serde_json::Value>,
1891    #[serde(skip_serializing_if = "Option::is_none")]
1892    pub ttl_seconds: Option<u64>,
1893    /// Optional explicit expiry Unix timestamp (seconds).
1894    /// If provided, takes precedence over ttl_seconds.
1895    /// On expiry the memory is hard-deleted by the decay engine, bypassing
1896    /// importance scoring.
1897    #[serde(skip_serializing_if = "Option::is_none")]
1898    pub expires_at: Option<u64>,
1899    /// Optional custom ID (auto-generated if not provided)
1900    #[serde(skip_serializing_if = "Option::is_none")]
1901    pub id: Option<String>,
1902}
1903
1904/// Response from storing a memory
1905#[derive(Debug, Serialize)]
1906pub struct StoreMemoryResponse {
1907    pub memory: Memory,
1908    pub embedding_time_ms: u64,
1909}
1910
1911/// Request to recall memories by semantic query
1912#[derive(Debug, Deserialize)]
1913pub struct RecallRequest {
1914    pub query: String,
1915    pub agent_id: String,
1916    #[serde(default = "default_top_k")]
1917    pub top_k: usize,
1918    #[serde(default)]
1919    pub memory_type: Option<MemoryType>,
1920    #[serde(default)]
1921    pub session_id: Option<String>,
1922    #[serde(default)]
1923    pub tags: Option<Vec<String>>,
1924    #[serde(default)]
1925    pub min_importance: Option<f32>,
1926    /// Include importance-weighted re-ranking (default: true)
1927    #[serde(default = "default_true")]
1928    pub importance_weighted: bool,
1929    /// COG-2: traverse KG depth-1 from recalled memories and include associatively linked memories
1930    #[serde(default)]
1931    pub include_associated: bool,
1932    /// COG-2: max number of associated memories to return (default: 10, max: 10)
1933    #[serde(default)]
1934    pub associated_memories_cap: Option<usize>,
1935    /// CE-7: only include memories created at or after this ISO-8601 timestamp (e.g. "2024-01-01T00:00:00Z")
1936    #[serde(default)]
1937    pub since: Option<String>,
1938    /// CE-7: only include memories created at or before this ISO-8601 timestamp (e.g. "2024-12-31T23:59:59Z")
1939    #[serde(default)]
1940    pub until: Option<String>,
1941    /// KG-3: KG traversal depth for associative recall (1–3, default 1).
1942    /// Requires `include_associated: true`. Depth 1 = direct neighbours only (COG-2 behaviour).
1943    #[serde(default)]
1944    pub associated_memories_depth: Option<u8>,
1945    /// KG-3: minimum edge weight to traverse (0.0–1.0, default 0.0 = all edges).
1946    /// Requires `include_associated: true`.
1947    #[serde(default)]
1948    pub associated_memories_min_weight: Option<f32>,
1949}
1950
1951/// Single recall result
1952#[derive(Debug, Serialize, Deserialize)]
1953pub struct RecallResult {
1954    pub memory: Memory,
1955    pub score: f32,
1956    /// Score after importance-weighted re-ranking
1957    #[serde(skip_serializing_if = "Option::is_none")]
1958    pub weighted_score: Option<f32>,
1959    /// Always-on multi-signal smart score (vector + importance + recency + frequency)
1960    #[serde(skip_serializing_if = "Option::is_none")]
1961    pub smart_score: Option<f32>,
1962    /// KG-3: traversal depth at which this memory was found (only set on associated_memories entries).
1963    /// 1 = direct neighbour of a primary result, 2 = two hops, 3 = three hops.
1964    #[serde(skip_serializing_if = "Option::is_none")]
1965    pub depth: Option<u8>,
1966}
1967
1968/// Response from recall
1969#[derive(Debug, Serialize)]
1970pub struct RecallResponse {
1971    pub memories: Vec<RecallResult>,
1972    pub query_embedding_time_ms: u64,
1973    pub search_time_ms: u64,
1974    /// COG-2: memories linked to recalled memories via KG depth-1 traversal.
1975    /// Only populated when `include_associated: true` in the request.
1976    #[serde(skip_serializing_if = "Option::is_none")]
1977    pub associated_memories: Option<Vec<RecallResult>>,
1978}
1979
1980/// Request to forget (delete) memories
1981#[derive(Debug, Deserialize)]
1982pub struct ForgetRequest {
1983    pub agent_id: String,
1984    #[serde(default)]
1985    pub memory_ids: Option<Vec<String>>,
1986    #[serde(default)]
1987    pub memory_type: Option<MemoryType>,
1988    #[serde(default)]
1989    pub session_id: Option<String>,
1990    #[serde(default)]
1991    pub tags: Option<Vec<String>>,
1992    /// Delete memories below this importance threshold
1993    #[serde(default)]
1994    pub below_importance: Option<f32>,
1995}
1996
1997/// Response from forget
1998#[derive(Debug, Serialize)]
1999pub struct ForgetResponse {
2000    pub deleted_count: usize,
2001}
2002
2003/// Request to update a memory
2004#[derive(Debug, Deserialize)]
2005pub struct UpdateMemoryRequest {
2006    #[serde(default)]
2007    pub content: Option<String>,
2008    #[serde(default)]
2009    pub importance: Option<f32>,
2010    #[serde(default)]
2011    pub tags: Option<Vec<String>>,
2012    #[serde(default)]
2013    pub metadata: Option<serde_json::Value>,
2014    #[serde(default)]
2015    pub memory_type: Option<MemoryType>,
2016}
2017
2018/// Request to update importance of a memory
2019#[derive(Debug, Deserialize)]
2020pub struct UpdateImportanceRequest {
2021    pub memory_id: String,
2022    pub importance: f32,
2023    pub agent_id: String,
2024}
2025
2026/// Request to consolidate related memories
2027#[derive(Debug, Deserialize)]
2028pub struct ConsolidateRequest {
2029    pub agent_id: String,
2030    /// Memory IDs to consolidate (if empty, auto-detect similar memories)
2031    #[serde(default)]
2032    pub memory_ids: Option<Vec<String>>,
2033    /// Similarity threshold for auto-detection (default: 0.85)
2034    #[serde(default = "default_consolidation_threshold")]
2035    pub threshold: f32,
2036    /// Type for the consolidated memory
2037    #[serde(default)]
2038    pub target_type: Option<MemoryType>,
2039}
2040
2041fn default_consolidation_threshold() -> f32 {
2042    0.85
2043}
2044
2045/// Response from consolidation
2046#[derive(Debug, Serialize)]
2047pub struct ConsolidateResponse {
2048    pub consolidated_memory: Memory,
2049    pub source_memory_ids: Vec<String>,
2050    pub memories_removed: usize,
2051}
2052
2053/// Feedback signal for active learning
2054#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
2055#[serde(rename_all = "lowercase")]
2056pub enum FeedbackSignal {
2057    /// Boost importance (×1.15, capped at 1.0). INT-1 canonical name.
2058    Upvote,
2059    /// Penalise importance (×0.85, floor 0.0). INT-1 canonical name.
2060    Downvote,
2061    /// Mark as irrelevant — sets `decay_flag=true`, no immediate importance change.
2062    Flag,
2063    /// Backward-compatible alias for `upvote`.
2064    Positive,
2065    /// Backward-compatible alias for `downvote`.
2066    Negative,
2067}
2068
2069/// One recorded feedback event stored in memory metadata (feedback_history).
2070#[derive(Debug, Clone, Serialize, Deserialize)]
2071pub struct FeedbackHistoryEntry {
2072    pub signal: FeedbackSignal,
2073    pub timestamp: u64,
2074    pub old_importance: f32,
2075    pub new_importance: f32,
2076}
2077
2078/// Request to provide feedback on a recalled memory (legacy — body contains memory_id)
2079#[derive(Debug, Deserialize)]
2080pub struct FeedbackRequest {
2081    pub agent_id: String,
2082    pub memory_id: String,
2083    pub signal: FeedbackSignal,
2084}
2085
2086/// Request for `POST /v1/memories/{id}/feedback` (INT-1 — memory_id in path)
2087#[derive(Debug, Deserialize)]
2088pub struct MemoryFeedbackRequest {
2089    pub agent_id: String,
2090    pub signal: FeedbackSignal,
2091}
2092
2093/// Response from feedback
2094#[derive(Debug, Serialize)]
2095pub struct FeedbackResponse {
2096    pub memory_id: String,
2097    pub new_importance: f32,
2098    pub signal: FeedbackSignal,
2099}
2100
2101/// Response from `GET /v1/memories/{id}/feedback`
2102#[derive(Debug, Serialize)]
2103pub struct FeedbackHistoryResponse {
2104    pub memory_id: String,
2105    pub entries: Vec<FeedbackHistoryEntry>,
2106}
2107
2108/// Response from `GET /v1/agents/{id}/feedback/summary`
2109#[derive(Debug, Serialize)]
2110pub struct AgentFeedbackSummary {
2111    pub agent_id: String,
2112    pub upvotes: u64,
2113    pub downvotes: u64,
2114    pub flags: u64,
2115    pub total_feedback: u64,
2116    /// Weighted-average importance across all non-expired memories (0.0–1.0).
2117    pub health_score: f32,
2118}
2119
2120/// Request for `PATCH /v1/memories/{id}/importance` (INT-1 — memory_id in path)
2121#[derive(Debug, Deserialize)]
2122pub struct MemoryImportancePatchRequest {
2123    pub agent_id: String,
2124    pub importance: f32,
2125}
2126
2127/// Query params for `GET /v1/feedback/health`
2128#[derive(Debug, Deserialize)]
2129pub struct FeedbackHealthQuery {
2130    pub agent_id: String,
2131}
2132
2133/// Response from `GET /v1/feedback/health`
2134#[derive(Debug, Serialize)]
2135pub struct FeedbackHealthResponse {
2136    pub agent_id: String,
2137    /// Mean importance of all non-expired memories (0.0–1.0). Higher = healthier.
2138    pub health_score: f32,
2139    pub memory_count: usize,
2140    pub avg_importance: f32,
2141}
2142
2143/// Request for advanced memory search
2144#[derive(Debug, Deserialize)]
2145pub struct SearchMemoriesRequest {
2146    pub agent_id: String,
2147    #[serde(default)]
2148    pub query: Option<String>,
2149    #[serde(default)]
2150    pub memory_type: Option<MemoryType>,
2151    #[serde(default)]
2152    pub session_id: Option<String>,
2153    #[serde(default)]
2154    pub tags: Option<Vec<String>>,
2155    #[serde(default)]
2156    pub min_importance: Option<f32>,
2157    #[serde(default)]
2158    pub max_importance: Option<f32>,
2159    #[serde(default)]
2160    pub created_after: Option<u64>,
2161    #[serde(default)]
2162    pub created_before: Option<u64>,
2163    #[serde(default = "default_top_k")]
2164    pub top_k: usize,
2165    #[serde(default)]
2166    pub sort_by: Option<MemorySortField>,
2167}
2168
2169/// Fields to sort memories by
2170#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
2171#[serde(rename_all = "snake_case")]
2172pub enum MemorySortField {
2173    CreatedAt,
2174    LastAccessedAt,
2175    Importance,
2176    AccessCount,
2177}
2178
2179/// Response from memory search
2180#[derive(Debug, Serialize)]
2181pub struct SearchMemoriesResponse {
2182    pub memories: Vec<RecallResult>,
2183    pub total_count: usize,
2184}
2185
2186// ============================================================================
2187// Dakera Session Request/Response Types
2188// ============================================================================
2189
2190/// Request to start a session
2191#[derive(Debug, Deserialize)]
2192pub struct SessionStartRequest {
2193    pub agent_id: String,
2194    #[serde(skip_serializing_if = "Option::is_none")]
2195    pub metadata: Option<serde_json::Value>,
2196    /// Optional custom session ID
2197    #[serde(skip_serializing_if = "Option::is_none")]
2198    pub id: Option<String>,
2199}
2200
2201/// Response from starting a session
2202#[derive(Debug, Serialize)]
2203pub struct SessionStartResponse {
2204    pub session: Session,
2205}
2206
2207/// Request to end a session
2208#[derive(Debug, Deserialize)]
2209pub struct SessionEndRequest {
2210    #[serde(default)]
2211    pub summary: Option<String>,
2212    /// Auto-generate summary from session memories
2213    #[serde(default)]
2214    pub auto_summarize: bool,
2215}
2216
2217/// Response from ending a session
2218#[derive(Debug, Serialize)]
2219pub struct SessionEndResponse {
2220    pub session: Session,
2221    pub memory_count: usize,
2222}
2223
2224/// Response listing sessions
2225#[derive(Debug, Serialize)]
2226pub struct ListSessionsResponse {
2227    pub sessions: Vec<Session>,
2228    pub total: usize,
2229}
2230
2231/// Response for session memories
2232#[derive(Debug, Serialize)]
2233pub struct SessionMemoriesResponse {
2234    pub session: Session,
2235    pub memories: Vec<Memory>,
2236    /// Total number of memories in this session (before pagination)
2237    #[serde(skip_serializing_if = "Option::is_none")]
2238    pub total: Option<usize>,
2239}
2240
2241// ============================================================================
2242// Dakera Agent & Knowledge Types
2243// ============================================================================
2244
2245/// Lightweight agent summary for batch listing (uses count() not get_all)
2246#[derive(Debug, Serialize, Deserialize, Clone)]
2247pub struct AgentSummary {
2248    pub agent_id: String,
2249    pub memory_count: usize,
2250    pub session_count: usize,
2251    pub active_sessions: usize,
2252}
2253
2254/// Agent memory statistics
2255#[derive(Debug, Serialize)]
2256pub struct AgentStats {
2257    pub agent_id: String,
2258    pub total_memories: usize,
2259    pub memories_by_type: std::collections::HashMap<String, usize>,
2260    pub total_sessions: usize,
2261    pub active_sessions: usize,
2262    pub avg_importance: f32,
2263    pub oldest_memory_at: Option<u64>,
2264    pub newest_memory_at: Option<u64>,
2265}
2266
2267/// Request for knowledge graph traversal
2268#[derive(Debug, Deserialize)]
2269pub struct KnowledgeGraphRequest {
2270    pub agent_id: String,
2271    pub memory_id: String,
2272    #[serde(default = "default_graph_depth")]
2273    pub depth: usize,
2274    #[serde(default = "default_graph_min_similarity")]
2275    pub min_similarity: f32,
2276}
2277
2278fn default_graph_depth() -> usize {
2279    2
2280}
2281
2282fn default_graph_min_similarity() -> f32 {
2283    0.7
2284}
2285
2286/// Knowledge graph node
2287#[derive(Debug, Serialize)]
2288pub struct KnowledgeGraphNode {
2289    pub memory: Memory,
2290    pub similarity: f32,
2291    pub related: Vec<KnowledgeGraphEdge>,
2292}
2293
2294/// Knowledge graph edge
2295#[derive(Debug, Serialize)]
2296pub struct KnowledgeGraphEdge {
2297    pub memory_id: String,
2298    pub similarity: f32,
2299    pub shared_tags: Vec<String>,
2300}
2301
2302/// Response from knowledge graph query
2303#[derive(Debug, Serialize)]
2304pub struct KnowledgeGraphResponse {
2305    pub root: KnowledgeGraphNode,
2306    pub total_nodes: usize,
2307}
2308
2309// ============================================================================
2310// Full Knowledge Graph Types (Global Network Topology)
2311// ============================================================================
2312
2313fn default_full_graph_max_nodes() -> usize {
2314    200
2315}
2316
2317fn default_full_graph_min_similarity() -> f32 {
2318    0.50
2319}
2320
2321fn default_full_graph_cluster_threshold() -> f32 {
2322    0.60
2323}
2324
2325fn default_full_graph_max_edges_per_node() -> usize {
2326    8
2327}
2328
2329/// Request for full knowledge graph (all memories, pairwise similarity)
2330#[derive(Debug, Deserialize)]
2331pub struct FullKnowledgeGraphRequest {
2332    pub agent_id: String,
2333    #[serde(default = "default_full_graph_max_nodes")]
2334    pub max_nodes: usize,
2335    #[serde(default = "default_full_graph_min_similarity")]
2336    pub min_similarity: f32,
2337    #[serde(default = "default_full_graph_cluster_threshold")]
2338    pub cluster_threshold: f32,
2339    #[serde(default = "default_full_graph_max_edges_per_node")]
2340    pub max_edges_per_node: usize,
2341}
2342
2343/// A node in the full knowledge graph
2344#[derive(Debug, Serialize)]
2345pub struct FullGraphNode {
2346    pub id: String,
2347    pub content: String,
2348    pub memory_type: String,
2349    pub importance: f32,
2350    pub tags: Vec<String>,
2351    pub created_at: Option<String>,
2352    pub cluster_id: usize,
2353    pub centrality: f32,
2354}
2355
2356/// An edge in the full knowledge graph
2357#[derive(Debug, Serialize)]
2358pub struct FullGraphEdge {
2359    pub source: String,
2360    pub target: String,
2361    pub similarity: f32,
2362    pub shared_tags: Vec<String>,
2363}
2364
2365/// A cluster of related memories
2366#[derive(Debug, Serialize)]
2367pub struct GraphCluster {
2368    pub id: usize,
2369    pub node_count: usize,
2370    pub top_tags: Vec<String>,
2371    pub avg_importance: f32,
2372}
2373
2374/// Statistics about the full knowledge graph
2375#[derive(Debug, Serialize)]
2376pub struct GraphStats {
2377    pub total_memories: usize,
2378    pub included_memories: usize,
2379    pub total_edges: usize,
2380    pub cluster_count: usize,
2381    pub density: f32,
2382    pub hub_memory_id: Option<String>,
2383}
2384
2385/// Response from full knowledge graph query
2386#[derive(Debug, Serialize)]
2387pub struct FullKnowledgeGraphResponse {
2388    pub nodes: Vec<FullGraphNode>,
2389    pub edges: Vec<FullGraphEdge>,
2390    pub clusters: Vec<GraphCluster>,
2391    pub stats: GraphStats,
2392}
2393
2394/// Request to summarize memories
2395#[derive(Debug, Deserialize)]
2396pub struct SummarizeRequest {
2397    pub agent_id: String,
2398    pub memory_ids: Vec<String>,
2399    #[serde(default)]
2400    pub target_type: Option<MemoryType>,
2401}
2402
2403/// Response from summarization
2404#[derive(Debug, Serialize)]
2405pub struct SummarizeResponse {
2406    pub summary_memory: Memory,
2407    pub source_count: usize,
2408}
2409
2410/// Request to deduplicate memories
2411#[derive(Debug, Deserialize)]
2412pub struct DeduplicateRequest {
2413    pub agent_id: String,
2414    #[serde(default = "default_dedup_threshold")]
2415    pub threshold: f32,
2416    #[serde(default)]
2417    pub memory_type: Option<MemoryType>,
2418    /// Dry run — report duplicates without merging
2419    #[serde(default)]
2420    pub dry_run: bool,
2421}
2422
2423fn default_dedup_threshold() -> f32 {
2424    0.92
2425}
2426
2427/// A group of duplicate memories
2428#[derive(Debug, Serialize)]
2429pub struct DuplicateGroup {
2430    pub canonical_id: String,
2431    pub duplicate_ids: Vec<String>,
2432    pub avg_similarity: f32,
2433}
2434
2435/// Response from deduplication
2436#[derive(Debug, Serialize)]
2437pub struct DeduplicateResponse {
2438    pub groups: Vec<DuplicateGroup>,
2439    pub duplicates_found: usize,
2440    pub duplicates_merged: usize,
2441}
2442
2443// ============================================================================
2444// Cross-Agent Memory Network Types (DASH-A)
2445// ============================================================================
2446
2447fn default_cross_agent_min_similarity() -> f32 {
2448    0.3
2449}
2450
2451fn default_cross_agent_max_nodes_per_agent() -> usize {
2452    50
2453}
2454
2455fn default_cross_agent_max_cross_edges() -> usize {
2456    200
2457}
2458
2459/// Request for cross-agent memory network graph
2460#[derive(Debug, Deserialize)]
2461pub struct CrossAgentNetworkRequest {
2462    /// Specific agent IDs to include (None = all agents)
2463    #[serde(default)]
2464    pub agent_ids: Option<Vec<String>>,
2465    /// Minimum cosine similarity for a cross-agent edge (default 0.3)
2466    #[serde(default = "default_cross_agent_min_similarity")]
2467    pub min_similarity: f32,
2468    /// Maximum memories per agent to include (top N by importance, default 50)
2469    #[serde(default = "default_cross_agent_max_nodes_per_agent")]
2470    pub max_nodes_per_agent: usize,
2471    /// Minimum importance score for a memory to be included (default 0.0)
2472    #[serde(default)]
2473    pub min_importance: f32,
2474    /// Maximum cross-agent edges to return (default 200)
2475    #[serde(default = "default_cross_agent_max_cross_edges")]
2476    pub max_cross_edges: usize,
2477}
2478
2479/// Summary info for an agent in the cross-agent network
2480#[derive(Debug, Serialize)]
2481pub struct AgentNetworkInfo {
2482    pub agent_id: String,
2483    pub memory_count: usize,
2484    pub avg_importance: f32,
2485}
2486
2487/// A memory node in the cross-agent network (includes agent_id)
2488#[derive(Debug, Serialize)]
2489pub struct AgentNetworkNode {
2490    pub id: String,
2491    pub agent_id: String,
2492    pub content: String,
2493    pub importance: f32,
2494    pub tags: Vec<String>,
2495    pub memory_type: String,
2496    pub created_at: u64,
2497}
2498
2499/// An edge between memories from two different agents
2500#[derive(Debug, Serialize)]
2501pub struct AgentNetworkEdge {
2502    pub source: String,
2503    pub target: String,
2504    pub source_agent: String,
2505    pub target_agent: String,
2506    pub similarity: f32,
2507}
2508
2509/// Statistics for the cross-agent network
2510#[derive(Debug, Serialize)]
2511pub struct AgentNetworkStats {
2512    pub total_agents: usize,
2513    pub total_nodes: usize,
2514    pub total_cross_edges: usize,
2515    pub density: f32,
2516}
2517
2518/// Response from cross-agent network query
2519#[derive(Debug, Serialize)]
2520pub struct CrossAgentNetworkResponse {
2521    pub node_count: usize,
2522    pub agents: Vec<AgentNetworkInfo>,
2523    pub nodes: Vec<AgentNetworkNode>,
2524    pub edges: Vec<AgentNetworkEdge>,
2525    pub stats: AgentNetworkStats,
2526}
2527
2528// ---------------------------------------------------------------------------
2529// CE-2: Batch recall / forget types
2530// ---------------------------------------------------------------------------
2531
2532/// Filter predicates for batch memory operations.
2533///
2534/// At least one field must be set for forget operations (safety guard).
2535#[derive(Debug, Deserialize, Default)]
2536pub struct BatchMemoryFilter {
2537    /// Restrict to memories that carry **all** listed tags.
2538    #[serde(default)]
2539    pub tags: Option<Vec<String>>,
2540    /// Minimum importance (inclusive).
2541    #[serde(default)]
2542    pub min_importance: Option<f32>,
2543    /// Maximum importance (inclusive).
2544    #[serde(default)]
2545    pub max_importance: Option<f32>,
2546    /// Only memories created at or after this Unix timestamp (seconds).
2547    #[serde(default)]
2548    pub created_after: Option<u64>,
2549    /// Only memories created before or at this Unix timestamp (seconds).
2550    #[serde(default)]
2551    pub created_before: Option<u64>,
2552    /// Restrict to a specific memory type.
2553    #[serde(default)]
2554    pub memory_type: Option<MemoryType>,
2555    /// Restrict to memories from a specific session.
2556    #[serde(default)]
2557    pub session_id: Option<String>,
2558}
2559
2560impl BatchMemoryFilter {
2561    /// Returns `true` if the filter has at least one constraint set.
2562    pub fn has_any(&self) -> bool {
2563        self.tags.is_some()
2564            || self.min_importance.is_some()
2565            || self.max_importance.is_some()
2566            || self.created_after.is_some()
2567            || self.created_before.is_some()
2568            || self.memory_type.is_some()
2569            || self.session_id.is_some()
2570    }
2571
2572    /// Returns `true` if the given memory matches all active filter predicates.
2573    pub fn matches(&self, memory: &Memory) -> bool {
2574        if let Some(ref tags) = self.tags {
2575            if !tags.is_empty() && !tags.iter().all(|t| memory.tags.contains(t)) {
2576                return false;
2577            }
2578        }
2579        if let Some(min) = self.min_importance {
2580            if memory.importance < min {
2581                return false;
2582            }
2583        }
2584        if let Some(max) = self.max_importance {
2585            if memory.importance > max {
2586                return false;
2587            }
2588        }
2589        if let Some(after) = self.created_after {
2590            if memory.created_at < after {
2591                return false;
2592            }
2593        }
2594        if let Some(before) = self.created_before {
2595            if memory.created_at > before {
2596                return false;
2597            }
2598        }
2599        if let Some(ref mt) = self.memory_type {
2600            if memory.memory_type != *mt {
2601                return false;
2602            }
2603        }
2604        if let Some(ref sid) = self.session_id {
2605            if memory.session_id.as_ref() != Some(sid) {
2606                return false;
2607            }
2608        }
2609        true
2610    }
2611}
2612
2613/// Request for `POST /v1/memories/recall/batch`
2614#[derive(Debug, Deserialize)]
2615pub struct BatchRecallRequest {
2616    /// Agent whose memory namespace to search.
2617    pub agent_id: String,
2618    /// Filter predicates to apply.
2619    #[serde(default)]
2620    pub filter: BatchMemoryFilter,
2621    /// Maximum number of results to return (default: 100).
2622    #[serde(default = "default_batch_limit")]
2623    pub limit: usize,
2624}
2625
2626fn default_batch_limit() -> usize {
2627    100
2628}
2629
2630/// Response from `POST /v1/memories/recall/batch`
2631#[derive(Debug, Serialize)]
2632pub struct BatchRecallResponse {
2633    pub memories: Vec<Memory>,
2634    pub total: usize,
2635    pub filtered: usize,
2636}
2637
2638/// Request for `DELETE /v1/memories/forget/batch`
2639#[derive(Debug, Deserialize)]
2640pub struct BatchForgetRequest {
2641    /// Agent whose memory namespace to purge from.
2642    pub agent_id: String,
2643    /// Filter predicates — **at least one must be set** (safety guard).
2644    pub filter: BatchMemoryFilter,
2645}
2646
2647/// Response from `DELETE /v1/memories/forget/batch`
2648#[derive(Debug, Serialize)]
2649pub struct BatchForgetResponse {
2650    pub deleted_count: usize,
2651}
2652
2653// ─────────────────────────────────────────────────────────────────────────────
2654// CE-4 — Entity extraction types
2655// ─────────────────────────────────────────────────────────────────────────────
2656
2657/// Request to update entity extraction config for a namespace.
2658/// `PATCH /v1/namespaces/{namespace}/config`
2659#[derive(Debug, Deserialize)]
2660pub struct NamespaceEntityConfigRequest {
2661    /// Enable or disable entity extraction for this namespace.
2662    pub extract_entities: bool,
2663    /// Entity types to extract via GLiNER (e.g. ["person","org","location"]).
2664    /// If empty and extract_entities=true, only the rule-based pre-pass runs.
2665    #[serde(default)]
2666    pub entity_types: Vec<String>,
2667}
2668
2669/// Response from `PATCH /v1/namespaces/{namespace}/config`
2670#[derive(Debug, Serialize, Deserialize)]
2671pub struct NamespaceEntityConfigResponse {
2672    pub namespace: String,
2673    pub extract_entities: bool,
2674    pub entity_types: Vec<String>,
2675}
2676
2677/// Request to extract entities from content without storing.
2678/// `POST /v1/memories/extract`
2679#[derive(Debug, Deserialize)]
2680pub struct ExtractEntitiesRequest {
2681    /// Text content to extract entities from.
2682    pub content: String,
2683    /// Entity types for GLiNER inference (optional).
2684    /// If omitted, only the rule-based pre-pass runs.
2685    #[serde(default)]
2686    pub entity_types: Vec<String>,
2687}
2688
2689/// A single extracted entity (shared with inference crate — mirrored here for API types).
2690#[derive(Debug, Clone, Serialize, Deserialize)]
2691pub struct EntityResult {
2692    pub entity_type: String,
2693    pub value: String,
2694    pub score: f32,
2695    pub start: usize,
2696    pub end: usize,
2697    /// Canonical tag form: `entity:<type>:<value>`
2698    pub tag: String,
2699}
2700
2701/// Response from `POST /v1/memories/extract` and `GET /v1/memories/{id}/entities`
2702#[derive(Debug, Serialize)]
2703pub struct ExtractEntitiesResponse {
2704    pub entities: Vec<EntityResult>,
2705    pub count: usize,
2706}
2707
2708// ============================================================================
2709// CE-5: Memory Knowledge Graph — request / response types
2710// ============================================================================
2711
2712/// GET /v1/memories/:id/graph
2713#[derive(Debug, Deserialize)]
2714pub struct GraphTraverseQuery {
2715    /// BFS depth limit (default 3, max 5).
2716    #[serde(default = "default_ce5_graph_depth")]
2717    pub depth: u32,
2718}
2719
2720fn default_ce5_graph_depth() -> u32 {
2721    3
2722}
2723
2724/// GET /v1/memories/:id/path
2725#[derive(Debug, Deserialize)]
2726pub struct GraphPathQuery {
2727    /// Target memory ID.
2728    pub to: String,
2729}
2730
2731/// POST /v1/memories/:id/links — create an explicit edge
2732#[derive(Debug, Deserialize)]
2733pub struct MemoryLinkRequest {
2734    /// The other memory ID to link to.
2735    pub target_id: String,
2736    /// Optional human-readable label (stored as `linked_by` edge).
2737    #[serde(skip_serializing_if = "Option::is_none")]
2738    pub label: Option<String>,
2739    /// Agent ID (for authorization).
2740    pub agent_id: String,
2741}
2742
2743/// Response from graph traversal.
2744#[derive(Debug, Serialize)]
2745pub struct GraphTraverseResponse {
2746    pub root_id: String,
2747    pub depth: u32,
2748    pub node_count: usize,
2749    pub nodes: Vec<GraphNodeResponse>,
2750}
2751
2752/// A single node in a graph traversal response.
2753#[derive(Debug, Serialize)]
2754pub struct GraphNodeResponse {
2755    pub memory_id: String,
2756    pub depth: u32,
2757    pub edges: Vec<GraphEdgeResponse>,
2758}
2759
2760/// A single edge in a graph response.
2761#[derive(Debug, Serialize)]
2762pub struct GraphEdgeResponse {
2763    pub from_id: String,
2764    pub to_id: String,
2765    pub edge_type: String,
2766    pub weight: f32,
2767    pub created_at: u64,
2768}
2769
2770/// Response from shortest-path query.
2771#[derive(Debug, Serialize)]
2772pub struct GraphPathResponse {
2773    pub from_id: String,
2774    pub to_id: String,
2775    /// Ordered list of memory IDs along the shortest path (inclusive).
2776    pub path: Vec<String>,
2777    pub hop_count: usize,
2778}
2779
2780/// Response from explicit link creation.
2781#[derive(Debug, Serialize)]
2782pub struct MemoryLinkResponse {
2783    pub from_id: String,
2784    pub to_id: String,
2785    pub edge_type: String,
2786}
2787
2788/// Response from agent graph export.
2789#[derive(Debug, Serialize)]
2790pub struct GraphExportResponse {
2791    pub agent_id: String,
2792    pub namespace: String,
2793    pub node_count: usize,
2794    pub edge_count: usize,
2795    pub edges: Vec<GraphEdgeResponse>,
2796}
2797
2798// ============================================================================
2799// KG-2: Graph Query & Export — request / response types
2800// ============================================================================
2801
2802/// GET /v1/knowledge/query — JSON DSL for graph filtering/traversal
2803#[derive(Debug, Deserialize)]
2804pub struct KgQueryParams {
2805    /// Agent ID whose graph to query (required).
2806    pub agent_id: String,
2807    /// Optional root memory ID — if set, performs BFS from this node first.
2808    #[serde(default)]
2809    pub root_id: Option<String>,
2810    /// Filter edges by type (comma-separated, e.g. "related_to,shares_entity").
2811    #[serde(default)]
2812    pub edge_type: Option<String>,
2813    /// Minimum edge weight (0.0–1.0).
2814    #[serde(default)]
2815    pub min_weight: Option<f32>,
2816    /// BFS depth when root_id is set (1–5, default 3).
2817    #[serde(default = "default_kg_depth")]
2818    pub max_depth: u32,
2819    /// Maximum number of edges to return (default 100, max 1000).
2820    #[serde(default = "default_kg_limit")]
2821    pub limit: usize,
2822}
2823
2824fn default_kg_depth() -> u32 {
2825    3
2826}
2827
2828fn default_kg_limit() -> usize {
2829    100
2830}
2831
2832/// Response from GET /v1/knowledge/query
2833#[derive(Debug, Serialize)]
2834pub struct KgQueryResponse {
2835    pub agent_id: String,
2836    pub node_count: usize,
2837    pub edge_count: usize,
2838    pub edges: Vec<GraphEdgeResponse>,
2839}
2840
2841/// GET /v1/knowledge/path — shortest path between two memory IDs
2842#[derive(Debug, Deserialize)]
2843pub struct KgPathParams {
2844    /// Agent ID for authorization.
2845    pub agent_id: String,
2846    /// Source memory ID.
2847    pub from: String,
2848    /// Target memory ID.
2849    pub to: String,
2850}
2851
2852/// Response from GET /v1/knowledge/path
2853#[derive(Debug, Serialize)]
2854pub struct KgPathResponse {
2855    pub agent_id: String,
2856    pub from_id: String,
2857    pub to_id: String,
2858    pub hop_count: usize,
2859    pub path: Vec<String>,
2860}
2861
2862/// GET /v1/knowledge/export — export graph as JSON or GraphML
2863#[derive(Debug, Deserialize)]
2864pub struct KgExportParams {
2865    /// Agent ID whose graph to export.
2866    pub agent_id: String,
2867    /// Export format: "json" (default) or "graphml".
2868    #[serde(default = "default_kg_format")]
2869    pub format: String,
2870}
2871
2872fn default_kg_format() -> String {
2873    "json".to_string()
2874}
2875
2876/// Response from GET /v1/knowledge/export (format=json)
2877#[derive(Debug, Serialize)]
2878pub struct KgExportJsonResponse {
2879    pub agent_id: String,
2880    pub format: String,
2881    pub node_count: usize,
2882    pub edge_count: usize,
2883    pub edges: Vec<GraphEdgeResponse>,
2884}
2885
2886// ============================================================================
2887// COG-1: Cognitive Memory Lifecycle — per-namespace memory policy
2888// ============================================================================
2889
2890fn default_working_ttl() -> Option<u64> {
2891    Some(14_400) // 4 hours
2892}
2893fn default_episodic_ttl() -> Option<u64> {
2894    Some(2_592_000) // 30 days
2895}
2896fn default_semantic_ttl() -> Option<u64> {
2897    Some(31_536_000) // 365 days
2898}
2899fn default_procedural_ttl() -> Option<u64> {
2900    Some(63_072_000) // 730 days
2901}
2902fn default_working_decay() -> DecayStrategy {
2903    DecayStrategy::Exponential
2904}
2905fn default_episodic_decay() -> DecayStrategy {
2906    DecayStrategy::PowerLaw
2907}
2908fn default_semantic_decay() -> DecayStrategy {
2909    DecayStrategy::Logarithmic
2910}
2911fn default_procedural_decay() -> DecayStrategy {
2912    DecayStrategy::Flat
2913}
2914fn default_sr_factor() -> f64 {
2915    1.0
2916}
2917fn default_sr_base_interval() -> u64 {
2918    86_400 // 1 day
2919}
2920fn default_consolidation_enabled() -> bool {
2921    false
2922}
2923fn default_policy_consolidation_threshold() -> f32 {
2924    0.92
2925}
2926fn default_consolidation_interval_hours() -> u32 {
2927    24
2928}
2929
2930/// Per-namespace memory lifecycle policy (COG-1).
2931///
2932/// Controls type-specific TTLs, decay curves, and spaced repetition behaviour.
2933/// All fields have sensible defaults; only override what you need.
2934#[derive(Debug, Clone, Serialize, Deserialize)]
2935pub struct MemoryPolicy {
2936    // ── Differential TTLs ────────────────────────────────────────────────────
2937    /// Default TTL for `working` memories in seconds (default: 4 h = 14 400 s).
2938    #[serde(
2939        default = "default_working_ttl",
2940        skip_serializing_if = "Option::is_none"
2941    )]
2942    pub working_ttl_seconds: Option<u64>,
2943    /// Default TTL for `episodic` memories in seconds (default: 30 d = 2 592 000 s).
2944    #[serde(
2945        default = "default_episodic_ttl",
2946        skip_serializing_if = "Option::is_none"
2947    )]
2948    pub episodic_ttl_seconds: Option<u64>,
2949    /// Default TTL for `semantic` memories in seconds (default: 365 d = 31 536 000 s).
2950    #[serde(
2951        default = "default_semantic_ttl",
2952        skip_serializing_if = "Option::is_none"
2953    )]
2954    pub semantic_ttl_seconds: Option<u64>,
2955    /// Default TTL for `procedural` memories in seconds (default: 730 d = 63 072 000 s).
2956    #[serde(
2957        default = "default_procedural_ttl",
2958        skip_serializing_if = "Option::is_none"
2959    )]
2960    pub procedural_ttl_seconds: Option<u64>,
2961
2962    // ── Decay curves ─────────────────────────────────────────────────────────
2963    /// Decay strategy for `working` memories (default: exponential).
2964    #[serde(default = "default_working_decay")]
2965    pub working_decay: DecayStrategy,
2966    /// Decay strategy for `episodic` memories (default: power_law).
2967    #[serde(default = "default_episodic_decay")]
2968    pub episodic_decay: DecayStrategy,
2969    /// Decay strategy for `semantic` memories (default: logarithmic).
2970    #[serde(default = "default_semantic_decay")]
2971    pub semantic_decay: DecayStrategy,
2972    /// Decay strategy for `procedural` memories (default: flat — no decay).
2973    #[serde(default = "default_procedural_decay")]
2974    pub procedural_decay: DecayStrategy,
2975
2976    // ── Spaced repetition ────────────────────────────────────────────────────
2977    /// Multiplier applied to the TTL extension on each recall.
2978    /// Extension = `access_count × sr_factor × sr_base_interval_seconds`.
2979    /// Set to 0.0 to disable spaced repetition. (default: 1.0)
2980    #[serde(default = "default_sr_factor")]
2981    pub spaced_repetition_factor: f64,
2982    /// Base interval in seconds for spaced repetition TTL extension (default: 86 400 = 1 day).
2983    #[serde(default = "default_sr_base_interval")]
2984    pub spaced_repetition_base_interval_seconds: u64,
2985
2986    // ── COG-3: Proactive consolidation ───────────────────────────────────────
2987    /// Enable background deduplication of semantically similar memories (default: false).
2988    #[serde(default = "default_consolidation_enabled")]
2989    pub consolidation_enabled: bool,
2990    /// Cosine-similarity threshold for merging memories (default: 0.92, range 0.85–0.99).
2991    #[serde(default = "default_policy_consolidation_threshold")]
2992    pub consolidation_threshold: f32,
2993    /// How often the background consolidation job runs, in hours (default: 24).
2994    #[serde(default = "default_consolidation_interval_hours")]
2995    pub consolidation_interval_hours: u32,
2996    /// Total number of memories merged since namespace creation (read-only).
2997    #[serde(default)]
2998    pub consolidated_count: u64,
2999
3000    // ── SEC-5: Per-namespace rate limiting ───────────────────────────────────
3001    /// Master rate-limit switch (default: false — opt-in to avoid breaking existing clients).
3002    /// Set to `true` to enforce `rate_limit_stores_per_minute` / `rate_limit_recalls_per_minute`.
3003    #[serde(default)]
3004    pub rate_limit_enabled: bool,
3005    /// Maximum `POST /v1/memory/store` operations per minute for this namespace.
3006    /// `None` = unlimited. Only enforced when `rate_limit_enabled = true`.
3007    #[serde(default, skip_serializing_if = "Option::is_none")]
3008    pub rate_limit_stores_per_minute: Option<u32>,
3009    /// Maximum `POST /v1/memory/recall` operations per minute for this namespace.
3010    /// `None` = unlimited. Only enforced when `rate_limit_enabled = true`.
3011    #[serde(default, skip_serializing_if = "Option::is_none")]
3012    pub rate_limit_recalls_per_minute: Option<u32>,
3013}
3014
3015impl Default for MemoryPolicy {
3016    fn default() -> Self {
3017        Self {
3018            working_ttl_seconds: default_working_ttl(),
3019            episodic_ttl_seconds: default_episodic_ttl(),
3020            semantic_ttl_seconds: default_semantic_ttl(),
3021            procedural_ttl_seconds: default_procedural_ttl(),
3022            working_decay: default_working_decay(),
3023            episodic_decay: default_episodic_decay(),
3024            semantic_decay: default_semantic_decay(),
3025            procedural_decay: default_procedural_decay(),
3026            spaced_repetition_factor: default_sr_factor(),
3027            spaced_repetition_base_interval_seconds: default_sr_base_interval(),
3028            consolidation_enabled: default_consolidation_enabled(),
3029            consolidation_threshold: default_policy_consolidation_threshold(),
3030            consolidation_interval_hours: default_consolidation_interval_hours(),
3031            consolidated_count: 0,
3032            rate_limit_enabled: false,
3033            rate_limit_stores_per_minute: None,
3034            rate_limit_recalls_per_minute: None,
3035        }
3036    }
3037}
3038
3039impl MemoryPolicy {
3040    /// Return the configured TTL for the given memory type, in seconds.
3041    pub fn ttl_for_type(&self, memory_type: &MemoryType) -> Option<u64> {
3042        match memory_type {
3043            MemoryType::Working => self.working_ttl_seconds,
3044            MemoryType::Episodic => self.episodic_ttl_seconds,
3045            MemoryType::Semantic => self.semantic_ttl_seconds,
3046            MemoryType::Procedural => self.procedural_ttl_seconds,
3047        }
3048    }
3049
3050    /// Return the configured decay strategy for the given memory type.
3051    pub fn decay_for_type(&self, memory_type: &MemoryType) -> DecayStrategy {
3052        match memory_type {
3053            MemoryType::Working => self.working_decay,
3054            MemoryType::Episodic => self.episodic_decay,
3055            MemoryType::Semantic => self.semantic_decay,
3056            MemoryType::Procedural => self.procedural_decay,
3057        }
3058    }
3059
3060    /// Compute the spaced repetition TTL extension in seconds.
3061    ///
3062    /// `extension = access_count × sr_factor × sr_base_interval_seconds`
3063    pub fn spaced_repetition_extension(&self, access_count: u32) -> u64 {
3064        if self.spaced_repetition_factor <= 0.0 {
3065            return 0;
3066        }
3067        let ext = access_count as f64
3068            * self.spaced_repetition_factor
3069            * self.spaced_repetition_base_interval_seconds as f64;
3070        ext.round() as u64
3071    }
3072}