matrixcode-core 0.3.7

MatrixCode Agent Core - Pure logic, no UI
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//! Skill discovery and loading.
//!
//! A "skill" is a markdown file with a YAML frontmatter block, or a directory
//! containing skill files. Two formats are supported:
//!
//! **Format 1: Single-file skill (SKILL.md)**
//! ```text
//! skills/
//!   my-skill/
//!     SKILL.md            # required, with frontmatter
//!     helper.py           # optional support files
//!     templates/...       # optional subdirs
//! ```
//!
//! **Format 2: Multi-file skills (like Claude Code plugins)**
//! ```text
//! skills/
//!   om/
//!     debug.md            # each .md file is a separate skill
//!     feature.md
//!     plan.md
//!     ...
//! ```
//!
//! Skills are surfaced to the model in two stages:
//! 1. At startup every skill's name + description is injected into the
//!    system prompt so the model knows what's available.
//! 2. The `skill` tool loads the full skill body (and lists the
//!    skill's files) when the model decides to use one. This keeps
//!    baseline token cost low even with many skills installed.

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

use anyhow::{Context, Result};

/// A loaded skill ready to be advertised to the model.
#[derive(Debug, Clone)]
pub struct Skill {
    /// Canonical identifier, taken from frontmatter `name` or file name.
    /// Used as the argument to the `skill` tool.
    pub name: String,
    /// Short one-line description shown in the system prompt.
    pub description: String,
    /// Absolute path to the skill directory.
    pub dir: PathBuf,
    /// Full markdown body (without frontmatter).
    pub body: String,
    /// The source .md file path (SKILL.md or other .md file).
    pub source_file: PathBuf,
}

impl Skill {
    /// Path to this skill's source file.
    pub fn skill_md(&self) -> PathBuf {
        self.source_file.clone()
    }
}

/// Walk the given roots and load skills from both formats:
/// - Format 1: directories with `SKILL.md` (backward compatible)
/// - Format 2: directories with multiple `.md` files (Claude Code style)
///
/// Missing roots are silently skipped so users can keep a personal
/// `~/.matrix/skills` directory without the project-local one (or
/// vice versa). Skills with duplicate names: first one wins, later ones
/// are dropped with a stderr warning so precedence is predictable.
pub fn discover_skills(roots: &[PathBuf]) -> Vec<Skill> {
    let mut out: Vec<Skill> = Vec::new();

    for root in roots {
        if !root.is_dir() {
            continue;
        }
        let entries = match std::fs::read_dir(root) {
            Ok(e) => e,
            Err(e) => {
                eprintln!("[warn] could not read skills dir {}: {e}", root.display());
                continue;
            }
        };
        for entry in entries.flatten() {
            let path = entry.path();
            if !path.is_dir() {
                continue;
            }
            
            // Try Format 1: SKILL.md exists
            let skill_md = path.join("SKILL.md");
            if skill_md.is_file() {
                match load_skill_from_file(&skill_md, &path) {
                    Ok(skill) => {
                        add_skill(&mut out, skill);
                    }
                    Err(e) => {
                        eprintln!("[warn] skipping skill at {}: {e}", path.display());
                    }
                }
                continue;
            }
            
            // Try Format 2: multiple .md files in directory
            load_multi_file_skills(&path, &mut out);
        }
    }

    out.sort_by(|a, b| a.name.cmp(&b.name));
    out
}

/// Add a skill to the list, checking for duplicates.
fn add_skill(out: &mut Vec<Skill>, skill: Skill) {
    if out.iter().any(|s| s.name == skill.name) {
        eprintln!(
            "[warn] duplicate skill name '{}' at {} (ignored)",
            skill.name,
            skill.source_file.display()
        );
        return;
    }
    out.push(skill);
}

/// Load skills from a directory containing multiple .md files.
/// Each .md file with frontmatter becomes a separate skill.
fn load_multi_file_skills(dir: &Path, out: &mut Vec<Skill>) {
    let entries = match std::fs::read_dir(dir) {
        Ok(e) => e,
        Err(e) => {
            eprintln!("[warn] could not read skill dir {}: {e}", dir.display());
            return;
        }
    };
    
    for entry in entries.flatten() {
        let path = entry.path();
        if !path.is_file() {
            continue;
        }
        
        // Only process .md files
        let ext = path.extension().and_then(|e| e.to_str());
        if ext != Some("md") {
            continue;
        }
        
        // Skip SKILL.md (already handled in Format 1)
        if path.file_name().and_then(|n| n.to_str()) == Some("SKILL.md") {
            continue;
        }
        
        match load_skill_from_file(&path, dir) {
            Ok(skill) => {
                add_skill(out, skill);
            }
            Err(e) => {
                // Only warn if the file has frontmatter (indicating it's meant to be a skill)
                let raw = std::fs::read_to_string(&path).unwrap_or_default();
                if raw.trim_start().starts_with("---") {
                    eprintln!("[warn] skipping skill file {}: {e}", path.display());
                }
            }
        }
    }
}

