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
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
use super::util::{capitalize_first, to_title_case};
use super::ClarityAssessment;

/// Filler words to remove from purpose strings during name derivation.
const FILLER_WORDS: &[&str] = &[
    "a", "an", "the", "to", "for", "from", "with", "and", "or", "that", "which", "this", "my",
    "of", "in", "on", "is", "are", "be",
];

/// Derive a kebab-case skill name from a natural language description.
///
/// Steps: lowercase → remove filler words → gerund-form first word →
/// join with hyphens → sanitize → truncate to 64 characters.
#[must_use]
pub fn derive_name(purpose: &str) -> String {
    let lower = purpose.to_lowercase();

    // Split into words, filter fillers.
    let words: Vec<&str> = lower
        .split_whitespace()
        .filter(|w| {
            let stripped = w.trim_matches(|c: char| !c.is_alphanumeric());
            !FILLER_WORDS.contains(&stripped)
        })
        .collect();

    if words.is_empty() {
        return "my-skill".to_string();
    }

    // Apply gerund form to the first word.
    let mut result_words: Vec<String> = Vec::with_capacity(words.len());
    let first = words[0].trim_matches(|c: char| !c.is_alphanumeric());
    result_words.push(to_gerund(first));

    for w in &words[1..] {
        let cleaned = w.trim_matches(|c: char| !c.is_alphanumeric());
        if !cleaned.is_empty() {
            result_words.push(cleaned.to_string());
        }
    }

    // Join with hyphens, sanitize.
    let joined = result_words.join("-");

    // Remove characters not in [a-z0-9-].
    let sanitized: String = joined
        .chars()
        .filter(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || *c == '-')
        .collect();

    // Collapse consecutive hyphens and trim.
    let collapsed = collapse_hyphens(&sanitized);
    let trimmed = collapsed.trim_matches('-');

    if trimmed.is_empty() {
        return "my-skill".to_string();
    }

    // Truncate to 64 characters at a hyphen boundary if possible.
    truncate_at_boundary(trimmed, 64)
}

/// Convert a word to gerund form (add "ing").
fn to_gerund(word: &str) -> String {
    if word.is_empty() {
        return word.to_string();
    }

    // Already ends in "ing".
    if word.ends_with("ing") {
        return word.to_string();
    }

    // Ends in "ie" → drop "ie", add "ying" (e.g., "die" → "dying").
    if let Some(stem) = word.strip_suffix("ie") {
        return format!("{stem}ying");
    }

    // Ends in "e" (not "ee") → drop "e", add "ing" (e.g., "analyze" → "analyzing").
    if word.ends_with('e') && !word.ends_with("ee") && word.len() > 1 {
        let stem = &word[..word.len() - 1];
        return format!("{stem}ing");
    }

    // CVC pattern for short words: double final consonant.
    // Only apply to common short words (3-4 chars) to avoid over-doubling.
    if word.len() >= 3 && word.len() <= 4 && is_cvc(word) {
        if let Some(last) = word.chars().last() {
            return format!("{word}{last}ing");
        }
    }

    // Default: just add "ing".
    format!("{word}ing")
}

/// Check if a word ends in consonant-vowel-consonant pattern.
fn is_cvc(word: &str) -> bool {
    let chars: Vec<char> = word.chars().collect();
    let len = chars.len();
    if len < 3 {
        return false;
    }
    let last = chars[len - 1];
    let second_last = chars[len - 2];
    let third_last = chars[len - 3];

    // Don't double w, x, or y.
    if last == 'w' || last == 'x' || last == 'y' {
        return false;
    }

    is_consonant(last) && is_vowel(second_last) && is_consonant(third_last)
}

fn is_vowel(c: char) -> bool {
    matches!(c, 'a' | 'e' | 'i' | 'o' | 'u')
}

fn is_consonant(c: char) -> bool {
    c.is_ascii_lowercase() && !is_vowel(c)
}

/// Collapse consecutive hyphens into a single hyphen.
fn collapse_hyphens(s: &str) -> String {
    let mut result = String::with_capacity(s.len());
    let mut prev_hyphen = false;
    for c in s.chars() {
        if c == '-' {
            if !prev_hyphen {
                result.push('-');
            }
            prev_hyphen = true;
        } else {
            result.push(c);
            prev_hyphen = false;
        }
    }
    result
}

/// Truncate a string to `max_len` characters, preferring a hyphen boundary.
fn truncate_at_boundary(s: &str, max_len: usize) -> String {
    if s.len() <= max_len {
        return s.to_string();
    }

    let truncated = &s[..max_len];
    // Find the last hyphen to break cleanly.
    if let Some(pos) = truncated.rfind('-') {
        if pos > 0 {
            return truncated[..pos].to_string();
        }
    }
    truncated.to_string()
}

