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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
//! Sources system for managing skill repositories

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tokio::sync::RwLock;

/// Main sources configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SourcesConfig {
    #[serde(default)]
    pub sources: Vec<SourceDefinition>,
}

/// Source definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SourceDefinition {
    pub name: String,
    #[serde(default = "default_priority")]
    pub priority: u32,
    pub source: SourceConfig,
}

impl SourceDefinition {
    /// Check if this source supports listing skills
    /// Returns true for git-marketplace, zip-url, and local sources
    /// Returns false for HTTP registries (http-registry)
    pub fn supports_listing(&self) -> bool {
        matches!(
            &self.source,
            SourceConfig::Git { .. } | SourceConfig::ZipUrl { .. } | SourceConfig::Local { .. }
        )
    }
}

/// Default priority value (0 = highest priority)
fn default_priority() -> u32 {
    0
}

/// Source authentication configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum SourceAuth {
    #[serde(rename = "pat")]
    Pat { env_var: String },
    #[serde(rename = "ssh-key")]
    SshKey { path: PathBuf },
    #[serde(rename = "basic")]
    Basic {
        username: String,
        password_env: String,
    },
}

/// Source configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum SourceConfig {
    #[serde(rename = "git")]
    Git {
        url: String,
        #[serde(default)]
        branch: Option<String>,
        #[serde(default)]
        tag: Option<String>,
        #[serde(default)]
        auth: Option<SourceAuth>,
    },
    #[serde(rename = "zip-url")]
    ZipUrl {
        base_url: String,
        #[serde(default)]
        auth: Option<SourceAuth>,
    },
    #[serde(rename = "local")]
    Local { path: PathBuf },
}

/// Information about a skill available from a source
#[derive(Debug, Clone)]
pub struct SkillInfo {
    pub id: String,
    pub name: String,
    pub description: String,
    pub version: Option<String>,
    pub source_name: String,
}

/// Marketplace.json structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MarketplaceJson {
    pub version: String,
    pub skills: Vec<MarketplaceSkill>,
}

/// Skill entry in marketplace.json
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MarketplaceSkill {
    pub id: String,
    pub name: String,
    pub description: String,
    pub version: String,
    #[serde(default)]
    pub author: Option<String>,
    #[serde(default)]
    pub download_url: Option<String>,
}

/// Claude Code marketplace.json format structures
/// This format is used by Claude Code standard repositories
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClaudeCodeMarketplaceJson {
    pub name: String,
    #[serde(default)]
    pub owner: Option<ClaudeCodeOwner>,
    #[serde(default)]
    pub metadata: Option<ClaudeCodeMetadata>,
    pub plugins: Vec<ClaudeCodePlugin>,
}

/// Owner information in Claude Code format
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClaudeCodeOwner {
    pub name: String,
    #[serde(default)]
    pub email: Option<String>,
}

/// Metadata in Claude Code format
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClaudeCodeMetadata {
    #[serde(default)]
    pub description: Option<String>,
    #[serde(default)]
    pub version: Option<String>,
}

/// Plugin entry in Claude Code format
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClaudeCodePlugin {
    pub name: String,
    #[serde(default)]
    pub description: Option<String>,
    #[serde(default)]
    pub source: Option<String>,
    #[serde(default)]
    pub strict: Option<bool>,
    pub skills: Vec<String>, // Array of skill paths (relative to repository root)
}

// Marketplace.json format: Only Claude Code format is supported

/// Cached marketplace.json entry
#[derive(Debug, Clone)]
struct CachedMarketplace {
    data: MarketplaceJson,
    fetched_at: DateTime<Utc>,
    ttl_seconds: u64,
}

impl CachedMarketplace {
    fn is_expired(&self) -> bool {
        let now = Utc::now();
        let elapsed = (now - self.fetched_at).num_seconds() as u64;
        elapsed > self.ttl_seconds
    }
}

/// Sources manager for handling multiple sources
pub struct SourcesManager {
    pub(crate) config_path: PathBuf,
    sources: HashMap<String, SourceDefinition>,
    marketplace_cache: Arc<RwLock<HashMap<String, CachedMarketplace>>>,
    cache_ttl_seconds: u64,
}

impl SourcesManager {
    /// Create a new sources manager
    pub fn new(config_path: PathBuf) -> Self {
        Self {
            config_path,
            sources: HashMap::new(),
            marketplace_cache: Arc::new(RwLock::new(HashMap::new())),
            cache_ttl_seconds: 300, // 5 minutes default TTL
        }
    }

