1use adk_core::Tool;
2use serde::{Deserialize, Serialize};
3use std::path::PathBuf;
4use std::sync::Arc;
5
6#[derive(Debug, Clone, Default, Serialize, Deserialize)]
8#[serde(default)]
9pub struct SkillFrontmatter {
10 pub name: String,
12 pub description: String,
14 pub version: Option<String>,
16 pub license: Option<String>,
18 pub compatibility: Option<String>,
20 #[serde(default)]
22 pub tags: Vec<String>,
23 #[serde(default, rename = "allowed-tools", deserialize_with = "names_or_list")]
28 pub allowed_tools: Vec<String>,
29 #[serde(default)]
31 pub references: Vec<String>,
32 pub trigger: Option<bool>,
34 pub hint: Option<String>,
36 #[serde(default)]
38 pub metadata: std::collections::HashMap<String, serde_json::Value>,
39 #[serde(default)]
41 pub triggers: Vec<String>,
42}
43
44fn names_or_list<'de, D>(deserializer: D) -> Result<Vec<String>, D::Error>
46where
47 D: serde::Deserializer<'de>,
48{
49 #[derive(Deserialize)]
50 #[serde(untagged)]
51 enum Names {
52 List(Vec<String>),
53 Text(String),
54 }
55 Ok(match Option::<Names>::deserialize(deserializer)? {
56 Some(Names::List(names)) => names,
57 Some(Names::Text(text)) => text
58 .split(|c: char| c.is_whitespace() || c == ',')
59 .filter(|name| !name.is_empty())
60 .map(str::to_string)
61 .collect(),
62 None => Vec::new(),
63 })
64}
65
66#[derive(Debug, Clone)]
68pub struct ParsedSkill {
69 pub name: String,
71 pub description: String,
73 pub version: Option<String>,
75 pub license: Option<String>,
77 pub compatibility: Option<String>,
79 pub tags: Vec<String>,
81 pub allowed_tools: Vec<String>,
83 pub references: Vec<String>,
85 pub trigger: bool,
87 pub hint: Option<String>,
89 pub metadata: std::collections::HashMap<String, serde_json::Value>,
91 pub triggers: Vec<String>,
93 pub body: String,
95}
96
97#[derive(Debug, Clone, Serialize)]
99pub struct SkillDocument {
100 pub id: String,
102 pub name: String,
104 pub description: String,
106 pub version: Option<String>,
108 pub license: Option<String>,
110 pub compatibility: Option<String>,
112 pub tags: Vec<String>,
114 pub allowed_tools: Vec<String>,
116 pub references: Vec<String>,
118 pub trigger: bool,
120 pub hint: Option<String>,
122 pub metadata: std::collections::HashMap<String, serde_json::Value>,
124 pub body: String,
126 pub path: PathBuf,
128 pub hash: String,
130 pub last_modified: Option<i64>,
132 pub triggers: Vec<String>,
134}
135
136impl SkillDocument {
137 pub fn engineer_instruction(&self, max_chars: usize, active_tools: &[Arc<dyn Tool>]) -> String {
140 let mut body = self.body.clone();
141 if body.chars().count() > max_chars {
142 body = body.chars().take(max_chars).collect();
143 body.push_str("\n[... truncated]");
144 }
145
146 let mut parts = Vec::new();
147 parts.push(format!("[skill:{}]", self.name));
148 parts.push(format!("# {}\n{}", self.name, self.description));
149
150 if !active_tools.is_empty() {
152 let names: Vec<_> = active_tools.iter().map(|t: &Arc<dyn Tool>| t.name()).collect();
153 parts.push(format!("You have access to the following tools: {}.", names.join(", ")));
154 }
155
156 parts.push(format!("## Instructions\n{}", body));
157 parts.push("[/skill]".to_string());
158
159 parts.join("\n\n")
160 }
161
162 pub fn engineer_prompt_block(&self, max_chars: usize) -> String {
164 let mut body = self.body.clone();
165 if body.chars().count() > max_chars {
166 body = body.chars().take(max_chars).collect();
167 }
168 format!("[skill:{}]\n{}\n[/skill]", self.name, body)
169 }
170}
171
172#[derive(Debug, Clone, Serialize)]
174pub struct SkillSummary {
175 pub id: String,
177 pub name: String,
179 pub description: String,
181 pub version: Option<String>,
183 pub license: Option<String>,
185 pub compatibility: Option<String>,
187 pub tags: Vec<String>,
189 pub allowed_tools: Vec<String>,
191 pub references: Vec<String>,
193 pub trigger: bool,
195 pub hint: Option<String>,
197 pub metadata: std::collections::HashMap<String, serde_json::Value>,
199 pub path: PathBuf,
201 pub hash: String,
203 pub last_modified: Option<i64>,
205 pub triggers: Vec<String>,
207}
208
209impl From<&SkillDocument> for SkillSummary {
210 fn from(value: &SkillDocument) -> Self {
211 Self {
212 id: value.id.clone(),
213 name: value.name.clone(),
214 description: value.description.clone(),
215 version: value.version.clone(),
216 license: value.license.clone(),
217 compatibility: value.compatibility.clone(),
218 tags: value.tags.clone(),
219 allowed_tools: value.allowed_tools.clone(),
220 references: value.references.clone(),
221 trigger: value.trigger,
222 hint: value.hint.clone(),
223 metadata: value.metadata.clone(),
224 path: value.path.clone(),
225 hash: value.hash.clone(),
226 last_modified: value.last_modified,
227 triggers: value.triggers.clone(),
228 }
229 }
230}
231
232#[derive(Debug, Clone, Default)]
234pub struct SkillIndex {
235 skills: Vec<SkillDocument>,
236}
237
238impl SkillIndex {
239 pub fn new(skills: Vec<SkillDocument>) -> Self {
240 Self { skills }
241 }
242
243 pub fn is_empty(&self) -> bool {
244 self.skills.is_empty()
245 }
246
247 pub fn len(&self) -> usize {
248 self.skills.len()
249 }
250
251 pub fn skills(&self) -> &[SkillDocument] {
253 &self.skills
254 }
255
256 pub fn summaries(&self) -> Vec<SkillSummary> {
258 self.skills.iter().map(SkillSummary::from).collect()
259 }
260
261 pub fn find_by_name(&self, name: &str) -> Option<&SkillDocument> {
263 self.skills.iter().find(|s| s.name == name)
264 }
265
266 pub fn find_by_id(&self, id: &str) -> Option<&SkillDocument> {
268 self.skills.iter().find(|s| s.id == id)
269 }
270}
271
272#[derive(Debug, Clone)]
274pub struct SelectionPolicy {
275 pub top_k: usize,
277 pub min_score: f32,
279 pub include_tags: Vec<String>,
281 pub exclude_tags: Vec<String>,
283}
284
285impl Default for SelectionPolicy {
286 fn default() -> Self {
287 Self { top_k: 1, min_score: 1.0, include_tags: Vec::new(), exclude_tags: Vec::new() }
288 }
289}
290
291#[derive(Debug, Clone, Serialize)]
293pub struct SkillMatch {
294 pub score: f32,
305 pub skill: SkillSummary,
307}