skillc 0.2.1

A development kit for Agent Skills - the open format for extending AI agent capabilities
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
//! Configuration and path helpers per [[RFC-0009]] and [[ADR-0001]]/[[ADR-0002]]

use crate::error::{Result, SkillcError};
use serde::Deserialize;
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use strum::EnumProperty;

/// Tokenizer preference for search indexing per [[RFC-0009:C-TOKENIZER]]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Tokenizer {
    /// Split on whitespace and punctuation (default)
    #[default]
    Ascii,
    /// Character-level tokenization for CJK content
    Cjk,
}

impl FromStr for Tokenizer {
    type Err = ();

    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "ascii" => Ok(Tokenizer::Ascii),
            "cjk" => Ok(Tokenizer::Cjk),
            _ => Err(()),
        }
    }
}

impl Tokenizer {
    /// Convert to string representation
    pub fn as_str(&self) -> &'static str {
        match self {
            Tokenizer::Ascii => "ascii",
            Tokenizer::Cjk => "cjk",
        }
    }
}

/// Deployment target for agent skills per ADR-0002.
///
/// Each variant maps to a specific directory structure in the user's home.
#[derive(
    Debug,
    Clone,
    Copy,
    PartialEq,
    Eq,
    Hash,
    clap::ValueEnum,
    strum::Display,
    strum::EnumString,
    strum::EnumIter,
    strum::EnumProperty,
)]
#[strum(serialize_all = "lowercase")]
#[clap(rename_all = "lowercase")]
pub enum Target {
    /// Claude Code → ~/.claude/skills/
    #[strum(props(dir = ".claude"))]
    Claude,
    /// Codex → ~/.codex/skills/
    #[strum(props(dir = ".codex"))]
    Codex,
    /// GitHub Copilot → ~/.github/skills/
    #[strum(props(dir = ".github"))]
    Copilot,
    /// Cursor → ~/.cursor/skills/
    #[strum(props(dir = ".cursor"))]
    Cursor,
    /// Gemini → ~/.gemini/skills/
    #[strum(props(dir = ".gemini"))]
    Gemini,
    /// Kiro → ~/.kiro/skills/
    #[strum(props(dir = ".kiro"))]
    Kiro,
    /// OpenCode → ~/.opencode/skills/
    #[strum(props(dir = ".opencode"))]
    Opencode,
    /// Trae → ~/.trae/skills/
    #[strum(props(dir = ".trae"))]
    Trae,
}

impl Target {
    /// Get the directory name for this target (e.g., ".claude", ".github").
    pub fn dir_name(&self) -> &'static str {
        self.get_str("dir").expect("all variants have dir prop")
    }

    /// Get the global skills path for this target.
    pub fn global_path(&self) -> Result<PathBuf> {
        let home = dirs::home_dir().ok_or_else(|| {
            SkillcError::Internal("could not determine home directory".to_string())
        })?;
        Ok(home.join(self.dir_name()).join("skills"))
    }

    /// Get the project-local skills path for this target.
    pub fn project_path(&self, project_root: &Path) -> PathBuf {
        project_root.join(self.dir_name()).join("skills")
    }
}

/// Target specification for CLI: either a known target or a custom path.
///
/// This allows `--target claude` (known) or `--target /custom/path` (custom).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TargetSpec {
    /// A known agent target
    Known(Target),
    /// A custom path (for testing or advanced users)
    Custom(PathBuf),
}

impl TargetSpec {
    /// Get the skills directory path for this target.
    ///
    /// For known targets, resolves to global or project-local path.
    /// For custom paths, returns the path directly.
    pub fn skills_path(&self, project_root: Option<&Path>) -> Result<PathBuf> {
        match self {
            TargetSpec::Known(t) => match project_root {
                Some(root) => Ok(t.project_path(root)),
                None => t.global_path(),
            },
            TargetSpec::Custom(p) => Ok(p.clone()),
        }
    }

    /// Check if this is a known target (eligible for project-local deployment).
    pub fn is_known(&self) -> bool {
        matches!(self, TargetSpec::Known(_))
    }
}

impl std::fmt::Display for TargetSpec {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            TargetSpec::Known(t) => write!(f, "{}", t),
            TargetSpec::Custom(p) => write!(f, "{}", p.display()),
        }
    }
}

