cortex-rs
Pure compiled native Rust memory engine with vector search and SQLite WAL
Brought by drakestapleton
| 1 | #![allow(dead_code)] |
| 2 | use serde::{Deserialize, Serialize}; |
| 3 | use serde_json::Value; |
| 4 | |
| 5 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 6 | #[serde(rename_all = "camelCase")] |
| 7 | pub struct CortexEntity { |
| 8 | pub id: String, |
| 9 | pub space_id: String, |
| 10 | pub space_slug: String, |
| 11 | pub entity_type: String, |
| 12 | pub canonical_name: String, |
| 13 | pub content: String, |
| 14 | pub aliases: Vec<String>, |
| 15 | pub metadata: Value, |
| 16 | pub confidence: f64, |
| 17 | pub revision: i64, |
| 18 | pub retracted: bool, |
| 19 | pub valid_from: Option<String>, |
| 20 | pub valid_to: Option<String>, |
| 21 | pub created_at: String, |
| 22 | } |
| 23 | |
| 24 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 25 | #[serde(rename_all = "camelCase")] |
| 26 | pub struct CortexClaim { |
| 27 | pub id: String, |
| 28 | pub space_id: String, |
| 29 | pub space_slug: String, |
| 30 | pub subject_entity_id: String, |
| 31 | pub predicate: String, |
| 32 | pub object_entity_id: Option<String>, |
| 33 | pub literal_value: Option<Value>, |
| 34 | pub confidence: f64, |
| 35 | pub metadata: Value, |
| 36 | pub retracted: bool, |
| 37 | pub created_at: String, |
| 38 | } |
| 39 | |
| 40 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 41 | #[serde(rename_all = "camelCase")] |
| 42 | pub struct ReceiptDetails { |
| 43 | pub id: String, |
| 44 | pub operation: String, |
| 45 | pub target_type: String, |
| 46 | pub target_id: String, |
| 47 | pub committed_at: String, |
| 48 | } |
| 49 | |
| 50 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 51 | pub struct CortexReceipt { |
| 52 | pub recorded: bool, |
| 53 | pub receipt: ReceiptDetails, |
| 54 | } |
| 55 | |
| 56 | #[derive(Debug, Clone, Deserialize)] |
| 57 | #[serde(rename_all = "camelCase")] |
| 58 | pub struct EntityWriteInput { |
| 59 | pub id: Option<String>, |
| 60 | #[serde(default = "default_space")] |
| 61 | pub space: String, |
| 62 | #[serde(default = "default_entity_type")] |
| 63 | pub entity_type: String, |
| 64 | pub canonical_name: String, |
| 65 | #[serde(default)] |
| 66 | pub content: String, |
| 67 | #[serde(default)] |
| 68 | pub aliases: Vec<String>, |
| 69 | #[serde(default = "default_json_object")] |
| 70 | pub metadata: Value, |
| 71 | #[serde(default = "default_confidence")] |
| 72 | pub confidence: f64, |
| 73 | pub valid_from: Option<String>, |
| 74 | pub valid_to: Option<String>, |
| 75 | pub external_id: Option<String>, |
| 76 | } |
| 77 | |
| 78 | fn default_space() -> String { "atlas-memory".to_string() } |
| 79 | fn default_entity_type() -> String { "discovery".to_string() } |
| 80 | fn default_json_object() -> Value { serde_json::json!({}) } |
| 81 | fn default_confidence() -> f64 { 1.0 } |
| 82 | |
| 83 | #[derive(Debug, Clone, Deserialize)] |
| 84 | #[serde(rename_all = "camelCase")] |
| 85 | pub struct ClaimWriteInput { |
| 86 | pub id: Option<String>, |
| 87 | #[serde(default = "default_space")] |
| 88 | pub space: String, |
| 89 | pub subject_entity_id: String, |
| 90 | pub predicate: String, |
| 91 | pub object_entity_id: Option<String>, |
| 92 | pub literal_value: Option<Value>, |
| 93 | #[serde(default = "default_confidence")] |
| 94 | pub confidence: f64, |
| 95 | #[serde(default = "default_json_object")] |
| 96 | pub metadata: Value, |
| 97 | } |
| 98 | |
| 99 | #[derive(Debug, Clone, Deserialize)] |
| 100 | #[serde(rename_all = "camelCase")] |
| 101 | pub struct RetractInput { |
| 102 | pub target_type: String, |
| 103 | pub target_id: String, |
| 104 | pub reason: Option<String>, |
| 105 | } |
| 106 | |
| 107 | #[derive(Debug, Clone, Deserialize)] |
| 108 | #[serde(tag = "kind")] |
| 109 | pub enum WritePayload { |
| 110 | #[serde(rename = "entity")] |
| 111 | Entity { value: EntityWriteInput }, |
| 112 | #[serde(rename = "claim")] |
| 113 | Claim { value: ClaimWriteInput }, |
| 114 | #[serde(rename = "retract")] |
| 115 | Retract { value: RetractInput }, |
| 116 | } |
| 117 | |
| 118 | #[derive(Debug, Clone, Deserialize)] |
| 119 | pub struct SearchParams { |
| 120 | #[serde(default)] |
| 121 | pub q: Option<String>, |
| 122 | #[serde(default)] |
| 123 | pub query: Option<String>, |
| 124 | pub space: Option<String>, |
| 125 | #[serde(default = "default_search_limit")] |
| 126 | pub limit: Option<usize>, |
| 127 | #[serde(default)] |
| 128 | pub include_retracted: Option<bool>, |
| 129 | } |
| 130 | |
| 131 | fn default_search_limit() -> Option<usize> { Some(12) } |
| 132 | |
| 133 | #[derive(Debug, Clone, Serialize)] |
| 134 | #[serde(rename_all = "camelCase")] |
| 135 | pub struct SearchResult { |
| 136 | #[serde(flatten)] |
| 137 | pub entity: CortexEntity, |
| 138 | pub lexical_score: f64, |
| 139 | pub graph_score: f64, |
| 140 | pub semantic_score: f64, |
| 141 | pub score: f64, |
| 142 | } |
| 143 | |
| 144 | #[derive(Debug, Clone, Serialize)] |
| 145 | pub struct SearchResponse { |
| 146 | pub results: Vec<SearchResult>, |
| 147 | pub degraded: Vec<String>, |
| 148 | #[serde(rename = "elapsedMs")] |
| 149 | pub elapsed_ms: f64, |
| 150 | } |
| 151 | |
| 152 | #[derive(Debug, Clone, Deserialize)] |
| 153 | #[serde(rename_all = "camelCase")] |
| 154 | pub struct RecallPayload { |
| 155 | pub query: String, |
| 156 | pub space: Option<String>, |
| 157 | #[serde(default = "default_recall_limit")] |
| 158 | pub limit: usize, |
| 159 | #[serde(default = "default_token_budget")] |
| 160 | pub token_budget: usize, |
| 161 | } |
| 162 | |
| 163 | fn default_recall_limit() -> usize { 5 } |
| 164 | fn default_token_budget() -> usize { 1200 } |
| 165 | |
| 166 | #[derive(Debug, Clone, Serialize)] |
| 167 | #[serde(rename_all = "camelCase")] |
| 168 | pub struct RecallComponentScores { |
| 169 | pub lexical: f64, |
| 170 | pub semantic: f64, |
| 171 | pub confidence_recency: f64, |
| 172 | pub graph: f64, |
| 173 | } |
| 174 | |
| 175 | #[derive(Debug, Clone, Serialize)] |
| 176 | #[serde(rename_all = "camelCase")] |
| 177 | pub struct RecallItem { |
| 178 | pub entity: CortexEntity, |
| 179 | pub final_score: f64, |
| 180 | pub component_scores: RecallComponentScores, |
| 181 | } |
| 182 | |
| 183 | #[derive(Debug, Clone, Serialize)] |
| 184 | pub struct RecallResponse { |
| 185 | pub results: Vec<RecallItem>, |
| 186 | pub degraded: Vec<String>, |
| 187 | #[serde(rename = "elapsedMs")] |
| 188 | pub elapsed_ms: f64, |
| 189 | } |
| 190 | |
| 191 | #[derive(Debug, Clone, Deserialize)] |
| 192 | #[serde(rename_all = "camelCase")] |
| 193 | pub struct TraversePayload { |
| 194 | pub subject_id: String, |
| 195 | pub space: Option<String>, |
| 196 | } |