agent-skills 0.2.0

Parse, validate, and work with Agent Skills as defined by the Agent Skills specification
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
//! Directory-based skill loading.

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

use crate::error::LoadError;
use crate::skill::Skill;

/// A loaded skill directory containing the skill and its files.
///
/// Provides access to the parsed SKILL.md as well as optional directories
/// (scripts/, references/, assets/).
///
/// # Examples
///
/// ```no_run
/// use agent_skills::SkillDirectory;
/// use std::path::Path;
///
/// let dir = SkillDirectory::load(Path::new("./my-skill")).unwrap();
/// println!("Loaded skill: {}", dir.skill().name());
///
/// // Check for optional directories
/// if dir.has_scripts() {
///     for script in dir.scripts().unwrap() {
///         println!("Found script: {}", script.display());
///     }
/// }
/// ```
#[derive(Debug, Clone)]
pub struct SkillDirectory {
    path: PathBuf,
    skill: Skill,
}

impl SkillDirectory {
    /// Loads a skill from a directory path.
    ///
    /// The directory must contain a SKILL.md file. The skill name in the
    /// frontmatter must match the directory name (per the specification).
    ///
    /// # Errors
    ///
    /// Returns `LoadError` if:
    /// - The directory doesn't exist
    /// - SKILL.md is missing
    /// - SKILL.md cannot be read
    /// - SKILL.md cannot be parsed
    /// - The skill name doesn't match the directory name
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use agent_skills::SkillDirectory;
    /// use std::path::Path;
    ///
    /// let dir = SkillDirectory::load(Path::new("./my-skill")).unwrap();
    /// assert_eq!(dir.skill().name().as_str(), "my-skill");
    /// ```
    pub fn load(path: impl AsRef<Path>) -> Result<Self, LoadError> {
        let path = path.as_ref();
        let path_str = path.display().to_string();

        // Check directory exists
        if !path.exists() {
            return Err(LoadError::DirectoryNotFound { path: path_str });
        }

        if !path.is_dir() {
            return Err(LoadError::DirectoryNotFound { path: path_str });
        }

        // Read SKILL.md
        let skill_file = path.join("SKILL.md");
        if !skill_file.exists() {
            return Err(LoadError::SkillFileNotFound { path: path_str });
        }

        let content = fs::read_to_string(&skill_file).map_err(|e| LoadError::IoError {
            path: skill_file.display().to_string(),
            kind: e.kind(),
            message: e.to_string(),
        })?;

        // Parse skill
        let skill = Skill::parse(&content)?;

        // Validate name matches directory name
        let dir_name = path
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or_default();

        if skill.name().as_str() != dir_name {
            return Err(LoadError::NameMismatch {
                directory_name: dir_name.to_string(),
                skill_name: skill.name().as_str().to_string(),
            });
        }

        Ok(Self {
            path: path.to_path_buf(),
            skill,
        })
    }

    /// Returns the directory path.
    #[must_use]
    pub fn path(&self) -> &Path {
        &self.path
    }

    /// Returns the parsed skill.
    #[must_use]
    pub const fn skill(&self) -> &Skill {
        &self.skill
    }

    /// Returns `true` if a scripts/ directory exists.
    #[must_use]
    pub fn has_scripts(&self) -> bool {
        self.path.join("scripts").is_dir()
    }

    /// Returns `true` if a references/ directory exists.
    #[must_use]
    pub fn has_references(&self) -> bool {
        self.path.join("references").is_dir()
    }

    /// Returns `true` if an assets/ directory exists.
    #[must_use]
    pub fn has_assets(&self) -> bool {
        self.path.join("assets").is_dir()
    }

    /// Lists files in the scripts/ directory.
    ///
    /// Returns an empty vector if the scripts/ directory doesn't exist.
    ///
    /// # Errors
    ///
    /// Returns `LoadError::IoError` if the directory cannot be read.
    pub fn scripts(&self) -> Result<Vec<PathBuf>, LoadError> {
        list_files_in_subdir(&self.path, "scripts")
    }