impl FromStr for TargetSpec {
    type Err = std::convert::Infallible;

    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        // Try known target first, fall back to custom path
        match s.parse::<Target>() {
            Ok(t) => Ok(TargetSpec::Known(t)),
            Err(_) => Ok(TargetSpec::Custom(PathBuf::from(s))),
        }
    }
}

/// Search configuration section per [[RFC-0009:C-FILES]]
#[derive(Debug, Clone, Default, Deserialize)]
pub struct SearchConfig {
    /// Tokenizer for search indexing
    #[serde(default)]
    pub tokenizer: Option<Tokenizer>,
}

/// Configuration file schema per [[RFC-0009:C-FILES]]
#[derive(Debug, Clone, Default, Deserialize)]
pub struct SkillcConfig {
    /// Schema version (optional, default: 1)
    #[serde(default)]
    pub version: Option<u32>,

    /// Search settings
    #[serde(default)]
    pub search: SearchConfig,
}

/// Load and parse a config file, handling errors per [[RFC-0009:C-FILES]]
fn load_config_file(path: &Path) -> Option<SkillcConfig> {
    let content = match fs::read_to_string(path) {
        Ok(c) => c,
        Err(_) => return None,
    };

    // Parse TOML
    let config: SkillcConfig = match toml::from_str(&content) {
        Ok(c) => c,
        Err(e) => {
            eprintln!(
                "warning: Failed to parse config file {}: {}",
                path.display(),
                e
            );
            return None;
        }
    };

    // Version validation per [[RFC-0009:C-FILES]]
    if let Some(version) = config.version {
        if version == 0 {
            eprintln!(
                "error: Invalid config version {} in {} (must be positive integer)",
                version,
                path.display()
            );
            return None; // Ignore entire file
        }
        if version > 1 {
            eprintln!(
                "warning: Config version {} in {} is newer than supported (1), using recognized fields only",
                version,
                path.display()
            );
            // Continue with recognized fields
        }
    }

    Some(config)
}

/// Find project config by walking up from cwd per [[RFC-0009:C-RESOLUTION]]
fn find_project_config() -> Option<PathBuf> {
    let mut dir = env::current_dir().ok()?;

    loop {
        let config_path = dir.join(".skillc").join("config.toml");
        if config_path.exists() {
            return Some(config_path);
        }

        if !dir.pop() {
            break;
        }
    }

    None
}

/// Get the resolved tokenizer preference per [[RFC-0009:C-RESOLUTION]]
///
/// Resolution order (highest priority first):
/// 1. SKILLC_TOKENIZER environment variable
/// 2. Project config
/// 3. Global config
/// 4. Default (ascii)
pub fn get_tokenizer() -> Tokenizer {
    // 1. Environment variable
    if let Ok(val) = env::var("SKILLC_TOKENIZER") {
        if let Ok(t) = val.parse::<Tokenizer>() {
            return t;
        } else {
            eprintln!(
                "warning: Invalid SKILLC_TOKENIZER value '{}', ignoring",
                val
            );
        }
    }

    // 2. Project config
    if let Some(path) = find_project_config()
        && let Some(config) = load_config_file(&path)
        && let Some(t) = config.search.tokenizer
    {
        return t;
    }

    // 3. Global config
    if let Ok(global_config_path) = global_skillc_dir().map(|d| d.join("config.toml"))
        && let Some(config) = load_config_file(&global_config_path)
        && let Some(t) = config.search.tokenizer
    {
        return t;
    }

    // 4. Default
    Tokenizer::default()
}

/// Get the global skillc directory.
///
/// Per [[RFC-0009:C-ENV-OVERRIDE]], checks `SKILLC_HOME` first, then falls back to `~/.skillc/`.
/// When `SKILLC_HOME` is set, it acts as the home directory override, so `.skillc` is appended.
///
/// Returns error if home directory cannot be determined.
pub fn global_skillc_dir() -> Result<PathBuf> {
    // Check SKILLC_HOME override first (enables cross-platform test isolation)
    // SKILLC_HOME acts as home directory override, so we append .skillc
    if let Ok(skillc_home) = env::var("SKILLC_HOME") {
        return Ok(PathBuf::from(skillc_home).join(".skillc"));
    }

    let home = dirs::home_dir()
        .ok_or_else(|| SkillcError::Internal("could not determine home directory".to_string()))?;
    Ok(home.join(".skillc"))
}

