claude.rs

  1use agent_client_protocol as acp;
  2use fs::Fs;
  3use settings::{SettingsStore, update_settings_file};
  4use std::path::Path;
  5use std::rc::Rc;
  6use std::sync::Arc;
  7use std::{any::Any, path::PathBuf};
  8
  9use anyhow::{Context as _, Result};
 10use gpui::{App, AppContext as _, SharedString, Task};
 11use project::agent_server_store::{AllAgentServersSettings, CLAUDE_CODE_NAME};
 12
 13use crate::{AgentServer, AgentServerDelegate, load_proxy_env};
 14use acp_thread::AgentConnection;
 15
 16#[derive(Clone)]
 17pub struct ClaudeCode;
 18
 19pub struct AgentServerLoginCommand {
 20    pub path: PathBuf,
 21    pub arguments: Vec<String>,
 22}
 23
 24impl AgentServer for ClaudeCode {
 25    fn telemetry_id(&self) -> &'static str {
 26        "claude-code"
 27    }
 28
 29    fn name(&self) -> SharedString {
 30        "Claude Code".into()
 31    }
 32
 33    fn logo(&self) -> ui::IconName {
 34        ui::IconName::AiClaude
 35    }
 36
 37    fn default_mode(&self, cx: &mut App) -> Option<acp::SessionModeId> {
 38        let settings = cx.read_global(|settings: &SettingsStore, _| {
 39            settings.get::<AllAgentServersSettings>(None).claude.clone()
 40        });
 41
 42        settings
 43            .as_ref()
 44            .and_then(|s| s.default_mode.clone().map(|m| acp::SessionModeId(m.into())))
 45    }
 46
 47    fn set_default_mode(&self, mode_id: Option<acp::SessionModeId>, fs: Arc<dyn Fs>, cx: &mut App) {
 48        update_settings_file(fs, cx, |settings, _| {
 49            settings
 50                .agent_servers
 51                .get_or_insert_default()
 52                .claude
 53                .get_or_insert_default()
 54                .default_mode = mode_id.map(|m| m.to_string())
 55        });
 56    }
 57
 58    fn connect(
 59        &self,
 60        root_dir: Option<&Path>,
 61        delegate: AgentServerDelegate,
 62        cx: &mut App,
 63    ) -> Task<Result<(Rc<dyn AgentConnection>, Option<task::SpawnInTerminal>)>> {
 64        let name = self.name();
 65        let root_dir = root_dir.map(|root_dir| root_dir.to_string_lossy().into_owned());
 66        let is_remote = delegate.project.read(cx).is_via_remote_server();
 67        let store = delegate.store.downgrade();
 68        let extra_env = load_proxy_env(cx);
 69        let default_mode = self.default_mode(cx);
 70
 71        cx.spawn(async move |cx| {
 72            let (command, root_dir, login) = store
 73                .update(cx, |store, cx| {
 74                    let agent = store
 75                        .get_external_agent(&CLAUDE_CODE_NAME.into())
 76                        .context("Claude Code is not registered")?;
 77                    anyhow::Ok(agent.get_command(
 78                        root_dir.as_deref(),
 79                        extra_env,
 80                        delegate.status_tx,
 81                        delegate.new_version_available,
 82                        &mut cx.to_async(),
 83                    ))
 84                })??
 85                .await?;
 86            let connection = crate::acp::connect(
 87                name,
 88                command,
 89                root_dir.as_ref(),
 90                default_mode,
 91                is_remote,
 92                cx,
 93            )
 94            .await?;
 95            Ok((connection, login))
 96        })
 97    }
 98
 99    fn into_any(self: Rc<Self>) -> Rc<dyn Any> {
100        self
101    }
102}