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);
});
cx.emit(AssistantEvent::StreamedCompletion);
Some(())
});
@ -745,7 +746,7 @@ impl Assistant {
fn open_ai_request_messages(&self, cx: &AppContext) -> Vec<RequestMessage> {
let buffer = self.buffer.read(cx);
self.messages(cx)
.map(|(message, metadata, range)| RequestMessage {
.map(|(_message, metadata, range)| RequestMessage {
role: metadata.role,
content: buffer.text_for_range(range).collect(),
})
@ -828,7 +829,7 @@ impl AssistantEditor {
cx.subscribe(&editor, Self::handle_editor_event),
];
Self {
let mut this = Self {
assistant,
editor,
blocks: Default::default(),
@ -837,7 +838,9 @@ impl AssistantEditor {
anchor: Anchor::max(),
},
_subscriptions,
}
};
this.update_message_headers(cx);
this
}
fn assist(&mut self, _: &Assist, cx: &mut ViewContext<Self>) {
@ -891,13 +894,48 @@ impl AssistantEditor {
cx: &mut ViewContext<Self>,
) {
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| {
let buffer = editor.buffer().read(cx).snapshot(cx);
let excerpt_id = *buffer.as_singleton().unwrap().0;
let old_blocks = std::mem::take(&mut self.blocks);
let new_blocks =
self.assistant
let new_blocks = self
.assistant
.read(cx)
.messages(cx)
.map(|(message, metadata, _)| BlockProperties {
@ -920,23 +958,19 @@ impl AssistantEditor {
cx,
|state, _| match metadata.role {
Role::User => {
let style =
style.user_sender.style_for(state, false);
let style = style.user_sender.style_for(state, false);
Label::new("You", style.text.clone())
.contained()
.with_style(style.container)
}
Role::Assistant => {
let style = style
.assistant_sender
.style_for(state, false);
let style = style.assistant_sender.style_for(state, false);
Label::new("Assistant", style.text.clone())
.contained()
.with_style(style.container)
}
Role::System => {
let style =
style.system_sender.style_for(state, false);
let style = style.system_sender.style_for(state, false);
Label::new("System", style.text.clone())
.contained()
.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>) {
self.editor.update(cx, |editor, cx| {
let snapshot = editor.snapshot(cx);
@ -1128,10 +1127,9 @@ impl AssistantEditor {
let assistant = self.assistant.read(cx);
if editor.selections.count() == 1 {
let selection = editor.selections.newest::<usize>(cx);
let mut offset = 0;
let mut copied_text = String::new();
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 {
break;
} else if message_range.end >= selection.range().start {
@ -1146,8 +1144,6 @@ impl AssistantEditor {
copied_text.push('\n');
}
}
offset = message_range.end;
}
if spanned_messages > 1 {