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        cx.spawn(async move |cx| {
45            let (command, root_dir, login) = store
46                .update(cx, |store, cx| {
47                    let agent = store
48                        .get_external_agent(&CLAUDE_CODE_NAME.into())
49                        .context("Claude Code is not registered")?;
50                    anyhow::Ok(agent.get_command(
51                        root_dir.as_deref(),
52                        Default::default(),
53                        delegate.status_tx,
54                        delegate.new_version_available,
55                        &mut cx.to_async(),
56                    ))
57                })??
58                .await?;
59            let connection =
60                crate::acp::connect(name, command, root_dir.as_ref(), is_remote, cx).await?;
61            Ok((connection, login))
62        })
63    }
64
65    fn into_any(self: Rc<Self>) -> Rc<dyn Any> {
66        self
67    }
68}