spark-rsi-mv
Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger
Brought by drakestapleton
spark-rsi-mv / src / propose / cortex.rs
| 1 | use serde::{Deserialize, Serialize}; |
| 2 | use std::fs; |
| 3 | use std::path::PathBuf; |
| 4 | use std::time::Duration; |
| 5 | |
| 6 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 7 | pub struct CortexSearchResult { |
| 8 | pub id: String, |
| 9 | #[serde(default, rename = "canonicalName")] |
| 10 | pub canonical_name: Option<String>, |
| 11 | pub content: String, |
| 12 | #[serde(default)] |
| 13 | pub score: Option<f64>, |
| 14 | } |
| 15 | |
| 16 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 17 | struct CortexSearchEnvelope { |
| 18 | #[serde(default)] |
| 19 | pub results: Vec<CortexSearchResult>, |
| 20 | } |
| 21 | |
| 22 | #[derive(Debug, Clone)] |
| 23 | pub struct CortexExperienceClient { |
| 24 | pub base_url: String, |
| 25 | pub space: String, |
| 26 | pub token: Option<String>, |
| 27 | client: reqwest::Client, |
| 28 | } |
| 29 | |
| 30 | impl CortexExperienceClient { |
| 31 | pub fn new(base_url: &str) -> Self { |
| 32 | let token = Self::resolve_token(); |
| 33 | let client = reqwest::Client::builder() |
| 34 | .timeout(Duration::from_secs(10)) |
| 35 | .build() |
| 36 | .unwrap_or_default(); |
| 37 | |
| 38 | Self { |
| 39 | base_url: base_url.trim_end_matches('/').to_string(), |
| 40 | space: "atlas-memory".to_string(), |
| 41 | token, |
| 42 | client, |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | pub fn with_space(mut self, space: &str) -> Self { |
| 47 | self.space = space.to_string(); |
| 48 | self |
| 49 | } |
| 50 | |
| 51 | pub fn with_token(mut self, token: &str) -> Self { |
| 52 | self.token = Some(token.to_string()); |
| 53 | self |
| 54 | } |
| 55 | |
| 56 | fn resolve_token() -> Option<String> { |
| 57 | if let Ok(tok) = std::env::var("CORTEX_TOKEN") { |
| 58 | let t = tok.trim().to_string(); |
| 59 | if !t.is_empty() { |
| 60 | return Some(t); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | let home = std::env::var("HOME").ok()?; |
| 65 | let token_path = PathBuf::from(home).join(".config/cortex/token"); |
| 66 | if token_path.exists() { |
| 67 | if let Ok(content) = fs::read_to_string(&token_path) { |
| 68 | let t = content.trim().to_string(); |
| 69 | if !t.is_empty() { |
| 70 | return Some(t); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | None |
| 75 | } |
| 76 | |
| 77 | pub async fn recall_lessons(&self, query: &str, limit: usize) -> Vec<String> { |
| 78 | let url = format!("{}/api/cortex/search", self.base_url); |
| 79 | let limit_str = limit.to_string(); |
| 80 | let mut req = self.client.get(&url).query(&[ |
| 81 | ("q", query), |
| 82 | ("space", &self.space), |
| 83 | ("limit", &limit_str), |
| 84 | ]); |
| 85 | |
| 86 | if let Some(ref tok) = self.token { |
| 87 | req = req.bearer_auth(tok); |
| 88 | } |
| 89 | |
| 90 | match req.send().await { |
| 91 | Ok(resp) if resp.status().is_success() => { |
| 92 | if let Ok(envelope) = resp.json::<CortexSearchEnvelope>().await { |
| 93 | envelope |
| 94 | .results |
| 95 | .into_iter() |
| 96 | .map(|r| { |
| 97 | if let Some(ref name) = r.canonical_name { |
| 98 | format!("[{}] {}", name, r.content) |
| 99 | } else { |
| 100 | r.content |
| 101 | } |
| 102 | }) |
| 103 | .collect() |
| 104 | } else { |
| 105 | Vec::new() |
| 106 | } |
| 107 | } |
| 108 | _ => Vec::new(), |
| 109 | } |
| 110 | } |
| 111 | } |