    /// Create a new sources manager with custom cache TTL
    pub fn with_cache_ttl(config_path: PathBuf, cache_ttl_seconds: u64) -> Self {
        Self {
            config_path,
            sources: HashMap::new(),
            marketplace_cache: Arc::new(RwLock::new(HashMap::new())),
            cache_ttl_seconds,
        }
    }

    /// Load sources from TOML file
    pub fn load(&mut self) -> Result<(), SourcesError> {
        if !self.config_path.exists() {
            // Create default empty config if file doesn't exist
            let config = SourcesConfig {
                sources: Vec::new(),
            };
            config.save_to_file(&self.config_path)?;
            self.sources = HashMap::new();
            return Ok(());
        }

        let config = SourcesConfig::load_from_file(&self.config_path)?;
        // Sort sources by priority (lower number = higher priority)
        let mut sorted_sources: Vec<SourceDefinition> = config.sources;
        sorted_sources.sort_by_key(|s| s.priority);

        self.sources = sorted_sources
            .into_iter()
            .map(|source| (source.name.clone(), source))
            .collect();

        Ok(())
    }

    /// Save sources to TOML file
    pub fn save(&self) -> Result<(), SourcesError> {
        let mut sources: Vec<SourceDefinition> = self.sources.values().cloned().collect();
        // Sort by priority before saving
        sources.sort_by_key(|s| s.priority);
        let config = SourcesConfig { sources };
        config.save_to_file(&self.config_path)?;
        Ok(())
    }

    /// Add a new source
    pub fn add_source(&mut self, name: String, config: SourceConfig) -> Result<(), SourcesError> {
        self.add_source_with_priority(name, config, 0)
    }

    /// Add a new source with priority
    pub fn add_source_with_priority(
        &mut self,
        name: String,
        config: SourceConfig,
        priority: u32,
    ) -> Result<(), SourcesError> {
        if self.sources.contains_key(&name) {
            return Err(SourcesError::AlreadyExists(name));
        }

        let definition = SourceDefinition {
            name: name.clone(),
            priority,
            source: config,
        };

        self.sources.insert(name, definition);
        Ok(())
    }

    /// Remove a source
    pub fn remove_source(&mut self, name: &str) -> Result<(), SourcesError> {
        if self.sources.remove(name).is_none() {
            return Err(SourcesError::SourceNotFound(name.to_string()));
        }
        Ok(())
    }

    /// Get a source by name
    pub fn get_source(&self, name: &str) -> Option<&SourceDefinition> {
        self.sources.get(name)
    }

    /// List all sources (sorted by priority)
    pub fn list_sources(&self) -> Vec<&SourceDefinition> {
        let mut sources: Vec<&SourceDefinition> = self.sources.values().collect();
        sources.sort_by_key(|s| s.priority);
        sources
    }

    /// Clear the marketplace cache
    pub async fn clear_cache(&self) {
        let mut cache = self.marketplace_cache.write().await;
        cache.clear();
    }

    /// Get available skills from all sources (checked in priority order)
    pub async fn get_available_skills(&self) -> Result<Vec<SkillInfo>, SourcesError> {
        let mut all_skills = Vec::new();

        // Get sources sorted by priority
        let mut sources: Vec<(&String, &SourceDefinition)> = self.sources.iter().collect();
        sources.sort_by_key(|(_, def)| def.priority);

        for (source_name, source_def) in sources {
            let skills = self.get_skills_from_source(source_name, source_def).await?;
            all_skills.extend(skills);
        }

        Ok(all_skills)
    }

    /// Get skills from a specific source
    pub async fn get_skills_from_source(
        &self,
        source_name: &str,
        source_def: &SourceDefinition,
    ) -> Result<Vec<SkillInfo>, SourcesError> {
        match &source_def.source {
            SourceConfig::Git { url, branch, .. } => {
                // Try to load marketplace.json from Git source
                // Pass branch info for proper URL construction
                self.load_marketplace_from_url_with_branch(url, branch.as_deref(), source_name)
                    .await
            }
            SourceConfig::ZipUrl { base_url, .. } => {
                // Load marketplace.json from ZipUrl source
                self.load_marketplace_from_url_with_branch(base_url, None, source_name)
                    .await
            }
            SourceConfig::Local { path } => {
                // Scan local path for skills
                self.scan_local_source(path, source_name).await
            }
        }
    }

