use crate::capability_types::{MountDirectoryBuilder, MountPoint};
use everruns_capability::CapabilityId;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
pub const SKILL_CAPABILITY_PREFIX: &str = "skill:";
pub const SKILLS_DISCOVERY_PATH: &str = "/.agents/skills";
pub const MAX_SKILLS_PER_CAPABILITY: usize = 50;
pub fn skill_capability_id(skill_id: Uuid) -> String {
format!("{SKILL_CAPABILITY_PREFIX}{skill_id}")
}
pub fn is_skill_capability(capability_id: &str) -> bool {
capability_id.starts_with(SKILL_CAPABILITY_PREFIX)
}
pub fn parse_skill_capability_id(capability_id: &str) -> Option<Uuid> {
capability_id
.strip_prefix(SKILL_CAPABILITY_PREFIX)
.and_then(|value| Uuid::parse_str(value).ok())
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SkillMeta {
pub name: String,
pub description: String,
pub source: SkillSource,
#[serde(default = "default_true")]
pub user_invocable: bool,
#[serde(default)]
pub disable_model_invocation: bool,
}
fn default_true() -> bool {
true
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum SkillSource {
Filesystem { path: String },
Registry { skill_id: String },
}
#[derive(Debug, Clone)]
pub struct SkillInstructions {
pub instructions: String,
pub files: Vec<(String, String)>,
}
#[derive(Debug, Clone)]
pub struct SkillContribution {
pub name: String,
pub description: String,
pub instructions: String,
pub files: Vec<(String, String)>,
pub user_invocable: bool,
pub disable_model_invocation: bool,
}
impl SkillContribution {
pub fn new(
name: impl Into<String>,
description: impl Into<String>,
instructions: impl Into<String>,
) -> Self {
Self {
name: name.into(),
description: description.into(),
instructions: instructions.into(),
files: Vec::new(),
user_invocable: true,
disable_model_invocation: false,
}
}
pub fn with_files(mut self, files: Vec<(String, String)>) -> Self {
self.files = files;
self
}
pub fn with_user_invocable(mut self, flag: bool) -> Self {
self.user_invocable = flag;
self
}
pub fn with_disable_model_invocation(mut self, flag: bool) -> Self {
self.disable_model_invocation = flag;
self
}
pub fn to_mount(&self, owner_id: &str) -> MountPoint {
let skill_md = reconstruct_skill_md(
&self.name,
&self.description,
&self.instructions,
self.user_invocable,
self.disable_model_invocation,
);
let mut builder = MountDirectoryBuilder::new().file("SKILL.md", &skill_md);
for (path, content) in &self.files {
builder = builder.file(path, content);
}
MountPoint::readonly(
format!("{SKILLS_DISCOVERY_PATH}/{}", self.name),
builder.build(),
owner_id,
)
}
}
pub fn reconstruct_skill_md(
name: &str,
description: &str,
instructions: &str,
user_invocable: bool,
disable_model_invocation: bool,
) -> String {
let safe_description = format!("\"{}\"", description.replace('"', "\\\""));
let invocable_line = if user_invocable {
String::new()
} else {
"user-invocable: false\n".to_string()
};
let model_invocation_line = if disable_model_invocation {
"disable-model-invocation: true\n".to_string()
} else {
String::new()
};
format!(
"---\nname: {name}\ndescription: {safe_description}\n{invocable_line}{model_invocation_line}---\n\n{instructions}"
)
}
pub fn discover_skills_from_entries(
entries: &[(String, String)],
) -> Vec<(SkillMeta, SkillInstructions)> {
let mut results = Vec::new();
for (path, content) in entries {
match crate::skill::parse_skill_md(content) {
Ok(parsed) => results.push((
SkillMeta {
name: parsed.name,
description: parsed.description,
source: SkillSource::Filesystem { path: path.clone() },
user_invocable: parsed.user_invocable,
disable_model_invocation: parsed.disable_model_invocation,
},
SkillInstructions {
instructions: parsed.instructions,
files: Vec::new(),
},
)),
Err(errors) => tracing::warn!(
path = %path,
errors = ?errors,
"Skipping invalid SKILL.md"
),
}
}
results
}
pub trait SkillCapabilityIdExt: Sized {
fn is_skill(&self) -> bool;
fn skill(skill_id: Uuid) -> Self;
fn skill_id(&self) -> Option<Uuid>;
}
impl SkillCapabilityIdExt for CapabilityId {
fn is_skill(&self) -> bool {
is_skill_capability(self.as_str())
}
fn skill(skill_id: Uuid) -> Self {
Self::new(skill_capability_id(skill_id))
}
fn skill_id(&self) -> Option<Uuid> {
parse_skill_capability_id(self.as_str())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn skill_identity_round_trips() {
let skill_id = Uuid::new_v4();
let capability_id = skill_capability_id(skill_id);
assert!(is_skill_capability(&capability_id));
assert_eq!(parse_skill_capability_id(&capability_id), Some(skill_id));
assert_eq!(CapabilityId::skill(skill_id).skill_id(), Some(skill_id));
}
#[test]
fn contribution_mount_uses_stable_discovery_path() {
let mount = SkillContribution::new("ops", "Operations", "Run safely.").to_mount("fixture");
assert_eq!(mount.path, "/.agents/skills/ops");
}
}