assistant2: Factor out ContextStrip (#22096)

This PR factors a `ContextStrip` view out of the `MessageEditor` so that
we can use it in other places.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2024-12-16 12:45:01 -05:00 committed by GitHub
parent ff2ad63037
commit caefdcd7f1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 154 additions and 125 deletions

View file

@ -3,6 +3,7 @@ mod assistant_panel;
mod assistant_settings; mod assistant_settings;
mod context; mod context;
mod context_picker; mod context_picker;
mod context_strip;
mod inline_assistant; mod inline_assistant;
mod message_editor; mod message_editor;
mod prompts; mod prompts;

View file

@ -16,7 +16,7 @@ use workspace::Workspace;
use crate::context_picker::fetch_context_picker::FetchContextPicker; use crate::context_picker::fetch_context_picker::FetchContextPicker;
use crate::context_picker::file_context_picker::FileContextPicker; use crate::context_picker::file_context_picker::FileContextPicker;
use crate::context_picker::thread_context_picker::ThreadContextPicker; use crate::context_picker::thread_context_picker::ThreadContextPicker;
use crate::message_editor::MessageEditor; use crate::context_strip::ContextStrip;
use crate::thread_store::ThreadStore; use crate::thread_store::ThreadStore;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -36,14 +36,14 @@ impl ContextPicker {
pub fn new( pub fn new(
workspace: WeakView<Workspace>, workspace: WeakView<Workspace>,
thread_store: WeakModel<ThreadStore>, thread_store: WeakModel<ThreadStore>,
message_editor: WeakView<MessageEditor>, context_strip: WeakView<ContextStrip>,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Self { ) -> Self {
let delegate = ContextPickerDelegate { let delegate = ContextPickerDelegate {
context_picker: cx.view().downgrade(), context_picker: cx.view().downgrade(),
workspace, workspace,
thread_store, thread_store,
message_editor, context_strip,
entries: vec![ entries: vec![
ContextPickerEntry { ContextPickerEntry {
name: "directory".into(), name: "directory".into(),
@ -122,7 +122,7 @@ pub(crate) struct ContextPickerDelegate {
context_picker: WeakView<ContextPicker>, context_picker: WeakView<ContextPicker>,
workspace: WeakView<Workspace>, workspace: WeakView<Workspace>,
thread_store: WeakModel<ThreadStore>, thread_store: WeakModel<ThreadStore>,
message_editor: WeakView<MessageEditor>, context_strip: WeakView<ContextStrip>,
entries: Vec<ContextPickerEntry>, entries: Vec<ContextPickerEntry>,
selected_ix: usize, selected_ix: usize,
} }
@ -161,7 +161,7 @@ impl PickerDelegate for ContextPickerDelegate {
FileContextPicker::new( FileContextPicker::new(
self.context_picker.clone(), self.context_picker.clone(),
self.workspace.clone(), self.workspace.clone(),
self.message_editor.clone(), self.context_strip.clone(),
cx, cx,
) )
})); }));
@ -171,7 +171,7 @@ impl PickerDelegate for ContextPickerDelegate {
FetchContextPicker::new( FetchContextPicker::new(
self.context_picker.clone(), self.context_picker.clone(),
self.workspace.clone(), self.workspace.clone(),
self.message_editor.clone(), self.context_strip.clone(),
cx, cx,
) )
})); }));
@ -181,7 +181,7 @@ impl PickerDelegate for ContextPickerDelegate {
ThreadContextPicker::new( ThreadContextPicker::new(
self.thread_store.clone(), self.thread_store.clone(),
self.context_picker.clone(), self.context_picker.clone(),
self.message_editor.clone(), self.context_strip.clone(),
cx, cx,
) )
})); }));

View file

