systemprompt_models/services/
plugin.rs1use std::fmt;
13
14use schemars::JsonSchema;
15use serde::{Deserialize, Serialize};
16use systemprompt_identifiers::PluginId;
17
18use crate::errors::ConfigValidationError;
19
20const fn default_true() -> bool {
21 true
22}
23
24#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
25#[serde(rename_all = "lowercase")]
26pub enum ComponentSource {
27 Instance,
28 #[default]
29 Explicit,
30}
31
32impl fmt::Display for ComponentSource {
33 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34 match self {
35 Self::Instance => write!(f, "instance"),
36 Self::Explicit => write!(f, "explicit"),
37 }
38 }
39}
40
41#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
42#[serde(rename_all = "lowercase")]
43pub enum ComponentFilter {
44 Enabled,
45}
46
47impl fmt::Display for ComponentFilter {
48 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49 match self {
50 Self::Enabled => write!(f, "enabled"),
51 }
52 }
53}
54
55#[derive(Debug, Clone, Serialize, Deserialize)]
56pub struct PluginConfigFile {
57 pub plugin: PluginConfig,
58}
59
60#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
61pub struct PluginVariableDef {
62 pub name: String,
63 #[serde(default)]
64 pub description: String,
65 #[serde(default = "default_true")]
66 pub required: bool,
67 #[serde(default)]
68 pub secret: bool,
69 #[serde(skip_serializing_if = "Option::is_none")]
70 pub example: Option<String>,
71}
72
73#[derive(Debug, Clone, Serialize, Deserialize)]
74pub struct PluginConfig {
75 pub id: PluginId,
76 pub name: String,
77 pub description: String,
78 pub version: String,
79 #[serde(default = "default_true")]
80 pub enabled: bool,
81 pub author: PluginAuthor,
82 pub keywords: Vec<String>,
83 pub license: String,
84 pub category: String,
85
86 pub skills: PluginComponentRef,
87 pub agents: PluginComponentRef,
88 #[serde(default)]
89 pub rules: PluginComponentRef,
90 #[serde(default)]
91 pub mcp_servers: PluginComponentRef,
92 #[serde(default)]
93 pub content_sources: PluginComponentRef,
94 #[serde(default)]
95 pub artifacts: PluginComponentRef,
96 #[serde(default)]
97 pub hooks: PluginHooksRef,
98 #[serde(default)]
99 pub scripts: Vec<PluginScript>,
100 #[serde(default, skip_serializing_if = "Vec::is_empty")]
101 pub dependencies: Vec<PluginDependency>,
102}
103
104#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
114#[serde(deny_unknown_fields)]
115pub struct PluginDependency {
116 pub name: String,
117 #[serde(default, skip_serializing_if = "Option::is_none")]
118 pub marketplace: Option<String>,
119 #[serde(default, skip_serializing_if = "Option::is_none")]
120 pub version: Option<String>,
121}
122
123impl PluginDependency {
124 fn validate(&self, key: &str) -> Result<(), ConfigValidationError> {
125 if self.name.trim().is_empty() {
126 return Err(ConfigValidationError::required(format!(
127 "Plugin '{key}': dependencies entries must name a plugin"
128 )));
129 }
130 if self
131 .marketplace
132 .as_deref()
133 .is_some_and(|m| m.trim().is_empty())
134 {
135 return Err(ConfigValidationError::invalid_field(format!(
136 "Plugin '{key}': dependency '{}' sets an empty marketplace",
137 self.name
138 )));
139 }
140 if let Some(range) = &self.version
141 && semver::VersionReq::parse(range).is_err()
142 {
143 return Err(ConfigValidationError::invalid_field(format!(
144 "Plugin '{key}': dependency '{}' version '{range}' is not a semver range",
145 self.name
146 )));
147 }
148 Ok(())
149 }
150}
151
152#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
164#[serde(deny_unknown_fields)]
165pub struct PluginHooksRef {
166 #[serde(default)]
167 pub governance: bool,
168 #[serde(default)]
169 pub comms: bool,
170 #[serde(default, skip_serializing_if = "Vec::is_empty")]
171 pub include: Vec<String>,
172}
173
174impl PluginHooksRef {
175 #[must_use]
176 pub const fn is_empty(&self) -> bool {
177 !self.governance && self.include.is_empty()
178 }
179}
180
181#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
182pub struct PluginComponentRef {
183 #[serde(default)]
184 pub source: ComponentSource,
185 #[serde(default, skip_serializing_if = "Option::is_none")]
186 pub filter: Option<ComponentFilter>,
187 #[serde(default, skip_serializing_if = "Vec::is_empty")]
188 pub include: Vec<String>,
189 #[serde(default, skip_serializing_if = "Vec::is_empty")]
190 pub exclude: Vec<String>,
191}
192
193#[derive(Debug, Clone, Serialize, Deserialize)]
194pub struct PluginScript {
195 pub name: String,
196 pub source: String,
197}
198
199#[derive(Debug, Clone, Serialize, Deserialize)]
200pub struct PluginAuthor {
201 pub name: String,
202 pub email: String,
203}
204
205#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
206pub struct PluginSummary {
207 pub id: PluginId,
208 pub name: String,
209 pub display_name: String,
210 pub enabled: bool,
211 pub skill_count: usize,
212 pub agent_count: usize,
213}
214
215impl From<&PluginConfig> for PluginSummary {
216 fn from(config: &PluginConfig) -> Self {
217 Self {
218 id: config.id.clone(),
219 name: config.name.clone(),
220 display_name: config.name.clone(),
221 enabled: config.enabled,
222 skill_count: config.skills.include.len(),
223 agent_count: config.agents.include.len(),
224 }
225 }
226}
227
228impl PluginConfig {
229 pub fn validate(&self, key: &str) -> Result<(), ConfigValidationError> {
230 let id_str = self.id.as_str();
231 if id_str.len() < 3 || id_str.len() > 50 {
232 return Err(ConfigValidationError::invalid_field(format!(
233 "Plugin '{key}': id must be between 3 and 50 characters"
234 )));
235 }
236
237 if !id_str
238 .chars()
239 .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-')
240 {
241 return Err(ConfigValidationError::invalid_field(format!(
242 "Plugin '{key}': id must be lowercase alphanumeric with hyphens only (kebab-case)"
243 )));
244 }
245
246 if self.version.is_empty() {
247 return Err(ConfigValidationError::required(format!(
248 "Plugin '{key}': version must not be empty"
249 )));
250 }
251
252 Self::validate_component_ref(&self.skills, key, "skills")?;
253 Self::validate_component_ref(&self.agents, key, "agents")?;
254 Self::validate_component_ref(&self.artifacts, key, "artifacts")?;
255 Self::validate_component_ref(&self.rules, key, "rules")?;
256
257 for dependency in &self.dependencies {
258 dependency.validate(key)?;
259 }
260 let mut seen = std::collections::BTreeSet::new();
261 for dependency in &self.dependencies {
262 if !seen.insert((dependency.name.as_str(), dependency.marketplace.as_deref())) {
263 return Err(ConfigValidationError::invalid_field(format!(
264 "Plugin '{key}': dependency '{}' is listed twice",
265 dependency.name
266 )));
267 }
268 }
269
270 Ok(())
271 }
272
273 fn validate_component_ref(
274 component: &PluginComponentRef,
275 key: &str,
276 field: &str,
277 ) -> Result<(), ConfigValidationError> {
278 if component.source == ComponentSource::Instance && !component.include.is_empty() {
279 return Err(ConfigValidationError::invalid_field(format!(
280 "Plugin '{key}': {field}.source is 'instance' but {field}.include is set (ignored)"
281 )));
282 }
283
284 Ok(())
285 }
286}