1#[cfg(test)]
2mod context_tests;
3
4use crate::patch::{AssistantEdit, AssistantPatch, AssistantPatchStatus};
5use anyhow::{Context as _, Result, anyhow};
6use assistant_slash_command::{
7 SlashCommandContent, SlashCommandEvent, SlashCommandLine, SlashCommandOutputSection,
8 SlashCommandResult, SlashCommandWorkingSet,
9};
10use assistant_slash_commands::FileCommandMetadata;
11use client::{self, proto, telemetry::Telemetry};
12use clock::ReplicaId;
13use collections::{HashMap, HashSet};
14use fs::{Fs, RemoveOptions};
15use futures::{FutureExt, StreamExt, future::Shared};
16use gpui::{
17 App, AppContext as _, Context, Entity, EventEmitter, RenderImage, SharedString, Subscription,
18 Task,
19};
20use language::{AnchorRangeExt, Bias, Buffer, LanguageRegistry, OffsetRangeExt, Point, ToOffset};
21use language_model::{
22 LanguageModel, LanguageModelCacheConfiguration, LanguageModelCompletionEvent,
23 LanguageModelImage, LanguageModelRegistry, LanguageModelRequest, LanguageModelRequestMessage,
24 LanguageModelToolUseId, MaxMonthlySpendReachedError, MessageContent, PaymentRequiredError,
25 Role, StopReason, report_assistant_event,
26};
27use open_ai::Model as OpenAiModel;
28use paths::contexts_dir;
29use project::Project;
30use prompt_store::PromptBuilder;
31use serde::{Deserialize, Serialize};
32use smallvec::SmallVec;
33use std::{
34 cmp::{Ordering, max},
35 fmt::Debug,
36 iter, mem,
37 ops::Range,
38 path::{Path, PathBuf},
39 str::FromStr as _,
40 sync::Arc,
41 time::{Duration, Instant},
42};
43use telemetry_events::{AssistantEventData, AssistantKind, AssistantPhase};
44use text::{BufferSnapshot, ToPoint};
45use ui::IconName;
46use util::{ResultExt, TryFutureExt, post_inc};
47use uuid::Uuid;
48
49#[derive(Clone, Eq, PartialEq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
50pub struct ContextId(String);
51
52impl ContextId {
53 pub fn new() -> Self {
54 Self(Uuid::new_v4().to_string())
55 }
56
57 pub fn from_proto(id: String) -> Self {
58 Self(id)
59 }
60
61 pub fn to_proto(&self) -> String {
62 self.0.clone()
63 }
64}
65
66#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
67pub struct MessageId(pub clock::Lamport);
68
69impl MessageId {
70 pub fn as_u64(self) -> u64 {
71 self.0.as_u64()
72 }
73}
74
75#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
76pub enum MessageStatus {
77 Pending,
78 Done,
79 Error(SharedString),
80 Canceled,
81}
82
83impl MessageStatus {
84 pub fn from_proto(status: proto::ContextMessageStatus) -> MessageStatus {
85 match status.variant {
86 Some(proto::context_message_status::Variant::Pending(_)) => MessageStatus::Pending,
87 Some(proto::context_message_status::Variant::Done(_)) => MessageStatus::Done,
88 Some(proto::context_message_status::Variant::Error(error)) => {
89 MessageStatus::Error(error.message.into())
90 }
91 Some(proto::context_message_status::Variant::Canceled(_)) => MessageStatus::Canceled,
92 None => MessageStatus::Pending,
93 }
94 }
95
96 pub fn to_proto(&self) -> proto::ContextMessageStatus {
97 match self {
98 MessageStatus::Pending => proto::ContextMessageStatus {
99 variant: Some(proto::context_message_status::Variant::Pending(
100 proto::context_message_status::Pending {},
101 )),
102 },
103 MessageStatus::Done => proto::ContextMessageStatus {
104 variant: Some(proto::context_message_status::Variant::Done(
105 proto::context_message_status::Done {},
106 )),
107 },
108 MessageStatus::Error(message) => proto::ContextMessageStatus {
109 variant: Some(proto::context_message_status::Variant::Error(
110 proto::context_message_status::Error {
111 message: message.to_string(),
112 },
113 )),
114 },
115 MessageStatus::Canceled => proto::ContextMessageStatus {
116 variant: Some(proto::context_message_status::Variant::Canceled(
117 proto::context_message_status::Canceled {},
118 )),
119 },
120 }
121 }
122}
123
124#[derive(Clone, Copy, Debug, PartialEq, Eq)]
125pub enum RequestType {
126 /// Request a normal chat response from the model.
127 Chat,
128 /// Add a preamble to the message, which tells the model to return a structured response that suggests edits.
129 SuggestEdits,
130}
131
132#[derive(Clone, Debug)]
133pub enum ContextOperation {
134 InsertMessage {
135 anchor: MessageAnchor,
136 metadata: MessageMetadata,
137 version: clock::Global,
138 },
139 UpdateMessage {
140 message_id: MessageId,
141 metadata: MessageMetadata,
142 version: clock::Global,
143 },
144 UpdateSummary {
145 summary: ContextSummary,
146 version: clock::Global,
147 },
148 SlashCommandStarted {
149 id: InvokedSlashCommandId,
150 output_range: Range<language::Anchor>,
151 name: String,
152 version: clock::Global,
153 },
154 SlashCommandFinished {
155 id: InvokedSlashCommandId,
156 timestamp: clock::Lamport,
157 error_message: Option<String>,
158 version: clock::Global,
159 },
160 SlashCommandOutputSectionAdded {
161 timestamp: clock::Lamport,
162 section: SlashCommandOutputSection<language::Anchor>,
163 version: clock::Global,
164 },
165 ThoughtProcessOutputSectionAdded {
166 timestamp: clock::Lamport,
167 section: ThoughtProcessOutputSection<language::Anchor>,
168 version: clock::Global,
169 },
170 BufferOperation(language::Operation),
171}
172
173impl ContextOperation {
174 pub fn from_proto(op: proto::ContextOperation) -> Result<Self> {
175 match op.variant.context("invalid variant")? {
176 proto::context_operation::Variant::InsertMessage(insert) => {
177 let message = insert.message.context("invalid message")?;
178 let id = MessageId(language::proto::deserialize_timestamp(
179 message.id.context("invalid id")?,
180 ));
181 Ok(Self::InsertMessage {
182 anchor: MessageAnchor {
183 id,
184 start: language::proto::deserialize_anchor(
185 message.start.context("invalid anchor")?,
186 )
187 .context("invalid anchor")?,
188 },
189 metadata: MessageMetadata {
190 role: Role::from_proto(message.role),
191 status: MessageStatus::from_proto(
192 message.status.context("invalid status")?,
193 ),
194 timestamp: id.0,
195 cache: None,
196 },
197 version: language::proto::deserialize_version(&insert.version),
198 })
199 }
200 proto::context_operation::Variant::UpdateMessage(update) => Ok(Self::UpdateMessage {
201 message_id: MessageId(language::proto::deserialize_timestamp(
202 update.message_id.context("invalid message id")?,
203 )),
204 metadata: MessageMetadata {
205 role: Role::from_proto(update.role),
206 status: MessageStatus::from_proto(update.status.context("invalid status")?),
207 timestamp: language::proto::deserialize_timestamp(
208 update.timestamp.context("invalid timestamp")?,
209 ),
210 cache: None,
211 },
212 version: language::proto::deserialize_version(&update.version),
213 }),
214 proto::context_operation::Variant::UpdateSummary(update) => Ok(Self::UpdateSummary {
215 summary: ContextSummary {
216 text: update.summary,
217 done: update.done,
218 timestamp: language::proto::deserialize_timestamp(
219 update.timestamp.context("invalid timestamp")?,
220 ),
221 },
222 version: language::proto::deserialize_version(&update.version),
223 }),
224 proto::context_operation::Variant::SlashCommandStarted(message) => {
225 Ok(Self::SlashCommandStarted {
226 id: InvokedSlashCommandId(language::proto::deserialize_timestamp(
227 message.id.context("invalid id")?,
228 )),
229 output_range: language::proto::deserialize_anchor_range(
230 message.output_range.context("invalid range")?,
231 )?,
232 name: message.name,
233 version: language::proto::deserialize_version(&message.version),
234 })
235 }
236 proto::context_operation::Variant::SlashCommandOutputSectionAdded(message) => {
237 let section = message.section.context("missing section")?;
238 Ok(Self::SlashCommandOutputSectionAdded {
239 timestamp: language::proto::deserialize_timestamp(
240 message.timestamp.context("missing timestamp")?,
241 ),
242 section: SlashCommandOutputSection {
243 range: language::proto::deserialize_anchor_range(
244 section.range.context("invalid range")?,
245 )?,
246 icon: section.icon_name.parse()?,
247 label: section.label.into(),
248 metadata: section
249 .metadata
250 .and_then(|metadata| serde_json::from_str(&metadata).log_err()),
251 },
252 version: language::proto::deserialize_version(&message.version),
253 })
254 }
255 proto::context_operation::Variant::SlashCommandCompleted(message) => {
256 Ok(Self::SlashCommandFinished {
257 id: InvokedSlashCommandId(language::proto::deserialize_timestamp(
258 message.id.context("invalid id")?,
259 )),
260 timestamp: language::proto::deserialize_timestamp(
261 message.timestamp.context("missing timestamp")?,
262 ),
263 error_message: message.error_message,
264 version: language::proto::deserialize_version(&message.version),
265 })
266 }
267 proto::context_operation::Variant::ThoughtProcessOutputSectionAdded(message) => {
268 let section = message.section.context("missing section")?;
269 Ok(Self::ThoughtProcessOutputSectionAdded {
270 timestamp: language::proto::deserialize_timestamp(
271 message.timestamp.context("missing timestamp")?,
272 ),
273 section: ThoughtProcessOutputSection {
274 range: language::proto::deserialize_anchor_range(
275 section.range.context("invalid range")?,
276 )?,
277 },
278 version: language::proto::deserialize_version(&message.version),
279 })
280 }
281 proto::context_operation::Variant::BufferOperation(op) => Ok(Self::BufferOperation(
282 language::proto::deserialize_operation(
283 op.operation.context("invalid buffer operation")?,
284 )?,
285 )),
286 }
287 }
288
289 pub fn to_proto(&self) -> proto::ContextOperation {
290 match self {
291 Self::InsertMessage {
292 anchor,
293 metadata,
294 version,
295 } => proto::ContextOperation {
296 variant: Some(proto::context_operation::Variant::InsertMessage(
297 proto::context_operation::InsertMessage {
298 message: Some(proto::ContextMessage {
299 id: Some(language::proto::serialize_timestamp(anchor.id.0)),
300 start: Some(language::proto::serialize_anchor(&anchor.start)),
301 role: metadata.role.to_proto() as i32,
302 status: Some(metadata.status.to_proto()),
303 }),
304 version: language::proto::serialize_version(version),
305 },
306 )),
307 },
308 Self::UpdateMessage {
309 message_id,
310 metadata,
311 version,
312 } => proto::ContextOperation {
313 variant: Some(proto::context_operation::Variant::UpdateMessage(
314 proto::context_operation::UpdateMessage {
315 message_id: Some(language::proto::serialize_timestamp(message_id.0)),
316 role: metadata.role.to_proto() as i32,
317 status: Some(metadata.status.to_proto()),
318 timestamp: Some(language::proto::serialize_timestamp(metadata.timestamp)),
319 version: language::proto::serialize_version(version),
320 },
321 )),
322 },
323 Self::UpdateSummary { summary, version } => proto::ContextOperation {
324 variant: Some(proto::context_operation::Variant::UpdateSummary(
325 proto::context_operation::UpdateSummary {
326 summary: summary.text.clone(),
327 done: summary.done,
328 timestamp: Some(language::proto::serialize_timestamp(summary.timestamp)),
329 version: language::proto::serialize_version(version),
330 },
331 )),
332 },
333 Self::SlashCommandStarted {
334 id,
335 output_range,
336 name,
337 version,
338 } => proto::ContextOperation {
339 variant: Some(proto::context_operation::Variant::SlashCommandStarted(
340 proto::context_operation::SlashCommandStarted {
341 id: Some(language::proto::serialize_timestamp(id.0)),
342 output_range: Some(language::proto::serialize_anchor_range(
343 output_range.clone(),
344 )),
345 name: name.clone(),
346 version: language::proto::serialize_version(version),
347 },
348 )),
349 },
350 Self::SlashCommandOutputSectionAdded {
351 timestamp,
352 section,
353 version,
354 } => proto::ContextOperation {
355 variant: Some(
356 proto::context_operation::Variant::SlashCommandOutputSectionAdded(
357 proto::context_operation::SlashCommandOutputSectionAdded {
358 timestamp: Some(language::proto::serialize_timestamp(*timestamp)),
359 section: Some({
360 let icon_name: &'static str = section.icon.into();
361 proto::SlashCommandOutputSection {
362 range: Some(language::proto::serialize_anchor_range(
363 section.range.clone(),
364 )),
365 icon_name: icon_name.to_string(),
366 label: section.label.to_string(),
367 metadata: section.metadata.as_ref().and_then(|metadata| {
368 serde_json::to_string(metadata).log_err()
369 }),
370 }
371 }),
372 version: language::proto::serialize_version(version),
373 },
374 ),
375 ),
376 },
377 Self::SlashCommandFinished {
378 id,
379 timestamp,
380 error_message,
381 version,
382 } => proto::ContextOperation {
383 variant: Some(proto::context_operation::Variant::SlashCommandCompleted(
384 proto::context_operation::SlashCommandCompleted {
385 id: Some(language::proto::serialize_timestamp(id.0)),
386 timestamp: Some(language::proto::serialize_timestamp(*timestamp)),
387 error_message: error_message.clone(),
388 version: language::proto::serialize_version(version),
389 },
390 )),
391 },
392 Self::ThoughtProcessOutputSectionAdded {
393 timestamp,
394 section,
395 version,
396 } => proto::ContextOperation {
397 variant: Some(
398 proto::context_operation::Variant::ThoughtProcessOutputSectionAdded(
399 proto::context_operation::ThoughtProcessOutputSectionAdded {
400 timestamp: Some(language::proto::serialize_timestamp(*timestamp)),
401 section: Some({
402 proto::ThoughtProcessOutputSection {
403 range: Some(language::proto::serialize_anchor_range(
404 section.range.clone(),
405 )),
406 }
407 }),
408 version: language::proto::serialize_version(version),
409 },
410 ),
411 ),
412 },
413 Self::BufferOperation(operation) => proto::ContextOperation {
414 variant: Some(proto::context_operation::Variant::BufferOperation(
415 proto::context_operation::BufferOperation {
416 operation: Some(language::proto::serialize_operation(operation)),
417 },
418 )),
419 },
420 }
421 }
422
423 fn timestamp(&self) -> clock::Lamport {
424 match self {
425 Self::InsertMessage { anchor, .. } => anchor.id.0,
426 Self::UpdateMessage { metadata, .. } => metadata.timestamp,
427 Self::UpdateSummary { summary, .. } => summary.timestamp,
428 Self::SlashCommandStarted { id, .. } => id.0,
429 Self::SlashCommandOutputSectionAdded { timestamp, .. }
430 | Self::SlashCommandFinished { timestamp, .. }
431 | Self::ThoughtProcessOutputSectionAdded { timestamp, .. } => *timestamp,
432 Self::BufferOperation(_) => {
433 panic!("reading the timestamp of a buffer operation is not supported")
434 }
435 }
436 }
437
438 /// Returns the current version of the context operation.
439 pub fn version(&self) -> &clock::Global {
440 match self {
441 Self::InsertMessage { version, .. }
442 | Self::UpdateMessage { version, .. }
443 | Self::UpdateSummary { version, .. }
444 | Self::SlashCommandStarted { version, .. }
445 | Self::SlashCommandOutputSectionAdded { version, .. }
446 | Self::SlashCommandFinished { version, .. }
447 | Self::ThoughtProcessOutputSectionAdded { version, .. } => version,
448 Self::BufferOperation(_) => {
449 panic!("reading the version of a buffer operation is not supported")
450 }
451 }
452 }
453}
454
455#[derive(Debug, Clone)]
456pub enum ContextEvent {
457 ShowAssistError(SharedString),
458 ShowPaymentRequiredError,
459 ShowMaxMonthlySpendReachedError,
460 MessagesEdited,
461 SummaryChanged,
462 SummaryGenerated,
463 StreamedCompletion,
464 StartedThoughtProcess(Range<language::Anchor>),
465 EndedThoughtProcess(language::Anchor),
466 PatchesUpdated {
467 removed: Vec<Range<language::Anchor>>,
468 updated: Vec<Range<language::Anchor>>,
469 },
470 InvokedSlashCommandChanged {
471 command_id: InvokedSlashCommandId,
472 },
473 ParsedSlashCommandsUpdated {
474 removed: Vec<Range<language::Anchor>>,
475 updated: Vec<ParsedSlashCommand>,
476 },
477 SlashCommandOutputSectionAdded {
478 section: SlashCommandOutputSection<language::Anchor>,
479 },
480 Operation(ContextOperation),
481}
482
483#[derive(Clone, Default, Debug)]
484pub struct ContextSummary {
485 pub text: String,
486 pub done: bool,
487 timestamp: clock::Lamport,
488}
489
490#[derive(Clone, Debug, Eq, PartialEq)]
491pub struct MessageAnchor {
492 pub id: MessageId,
493 pub start: language::Anchor,
494}
495
496#[derive(Clone, Debug, Eq, PartialEq)]
497pub enum CacheStatus {
498 Pending,
499 Cached,
500}
501
502#[derive(Clone, Debug, Eq, PartialEq)]
503pub struct MessageCacheMetadata {
504 pub is_anchor: bool,
505 pub is_final_anchor: bool,
506 pub status: CacheStatus,
507 pub cached_at: clock::Global,
508}
509
510#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
511pub struct MessageMetadata {
512 pub role: Role,
513 pub status: MessageStatus,
514 pub timestamp: clock::Lamport,
515 #[serde(skip)]
516 pub cache: Option<MessageCacheMetadata>,
517}
518
519impl From<&Message> for MessageMetadata {
520 fn from(message: &Message) -> Self {
521 Self {
522 role: message.role,
523 status: message.status.clone(),
524 timestamp: message.id.0,
525 cache: message.cache.clone(),
526 }
527 }
528}
529
530impl MessageMetadata {
531 pub fn is_cache_valid(&self, buffer: &BufferSnapshot, range: &Range<usize>) -> bool {
532 let result = match &self.cache {
533 Some(MessageCacheMetadata { cached_at, .. }) => !buffer.has_edits_since_in_range(
534 &cached_at,
535 Range {
536 start: buffer.anchor_at(range.start, Bias::Right),
537 end: buffer.anchor_at(range.end, Bias::Left),
538 },
539 ),
540 _ => false,
541 };
542 result
543 }
544}
545
546#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
547pub struct ThoughtProcessOutputSection<T> {
548 pub range: Range<T>,
549}
550
551impl ThoughtProcessOutputSection<language::Anchor> {
552 pub fn is_valid(&self, buffer: &language::TextBuffer) -> bool {
553 self.range.start.is_valid(buffer) && !self.range.to_offset(buffer).is_empty()
554 }
555}
556
557#[derive(Clone, Debug)]
558pub struct Message {
559 pub offset_range: Range<usize>,
560 pub index_range: Range<usize>,
561 pub anchor_range: Range<language::Anchor>,
562 pub id: MessageId,
563 pub role: Role,
564 pub status: MessageStatus,
565 pub cache: Option<MessageCacheMetadata>,
566}
567
568#[derive(Debug, Clone)]
569pub enum Content {
570 Image {
571 anchor: language::Anchor,
572 image_id: u64,
573 render_image: Arc<RenderImage>,
574 image: Shared<Task<Option<LanguageModelImage>>>,
575 },
576}
577
578impl Content {
579 fn range(&self) -> Range<language::Anchor> {
580 match self {
581 Self::Image { anchor, .. } => *anchor..*anchor,
582 }
583 }
584
585 fn cmp(&self, other: &Self, buffer: &BufferSnapshot) -> Ordering {
586 let self_range = self.range();
587 let other_range = other.range();
588 if self_range.end.cmp(&other_range.start, buffer).is_lt() {
589 Ordering::Less
590 } else if self_range.start.cmp(&other_range.end, buffer).is_gt() {
591 Ordering::Greater
592 } else {
593 Ordering::Equal
594 }
595 }
596}
597
598struct PendingCompletion {
599 id: usize,
600 assistant_message_id: MessageId,
601 _task: Task<()>,
602}
603
604#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
605pub struct InvokedSlashCommandId(clock::Lamport);
606
607#[derive(Clone, Debug)]
608pub struct XmlTag {
609 pub kind: XmlTagKind,
610 pub range: Range<text::Anchor>,
611 pub is_open_tag: bool,
612}
613
614#[derive(Copy, Clone, Debug, strum::EnumString, PartialEq, Eq, strum::AsRefStr)]
615#[strum(serialize_all = "snake_case")]
616pub enum XmlTagKind {
617 Patch,
618 Title,
619 Edit,
620 Path,
621 Description,
622 OldText,
623 NewText,
624 Operation,
625}
626
627pub struct AssistantContext {
628 id: ContextId,
629 timestamp: clock::Lamport,
630 version: clock::Global,
631 pending_ops: Vec<ContextOperation>,
632 operations: Vec<ContextOperation>,
633 buffer: Entity<Buffer>,
634 parsed_slash_commands: Vec<ParsedSlashCommand>,
635 invoked_slash_commands: HashMap<InvokedSlashCommandId, InvokedSlashCommand>,
636 edits_since_last_parse: language::Subscription,
637 slash_commands: Arc<SlashCommandWorkingSet>,
638 slash_command_output_sections: Vec<SlashCommandOutputSection<language::Anchor>>,
639 thought_process_output_sections: Vec<ThoughtProcessOutputSection<language::Anchor>>,
640 message_anchors: Vec<MessageAnchor>,
641 contents: Vec<Content>,
642 messages_metadata: HashMap<MessageId, MessageMetadata>,
643 summary: Option<ContextSummary>,
644 summary_task: Task<Option<()>>,
645 completion_count: usize,
646 pending_completions: Vec<PendingCompletion>,
647 token_count: Option<usize>,
648 pending_token_count: Task<Option<()>>,
649 pending_save: Task<Result<()>>,
650 pending_cache_warming_task: Task<Option<()>>,
651 path: Option<PathBuf>,
652 _subscriptions: Vec<Subscription>,
653 telemetry: Option<Arc<Telemetry>>,
654 language_registry: Arc<LanguageRegistry>,
655 patches: Vec<AssistantPatch>,
656 xml_tags: Vec<XmlTag>,
657 project: Option<Entity<Project>>,
658 prompt_builder: Arc<PromptBuilder>,
659}
660
661trait ContextAnnotation {
662 fn range(&self) -> &Range<language::Anchor>;
663}
664
665impl ContextAnnotation for ParsedSlashCommand {
666 fn range(&self) -> &Range<language::Anchor> {
667 &self.source_range
668 }
669}
670
671impl ContextAnnotation for AssistantPatch {
672 fn range(&self) -> &Range<language::Anchor> {
673 &self.range
674 }
675}
676
677impl ContextAnnotation for XmlTag {
678 fn range(&self) -> &Range<language::Anchor> {
679 &self.range
680 }
681}
682
683impl EventEmitter<ContextEvent> for AssistantContext {}
684
685impl AssistantContext {
686 pub fn local(
687 language_registry: Arc<LanguageRegistry>,
688 project: Option<Entity<Project>>,
689 telemetry: Option<Arc<Telemetry>>,
690 prompt_builder: Arc<PromptBuilder>,
691 slash_commands: Arc<SlashCommandWorkingSet>,
692 cx: &mut Context<Self>,
693 ) -> Self {
694 Self::new(
695 ContextId::new(),
696 ReplicaId::default(),
697 language::Capability::ReadWrite,
698 language_registry,
699 prompt_builder,
700 slash_commands,
701 project,
702 telemetry,
703 cx,
704 )
705 }
706
707 pub fn new(
708 id: ContextId,
709 replica_id: ReplicaId,
710 capability: language::Capability,
711 language_registry: Arc<LanguageRegistry>,
712 prompt_builder: Arc<PromptBuilder>,
713 slash_commands: Arc<SlashCommandWorkingSet>,
714 project: Option<Entity<Project>>,
715 telemetry: Option<Arc<Telemetry>>,
716 cx: &mut Context<Self>,
717 ) -> Self {
718 let buffer = cx.new(|_cx| {
719 let buffer = Buffer::remote(
720 language::BufferId::new(1).unwrap(),
721 replica_id,
722 capability,
723 "",
724 );
725 buffer.set_language_registry(language_registry.clone());
726 buffer
727 });
728 let edits_since_last_slash_command_parse =
729 buffer.update(cx, |buffer, _| buffer.subscribe());
730 let mut this = Self {
731 id,
732 timestamp: clock::Lamport::new(replica_id),
733 version: clock::Global::new(),
734 pending_ops: Vec::new(),
735 operations: Vec::new(),
736 message_anchors: Default::default(),
737 contents: Default::default(),
738 messages_metadata: Default::default(),
739 parsed_slash_commands: Vec::new(),
740 invoked_slash_commands: HashMap::default(),
741 slash_command_output_sections: Vec::new(),
742 thought_process_output_sections: Vec::new(),
743 edits_since_last_parse: edits_since_last_slash_command_parse,
744 summary: None,
745 summary_task: Task::ready(None),
746 completion_count: Default::default(),
747 pending_completions: Default::default(),
748 token_count: None,
749 pending_token_count: Task::ready(None),
750 pending_cache_warming_task: Task::ready(None),
751 _subscriptions: vec![cx.subscribe(&buffer, Self::handle_buffer_event)],
752 pending_save: Task::ready(Ok(())),
753 path: None,
754 buffer,
755 telemetry,
756 project,
757 language_registry,
758 slash_commands,
759 patches: Vec::new(),
760 xml_tags: Vec::new(),
761 prompt_builder,
762 };
763
764 let first_message_id = MessageId(clock::Lamport {
765 replica_id: 0,
766 value: 0,
767 });
768 let message = MessageAnchor {
769 id: first_message_id,
770 start: language::Anchor::MIN,
771 };
772 this.messages_metadata.insert(
773 first_message_id,
774 MessageMetadata {
775 role: Role::User,
776 status: MessageStatus::Done,
777 timestamp: first_message_id.0,
778 cache: None,
779 },
780 );
781 this.message_anchors.push(message);
782
783 this.set_language(cx);
784 this.count_remaining_tokens(cx);
785 this
786 }
787
788 pub(crate) fn serialize(&self, cx: &App) -> SavedContext {
789 let buffer = self.buffer.read(cx);
790 SavedContext {
791 id: Some(self.id.clone()),
792 zed: "context".into(),
793 version: SavedContext::VERSION.into(),
794 text: buffer.text(),
795 messages: self
796 .messages(cx)
797 .map(|message| SavedMessage {
798 id: message.id,
799 start: message.offset_range.start,
800 metadata: self.messages_metadata[&message.id].clone(),
801 })
802 .collect(),
803 summary: self
804 .summary
805 .as_ref()
806 .map(|summary| summary.text.clone())
807 .unwrap_or_default(),
808 slash_command_output_sections: self
809 .slash_command_output_sections
810 .iter()
811 .filter_map(|section| {
812 if section.is_valid(buffer) {
813 let range = section.range.to_offset(buffer);
814 Some(assistant_slash_command::SlashCommandOutputSection {
815 range,
816 icon: section.icon,
817 label: section.label.clone(),
818 metadata: section.metadata.clone(),
819 })
820 } else {
821 None
822 }
823 })
824 .collect(),
825 thought_process_output_sections: self
826 .thought_process_output_sections
827 .iter()
828 .filter_map(|section| {
829 if section.is_valid(buffer) {
830 let range = section.range.to_offset(buffer);
831 Some(ThoughtProcessOutputSection { range })
832 } else {
833 None
834 }
835 })
836 .collect(),
837 }
838 }
839
840 pub fn deserialize(
841 saved_context: SavedContext,
842 path: PathBuf,
843 language_registry: Arc<LanguageRegistry>,
844 prompt_builder: Arc<PromptBuilder>,
845 slash_commands: Arc<SlashCommandWorkingSet>,
846 project: Option<Entity<Project>>,
847 telemetry: Option<Arc<Telemetry>>,
848 cx: &mut Context<Self>,
849 ) -> Self {
850 let id = saved_context.id.clone().unwrap_or_else(ContextId::new);
851 let mut this = Self::new(
852 id,
853 ReplicaId::default(),
854 language::Capability::ReadWrite,
855 language_registry,
856 prompt_builder,
857 slash_commands,
858 project,
859 telemetry,
860 cx,
861 );
862 this.path = Some(path);
863 this.buffer.update(cx, |buffer, cx| {
864 buffer.set_text(saved_context.text.as_str(), cx)
865 });
866 let operations = saved_context.into_ops(&this.buffer, cx);
867 this.apply_ops(operations, cx);
868 this
869 }
870
871 pub fn id(&self) -> &ContextId {
872 &self.id
873 }
874
875 pub fn replica_id(&self) -> ReplicaId {
876 self.timestamp.replica_id
877 }
878
879 pub fn version(&self, cx: &App) -> ContextVersion {
880 ContextVersion {
881 context: self.version.clone(),
882 buffer: self.buffer.read(cx).version(),
883 }
884 }
885
886 pub fn slash_commands(&self) -> &Arc<SlashCommandWorkingSet> {
887 &self.slash_commands
888 }
889
890 pub fn set_capability(&mut self, capability: language::Capability, cx: &mut Context<Self>) {
891 self.buffer
892 .update(cx, |buffer, cx| buffer.set_capability(capability, cx));
893 }
894
895 fn next_timestamp(&mut self) -> clock::Lamport {
896 let timestamp = self.timestamp.tick();
897 self.version.observe(timestamp);
898 timestamp
899 }
900
901 pub fn serialize_ops(
902 &self,
903 since: &ContextVersion,
904 cx: &App,
905 ) -> Task<Vec<proto::ContextOperation>> {
906 let buffer_ops = self
907 .buffer
908 .read(cx)
909 .serialize_ops(Some(since.buffer.clone()), cx);
910
911 let mut context_ops = self
912 .operations
913 .iter()
914 .filter(|op| !since.context.observed(op.timestamp()))
915 .cloned()
916 .collect::<Vec<_>>();
917 context_ops.extend(self.pending_ops.iter().cloned());
918
919 cx.background_spawn(async move {
920 let buffer_ops = buffer_ops.await;
921 context_ops.sort_unstable_by_key(|op| op.timestamp());
922 buffer_ops
923 .into_iter()
924 .map(|op| proto::ContextOperation {
925 variant: Some(proto::context_operation::Variant::BufferOperation(
926 proto::context_operation::BufferOperation {
927 operation: Some(op),
928 },
929 )),
930 })
931 .chain(context_ops.into_iter().map(|op| op.to_proto()))
932 .collect()
933 })
934 }
935
936 pub fn apply_ops(
937 &mut self,
938 ops: impl IntoIterator<Item = ContextOperation>,
939 cx: &mut Context<Self>,
940 ) {
941 let mut buffer_ops = Vec::new();
942 for op in ops {
943 match op {
944 ContextOperation::BufferOperation(buffer_op) => buffer_ops.push(buffer_op),
945 op @ _ => self.pending_ops.push(op),
946 }
947 }
948 self.buffer
949 .update(cx, |buffer, cx| buffer.apply_ops(buffer_ops, cx));
950 self.flush_ops(cx);
951 }
952
953 fn flush_ops(&mut self, cx: &mut Context<AssistantContext>) {
954 let mut changed_messages = HashSet::default();
955 let mut summary_generated = false;
956
957 self.pending_ops.sort_unstable_by_key(|op| op.timestamp());
958 for op in mem::take(&mut self.pending_ops) {
959 if !self.can_apply_op(&op, cx) {
960 self.pending_ops.push(op);
961 continue;
962 }
963
964 let timestamp = op.timestamp();
965 match op.clone() {
966 ContextOperation::InsertMessage {
967 anchor, metadata, ..
968 } => {
969 if self.messages_metadata.contains_key(&anchor.id) {
970 // We already applied this operation.
971 } else {
972 changed_messages.insert(anchor.id);
973 self.insert_message(anchor, metadata, cx);
974 }
975 }
976 ContextOperation::UpdateMessage {
977 message_id,
978 metadata: new_metadata,
979 ..
980 } => {
981 let metadata = self.messages_metadata.get_mut(&message_id).unwrap();
982 if new_metadata.timestamp > metadata.timestamp {
983 *metadata = new_metadata;
984 changed_messages.insert(message_id);
985 }
986 }
987 ContextOperation::UpdateSummary {
988 summary: new_summary,
989 ..
990 } => {
991 if self
992 .summary
993 .as_ref()
994 .map_or(true, |summary| new_summary.timestamp > summary.timestamp)
995 {
996 self.summary = Some(new_summary);
997 summary_generated = true;
998 }
999 }
1000 ContextOperation::SlashCommandStarted {
1001 id,
1002 output_range,
1003 name,
1004 ..
1005 } => {
1006 self.invoked_slash_commands.insert(
1007 id,
1008 InvokedSlashCommand {
1009 name: name.into(),
1010 range: output_range,
1011 run_commands_in_ranges: Vec::new(),
1012 status: InvokedSlashCommandStatus::Running(Task::ready(())),
1013 transaction: None,
1014 timestamp: id.0,
1015 },
1016 );
1017 cx.emit(ContextEvent::InvokedSlashCommandChanged { command_id: id });
1018 }
1019 ContextOperation::SlashCommandOutputSectionAdded { section, .. } => {
1020 let buffer = self.buffer.read(cx);
1021 if let Err(ix) = self
1022 .slash_command_output_sections
1023 .binary_search_by(|probe| probe.range.cmp(§ion.range, buffer))
1024 {
1025 self.slash_command_output_sections
1026 .insert(ix, section.clone());
1027 cx.emit(ContextEvent::SlashCommandOutputSectionAdded { section });
1028 }
1029 }
1030 ContextOperation::ThoughtProcessOutputSectionAdded { section, .. } => {
1031 let buffer = self.buffer.read(cx);
1032 if let Err(ix) = self
1033 .thought_process_output_sections
1034 .binary_search_by(|probe| probe.range.cmp(§ion.range, buffer))
1035 {
1036 self.thought_process_output_sections
1037 .insert(ix, section.clone());
1038 }
1039 }
1040 ContextOperation::SlashCommandFinished {
1041 id,
1042 error_message,
1043 timestamp,
1044 ..
1045 } => {
1046 if let Some(slash_command) = self.invoked_slash_commands.get_mut(&id) {
1047 if timestamp > slash_command.timestamp {
1048 slash_command.timestamp = timestamp;
1049 match error_message {
1050 Some(message) => {
1051 slash_command.status =
1052 InvokedSlashCommandStatus::Error(message.into());
1053 }
1054 None => {
1055 slash_command.status = InvokedSlashCommandStatus::Finished;
1056 }
1057 }
1058 cx.emit(ContextEvent::InvokedSlashCommandChanged { command_id: id });
1059 }
1060 }
1061 }
1062 ContextOperation::BufferOperation(_) => unreachable!(),
1063 }
1064
1065 self.version.observe(timestamp);
1066 self.timestamp.observe(timestamp);
1067 self.operations.push(op);
1068 }
1069
1070 if !changed_messages.is_empty() {
1071 self.message_roles_updated(changed_messages, cx);
1072 cx.emit(ContextEvent::MessagesEdited);
1073 cx.notify();
1074 }
1075
1076 if summary_generated {
1077 cx.emit(ContextEvent::SummaryChanged);
1078 cx.emit(ContextEvent::SummaryGenerated);
1079 cx.notify();
1080 }
1081 }
1082
1083 fn can_apply_op(&self, op: &ContextOperation, cx: &App) -> bool {
1084 if !self.version.observed_all(op.version()) {
1085 return false;
1086 }
1087
1088 match op {
1089 ContextOperation::InsertMessage { anchor, .. } => self
1090 .buffer
1091 .read(cx)
1092 .version
1093 .observed(anchor.start.timestamp),
1094 ContextOperation::UpdateMessage { message_id, .. } => {
1095 self.messages_metadata.contains_key(message_id)
1096 }
1097 ContextOperation::UpdateSummary { .. } => true,
1098 ContextOperation::SlashCommandStarted { output_range, .. } => {
1099 self.has_received_operations_for_anchor_range(output_range.clone(), cx)
1100 }
1101 ContextOperation::SlashCommandOutputSectionAdded { section, .. } => {
1102 self.has_received_operations_for_anchor_range(section.range.clone(), cx)
1103 }
1104 ContextOperation::ThoughtProcessOutputSectionAdded { section, .. } => {
1105 self.has_received_operations_for_anchor_range(section.range.clone(), cx)
1106 }
1107 ContextOperation::SlashCommandFinished { .. } => true,
1108 ContextOperation::BufferOperation(_) => {
1109 panic!("buffer operations should always be applied")
1110 }
1111 }
1112 }
1113
1114 fn has_received_operations_for_anchor_range(
1115 &self,
1116 range: Range<text::Anchor>,
1117 cx: &App,
1118 ) -> bool {
1119 let version = &self.buffer.read(cx).version;
1120 let observed_start = range.start == language::Anchor::MIN
1121 || range.start == language::Anchor::MAX
1122 || version.observed(range.start.timestamp);
1123 let observed_end = range.end == language::Anchor::MIN
1124 || range.end == language::Anchor::MAX
1125 || version.observed(range.end.timestamp);
1126 observed_start && observed_end
1127 }
1128
1129 fn push_op(&mut self, op: ContextOperation, cx: &mut Context<Self>) {
1130 self.operations.push(op.clone());
1131 cx.emit(ContextEvent::Operation(op));
1132 }
1133
1134 pub fn buffer(&self) -> &Entity<Buffer> {
1135 &self.buffer
1136 }
1137
1138 pub fn language_registry(&self) -> Arc<LanguageRegistry> {
1139 self.language_registry.clone()
1140 }
1141
1142 pub fn project(&self) -> Option<Entity<Project>> {
1143 self.project.clone()
1144 }
1145
1146 pub fn prompt_builder(&self) -> Arc<PromptBuilder> {
1147 self.prompt_builder.clone()
1148 }
1149
1150 pub fn path(&self) -> Option<&Path> {
1151 self.path.as_deref()
1152 }
1153
1154 pub fn summary(&self) -> Option<&ContextSummary> {
1155 self.summary.as_ref()
1156 }
1157
1158 pub fn patch_containing(&self, position: Point, cx: &App) -> Option<&AssistantPatch> {
1159 let buffer = self.buffer.read(cx);
1160 let index = self.patches.binary_search_by(|patch| {
1161 let patch_range = patch.range.to_point(&buffer);
1162 if position < patch_range.start {
1163 Ordering::Greater
1164 } else if position > patch_range.end {
1165 Ordering::Less
1166 } else {
1167 Ordering::Equal
1168 }
1169 });
1170 if let Ok(ix) = index {
1171 Some(&self.patches[ix])
1172 } else {
1173 None
1174 }
1175 }
1176
1177 pub fn patch_ranges(&self) -> impl Iterator<Item = Range<language::Anchor>> + '_ {
1178 self.patches.iter().map(|patch| patch.range.clone())
1179 }
1180
1181 pub fn patch_for_range(
1182 &self,
1183 range: &Range<language::Anchor>,
1184 cx: &App,
1185 ) -> Option<&AssistantPatch> {
1186 let buffer = self.buffer.read(cx);
1187 let index = self.patch_index_for_range(range, buffer).ok()?;
1188 Some(&self.patches[index])
1189 }
1190
1191 fn patch_index_for_range(
1192 &self,
1193 tagged_range: &Range<text::Anchor>,
1194 buffer: &text::BufferSnapshot,
1195 ) -> Result<usize, usize> {
1196 self.patches
1197 .binary_search_by(|probe| probe.range.cmp(&tagged_range, buffer))
1198 }
1199
1200 pub fn parsed_slash_commands(&self) -> &[ParsedSlashCommand] {
1201 &self.parsed_slash_commands
1202 }
1203
1204 pub fn invoked_slash_command(
1205 &self,
1206 command_id: &InvokedSlashCommandId,
1207 ) -> Option<&InvokedSlashCommand> {
1208 self.invoked_slash_commands.get(command_id)
1209 }
1210
1211 pub fn slash_command_output_sections(&self) -> &[SlashCommandOutputSection<language::Anchor>] {
1212 &self.slash_command_output_sections
1213 }
1214
1215 pub fn thought_process_output_sections(
1216 &self,
1217 ) -> &[ThoughtProcessOutputSection<language::Anchor>] {
1218 &self.thought_process_output_sections
1219 }
1220
1221 pub fn contains_files(&self, cx: &App) -> bool {
1222 let buffer = self.buffer.read(cx);
1223 self.slash_command_output_sections.iter().any(|section| {
1224 section.is_valid(buffer)
1225 && section
1226 .metadata
1227 .as_ref()
1228 .and_then(|metadata| {
1229 serde_json::from_value::<FileCommandMetadata>(metadata.clone()).ok()
1230 })
1231 .is_some()
1232 })
1233 }
1234
1235 fn set_language(&mut self, cx: &mut Context<Self>) {
1236 let markdown = self.language_registry.language_for_name("Markdown");
1237 cx.spawn(async move |this, cx| {
1238 let markdown = markdown.await?;
1239 this.update(cx, |this, cx| {
1240 this.buffer
1241 .update(cx, |buffer, cx| buffer.set_language(Some(markdown), cx));
1242 })
1243 })
1244 .detach_and_log_err(cx);
1245 }
1246
1247 fn handle_buffer_event(
1248 &mut self,
1249 _: Entity<Buffer>,
1250 event: &language::BufferEvent,
1251 cx: &mut Context<Self>,
1252 ) {
1253 match event {
1254 language::BufferEvent::Operation {
1255 operation,
1256 is_local: true,
1257 } => cx.emit(ContextEvent::Operation(ContextOperation::BufferOperation(
1258 operation.clone(),
1259 ))),
1260 language::BufferEvent::Edited => {
1261 self.count_remaining_tokens(cx);
1262 self.reparse(cx);
1263 cx.emit(ContextEvent::MessagesEdited);
1264 }
1265 _ => {}
1266 }
1267 }
1268
1269 pub fn token_count(&self) -> Option<usize> {
1270 self.token_count
1271 }
1272
1273 pub(crate) fn count_remaining_tokens(&mut self, cx: &mut Context<Self>) {
1274 // Assume it will be a Chat request, even though that takes fewer tokens (and risks going over the limit),
1275 // because otherwise you see in the UI that your empty message has a bunch of tokens already used.
1276 let request = self.to_completion_request(RequestType::Chat, cx);
1277 let Some(model) = LanguageModelRegistry::read_global(cx).default_model() else {
1278 return;
1279 };
1280 let debounce = self.token_count.is_some();
1281 self.pending_token_count = cx.spawn(async move |this, cx| {
1282 async move {
1283 if debounce {
1284 cx.background_executor()
1285 .timer(Duration::from_millis(200))
1286 .await;
1287 }
1288
1289 let token_count = cx
1290 .update(|cx| model.model.count_tokens(request, cx))?
1291 .await?;
1292 this.update(cx, |this, cx| {
1293 this.token_count = Some(token_count);
1294 this.start_cache_warming(&model.model, cx);
1295 cx.notify()
1296 })
1297 }
1298 .log_err()
1299 .await
1300 });
1301 }
1302
1303 pub fn mark_cache_anchors(
1304 &mut self,
1305 cache_configuration: &Option<LanguageModelCacheConfiguration>,
1306 speculative: bool,
1307 cx: &mut Context<Self>,
1308 ) -> bool {
1309 let cache_configuration =
1310 cache_configuration
1311 .as_ref()
1312 .unwrap_or(&LanguageModelCacheConfiguration {
1313 max_cache_anchors: 0,
1314 should_speculate: false,
1315 min_total_token: 0,
1316 });
1317
1318 let messages: Vec<Message> = self.messages(cx).collect();
1319
1320 let mut sorted_messages = messages.clone();
1321 if speculative {
1322 // Avoid caching the last message if this is a speculative cache fetch as
1323 // it's likely to change.
1324 sorted_messages.pop();
1325 }
1326 sorted_messages.retain(|m| m.role == Role::User);
1327 sorted_messages.sort_by(|a, b| b.offset_range.len().cmp(&a.offset_range.len()));
1328
1329 let cache_anchors = if self.token_count.unwrap_or(0) < cache_configuration.min_total_token {
1330 // If we have't hit the minimum threshold to enable caching, don't cache anything.
1331 0
1332 } else {
1333 // Save 1 anchor for the inline assistant to use.
1334 max(cache_configuration.max_cache_anchors, 1) - 1
1335 };
1336 sorted_messages.truncate(cache_anchors);
1337
1338 let anchors: HashSet<MessageId> = sorted_messages
1339 .into_iter()
1340 .map(|message| message.id)
1341 .collect();
1342
1343 let buffer = self.buffer.read(cx).snapshot();
1344 let invalidated_caches: HashSet<MessageId> = messages
1345 .iter()
1346 .scan(false, |encountered_invalid, message| {
1347 let message_id = message.id;
1348 let is_invalid = self
1349 .messages_metadata
1350 .get(&message_id)
1351 .map_or(true, |metadata| {
1352 !metadata.is_cache_valid(&buffer, &message.offset_range)
1353 || *encountered_invalid
1354 });
1355 *encountered_invalid |= is_invalid;
1356 Some(if is_invalid { Some(message_id) } else { None })
1357 })
1358 .flatten()
1359 .collect();
1360
1361 let last_anchor = messages.iter().rev().find_map(|message| {
1362 if anchors.contains(&message.id) {
1363 Some(message.id)
1364 } else {
1365 None
1366 }
1367 });
1368
1369 let mut new_anchor_needs_caching = false;
1370 let current_version = &buffer.version;
1371 // If we have no anchors, mark all messages as not being cached.
1372 let mut hit_last_anchor = last_anchor.is_none();
1373
1374 for message in messages.iter() {
1375 if hit_last_anchor {
1376 self.update_metadata(message.id, cx, |metadata| metadata.cache = None);
1377 continue;
1378 }
1379
1380 if let Some(last_anchor) = last_anchor {
1381 if message.id == last_anchor {
1382 hit_last_anchor = true;
1383 }
1384 }
1385
1386 new_anchor_needs_caching = new_anchor_needs_caching
1387 || (invalidated_caches.contains(&message.id) && anchors.contains(&message.id));
1388
1389 self.update_metadata(message.id, cx, |metadata| {
1390 let cache_status = if invalidated_caches.contains(&message.id) {
1391 CacheStatus::Pending
1392 } else {
1393 metadata
1394 .cache
1395 .as_ref()
1396 .map_or(CacheStatus::Pending, |cm| cm.status.clone())
1397 };
1398 metadata.cache = Some(MessageCacheMetadata {
1399 is_anchor: anchors.contains(&message.id),
1400 is_final_anchor: hit_last_anchor,
1401 status: cache_status,
1402 cached_at: current_version.clone(),
1403 });
1404 });
1405 }
1406 new_anchor_needs_caching
1407 }
1408
1409 fn start_cache_warming(&mut self, model: &Arc<dyn LanguageModel>, cx: &mut Context<Self>) {
1410 let cache_configuration = model.cache_configuration();
1411
1412 if !self.mark_cache_anchors(&cache_configuration, true, cx) {
1413 return;
1414 }
1415 if !self.pending_completions.is_empty() {
1416 return;
1417 }
1418 if let Some(cache_configuration) = cache_configuration {
1419 if !cache_configuration.should_speculate {
1420 return;
1421 }
1422 }
1423
1424 let request = {
1425 let mut req = self.to_completion_request(RequestType::Chat, cx);
1426 // Skip the last message because it's likely to change and
1427 // therefore would be a waste to cache.
1428 req.messages.pop();
1429 req.messages.push(LanguageModelRequestMessage {
1430 role: Role::User,
1431 content: vec!["Respond only with OK, nothing else.".into()],
1432 cache: false,
1433 });
1434 req
1435 };
1436
1437 let model = Arc::clone(model);
1438 self.pending_cache_warming_task = cx.spawn(async move |this, cx| {
1439 async move {
1440 match model.stream_completion(request, &cx).await {
1441 Ok(mut stream) => {
1442 stream.next().await;
1443 log::info!("Cache warming completed successfully");
1444 }
1445 Err(e) => {
1446 log::warn!("Cache warming failed: {}", e);
1447 }
1448 };
1449 this.update(cx, |this, cx| {
1450 this.update_cache_status_for_completion(cx);
1451 })
1452 .ok();
1453 anyhow::Ok(())
1454 }
1455 .log_err()
1456 .await
1457 });
1458 }
1459
1460 pub fn update_cache_status_for_completion(&mut self, cx: &mut Context<Self>) {
1461 let cached_message_ids: Vec<MessageId> = self
1462 .messages_metadata
1463 .iter()
1464 .filter_map(|(message_id, metadata)| {
1465 metadata.cache.as_ref().and_then(|cache| {
1466 if cache.status == CacheStatus::Pending {
1467 Some(*message_id)
1468 } else {
1469 None
1470 }
1471 })
1472 })
1473 .collect();
1474
1475 for message_id in cached_message_ids {
1476 self.update_metadata(message_id, cx, |metadata| {
1477 if let Some(cache) = &mut metadata.cache {
1478 cache.status = CacheStatus::Cached;
1479 }
1480 });
1481 }
1482 cx.notify();
1483 }
1484
1485 pub fn reparse(&mut self, cx: &mut Context<Self>) {
1486 let buffer = self.buffer.read(cx).text_snapshot();
1487 let mut row_ranges = self
1488 .edits_since_last_parse
1489 .consume()
1490 .into_iter()
1491 .map(|edit| {
1492 let start_row = buffer.offset_to_point(edit.new.start).row;
1493 let end_row = buffer.offset_to_point(edit.new.end).row + 1;
1494 start_row..end_row
1495 })
1496 .peekable();
1497
1498 let mut removed_parsed_slash_command_ranges = Vec::new();
1499 let mut updated_parsed_slash_commands = Vec::new();
1500 let mut removed_patches = Vec::new();
1501 let mut updated_patches = Vec::new();
1502 while let Some(mut row_range) = row_ranges.next() {
1503 while let Some(next_row_range) = row_ranges.peek() {
1504 if row_range.end >= next_row_range.start {
1505 row_range.end = next_row_range.end;
1506 row_ranges.next();
1507 } else {
1508 break;
1509 }
1510 }
1511
1512 let start = buffer.anchor_before(Point::new(row_range.start, 0));
1513 let end = buffer.anchor_after(Point::new(
1514 row_range.end - 1,
1515 buffer.line_len(row_range.end - 1),
1516 ));
1517
1518 self.reparse_slash_commands_in_range(
1519 start..end,
1520 &buffer,
1521 &mut updated_parsed_slash_commands,
1522 &mut removed_parsed_slash_command_ranges,
1523 cx,
1524 );
1525 self.invalidate_pending_slash_commands(&buffer, cx);
1526 self.reparse_patches_in_range(
1527 start..end,
1528 &buffer,
1529 &mut updated_patches,
1530 &mut removed_patches,
1531 cx,
1532 );
1533 }
1534
1535 if !updated_parsed_slash_commands.is_empty()
1536 || !removed_parsed_slash_command_ranges.is_empty()
1537 {
1538 cx.emit(ContextEvent::ParsedSlashCommandsUpdated {
1539 removed: removed_parsed_slash_command_ranges,
1540 updated: updated_parsed_slash_commands,
1541 });
1542 }
1543
1544 if !updated_patches.is_empty() || !removed_patches.is_empty() {
1545 cx.emit(ContextEvent::PatchesUpdated {
1546 removed: removed_patches,
1547 updated: updated_patches,
1548 });
1549 }
1550 }
1551
1552 fn reparse_slash_commands_in_range(
1553 &mut self,
1554 range: Range<text::Anchor>,
1555 buffer: &BufferSnapshot,
1556 updated: &mut Vec<ParsedSlashCommand>,
1557 removed: &mut Vec<Range<text::Anchor>>,
1558 cx: &App,
1559 ) {
1560 let old_range = self.pending_command_indices_for_range(range.clone(), cx);
1561
1562 let mut new_commands = Vec::new();
1563 let mut lines = buffer.text_for_range(range).lines();
1564 let mut offset = lines.offset();
1565 while let Some(line) = lines.next() {
1566 if let Some(command_line) = SlashCommandLine::parse(line) {
1567 let name = &line[command_line.name.clone()];
1568 let arguments = command_line
1569 .arguments
1570 .iter()
1571 .filter_map(|argument_range| {
1572 if argument_range.is_empty() {
1573 None
1574 } else {
1575 line.get(argument_range.clone())
1576 }
1577 })
1578 .map(ToOwned::to_owned)
1579 .collect::<SmallVec<_>>();
1580 if let Some(command) = self.slash_commands.command(name, cx) {
1581 if !command.requires_argument() || !arguments.is_empty() {
1582 let start_ix = offset + command_line.name.start - 1;
1583 let end_ix = offset
1584 + command_line
1585 .arguments
1586 .last()
1587 .map_or(command_line.name.end, |argument| argument.end);
1588 let source_range =
1589 buffer.anchor_after(start_ix)..buffer.anchor_after(end_ix);
1590 let pending_command = ParsedSlashCommand {
1591 name: name.to_string(),
1592 arguments,
1593 source_range,
1594 status: PendingSlashCommandStatus::Idle,
1595 };
1596 updated.push(pending_command.clone());
1597 new_commands.push(pending_command);
1598 }
1599 }
1600 }
1601
1602 offset = lines.offset();
1603 }
1604
1605 let removed_commands = self.parsed_slash_commands.splice(old_range, new_commands);
1606 removed.extend(removed_commands.map(|command| command.source_range));
1607 }
1608
1609 fn invalidate_pending_slash_commands(
1610 &mut self,
1611 buffer: &BufferSnapshot,
1612 cx: &mut Context<Self>,
1613 ) {
1614 let mut invalidated_command_ids = Vec::new();
1615 for (&command_id, command) in self.invoked_slash_commands.iter_mut() {
1616 if !matches!(command.status, InvokedSlashCommandStatus::Finished)
1617 && (!command.range.start.is_valid(buffer) || !command.range.end.is_valid(buffer))
1618 {
1619 command.status = InvokedSlashCommandStatus::Finished;
1620 cx.emit(ContextEvent::InvokedSlashCommandChanged { command_id });
1621 invalidated_command_ids.push(command_id);
1622 }
1623 }
1624
1625 for command_id in invalidated_command_ids {
1626 let version = self.version.clone();
1627 let timestamp = self.next_timestamp();
1628 self.push_op(
1629 ContextOperation::SlashCommandFinished {
1630 id: command_id,
1631 timestamp,
1632 error_message: None,
1633 version: version.clone(),
1634 },
1635 cx,
1636 );
1637 }
1638 }
1639
1640 fn reparse_patches_in_range(
1641 &mut self,
1642 range: Range<text::Anchor>,
1643 buffer: &BufferSnapshot,
1644 updated: &mut Vec<Range<text::Anchor>>,
1645 removed: &mut Vec<Range<text::Anchor>>,
1646 cx: &mut Context<Self>,
1647 ) {
1648 // Rebuild the XML tags in the edited range.
1649 let intersecting_tags_range =
1650 self.indices_intersecting_buffer_range(&self.xml_tags, range.clone(), cx);
1651 let new_tags = self.parse_xml_tags_in_range(buffer, range.clone(), cx);
1652 self.xml_tags
1653 .splice(intersecting_tags_range.clone(), new_tags);
1654
1655 // Find which patches intersect the changed range.
1656 let intersecting_patches_range =
1657 self.indices_intersecting_buffer_range(&self.patches, range.clone(), cx);
1658
1659 // Reparse all tags after the last unchanged patch before the change.
1660 let mut tags_start_ix = 0;
1661 if let Some(preceding_unchanged_patch) =
1662 self.patches[..intersecting_patches_range.start].last()
1663 {
1664 tags_start_ix = match self.xml_tags.binary_search_by(|tag| {
1665 tag.range
1666 .start
1667 .cmp(&preceding_unchanged_patch.range.end, buffer)
1668 .then(Ordering::Less)
1669 }) {
1670 Ok(ix) | Err(ix) => ix,
1671 };
1672 }
1673
1674 // Rebuild the patches in the range.
1675 let new_patches = self.parse_patches(tags_start_ix, range.end, buffer, cx);
1676 updated.extend(new_patches.iter().map(|patch| patch.range.clone()));
1677 let removed_patches = self.patches.splice(intersecting_patches_range, new_patches);
1678 removed.extend(
1679 removed_patches
1680 .map(|patch| patch.range)
1681 .filter(|range| !updated.contains(&range)),
1682 );
1683 }
1684
1685 fn parse_xml_tags_in_range(
1686 &self,
1687 buffer: &BufferSnapshot,
1688 range: Range<text::Anchor>,
1689 cx: &App,
1690 ) -> Vec<XmlTag> {
1691 let mut messages = self.messages(cx).peekable();
1692
1693 let mut tags = Vec::new();
1694 let mut lines = buffer.text_for_range(range).lines();
1695 let mut offset = lines.offset();
1696
1697 while let Some(line) = lines.next() {
1698 while let Some(message) = messages.peek() {
1699 if offset < message.offset_range.end {
1700 break;
1701 } else {
1702 messages.next();
1703 }
1704 }
1705
1706 let is_assistant_message = messages
1707 .peek()
1708 .map_or(false, |message| message.role == Role::Assistant);
1709 if is_assistant_message {
1710 for (start_ix, _) in line.match_indices('<') {
1711 let mut name_start_ix = start_ix + 1;
1712 let closing_bracket_ix = line[start_ix..].find('>').map(|i| start_ix + i);
1713 if let Some(closing_bracket_ix) = closing_bracket_ix {
1714 let end_ix = closing_bracket_ix + 1;
1715 let mut is_open_tag = true;
1716 if line[name_start_ix..closing_bracket_ix].starts_with('/') {
1717 name_start_ix += 1;
1718 is_open_tag = false;
1719 }
1720 let tag_inner = &line[name_start_ix..closing_bracket_ix];
1721 let tag_name_len = tag_inner
1722 .find(|c: char| c.is_whitespace())
1723 .unwrap_or(tag_inner.len());
1724 if let Ok(kind) = XmlTagKind::from_str(&tag_inner[..tag_name_len]) {
1725 tags.push(XmlTag {
1726 range: buffer.anchor_after(offset + start_ix)
1727 ..buffer.anchor_before(offset + end_ix),
1728 is_open_tag,
1729 kind,
1730 });
1731 };
1732 }
1733 }
1734 }
1735
1736 offset = lines.offset();
1737 }
1738 tags
1739 }
1740
1741 fn parse_patches(
1742 &mut self,
1743 tags_start_ix: usize,
1744 buffer_end: text::Anchor,
1745 buffer: &BufferSnapshot,
1746 cx: &App,
1747 ) -> Vec<AssistantPatch> {
1748 let mut new_patches = Vec::new();
1749 let mut pending_patch = None;
1750 let mut patch_tag_depth = 0;
1751 let mut tags = self.xml_tags[tags_start_ix..].iter().peekable();
1752 'tags: while let Some(tag) = tags.next() {
1753 if tag.range.start.cmp(&buffer_end, buffer).is_gt() && patch_tag_depth == 0 {
1754 break;
1755 }
1756
1757 if tag.kind == XmlTagKind::Patch && tag.is_open_tag {
1758 patch_tag_depth += 1;
1759 let patch_start = tag.range.start;
1760 let mut edits = Vec::<Result<AssistantEdit>>::new();
1761 let mut patch = AssistantPatch {
1762 range: patch_start..patch_start,
1763 title: String::new().into(),
1764 edits: Default::default(),
1765 status: crate::AssistantPatchStatus::Pending,
1766 };
1767
1768 while let Some(tag) = tags.next() {
1769 if tag.kind == XmlTagKind::Patch && !tag.is_open_tag {
1770 patch_tag_depth -= 1;
1771 if patch_tag_depth == 0 {
1772 patch.range.end = tag.range.end;
1773
1774 // Include the line immediately after this <patch> tag if it's empty.
1775 let patch_end_offset = patch.range.end.to_offset(buffer);
1776 let mut patch_end_chars = buffer.chars_at(patch_end_offset);
1777 if patch_end_chars.next() == Some('\n')
1778 && patch_end_chars.next().map_or(true, |ch| ch == '\n')
1779 {
1780 let messages = self.messages_for_offsets(
1781 [patch_end_offset, patch_end_offset + 1],
1782 cx,
1783 );
1784 if messages.len() == 1 {
1785 patch.range.end = buffer.anchor_before(patch_end_offset + 1);
1786 }
1787 }
1788
1789 edits.sort_unstable_by(|a, b| {
1790 if let (Ok(a), Ok(b)) = (a, b) {
1791 a.path.cmp(&b.path)
1792 } else {
1793 Ordering::Equal
1794 }
1795 });
1796 patch.edits = edits.into();
1797 patch.status = AssistantPatchStatus::Ready;
1798 new_patches.push(patch);
1799 continue 'tags;
1800 }
1801 }
1802
1803 if tag.kind == XmlTagKind::Title && tag.is_open_tag {
1804 let content_start = tag.range.end;
1805 while let Some(tag) = tags.next() {
1806 if tag.kind == XmlTagKind::Title && !tag.is_open_tag {
1807 let content_end = tag.range.start;
1808 patch.title =
1809 trimmed_text_in_range(buffer, content_start..content_end)
1810 .into();
1811 break;
1812 }
1813 }
1814 }
1815
1816 if tag.kind == XmlTagKind::Edit && tag.is_open_tag {
1817 let mut path = None;
1818 let mut old_text = None;
1819 let mut new_text = None;
1820 let mut operation = None;
1821 let mut description = None;
1822
1823 while let Some(tag) = tags.next() {
1824 if tag.kind == XmlTagKind::Edit && !tag.is_open_tag {
1825 edits.push(AssistantEdit::new(
1826 path,
1827 operation,
1828 old_text,
1829 new_text,
1830 description,
1831 ));
1832 break;
1833 }
1834
1835 if tag.is_open_tag
1836 && [
1837 XmlTagKind::Path,
1838 XmlTagKind::OldText,
1839 XmlTagKind::NewText,
1840 XmlTagKind::Operation,
1841 XmlTagKind::Description,
1842 ]
1843 .contains(&tag.kind)
1844 {
1845 let kind = tag.kind;
1846 let content_start = tag.range.end;
1847 if let Some(tag) = tags.peek() {
1848 if tag.kind == kind && !tag.is_open_tag {
1849 let tag = tags.next().unwrap();
1850 let content_end = tag.range.start;
1851 let content = trimmed_text_in_range(
1852 buffer,
1853 content_start..content_end,
1854 );
1855 match kind {
1856 XmlTagKind::Path => path = Some(content),
1857 XmlTagKind::Operation => operation = Some(content),
1858 XmlTagKind::OldText => {
1859 old_text = Some(content).filter(|s| !s.is_empty())
1860 }
1861 XmlTagKind::NewText => {
1862 new_text = Some(content).filter(|s| !s.is_empty())
1863 }
1864 XmlTagKind::Description => {
1865 description =
1866 Some(content).filter(|s| !s.is_empty())
1867 }
1868 _ => {}
1869 }
1870 }
1871 }
1872 }
1873 }
1874 }
1875 }
1876
1877 patch.edits = edits.into();
1878 pending_patch = Some(patch);
1879 }
1880 }
1881
1882 if let Some(mut pending_patch) = pending_patch {
1883 let patch_start = pending_patch.range.start.to_offset(buffer);
1884 if let Some(message) = self.message_for_offset(patch_start, cx) {
1885 if message.anchor_range.end == text::Anchor::MAX {
1886 pending_patch.range.end = text::Anchor::MAX;
1887 } else {
1888 let message_end = buffer.anchor_after(message.offset_range.end - 1);
1889 pending_patch.range.end = message_end;
1890 }
1891 } else {
1892 pending_patch.range.end = text::Anchor::MAX;
1893 }
1894
1895 new_patches.push(pending_patch);
1896 }
1897
1898 new_patches
1899 }
1900
1901 pub fn pending_command_for_position(
1902 &mut self,
1903 position: language::Anchor,
1904 cx: &mut Context<Self>,
1905 ) -> Option<&mut ParsedSlashCommand> {
1906 let buffer = self.buffer.read(cx);
1907 match self
1908 .parsed_slash_commands
1909 .binary_search_by(|probe| probe.source_range.end.cmp(&position, buffer))
1910 {
1911 Ok(ix) => Some(&mut self.parsed_slash_commands[ix]),
1912 Err(ix) => {
1913 let cmd = self.parsed_slash_commands.get_mut(ix)?;
1914 if position.cmp(&cmd.source_range.start, buffer).is_ge()
1915 && position.cmp(&cmd.source_range.end, buffer).is_le()
1916 {
1917 Some(cmd)
1918 } else {
1919 None
1920 }
1921 }
1922 }
1923 }
1924
1925 pub fn pending_commands_for_range(
1926 &self,
1927 range: Range<language::Anchor>,
1928 cx: &App,
1929 ) -> &[ParsedSlashCommand] {
1930 let range = self.pending_command_indices_for_range(range, cx);
1931 &self.parsed_slash_commands[range]
1932 }
1933
1934 fn pending_command_indices_for_range(
1935 &self,
1936 range: Range<language::Anchor>,
1937 cx: &App,
1938 ) -> Range<usize> {
1939 self.indices_intersecting_buffer_range(&self.parsed_slash_commands, range, cx)
1940 }
1941
1942 fn indices_intersecting_buffer_range<T: ContextAnnotation>(
1943 &self,
1944 all_annotations: &[T],
1945 range: Range<language::Anchor>,
1946 cx: &App,
1947 ) -> Range<usize> {
1948 let buffer = self.buffer.read(cx);
1949 let start_ix = match all_annotations
1950 .binary_search_by(|probe| probe.range().end.cmp(&range.start, &buffer))
1951 {
1952 Ok(ix) | Err(ix) => ix,
1953 };
1954 let end_ix = match all_annotations
1955 .binary_search_by(|probe| probe.range().start.cmp(&range.end, &buffer))
1956 {
1957 Ok(ix) => ix + 1,
1958 Err(ix) => ix,
1959 };
1960 start_ix..end_ix
1961 }
1962
1963 pub fn insert_command_output(
1964 &mut self,
1965 command_source_range: Range<language::Anchor>,
1966 name: &str,
1967 output: Task<SlashCommandResult>,
1968 ensure_trailing_newline: bool,
1969 cx: &mut Context<Self>,
1970 ) {
1971 let version = self.version.clone();
1972 let command_id = InvokedSlashCommandId(self.next_timestamp());
1973
1974 const PENDING_OUTPUT_END_MARKER: &str = "…";
1975
1976 let (command_range, command_source_range, insert_position, first_transaction) =
1977 self.buffer.update(cx, |buffer, cx| {
1978 let command_source_range = command_source_range.to_offset(buffer);
1979 let mut insertion = format!("\n{PENDING_OUTPUT_END_MARKER}");
1980 if ensure_trailing_newline {
1981 insertion.push('\n');
1982 }
1983
1984 buffer.finalize_last_transaction();
1985 buffer.start_transaction();
1986 buffer.edit(
1987 [(
1988 command_source_range.end..command_source_range.end,
1989 insertion,
1990 )],
1991 None,
1992 cx,
1993 );
1994 let first_transaction = buffer.end_transaction(cx).unwrap();
1995 buffer.finalize_last_transaction();
1996
1997 let insert_position = buffer.anchor_after(command_source_range.end + 1);
1998 let command_range = buffer.anchor_after(command_source_range.start)
1999 ..buffer.anchor_before(
2000 command_source_range.end + 1 + PENDING_OUTPUT_END_MARKER.len(),
2001 );
2002 let command_source_range = buffer.anchor_before(command_source_range.start)
2003 ..buffer.anchor_before(command_source_range.end + 1);
2004 (
2005 command_range,
2006 command_source_range,
2007 insert_position,
2008 first_transaction,
2009 )
2010 });
2011 self.reparse(cx);
2012
2013 let insert_output_task = cx.spawn(async move |this, cx| {
2014 let run_command = async {
2015 let mut stream = output.await?;
2016
2017 struct PendingSection {
2018 start: language::Anchor,
2019 icon: IconName,
2020 label: SharedString,
2021 metadata: Option<serde_json::Value>,
2022 }
2023
2024 let mut pending_section_stack: Vec<PendingSection> = Vec::new();
2025 let mut last_role: Option<Role> = None;
2026 let mut last_section_range = None;
2027
2028 while let Some(event) = stream.next().await {
2029 let event = event?;
2030 this.update(cx, |this, cx| {
2031 this.buffer.update(cx, |buffer, _cx| {
2032 buffer.finalize_last_transaction();
2033 buffer.start_transaction()
2034 });
2035
2036 match event {
2037 SlashCommandEvent::StartMessage {
2038 role,
2039 merge_same_roles,
2040 } => {
2041 if !merge_same_roles && Some(role) != last_role {
2042 let offset = this.buffer.read_with(cx, |buffer, _cx| {
2043 insert_position.to_offset(buffer)
2044 });
2045 this.insert_message_at_offset(
2046 offset,
2047 role,
2048 MessageStatus::Pending,
2049 cx,
2050 );
2051 }
2052
2053 last_role = Some(role);
2054 }
2055 SlashCommandEvent::StartSection {
2056 icon,
2057 label,
2058 metadata,
2059 } => {
2060 this.buffer.update(cx, |buffer, cx| {
2061 let insert_point = insert_position.to_point(buffer);
2062 if insert_point.column > 0 {
2063 buffer.edit([(insert_point..insert_point, "\n")], None, cx);
2064 }
2065
2066 pending_section_stack.push(PendingSection {
2067 start: buffer.anchor_before(insert_position),
2068 icon,
2069 label,
2070 metadata,
2071 });
2072 });
2073 }
2074 SlashCommandEvent::Content(SlashCommandContent::Text {
2075 text,
2076 run_commands_in_text,
2077 }) => {
2078 let start = this.buffer.read(cx).anchor_before(insert_position);
2079
2080 this.buffer.update(cx, |buffer, cx| {
2081 buffer.edit(
2082 [(insert_position..insert_position, text)],
2083 None,
2084 cx,
2085 )
2086 });
2087
2088 let end = this.buffer.read(cx).anchor_before(insert_position);
2089 if run_commands_in_text {
2090 if let Some(invoked_slash_command) =
2091 this.invoked_slash_commands.get_mut(&command_id)
2092 {
2093 invoked_slash_command
2094 .run_commands_in_ranges
2095 .push(start..end);
2096 }
2097 }
2098 }
2099 SlashCommandEvent::EndSection => {
2100 if let Some(pending_section) = pending_section_stack.pop() {
2101 let offset_range = (pending_section.start..insert_position)
2102 .to_offset(this.buffer.read(cx));
2103 if !offset_range.is_empty() {
2104 let range = this.buffer.update(cx, |buffer, _cx| {
2105 buffer.anchor_after(offset_range.start)
2106 ..buffer.anchor_before(offset_range.end)
2107 });
2108 this.insert_slash_command_output_section(
2109 SlashCommandOutputSection {
2110 range: range.clone(),
2111 icon: pending_section.icon,
2112 label: pending_section.label,
2113 metadata: pending_section.metadata,
2114 },
2115 cx,
2116 );
2117 last_section_range = Some(range);
2118 }
2119 }
2120 }
2121 }
2122
2123 this.buffer.update(cx, |buffer, cx| {
2124 if let Some(event_transaction) = buffer.end_transaction(cx) {
2125 buffer.merge_transactions(event_transaction, first_transaction);
2126 }
2127 });
2128 })?;
2129 }
2130
2131 this.update(cx, |this, cx| {
2132 this.buffer.update(cx, |buffer, cx| {
2133 buffer.finalize_last_transaction();
2134 buffer.start_transaction();
2135
2136 let mut deletions = vec![(command_source_range.to_offset(buffer), "")];
2137 let insert_position = insert_position.to_offset(buffer);
2138 let command_range_end = command_range.end.to_offset(buffer);
2139
2140 if buffer.contains_str_at(insert_position, PENDING_OUTPUT_END_MARKER) {
2141 deletions.push((
2142 insert_position..insert_position + PENDING_OUTPUT_END_MARKER.len(),
2143 "",
2144 ));
2145 }
2146
2147 if ensure_trailing_newline
2148 && buffer.contains_str_at(command_range_end, "\n")
2149 {
2150 let newline_offset = insert_position.saturating_sub(1);
2151 if buffer.contains_str_at(newline_offset, "\n")
2152 && last_section_range.map_or(true, |last_section_range| {
2153 !last_section_range
2154 .to_offset(buffer)
2155 .contains(&newline_offset)
2156 })
2157 {
2158 deletions.push((command_range_end..command_range_end + 1, ""));
2159 }
2160 }
2161
2162 buffer.edit(deletions, None, cx);
2163
2164 if let Some(deletion_transaction) = buffer.end_transaction(cx) {
2165 buffer.merge_transactions(deletion_transaction, first_transaction);
2166 }
2167 });
2168 })?;
2169
2170 debug_assert!(pending_section_stack.is_empty());
2171
2172 anyhow::Ok(())
2173 };
2174
2175 let command_result = run_command.await;
2176
2177 this.update(cx, |this, cx| {
2178 let version = this.version.clone();
2179 let timestamp = this.next_timestamp();
2180 let Some(invoked_slash_command) = this.invoked_slash_commands.get_mut(&command_id)
2181 else {
2182 return;
2183 };
2184 let mut error_message = None;
2185 match command_result {
2186 Ok(()) => {
2187 invoked_slash_command.status = InvokedSlashCommandStatus::Finished;
2188 }
2189 Err(error) => {
2190 let message = error.to_string();
2191 invoked_slash_command.status =
2192 InvokedSlashCommandStatus::Error(message.clone().into());
2193 error_message = Some(message);
2194 }
2195 }
2196
2197 cx.emit(ContextEvent::InvokedSlashCommandChanged { command_id });
2198 this.push_op(
2199 ContextOperation::SlashCommandFinished {
2200 id: command_id,
2201 timestamp,
2202 error_message,
2203 version,
2204 },
2205 cx,
2206 );
2207 })
2208 .ok();
2209 });
2210
2211 self.invoked_slash_commands.insert(
2212 command_id,
2213 InvokedSlashCommand {
2214 name: name.to_string().into(),
2215 range: command_range.clone(),
2216 run_commands_in_ranges: Vec::new(),
2217 status: InvokedSlashCommandStatus::Running(insert_output_task),
2218 transaction: Some(first_transaction),
2219 timestamp: command_id.0,
2220 },
2221 );
2222 cx.emit(ContextEvent::InvokedSlashCommandChanged { command_id });
2223 self.push_op(
2224 ContextOperation::SlashCommandStarted {
2225 id: command_id,
2226 output_range: command_range,
2227 name: name.to_string(),
2228 version,
2229 },
2230 cx,
2231 );
2232 }
2233
2234 fn insert_slash_command_output_section(
2235 &mut self,
2236 section: SlashCommandOutputSection<language::Anchor>,
2237 cx: &mut Context<Self>,
2238 ) {
2239 let buffer = self.buffer.read(cx);
2240 let insertion_ix = match self
2241 .slash_command_output_sections
2242 .binary_search_by(|probe| probe.range.cmp(§ion.range, buffer))
2243 {
2244 Ok(ix) | Err(ix) => ix,
2245 };
2246 self.slash_command_output_sections
2247 .insert(insertion_ix, section.clone());
2248 cx.emit(ContextEvent::SlashCommandOutputSectionAdded {
2249 section: section.clone(),
2250 });
2251 let version = self.version.clone();
2252 let timestamp = self.next_timestamp();
2253 self.push_op(
2254 ContextOperation::SlashCommandOutputSectionAdded {
2255 timestamp,
2256 section,
2257 version,
2258 },
2259 cx,
2260 );
2261 }
2262
2263 fn insert_thought_process_output_section(
2264 &mut self,
2265 section: ThoughtProcessOutputSection<language::Anchor>,
2266 cx: &mut Context<Self>,
2267 ) {
2268 let buffer = self.buffer.read(cx);
2269 let insertion_ix = match self
2270 .thought_process_output_sections
2271 .binary_search_by(|probe| probe.range.cmp(§ion.range, buffer))
2272 {
2273 Ok(ix) | Err(ix) => ix,
2274 };
2275 self.thought_process_output_sections
2276 .insert(insertion_ix, section.clone());
2277 // cx.emit(ContextEvent::ThoughtProcessOutputSectionAdded {
2278 // section: section.clone(),
2279 // });
2280 let version = self.version.clone();
2281 let timestamp = self.next_timestamp();
2282 self.push_op(
2283 ContextOperation::ThoughtProcessOutputSectionAdded {
2284 timestamp,
2285 section,
2286 version,
2287 },
2288 cx,
2289 );
2290 }
2291
2292 pub fn completion_provider_changed(&mut self, cx: &mut Context<Self>) {
2293 self.count_remaining_tokens(cx);
2294 }
2295
2296 fn get_last_valid_message_id(&self, cx: &Context<Self>) -> Option<MessageId> {
2297 self.message_anchors.iter().rev().find_map(|message| {
2298 message
2299 .start
2300 .is_valid(self.buffer.read(cx))
2301 .then_some(message.id)
2302 })
2303 }
2304
2305 pub fn assist(
2306 &mut self,
2307 request_type: RequestType,
2308 cx: &mut Context<Self>,
2309 ) -> Option<MessageAnchor> {
2310 let model_registry = LanguageModelRegistry::read_global(cx);
2311 let model = model_registry.default_model()?;
2312 let last_message_id = self.get_last_valid_message_id(cx)?;
2313
2314 if !model.provider.is_authenticated(cx) {
2315 log::info!("completion provider has no credentials");
2316 return None;
2317 }
2318
2319 let model = model.model;
2320
2321 // Compute which messages to cache, including the last one.
2322 self.mark_cache_anchors(&model.cache_configuration(), false, cx);
2323
2324 let request = self.to_completion_request(request_type, cx);
2325
2326 let assistant_message = self
2327 .insert_message_after(last_message_id, Role::Assistant, MessageStatus::Pending, cx)
2328 .unwrap();
2329
2330 // Queue up the user's next reply.
2331 let user_message = self
2332 .insert_message_after(assistant_message.id, Role::User, MessageStatus::Done, cx)
2333 .unwrap();
2334
2335 let pending_completion_id = post_inc(&mut self.completion_count);
2336
2337 let task = cx.spawn({
2338 async move |this, cx| {
2339 let stream = model.stream_completion(request, &cx);
2340 let assistant_message_id = assistant_message.id;
2341 let mut response_latency = None;
2342 let stream_completion = async {
2343 let request_start = Instant::now();
2344 let mut events = stream.await?;
2345 let mut stop_reason = StopReason::EndTurn;
2346 let mut thought_process_stack = Vec::new();
2347
2348 const THOUGHT_PROCESS_START_MARKER: &str = "<think>\n";
2349 const THOUGHT_PROCESS_END_MARKER: &str = "\n</think>";
2350
2351 while let Some(event) = events.next().await {
2352 if response_latency.is_none() {
2353 response_latency = Some(request_start.elapsed());
2354 }
2355 let event = event?;
2356
2357 let mut context_event = None;
2358 let mut thought_process_output_section = None;
2359
2360 this.update(cx, |this, cx| {
2361 let message_ix = this
2362 .message_anchors
2363 .iter()
2364 .position(|message| message.id == assistant_message_id)?;
2365 this.buffer.update(cx, |buffer, cx| {
2366 let message_old_end_offset = this.message_anchors[message_ix + 1..]
2367 .iter()
2368 .find(|message| message.start.is_valid(buffer))
2369 .map_or(buffer.len(), |message| {
2370 message.start.to_offset(buffer).saturating_sub(1)
2371 });
2372
2373 match event {
2374 LanguageModelCompletionEvent::StartMessage { .. } => {}
2375 LanguageModelCompletionEvent::Stop(reason) => {
2376 stop_reason = reason;
2377 }
2378 LanguageModelCompletionEvent::Thinking { text: chunk, .. } => {
2379 if thought_process_stack.is_empty() {
2380 let start =
2381 buffer.anchor_before(message_old_end_offset);
2382 thought_process_stack.push(start);
2383 let chunk =
2384 format!("{THOUGHT_PROCESS_START_MARKER}{chunk}{THOUGHT_PROCESS_END_MARKER}");
2385 let chunk_len = chunk.len();
2386 buffer.edit(
2387 [(
2388 message_old_end_offset..message_old_end_offset,
2389 chunk,
2390 )],
2391 None,
2392 cx,
2393 );
2394 let end = buffer
2395 .anchor_before(message_old_end_offset + chunk_len);
2396 context_event = Some(
2397 ContextEvent::StartedThoughtProcess(start..end),
2398 );
2399 } else {
2400 // This ensures that all the thinking chunks are inserted inside the thinking tag
2401 let insertion_position =
2402 message_old_end_offset - THOUGHT_PROCESS_END_MARKER.len();
2403 buffer.edit(
2404 [(insertion_position..insertion_position, chunk)],
2405 None,
2406 cx,
2407 );
2408 }
2409 }
2410 LanguageModelCompletionEvent::Text(mut chunk) => {
2411 if let Some(start) = thought_process_stack.pop() {
2412 let end = buffer.anchor_before(message_old_end_offset);
2413 context_event =
2414 Some(ContextEvent::EndedThoughtProcess(end));
2415 thought_process_output_section =
2416 Some(ThoughtProcessOutputSection {
2417 range: start..end,
2418 });
2419 chunk.insert_str(0, "\n\n");
2420 }
2421
2422 buffer.edit(
2423 [(
2424 message_old_end_offset..message_old_end_offset,
2425 chunk,
2426 )],
2427 None,
2428 cx,
2429 );
2430 }
2431 LanguageModelCompletionEvent::ToolUse(_) => {}
2432 LanguageModelCompletionEvent::UsageUpdate(_) => {}
2433 }
2434 });
2435
2436 if let Some(section) = thought_process_output_section.take() {
2437 this.insert_thought_process_output_section(section, cx);
2438 }
2439 if let Some(context_event) = context_event.take() {
2440 cx.emit(context_event);
2441 }
2442
2443 cx.emit(ContextEvent::StreamedCompletion);
2444
2445 Some(())
2446 })?;
2447 smol::future::yield_now().await;
2448 }
2449 this.update(cx, |this, cx| {
2450 this.pending_completions
2451 .retain(|completion| completion.id != pending_completion_id);
2452 this.summarize(false, cx);
2453 this.update_cache_status_for_completion(cx);
2454 })?;
2455
2456 anyhow::Ok(stop_reason)
2457 };
2458
2459 let result = stream_completion.await;
2460
2461 this.update(cx, |this, cx| {
2462 let error_message = if let Some(error) = result.as_ref().err() {
2463 if error.is::<PaymentRequiredError>() {
2464 cx.emit(ContextEvent::ShowPaymentRequiredError);
2465 this.update_metadata(assistant_message_id, cx, |metadata| {
2466 metadata.status = MessageStatus::Canceled;
2467 });
2468 Some(error.to_string())
2469 } else if error.is::<MaxMonthlySpendReachedError>() {
2470 cx.emit(ContextEvent::ShowMaxMonthlySpendReachedError);
2471 this.update_metadata(assistant_message_id, cx, |metadata| {
2472 metadata.status = MessageStatus::Canceled;
2473 });
2474 Some(error.to_string())
2475 } else {
2476 let error_message = error
2477 .chain()
2478 .map(|err| err.to_string())
2479 .collect::<Vec<_>>()
2480 .join("\n");
2481 cx.emit(ContextEvent::ShowAssistError(SharedString::from(
2482 error_message.clone(),
2483 )));
2484 this.update_metadata(assistant_message_id, cx, |metadata| {
2485 metadata.status =
2486 MessageStatus::Error(SharedString::from(error_message.clone()));
2487 });
2488 Some(error_message)
2489 }
2490 } else {
2491 this.update_metadata(assistant_message_id, cx, |metadata| {
2492 metadata.status = MessageStatus::Done;
2493 });
2494 None
2495 };
2496
2497 let language_name = this
2498 .buffer
2499 .read(cx)
2500 .language()
2501 .map(|language| language.name());
2502 report_assistant_event(
2503 AssistantEventData {
2504 conversation_id: Some(this.id.0.clone()),
2505 kind: AssistantKind::Panel,
2506 phase: AssistantPhase::Response,
2507 message_id: None,
2508 model: model.telemetry_id(),
2509 model_provider: model.provider_id().to_string(),
2510 response_latency,
2511 error_message,
2512 language_name: language_name.map(|name| name.to_proto()),
2513 },
2514 this.telemetry.clone(),
2515 cx.http_client(),
2516 model.api_key(cx),
2517 cx.background_executor(),
2518 );
2519
2520 if let Ok(stop_reason) = result {
2521 match stop_reason {
2522 StopReason::ToolUse => {}
2523 StopReason::EndTurn => {}
2524 StopReason::MaxTokens => {}
2525 }
2526 }
2527 })
2528 .ok();
2529 }
2530 });
2531
2532 self.pending_completions.push(PendingCompletion {
2533 id: pending_completion_id,
2534 assistant_message_id: assistant_message.id,
2535 _task: task,
2536 });
2537
2538 Some(user_message)
2539 }
2540
2541 pub fn to_completion_request(
2542 &self,
2543 request_type: RequestType,
2544 cx: &App,
2545 ) -> LanguageModelRequest {
2546 let buffer = self.buffer.read(cx);
2547
2548 let mut contents = self.contents(cx).peekable();
2549
2550 fn collect_text_content(buffer: &Buffer, range: Range<usize>) -> Option<String> {
2551 let text: String = buffer.text_for_range(range.clone()).collect();
2552 if text.trim().is_empty() {
2553 None
2554 } else {
2555 Some(text)
2556 }
2557 }
2558
2559 let mut completion_request = LanguageModelRequest {
2560 thread_id: None,
2561 prompt_id: None,
2562 mode: None,
2563 messages: Vec::new(),
2564 tools: Vec::new(),
2565 stop: Vec::new(),
2566 temperature: None,
2567 };
2568 for message in self.messages(cx) {
2569 if message.status != MessageStatus::Done {
2570 continue;
2571 }
2572
2573 let mut offset = message.offset_range.start;
2574 let mut request_message = LanguageModelRequestMessage {
2575 role: message.role,
2576 content: Vec::new(),
2577 cache: message
2578 .cache
2579 .as_ref()
2580 .map_or(false, |cache| cache.is_anchor),
2581 };
2582
2583 while let Some(content) = contents.peek() {
2584 if content
2585 .range()
2586 .end
2587 .cmp(&message.anchor_range.end, buffer)
2588 .is_lt()
2589 {
2590 let content = contents.next().unwrap();
2591 let range = content.range().to_offset(buffer);
2592 request_message.content.extend(
2593 collect_text_content(buffer, offset..range.start).map(MessageContent::Text),
2594 );
2595
2596 match content {
2597 Content::Image { image, .. } => {
2598 if let Some(image) = image.clone().now_or_never().flatten() {
2599 request_message
2600 .content
2601 .push(language_model::MessageContent::Image(image));
2602 }
2603 }
2604 }
2605
2606 offset = range.end;
2607 } else {
2608 break;
2609 }
2610 }
2611
2612 request_message.content.extend(
2613 collect_text_content(buffer, offset..message.offset_range.end)
2614 .map(MessageContent::Text),
2615 );
2616
2617 if !request_message.contents_empty() {
2618 completion_request.messages.push(request_message);
2619 }
2620 }
2621
2622 if let RequestType::SuggestEdits = request_type {
2623 if let Ok(preamble) = self.prompt_builder.generate_suggest_edits_prompt() {
2624 let last_elem_index = completion_request.messages.len();
2625
2626 completion_request
2627 .messages
2628 .push(LanguageModelRequestMessage {
2629 role: Role::User,
2630 content: vec![MessageContent::Text(preamble)],
2631 cache: false,
2632 });
2633
2634 // The preamble message should be sent right before the last actual user message.
2635 completion_request
2636 .messages
2637 .swap(last_elem_index, last_elem_index.saturating_sub(1));
2638 }
2639 }
2640
2641 completion_request
2642 }
2643
2644 pub fn cancel_last_assist(&mut self, cx: &mut Context<Self>) -> bool {
2645 if let Some(pending_completion) = self.pending_completions.pop() {
2646 self.update_metadata(pending_completion.assistant_message_id, cx, |metadata| {
2647 if metadata.status == MessageStatus::Pending {
2648 metadata.status = MessageStatus::Canceled;
2649 }
2650 });
2651 true
2652 } else {
2653 false
2654 }
2655 }
2656
2657 pub fn cycle_message_roles(&mut self, ids: HashSet<MessageId>, cx: &mut Context<Self>) {
2658 for id in &ids {
2659 if let Some(metadata) = self.messages_metadata.get(id) {
2660 let role = metadata.role.cycle();
2661 self.update_metadata(*id, cx, |metadata| metadata.role = role);
2662 }
2663 }
2664
2665 self.message_roles_updated(ids, cx);
2666 }
2667
2668 fn message_roles_updated(&mut self, ids: HashSet<MessageId>, cx: &mut Context<Self>) {
2669 let mut ranges = Vec::new();
2670 for message in self.messages(cx) {
2671 if ids.contains(&message.id) {
2672 ranges.push(message.anchor_range.clone());
2673 }
2674 }
2675
2676 let buffer = self.buffer.read(cx).text_snapshot();
2677 let mut updated = Vec::new();
2678 let mut removed = Vec::new();
2679 for range in ranges {
2680 self.reparse_patches_in_range(range, &buffer, &mut updated, &mut removed, cx);
2681 }
2682
2683 if !updated.is_empty() || !removed.is_empty() {
2684 cx.emit(ContextEvent::PatchesUpdated { removed, updated })
2685 }
2686 }
2687
2688 pub fn update_metadata(
2689 &mut self,
2690 id: MessageId,
2691 cx: &mut Context<Self>,
2692 f: impl FnOnce(&mut MessageMetadata),
2693 ) {
2694 let version = self.version.clone();
2695 let timestamp = self.next_timestamp();
2696 if let Some(metadata) = self.messages_metadata.get_mut(&id) {
2697 f(metadata);
2698 metadata.timestamp = timestamp;
2699 let operation = ContextOperation::UpdateMessage {
2700 message_id: id,
2701 metadata: metadata.clone(),
2702 version,
2703 };
2704 self.push_op(operation, cx);
2705 cx.emit(ContextEvent::MessagesEdited);
2706 cx.notify();
2707 }
2708 }
2709
2710 pub fn insert_message_after(
2711 &mut self,
2712 message_id: MessageId,
2713 role: Role,
2714 status: MessageStatus,
2715 cx: &mut Context<Self>,
2716 ) -> Option<MessageAnchor> {
2717 if let Some(prev_message_ix) = self
2718 .message_anchors
2719 .iter()
2720 .position(|message| message.id == message_id)
2721 {
2722 // Find the next valid message after the one we were given.
2723 let mut next_message_ix = prev_message_ix + 1;
2724 while let Some(next_message) = self.message_anchors.get(next_message_ix) {
2725 if next_message.start.is_valid(self.buffer.read(cx)) {
2726 break;
2727 }
2728 next_message_ix += 1;
2729 }
2730
2731 let buffer = self.buffer.read(cx);
2732 let offset = self
2733 .message_anchors
2734 .get(next_message_ix)
2735 .map_or(buffer.len(), |message| {
2736 buffer.clip_offset(message.start.to_offset(buffer) - 1, Bias::Left)
2737 });
2738 Some(self.insert_message_at_offset(offset, role, status, cx))
2739 } else {
2740 None
2741 }
2742 }
2743
2744 fn insert_message_at_offset(
2745 &mut self,
2746 offset: usize,
2747 role: Role,
2748 status: MessageStatus,
2749 cx: &mut Context<Self>,
2750 ) -> MessageAnchor {
2751 let start = self.buffer.update(cx, |buffer, cx| {
2752 buffer.edit([(offset..offset, "\n")], None, cx);
2753 buffer.anchor_before(offset + 1)
2754 });
2755
2756 let version = self.version.clone();
2757 let anchor = MessageAnchor {
2758 id: MessageId(self.next_timestamp()),
2759 start,
2760 };
2761 let metadata = MessageMetadata {
2762 role,
2763 status,
2764 timestamp: anchor.id.0,
2765 cache: None,
2766 };
2767 self.insert_message(anchor.clone(), metadata.clone(), cx);
2768 self.push_op(
2769 ContextOperation::InsertMessage {
2770 anchor: anchor.clone(),
2771 metadata,
2772 version,
2773 },
2774 cx,
2775 );
2776 anchor
2777 }
2778
2779 pub fn insert_content(&mut self, content: Content, cx: &mut Context<Self>) {
2780 let buffer = self.buffer.read(cx);
2781 let insertion_ix = match self
2782 .contents
2783 .binary_search_by(|probe| probe.cmp(&content, buffer))
2784 {
2785 Ok(ix) => {
2786 self.contents.remove(ix);
2787 ix
2788 }
2789 Err(ix) => ix,
2790 };
2791 self.contents.insert(insertion_ix, content);
2792 cx.emit(ContextEvent::MessagesEdited);
2793 }
2794
2795 pub fn contents<'a>(&'a self, cx: &'a App) -> impl 'a + Iterator<Item = Content> {
2796 let buffer = self.buffer.read(cx);
2797 self.contents
2798 .iter()
2799 .filter(|content| {
2800 let range = content.range();
2801 range.start.is_valid(buffer) && range.end.is_valid(buffer)
2802 })
2803 .cloned()
2804 }
2805
2806 pub fn split_message(
2807 &mut self,
2808 range: Range<usize>,
2809 cx: &mut Context<Self>,
2810 ) -> (Option<MessageAnchor>, Option<MessageAnchor>) {
2811 let start_message = self.message_for_offset(range.start, cx);
2812 let end_message = self.message_for_offset(range.end, cx);
2813 if let Some((start_message, end_message)) = start_message.zip(end_message) {
2814 // Prevent splitting when range spans multiple messages.
2815 if start_message.id != end_message.id {
2816 return (None, None);
2817 }
2818
2819 let message = start_message;
2820 let role = message.role;
2821 let mut edited_buffer = false;
2822
2823 let mut suffix_start = None;
2824
2825 // TODO: why did this start panicking?
2826 if range.start > message.offset_range.start
2827 && range.end < message.offset_range.end.saturating_sub(1)
2828 {
2829 if self.buffer.read(cx).chars_at(range.end).next() == Some('\n') {
2830 suffix_start = Some(range.end + 1);
2831 } else if self.buffer.read(cx).reversed_chars_at(range.end).next() == Some('\n') {
2832 suffix_start = Some(range.end);
2833 }
2834 }
2835
2836 let version = self.version.clone();
2837 let suffix = if let Some(suffix_start) = suffix_start {
2838 MessageAnchor {
2839 id: MessageId(self.next_timestamp()),
2840 start: self.buffer.read(cx).anchor_before(suffix_start),
2841 }
2842 } else {
2843 self.buffer.update(cx, |buffer, cx| {
2844 buffer.edit([(range.end..range.end, "\n")], None, cx);
2845 });
2846 edited_buffer = true;
2847 MessageAnchor {
2848 id: MessageId(self.next_timestamp()),
2849 start: self.buffer.read(cx).anchor_before(range.end + 1),
2850 }
2851 };
2852
2853 let suffix_metadata = MessageMetadata {
2854 role,
2855 status: MessageStatus::Done,
2856 timestamp: suffix.id.0,
2857 cache: None,
2858 };
2859 self.insert_message(suffix.clone(), suffix_metadata.clone(), cx);
2860 self.push_op(
2861 ContextOperation::InsertMessage {
2862 anchor: suffix.clone(),
2863 metadata: suffix_metadata,
2864 version,
2865 },
2866 cx,
2867 );
2868
2869 let new_messages =
2870 if range.start == range.end || range.start == message.offset_range.start {
2871 (None, Some(suffix))
2872 } else {
2873 let mut prefix_end = None;
2874 if range.start > message.offset_range.start
2875 && range.end < message.offset_range.end - 1
2876 {
2877 if self.buffer.read(cx).chars_at(range.start).next() == Some('\n') {
2878 prefix_end = Some(range.start + 1);
2879 } else if self.buffer.read(cx).reversed_chars_at(range.start).next()
2880 == Some('\n')
2881 {
2882 prefix_end = Some(range.start);
2883 }
2884 }
2885
2886 let version = self.version.clone();
2887 let selection = if let Some(prefix_end) = prefix_end {
2888 MessageAnchor {
2889 id: MessageId(self.next_timestamp()),
2890 start: self.buffer.read(cx).anchor_before(prefix_end),
2891 }
2892 } else {
2893 self.buffer.update(cx, |buffer, cx| {
2894 buffer.edit([(range.start..range.start, "\n")], None, cx)
2895 });
2896 edited_buffer = true;
2897 MessageAnchor {
2898 id: MessageId(self.next_timestamp()),
2899 start: self.buffer.read(cx).anchor_before(range.end + 1),
2900 }
2901 };
2902
2903 let selection_metadata = MessageMetadata {
2904 role,
2905 status: MessageStatus::Done,
2906 timestamp: selection.id.0,
2907 cache: None,
2908 };
2909 self.insert_message(selection.clone(), selection_metadata.clone(), cx);
2910 self.push_op(
2911 ContextOperation::InsertMessage {
2912 anchor: selection.clone(),
2913 metadata: selection_metadata,
2914 version,
2915 },
2916 cx,
2917 );
2918
2919 (Some(selection), Some(suffix))
2920 };
2921
2922 if !edited_buffer {
2923 cx.emit(ContextEvent::MessagesEdited);
2924 }
2925 new_messages
2926 } else {
2927 (None, None)
2928 }
2929 }
2930
2931 fn insert_message(
2932 &mut self,
2933 new_anchor: MessageAnchor,
2934 new_metadata: MessageMetadata,
2935 cx: &mut Context<Self>,
2936 ) {
2937 cx.emit(ContextEvent::MessagesEdited);
2938
2939 self.messages_metadata.insert(new_anchor.id, new_metadata);
2940
2941 let buffer = self.buffer.read(cx);
2942 let insertion_ix = self
2943 .message_anchors
2944 .iter()
2945 .position(|anchor| {
2946 let comparison = new_anchor.start.cmp(&anchor.start, buffer);
2947 comparison.is_lt() || (comparison.is_eq() && new_anchor.id > anchor.id)
2948 })
2949 .unwrap_or(self.message_anchors.len());
2950 self.message_anchors.insert(insertion_ix, new_anchor);
2951 }
2952
2953 pub fn summarize(&mut self, mut replace_old: bool, cx: &mut Context<Self>) {
2954 let Some(model) = LanguageModelRegistry::read_global(cx).default_model() else {
2955 return;
2956 };
2957
2958 if replace_old || (self.message_anchors.len() >= 2 && self.summary.is_none()) {
2959 if !model.provider.is_authenticated(cx) {
2960 return;
2961 }
2962
2963 let mut request = self.to_completion_request(RequestType::Chat, cx);
2964 request.messages.push(LanguageModelRequestMessage {
2965 role: Role::User,
2966 content: vec![
2967 "Generate a concise 3-7 word title for this conversation, omitting punctuation. Go straight to the title, without any preamble and prefix like `Here's a concise suggestion:...` or `Title:`"
2968 .into(),
2969 ],
2970 cache: false,
2971 });
2972
2973 // If there is no summary, it is set with `done: false` so that "Loading Summary…" can
2974 // be displayed.
2975 if self.summary.is_none() {
2976 self.summary = Some(ContextSummary {
2977 text: "".to_string(),
2978 done: false,
2979 timestamp: clock::Lamport::default(),
2980 });
2981 replace_old = true;
2982 }
2983
2984 self.summary_task = cx.spawn(async move |this, cx| {
2985 async move {
2986 let stream = model.model.stream_completion_text(request, &cx);
2987 let mut messages = stream.await?;
2988
2989 let mut replaced = !replace_old;
2990 while let Some(message) = messages.stream.next().await {
2991 let text = message?;
2992 let mut lines = text.lines();
2993 this.update(cx, |this, cx| {
2994 let version = this.version.clone();
2995 let timestamp = this.next_timestamp();
2996 let summary = this.summary.get_or_insert(ContextSummary::default());
2997 if !replaced && replace_old {
2998 summary.text.clear();
2999 replaced = true;
3000 }
3001 summary.text.extend(lines.next());
3002 summary.timestamp = timestamp;
3003 let operation = ContextOperation::UpdateSummary {
3004 summary: summary.clone(),
3005 version,
3006 };
3007 this.push_op(operation, cx);
3008 cx.emit(ContextEvent::SummaryChanged);
3009 cx.emit(ContextEvent::SummaryGenerated);
3010 })?;
3011
3012 // Stop if the LLM generated multiple lines.
3013 if lines.next().is_some() {
3014 break;
3015 }
3016 }
3017
3018 this.update(cx, |this, cx| {
3019 let version = this.version.clone();
3020 let timestamp = this.next_timestamp();
3021 if let Some(summary) = this.summary.as_mut() {
3022 summary.done = true;
3023 summary.timestamp = timestamp;
3024 let operation = ContextOperation::UpdateSummary {
3025 summary: summary.clone(),
3026 version,
3027 };
3028 this.push_op(operation, cx);
3029 cx.emit(ContextEvent::SummaryChanged);
3030 cx.emit(ContextEvent::SummaryGenerated);
3031 }
3032 })?;
3033
3034 anyhow::Ok(())
3035 }
3036 .log_err()
3037 .await
3038 });
3039 }
3040 }
3041
3042 fn message_for_offset(&self, offset: usize, cx: &App) -> Option<Message> {
3043 self.messages_for_offsets([offset], cx).pop()
3044 }
3045
3046 pub fn messages_for_offsets(
3047 &self,
3048 offsets: impl IntoIterator<Item = usize>,
3049 cx: &App,
3050 ) -> Vec<Message> {
3051 let mut result = Vec::new();
3052
3053 let mut messages = self.messages(cx).peekable();
3054 let mut offsets = offsets.into_iter().peekable();
3055 let mut current_message = messages.next();
3056 while let Some(offset) = offsets.next() {
3057 // Locate the message that contains the offset.
3058 while current_message.as_ref().map_or(false, |message| {
3059 !message.offset_range.contains(&offset) && messages.peek().is_some()
3060 }) {
3061 current_message = messages.next();
3062 }
3063 let Some(message) = current_message.as_ref() else {
3064 break;
3065 };
3066
3067 // Skip offsets that are in the same message.
3068 while offsets.peek().map_or(false, |offset| {
3069 message.offset_range.contains(offset) || messages.peek().is_none()
3070 }) {
3071 offsets.next();
3072 }
3073
3074 result.push(message.clone());
3075 }
3076 result
3077 }
3078
3079 fn messages_from_anchors<'a>(
3080 &'a self,
3081 message_anchors: impl Iterator<Item = &'a MessageAnchor> + 'a,
3082 cx: &'a App,
3083 ) -> impl 'a + Iterator<Item = Message> {
3084 let buffer = self.buffer.read(cx);
3085
3086 Self::messages_from_iters(buffer, &self.messages_metadata, message_anchors.enumerate())
3087 }
3088
3089 pub fn messages<'a>(&'a self, cx: &'a App) -> impl 'a + Iterator<Item = Message> {
3090 self.messages_from_anchors(self.message_anchors.iter(), cx)
3091 }
3092
3093 pub fn messages_from_iters<'a>(
3094 buffer: &'a Buffer,
3095 metadata: &'a HashMap<MessageId, MessageMetadata>,
3096 messages: impl Iterator<Item = (usize, &'a MessageAnchor)> + 'a,
3097 ) -> impl 'a + Iterator<Item = Message> {
3098 let mut messages = messages.peekable();
3099
3100 iter::from_fn(move || {
3101 if let Some((start_ix, message_anchor)) = messages.next() {
3102 let metadata = metadata.get(&message_anchor.id)?;
3103
3104 let message_start = message_anchor.start.to_offset(buffer);
3105 let mut message_end = None;
3106 let mut end_ix = start_ix;
3107 while let Some((_, next_message)) = messages.peek() {
3108 if next_message.start.is_valid(buffer) {
3109 message_end = Some(next_message.start);
3110 break;
3111 } else {
3112 end_ix += 1;
3113 messages.next();
3114 }
3115 }
3116 let message_end_anchor = message_end.unwrap_or(language::Anchor::MAX);
3117 let message_end = message_end_anchor.to_offset(buffer);
3118
3119 return Some(Message {
3120 index_range: start_ix..end_ix,
3121 offset_range: message_start..message_end,
3122 anchor_range: message_anchor.start..message_end_anchor,
3123 id: message_anchor.id,
3124 role: metadata.role,
3125 status: metadata.status.clone(),
3126 cache: metadata.cache.clone(),
3127 });
3128 }
3129 None
3130 })
3131 }
3132
3133 pub fn save(
3134 &mut self,
3135 debounce: Option<Duration>,
3136 fs: Arc<dyn Fs>,
3137 cx: &mut Context<AssistantContext>,
3138 ) {
3139 if self.replica_id() != ReplicaId::default() {
3140 // Prevent saving a remote context for now.
3141 return;
3142 }
3143
3144 self.pending_save = cx.spawn(async move |this, cx| {
3145 if let Some(debounce) = debounce {
3146 cx.background_executor().timer(debounce).await;
3147 }
3148
3149 let (old_path, summary) = this.read_with(cx, |this, _| {
3150 let path = this.path.clone();
3151 let summary = if let Some(summary) = this.summary.as_ref() {
3152 if summary.done {
3153 Some(summary.text.clone())
3154 } else {
3155 None
3156 }
3157 } else {
3158 None
3159 };
3160 (path, summary)
3161 })?;
3162
3163 if let Some(summary) = summary {
3164 let context = this.read_with(cx, |this, cx| this.serialize(cx))?;
3165 let mut discriminant = 1;
3166 let mut new_path;
3167 loop {
3168 new_path = contexts_dir().join(&format!(
3169 "{} - {}.zed.json",
3170 summary.trim(),
3171 discriminant
3172 ));
3173 if fs.is_file(&new_path).await {
3174 discriminant += 1;
3175 } else {
3176 break;
3177 }
3178 }
3179
3180 fs.create_dir(contexts_dir().as_ref()).await?;
3181 fs.atomic_write(new_path.clone(), serde_json::to_string(&context).unwrap())
3182 .await?;
3183 if let Some(old_path) = old_path {
3184 if new_path != old_path {
3185 fs.remove_file(
3186 &old_path,
3187 RemoveOptions {
3188 recursive: false,
3189 ignore_if_not_exists: true,
3190 },
3191 )
3192 .await?;
3193 }
3194 }
3195
3196 this.update(cx, |this, _| this.path = Some(new_path))?;
3197 }
3198
3199 Ok(())
3200 });
3201 }
3202
3203 pub fn set_custom_summary(&mut self, custom_summary: String, cx: &mut Context<Self>) {
3204 let timestamp = self.next_timestamp();
3205 let summary = self.summary.get_or_insert(ContextSummary::default());
3206 summary.timestamp = timestamp;
3207 summary.done = true;
3208 summary.text = custom_summary;
3209 cx.emit(ContextEvent::SummaryChanged);
3210 }
3211
3212 pub const DEFAULT_SUMMARY: SharedString = SharedString::new_static("New Text Thread");
3213
3214 pub fn summary_or_default(&self) -> SharedString {
3215 self.summary
3216 .as_ref()
3217 .map(|summary| summary.text.clone().into())
3218 .unwrap_or(Self::DEFAULT_SUMMARY)
3219 }
3220}
3221
3222fn trimmed_text_in_range(buffer: &BufferSnapshot, range: Range<text::Anchor>) -> String {
3223 let mut is_start = true;
3224 let mut content = buffer
3225 .text_for_range(range)
3226 .map(|mut chunk| {
3227 if is_start {
3228 chunk = chunk.trim_start_matches('\n');
3229 if !chunk.is_empty() {
3230 is_start = false;
3231 }
3232 }
3233 chunk
3234 })
3235 .collect::<String>();
3236 content.truncate(content.trim_end().len());
3237 content
3238}
3239
3240#[derive(Debug, Default)]
3241pub struct ContextVersion {
3242 context: clock::Global,
3243 buffer: clock::Global,
3244}
3245
3246impl ContextVersion {
3247 pub fn from_proto(proto: &proto::ContextVersion) -> Self {
3248 Self {
3249 context: language::proto::deserialize_version(&proto.context_version),
3250 buffer: language::proto::deserialize_version(&proto.buffer_version),
3251 }
3252 }
3253
3254 pub fn to_proto(&self, context_id: ContextId) -> proto::ContextVersion {
3255 proto::ContextVersion {
3256 context_id: context_id.to_proto(),
3257 context_version: language::proto::serialize_version(&self.context),
3258 buffer_version: language::proto::serialize_version(&self.buffer),
3259 }
3260 }
3261}
3262
3263#[derive(Debug, Clone)]
3264pub struct ParsedSlashCommand {
3265 pub name: String,
3266 pub arguments: SmallVec<[String; 3]>,
3267 pub status: PendingSlashCommandStatus,
3268 pub source_range: Range<language::Anchor>,
3269}
3270
3271#[derive(Debug)]
3272pub struct InvokedSlashCommand {
3273 pub name: SharedString,
3274 pub range: Range<language::Anchor>,
3275 pub run_commands_in_ranges: Vec<Range<language::Anchor>>,
3276 pub status: InvokedSlashCommandStatus,
3277 pub transaction: Option<language::TransactionId>,
3278 timestamp: clock::Lamport,
3279}
3280
3281#[derive(Debug)]
3282pub enum InvokedSlashCommandStatus {
3283 Running(Task<()>),
3284 Error(SharedString),
3285 Finished,
3286}
3287
3288#[derive(Debug, Clone)]
3289pub enum PendingSlashCommandStatus {
3290 Idle,
3291 Running { _task: Shared<Task<()>> },
3292 Error(String),
3293}
3294
3295#[derive(Debug, Clone)]
3296pub struct PendingToolUse {
3297 pub id: LanguageModelToolUseId,
3298 pub name: String,
3299 pub input: serde_json::Value,
3300 pub status: PendingToolUseStatus,
3301 pub source_range: Range<language::Anchor>,
3302}
3303
3304#[derive(Debug, Clone)]
3305pub enum PendingToolUseStatus {
3306 Idle,
3307 Running { _task: Shared<Task<()>> },
3308 Error(String),
3309}
3310
3311impl PendingToolUseStatus {
3312 pub fn is_idle(&self) -> bool {
3313 matches!(self, PendingToolUseStatus::Idle)
3314 }
3315}
3316
3317#[derive(Serialize, Deserialize)]
3318pub struct SavedMessage {
3319 pub id: MessageId,
3320 pub start: usize,
3321 pub metadata: MessageMetadata,
3322}
3323
3324#[derive(Serialize, Deserialize)]
3325pub struct SavedContext {
3326 pub id: Option<ContextId>,
3327 pub zed: String,
3328 pub version: String,
3329 pub text: String,
3330 pub messages: Vec<SavedMessage>,
3331 pub summary: String,
3332 pub slash_command_output_sections:
3333 Vec<assistant_slash_command::SlashCommandOutputSection<usize>>,
3334 #[serde(default)]
3335 pub thought_process_output_sections: Vec<ThoughtProcessOutputSection<usize>>,
3336}
3337
3338impl SavedContext {
3339 pub const VERSION: &'static str = "0.4.0";
3340
3341 pub fn from_json(json: &str) -> Result<Self> {
3342 let saved_context_json = serde_json::from_str::<serde_json::Value>(json)?;
3343 match saved_context_json
3344 .get("version")
3345 .ok_or_else(|| anyhow!("version not found"))?
3346 {
3347 serde_json::Value::String(version) => match version.as_str() {
3348 SavedContext::VERSION => {
3349 Ok(serde_json::from_value::<SavedContext>(saved_context_json)?)
3350 }
3351 SavedContextV0_3_0::VERSION => {
3352 let saved_context =
3353 serde_json::from_value::<SavedContextV0_3_0>(saved_context_json)?;
3354 Ok(saved_context.upgrade())
3355 }
3356 SavedContextV0_2_0::VERSION => {
3357 let saved_context =
3358 serde_json::from_value::<SavedContextV0_2_0>(saved_context_json)?;
3359 Ok(saved_context.upgrade())
3360 }
3361 SavedContextV0_1_0::VERSION => {
3362 let saved_context =
3363 serde_json::from_value::<SavedContextV0_1_0>(saved_context_json)?;
3364 Ok(saved_context.upgrade())
3365 }
3366 _ => Err(anyhow!("unrecognized saved context version: {}", version)),
3367 },
3368 _ => Err(anyhow!("version not found on saved context")),
3369 }
3370 }
3371
3372 fn into_ops(
3373 self,
3374 buffer: &Entity<Buffer>,
3375 cx: &mut Context<AssistantContext>,
3376 ) -> Vec<ContextOperation> {
3377 let mut operations = Vec::new();
3378 let mut version = clock::Global::new();
3379 let mut next_timestamp = clock::Lamport::new(ReplicaId::default());
3380
3381 let mut first_message_metadata = None;
3382 for message in self.messages {
3383 if message.id == MessageId(clock::Lamport::default()) {
3384 first_message_metadata = Some(message.metadata);
3385 } else {
3386 operations.push(ContextOperation::InsertMessage {
3387 anchor: MessageAnchor {
3388 id: message.id,
3389 start: buffer.read(cx).anchor_before(message.start),
3390 },
3391 metadata: MessageMetadata {
3392 role: message.metadata.role,
3393 status: message.metadata.status,
3394 timestamp: message.metadata.timestamp,
3395 cache: None,
3396 },
3397 version: version.clone(),
3398 });
3399 version.observe(message.id.0);
3400 next_timestamp.observe(message.id.0);
3401 }
3402 }
3403
3404 if let Some(metadata) = first_message_metadata {
3405 let timestamp = next_timestamp.tick();
3406 operations.push(ContextOperation::UpdateMessage {
3407 message_id: MessageId(clock::Lamport::default()),
3408 metadata: MessageMetadata {
3409 role: metadata.role,
3410 status: metadata.status,
3411 timestamp,
3412 cache: None,
3413 },
3414 version: version.clone(),
3415 });
3416 version.observe(timestamp);
3417 }
3418
3419 let buffer = buffer.read(cx);
3420 for section in self.slash_command_output_sections {
3421 let timestamp = next_timestamp.tick();
3422 operations.push(ContextOperation::SlashCommandOutputSectionAdded {
3423 timestamp,
3424 section: SlashCommandOutputSection {
3425 range: buffer.anchor_after(section.range.start)
3426 ..buffer.anchor_before(section.range.end),
3427 icon: section.icon,
3428 label: section.label,
3429 metadata: section.metadata,
3430 },
3431 version: version.clone(),
3432 });
3433
3434 version.observe(timestamp);
3435 }
3436
3437 for section in self.thought_process_output_sections {
3438 let timestamp = next_timestamp.tick();
3439 operations.push(ContextOperation::ThoughtProcessOutputSectionAdded {
3440 timestamp,
3441 section: ThoughtProcessOutputSection {
3442 range: buffer.anchor_after(section.range.start)
3443 ..buffer.anchor_before(section.range.end),
3444 },
3445 version: version.clone(),
3446 });
3447
3448 version.observe(timestamp);
3449 }
3450
3451 let timestamp = next_timestamp.tick();
3452 operations.push(ContextOperation::UpdateSummary {
3453 summary: ContextSummary {
3454 text: self.summary,
3455 done: true,
3456 timestamp,
3457 },
3458 version: version.clone(),
3459 });
3460 version.observe(timestamp);
3461
3462 operations
3463 }
3464}
3465
3466#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
3467struct SavedMessageIdPreV0_4_0(usize);
3468
3469#[derive(Serialize, Deserialize)]
3470struct SavedMessagePreV0_4_0 {
3471 id: SavedMessageIdPreV0_4_0,
3472 start: usize,
3473}
3474
3475#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
3476struct SavedMessageMetadataPreV0_4_0 {
3477 role: Role,
3478 status: MessageStatus,
3479}
3480
3481#[derive(Serialize, Deserialize)]
3482struct SavedContextV0_3_0 {
3483 id: Option<ContextId>,
3484 zed: String,
3485 version: String,
3486 text: String,
3487 messages: Vec<SavedMessagePreV0_4_0>,
3488 message_metadata: HashMap<SavedMessageIdPreV0_4_0, SavedMessageMetadataPreV0_4_0>,
3489 summary: String,
3490 slash_command_output_sections: Vec<assistant_slash_command::SlashCommandOutputSection<usize>>,
3491}
3492
3493impl SavedContextV0_3_0 {
3494 const VERSION: &'static str = "0.3.0";
3495
3496 fn upgrade(self) -> SavedContext {
3497 SavedContext {
3498 id: self.id,
3499 zed: self.zed,
3500 version: SavedContext::VERSION.into(),
3501 text: self.text,
3502 messages: self
3503 .messages
3504 .into_iter()
3505 .filter_map(|message| {
3506 let metadata = self.message_metadata.get(&message.id)?;
3507 let timestamp = clock::Lamport {
3508 replica_id: ReplicaId::default(),
3509 value: message.id.0 as u32,
3510 };
3511 Some(SavedMessage {
3512 id: MessageId(timestamp),
3513 start: message.start,
3514 metadata: MessageMetadata {
3515 role: metadata.role,
3516 status: metadata.status.clone(),
3517 timestamp,
3518 cache: None,
3519 },
3520 })
3521 })
3522 .collect(),
3523 summary: self.summary,
3524 slash_command_output_sections: self.slash_command_output_sections,
3525 thought_process_output_sections: Vec::new(),
3526 }
3527 }
3528}
3529
3530#[derive(Serialize, Deserialize)]
3531struct SavedContextV0_2_0 {
3532 id: Option<ContextId>,
3533 zed: String,
3534 version: String,
3535 text: String,
3536 messages: Vec<SavedMessagePreV0_4_0>,
3537 message_metadata: HashMap<SavedMessageIdPreV0_4_0, SavedMessageMetadataPreV0_4_0>,
3538 summary: String,
3539}
3540
3541impl SavedContextV0_2_0 {
3542 const VERSION: &'static str = "0.2.0";
3543
3544 fn upgrade(self) -> SavedContext {
3545 SavedContextV0_3_0 {
3546 id: self.id,
3547 zed: self.zed,
3548 version: SavedContextV0_3_0::VERSION.to_string(),
3549 text: self.text,
3550 messages: self.messages,
3551 message_metadata: self.message_metadata,
3552 summary: self.summary,
3553 slash_command_output_sections: Vec::new(),
3554 }
3555 .upgrade()
3556 }
3557}
3558
3559#[derive(Serialize, Deserialize)]
3560struct SavedContextV0_1_0 {
3561 id: Option<ContextId>,
3562 zed: String,
3563 version: String,
3564 text: String,
3565 messages: Vec<SavedMessagePreV0_4_0>,
3566 message_metadata: HashMap<SavedMessageIdPreV0_4_0, SavedMessageMetadataPreV0_4_0>,
3567 summary: String,
3568 api_url: Option<String>,
3569 model: OpenAiModel,
3570}
3571
3572impl SavedContextV0_1_0 {
3573 const VERSION: &'static str = "0.1.0";
3574
3575 fn upgrade(self) -> SavedContext {
3576 SavedContextV0_2_0 {
3577 id: self.id,
3578 zed: self.zed,
3579 version: SavedContextV0_2_0::VERSION.to_string(),
3580 text: self.text,
3581 messages: self.messages,
3582 message_metadata: self.message_metadata,
3583 summary: self.summary,
3584 }
3585 .upgrade()
3586 }
3587}
3588
3589#[derive(Debug, Clone)]
3590pub struct SavedContextMetadata {
3591 pub title: String,
3592 pub path: PathBuf,
3593 pub mtime: chrono::DateTime<chrono::Local>,
3594}