//! Embed the complete, sorted skill library without runtime filesystem access. use std::{ env, error::Error, fs, path::{Path, PathBuf}, }; fn files(directory: &Path) -> Result, Box> { let mut result = Vec::new(); for entry in fs::read_dir(directory)? { let entry = entry?; let kind = entry.file_type()?; if kind.is_symlink() { return Err(format!("Skill resources may not be symlinks: {}", entry.path().display()).into()); } if kind.is_dir() { result.extend(files(&entry.path())?); } else if kind.is_file() { result.push(entry.path()); } } result.sort(); Ok(result) } pub fn generate() -> Result<(), Box> { let root = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?).join("../../skills").canonicalize()?; println!("cargo:rerun-if-changed={}", root.display()); let mut folders = fs::read_dir(&root)?.collect::, _>>()?; folders.sort_by_key(|entry| entry.file_name()); let mut output = String::from("const SKILLS: &[Skill] = &[\n"); let mut catalog = Vec::new(); for entry in folders { let folder = entry.path(); if !entry.file_type()?.is_dir() || !folder.join("SKILL.md").is_file() { continue; } let name = entry.file_name().to_string_lossy().into_owned(); let text = fs::read_to_string(folder.join("SKILL.md"))?; let description = text .lines() .find_map(|line| line.strip_prefix("description: ")) .ok_or_else(|| format!("Missing skill description: {name}"))? .trim_matches('"'); output.push_str(&format!("Skill {{ name: {name:?}, description: {description:?}, files: &[\n")); let paths = files(&folder)?; let mut names = Vec::new(); for path in paths { let relative = path.strip_prefix(&folder)?.to_string_lossy().replace('\\', "/"); names.push(relative.clone()); // Include only text resources. A new non-text asset must get an explicit transport. fs::read_to_string(&path)?; output.push_str(&format!("({relative:?}, include_str!({path:?})),\n")); } output.push_str("] },\n"); catalog.push(format!("- {name}: {description} Files: {}", names.join(", "))); } output.push_str("];\n"); let out = PathBuf::from(env::var("OUT_DIR")?); fs::write(out.join("skills.rs"), output)?; fs::write(out.join("skill_catalog.rs"), format!("const AGENT_SKILL_CATALOG: &str = {:?};\n", catalog.join("\n")))?; Ok(()) }