assistant2: Extract method for adding a new profile to the settings (#27810)

Marshall Bowers created

This PR extracts a method for adding a new profile to the settings to
reduce the amount of code required inline.

Release Notes:

- N/A

Change summary

crates/assistant2/src/assistant_configuration/manage_profiles_modal.rs | 39 
crates/assistant_settings/src/assistant_settings.rs                    | 50 
2 files changed, 46 insertions(+), 43 deletions(-)

Detailed changes

crates/assistant2/src/assistant_configuration/manage_profiles_modal.rs 🔗

@@ -2,10 +2,7 @@ mod profile_modal_header;
 
 use std::sync::Arc;
 
-use assistant_settings::{
-    AgentProfile, AgentProfileContent, AssistantSettings, AssistantSettingsContent,
-    ContextServerPresetContent, VersionedAssistantSettingsContent,
-};
+use assistant_settings::{AgentProfile, AssistantSettings};
 use assistant_tool::ToolWorkingSet;
 use convert_case::{Case, Casing as _};
 use editor::Editor;
@@ -18,6 +15,7 @@ use settings::{Settings as _, update_settings_file};
 use ui::{
     KeyBinding, ListItem, ListItemSpacing, ListSeparator, Navigable, NavigableEntry, prelude::*,
 };
+use util::ResultExt as _;
 use workspace::{ModalView, Workspace};
 
 use crate::assistant_configuration::manage_profiles_modal::profile_modal_header::ProfileModalHeader;
@@ -261,37 +259,8 @@ impl ManageProfilesModal {
 
     fn create_profile(&self, profile_id: Arc<str>, profile: AgentProfile, cx: &mut Context<Self>) {
         update_settings_file::<AssistantSettings>(self.fs.clone(), cx, {
-            move |settings, _cx| match settings {
-                AssistantSettingsContent::Versioned(VersionedAssistantSettingsContent::V2(
-                    settings,
-                )) => {
-                    let profiles = settings.profiles.get_or_insert_default();
-                    if profiles.contains_key(&profile_id) {
-                        log::error!("profile with ID '{profile_id}' already exists");
-                        return;
-                    }
-
-                    profiles.insert(
-                        profile_id,
-                        AgentProfileContent {
-                            name: profile.name.into(),
-                            tools: profile.tools,
-                            context_servers: profile
-                                .context_servers
-                                .into_iter()
-                                .map(|(server_id, preset)| {
-                                    (
-                                        server_id,
-                                        ContextServerPresetContent {
-                                            tools: preset.tools,
-                                        },
-                                    )
-                                })
-                                .collect(),
-                        },
-                    );
-                }
-                _ => {}
+            move |settings, _cx| {
+                settings.create_profile(profile_id, profile).log_err();
             }
         });
     }

crates/assistant_settings/src/assistant_settings.rs 🔗

@@ -4,6 +4,7 @@ use std::sync::Arc;
 
 use ::open_ai::Model as OpenAiModel;
 use anthropic::Model as AnthropicModel;
+use anyhow::{Result, bail};
 use deepseek::Model as DeepseekModel;
 use feature_flags::{Assistant2FeatureFlag, FeatureFlagAppExt};
 use gpui::{App, Pixels};
@@ -325,15 +326,48 @@ impl AssistantSettingsContent {
     }
 
     pub fn set_profile(&mut self, profile_id: Arc<str>) {
-        match self {
-            AssistantSettingsContent::Versioned(settings) => match settings {
-                VersionedAssistantSettingsContent::V2(settings) => {
-                    settings.default_profile = Some(profile_id);
-                }
-                VersionedAssistantSettingsContent::V1(_) => {}
-            },
-            AssistantSettingsContent::Legacy(_) => {}
+        let AssistantSettingsContent::Versioned(VersionedAssistantSettingsContent::V2(settings)) =
+            self
+        else {
+            return;
+        };
+
+        settings.default_profile = Some(profile_id);
+    }
+
+    pub fn create_profile(&mut self, profile_id: Arc<str>, profile: AgentProfile) -> Result<()> {
+        let AssistantSettingsContent::Versioned(VersionedAssistantSettingsContent::V2(settings)) =
+            self
+        else {
+            return Ok(());
+        };
+
+        let profiles = settings.profiles.get_or_insert_default();
+        if profiles.contains_key(&profile_id) {
+            bail!("profile with ID '{profile_id}' already exists");
         }
+
+        profiles.insert(
+            profile_id,
+            AgentProfileContent {
+                name: profile.name.into(),
+                tools: profile.tools,
+                context_servers: profile
+                    .context_servers
+                    .into_iter()
+                    .map(|(server_id, preset)| {
+                        (
+                            server_id,
+                            ContextServerPresetContent {
+                                tools: preset.tools,
+                            },
+                        )
+                    })
+                    .collect(),
+            },
+        );
+
+        Ok(())
     }
 }