Skip to content

Commit f684f8a

Browse files
authored
fix(agent): agent fallback behavior (#2281)
* fixes agent fallback behavior * fixes typo * adjusts boolean statement
1 parent dba2e96 commit f684f8a

File tree

2 files changed

+63
-35
lines changed

2 files changed

+63
-35
lines changed

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

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ use std::path::{
1515
};
1616

1717
use context_migrate::ContextMigrate;
18-
use crossterm::style::Stylize as _;
18+
use crossterm::style::{
19+
Color,
20+
Stylize as _,
21+
};
1922
use crossterm::{
2023
queue,
2124
style,
@@ -47,6 +50,7 @@ use super::chat::tools::{
4750
NATIVE_TOOLS,
4851
ToolOrigin,
4952
};
53+
use crate::database::settings::Setting;
5054
use crate::os::Os;
5155
use crate::util::{
5256
MCP_SERVER_TOOL_DELIMITER,
@@ -432,11 +436,51 @@ impl Agents {
432436

433437
local_agents.append(&mut global_agents);
434438

435-
// If we are told which agent to set as active, we will fall back to a default whose
436-
// lifetime matches that of the session
437-
if agent_name.is_none() {
439+
// Assume agent in the following order of priority:
440+
// 1. The agent name specified by the start command via --agent (this is the agent_name that's
441+
// passed in)
442+
// 2. If the above is missing or invalid, assume one that is specified by chat.defaultAgent
443+
// 3. If the above is missing or invalid, assume the in-memory default
444+
let active_idx = 'active_idx: {
445+
if let Some(name) = agent_name {
446+
if local_agents.iter().any(|a| a.name.as_str() == name) {
447+
break 'active_idx name.to_string();
448+
}
449+
let _ = queue!(
450+
output,
451+
style::SetForegroundColor(Color::Red),
452+
style::Print("Error"),
453+
style::SetForegroundColor(Color::Yellow),
454+
style::Print(format!(
455+
": no agent with name {} found. Falling back to user specified default",
456+
name
457+
)),
458+
style::Print("\n"),
459+
style::SetForegroundColor(Color::Reset)
460+
);
461+
}
462+
463+
if let Some(user_set_default) = os.database.settings.get_string(Setting::ChatDefaultAgent) {
464+
if local_agents.iter().any(|a| a.name == user_set_default) {
465+
break 'active_idx user_set_default;
466+
}
467+
let _ = queue!(
468+
output,
469+
style::SetForegroundColor(Color::Red),
470+
style::Print("Error"),
471+
style::SetForegroundColor(Color::Yellow),
472+
style::Print(format!(
473+
": user defined default {} not found. Falling back to in-memory default",
474+
user_set_default
475+
)),
476+
style::Print("\n"),
477+
style::SetForegroundColor(Color::Reset)
478+
);
479+
}
480+
438481
local_agents.push(Agent::default());
439-
}
482+
"default".to_string()
483+
};
440484

441485
let _ = output.flush();
442486

@@ -445,7 +489,7 @@ impl Agents {
445489
.into_iter()
446490
.map(|a| (a.name.clone(), a))
447491
.collect::<HashMap<_, _>>(),
448-
active_idx: agent_name.unwrap_or("default").to_string(),
492+
active_idx,
449493
..Default::default()
450494
}
451495
}

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

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -229,39 +229,23 @@ impl ChatArgs {
229229
}
230230

231231
let agents = {
232-
let mut default_agent_name = None::<String>;
233-
let agent_name = if let Some(agent) = self.agent.as_deref() {
234-
Some(agent)
235-
} else if let Some(agent) = os.database.settings.get_string(Setting::ChatDefaultAgent) {
236-
default_agent_name.replace(agent);
237-
default_agent_name.as_deref()
238-
} else {
239-
None
240-
};
241232
let skip_migration = self.no_interactive || !self.migrate;
242-
let mut agents = Agents::load(os, agent_name, skip_migration, &mut stderr).await;
233+
let mut agents = Agents::load(os, self.agent.as_deref(), skip_migration, &mut stderr).await;
243234
agents.trust_all_tools = self.trust_all_tools;
244235

245-
if let Some(name) = self.agent.as_ref() {
246-
match agents.switch(name) {
247-
Ok(agent) if !agent.mcp_servers.mcp_servers.is_empty() => {
248-
if !self.no_interactive
249-
&& !os.database.settings.get_bool(Setting::McpLoadedBefore).unwrap_or(false)
250-
{
251-
execute!(
252-
stderr,
253-
style::Print(
254-
"To learn more about MCP safety, see https://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/command-line-mcp-security.html\n\n"
255-
)
256-
)?;
257-
}
258-
os.database.settings.set(Setting::McpLoadedBefore, true).await?;
259-
},
260-
Err(e) => {
261-
let _ = execute!(stderr, style::Print(format!("Error switching profile: {}", e)));
262-
},
263-
_ => {},
236+
if agents
237+
.get_active()
238+
.is_some_and(|a| !a.mcp_servers.mcp_servers.is_empty())
239+
{
240+
if !self.no_interactive && !os.database.settings.get_bool(Setting::McpLoadedBefore).unwrap_or(false) {
241+
execute!(
242+
stderr,
243+
style::Print(
244+
"To learn more about MCP safety, see https://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/command-line-mcp-security.html\n\n"
245+
)
246+
)?;
264247
}
248+
os.database.settings.set(Setting::McpLoadedBefore, true).await?;
265249
}
266250

267251
if let Some(trust_tools) = self.trust_tools.take() {

0 commit comments

Comments
 (0)