interplane.Get Omega
Read in your language

aien-sovereign-core

Autonomous sovereign intelligence engine and runtime core for NVIDIA DGX Spark

Brought by drakestapleton

branch main Branches Tags History

aien-sovereign-core / crates / aien-cli / src / skills.rs

163 lines · 5710 bytes Raw Blame History
1use std::fs;
2use std::path::{Path, PathBuf};
3use colored::*;
4use serde::{Deserialize, Serialize};
5use serde_json::{json, Value};
6
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct Skill {
9 pub name: String,
10 pub description: String,
11 pub path: String,
12 pub has_scripts: bool,
13}
14
15pub fn get_skills_dir() -> PathBuf {
16 PathBuf::from("/home/drakestapleton/skills")
17}
18
19fn extract_frontmatter_field(content: &str, field: &str) -> Option<String> {
20 let mut in_frontmatter = false;
21 for line in content.lines() {
22 let trimmed = line.trim();
23 if trimmed == "---" {
24 if in_frontmatter {
25 break;
26 } else {
27 in_frontmatter = true;
28 continue;
29 }
30 }
31 if in_frontmatter {
32 if let Some(rest) = trimmed.strip_prefix(&format!("{}:", field)) {
33 return Some(rest.trim().trim_matches('"').trim_matches('\'').to_string());
34 }
35 }
36 }
37 None
38}
39
40pub fn discover_skills() -> Vec<Skill> {
41 let dir = get_skills_dir();
42 let mut skills = Vec::new();
43 if !dir.exists() {
44 let _ = fs::create_dir_all(&dir);
45 return skills;
46 }
47
48 if let Ok(entries) = fs::read_dir(&dir) {
49 for entry in entries.flatten() {
50 let path = entry.path();
51 if path.is_dir() {
52 let skill_md = path.join("SKILL.md");
53 if skill_md.exists() {
54 let content = fs::read_to_string(&skill_md).unwrap_or_default();
55 let name = extract_frontmatter_field(&content, "name")
56 .unwrap_or_else(|| path.file_name().unwrap_or_default().to_string_lossy().to_string());
57 let desc = extract_frontmatter_field(&content, "description")
58 .unwrap_or_else(|| "No description provided.".to_string());
59 let has_scripts = path.join("scripts").exists();
60 skills.push(Skill {
61 name,
62 description: desc,
63 path: skill_md.display().to_string(),
64 has_scripts,
65 });
66 }
67 }
68 }
69 }
70 skills
71}
72
73pub fn format_skills_tui() -> String {
74 let skills = discover_skills();
75 let mut out = String::new();
76 out.push_str(&format!("{}\n", "=== AIEN Sovereign Skill Registry ===".cyan().bold()));
77 out.push_str(&format!("Directory: {}\n\n", get_skills_dir().display().to_string().dimmed()));
78
79 if skills.is_empty() {
80 out.push_str(&format!("{}\n", "No skills currently installed in ~/skills/. Create a folder with SKILL.md to define one.".dimmed()));
81 return out;
82 }
83
84 for (i, s) in skills.iter().enumerate() {
85 let scripts_badge = if s.has_scripts { "[scripts]".yellow() } else { "".normal() };
86 out.push_str(&format!("{}. {} {} ({})\n", i + 1, s.name.bold().green(), scripts_badge, s.path.dimmed()));
87 out.push_str(&format!(" {}\n\n", s.description.dimmed()));
88 }
89
90 out
91}
92
93pub fn read_skill_content(name: &str) -> Result<String, String> {
94 let dir = get_skills_dir();
95 let skill_path = dir.join(name).join("SKILL.md");
96 if skill_path.exists() {
97 fs::read_to_string(&skill_path).map_err(|e| e.to_string())
98 } else {
99 // Try searching by name match
100 for s in discover_skills() {
101 if s.name.to_lowercase() == name.to_lowercase() {
102 return fs::read_to_string(&s.path).map_err(|e| e.to_string());
103 }
104 }
105 Err(format!("Skill '{}' not found in ~/skills/", name))
106 }
107}
108
109pub fn skills_dispatch_tool(args: &Value) -> Value {
110 let action = args.get("action").and_then(Value::as_str).unwrap_or("list");
111
112 match action {
113 "list" => {
114 let skills = discover_skills();
115 json!({
116 "status": "ok",
117 "total": skills.len(),
118 "skills": skills
119 })
120 },
121 "read" | "load" => {
122 let name = args.get("name").and_then(Value::as_str).unwrap_or("");
123 match read_skill_content(name) {
124 Ok(content) => json!({"status": "ok", "name": name, "content": content}),
125 Err(e) => json!({"status": "error", "error": e}),
126 }
127 },
128 "optimize" => {
129 let name = args.get("name").and_then(Value::as_str).unwrap_or("atlas-skillopt");
130 optimize_skill(name)
131 },
132 _ => json!({"error": format!("Unknown skill action '{}'", action)})
133 }
134}
135
136pub fn optimize_skill(name: &str) -> Value {
137 let script = "/home/drakestapleton/atlas-skillopt-stage.sh";
138 if !Path::new(script).exists() {
139 return json!({"status": "error", "error": "atlas-skillopt-stage.sh not found"});
140 }
141 let output = std::process::Command::new("bash")
142 .arg(script)
143 .output();
144 match output {
145 Ok(out) => {
146 let stdout = String::from_utf8_lossy(&out.stdout).to_string();
147 let stderr = String::from_utf8_lossy(&out.stderr).to_string();
148 json!({
149 "status": if out.status.success() { "ok" } else { "failed" },
150 "target_skill": name,
151 "stdout": stdout,
152 "stderr": stderr
153 })
154 },
155 Err(e) => json!({"status": "error", "error": e.to_string()})
156 }
157}
158
159pub fn run_optimize_cli(name: &str) {
160 println!("{}", format!("Starting Microsoft SkillOpt self-improvement loop for '{}'...", name).cyan().bold());
161 let res = optimize_skill(name);
162 println!("{}", serde_json::to_string_pretty(&res).unwrap_or_default());
163}