1mod profile_modal_header;
2
3use std::sync::Arc;
4
5use agent_settings::{AgentProfile, AgentProfileId, AgentSettings, builtin_profiles};
6use assistant_tool::ToolWorkingSet;
7use editor::Editor;
8use fs::Fs;
9use gpui::{DismissEvent, Entity, EventEmitter, FocusHandle, Focusable, Subscription, prelude::*};
10use settings::Settings as _;
11use ui::{
12 KeyBinding, ListItem, ListItemSpacing, ListSeparator, Navigable, NavigableEntry, prelude::*,
13};
14use workspace::{ModalView, Workspace};
15
16use crate::agent_configuration::manage_profiles_modal::profile_modal_header::ProfileModalHeader;
17use crate::agent_configuration::tool_picker::{ToolPicker, ToolPickerDelegate};
18use crate::{AgentPanel, ManageProfiles};
19
20use super::tool_picker::ToolPickerMode;
21
22enum Mode {
23 ChooseProfile(ChooseProfileMode),
24 NewProfile(NewProfileMode),
25 ViewProfile(ViewProfileMode),
26 ConfigureTools {
27 profile_id: AgentProfileId,
28 tool_picker: Entity<ToolPicker>,
29 _subscription: Subscription,
30 },
31 ConfigureMcps {
32 profile_id: AgentProfileId,
33 tool_picker: Entity<ToolPicker>,
34 _subscription: Subscription,
35 },
36}
37
38impl Mode {
39 pub fn choose_profile(_window: &mut Window, cx: &mut Context<ManageProfilesModal>) -> Self {
40 let settings = AgentSettings::get_global(cx);
41
42 let mut builtin_profiles = Vec::new();
43 let mut custom_profiles = Vec::new();
44
45 for (profile_id, profile) in settings.profiles.iter() {
46 let entry = ProfileEntry {
47 id: profile_id.clone(),
48 name: profile.name.clone(),
49 navigation: NavigableEntry::focusable(cx),
50 };
51 if builtin_profiles::is_builtin(profile_id) {
52 builtin_profiles.push(entry);
53 } else {
54 custom_profiles.push(entry);
55 }
56 }
57
58 builtin_profiles.sort_unstable_by(|a, b| a.name.cmp(&b.name));
59 custom_profiles.sort_unstable_by(|a, b| a.name.cmp(&b.name));
60
61 Self::ChooseProfile(ChooseProfileMode {
62 builtin_profiles,
63 custom_profiles,
64 add_new_profile: NavigableEntry::focusable(cx),
65 })
66 }
67}
68
69#[derive(Clone)]
70struct ProfileEntry {
71 pub id: AgentProfileId,
72 pub name: SharedString,
73 pub navigation: NavigableEntry,
74}
75
76#[derive(Clone)]
77pub struct ChooseProfileMode {
78 builtin_profiles: Vec<ProfileEntry>,
79 custom_profiles: Vec<ProfileEntry>,
80 add_new_profile: NavigableEntry,
81}
82
83#[derive(Clone)]
84pub struct ViewProfileMode {
85 profile_id: AgentProfileId,
86 fork_profile: NavigableEntry,
87 configure_tools: NavigableEntry,
88 configure_mcps: NavigableEntry,
89 cancel_item: NavigableEntry,
90}
91
92#[derive(Clone)]
93pub struct NewProfileMode {
94 name_editor: Entity<Editor>,
95 base_profile_id: Option<AgentProfileId>,
96}
97
98pub struct ManageProfilesModal {
99 fs: Arc<dyn Fs>,
100 tools: Entity<ToolWorkingSet>,
101 focus_handle: FocusHandle,
102 mode: Mode,
103}
104
105impl ManageProfilesModal {
106 pub fn register(
107 workspace: &mut Workspace,
108 _window: Option<&mut Window>,
109 _cx: &mut Context<Workspace>,
110 ) {
111 workspace.register_action(|workspace, action: &ManageProfiles, window, cx| {
112 if let Some(panel) = workspace.panel::<AgentPanel>(cx) {
113 let fs = workspace.app_state().fs.clone();
114 let thread_store = panel.read(cx).thread_store();
115 let tools = thread_store.read(cx).tools();
116 workspace.toggle_modal(window, cx, |window, cx| {
117 let mut this = Self::new(fs, tools, window, cx);
118
119 if let Some(profile_id) = action.customize_tools.clone() {
120 this.configure_builtin_tools(profile_id, window, cx);
121 }
122
123 this
124 })
125 }
126 });
127 }
128
129 pub fn new(
130 fs: Arc<dyn Fs>,
131 tools: Entity<ToolWorkingSet>,
132 window: &mut Window,
133 cx: &mut Context<Self>,
134 ) -> Self {
135 let focus_handle = cx.focus_handle();
136
137 Self {
138 fs,
139 tools,
140 focus_handle,
141 mode: Mode::choose_profile(window, cx),
142 }
143 }
144
145 fn choose_profile(&mut self, window: &mut Window, cx: &mut Context<Self>) {
146 self.mode = Mode::choose_profile(window, cx);
147 self.focus_handle(cx).focus(window);
148 }
149
150 fn new_profile(
151 &mut self,
152 base_profile_id: Option<AgentProfileId>,
153 window: &mut Window,
154 cx: &mut Context<Self>,
155 ) {
156 let name_editor = cx.new(|cx| Editor::single_line(window, cx));
157 name_editor.update(cx, |editor, cx| {
158 editor.set_placeholder_text("Profile name", window, cx);
159 });
160
161 self.mode = Mode::NewProfile(NewProfileMode {
162 name_editor,
163 base_profile_id,
164 });
165 self.focus_handle(cx).focus(window);
166 }
167
168 pub fn view_profile(
169 &mut self,
170 profile_id: AgentProfileId,
171 window: &mut Window,
172 cx: &mut Context<Self>,
173 ) {
174 self.mode = Mode::ViewProfile(ViewProfileMode {
175 profile_id,
176 fork_profile: NavigableEntry::focusable(cx),
177 configure_tools: NavigableEntry::focusable(cx),
178 configure_mcps: NavigableEntry::focusable(cx),
179 cancel_item: NavigableEntry::focusable(cx),
180 });
181 self.focus_handle(cx).focus(window);
182 }
183
184 fn configure_mcp_tools(
185 &mut self,
186 profile_id: AgentProfileId,
187 window: &mut Window,
188 cx: &mut Context<Self>,
189 ) {
190 let settings = AgentSettings::get_global(cx);
191 let Some(profile) = settings.profiles.get(&profile_id).cloned() else {
192 return;
193 };
194
195 let tool_picker = cx.new(|cx| {
196 let delegate = ToolPickerDelegate::new(
197 ToolPickerMode::McpTools,
198 self.fs.clone(),
199 self.tools.clone(),
200 profile_id.clone(),
201 profile,
202 cx,
203 );
204 ToolPicker::mcp_tools(delegate, window, cx)
205 });
206 let dismiss_subscription = cx.subscribe_in(&tool_picker, window, {
207 let profile_id = profile_id.clone();
208 move |this, _tool_picker, _: &DismissEvent, window, cx| {
209 this.view_profile(profile_id.clone(), window, cx);
210 }
211 });
212
213 self.mode = Mode::ConfigureMcps {
214 profile_id,
215 tool_picker,
216 _subscription: dismiss_subscription,
217 };
218 self.focus_handle(cx).focus(window);
219 }
220
221 fn configure_builtin_tools(
222 &mut self,
223 profile_id: AgentProfileId,
224 window: &mut Window,
225 cx: &mut Context<Self>,
226 ) {
227 let settings = AgentSettings::get_global(cx);
228 let Some(profile) = settings.profiles.get(&profile_id).cloned() else {
229 return;
230 };
231
232 let tool_picker = cx.new(|cx| {
233 let delegate = ToolPickerDelegate::new(
234 ToolPickerMode::BuiltinTools,
235 self.fs.clone(),
236 self.tools.clone(),
237 profile_id.clone(),
238 profile,
239 cx,
240 );
241 ToolPicker::builtin_tools(delegate, window, cx)
242 });
243 let dismiss_subscription = cx.subscribe_in(&tool_picker, window, {
244 let profile_id = profile_id.clone();
245 move |this, _tool_picker, _: &DismissEvent, window, cx| {
246 this.view_profile(profile_id.clone(), window, cx);
247 }
248 });
249
250 self.mode = Mode::ConfigureTools {
251 profile_id,
252 tool_picker,
253 _subscription: dismiss_subscription,
254 };
255 self.focus_handle(cx).focus(window);
256 }
257
258 fn confirm(&mut self, window: &mut Window, cx: &mut Context<Self>) {
259 match &self.mode {
260 Mode::ChooseProfile { .. } => {}
261 Mode::NewProfile(mode) => {
262 let name = mode.name_editor.read(cx).text(cx);
263
264 let profile_id =
265 AgentProfile::create(name, mode.base_profile_id.clone(), self.fs.clone(), cx);
266 self.view_profile(profile_id, window, cx);
267 }
268 Mode::ViewProfile(_) => {}
269 Mode::ConfigureTools { .. } => {}
270 Mode::ConfigureMcps { .. } => {}
271 }
272 }
273
274 fn cancel(&mut self, window: &mut Window, cx: &mut Context<Self>) {
275 match &self.mode {
276 Mode::ChooseProfile { .. } => {
277 cx.emit(DismissEvent);
278 }
279 Mode::NewProfile(mode) => {
280 if let Some(profile_id) = mode.base_profile_id.clone() {
281 self.view_profile(profile_id, window, cx);
282 } else {
283 self.choose_profile(window, cx);
284 }
285 }
286 Mode::ViewProfile(_) => self.choose_profile(window, cx),
287 Mode::ConfigureTools { profile_id, .. } => {
288 self.view_profile(profile_id.clone(), window, cx)
289 }
290 Mode::ConfigureMcps { profile_id, .. } => {
291 self.view_profile(profile_id.clone(), window, cx)
292 }
293 }
294 }
295}
296
297impl ModalView for ManageProfilesModal {}
298
299impl Focusable for ManageProfilesModal {
300 fn focus_handle(&self, cx: &App) -> FocusHandle {
301 match &self.mode {
302 Mode::ChooseProfile(_) => self.focus_handle.clone(),
303 Mode::NewProfile(mode) => mode.name_editor.focus_handle(cx),
304 Mode::ViewProfile(_) => self.focus_handle.clone(),
305 Mode::ConfigureTools { tool_picker, .. } => tool_picker.focus_handle(cx),
306 Mode::ConfigureMcps { tool_picker, .. } => tool_picker.focus_handle(cx),
307 }
308 }
309}
310
311impl EventEmitter<DismissEvent> for ManageProfilesModal {}
312
313impl ManageProfilesModal {
314 fn render_profile(
315 &self,
316 profile: &ProfileEntry,
317 window: &mut Window,
318 cx: &mut Context<Self>,
319 ) -> impl IntoElement + use<> {
320 div()
321 .id(SharedString::from(format!("profile-{}", profile.id)))
322 .track_focus(&profile.navigation.focus_handle)
323 .on_action({
324 let profile_id = profile.id.clone();
325 cx.listener(move |this, _: &menu::Confirm, window, cx| {
326 this.view_profile(profile_id.clone(), window, cx);
327 })
328 })
329 .child(
330 ListItem::new(SharedString::from(format!("profile-{}", profile.id)))
331 .toggle_state(profile.navigation.focus_handle.contains_focused(window, cx))
332 .inset(true)
333 .spacing(ListItemSpacing::Sparse)
334 .child(Label::new(profile.name.clone()))
335 .end_slot(
336 h_flex()
337 .gap_1()
338 .child(
339 Label::new("Customize")
340 .size(LabelSize::Small)
341 .color(Color::Muted),
342 )
343 .children(KeyBinding::for_action_in(
344 &menu::Confirm,
345 &self.focus_handle,
346 window,
347 cx,
348 )),
349 )
350 .on_click({
351 let profile_id = profile.id.clone();
352 cx.listener(move |this, _, window, cx| {
353 this.view_profile(profile_id.clone(), window, cx);
354 })
355 }),
356 )
357 }
358
359 fn render_choose_profile(
360 &mut self,
361 mode: ChooseProfileMode,
362 window: &mut Window,
363 cx: &mut Context<Self>,
364 ) -> impl IntoElement {
365 Navigable::new(
366 div()
367 .track_focus(&self.focus_handle(cx))
368 .size_full()
369 .child(ProfileModalHeader::new("Agent Profiles", None))
370 .child(
371 v_flex()
372 .pb_1()
373 .child(ListSeparator)
374 .children(
375 mode.builtin_profiles
376 .iter()
377 .map(|profile| self.render_profile(profile, window, cx)),
378 )
379 .when(!mode.custom_profiles.is_empty(), |this| {
380 this.child(ListSeparator)
381 .child(
382 div().pl_2().pb_1().child(
383 Label::new("Custom Profiles")
384 .size(LabelSize::Small)
385 .color(Color::Muted),
386 ),
387 )
388 .children(
389 mode.custom_profiles
390 .iter()
391 .map(|profile| self.render_profile(profile, window, cx)),
392 )
393 })
394 .child(ListSeparator)
395 .child(
396 div()
397 .id("new-profile")
398 .track_focus(&mode.add_new_profile.focus_handle)
399 .on_action(cx.listener(|this, _: &menu::Confirm, window, cx| {
400 this.new_profile(None, window, cx);
401 }))
402 .child(
403 ListItem::new("new-profile")
404 .toggle_state(
405 mode.add_new_profile
406 .focus_handle
407 .contains_focused(window, cx),
408 )
409 .inset(true)
410 .spacing(ListItemSpacing::Sparse)
411 .start_slot(Icon::new(IconName::Plus))
412 .child(Label::new("Add New Profile"))
413 .on_click({
414 cx.listener(move |this, _, window, cx| {
415 this.new_profile(None, window, cx);
416 })
417 }),
418 ),
419 ),
420 )
421 .into_any_element(),
422 )
423 .map(|mut navigable| {
424 for profile in mode.builtin_profiles {
425 navigable = navigable.entry(profile.navigation);
426 }
427 for profile in mode.custom_profiles {
428 navigable = navigable.entry(profile.navigation);
429 }
430
431 navigable
432 })
433 .entry(mode.add_new_profile)
434 }
435
436 fn render_new_profile(
437 &mut self,
438 mode: NewProfileMode,
439 _window: &mut Window,
440 cx: &mut Context<Self>,
441 ) -> impl IntoElement {
442 let settings = AgentSettings::get_global(cx);
443
444 let base_profile_name = mode.base_profile_id.as_ref().map(|base_profile_id| {
445 settings
446 .profiles
447 .get(base_profile_id)
448 .map(|profile| profile.name.clone())
449 .unwrap_or_else(|| "Unknown".into())
450 });
451
452 v_flex()
453 .id("new-profile")
454 .track_focus(&self.focus_handle(cx))
455 .child(ProfileModalHeader::new(
456 match &base_profile_name {
457 Some(base_profile) => format!("Fork {base_profile}"),
458 None => "New Profile".into(),
459 },
460 match base_profile_name {
461 Some(_) => Some(IconName::Scissors),
462 None => Some(IconName::Plus),
463 },
464 ))
465 .child(ListSeparator)
466 .child(h_flex().p_2().child(mode.name_editor))
467 }
468
469 fn render_view_profile(
470 &mut self,
471 mode: ViewProfileMode,
472 window: &mut Window,
473 cx: &mut Context<Self>,
474 ) -> impl IntoElement {
475 let settings = AgentSettings::get_global(cx);
476
477 let profile_name = settings
478 .profiles
479 .get(&mode.profile_id)
480 .map(|profile| profile.name.clone())
481 .unwrap_or_else(|| "Unknown".into());
482
483 let icon = match mode.profile_id.as_str() {
484 "write" => IconName::Pencil,
485 "ask" => IconName::Chat,
486 _ => IconName::UserRoundPen,
487 };
488
489 Navigable::new(
490 div()
491 .track_focus(&self.focus_handle(cx))
492 .size_full()
493 .child(ProfileModalHeader::new(profile_name, Some(icon)))
494 .child(
495 v_flex()
496 .pb_1()
497 .child(ListSeparator)
498 .child(
499 div()
500 .id("fork-profile")
501 .track_focus(&mode.fork_profile.focus_handle)
502 .on_action({
503 let profile_id = mode.profile_id.clone();
504 cx.listener(move |this, _: &menu::Confirm, window, cx| {
505 this.new_profile(Some(profile_id.clone()), window, cx);
506 })
507 })
508 .child(
509 ListItem::new("fork-profile")
510 .toggle_state(
511 mode.fork_profile
512 .focus_handle
513 .contains_focused(window, cx),
514 )
515 .inset(true)
516 .spacing(ListItemSpacing::Sparse)
517 .start_slot(
518 Icon::new(IconName::Scissors)
519 .size(IconSize::Small)
520 .color(Color::Muted),
521 )
522 .child(Label::new("Fork Profile"))
523 .on_click({
524 let profile_id = mode.profile_id.clone();
525 cx.listener(move |this, _, window, cx| {
526 this.new_profile(
527 Some(profile_id.clone()),
528 window,
529 cx,
530 );
531 })
532 }),
533 ),
534 )
535 .child(
536 div()
537 .id("configure-builtin-tools")
538 .track_focus(&mode.configure_tools.focus_handle)
539 .on_action({
540 let profile_id = mode.profile_id.clone();
541 cx.listener(move |this, _: &menu::Confirm, window, cx| {
542 this.configure_builtin_tools(
543 profile_id.clone(),
544 window,
545 cx,
546 );
547 })
548 })
549 .child(
550 ListItem::new("configure-builtin-tools-item")
551 .toggle_state(
552 mode.configure_tools
553 .focus_handle
554 .contains_focused(window, cx),
555 )
556 .inset(true)
557 .spacing(ListItemSpacing::Sparse)
558 .start_slot(
559 Icon::new(IconName::Settings)
560 .size(IconSize::Small)
561 .color(Color::Muted),
562 )
563 .child(Label::new("Configure Built-in Tools"))
564 .on_click({
565 let profile_id = mode.profile_id.clone();
566 cx.listener(move |this, _, window, cx| {
567 this.configure_builtin_tools(
568 profile_id.clone(),
569 window,
570 cx,
571 );
572 })
573 }),
574 ),
575 )
576 .child(
577 div()
578 .id("configure-mcps")
579 .track_focus(&mode.configure_mcps.focus_handle)
580 .on_action({
581 let profile_id = mode.profile_id.clone();
582 cx.listener(move |this, _: &menu::Confirm, window, cx| {
583 this.configure_mcp_tools(profile_id.clone(), window, cx);
584 })
585 })
586 .child(
587 ListItem::new("configure-mcp-tools")
588 .toggle_state(
589 mode.configure_mcps
590 .focus_handle
591 .contains_focused(window, cx),
592 )
593 .inset(true)
594 .spacing(ListItemSpacing::Sparse)
595 .start_slot(
596 Icon::new(IconName::ToolHammer)
597 .size(IconSize::Small)
598 .color(Color::Muted),
599 )
600 .child(Label::new("Configure MCP Tools"))
601 .on_click({
602 let profile_id = mode.profile_id.clone();
603 cx.listener(move |this, _, window, cx| {
604 this.configure_mcp_tools(
605 profile_id.clone(),
606 window,
607 cx,
608 );
609 })
610 }),
611 ),
612 )
613 .child(ListSeparator)
614 .child(
615 div()
616 .id("cancel-item")
617 .track_focus(&mode.cancel_item.focus_handle)
618 .on_action({
619 cx.listener(move |this, _: &menu::Confirm, window, cx| {
620 this.cancel(window, cx);
621 })
622 })
623 .child(
624 ListItem::new("cancel-item")
625 .toggle_state(
626 mode.cancel_item
627 .focus_handle
628 .contains_focused(window, cx),
629 )
630 .inset(true)
631 .spacing(ListItemSpacing::Sparse)
632 .start_slot(
633 Icon::new(IconName::ArrowLeft)
634 .size(IconSize::Small)
635 .color(Color::Muted),
636 )
637 .child(Label::new("Go Back"))
638 .end_slot(
639 div().children(
640 KeyBinding::for_action_in(
641 &menu::Cancel,
642 &self.focus_handle,
643 window,
644 cx,
645 )
646 .map(|kb| kb.size(rems_from_px(12.))),
647 ),
648 )
649 .on_click({
650 cx.listener(move |this, _, window, cx| {
651 this.cancel(window, cx);
652 })
653 }),
654 ),
655 ),
656 )
657 .into_any_element(),
658 )
659 .entry(mode.fork_profile)
660 .entry(mode.configure_tools)
661 .entry(mode.configure_mcps)
662 .entry(mode.cancel_item)
663 }
664}
665
666impl Render for ManageProfilesModal {
667 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
668 let settings = AgentSettings::get_global(cx);
669
670 let go_back_item = div()
671 .id("cancel-item")
672 .track_focus(&self.focus_handle)
673 .on_action({
674 cx.listener(move |this, _: &menu::Confirm, window, cx| {
675 this.cancel(window, cx);
676 })
677 })
678 .child(
679 ListItem::new("cancel-item")
680 .toggle_state(self.focus_handle.contains_focused(window, cx))
681 .inset(true)
682 .spacing(ListItemSpacing::Sparse)
683 .start_slot(
684 Icon::new(IconName::ArrowLeft)
685 .size(IconSize::Small)
686 .color(Color::Muted),
687 )
688 .child(Label::new("Go Back"))
689 .end_slot(
690 div().children(
691 KeyBinding::for_action_in(
692 &menu::Cancel,
693 &self.focus_handle,
694 window,
695 cx,
696 )
697 .map(|kb| kb.size(rems_from_px(12.))),
698 ),
699 )
700 .on_click({
701 cx.listener(move |this, _, window, cx| {
702 this.cancel(window, cx);
703 })
704 }),
705 );
706
707 div()
708 .elevation_3(cx)
709 .w(rems(34.))
710 .key_context("ManageProfilesModal")
711 .on_action(cx.listener(|this, _: &menu::Cancel, window, cx| this.cancel(window, cx)))
712 .on_action(cx.listener(|this, _: &menu::Confirm, window, cx| this.confirm(window, cx)))
713 .capture_any_mouse_down(cx.listener(|this, _, window, cx| {
714 this.focus_handle(cx).focus(window);
715 }))
716 .on_mouse_down_out(cx.listener(|_this, _, _, cx| cx.emit(DismissEvent)))
717 .child(match &self.mode {
718 Mode::ChooseProfile(mode) => self
719 .render_choose_profile(mode.clone(), window, cx)
720 .into_any_element(),
721 Mode::NewProfile(mode) => self
722 .render_new_profile(mode.clone(), window, cx)
723 .into_any_element(),
724 Mode::ViewProfile(mode) => self
725 .render_view_profile(mode.clone(), window, cx)
726 .into_any_element(),
727 Mode::ConfigureTools {
728 profile_id,
729 tool_picker,
730 ..
731 } => {
732 let profile_name = settings
733 .profiles
734 .get(profile_id)
735 .map(|profile| profile.name.clone())
736 .unwrap_or_else(|| "Unknown".into());
737
738 v_flex()
739 .pb_1()
740 .child(ProfileModalHeader::new(
741 format!("{profile_name} — Configure Built-in Tools"),
742 Some(IconName::Cog),
743 ))
744 .child(ListSeparator)
745 .child(tool_picker.clone())
746 .child(ListSeparator)
747 .child(go_back_item)
748 .into_any_element()
749 }
750 Mode::ConfigureMcps {
751 profile_id,
752 tool_picker,
753 ..
754 } => {
755 let profile_name = settings
756 .profiles
757 .get(profile_id)
758 .map(|profile| profile.name.clone())
759 .unwrap_or_else(|| "Unknown".into());
760
761 v_flex()
762 .pb_1()
763 .child(ProfileModalHeader::new(
764 format!("{profile_name} — Configure MCP Tools"),
765 Some(IconName::ToolHammer),
766 ))
767 .child(ListSeparator)
768 .child(tool_picker.clone())
769 .child(ListSeparator)
770 .child(go_back_item)
771 .into_any_element()
772 }
773 })
774 }
775}