Skip to main content

surjection_core/model/
skill_meta.rs

1//! `store/skill-meta/<id>.toml`: skill metadata kept outside the skill
2//! directory (SPEC 12.1).
3
4use schemars::JsonSchema;
5use serde::{Deserialize, Serialize};
6
7use crate::error::ErrorCode;
8use crate::model::clients::ClientSelection;
9use crate::model::ids::{self, IdKind};
10use crate::model::validate::Diagnostics;
11use crate::model::{SCHEMA_VERSION, StoreModel};
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
14#[serde(rename_all = "kebab-case")]
15pub enum SkillSourceKind {
16    Git,
17    Local,
18    AgentsCli,
19    Catalog,
20    Authored,
21}
22
23/// How a skill reaches client skill directories (SPEC 12.2).
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
25#[serde(rename_all = "lowercase")]
26pub enum DistributionMethod {
27    Link,
28    Copy,
29}
30
31#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
32#[schemars(title = "Surjection skill metadata (store/skill-meta/<id>.toml)")]
33pub struct SkillMeta {
34    /// Schema version; always 1.
35    #[schemars(extend("const" = 1))]
36    pub schema: i64,
37    /// Skill id; equals the file name. Identity, not the name.
38    #[schemars(extend("pattern" = ids::SLUG_PATTERN))]
39    pub id: String,
40    /// Name from the SKILL.md frontmatter.
41    #[serde(default, skip_serializing_if = "Option::is_none")]
42    pub name: Option<String>,
43    #[serde(default, skip_serializing_if = "Option::is_none")]
44    pub tags: Option<Vec<String>>,
45    #[serde(default, skip_serializing_if = "Option::is_none")]
46    pub source: Option<SkillSource>,
47    #[serde(default, skip_serializing_if = "Option::is_none")]
48    pub enable: Option<SkillEnable>,
49    /// The bundled copy Surjection wrote (the Surjection skill for agents, #8).
50    /// Marks the skill as bundled on its own, without `[source]`: 0.5.x
51    /// reads an unknown `[source] kind` as an error but keeps this table.
52    #[serde(default, skip_serializing_if = "Option::is_none")]
53    pub bundled: Option<BundledCopy>,
54    #[serde(flatten)]
55    #[schemars(skip)]
56    pub extra: toml::Table,
57}
58
59impl StoreModel for SkillMeta {
60    const SCHEMA_NAME: &'static str = "skill-meta";
61}
62
63#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
64pub struct SkillSource {
65    pub kind: SkillSourceKind,
66    /// Repository URL (git).
67    #[serde(default, skip_serializing_if = "Option::is_none")]
68    pub url: Option<String>,
69    /// Skill directory inside the source.
70    #[serde(default, skip_serializing_if = "Option::is_none")]
71    pub path: Option<String>,
72    /// Branch, tag or commit followed for updates (git); a commit pins it.
73    #[serde(rename = "ref", default, skip_serializing_if = "Option::is_none")]
74    pub git_ref: Option<String>,
75    /// Commit the store copy came from (git).
76    #[serde(default, skip_serializing_if = "Option::is_none")]
77    pub commit: Option<String>,
78    /// The catalog entry it was installed from, `<catalog>/<entry id>`
79    /// (#26).
80    #[serde(default, skip_serializing_if = "Option::is_none")]
81    pub catalog: Option<String>,
82    #[serde(flatten)]
83    #[schemars(skip)]
84    pub extra: toml::Table,
85}
86
87#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
88pub struct SkillEnable {
89    /// Client ids, or "all-detected". Unknown ids are a warning and are kept.
90    #[serde(default, skip_serializing_if = "Option::is_none")]
91    pub clients: Option<ClientSelection>,
92    #[serde(default, skip_serializing_if = "Option::is_none")]
93    pub method: Option<DistributionMethod>,
94    #[serde(flatten)]
95    #[schemars(skip)]
96    pub extra: toml::Table,
97}
98
99/// The copy of a bundled skill Surjection last wrote into the store: content
100/// with another hash was edited by the user and is never updated.
101#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
102pub struct BundledCopy {
103    /// The Surjection version the content shipped with.
104    pub version: String,
105    /// `sha256:<hex>` of the content as written (paths and bytes, sorted).
106    pub hash: String,
107    #[serde(flatten)]
108    #[schemars(skip)]
109    pub extra: toml::Table,
110}
111
112impl SkillMeta {
113    pub fn new(id: impl Into<String>) -> Self {
114        Self {
115            schema: SCHEMA_VERSION,
116            id: id.into(),
117            name: None,
118            tags: None,
119            source: None,
120            enable: None,
121            bundled: None,
122            extra: toml::Table::new(),
123        }
124    }
125
126    /// Validates per SPEC 12.1. `file_id` is the file name without `.toml`.
127    pub fn validate(&self, file_id: Option<&str>) -> Diagnostics {
128        let mut d = Diagnostics::new();
129        d.check_named_id(IdKind::Skill, &self.id, "id", file_id);
130        if let Some(source) = &self.source {
131            if source.kind == SkillSourceKind::Git && source.url.is_none() {
132                d.error(
133                    ErrorCode::MissingField,
134                    "source.url",
135                    "git sources need `url`",
136                );
137            }
138            if let Some(commit) = &source.commit
139                && !ids::is_commit_id(commit)
140            {
141                d.error(
142                    ErrorCode::InvalidValue,
143                    "source.commit",
144                    format!("{commit:?} is not a git commit id"),
145                );
146            }
147        }
148        if let Some(clients) = self.enable.as_ref().and_then(|e| e.clients.as_ref()) {
149            d.check_client_selection(clients, "enable.clients");
150        }
151        d
152    }
153}