skill-runtime 0.3.0

Core execution engine for Skill - WASM sandbox, Docker runtime, and native skill execution
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
// Git Skill Loader - Clone and build skills from Git repositories
//
// Supports:
// - Cloning via git2 (pure Rust, no CLI dependency)
// - Auto-detection of skill type (Rust, JS/TS, Python, pre-built WASM)
// - Caching cloned repositories for fast subsequent access
// - Version pinning via tags, branches, or commits

use anyhow::{Context, Result};
use git2::{FetchOptions, RemoteCallbacks, Repository};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use std::process::Command;
use tracing::{debug, info, warn};

use crate::git_source::GitSource;

/// Skill type detected from repository structure
#[derive(Debug, Clone, PartialEq)]
pub enum SkillType {
    /// Pre-built WASM component (no build needed)
    PrebuiltWasm(PathBuf),
    /// JavaScript skill (needs jco componentize)
    JavaScript(PathBuf),
    /// TypeScript skill (needs tsc + jco)
    TypeScript(PathBuf),
    /// Rust skill (needs cargo build --target wasm32-wasip1)
    Rust,
    /// Python skill (needs componentize-py)
    Python(PathBuf),
    /// Unknown - cannot determine how to build
    Unknown,
}

impl std::fmt::Display for SkillType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SkillType::PrebuiltWasm(_) => write!(f, "Pre-built WASM"),
            SkillType::JavaScript(_) => write!(f, "JavaScript"),
            SkillType::TypeScript(_) => write!(f, "TypeScript"),
            SkillType::Rust => write!(f, "Rust"),
            SkillType::Python(_) => write!(f, "Python"),
            SkillType::Unknown => write!(f, "Unknown"),
        }
    }
}

/// Metadata about a cloned skill repository
#[derive(Debug, Clone)]
pub struct ClonedSkill {
    /// Original Git source
    pub source: GitSource,
    /// Local path to cloned repository
    pub local_path: PathBuf,
    /// Detected skill type
    pub skill_type: SkillType,
    /// Skill name (from manifest or repo name)
    pub skill_name: String,
    /// Skill version (if found in manifest)
    pub version: Option<String>,
}

