message_editor.rs

  1use std::sync::Arc;
  2
  3use collections::HashSet;
  4use editor::actions::MoveUp;
  5use editor::{ContextMenuOptions, ContextMenuPlacement, Editor, EditorElement, EditorStyle};
  6use file_icons::FileIcons;
  7use fs::Fs;
  8use gpui::{
  9    Animation, AnimationExt, App, DismissEvent, Entity, Focusable, Subscription, TextStyle,
 10    WeakEntity,
 11};
 12use language_model::LanguageModelRegistry;
 13use language_model_selector::ToggleModelSelector;
 14use project::Project;
 15use settings::Settings;
 16use std::time::Duration;
 17use theme::ThemeSettings;
 18use ui::{
 19    prelude::*, ButtonLike, Disclosure, KeyBinding, PlatformStyle, PopoverMenu, PopoverMenuHandle,
 20    Tooltip,
 21};
 22use util::ResultExt;
 23use vim_mode_setting::VimModeSetting;
 24use workspace::Workspace;
 25
 26use crate::assistant_model_selector::AssistantModelSelector;
 27use crate::context_picker::{ConfirmBehavior, ContextPicker, ContextPickerCompletionProvider};
 28use crate::context_store::{refresh_context_store_text, ContextStore};
 29use crate::context_strip::{ContextStrip, ContextStripEvent, SuggestContextKind};
 30use crate::profile_selector::ProfileSelector;
 31use crate::thread::{RequestKind, Thread};
 32use crate::thread_store::ThreadStore;
 33use crate::{
 34    AssistantDiff, Chat, ChatMode, RemoveAllContext, ThreadEvent, ToggleContextPicker,
 35    ToggleProfileSelector,
 36};
 37
 38pub struct MessageEditor {
 39    thread: Entity<Thread>,
 40    editor: Entity<Editor>,
 41    #[allow(dead_code)]
 42    workspace: WeakEntity<Workspace>,
 43    project: Entity<Project>,
 44    context_store: Entity<ContextStore>,
 45    context_strip: Entity<ContextStrip>,
 46    context_picker_menu_handle: PopoverMenuHandle<ContextPicker>,
 47    inline_context_picker: Entity<ContextPicker>,
 48    inline_context_picker_menu_handle: PopoverMenuHandle<ContextPicker>,
 49    model_selector: Entity<AssistantModelSelector>,
 50    profile_selector: Entity<ProfileSelector>,
 51    edits_expanded: bool,
 52    _subscriptions: Vec<Subscription>,
 53}
 54
 55impl MessageEditor {
 56    pub fn new(
 57        fs: Arc<dyn Fs>,
 58        workspace: WeakEntity<Workspace>,
 59        context_store: Entity<ContextStore>,
 60        thread_store: WeakEntity<ThreadStore>,
 61        thread: Entity<Thread>,
 62        window: &mut Window,
 63        cx: &mut Context<Self>,
 64    ) -> Self {
 65        let context_picker_menu_handle = PopoverMenuHandle::default();
 66        let inline_context_picker_menu_handle = PopoverMenuHandle::default();
 67        let model_selector_menu_handle = PopoverMenuHandle::default();
 68
 69        let editor = cx.new(|cx| {
 70            let mut editor = Editor::auto_height(10, window, cx);
 71            editor.set_placeholder_text("Ask anything, @ to mention, ↑ to select", cx);
 72            editor.set_show_indent_guides(false, cx);
 73            editor.set_context_menu_options(ContextMenuOptions {
 74                min_entries_visible: 12,
 75                max_entries_visible: 12,
 76                placement: Some(ContextMenuPlacement::Above),
 77            });
 78
 79            editor
 80        });
 81
 82        let editor_entity = editor.downgrade();
 83        editor.update(cx, |editor, _| {
 84            editor.set_completion_provider(Some(Box::new(ContextPickerCompletionProvider::new(
 85                workspace.clone(),
 86                context_store.downgrade(),
 87                Some(thread_store.clone()),
 88                editor_entity,
 89            ))));
 90        });
 91
 92        let inline_context_picker = cx.new(|cx| {
 93            ContextPicker::new(
 94                workspace.clone(),
 95                Some(thread_store.clone()),
 96                context_store.downgrade(),
 97                ConfirmBehavior::Close,
 98                window,
 99                cx,
100            )
101        });
102
103        let context_strip = cx.new(|cx| {
104            ContextStrip::new(
105                context_store.clone(),
106                workspace.clone(),
107                Some(thread_store.clone()),
108                context_picker_menu_handle.clone(),
109                SuggestContextKind::File,
110                window,
111                cx,
112            )
113        });
114
115        let subscriptions = vec![
116            cx.subscribe_in(
117                &inline_context_picker,
118                window,
119                Self::handle_inline_context_picker_event,
120            ),
121            cx.subscribe_in(&context_strip, window, Self::handle_context_strip_event),
122        ];
123
124        Self {
125            editor: editor.clone(),
126            project: thread.read(cx).project().clone(),
127            thread,
128            workspace,
129            context_store,
130            context_strip,
131            context_picker_menu_handle,
132            inline_context_picker,
133            inline_context_picker_menu_handle,
134            model_selector: cx.new(|cx| {
135                AssistantModelSelector::new(
136                    fs.clone(),
137                    model_selector_menu_handle,
138                    editor.focus_handle(cx),
139                    window,
140                    cx,
141                )
142            }),
143            edits_expanded: false,
144            profile_selector: cx
145                .new(|cx| ProfileSelector::new(fs, thread_store, editor.focus_handle(cx), cx)),
146            _subscriptions: subscriptions,
147        }
148    }
149
150    fn toggle_chat_mode(&mut self, _: &ChatMode, _window: &mut Window, cx: &mut Context<Self>) {
151        cx.notify();
152    }
153
154    fn toggle_context_picker(
155        &mut self,
156        _: &ToggleContextPicker,
157        window: &mut Window,
158        cx: &mut Context<Self>,
159    ) {
160        self.context_picker_menu_handle.toggle(window, cx);
161    }
162    pub fn remove_all_context(
163        &mut self,
164        _: &RemoveAllContext,
165        _window: &mut Window,
166        cx: &mut Context<Self>,
167    ) {
168        self.context_store.update(cx, |store, _cx| store.clear());
169        cx.notify();
170    }
171
172    fn chat(&mut self, _: &Chat, window: &mut Window, cx: &mut Context<Self>) {
173        if self.is_editor_empty(cx) {
174            return;
175        }
176
177        if self.thread.read(cx).is_generating() {
178            return;
179        }
180
181        self.send_to_model(RequestKind::Chat, window, cx);
182    }
183
184    fn is_editor_empty(&self, cx: &App) -> bool {
185        self.editor.read(cx).text(cx).is_empty()
186    }
187
188    fn is_model_selected(&self, cx: &App) -> bool {
189        LanguageModelRegistry::read_global(cx)
190            .active_model()
191            .is_some()
192    }
193
194    fn send_to_model(
195        &mut self,
196        request_kind: RequestKind,
197        window: &mut Window,
198        cx: &mut Context<Self>,
199    ) {
200        let provider = LanguageModelRegistry::read_global(cx).active_provider();
201        if provider
202            .as_ref()
203            .map_or(false, |provider| provider.must_accept_terms(cx))
204        {
205            cx.notify();
206            return;
207        }
208
209        let model_registry = LanguageModelRegistry::read_global(cx);
210        let Some(model) = model_registry.active_model() else {
211            return;
212        };
213
214        let user_message = self.editor.update(cx, |editor, cx| {
215            let text = editor.text(cx);
216            editor.clear(window, cx);
217            text
218        });
219
220        let refresh_task =
221            refresh_context_store_text(self.context_store.clone(), &HashSet::default(), cx);
222
223        let system_prompt_context_task = self.thread.read(cx).load_system_prompt_context(cx);
224
225        let thread = self.thread.clone();
226        let context_store = self.context_store.clone();
227        let checkpoint = self.project.read(cx).git_store().read(cx).checkpoint(cx);
228        cx.spawn(async move |_, cx| {
229            let checkpoint = checkpoint.await.ok();
230            refresh_task.await;
231            let (system_prompt_context, load_error) = system_prompt_context_task.await;
232            thread
233                .update(cx, |thread, cx| {
234                    thread.set_system_prompt_context(system_prompt_context);
235                    if let Some(load_error) = load_error {
236                        cx.emit(ThreadEvent::ShowError(load_error));
237                    }
238                })
239                .ok();
240            thread
241                .update(cx, |thread, cx| {
242                    let context = context_store.read(cx).snapshot(cx).collect::<Vec<_>>();
243                    thread.action_log().update(cx, |action_log, cx| {
244                        action_log.clear_reviewed_changes(cx);
245                    });
246                    thread.insert_user_message(user_message, context, checkpoint, cx);
247                    thread.send_to_model(model, request_kind, cx);
248                })
249                .ok();
250        })
251        .detach();
252    }
253
254    fn handle_inline_context_picker_event(
255        &mut self,
256        _inline_context_picker: &Entity<ContextPicker>,
257        _event: &DismissEvent,
258        window: &mut Window,
259        cx: &mut Context<Self>,
260    ) {
261        let editor_focus_handle = self.editor.focus_handle(cx);
262        window.focus(&editor_focus_handle);
263    }
264
265    fn handle_context_strip_event(
266        &mut self,
267        _context_strip: &Entity<ContextStrip>,
268        event: &ContextStripEvent,
269        window: &mut Window,
270        cx: &mut Context<Self>,
271    ) {
272        match event {
273            ContextStripEvent::PickerDismissed
274            | ContextStripEvent::BlurredEmpty
275            | ContextStripEvent::BlurredDown => {
276                let editor_focus_handle = self.editor.focus_handle(cx);
277                window.focus(&editor_focus_handle);
278            }
279            ContextStripEvent::BlurredUp => {}
280        }
281    }
282
283    fn move_up(&mut self, _: &MoveUp, window: &mut Window, cx: &mut Context<Self>) {
284        if self.context_picker_menu_handle.is_deployed()
285            || self.inline_context_picker_menu_handle.is_deployed()
286        {
287            cx.propagate();
288        } else {
289            self.context_strip.focus_handle(cx).focus(window);
290        }
291    }
292
293    fn handle_review_click(&self, window: &mut Window, cx: &mut Context<Self>) {
294        AssistantDiff::deploy(self.thread.clone(), self.workspace.clone(), window, cx).log_err();
295    }
296}
297
298impl Focusable for MessageEditor {
299    fn focus_handle(&self, cx: &App) -> gpui::FocusHandle {
300        self.editor.focus_handle(cx)
301    }
302}
303
304impl Render for MessageEditor {
305    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
306        let font_size = TextSize::Default.rems(cx);
307        let line_height = font_size.to_pixels(window.rem_size()) * 1.5;
308
309        let focus_handle = self.editor.focus_handle(cx);
310        let inline_context_picker = self.inline_context_picker.clone();
311
312        let is_generating = self.thread.read(cx).is_generating();
313        let is_model_selected = self.is_model_selected(cx);
314        let is_editor_empty = self.is_editor_empty(cx);
315        let submit_label_color = if is_editor_empty {
316            Color::Muted
317        } else {
318            Color::Default
319        };
320
321        let vim_mode_enabled = VimModeSetting::get_global(cx).0;
322        let platform = PlatformStyle::platform();
323        let linux = platform == PlatformStyle::Linux;
324        let windows = platform == PlatformStyle::Windows;
325        let button_width = if linux || windows || vim_mode_enabled {
326            px(82.)
327        } else {
328            px(64.)
329        };
330
331        let action_log = self.thread.read(cx).action_log();
332        let changed_buffers = action_log.read(cx).changed_buffers(cx);
333        let changed_buffers_count = changed_buffers.len();
334        let editor_bg_color = cx.theme().colors().editor_background;
335
336        v_flex()
337            .size_full()
338            .when(is_generating, |parent| {
339                let focus_handle = self.editor.focus_handle(cx).clone();
340                parent.child(
341                    h_flex().py_3().w_full().justify_center().child(
342                        h_flex()
343                            .flex_none()
344                            .pl_2()
345                            .pr_1()
346                            .py_1()
347                            .bg(editor_bg_color)
348                            .border_1()
349                            .border_color(cx.theme().colors().border_variant)
350                            .rounded_lg()
351                            .shadow_md()
352                            .gap_1()
353                            .child(
354                                Icon::new(IconName::ArrowCircle)
355                                    .size(IconSize::XSmall)
356                                    .color(Color::Muted)
357                                    .with_animation(
358                                        "arrow-circle",
359                                        Animation::new(Duration::from_secs(2)).repeat(),
360                                        |icon, delta| {
361                                            icon.transform(gpui::Transformation::rotate(
362                                                gpui::percentage(delta),
363                                            ))
364                                        },
365                                    ),
366                            )
367                            .child(
368                                Label::new("Generating…")
369                                    .size(LabelSize::XSmall)
370                                    .color(Color::Muted),
371                            )
372                            .child(ui::Divider::vertical())
373                            .child(
374                                Button::new("cancel-generation", "Cancel")
375                                    .label_size(LabelSize::XSmall)
376                                    .key_binding(
377                                        KeyBinding::for_action_in(
378                                            &editor::actions::Cancel,
379                                            &focus_handle,
380                                            window,
381                                            cx,
382                                        )
383                                        .map(|kb| kb.size(rems_from_px(10.))),
384                                    )
385                                    .on_click(move |_event, window, cx| {
386                                        focus_handle.dispatch_action(
387                                            &editor::actions::Cancel,
388                                            window,
389                                            cx,
390                                        );
391                                    }),
392                            ),
393                    ),
394                )
395            })
396            .when(changed_buffers_count > 0, |parent| {
397                parent.child(
398                    v_flex()
399                        .mx_2()
400                        .bg(cx.theme().colors().element_background)
401                        .border_1()
402                        .border_b_0()
403                        .border_color(cx.theme().colors().border)
404                        .rounded_t_md()
405                        .child(
406                            h_flex()
407                                .p_2()
408                                .justify_between()
409                                .child(
410                                    h_flex()
411                                        .gap_2()
412                                        .child(
413                                            Disclosure::new(
414                                                "edits-disclosure",
415                                                self.edits_expanded,
416                                            )
417                                            .on_click(
418                                                cx.listener(|this, _ev, _window, cx| {
419                                                    this.edits_expanded = !this.edits_expanded;
420                                                    cx.notify();
421                                                }),
422                                            ),
423                                        )
424                                        .child(
425                                            Label::new("Edits")
426                                                .size(LabelSize::XSmall)
427                                                .color(Color::Muted),
428                                        )
429                                        .child(
430                                            Label::new("")
431                                                .size(LabelSize::XSmall)
432                                                .color(Color::Muted),
433                                        )
434                                        .child(
435                                            Label::new(format!(
436                                                "{} {}",
437                                                changed_buffers_count,
438                                                if changed_buffers_count == 1 {
439                                                    "file"
440                                                } else {
441                                                    "files"
442                                                }
443                                            ))
444                                            .size(LabelSize::XSmall)
445                                            .color(Color::Muted),
446                                        ),
447                                )
448                                .child(
449                                    Button::new("review", "Review")
450                                        .label_size(LabelSize::XSmall)
451                                        .on_click(cx.listener(|this, _, window, cx| {
452                                            this.handle_review_click(window, cx)
453                                        })),
454                                ),
455                        )
456                        .when(self.edits_expanded, |parent| {
457                            parent.child(
458                                v_flex().bg(cx.theme().colors().editor_background).children(
459                                    changed_buffers.into_iter().enumerate().flat_map(
460                                        |(index, (buffer, changed))| {
461                                            let file = buffer.read(cx).file()?;
462                                            let path = file.path();
463
464                                            let parent_label = path.parent().and_then(|parent| {
465                                                let parent_str = parent.to_string_lossy();
466
467                                                if parent_str.is_empty() {
468                                                    None
469                                                } else {
470                                                    Some(
471                                                        Label::new(format!(
472                                                            "{}{}",
473                                                            parent_str,
474                                                            std::path::MAIN_SEPARATOR_STR
475                                                        ))
476                                                        .color(Color::Muted)
477                                                        .size(LabelSize::Small),
478                                                    )
479                                                }
480                                            });
481
482                                            let name_label = path.file_name().map(|name| {
483                                                Label::new(name.to_string_lossy().to_string())
484                                                    .size(LabelSize::Small)
485                                            });
486
487                                            let file_icon = FileIcons::get_icon(&path, cx)
488                                                .map(Icon::from_path)
489                                                .unwrap_or_else(|| Icon::new(IconName::File));
490
491                                            let element = div()
492                                                .p_2()
493                                                .when(index + 1 < changed_buffers_count, |parent| {
494                                                    parent
495                                                        .border_color(cx.theme().colors().border)
496                                                        .border_b_1()
497                                                })
498                                                .child(
499                                                    h_flex()
500                                                        .gap_2()
501                                                        .child(file_icon)
502                                                        .child(
503                                                            // TODO: handle overflow
504                                                            h_flex()
505                                                                .children(parent_label)
506                                                                .children(name_label),
507                                                        )
508                                                        // TODO: show lines changed
509                                                        .child(
510                                                            Label::new("+").color(Color::Created),
511                                                        )
512                                                        .child(
513                                                            Label::new("-").color(Color::Deleted),
514                                                        )
515                                                        .when(!changed.needs_review, |parent| {
516                                                            parent.child(
517                                                                Icon::new(IconName::Check)
518                                                                    .color(Color::Success),
519                                                            )
520                                                        }),
521                                                );
522
523                                            Some(element)
524                                        },
525                                    ),
526                                ),
527                            )
528                        }),
529                )
530            })
531            .child(
532                v_flex()
533                    .key_context("MessageEditor")
534                    .on_action(cx.listener(Self::chat))
535                    .on_action(cx.listener(|this, _: &ToggleProfileSelector, window, cx| {
536                        this.profile_selector
537                            .read(cx)
538                            .menu_handle()
539                            .toggle(window, cx);
540                    }))
541                    .on_action(cx.listener(|this, _: &ToggleModelSelector, window, cx| {
542                        this.model_selector
543                            .update(cx, |model_selector, cx| model_selector.toggle(window, cx));
544                    }))
545                    .on_action(cx.listener(Self::toggle_context_picker))
546                    .on_action(cx.listener(Self::remove_all_context))
547                    .on_action(cx.listener(Self::move_up))
548                    .on_action(cx.listener(Self::toggle_chat_mode))
549                    .gap_2()
550                    .p_2()
551                    .bg(editor_bg_color)
552                    .border_t_1()
553                    .border_color(cx.theme().colors().border)
554                    .child(h_flex().justify_between().child(self.context_strip.clone()))
555                    .child(
556                        v_flex()
557                            .gap_5()
558                            .child({
559                                let settings = ThemeSettings::get_global(cx);
560                                let text_style = TextStyle {
561                                    color: cx.theme().colors().text,
562                                    font_family: settings.ui_font.family.clone(),
563                                    font_fallbacks: settings.ui_font.fallbacks.clone(),
564                                    font_features: settings.ui_font.features.clone(),
565                                    font_size: font_size.into(),
566                                    font_weight: settings.ui_font.weight,
567                                    line_height: line_height.into(),
568                                    ..Default::default()
569                                };
570
571                                EditorElement::new(
572                                    &self.editor,
573                                    EditorStyle {
574                                        background: editor_bg_color,
575                                        local_player: cx.theme().players().local(),
576                                        text: text_style,
577                                        syntax: cx.theme().syntax().clone(),
578                                        ..Default::default()
579                                    },
580                                )
581                            })
582                            .child(
583                                PopoverMenu::new("inline-context-picker")
584                                    .menu(move |window, cx| {
585                                        inline_context_picker.update(cx, |this, cx| {
586                                            this.init(window, cx);
587                                        });
588
589                                        Some(inline_context_picker.clone())
590                                    })
591                                    .attach(gpui::Corner::TopLeft)
592                                    .anchor(gpui::Corner::BottomLeft)
593                                    .offset(gpui::Point {
594                                        x: px(0.0),
595                                        y: (-ThemeSettings::get_global(cx).ui_font_size(cx) * 2)
596                                            - px(4.0),
597                                    })
598                                    .with_handle(self.inline_context_picker_menu_handle.clone()),
599                            )
600                            .child(
601                                h_flex()
602                                    .justify_between()
603                                    .child(h_flex().gap_2().child(self.profile_selector.clone()))
604                                    .child(
605                                        h_flex().gap_1().child(self.model_selector.clone()).child(
606                                            ButtonLike::new("submit-message")
607                                                .width(button_width.into())
608                                                .style(ButtonStyle::Filled)
609                                                .disabled(
610                                                    is_editor_empty
611                                                        || !is_model_selected
612                                                        || is_generating,
613                                                )
614                                                .child(
615                                                    h_flex()
616                                                        .w_full()
617                                                        .justify_between()
618                                                        .child(
619                                                            Label::new("Submit")
620                                                                .size(LabelSize::Small)
621                                                                .color(submit_label_color),
622                                                        )
623                                                        .children(
624                                                            KeyBinding::for_action_in(
625                                                                &Chat,
626                                                                &focus_handle,
627                                                                window,
628                                                                cx,
629                                                            )
630                                                            .map(|binding| {
631                                                                binding
632                                                                    .when(vim_mode_enabled, |kb| {
633                                                                        kb.size(rems_from_px(12.))
634                                                                    })
635                                                                    .into_any_element()
636                                                            }),
637                                                        ),
638                                                )
639                                                .on_click(move |_event, window, cx| {
640                                                    focus_handle.dispatch_action(&Chat, window, cx);
641                                                })
642                                                .when(is_editor_empty, |button| {
643                                                    button.tooltip(Tooltip::text(
644                                                        "Type a message to submit",
645                                                    ))
646                                                })
647                                                .when(is_generating, |button| {
648                                                    button.tooltip(Tooltip::text(
649                                                        "Cancel to submit a new message",
650                                                    ))
651                                                })
652                                                .when(!is_model_selected, |button| {
653                                                    button.tooltip(Tooltip::text(
654                                                        "Select a model to continue",
655                                                    ))
656                                                }),
657                                        ),
658                                    ),
659                            ),
660                    ),
661            )
662    }
663}