Better search paths and handling of CLI providers (#5554)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -2,6 +2,7 @@ use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use rmcp::model::Role;
|
||||
use serde_json::{json, Value};
|
||||
use std::ffi::OsString;
|
||||
use std::path::PathBuf;
|
||||
use std::process::Stdio;
|
||||
use tokio::io::{AsyncBufReadExt, BufReader};
|
||||
@@ -10,8 +11,11 @@ use tokio::process::Command;
|
||||
use super::base::{ConfigKey, Provider, ProviderMetadata, ProviderUsage, Usage};
|
||||
use super::errors::ProviderError;
|
||||
use super::utils::{filter_extensions_from_system_prompt, RequestLog};
|
||||
use crate::config::base::CursorAgentCommand;
|
||||
use crate::config::search_path::SearchPaths;
|
||||
use crate::conversation::message::{Message, MessageContent};
|
||||
use crate::model::ModelConfig;
|
||||
use crate::subprocess::configure_command_no_window;
|
||||
use rmcp::model::Tool;
|
||||
|
||||
pub const CURSOR_AGENT_DEFAULT_MODEL: &str = "auto";
|
||||
@@ -21,7 +25,7 @@ pub const CURSOR_AGENT_DOC_URL: &str = "https://docs.cursor.com/en/cli/overview"
|
||||
|
||||
#[derive(Debug, serde::Serialize)]
|
||||
pub struct CursorAgentProvider {
|
||||
command: String,
|
||||
command: PathBuf,
|
||||
model: ModelConfig,
|
||||
#[serde(skip)]
|
||||
name: String,
|
||||
@@ -30,15 +34,8 @@ pub struct CursorAgentProvider {
|
||||
impl CursorAgentProvider {
|
||||
pub async fn from_env(model: ModelConfig) -> Result<Self> {
|
||||
let config = crate::config::Config::global();
|
||||
let command: String = config
|
||||
.get_param("CURSOR_AGENT_COMMAND")
|
||||
.unwrap_or_else(|_| "cursor-agent".to_string());
|
||||
|
||||
let resolved_command = if !command.contains('/') {
|
||||
Self::find_cursor_agent_executable(&command).unwrap_or(command)
|
||||
} else {
|
||||
command
|
||||
};
|
||||
let command: OsString = config.get_cursor_agent_command().unwrap_or_default().into();
|
||||
let resolved_command = SearchPaths::builder().with_npm().resolve(command)?;
|
||||
|
||||
Ok(Self {
|
||||
command: resolved_command,
|
||||
@@ -58,60 +55,6 @@ impl CursorAgentProvider {
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
/// Search for cursor-agent executable in common installation locations
|
||||
fn find_cursor_agent_executable(command_name: &str) -> Option<String> {
|
||||
let home = std::env::var("HOME").ok()?;
|
||||
|
||||
let search_paths = vec![
|
||||
format!("/opt/homebrew/bin/{}", command_name),
|
||||
format!("/usr/bin/{}", command_name),
|
||||
format!("/usr/local/bin/{}", command_name),
|
||||
format!("{}/.local/bin/{}", home, command_name),
|
||||
format!("{}/bin/{}", home, command_name),
|
||||
];
|
||||
|
||||
for path in search_paths {
|
||||
let path_buf = PathBuf::from(&path);
|
||||
if path_buf.exists() && path_buf.is_file() {
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
if let Ok(metadata) = std::fs::metadata(&path_buf) {
|
||||
let permissions = metadata.permissions();
|
||||
if permissions.mode() & 0o111 != 0 {
|
||||
tracing::info!("Found cursor-agent executable at: {}", path);
|
||||
return Some(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
{
|
||||
tracing::info!("Found cursor-agent executable at: {}", path);
|
||||
return Some(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Ok(path_var) = std::env::var("PATH") {
|
||||
#[cfg(unix)]
|
||||
let path_separator = ':';
|
||||
#[cfg(windows)]
|
||||
let path_separator = ';';
|
||||
|
||||
for dir in path_var.split(path_separator) {
|
||||
let path_buf = PathBuf::from(dir).join(command_name);
|
||||
if path_buf.exists() && path_buf.is_file() {
|
||||
let full_path = path_buf.to_string_lossy().to_string();
|
||||
tracing::info!("Found cursor-agent executable in PATH at: {}", full_path);
|
||||
return Some(full_path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tracing::warn!("Could not find cursor-agent executable in common locations");
|
||||
None
|
||||
}
|
||||
|
||||
/// Convert goose messages to a simple prompt format for cursor-agent CLI
|
||||
fn messages_to_cursor_agent_format(&self, system: &str, messages: &[Message]) -> String {
|
||||
let mut full_prompt = String::new();
|
||||
@@ -240,7 +183,7 @@ impl CursorAgentProvider {
|
||||
|
||||
if std::env::var("GOOSE_CURSOR_AGENT_DEBUG").is_ok() {
|
||||
println!("=== CURSOR AGENT PROVIDER DEBUG ===");
|
||||
println!("Command: {}", self.command);
|
||||
println!("Command: {:?}", self.command);
|
||||
println!("Original system prompt length: {} chars", system.len());
|
||||
println!(
|
||||
"Filtered system prompt length: {} chars",
|
||||
@@ -252,6 +195,11 @@ impl CursorAgentProvider {
|
||||
}
|
||||
|
||||
let mut cmd = Command::new(&self.command);
|
||||
configure_command_no_window(&mut cmd);
|
||||
|
||||
if let Ok(path) = SearchPaths::builder().with_npm().path() {
|
||||
cmd.env("PATH", path);
|
||||
}
|
||||
|
||||
// Only pass model parameter if it's in the known models list
|
||||
if CURSOR_AGENT_KNOWN_MODELS.contains(&self.model.model_name.as_str()) {
|
||||
@@ -269,8 +217,8 @@ impl CursorAgentProvider {
|
||||
let mut child = cmd
|
||||
.spawn()
|
||||
.map_err(|e| ProviderError::RequestFailed(format!(
|
||||
"Failed to spawn cursor-agent CLI command '{}': {}. \
|
||||
Make sure the cursor-agent CLI is installed and in your PATH, or set CURSOR_AGENT_COMMAND in your config to the correct path.",
|
||||
"Failed to spawn cursor-agent CLI command '{:?}': {}. \
|
||||
Make sure the cursor-agent CLI is installed and available in the configured search paths, or set CURSOR_AGENT_COMMAND in your config to the correct path.",
|
||||
self.command, e
|
||||
)))?;
|
||||
|
||||
@@ -382,11 +330,8 @@ impl Provider for CursorAgentProvider {
|
||||
CURSOR_AGENT_DEFAULT_MODEL,
|
||||
CURSOR_AGENT_KNOWN_MODELS.to_vec(),
|
||||
CURSOR_AGENT_DOC_URL,
|
||||
vec![ConfigKey::new(
|
||||
"CURSOR_AGENT_COMMAND",
|
||||
false,
|
||||
false,
|
||||
Some("cursor-agent"),
|
||||
vec![ConfigKey::from_value_type::<CursorAgentCommand>(
|
||||
true, false,
|
||||
)],
|
||||
)
|
||||
}
|
||||
@@ -442,19 +387,3 @@ impl Provider for CursorAgentProvider {
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::ModelConfig;
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_cursor_agent_valid_model() {
|
||||
// Test that a valid model is preserved
|
||||
let valid_model = ModelConfig::new_or_fail("gpt-5");
|
||||
let provider = CursorAgentProvider::from_env(valid_model).await.unwrap();
|
||||
let config = provider.get_model_config();
|
||||
|
||||
assert_eq!(config.model_name, "gpt-5");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user