aien-sovereign-core
Autonomous sovereign intelligence engine and runtime core for NVIDIA DGX Spark
Brought by drakestapleton
aien-sovereign-core / crates / spark-mail-rs / src / cortex_sync.rs
| 1 | use crate::models::EmailMessage; |
| 2 | use reqwest::Client; |
| 3 | use serde_json::json; |
| 4 | use std::fs; |
| 5 | use std::path::Path; |
| 6 | use std::time::Duration; |
| 7 | |
| 8 | #[derive(Clone, Debug)] |
| 9 | pub struct CortexSync { |
| 10 | cortex_url: String, |
| 11 | client: Client, |
| 12 | } |
| 13 | |
| 14 | impl CortexSync { |
| 15 | pub fn new(cortex_url: Option<&str>) -> Self { |
| 16 | let url = cortex_url.unwrap_or("http://127.0.0.1:18080").to_string(); |
| 17 | let client = Client::builder() |
| 18 | .timeout(Duration::from_secs(3)) |
| 19 | .build() |
| 20 | .unwrap_or_else(|_| Client::new()); |
| 21 | Self { cortex_url: url, client } |
| 22 | } |
| 23 | |
| 24 | fn get_cortex_token(&self) -> String { |
| 25 | let token_path = "/home/drakestapleton/.config/cortex/token"; |
| 26 | if Path::new(token_path).exists() { |
| 27 | fs::read_to_string(token_path).unwrap_or_default().trim().to_string() |
| 28 | } else { |
| 29 | String::new() |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | pub async fn check_health(&self) -> bool { |
| 34 | self.client |
| 35 | .get(format!("{}/health", self.cortex_url)) |
| 36 | .timeout(Duration::from_millis(500)) |
| 37 | .send() |
| 38 | .await |
| 39 | .map(|r| r.status().is_success()) |
| 40 | .unwrap_or(false) |
| 41 | } |
| 42 | |
| 43 | pub async fn commit_mail(&self, msg: &EmailMessage) -> Result<(), String> { |
| 44 | let token = self.get_cortex_token(); |
| 45 | let formatted_content = format!( |
| 46 | "From: {}\nTo: {}\nSubject: {}\nDate: {}\nFolder: {}\n\n{}", |
| 47 | msg.from, |
| 48 | msg.to.join(", "), |
| 49 | msg.subject, |
| 50 | msg.received_at, |
| 51 | msg.folder, |
| 52 | msg.body |
| 53 | ); |
| 54 | |
| 55 | let payload = json!({ |
| 56 | "kind": "entity", |
| 57 | "value": { |
| 58 | "canonicalName": format!("mail:{}", msg.id), |
| 59 | "entityType": "correspondence", |
| 60 | "content": formatted_content, |
| 61 | "metadata": { |
| 62 | "mail_id": msg.id, |
| 63 | "subject": msg.subject, |
| 64 | "from": msg.from, |
| 65 | "to": msg.to, |
| 66 | "folder": msg.folder, |
| 67 | "received_at": msg.received_at |
| 68 | }, |
| 69 | "space": "atlas-memory" |
| 70 | } |
| 71 | }); |
| 72 | |
| 73 | let mut req = self.client |
| 74 | .post(format!("{}/api/cortex/write", self.cortex_url)) |
| 75 | .header("Content-Type", "application/json"); |
| 76 | |
| 77 | if !token.is_empty() { |
| 78 | req = req.header("Authorization", format!("Bearer {}", token)); |
| 79 | } |
| 80 | |
| 81 | let resp = req.json(&payload) |
| 82 | .send() |
| 83 | .await |
| 84 | .map_err(|e| format!("Cortex connection failed: {}", e))?; |
| 85 | |
| 86 | if resp.status().is_success() { |
| 87 | Ok(()) |
| 88 | } else { |
| 89 | Err(format!("Cortex write returned HTTP {}", resp.status())) |
| 90 | } |
| 91 | } |
| 92 | } |