apollo-agent 0.5.0

Local-first Rust AI agent runtime — Telegram-first, trait-driven, SurrealDB + RocksDB state layer.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! Skills system — scan SKILL.md files, match against user requests,
//! inject matched skill instructions into the system prompt.
//! Supports template variables and inline shell execution.

pub mod curator;

use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};

#[derive(Debug, Clone)]
pub struct Skill {
    pub name: String,
    pub description: String,
    pub location: PathBuf,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ManagedSkillInput {
    pub name: String,
    pub description: String,
    pub body: String,
}

/// Scan known skill directories for SKILL.md files
pub fn discover_skills() -> Vec<Skill> {
    discover_skills_for_workspace(None)
}

pub fn discover_skills_for_workspace(workspace: Option<&Path>) -> Vec<Skill> {
    let mut skills = Vec::new();
    let home = dirs::home_dir().unwrap_or_default();

    let openclaw_skills = home.join(".npm-global/lib/node_modules/openclaw/skills");
    scan_skill_dir(&openclaw_skills, &mut skills);

    let workspace_skills = home.join(".openclaw/workspace/skills");
    scan_skill_dir(&workspace_skills, &mut skills);

    if let Some(workspace) = workspace {
        let managed = managed_skills_dir(workspace);
        scan_skill_dir(&managed, &mut skills);

        // Host plugin directories carry SKILL.md too. `discover_host_plugins`
        // scanned these and only logged what it found, so an OpenClaw plugin
        // dropped into `plugins/` was discovered and then ignored by the one
        // system that could have used it.
        for dir in host_plugin_skill_dirs(workspace) {
            scan_skill_dir(&dir, &mut skills);
        }
    }

    skills
}

/// Plugin roots that may contain SKILL.md-bearing directories. Same roots
/// `plugin_hosts::discover_host_plugins` scans, deliberately.
fn host_plugin_skill_dirs(workspace: &Path) -> Vec<PathBuf> {
    vec![
        workspace.join("plugins"),
        workspace.join(".openclaw/plugins"),
        workspace.join(".hermes/plugins"),
    ]
}

pub fn managed_skills_dir(workspace: &Path) -> PathBuf {
    workspace.join(".apollo/skills")
}

fn scan_skill_dir(dir: &Path, skills: &mut Vec<Skill>) {
    if !dir.is_dir() {
        return;
    }

    if let Ok(entries) = std::fs::read_dir(dir) {
        for entry in entries.flatten() {
            let skill_dir = entry.path();
            if !skill_dir.is_dir() {
                continue;
            }

            let skill_md = skill_dir.join("SKILL.md");
            if !skill_md.exists() {
                continue;
            }

            if let Ok(content) = std::fs::read_to_string(&skill_md) {
                if let Some(skill) = parse_skill_frontmatter(&content, &skill_md) {
                    skills.push(skill);
                }
            }
        }
    }
}

fn extract_frontmatter(content: &str) -> Option<&str> {
    let rest = content.strip_prefix("---")?;
    let end = rest.find("---")?;
    Some(&rest[..end])
}

fn default_skill_name(path: &Path) -> String {
    path.parent()
        .and_then(|p| p.file_name())
        .map(|n| n.to_string_lossy().to_string())
        .unwrap_or_else(|| "unknown".to_string())
}

/// Parse YAML-like frontmatter from SKILL.md
fn parse_skill_frontmatter(content: &str, path: &Path) -> Option<Skill> {
    let frontmatter = extract_frontmatter(content)?;

    let mut name = None;
    let mut description = None;

    for line in frontmatter.lines() {
        let trimmed = line.trim();
        if let Some(val) = trimmed.strip_prefix("name:") {
            name = Some(val.trim().trim_matches('\'').trim_matches('"').to_string());
        }
        if let Some(val) = trimmed.strip_prefix("description:") {
            description = Some(val.trim().trim_matches('\'').trim_matches('"').to_string());
        }
    }

    Some(Skill {
        name: name.unwrap_or_else(|| default_skill_name(path)),
        description: description.unwrap_or_default(),
        location: path.to_path_buf(),
    })
}

// ── Matching ──────────────────────────────────────────────────────────────

const STOPWORDS: &[&str] = &[
    "the", "and", "for", "are", "but", "not", "you", "all", "can", "had", "her", "was", "one",
    "our", "out", "has", "have", "been", "some", "them", "than", "its", "over", "such", "that",
    "this", "with", "will", "each", "from", "they", "were", "which", "their", "said", "what",
    "when", "who", "how", "use", "new", "now", "way", "may", "get", "got", "set", "let", "any",
    "also", "into", "just", "only", "very", "even", "most", "other", "need", "make", "like",
    "does", "your", "more", "want", "should",
];

fn is_stopword(word: &str) -> bool {
    STOPWORDS.contains(&word)
}

pub fn match_skill<'a>(skills: &'a [Skill], user_message: &str) -> Option<&'a Skill> {
    let msg_lower = user_message.to_lowercase();
    let msg_words: Vec<&str> = msg_lower.split_whitespace().collect();

    let mut best_score = 0.0f32;
    let mut best_skill = None;

    for skill in skills {
        let desc_lower = skill.description.to_lowercase();
        let name_lower = skill.name.to_lowercase();
        let mut score = 0.0f32;

        if msg_lower.contains(&name_lower) && name_lower.len() >= 4 {
            score += 10.0;
        }

        for word in &msg_words {
            if word.len() < 4 || is_stopword(word) {
                continue;
            }
            if desc_lower.contains(word) {
                score += 1.0;
            }
        }

        for word in desc_lower.split(|c: char| !c.is_alphanumeric()) {
            if word.len() < 4 || is_stopword(word) {
                continue;
            }
            if msg_lower.contains(word) {
                score += 1.0;
            }
        }

        if score > best_score {
            best_score = score;
            best_skill = Some(skill);
        }
    }

    if best_score >= 5.0 {
        best_skill
    } else {
        None
    }
}

