agent_hub_core/
skill_source.rs1use std::{
4 collections::HashMap,
5 io::Read,
6 sync::{Arc, Mutex},
7};
8
9use anyhow::{anyhow, ensure, Result};
10use serde::{Deserialize, Serialize};
11
12#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
14pub struct SkillSummary {
15 pub id: String,
16 pub name: String,
17 pub description: Option<String>,
18}
19
20#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
22pub struct SkillVersion {
23 pub version: String,
24 pub sha256: Option<String>,
26}
27
28#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
30pub struct SkillSearchPage {
31 pub skills: Vec<SkillSummary>,
32 pub next_cursor: Option<String>,
33}
34
35pub trait SkillSource: Send + Sync {
40 fn search(&self, query: &str, cursor: Option<&str>) -> Result<SkillSearchPage>;
42
43 fn versions(&self, skill_id: &str) -> Result<Vec<SkillVersion>>;
45
46 fn download(&self, skill_id: &str, version: &str) -> Result<Box<dyn Read + Send>>;
48}
49
50#[derive(Default)]
52pub struct SkillSourceRegistry {
53 sources: Mutex<HashMap<String, Arc<dyn SkillSource>>>,
54}
55
56pub fn valid_source_id(id: &str) -> bool {
58 !id.is_empty()
59 && id
60 .bytes()
61 .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
62}
63
64#[cfg(test)]
65mod tests;
66
67impl SkillSourceRegistry {
68 pub fn register(&self, id: &str, source: impl SkillSource + 'static) -> Result<()> {
73 ensure!(valid_source_id(id), "Invalid skill catalog ID");
74 let mut sources = self
75 .sources
76 .lock()
77 .map_err(|_| anyhow!("Skill catalog registry is unavailable"))?;
78 ensure!(
79 !sources.contains_key(id),
80 "Skill catalog is already registered"
81 );
82 sources.insert(id.into(), Arc::new(source));
83 Ok(())
84 }
85
86 pub fn get(&self, id: &str) -> Result<Option<Arc<dyn SkillSource>>> {
88 let sources = self
89 .sources
90 .lock()
91 .map_err(|_| anyhow!("Skill catalog registry is unavailable"))?;
92 Ok(sources.get(id).cloned())
93 }
94}