1use crate::{AgentServerCommand, AgentServerDelegate};
2use acp_thread::AgentConnection;
3use anyhow::Result;
4use gpui::{App, SharedString, Task};
5use std::{path::Path, rc::Rc};
6use ui::IconName;
7
8/// A generic agent server implementation for custom user-defined agents
9pub struct CustomAgentServer {
10 name: SharedString,
11 command: AgentServerCommand,
12}
13
14impl CustomAgentServer {
15 pub fn new(name: SharedString, command: AgentServerCommand) -> Self {
16 Self { name, command }
17 }
18}
19
20impl crate::AgentServer for CustomAgentServer {
21 fn telemetry_id(&self) -> &'static str {
22 "custom"
23 }
24
25 fn name(&self) -> SharedString {
26 self.name.clone()
27 }
28
29 fn logo(&self) -> IconName {
30 IconName::Terminal
31 }
32
33 fn connect(
34 &self,
35 root_dir: &Path,
36 delegate: AgentServerDelegate,
37 cx: &mut App,
38 ) -> Task<Result<Rc<dyn AgentConnection>>> {
39 let server_name = self.name();
40 let mut command = self.command.clone();
41 let root_dir = root_dir.to_path_buf();
42
43 // Get the project environment variables for the root directory
44 let project_env = delegate.project().update(cx, |project, cx| {
45 project.directory_environment(root_dir.as_path().into(), cx)
46 });
47
48 cx.spawn(async move |cx| {
49 // Start with project environment variables (from shell, .env files, etc.)
50 let mut env = project_env.await.unwrap_or_default();
51
52 // Merge with any existing command env (command env takes precedence)
53 if let Some(command_env) = &command.env {
54 env.extend(command_env.clone());
55 }
56
57 // Set the merged environment back on the command
58 command.env = Some(env);
59
60 crate::acp::connect(server_name, command, &root_dir, cx).await
61 })
62 }
63
64 fn into_any(self: Rc<Self>) -> Rc<dyn std::any::Any> {
65 self
66 }
67}