Skip to main content

base/interface/
skill_provider.rs

1//! `SkillProvider` — where a session's skills come from.
2//!
3//! Skills arrive from three places: `SKILL.md` files under the three
4//! directory tiers, the built-ins compiled into the binary, and the tools of
5//! every connected MCP server turned into invocable skills. Each is a
6//! `SkillProvider` handed to `SkillManager::load_from`, so a deployment that
7//! keeps its skills anywhere else (a database, a company-wide bundle server,
8//! a git remote) adds one more source rather than a method on the manager.
9//!
10//! # A source owns its bodies too
11//!
12//! Listing a skill and expanding it are two different reads, and only the
13//! first one is satisfied by metadata. The built-in sources that hold their
14//! content in memory said so through a sentinel `path` — `(bundled:name)`,
15//! `(mcp:server:tool)` — that the manager pattern-matched on. A provider says
16//! it directly instead, via [`SkillProvider::body`], so a source whose skills
17//! are not files is a complete source rather than one that lists skills
18//! nobody can run.
19
20use crate::skill::SkillEntry;
21
22/// What a provider does about a name that is already loaded.
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
24pub enum SkillPrecedence {
25    /// Replace what is there.
26    Override,
27    /// Leave what is there and register only the names nobody claimed.
28    Fallback,
29}
30
31/// A place skills come from.
32pub trait SkillProvider: Send + Sync {
33    /// Stable identifier, for logs and for a host that wants to find its own
34    /// source again.
35    fn id(&self) -> &str;
36
37    /// What this source does about a name another source already registered.
38    ///
39    /// The engine's own answer is that the disk wins: a project that writes
40    /// its own `review` skill means to override the built-in one.
41    fn precedence(&self) -> SkillPrecedence {
42        SkillPrecedence::Fallback
43    }
44
45    /// Everything this source has, as of now.
46    fn skills(&self) -> Vec<SkillEntry>;
47
48    /// The full text of one of this source's skills, when the source holds it
49    /// rather than a file does. `None` means "read the entry's `path`", which
50    /// is the right answer for any source backed by the filesystem.
51    fn body(&self, _name: &str) -> Option<String> {
52        None
53    }
54}
55
56/// A fixed list of skills held in memory.
57///
58/// The shape an embedding program reaches for: it already has the skills —
59/// fetched from its own service, generated from its own configuration, or
60/// written inline in the host binary — and wants them in the catalog without
61/// first writing them to a directory for the engine to scan back.
62pub struct StaticSkills {
63    id: String,
64    entries: Vec<SkillEntry>,
65    bodies: std::collections::HashMap<String, String>,
66    precedence: SkillPrecedence,
67}
68
69impl StaticSkills {
70    pub fn new(id: impl Into<String>, entries: Vec<SkillEntry>) -> Self {
71        Self {
72            id: id.into(),
73            entries,
74            bodies: std::collections::HashMap::new(),
75            precedence: SkillPrecedence::Fallback,
76        }
77    }
78
79    /// Supply the text for one skill, for entries whose `path` points at
80    /// nothing readable.
81    pub fn with_body(mut self, name: impl Into<String>, body: impl Into<String>) -> Self {
82        self.bodies.insert(name.into(), body.into());
83        self
84    }
85
86    pub fn overriding(mut self) -> Self {
87        self.precedence = SkillPrecedence::Override;
88        self
89    }
90}
91
92impl SkillProvider for StaticSkills {
93    fn id(&self) -> &str {
94        &self.id
95    }
96
97    fn precedence(&self) -> SkillPrecedence {
98        self.precedence
99    }
100
101    fn skills(&self) -> Vec<SkillEntry> {
102        self.entries.clone()
103    }
104
105    fn body(&self, name: &str) -> Option<String> {
106        self.bodies.get(name).cloned()
107    }
108}
109
110#[cfg(test)]
111mod tests {
112    use super::*;
113
114    fn entry(name: &str) -> SkillEntry {
115        SkillEntry {
116            name: name.to_string(),
117            description: format!("{name} description"),
118            ..Default::default()
119        }
120    }
121
122    #[test]
123    fn a_static_source_carries_its_own_bodies() {
124        let source = StaticSkills::new("host", vec![entry("deploy")])
125            .with_body("deploy", "run the deploy checklist");
126        assert_eq!(source.id(), "host");
127        assert_eq!(source.skills().len(), 1);
128        assert_eq!(
129            source.body("deploy").as_deref(),
130            Some("run the deploy checklist")
131        );
132        assert!(source.body("nothing").is_none());
133    }
134
135    #[test]
136    fn a_source_defers_to_what_is_already_loaded_unless_it_says_otherwise() {
137        assert_eq!(
138            StaticSkills::new("host", vec![]).precedence(),
139            SkillPrecedence::Fallback
140        );
141        assert_eq!(
142            StaticSkills::new("host", vec![]).overriding().precedence(),
143            SkillPrecedence::Override
144        );
145    }
146}