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 #[default]
28 Instance,
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 mcp_servers: PluginComponentRef,
90 #[serde(default)]
91 pub content_sources: PluginComponentRef,
92 #[serde(default)]
93 pub scripts: Vec<PluginScript>,
94}
95
96#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
97pub struct PluginComponentRef {
98 #[serde(default)]
99 pub source: ComponentSource,
100 #[serde(default, skip_serializing_if = "Option::is_none")]
101 pub filter: Option<ComponentFilter>,
102 #[serde(default, skip_serializing_if = "Vec::is_empty")]
103 pub include: Vec<String>,
104 #[serde(default, skip_serializing_if = "Vec::is_empty")]
105 pub exclude: Vec<String>,
106}
107
108#[derive(Debug, Clone, Serialize, Deserialize)]
109pub struct PluginScript {
110 pub name: String,
111 pub source: String,
112}
113
114#[derive(Debug, Clone, Serialize, Deserialize)]
115pub struct PluginAuthor {
116 pub name: String,
117 pub email: String,
118}
119
120#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
121pub struct PluginSummary {
122 pub id: PluginId,
123 pub name: String,
124 pub display_name: String,
125 pub enabled: bool,
126 pub skill_count: usize,
127 pub agent_count: usize,
128}
129
130impl From<&PluginConfig> for PluginSummary {
131 fn from(config: &PluginConfig) -> Self {
132 Self {
133 id: config.id.clone(),
134 name: config.name.clone(),
135 display_name: config.name.clone(),
136 enabled: config.enabled,
137 skill_count: config.skills.include.len(),
138 agent_count: config.agents.include.len(),
139 }
140 }
141}
142
143impl PluginConfig {
144 pub fn validate(&self, key: &str) -> Result<(), ConfigValidationError> {
145 let id_str = self.id.as_str();
146 if id_str.len() < 3 || id_str.len() > 50 {
147 return Err(ConfigValidationError::invalid_field(format!(
148 "Plugin '{key}': id must be between 3 and 50 characters"
149 )));
150 }
151
152 if !id_str
153 .chars()
154 .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-')
155 {
156 return Err(ConfigValidationError::invalid_field(format!(
157 "Plugin '{key}': id must be lowercase alphanumeric with hyphens only (kebab-case)"
158 )));
159 }
160
161 if self.version.is_empty() {
162 return Err(ConfigValidationError::required(format!(
163 "Plugin '{key}': version must not be empty"
164 )));
165 }
166
167 Self::validate_component_ref(&self.skills, key, "skills")?;
168 Self::validate_component_ref(&self.agents, key, "agents")?;
169
170 Ok(())
171 }
172
173 fn validate_component_ref(
174 component: &PluginComponentRef,
175 key: &str,
176 field: &str,
177 ) -> Result<(), ConfigValidationError> {
178 if component.source == ComponentSource::Explicit && component.include.is_empty() {
179 return Err(ConfigValidationError::invalid_field(format!(
180 "Plugin '{key}': {field}.source is 'explicit' but {field}.include is empty"
181 )));
182 }
183
184 Ok(())
185 }
186}