skillfile-sources 1.5.0

Source fetching and caching for skillfile: resolver, strategies, sync
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
//! Registry client for searching community skills and agents.
//!
//! Queries multiple registries (agentskill.sh, skills.sh, skillhub.club) for
//! published skills and agents. Each registry implements the [`Registry`] trait,
//! and results are aggregated into a unified [`SearchResponse`].
//!
//! # Example
//!
//! ```no_run
//! use skillfile_sources::registry::{search_all, SearchOptions};
//!
//! let results = search_all("code review", &SearchOptions::default()).unwrap();
//! for r in &results.items {
//!     println!("{} ({}): {}", r.name, r.registry.as_str(), r.description.as_deref().unwrap_or(""));
//! }
//! ```

pub mod agentskill;
mod scrape;
mod skillhub;
mod skillssh;

#[cfg(test)]
pub(crate) mod test_support;

use crate::http::{HttpClient, UreqClient};
use skillfile_core::error::SkillfileError;

// Re-export registry implementations for `all_registries()` and `search_registry_with_client()`.
use agentskill::AgentskillSh;
use skillhub::SkillhubClub;
use skillssh::SkillsSh;

// Re-export the detail API from the agentskill module.
pub use agentskill::{
    fetch_agentskill_github_meta, scrape_github_meta_from_page, AgentskillGithubMeta,
};

// ===========================================================================
// Public types
// ===========================================================================

/// Identifies which registry a search result came from.
///
/// Replaces raw strings with a closed enum so registry-specific logic
/// (colors, audit support, display names) can be matched exhaustively
/// instead of branching on stringly-typed values.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize)]
pub enum RegistryId {
    #[serde(rename = "agentskill.sh")]
    AgentskillSh,
    #[serde(rename = "skills.sh")]
    SkillsSh,
    #[serde(rename = "skillhub.club")]
    SkillhubClub,
}

impl RegistryId {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::AgentskillSh => "agentskill.sh",
            Self::SkillsSh => "skills.sh",
            Self::SkillhubClub => "skillhub.club",
        }
    }

    /// Whether this registry provides per-skill security audit results
    /// (fetched from the skill's HTML page).
    pub fn has_security_audits(&self) -> bool {
        matches!(self, Self::SkillsSh)
    }
}

impl std::fmt::Display for RegistryId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

impl std::str::FromStr for RegistryId {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "agentskill.sh" => Ok(Self::AgentskillSh),
            "skills.sh" => Ok(Self::SkillsSh),
            "skillhub.club" => Ok(Self::SkillhubClub),
            _ => Err(format!("unknown registry: {s}")),
        }
    }
}

#[derive(Debug, Clone)]
pub struct SearchOptions {
    /// Maximum number of results to return.
    pub limit: usize,
    /// Minimum security score (0-100). `None` means no filter.
    pub min_score: Option<u8>,
}

impl Default for SearchOptions {
    fn default() -> Self {
        Self {
            limit: 20,
            min_score: None,
        }
    }
}

#[derive(Debug, Clone, serde::Serialize)]
pub struct SearchResult {
    pub name: String,
    pub owner: String,
    pub description: Option<String>,
    pub security_score: Option<u8>,
    pub stars: Option<u32>,
    pub url: String,
    pub registry: RegistryId,
    /// GitHub `owner/repo` if known from the registry metadata.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source_repo: Option<String>,
    /// Path within the GitHub repo (e.g. `skills/foo/SKILL.md`), if known.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source_path: Option<String>,
}

#[derive(Debug, Clone, serde::Serialize)]
pub struct SearchResponse {
    /// Matching results (up to `limit`).
    pub items: Vec<SearchResult>,
    /// Total number of matches across queried registries.
    pub total: usize,
}

// ===========================================================================
// Registry trait
// ===========================================================================

pub(crate) struct SearchQuery<'a> {
    pub client: &'a dyn HttpClient,
    pub query: &'a str,
    pub opts: &'a SearchOptions,
}

pub(crate) trait Registry: Send + Sync {
    fn name(&self) -> &str;