/// Load a skill from a specific .md file.
/// Public so tests and the `skill` tool can reload a specific skill.
pub fn load_skill_from_file(md_path: &Path, dir: &Path) -> Result<Skill> {
    let raw = std::fs::read_to_string(md_path)
        .with_context(|| format!("reading {}", md_path.display()))?;
    let (front, body) = split_frontmatter(&raw)
        .with_context(|| format!("parsing frontmatter of {}", md_path.display()))?;

    // Name: frontmatter > filename (without .md) > directory name
    let name = front
        .get("name")
        .cloned()
        .filter(|s| !s.is_empty())
        .or_else(|| {
            md_path
                .file_stem()
                .and_then(|n| n.to_str())
                .map(|s| s.to_string())
        })
        .or_else(|| {
            dir.file_name()
                .and_then(|n| n.to_str())
                .map(|s| s.to_string())
        })
        .ok_or_else(|| anyhow::anyhow!("skill has no 'name' in frontmatter"))?;

    let description = front
        .get("description")
        .cloned()
        .unwrap_or_else(|| "(no description)".to_string());

    Ok(Skill {
        name,
        description,
        dir: dir.to_path_buf(),
        body: body.to_string(),
        source_file: md_path.to_path_buf(),
    })
}

/// Load a skill from a directory with SKILL.md (backward compatible).
pub fn load_skill(dir: &Path) -> Result<Skill> {
    let md_path = dir.join("SKILL.md");
    load_skill_from_file(&md_path, dir)
}

/// Minimal YAML-frontmatter parser: supports the shape
/// ```text
/// ---
/// name: foo
/// description: bar baz
/// ---
/// body...
/// ```
/// Only flat `key: value` pairs are recognised. Values may be wrapped
/// in matching single or double quotes. Missing frontmatter is treated
/// as empty (body == whole file), which keeps bare markdown files from
/// being rejected outright — though they'll fall back to the directory
/// name and generic description.
fn split_frontmatter(raw: &str) -> Result<(std::collections::BTreeMap<String, String>, &str)> {
    let mut front = std::collections::BTreeMap::new();

    let trimmed = raw.trim_start_matches('\u{feff}'); // strip BOM if any
    let Some(rest) = trimmed.strip_prefix("---") else {
        return Ok((front, trimmed));
    };
    // Require newline right after opening ---
    let rest = rest.strip_prefix('\n').or_else(|| rest.strip_prefix("\r\n"));
    let Some(rest) = rest else {
        return Ok((front, trimmed));
    };

    // Find closing --- on its own line.
    let mut end_idx: Option<usize> = None;
    let mut cursor = 0usize;
    for line in rest.split_inclusive('\n') {
        let trimmed_line = line.trim_end_matches(['\n', '\r']);
        if trimmed_line == "---" {
            end_idx = Some(cursor + line.len());
            break;
        }
        cursor += line.len();
    }
    let Some(end) = end_idx else {
        // No closing delimiter — treat whole file as body, no frontmatter.
        return Ok((front, trimmed));
    };

    let front_block = &rest[..cursor];
    let body = rest[end..].trim_start_matches(['\n', '\r']);

    for line in front_block.lines() {
        let line = line.trim();
        if line.is_empty() || line.starts_with('#') {
            continue;
        }
        let Some((k, v)) = line.split_once(':') else {
            continue;
        };
        let key = k.trim().to_string();
        let val = unquote(v.trim());
        if !key.is_empty() {
            front.insert(key, val);
        }
    }

    Ok((front, body))
}

fn unquote(s: &str) -> String {
    let bytes = s.as_bytes();
    if bytes.len() >= 2
        && ((bytes[0] == b'"' && bytes[bytes.len() - 1] == b'"')
            || (bytes[0] == b'\'' && bytes[bytes.len() - 1] == b'\''))
    {
        return s[1..s.len() - 1].to_string();
    }
    s.to_string()
}

