fastskill-core 0.9.112

FastSkill core library - AI Skills management toolkit
Documentation
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
//! Main FastSkill service implementation

use crate::core::blob_storage::BlobStorageConfig;
use crate::events::EventBus;
use crate::execution::{ExecutionConfig, ExecutionSandbox};
use crate::storage::ZipHandler;
use crate::validation::{SkillValidator, ZipValidator};
use std::path::PathBuf;
use std::sync::Arc;
use tracing::info;

/// HTTP server CORS configuration
#[derive(Debug, Clone, Default)]
pub struct HttpServerConfig {
    /// List of origins allowed for CORS (required when server is used)
    pub allowed_origins: Vec<String>,

    /// Optional: allow list of request headers
    /// Default: ["Content-Type", "Authorization"] if unset
    pub allowed_headers: Vec<String>,
}

/// Main service configuration
#[derive(Debug, Clone)]
pub struct ServiceConfig {
    /// Base directory for skill storage
    pub skill_storage_path: PathBuf,

    /// Execution configuration
    pub execution: ExecutionConfig,

    /// Hot reloading configuration
    pub hot_reload: HotReloadConfig,

    /// Cache configuration
    pub cache: CacheConfig,

    /// Embedding configuration
    pub embedding: Option<EmbeddingConfig>,

    /// Security configuration
    pub security: SecurityConfig,

    /// Staging directory for registry publishing
    pub staging_dir: Option<PathBuf>,

    /// Registry blob storage configuration
    pub registry_blob_storage: Option<BlobStorageConfig>,

    /// Registry index path
    pub registry_index_path: Option<PathBuf>,

    /// Registry blob base URL
    pub registry_blob_base_url: Option<String>,

    /// HTTP server configuration
    pub http_server: Option<HttpServerConfig>,
}

impl Default for ServiceConfig {
    fn default() -> Self {
        Self {
            skill_storage_path: PathBuf::from("./skills"),
            execution: ExecutionConfig::default(),
            hot_reload: HotReloadConfig::default(),
            cache: CacheConfig::default(),
            embedding: None,
            security: SecurityConfig::default(),
            staging_dir: None,
            registry_blob_storage: None,
            registry_index_path: None,
            registry_blob_base_url: None,
            http_server: None,
        }
    }
}

/// Hot reloading configuration
#[derive(Debug, Clone)]
pub struct HotReloadConfig {
    /// Enable hot reloading
    pub enabled: bool,

    /// Directories to watch for changes
    pub watch_paths: Vec<PathBuf>,

    /// Debounce duration for file changes (ms)
    pub debounce_ms: u64,

    /// Automatically reload on file changes
    pub auto_reload: bool,
}

impl Default for HotReloadConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            watch_paths: vec![PathBuf::from("./skills")],
            debounce_ms: 1000,
            auto_reload: true,
        }
    }
}

/// Cache configuration
#[derive(Debug, Clone)]
pub struct CacheConfig {
    /// Maximum cache size (number of skills)
    pub max_size: usize,

    /// Cache TTL for metadata (seconds)
    pub metadata_ttl: u64,

    /// Cache TTL for content (seconds)
    pub content_ttl: u64,
}

impl Default for CacheConfig {
    fn default() -> Self {
        Self {
            max_size: 1000,
            metadata_ttl: 300, // 5 minutes
            content_ttl: 60,   // 1 minute
        }
    }
}

/// Embedding configuration
#[derive(Debug, Clone)]
pub struct EmbeddingConfig {
    /// OpenAI API base URL
    pub openai_base_url: String,

    /// Embedding model name
    pub embedding_model: String,

    /// Custom path for vector index database
    pub index_path: Option<PathBuf>,
}

/// Security configuration
#[derive(Debug, Clone)]
pub struct SecurityConfig {
    /// Enable security sandboxing
    pub enable_sandbox: bool,

    /// Allowed file system paths for scripts
    pub allowed_paths: Vec<PathBuf>,

    /// Audit logging configuration
    pub audit_logging: bool,

    /// Maximum script execution time
    pub max_execution_time: std::time::Duration,
}

impl Default for SecurityConfig {
    fn default() -> Self {
        Self {
            enable_sandbox: true,
            allowed_paths: vec![PathBuf::from("/tmp"), PathBuf::from("./temp")],
            audit_logging: true,
            max_execution_time: std::time::Duration::from_secs(60),
        }
    }
}

/// Unique identifier for a skill
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct SkillId(String);

