aigent 0.7.1

A library, CLI, and Claude plugin for managing agent skill definitions
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
use std::collections::HashMap;

use clap::ValueEnum;

use super::util::to_title_case;

/// Skill template variant for `init` and `build`.
///
/// Each variant generates a different directory structure and SKILL.md
/// content pattern, following the
/// [Anthropic skill specification](https://platform.claude.com/docs/en/agents-and-tools/agent-skills/best-practices).
#[derive(Debug, Clone, Copy, Default, ValueEnum, PartialEq, Eq)]
pub enum SkillTemplate {
    /// Basic SKILL.md only (default)
    #[default]
    Minimal,
    /// SKILL.md + REFERENCE.md + EXAMPLES.md
    ReferenceGuide,
    /// SKILL.md + reference/domain.md
    DomainSpecific,
    /// SKILL.md with checklist/workflow pattern
    Workflow,
    /// SKILL.md + scripts/run.sh
    CodeSkill,
    /// SKILL.md with Claude Code extension fields
    ClaudeCode,
}

/// Generate template files for a given template variant and skill name.
///
/// Returns a map of relative path → content. The skill name should be
/// kebab-case. Every template includes at least a `SKILL.md` entry.
#[must_use]
pub fn template_files(template: SkillTemplate, dir_name: &str) -> HashMap<String, String> {
    let name = to_kebab_case(dir_name);
    let name = if name.is_empty() {
        "my-skill".to_string()
    } else {
        name
    };
    let title = to_title_case(&name);

    let mut files = HashMap::new();

    match template {
        SkillTemplate::Minimal => {
            files.insert("SKILL.md".to_string(), minimal_skill_md(&name, &title));
        }
        SkillTemplate::ReferenceGuide => {
            files.insert(
                "SKILL.md".to_string(),
                reference_guide_skill_md(&name, &title),
            );
            files.insert("REFERENCE.md".to_string(), reference_md(&title));
            files.insert("EXAMPLES.md".to_string(), examples_md(&title));
        }
        SkillTemplate::DomainSpecific => {
            files.insert(
                "SKILL.md".to_string(),
                domain_specific_skill_md(&name, &title),
            );
            files.insert(
                "reference/domain.md".to_string(),
                domain_reference_md(&title),
            );
        }
        SkillTemplate::Workflow => {
            files.insert("SKILL.md".to_string(), workflow_skill_md(&name, &title));
        }
        SkillTemplate::CodeSkill => {
            files.insert("SKILL.md".to_string(), code_skill_md(&name, &title));
            files.insert("scripts/run.sh".to_string(), run_script(&name));
        }
        SkillTemplate::ClaudeCode => {
            files.insert("SKILL.md".to_string(), claude_code_skill_md(&name, &title));
        }
    }

    files
}

/// Generate a SKILL.md template for `init` (backward-compatible wrapper).
///
/// The `dir_name` is used as the `name` field (kebab-cased) and the heading
/// (title-cased). Uses the `Minimal` template.
#[must_use]
pub fn skill_template(dir_name: &str) -> String {
    let files = template_files(SkillTemplate::Minimal, dir_name);
    files.get("SKILL.md").cloned().unwrap_or_default()
}

// ── Template content generators ────────────────────────────────────────

fn minimal_skill_md(name: &str, title: &str) -> String {
    format!(
        "---\n\
         name: {name}\n\
         description: Describe what this skill does and when to use it\n\
         ---\n\
         \n\
         # {title}\n\
         \n\
         ## Quick start\n\
         \n\
         [Add quick start instructions here]\n\
         \n\
         ## Usage\n\
         \n\
         [Add detailed usage instructions here]\n"
    )
}

fn reference_guide_skill_md(name: &str, title: &str) -> String {
    format!(
        "---\n\
         name: {name}\n\
         description: Describe what this skill does and when to use it\n\
         ---\n\
         \n\
         # {title}\n\
         \n\
         ## Quick start\n\
         \n\
         [Add quick start instructions here]\n\
         \n\
         ## Usage\n\
         \n\
         [Add detailed usage instructions here]\n\
         \n\
         ## Reference\n\
         \n\
         See [REFERENCE.md](REFERENCE.md) for detailed reference documentation.\n\
         \n\
         ## Examples\n\
         \n\
         See [EXAMPLES.md](EXAMPLES.md) for usage examples.\n"
    )
}

fn reference_md(title: &str) -> String {
    format!(
        "# {title} Reference\n\
         \n\
         ## API\n\
         \n\
         [Document the API or interface here]\n\
         \n\
         ## Configuration\n\
         \n\
         [Document configuration options here]\n\
         \n\
         ## Options\n\
         \n\
         [Document available options here]\n"
    )
}

