Set assistant editor's title based on the first question/answer pair

Co-Authored-By: Julia Risley <julia@zed.dev>
This commit is contained in:
Antonio Scandurra 2023-06-06 15:59:10 +02:00
parent 69b8267b6b
commit 9c59146026
3 changed files with 111 additions and 13 deletions

View file

@ -197,7 +197,7 @@
} }
}, },
{ {
"context": "ContextEditor > Editor", "context": "AssistantEditor > Editor",
"bindings": { "bindings": {
"cmd-enter": "assistant::Assist", "cmd-enter": "assistant::Assist",
"cmd->": "assistant::QuoteSelection" "cmd->": "assistant::QuoteSelection"

View file

@ -19,8 +19,8 @@ use gpui::{
use isahc::{http::StatusCode, Request, RequestExt}; use isahc::{http::StatusCode, Request, RequestExt};
use language::{language_settings::SoftWrap, Buffer, LanguageRegistry}; use language::{language_settings::SoftWrap, Buffer, LanguageRegistry};
use settings::SettingsStore; use settings::SettingsStore;
use std::{cell::RefCell, io, rc::Rc, sync::Arc, time::Duration}; use std::{borrow::Cow, cell::RefCell, io, rc::Rc, sync::Arc, time::Duration};
use util::{post_inc, ResultExt, TryFutureExt}; use util::{post_inc, truncate_and_trailoff, ResultExt, TryFutureExt};
use workspace::{ use workspace::{
dock::{DockPosition, Panel}, dock::{DockPosition, Panel},
item::Item, item::Item,
@ -69,7 +69,7 @@ pub struct AssistantPanel {
has_read_credentials: bool, has_read_credentials: bool,
languages: Arc<LanguageRegistry>, languages: Arc<LanguageRegistry>,
fs: Arc<dyn Fs>, fs: Arc<dyn Fs>,
_subscriptions: Vec<Subscription>, subscriptions: Vec<Subscription>,
} }
impl AssistantPanel { impl AssistantPanel {
@ -145,11 +145,11 @@ impl AssistantPanel {
fs: workspace.app_state().fs.clone(), fs: workspace.app_state().fs.clone(),
width: None, width: None,
height: None, height: None,
_subscriptions: Default::default(), subscriptions: Default::default(),
}; };
let mut old_dock_position = this.position(cx); let mut old_dock_position = this.position(cx);
this._subscriptions = vec![ this.subscriptions = vec![
cx.observe(&this.pane, |_, _, cx| cx.notify()), cx.observe(&this.pane, |_, _, cx| cx.notify()),
cx.subscribe(&this.pane, Self::handle_pane_event), cx.subscribe(&this.pane, Self::handle_pane_event),
cx.observe_global::<SettingsStore, _>(move |this, cx| { cx.observe_global::<SettingsStore, _>(move |this, cx| {
@ -186,11 +186,24 @@ impl AssistantPanel {
let focus = self.has_focus(cx); let focus = self.has_focus(cx);
let editor = cx let editor = cx
.add_view(|cx| AssistantEditor::new(self.api_key.clone(), self.languages.clone(), cx)); .add_view(|cx| AssistantEditor::new(self.api_key.clone(), self.languages.clone(), cx));
self.subscriptions
.push(cx.subscribe(&editor, Self::handle_assistant_editor_event));
self.pane.update(cx, |pane, cx| { self.pane.update(cx, |pane, cx| {
pane.add_item(Box::new(editor), true, focus, None, cx) pane.add_item(Box::new(editor), true, focus, None, cx)
}); });
} }
fn handle_assistant_editor_event(
&mut self,
_: ViewHandle<AssistantEditor>,
event: &AssistantEditorEvent,
cx: &mut ViewContext<Self>,
) {
match event {
AssistantEditorEvent::TabContentChanged => self.pane.update(cx, |_, cx| cx.notify()),
}
}
fn save_api_key(&mut self, _: &menu::Confirm, cx: &mut ViewContext<Self>) { fn save_api_key(&mut self, _: &menu::Confirm, cx: &mut ViewContext<Self>) {
if let Some(api_key) = self if let Some(api_key) = self
.api_key_editor .api_key_editor
@ -396,12 +409,15 @@ impl Panel for AssistantPanel {
enum AssistantEvent { enum AssistantEvent {
MessagesEdited { ids: Vec<ExcerptId> }, MessagesEdited { ids: Vec<ExcerptId> },
SummaryChanged,
} }
struct Assistant { struct Assistant {
buffer: ModelHandle<MultiBuffer>, buffer: ModelHandle<MultiBuffer>,
messages: Vec<Message>, messages: Vec<Message>,
messages_by_id: HashMap<ExcerptId, Message>, messages_by_id: HashMap<ExcerptId, Message>,
summary: Option<String>,
pending_summary: Task<Option<()>>,
completion_count: usize, completion_count: usize,
pending_completions: Vec<PendingCompletion>, pending_completions: Vec<PendingCompletion>,
languages: Arc<LanguageRegistry>, languages: Arc<LanguageRegistry>,
@ -428,6 +444,8 @@ impl Assistant {
let mut this = Self { let mut this = Self {
messages: Default::default(), messages: Default::default(),
messages_by_id: Default::default(), messages_by_id: Default::default(),
summary: None,
pending_summary: Task::ready(None),
completion_count: Default::default(), completion_count: Default::default(),
pending_completions: Default::default(), pending_completions: Default::default(),
languages: language_registry, languages: language_registry,
@ -540,9 +558,10 @@ impl Assistant {
} }
} }
this.update(&mut cx, |this, _| { this.update(&mut cx, |this, cx| {
this.pending_completions this.pending_completions
.retain(|completion| completion.id != this.completion_count); .retain(|completion| completion.id != this.completion_count);
this.summarize(cx);
}); });
anyhow::Ok(()) anyhow::Ok(())
@ -634,6 +653,54 @@ impl Assistant {
self.messages_by_id.insert(excerpt_id, message.clone()); self.messages_by_id.insert(excerpt_id, message.clone());
message message
} }
fn summarize(&mut self, cx: &mut ModelContext<Self>) {
if self.messages.len() >= 2 && self.summary.is_none() {
let api_key = self.api_key.borrow().clone();
if let Some(api_key) = api_key {
let messages = self
.messages
.iter()
.take(2)
.map(|message| RequestMessage {
role: message.role,
content: message.content.read(cx).text(),
})
.chain(Some(RequestMessage {
role: Role::User,
content: "Summarize the conversation into a short title without punctuation and with as few characters as possible"
.into(),
}))
.collect();
let request = OpenAIRequest {
model: self.model.clone(),
messages,
stream: true,
};
let stream = stream_completion(api_key, cx.background().clone(), request);
self.pending_summary = cx.spawn(|this, mut cx| {
async move {
let mut messages = stream.await?;
while let Some(message) = messages.next().await {
let mut message = message?;
if let Some(choice) = message.choices.pop() {
let text = choice.delta.content.unwrap_or_default();
this.update(&mut cx, |this, cx| {
this.summary.get_or_insert(String::new()).push_str(&text);
cx.emit(AssistantEvent::SummaryChanged);
});
}
}
anyhow::Ok(())
}
.log_err()
});
}
}
}
} }
struct PendingCompletion { struct PendingCompletion {
@ -641,6 +708,10 @@ struct PendingCompletion {
_task: Task<Option<()>>, _task: Task<Option<()>>,
} }
enum AssistantEditorEvent {
TabContentChanged,
}
struct AssistantEditor { struct AssistantEditor {
assistant: ModelHandle<Assistant>, assistant: ModelHandle<Assistant>,
editor: ViewHandle<Editor>, editor: ViewHandle<Editor>,
@ -712,9 +783,9 @@ impl AssistantEditor {
]; ];
Self { Self {
_subscriptions,
assistant, assistant,
editor, editor,
_subscriptions,
} }
} }
@ -767,6 +838,9 @@ impl AssistantEditor {
assistant.remove_empty_messages(ids, selection_heads, cx) assistant.remove_empty_messages(ids, selection_heads, cx)
}); });
} }
AssistantEvent::SummaryChanged => {
cx.emit(AssistantEditorEvent::TabContentChanged);
}
} }
} }
@ -844,15 +918,23 @@ impl AssistantEditor {
assistant.set_model(new_model.into(), cx); assistant.set_model(new_model.into(), cx);
}); });
} }
fn title(&self, cx: &AppContext) -> String {
self.assistant
.read(cx)
.summary
.clone()
.unwrap_or_else(|| "New Context".into())
}
} }
impl Entity for AssistantEditor { impl Entity for AssistantEditor {
type Event = (); type Event = AssistantEditorEvent;
} }
impl View for AssistantEditor { impl View for AssistantEditor {
fn ui_name() -> &'static str { fn ui_name() -> &'static str {
"ContextEditor" "AssistantEditor"
} }
fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> { fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
@ -914,9 +996,14 @@ impl Item for AssistantEditor {
&self, &self,
_: Option<usize>, _: Option<usize>,
style: &theme::Tab, style: &theme::Tab,
_: &gpui::AppContext, cx: &gpui::AppContext,
) -> AnyElement<V> { ) -> AnyElement<V> {
Label::new("New Context", style.label.clone()).into_any() let title = truncate_and_trailoff(&self.title(cx), editor::MAX_TAB_TITLE_LEN);
Label::new(title, style.label.clone()).into_any()
}
fn tab_tooltip_text(&self, cx: &AppContext) -> Option<Cow<str>> {
Some(self.title(cx).into())
} }
} }
@ -964,9 +1051,19 @@ async fn stream_completion(
while let Some(line) = lines.next().await { while let Some(line) = lines.next().await {
if let Some(event) = parse_line(line).transpose() { if let Some(event) = parse_line(line).transpose() {
let done = event.as_ref().map_or(false, |event| {
event
.choices
.last()
.map_or(false, |choice| choice.finish_reason.is_some())
});
if tx.unbounded_send(event).is_err() { if tx.unbounded_send(event).is_err() {
break; break;
} }
if done {
break;
}
} }
} }

View file

@ -1151,7 +1151,8 @@ impl Pane {
let theme = theme::current(cx).clone(); let theme = theme::current(cx).clone();
let mut tooltip_theme = theme.tooltip.clone(); let mut tooltip_theme = theme.tooltip.clone();
tooltip_theme.max_text_width = None; tooltip_theme.max_text_width = None;
let tab_tooltip_text = item.tab_tooltip_text(cx).map(|a| a.to_string()); let tab_tooltip_text =
item.tab_tooltip_text(cx).map(|text| text.into_owned());
move |mouse_state, cx| { move |mouse_state, cx| {
let tab_style = let tab_style =