    /// Convert Claude Code format to FastSkill internal format
    /// This extracts skills from plugins by resolving skill paths
    async fn convert_claude_to_fastskill_format(
        &self,
        claude_marketplace: ClaudeCodeMarketplaceJson,
        base_url: String,
        _source_name: &str,
    ) -> Result<MarketplaceJson, SourcesError> {
        let mut skills = Vec::new();
        let owner_name = claude_marketplace.owner.as_ref().map(|o| o.name.clone());
        let metadata_version = claude_marketplace
            .metadata
            .as_ref()
            .and_then(|m| m.version.clone());

        for plugin in claude_marketplace.plugins {
            let plugin_source = plugin.source.as_deref().unwrap_or("./");

            for skill_path in plugin.skills {
                // Resolve skill path relative to plugin source
                let resolved_path = if skill_path.starts_with("./") {
                    // Relative to plugin source
                    format!(
                        "{}{}",
                        plugin_source.trim_end_matches('/'),
                        &skill_path[1..]
                    )
                } else if skill_path.starts_with('/') {
                    // Absolute from repo root
                    skill_path.trim_start_matches('/').to_string()
                } else {
                    // Relative to plugin source
                    format!("{}/{}", plugin_source.trim_end_matches('/'), skill_path)
                };

                // Extract skill ID from path (use directory name or last component)
                let skill_id = resolved_path
                    .trim_end_matches('/')
                    .split('/')
                    .next_back()
                    .unwrap_or(&resolved_path)
                    .to_string();

                // Use plugin description as fallback, or metadata description
                let description = plugin
                    .description
                    .clone()
                    .or_else(|| {
                        claude_marketplace
                            .metadata
                            .as_ref()
                            .and_then(|m| m.description.clone())
                    })
                    .unwrap_or_else(|| format!("Skill from {}", plugin.name));

                // Construct download URL if base_url is provided
                let download_url = if base_url.contains("github.com")
                    && !base_url.contains("raw.githubusercontent.com")
                {
                    let repo_path = base_url
                        .trim_start_matches("https://github.com/")
                        .trim_start_matches("http://github.com/")
                        .trim_end_matches(".git")
                        .trim_end_matches('/');
                    Some(format!(
                        "https://github.com/{}/tree/main/{}",
                        repo_path, resolved_path
                    ))
                } else if !base_url.is_empty() {
                    let base = base_url.trim_end_matches('/');
                    Some(format!("{}/{}", base, resolved_path))
                } else {
                    None
                };

                skills.push(MarketplaceSkill {
                    id: skill_id.clone(),
                    name: skill_id.clone(), // Use ID as name if not available
                    description,
                    version: metadata_version
                        .clone()
                        .unwrap_or_else(|| "1.0.0".to_string()),
                    author: owner_name.clone(),
                    download_url,
                });
            }
        }

        Ok(MarketplaceJson {
            version: "1.0".to_string(),
            skills,
        })
    }

    /// Try to fetch marketplace.json from a URL
    /// Only Claude Code format is supported
    /// Tries Claude Code standard location first (.claude-plugin/marketplace.json), then root location
    async fn try_fetch_marketplace(
        &self,
        url: &str,
        base_repo_url: Option<&str>,
    ) -> Result<MarketplaceJson, SourcesError> {
        let client = reqwest::Client::new();
        let response = client.get(url).send().await.map_err(|e| {
            SourcesError::Network(format!("Failed to fetch marketplace.json: {}", e))
        })?;

        if !response.status().is_success() {
            return Err(SourcesError::Network(format!(
                "Failed to fetch marketplace.json: HTTP {}",
                response.status()
            )));
        }

        // Parse as Claude Code format (only supported format)
        let claude_marketplace: ClaudeCodeMarketplaceJson = response.json().await.map_err(|e| {
            SourcesError::Parse(format!(
                "Failed to parse Claude Code marketplace.json: {}",
                e
            ))
        })?;

        // Extract base repository URL for path resolution
        // If base_repo_url is provided, use it; otherwise try to extract from raw URL
        let base_url = if let Some(repo_url) = base_repo_url {
            repo_url.to_string()
        } else if url.contains("raw.githubusercontent.com") {
            // Extract repo path from raw.githubusercontent.com URL
            // e.g., https://raw.githubusercontent.com/owner/repo/branch/.claude-plugin/marketplace.json
            // -> https://github.com/owner/repo
            let parts: Vec<&str> = url.split('/').collect();
            if parts.len() >= 5 {
                let owner = parts[3];
                let repo = parts[4];
                format!("https://github.com/{}/{}", owner, repo)
            } else {
                String::new()
            }
        } else {
            // For other URLs, extract base by removing filename
            if let Some(pos) = url.rfind('/') {
                url[..pos].to_string()
            } else {
                url.to_string()
            }
        };

        // Convert Claude Code format to FastSkill internal format
        let marketplace = self
            .convert_claude_to_fastskill_format(claude_marketplace, base_url, "")
            .await?;

        // Validate marketplace.json structure
        for skill in &marketplace.skills {
            if skill.id.is_empty() || skill.name.is_empty() || skill.description.is_empty() {
                return Err(SourcesError::Parse(
                    "Invalid marketplace.json: skills must have id, name, and description"
                        .to_string(),
                ));
            }
        }

        Ok(marketplace)
    }

