This commit is contained in:
Antonio Scandurra 2023-06-14 09:47:30 +02:00
parent 7dab17e233
commit 75ad76bfb2

View file

@ -611,6 +611,7 @@ impl Assistant {
}; };
buffer.edit([(offset..offset, text)], None, cx); buffer.edit([(offset..offset, text)], None, cx);
}); });
cx.emit(AssistantEvent::StreamedCompletion);
Some(()) Some(())
}); });
@ -745,7 +746,7 @@ impl Assistant {
fn open_ai_request_messages(&self, cx: &AppContext) -> Vec<RequestMessage> { fn open_ai_request_messages(&self, cx: &AppContext) -> Vec<RequestMessage> {
let buffer = self.buffer.read(cx); let buffer = self.buffer.read(cx);
self.messages(cx) self.messages(cx)
.map(|(message, metadata, range)| RequestMessage { .map(|(_message, metadata, range)| RequestMessage {
role: metadata.role, role: metadata.role,
content: buffer.text_for_range(range).collect(), content: buffer.text_for_range(range).collect(),
}) })
@ -828,7 +829,7 @@ impl AssistantEditor {
cx.subscribe(&editor, Self::handle_editor_event), cx.subscribe(&editor, Self::handle_editor_event),
]; ];
Self { let mut this = Self {
assistant, assistant,
editor, editor,
blocks: Default::default(), blocks: Default::default(),
@ -837,7 +838,9 @@ impl AssistantEditor {
anchor: Anchor::max(), anchor: Anchor::max(),
}, },
_subscriptions, _subscriptions,
} };
this.update_message_headers(cx);
this
} }
fn assist(&mut self, _: &Assist, cx: &mut ViewContext<Self>) { fn assist(&mut self, _: &Assist, cx: &mut ViewContext<Self>) {
@ -891,13 +894,48 @@ impl AssistantEditor {
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) { ) {
match event { match event {
AssistantEvent::MessagesEdited => { AssistantEvent::MessagesEdited => self.update_message_headers(cx),
AssistantEvent::SummaryChanged => {
cx.emit(AssistantEditorEvent::TabContentChanged);
}
AssistantEvent::StreamedCompletion => {
self.editor.update(cx, |editor, cx| {
let snapshot = editor.snapshot(cx);
let scroll_bottom_row = self
.scroll_bottom
.anchor
.to_display_point(&snapshot.display_snapshot)
.row();
let scroll_bottom = scroll_bottom_row as f32 + self.scroll_bottom.offset.y();
let visible_line_count = editor.visible_line_count().unwrap_or(0.);
let scroll_top = scroll_bottom - visible_line_count;
editor
.set_scroll_position(vec2f(self.scroll_bottom.offset.x(), scroll_top), cx);
});
}
}
}
fn handle_editor_event(
&mut self,
_: ViewHandle<Editor>,
event: &editor::Event,
cx: &mut ViewContext<Self>,
) {
match event {
editor::Event::ScrollPositionChanged { .. } => self.update_scroll_bottom(cx),
_ => {}
}
}
fn update_message_headers(&mut self, cx: &mut ViewContext<Self>) {
self.editor.update(cx, |editor, cx| { self.editor.update(cx, |editor, cx| {
let buffer = editor.buffer().read(cx).snapshot(cx); let buffer = editor.buffer().read(cx).snapshot(cx);
let excerpt_id = *buffer.as_singleton().unwrap().0; let excerpt_id = *buffer.as_singleton().unwrap().0;
let old_blocks = std::mem::take(&mut self.blocks); let old_blocks = std::mem::take(&mut self.blocks);
let new_blocks = let new_blocks = self
self.assistant .assistant
.read(cx) .read(cx)
.messages(cx) .messages(cx)
.map(|(message, metadata, _)| BlockProperties { .map(|(message, metadata, _)| BlockProperties {
@ -920,23 +958,19 @@ impl AssistantEditor {
cx, cx,
|state, _| match metadata.role { |state, _| match metadata.role {
Role::User => { Role::User => {
let style = let style = style.user_sender.style_for(state, false);
style.user_sender.style_for(state, false);
Label::new("You", style.text.clone()) Label::new("You", style.text.clone())
.contained() .contained()
.with_style(style.container) .with_style(style.container)
} }
Role::Assistant => { Role::Assistant => {
let style = style let style = style.assistant_sender.style_for(state, false);
.assistant_sender
.style_for(state, false);
Label::new("Assistant", style.text.clone()) Label::new("Assistant", style.text.clone())
.contained() .contained()
.with_style(style.container) .with_style(style.container)
} }
Role::System => { Role::System => {
let style = let style = style.system_sender.style_for(state, false);
style.system_sender.style_for(state, false);
Label::new("System", style.text.clone()) Label::new("System", style.text.clone())
.contained() .contained()
.with_style(style.container) .with_style(style.container)
@ -997,41 +1031,6 @@ impl AssistantEditor {
}); });
} }
AssistantEvent::SummaryChanged => {
cx.emit(AssistantEditorEvent::TabContentChanged);
}
AssistantEvent::StreamedCompletion => {
self.editor.update(cx, |editor, cx| {
let snapshot = editor.snapshot(cx);
let scroll_bottom_row = self
.scroll_bottom
.anchor
.to_display_point(&snapshot.display_snapshot)
.row();
let scroll_bottom = scroll_bottom_row as f32 + self.scroll_bottom.offset.y();
let visible_line_count = editor.visible_line_count().unwrap_or(0.);
let scroll_top = scroll_bottom - visible_line_count;
editor
.set_scroll_position(vec2f(self.scroll_bottom.offset.x(), scroll_top), cx);
});
}
}
}
fn handle_editor_event(
&mut self,
_: ViewHandle<Editor>,
event: &editor::Event,
cx: &mut ViewContext<Self>,
) {
match event {
editor::Event::ScrollPositionChanged { .. } => self.update_scroll_bottom(cx),
_ => {}
}
}
fn update_scroll_bottom(&mut self, cx: &mut ViewContext<Self>) { fn update_scroll_bottom(&mut self, cx: &mut ViewContext<Self>) {
self.editor.update(cx, |editor, cx| { self.editor.update(cx, |editor, cx| {
let snapshot = editor.snapshot(cx); let snapshot = editor.snapshot(cx);
@ -1128,10 +1127,9 @@ impl AssistantEditor {
let assistant = self.assistant.read(cx); let assistant = self.assistant.read(cx);
if editor.selections.count() == 1 { if editor.selections.count() == 1 {
let selection = editor.selections.newest::<usize>(cx); let selection = editor.selections.newest::<usize>(cx);
let mut offset = 0;
let mut copied_text = String::new(); let mut copied_text = String::new();
let mut spanned_messages = 0; let mut spanned_messages = 0;
for (message, metadata, message_range) in assistant.messages(cx) { for (_message, metadata, message_range) in assistant.messages(cx) {
if message_range.start >= selection.range().end { if message_range.start >= selection.range().end {
break; break;
} else if message_range.end >= selection.range().start { } else if message_range.end >= selection.range().start {
@ -1146,8 +1144,6 @@ impl AssistantEditor {
copied_text.push('\n'); copied_text.push('\n');
} }
} }
offset = message_range.end;
} }
if spanned_messages > 1 { if spanned_messages > 1 {