Skip to main content

base/interface/
skill_catalog.rs

1//! `SkillCatalog` — the skills a session can list, invoke and be reminded of.
2//!
3//! The turn loop asks this, not the skills module, which skills exist, what
4//! one expands to, and how often each has been used. A session assembled
5//! without skills answers from [`NoSkills`].
6
7use std::path::PathBuf;
8
9use futures::future::BoxFuture;
10
11use crate::interface::tool::ToolContext;
12use crate::skill::SkillEntry;
13
14/// One invocation's worth of a skill: the text the conversation receives,
15/// and the `(tool, pattern)` grants its `allowed-tools` makes for the rest of
16/// the turn — already narrowed to what its source may grant.
17#[derive(Debug, Clone, Default, PartialEq, Eq)]
18pub struct SkillExpansion {
19    pub text: String,
20    pub grants: Vec<(String, Option<String>)>,
21}
22
23pub trait SkillCatalog: Send + Sync {
24    /// Sorted by name, so the listing a prompt carries is stable.
25    fn list(&self) -> Vec<SkillEntry>;
26    fn get(&self, name: &str) -> Option<SkillEntry>;
27    /// The body a skill expands to when invoked.
28    fn body(&self, name: &str) -> Option<String>;
29    /// Expand `name` for an invocation with `args`, running what its body
30    /// asks to run through `ctx.inline_commands`. `None` when the catalog does
31    /// not hold it; `Err` is a message for the model, such as a bundled file
32    /// that escapes the skill's folder.
33    fn expand<'a>(
34        &'a self,
35        name: &'a str,
36        args: &'a str,
37        ctx: &'a ToolContext,
38    ) -> BoxFuture<'a, Option<Result<SkillExpansion, String>>> {
39        Box::pin(async move {
40            let entry = self.get(name)?;
41            let body = self.body(name)?;
42            let text = crate::skill::expand_skill_vars_named(
43                &body,
44                args,
45                entry.arguments.as_deref().unwrap_or(&[]),
46                Some(&crate::skill::InvocationVars {
47                    skill_md: &entry.path,
48                    session_id: Some(&ctx.session_id),
49                }),
50            );
51            Some(Ok(SkillExpansion {
52                text,
53                grants: Vec::new(),
54            }))
55        })
56    }
57    /// Reloads skills whose files changed since the last call; how many did.
58    fn reload_changed(&self) -> usize;
59    /// Skills found under `paths` that the catalog did not hold yet.
60    fn discover_for_paths(&self, paths: &[PathBuf]) -> Vec<SkillEntry>;
61    /// Conditional skills whose `paths` patterns match one of `files`, newly
62    /// made available.
63    fn activate_for_paths(&self, files: &[PathBuf]) -> Vec<SkillEntry>;
64    fn invocation_count(&self, name: &str) -> u32;
65    /// A sequence number that orders invocations; 0 for one never invoked.
66    fn last_invoked_seq(&self, name: &str) -> u64;
67}
68
69pub struct NoSkills;
70
71impl SkillCatalog for NoSkills {
72    fn list(&self) -> Vec<SkillEntry> {
73        Vec::new()
74    }
75    fn get(&self, _: &str) -> Option<SkillEntry> {
76        None
77    }
78    fn body(&self, _: &str) -> Option<String> {
79        None
80    }
81    fn reload_changed(&self) -> usize {
82        0
83    }
84    fn discover_for_paths(&self, _: &[PathBuf]) -> Vec<SkillEntry> {
85        Vec::new()
86    }
87    fn activate_for_paths(&self, _: &[PathBuf]) -> Vec<SkillEntry> {
88        Vec::new()
89    }
90    fn invocation_count(&self, _: &str) -> u32 {
91        0
92    }
93    fn last_invoked_seq(&self, _: &str) -> u64 {
94        0
95    }
96}