/// Get the global source store (~/.skillc/skills/).
pub fn global_source_store() -> Result<PathBuf> {
    Ok(global_skillc_dir()?.join("skills"))
}

/// Find project root by walking up from CWD, looking for `.skillc/` directory.
///
/// Returns the project root directory (containing `.skillc/`) if found.
/// Note: The home directory (or SKILLC_HOME) is excluded - its `.skillc/` is the global store, not a project.
pub fn find_project_root() -> Option<PathBuf> {
    // Get the home directory to exclude from project root search
    // If SKILLC_HOME is set, use that; otherwise use the real home
    let excluded_home = if let Ok(skillc_home) = env::var("SKILLC_HOME") {
        Some(PathBuf::from(skillc_home))
    } else {
        dirs::home_dir()
    };

    let mut dir = env::current_dir().ok()?;

    loop {
        // Don't treat home directory as project root (its .skillc is the global store)
        if Some(&dir) != excluded_home.as_ref() && dir.join(".skillc").is_dir() {
            return Some(dir);
        }
        if !dir.pop() {
            break;
        }
    }

    None
}

/// Find a skill in the project source store by walking up from CWD.
///
/// Returns `(skill_path, project_root)` if found.
pub fn find_project_skill(skill_name: &str) -> Option<(PathBuf, PathBuf)> {
    let project_root = find_project_root()?;
    let skill_path = crate::util::project_skill_dir(&project_root, skill_name);

    if crate::util::is_valid_skill(&skill_path) {
        Some((skill_path, project_root))
    } else {
        None
    }
}

/// Resolve the source store and whether it's project-local.
///
/// Returns `(source_store_path, is_local)`.
pub fn resolve_source_store() -> Result<(PathBuf, bool)> {
    if let Some(root) = find_project_root() {
        Ok((crate::util::project_skills_dir(&root), true))
    } else {
        Ok((global_source_store()?, false))
    }
}

/// Get the target path from a string (for MCP/legacy compatibility).
///
/// Parses to `Target` enum if known, otherwise treats as direct path.
pub fn get_target_path(target: &str) -> Result<PathBuf> {
    match target.parse::<Target>() {
        Ok(t) => t.global_path(),
        Err(_) => Ok(PathBuf::from(target)),
    }
}

/// Get the global registry path (~/.skillc/registry.json)
pub fn global_registry_path() -> Result<PathBuf> {
    Ok(global_skillc_dir()?.join("registry.json"))
}

/// Get the project-local runtime SSOT store (`<project>/.skillc/runtime/`)
///
/// Walks up from CWD to find project root, then returns its runtime directory.
/// Returns None if no project root is found.
pub fn project_runtime_store() -> Option<PathBuf> {
    let project_root = find_project_root()?;
    Some(crate::util::project_runtime_dir(&project_root))
}

/// Get the global runtime SSOT store (~/.skillc/runtime/)
///
/// This is the SSOT location for globally compiled skills,
/// distinct from agent directories like ~/.claude/skills/.
pub fn global_runtime_store() -> Result<PathBuf> {
    Ok(global_skillc_dir()?.join("runtime"))
}

/// Ensure a directory exists, creating it if necessary
pub fn ensure_dir(path: &Path) -> std::io::Result<()> {
    if !path.exists() {
        std::fs::create_dir_all(path)?;
    }
    Ok(())
}

