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, Write as _},
36 iter, mem,
37 ops::Range,
38 path::Path,
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, Debug, 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<Arc<Path>>,
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: Arc<Path>,
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<&Arc<Path>> {
1151 self.path.as_ref()
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::StatusUpdate { .. } => {}
2375 LanguageModelCompletionEvent::StartMessage { .. } => {}
2376 LanguageModelCompletionEvent::Stop(reason) => {
2377 stop_reason = reason;
2378 }
2379 LanguageModelCompletionEvent::Thinking { text: chunk, .. } => {
2380 if thought_process_stack.is_empty() {
2381 let start =
2382 buffer.anchor_before(message_old_end_offset);
2383 thought_process_stack.push(start);
2384 let chunk =
2385 format!("{THOUGHT_PROCESS_START_MARKER}{chunk}{THOUGHT_PROCESS_END_MARKER}");
2386 let chunk_len = chunk.len();
2387 buffer.edit(
2388 [(
2389 message_old_end_offset..message_old_end_offset,
2390 chunk,
2391 )],
2392 None,
2393 cx,
2394 );
2395 let end = buffer
2396 .anchor_before(message_old_end_offset + chunk_len);
2397 context_event = Some(
2398 ContextEvent::StartedThoughtProcess(start..end),
2399 );
2400 } else {
2401 // This ensures that all the thinking chunks are inserted inside the thinking tag
2402 let insertion_position =
2403 message_old_end_offset - THOUGHT_PROCESS_END_MARKER.len();
2404 buffer.edit(
2405 [(insertion_position..insertion_position, chunk)],
2406 None,
2407 cx,
2408 );
2409 }
2410 }
2411 LanguageModelCompletionEvent::Text(mut chunk) => {
2412 if let Some(start) = thought_process_stack.pop() {
2413 let end = buffer.anchor_before(message_old_end_offset);
2414 context_event =
2415 Some(ContextEvent::EndedThoughtProcess(end));
2416 thought_process_output_section =
2417 Some(ThoughtProcessOutputSection {
2418 range: start..end,
2419 });
2420 chunk.insert_str(0, "\n\n");
2421 }
2422
2423 buffer.edit(
2424 [(
2425 message_old_end_offset..message_old_end_offset,
2426 chunk,
2427 )],
2428 None,
2429 cx,
2430 );
2431 }
2432 LanguageModelCompletionEvent::ToolUse(_) |
2433 LanguageModelCompletionEvent::UsageUpdate(_) => {}
2434 }
2435 });
2436
2437 if let Some(section) = thought_process_output_section.take() {
2438 this.insert_thought_process_output_section(section, cx);
2439 }
2440 if let Some(context_event) = context_event.take() {
2441 cx.emit(context_event);
2442 }
2443
2444 cx.emit(ContextEvent::StreamedCompletion);
2445
2446 Some(())
2447 })?;
2448 smol::future::yield_now().await;
2449 }
2450 this.update(cx, |this, cx| {
2451 this.pending_completions
2452 .retain(|completion| completion.id != pending_completion_id);
2453 this.summarize(false, cx);
2454 this.update_cache_status_for_completion(cx);
2455 })?;
2456
2457 anyhow::Ok(stop_reason)
2458 };
2459
2460 let result = stream_completion.await;
2461
2462 this.update(cx, |this, cx| {
2463 let error_message = if let Some(error) = result.as_ref().err() {
2464 if error.is::<PaymentRequiredError>() {
2465 cx.emit(ContextEvent::ShowPaymentRequiredError);
2466 this.update_metadata(assistant_message_id, cx, |metadata| {
2467 metadata.status = MessageStatus::Canceled;
2468 });
2469 Some(error.to_string())
2470 } else if error.is::<MaxMonthlySpendReachedError>() {
2471 cx.emit(ContextEvent::ShowMaxMonthlySpendReachedError);
2472 this.update_metadata(assistant_message_id, cx, |metadata| {
2473 metadata.status = MessageStatus::Canceled;
2474 });
2475 Some(error.to_string())
2476 } else {
2477 let error_message = error
2478 .chain()
2479 .map(|err| err.to_string())
2480 .collect::<Vec<_>>()
2481 .join("\n");
2482 cx.emit(ContextEvent::ShowAssistError(SharedString::from(
2483 error_message.clone(),
2484 )));
2485 this.update_metadata(assistant_message_id, cx, |metadata| {
2486 metadata.status =
2487 MessageStatus::Error(SharedString::from(error_message.clone()));
2488 });
2489 Some(error_message)
2490 }
2491 } else {
2492 this.update_metadata(assistant_message_id, cx, |metadata| {
2493 metadata.status = MessageStatus::Done;
2494 });
2495 None
2496 };
2497
2498 let language_name = this
2499 .buffer
2500 .read(cx)
2501 .language()
2502 .map(|language| language.name());
2503 report_assistant_event(
2504 AssistantEventData {
2505 conversation_id: Some(this.id.0.clone()),
2506 kind: AssistantKind::Panel,
2507 phase: AssistantPhase::Response,
2508 message_id: None,
2509 model: model.telemetry_id(),
2510 model_provider: model.provider_id().to_string(),
2511 response_latency,
2512 error_message,
2513 language_name: language_name.map(|name| name.to_proto()),
2514 },
2515 this.telemetry.clone(),
2516 cx.http_client(),
2517 model.api_key(cx),
2518 cx.background_executor(),
2519 );
2520
2521 if let Ok(stop_reason) = result {
2522 match stop_reason {
2523 StopReason::ToolUse => {}
2524 StopReason::EndTurn => {}
2525 StopReason::MaxTokens => {}
2526 }
2527 }
2528 })
2529 .ok();
2530 }
2531 });
2532
2533 self.pending_completions.push(PendingCompletion {
2534 id: pending_completion_id,
2535 assistant_message_id: assistant_message.id,
2536 _task: task,
2537 });
2538
2539 Some(user_message)
2540 }
2541
2542 pub fn to_xml(&self, cx: &App) -> String {
2543 let mut output = String::new();
2544 let buffer = self.buffer.read(cx);
2545 for message in self.messages(cx) {
2546 if message.status != MessageStatus::Done {
2547 continue;
2548 }
2549
2550 writeln!(&mut output, "<{}>", message.role).unwrap();
2551 for chunk in buffer.text_for_range(message.offset_range) {
2552 output.push_str(chunk);
2553 }
2554 if !output.ends_with('\n') {
2555 output.push('\n');
2556 }
2557 writeln!(&mut output, "</{}>", message.role).unwrap();
2558 }
2559 output
2560 }
2561
2562 pub fn to_completion_request(
2563 &self,
2564 request_type: RequestType,
2565 cx: &App,
2566 ) -> LanguageModelRequest {
2567 let buffer = self.buffer.read(cx);
2568
2569 let mut contents = self.contents(cx).peekable();
2570
2571 fn collect_text_content(buffer: &Buffer, range: Range<usize>) -> Option<String> {
2572 let text: String = buffer.text_for_range(range.clone()).collect();
2573 if text.trim().is_empty() {
2574 None
2575 } else {
2576 Some(text)
2577 }
2578 }
2579
2580 let mut completion_request = LanguageModelRequest {
2581 thread_id: None,
2582 prompt_id: None,
2583 mode: None,
2584 messages: Vec::new(),
2585 tools: Vec::new(),
2586 stop: Vec::new(),
2587 temperature: None,
2588 };
2589 for message in self.messages(cx) {
2590 if message.status != MessageStatus::Done {
2591 continue;
2592 }
2593
2594 let mut offset = message.offset_range.start;
2595 let mut request_message = LanguageModelRequestMessage {
2596 role: message.role,
2597 content: Vec::new(),
2598 cache: message
2599 .cache
2600 .as_ref()
2601 .map_or(false, |cache| cache.is_anchor),
2602 };
2603
2604 while let Some(content) = contents.peek() {
2605 if content
2606 .range()
2607 .end
2608 .cmp(&message.anchor_range.end, buffer)
2609 .is_lt()
2610 {
2611 let content = contents.next().unwrap();
2612 let range = content.range().to_offset(buffer);
2613 request_message.content.extend(
2614 collect_text_content(buffer, offset..range.start).map(MessageContent::Text),
2615 );
2616
2617 match content {
2618 Content::Image { image, .. } => {
2619 if let Some(image) = image.clone().now_or_never().flatten() {
2620 request_message
2621 .content
2622 .push(language_model::MessageContent::Image(image));
2623 }
2624 }
2625 }
2626
2627 offset = range.end;
2628 } else {
2629 break;
2630 }
2631 }
2632
2633 request_message.content.extend(
2634 collect_text_content(buffer, offset..message.offset_range.end)
2635 .map(MessageContent::Text),
2636 );
2637
2638 if !request_message.contents_empty() {
2639 completion_request.messages.push(request_message);
2640 }
2641 }
2642
2643 if let RequestType::SuggestEdits = request_type {
2644 if let Ok(preamble) = self.prompt_builder.generate_suggest_edits_prompt() {
2645 let last_elem_index = completion_request.messages.len();
2646
2647 completion_request
2648 .messages
2649 .push(LanguageModelRequestMessage {
2650 role: Role::User,
2651 content: vec![MessageContent::Text(preamble)],
2652 cache: false,
2653 });
2654
2655 // The preamble message should be sent right before the last actual user message.
2656 completion_request
2657 .messages
2658 .swap(last_elem_index, last_elem_index.saturating_sub(1));
2659 }
2660 }
2661
2662 completion_request
2663 }
2664
2665 pub fn cancel_last_assist(&mut self, cx: &mut Context<Self>) -> bool {
2666 if let Some(pending_completion) = self.pending_completions.pop() {
2667 self.update_metadata(pending_completion.assistant_message_id, cx, |metadata| {
2668 if metadata.status == MessageStatus::Pending {
2669 metadata.status = MessageStatus::Canceled;
2670 }
2671 });
2672 true
2673 } else {
2674 false
2675 }
2676 }
2677
2678 pub fn cycle_message_roles(&mut self, ids: HashSet<MessageId>, cx: &mut Context<Self>) {
2679 for id in &ids {
2680 if let Some(metadata) = self.messages_metadata.get(id) {
2681 let role = metadata.role.cycle();
2682 self.update_metadata(*id, cx, |metadata| metadata.role = role);
2683 }
2684 }
2685
2686 self.message_roles_updated(ids, cx);
2687 }
2688
2689 fn message_roles_updated(&mut self, ids: HashSet<MessageId>, cx: &mut Context<Self>) {
2690 let mut ranges = Vec::new();
2691 for message in self.messages(cx) {
2692 if ids.contains(&message.id) {
2693 ranges.push(message.anchor_range.clone());
2694 }
2695 }
2696
2697 let buffer = self.buffer.read(cx).text_snapshot();
2698 let mut updated = Vec::new();
2699 let mut removed = Vec::new();
2700 for range in ranges {
2701 self.reparse_patches_in_range(range, &buffer, &mut updated, &mut removed, cx);
2702 }
2703
2704 if !updated.is_empty() || !removed.is_empty() {
2705 cx.emit(ContextEvent::PatchesUpdated { removed, updated })
2706 }
2707 }
2708
2709 pub fn update_metadata(
2710 &mut self,
2711 id: MessageId,
2712 cx: &mut Context<Self>,
2713 f: impl FnOnce(&mut MessageMetadata),
2714 ) {
2715 let version = self.version.clone();
2716 let timestamp = self.next_timestamp();
2717 if let Some(metadata) = self.messages_metadata.get_mut(&id) {
2718 f(metadata);
2719 metadata.timestamp = timestamp;
2720 let operation = ContextOperation::UpdateMessage {
2721 message_id: id,
2722 metadata: metadata.clone(),
2723 version,
2724 };
2725 self.push_op(operation, cx);
2726 cx.emit(ContextEvent::MessagesEdited);
2727 cx.notify();
2728 }
2729 }
2730
2731 pub fn insert_message_after(
2732 &mut self,
2733 message_id: MessageId,
2734 role: Role,
2735 status: MessageStatus,
2736 cx: &mut Context<Self>,
2737 ) -> Option<MessageAnchor> {
2738 if let Some(prev_message_ix) = self
2739 .message_anchors
2740 .iter()
2741 .position(|message| message.id == message_id)
2742 {
2743 // Find the next valid message after the one we were given.
2744 let mut next_message_ix = prev_message_ix + 1;
2745 while let Some(next_message) = self.message_anchors.get(next_message_ix) {
2746 if next_message.start.is_valid(self.buffer.read(cx)) {
2747 break;
2748 }
2749 next_message_ix += 1;
2750 }
2751
2752 let buffer = self.buffer.read(cx);
2753 let offset = self
2754 .message_anchors
2755 .get(next_message_ix)
2756 .map_or(buffer.len(), |message| {
2757 buffer.clip_offset(message.start.to_offset(buffer) - 1, Bias::Left)
2758 });
2759 Some(self.insert_message_at_offset(offset, role, status, cx))
2760 } else {
2761 None
2762 }
2763 }
2764
2765 fn insert_message_at_offset(
2766 &mut self,
2767 offset: usize,
2768 role: Role,
2769 status: MessageStatus,
2770 cx: &mut Context<Self>,
2771 ) -> MessageAnchor {
2772 let start = self.buffer.update(cx, |buffer, cx| {
2773 buffer.edit([(offset..offset, "\n")], None, cx);
2774 buffer.anchor_before(offset + 1)
2775 });
2776
2777 let version = self.version.clone();
2778 let anchor = MessageAnchor {
2779 id: MessageId(self.next_timestamp()),
2780 start,
2781 };
2782 let metadata = MessageMetadata {
2783 role,
2784 status,
2785 timestamp: anchor.id.0,
2786 cache: None,
2787 };
2788 self.insert_message(anchor.clone(), metadata.clone(), cx);
2789 self.push_op(
2790 ContextOperation::InsertMessage {
2791 anchor: anchor.clone(),
2792 metadata,
2793 version,
2794 },
2795 cx,
2796 );
2797 anchor
2798 }
2799
2800 pub fn insert_content(&mut self, content: Content, cx: &mut Context<Self>) {
2801 let buffer = self.buffer.read(cx);
2802 let insertion_ix = match self
2803 .contents
2804 .binary_search_by(|probe| probe.cmp(&content, buffer))
2805 {
2806 Ok(ix) => {
2807 self.contents.remove(ix);
2808 ix
2809 }
2810 Err(ix) => ix,
2811 };
2812 self.contents.insert(insertion_ix, content);
2813 cx.emit(ContextEvent::MessagesEdited);
2814 }
2815
2816 pub fn contents<'a>(&'a self, cx: &'a App) -> impl 'a + Iterator<Item = Content> {
2817 let buffer = self.buffer.read(cx);
2818 self.contents
2819 .iter()
2820 .filter(|content| {
2821 let range = content.range();
2822 range.start.is_valid(buffer) && range.end.is_valid(buffer)
2823 })
2824 .cloned()
2825 }
2826
2827 pub fn split_message(
2828 &mut self,
2829 range: Range<usize>,
2830 cx: &mut Context<Self>,
2831 ) -> (Option<MessageAnchor>, Option<MessageAnchor>) {
2832 let start_message = self.message_for_offset(range.start, cx);
2833 let end_message = self.message_for_offset(range.end, cx);
2834 if let Some((start_message, end_message)) = start_message.zip(end_message) {
2835 // Prevent splitting when range spans multiple messages.
2836 if start_message.id != end_message.id {
2837 return (None, None);
2838 }
2839
2840 let message = start_message;
2841 let role = message.role;
2842 let mut edited_buffer = false;
2843
2844 let mut suffix_start = None;
2845
2846 // TODO: why did this start panicking?
2847 if range.start > message.offset_range.start
2848 && range.end < message.offset_range.end.saturating_sub(1)
2849 {
2850 if self.buffer.read(cx).chars_at(range.end).next() == Some('\n') {
2851 suffix_start = Some(range.end + 1);
2852 } else if self.buffer.read(cx).reversed_chars_at(range.end).next() == Some('\n') {
2853 suffix_start = Some(range.end);
2854 }
2855 }
2856
2857 let version = self.version.clone();
2858 let suffix = if let Some(suffix_start) = suffix_start {
2859 MessageAnchor {
2860 id: MessageId(self.next_timestamp()),
2861 start: self.buffer.read(cx).anchor_before(suffix_start),
2862 }
2863 } else {
2864 self.buffer.update(cx, |buffer, cx| {
2865 buffer.edit([(range.end..range.end, "\n")], None, cx);
2866 });
2867 edited_buffer = true;
2868 MessageAnchor {
2869 id: MessageId(self.next_timestamp()),
2870 start: self.buffer.read(cx).anchor_before(range.end + 1),
2871 }
2872 };
2873
2874 let suffix_metadata = MessageMetadata {
2875 role,
2876 status: MessageStatus::Done,
2877 timestamp: suffix.id.0,
2878 cache: None,
2879 };
2880 self.insert_message(suffix.clone(), suffix_metadata.clone(), cx);
2881 self.push_op(
2882 ContextOperation::InsertMessage {
2883 anchor: suffix.clone(),
2884 metadata: suffix_metadata,
2885 version,
2886 },
2887 cx,
2888 );
2889
2890 let new_messages =
2891 if range.start == range.end || range.start == message.offset_range.start {
2892 (None, Some(suffix))
2893 } else {
2894 let mut prefix_end = None;
2895 if range.start > message.offset_range.start
2896 && range.end < message.offset_range.end - 1
2897 {
2898 if self.buffer.read(cx).chars_at(range.start).next() == Some('\n') {
2899 prefix_end = Some(range.start + 1);
2900 } else if self.buffer.read(cx).reversed_chars_at(range.start).next()
2901 == Some('\n')
2902 {
2903 prefix_end = Some(range.start);
2904 }
2905 }
2906
2907 let version = self.version.clone();
2908 let selection = if let Some(prefix_end) = prefix_end {
2909 MessageAnchor {
2910 id: MessageId(self.next_timestamp()),
2911 start: self.buffer.read(cx).anchor_before(prefix_end),
2912 }
2913 } else {
2914 self.buffer.update(cx, |buffer, cx| {
2915 buffer.edit([(range.start..range.start, "\n")], None, cx)
2916 });
2917 edited_buffer = true;
2918 MessageAnchor {
2919 id: MessageId(self.next_timestamp()),
2920 start: self.buffer.read(cx).anchor_before(range.end + 1),
2921 }
2922 };
2923
2924 let selection_metadata = MessageMetadata {
2925 role,
2926 status: MessageStatus::Done,
2927 timestamp: selection.id.0,
2928 cache: None,
2929 };
2930 self.insert_message(selection.clone(), selection_metadata.clone(), cx);
2931 self.push_op(
2932 ContextOperation::InsertMessage {
2933 anchor: selection.clone(),
2934 metadata: selection_metadata,
2935 version,
2936 },
2937 cx,
2938 );
2939
2940 (Some(selection), Some(suffix))
2941 };
2942
2943 if !edited_buffer {
2944 cx.emit(ContextEvent::MessagesEdited);
2945 }
2946 new_messages
2947 } else {
2948 (None, None)
2949 }
2950 }
2951
2952 fn insert_message(
2953 &mut self,
2954 new_anchor: MessageAnchor,
2955 new_metadata: MessageMetadata,
2956 cx: &mut Context<Self>,
2957 ) {
2958 cx.emit(ContextEvent::MessagesEdited);
2959
2960 self.messages_metadata.insert(new_anchor.id, new_metadata);
2961
2962 let buffer = self.buffer.read(cx);
2963 let insertion_ix = self
2964 .message_anchors
2965 .iter()
2966 .position(|anchor| {
2967 let comparison = new_anchor.start.cmp(&anchor.start, buffer);
2968 comparison.is_lt() || (comparison.is_eq() && new_anchor.id > anchor.id)
2969 })
2970 .unwrap_or(self.message_anchors.len());
2971 self.message_anchors.insert(insertion_ix, new_anchor);
2972 }
2973
2974 pub fn summarize(&mut self, mut replace_old: bool, cx: &mut Context<Self>) {
2975 let Some(model) = LanguageModelRegistry::read_global(cx).default_model() else {
2976 return;
2977 };
2978
2979 if replace_old || (self.message_anchors.len() >= 2 && self.summary.is_none()) {
2980 if !model.provider.is_authenticated(cx) {
2981 return;
2982 }
2983
2984 let mut request = self.to_completion_request(RequestType::Chat, cx);
2985 request.messages.push(LanguageModelRequestMessage {
2986 role: Role::User,
2987 content: vec![
2988 "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:`"
2989 .into(),
2990 ],
2991 cache: false,
2992 });
2993
2994 // If there is no summary, it is set with `done: false` so that "Loading Summary…" can
2995 // be displayed.
2996 if self.summary.is_none() {
2997 self.summary = Some(ContextSummary {
2998 text: "".to_string(),
2999 done: false,
3000 timestamp: clock::Lamport::default(),
3001 });
3002 replace_old = true;
3003 }
3004
3005 self.summary_task = cx.spawn(async move |this, cx| {
3006 async move {
3007 let stream = model.model.stream_completion_text(request, &cx);
3008 let mut messages = stream.await?;
3009
3010 let mut replaced = !replace_old;
3011 while let Some(message) = messages.stream.next().await {
3012 let text = message?;
3013 let mut lines = text.lines();
3014 this.update(cx, |this, cx| {
3015 let version = this.version.clone();
3016 let timestamp = this.next_timestamp();
3017 let summary = this.summary.get_or_insert(ContextSummary::default());
3018 if !replaced && replace_old {
3019 summary.text.clear();
3020 replaced = true;
3021 }
3022 summary.text.extend(lines.next());
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 // Stop if the LLM generated multiple lines.
3034 if lines.next().is_some() {
3035 break;
3036 }
3037 }
3038
3039 this.update(cx, |this, cx| {
3040 let version = this.version.clone();
3041 let timestamp = this.next_timestamp();
3042 if let Some(summary) = this.summary.as_mut() {
3043 summary.done = true;
3044 summary.timestamp = timestamp;
3045 let operation = ContextOperation::UpdateSummary {
3046 summary: summary.clone(),
3047 version,
3048 };
3049 this.push_op(operation, cx);
3050 cx.emit(ContextEvent::SummaryChanged);
3051 cx.emit(ContextEvent::SummaryGenerated);
3052 }
3053 })?;
3054
3055 anyhow::Ok(())
3056 }
3057 .log_err()
3058 .await
3059 });
3060 }
3061 }
3062
3063 fn message_for_offset(&self, offset: usize, cx: &App) -> Option<Message> {
3064 self.messages_for_offsets([offset], cx).pop()
3065 }
3066
3067 pub fn messages_for_offsets(
3068 &self,
3069 offsets: impl IntoIterator<Item = usize>,
3070 cx: &App,
3071 ) -> Vec<Message> {
3072 let mut result = Vec::new();
3073
3074 let mut messages = self.messages(cx).peekable();
3075 let mut offsets = offsets.into_iter().peekable();
3076 let mut current_message = messages.next();
3077 while let Some(offset) = offsets.next() {
3078 // Locate the message that contains the offset.
3079 while current_message.as_ref().map_or(false, |message| {
3080 !message.offset_range.contains(&offset) && messages.peek().is_some()
3081 }) {
3082 current_message = messages.next();
3083 }
3084 let Some(message) = current_message.as_ref() else {
3085 break;
3086 };
3087
3088 // Skip offsets that are in the same message.
3089 while offsets.peek().map_or(false, |offset| {
3090 message.offset_range.contains(offset) || messages.peek().is_none()
3091 }) {
3092 offsets.next();
3093 }
3094
3095 result.push(message.clone());
3096 }
3097 result
3098 }
3099
3100 fn messages_from_anchors<'a>(
3101 &'a self,
3102 message_anchors: impl Iterator<Item = &'a MessageAnchor> + 'a,
3103 cx: &'a App,
3104 ) -> impl 'a + Iterator<Item = Message> {
3105 let buffer = self.buffer.read(cx);
3106
3107 Self::messages_from_iters(buffer, &self.messages_metadata, message_anchors.enumerate())
3108 }
3109
3110 pub fn messages<'a>(&'a self, cx: &'a App) -> impl 'a + Iterator<Item = Message> {
3111 self.messages_from_anchors(self.message_anchors.iter(), cx)
3112 }
3113
3114 pub fn messages_from_iters<'a>(
3115 buffer: &'a Buffer,
3116 metadata: &'a HashMap<MessageId, MessageMetadata>,
3117 messages: impl Iterator<Item = (usize, &'a MessageAnchor)> + 'a,
3118 ) -> impl 'a + Iterator<Item = Message> {
3119 let mut messages = messages.peekable();
3120
3121 iter::from_fn(move || {
3122 if let Some((start_ix, message_anchor)) = messages.next() {
3123 let metadata = metadata.get(&message_anchor.id)?;
3124
3125 let message_start = message_anchor.start.to_offset(buffer);
3126 let mut message_end = None;
3127 let mut end_ix = start_ix;
3128 while let Some((_, next_message)) = messages.peek() {
3129 if next_message.start.is_valid(buffer) {
3130 message_end = Some(next_message.start);
3131 break;
3132 } else {
3133 end_ix += 1;
3134 messages.next();
3135 }
3136 }
3137 let message_end_anchor = message_end.unwrap_or(language::Anchor::MAX);
3138 let message_end = message_end_anchor.to_offset(buffer);
3139
3140 return Some(Message {
3141 index_range: start_ix..end_ix,
3142 offset_range: message_start..message_end,
3143 anchor_range: message_anchor.start..message_end_anchor,
3144 id: message_anchor.id,
3145 role: metadata.role,
3146 status: metadata.status.clone(),
3147 cache: metadata.cache.clone(),
3148 });
3149 }
3150 None
3151 })
3152 }
3153
3154 pub fn save(
3155 &mut self,
3156 debounce: Option<Duration>,
3157 fs: Arc<dyn Fs>,
3158 cx: &mut Context<AssistantContext>,
3159 ) {
3160 if self.replica_id() != ReplicaId::default() {
3161 // Prevent saving a remote context for now.
3162 return;
3163 }
3164
3165 self.pending_save = cx.spawn(async move |this, cx| {
3166 if let Some(debounce) = debounce {
3167 cx.background_executor().timer(debounce).await;
3168 }
3169
3170 let (old_path, summary) = this.read_with(cx, |this, _| {
3171 let path = this.path.clone();
3172 let summary = if let Some(summary) = this.summary.as_ref() {
3173 if summary.done {
3174 Some(summary.text.clone())
3175 } else {
3176 None
3177 }
3178 } else {
3179 None
3180 };
3181 (path, summary)
3182 })?;
3183
3184 if let Some(summary) = summary {
3185 let context = this.read_with(cx, |this, cx| this.serialize(cx))?;
3186 let mut discriminant = 1;
3187 let mut new_path;
3188 loop {
3189 new_path = contexts_dir().join(&format!(
3190 "{} - {}.zed.json",
3191 summary.trim(),
3192 discriminant
3193 ));
3194 if fs.is_file(&new_path).await {
3195 discriminant += 1;
3196 } else {
3197 break;
3198 }
3199 }
3200
3201 fs.create_dir(contexts_dir().as_ref()).await?;
3202 fs.atomic_write(new_path.clone(), serde_json::to_string(&context).unwrap())
3203 .await?;
3204 if let Some(old_path) = old_path {
3205 if new_path.as_path() != old_path.as_ref() {
3206 fs.remove_file(
3207 &old_path,
3208 RemoveOptions {
3209 recursive: false,
3210 ignore_if_not_exists: true,
3211 },
3212 )
3213 .await?;
3214 }
3215 }
3216
3217 this.update(cx, |this, _| this.path = Some(new_path.into()))?;
3218 }
3219
3220 Ok(())
3221 });
3222 }
3223
3224 pub fn set_custom_summary(&mut self, custom_summary: String, cx: &mut Context<Self>) {
3225 let timestamp = self.next_timestamp();
3226 let summary = self.summary.get_or_insert(ContextSummary::default());
3227 summary.timestamp = timestamp;
3228 summary.done = true;
3229 summary.text = custom_summary;
3230 cx.emit(ContextEvent::SummaryChanged);
3231 }
3232
3233 pub const DEFAULT_SUMMARY: SharedString = SharedString::new_static("New Text Thread");
3234
3235 pub fn summary_or_default(&self) -> SharedString {
3236 self.summary
3237 .as_ref()
3238 .map(|summary| summary.text.clone().into())
3239 .unwrap_or(Self::DEFAULT_SUMMARY)
3240 }
3241}
3242
3243fn trimmed_text_in_range(buffer: &BufferSnapshot, range: Range<text::Anchor>) -> String {
3244 let mut is_start = true;
3245 let mut content = buffer
3246 .text_for_range(range)
3247 .map(|mut chunk| {
3248 if is_start {
3249 chunk = chunk.trim_start_matches('\n');
3250 if !chunk.is_empty() {
3251 is_start = false;
3252 }
3253 }
3254 chunk
3255 })
3256 .collect::<String>();
3257 content.truncate(content.trim_end().len());
3258 content
3259}
3260
3261#[derive(Debug, Default)]
3262pub struct ContextVersion {
3263 context: clock::Global,
3264 buffer: clock::Global,
3265}
3266
3267impl ContextVersion {
3268 pub fn from_proto(proto: &proto::ContextVersion) -> Self {
3269 Self {
3270 context: language::proto::deserialize_version(&proto.context_version),
3271 buffer: language::proto::deserialize_version(&proto.buffer_version),
3272 }
3273 }
3274
3275 pub fn to_proto(&self, context_id: ContextId) -> proto::ContextVersion {
3276 proto::ContextVersion {
3277 context_id: context_id.to_proto(),
3278 context_version: language::proto::serialize_version(&self.context),
3279 buffer_version: language::proto::serialize_version(&self.buffer),
3280 }
3281 }
3282}
3283
3284#[derive(Debug, Clone)]
3285pub struct ParsedSlashCommand {
3286 pub name: String,
3287 pub arguments: SmallVec<[String; 3]>,
3288 pub status: PendingSlashCommandStatus,
3289 pub source_range: Range<language::Anchor>,
3290}
3291
3292#[derive(Debug)]
3293pub struct InvokedSlashCommand {
3294 pub name: SharedString,
3295 pub range: Range<language::Anchor>,
3296 pub run_commands_in_ranges: Vec<Range<language::Anchor>>,
3297 pub status: InvokedSlashCommandStatus,
3298 pub transaction: Option<language::TransactionId>,
3299 timestamp: clock::Lamport,
3300}
3301
3302#[derive(Debug)]
3303pub enum InvokedSlashCommandStatus {
3304 Running(Task<()>),
3305 Error(SharedString),
3306 Finished,
3307}
3308
3309#[derive(Debug, Clone)]
3310pub enum PendingSlashCommandStatus {
3311 Idle,
3312 Running { _task: Shared<Task<()>> },
3313 Error(String),
3314}
3315
3316#[derive(Debug, Clone)]
3317pub struct PendingToolUse {
3318 pub id: LanguageModelToolUseId,
3319 pub name: String,
3320 pub input: serde_json::Value,
3321 pub status: PendingToolUseStatus,
3322 pub source_range: Range<language::Anchor>,
3323}
3324
3325#[derive(Debug, Clone)]
3326pub enum PendingToolUseStatus {
3327 Idle,
3328 Running { _task: Shared<Task<()>> },
3329 Error(String),
3330}
3331
3332impl PendingToolUseStatus {
3333 pub fn is_idle(&self) -> bool {
3334 matches!(self, PendingToolUseStatus::Idle)
3335 }
3336}
3337
3338#[derive(Serialize, Deserialize)]
3339pub struct SavedMessage {
3340 pub id: MessageId,
3341 pub start: usize,
3342 pub metadata: MessageMetadata,
3343}
3344
3345#[derive(Serialize, Deserialize)]
3346pub struct SavedContext {
3347 pub id: Option<ContextId>,
3348 pub zed: String,
3349 pub version: String,
3350 pub text: String,
3351 pub messages: Vec<SavedMessage>,
3352 pub summary: String,
3353 pub slash_command_output_sections:
3354 Vec<assistant_slash_command::SlashCommandOutputSection<usize>>,
3355 #[serde(default)]
3356 pub thought_process_output_sections: Vec<ThoughtProcessOutputSection<usize>>,
3357}
3358
3359impl SavedContext {
3360 pub const VERSION: &'static str = "0.4.0";
3361
3362 pub fn from_json(json: &str) -> Result<Self> {
3363 let saved_context_json = serde_json::from_str::<serde_json::Value>(json)?;
3364 match saved_context_json
3365 .get("version")
3366 .ok_or_else(|| anyhow!("version not found"))?
3367 {
3368 serde_json::Value::String(version) => match version.as_str() {
3369 SavedContext::VERSION => {
3370 Ok(serde_json::from_value::<SavedContext>(saved_context_json)?)
3371 }
3372 SavedContextV0_3_0::VERSION => {
3373 let saved_context =
3374 serde_json::from_value::<SavedContextV0_3_0>(saved_context_json)?;
3375 Ok(saved_context.upgrade())
3376 }
3377 SavedContextV0_2_0::VERSION => {
3378 let saved_context =
3379 serde_json::from_value::<SavedContextV0_2_0>(saved_context_json)?;
3380 Ok(saved_context.upgrade())
3381 }
3382 SavedContextV0_1_0::VERSION => {
3383 let saved_context =
3384 serde_json::from_value::<SavedContextV0_1_0>(saved_context_json)?;
3385 Ok(saved_context.upgrade())
3386 }
3387 _ => Err(anyhow!("unrecognized saved context version: {}", version)),
3388 },
3389 _ => Err(anyhow!("version not found on saved context")),
3390 }
3391 }
3392
3393 fn into_ops(
3394 self,
3395 buffer: &Entity<Buffer>,
3396 cx: &mut Context<AssistantContext>,
3397 ) -> Vec<ContextOperation> {
3398 let mut operations = Vec::new();
3399 let mut version = clock::Global::new();
3400 let mut next_timestamp = clock::Lamport::new(ReplicaId::default());
3401
3402 let mut first_message_metadata = None;
3403 for message in self.messages {
3404 if message.id == MessageId(clock::Lamport::default()) {
3405 first_message_metadata = Some(message.metadata);
3406 } else {
3407 operations.push(ContextOperation::InsertMessage {
3408 anchor: MessageAnchor {
3409 id: message.id,
3410 start: buffer.read(cx).anchor_before(message.start),
3411 },
3412 metadata: MessageMetadata {
3413 role: message.metadata.role,
3414 status: message.metadata.status,
3415 timestamp: message.metadata.timestamp,
3416 cache: None,
3417 },
3418 version: version.clone(),
3419 });
3420 version.observe(message.id.0);
3421 next_timestamp.observe(message.id.0);
3422 }
3423 }
3424
3425 if let Some(metadata) = first_message_metadata {
3426 let timestamp = next_timestamp.tick();
3427 operations.push(ContextOperation::UpdateMessage {
3428 message_id: MessageId(clock::Lamport::default()),
3429 metadata: MessageMetadata {
3430 role: metadata.role,
3431 status: metadata.status,
3432 timestamp,
3433 cache: None,
3434 },
3435 version: version.clone(),
3436 });
3437 version.observe(timestamp);
3438 }
3439
3440 let buffer = buffer.read(cx);
3441 for section in self.slash_command_output_sections {
3442 let timestamp = next_timestamp.tick();
3443 operations.push(ContextOperation::SlashCommandOutputSectionAdded {
3444 timestamp,
3445 section: SlashCommandOutputSection {
3446 range: buffer.anchor_after(section.range.start)
3447 ..buffer.anchor_before(section.range.end),
3448 icon: section.icon,
3449 label: section.label,
3450 metadata: section.metadata,
3451 },
3452 version: version.clone(),
3453 });
3454
3455 version.observe(timestamp);
3456 }
3457
3458 for section in self.thought_process_output_sections {
3459 let timestamp = next_timestamp.tick();
3460 operations.push(ContextOperation::ThoughtProcessOutputSectionAdded {
3461 timestamp,
3462 section: ThoughtProcessOutputSection {
3463 range: buffer.anchor_after(section.range.start)
3464 ..buffer.anchor_before(section.range.end),
3465 },
3466 version: version.clone(),
3467 });
3468
3469 version.observe(timestamp);
3470 }
3471
3472 let timestamp = next_timestamp.tick();
3473 operations.push(ContextOperation::UpdateSummary {
3474 summary: ContextSummary {
3475 text: self.summary,
3476 done: true,
3477 timestamp,
3478 },
3479 version: version.clone(),
3480 });
3481 version.observe(timestamp);
3482
3483 operations
3484 }
3485}
3486
3487#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
3488struct SavedMessageIdPreV0_4_0(usize);
3489
3490#[derive(Serialize, Deserialize)]
3491struct SavedMessagePreV0_4_0 {
3492 id: SavedMessageIdPreV0_4_0,
3493 start: usize,
3494}
3495
3496#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
3497struct SavedMessageMetadataPreV0_4_0 {
3498 role: Role,
3499 status: MessageStatus,
3500}
3501
3502#[derive(Serialize, Deserialize)]
3503struct SavedContextV0_3_0 {
3504 id: Option<ContextId>,
3505 zed: String,
3506 version: String,
3507 text: String,
3508 messages: Vec<SavedMessagePreV0_4_0>,
3509 message_metadata: HashMap<SavedMessageIdPreV0_4_0, SavedMessageMetadataPreV0_4_0>,
3510 summary: String,
3511 slash_command_output_sections: Vec<assistant_slash_command::SlashCommandOutputSection<usize>>,
3512}
3513
3514impl SavedContextV0_3_0 {
3515 const VERSION: &'static str = "0.3.0";
3516
3517 fn upgrade(self) -> SavedContext {
3518 SavedContext {
3519 id: self.id,
3520 zed: self.zed,
3521 version: SavedContext::VERSION.into(),
3522 text: self.text,
3523 messages: self
3524 .messages
3525 .into_iter()
3526 .filter_map(|message| {
3527 let metadata = self.message_metadata.get(&message.id)?;
3528 let timestamp = clock::Lamport {
3529 replica_id: ReplicaId::default(),
3530 value: message.id.0 as u32,
3531 };
3532 Some(SavedMessage {
3533 id: MessageId(timestamp),
3534 start: message.start,
3535 metadata: MessageMetadata {
3536 role: metadata.role,
3537 status: metadata.status.clone(),
3538 timestamp,
3539 cache: None,
3540 },
3541 })
3542 })
3543 .collect(),
3544 summary: self.summary,
3545 slash_command_output_sections: self.slash_command_output_sections,
3546 thought_process_output_sections: Vec::new(),
3547 }
3548 }
3549}
3550
3551#[derive(Serialize, Deserialize)]
3552struct SavedContextV0_2_0 {
3553 id: Option<ContextId>,
3554 zed: String,
3555 version: String,
3556 text: String,
3557 messages: Vec<SavedMessagePreV0_4_0>,
3558 message_metadata: HashMap<SavedMessageIdPreV0_4_0, SavedMessageMetadataPreV0_4_0>,
3559 summary: String,
3560}
3561
3562impl SavedContextV0_2_0 {
3563 const VERSION: &'static str = "0.2.0";
3564
3565 fn upgrade(self) -> SavedContext {
3566 SavedContextV0_3_0 {
3567 id: self.id,
3568 zed: self.zed,
3569 version: SavedContextV0_3_0::VERSION.to_string(),
3570 text: self.text,
3571 messages: self.messages,
3572 message_metadata: self.message_metadata,
3573 summary: self.summary,
3574 slash_command_output_sections: Vec::new(),
3575 }
3576 .upgrade()
3577 }
3578}
3579
3580#[derive(Serialize, Deserialize)]
3581struct SavedContextV0_1_0 {
3582 id: Option<ContextId>,
3583 zed: String,
3584 version: String,
3585 text: String,
3586 messages: Vec<SavedMessagePreV0_4_0>,
3587 message_metadata: HashMap<SavedMessageIdPreV0_4_0, SavedMessageMetadataPreV0_4_0>,
3588 summary: String,
3589 api_url: Option<String>,
3590 model: OpenAiModel,
3591}
3592
3593impl SavedContextV0_1_0 {
3594 const VERSION: &'static str = "0.1.0";
3595
3596 fn upgrade(self) -> SavedContext {
3597 SavedContextV0_2_0 {
3598 id: self.id,
3599 zed: self.zed,
3600 version: SavedContextV0_2_0::VERSION.to_string(),
3601 text: self.text,
3602 messages: self.messages,
3603 message_metadata: self.message_metadata,
3604 summary: self.summary,
3605 }
3606 .upgrade()
3607 }
3608}
3609
3610#[derive(Debug, Clone)]
3611pub struct SavedContextMetadata {
3612 pub title: String,
3613 pub path: Arc<Path>,
3614 pub mtime: chrono::DateTime<chrono::Local>,
3615}