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