@ -13,7 +13,7 @@ use workspace::Workspace;
use crate::context::ContextKind; use crate::context::ContextKind;
use crate::context_picker::ContextPicker; use crate::context_picker::ContextPicker;
use crate::message_editor::MessageEditor; use crate::context_strip::ContextStrip;
pub struct FetchContextPicker { pub struct FetchContextPicker {
picker: View<Picker<FetchContextPickerDelegate>>, picker: View<Picker<FetchContextPickerDelegate>>,
@ -23,10 +23,10 @@ impl FetchContextPicker {
pub fn new( pub fn new(
context_picker: WeakView<ContextPicker>, context_picker: WeakView<ContextPicker>,
workspace: WeakView<Workspace>, workspace: WeakView<Workspace>,
message_editor: WeakView<MessageEditor>, context_strip: WeakView<ContextStrip>,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Self { ) -> Self {
let delegate = FetchContextPickerDelegate::new(context_picker, workspace, message_editor); let delegate = FetchContextPickerDelegate::new(context_picker, workspace, context_strip);
let picker = cx.new_view(|cx| Picker::uniform_list(delegate, cx)); let picker = cx.new_view(|cx| Picker::uniform_list(delegate, cx));
Self { picker } Self { picker }
@ -55,7 +55,7 @@ enum ContentType {
pub struct FetchContextPickerDelegate { pub struct FetchContextPickerDelegate {
context_picker: WeakView<ContextPicker>, context_picker: WeakView<ContextPicker>,
workspace: WeakView<Workspace>, workspace: WeakView<Workspace>,
message_editor: WeakView<MessageEditor>, context_strip: WeakView<ContextStrip>,
url: String, url: String,
} }
@ -63,12 +63,12 @@ impl FetchContextPickerDelegate {
pub fn new( pub fn new(
context_picker: WeakView<ContextPicker>, context_picker: WeakView<ContextPicker>,
workspace: WeakView<Workspace>, workspace: WeakView<Workspace>,
message_editor: WeakView<MessageEditor>, context_strip: WeakView<ContextStrip>,
) -> Self { ) -> Self {
FetchContextPickerDelegate { FetchContextPickerDelegate {
context_picker, context_picker,
workspace, workspace,
message_editor, context_strip,
url: String::new(), url: String::new(),
} }
} }
@ -189,9 +189,9 @@ impl PickerDelegate for FetchContextPickerDelegate {
this.update(&mut cx, |this, cx| { this.update(&mut cx, |this, cx| {
this.delegate this.delegate
.message_editor .context_strip
.update(cx, |message_editor, _cx| { .update(cx, |context_strip, _cx| {
message_editor.insert_context(ContextKind::FetchedUrl, url, text); context_strip.insert_context(ContextKind::FetchedUrl, url, text);
}) })
})??; })??;

View file

@ -14,7 +14,7 @@ use workspace::Workspace;
use crate::context::ContextKind; use crate::context::ContextKind;
use crate::context_picker::ContextPicker; use crate::context_picker::ContextPicker;
use crate::message_editor::MessageEditor; use crate::context_strip::ContextStrip;
pub struct FileContextPicker { pub struct FileContextPicker {
picker: View<Picker<FileContextPickerDelegate>>, picker: View<Picker<FileContextPickerDelegate>>,
@ -24,10 +24,10 @@ impl FileContextPicker {
pub fn new( pub fn new(
context_picker: WeakView<ContextPicker>, context_picker: WeakView<ContextPicker>,
workspace: WeakView<Workspace>, workspace: WeakView<Workspace>,
message_editor: WeakView<MessageEditor>, context_strip: WeakView<ContextStrip>,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Self { ) -> Self {
let delegate = FileContextPickerDelegate::new(context_picker, workspace, message_editor); let delegate = FileContextPickerDelegate::new(context_picker, workspace, context_strip);
let picker = cx.new_view(|cx| Picker::uniform_list(delegate, cx)); let picker = cx.new_view(|cx| Picker::uniform_list(delegate, cx));
Self { picker } Self { picker }
@ -49,7 +49,7 @@ impl Render for FileContextPicker {
pub struct FileContextPickerDelegate { pub struct FileContextPickerDelegate {
context_picker: WeakView<ContextPicker>, context_picker: WeakView<ContextPicker>,
workspace: WeakView<Workspace>, workspace: WeakView<Workspace>,
message_editor: WeakView<MessageEditor>, context_strip: WeakView<ContextStrip>,
matches: Vec<PathMatch>, matches: Vec<PathMatch>,
selected_index: usize, selected_index: usize,
} }
@ -58,12 +58,12 @@ impl FileContextPickerDelegate {
pub fn new( pub fn new(
context_picker: WeakView<ContextPicker>, context_picker: WeakView<ContextPicker>,
workspace: WeakView<Workspace>, workspace: WeakView<Workspace>,
message_editor: WeakView<MessageEditor>, context_strip: WeakView<ContextStrip>,
) -> Self { ) -> Self {
Self { Self {
context_picker, context_picker,
workspace, workspace,
message_editor, context_strip,
matches: Vec::new(), matches: Vec::new(),
selected_index: 0, selected_index: 0,
} }
@ -214,24 +214,22 @@ impl PickerDelegate for FileContextPickerDelegate {
let buffer = open_buffer_task.await?; let buffer = open_buffer_task.await?;
this.update(&mut cx, |this, cx| { this.update(&mut cx, |this, cx| {
this.delegate this.delegate.context_strip.update(cx, |context_strip, cx| {
.message_editor let mut text = String::new();
.update(cx, |message_editor, cx| { text.push_str(&codeblock_fence_for_path(Some(&path), None));
let mut text = String::new(); text.push_str(&buffer.read(cx).text());
text.push_str(&codeblock_fence_for_path(Some(&path), None)); if !text.ends_with('\n') {
text.push_str(&buffer.read(cx).text()); text.push('\n');
if !text.ends_with('\n') { }
text.push('\n');
}
text.push_str("```\n"); text.push_str("```\n");
message_editor.insert_context( context_strip.insert_context(
ContextKind::File, ContextKind::File,
path.to_string_lossy().to_string(), path.to_string_lossy().to_string(),
text, text,
); );
}) })
})??; })??;
anyhow::Ok(()) anyhow::Ok(())

View file

@ -7,7 +7,7 @@ use ui::{prelude::*, ListItem};
use crate::context::ContextKind; use crate::context::ContextKind;
use crate::context_picker::ContextPicker; use crate::context_picker::ContextPicker;
use crate::message_editor::MessageEditor; use crate::context_strip::ContextStrip;
use crate::thread::ThreadId; use crate::thread::ThreadId;
use crate::thread_store::ThreadStore; use crate::thread_store::ThreadStore;
@ -19,11 +19,11 @@ impl ThreadContextPicker {
pub fn new( pub fn new(
thread_store: WeakModel<ThreadStore>, thread_store: WeakModel<ThreadStore>,
context_picker: WeakView<ContextPicker>, context_picker: WeakView<ContextPicker>,
message_editor: WeakView<MessageEditor>, context_strip: WeakView<ContextStrip>,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Self { ) -> Self {
let delegate = let delegate =
ThreadContextPickerDelegate::new(thread_store, context_picker, message_editor); ThreadContextPickerDelegate::new(thread_store, context_picker, context_strip);
let picker = cx.new_view(|cx| Picker::uniform_list(delegate, cx)); let picker = cx.new_view(|cx| Picker::uniform_list(delegate, cx));
ThreadContextPicker { picker } ThreadContextPicker { picker }
@ -51,7 +51,7 @@ struct ThreadContextEntry {
pub struct ThreadContextPickerDelegate { pub struct ThreadContextPickerDelegate {
thread_store: WeakModel<ThreadStore>, thread_store: WeakModel<ThreadStore>,
context_picker: WeakView<ContextPicker>, context_picker: WeakView<ContextPicker>,
message_editor: WeakView<MessageEditor>, context_strip: WeakView<ContextStrip>,
matches: Vec<ThreadContextEntry>, matches: Vec<ThreadContextEntry>,
selected_index: usize, selected_index: usize,
} }
@ -60,12 +60,12 @@ impl ThreadContextPickerDelegate {
pub fn new( pub fn new(
thread_store: WeakModel<ThreadStore>, thread_store: WeakModel<ThreadStore>,
context_picker: WeakView<ContextPicker>, context_picker: WeakView<ContextPicker>,
message_editor: WeakView<MessageEditor>, context_strip: WeakView<ContextStrip>,
) -> Self { ) -> Self {
ThreadContextPickerDelegate { ThreadContextPickerDelegate {
thread_store, thread_store,
context_picker, context_picker,
message_editor, context_strip,
matches: Vec::new(), matches: Vec::new(),
selected_index: 0, selected_index: 0,
} }
@ -157,8 +157,8 @@ impl PickerDelegate for ThreadContextPickerDelegate {
return; return;
}; };
self.message_editor self.context_strip
.update(cx, |message_editor, cx| { .update(cx, |context_strip, cx| {
let text = thread.update(cx, |thread, _cx| { let text = thread.update(cx, |thread, _cx| {
let mut text = String::new(); let mut text = String::new();
@ -177,7 +177,7 @@ impl PickerDelegate for ThreadContextPickerDelegate {
text text
}); });
message_editor.insert_context(ContextKind::Thread, entry.summary.clone(), text); context_strip.insert_context(ContextKind::Thread, entry.summary.clone(), text);
}) })
.ok(); .ok();
} }

View file

@ -0,0 +1,101 @@
use std::rc::Rc;
use gpui::{View, WeakModel, WeakView};
use ui::{prelude::*, IconButtonShape, PopoverMenu, PopoverMenuHandle, Tooltip};
use workspace::Workspace;
use crate::context::{Context, ContextId, ContextKind};
use crate::context_picker::ContextPicker;
use crate::thread_store::ThreadStore;
use crate::ui::ContextPill;
pub struct ContextStrip {
context: Vec<Context>,
next_context_id: ContextId,
context_picker: View<ContextPicker>,
pub(crate) context_picker_handle: PopoverMenuHandle<ContextPicker>,
}
impl ContextStrip {
pub fn new(
workspace: WeakView<Workspace>,
thread_store: WeakModel<ThreadStore>,
cx: &mut ViewContext<Self>,
) -> Self {
let weak_self = cx.view().downgrade();
Self {
context: Vec::new(),
next_context_id: ContextId(0),
context_picker: cx.new_view(|cx| {
ContextPicker::new(workspace.clone(), thread_store.clone(), weak_self, cx)
}),
context_picker_handle: PopoverMenuHandle::default(),
}
}
pub fn drain(&mut self) -> Vec<Context> {
self.context.drain(..).collect()
}
pub fn insert_context(
&mut self,
kind: ContextKind,
name: impl Into<SharedString>,
text: impl Into<SharedString>,
) {
self.context.push(Context {
id: self.next_context_id.post_inc(),
name: name.into(),
kind,
text: text.into(),
});
}
}
impl Render for ContextStrip {
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
let context_picker = self.context_picker.clone();
h_flex()
.flex_wrap()
.gap_2()
.child(
PopoverMenu::new("context-picker")
.menu(move |_cx| Some(context_picker.clone()))
.trigger(
IconButton::new("add-context", IconName::Plus)
.shape(IconButtonShape::Square)
.icon_size(IconSize::Small),
)
.attach(gpui::AnchorCorner::TopLeft)
.anchor(gpui::AnchorCorner::BottomLeft)
.offset(gpui::Point {
x: px(0.0),
y: px(-16.0),
})
.with_handle(self.context_picker_handle.clone()),
)
.children(self.context.iter().map(|context| {
ContextPill::new(context.clone()).on_remove({
let context = context.clone();
Rc::new(cx.listener(move |this, _event, cx| {
this.context.retain(|other| other.id != context.id);
cx.notify();
}))
})
}))
.when(!self.context.is_empty(), |parent| {
parent.child(
IconButton::new("remove-all-context", IconName::Eraser)
.shape(IconButtonShape::Square)
.icon_size(IconSize::Small)
.tooltip(move |cx| Tooltip::text("Remove All Context", cx))
.on_click(cx.listener(|this, _event, cx| {
this.context.clear();
cx.notify();
})),
)
})
}
}

View file

@ -1,31 +1,21 @@
use std::rc::Rc;
use editor::{Editor, EditorElement, EditorStyle}; use editor::{Editor, EditorElement, EditorStyle};
use gpui::{AppContext, FocusableView, Model, TextStyle, View, WeakModel, WeakView}; use gpui::{AppContext, FocusableView, Model, TextStyle, View, WeakModel, WeakView};
use language_model::{LanguageModelRegistry, LanguageModelRequestTool}; use language_model::{LanguageModelRegistry, LanguageModelRequestTool};
use language_model_selector::{LanguageModelSelector, LanguageModelSelectorPopoverMenu}; use language_model_selector::{LanguageModelSelector, LanguageModelSelectorPopoverMenu};
use settings::Settings; use settings::Settings;
use theme::ThemeSettings; use theme::ThemeSettings;
use ui::{ use ui::{prelude::*, ButtonLike, CheckboxWithLabel, ElevationIndex, KeyBinding, Tooltip};
prelude::*, ButtonLike, CheckboxWithLabel, ElevationIndex, IconButtonShape, KeyBinding,
PopoverMenu, PopoverMenuHandle, Tooltip,
};
use workspace::Workspace; use workspace::Workspace;
use crate::context::{Context, ContextId, ContextKind}; use crate::context_strip::ContextStrip;
use crate::context_picker::ContextPicker;
use crate::thread::{RequestKind, Thread}; use crate::thread::{RequestKind, Thread};
use crate::thread_store::ThreadStore; use crate::thread_store::ThreadStore;
use crate::ui::ContextPill;
use crate::{Chat, ToggleModelSelector}; use crate::{Chat, ToggleModelSelector};
pub struct MessageEditor { pub struct MessageEditor {
thread: Model<Thread>, thread: Model<Thread>,
editor: View<Editor>, editor: View<Editor>,
context: Vec<Context>, context_strip: View<ContextStrip>,
next_context_id: ContextId,
context_picker: View<ContextPicker>,
pub(crate) context_picker_handle: PopoverMenuHandle<ContextPicker>,
language_model_selector: View<LanguageModelSelector>, language_model_selector: View<LanguageModelSelector>,
use_tools: bool, use_tools: bool,
} }
@ -37,7 +27,6 @@ impl MessageEditor {
thread: Model<Thread>, thread: Model<Thread>,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Self { ) -> Self {
let weak_self = cx.view().downgrade();
Self { Self {
thread, thread,
editor: cx.new_view(|cx| { editor: cx.new_view(|cx| {
@ -46,12 +35,8 @@ impl MessageEditor {
editor editor
}), }),
context: Vec::new(), context_strip: cx
next_context_id: ContextId(0), .new_view(|cx| ContextStrip::new(workspace.clone(), thread_store.clone(), cx)),
context_picker: cx.new_view(|cx| {
ContextPicker::new(workspace.clone(), thread_store.clone(), weak_self, cx)
}),
context_picker_handle: PopoverMenuHandle::default(),
language_model_selector: cx.new_view(|cx| { language_model_selector: cx.new_view(|cx| {
LanguageModelSelector::new( LanguageModelSelector::new(
|model, _cx| { |model, _cx| {
@ -64,20 +49,6 @@ impl MessageEditor {
} }
} }
pub fn insert_context(
&mut self,
kind: ContextKind,
name: impl Into<SharedString>,
text: impl Into<SharedString>,
) {
self.context.push(Context {
id: self.next_context_id.post_inc(),
name: name.into(),
kind,
text: text.into(),
});
}
fn chat(&mut self, _: &Chat, cx: &mut ViewContext<Self>) { fn chat(&mut self, _: &Chat, cx: &mut ViewContext<Self>) {
self.send_to_model(RequestKind::Chat, cx); self.send_to_model(RequestKind::Chat, cx);
} }
@ -104,7 +75,7 @@ impl MessageEditor {
editor.clear(cx); editor.clear(cx);
text text
}); });
let context = self.context.drain(..).collect::<Vec<_>>(); let context = self.context_strip.update(cx, |this, _cx| this.drain());
self.thread.update(cx, |thread, cx| { self.thread.update(cx, |thread, cx| {
thread.insert_user_message(user_message, context, cx); thread.insert_user_message(user_message, context, cx);
@ -190,7 +161,6 @@ impl Render for MessageEditor {
let font_size = TextSize::Default.rems(cx); let font_size = TextSize::Default.rems(cx);
let line_height = font_size.to_pixels(cx.rem_size()) * 1.3; let line_height = font_size.to_pixels(cx.rem_size()) * 1.3;
let focus_handle = self.editor.focus_handle(cx); let focus_handle = self.editor.focus_handle(cx);
let context_picker = self.context_picker.clone();
v_flex() v_flex()
.key_context("MessageEditor") .key_context("MessageEditor")
@ -199,48 +169,7 @@ impl Render for MessageEditor {
.gap_2() .gap_2()
.p_2() .p_2()
.bg(cx.theme().colors().editor_background) .bg(cx.theme().colors().editor_background)
.child( .child(self.context_strip.clone())
h_flex()
.flex_wrap()
.gap_2()
.child(
PopoverMenu::new("context-picker")
.menu(move |_cx| Some(context_picker.clone()))
.trigger(
IconButton::new("add-context", IconName::Plus)
.shape(IconButtonShape::Square)
.icon_size(IconSize::Small),
)
.attach(gpui::AnchorCorner::TopLeft)
.anchor(gpui::AnchorCorner::BottomLeft)
.offset(gpui::Point {
x: px(0.0),
y: px(-16.0),
})
.with_handle(self.context_picker_handle.clone()),
)
.children(self.context.iter().map(|context| {
ContextPill::new(context.clone()).on_remove({
let context = context.clone();
Rc::new(cx.listener(move |this, _event, cx| {
this.context.retain(|other| other.id != context.id);
cx.notify();
}))
})
}))
.when(!self.context.is_empty(), |parent| {
parent.child(
IconButton::new("remove-all-context", IconName::Eraser)
.shape(IconButtonShape::Square)
.icon_size(IconSize::Small)
.tooltip(move |cx| Tooltip::text("Remove All Context", cx))
.on_click(cx.listener(|this, _event, cx| {
this.context.clear();
cx.notify();
})),
)
}),
)
.child({ .child({
let settings = ThemeSettings::get_global(cx); let settings = ThemeSettings::get_global(cx);
let text_style = TextStyle { let text_style = TextStyle {