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