| 1 | use serde::{Deserialize, Serialize}; |
| 2 | |
| 3 | use crate::action::AgentAction; |
| 4 | use crate::profile::AgentProfile; |
| 5 | use crate::template::{is_known_placeholder, placeholders_in}; |
| 6 | |
| 7 | pub const SETTINGS_KEY: &str = "agents.settings"; |
| 8 | |
| 9 | #[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)] |
| 10 | pub enum SettingsProblem { |
| 11 | #[error("action {action_id} references unknown agent {agent_id}")] |
| 12 | UnknownAgent { action_id: String, agent_id: String }, |
| 13 | #[error("agent {agent_id} has an empty command")] |
| 14 | EmptyCommand { agent_id: String }, |
| 15 | #[error("{item_id} uses unknown placeholder {{{placeholder}}}")] |
| 16 | UnknownPlaceholder { |
| 17 | item_id: String, |
| 18 | placeholder: String, |
| 19 | }, |
| 20 | } |
| 21 | |
| 22 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 23 | #[serde(default)] |
| 24 | pub struct AgentSettings { |
| 25 | pub agents: Vec<AgentProfile>, |
| 26 | pub actions: Vec<AgentAction>, |
| 27 | } |
| 28 | |
| 29 | impl Default for AgentSettings { |
| 30 | fn default() -> Self { |
| 31 | Self { |
| 32 | agents: vec![AgentProfile::claude_wsl(), AgentProfile::copilot_wsl()], |
| 33 | actions: vec![ |
| 34 | AgentAction::answer_this("claude"), |
| 35 | AgentAction::build_this("claude"), |
| 36 | ], |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | fn check_placeholders(item_id: &str, texts: &[&str], problems: &mut Vec<SettingsProblem>) { |
| 42 | for text in texts { |
| 43 | for placeholder in placeholders_in(text) { |
| 44 | if !is_known_placeholder(&placeholder) { |
| 45 | problems.push(SettingsProblem::UnknownPlaceholder { |
| 46 | item_id: item_id.to_string(), |
| 47 | placeholder, |
| 48 | }); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | impl AgentSettings { |
| 55 | pub fn to_json(&self) -> String { |
| 56 | serde_json::to_string(self).unwrap_or_else(|_| "{}".to_string()) |
| 57 | } |
| 58 | |
| 59 | pub fn from_json(json: &str) -> Result<Self, serde_json::Error> { |
| 60 | serde_json::from_str(json) |
| 61 | } |
| 62 | |
| 63 | pub fn validate(&self) -> Vec<SettingsProblem> { |
| 64 | let mut problems = Vec::new(); |
| 65 | for agent in &self.agents { |
| 66 | if agent.command.trim().is_empty() { |
| 67 | problems.push(SettingsProblem::EmptyCommand { |
| 68 | agent_id: agent.id.clone(), |
| 69 | }); |
| 70 | } |
| 71 | check_placeholders( |
| 72 | &agent.id, |
| 73 | &[&agent.command, &agent.open_session_command], |
| 74 | &mut problems, |
| 75 | ); |
| 76 | } |
| 77 | for action in &self.actions { |
| 78 | if !self.agents.iter().any(|agent| agent.id == action.agent_id) { |
| 79 | problems.push(SettingsProblem::UnknownAgent { |
| 80 | action_id: action.id.clone(), |
| 81 | agent_id: action.agent_id.clone(), |
| 82 | }); |
| 83 | } |
| 84 | check_placeholders( |
| 85 | &action.id, |
| 86 | &[&action.prompt_template, &action.extra_args], |
| 87 | &mut problems, |
| 88 | ); |
| 89 | } |
| 90 | problems |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | #[cfg(test)] |
| 95 | mod tests { |
| 96 | use super::*; |
| 97 | |
| 98 | #[test] |
| 99 | fn default_round_trips_and_is_valid() { |
| 100 | let settings = AgentSettings::default(); |
| 101 | let loaded = AgentSettings::from_json(&settings.to_json()).unwrap(); |
| 102 | assert_eq!(loaded, settings); |
| 103 | assert!(settings.validate().is_empty()); |
| 104 | } |
| 105 | |
| 106 | #[test] |
| 107 | fn empty_object_loads_defaults() { |
| 108 | assert_eq!( |
| 109 | AgentSettings::from_json("{}").unwrap(), |
| 110 | AgentSettings::default() |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | #[test] |
| 115 | fn partial_json_fills_missing_fields() { |
| 116 | let loaded = AgentSettings::from_json( |
| 117 | r#"{"agents":[{"id":"x","command":"x {session_id}"}],"actions":[{"id":"a","agent_id":"x"}]}"#, |
| 118 | ) |
| 119 | .unwrap(); |
| 120 | assert_eq!(loaded.agents.len(), 1); |
| 121 | assert_eq!( |
| 122 | loaded.agents[0].output, |
| 123 | crate::OutputFormat::ClaudeStreamJson |
| 124 | ); |
| 125 | assert_eq!(loaded.actions[0].working_dir, ""); |
| 126 | let only_actions = AgentSettings::from_json(r#"{"actions":[]}"#).unwrap(); |
| 127 | assert_eq!(only_actions.agents.len(), 2); |
| 128 | } |
| 129 | |
| 130 | #[test] |
| 131 | fn validate_finds_problems() { |
| 132 | let mut settings = AgentSettings::default(); |
| 133 | settings.actions[0].agent_id = "ghost".to_string(); |
| 134 | settings.agents[1].command = String::new(); |
| 135 | settings.agents[0].command.push_str(" {bogus}"); |
| 136 | settings.actions[1].prompt_template.push_str(" {nope}"); |
| 137 | let problems = settings.validate(); |
| 138 | assert!(problems.contains(&SettingsProblem::UnknownAgent { |
| 139 | action_id: "answer-this".to_string(), |
| 140 | agent_id: "ghost".to_string(), |
| 141 | })); |
| 142 | assert!(problems.contains(&SettingsProblem::EmptyCommand { |
| 143 | agent_id: "copilot".to_string(), |
| 144 | })); |
| 145 | assert!(problems.contains(&SettingsProblem::UnknownPlaceholder { |
| 146 | item_id: "claude".to_string(), |
| 147 | placeholder: "bogus".to_string(), |
| 148 | })); |
| 149 | assert!(problems.contains(&SettingsProblem::UnknownPlaceholder { |
| 150 | item_id: "build-this".to_string(), |
| 151 | placeholder: "nope".to_string(), |
| 152 | })); |
| 153 | } |
| 154 | } |