| 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 | let mut settings: Self = serde_json::from_str(json)?; |
| 61 | for agent in &mut settings.agents { |
| 62 | agent.migrate_open_session_command(); |
| 63 | } |
| 64 | Ok(settings) |
| 65 | } |
| 66 | |
| 67 | pub fn validate(&self) -> Vec<SettingsProblem> { |
| 68 | let mut problems = Vec::new(); |
| 69 | for agent in &self.agents { |
| 70 | if agent.command.trim().is_empty() { |
| 71 | problems.push(SettingsProblem::EmptyCommand { |
| 72 | agent_id: agent.id.clone(), |
| 73 | }); |
| 74 | } |
| 75 | check_placeholders( |
| 76 | &agent.id, |
| 77 | &[&agent.command, &agent.open_session_command], |
| 78 | &mut problems, |
| 79 | ); |
| 80 | } |
| 81 | for action in &self.actions { |
| 82 | if !self.agents.iter().any(|agent| agent.id == action.agent_id) { |
| 83 | problems.push(SettingsProblem::UnknownAgent { |
| 84 | action_id: action.id.clone(), |
| 85 | agent_id: action.agent_id.clone(), |
| 86 | }); |
| 87 | } |
| 88 | check_placeholders( |
| 89 | &action.id, |
| 90 | &[&action.prompt_template, &action.extra_args], |
| 91 | &mut problems, |
| 92 | ); |
| 93 | } |
| 94 | problems |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | #[cfg(test)] |
| 99 | mod tests { |
| 100 | use super::*; |
| 101 | |
| 102 | #[test] |
| 103 | fn default_round_trips_and_is_valid() { |
| 104 | let settings = AgentSettings::default(); |
| 105 | let loaded = AgentSettings::from_json(&settings.to_json()).unwrap(); |
| 106 | assert_eq!(loaded, settings); |
| 107 | assert!(settings.validate().is_empty()); |
| 108 | } |
| 109 | |
| 110 | #[test] |
| 111 | fn from_json_migrates_legacy_wsl_open_session_commands_only() { |
| 112 | let legacy = AgentSettings::from_json(&format!( |
| 113 | r#"{{"agents":[{{"id":"claude","open_session_command":"{}"}},{{"id":"copilot","open_session_command":"{}"}},{{"id":"mine","open_session_command":"wt.exe wsl.exe -e claude --resume {{session_id}}"}}]}}"#, |
| 114 | crate::profile::LEGACY_CLAUDE_WSL_OPEN_SESSION, |
| 115 | crate::profile::LEGACY_COPILOT_WSL_OPEN_SESSION, |
| 116 | )) |
| 117 | .unwrap(); |
| 118 | assert_eq!( |
| 119 | legacy.agents[0].open_session_command, |
| 120 | AgentProfile::claude_wsl().open_session_command |
| 121 | ); |
| 122 | assert_eq!( |
| 123 | legacy.agents[1].open_session_command, |
| 124 | AgentProfile::copilot_wsl().open_session_command |
| 125 | ); |
| 126 | assert_eq!( |
| 127 | legacy.agents[2].open_session_command, |
| 128 | "wt.exe wsl.exe -e claude --resume {session_id}" |
| 129 | ); |
| 130 | } |
| 131 | |
| 132 | #[test] |
| 133 | fn empty_object_loads_defaults() { |
| 134 | assert_eq!( |
| 135 | AgentSettings::from_json("{}").unwrap(), |
| 136 | AgentSettings::default() |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | #[test] |
| 141 | fn partial_json_fills_missing_fields() { |
| 142 | let loaded = AgentSettings::from_json( |
| 143 | r#"{"agents":[{"id":"x","command":"x {session_id}"}],"actions":[{"id":"a","agent_id":"x"}]}"#, |
| 144 | ) |
| 145 | .unwrap(); |
| 146 | assert_eq!(loaded.agents.len(), 1); |
| 147 | assert_eq!( |
| 148 | loaded.agents[0].output, |
| 149 | crate::OutputFormat::ClaudeStreamJson |
| 150 | ); |
| 151 | assert_eq!(loaded.actions[0].working_dir, ""); |
| 152 | let only_actions = AgentSettings::from_json(r#"{"actions":[]}"#).unwrap(); |
| 153 | assert_eq!(only_actions.agents.len(), 2); |
| 154 | } |
| 155 | |
| 156 | #[test] |
| 157 | fn validate_finds_problems() { |
| 158 | let mut settings = AgentSettings::default(); |
| 159 | settings.actions[0].agent_id = "ghost".to_string(); |
| 160 | settings.agents[1].command = String::new(); |
| 161 | settings.agents[0].command.push_str(" {bogus}"); |
| 162 | settings.actions[1].prompt_template.push_str(" {nope}"); |
| 163 | let problems = settings.validate(); |
| 164 | assert!(problems.contains(&SettingsProblem::UnknownAgent { |
| 165 | action_id: "answer-this".to_string(), |
| 166 | agent_id: "ghost".to_string(), |
| 167 | })); |
| 168 | assert!(problems.contains(&SettingsProblem::EmptyCommand { |
| 169 | agent_id: "copilot".to_string(), |
| 170 | })); |
| 171 | assert!(problems.contains(&SettingsProblem::UnknownPlaceholder { |
| 172 | item_id: "claude".to_string(), |
| 173 | placeholder: "bogus".to_string(), |
| 174 | })); |
| 175 | assert!(problems.contains(&SettingsProblem::UnknownPlaceholder { |
| 176 | item_id: "build-this".to_string(), |
| 177 | placeholder: "nope".to_string(), |
| 178 | })); |
| 179 | } |
| 180 | } |