agent2: Port profile selector (#36244)

Bennet Bo Fenner created

Release Notes:

- N/A

Change summary

crates/agent2/src/thread.rs             |  4 ++
crates/agent_ui/src/acp/thread_view.rs  | 42 +++++++++++++++++++++-
crates/agent_ui/src/message_editor.rs   | 27 ++++++++++++--
crates/agent_ui/src/profile_selector.rs | 50 ++++++++++++--------------
4 files changed, 91 insertions(+), 32 deletions(-)

Detailed changes

crates/agent2/src/thread.rs 🔗

@@ -499,6 +499,10 @@ impl Thread {
         self.tools.remove(name).is_some()
     }
 
+    pub fn profile(&self) -> &AgentProfileId {
+        &self.profile_id
+    }
+
     pub fn set_profile(&mut self, profile_id: AgentProfileId) {
         self.profile_id = profile_id;
     }

crates/agent_ui/src/acp/thread_view.rs 🔗

@@ -7,7 +7,7 @@ use action_log::ActionLog;
 use agent::{TextThreadStore, ThreadStore};
 use agent_client_protocol::{self as acp};
 use agent_servers::AgentServer;
-use agent_settings::{AgentSettings, CompletionMode, NotifyWhenAgentWaiting};
+use agent_settings::{AgentProfileId, AgentSettings, CompletionMode, NotifyWhenAgentWaiting};
 use anyhow::bail;
 use audio::{Audio, Sound};
 use buffer_diff::BufferDiff;
@@ -16,6 +16,7 @@ use collections::{HashMap, HashSet};
 use editor::scroll::Autoscroll;
 use editor::{Editor, EditorMode, MultiBuffer, PathKey, SelectionEffects};
 use file_icons::FileIcons;
+use fs::Fs;
 use gpui::{
     Action, Animation, AnimationExt, App, BorderStyle, ClickEvent, ClipboardItem, EdgesRefinement,
     Empty, Entity, FocusHandle, Focusable, Hsla, Length, ListOffset, ListState, MouseButton,
@@ -29,6 +30,7 @@ use project::Project;
 use prompt_store::PromptId;
 use rope::Point;
 use settings::{Settings as _, SettingsStore};
+use std::sync::Arc;
 use std::{collections::BTreeMap, process::ExitStatus, rc::Rc, time::Duration};
 use text::Anchor;
 use theme::ThemeSettings;
@@ -45,10 +47,11 @@ use super::entry_view_state::EntryViewState;
 use crate::acp::AcpModelSelectorPopover;
 use crate::acp::message_editor::{MessageEditor, MessageEditorEvent};
 use crate::agent_diff::AgentDiff;
+use crate::profile_selector::{ProfileProvider, ProfileSelector};
 use crate::ui::{AgentNotification, AgentNotificationEvent, BurnModeTooltip};
 use crate::{
     AgentDiffPane, AgentPanel, ContinueThread, ContinueWithBurnMode, ExpandMessageEditor, Follow,
-    KeepAll, OpenAgentDiff, RejectAll, ToggleBurnMode,
+    KeepAll, OpenAgentDiff, RejectAll, ToggleBurnMode, ToggleProfileSelector,
 };
 
 const RESPONSE_PADDING_X: Pixels = px(19.);
@@ -78,6 +81,22 @@ impl ThreadError {
     }
 }
 
+impl ProfileProvider for Entity<agent2::Thread> {
+    fn profile_id(&self, cx: &App) -> AgentProfileId {
+        self.read(cx).profile().clone()
+    }
+
+    fn set_profile(&self, profile_id: AgentProfileId, cx: &mut App) {
+        self.update(cx, |thread, _cx| {
+            thread.set_profile(profile_id);
+        });
+    }
+
+    fn profiles_supported(&self, cx: &App) -> bool {
+        self.read(cx).model().supports_tools()
+    }
+}
+
 pub struct AcpThreadView {
     agent: Rc<dyn AgentServer>,
     workspace: WeakEntity<Workspace>,
@@ -88,6 +107,7 @@ pub struct AcpThreadView {
     entry_view_state: EntryViewState,
     message_editor: Entity<MessageEditor>,
     model_selector: Option<Entity<AcpModelSelectorPopover>>,
+    profile_selector: Option<Entity<ProfileSelector>>,
     notifications: Vec<WindowHandle<AgentNotification>>,
     notification_subscriptions: HashMap<WindowHandle<AgentNotification>, Vec<Subscription>>,
     thread_error: Option<ThreadError>,
@@ -170,6 +190,7 @@ impl AcpThreadView {
             thread_state: Self::initial_state(agent, workspace, project, window, cx),
             message_editor,
             model_selector: None,
+            profile_selector: None,
             notifications: Vec::new(),
             notification_subscriptions: HashMap::default(),
             entry_view_state: EntryViewState::default(),
@@ -297,6 +318,17 @@ impl AcpThreadView {
                             _subscription: [thread_subscription, action_log_subscription],
                         };
 
+                        this.profile_selector = this.as_native_thread(cx).map(|thread| {
+                            cx.new(|cx| {
+                                ProfileSelector::new(
+                                    <dyn Fs>::global(cx),
+                                    Arc::new(thread.clone()),
+                                    this.focus_handle(cx),
+                                    cx,
+                                )
+                            })
+                        });
+
                         cx.notify();
                     }
                     Err(err) => {
@@ -2315,6 +2347,11 @@ impl AcpThreadView {
 
         v_flex()
             .on_action(cx.listener(Self::expand_message_editor))
+            .on_action(cx.listener(|this, _: &ToggleProfileSelector, window, cx| {
+                if let Some(profile_selector) = this.profile_selector.as_ref() {
+                    profile_selector.read(cx).menu_handle().toggle(window, cx);
+                }
+            }))
             .on_action(cx.listener(|this, _: &ToggleModelSelector, window, cx| {
                 if let Some(model_selector) = this.model_selector.as_ref() {
                     model_selector
@@ -2378,6 +2415,7 @@ impl AcpThreadView {
                     .child(
                         h_flex()
                             .gap_1()
+                            .children(self.profile_selector.clone())
                             .children(self.model_selector.clone())
                             .child(self.render_send_button(cx)),
                     ),

crates/agent_ui/src/message_editor.rs 🔗

@@ -14,7 +14,7 @@ use agent::{
     context::{AgentContextKey, ContextLoadResult, load_context},
     context_store::ContextStoreEvent,
 };
-use agent_settings::{AgentSettings, CompletionMode};
+use agent_settings::{AgentProfileId, AgentSettings, CompletionMode};
 use ai_onboarding::ApiKeysWithProviders;
 use buffer_diff::BufferDiff;
 use cloud_llm_client::CompletionIntent;
@@ -55,7 +55,7 @@ use zed_actions::agent::ToggleModelSelector;
 
 use crate::context_picker::{ContextPicker, ContextPickerCompletionProvider, crease_for_mention};
 use crate::context_strip::{ContextStrip, ContextStripEvent, SuggestContextKind};
-use crate::profile_selector::ProfileSelector;
+use crate::profile_selector::{ProfileProvider, ProfileSelector};
 use crate::{
     ActiveThread, AgentDiffPane, ChatWithFollow, ExpandMessageEditor, Follow, KeepAll,
     ModelUsageContext, NewThread, OpenAgentDiff, RejectAll, RemoveAllContext, ToggleBurnMode,
@@ -152,6 +152,24 @@ pub(crate) fn create_editor(
     editor
 }
 
+impl ProfileProvider for Entity<Thread> {
+    fn profiles_supported(&self, cx: &App) -> bool {
+        self.read(cx)
+            .configured_model()
+            .map_or(false, |model| model.model.supports_tools())
+    }
+
+    fn profile_id(&self, cx: &App) -> AgentProfileId {
+        self.read(cx).profile().id().clone()
+    }
+
+    fn set_profile(&self, profile_id: AgentProfileId, cx: &mut App) {
+        self.update(cx, |this, cx| {
+            this.set_profile(profile_id, cx);
+        });
+    }
+}
+
 impl MessageEditor {
     pub fn new(
         fs: Arc<dyn Fs>,
@@ -221,8 +239,9 @@ impl MessageEditor {
             )
         });
 
-        let profile_selector =
-            cx.new(|cx| ProfileSelector::new(fs, thread.clone(), editor.focus_handle(cx), cx));
+        let profile_selector = cx.new(|cx| {
+            ProfileSelector::new(fs, Arc::new(thread.clone()), editor.focus_handle(cx), cx)
+        });
 
         Self {
             editor: editor.clone(),

crates/agent_ui/src/profile_selector.rs 🔗

@@ -1,12 +1,8 @@
 use crate::{ManageProfiles, ToggleProfileSelector};
-use agent::{
-    Thread,
-    agent_profile::{AgentProfile, AvailableProfiles},
-};
+use agent::agent_profile::{AgentProfile, AvailableProfiles};
 use agent_settings::{AgentDockPosition, AgentProfileId, AgentSettings, builtin_profiles};
 use fs::Fs;
-use gpui::{Action, Empty, Entity, FocusHandle, Subscription, prelude::*};
-use language_model::LanguageModelRegistry;
+use gpui::{Action, Entity, FocusHandle, Subscription, prelude::*};
 use settings::{Settings as _, SettingsStore, update_settings_file};
 use std::sync::Arc;
 use ui::{
@@ -14,10 +10,22 @@ use ui::{
     prelude::*,
 };
 
+/// Trait for types that can provide and manage agent profiles
+pub trait ProfileProvider {
+    /// Get the current profile ID
+    fn profile_id(&self, cx: &App) -> AgentProfileId;
+
+    /// Set the profile ID
+    fn set_profile(&self, profile_id: AgentProfileId, cx: &mut App);
+
+    /// Check if profiles are supported in the current context (e.g. if the model that is selected has tool support)
+    fn profiles_supported(&self, cx: &App) -> bool;
+}
+
 pub struct ProfileSelector {
     profiles: AvailableProfiles,
     fs: Arc<dyn Fs>,
-    thread: Entity<Thread>,
+    provider: Arc<dyn ProfileProvider>,
     menu_handle: PopoverMenuHandle<ContextMenu>,
     focus_handle: FocusHandle,
     _subscriptions: Vec<Subscription>,
@@ -26,7 +34,7 @@ pub struct ProfileSelector {
 impl ProfileSelector {
     pub fn new(
         fs: Arc<dyn Fs>,
-        thread: Entity<Thread>,
+        provider: Arc<dyn ProfileProvider>,
         focus_handle: FocusHandle,
         cx: &mut Context<Self>,
     ) -> Self {
@@ -37,7 +45,7 @@ impl ProfileSelector {
         Self {
             profiles: AgentProfile::available_profiles(cx),
             fs,
-            thread,
+            provider,
             menu_handle: PopoverMenuHandle::default(),
             focus_handle,
             _subscriptions: vec![settings_subscription],
@@ -113,10 +121,10 @@ impl ProfileSelector {
             builtin_profiles::MINIMAL => Some("Chat about anything with no tools."),
             _ => None,
         };
-        let thread_profile_id = self.thread.read(cx).profile().id();
+        let thread_profile_id = self.provider.profile_id(cx);
 
         let entry = ContextMenuEntry::new(profile_name.clone())
-            .toggleable(IconPosition::End, &profile_id == thread_profile_id);
+            .toggleable(IconPosition::End, profile_id == thread_profile_id);
 
         let entry = if let Some(doc_text) = documentation {
             entry.documentation_aside(documentation_side(settings.dock), move |_| {
@@ -128,7 +136,7 @@ impl ProfileSelector {
 
         entry.handler({
             let fs = self.fs.clone();
-            let thread = self.thread.clone();
+            let provider = self.provider.clone();
             let profile_id = profile_id.clone();
             move |_window, cx| {
                 update_settings_file::<AgentSettings>(fs.clone(), cx, {
@@ -138,9 +146,7 @@ impl ProfileSelector {
                     }
                 });
 
-                thread.update(cx, |this, cx| {
-                    this.set_profile(profile_id.clone(), cx);
-                });
+                provider.set_profile(profile_id.clone(), cx);
             }
         })
     }
@@ -149,22 +155,14 @@ impl ProfileSelector {
 impl Render for ProfileSelector {
     fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
         let settings = AgentSettings::get_global(cx);
-        let profile_id = self.thread.read(cx).profile().id();
-        let profile = settings.profiles.get(profile_id);
+        let profile_id = self.provider.profile_id(cx);
+        let profile = settings.profiles.get(&profile_id);
 
         let selected_profile = profile
             .map(|profile| profile.name.clone())
             .unwrap_or_else(|| "Unknown".into());
 
-        let configured_model = self.thread.read(cx).configured_model().or_else(|| {
-            let model_registry = LanguageModelRegistry::read_global(cx);
-            model_registry.default_model()
-        });
-        let Some(configured_model) = configured_model else {
-            return Empty.into_any_element();
-        };
-
-        if configured_model.model.supports_tools() {
+        if self.provider.profiles_supported(cx) {
             let this = cx.entity().clone();
             let focus_handle = self.focus_handle.clone();
             let trigger_button = Button::new("profile-selector-model", selected_profile)