Restore gemini change
This commit is contained in:
parent
477731d77d
commit
b48faddaf4
1 changed files with 152 additions and 28 deletions
|
@ -1,18 +1,25 @@
|
||||||
use project::Project;
|
use anyhow::anyhow;
|
||||||
use settings::SettingsStore;
|
use std::cell::RefCell;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
use util::ResultExt as _;
|
||||||
|
|
||||||
use anyhow::Result;
|
use crate::{AgentServer, AgentServerCommand, AgentServerVersion};
|
||||||
use gpui::{App, Entity, Task};
|
use acp_thread::{AgentConnection, LoadError, OldAcpAgentConnection, OldAcpClientDelegate};
|
||||||
|
use agentic_coding_protocol as acp_old;
|
||||||
|
use anyhow::{Context as _, Result};
|
||||||
|
use gpui::{AppContext as _, AsyncApp, Entity, Task, WeakEntity};
|
||||||
|
use project::Project;
|
||||||
|
use settings::SettingsStore;
|
||||||
|
use ui::App;
|
||||||
|
|
||||||
use crate::acp_connection::AcpConnection;
|
use crate::AllAgentServersSettings;
|
||||||
use crate::{AgentServer, AgentServerCommand, AllAgentServersSettings};
|
|
||||||
use acp_thread::AgentConnection;
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Gemini;
|
pub struct Gemini;
|
||||||
|
|
||||||
|
const ACP_ARG: &str = "--experimental-acp";
|
||||||
|
|
||||||
impl AgentServer for Gemini {
|
impl AgentServer for Gemini {
|
||||||
fn name(&self) -> &'static str {
|
fn name(&self) -> &'static str {
|
||||||
"Gemini"
|
"Gemini"
|
||||||
|
@ -32,49 +39,166 @@ impl AgentServer for Gemini {
|
||||||
|
|
||||||
fn connect(
|
fn connect(
|
||||||
&self,
|
&self,
|
||||||
_root_dir: &Path,
|
root_dir: &Path,
|
||||||
project: &Entity<Project>,
|
project: &Entity<Project>,
|
||||||
cx: &mut App,
|
cx: &mut App,
|
||||||
) -> Task<Result<Rc<dyn AgentConnection>>> {
|
) -> Task<Result<Rc<dyn AgentConnection>>> {
|
||||||
|
let root_dir = root_dir.to_path_buf();
|
||||||
let project = project.clone();
|
let project = project.clone();
|
||||||
let server_name = self.name();
|
let this = self.clone();
|
||||||
|
let name = self.name();
|
||||||
|
|
||||||
cx.spawn(async move |cx| {
|
cx.spawn(async move |cx| {
|
||||||
let settings = cx.read_global(|settings: &SettingsStore, _| {
|
let command = this.command(&project, cx).await?;
|
||||||
settings.get::<AllAgentServersSettings>(None).gemini.clone()
|
|
||||||
})?;
|
|
||||||
|
|
||||||
let Some(command) = AgentServerCommand::resolve(
|
let mut child = util::command::new_smol_command(&command.path)
|
||||||
"gemini",
|
.args(command.args.iter())
|
||||||
&["--experimental-mcp"],
|
.current_dir(root_dir)
|
||||||
settings,
|
.stdin(std::process::Stdio::piped())
|
||||||
&project,
|
.stdout(std::process::Stdio::piped())
|
||||||
cx,
|
.stderr(std::process::Stdio::inherit())
|
||||||
)
|
.kill_on_drop(true)
|
||||||
.await
|
.spawn()?;
|
||||||
else {
|
|
||||||
anyhow::bail!("Failed to find gemini binary");
|
|
||||||
};
|
|
||||||
|
|
||||||
let conn = AcpConnection::stdio(server_name, command, cx).await?;
|
let stdin = child.stdin.take().unwrap();
|
||||||
Ok(Rc::new(conn) as _)
|
let stdout = child.stdout.take().unwrap();
|
||||||
|
|
||||||
|
let foreground_executor = cx.foreground_executor().clone();
|
||||||
|
|
||||||
|
let thread_rc = Rc::new(RefCell::new(WeakEntity::new_invalid()));
|
||||||
|
|
||||||
|
let (connection, io_fut) = acp_old::AgentConnection::connect_to_agent(
|
||||||
|
OldAcpClientDelegate::new(thread_rc.clone(), cx.clone()),
|
||||||
|
stdin,
|
||||||
|
stdout,
|
||||||
|
move |fut| foreground_executor.spawn(fut).detach(),
|
||||||
|
);
|
||||||
|
|
||||||
|
let io_task = cx.background_spawn(async move {
|
||||||
|
io_fut.await.log_err();
|
||||||
|
});
|
||||||
|
|
||||||
|
let child_status = cx.background_spawn(async move {
|
||||||
|
let result = match child.status().await {
|
||||||
|
Err(e) => Err(anyhow!(e)),
|
||||||
|
Ok(result) if result.success() => Ok(()),
|
||||||
|
Ok(result) => {
|
||||||
|
if let Some(AgentServerVersion::Unsupported {
|
||||||
|
error_message,
|
||||||
|
upgrade_message,
|
||||||
|
upgrade_command,
|
||||||
|
}) = this.version(&command).await.log_err()
|
||||||
|
{
|
||||||
|
Err(anyhow!(LoadError::Unsupported {
|
||||||
|
error_message,
|
||||||
|
upgrade_message,
|
||||||
|
upgrade_command
|
||||||
|
}))
|
||||||
|
} else {
|
||||||
|
Err(anyhow!(LoadError::Exited(result.code().unwrap_or(-127))))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
drop(io_task);
|
||||||
|
result
|
||||||
|
});
|
||||||
|
|
||||||
|
let connection: Rc<dyn AgentConnection> = Rc::new(OldAcpAgentConnection {
|
||||||
|
name,
|
||||||
|
connection,
|
||||||
|
child_status,
|
||||||
|
});
|
||||||
|
|
||||||
|
Ok(connection)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Gemini {
|
||||||
|
async fn command(
|
||||||
|
&self,
|
||||||
|
project: &Entity<Project>,
|
||||||
|
cx: &mut AsyncApp,
|
||||||
|
) -> Result<AgentServerCommand> {
|
||||||
|
let settings = cx.read_global(|settings: &SettingsStore, _| {
|
||||||
|
settings.get::<AllAgentServersSettings>(None).gemini.clone()
|
||||||
|
})?;
|
||||||
|
|
||||||
|
if let Some(command) =
|
||||||
|
AgentServerCommand::resolve("gemini", &[ACP_ARG], settings, &project, cx).await
|
||||||
|
{
|
||||||
|
return Ok(command);
|
||||||
|
};
|
||||||
|
|
||||||
|
let (fs, node_runtime) = project.update(cx, |project, _| {
|
||||||
|
(project.fs().clone(), project.node_runtime().cloned())
|
||||||
|
})?;
|
||||||
|
let node_runtime = node_runtime.context("gemini not found on path")?;
|
||||||
|
|
||||||
|
let directory = ::paths::agent_servers_dir().join("gemini");
|
||||||
|
fs.create_dir(&directory).await?;
|
||||||
|
node_runtime
|
||||||
|
.npm_install_packages(&directory, &[("@google/gemini-cli", "latest")])
|
||||||
|
.await?;
|
||||||
|
let path = directory.join("node_modules/.bin/gemini");
|
||||||
|
|
||||||
|
Ok(AgentServerCommand {
|
||||||
|
path,
|
||||||
|
args: vec![ACP_ARG.into()],
|
||||||
|
env: None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn version(&self, command: &AgentServerCommand) -> Result<AgentServerVersion> {
|
||||||
|
let version_fut = util::command::new_smol_command(&command.path)
|
||||||
|
.args(command.args.iter())
|
||||||
|
.arg("--version")
|
||||||
|
.kill_on_drop(true)
|
||||||
|
.output();
|
||||||
|
|
||||||
|
let help_fut = util::command::new_smol_command(&command.path)
|
||||||
|
.args(command.args.iter())
|
||||||
|
.arg("--help")
|
||||||
|
.kill_on_drop(true)
|
||||||
|
.output();
|
||||||
|
|
||||||
|
let (version_output, help_output) = futures::future::join(version_fut, help_fut).await;
|
||||||
|
|
||||||
|
let current_version = String::from_utf8(version_output?.stdout)?;
|
||||||
|
let supported = String::from_utf8(help_output?.stdout)?.contains(ACP_ARG);
|
||||||
|
|
||||||
|
if supported {
|
||||||
|
Ok(AgentServerVersion::Supported)
|
||||||
|
} else {
|
||||||
|
Ok(AgentServerVersion::Unsupported {
|
||||||
|
error_message: format!(
|
||||||
|
"Your installed version of Gemini {} doesn't support the Agentic Coding Protocol (ACP).",
|
||||||
|
current_version
|
||||||
|
).into(),
|
||||||
|
upgrade_message: "Upgrade Gemini to Latest".into(),
|
||||||
|
upgrade_command: "npm install -g @google/gemini-cli@latest".into(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub(crate) mod tests {
|
pub(crate) mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::AgentServerCommand;
|
use crate::AgentServerCommand;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
crate::common_e2e_tests!(Gemini, allow_option_id = "allow");
|
crate::common_e2e_tests!(Gemini, allow_option_id = "0");
|
||||||
|
|
||||||
pub fn local_command() -> AgentServerCommand {
|
pub fn local_command() -> AgentServerCommand {
|
||||||
let cli_path = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../../gemini/packages/cli");
|
let cli_path = Path::new(env!("CARGO_MANIFEST_DIR"))
|
||||||
|
.join("../../../gemini-cli/packages/cli")
|
||||||
|
.to_string_lossy()
|
||||||
|
.to_string();
|
||||||
|
|
||||||
AgentServerCommand {
|
AgentServerCommand {
|
||||||
path: "node".into(),
|
path: "node".into(),
|
||||||
args: vec![cli_path.to_string_lossy().to_string()],
|
args: vec![cli_path, ACP_ARG.into()],
|
||||||
env: None,
|
env: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue