Skip to main content

graphrt_core/
skill.rs

1//! Skill contract between a skill store and the nodes that use skills.
2//!
3//! A skill is a package of instructions plus optional reference files and
4//! tool requests, loaded only when a task needs it. This module holds only
5//! the contract: the core never reads a skill from disk. A host registers a
6//! [`Skills`] service (for example the store of the `graphrt-skill` crate)
7//! and the `agent` node asks it what the skills an agent lists resolve to.
8//!
9//! What a node gets for one run is an [`AttachedSkills`]:
10//!
11//! - `catalog`: one short line per skill for the system prompt;
12//! - `runtime`: a tool provider scoped to those skills, with
13//!   [`LOAD_SKILL_TOOL`] and [`READ_SKILL_FILE_TOOL`];
14//! - `grants`: the tools the owner granted each skill.
15//!
16//! A skill never grants itself a tool. The grants come from the store, where
17//! the owner recorded them, and a node offers a granted tool to the model only
18//! after the skill was loaded in that run.
19
20use std::fmt;
21use std::sync::Arc;
22
23use futures::future::BoxFuture;
24
25use crate::tool::ToolRuntime;
26
27/// Provider name of the skill tools in logs and tool references.
28pub const SKILL_PROVIDER: &str = "skills";
29/// Tool that returns the instructions of one skill. Argument: `{"name": "<skill>"}`.
30pub const LOAD_SKILL_TOOL: &str = "load_skill";
31/// Tool that returns one file of a skill. Arguments: `{"name": "<skill>", "path": "<file>"}`.
32pub const READ_SKILL_FILE_TOOL: &str = "read_skill_file";
33
34/// A tool the owner granted to a skill.
35#[derive(Clone, Debug, PartialEq, Eq)]
36pub struct SkillToolGrant {
37    /// The skill the grant belongs to.
38    pub skill: String,
39    /// Tool provider in the [`crate::tool::ToolRegistry`].
40    pub provider: String,
41    /// Tool name, or `*` for every tool of the provider.
42    pub tool: String,
43    /// Whether each call waits for approval first. `true` unless the owner
44    /// chose otherwise.
45    pub ask: bool,
46}
47
48/// What the skills an agent lists resolve to for one run.
49#[derive(Clone, Debug)]
50pub struct AttachedSkills {
51    /// Catalog for the system prompt: how to load a skill, then one line per
52    /// skill. Empty when no listed skill is enabled.
53    pub catalog: String,
54    /// The skill tools, scoped to the attached skills.
55    pub runtime: Arc<dyn ToolRuntime>,
56    /// Tools granted to the attached skills.
57    pub grants: Vec<SkillToolGrant>,
58}
59
60impl AttachedSkills {
61    /// Whether there is nothing to attach (every listed skill is disabled).
62    pub fn is_empty(&self) -> bool {
63        self.catalog.is_empty()
64    }
65}
66
67/// A skill could not be attached.
68#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
69#[error("{message}")]
70pub struct SkillError {
71    /// Stable error kind, for example `skill_not_found`.
72    pub kind: &'static str,
73    /// What went wrong, safe to show to the owner.
74    pub message: String,
75}
76
77impl SkillError {
78    /// An error of `kind` with `message`.
79    pub fn new(kind: &'static str, message: impl Into<String>) -> Self {
80        Self { kind, message: message.into() }
81    }
82}
83
84/// A source of installed skills.
85pub trait SkillSource: Send + Sync + fmt::Debug {
86    /// Resolve `names` for one run.
87    ///
88    /// A disabled skill is left out without an error. A name that is not
89    /// installed is an error, so a typo never runs an agent without the skill
90    /// its author expected.
91    fn attach<'a>(&'a self, names: &'a [String]) -> BoxFuture<'a, Result<AttachedSkills, SkillError>>;
92}
93
94/// Service registered in [`crate::node::Services`]: where nodes find skills.
95#[derive(Clone, Debug)]
96pub struct Skills(pub Arc<dyn SkillSource>);
97
98impl Skills {
99    /// Wrap a skill source as a service.
100    pub fn new(source: Arc<dyn SkillSource>) -> Self {
101        Self(source)
102    }
103}