diff --git a/crates/ai/src/assistant.rs b/crates/ai/src/assistant.rs index b147bab3b3..c15b46d0e9 100644 --- a/crates/ai/src/assistant.rs +++ b/crates/ai/src/assistant.rs @@ -464,12 +464,18 @@ struct SavedConversationPath { had_summary: bool, } +#[derive(Default)] +struct Summary { + text: String, + done: bool, +} + struct Assistant { buffer: ModelHandle, message_anchors: Vec, messages_metadata: HashMap, next_message_id: MessageId, - summary: Option, + summary: Option, pending_summary: Task>, completion_count: usize, pending_completions: Vec, @@ -954,12 +960,22 @@ impl Assistant { 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); + this.summary + .get_or_insert(Default::default()) + .text + .push_str(&text); cx.emit(AssistantEvent::SummaryChanged); }); } } + this.update(&mut cx, |this, cx| { + if let Some(summary) = this.summary.as_mut() { + summary.done = true; + cx.emit(AssistantEvent::SummaryChanged); + } + }); + anyhow::Ok(()) } .log_err() @@ -1056,8 +1072,19 @@ impl Assistant { }), }; - let (old_path, summary) = - this.read_with(&cx, |this, _| (this.path.clone(), this.summary.clone())); + let (old_path, summary) = this.read_with(&cx, |this, _| { + let path = this.path.clone(); + let summary = if let Some(summary) = this.summary.as_ref() { + if summary.done { + Some(summary.text.clone()) + } else { + None + } + } else { + None + }; + (path, summary) + }); let mut new_path = None; if let Some(old_path) = old_path.as_ref() { if old_path.had_summary || summary.is_none() { @@ -1555,7 +1582,8 @@ impl AssistantEditor { self.assistant .read(cx) .summary - .clone() + .as_ref() + .map(|summary| summary.text.clone()) .unwrap_or_else(|| "New Context".into()) } }