    /// Load marketplace.json from a URL
    /// Tries Claude Code standard location (.claude-plugin/marketplace.json) first, then root (marketplace.json)
    async fn load_marketplace_from_url_with_branch(
        &self,
        base_url: &str,
        branch: Option<&str>,
        source_name: &str,
    ) -> Result<Vec<SkillInfo>, SourcesError> {
        // Construct marketplace.json URLs (try both locations)
        // Priority 1: Claude Code standard location (.claude-plugin/marketplace.json)
        // Priority 2: Root location (marketplace.json)
        let branch_name = branch.unwrap_or("main");
        let (claude_plugin_url, root_url) =
            if base_url.contains("github.com") && !base_url.contains("raw.githubusercontent.com") {
                // Convert GitHub URL to raw content URL
                let repo_path = base_url
                    .trim_start_matches("https://github.com/")
                    .trim_start_matches("http://github.com/")
                    .trim_end_matches(".git")
                    .trim_end_matches('/');
                (
                    format!(
                        "https://raw.githubusercontent.com/{}/{}/.claude-plugin/marketplace.json",
                        repo_path, branch_name
                    ),
                    format!(
                        "https://raw.githubusercontent.com/{}/{}/marketplace.json",
                        repo_path, branch_name
                    ),
                )
            } else {
                let base = if base_url.ends_with('/') {
                    base_url.to_string()
                } else {
                    format!("{}/", base_url)
                };
                (
                    format!("{}.claude-plugin/marketplace.json", base),
                    format!("{}marketplace.json", base),
                )
            };

        // Check cache first (try both URLs)
        let cache_key = claude_plugin_url.clone();
        {
            let cache = self.marketplace_cache.read().await;
            if let Some(cached) = cache.get(&cache_key) {
                if !cached.is_expired() {
                    return Ok(cached
                        .data
                        .skills
                        .iter()
                        .map(|skill| SkillInfo {
                            id: skill.id.clone(),
                            name: skill.name.clone(),
                            description: skill.description.clone(),
                            version: Some(skill.version.clone()),
                            source_name: source_name.to_string(),
                        })
                        .collect());
                }
            }
            // Also check root URL cache
            if let Some(cached) = cache.get(&root_url) {
                if !cached.is_expired() {
                    return Ok(cached
                        .data
                        .skills
                        .iter()
                        .map(|skill| SkillInfo {
                            id: skill.id.clone(),
                            name: skill.name.clone(),
                            description: skill.description.clone(),
                            version: Some(skill.version.clone()),
                            source_name: source_name.to_string(),
                        })
                        .collect());
                }
            }
        }

        // Try Claude Code standard location first
        let (marketplace, successful_url) = match self
            .try_fetch_marketplace(&claude_plugin_url, Some(base_url))
            .await
        {
            Ok(m) => {
                tracing::debug!(
                    "Loaded marketplace.json from Claude Code standard location: {}",
                    claude_plugin_url
                );
                (m, claude_plugin_url.clone())
            }
            Err(e) => {
                // Fall back to root location
                tracing::debug!(
                    "Claude Code location failed ({}), trying root location: {}",
                    e,
                    root_url
                );
                match self.try_fetch_marketplace(&root_url, Some(base_url)).await {
                    Ok(m) => {
                        tracing::debug!("Loaded marketplace.json from root location: {}", root_url);
                        (m, root_url.clone())
                    }
                    Err(e2) => {
                        return Err(SourcesError::Network(format!(
                            "Failed to fetch marketplace.json from both locations. Claude Code location (.claude-plugin/marketplace.json): {}. Root location (marketplace.json): {}",
                            e, e2
                        )));
                    }
                }
            }
        };

        {
            let mut cache = self.marketplace_cache.write().await;
            cache.insert(
                successful_url.clone(),
                CachedMarketplace {
                    data: marketplace.clone(),
                    fetched_at: Utc::now(),
                    ttl_seconds: self.cache_ttl_seconds,
                },
            );
        }

        // Convert to SkillInfo
        Ok(marketplace
            .skills
            .iter()
            .map(|skill| SkillInfo {
                id: skill.id.clone(),
                name: skill.name.clone(),
                description: skill.description.clone(),
                version: Some(skill.version.clone()),
                source_name: source_name.to_string(),
            })
            .collect())
    }

