message_editor.rs

  1use std::sync::Arc;
  2
  3use editor::actions::MoveUp;
  4use editor::{Editor, EditorElement, EditorEvent, EditorStyle};
  5use fs::Fs;
  6use gpui::{
  7    pulsating_between, Animation, AnimationExt, AppContext, DismissEvent, FocusableView, Model,
  8    Subscription, TextStyle, View, WeakModel, WeakView,
  9};
 10use language_model::{LanguageModelRegistry, LanguageModelRequestTool};
 11use language_model_selector::LanguageModelSelector;
 12use rope::Point;
 13use settings::Settings;
 14use std::time::Duration;
 15use theme::ThemeSettings;
 16use ui::{prelude::*, ButtonLike, KeyBinding, PopoverMenu, PopoverMenuHandle, Switch, TintColor};
 17use workspace::Workspace;
 18
 19use crate::assistant_model_selector::AssistantModelSelector;
 20use crate::context_picker::{ConfirmBehavior, ContextPicker};
 21use crate::context_store::{refresh_context_store_text, ContextStore};
 22use crate::context_strip::{ContextStrip, ContextStripEvent, SuggestContextKind};
 23use crate::thread::{RequestKind, Thread};
 24use crate::thread_store::ThreadStore;
 25use crate::{Chat, ChatMode, RemoveAllContext, ToggleContextPicker, ToggleModelSelector};
 26
 27pub struct MessageEditor {
 28    thread: Model<Thread>,
 29    editor: View<Editor>,
 30    context_store: Model<ContextStore>,
 31    context_strip: View<ContextStrip>,
 32    context_picker_menu_handle: PopoverMenuHandle<ContextPicker>,
 33    inline_context_picker: View<ContextPicker>,
 34    inline_context_picker_menu_handle: PopoverMenuHandle<ContextPicker>,
 35    model_selector: View<AssistantModelSelector>,
 36    model_selector_menu_handle: PopoverMenuHandle<LanguageModelSelector>,
 37    use_tools: bool,
 38    _subscriptions: Vec<Subscription>,
 39}
 40
 41impl MessageEditor {
 42    pub fn new(
 43        fs: Arc<dyn Fs>,
 44        workspace: WeakView<Workspace>,
 45        thread_store: WeakModel<ThreadStore>,
 46        thread: Model<Thread>,
 47        cx: &mut ViewContext<Self>,
 48    ) -> Self {
 49        let context_store = cx.new_model(|_cx| ContextStore::new(workspace.clone()));
 50        let context_picker_menu_handle = PopoverMenuHandle::default();
 51        let inline_context_picker_menu_handle = PopoverMenuHandle::default();
 52        let model_selector_menu_handle = PopoverMenuHandle::default();
 53
 54        let editor = cx.new_view(|cx| {
 55            let mut editor = Editor::auto_height(10, cx);
 56            editor.set_placeholder_text("Ask anything, @ to mention, ↑ to select", cx);
 57            editor.set_show_indent_guides(false, cx);
 58
 59            editor
 60        });
 61
 62        let inline_context_picker = cx.new_view(|cx| {
 63            ContextPicker::new(
 64                workspace.clone(),
 65                Some(thread_store.clone()),
 66                context_store.downgrade(),
 67                editor.downgrade(),
 68                ConfirmBehavior::Close,
 69                cx,
 70            )
 71        });
 72
 73        let context_strip = cx.new_view(|cx| {
 74            ContextStrip::new(
 75                context_store.clone(),
 76                workspace.clone(),
 77                editor.downgrade(),
 78                Some(thread_store.clone()),
 79                context_picker_menu_handle.clone(),
 80                SuggestContextKind::File,
 81                cx,
 82            )
 83        });
 84
 85        let subscriptions = vec![
 86            cx.subscribe(&editor, Self::handle_editor_event),
 87            cx.subscribe(
 88                &inline_context_picker,
 89                Self::handle_inline_context_picker_event,
 90            ),
 91            cx.subscribe(&context_strip, Self::handle_context_strip_event),
 92        ];
 93
 94        Self {
 95            thread,
 96            editor: editor.clone(),
 97            context_store,
 98            context_strip,
 99            context_picker_menu_handle,
100            inline_context_picker,
101            inline_context_picker_menu_handle,
102            model_selector: cx.new_view(|cx| {
103                AssistantModelSelector::new(
104                    fs,
105                    model_selector_menu_handle.clone(),
106                    editor.focus_handle(cx),
107                    cx,
108                )
109            }),
110            model_selector_menu_handle,
111            use_tools: false,
112            _subscriptions: subscriptions,
113        }
114    }
115
116    fn toggle_model_selector(&mut self, _: &ToggleModelSelector, cx: &mut ViewContext<Self>) {
117        self.model_selector_menu_handle.toggle(cx)
118    }
119
120    fn toggle_chat_mode(&mut self, _: &ChatMode, cx: &mut ViewContext<Self>) {
121        self.use_tools = !self.use_tools;
122        cx.notify();
123    }
124
125    fn toggle_context_picker(&mut self, _: &ToggleContextPicker, cx: &mut ViewContext<Self>) {
126        self.context_picker_menu_handle.toggle(cx);
127    }
128
129    pub fn remove_all_context(&mut self, _: &RemoveAllContext, cx: &mut ViewContext<Self>) {
130        self.context_store.update(cx, |store, _cx| store.clear());
131        cx.notify();
132    }
133
134    fn chat(&mut self, _: &Chat, cx: &mut ViewContext<Self>) {
135        self.send_to_model(RequestKind::Chat, cx);
136    }
137
138    fn send_to_model(&mut self, request_kind: RequestKind, cx: &mut ViewContext<Self>) {
139        let provider = LanguageModelRegistry::read_global(cx).active_provider();
140        if provider
141            .as_ref()
142            .map_or(false, |provider| provider.must_accept_terms(cx))
143        {
144            cx.notify();
145            return;
146        }
147
148        let model_registry = LanguageModelRegistry::read_global(cx);
149        let Some(model) = model_registry.active_model() else {
150            return;
151        };
152
153        let user_message = self.editor.update(cx, |editor, cx| {
154            let text = editor.text(cx);
155            editor.clear(cx);
156            text
157        });
158
159        let refresh_task = refresh_context_store_text(self.context_store.clone(), cx);
160
161        let thread = self.thread.clone();
162        let context_store = self.context_store.clone();
163        let use_tools = self.use_tools;
164        cx.spawn(move |_, mut cx| async move {
165            refresh_task.await;
166            thread
167                .update(&mut cx, |thread, cx| {
168                    let context = context_store.read(cx).snapshot(cx).collect::<Vec<_>>();
169                    thread.insert_user_message(user_message, context, cx);
170                    let mut request = thread.to_completion_request(request_kind, cx);
171
172                    if use_tools {
173                        request.tools = thread
174                            .tools()
175                            .tools(cx)
176                            .into_iter()
177                            .map(|tool| LanguageModelRequestTool {
178                                name: tool.name(),
179                                description: tool.description(),
180                                input_schema: tool.input_schema(),
181                            })
182                            .collect();
183                    }
184
185                    thread.stream_completion(request, model, cx)
186                })
187                .ok();
188        })
189        .detach();
190    }
191
192    fn handle_editor_event(
193        &mut self,
194        editor: View<Editor>,
195        event: &EditorEvent,
196        cx: &mut ViewContext<Self>,
197    ) {
198        match event {
199            EditorEvent::SelectionsChanged { .. } => {
200                editor.update(cx, |editor, cx| {
201                    let snapshot = editor.buffer().read(cx).snapshot(cx);
202                    let newest_cursor = editor.selections.newest::<Point>(cx).head();
203                    if newest_cursor.column > 0 {
204                        let behind_cursor = Point::new(newest_cursor.row, newest_cursor.column - 1);
205                        let char_behind_cursor = snapshot.chars_at(behind_cursor).next();
206                        if char_behind_cursor == Some('@') {
207                            self.inline_context_picker_menu_handle.show(cx);
208                        }
209                    }
210                });
211            }
212            _ => {}
213        }
214    }
215
216    fn handle_inline_context_picker_event(
217        &mut self,
218        _inline_context_picker: View<ContextPicker>,
219        _event: &DismissEvent,
220        cx: &mut ViewContext<Self>,
221    ) {
222        let editor_focus_handle = self.editor.focus_handle(cx);
223        cx.focus(&editor_focus_handle);
224    }
225
226    fn handle_context_strip_event(
227        &mut self,
228        _context_strip: View<ContextStrip>,
229        event: &ContextStripEvent,
230        cx: &mut ViewContext<Self>,
231    ) {
232        match event {
233            ContextStripEvent::PickerDismissed
234            | ContextStripEvent::BlurredEmpty
235            | ContextStripEvent::BlurredDown => {
236                let editor_focus_handle = self.editor.focus_handle(cx);
237                cx.focus(&editor_focus_handle);
238            }
239            ContextStripEvent::BlurredUp => {}
240        }
241    }
242
243    fn move_up(&mut self, _: &MoveUp, cx: &mut ViewContext<Self>) {
244        if self.context_picker_menu_handle.is_deployed() {
245            cx.propagate();
246        } else {
247            cx.focus_view(&self.context_strip);
248        }
249    }
250}
251
252impl FocusableView for MessageEditor {
253    fn focus_handle(&self, cx: &AppContext) -> gpui::FocusHandle {
254        self.editor.focus_handle(cx)
255    }
256}
257
258impl Render for MessageEditor {
259    fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
260        let font_size = TextSize::Default.rems(cx);
261        let line_height = font_size.to_pixels(cx.rem_size()) * 1.5;
262        let focus_handle = self.editor.focus_handle(cx);
263        let inline_context_picker = self.inline_context_picker.clone();
264        let bg_color = cx.theme().colors().editor_background;
265        let is_streaming_completion = self.thread.read(cx).is_streaming();
266        let button_width = px(64.);
267
268        v_flex()
269            .key_context("MessageEditor")
270            .on_action(cx.listener(Self::chat))
271            .on_action(cx.listener(Self::toggle_model_selector))
272            .on_action(cx.listener(Self::toggle_context_picker))
273            .on_action(cx.listener(Self::remove_all_context))
274            .on_action(cx.listener(Self::move_up))
275            .on_action(cx.listener(Self::toggle_chat_mode))
276            .size_full()
277            .gap_2()
278            .p_2()
279            .bg(bg_color)
280            .child(self.context_strip.clone())
281            .child(
282                v_flex()
283                    .gap_4()
284                    .child({
285                        let settings = ThemeSettings::get_global(cx);
286                        let text_style = TextStyle {
287                            color: cx.theme().colors().text,
288                            font_family: settings.ui_font.family.clone(),
289                            font_features: settings.ui_font.features.clone(),
290                            font_size: font_size.into(),
291                            font_weight: settings.ui_font.weight,
292                            line_height: line_height.into(),
293                            ..Default::default()
294                        };
295
296                        EditorElement::new(
297                            &self.editor,
298                            EditorStyle {
299                                background: bg_color,
300                                local_player: cx.theme().players().local(),
301                                text: text_style,
302                                ..Default::default()
303                            },
304                        )
305                    })
306                    .child(
307                        PopoverMenu::new("inline-context-picker")
308                            .menu(move |cx| {
309                                inline_context_picker.update(cx, |this, cx| {
310                                    this.init(cx);
311                                });
312
313                                Some(inline_context_picker.clone())
314                            })
315                            .attach(gpui::Corner::TopLeft)
316                            .anchor(gpui::Corner::BottomLeft)
317                            .offset(gpui::Point {
318                                x: px(0.0),
319                                y: px(-ThemeSettings::clamp_font_size(
320                                    ThemeSettings::get_global(cx).ui_font_size,
321                                )
322                                .0 * 2.0)
323                                    - px(4.0),
324                            })
325                            .with_handle(self.inline_context_picker_menu_handle.clone()),
326                    )
327                    .child(
328                        h_flex()
329                            .justify_between()
330                            .child(
331                                Switch::new("use-tools", self.use_tools.into())
332                                    .label("Tools")
333                                    .on_click(cx.listener(|this, selection, _cx| {
334                                        this.use_tools = match selection {
335                                            ToggleState::Selected => true,
336                                            ToggleState::Unselected
337                                            | ToggleState::Indeterminate => false,
338                                        };
339                                    }))
340                                    .key_binding(KeyBinding::for_action_in(
341                                        &ChatMode,
342                                        &focus_handle,
343                                        cx,
344                                    )),
345                            )
346                            .child(h_flex().gap_1().child(self.model_selector.clone()).child(
347                                if is_streaming_completion {
348                                    ButtonLike::new("cancel-generation")
349                                        .width(button_width.into())
350                                        .style(ButtonStyle::Tinted(TintColor::Accent))
351                                        .child(
352                                            h_flex()
353                                                .w_full()
354                                                .justify_between()
355                                                .child(
356                                                    Label::new("Cancel")
357                                                        .size(LabelSize::Small)
358                                                        .with_animation(
359                                                            "pulsating-label",
360                                                            Animation::new(Duration::from_secs(2))
361                                                                .repeat()
362                                                                .with_easing(pulsating_between(
363                                                                    0.4, 0.8,
364                                                                )),
365                                                            |label, delta| label.alpha(delta),
366                                                        ),
367                                                )
368                                                .children(
369                                                    KeyBinding::for_action_in(
370                                                        &editor::actions::Cancel,
371                                                        &focus_handle,
372                                                        cx,
373                                                    )
374                                                    .map(|binding| binding.into_any_element()),
375                                                ),
376                                        )
377                                        .on_click(move |_event, cx| {
378                                            focus_handle
379                                                .dispatch_action(&editor::actions::Cancel, cx);
380                                        })
381                                } else {
382                                    ButtonLike::new("submit-message")
383                                        .width(button_width.into())
384                                        .style(ButtonStyle::Filled)
385                                        .child(
386                                            h_flex()
387                                                .w_full()
388                                                .justify_between()
389                                                .child(Label::new("Submit").size(LabelSize::Small))
390                                                .children(
391                                                    KeyBinding::for_action_in(
392                                                        &Chat,
393                                                        &focus_handle,
394                                                        cx,
395                                                    )
396                                                    .map(|binding| binding.into_any_element()),
397                                                ),
398                                        )
399                                        .on_click(move |_event, cx| {
400                                            focus_handle.dispatch_action(&Chat, cx);
401                                        })
402                                },
403                            )),
404                    ),
405            )
406    }
407}