agent.rs

  1use collections::{HashMap, IndexMap};
  2use gpui::SharedString;
  3use schemars::{JsonSchema, json_schema};
  4use serde::{Deserialize, Serialize};
  5use serde_with::skip_serializing_none;
  6use settings_macros::MergeFrom;
  7use std::{borrow::Cow, path::PathBuf, sync::Arc};
  8
  9use crate::DockPosition;
 10
 11#[skip_serializing_none]
 12#[derive(Clone, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom, Debug, Default)]
 13pub struct AgentSettingsContent {
 14    /// Whether the Agent is enabled.
 15    ///
 16    /// Default: true
 17    pub enabled: Option<bool>,
 18    /// Whether to show the agent panel button in the status bar.
 19    ///
 20    /// Default: true
 21    pub button: Option<bool>,
 22    /// Where to dock the agent panel.
 23    ///
 24    /// Default: right
 25    pub dock: Option<DockPosition>,
 26    /// Default width in pixels when the agent panel is docked to the left or right.
 27    ///
 28    /// Default: 640
 29    pub default_width: Option<f32>,
 30    /// Default height in pixels when the agent panel is docked to the bottom.
 31    ///
 32    /// Default: 320
 33    pub default_height: Option<f32>,
 34    /// The default model to use when creating new chats and for other features when a specific model is not specified.
 35    pub default_model: Option<LanguageModelSelection>,
 36    /// Model to use for the inline assistant. Defaults to default_model when not specified.
 37    pub inline_assistant_model: Option<LanguageModelSelection>,
 38    /// Model to use for generating git commit messages. Defaults to default_model when not specified.
 39    pub commit_message_model: Option<LanguageModelSelection>,
 40    /// Model to use for generating thread summaries. Defaults to default_model when not specified.
 41    pub thread_summary_model: Option<LanguageModelSelection>,
 42    /// Additional models with which to generate alternatives when performing inline assists.
 43    pub inline_alternatives: Option<Vec<LanguageModelSelection>>,
 44    /// The default profile to use in the Agent.
 45    ///
 46    /// Default: write
 47    pub default_profile: Option<Arc<str>>,
 48    /// Which view type to show by default in the agent panel.
 49    ///
 50    /// Default: "thread"
 51    pub default_view: Option<DefaultAgentView>,
 52    /// The available agent profiles.
 53    pub profiles: Option<IndexMap<Arc<str>, AgentProfileContent>>,
 54    /// Whenever a tool action would normally wait for your confirmation
 55    /// that you allow it, always choose to allow it.
 56    ///
 57    /// This setting has no effect on external agents that support permission modes, such as Claude Code.
 58    ///
 59    /// Set `agent_servers.claude.default_mode` to `bypassPermissions`, to disable all permission requests when using Claude Code.
 60    ///
 61    /// Default: false
 62    pub always_allow_tool_actions: Option<bool>,
 63    /// Where to show a popup notification when the agent is waiting for user input.
 64    ///
 65    /// Default: "primary_screen"
 66    pub notify_when_agent_waiting: Option<NotifyWhenAgentWaiting>,
 67    /// Whether to play a sound when the agent has either completed its response, or needs user input.
 68    ///
 69    /// Default: false
 70    pub play_sound_when_agent_done: Option<bool>,
 71    /// Whether to stream edits from the agent as they are received.
 72    ///
 73    /// Default: false
 74    pub stream_edits: Option<bool>,
 75    /// Whether to display agent edits in single-file editors in addition to the review multibuffer pane.
 76    ///
 77    /// Default: true
 78    pub single_file_review: Option<bool>,
 79    /// Additional parameters for language model requests. When making a request
 80    /// to a model, parameters will be taken from the last entry in this list
 81    /// that matches the model's provider and name. In each entry, both provider
 82    /// and model are optional, so that you can specify parameters for either
 83    /// one.
 84    ///
 85    /// Default: []
 86    #[serde(default)]
 87    pub model_parameters: Vec<LanguageModelParameters>,
 88    /// What completion mode to enable for new threads
 89    ///
 90    /// Default: normal
 91    pub preferred_completion_mode: Option<CompletionMode>,
 92    /// Whether to show thumb buttons for feedback in the agent panel.
 93    ///
 94    /// Default: true
 95    pub enable_feedback: Option<bool>,
 96    /// Whether to have edit cards in the agent panel expanded, showing a preview of the full diff.
 97    ///
 98    /// Default: true
 99    pub expand_edit_card: Option<bool>,
100    /// Whether to have terminal cards in the agent panel expanded, showing the whole command output.
101    ///
102    /// Default: true
103    pub expand_terminal_card: Option<bool>,
104    /// Whether to always use cmd-enter (or ctrl-enter on Linux or Windows) to send messages in the agent panel.
105    ///
106    /// Default: false
107    pub use_modifier_to_send: Option<bool>,
108    /// Minimum number of lines of height the agent message editor should have.
109    ///
110    /// Default: 4
111    pub message_editor_min_lines: Option<usize>,
112}
113
114impl AgentSettingsContent {
115    pub fn set_dock(&mut self, dock: DockPosition) {
116        self.dock = Some(dock);
117    }
118
119    pub fn set_model(&mut self, language_model: LanguageModelSelection) {
120        // let model = language_model.id().0.to_string();
121        // let provider = language_model.provider_id().0.to_string();
122        // self.default_model = Some(LanguageModelSelection {
123        //     provider: provider.into(),
124        //     model,
125        // });
126        self.default_model = Some(language_model)
127    }
128
129    pub fn set_inline_assistant_model(&mut self, provider: String, model: String) {
130        self.inline_assistant_model = Some(LanguageModelSelection {
131            provider: provider.into(),
132            model,
133        });
134    }
135
136    pub fn set_commit_message_model(&mut self, provider: String, model: String) {
137        self.commit_message_model = Some(LanguageModelSelection {
138            provider: provider.into(),
139            model,
140        });
141    }
142
143    pub fn set_thread_summary_model(&mut self, provider: String, model: String) {
144        self.thread_summary_model = Some(LanguageModelSelection {
145            provider: provider.into(),
146            model,
147        });
148    }
149
150    pub fn set_always_allow_tool_actions(&mut self, allow: bool) {
151        self.always_allow_tool_actions = Some(allow);
152    }
153
154    pub fn set_play_sound_when_agent_done(&mut self, allow: bool) {
155        self.play_sound_when_agent_done = Some(allow);
156    }
157
158    pub fn set_single_file_review(&mut self, allow: bool) {
159        self.single_file_review = Some(allow);
160    }
161
162    pub fn set_use_modifier_to_send(&mut self, always_use: bool) {
163        self.use_modifier_to_send = Some(always_use);
164    }
165
166    pub fn set_profile(&mut self, profile_id: Arc<str>) {
167        self.default_profile = Some(profile_id);
168    }
169}
170
171#[skip_serializing_none]
172#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, JsonSchema, MergeFrom)]
173pub struct AgentProfileContent {
174    pub name: Arc<str>,
175    #[serde(default)]
176    pub tools: IndexMap<Arc<str>, bool>,
177    /// Whether all context servers are enabled by default.
178    pub enable_all_context_servers: Option<bool>,
179    #[serde(default)]
180    pub context_servers: IndexMap<Arc<str>, ContextServerPresetContent>,
181}
182
183#[skip_serializing_none]
184#[derive(Debug, PartialEq, Clone, Default, Serialize, Deserialize, JsonSchema, MergeFrom)]
185pub struct ContextServerPresetContent {
186    pub tools: IndexMap<Arc<str>, bool>,
187}
188
189#[derive(Copy, Clone, Default, Debug, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)]
190#[serde(rename_all = "snake_case")]
191pub enum DefaultAgentView {
192    #[default]
193    Thread,
194    TextThread,
195}
196
197#[derive(
198    Copy,
199    Clone,
200    Default,
201    Debug,
202    Serialize,
203    Deserialize,
204    JsonSchema,
205    MergeFrom,
206    PartialEq,
207    strum::VariantArray,
208    strum::VariantNames,
209)]
210#[serde(rename_all = "snake_case")]
211pub enum NotifyWhenAgentWaiting {
212    #[default]
213    PrimaryScreen,
214    AllScreens,
215    Never,
216}
217
218#[skip_serializing_none]
219#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, MergeFrom, PartialEq)]
220pub struct LanguageModelSelection {
221    pub provider: LanguageModelProviderSetting,
222    pub model: String,
223}
224
225#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, MergeFrom, PartialEq, Default)]
226#[serde(rename_all = "snake_case")]
227pub enum CompletionMode {
228    #[default]
229    Normal,
230    #[serde(alias = "max")]
231    Burn,
232}
233
234#[skip_serializing_none]
235#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, MergeFrom, PartialEq)]
236pub struct LanguageModelParameters {
237    pub provider: Option<LanguageModelProviderSetting>,
238    pub model: Option<SharedString>,
239    pub temperature: Option<f32>,
240}
241
242#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, MergeFrom)]
243pub struct LanguageModelProviderSetting(pub String);
244
245impl JsonSchema for LanguageModelProviderSetting {
246    fn schema_name() -> Cow<'static, str> {
247        "LanguageModelProviderSetting".into()
248    }
249
250    fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
251        // list the builtin providers as a subset so that we still auto complete them in the settings
252        json_schema!({
253            "anyOf": [
254                {
255                    "type": "string",
256                    "enum": [
257                        "amazon-bedrock",
258                        "anthropic",
259                        "copilot_chat",
260                        "deepseek",
261                        "google",
262                        "lmstudio",
263                        "mistral",
264                        "ollama",
265                        "openai",
266                        "openrouter",
267                        "vercel",
268                        "x_ai",
269                        "zed.dev"
270                    ]
271                },
272                {
273                    "type": "string",
274                }
275            ]
276        })
277    }
278}
279
280impl From<String> for LanguageModelProviderSetting {
281    fn from(provider: String) -> Self {
282        Self(provider)
283    }
284}
285
286impl From<&str> for LanguageModelProviderSetting {
287    fn from(provider: &str) -> Self {
288        Self(provider.to_string())
289    }
290}
291
292#[skip_serializing_none]
293#[derive(Default, PartialEq, Deserialize, Serialize, Clone, JsonSchema, MergeFrom, Debug)]
294pub struct AllAgentServersSettings {
295    pub gemini: Option<BuiltinAgentServerSettings>,
296    pub claude: Option<BuiltinAgentServerSettings>,
297    pub codex: Option<BuiltinAgentServerSettings>,
298
299    /// Custom agent servers configured by the user
300    #[serde(flatten)]
301    pub custom: HashMap<SharedString, CustomAgentServerSettings>,
302}
303
304#[skip_serializing_none]
305#[derive(Default, Deserialize, Serialize, Clone, JsonSchema, MergeFrom, Debug, PartialEq)]
306pub struct BuiltinAgentServerSettings {
307    /// Absolute path to a binary to be used when launching this agent.
308    ///
309    /// This can be used to run a specific binary without automatic downloads or searching `$PATH`.
310    #[serde(rename = "command")]
311    pub path: Option<PathBuf>,
312    /// If a binary is specified in `command`, it will be passed these arguments.
313    pub args: Option<Vec<String>>,
314    /// If a binary is specified in `command`, it will be passed these environment variables.
315    pub env: Option<HashMap<String, String>>,
316    /// Whether to skip searching `$PATH` for an agent server binary when
317    /// launching this agent.
318    ///
319    /// This has no effect if a `command` is specified. Otherwise, when this is
320    /// `false`, Zed will search `$PATH` for an agent server binary and, if one
321    /// is found, use it for threads with this agent. If no agent binary is
322    /// found on `$PATH`, Zed will automatically install and use its own binary.
323    /// When this is `true`, Zed will not search `$PATH`, and will always use
324    /// its own binary.
325    ///
326    /// Default: true
327    pub ignore_system_version: Option<bool>,
328    /// The default mode to use for this agent.
329    ///
330    /// Note: Not only all agents support modes.
331    ///
332    /// Default: None
333    pub default_mode: Option<String>,
334}
335
336#[skip_serializing_none]
337#[derive(Deserialize, Serialize, Clone, JsonSchema, MergeFrom, Debug, PartialEq)]
338pub struct CustomAgentServerSettings {
339    #[serde(rename = "command")]
340    pub path: PathBuf,
341    #[serde(default)]
342    pub args: Vec<String>,
343    pub env: Option<HashMap<String, String>>,
344    /// The default mode to use for this agent.
345    ///
346    /// Note: Not only all agents support modes.
347    ///
348    /// Default: None
349    pub default_mode: Option<String>,
350}