/// Generate a template-based description from a purpose string.
#[must_use]
pub fn generate_description(purpose: &str, _name: &str) -> String {
    let capitalized = capitalize_first(purpose.trim());

    // Add period if not already present.
    let sentence = if capitalized.ends_with('.') || capitalized.ends_with('!') {
        capitalized
    } else {
        format!("{capitalized}.")
    };

    // Derive trigger context from purpose.
    let trigger = derive_trigger(purpose);
    let description = format!("{sentence} Use when {trigger}.");

    // Truncate to 1024 characters if needed (char-safe for multibyte UTF-8).
    if description.chars().count() > 1024 {
        description.chars().take(1024).collect()
    } else {
        description
    }
}

/// Derive a trigger context from the purpose string.
fn derive_trigger(purpose: &str) -> String {
    let words: Vec<&str> = purpose.split_whitespace().collect();
    if words.len() >= 3 {
        // Use the last significant word as the object.
        if let Some(last_word) = words.last() {
            let last = last_word.trim_matches(|c: char| !c.is_alphanumeric());
            if !last.is_empty() {
                return format!("working with {last}");
            }
        }
    }
    "this capability is needed".to_string()
}

/// Generate a template-based markdown body.
#[must_use]
pub fn generate_body(purpose: &str, name: &str, _description: &str) -> String {
    let title = to_title_case(name);
    let version = env!("CARGO_PKG_VERSION");

    format!(
        "# {title}\n\
         \n\
         ## Quick start\n\
         \n\
         {purpose}\n\
         \n\
         ## Usage\n\
         \n\
         Use this skill to {purpose}.\n\
         \n\
         ## Notes\n\
         \n\
         - Generated by aigent {version}\n\
         - Edit this file to customize the skill\n"
    )
}