impl SkillId {
    /// Create a new SkillId with validation
    pub fn new(id: String) -> Result<Self, ServiceError> {
        if id.trim().is_empty() {
            return Err(ServiceError::Validation(
                "Skill ID cannot be empty".to_string(),
            ));
        }
        if id.len() > 255 {
            return Err(ServiceError::Validation(
                "Skill ID too long (max 255 characters)".to_string(),
            ));
        }
        // Reject forward slashes (scope should be handled separately)
        if id.contains('/') {
            return Err(ServiceError::Validation(
                "Skill ID cannot contain forward slashes. Scope should be handled separately during publishing.".to_string(),
            ));
        }
        // Basic validation for allowed characters (alphanumeric, dash, underscore)
        if !id
            .chars()
            .all(|c| c.is_alphanumeric() || c == '-' || c == '_')
        {
            return Err(ServiceError::Validation("Skill ID contains invalid characters (only alphanumeric, dash, underscore allowed)".to_string()));
        }
        Ok(Self(id))
    }

    /// Get the string value
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Convert to owned string
    pub fn into_string(self) -> String {
        self.0
    }
}

impl std::fmt::Display for SkillId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<SkillId> for String {
    fn from(id: SkillId) -> String {
        id.0
    }
}

impl TryFrom<String> for SkillId {
    type Error = ServiceError;

    fn try_from(s: String) -> Result<Self, Self::Error> {
        SkillId::new(s)
    }
}

impl AsRef<str> for SkillId {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

impl serde::Serialize for SkillId {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(&self.0)
    }
}

impl<'de> serde::Deserialize<'de> for SkillId {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        SkillId::new(s).map_err(serde::de::Error::custom)
    }
}

/// Main service error type
#[derive(Debug, thiserror::Error)]
pub enum ServiceError {
    #[error("Storage error: {0}")]
    Storage(String),

    #[error("Execution error: {0}")]
    Execution(#[from] crate::execution::ExecutionError),

    #[error("Validation error: {0}")]
    Validation(String),

    #[error("Event error: {0}")]
    Event(String),

    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    #[error("Configuration error: {0}")]
    Config(String),

    #[error("Skill not found: {0}")]
    SkillNotFound(String),

    #[error("Invalid operation: {0}")]
    InvalidOperation(String),

    #[error("Custom error: {0}")]
    Custom(String),
}

/// Main FastSkill service
///
/// Note: This struct does not derive Debug because it contains Arc<dyn Trait> fields
/// which cannot implement Debug. This is acceptable for enterprise software.
pub struct FastSkillService {
    /// Service configuration
    config: ServiceConfig,

    /// Skill manager (shared instance)
    skill_manager: Arc<dyn crate::core::skill_manager::SkillManagementService>,

    /// Metadata service (depends on skill manager)
    metadata_service: Arc<dyn crate::core::metadata::MetadataService>,

    /// Vector index service (optional, for embedding search)
    vector_index_service: Option<Arc<dyn crate::core::vector_index::VectorIndexService>>,

    /// Skill storage backend
    storage: Arc<dyn crate::storage::StorageBackend>,

    /// ZIP package handler
    #[allow(dead_code)]
    zip_handler: Arc<ZipHandler>,

    /// Execution sandbox
    #[allow(dead_code)]
    sandbox: Arc<ExecutionSandbox>,

    /// Skill validator
    #[allow(dead_code)]
    skill_validator: Arc<SkillValidator>,

    /// ZIP validator
    #[allow(dead_code)]
    zip_validator: Arc<ZipValidator>,

    /// Event bus for notifications
    #[allow(dead_code)]
    event_bus: Arc<EventBus>,

    /// Hot reload manager
    hot_reload_manager: Option<Arc<crate::storage::hot_reload::HotReloadManager>>,