// ── Template variable substitution / inline shell ─────────────────
// Ported from hermes-agent skill_preprocessing.py

use regex::Regex;
use std::sync::OnceLock;

fn skill_template_re() -> &'static Regex {
    static RE: OnceLock<Regex> = OnceLock::new();
    RE.get_or_init(|| Regex::new(r"\$\{(HERMES_SKILL_DIR|HERMES_SESSION_ID)\}").unwrap())
}

fn inline_shell_re() -> &'static Regex {
    static RE: OnceLock<Regex> = OnceLock::new();
    RE.get_or_init(|| Regex::new(r"!`([^`\n]+)`").unwrap())
}

/// Load skill content raw (no preprocessing).
pub fn load_skill_content(skill: &Skill) -> Option<String> {
    std::fs::read_to_string(&skill.location).ok()
}

/// Preprocess skill content: substitute template vars and expand inline shell.
pub fn preprocess_skill_content(
    content: &str,
    skill_dir: Option<&Path>,
    session_id: Option<&str>,
    cwd: Option<&Path>,
) -> String {
    let content = substitute_template_vars(content, skill_dir, session_id);
    expand_inline_shell(&content, cwd, 30)
}

/// Substitute `${HERMES_SKILL_DIR}` and `${HERMES_SESSION_ID}` in skill content.
/// Unresolved tokens are left as-is so the author can debug them.
pub fn substitute_template_vars(
    content: &str,
    skill_dir: Option<&Path>,
    session_id: Option<&str>,
) -> String {
    skill_template_re()
        .replace_all(content, |caps: &regex::Captures| {
            match caps.get(1).map(|m| m.as_str()) {
                Some("HERMES_SKILL_DIR") => skill_dir
                    .map(|p| p.to_string_lossy().to_string())
                    .unwrap_or_else(|| caps[0].to_string()),
                Some("HERMES_SESSION_ID") => session_id
                    .map(|s| s.to_string())
                    .unwrap_or_else(|| caps[0].to_string()),
                _ => caps[0].to_string(),
            }
        })
        .to_string()
}

/// Execute inline shell snippets (`!\`command\``) in skill content.
/// Replaces each snippet with its stdout (trimmed).
/// Failures produce a short `[inline-shell error: ...]` marker.
pub fn expand_inline_shell(content: &str, cwd: Option<&Path>, _timeout_secs: u64) -> String {
    inline_shell_re()
        .replace_all(content, |caps: &regex::Captures| {
            let cmd = caps.get(1).map(|m| m.as_str()).unwrap_or("");
            if cmd.is_empty() {
                return String::new();
            }
            match std::process::Command::new("sh")
                .arg("-c")
                .arg(cmd)
                .current_dir(cwd.unwrap_or(Path::new(".")))
                .output()
            {
                Ok(output) if output.status.success() => {
                    let stdout = String::from_utf8_lossy(&output.stdout).trim().to_string();
                    match crate::text::truncate_chars_counted(&stdout, 4000) {
                        Some((head, dropped)) => {
                            format!("{}… [truncated {} chars]", head, dropped)
                        }
                        None => stdout,
                    }
                }
                Ok(output) => {
                    let stderr = String::from_utf8_lossy(&output.stderr);
                    format!(
                        "[inline-shell error: {}]",
                        stderr.trim().chars().take(120).collect::<String>()
                    )
                }
                Err(e) => format!("[inline-shell error: {}]", e),
            }
        })
        .to_string()
}

