-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmanager.rs
More file actions
638 lines (539 loc) · 20.8 KB
/
manager.rs
File metadata and controls
638 lines (539 loc) · 20.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
//! Skill manager with hot reloading support.
//!
//! Provides centralized skill loading, management, and hot reloading.
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use dashmap::DashMap;
use tokio::sync::mpsc;
use tracing::{debug, error, info, warn};
use crate::error::{SkillError, SkillResult};
use crate::skill::Skill;
use crate::validation::SkillValidator;
use crate::watcher::{SkillWatcher, WatchEvent};
/// Event sent when skills are reloaded.
#[derive(Debug, Clone)]
pub struct ReloadEvent {
/// Number of skills loaded.
pub count: usize,
/// Paths that were scanned.
pub paths: Vec<PathBuf>,
/// Skills that failed to load.
pub failed: Vec<(PathBuf, String)>,
}
/// Manager for loading and managing skills.
pub struct SkillManager {
/// Loaded skills (thread-safe).
skills: Arc<DashMap<String, Skill>>,
/// Skill search directories.
skill_dirs: Vec<PathBuf>,
/// File system watcher for hot reloading.
watcher: Option<SkillWatcher>,
/// Channel for reload notifications.
reload_tx: Option<mpsc::Sender<ReloadEvent>>,
/// Current project path.
project_path: Option<PathBuf>,
/// Whether hot reloading is enabled.
hot_reload_enabled: bool,
}
impl SkillManager {
/// Creates a new skill manager with the given search directories.
pub fn new(skill_dirs: Vec<PathBuf>) -> Self {
Self {
skills: Arc::new(DashMap::new()),
skill_dirs,
watcher: None,
reload_tx: None,
project_path: None,
hot_reload_enabled: false,
}
}
/// Creates a skill manager with default search paths.
///
/// Default paths:
/// 1. `.cortex/skills/` (project-local, if project path set)
/// 2. `~/.config/cortex/skills/` (global)
pub fn with_defaults() -> Self {
let mut dirs = Vec::new();
// Global skills directory
if let Some(config_dir) = dirs::config_dir() {
dirs.push(config_dir.join("cortex/skills"));
}
Self::new(dirs)
}
/// Sets the project path and adds project-local skills directory.
pub fn set_project_path(&mut self, path: impl Into<PathBuf>) {
let path = path.into();
let project_skills = path.join(".cortex/skills");
// Add project skills directory at the beginning (highest priority)
if !self.skill_dirs.contains(&project_skills) {
self.skill_dirs.insert(0, project_skills);
}
self.project_path = Some(path);
}
/// Sets the reload notification channel.
pub fn with_reload_channel(mut self, tx: mpsc::Sender<ReloadEvent>) -> Self {
self.reload_tx = Some(tx);
self
}
/// Adds a skill search directory.
pub fn add_skill_dir(&mut self, dir: PathBuf) {
if !self.skill_dirs.contains(&dir) {
self.skill_dirs.push(dir);
}
}
/// Returns the skill search directories.
pub fn skill_dirs(&self) -> &[PathBuf] {
&self.skill_dirs
}
/// Loads all skills from configured directories.
///
/// Returns the number of skills loaded.
pub async fn load_all(&self) -> SkillResult<usize> {
let mut loaded = 0;
let mut failed = Vec::new();
self.skills.clear();
for dir in &self.skill_dirs {
if !dir.exists() {
debug!("Skill directory does not exist: {:?}", dir);
continue;
}
debug!("Scanning skill directory: {:?}", dir);
// Scan for skill directories (those containing SKILL.toml)
let mut entries = match tokio::fs::read_dir(dir).await {
Ok(entries) => entries,
Err(e) => {
warn!("Failed to read skill directory {:?}: {}", dir, e);
continue;
}
};
while let Ok(Some(entry)) = entries.next_entry().await {
let path = entry.path();
if !path.is_dir() {
continue;
}
let skill_toml = path.join("SKILL.toml");
if skill_toml.exists() {
match self.load_skill(&skill_toml).await {
Ok(skill) => {
// Validate the skill
let validation = SkillValidator::validate(&skill);
if !validation.is_valid {
warn!(
"Skill '{}' failed validation: {:?}",
skill.name, validation.errors
);
failed.push((skill_toml, validation.errors.join("; ")));
continue;
}
// Log warnings
for warning in &validation.warnings {
debug!("Skill '{}' warning: {}", skill.name, warning);
}
info!("Loaded skill: {} ({})", skill.name, skill.id);
self.skills.insert(skill.id.clone(), skill);
loaded += 1;
}
Err(e) => {
if e.is_recoverable() {
debug!("Skipped skill at {:?}: {}", path, e);
} else {
warn!("Failed to load skill at {:?}: {}", path, e);
}
failed.push((skill_toml, e.to_string()));
}
}
}
}
}
info!(
"Loaded {} skills from {} directories ({} failed)",
loaded,
self.skill_dirs.len(),
failed.len()
);
// Notify reload listeners
if let Some(ref tx) = self.reload_tx {
let _ = tx
.send(ReloadEvent {
count: loaded,
paths: self.skill_dirs.clone(),
failed,
})
.await;
}
Ok(loaded)
}
/// Loads a single skill from a SKILL.toml path.
async fn load_skill(&self, toml_path: &Path) -> SkillResult<Skill> {
crate::parser::parse_skill_toml_async(toml_path).await
}
/// Reloads all skills (useful for hotkey or manual refresh).
pub async fn reload(&self) -> SkillResult<usize> {
debug!("Reloading skills...");
self.load_all().await
}
/// Reloads a single skill by path.
pub async fn reload_skill(&self, skill_dir: &Path) -> SkillResult<()> {
let toml_path = skill_dir.join("SKILL.toml");
let skill = self.load_skill(&toml_path).await?;
// Validate
let validation = SkillValidator::validate(&skill);
if !validation.is_valid {
return Err(SkillError::Validation(validation.errors.join("; ")));
}
info!("Reloaded skill: {}", skill.name);
self.skills.insert(skill.id.clone(), skill);
Ok(())
}
/// Gets a skill by ID.
pub fn get(&self, id: &str) -> Option<Skill> {
self.skills.get(id).map(|s| s.value().clone())
}
/// Gets a skill by ID (async version for compatibility).
pub async fn get_async(&self, id: &str) -> Option<Skill> {
self.get(id)
}
/// Lists all loaded skills.
pub fn list(&self) -> Vec<Skill> {
self.skills.iter().map(|s| s.value().clone()).collect()
}
/// Lists all loaded skills (async version).
pub async fn list_async(&self) -> Vec<Skill> {
self.list()
}
/// Returns the number of loaded skills.
pub fn len(&self) -> usize {
self.skills.len()
}
/// Returns true if no skills are loaded.
pub fn is_empty(&self) -> bool {
self.skills.is_empty()
}
/// Searches for skills matching a pattern.
///
/// Supports glob-style wildcards:
/// - `*` - matches all skills
/// - `code-*` - matches skills starting with "code-"
/// - `*-review` - matches skills ending with "-review"
pub fn match_pattern(&self, pattern: &str) -> Vec<Skill> {
if pattern == "*" {
return self.list();
}
let glob = match glob::Pattern::new(&pattern.to_lowercase()) {
Ok(g) => g,
Err(_) => return Vec::new(),
};
self.skills
.iter()
.filter(|s| {
glob.matches(&s.value().id.to_lowercase())
|| glob.matches(&s.value().name.to_lowercase())
})
.map(|s| s.value().clone())
.collect()
}
/// Searches for skills matching a pattern (async version).
pub async fn match_pattern_async(&self, pattern: &str) -> Vec<Skill> {
self.match_pattern(pattern)
}
/// Finds skills by tag.
pub fn find_by_tag(&self, tag: &str) -> Vec<Skill> {
let tag_lower = tag.to_lowercase();
self.skills
.iter()
.filter(|s| {
s.value()
.metadata
.tags
.iter()
.any(|t| t.to_lowercase() == tag_lower)
})
.map(|s| s.value().clone())
.collect()
}
/// Finds skills by author.
pub fn find_by_author(&self, author: &str) -> Vec<Skill> {
let author_lower = author.to_lowercase();
self.skills
.iter()
.filter(|s| {
s.value()
.metadata
.author
.as_ref()
.is_some_and(|a| a.to_lowercase().contains(&author_lower))
})
.map(|s| s.value().clone())
.collect()
}
/// Returns auto-allowed skills.
pub fn auto_allowed_skills(&self) -> Vec<Skill> {
self.skills
.iter()
.filter(|s| s.value().is_auto_allowed())
.map(|s| s.value().clone())
.collect()
}
/// Enables hot reloading for skill directories.
pub async fn enable_hot_reload(&mut self) -> SkillResult<()> {
if self.hot_reload_enabled {
return Ok(());
}
let (event_tx, mut event_rx) = mpsc::channel(100);
let mut watcher = SkillWatcher::new(event_tx);
watcher.start(self.skill_dirs.clone())?;
self.watcher = Some(watcher);
self.hot_reload_enabled = true;
// Spawn handler for watch events
let skills = self.skills.clone();
let _reload_tx = self.reload_tx.clone();
let _skill_dirs = self.skill_dirs.clone();
tokio::spawn(async move {
while let Some(event) = event_rx.recv().await {
match event {
WatchEvent::Modified(path) | WatchEvent::Created(path) => {
// Find the skill directory
if let Some(skill_dir) = path.parent() {
let toml_path = skill_dir.join("SKILL.toml");
if toml_path.exists() {
match crate::parser::parse_skill_toml_async(&toml_path).await {
Ok(skill) => {
let validation = SkillValidator::validate(&skill);
if validation.is_valid {
info!("Hot-reloaded skill: {}", skill.name);
skills.insert(skill.id.clone(), skill);
} else {
warn!(
"Hot-reload validation failed for {:?}: {:?}",
toml_path, validation.errors
);
}
}
Err(e) => {
warn!("Hot-reload failed for {:?}: {}", toml_path, e);
}
}
}
}
}
WatchEvent::Deleted(path) => {
// Remove skill if its SKILL.toml was deleted
if let Some(skill_dir) = path.parent() {
let skill_id = skill_dir
.file_name()
.and_then(|n| n.to_str())
.unwrap_or("")
.to_lowercase();
if skills.remove(&skill_id).is_some() {
info!("Hot-removed skill: {}", skill_id);
}
}
}
WatchEvent::ReloadAll => {
info!("Many changes detected, performing full reload...");
// Clear and reload all
skills.clear();
// Note: Full reload would need access to load_skill method
// For simplicity, we just clear and let the next list() trigger reload
}
WatchEvent::Error(e) => {
error!("Watch error: {}", e);
}
}
}
});
info!("Hot reloading enabled");
Ok(())
}
/// Disables hot reloading.
pub fn disable_hot_reload(&mut self) {
if let Some(mut watcher) = self.watcher.take() {
watcher.stop();
}
self.hot_reload_enabled = false;
info!("Hot reloading disabled");
}
/// Returns true if hot reloading is enabled.
pub fn is_hot_reload_enabled(&self) -> bool {
self.hot_reload_enabled
}
/// Groups skills by source directory.
pub fn by_source(&self) -> HashMap<PathBuf, Vec<Skill>> {
let mut grouped = HashMap::new();
for skill in self.skills.iter() {
if let Some(parent) = skill.value().source_path.parent() {
grouped
.entry(parent.to_path_buf())
.or_insert_with(Vec::new)
.push(skill.value().clone());
}
}
grouped
}
/// Checks if a skill with the given ID exists.
pub fn exists(&self, id: &str) -> bool {
self.skills.contains_key(id)
}
}
impl Default for SkillManager {
fn default() -> Self {
Self::with_defaults()
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use tempfile::TempDir;
fn create_test_skill(dir: &Path, name: &str, description: &str, auto_allowed: bool) {
let skill_dir = dir.join(name);
fs::create_dir_all(&skill_dir).unwrap();
let toml_content = format!(
r#"
name = "{}"
description = "{}"
version = "1.0.0"
auto_allowed = {}
timeout = 60
tags = ["test"]
"#,
name, description, auto_allowed
);
fs::write(skill_dir.join("SKILL.toml"), toml_content).unwrap();
fs::write(skill_dir.join("skill.md"), format!("Prompt for {}", name)).unwrap();
}
#[tokio::test]
async fn test_manager_new() {
let manager = SkillManager::new(vec![]);
assert!(manager.is_empty());
}
#[tokio::test]
async fn test_manager_with_defaults() {
let manager = SkillManager::with_defaults();
assert!(!manager.skill_dirs().is_empty());
}
#[tokio::test]
async fn test_load_skills() {
let temp = TempDir::new().unwrap();
create_test_skill(temp.path(), "skill-one", "First skill", false);
create_test_skill(temp.path(), "skill-two", "Second skill", true);
let manager = SkillManager::new(vec![temp.path().to_path_buf()]);
let count = manager.load_all().await.unwrap();
assert_eq!(count, 2);
assert_eq!(manager.len(), 2);
assert!(manager.exists("skill-one"));
assert!(manager.exists("skill-two"));
}
#[tokio::test]
async fn test_get_skill() {
let temp = TempDir::new().unwrap();
create_test_skill(temp.path(), "test-skill", "Test", false);
let manager = SkillManager::new(vec![temp.path().to_path_buf()]);
manager.load_all().await.unwrap();
let skill = manager.get("test-skill");
assert!(skill.is_some());
assert_eq!(skill.unwrap().name, "test-skill");
assert!(manager.get("nonexistent").is_none());
}
#[tokio::test]
async fn test_list_skills() {
let temp = TempDir::new().unwrap();
create_test_skill(temp.path(), "skill-a", "A", false);
create_test_skill(temp.path(), "skill-b", "B", false);
let manager = SkillManager::new(vec![temp.path().to_path_buf()]);
manager.load_all().await.unwrap();
let skills = manager.list();
assert_eq!(skills.len(), 2);
}
#[tokio::test]
async fn test_match_pattern() {
let temp = TempDir::new().unwrap();
create_test_skill(temp.path(), "code-review", "Review", false);
create_test_skill(temp.path(), "code-lint", "Lint", false);
create_test_skill(temp.path(), "test-runner", "Test", false);
let manager = SkillManager::new(vec![temp.path().to_path_buf()]);
manager.load_all().await.unwrap();
// Match all
let all = manager.match_pattern("*");
assert_eq!(all.len(), 3);
// Match prefix
let code = manager.match_pattern("code-*");
assert_eq!(code.len(), 2);
// Match suffix
let review = manager.match_pattern("*-review");
assert_eq!(review.len(), 1);
}
#[tokio::test]
async fn test_auto_allowed_skills() {
let temp = TempDir::new().unwrap();
create_test_skill(temp.path(), "auto-skill", "Auto", true);
create_test_skill(temp.path(), "manual-skill", "Manual", false);
let manager = SkillManager::new(vec![temp.path().to_path_buf()]);
manager.load_all().await.unwrap();
let auto = manager.auto_allowed_skills();
assert_eq!(auto.len(), 1);
assert_eq!(auto[0].id, "auto-skill");
}
#[tokio::test]
async fn test_find_by_tag() {
let temp = TempDir::new().unwrap();
create_test_skill(temp.path(), "tagged-skill", "Tagged", false);
let manager = SkillManager::new(vec![temp.path().to_path_buf()]);
manager.load_all().await.unwrap();
let found = manager.find_by_tag("test");
assert_eq!(found.len(), 1);
let not_found = manager.find_by_tag("nonexistent");
assert!(not_found.is_empty());
}
#[tokio::test]
async fn test_reload() {
let temp = TempDir::new().unwrap();
create_test_skill(temp.path(), "skill", "Test", false);
let manager = SkillManager::new(vec![temp.path().to_path_buf()]);
manager.load_all().await.unwrap();
assert_eq!(manager.len(), 1);
// Add another skill
create_test_skill(temp.path(), "new-skill", "New", false);
// Reload
let count = manager.reload().await.unwrap();
assert_eq!(count, 2);
assert_eq!(manager.len(), 2);
}
#[tokio::test]
async fn test_set_project_path() {
let mut manager = SkillManager::with_defaults();
let initial_count = manager.skill_dirs().len();
manager.set_project_path("/test/project");
// Should have one more directory (project-local)
assert_eq!(manager.skill_dirs().len(), initial_count + 1);
assert!(manager.skill_dirs()[0].ends_with(".cortex/skills"));
}
#[tokio::test]
async fn test_by_source() {
let temp = TempDir::new().unwrap();
create_test_skill(temp.path(), "skill-a", "A", false);
create_test_skill(temp.path(), "skill-b", "B", false);
let manager = SkillManager::new(vec![temp.path().to_path_buf()]);
manager.load_all().await.unwrap();
let grouped = manager.by_source();
// Skills are grouped by their parent directory (the temp directory)
// Both skills share the same parent, so there should be 1 group with 2 skills
assert_eq!(grouped.len(), 1);
let skills_in_group = grouped.values().next().unwrap();
assert_eq!(skills_in_group.len(), 2);
}
#[tokio::test]
async fn test_empty_directory() {
let temp = TempDir::new().unwrap();
let manager = SkillManager::new(vec![temp.path().to_path_buf()]);
let count = manager.load_all().await.unwrap();
assert_eq!(count, 0);
}
#[tokio::test]
async fn test_nonexistent_directory() {
let manager = SkillManager::new(vec![PathBuf::from("/nonexistent/path")]);
let count = manager.load_all().await.unwrap();
assert_eq!(count, 0);
}
}