aien-sovereign-core
Autonomous sovereign intelligence engine and runtime core for NVIDIA DGX Spark
Brought by drakestapleton
aien-sovereign-core / crates / aien-cli / src / goals.rs
| 1 | use serde::{Deserialize, Serialize}; |
| 2 | use chrono::Utc; |
| 3 | use colored::*; |
| 4 | use std::fs; |
| 5 | use std::path::{Path, PathBuf}; |
| 6 | use serde_json::{json, Value}; |
| 7 | |
| 8 | #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] |
| 9 | pub enum GoalStatus { |
| 10 | Active, |
| 11 | Completed, |
| 12 | Paused, |
| 13 | } |
| 14 | |
| 15 | impl std::fmt::Display for GoalStatus { |
| 16 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 17 | match self { |
| 18 | GoalStatus::Active => write!(f, "Active"), |
| 19 | GoalStatus::Completed => write!(f, "Completed"), |
| 20 | GoalStatus::Paused => write!(f, "Paused"), |
| 21 | } |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 26 | pub struct Milestone { |
| 27 | pub id: usize, |
| 28 | pub description: String, |
| 29 | pub completed: bool, |
| 30 | } |
| 31 | |
| 32 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 33 | pub struct Goal { |
| 34 | pub id: String, |
| 35 | pub title: String, |
| 36 | pub description: String, |
| 37 | pub status: GoalStatus, |
| 38 | pub milestones: Vec<Milestone>, |
| 39 | pub created_at: String, |
| 40 | pub updated_at: String, |
| 41 | } |
| 42 | |
| 43 | #[derive(Debug, Clone, Serialize, Deserialize, Default)] |
| 44 | pub struct GoalManifest { |
| 45 | pub project_name: String, |
| 46 | pub goals: Vec<Goal>, |
| 47 | } |
| 48 | |
| 49 | pub fn get_goals_path() -> PathBuf { |
| 50 | let local = Path::new(".goals.json"); |
| 51 | if local.exists() { |
| 52 | return local.to_path_buf(); |
| 53 | } |
| 54 | PathBuf::from("/home/drakestapleton/basecamp/goals.json") |
| 55 | } |
| 56 | |
| 57 | pub fn load_goals() -> GoalManifest { |
| 58 | let path = get_goals_path(); |
| 59 | if path.exists() { |
| 60 | if let Ok(content) = fs::read_to_string(&path) { |
| 61 | if let Ok(manifest) = serde_json::from_str::<GoalManifest>(&content) { |
| 62 | return manifest; |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | GoalManifest { |
| 67 | project_name: "Sovereign Workspace".to_string(), |
| 68 | goals: Vec::new(), |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | pub fn save_goals(manifest: &GoalManifest) -> Result<PathBuf, String> { |
| 73 | let path = get_goals_path(); |
| 74 | let json_str = serde_json::to_string_pretty(manifest).map_err(|e| e.to_string())?; |
| 75 | fs::write(&path, json_str).map_err(|e| e.to_string())?; |
| 76 | Ok(path) |
| 77 | } |
| 78 | |
| 79 | pub fn add_goal(title: &str, description: &str, milestones_raw: Vec<String>) -> Goal { |
| 80 | let mut manifest = load_goals(); |
| 81 | let id = format!("goal-{}", Utc::now().timestamp()); |
| 82 | |
| 83 | let milestones: Vec<Milestone> = milestones_raw.into_iter().enumerate().map(|(idx, d)| Milestone { |
| 84 | id: idx + 1, |
| 85 | description: d, |
| 86 | completed: false, |
| 87 | }).collect(); |
| 88 | |
| 89 | let now_str = Utc::now().to_rfc3339(); |
| 90 | let goal = Goal { |
| 91 | id: id.clone(), |
| 92 | title: title.to_string(), |
| 93 | description: description.to_string(), |
| 94 | status: GoalStatus::Active, |
| 95 | milestones, |
| 96 | created_at: now_str.clone(), |
| 97 | updated_at: now_str, |
| 98 | }; |
| 99 | |
| 100 | manifest.goals.push(goal.clone()); |
| 101 | let _ = save_goals(&manifest); |
| 102 | goal |
| 103 | } |
| 104 | |
| 105 | pub fn complete_goal(target_id: &str) -> Result<String, String> { |
| 106 | let mut manifest = load_goals(); |
| 107 | let mut found = false; |
| 108 | let mut title = String::new(); |
| 109 | |
| 110 | for g in manifest.goals.iter_mut() { |
| 111 | if g.id == target_id || g.title.to_lowercase().contains(&target_id.to_lowercase()) { |
| 112 | g.status = GoalStatus::Completed; |
| 113 | for m in g.milestones.iter_mut() { |
| 114 | m.completed = true; |
| 115 | } |
| 116 | g.updated_at = Utc::now().to_rfc3339(); |
| 117 | title = g.title.clone(); |
| 118 | found = true; |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | if found { |
| 124 | let _ = save_goals(&manifest); |
| 125 | Ok(format!("Goal '{}' ({}) marked as Completed.", title, target_id)) |
| 126 | } else { |
| 127 | Err(format!("Goal '{}' not found in manifest.", target_id)) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | pub fn complete_milestone(target_id: &str, milestone_id: usize) -> Result<String, String> { |
| 132 | let mut manifest = load_goals(); |
| 133 | let mut found = false; |
| 134 | let mut all_completed = false; |
| 135 | |
| 136 | for g in manifest.goals.iter_mut() { |
| 137 | if g.id == target_id || g.title.to_lowercase().contains(&target_id.to_lowercase()) { |
| 138 | for m in g.milestones.iter_mut() { |
| 139 | if m.id == milestone_id { |
| 140 | m.completed = true; |
| 141 | found = true; |
| 142 | } |
| 143 | } |
| 144 | g.updated_at = Utc::now().to_rfc3339(); |
| 145 | if g.milestones.iter().all(|m| m.completed) { |
| 146 | g.status = GoalStatus::Completed; |
| 147 | all_completed = true; |
| 148 | } |
| 149 | break; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | if found { |
| 154 | let _ = save_goals(&manifest); |
| 155 | if all_completed { |
| 156 | Ok(format!("Milestone {} completed. All milestones finished! Goal is now Completed.", milestone_id)) |
| 157 | } else { |
| 158 | Ok(format!("Milestone {} completed.", milestone_id)) |
| 159 | } |
| 160 | } else { |
| 161 | Err(format!("Milestone {} for goal '{}' not found.", milestone_id, target_id)) |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | pub fn format_goals_tui() -> String { |
| 166 | let manifest = load_goals(); |
| 167 | let mut out = String::new(); |
| 168 | |
| 169 | out.push_str(&format!("{}\n", format!("=== Project Goals: {} ===", manifest.project_name).cyan().bold())); |
| 170 | if manifest.goals.is_empty() { |
| 171 | out.push_str(&format!("{}\n", "No active goals in manifest. Use '/goal new <title>' to create one.".dimmed())); |
| 172 | return out; |
| 173 | } |
| 174 | |
| 175 | for (i, g) in manifest.goals.iter().enumerate() { |
| 176 | let status_colored = match g.status { |
| 177 | GoalStatus::Active => "[ACTIVE]".green().bold(), |
| 178 | GoalStatus::Completed => "[DONE]".dimmed(), |
| 179 | GoalStatus::Paused => "[PAUSED]".yellow(), |
| 180 | }; |
| 181 | |
| 182 | out.push_str(&format!("{}. {} {} ({})\n", i + 1, status_colored, g.title.bold(), g.id.cyan())); |
| 183 | if !g.description.is_empty() { |
| 184 | out.push_str(&format!(" {}\n", g.description.dimmed())); |
| 185 | } |
| 186 | |
| 187 | for m in &g.milestones { |
| 188 | let mark = if m.completed { "✓".green() } else { "○".dimmed() }; |
| 189 | out.push_str(&format!(" {} {}. {}\n", mark, m.id, m.description)); |
| 190 | } |
| 191 | out.push('\n'); |
| 192 | } |
| 193 | |
| 194 | out |
| 195 | } |
| 196 | |
| 197 | pub fn goals_dispatch_tool(args: &Value) -> Value { |
| 198 | let action = args.get("action").and_then(Value::as_str).unwrap_or("list"); |
| 199 | |
| 200 | match action { |
| 201 | "milestone_done" | "advance" => { |
| 202 | let id = args.get("id").and_then(Value::as_str).unwrap_or(""); |
| 203 | let m_id = args.get("milestone_id").and_then(Value::as_u64).unwrap_or(1) as usize; |
| 204 | match complete_milestone(id, m_id) { |
| 205 | Ok(msg) => json!({"status": "ok", "message": msg}), |
| 206 | Err(e) => json!({"status": "error", "error": e}), |
| 207 | } |
| 208 | }, |
| 209 | "new" | "create" => { |
| 210 | let title = args.get("title").and_then(Value::as_str).unwrap_or("Untitled Goal"); |
| 211 | let desc = args.get("description").and_then(Value::as_str).unwrap_or(""); |
| 212 | let milestones: Vec<String> = args.get("milestones") |
| 213 | .and_then(Value::as_array) |
| 214 | .map(|arr| arr.iter().filter_map(Value::as_str).map(|s| s.to_string()).collect()) |
| 215 | .unwrap_or_default(); |
| 216 | |
| 217 | let g = add_goal(title, desc, milestones); |
| 218 | json!({ |
| 219 | "status": "ok", |
| 220 | "goal": g, |
| 221 | "message": format!("Goal '{}' initialized successfully.", title) |
| 222 | }) |
| 223 | }, |
| 224 | "done" | "complete" => { |
| 225 | let id = args.get("id").and_then(Value::as_str).unwrap_or(""); |
| 226 | match complete_goal(id) { |
| 227 | Ok(msg) => json!({"status": "ok", "message": msg}), |
| 228 | Err(e) => json!({"status": "error", "error": e}), |
| 229 | } |
| 230 | }, |
| 231 | "list" => { |
| 232 | let manifest = load_goals(); |
| 233 | json!({ |
| 234 | "status": "ok", |
| 235 | "project": manifest.project_name, |
| 236 | "total_goals": manifest.goals.len(), |
| 237 | "goals": manifest.goals |
| 238 | }) |
| 239 | }, |
| 240 | _ => json!({"error": format!("Unknown goal action '{}'", action)}) |
| 241 | } |
| 242 | } |