1use assistant_settings::{
2 AgentProfile, AgentProfileId, AssistantDockPosition, AssistantSettings, GroupedAgentProfiles,
3 builtin_profiles,
4};
5use gpui::{Action, Entity, FocusHandle, Subscription, WeakEntity, prelude::*};
6use language_model::LanguageModelRegistry;
7use settings::{Settings as _, SettingsStore};
8use ui::{
9 ContextMenu, ContextMenuEntry, DocumentationSide, PopoverMenu, PopoverMenuHandle, Tooltip,
10 prelude::*,
11};
12use util::ResultExt as _;
13
14use crate::{ManageProfiles, Thread, ThreadStore, ToggleProfileSelector};
15
16pub struct ProfileSelector {
17 profiles: GroupedAgentProfiles,
18 thread: Entity<Thread>,
19 thread_store: WeakEntity<ThreadStore>,
20 menu_handle: PopoverMenuHandle<ContextMenu>,
21 focus_handle: FocusHandle,
22 _subscriptions: Vec<Subscription>,
23}
24
25impl ProfileSelector {
26 pub fn new(
27 thread: Entity<Thread>,
28 thread_store: WeakEntity<ThreadStore>,
29 focus_handle: FocusHandle,
30 cx: &mut Context<Self>,
31 ) -> Self {
32 let settings_subscription = cx.observe_global::<SettingsStore>(move |this, cx| {
33 this.refresh_profiles(cx);
34 });
35
36 Self {
37 profiles: GroupedAgentProfiles::from_settings(AssistantSettings::get_global(cx)),
38 thread,
39 thread_store,
40 menu_handle: PopoverMenuHandle::default(),
41 focus_handle,
42 _subscriptions: vec![settings_subscription],
43 }
44 }
45
46 pub fn menu_handle(&self) -> PopoverMenuHandle<ContextMenu> {
47 self.menu_handle.clone()
48 }
49
50 fn refresh_profiles(&mut self, cx: &mut Context<Self>) {
51 self.profiles = GroupedAgentProfiles::from_settings(AssistantSettings::get_global(cx));
52 }
53
54 fn build_context_menu(
55 &self,
56 window: &mut Window,
57 cx: &mut Context<Self>,
58 ) -> Entity<ContextMenu> {
59 ContextMenu::build(window, cx, |mut menu, _window, cx| {
60 let settings = AssistantSettings::get_global(cx);
61 for (profile_id, profile) in self.profiles.builtin.iter() {
62 menu = menu.item(self.menu_entry_for_profile(
63 profile_id.clone(),
64 profile,
65 settings,
66 cx,
67 ));
68 }
69
70 if !self.profiles.custom.is_empty() {
71 menu = menu.separator().header("Custom Profiles");
72 for (profile_id, profile) in self.profiles.custom.iter() {
73 menu = menu.item(self.menu_entry_for_profile(
74 profile_id.clone(),
75 profile,
76 settings,
77 cx,
78 ));
79 }
80 }
81
82 menu = menu.separator();
83 menu = menu.item(ContextMenuEntry::new("Configure Profiles…").handler(
84 move |window, cx| {
85 window.dispatch_action(ManageProfiles::default().boxed_clone(), cx);
86 },
87 ));
88
89 menu
90 })
91 }
92
93 fn menu_entry_for_profile(
94 &self,
95 profile_id: AgentProfileId,
96 profile: &AgentProfile,
97 settings: &AssistantSettings,
98 cx: &App,
99 ) -> ContextMenuEntry {
100 let documentation = match profile.name.to_lowercase().as_str() {
101 builtin_profiles::WRITE => Some("Get help to write anything."),
102 builtin_profiles::ASK => Some("Chat about your codebase."),
103 builtin_profiles::MINIMAL => Some("Chat about anything with no tools."),
104 _ => None,
105 };
106
107 let current_profile_id = self.thread.read(cx).configured_profile_id();
108
109 let entry = ContextMenuEntry::new(profile.name.clone()).toggleable(
110 IconPosition::End,
111 Some(profile_id.clone()) == current_profile_id,
112 );
113
114 let entry = if let Some(doc_text) = documentation {
115 entry.documentation_aside(documentation_side(settings.dock), move |_| {
116 Label::new(doc_text).into_any_element()
117 })
118 } else {
119 entry
120 };
121
122 entry.handler({
123 let thread_store = self.thread_store.clone();
124 let profile_id = profile_id.clone();
125 let thread = self.thread.clone();
126
127 move |_window, cx| {
128 thread.update(cx, |thread, cx| {
129 thread.set_configured_profile_id(Some(profile_id.clone()), cx);
130 });
131
132 thread_store
133 .update(cx, |this, cx| {
134 this.load_profile_by_id(profile_id.clone(), cx);
135 })
136 .log_err();
137 }
138 })
139 }
140}
141
142impl Render for ProfileSelector {
143 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
144 let settings = AssistantSettings::get_global(cx);
145 let profile_id = self
146 .thread
147 .read(cx)
148 .configured_profile_id()
149 .unwrap_or(settings.default_profile.clone());
150 let profile = settings.profiles.get(&profile_id).cloned();
151
152 let selected_profile = profile
153 .map(|profile| profile.name.clone())
154 .unwrap_or_else(|| "Unknown".into());
155
156 let configured_model = self
157 .thread
158 .read_with(cx, |thread, _cx| thread.configured_model())
159 .or_else(|| {
160 let model_registry = LanguageModelRegistry::read_global(cx);
161 model_registry.default_model()
162 });
163 let supports_tools =
164 configured_model.map_or(false, |default| default.model.supports_tools());
165
166 if supports_tools {
167 let this = cx.entity().clone();
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
177 PopoverMenu::new("profile-selector")
178 .trigger_with_tooltip(trigger_button, {
179 let focus_handle = focus_handle.clone();
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 .into_any_element()
202 } else {
203 Button::new("tools-not-supported-button", "Tools Unsupported")
204 .disabled(true)
205 .label_size(LabelSize::Small)
206 .color(Color::Muted)
207 .tooltip(Tooltip::text("This model does not support tools."))
208 .into_any_element()
209 }
210 }
211}
212
213fn documentation_side(position: AssistantDockPosition) -> DocumentationSide {
214 match position {
215 AssistantDockPosition::Left => DocumentationSide::Right,
216 AssistantDockPosition::Bottom => DocumentationSide::Left,
217 AssistantDockPosition::Right => DocumentationSide::Left,
218 }
219}