profile_selector.rs

  1use std::sync::Arc;
  2
  3use agent_settings::{AgentDockPosition, AgentProfileId, AgentSettings, builtin_profiles};
  4use fs::Fs;
  5use gpui::{Action, Empty, Entity, FocusHandle, Subscription, prelude::*};
  6use language_model::LanguageModelRegistry;
  7use settings::{Settings as _, SettingsStore, update_settings_file};
  8use ui::{
  9    ContextMenu, ContextMenuEntry, DocumentationSide, PopoverMenu, PopoverMenuHandle, Tooltip,
 10    prelude::*,
 11};
 12
 13use crate::{
 14    ManageProfiles, Thread, ToggleProfileSelector,
 15    agent_profile::{AgentProfile, AvailableProfiles},
 16};
 17
 18pub struct ProfileSelector {
 19    profiles: AvailableProfiles,
 20    fs: Arc<dyn Fs>,
 21    thread: Entity<Thread>,
 22    menu_handle: PopoverMenuHandle<ContextMenu>,
 23    focus_handle: FocusHandle,
 24    _subscriptions: Vec<Subscription>,
 25}
 26
 27impl ProfileSelector {
 28    pub fn new(
 29        fs: Arc<dyn Fs>,
 30        thread: Entity<Thread>,
 31        focus_handle: FocusHandle,
 32        cx: &mut Context<Self>,
 33    ) -> Self {
 34        let settings_subscription = cx.observe_global::<SettingsStore>(move |this, cx| {
 35            this.refresh_profiles(cx);
 36        });
 37
 38        Self {
 39            profiles: AgentProfile::available_profiles(cx),
 40            fs,
 41            thread,
 42            menu_handle: PopoverMenuHandle::default(),
 43            focus_handle,
 44            _subscriptions: vec![settings_subscription],
 45        }
 46    }
 47
 48    pub fn menu_handle(&self) -> PopoverMenuHandle<ContextMenu> {
 49        self.menu_handle.clone()
 50    }
 51
 52    fn refresh_profiles(&mut self, cx: &mut Context<Self>) {
 53        self.profiles = AgentProfile::available_profiles(cx);
 54    }
 55
 56    fn build_context_menu(
 57        &self,
 58        window: &mut Window,
 59        cx: &mut Context<Self>,
 60    ) -> Entity<ContextMenu> {
 61        ContextMenu::build(window, cx, |mut menu, _window, cx| {
 62            let settings = AgentSettings::get_global(cx);
 63
 64            let mut found_non_builtin = false;
 65            for (profile_id, profile_name) in self.profiles.iter() {
 66                if !builtin_profiles::is_builtin(profile_id) {
 67                    found_non_builtin = true;
 68                    continue;
 69                }
 70                menu = menu.item(self.menu_entry_for_profile(
 71                    profile_id.clone(),
 72                    profile_name,
 73                    settings,
 74                    cx,
 75                ));
 76            }
 77
 78            if found_non_builtin {
 79                menu = menu.separator().header("Custom Profiles");
 80                for (profile_id, profile_name) in self.profiles.iter() {
 81                    if builtin_profiles::is_builtin(profile_id) {
 82                        continue;
 83                    }
 84                    menu = menu.item(self.menu_entry_for_profile(
 85                        profile_id.clone(),
 86                        profile_name,
 87                        settings,
 88                        cx,
 89                    ));
 90                }
 91            }
 92
 93            menu = menu.separator();
 94            menu = menu.item(ContextMenuEntry::new("Configure Profiles…").handler(
 95                move |window, cx| {
 96                    window.dispatch_action(ManageProfiles::default().boxed_clone(), cx);
 97                },
 98            ));
 99
100            menu
101        })
102    }
103
104    fn menu_entry_for_profile(
105        &self,
106        profile_id: AgentProfileId,
107        profile_name: &SharedString,
108        settings: &AgentSettings,
109        cx: &App,
110    ) -> ContextMenuEntry {
111        let documentation = match profile_name.to_lowercase().as_str() {
112            builtin_profiles::WRITE => Some("Get help to write anything."),
113            builtin_profiles::ASK => Some("Chat about your codebase."),
114            builtin_profiles::MINIMAL => Some("Chat about anything with no tools."),
115            _ => None,
116        };
117        let thread_profile_id = self.thread.read(cx).profile().id();
118
119        let entry = ContextMenuEntry::new(profile_name.clone())
120            .toggleable(IconPosition::End, &profile_id == thread_profile_id);
121
122        let entry = if let Some(doc_text) = documentation {
123            entry.documentation_aside(documentation_side(settings.dock), move |_| {
124                Label::new(doc_text).into_any_element()
125            })
126        } else {
127            entry
128        };
129
130        entry.handler({
131            let fs = self.fs.clone();
132            let thread = self.thread.clone();
133            let profile_id = profile_id.clone();
134            move |_window, cx| {
135                update_settings_file::<AgentSettings>(fs.clone(), cx, {
136                    let profile_id = profile_id.clone();
137                    move |settings, _cx| {
138                        settings.set_profile(profile_id.clone());
139                    }
140                });
141
142                thread.update(cx, |this, cx| {
143                    this.set_profile(profile_id.clone(), cx);
144                });
145            }
146        })
147    }
148}
149
150impl Render for ProfileSelector {
151    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
152        let settings = AgentSettings::get_global(cx);
153        let profile_id = self.thread.read(cx).profile().id();
154        let profile = settings.profiles.get(profile_id);
155
156        let selected_profile = profile
157            .map(|profile| profile.name.clone())
158            .unwrap_or_else(|| "Unknown".into());
159
160        let configured_model = self.thread.read(cx).configured_model().or_else(|| {
161            let model_registry = LanguageModelRegistry::read_global(cx);
162            model_registry.default_model()
163        });
164        let Some(configured_model) = configured_model else {
165            return Empty.into_any_element();
166        };
167
168        if configured_model.model.supports_tools() {
169            let this = cx.entity().clone();
170            let focus_handle = self.focus_handle.clone();
171            let trigger_button = Button::new("profile-selector-model", selected_profile)
172                .label_size(LabelSize::Small)
173                .color(Color::Muted)
174                .icon(IconName::ChevronDown)
175                .icon_size(IconSize::XSmall)
176                .icon_position(IconPosition::End)
177                .icon_color(Color::Muted);
178
179            PopoverMenu::new("profile-selector")
180                .trigger_with_tooltip(trigger_button, {
181                    let focus_handle = focus_handle.clone();
182                    move |window, cx| {
183                        Tooltip::for_action_in(
184                            "Toggle Profile Menu",
185                            &ToggleProfileSelector,
186                            &focus_handle,
187                            window,
188                            cx,
189                        )
190                    }
191                })
192                .anchor(
193                    if documentation_side(settings.dock) == DocumentationSide::Left {
194                        gpui::Corner::BottomRight
195                    } else {
196                        gpui::Corner::BottomLeft
197                    },
198                )
199                .with_handle(self.menu_handle.clone())
200                .menu(move |window, cx| {
201                    Some(this.update(cx, |this, cx| this.build_context_menu(window, cx)))
202                })
203                .into_any_element()
204        } else {
205            Button::new("tools-not-supported-button", "Tools Unsupported")
206                .disabled(true)
207                .label_size(LabelSize::Small)
208                .color(Color::Muted)
209                .tooltip(Tooltip::text("This model does not support tools."))
210                .into_any_element()
211        }
212    }
213}
214
215fn documentation_side(position: AgentDockPosition) -> DocumentationSide {
216    match position {
217        AgentDockPosition::Left => DocumentationSide::Right,
218        AgentDockPosition::Bottom => DocumentationSide::Left,
219        AgentDockPosition::Right => DocumentationSide::Left,
220    }
221}