| 1 | use std::collections::HashSet; |
| 2 | use std::path::Path; |
| 3 | |
| 4 | use super::{Skill, SkillRegistry, SkillSource}; |
| 5 | |
| 6 | #[derive(Debug, PartialEq, Eq)] |
| 7 | pub struct Mention<'a> { |
| 8 | pub name: &'a str, |
| 9 | pub path: Option<&'a str>, |
| 10 | } |
| 11 | |
| 12 | fn name_char(c: char) -> bool { |
| 13 | c.is_alphanumeric() || matches!(c, '-' | '_' | ':' | '.') |
| 14 | } |
| 15 | |
| 16 | pub fn parse(text: &str) -> Vec<Mention<'_>> { |
| 17 | let mut mentions = Vec::new(); |
| 18 | let mut rest = text; |
| 19 | while mentions.len() < 32 |
| 20 | && let Some(offset) = rest.find(['[', '$']) |
| 21 | { |
| 22 | let marker = &rest[offset..]; |
| 23 | if let Some(label) = marker.strip_prefix('[') { |
| 24 | let name = label.strip_prefix('$').unwrap_or(label); |
| 25 | let end = name.find(|c| !name_char(c)).unwrap_or(name.len()); |
| 26 | if end > 0 |
| 27 | && let Some(target) = name[end..].strip_prefix("](") |
| 28 | { |
| 29 | if let Some(close) = target.find([')', '\n', '[', ']']) |
| 30 | && target.as_bytes()[close] == b')' |
| 31 | { |
| 32 | let path = &target[..close]; |
| 33 | let path = path |
| 34 | .strip_prefix('<') |
| 35 | .and_then(|p| p.strip_suffix('>')) |
| 36 | .unwrap_or(path); |
| 37 | if Path::new(path) |
| 38 | .file_name() |
| 39 | .is_some_and(|file| file == "SKILL.md") |
| 40 | { |
| 41 | mentions.push(Mention { |
| 42 | name: &name[..end], |
| 43 | path: Some(path), |
| 44 | }); |
| 45 | } |
| 46 | rest = &target[close + 1..]; |
| 47 | continue; |
| 48 | } |
| 49 | rest = target; |
| 50 | continue; |
| 51 | } |
| 52 | rest = label; |
| 53 | continue; |
| 54 | } |
| 55 | let tail = &marker[1..]; |
| 56 | let end = tail.find(|c| !name_char(c)).unwrap_or(tail.len()); |
| 57 | let name = tail[..end].trim_end_matches('.'); |
| 58 | if !name.is_empty() |
| 59 | && name.chars().any(|c| c.is_lowercase() || !c.is_ascii()) |
| 60 | && !rest[..offset].ends_with(|c: char| c == '\\' || c.is_alphanumeric()) |
| 61 | { |
| 62 | mentions.push(Mention { name, path: None }); |
| 63 | } |
| 64 | rest = &tail[end..]; |
| 65 | } |
| 66 | mentions |
| 67 | } |
| 68 | |
| 69 | #[derive(Default)] |
| 70 | pub struct Selection<'a> { |
| 71 | pub skills: Vec<&'a Skill>, |
| 72 | pub ambiguous: Vec<String>, |
| 73 | } |
| 74 | |
| 75 | pub fn select<'a>( |
| 76 | registry: &'a SkillRegistry, |
| 77 | workspace: &Path, |
| 78 | mentions: &[Mention<'_>], |
| 79 | ) -> Selection<'a> { |
| 80 | let mut selected = Selection::default(); |
| 81 | let mut seen = HashSet::new(); |
| 82 | let mut ambiguous = HashSet::new(); |
| 83 | for mention in mentions { |
| 84 | let name_matches = |skill: &&Skill| { |
| 85 | skill.name.eq_ignore_ascii_case(mention.name) |
| 86 | || skill |
| 87 | .aliases |
| 88 | .iter() |
| 89 | .any(|alias| alias.eq_ignore_ascii_case(mention.name)) |
| 90 | }; |
| 91 | let mut matches: Vec<_> = registry |
| 92 | .skills |
| 93 | .iter() |
| 94 | .chain(®istry.shadowed) |
| 95 | .filter(|skill| skill.invocation.user_invocable()) |
| 96 | .filter(name_matches) |
| 97 | .collect(); |
| 98 | if let Some(path) = mention.path { |
| 99 | let path = workspace.join(path); |
| 100 | matches |
| 101 | .retain(|skill| matches!(skill.source, SkillSource::Native) && skill.path == path); |
| 102 | } else if matches |
| 103 | .iter() |
| 104 | .any(|skill| skill.name.eq_ignore_ascii_case(mention.name)) |
| 105 | { |
| 106 | matches.retain(|skill| skill.name.eq_ignore_ascii_case(mention.name)); |
| 107 | } |
| 108 | match matches.as_slice() { |
| 109 | [skill] if seen.insert((skill.name.as_str(), skill.path.as_path())) => { |
| 110 | selected.skills.push(skill) |
| 111 | } |
| 112 | [_, _, ..] if ambiguous.insert(mention.name) => { |
| 113 | selected.ambiguous.push(mention.name.to_owned()) |
| 114 | } |
| 115 | _ => {} |
| 116 | } |
| 117 | } |
| 118 | selected.ambiguous.retain(|name| { |
| 119 | !selected.skills.iter().any(|skill| { |
| 120 | skill.name.eq_ignore_ascii_case(name) |
| 121 | || skill |
| 122 | .aliases |
| 123 | .iter() |
| 124 | .any(|alias| alias.eq_ignore_ascii_case(name)) |
| 125 | }) |
| 126 | }); |
| 127 | selected |
| 128 | } |
| 129 | |
| 130 | #[cfg(test)] |
| 131 | mod tests { |
| 132 | use super::*; |
| 133 | |
| 134 | #[test] |
| 135 | fn links_select_exact_discovered_paths_when_names_collide() { |
| 136 | let tmp = tempfile::tempdir().unwrap(); |
| 137 | let first = tmp.path().join("project"); |
| 138 | let second = tmp.path().join("personal"); |
| 139 | for root in [&first, &second] { |
| 140 | let path = root.join("proof"); |
| 141 | std::fs::create_dir_all(&path).unwrap(); |
| 142 | std::fs::write( |
| 143 | path.join("SKILL.md"), |
| 144 | "---\nname: proof\ndescription: Write a proof\n---\nExample body", |
| 145 | ) |
| 146 | .unwrap(); |
| 147 | let policy = path.join("agents/openai.yaml"); |
| 148 | std::fs::create_dir_all(policy.parent().unwrap()).unwrap(); |
| 149 | std::fs::write(policy, "policy:\n allow_implicit_invocation: false\n").unwrap(); |
| 150 | } |
| 151 | let registry = crate::discover_from_directories_with_plugins([first, second], None); |
| 152 | let prompt = "$proof then [proof](personal/proof/SKILL.md) and [proof](../proof/SKILL.md)"; |
| 153 | let selection = select(®istry, tmp.path(), &parse(prompt)); |
| 154 | assert_eq!( |
| 155 | select(®istry, tmp.path(), &parse("$proof")).ambiguous, |
| 156 | ["proof"] |
| 157 | ); |
| 158 | assert!(selection.ambiguous.is_empty()); |
| 159 | assert_eq!(selection.skills.len(), 1); |
| 160 | let skill = selection.skills[0]; |
| 161 | assert_eq!(skill.path, tmp.path().join("personal/proof/SKILL.md")); |
| 162 | assert!(skill.invocation.user_invocable()); |
| 163 | assert!(!skill.invocation.model_invocable()); |
| 164 | } |
| 165 | |
| 166 | #[test] |
| 167 | fn ignores_escaped_sigils_and_environment_variables() { |
| 168 | let result = |
| 169 | parse(r"Run $review. Leave \$review and $PATH alone. [Git](https://example.test)"); |
| 170 | assert_eq!( |
| 171 | result, |
| 172 | [Mention { |
| 173 | name: "review", |
| 174 | path: None |
| 175 | }] |
| 176 | ); |
| 177 | } |
| 178 | } |
| 179 |