    fn search(&self, q: &SearchQuery<'_>) -> Result<SearchResponse, SkillfileError>;

    /// Each registry extracts the content from its own data source:
    /// agentskill.sh scrapes Nuxt hydration data, skills.sh fetches from
    /// `raw.githubusercontent.com`. Default returns `None` (not supported).
    fn fetch_skill_content(
        &self,
        _client: &dyn HttpClient,
        _item: &SearchResult,
    ) -> Option<String> {
        None
    }
}

/// Returns registries to query by default (public, no auth required).
pub(crate) fn all_registries() -> Vec<Box<dyn Registry>> {
    let mut regs: Vec<Box<dyn Registry>> = vec![Box::new(AgentskillSh), Box::new(SkillsSh)];
    // skillhub.club requires an API key — only include when configured.
    if std::env::var("SKILLHUB_API_KEY").is_ok_and(|k| !k.is_empty()) {
        regs.push(Box::new(SkillhubClub));
    }
    regs
}

/// Valid registry names for `--registry` flag validation.
pub const REGISTRY_NAMES: &[&str] = &["agentskill.sh", "skills.sh", "skillhub.club"];

// ===========================================================================
// Public search functions
// ===========================================================================

/// Iterates over all registries, collecting results (skipping registries that
/// fail with a warning), applies `min_score` filter, and returns combined
/// results.
pub fn search_all(query: &str, opts: &SearchOptions) -> Result<SearchResponse, SkillfileError> {
    let client = UreqClient::new();
    search_all_with_client(&client, query, opts)
}

/// Search all registries using an injected HTTP client (for testing).
pub fn search_all_with_client(
    client: &dyn HttpClient,
    query: &str,
    opts: &SearchOptions,
) -> Result<SearchResponse, SkillfileError> {
    let registries = all_registries();
    let mut all_items = Vec::new();
    let mut total = 0;

    for reg in &registries {
        match reg.search(&SearchQuery {
            client,
            query,
            opts,
        }) {
            Ok(resp) => {
                total += resp.total;
                all_items.extend(resp.items);
            }
            Err(e) => {
                eprintln!("warning: {} search failed: {e}", reg.name());
            }
        }
    }

    let mut resp = SearchResponse {
        items: all_items,
        total,
    };
    post_process(&mut resp, opts);

    Ok(resp)
}

/// Search a single registry by name.
///
/// Returns an error if the registry name is not recognized.
pub fn search_registry(
    registry_name: &str,
    query: &str,
    opts: &SearchOptions,
) -> Result<SearchResponse, SkillfileError> {
    let client = UreqClient::new();
    search_registry_with_client(
        registry_name,
        &SearchQuery {
            client: &client,
            query,
            opts,
        },
    )
}

/// Search a single registry by name using an injected HTTP client (for testing).
pub(crate) fn search_registry_with_client(
    registry_name: &str,
    q: &SearchQuery<'_>,
) -> Result<SearchResponse, SkillfileError> {
    let reg: Box<dyn Registry> = match registry_name {
        "agentskill.sh" => Box::new(AgentskillSh),
        "skills.sh" => Box::new(SkillsSh),
        "skillhub.club" => Box::new(SkillhubClub),
        _ => {
            return Err(SkillfileError::Manifest(format!(
                "unknown registry '{registry_name}'. Valid registries: {}",
                REGISTRY_NAMES.join(", ")
            )));
        }
    };

    let mut resp = reg.search(q)?;
    post_process(&mut resp, q.opts);

    Ok(resp)
}

/// Fetch raw SKILL.md content for a search result.
///
/// Dispatches to the correct registry's content fetcher based on
/// `item.registry`. Returns `None` if the registry doesn't support
/// content fetching or if the fetch fails.
pub fn fetch_skill_content_for(item: &SearchResult) -> Option<String> {
    let client = UreqClient::new();
    match item.registry {
        RegistryId::AgentskillSh => AgentskillSh.fetch_skill_content(&client, item),
        RegistryId::SkillsSh => SkillsSh.fetch_skill_content(&client, item),
        RegistryId::SkillhubClub => None,
    }
}

/// Backward-compatible entry point — searches agentskill.sh only.
pub fn search(query: &str, opts: &SearchOptions) -> Result<SearchResponse, SkillfileError> {
    let client = UreqClient::new();
    search_with_client(&client, query, opts)
}

/// Search agentskill.sh using an injected HTTP client (for testing).
pub fn search_with_client(
    client: &dyn HttpClient,
    query: &str,
    opts: &SearchOptions,
) -> Result<SearchResponse, SkillfileError> {
    let reg = AgentskillSh;
    let mut resp = reg.search(&SearchQuery {
        client,
        query,
        opts,
    })?;
    post_process(&mut resp, opts);

    Ok(resp)
}

// ===========================================================================
// Helpers
// ===========================================================================

/// Apply post-processing to search results: filter by `min_score`, sort by
/// popularity, and truncate to `limit`.
///
/// Every public search function (`search_all`, `search_registry`, `search`)
/// pipes its raw results through this helper so behavior is consistent.
fn post_process(resp: &mut SearchResponse, opts: &SearchOptions) {
    if let Some(min) = opts.min_score {
        resp.items.retain(|r| r.security_score.unwrap_or(0) >= min);
    }
    sort_by_popularity(&mut resp.items);
    resp.items.truncate(opts.limit);
}

/// Sort results by popularity (descending), then by security score (descending).
///
/// Each registry maps its own popularity metric (GitHub stars, install count,
/// etc.) into the common `stars` field. This function sorts on that normalized
/// value so the most popular results appear first regardless of registry.
/// Items without a popularity signal sink to the bottom.
fn sort_by_popularity(items: &mut [SearchResult]) {
    items.sort_by(|a, b| {
        let pop = b.stars.unwrap_or(0).cmp(&a.stars.unwrap_or(0));
        if pop != std::cmp::Ordering::Equal {
            return pop;
        }
        b.security_score
            .unwrap_or(0)
            .cmp(&a.security_score.unwrap_or(0))
    });
}

// ===========================================================================
// Tests — aggregation, sorting, utilities
// ===========================================================================

#[cfg(test)]
#[allow(unsafe_code)]
mod tests {
    use super::*;
    use std::sync::Mutex;
    use test_support::MockClient;

