## Goal This PR creates the initial settings ui structure with the primary goal of making a settings UI that is - Comprehensive: All settings are available through the UI - Correct: Easy to understand the underlying JSON file from the UI - Intuitive - Easy to implement per setting so that UI is not a hindrance to future settings changes ### Structure The overall structure is settings layer -> data layer -> ui layer. The settings layer is the pre-existing settings definitions, that implement the `Settings` trait. The data layer is constructed from settings primarily through the `SettingsUi` trait, and it's associated derive macro. The data layer tracks the grouping of the settings, the json path of the settings, and a data representation of how to render the controls for the setting in the UI, that is either a marker value for the component to use (avoiding a dependency on the `ui` crate) or a custom render function. Abstracting the data layer from the ui layer allows crates depending on `settings` to implement their own UI without having to add additional UI dependencies, thus avoiding circular dependencies. In cases where custom UI is desired, and a creating a custom render function in the same crate is infeasible due to circular dependencies, the current solution is to implement a marker for the component in the `settings` crate, and then handle the rendering of that component in `settings_ui`. ### Foundation This PR creates a macro and a trait both called `SettingsUi`. The `SettingsUi` trait is added as a new trait bound on the `Settings` trait, this allows the type system to guarantee that all settings implement UI functionality. The macro is used to derived the trait for most types, and can be modified through attributes for unique cases as well. A derive-macro is used to generate the settings UI trait impl, allowing it the UI generation to be generated from the static information in our code base (`default.json`, Struct/Enum names, field names, `serde` attributes, etc). This allows the UI to be auto-generated for the most part, and ensures consistency across the UI. #### Immediate Follow ups - Add a new `SettingsPath` trait that will be a trait bound on `SettingsUi` and `Settings` - This trait will replace the `Settings::key` value to enable `SettingsUi` to infer the json path of it's derived type - Figure out how to render `Option<T> where T: SettingsUi` correctly - Handle `serde` attributes in the `SettingsUi` proc macro to correctly get json path from a type's field and identity Release Notes: - N/A --------- Co-authored-by: Ben Kunkle <ben@zed.dev>
112 lines
3.7 KiB
Rust
112 lines
3.7 KiB
Rust
use std::path::PathBuf;
|
|
|
|
use crate::AgentServerCommand;
|
|
use anyhow::Result;
|
|
use collections::HashMap;
|
|
use gpui::{App, SharedString};
|
|
use schemars::JsonSchema;
|
|
use serde::{Deserialize, Serialize};
|
|
use settings::{Settings, SettingsSources, SettingsUi};
|
|
|
|
pub fn init(cx: &mut App) {
|
|
AllAgentServersSettings::register(cx);
|
|
}
|
|
|
|
#[derive(Default, Deserialize, Serialize, Clone, JsonSchema, Debug, SettingsUi)]
|
|
pub struct AllAgentServersSettings {
|
|
pub gemini: Option<BuiltinAgentServerSettings>,
|
|
pub claude: Option<CustomAgentServerSettings>,
|
|
|
|
/// Custom agent servers configured by the user
|
|
#[serde(flatten)]
|
|
pub custom: HashMap<SharedString, CustomAgentServerSettings>,
|
|
}
|
|
|
|
#[derive(Default, Deserialize, Serialize, Clone, JsonSchema, Debug, PartialEq)]
|
|
pub struct BuiltinAgentServerSettings {
|
|
/// Absolute path to a binary to be used when launching this agent.
|
|
///
|
|
/// This can be used to run a specific binary without automatic downloads or searching `$PATH`.
|
|
#[serde(rename = "command")]
|
|
pub path: Option<PathBuf>,
|
|
/// If a binary is specified in `command`, it will be passed these arguments.
|
|
pub args: Option<Vec<String>>,
|
|
/// If a binary is specified in `command`, it will be passed these environment variables.
|
|
pub env: Option<HashMap<String, String>>,
|
|
/// Whether to skip searching `$PATH` for an agent server binary when
|
|
/// launching this agent.
|
|
///
|
|
/// This has no effect if a `command` is specified. Otherwise, when this is
|
|
/// `false`, Zed will search `$PATH` for an agent server binary and, if one
|
|
/// is found, use it for threads with this agent. If no agent binary is
|
|
/// found on `$PATH`, Zed will automatically install and use its own binary.
|
|
/// When this is `true`, Zed will not search `$PATH`, and will always use
|
|
/// its own binary.
|
|
///
|
|
/// Default: true
|
|
pub ignore_system_version: Option<bool>,
|
|
}
|
|
|
|
impl BuiltinAgentServerSettings {
|
|
pub(crate) fn custom_command(self) -> Option<AgentServerCommand> {
|
|
self.path.map(|path| AgentServerCommand {
|
|
path,
|
|
args: self.args.unwrap_or_default(),
|
|
env: self.env,
|
|
})
|
|
}
|
|
}
|
|
|
|
impl From<AgentServerCommand> for BuiltinAgentServerSettings {
|
|
fn from(value: AgentServerCommand) -> Self {
|
|
BuiltinAgentServerSettings {
|
|
path: Some(value.path),
|
|
args: Some(value.args),
|
|
env: value.env,
|
|
..Default::default()
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Deserialize, Serialize, Clone, JsonSchema, Debug, PartialEq)]
|
|
pub struct CustomAgentServerSettings {
|
|
#[serde(flatten)]
|
|
pub command: AgentServerCommand,
|
|
}
|
|
|
|
impl settings::Settings for AllAgentServersSettings {
|
|
const KEY: Option<&'static str> = Some("agent_servers");
|
|
|
|
type FileContent = Self;
|
|
|
|
fn load(sources: SettingsSources<Self::FileContent>, _: &mut App) -> Result<Self> {
|
|
let mut settings = AllAgentServersSettings::default();
|
|
|
|
for AllAgentServersSettings {
|
|
gemini,
|
|
claude,
|
|
custom,
|
|
} in sources.defaults_and_customizations()
|
|
{
|
|
if gemini.is_some() {
|
|
settings.gemini = gemini.clone();
|
|
}
|
|
if claude.is_some() {
|
|
settings.claude = claude.clone();
|
|
}
|
|
|
|
// Merge custom agents
|
|
for (name, config) in custom {
|
|
// Skip built-in agent names to avoid conflicts
|
|
if name != "gemini" && name != "claude" {
|
|
settings.custom.insert(name.clone(), config.clone());
|
|
}
|
|
}
|
|
}
|
|
|
|
Ok(settings)
|
|
}
|
|
|
|
fn import_from_vscode(_vscode: &settings::VsCodeSettings, _current: &mut Self::FileContent) {}
|
|
}
|