    /// Service state
    initialized: bool,
}

impl FastSkillService {
    /// Create a new service instance
    pub async fn new(config: ServiceConfig) -> Result<Self, ServiceError> {
        // Initialize logging only if not already initialized
        crate::init_logging();

        info!("Initializing FastSkill service v{}", crate::VERSION);

        // Create storage backend
        let storage = Arc::new(
            crate::storage::FilesystemStorage::new(config.skill_storage_path.clone()).await?,
        );

        // Create ZIP handler
        let zip_handler = Arc::new(crate::storage::ZipHandler::new()?);

        // Create execution sandbox
        let sandbox = Arc::new(crate::execution::ExecutionSandbox::new(
            config.execution.clone(),
        )?);

        // Create validators
        let skill_validator = Arc::new(crate::validation::SkillValidator::new());
        let zip_validator = Arc::new(crate::validation::ZipValidator::new());

        // Create event bus
        let event_bus = Arc::new(crate::events::EventBus::new());

        // Create skill manager
        let skill_manager = Arc::new(crate::core::skill_manager::SkillManager::new());

        // Create metadata service (depends on skill manager)
        let metadata_service = Arc::new(crate::core::metadata::MetadataServiceImpl::new(
            skill_manager.clone(),
        ));

        // Create vector index service if embedding is configured
        let vector_index_service = if let Some(embedding_config) = &config.embedding {
            Some(Arc::new(
                crate::core::vector_index::VectorIndexServiceImpl::with_config(
                    embedding_config,
                    &config.skill_storage_path,
                ),
            )
                as Arc<dyn crate::core::vector_index::VectorIndexService>)
        } else {
            None
        };

        // Create hot reload manager if enabled
        let hot_reload_manager = if config.hot_reload.enabled {
            Some(Arc::new(crate::storage::hot_reload::HotReloadManager::new(
                storage.clone(),
                event_bus.clone(),
            )?))
        } else {
            None
        };

        Ok(Self {
            config,
            skill_manager,
            metadata_service,
            vector_index_service,
            storage,
            zip_handler,
            sandbox,
            skill_validator,
            zip_validator,
            event_bus,
            hot_reload_manager,
            initialized: false,
        })
    }

    /// Initialize the service
    pub async fn initialize(&mut self) -> Result<(), ServiceError> {
        if self.initialized {
            return Ok(());
        }

        info!("Initializing service components...");

        // Initialize storage
        self.storage.initialize().await?;

        // Initialize hot reload if enabled
        if let Some(hot_reload) = &self.hot_reload_manager {
            hot_reload
                .enable_hot_reloading(self.config.hot_reload.watch_paths.clone())
                .await?;
        }

        // Auto-index skills from filesystem
        self.auto_index_skills_from_filesystem().await?;

        self.initialized = true;
        info!("Service initialization complete");

        Ok(())
    }

    /// Shutdown the service
    pub async fn shutdown(&mut self) -> Result<(), ServiceError> {
        info!("Shutting down service...");

        // Disable hot reloading
        if let Some(hot_reload) = &self.hot_reload_manager {
            hot_reload.disable_hot_reloading().await?;
        }

        // Clear any caches
        self.storage.clear_cache().await?;

        self.initialized = false;
        info!("Service shutdown complete");

        Ok(())
    }

    /// Get skill manager service
    pub fn skill_manager(&self) -> Arc<dyn crate::core::skill_manager::SkillManagementService> {
        self.skill_manager.clone()
    }

    /// Get metadata service
    pub fn metadata_service(&self) -> Arc<dyn crate::core::metadata::MetadataService> {
        self.metadata_service.clone()
    }

    /// Get vector index service (if available)
    pub fn vector_index_service(
        &self,
    ) -> Option<Arc<dyn crate::core::vector_index::VectorIndexService>> {
        self.vector_index_service.clone()
    }

    /// Get loading service
    pub fn loading_service(&self) -> Arc<dyn crate::core::loading::ProgressiveLoadingService> {
        Arc::new(crate::core::loading::LoadingService::new())
    }

    /// Get tool calling service
    pub fn tool_service(&self) -> Arc<dyn crate::core::tool_calling::ToolCallingService> {
        Arc::new(crate::core::tool_calling::ToolCallingServiceImpl::new())
    }

    /// Get routing service
    pub fn routing_service(&self) -> Arc<dyn crate::core::routing::RoutingService> {
        Arc::new(crate::core::routing::RoutingServiceImpl::new(
            self.metadata_service.clone(),
        ))
    }

    /// Get service configuration
    pub fn config(&self) -> &ServiceConfig {
        &self.config
    }

    /// Get context resolver for machine-first skill resolution
    pub fn context_resolver(&self) -> crate::core::context_resolver::ContextResolver {
        crate::core::context_resolver::ContextResolver::new(
            self.skill_manager.clone(),
            self.metadata_service.clone(),
            self.vector_index_service.clone(),
            self.config.embedding.clone(),
            self.config.skill_storage_path.clone(),
        )
    }

    /// Check if service is initialized
    pub fn is_initialized(&self) -> bool {
        self.initialized
    }

