Skip to main content

agent_hub_core/
skill_source.rs

1//! Contract for a catalog of versioned skills, shared by desktop adapters.
2
3use std::{
4    collections::HashMap,
5    io::Read,
6    sync::{Arc, Mutex},
7};
8
9use anyhow::{anyhow, ensure, Result};
10use serde::{Deserialize, Serialize};
11
12/// Catalog entry. The ID belongs to its source and is opaque to the host.
13#[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/// A specific revision of a catalog entry. Versions are opaque source labels.
21#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
22pub struct SkillVersion {
23    pub version: String,
24    /// Optional SHA-256 of the ZIP bytes, as 64 hexadecimal characters.
25    pub sha256: Option<String>,
26}
27
28/// A bounded page from the catalog. Pass the opaque cursor to request more.
29#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
30pub struct SkillSearchPage {
31    pub skills: Vec<SkillSummary>,
32    pub next_cursor: Option<String>,
33}
34
35/// A blocking catalog adapter. A desktop caller runs these calls off the UI thread.
36///
37/// An adapter owns its transport and credentials. IDs, versions and cursors
38/// must be passed to the provider as data. They are not local paths.
39pub trait SkillSource: Send + Sync {
40    /// Search the provider and return one bounded page.
41    fn search(&self, query: &str, cursor: Option<&str>) -> Result<SkillSearchPage>;
42
43    /// List the available versions for a provider-specific skill ID.
44    fn versions(&self, skill_id: &str) -> Result<Vec<SkillVersion>>;
45
46    /// Stream the ZIP for exactly the requested version, even when newer versions exist.
47    fn download(&self, skill_id: &str, version: &str) -> Result<Box<dyn Read + Send>>;
48}
49
50/// Sources registered by trusted desktop extensions at startup.
51#[derive(Default)]
52pub struct SkillSourceRegistry {
53    sources: Mutex<HashMap<String, Arc<dyn SkillSource>>>,
54}
55
56/// Check the syntax of a durable catalog identifier.
57pub 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    /// Register one adapter under a stable, non-secret identifier.
69    ///
70    /// Duplicate identifiers are rejected so an extension cannot silently
71    /// replace the source referenced by existing installations.
72    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    /// Look up an adapter without holding the registry lock during its I/O.
87    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}