1use crate::{AgentServerDelegate, load_proxy_env};
2use acp_thread::AgentConnection;
3use agent_client_protocol as acp;
4use anyhow::{Context as _, Result};
5use fs::Fs;
6use gpui::{App, AppContext as _, SharedString, Task};
7use project::agent_server_store::{AllAgentServersSettings, ExternalAgentServerName};
8use settings::{SettingsStore, update_settings_file};
9use std::{path::Path, rc::Rc, sync::Arc};
10use ui::IconName;
11
12/// A generic agent server implementation for custom user-defined agents
13pub struct CustomAgentServer {
14 name: SharedString,
15}
16
17impl CustomAgentServer {
18 pub fn new(name: SharedString) -> Self {
19 Self { name }
20 }
21}
22
23impl crate::AgentServer for CustomAgentServer {
24 fn telemetry_id(&self) -> &'static str {
25 "custom"
26 }
27
28 fn name(&self) -> SharedString {
29 self.name.clone()
30 }
31
32 fn logo(&self) -> IconName {
33 IconName::Terminal
34 }
35
36 fn default_mode(&self, cx: &mut App) -> Option<acp::SessionModeId> {
37 let settings = cx.read_global(|settings: &SettingsStore, _| {
38 settings
39 .get::<AllAgentServersSettings>(None)
40 .custom
41 .get(&self.name())
42 .cloned()
43 });
44
45 settings
46 .as_ref()
47 .and_then(|s| s.default_mode.clone().map(|m| acp::SessionModeId(m.into())))
48 }
49
50 fn set_default_mode(&self, mode_id: Option<acp::SessionModeId>, fs: Arc<dyn Fs>, cx: &mut App) {
51 let name = self.name();
52 update_settings_file(fs, cx, move |settings, _| {
53 if let Some(settings) = settings
54 .agent_servers
55 .get_or_insert_default()
56 .custom
57 .get_mut(&name)
58 {
59 settings.default_mode = mode_id.map(|m| m.to_string())
60 }
61 });
62 }
63
64 fn default_model(&self, cx: &mut App) -> Option<acp::ModelId> {
65 let settings = cx.read_global(|settings: &SettingsStore, _| {
66 settings
67 .get::<AllAgentServersSettings>(None)
68 .custom
69 .get(&self.name())
70 .cloned()
71 });
72
73 settings
74 .as_ref()
75 .and_then(|s| s.default_model.clone().map(|m| acp::ModelId(m.into())))
76 }
77
78 fn set_default_model(&self, model_id: Option<acp::ModelId>, fs: Arc<dyn Fs>, cx: &mut App) {
79 let name = self.name();
80 update_settings_file(fs, cx, move |settings, _| {
81 if let Some(settings) = settings
82 .agent_servers
83 .get_or_insert_default()
84 .custom
85 .get_mut(&name)
86 {
87 settings.default_model = model_id.map(|m| m.to_string())
88 }
89 });
90 }
91
92 fn connect(
93 &self,
94 root_dir: Option<&Path>,
95 delegate: AgentServerDelegate,
96 cx: &mut App,
97 ) -> Task<Result<(Rc<dyn AgentConnection>, Option<task::SpawnInTerminal>)>> {
98 let name = self.name();
99 let telemetry_id = self.telemetry_id();
100 let root_dir = root_dir.map(|root_dir| root_dir.to_string_lossy().into_owned());
101 let is_remote = delegate.project.read(cx).is_via_remote_server();
102 let default_mode = self.default_mode(cx);
103 let default_model = self.default_model(cx);
104 let store = delegate.store.downgrade();
105 let extra_env = load_proxy_env(cx);
106
107 cx.spawn(async move |cx| {
108 let (command, root_dir, login) = store
109 .update(cx, |store, cx| {
110 let agent = store
111 .get_external_agent(&ExternalAgentServerName(name.clone()))
112 .with_context(|| {
113 format!("Custom agent server `{}` is not registered", name)
114 })?;
115 anyhow::Ok(agent.get_command(
116 root_dir.as_deref(),
117 extra_env,
118 delegate.status_tx,
119 delegate.new_version_available,
120 &mut cx.to_async(),
121 ))
122 })??
123 .await?;
124 let connection = crate::acp::connect(
125 name,
126 telemetry_id,
127 command,
128 root_dir.as_ref(),
129 default_mode,
130 default_model,
131 is_remote,
132 cx,
133 )
134 .await?;
135 Ok((connection, login))
136 })
137 }
138
139 fn into_any(self: Rc<Self>) -> Rc<dyn std::any::Any> {
140 self
141 }
142}