// ── Save managed skill ────────────────────────────────────────────────────

pub fn save_managed_skill(workspace: &Path, input: &ManagedSkillInput) -> anyhow::Result<PathBuf> {
    let dir = managed_skills_dir(workspace).join(slugify(&input.name));
    std::fs::create_dir_all(&dir)?;
    let path = dir.join("SKILL.md");
    let content = format!(
        "---\nname: {}\ndescription: {}\n---\n\n{}\n",
        input.name, input.description, input.body
    );
    std::fs::write(&path, content)?;
    Ok(path)
}

fn slugify(name: &str) -> String {
    name.to_lowercase()
        .chars()
        .map(|c| if c.is_ascii_alphanumeric() { c } else { '-' })
        .collect::<String>()
        .trim_matches('-')
        .to_string()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn host_plugin_skills_are_discovered() {
        let dir = tempfile::tempdir().unwrap();
        let workspace = dir.path();

        // An OpenClaw-style plugin dropped into the workspace. Discovery used
        // to find this and log it; nothing loaded it.
        let plugin = workspace.join("plugins/weather");
        std::fs::create_dir_all(&plugin).unwrap();
        std::fs::write(
            plugin.join("SKILL.md"),
            "---\nname: weather\ndescription: forecast lookup\n---\n# Weather\n",
        )
        .unwrap();

        let skills = discover_skills_for_workspace(Some(workspace));
        assert!(
            skills.iter().any(|s| s.name == "weather"),
            "host plugin SKILL.md was discovered but never loaded; got {:?}",
            skills.iter().map(|s| &s.name).collect::<Vec<_>>()
        );
    }

    #[test]
    fn hermes_and_openclaw_plugin_roots_are_both_scanned() {
        let dir = tempfile::tempdir().unwrap();
        let workspace = dir.path();

        for (root, name) in [
            (".hermes/plugins", "hermes-skill"),
            (".openclaw/plugins", "openclaw-skill"),
        ] {
            let plugin = workspace.join(root).join(name);
            std::fs::create_dir_all(&plugin).unwrap();
            std::fs::write(
                plugin.join("SKILL.md"),
                format!("---\nname: {name}\ndescription: d\n---\n"),
            )
            .unwrap();
        }

        let found: Vec<String> = discover_skills_for_workspace(Some(workspace))
            .into_iter()
            .map(|s| s.name)
            .collect();
        assert!(found.contains(&"hermes-skill".to_string()), "{found:?}");
        assert!(found.contains(&"openclaw-skill".to_string()), "{found:?}");
    }

    #[test]
    fn test_parse_frontmatter() {
        let content = "---\nname: test-skill\ndescription: A test skill\n---\n# Content";
        let skill = parse_skill_frontmatter(content, Path::new("/tmp/test/SKILL.md"));
        assert!(skill.is_some());
        let s = skill.unwrap();
        assert_eq!(s.name, "test-skill");
        assert_eq!(s.description, "A test skill");
    }

    #[test]
    fn test_match_skill() {
        let skills = vec![
            Skill {
                name: "weather".to_string(),
                description: "Get weather forecasts for any location".to_string(),
                location: PathBuf::from("/tmp/weather/SKILL.md"),
            },
            Skill {
                name: "github".to_string(),
                description: "GitHub operations, PRs, issues, code review".to_string(),
                location: PathBuf::from("/tmp/github/SKILL.md"),
            },
        ];

        let matched = match_skill(&skills, "what's the weather in Melbourne?");
        assert!(matched.is_some());
        assert_eq!(matched.unwrap().name, "weather");

        let matched = match_skill(&skills, "review the github issues");
        assert!(matched.is_some());
        assert_eq!(matched.unwrap().name, "github");
    }

    #[test]
    fn test_template_substitution() {
        let result = substitute_template_vars(
            "Run from ${HERMES_SKILL_DIR} for session ${HERMES_SESSION_ID}",
            Some(Path::new("/tmp/myskill")),
            Some("sess_123"),
        );
        assert_eq!(result, "Run from /tmp/myskill for session sess_123");
    }

    #[test]
    fn test_unknown_template_preserved() {
        let content = "Token ${UNKNOWN} stays";
        let result = substitute_template_vars(content, None, None);
        assert_eq!(result, "Token ${UNKNOWN} stays");
    }

    #[test]
    fn test_inline_shell_expansion() {
        let result = expand_inline_shell("Date is !`echo hello`", None, 5);
        assert!(result.contains("hello"));
    }
}