Skip to content

Commit ae134a9

Browse files
authored
fix(agent): local agents being sourced from incorrect local path (#2398)
1 parent 98bd4c9 commit ae134a9

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ impl Agent {
218218
.to_string();
219219

220220
self.name = name.clone();
221+
self.path = Some(path.to_path_buf());
221222

222223
if let (true, Some(global_mcp_config)) = (self.use_legacy_mcp_json, global_mcp_config) {
223224
let mut stderr = std::io::stderr();

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,19 +214,16 @@ pub async fn create_agent(
214214
from: Option<String>,
215215
) -> Result<PathBuf> {
216216
let path = if let Some(path) = path {
217-
let path = PathBuf::from(path);
217+
let mut path = PathBuf::from(path);
218+
if path.is_relative() {
219+
path = os.env.current_dir()?.join(path);
220+
}
218221

219-
// If path points to a file, strip the filename to get the directory
220222
if !path.is_dir() {
221223
bail!("Path must be a directory");
222224
}
223225

224-
let last_three_segments = agent_config_dir();
225-
if path.ends_with(&last_three_segments) {
226-
path
227-
} else {
228-
path.join(&last_three_segments)
229-
}
226+
agent_config_dir(path)?
230227
} else {
231228
directories::chat_global_agent_path(os)?
232229
};

crates/chat-cli/src/util/directories.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use std::path::PathBuf;
1+
use std::path::{
2+
PathBuf,
3+
StripPrefixError,
4+
};
25

36
use thiserror::Error;
47

@@ -23,6 +26,8 @@ pub enum DirectoryError {
2326
FromVecWithNul(#[from] std::ffi::FromVecWithNulError),
2427
#[error(transparent)]
2528
IntoString(#[from] std::ffi::IntoStringError),
29+
#[error(transparent)]
30+
StripPrefix(#[from] StripPrefixError),
2631
}
2732

2833
type Result<T, E = DirectoryError> = std::result::Result<T, E>;
@@ -142,23 +147,22 @@ pub fn chat_legacy_mcp_config(os: &Os) -> Result<PathBuf> {
142147

143148
/// The directory to the directory containing global agents
144149
pub fn chat_global_agent_path(os: &Os) -> Result<PathBuf> {
145-
Ok(home_dir(os)?.join(agent_config_dir()))
150+
Ok(home_dir(os)?.join(".aws").join("amazonq").join("agents"))
146151
}
147152

148153
/// The directory to the directory containing config for the `/context` feature in `q chat`.
149154
pub fn chat_local_agent_dir() -> Result<PathBuf> {
150155
let cwd = std::env::current_dir()?;
151-
Ok(cwd.join(agent_config_dir()))
156+
Ok(cwd.join(".amazonq").join("agents"))
152157
}
153158

154-
/// The relative path to the agent configuration directory
159+
/// Derives the absolute path to an agent config directory given a "workspace directory".
160+
/// A workspace directory is a directory where q chat is to be launched
155161
///
156-
/// This directory contains agent configuration files for Amazon Q.
157-
/// The path is relative and should be joined with either the home directory
158-
/// for global agents or the current working directory for local agents.
159-
// TODO [dingfeli]: implement Brandon's suggestion: https://github.com/aws/amazon-q-developer-cli/pull/2307#discussion_r2207989985
160-
pub fn agent_config_dir() -> PathBuf {
161-
PathBuf::from(".aws/amazonq/agents")
162+
/// For example, if the given path is /path/one, then the derived config path would be
163+
/// `/path/one/.amazonq/agents`
164+
pub fn agent_config_dir(workspace_dir: PathBuf) -> Result<PathBuf> {
165+
Ok(workspace_dir.join(".amazonq/agents"))
162166
}
163167

164168
/// The directory to the directory containing config for the `/context` feature in `q chat`.

0 commit comments

Comments
 (0)