Skip to content

Commit 4ea78b9

Browse files
authored
feat(agent): add edit subcommand to modify existing agents (#2845)
- Add Edit subcommand to AgentSubcommands enum - Implement edit functionality that opens existing agent files in editor - Use Agent::get_agent_by_name to locate and load existing agents - Include post-edit validation to ensure JSON remains valid - Add comprehensive tests for the new edit subcommand - Support both --name and -n flags for agent name specification 🤖 Assisted by Amazon Q Developer Co-authored-by: Matt Lee <[email protected]>
1 parent 458e3c1 commit 4ea78b9

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

crates/chat-cli/src/cli/agent/root_command_args.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ pub enum AgentSubcommands {
4646
#[arg(long, short)]
4747
from: Option<String>,
4848
},
49+
/// Edit an existing agent config
50+
Edit {
51+
/// Name of the agent to edit
52+
#[arg(long, short)]
53+
name: String,
54+
},
4955
/// Validate a config with the given path
5056
Validate {
5157
#[arg(long, short)]
@@ -138,6 +144,38 @@ impl AgentArgs {
138144
path_with_file_name.display()
139145
)?;
140146
},
147+
Some(AgentSubcommands::Edit { name }) => {
148+
let _agents = Agents::load(os, None, true, &mut stderr, mcp_enabled).await.0;
149+
let (_agent, path_with_file_name) = Agent::get_agent_by_name(os, &name).await?;
150+
151+
let editor_cmd = std::env::var("EDITOR").unwrap_or_else(|_| "vi".to_string());
152+
let mut cmd = std::process::Command::new(editor_cmd);
153+
154+
let status = cmd.arg(&path_with_file_name).status()?;
155+
if !status.success() {
156+
bail!("Editor process did not exit with success");
157+
}
158+
159+
let Ok(content) = os.fs.read(&path_with_file_name).await else {
160+
bail!(
161+
"Post edit validation failed. Error opening {}. Aborting",
162+
path_with_file_name.display()
163+
);
164+
};
165+
if let Err(e) = serde_json::from_slice::<Agent>(&content) {
166+
bail!(
167+
"Post edit validation failed for agent '{name}' at path: {}. Malformed config detected: {e}",
168+
path_with_file_name.display()
169+
);
170+
}
171+
172+
writeln!(
173+
stderr,
174+
"\n✏️ Edited agent {} '{}'\n",
175+
name,
176+
path_with_file_name.display()
177+
)?;
178+
},
141179
Some(AgentSubcommands::Validate { path }) => {
142180
let mut global_mcp_config = None::<McpServerConfig>;
143181
let agent = Agent::load(os, path.as_str(), &mut global_mcp_config, mcp_enabled, &mut stderr).await;
@@ -386,4 +424,24 @@ mod tests {
386424
})
387425
);
388426
}
427+
428+
#[test]
429+
fn test_agent_subcommand_edit() {
430+
assert_parse!(
431+
["agent", "edit", "--name", "existing_agent"],
432+
RootSubcommand::Agent(AgentArgs {
433+
cmd: Some(AgentSubcommands::Edit {
434+
name: "existing_agent".to_string(),
435+
})
436+
})
437+
);
438+
assert_parse!(
439+
["agent", "edit", "-n", "existing_agent"],
440+
RootSubcommand::Agent(AgentArgs {
441+
cmd: Some(AgentSubcommands::Edit {
442+
name: "existing_agent".to_string(),
443+
})
444+
})
445+
);
446+
}
389447
}

0 commit comments

Comments
 (0)