1mod cli_detector;
4pub mod empirical;
5mod github_search;
6mod llm_generator;
7
8pub use cli_detector::{
9 detect_cli_environment, get_available_cli, get_cli_binary, is_cli_available, CliEnvironment,
10};
11pub use empirical::{
12 cluster_sessions, format_as_skill_md, generate_skill_from_cluster, ClusteredBehavior,
13 ClusteringResult, EmpiricalSkillContent, FailurePattern, SuccessPattern,
14};
15pub use github_search::{
16 fetch_skill_content, search_github_skills, search_skills_advanced, GitHubSkillResult,
17};
18pub use llm_generator::{generate_skill_sync, generate_skill_with_llm};
19
20use crate::context::ProjectProfile;
21use serde::{Deserialize, Serialize};
22
23#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
25pub enum CreationMethod {
26 GitHubSearch,
28 LLMGenerate,
30 Empirical,
32 #[default]
34 Both,
35}
36
37impl std::fmt::Display for CreationMethod {
38 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39 match self {
40 Self::GitHubSearch => write!(f, "github"),
41 Self::LLMGenerate => write!(f, "llm"),
42 Self::Empirical => write!(f, "empirical"),
43 Self::Both => write!(f, "both"),
44 }
45 }
46}
47
48impl std::str::FromStr for CreationMethod {
49 type Err = String;
50
51 fn from_str(s: &str) -> Result<Self, Self::Err> {
52 match s.to_lowercase().as_str() {
53 "github" | "search" => Ok(Self::GitHubSearch),
54 "llm" | "generate" => Ok(Self::LLMGenerate),
55 "empirical" | "pattern" | "patterns" => Ok(Self::Empirical),
56 "both" | "all" => Ok(Self::Both),
57 _ => Err(format!("Unknown creation method: {}", s)),
58 }
59 }
60}
61
62#[derive(Debug, Clone, Serialize, Deserialize)]
64pub struct CreateSkillRequest {
65 pub name: String,
67 pub description: String,
69 pub target_dir: Option<String>,
71 pub method: CreationMethod,
73 pub project_context: Option<ProjectProfile>,
75 pub dry_run: bool,
77}
78
79impl CreateSkillRequest {
80 pub fn new(name: impl Into<String>, description: impl Into<String>) -> Self {
82 Self {
83 name: name.into(),
84 description: description.into(),
85 target_dir: None,
86 method: CreationMethod::Both,
87 project_context: None,
88 dry_run: false,
89 }
90 }
91
92 pub fn with_target_dir(mut self, dir: impl Into<String>) -> Self {
94 self.target_dir = Some(dir.into());
95 self
96 }
97
98 pub fn with_method(mut self, method: CreationMethod) -> Self {
100 self.method = method;
101 self
102 }
103
104 pub fn with_context(mut self, context: ProjectProfile) -> Self {
106 self.project_context = Some(context);
107 self
108 }
109
110 pub fn dry_run(mut self) -> Self {
112 self.dry_run = true;
113 self
114 }
115}
116
117#[derive(Debug, Clone, Serialize, Deserialize)]
119pub struct CreateSkillResult {
120 pub success: bool,
122 pub path: Option<String>,
124 pub method_used: CreationMethod,
126 pub content: Option<String>,
128 pub github_results: Option<Vec<GitHubSkillResult>>,
130 pub error: Option<String>,
132}
133
134impl CreateSkillResult {
135 pub fn success(method: CreationMethod, content: String, path: Option<String>) -> Self {
137 Self {
138 success: true,
139 path,
140 method_used: method,
141 content: Some(content),
142 github_results: None,
143 error: None,
144 }
145 }
146
147 pub fn failure(method: CreationMethod, error: impl Into<String>) -> Self {
149 Self {
150 success: false,
151 path: None,
152 method_used: method,
153 content: None,
154 github_results: None,
155 error: Some(error.into()),
156 }
157 }
158
159 pub fn with_github_results(mut self, results: Vec<GitHubSkillResult>) -> Self {
161 self.github_results = Some(results);
162 self
163 }
164}
165
166#[cfg(test)]
167mod tests {
168 use super::*;
169
170 mod creation_method {
171 use super::*;
172
173 #[test]
174 fn default_is_both() {
175 assert_eq!(CreationMethod::default(), CreationMethod::Both);
176 }
177
178 #[test]
179 fn display_github_search() {
180 assert_eq!(CreationMethod::GitHubSearch.to_string(), "github");
181 }
182
183 #[test]
184 fn display_llm_generate() {
185 assert_eq!(CreationMethod::LLMGenerate.to_string(), "llm");
186 }
187
188 #[test]
189 fn display_empirical() {
190 assert_eq!(CreationMethod::Empirical.to_string(), "empirical");
191 }
192
193 #[test]
194 fn display_both() {
195 assert_eq!(CreationMethod::Both.to_string(), "both");
196 }
197
198 #[test]
199 fn from_str_github_variants() {
200 assert_eq!(
201 "github".parse::<CreationMethod>().unwrap(),
202 CreationMethod::GitHubSearch
203 );
204 assert_eq!(
205 "search".parse::<CreationMethod>().unwrap(),
206 CreationMethod::GitHubSearch
207 );
208 assert_eq!(
209 "GITHUB".parse::<CreationMethod>().unwrap(),
210 CreationMethod::GitHubSearch
211 );
212 }
213
214 #[test]
215 fn from_str_llm_variants() {
216 assert_eq!(
217 "llm".parse::<CreationMethod>().unwrap(),
218 CreationMethod::LLMGenerate
219 );
220 assert_eq!(
221 "generate".parse::<CreationMethod>().unwrap(),
222 CreationMethod::LLMGenerate
223 );
224 assert_eq!(
225 "LLM".parse::<CreationMethod>().unwrap(),
226 CreationMethod::LLMGenerate
227 );
228 }
229
230 #[test]
231 fn from_str_empirical_variants() {
232 assert_eq!(
233 "empirical".parse::<CreationMethod>().unwrap(),
234 CreationMethod::Empirical
235 );
236 assert_eq!(
237 "pattern".parse::<CreationMethod>().unwrap(),
238 CreationMethod::Empirical
239 );
240 assert_eq!(
241 "patterns".parse::<CreationMethod>().unwrap(),
242 CreationMethod::Empirical
243 );
244 }
245
246 #[test]
247 fn from_str_both_variants() {
248 assert_eq!(
249 "both".parse::<CreationMethod>().unwrap(),
250 CreationMethod::Both
251 );
252 assert_eq!(
253 "all".parse::<CreationMethod>().unwrap(),
254 CreationMethod::Both
255 );
256 }
257
258 #[test]
259 fn from_str_invalid() {
260 let result = "invalid".parse::<CreationMethod>();
261 assert!(result.is_err());
262 assert_eq!(
263 result.unwrap_err(),
264 "Unknown creation method: invalid".to_string()
265 );
266 }
267
268 #[test]
269 fn serialize_deserialize_roundtrip() {
270 let methods = [
271 CreationMethod::GitHubSearch,
272 CreationMethod::LLMGenerate,
273 CreationMethod::Empirical,
274 CreationMethod::Both,
275 ];
276
277 for method in methods {
278 let json = serde_json::to_string(&method).unwrap();
279 let parsed: CreationMethod = serde_json::from_str(&json).unwrap();
280 assert_eq!(method, parsed);
281 }
282 }
283
284 #[test]
285 fn clone_and_debug() {
286 let method = CreationMethod::LLMGenerate;
287 let cloned = method.clone();
288 assert_eq!(method, cloned);
289
290 let debug = format!("{:?}", method);
291 assert!(debug.contains("LLMGenerate"));
292 }
293 }
294
295 mod create_skill_request {
296 use super::*;
297
298 #[test]
299 fn new_sets_name_and_description() {
300 let request = CreateSkillRequest::new("test-skill", "A test skill");
301
302 assert_eq!(request.name, "test-skill");
303 assert_eq!(request.description, "A test skill");
304 }
305
306 #[test]
307 fn new_has_default_values() {
308 let request = CreateSkillRequest::new("name", "desc");
309
310 assert!(request.target_dir.is_none());
311 assert_eq!(request.method, CreationMethod::Both);
312 assert!(request.project_context.is_none());
313 assert!(!request.dry_run);
314 }
315
316 #[test]
317 fn with_target_dir() {
318 let request =
319 CreateSkillRequest::new("name", "desc").with_target_dir("/path/to/skills");
320
321 assert_eq!(request.target_dir, Some("/path/to/skills".to_string()));
322 }
323
324 #[test]
325 fn with_method() {
326 let request =
327 CreateSkillRequest::new("name", "desc").with_method(CreationMethod::GitHubSearch);
328
329 assert_eq!(request.method, CreationMethod::GitHubSearch);
330 }
331
332 #[test]
333 fn dry_run_enables_flag() {
334 let request = CreateSkillRequest::new("name", "desc").dry_run();
335
336 assert!(request.dry_run);
337 }
338
339 #[test]
340 fn builder_chaining() {
341 let request = CreateSkillRequest::new("chained-skill", "Full builder test")
342 .with_target_dir("/skills")
343 .with_method(CreationMethod::LLMGenerate)
344 .dry_run();
345
346 assert_eq!(request.name, "chained-skill");
347 assert_eq!(request.description, "Full builder test");
348 assert_eq!(request.target_dir, Some("/skills".to_string()));
349 assert_eq!(request.method, CreationMethod::LLMGenerate);
350 assert!(request.dry_run);
351 }
352
353 #[test]
354 fn serialize_deserialize_roundtrip() {
355 let request = CreateSkillRequest::new("serialization-test", "Testing JSON")
356 .with_target_dir("/test")
357 .with_method(CreationMethod::Empirical)
358 .dry_run();
359
360 let json = serde_json::to_string(&request).unwrap();
361 let parsed: CreateSkillRequest = serde_json::from_str(&json).unwrap();
362
363 assert_eq!(request.name, parsed.name);
364 assert_eq!(request.description, parsed.description);
365 assert_eq!(request.target_dir, parsed.target_dir);
366 assert_eq!(request.method, parsed.method);
367 assert_eq!(request.dry_run, parsed.dry_run);
368 }
369
370 #[test]
371 fn clone_and_debug() {
372 let request = CreateSkillRequest::new("debug-test", "Testing debug");
373 let cloned = request.clone();
374
375 assert_eq!(request.name, cloned.name);
376
377 let debug = format!("{:?}", request);
378 assert!(debug.contains("debug-test"));
379 }
380 }
381
382 mod create_skill_result {
383 use super::*;
384
385 #[test]
386 fn success_factory() {
387 let result = CreateSkillResult::success(
388 CreationMethod::LLMGenerate,
389 "# Skill content".to_string(),
390 Some("/path/skill.md".to_string()),
391 );
392
393 assert!(result.success);
394 assert_eq!(result.method_used, CreationMethod::LLMGenerate);
395 assert_eq!(result.content, Some("# Skill content".to_string()));
396 assert_eq!(result.path, Some("/path/skill.md".to_string()));
397 assert!(result.github_results.is_none());
398 assert!(result.error.is_none());
399 }
400
401 #[test]
402 fn success_without_path() {
403 let result =
404 CreateSkillResult::success(CreationMethod::Both, "content".to_string(), None);
405
406 assert!(result.success);
407 assert!(result.path.is_none());
408 assert!(result.content.is_some());
409 }
410
411 #[test]
412 fn failure_factory() {
413 let result =
414 CreateSkillResult::failure(CreationMethod::GitHubSearch, "API rate limit exceeded");
415
416 assert!(!result.success);
417 assert_eq!(result.method_used, CreationMethod::GitHubSearch);
418 assert_eq!(result.error, Some("API rate limit exceeded".to_string()));
419 assert!(result.path.is_none());
420 assert!(result.content.is_none());
421 assert!(result.github_results.is_none());
422 }
423
424 #[test]
425 fn with_github_results() {
426 let github_result = GitHubSkillResult {
427 repo_url: "https://github.com/owner/repo".to_string(),
428 skill_path: "SKILL.md".to_string(),
429 file_url: "https://github.com/owner/repo/blob/main/SKILL.md".to_string(),
430 description: Some("A skill".to_string()),
431 stars: 42,
432 last_updated: "2024-01-01T00:00:00Z".to_string(),
433 raw_url: Some(
434 "https://raw.githubusercontent.com/owner/repo/main/SKILL.md".to_string(),
435 ),
436 };
437
438 let result = CreateSkillResult::success(
439 CreationMethod::GitHubSearch,
440 "content".to_string(),
441 None,
442 )
443 .with_github_results(vec![github_result.clone()]);
444
445 assert!(result.github_results.is_some());
446 let results = result.github_results.unwrap();
447 assert_eq!(results.len(), 1);
448 assert_eq!(results[0].skill_path, "SKILL.md");
449 assert_eq!(results[0].stars, 42);
450 }
451
452 #[test]
453 fn serialize_deserialize_roundtrip() {
454 let result = CreateSkillResult::success(
455 CreationMethod::Empirical,
456 "Empirical content".to_string(),
457 Some("/empirical/skill.md".to_string()),
458 );
459
460 let json = serde_json::to_string(&result).unwrap();
461 let parsed: CreateSkillResult = serde_json::from_str(&json).unwrap();
462
463 assert_eq!(result.success, parsed.success);
464 assert_eq!(result.method_used, parsed.method_used);
465 assert_eq!(result.content, parsed.content);
466 assert_eq!(result.path, parsed.path);
467 }
468
469 #[test]
470 fn clone_and_debug() {
471 let result = CreateSkillResult::failure(CreationMethod::Both, "test error");
472 let cloned = result.clone();
473
474 assert_eq!(result.success, cloned.success);
475 assert_eq!(result.error, cloned.error);
476
477 let debug = format!("{:?}", result);
478 assert!(debug.contains("test error"));
479 }
480 }
481}