    /// Get marketplace.json for a specific source
    /// Tries Claude Code standard location (.claude-plugin/marketplace.json) first, then root (marketplace.json)
    pub async fn get_marketplace_json(
        &self,
        source_name: &str,
    ) -> Result<MarketplaceJson, SourcesError> {
        let source_def = self
            .sources
            .get(source_name)
            .ok_or_else(|| SourcesError::SourceNotFound(source_name.to_string()))?;

        let (base_url, branch) = match &source_def.source {
            SourceConfig::Git { url, branch, .. } => {
                (url.as_str(), branch.as_deref().unwrap_or("main"))
            }
            SourceConfig::ZipUrl { base_url, .. } => (base_url.as_str(), ""),
            SourceConfig::Local { .. } => {
                return Err(SourcesError::Network(
                    "Local sources do not support marketplace.json".to_string(),
                ));
            }
        };

        // Construct marketplace.json URLs (try both locations)
        // Priority 1: Claude Code standard location (.claude-plugin/marketplace.json)
        // Priority 2: Root location (marketplace.json)
        let (claude_plugin_url, root_url) =
            if base_url.contains("github.com") && !base_url.contains("raw.githubusercontent.com") {
                // Convert GitHub URL to raw content URL
                let repo_path = base_url
                    .trim_start_matches("https://github.com/")
                    .trim_start_matches("http://github.com/")
                    .trim_end_matches(".git")
                    .trim_end_matches('/');
                (
                    format!(
                        "https://raw.githubusercontent.com/{}/{}/.claude-plugin/marketplace.json",
                        repo_path, branch
                    ),
                    format!(
                        "https://raw.githubusercontent.com/{}/{}/marketplace.json",
                        repo_path, branch
                    ),
                )
            } else {
                let base = if base_url.ends_with('/') {
                    base_url.to_string()
                } else {
                    format!("{}/", base_url)
                };
                (
                    format!("{}.claude-plugin/marketplace.json", base),
                    format!("{}marketplace.json", base),
                )
            };

        // Check cache first (try both URLs)
        {
            let cache = self.marketplace_cache.read().await;
            if let Some(cached) = cache.get(&claude_plugin_url) {
                if !cached.is_expired() {
                    return Ok(cached.data.clone());
                }
            }
            // Also check root URL cache
            if let Some(cached) = cache.get(&root_url) {
                if !cached.is_expired() {
                    return Ok(cached.data.clone());
                }
            }
        }

        // Try Claude Code standard location first
        let (marketplace, successful_url) = match self
            .try_fetch_marketplace(&claude_plugin_url, Some(base_url))
            .await
        {
            Ok(m) => {
                tracing::debug!(
                    "Loaded marketplace.json from Claude Code standard location: {}",
                    claude_plugin_url
                );
                (m, claude_plugin_url.clone())
            }
            Err(e) => {
                // Fall back to root location
                tracing::debug!(
                    "Claude Code location failed ({}), trying root location: {}",
                    e,
                    root_url
                );
                match self.try_fetch_marketplace(&root_url, Some(base_url)).await {
                    Ok(m) => {
                        tracing::debug!("Loaded marketplace.json from root location: {}", root_url);
                        (m, root_url.clone())
                    }
                    Err(e2) => {
                        return Err(SourcesError::Network(format!(
                            "Failed to fetch marketplace.json from both locations. Claude Code location (.claude-plugin/marketplace.json): {}. Root location (marketplace.json): {}",
                            e, e2
                        )));
                    }
                }
            }
        };

        // Cache the result (use the URL that succeeded)
        {
            let mut cache = self.marketplace_cache.write().await;
            cache.insert(
                successful_url.clone(),
                CachedMarketplace {
                    data: marketplace.clone(),
                    fetched_at: Utc::now(),
                    ttl_seconds: self.cache_ttl_seconds,
                },
            );
        }

        Ok(marketplace)
    }

