systemprompt_models/validators/
agents.rs1use super::ValidationConfigProvider;
2use crate::ServicesConfig;
3use std::collections::HashMap;
4use std::path::Path;
5use systemprompt_traits::validation_report::{ValidationError, ValidationReport};
6use systemprompt_traits::{ConfigProvider, DomainConfig, DomainConfigError};
7
8#[derive(Debug, Default)]
9pub struct AgentConfigValidator {
10 config: Option<ServicesConfig>,
11 skills_path: Option<String>,
12}
13
14impl AgentConfigValidator {
15 pub fn new() -> Self {
16 Self::default()
17 }
18}
19
20impl DomainConfig for AgentConfigValidator {
21 fn domain_id(&self) -> &'static str {
22 "agents"
23 }
24
25 fn priority(&self) -> u32 {
26 30
27 }
28
29 fn load(&mut self, config: &dyn ConfigProvider) -> Result<(), DomainConfigError> {
30 let skills_path = config
31 .get("skills_path")
32 .ok_or_else(|| DomainConfigError::NotFound("skills_path not configured".into()))?;
33
34 self.skills_path = Some(skills_path);
35
36 let provider = config
37 .as_any()
38 .downcast_ref::<ValidationConfigProvider>()
39 .ok_or_else(|| {
40 DomainConfigError::LoadError(
41 "Expected ValidationConfigProvider with merged ServicesConfig".into(),
42 )
43 })?;
44
45 self.config = Some(provider.services_config().clone());
46 Ok(())
47 }
48
49 fn validate(&self) -> Result<ValidationReport, DomainConfigError> {
50 let mut report = ValidationReport::new("agents");
51
52 let config = self
53 .config
54 .as_ref()
55 .ok_or_else(|| DomainConfigError::ValidationError("Not loaded".into()))?;
56
57 let skills_path = self
58 .skills_path
59 .as_ref()
60 .ok_or_else(|| DomainConfigError::ValidationError("Skills path not set".into()))?;
61
62 if !Path::new(skills_path).exists() {
63 report.add_error(
64 ValidationError::new("skills_path", "Skills directory does not exist")
65 .with_path(skills_path)
66 .with_suggestion("Create the skills directory"),
67 );
68 }
69
70 Self::validate_port_uniqueness(config, &mut report);
71 for (name, agent) in &config.agents {
72 Self::validate_agent(name, agent, config, skills_path, &mut report);
73 }
74
75 Ok(report)
76 }
77}
78
79impl AgentConfigValidator {
80 fn validate_port_uniqueness(config: &ServicesConfig, report: &mut ValidationReport) {
81 let mut used_ports: HashMap<u16, String> = HashMap::new();
82 for (name, agent) in &config.agents {
83 if let Some(existing) = used_ports.get(&agent.port) {
84 report.add_error(
85 ValidationError::new(
86 format!("agents.{}.port", name),
87 format!("Port {} already used by agent '{}'", agent.port, existing),
88 )
89 .with_suggestion("Assign unique ports to each agent"),
90 );
91 } else {
92 used_ports.insert(agent.port, name.clone());
93 }
94 }
95 }
96
97 fn validate_agent(
98 name: &str,
99 agent: &crate::services::AgentConfig,
100 config: &ServicesConfig,
101 skills_path: &str,
102 report: &mut ValidationReport,
103 ) {
104 if agent.name.is_empty() {
105 report.add_error(ValidationError::new(
106 format!("agents.{}.name", name),
107 "Agent name cannot be empty",
108 ));
109 }
110
111 for skill_id in &agent.metadata.skills.include {
118 let skill_path = Path::new(skills_path).join(skill_id);
119 if !skill_path.exists() {
120 report.add_error(
121 ValidationError::new(
122 format!("agents.{}.metadata.skills", name),
123 format!("Skill '{}' directory not found", skill_id),
124 )
125 .with_path(&skill_path)
126 .with_suggestion("Create the skill directory or remove it from the agent"),
127 );
128 }
129 }
130
131 for mcp_server in &agent.metadata.mcp_servers.include {
132 Self::validate_agent_mcp_ref(name, agent, mcp_server, config, report);
133 }
134 }
135
136 fn validate_agent_mcp_ref(
137 name: &str,
138 agent: &crate::services::AgentConfig,
139 mcp_server: &str,
140 config: &ServicesConfig,
141 report: &mut ValidationReport,
142 ) {
143 let Some(mcp_config) = config.mcp_servers.get(mcp_server) else {
144 report.add_error(
145 ValidationError::new(
146 format!("agents.{}.metadata.mcp_servers", name),
147 format!("MCP server '{}' is not defined", mcp_server),
148 )
149 .with_suggestion(
150 "Define the MCP server in mcp_servers or remove it from the agent",
151 ),
152 );
153 return;
154 };
155
156 if mcp_config.dev_only && !agent.dev_only {
157 report.add_error(
158 ValidationError::new(
159 format!("agents.{}.metadata.mcp_servers", name),
160 format!(
161 "Production agent '{}' references dev-only MCP server '{}'",
162 name, mcp_server
163 ),
164 )
165 .with_suggestion(
166 "Either mark the agent as dev_only: true, or use a production MCP server",
167 ),
168 );
169 }
170 }
171}