/// Cache metadata for tracking cloned repositories
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct SourceCache {
    pub entries: std::collections::HashMap<String, SourceCacheEntry>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SourceCacheEntry {
    pub url: String,
    pub git_ref: String,
    pub commit: String,
    pub cloned_at: chrono::DateTime<chrono::Utc>,
    pub skill_name: String,
}

/// Loads skills from Git repositories
pub struct GitSkillLoader {
    /// Directory for cloned repositories
    sources_dir: PathBuf,
    /// Cache file path
    cache_path: PathBuf,
}

impl GitSkillLoader {
    /// Create a new GitSkillLoader
    pub fn new() -> Result<Self> {
        let home = dirs::home_dir().context("Failed to get home directory")?;
        let base_dir = home.join(".skill-engine");
        let sources_dir = base_dir.join("sources");
        let cache_path = base_dir.join("sources.json");

        std::fs::create_dir_all(&sources_dir)
            .with_context(|| format!("Failed to create sources directory: {}", sources_dir.display()))?;

        Ok(Self {
            sources_dir,
            cache_path,
        })
    }

    /// Get the directory for a cloned repo
    pub fn get_repo_dir(&self, source: &GitSource) -> PathBuf {
        self.sources_dir.join(&source.owner).join(&source.repo)
    }

    /// Check if a repo is already cloned
    pub fn is_cloned(&self, source: &GitSource) -> bool {
        self.get_repo_dir(source).join(".git").exists()
    }

    /// Clone or update a Git repository and prepare for loading
    pub async fn clone_skill(&self, source: &GitSource, force: bool) -> Result<ClonedSkill> {
        let repo_dir = self.get_repo_dir(source);

        if force && repo_dir.exists() {
            info!(path = %repo_dir.display(), "Force flag set, removing existing clone");
            std::fs::remove_dir_all(&repo_dir)?;
        }

        // Clone or update
        if repo_dir.join(".git").exists() {
            info!(
                repo = %source.repo,
                path = %repo_dir.display(),
                "Repository already cloned, checking ref..."
            );
            self.checkout_ref(&repo_dir, source)?;
        } else {
            info!(
                url = %source.url,
                path = %repo_dir.display(),
                "Cloning repository..."
            );
            self.clone_repo(source, &repo_dir)?;
        }

        // Detect skill type
        let skill_type = self.detect_skill_type(&repo_dir)?;
        info!(skill_type = %skill_type, "Detected skill type");

        // Extract metadata
        let (skill_name, version) = self.extract_metadata(&repo_dir, source)?;

        // Update cache
        self.update_cache(source, &repo_dir, &skill_name)?;

        Ok(ClonedSkill {
            source: source.clone(),
            local_path: repo_dir,
            skill_type,
            skill_name,
            version,
        })
    }

    /// Build the skill if necessary and return the WASM component path
    pub async fn build_skill(&self, cloned: &ClonedSkill) -> Result<PathBuf> {
        match &cloned.skill_type {
            SkillType::PrebuiltWasm(path) => {
                info!(path = %path.display(), "Using pre-built WASM");
                Ok(path.clone())
            }
            SkillType::JavaScript(entry) => {
                self.build_js_skill(&cloned.local_path, entry, false).await
            }
            SkillType::TypeScript(entry) => {
                self.build_js_skill(&cloned.local_path, entry, true).await
            }
            SkillType::Rust => self.build_rust_skill(&cloned.local_path).await,
            SkillType::Python(entry) => {
                self.build_python_skill(&cloned.local_path, entry).await
            }
            SkillType::Unknown => {
                anyhow::bail!(
                    "Cannot determine how to build this skill.\n\
                     Expected one of:\n\
                     - skill.wasm (pre-built)\n\
                     - Cargo.toml (Rust)\n\
                     - package.json + *.ts/*.js (JavaScript/TypeScript)\n\
                     - pyproject.toml + *.py (Python)"
                )
            }
        }
    }

    /// Remove a cloned repository
    pub fn remove_source(&self, source: &GitSource) -> Result<()> {
        let repo_dir = self.get_repo_dir(source);
        if repo_dir.exists() {
            std::fs::remove_dir_all(&repo_dir)?;
            info!(path = %repo_dir.display(), "Removed cloned repository");
        }
        Ok(())
    }

    // --- Private methods ---

    fn clone_repo(&self, source: &GitSource, dest: &Path) -> Result<()> {
        std::fs::create_dir_all(dest.parent().unwrap())?;

        // Set up callbacks for progress
        let mut callbacks = RemoteCallbacks::new();
        callbacks.transfer_progress(|progress| {
            debug!(
                "Receiving objects: {}/{}",
                progress.received_objects(),
                progress.total_objects()
            );
            true
        });

        let mut fetch_options = FetchOptions::new();
        fetch_options.remote_callbacks(callbacks);

        // Clone the repository
        let mut builder = git2::build::RepoBuilder::new();
        builder.fetch_options(fetch_options);

        let repo = builder
            .clone(&source.url, dest)
            .with_context(|| format!("Failed to clone repository: {}", source.url))?;

        // Checkout specific ref if not default branch
        if let Some(refspec) = source.git_ref.as_refspec() {
            self.checkout_ref_in_repo(&repo, refspec)?;
        }

        Ok(())
    }

    fn checkout_ref(&self, repo_dir: &Path, source: &GitSource) -> Result<()> {
        let repo = Repository::open(repo_dir)
            .with_context(|| format!("Failed to open repository: {}", repo_dir.display()))?;

        // Fetch updates if not a pinned ref
        if !source.git_ref.is_pinned() {
            debug!("Fetching updates from origin...");
            let mut remote = repo.find_remote("origin")?;
            remote.fetch(&["refs/heads/*:refs/heads/*"], None, None)?;
        }

        if let Some(refspec) = source.git_ref.as_refspec() {
            self.checkout_ref_in_repo(&repo, refspec)?;
        }

        Ok(())
    }

    fn checkout_ref_in_repo(&self, repo: &Repository, refspec: &str) -> Result<()> {
        info!(refspec = %refspec, "Checking out ref");

        // Try to find the reference
        let reference = repo
            .resolve_reference_from_short_name(refspec)
            .or_else(|_| repo.find_reference(&format!("refs/tags/{}", refspec)))
            .or_else(|_| repo.find_reference(&format!("refs/heads/{}", refspec)))
            .with_context(|| format!("Could not find ref: {}", refspec))?;

        let commit = reference.peel_to_commit()?;

        // Checkout the commit
        repo.checkout_tree(commit.as_object(), None)?;
        repo.set_head_detached(commit.id())?;

        Ok(())
    }

    fn detect_skill_type(&self, repo_dir: &Path) -> Result<SkillType> {
        // Priority order for detection

        // 1. Pre-built WASM
        let wasm_candidates = [
            repo_dir.join("skill.wasm"),
            repo_dir.join("dist/skill.wasm"),
            repo_dir.join("build/skill.wasm"),
        ];
        for candidate in &wasm_candidates {
            if candidate.exists() {
                return Ok(SkillType::PrebuiltWasm(candidate.clone()));
            }
        }

        // 2. Check for Cargo.toml (Rust)
        let cargo_toml = repo_dir.join("Cargo.toml");
        if cargo_toml.exists() {
            let content = std::fs::read_to_string(&cargo_toml)?;
            // Check if it's likely a WASM project
            if content.contains("cdylib") || content.contains("wasm32") || content.contains("wasm") {
                return Ok(SkillType::Rust);
            }
        }

        // 3. Check for package.json (JS/TS)
        let package_json = repo_dir.join("package.json");
        if package_json.exists() {
            // Look for TypeScript first
            let ts_candidates = [
                repo_dir.join("skill.ts"),
                repo_dir.join("src/skill.ts"),
                repo_dir.join("src/index.ts"),
                repo_dir.join("index.ts"),
            ];
            for candidate in ts_candidates {
                if candidate.exists() {
                    return Ok(SkillType::TypeScript(candidate));
                }
            }

            // Then JavaScript
            let js_candidates = [
                repo_dir.join("skill.js"),
                repo_dir.join("src/skill.js"),
                repo_dir.join("src/index.js"),
                repo_dir.join("index.js"),
            ];
            for candidate in js_candidates {
                if candidate.exists() {
                    return Ok(SkillType::JavaScript(candidate));
                }
            }
        }

        // 4. Check for Python (pyproject.toml or requirements.txt + main.py)
        let has_python_config =
            repo_dir.join("pyproject.toml").exists() || repo_dir.join("requirements.txt").exists();
        if has_python_config {
            let py_candidates = [
                repo_dir.join("skill.py"),
                repo_dir.join("src/main.py"),
                repo_dir.join("main.py"),
                repo_dir.join("src/skill.py"),
            ];
            for candidate in py_candidates {
                if candidate.exists() {
                    return Ok(SkillType::Python(candidate));
                }
            }
        }

        Ok(SkillType::Unknown)
    }

    fn extract_metadata(
        &self,
        repo_dir: &Path,
        source: &GitSource,
    ) -> Result<(String, Option<String>)> {
        // Try to read skill.yaml
        let skill_yaml_path = repo_dir.join("skill.yaml");
        if skill_yaml_path.exists() {
            let contents = std::fs::read_to_string(&skill_yaml_path)?;
            if let Ok(yaml) = serde_yaml::from_str::<serde_yaml::Value>(&contents) {
                let name = yaml["name"]
                    .as_str()
                    .unwrap_or(&source.repo)
                    .to_string();
                let version = yaml["version"].as_str().map(|s| s.to_string());
                return Ok((name, version));
            }
        }

        // Try SKILL.md frontmatter
        let skill_md_path = repo_dir.join("SKILL.md");
        if skill_md_path.exists() {
            let contents = std::fs::read_to_string(&skill_md_path)?;
            if let Some(frontmatter) = extract_yaml_frontmatter(&contents) {
                if let Ok(yaml) = serde_yaml::from_str::<serde_yaml::Value>(frontmatter) {
                    let name = yaml["name"]
                        .as_str()
                        .unwrap_or(&source.repo)
                        .to_string();
                    let version = yaml["version"].as_str().map(|s| s.to_string());
                    return Ok((name, version));
                }
            }
        }

        // Try package.json
        let package_json_path = repo_dir.join("package.json");
        if package_json_path.exists() {
            let contents = std::fs::read_to_string(&package_json_path)?;
            if let Ok(json) = serde_json::from_str::<serde_json::Value>(&contents) {
                let name = json["name"]
                    .as_str()
                    .unwrap_or(&source.repo)
                    .to_string();
                let version = json["version"].as_str().map(|s| s.to_string());
                return Ok((name, version));
            }
        }

        // Try Cargo.toml
        let cargo_toml_path = repo_dir.join("Cargo.toml");
        if cargo_toml_path.exists() {
            let contents = std::fs::read_to_string(&cargo_toml_path)?;
            if let Ok(toml) = toml::from_str::<toml::Value>(&contents) {
                if let Some(package) = toml.get("package") {
                    let name = package["name"]
                        .as_str()
                        .unwrap_or(&source.repo)
                        .to_string();
                    let version = package["version"].as_str().map(|s| s.to_string());
                    return Ok((name, version));
                }
            }
        }

        // Fall back to repo name
        Ok((source.repo.clone(), None))
    }

    fn update_cache(
        &self,
        source: &GitSource,
        repo_dir: &Path,
        skill_name: &str,
    ) -> Result<()> {
        let mut cache = self.load_cache();

        // Get current commit
        let commit = if let Ok(repo) = Repository::open(repo_dir) {
            repo.head()
                .ok()
                .and_then(|h| h.peel_to_commit().ok())
                .map(|c| c.id().to_string())
                .unwrap_or_default()
        } else {
            String::new()
        };

        cache.entries.insert(
            source.cache_key(),
            SourceCacheEntry {
                url: source.url.clone(),
                git_ref: source.git_ref.to_string(),
                commit,
                cloned_at: chrono::Utc::now(),
                skill_name: skill_name.to_string(),
            },
        );

        self.save_cache(&cache)?;
        Ok(())
    }

    fn load_cache(&self) -> SourceCache {
        std::fs::read_to_string(&self.cache_path)
            .ok()
            .and_then(|s| serde_json::from_str(&s).ok())
            .unwrap_or_default()
    }

    fn save_cache(&self, cache: &SourceCache) -> Result<()> {
        let content = serde_json::to_string_pretty(cache)?;
        std::fs::write(&self.cache_path, content)?;
        Ok(())
    }

    async fn build_js_skill(
        &self,
        repo_dir: &Path,
        entry: &Path,
        _is_typescript: bool,
    ) -> Result<PathBuf> {
        info!(entry = %entry.display(), "Building JavaScript/TypeScript skill");

        // Install dependencies if node_modules doesn't exist
        if !repo_dir.join("node_modules").exists() {
            info!("Installing npm dependencies...");
            let status = Command::new("npm")
                .args(["install"])
                .current_dir(repo_dir)
                .status()
                .context("Failed to run npm install. Is npm installed?")?;

            if !status.success() {
                anyhow::bail!("npm install failed");
            }
        }

        // Check if there's a build script
        let package_json: serde_json::Value = serde_json::from_str(
            &std::fs::read_to_string(repo_dir.join("package.json"))?,
        )?;

        // Run build if available
        if package_json
            .get("scripts")
            .and_then(|s| s.get("build"))
            .is_some()
        {
            info!("Running npm build...");
            let status = Command::new("npm")
                .args(["run", "build"])
                .current_dir(repo_dir)
                .status()?;

            if !status.success() {
                warn!("npm build failed, attempting direct componentize");
            }
        }

        // Check for componentize script
        if package_json
            .get("scripts")
            .and_then(|s| s.get("componentize"))
            .is_some()
        {
            info!("Running componentize script...");
            let status = Command::new("npm")
                .args(["run", "componentize"])
                .current_dir(repo_dir)
                .status()?;

            if status.success() {
                // Look for output WASM
                let wasm_candidates = [
                    repo_dir.join("skill.wasm"),
                    repo_dir.join("dist/skill.wasm"),
                ];
                for candidate in wasm_candidates {
                    if candidate.exists() {
                        return Ok(candidate);
                    }
                }
            }
        }

        // Direct componentize with jco
        let output_wasm = repo_dir.join("skill.wasm");

        info!("Running jco componentize...");
        let status = Command::new("npx")
            .args([
                "@bytecodealliance/jco",
                "componentize",
                entry.to_str().unwrap(),
                "-o",
                output_wasm.to_str().unwrap(),
            ])
            .current_dir(repo_dir)
            .status()
            .context("Failed to run jco componentize. Is jco installed?")?;

        if !status.success() {
            anyhow::bail!("jco componentize failed");
        }

        Ok(output_wasm)
    }

    async fn build_rust_skill(&self, repo_dir: &Path) -> Result<PathBuf> {
        info!("Building Rust skill...");

        let status = Command::new("cargo")
            .args(["build", "--release", "--target", "wasm32-wasip1"])
            .current_dir(repo_dir)
            .status()
            .context("Failed to run cargo build. Is cargo and wasm32-wasip1 target installed?")?;

        if !status.success() {
            anyhow::bail!(
                "cargo build failed. Make sure you have the wasm32-wasip1 target:\n\
                 rustup target add wasm32-wasip1"
            );
        }

        // Find the output WASM
        let target_dir = repo_dir.join("target/wasm32-wasip1/release");
        for entry in std::fs::read_dir(&target_dir)? {
            let entry = entry?;
            let path = entry.path();
            if path.extension().map_or(false, |e| e == "wasm") {
                info!(wasm = %path.display(), "Found compiled WASM");
                return Ok(path);
            }
        }

        anyhow::bail!(
            "No .wasm file found in target/wasm32-wasip1/release/\n\
             Make sure Cargo.toml has crate-type = [\"cdylib\"]"
        )
    }

    async fn build_python_skill(&self, repo_dir: &Path, entry: &Path) -> Result<PathBuf> {
        info!(entry = %entry.display(), "Building Python skill");

        let output_wasm = repo_dir.join("skill.wasm");

        // Find WIT file
        let wit_candidates = [
            repo_dir.join("skill.wit"),
            repo_dir.join("wit/skill.wit"),
            repo_dir.join("skill-interface.wit"),
        ];

        let wit_path = wit_candidates
            .iter()
            .find(|p| p.exists())
            .context("No WIT interface file found. Expected skill.wit or wit/skill.wit")?;

        let status = Command::new("componentize-py")
            .args([
                "-d",
                wit_path.to_str().unwrap(),
                "-w",
                "skill",
                "componentize",
                entry.to_str().unwrap(),
                "-o",
                output_wasm.to_str().unwrap(),
            ])
            .current_dir(repo_dir)
            .status()
            .context("Failed to run componentize-py. Install it with: pip install componentize-py")?;

        if !status.success() {
            anyhow::bail!("componentize-py failed");
        }

        Ok(output_wasm)
    }
}

impl Default for GitSkillLoader {
    fn default() -> Self {
        Self::new().expect("Failed to create GitSkillLoader")
    }
}

fn extract_yaml_frontmatter(content: &str) -> Option<&str> {
    if !content.starts_with("---") {
        return None;
    }
    let rest = &content[3..];
    let end = rest.find("---")?;
    Some(rest[..end].trim())
}

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

    #[test]
    fn test_skill_type_display() {
        assert_eq!(format!("{}", SkillType::Rust), "Rust");
        assert_eq!(
            format!("{}", SkillType::PrebuiltWasm(PathBuf::from("test.wasm"))),
            "Pre-built WASM"
        );
    }

    #[test]
    fn test_extract_yaml_frontmatter() {
        let content = "---\nname: test\nversion: 1.0\n---\n\n# Test";
        let fm = extract_yaml_frontmatter(content);
        assert!(fm.is_some());
        assert!(fm.unwrap().contains("name: test"));
    }

    #[test]
    fn test_no_frontmatter() {
        let content = "# Just markdown\n\nNo frontmatter here.";
        assert!(extract_yaml_frontmatter(content).is_none());
    }
}