    /// Lists files in the references/ directory.
    ///
    /// Returns an empty vector if the references/ directory doesn't exist.
    ///
    /// # Errors
    ///
    /// Returns `LoadError::IoError` if the directory cannot be read.
    pub fn references(&self) -> Result<Vec<PathBuf>, LoadError> {
        list_files_in_subdir(&self.path, "references")
    }

    /// Lists files in the assets/ directory.
    ///
    /// Returns an empty vector if the assets/ directory doesn't exist.
    ///
    /// # Errors
    ///
    /// Returns `LoadError::IoError` if the directory cannot be read.
    pub fn assets(&self) -> Result<Vec<PathBuf>, LoadError> {
        list_files_in_subdir(&self.path, "assets")
    }

    /// Reads a reference file by name.
    ///
    /// The name is relative to the references/ directory.
    ///
    /// # Errors
    ///
    /// Returns `LoadError::FileNotFound` if the file doesn't exist.
    /// Returns `LoadError::IoError` if the file cannot be read.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use agent_skills::SkillDirectory;
    /// use std::path::Path;
    ///
    /// let dir = SkillDirectory::load(Path::new("./my-skill")).unwrap();
    /// let content = dir.read_reference("REFERENCE.md").unwrap();
    /// ```
    pub fn read_reference(&self, name: &str) -> Result<String, LoadError> {
        let file_path = self.path.join("references").join(name);
        read_file_as_string(&file_path)
    }

    /// Reads a script file by name.
    ///
    /// The name is relative to the scripts/ directory.
    ///
    /// # Errors
    ///
    /// Returns `LoadError::FileNotFound` if the file doesn't exist.
    /// Returns `LoadError::IoError` if the file cannot be read.
    pub fn read_script(&self, name: &str) -> Result<String, LoadError> {
        let file_path = self.path.join("scripts").join(name);
        read_file_as_string(&file_path)
    }

    /// Reads an asset file by name as bytes.
    ///
    /// The name is relative to the assets/ directory.
    ///
    /// # Errors
    ///
    /// Returns `LoadError::FileNotFound` if the file doesn't exist.
    /// Returns `LoadError::IoError` if the file cannot be read.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use agent_skills::SkillDirectory;
    /// use std::path::Path;
    ///
    /// let dir = SkillDirectory::load(Path::new("./my-skill")).unwrap();
    /// let bytes = dir.read_asset("template.txt").unwrap();
    /// ```
    pub fn read_asset(&self, name: &str) -> Result<Vec<u8>, LoadError> {
        let file_path = self.path.join("assets").join(name);
        read_file_as_bytes(&file_path)
    }

    /// Reads an asset file by name as a string (UTF-8).
    ///
    /// # Errors
    ///
    /// Returns `LoadError::FileNotFound` if the file doesn't exist.
    /// Returns `LoadError::IoError` if the file cannot be read or is not valid UTF-8.
    pub fn read_asset_string(&self, name: &str) -> Result<String, LoadError> {
        let file_path = self.path.join("assets").join(name);
        read_file_as_string(&file_path)
    }
}

/// Lists files in a subdirectory of a skill directory.
fn list_files_in_subdir(base_path: &Path, subdir: &str) -> Result<Vec<PathBuf>, LoadError> {
    let dir_path = base_path.join(subdir);

    if !dir_path.exists() {
        return Ok(Vec::new());
    }

    let entries = fs::read_dir(&dir_path).map_err(|e| LoadError::IoError {
        path: dir_path.display().to_string(),
        kind: e.kind(),
        message: e.to_string(),
    })?;

    let mut files = Vec::new();
    for entry in entries {
        let entry = entry.map_err(|e| LoadError::IoError {
            path: dir_path.display().to_string(),
            kind: e.kind(),
            message: e.to_string(),
        })?;
        let path = entry.path();
        if path.is_file() {
            files.push(path);
        }
    }

    // Sort for consistent ordering
    files.sort();
    Ok(files)
}

/// Reads a file as a string.
fn read_file_as_string(path: &Path) -> Result<String, LoadError> {
    let path_str = path.display().to_string();

    if !path.exists() {
        return Err(LoadError::FileNotFound { path: path_str });
    }

    fs::read_to_string(path).map_err(|e| LoadError::IoError {
        path: path_str,
        kind: e.kind(),
        message: e.to_string(),
    })
}