/// Render the skills catalogue body for insertion into a prompt section.
/// Returns `None` if there are no skills, so callers can skip injection
/// entirely rather than bolting on an empty section.
pub fn format_catalogue(skills: &[Skill]) -> Option<String> {
    if skills.is_empty() {
        return None;
    }
    let mut s = String::from(
        "Use the `skill` tool with the skill's name to load its full instructions:
",
    );
    for sk in skills {
        s.push_str(&format!("- {}: {}
", sk.name, sk.description));
    }
    Some(s)
}

/// List files inside a skill directory (recursive), relative to the
/// skill root. Used by the `skill` tool so the model knows what
/// supporting files it can `read` next.
pub fn list_skill_files(dir: &Path) -> Vec<String> {
    let mut out = Vec::new();
    walk(dir, dir, &mut out);
    out.sort();
    out
}

fn walk(root: &Path, cur: &Path, out: &mut Vec<String>) {
    let entries = match std::fs::read_dir(cur) {
        Ok(e) => e,
        Err(_) => return,
    };
    for entry in entries.flatten() {
        let p = entry.path();
        let file_type = match entry.file_type() {
            Ok(t) => t,
            Err(_) => continue,
        };
        if file_type.is_dir() {
            walk(root, &p, out);
        } else if file_type.is_file()
            && let Ok(rel) = p.strip_prefix(root) {
                out.push(rel.display().to_string());
            }
    }
}

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

    fn write_file(path: &Path, body: &str) {
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        std::fs::write(path, body).unwrap();
    }

    #[test]
    fn parses_basic_frontmatter() {
        let (front, body) =
            split_frontmatter("---\nname: foo\ndescription: hi there\n---\nbody text\n").unwrap();
        assert_eq!(front.get("name").unwrap(), "foo");
        assert_eq!(front.get("description").unwrap(), "hi there");
        assert_eq!(body, "body text\n");
    }

    #[test]
    fn quoted_values_are_unwrapped() {
        let (front, _) =
            split_frontmatter("---\nname: 'foo bar'\ndescription: \"baz\"\n---\nx").unwrap();
        assert_eq!(front.get("name").unwrap(), "foo bar");
        assert_eq!(front.get("description").unwrap(), "baz");
    }

    #[test]
    fn missing_frontmatter_returns_whole_body() {
        let (front, body) = split_frontmatter("just markdown\nno front").unwrap();
        assert!(front.is_empty());
        assert_eq!(body, "just markdown\nno front");
    }

    #[test]
    fn unclosed_frontmatter_falls_back_to_body() {
        let (front, body) = split_frontmatter("---\nname: foo\nbody without close").unwrap();
        assert!(front.is_empty());
        assert!(body.starts_with("---"));
    }

    #[test]
    fn discover_loads_skill_directory() {
        let tmp = tempdir().unwrap();
        let root = tmp.path().join("skills");
        write_file(
            &root.join("greet/SKILL.md"),
            "---\nname: greet\ndescription: say hi\n---\nSay hello to the user.\n",
        );
        write_file(&root.join("greet/extra.txt"), "support file");

        let skills = discover_skills(&[root]);
        assert_eq!(skills.len(), 1);
        assert_eq!(skills[0].name, "greet");
        assert_eq!(skills[0].description, "say hi");
        assert!(skills[0].body.contains("Say hello"));
        let files = list_skill_files(&skills[0].dir);
        assert!(files.iter().any(|f| f == "SKILL.md"));
        assert!(files.iter().any(|f| f == "extra.txt"));
    }

    #[test]
    fn discover_loads_multi_file_skills() {
        let tmp = tempdir().unwrap();
        let root = tmp.path().join("skills");
        write_file(
            &root.join("om/debug.md"),
            "---\nname: debug\ndescription: debug issues\n---\nDebug workflow.\n",
        );
        write_file(
            &root.join("om/feature.md"),
            "---\nname: feature\ndescription: build features\n---\nFeature workflow.\n",
        );

        let skills = discover_skills(&[root]);
        assert_eq!(skills.len(), 2);
        
        let debug_skill = skills.iter().find(|s| s.name == "debug").unwrap();
        assert_eq!(debug_skill.description, "debug issues");
        assert!(debug_skill.body.contains("Debug workflow"));
        
        let feature_skill = skills.iter().find(|s| s.name == "feature").unwrap();
        assert_eq!(feature_skill.description, "build features");
        assert!(feature_skill.body.contains("Feature workflow"));
    }

    #[test]
    fn multi_file_skill_name_from_filename() {
        let tmp = tempdir().unwrap();
        let root = tmp.path().join("skills");
        // No 'name' in frontmatter - should use filename
        write_file(
            &root.join("utils/helper.md"),
            "---\ndescription: a helper\n---\nHelper content.\n",
        );

        let skills = discover_skills(&[root]);
        assert_eq!(skills.len(), 1);
        assert_eq!(skills[0].name, "helper");
    }

    #[test]
    fn duplicate_names_are_dropped() {
        let tmp = tempdir().unwrap();
        let a = tmp.path().join("a");
        let b = tmp.path().join("b");
        write_file(
            &a.join("x/SKILL.md"),
            "---\nname: x\ndescription: first\n---\nA\n",
        );
        write_file(
            &b.join("x/SKILL.md"),
            "---\nname: x\ndescription: second\n---\nB\n",
        );
        let skills = discover_skills(&[a, b]);
        assert_eq!(skills.len(), 1);
        assert_eq!(skills[0].description, "first");
    }

    #[test]
    fn missing_root_is_skipped() {
        let skills = discover_skills(&[PathBuf::from("/definitely/not/here")]);
        assert!(skills.is_empty());
    }

    #[test]
    fn catalogue_renders_or_skips() {
        assert!(format_catalogue(&[]).is_none());
        let s = Skill {
            name: "demo".into(),
            description: "does stuff".into(),
            dir: PathBuf::from("/tmp"),
            body: String::new(),
            source_file: PathBuf::from("/tmp/demo.md"),
        };
        let cat = format_catalogue(&[s]).unwrap();
        assert!(cat.contains("Use the `skill` tool"));
        assert!(cat.contains("demo: does stuff"));
        assert!(!cat.contains("Available skills"));
    }
}