    /// Scan a local directory for skills
    async fn scan_local_source(
        &self,
        path: &PathBuf,
        source_name: &str,
    ) -> Result<Vec<SkillInfo>, SourcesError> {
        use walkdir::WalkDir;

        let resolved_path = if path.is_absolute() {
            path.clone()
        } else {
            // Resolve relative to current directory
            std::env::current_dir()
                .map_err(SourcesError::Io)?
                .join(path)
        };

        if !resolved_path.exists() {
            return Err(SourcesError::NotFound(resolved_path));
        }

        if !resolved_path.is_dir() {
            return Err(SourcesError::Io(std::io::Error::new(
                std::io::ErrorKind::NotADirectory,
                format!("Path is not a directory: {}", resolved_path.display()),
            )));
        }

        let mut skills = Vec::new();

        // Walk directory recursively
        for entry in WalkDir::new(&resolved_path)
            .into_iter()
            .filter_map(|e| e.ok())
        {
            let entry_path = entry.path();
            if entry_path.is_file()
                && entry_path.file_name() == Some(std::ffi::OsStr::new("SKILL.md"))
            {
                // Found a skill directory
                if let Some(skill_dir) = entry_path.parent() {
                    // Try to extract skill metadata
                    if let Ok(skill_info) =
                        self.extract_skill_info_from_path(skill_dir, source_name)
                    {
                        skills.push(skill_info);
                    }
                }
            }
        }

        Ok(skills)
    }

    /// Extract skill information from a local path
    fn extract_skill_info_from_path(
        &self,
        skill_path: &Path,
        source_name: &str,
    ) -> Result<SkillInfo, SourcesError> {
        use std::fs;

        let skill_file = skill_path.join("SKILL.md");
        if !skill_file.exists() {
            return Err(SourcesError::NotFound(skill_file));
        }

        // Read SKILL.md to extract metadata
        let content = fs::read_to_string(&skill_file).map_err(SourcesError::Io)?;

        // Extract frontmatter (simple YAML frontmatter parser)
        let (id, name, description, version) =
            self.parse_skill_frontmatter(&content, skill_path)?;

        Ok(SkillInfo {
            id,
            name,
            description,
            version: Some(version),
            source_name: source_name.to_string(),
        })
    }

    /// Parse YAML frontmatter from SKILL.md
    fn parse_skill_frontmatter(
        &self,
        content: &str,
        skill_path: &Path,
    ) -> Result<(String, String, String, String), SourcesError> {
        // Simple frontmatter parser - look for --- delimited YAML
        if !content.starts_with("---\n") {
            // No frontmatter, use directory name as fallback
            let id = skill_path
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("unknown")
                .to_string();
            return Ok((
                id.clone(),
                id.clone(),
                "No description".to_string(),
                "1.0.0".to_string(),
            ));
        }

        // Find end of frontmatter
        let end_marker = content[4..]
            .find("---\n")
            .ok_or_else(|| SourcesError::Parse("Invalid frontmatter format".to_string()))?;

        let frontmatter = &content[4..end_marker + 4];

        // Simple YAML parsing - just extract key fields
        let mut id = None;
        let mut name = None;
        let mut description = None;
        let mut version = None;

        for line in frontmatter.lines() {
            let line = line.trim();
            if line.starts_with("id:") {
                id = line
                    .split(':')
                    .nth(1)
                    .map(|s| s.trim().trim_matches('"').to_string());
            } else if line.starts_with("name:") {
                name = line
                    .split(':')
                    .nth(1)
                    .map(|s| s.trim().trim_matches('"').to_string());
            } else if line.starts_with("description:") {
                description = line
                    .split(':')
                    .nth(1)
                    .map(|s| s.trim().trim_matches('"').to_string());
            } else if line.starts_with("version:") {
                version = line
                    .split(':')
                    .nth(1)
                    .map(|s| s.trim().trim_matches('"').to_string());
            }
        }

        // Use directory name as fallback for id
        let skill_id = id.unwrap_or_else(|| {
            skill_path
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("unknown")
                .to_string()
        });

        Ok((
            skill_id.clone(),
            name.unwrap_or(skill_id.clone()),
            description.unwrap_or_else(|| "No description".to_string()),
            version.unwrap_or_else(|| "1.0.0".to_string()),
        ))
    }
}