    /// Auto-index skills from the filesystem by scanning for SKILL.md files
    async fn auto_index_skills_from_filesystem(&self) -> Result<(), ServiceError> {
        use walkdir::WalkDir;

        let mut indexed_count = 0;

        // Walk the skills directory recursively
        for entry in WalkDir::new(&self.config.skill_storage_path)
            .into_iter()
            .filter_entry(|e| {
                // Skip hidden directories and common system directories
                !e.file_name()
                    .to_str()
                    .map(|s| {
                        s.starts_with('.')
                            || s == "node_modules"
                            || s == "target"
                            || s == "__pycache__"
                    })
                    .unwrap_or(false)
            })
        {
            let entry = entry.map_err(|e| {
                ServiceError::Custom(format!("Failed to read directory entry: {}", e))
            })?;

            // Look for SKILL.md or skill.md files
            if entry.file_type().is_file() {
                let fname = entry.file_name();
                if fname == "SKILL.md" || fname == "skill.md" {
                    let skill_file = entry.path();

                    match self.try_index_skill_from_file(skill_file).await {
                        Ok(_) => {
                            indexed_count += 1;
                        }
                        Err(e) => {
                            tracing::warn!(
                                "Failed to index skill at {}: {}",
                                skill_file.display(),
                                e
                            );
                        }
                    }
                }
            }
        }

        if indexed_count > 0 {
            info!("Auto-indexed {} skills from filesystem", indexed_count);
        }

        Ok(())
    }

    /// Try to index a single skill from its SKILL.md file
    async fn try_index_skill_from_file(
        &self,
        skill_file: &std::path::Path,
    ) -> Result<(), ServiceError> {
        // Read the SKILL.md file
        let content = tokio::fs::read_to_string(skill_file)
            .await
            .map_err(|e| ServiceError::Custom(format!("Failed to read SKILL.md: {}", e)))?;

        // Parse the frontmatter
        let frontmatter = crate::core::metadata::parse_yaml_frontmatter(&content)?;

        // Get the skill directory (parent of SKILL.md)
        let skill_dir = skill_file
            .parent()
            .ok_or_else(|| ServiceError::Custom("SKILL.md has no parent directory".to_string()))?;

        // Use directory name as skill ID
        let skill_id_str = skill_dir
            .file_name()
            .and_then(|n| n.to_str())
            .ok_or_else(|| ServiceError::Custom("Invalid skill directory name".to_string()))?
            .to_string();
        let skill_id = SkillId::new(skill_id_str)?;

        // Create skill definition from frontmatter
        let mut skill = crate::core::skill_manager::SkillDefinition::new(
            skill_id.clone(),
            frontmatter.name,
            frontmatter.description,
            frontmatter.version.unwrap_or_else(|| "1.0.0".to_string()),
        );

        // Set additional fields
        skill.author = frontmatter.author;
        skill.skill_file = skill_file.to_path_buf();

        // Set timestamps
        skill.created_at = chrono::Utc::now();
        skill.updated_at = chrono::Utc::now();

        // Try to register the skill (ignore if it already exists)
        match self.skill_manager.register_skill(skill).await {
            Ok(_) => Ok(()),
            Err(crate::core::service::ServiceError::Custom(msg))
                if msg.contains("already exists") =>
            {
                // Skill already registered, that's fine
                Ok(())
            }
            Err(e) => Err(e),
        }
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    #[tokio::test]
    async fn test_service_creation() {
        let temp_dir = TempDir::new().unwrap();
        let config = ServiceConfig {
            skill_storage_path: temp_dir.path().to_path_buf(),
            ..Default::default()
        };

        let mut service = FastSkillService::new(config).await.unwrap();
        assert!(!service.is_initialized());

        service.initialize().await.unwrap();
        assert!(service.is_initialized());
    }

    #[tokio::test]
    async fn test_service_shutdown() {
        let temp_dir = TempDir::new().unwrap();
        let config = ServiceConfig {
            skill_storage_path: temp_dir.path().to_path_buf(),
            ..Default::default()
        };

        let mut service = FastSkillService::new(config).await.unwrap();
        service.initialize().await.unwrap();

        service.shutdown().await.unwrap();
        assert!(!service.is_initialized());
    }

    #[test]
    fn test_skill_id_new_validates_input() {
        assert!(SkillId::new("valid-id".to_string()).is_ok());
        assert!(SkillId::new("valid_id_123".to_string()).is_ok());
        assert!(SkillId::new("".to_string()).is_err());
        assert!(SkillId::new("bad/id".to_string()).is_err());
        assert!(SkillId::new("id with spaces".to_string()).is_err());
    }

    #[test]
    fn test_skill_id_try_from_validates_input() {
        // TryFrom should validate input
        assert!(SkillId::try_from("valid-id".to_string()).is_ok());
        assert!(SkillId::try_from("".to_string()).is_err());
        assert!(SkillId::try_from("bad/id".to_string()).is_err());
    }
}