Skip to content

Commit 697dc64

Browse files
authored
fix(agent): add edit subcommand support to /agent slash command (#2854)
- Add Edit variant to AgentSubcommand enum in profile.rs - Implement edit functionality for slash command usage - Use Agent::get_agent_by_name to locate existing agents - Include post-edit validation and agent reloading - Add edit case to name() method for proper command routing - Enables /agent edit --name <agent> usage in chat sessions 🤖 Assisted by Amazon Q Developer Co-authored-by: Matt Lee <[email protected]>
1 parent c4a0910 commit 697dc64

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

crates/chat-cli/src/cli/chat/cli/profile.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ pub enum AgentSubcommand {
7777
#[arg(long, short)]
7878
from: Option<String>,
7979
},
80+
/// Edit an existing agent configuration
81+
Edit {
82+
/// Name of the agent to edit
83+
#[arg(long, short)]
84+
name: String,
85+
},
8086
/// Generate an agent configuration using AI
8187
Generate {},
8288
/// Delete the specified agent
@@ -242,6 +248,64 @@ impl AgentSubcommand {
242248
)?;
243249
},
244250

251+
Self::Edit { name } => {
252+
let (_agent, path_with_file_name) = Agent::get_agent_by_name(os, &name)
253+
.await
254+
.map_err(|e| ChatError::Custom(Cow::Owned(e.to_string())))?;
255+
256+
let editor_cmd = std::env::var("EDITOR").unwrap_or_else(|_| "vi".to_string());
257+
let mut cmd = std::process::Command::new(editor_cmd);
258+
259+
let status = cmd.arg(&path_with_file_name).status()?;
260+
if !status.success() {
261+
return Err(ChatError::Custom("Editor process did not exit with success".into()));
262+
}
263+
264+
let updated_agent = Agent::load(
265+
os,
266+
&path_with_file_name,
267+
&mut None,
268+
session.conversation.mcp_enabled,
269+
&mut session.stderr,
270+
)
271+
.await;
272+
match updated_agent {
273+
Ok(agent) => {
274+
session.conversation.agents.agents.insert(agent.name.clone(), agent);
275+
},
276+
Err(e) => {
277+
execute!(
278+
session.stderr,
279+
style::SetForegroundColor(Color::Red),
280+
style::Print("Error: "),
281+
style::ResetColor,
282+
style::Print(&e),
283+
style::Print("\n"),
284+
)?;
285+
286+
return Err(ChatError::Custom(
287+
format!("Post edit validation failed for agent '{name}'. Malformed config detected: {e}")
288+
.into(),
289+
));
290+
},
291+
}
292+
293+
execute!(
294+
session.stderr,
295+
style::SetForegroundColor(Color::Green),
296+
style::Print("Agent "),
297+
style::SetForegroundColor(Color::Cyan),
298+
style::Print(name),
299+
style::SetForegroundColor(Color::Green),
300+
style::Print(" has been edited successfully"),
301+
style::SetForegroundColor(Color::Reset),
302+
style::Print("\n"),
303+
style::SetForegroundColor(Color::Yellow),
304+
style::Print("Changes take effect on next launch"),
305+
style::SetForegroundColor(Color::Reset)
306+
)?;
307+
},
308+
245309
Self::Generate {} => {
246310
let agent_name = match crate::util::input("Enter agent name: ", None) {
247311
Ok(input) => input.trim().to_string(),
@@ -440,6 +504,7 @@ impl AgentSubcommand {
440504
match self {
441505
Self::List => "list",
442506
Self::Create { .. } => "create",
507+
Self::Edit { .. } => "edit",
443508
Self::Generate { .. } => "generate",
444509
Self::Delete { .. } => "delete",
445510
Self::Set { .. } => "set",

0 commit comments

Comments
 (0)