BlueGit

ict rusty-teams

Files

rusty-teams / crates / agents / src / settings.rs

settings.rs

settings.rs 6.4 KiB Download Raw
1use serde::{Deserialize, Serialize};
2
3use crate::action::AgentAction;
4use crate::profile::AgentProfile;
5use crate::template::{is_known_placeholder, placeholders_in};
6
7pub const SETTINGS_KEY: &str = "agents.settings";
8
9#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
10pub 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)]
24pub struct AgentSettings {
25 pub agents: Vec<AgentProfile>,
26 pub actions: Vec<AgentAction>,
27 pub resume_interrupted_runs: bool,
28}
29
30impl Default for AgentSettings {
31 fn default() -> Self {
32 Self {
33 agents: vec![AgentProfile::claude_wsl(), AgentProfile::copilot_wsl()],
34 actions: vec![
35 AgentAction::answer_this("claude"),
36 AgentAction::build_this("claude"),
37 ],
38 resume_interrupted_runs: true,
39 }
40 }
41}
42
43fn check_placeholders(item_id: &str, texts: &[&str], problems: &mut Vec<SettingsProblem>) {
44 for text in texts {
45 for placeholder in placeholders_in(text) {
46 if !is_known_placeholder(&placeholder) {
47 problems.push(SettingsProblem::UnknownPlaceholder {
48 item_id: item_id.to_string(),
49 placeholder,
50 });
51 }
52 }
53 }
54}
55
56impl AgentSettings {
57 pub fn to_json(&self) -> String {
58 serde_json::to_string(self).unwrap_or_else(|_| "{}".to_string())
59 }
60
61 pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
62 let mut settings: Self = serde_json::from_str(json)?;
63 for agent in &mut settings.agents {
64 agent.migrate_open_session_command();
65 }
66 Ok(settings)
67 }
68
69 pub fn validate(&self) -> Vec<SettingsProblem> {
70 let mut problems = Vec::new();
71 for agent in &self.agents {
72 if agent.command.trim().is_empty() {
73 problems.push(SettingsProblem::EmptyCommand {
74 agent_id: agent.id.clone(),
75 });
76 }
77 check_placeholders(
78 &agent.id,
79 &[&agent.command, &agent.open_session_command],
80 &mut problems,
81 );
82 }
83 for action in &self.actions {
84 if !self.agents.iter().any(|agent| agent.id == action.agent_id) {
85 problems.push(SettingsProblem::UnknownAgent {
86 action_id: action.id.clone(),
87 agent_id: action.agent_id.clone(),
88 });
89 }
90 check_placeholders(
91 &action.id,
92 &[&action.prompt_template, &action.extra_args],
93 &mut problems,
94 );
95 }
96 problems
97 }
98}
99
100#[cfg(test)]
101mod tests {
102 use super::*;
103
104 #[test]
105 fn default_round_trips_and_is_valid() {
106 let settings = AgentSettings::default();
107 let loaded = AgentSettings::from_json(&settings.to_json()).unwrap();
108 assert_eq!(loaded, settings);
109 assert!(settings.validate().is_empty());
110 }
111
112 #[test]
113 fn from_json_migrates_legacy_wsl_open_session_commands_only() {
114 let legacy = AgentSettings::from_json(&format!(
115 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}}"}}]}}"#,
116 crate::profile::LEGACY_CLAUDE_WSL_OPEN_SESSION,
117 crate::profile::LEGACY_COPILOT_WSL_OPEN_SESSION,
118 ))
119 .unwrap();
120 assert_eq!(
121 legacy.agents[0].open_session_command,
122 AgentProfile::claude_wsl().open_session_command
123 );
124 assert_eq!(
125 legacy.agents[1].open_session_command,
126 AgentProfile::copilot_wsl().open_session_command
127 );
128 assert_eq!(
129 legacy.agents[2].open_session_command,
130 "wt.exe wsl.exe -e claude --resume {session_id}"
131 );
132 }
133
134 #[test]
135 fn empty_object_loads_defaults() {
136 assert_eq!(
137 AgentSettings::from_json("{}").unwrap(),
138 AgentSettings::default()
139 );
140 }
141
142 #[test]
143 fn resume_defaults_on_and_can_be_switched_off() {
144 assert!(
145 AgentSettings::from_json("{}")
146 .unwrap()
147 .resume_interrupted_runs
148 );
149 let off = AgentSettings::from_json(r#"{"resume_interrupted_runs":false}"#).unwrap();
150 assert!(!off.resume_interrupted_runs);
151 }
152
153 #[test]
154 fn partial_json_fills_missing_fields() {
155 let loaded = AgentSettings::from_json(
156 r#"{"agents":[{"id":"x","command":"x {session_id}"}],"actions":[{"id":"a","agent_id":"x"}]}"#,
157 )
158 .unwrap();
159 assert_eq!(loaded.agents.len(), 1);
160 assert_eq!(
161 loaded.agents[0].output,
162 crate::OutputFormat::ClaudeStreamJson
163 );
164 assert_eq!(loaded.actions[0].working_dir, "");
165 let only_actions = AgentSettings::from_json(r#"{"actions":[]}"#).unwrap();
166 assert_eq!(only_actions.agents.len(), 2);
167 }
168
169 #[test]
170 fn validate_finds_problems() {
171 let mut settings = AgentSettings::default();
172 settings.actions[0].agent_id = "ghost".to_string();
173 settings.agents[1].command = String::new();
174 settings.agents[0].command.push_str(" {bogus}");
175 settings.actions[1].prompt_template.push_str(" {nope}");
176 let problems = settings.validate();
177 assert!(problems.contains(&SettingsProblem::UnknownAgent {
178 action_id: "answer-this".to_string(),
179 agent_id: "ghost".to_string(),
180 }));
181 assert!(problems.contains(&SettingsProblem::EmptyCommand {
182 agent_id: "copilot".to_string(),
183 }));
184 assert!(problems.contains(&SettingsProblem::UnknownPlaceholder {
185 item_id: "claude".to_string(),
186 placeholder: "bogus".to_string(),
187 }));
188 assert!(problems.contains(&SettingsProblem::UnknownPlaceholder {
189 item_id: "build-this".to_string(),
190 placeholder: "nope".to_string(),
191 }));
192 }
193}