assistant2: Allow removing individual context (#21868)

This PR adds the ability to remove individual pieces of context from the
message editor:

<img width="1159" alt="Screenshot 2024-12-11 at 12 38 45 PM"
src="https://github.com/user-attachments/assets/77d04272-f667-4ebb-a567-84b382afef3d"
/>

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2024-12-11 12:51:05 -05:00 committed by GitHub
parent 124e63d07c
commit b3ffbea376
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 65 additions and 18 deletions

View file

@ -1,3 +1,5 @@
use std::rc::Rc;
use editor::{Editor, EditorElement, EditorStyle};
use gpui::{AppContext, FocusableView, Model, TextStyle, View};
use language_model::{LanguageModelRegistry, LanguageModelRequestTool};
@ -10,7 +12,7 @@ use ui::{
PopoverMenuHandle, Tooltip,
};
use crate::context::{Context, ContextKind};
use crate::context::{Context, ContextId, ContextKind};
use crate::context_picker::{ContextPicker, ContextPickerDelegate};
use crate::thread::{RequestKind, Thread};
use crate::ui::ContextPill;
@ -20,19 +22,14 @@ pub struct MessageEditor {
thread: Model<Thread>,
editor: View<Editor>,
context: Vec<Context>,
next_context_id: ContextId,
pub(crate) context_picker_handle: PopoverMenuHandle<Picker<ContextPickerDelegate>>,
use_tools: bool,
}
impl MessageEditor {
pub fn new(thread: Model<Thread>, cx: &mut ViewContext<Self>) -> Self {
let mocked_context = vec![Context {
name: "shape.rs".into(),
kind: ContextKind::File,
text: "```rs\npub enum Shape {\n Circle,\n Square,\n Triangle,\n}".into(),
}];
Self {
let mut this = Self {
thread,
editor: cx.new_view(|cx| {
let mut editor = Editor::auto_height(80, cx);
@ -40,10 +37,20 @@ impl MessageEditor {
editor
}),
context: mocked_context,
context: Vec::new(),
next_context_id: ContextId(0),
context_picker_handle: PopoverMenuHandle::default(),
use_tools: false,
}
};
this.context.push(Context {
id: this.next_context_id.post_inc(),
name: "shape.rs".into(),
kind: ContextKind::File,
text: "```rs\npub enum Shape {\n Circle,\n Square,\n Triangle,\n}".into(),
});
this
}
fn chat(&mut self, _: &Chat, cx: &mut ViewContext<Self>) {
@ -178,11 +185,15 @@ impl Render for MessageEditor {
.shape(IconButtonShape::Square)
.icon_size(IconSize::Small),
))
.children(
self.context
.iter()
.map(|context| ContextPill::new(context.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)