fn examples_md(title: &str) -> String {
    format!(
        "# {title} Examples\n\
         \n\
         ## Basic usage\n\
         \n\
         ```\n\
         [Add a basic usage example here]\n\
         ```\n\
         \n\
         ## Advanced usage\n\
         \n\
         ```\n\
         [Add an advanced usage example here]\n\
         ```\n"
    )
}

fn domain_specific_skill_md(name: &str, title: &str) -> String {
    format!(
        "---\n\
         name: {name}\n\
         description: Describe what this skill does and when to use it\n\
         ---\n\
         \n\
         # {title}\n\
         \n\
         ## Quick start\n\
         \n\
         [Add quick start instructions here]\n\
         \n\
         ## Domain knowledge\n\
         \n\
         See [reference/domain.md](reference/domain.md) for domain-specific reference.\n\
         \n\
         ## Usage\n\
         \n\
         [Add detailed usage instructions here]\n"
    )
}

fn domain_reference_md(title: &str) -> String {
    format!(
        "# {title} Domain Reference\n\
         \n\
         ## Terminology\n\
         \n\
         [Define domain-specific terms here]\n\
         \n\
         ## Rules\n\
         \n\
         [Document domain rules and constraints here]\n\
         \n\
         ## Patterns\n\
         \n\
         [Document common patterns here]\n"
    )
}

fn workflow_skill_md(name: &str, title: &str) -> String {
    format!(
        "---\n\
         name: {name}\n\
         description: Describe what this skill does and when to use it\n\
         ---\n\
         \n\
         # {title}\n\
         \n\
         ## Workflow\n\
         \n\
         Follow these steps in order:\n\
         \n\
         1. [ ] **Step 1**: [Describe first step]\n\
         2. [ ] **Step 2**: [Describe second step]\n\
         3. [ ] **Step 3**: [Describe third step]\n\
         \n\
         ## Checklist\n\
         \n\
         Before completing, verify:\n\
         \n\
         - [ ] [First verification item]\n\
         - [ ] [Second verification item]\n\
         - [ ] [Third verification item]\n"
    )
}

fn code_skill_md(name: &str, title: &str) -> String {
    format!(
        "---\n\
         name: {name}\n\
         description: Describe what this skill does and when to use it\n\
         allowed-tools: Bash(./scripts/run.sh *)\n\
         ---\n\
         \n\
         # {title}\n\
         \n\
         ## Quick start\n\
         \n\
         Run the script:\n\
         \n\
         ```bash\n\
         ./scripts/run.sh [arguments]\n\
         ```\n\
         \n\
         ## Usage\n\
         \n\
         [Add detailed usage instructions here]\n"
    )
}

fn run_script(name: &str) -> String {
    format!(
        "#!/usr/bin/env bash\n\
         set -euo pipefail\n\
         \n\
         # {name} — main script\n\
         # Usage: ./scripts/run.sh [arguments]\n\
         \n\
         main() {{\n\
         \x20\x20\x20\x20echo \"{name}: not yet implemented\"\n\
         \x20\x20\x20\x20exit 1\n\
         }}\n\
         \n\
         main \"$@\"\n"
    )
}

fn claude_code_skill_md(name: &str, title: &str) -> String {
    format!(
        "---\n\
         name: {name}\n\
         description: Describe what this skill does and when to use it\n\
         allowed-tools: Bash(*), Read, Write, Glob\n\
         user-invocable: true\n\
         argument-hint: \"[arguments]\"\n\
         ---\n\
         \n\
         # {title}\n\
         \n\
         ## Quick start\n\
         \n\
         [Add quick start instructions here]\n\
         \n\
         ## Usage\n\
         \n\
         [Add detailed usage instructions here]\n"
    )
}

// ── Utility ────────────────────────────────────────────────────────────

