claude.rs

 1use std::path::Path;
 2use std::rc::Rc;
 3use std::{any::Any, path::PathBuf};
 4
 5use anyhow::{Context as _, Result};
 6use gpui::{App, SharedString, Task};
 7use project::agent_server_store::CLAUDE_CODE_NAME;
 8
 9use crate::{AgentServer, AgentServerDelegate};
10use acp_thread::AgentConnection;
11
12#[derive(Clone)]
13pub struct ClaudeCode;
14
15pub struct AgentServerLoginCommand {
16    pub path: PathBuf,
17    pub arguments: Vec<String>,
18}
19
20impl AgentServer for ClaudeCode {
21    fn telemetry_id(&self) -> &'static str {
22        "claude-code"
23    }
24
25    fn name(&self) -> SharedString {
26        "Claude Code".into()
27    }
28
29    fn logo(&self) -> ui::IconName {
30        ui::IconName::AiClaude
31    }
32
33    fn connect(
34        &self,
35        root_dir: Option<&Path>,
36        delegate: AgentServerDelegate,
37        cx: &mut App,
38    ) -> Task<Result<(Rc<dyn AgentConnection>, Option<task::SpawnInTerminal>)>> {
39        let name = self.name();
40        let root_dir = root_dir.map(|root_dir| root_dir.to_string_lossy().to_string());
41        let is_remote = delegate.project.read(cx).is_via_remote_server();
42        let store = delegate.store.downgrade();
43
44        // Get the project environment variables for the root directory
45        let project_env = delegate.project().update(cx, |project, cx| {
46            if let Some(path) = project.active_project_directory(cx) {
47                Some(project.directory_environment(path, cx))
48            } else {
49                None
50            }
51        });
52
53        cx.spawn(async move |cx| {
54            let (command, root_dir, login) = store
55                .update(cx, |store, cx| {
56                    let agent = store
57                        .get_external_agent(&CLAUDE_CODE_NAME.into())
58                        .context("Claude Code is not registered")?;
59                    anyhow::Ok(agent.get_command(
60                        root_dir.as_deref(),
61                        Default::default(),
62                        delegate.status_tx,
63                        delegate.new_version_available,
64                        &mut cx.to_async(),
65                    ))
66                })??
67                .await?;
68            let connection =
69                crate::acp::connect(name, command, root_dir.as_ref(), is_remote, cx).await?;
70            Ok((connection, login))
71        })
72    }
73
74    fn into_any(self: Rc<Self>) -> Rc<dyn Any> {
75        self
76    }
77}