/// Get canonicalized current working directory as string, with fallback.
/// Per [[RFC-0007:C-LOGGING]], paths must be canonicalized (symlinks resolved).
pub fn get_cwd() -> String {
    env::current_dir()
        .and_then(|p| p.canonicalize())
        .map(|p| p.to_string_lossy().to_string())
        .unwrap_or_else(|_| "<unknown>".to_string())
}

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

    #[test]
    fn test_tokenizer_from_str() {
        assert_eq!("ascii".parse::<Tokenizer>().ok(), Some(Tokenizer::Ascii));
        assert_eq!("ASCII".parse::<Tokenizer>().ok(), Some(Tokenizer::Ascii));
        assert_eq!("cjk".parse::<Tokenizer>().ok(), Some(Tokenizer::Cjk));
        assert_eq!("CJK".parse::<Tokenizer>().ok(), Some(Tokenizer::Cjk));
        assert_eq!("invalid".parse::<Tokenizer>().ok(), None);
        assert_eq!("".parse::<Tokenizer>().ok(), None);
    }

    #[test]
    fn test_tokenizer_as_str() {
        assert_eq!(Tokenizer::Ascii.as_str(), "ascii");
        assert_eq!(Tokenizer::Cjk.as_str(), "cjk");
    }

    #[test]
    fn test_tokenizer_default() {
        assert_eq!(Tokenizer::default(), Tokenizer::Ascii);
    }

    #[test]
    fn test_load_config_file_not_found() {
        let result = load_config_file(Path::new("/nonexistent/config.toml"));
        assert!(result.is_none());
    }

    #[test]
    fn test_load_config_file_empty() {
        let temp = TempDir::new().expect("create temp dir");
        let config_path = temp.path().join("config.toml");
        fs::write(&config_path, "").expect("write config");

        let result = load_config_file(&config_path);
        assert!(result.is_some());
        let config = result.expect("expected result");
        assert_eq!(config.version, None);
        assert_eq!(config.search.tokenizer, None);
    }

    #[test]
    fn test_load_config_file_with_tokenizer() {
        let temp = TempDir::new().expect("create temp dir");
        let config_path = temp.path().join("config.toml");
        fs::write(
            &config_path,
            r#"
[search]
tokenizer = "cjk"
"#,
        )
        .expect("test operation");

        let result = load_config_file(&config_path);
        assert!(result.is_some());
        let config = result.expect("expected result");
        assert_eq!(config.search.tokenizer, Some(Tokenizer::Cjk));
    }

    #[test]
    fn test_load_config_file_with_version() {
        let temp = TempDir::new().expect("create temp dir");
        let config_path = temp.path().join("config.toml");
        fs::write(
            &config_path,
            r#"
version = 1

[search]
tokenizer = "ascii"
"#,
        )
        .expect("test operation");

        let result = load_config_file(&config_path);
        assert!(result.is_some());
        let config = result.expect("expected result");
        assert_eq!(config.version, Some(1));
        assert_eq!(config.search.tokenizer, Some(Tokenizer::Ascii));
    }

    #[test]
    fn test_load_config_file_invalid_version_zero() {
        let temp = TempDir::new().expect("create temp dir");
        let config_path = temp.path().join("config.toml");
        fs::write(
            &config_path,
            r#"
version = 0
"#,
        )
        .expect("test operation");

        // Version 0 is invalid - should ignore entire file
        let result = load_config_file(&config_path);
        assert!(result.is_none());
    }

    #[test]
    fn test_load_config_file_future_version() {
        let temp = TempDir::new().expect("create temp dir");
        let config_path = temp.path().join("config.toml");
        fs::write(
            &config_path,
            r#"
version = 99

[search]
tokenizer = "cjk"
"#,
        )
        .expect("test operation");

        // Future version - should warn but continue with recognized fields
        let result = load_config_file(&config_path);
        assert!(result.is_some());
        let config = result.expect("expected result");
        assert_eq!(config.search.tokenizer, Some(Tokenizer::Cjk));
    }

    #[test]
    fn test_load_config_file_unknown_keys() {
        let temp = TempDir::new().expect("create temp dir");
        let config_path = temp.path().join("config.toml");
        fs::write(
            &config_path,
            r#"
[search]
tokenizer = "ascii"
unknown_key = "ignored"

[unknown_section]
foo = "bar"
"#,
        )
        .expect("test operation");

        // Unknown keys should be ignored (serde default behavior)
        let result = load_config_file(&config_path);
        assert!(result.is_some());
        let config = result.expect("expected result");
        assert_eq!(config.search.tokenizer, Some(Tokenizer::Ascii));
    }

    #[test]
    fn test_load_config_file_invalid_toml() {
        let temp = TempDir::new().expect("create temp dir");
        let config_path = temp.path().join("config.toml");
        fs::write(&config_path, "this is not valid toml [[[").expect("write invalid config");

        // Invalid TOML should return None
        let result = load_config_file(&config_path);
        assert!(result.is_none());
    }
}