    /// Serializes tests that manipulate the `SKILLHUB_API_KEY` env var.
    static SKILLHUB_ENV_LOCK: Mutex<()> = Mutex::new(());

    fn agentskill_mock_response() -> String {
        r#"{
            "results": [
                {
                    "slug": "alice/code-reviewer",
                    "name": "code-reviewer",
                    "owner": "alice",
                    "description": "Review code changes",
                    "securityScore": 92,
                    "githubStars": 150
                },
                {
                    "slug": "bob/pr-review",
                    "name": "pr-review",
                    "owner": "bob",
                    "description": "Automated PR reviews",
                    "securityScore": 65,
                    "githubStars": 30
                }
            ],
            "total": 2,
            "hasMore": false,
            "totalExact": true
        }"#
        .to_string()
    }

    fn skillssh_mock_response() -> String {
        r#"{
            "query": "docker",
            "searchType": "fuzzy",
            "skills": [
                {
                    "id": "dockerfan/docker-helper/docker-helper",
                    "skillId": "docker-helper",
                    "name": "docker-helper",
                    "installs": 500,
                    "source": "dockerfan/docker-helper"
                },
                {
                    "id": "k8suser/k8s-deploy/k8s-deploy",
                    "skillId": "k8s-deploy",
                    "name": "k8s-deploy",
                    "installs": 200,
                    "source": "k8suser/k8s-deploy"
                }
            ],
            "count": 2,
            "duration_ms": 35
        }"#
        .to_string()
    }

    // -- Aggregation tests ------------------------------------------------------

    #[test]
    fn search_all_aggregates_results() {
        let _guard = SKILLHUB_ENV_LOCK.lock().unwrap();
        unsafe { std::env::remove_var("SKILLHUB_API_KEY") };
        let client = MockClient::new(vec![
            Ok(agentskill_mock_response()),
            Ok(skillssh_mock_response()),
        ]);
        let resp = search_all_with_client(&client, "test", &SearchOptions::default()).unwrap();
        assert_eq!(resp.items.len(), 4);
        let registries: Vec<RegistryId> = resp.items.iter().map(|r| r.registry).collect();
        assert!(registries.contains(&RegistryId::AgentskillSh));
        assert!(registries.contains(&RegistryId::SkillsSh));
    }

    #[test]
    fn search_all_skips_failed_registry() {
        let _guard = SKILLHUB_ENV_LOCK.lock().unwrap();
        unsafe { std::env::remove_var("SKILLHUB_API_KEY") };
        let client = MockClient::new(vec![
            Err("connection refused".to_string()),
            Ok(skillssh_mock_response()),
        ]);
        let resp = search_all_with_client(&client, "test", &SearchOptions::default()).unwrap();
        assert_eq!(resp.items.len(), 2);
        assert_eq!(resp.items[0].registry, RegistryId::SkillsSh);
    }

    #[test]
    fn search_all_applies_min_score_filter() {
        let _guard = SKILLHUB_ENV_LOCK.lock().unwrap();
        unsafe { std::env::remove_var("SKILLHUB_API_KEY") };
        let client = MockClient::new(vec![
            Ok(agentskill_mock_response()),
            Ok(skillssh_mock_response()),
        ]);
        let opts = SearchOptions {
            limit: 10,
            min_score: Some(80),
        };
        let resp = search_all_with_client(&client, "test", &opts).unwrap();
        assert_eq!(resp.items.len(), 1);
        assert_eq!(resp.items[0].name, "code-reviewer");
    }

    #[test]
    fn search_registry_filters_by_name() {
        let client = MockClient::new(vec![Ok(skillssh_mock_response())]);
        let resp = search_registry_with_client(
            "skills.sh",
            &SearchQuery {
                client: &client,
                query: "docker",
                opts: &SearchOptions::default(),
            },
        )
        .unwrap();
        assert_eq!(resp.items.len(), 2);
        assert!(resp
            .items
            .iter()
            .all(|r| r.registry == RegistryId::SkillsSh));
    }

    #[test]
    fn search_registry_rejects_unknown_name() {
        let client = MockClient::new(vec![]);
        let result = search_registry_with_client(
            "nonexistent.io",
            &SearchQuery {
                client: &client,
                query: "test",
                opts: &SearchOptions::default(),
            },
        );
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("unknown registry"), "got: {err}");
    }

    #[test]
    fn search_result_includes_registry_field() {
        let client = MockClient::new(vec![Ok(agentskill_mock_response())]);
        let resp = search_with_client(&client, "test", &SearchOptions::default()).unwrap();
        for item in &resp.items {
            assert_eq!(item.registry, RegistryId::AgentskillSh);
        }
    }

    #[test]
    fn default_search_options() {
        let opts = SearchOptions::default();
        assert_eq!(opts.limit, 20);
        assert!(opts.min_score.is_none());
    }

    #[test]
    fn all_registries_default_excludes_skillhub() {
        let regs = all_registries();
        assert!(regs.len() >= 2);
        assert_eq!(regs[0].name(), "agentskill.sh");
        assert_eq!(regs[1].name(), "skills.sh");
    }

    #[test]
    fn registry_names_covers_all_known() {
        assert_eq!(
            REGISTRY_NAMES,
            &["agentskill.sh", "skills.sh", "skillhub.club"]
        );
    }

    // -- Sorting tests ----------------------------------------------------------

    #[test]
    fn sort_by_popularity_orders_by_stars_desc() {
        let mut items = vec![
            SearchResult {
                name: "low".into(),
                stars: Some(10),
                ..make_result("low")
            },
            SearchResult {
                name: "high".into(),
                stars: Some(500),
                ..make_result("high")
            },
            SearchResult {
                name: "mid".into(),
                stars: Some(100),
                ..make_result("mid")
            },
        ];
        sort_by_popularity(&mut items);
        assert_eq!(items[0].name, "high");
        assert_eq!(items[1].name, "mid");
        assert_eq!(items[2].name, "low");
    }

    #[test]
    fn sort_by_popularity_uses_score_as_tiebreaker() {
        let mut items = vec![
            SearchResult {
                name: "low-score".into(),
                stars: Some(100),
                security_score: Some(50),
                ..make_result("low-score")
            },
            SearchResult {
                name: "high-score".into(),
                stars: Some(100),
                security_score: Some(95),
                ..make_result("high-score")
            },
        ];
        sort_by_popularity(&mut items);
        assert_eq!(items[0].name, "high-score");
        assert_eq!(items[1].name, "low-score");
    }

    #[test]
    fn sort_by_popularity_none_stars_sort_last() {
        let mut items = vec![
            SearchResult {
                name: "no-stars".into(),
                stars: None,
                ..make_result("no-stars")
            },
            SearchResult {
                name: "has-stars".into(),
                stars: Some(1),
                ..make_result("has-stars")
            },
        ];
        sort_by_popularity(&mut items);
        assert_eq!(items[0].name, "has-stars");
        assert_eq!(items[1].name, "no-stars");
    }

    #[test]
    fn search_all_returns_sorted_results() {
        let _guard = SKILLHUB_ENV_LOCK.lock().unwrap();
        unsafe { std::env::remove_var("SKILLHUB_API_KEY") };
        let client = MockClient::new(vec![
            Ok(agentskill_mock_response()),
            Ok(skillssh_mock_response()),
        ]);
        let resp = search_all_with_client(&client, "test", &SearchOptions::default()).unwrap();
        assert_eq!(resp.items[0].name, "docker-helper");
        assert_eq!(resp.items[1].name, "k8s-deploy");
        assert_eq!(resp.items[2].name, "code-reviewer");
        assert_eq!(resp.items[3].name, "pr-review");
    }

    #[test]
    fn search_with_client_sorts_results() {
        let json = r#"{
            "results": [
                {"name": "aaa-low", "owner": "a", "githubStars": 10},
                {"name": "bbb-high", "owner": "b", "githubStars": 500}
            ],
            "total": 2
        }"#;
        let client = MockClient::new(vec![Ok(json.to_string())]);
        let resp = search_with_client(&client, "test", &SearchOptions::default()).unwrap();
        assert_eq!(resp.items[0].name, "bbb-high");
        assert_eq!(resp.items[1].name, "aaa-low");
    }

    #[test]
    fn post_process_filters_and_sorts() {
        let mut resp = SearchResponse {
            total: 3,
            items: vec![
                SearchResult {
                    name: "low-score-low-stars".into(),
                    security_score: Some(30),
                    stars: Some(10),
                    ..make_result("low-score-low-stars")
                },
                SearchResult {
                    name: "high-score-high-stars".into(),
                    security_score: Some(90),
                    stars: Some(500),
                    ..make_result("high-score-high-stars")
                },
                SearchResult {
                    name: "mid-score-mid-stars".into(),
                    security_score: Some(60),
                    stars: Some(100),
                    ..make_result("mid-score-mid-stars")
                },
            ],
        };
        let opts = SearchOptions {
            min_score: Some(50),
            ..Default::default()
        };
        post_process(&mut resp, &opts);

        assert_eq!(resp.items.len(), 2);
        assert_eq!(resp.items[0].name, "high-score-high-stars");
        assert_eq!(resp.items[1].name, "mid-score-mid-stars");
    }

    #[test]
    fn post_process_no_filter_only_sorts() {
        let mut resp = SearchResponse {
            total: 2,
            items: vec![
                SearchResult {
                    name: "few".into(),
                    stars: Some(5),
                    ..make_result("few")
                },
                SearchResult {
                    name: "many".into(),
                    stars: Some(999),
                    ..make_result("many")
                },
            ],
        };
        post_process(&mut resp, &SearchOptions::default());
        assert_eq!(resp.items[0].name, "many");
        assert_eq!(resp.items[1].name, "few");
    }

    #[test]
    fn post_process_truncates_to_limit() {
        let mut resp = SearchResponse {
            total: 5,
            items: (0..5)
                .map(|i| SearchResult {
                    name: format!("item-{i}"),
                    stars: Some(100 - i),
                    ..make_result(&format!("item-{i}"))
                })
                .collect(),
        };
        let opts = SearchOptions {
            limit: 3,
            ..Default::default()
        };
        post_process(&mut resp, &opts);
        assert_eq!(resp.items.len(), 3);
        assert_eq!(resp.items[0].name, "item-0");
        assert_eq!(resp.items[2].name, "item-2");
    }

    /// Helper to create a minimal `SearchResult` for sorting tests.
    fn make_result(name: &str) -> SearchResult {
        SearchResult {
            name: name.to_string(),
            owner: String::new(),
            description: None,
            security_score: None,
            stars: None,
            url: String::new(),
            registry: RegistryId::AgentskillSh,
            source_repo: None,
            source_path: None,
        }
    }
}