/// Convert a string to kebab-case: lowercase, replace non-alphanumeric with
/// hyphens, collapse consecutive hyphens, trim leading/trailing hyphens.
fn to_kebab_case(s: &str) -> String {
    let lower = s.to_lowercase();
    let mut result = String::with_capacity(lower.len());
    let mut prev_hyphen = false;

    for c in lower.chars() {
        if c.is_ascii_alphanumeric() {
            result.push(c);
            prev_hyphen = false;
        } else if !prev_hyphen && !result.is_empty() {
            result.push('-');
            prev_hyphen = true;
        }
    }

    result.trim_matches('-').to_string()
}

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

    #[test]
    fn minimal_template_matches_legacy_output() {
        let legacy = skill_template("my-skill");
        let files = template_files(SkillTemplate::Minimal, "my-skill");
        assert_eq!(files.get("SKILL.md").unwrap(), &legacy);
    }

    #[test]
    fn minimal_template_produces_only_skill_md() {
        let files = template_files(SkillTemplate::Minimal, "test-skill");
        assert_eq!(files.len(), 1);
        assert!(files.contains_key("SKILL.md"));
    }

    #[test]
    fn reference_guide_template_produces_three_files() {
        let files = template_files(SkillTemplate::ReferenceGuide, "test-skill");
        assert_eq!(files.len(), 3);
        assert!(files.contains_key("SKILL.md"));
        assert!(files.contains_key("REFERENCE.md"));
        assert!(files.contains_key("EXAMPLES.md"));
    }

    #[test]
    fn domain_specific_template_produces_two_files() {
        let files = template_files(SkillTemplate::DomainSpecific, "test-skill");
        assert_eq!(files.len(), 2);
        assert!(files.contains_key("SKILL.md"));
        assert!(files.contains_key("reference/domain.md"));
    }

    #[test]
    fn workflow_template_produces_only_skill_md() {
        let files = template_files(SkillTemplate::Workflow, "test-skill");
        assert_eq!(files.len(), 1);
        assert!(files.contains_key("SKILL.md"));
        let content = files.get("SKILL.md").unwrap();
        assert!(
            content.contains("[ ]"),
            "workflow should have checklist items"
        );
    }

    #[test]
    fn code_skill_template_produces_two_files() {
        let files = template_files(SkillTemplate::CodeSkill, "test-skill");
        assert_eq!(files.len(), 2);
        assert!(files.contains_key("SKILL.md"));
        assert!(files.contains_key("scripts/run.sh"));
    }

    #[test]
    fn code_skill_script_has_shebang() {
        let files = template_files(SkillTemplate::CodeSkill, "test-skill");
        let script = files.get("scripts/run.sh").unwrap();
        assert!(script.starts_with("#!/usr/bin/env bash"));
    }

    #[test]
    fn code_skill_script_has_strict_mode() {
        let files = template_files(SkillTemplate::CodeSkill, "test-skill");
        let script = files.get("scripts/run.sh").unwrap();
        assert!(script.contains("set -euo pipefail"));
    }

    #[test]
    fn claude_code_template_has_extension_fields() {
        let files = template_files(SkillTemplate::ClaudeCode, "test-skill");
        let content = files.get("SKILL.md").unwrap();
        assert!(content.contains("user-invocable: true"));
        assert!(content.contains("argument-hint:"));
    }

    #[test]
    fn claude_code_template_produces_only_skill_md() {
        let files = template_files(SkillTemplate::ClaudeCode, "test-skill");
        assert_eq!(files.len(), 1);
        assert!(files.contains_key("SKILL.md"));
    }

    #[test]
    fn template_names_derive_from_dir_name() {
        let files = template_files(SkillTemplate::Minimal, "My Cool Skill");
        let content = files.get("SKILL.md").unwrap();
        assert!(content.contains("name: my-cool-skill"));
    }

    #[test]
    fn empty_dir_name_defaults_to_my_skill() {
        let files = template_files(SkillTemplate::Minimal, "");
        let content = files.get("SKILL.md").unwrap();
        assert!(content.contains("name: my-skill"));
    }

    #[test]
    fn all_templates_have_valid_frontmatter() {
        let templates = [
            SkillTemplate::Minimal,
            SkillTemplate::ReferenceGuide,
            SkillTemplate::DomainSpecific,
            SkillTemplate::Workflow,
            SkillTemplate::CodeSkill,
            SkillTemplate::ClaudeCode,
        ];
        for t in templates {
            let files = template_files(t, "test-skill");
            let content = files.get("SKILL.md").unwrap();
            assert!(
                content.starts_with("---\n"),
                "{t:?} template should start with frontmatter"
            );
            assert!(
                content.contains("name: test-skill"),
                "{t:?} template should contain name"
            );
            assert!(
                content.contains("description:"),
                "{t:?} template should contain description"
            );
        }
    }

    #[test]
    fn to_kebab_case_simple() {
        assert_eq!(to_kebab_case("Hello World"), "hello-world");
    }

    #[test]
    fn to_kebab_case_already_kebab() {
        assert_eq!(to_kebab_case("my-skill"), "my-skill");
    }

    #[test]
    fn to_kebab_case_special_chars() {
        assert_eq!(to_kebab_case("foo@bar!baz"), "foo-bar-baz");
    }

    #[test]
    fn to_kebab_case_empty() {
        assert_eq!(to_kebab_case(""), "");
    }
}