/// Reads a file as bytes.
fn read_file_as_bytes(path: &Path) -> Result<Vec<u8>, LoadError> {
    let path_str = path.display().to_string();

    if !path.exists() {
        return Err(LoadError::FileNotFound { path: path_str });
    }

    fs::read(path).map_err(|e| LoadError::IoError {
        path: path_str,
        kind: e.kind(),
        message: e.to_string(),
    })
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    fn create_skill_dir(temp: &TempDir, name: &str, content: &str) -> PathBuf {
        let skill_dir = temp.path().join(name);
        fs::create_dir(&skill_dir).unwrap();
        fs::write(skill_dir.join("SKILL.md"), content).unwrap();
        skill_dir
    }

    fn minimal_skill_content(name: &str) -> String {
        format!(
            r#"---
name: {name}
description: Test skill.
---
# Instructions
"#
        )
    }

    #[test]
    fn loads_valid_skill_directory() {
        let temp = TempDir::new().unwrap();
        let skill_dir = create_skill_dir(&temp, "my-skill", &minimal_skill_content("my-skill"));

        let result = SkillDirectory::load(&skill_dir);
        assert!(result.is_ok(), "Expected Ok, got: {:?}", result);
        let dir = result.unwrap();
        assert_eq!(dir.skill().name().as_str(), "my-skill");
    }

    #[test]
    fn rejects_nonexistent_directory() {
        let result = SkillDirectory::load("/nonexistent/path");
        assert!(matches!(result, Err(LoadError::DirectoryNotFound { .. })));
    }

    #[test]
    fn rejects_missing_skill_file() {
        let temp = TempDir::new().unwrap();
        let skill_dir = temp.path().join("empty-skill");
        fs::create_dir(&skill_dir).unwrap();

        let result = SkillDirectory::load(&skill_dir);
        assert!(matches!(result, Err(LoadError::SkillFileNotFound { .. })));
    }

    #[test]
    fn detects_name_mismatch() {
        let temp = TempDir::new().unwrap();
        let skill_dir = create_skill_dir(&temp, "my-skill", &minimal_skill_content("other-name"));

        let result = SkillDirectory::load(&skill_dir);
        assert!(matches!(
            result,
            Err(LoadError::NameMismatch {
                directory_name,
                skill_name,
            }) if directory_name == "my-skill" && skill_name == "other-name"
        ));
    }

    #[test]
    fn has_scripts_returns_false_without_scripts_dir() {
        let temp = TempDir::new().unwrap();
        let skill_dir = create_skill_dir(&temp, "my-skill", &minimal_skill_content("my-skill"));

        let dir = SkillDirectory::load(&skill_dir).unwrap();
        assert!(!dir.has_scripts());
    }

    #[test]
    fn has_scripts_returns_true_with_scripts_dir() {
        let temp = TempDir::new().unwrap();
        let skill_dir = create_skill_dir(&temp, "my-skill", &minimal_skill_content("my-skill"));
        fs::create_dir(skill_dir.join("scripts")).unwrap();

        let dir = SkillDirectory::load(&skill_dir).unwrap();
        assert!(dir.has_scripts());
    }

    #[test]
    fn lists_scripts() {
        let temp = TempDir::new().unwrap();
        let skill_dir = create_skill_dir(&temp, "my-skill", &minimal_skill_content("my-skill"));
        let scripts_dir = skill_dir.join("scripts");
        fs::create_dir(&scripts_dir).unwrap();
        fs::write(scripts_dir.join("run.sh"), "#!/bin/bash").unwrap();
        fs::write(scripts_dir.join("build.py"), "# Python").unwrap();

        let dir = SkillDirectory::load(&skill_dir).unwrap();
        let scripts = dir.scripts().unwrap();
        assert_eq!(scripts.len(), 2);
    }

    #[test]
    fn scripts_returns_empty_without_scripts_dir() {
        let temp = TempDir::new().unwrap();
        let skill_dir = create_skill_dir(&temp, "my-skill", &minimal_skill_content("my-skill"));

        let dir = SkillDirectory::load(&skill_dir).unwrap();
        let scripts = dir.scripts().unwrap();
        assert!(scripts.is_empty());
    }

    #[test]
    fn has_references_works() {
        let temp = TempDir::new().unwrap();
        let skill_dir = create_skill_dir(&temp, "my-skill", &minimal_skill_content("my-skill"));

        let dir = SkillDirectory::load(&skill_dir).unwrap();
        assert!(!dir.has_references());

        fs::create_dir(skill_dir.join("references")).unwrap();
        let dir = SkillDirectory::load(&skill_dir).unwrap();
        assert!(dir.has_references());
    }

    #[test]
    fn has_assets_works() {
        let temp = TempDir::new().unwrap();
        let skill_dir = create_skill_dir(&temp, "my-skill", &minimal_skill_content("my-skill"));

        let dir = SkillDirectory::load(&skill_dir).unwrap();
        assert!(!dir.has_assets());

        fs::create_dir(skill_dir.join("assets")).unwrap();
        let dir = SkillDirectory::load(&skill_dir).unwrap();
        assert!(dir.has_assets());
    }

    #[test]
    fn reads_reference_file() {
        let temp = TempDir::new().unwrap();
        let skill_dir = create_skill_dir(&temp, "my-skill", &minimal_skill_content("my-skill"));
        let refs_dir = skill_dir.join("references");
        fs::create_dir(&refs_dir).unwrap();
        fs::write(refs_dir.join("REFERENCE.md"), "# Reference\n\nContent").unwrap();

        let dir = SkillDirectory::load(&skill_dir).unwrap();
        let content = dir.read_reference("REFERENCE.md").unwrap();
        assert!(content.contains("# Reference"));
    }

    #[test]
    fn read_reference_returns_error_for_missing_file() {
        let temp = TempDir::new().unwrap();
        let skill_dir = create_skill_dir(&temp, "my-skill", &minimal_skill_content("my-skill"));

        let dir = SkillDirectory::load(&skill_dir).unwrap();
        let result = dir.read_reference("nonexistent.md");
        assert!(matches!(result, Err(LoadError::FileNotFound { .. })));
    }

    #[test]
    fn reads_asset_as_bytes() {
        let temp = TempDir::new().unwrap();
        let skill_dir = create_skill_dir(&temp, "my-skill", &minimal_skill_content("my-skill"));
        let assets_dir = skill_dir.join("assets");
        fs::create_dir(&assets_dir).unwrap();
        fs::write(assets_dir.join("data.bin"), [0x00, 0x01, 0x02]).unwrap();

        let dir = SkillDirectory::load(&skill_dir).unwrap();
        let bytes = dir.read_asset("data.bin").unwrap();
        assert_eq!(bytes, vec![0x00, 0x01, 0x02]);
    }

    #[test]
    fn reads_asset_as_string() {
        let temp = TempDir::new().unwrap();
        let skill_dir = create_skill_dir(&temp, "my-skill", &minimal_skill_content("my-skill"));
        let assets_dir = skill_dir.join("assets");
        fs::create_dir(&assets_dir).unwrap();
        fs::write(assets_dir.join("template.txt"), "Hello, World!").unwrap();

        let dir = SkillDirectory::load(&skill_dir).unwrap();
        let content = dir.read_asset_string("template.txt").unwrap();
        assert_eq!(content, "Hello, World!");
    }

    #[test]
    fn reads_script_file() {
        let temp = TempDir::new().unwrap();
        let skill_dir = create_skill_dir(&temp, "my-skill", &minimal_skill_content("my-skill"));
        let scripts_dir = skill_dir.join("scripts");
        fs::create_dir(&scripts_dir).unwrap();
        fs::write(scripts_dir.join("run.sh"), "#!/bin/bash\necho hello").unwrap();

        let dir = SkillDirectory::load(&skill_dir).unwrap();
        let content = dir.read_script("run.sh").unwrap();
        assert!(content.contains("#!/bin/bash"));
    }

    #[test]
    fn path_returns_directory_path() {
        let temp = TempDir::new().unwrap();
        let skill_dir = create_skill_dir(&temp, "my-skill", &minimal_skill_content("my-skill"));

        let dir = SkillDirectory::load(&skill_dir).unwrap();
        assert_eq!(dir.path(), skill_dir);
    }
}