/// Evaluate if a purpose description is clear enough for autonomous generation.
///
/// Deterministic heuristics based on word count and structure.
#[must_use]
pub fn assess_clarity(purpose: &str) -> ClarityAssessment {
    let trimmed = purpose.trim();
    let word_count = trimmed.split_whitespace().count();

    // Too short.
    if word_count < 3 {
        return ClarityAssessment {
            clear: false,
            questions: vec![
                "Can you provide more detail about what the skill should do?".to_string(),
            ],
        };
    }

    // Contains question mark — user is asking, not describing.
    if trimmed.contains('?') {
        return ClarityAssessment {
            clear: false,
            questions: vec![
                "Please provide a statement describing the skill, not a question.".to_string(),
            ],
        };
    }

    // Long enough to be clear.
    if word_count > 10 {
        return ClarityAssessment {
            clear: true,
            questions: vec![],
        };
    }

    // Medium length — check for verb-like words (heuristic).
    let has_verb = trimmed.split_whitespace().any(|w| {
        let lower = w.to_lowercase();
        lower.ends_with("ing")
            || lower.ends_with("ate")
            || lower.ends_with("ize")
            || lower.ends_with("ify")
            || lower.ends_with("ect")
            || matches!(
                lower.as_str(),
                "run"
                    | "get"
                    | "set"
                    | "add"
                    | "put"
                    | "use"
                    | "make"
                    | "read"
                    | "write"
                    | "send"
                    | "find"
                    | "check"
                    | "build"
                    | "create"
                    | "delete"
                    | "update"
                    | "parse"
                    | "format"
                    | "deploy"
                    | "process"
                    | "analyze"
                    | "generate"
                    | "validate"
                    | "convert"
                    | "extract"
                    | "transform"
                    | "handle"
                    | "manage"
            )
    });

    if has_verb {
        ClarityAssessment {
            clear: true,
            questions: vec![],
        }
    } else {
        ClarityAssessment {
            clear: false,
            questions: vec![
                "Can you describe the specific task or workflow this skill should handle?"
                    .to_string(),
            ],
        }
    }
}

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

    // ── derive_name tests (1-10) ──────────────────────────────────────

    #[test]
    fn derive_name_process_pdf_files() {
        let name = derive_name("Process PDF files");
        assert!(
            name.starts_with("processing"),
            "expected gerund form, got: {name}"
        );
    }

    #[test]
    fn derive_name_analyze_gerund() {
        let name = derive_name("Analyze spreadsheet data");
        assert!(
            name.starts_with("analyzing"),
            "expected 'analyzing', got: {name}"
        );
    }

    #[test]
    fn derive_name_run_cvc_doubling() {
        let name = derive_name("Run database migrations");
        assert!(
            name.starts_with("running"),
            "expected 'running' (CVC doubling), got: {name}"
        );
    }

    #[test]
    fn derive_name_already_gerund() {
        let name = derive_name("processing files");
        assert!(
            name.starts_with("processing"),
            "expected to keep 'processing', got: {name}"
        );
    }

    #[test]
    fn derive_name_single_word() {
        let name = derive_name("deploy");
        assert_eq!(name, "deploying");
    }

    #[test]
    fn derive_name_filler_words_removed() {
        let name = derive_name("a tool for the processing of data");
        // "a", "for", "the", "of" are fillers; "tool", "processing", "data" remain.
        // "tool" → "tooling" (gerund), then "processing", "data".
        assert!(
            !name.contains("-a-") && !name.contains("-for-") && !name.contains("-the-"),
            "filler words should be removed, got: {name}"
        );
        assert_eq!(name, "tooling-processing-data");
    }

    #[test]
    fn derive_name_special_characters_stripped() {
        let name = derive_name("Process PDFs!");
        assert!(
            name.chars()
                .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-'),
            "should only contain [a-z0-9-], got: {name}"
        );
    }

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

    #[test]
    fn derive_name_long_input_truncated() {
        let long_purpose = "process ".repeat(20) + "the final long word";
        let name = derive_name(&long_purpose);
        assert!(
            name.len() <= 64,
            "should be ≤ 64 chars, got {} chars: {name}",
            name.len()
        );
    }

    #[test]
    fn derive_name_passes_validation() {
        let name = derive_name("Process PDF files for automated archival");
        // No uppercase.
        assert_eq!(name, name.to_lowercase(), "should be lowercase");
        // No consecutive hyphens.
        assert!(!name.contains("--"), "should not have consecutive hyphens");
        // No leading/trailing hyphens.
        assert!(!name.starts_with('-'), "should not start with hyphen");
        assert!(!name.ends_with('-'), "should not end with hyphen");
    }

    // ── generate_description tests (11-14) ────────────────────────────

    #[test]
    fn generate_description_non_empty() {
        let desc = generate_description("Process PDF files", "processing-pdf-files");
        assert!(!desc.is_empty(), "description should not be empty");
    }

    #[test]
    fn generate_description_contains_purpose_words() {
        let desc = generate_description("Process PDF files", "processing-pdf-files");
        let lower = desc.to_lowercase();
        assert!(
            lower.contains("process") || lower.contains("pdf") || lower.contains("files"),
            "should contain purpose-related words, got: {desc}"
        );
    }

    #[test]
    fn generate_description_within_limit() {
        let long_purpose = "word ".repeat(300);
        let desc = generate_description(&long_purpose, "long-name");
        assert!(
            desc.len() <= 1024,
            "should be ≤ 1024 chars, got {} chars",
            desc.len()
        );
    }

    #[test]
    fn generate_description_third_person() {
        let desc = generate_description("Process PDF files", "processing-pdf-files");
        assert!(
            !desc.starts_with("I ") && !desc.starts_with("You "),
            "should be third person, got: {desc}"
        );
    }

    // ── generate_body tests (15-18) ───────────────────────────────────

    #[test]
    fn generate_body_non_empty() {
        let body = generate_body("Process PDFs", "processing-pdfs", "Processes PDFs.");
        assert!(!body.is_empty(), "body should not be empty");
    }

    #[test]
    fn generate_body_contains_heading_with_name() {
        let body = generate_body("Process PDFs", "processing-pdfs", "Processes PDFs.");
        assert!(
            body.contains("# Processing Pdfs"),
            "should contain heading with skill name, got:\n{body}"
        );
    }

    #[test]
    fn generate_body_contains_quick_start() {
        let body = generate_body("Process PDFs", "processing-pdfs", "Processes PDFs.");
        assert!(
            body.contains("## Quick start"),
            "should contain Quick start section"
        );
    }

    #[test]
    fn generate_body_contains_version() {
        let body = generate_body("Process PDFs", "processing-pdfs", "Processes PDFs.");
        let version = env!("CARGO_PKG_VERSION");
        assert!(
            body.contains(version),
            "should contain aigent version {version}"
        );
    }

    // ── assess_clarity tests (19-23) ──────────────────────────────────

    #[test]
    fn assess_clarity_short_input_not_clear() {
        let result = assess_clarity("do stuff");
        assert!(!result.clear, "short input should not be clear");
    }

    #[test]
    fn assess_clarity_question_not_clear() {
        let result = assess_clarity("What should this skill do?");
        assert!(!result.clear, "question should not be clear");
    }

    #[test]
    fn assess_clarity_detailed_purpose_clear() {
        let result = assess_clarity(
            "Process PDF files and extract text content for automated archival in a database",
        );
        assert!(
            result.clear,
            "detailed purpose (> 10 words) should be clear"
        );
    }

    #[test]
    fn assess_clarity_clear_has_empty_questions() {
        let result = assess_clarity(
            "Process PDF files and extract text content for automated archival in a database",
        );
        assert!(
            result.questions.is_empty(),
            "clear assessment should have empty questions"
        );
    }

    #[test]
    fn assess_clarity_unclear_has_questions() {
        let result = assess_clarity("do stuff");
        assert!(
            !result.questions.is_empty(),
            "unclear assessment should have non-empty questions"
        );
    }
}