impl SourcesConfig {
    /// Load sources config from TOML file
    pub fn load_from_file(path: &Path) -> Result<Self, SourcesError> {
        if !path.exists() {
            return Err(SourcesError::NotFound(path.to_path_buf()));
        }

        let content = std::fs::read_to_string(path).map_err(SourcesError::Io)?;

        let config: SourcesConfig =
            toml::from_str(&content).map_err(|e| SourcesError::Parse(e.to_string()))?;

        Ok(config)
    }

    /// Save sources config to TOML file
    pub fn save_to_file(&self, path: &Path) -> Result<(), SourcesError> {
        let content =
            toml::to_string_pretty(self).map_err(|e| SourcesError::Serialize(e.to_string()))?;

        std::fs::write(path, content).map_err(SourcesError::Io)?;

        Ok(())
    }
}

/// Sources-related errors
#[derive(Debug, thiserror::Error)]
pub enum SourcesError {
    #[error("Sources config file not found: {0}")]
    NotFound(PathBuf),

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

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

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

    #[error("Source already exists: {0}")]
    AlreadyExists(String),

    #[error("Source not found: {0}")]
    SourceNotFound(String),

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

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

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

    #[test]
    fn test_sources_config_parsing() {
        let toml_content = r#"
            [[sources]]
            name = "team-tools"
            source = { type = "git", url = "https://github.com/org/team-plugins.git", branch = "main" }

            [[sources]]
            name = "official-skills"
            source = { type = "zip-url", base_url = "https://skills.example.com/" }

            [[sources]]
            name = "local"
            source = { type = "local", path = "./local-sources" }
        "#;

        let config: SourcesConfig = toml::from_str(toml_content).unwrap();

        assert_eq!(config.sources.len(), 3);
        assert_eq!(config.sources[0].name, "team-tools");
        assert_eq!(config.sources[1].name, "official-skills");
        assert_eq!(config.sources[2].name, "local");
    }

    #[test]
    fn test_source_config_variants() {
        // Test Git source
        let git_config = SourceConfig::Git {
            url: "https://github.com/org/repo.git".to_string(),
            branch: Some("main".to_string()),
            tag: None,
            auth: None,
        };

        // Test ZipUrl source
        let zip_config = SourceConfig::ZipUrl {
            base_url: "https://skills.example.com/".to_string(),
            auth: None,
        };

        // Test Local source
        let local_config = SourceConfig::Local {
            path: PathBuf::from("./local-sources"),
        };

        // Verify they serialize correctly
        let git_toml = toml::to_string(&git_config).unwrap();
        assert!(git_toml.contains("type = \"git\""));

        let zip_toml = toml::to_string(&zip_config).unwrap();
        assert!(zip_toml.contains("type = \"zip-url\""));

        let local_toml = toml::to_string(&local_config).unwrap();
        assert!(local_toml.contains("type = \"local\""));
    }

    #[tokio::test]
    async fn test_sources_manager() {
        let temp_file = NamedTempFile::new().unwrap();
        let config_path = temp_file.path().to_path_buf();

        let mut manager = SourcesManager::new(config_path.clone());

        // Load (should create empty config)
        manager.load().unwrap();
        assert_eq!(manager.list_sources().len(), 0);

        // Add sources
        manager
            .add_source(
                "team-tools".to_string(),
                SourceConfig::Git {
                    url: "https://github.com/org/team-plugins.git".to_string(),
                    branch: Some("main".to_string()),
                    tag: None,
                    auth: None,
                },
            )
            .unwrap();

        manager
            .add_source(
                "official".to_string(),
                SourceConfig::ZipUrl {
                    base_url: "https://skills.example.com/".to_string(),
                    auth: None,
                },
            )
            .unwrap();

        assert_eq!(manager.list_sources().len(), 2);

        // Save and reload
        manager.save().unwrap();

        let mut new_manager = SourcesManager::new(config_path);
        new_manager.load().unwrap();
        assert_eq!(new_manager.list_sources().len(), 2);
    }
}