text_thread.rs

   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(&section.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(&section.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(&section.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(&section.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            bypass_rate_limit: false,
2273        };
2274        for message in self.messages(cx) {
2275            if message.status != MessageStatus::Done {
2276                continue;
2277            }
2278
2279            let mut offset = message.offset_range.start;
2280            let mut request_message = LanguageModelRequestMessage {
2281                role: message.role,
2282                content: Vec::new(),
2283                cache: message.cache.as_ref().is_some_and(|cache| cache.is_anchor),
2284                reasoning_details: None,
2285            };
2286
2287            while let Some(content) = contents.peek() {
2288                if content
2289                    .range()
2290                    .end
2291                    .cmp(&message.anchor_range.end, buffer)
2292                    .is_lt()
2293                {
2294                    let content = contents.next().unwrap();
2295                    let range = content.range().to_offset(buffer);
2296                    request_message.content.extend(
2297                        collect_text_content(buffer, offset..range.start).map(MessageContent::Text),
2298                    );
2299
2300                    match content {
2301                        Content::Image { image, .. } => {
2302                            if let Some(image) = image.clone().now_or_never().flatten() {
2303                                request_message
2304                                    .content
2305                                    .push(language_model::MessageContent::Image(image));
2306                            }
2307                        }
2308                    }
2309
2310                    offset = range.end;
2311                } else {
2312                    break;
2313                }
2314            }
2315
2316            request_message.content.extend(
2317                collect_text_content(buffer, offset..message.offset_range.end)
2318                    .map(MessageContent::Text),
2319            );
2320
2321            if !request_message.contents_empty() {
2322                completion_request.messages.push(request_message);
2323            }
2324        }
2325
2326        completion_request
2327    }
2328
2329    pub fn cancel_last_assist(&mut self, cx: &mut Context<Self>) -> bool {
2330        if let Some(pending_completion) = self.pending_completions.pop() {
2331            self.update_metadata(pending_completion.assistant_message_id, cx, |metadata| {
2332                if metadata.status == MessageStatus::Pending {
2333                    metadata.status = MessageStatus::Canceled;
2334                }
2335            });
2336            true
2337        } else {
2338            false
2339        }
2340    }
2341
2342    pub fn cycle_message_roles(&mut self, ids: HashSet<MessageId>, cx: &mut Context<Self>) {
2343        for id in &ids {
2344            if let Some(metadata) = self.messages_metadata.get(id) {
2345                let role = metadata.role.cycle();
2346                self.update_metadata(*id, cx, |metadata| metadata.role = role);
2347            }
2348        }
2349
2350        self.message_roles_updated(ids, cx);
2351    }
2352
2353    fn message_roles_updated(&mut self, ids: HashSet<MessageId>, cx: &mut Context<Self>) {
2354        let mut ranges = Vec::new();
2355        for message in self.messages(cx) {
2356            if ids.contains(&message.id) {
2357                ranges.push(message.anchor_range.clone());
2358            }
2359        }
2360    }
2361
2362    pub fn update_metadata(
2363        &mut self,
2364        id: MessageId,
2365        cx: &mut Context<Self>,
2366        f: impl FnOnce(&mut MessageMetadata),
2367    ) {
2368        let version = self.version.clone();
2369        let timestamp = self.next_timestamp();
2370        if let Some(metadata) = self.messages_metadata.get_mut(&id) {
2371            f(metadata);
2372            metadata.timestamp = timestamp;
2373            let operation = TextThreadOperation::UpdateMessage {
2374                message_id: id,
2375                metadata: metadata.clone(),
2376                version,
2377            };
2378            self.push_op(operation, cx);
2379            cx.emit(TextThreadEvent::MessagesEdited);
2380            cx.notify();
2381        }
2382    }
2383
2384    pub fn insert_message_after(
2385        &mut self,
2386        message_id: MessageId,
2387        role: Role,
2388        status: MessageStatus,
2389        cx: &mut Context<Self>,
2390    ) -> Option<MessageAnchor> {
2391        if let Some(prev_message_ix) = self
2392            .message_anchors
2393            .iter()
2394            .position(|message| message.id == message_id)
2395        {
2396            // Find the next valid message after the one we were given.
2397            let mut next_message_ix = prev_message_ix + 1;
2398            while let Some(next_message) = self.message_anchors.get(next_message_ix) {
2399                if next_message.start.is_valid(self.buffer.read(cx)) {
2400                    break;
2401                }
2402                next_message_ix += 1;
2403            }
2404
2405            let buffer = self.buffer.read(cx);
2406            let offset = self
2407                .message_anchors
2408                .get(next_message_ix)
2409                .map_or(buffer.len(), |message| {
2410                    buffer.clip_offset(message.start.to_previous_offset(buffer), Bias::Left)
2411                });
2412            Some(self.insert_message_at_offset(offset, role, status, cx))
2413        } else {
2414            None
2415        }
2416    }
2417
2418    fn insert_message_at_offset(
2419        &mut self,
2420        offset: usize,
2421        role: Role,
2422        status: MessageStatus,
2423        cx: &mut Context<Self>,
2424    ) -> MessageAnchor {
2425        let start = self.buffer.update(cx, |buffer, cx| {
2426            buffer.edit([(offset..offset, "\n")], None, cx);
2427            buffer.anchor_before(offset + 1)
2428        });
2429
2430        let version = self.version.clone();
2431        let anchor = MessageAnchor {
2432            id: MessageId(self.next_timestamp()),
2433            start,
2434        };
2435        let metadata = MessageMetadata {
2436            role,
2437            status,
2438            timestamp: anchor.id.0,
2439            cache: None,
2440        };
2441        self.insert_message(anchor.clone(), metadata.clone(), cx);
2442        self.push_op(
2443            TextThreadOperation::InsertMessage {
2444                anchor: anchor.clone(),
2445                metadata,
2446                version,
2447            },
2448            cx,
2449        );
2450        anchor
2451    }
2452
2453    pub fn insert_content(&mut self, content: Content, cx: &mut Context<Self>) {
2454        let buffer = self.buffer.read(cx);
2455        let insertion_ix = match self
2456            .contents
2457            .binary_search_by(|probe| probe.cmp(&content, buffer))
2458        {
2459            Ok(ix) => {
2460                self.contents.remove(ix);
2461                ix
2462            }
2463            Err(ix) => ix,
2464        };
2465        self.contents.insert(insertion_ix, content);
2466        cx.emit(TextThreadEvent::MessagesEdited);
2467    }
2468
2469    pub fn contents<'a>(&'a self, cx: &'a App) -> impl 'a + Iterator<Item = Content> {
2470        let buffer = self.buffer.read(cx);
2471        self.contents
2472            .iter()
2473            .filter(|content| {
2474                let range = content.range();
2475                range.start.is_valid(buffer) && range.end.is_valid(buffer)
2476            })
2477            .cloned()
2478    }
2479
2480    pub fn split_message(
2481        &mut self,
2482        range: Range<usize>,
2483        cx: &mut Context<Self>,
2484    ) -> (Option<MessageAnchor>, Option<MessageAnchor>) {
2485        let start_message = self.message_for_offset(range.start, cx);
2486        let end_message = self.message_for_offset(range.end, cx);
2487        if let Some((start_message, end_message)) = start_message.zip(end_message) {
2488            // Prevent splitting when range spans multiple messages.
2489            if start_message.id != end_message.id {
2490                return (None, None);
2491            }
2492
2493            let message = start_message;
2494            let at_end = range.end >= message.offset_range.end.saturating_sub(1);
2495            let role_after = if range.start == range.end || at_end {
2496                Role::User
2497            } else {
2498                message.role
2499            };
2500            let role = message.role;
2501            let mut edited_buffer = false;
2502
2503            let mut suffix_start = None;
2504
2505            // TODO: why did this start panicking?
2506            if range.start > message.offset_range.start
2507                && range.end < message.offset_range.end.saturating_sub(1)
2508            {
2509                if self.buffer.read(cx).chars_at(range.end).next() == Some('\n') {
2510                    suffix_start = Some(range.end + 1);
2511                } else if self.buffer.read(cx).reversed_chars_at(range.end).next() == Some('\n') {
2512                    suffix_start = Some(range.end);
2513                }
2514            }
2515
2516            let version = self.version.clone();
2517            let suffix = if let Some(suffix_start) = suffix_start {
2518                MessageAnchor {
2519                    id: MessageId(self.next_timestamp()),
2520                    start: self.buffer.read(cx).anchor_before(suffix_start),
2521                }
2522            } else {
2523                self.buffer.update(cx, |buffer, cx| {
2524                    buffer.edit([(range.end..range.end, "\n")], None, cx);
2525                });
2526                edited_buffer = true;
2527                MessageAnchor {
2528                    id: MessageId(self.next_timestamp()),
2529                    start: self.buffer.read(cx).anchor_before(range.end + 1),
2530                }
2531            };
2532
2533            let suffix_metadata = MessageMetadata {
2534                role: role_after,
2535                status: MessageStatus::Done,
2536                timestamp: suffix.id.0,
2537                cache: None,
2538            };
2539            self.insert_message(suffix.clone(), suffix_metadata.clone(), cx);
2540            self.push_op(
2541                TextThreadOperation::InsertMessage {
2542                    anchor: suffix.clone(),
2543                    metadata: suffix_metadata,
2544                    version,
2545                },
2546                cx,
2547            );
2548
2549            let new_messages =
2550                if range.start == range.end || range.start == message.offset_range.start {
2551                    (None, Some(suffix))
2552                } else {
2553                    let mut prefix_end = None;
2554                    if range.start > message.offset_range.start
2555                        && range.end < message.offset_range.end - 1
2556                    {
2557                        if self.buffer.read(cx).chars_at(range.start).next() == Some('\n') {
2558                            prefix_end = Some(range.start + 1);
2559                        } else if self.buffer.read(cx).reversed_chars_at(range.start).next()
2560                            == Some('\n')
2561                        {
2562                            prefix_end = Some(range.start);
2563                        }
2564                    }
2565
2566                    let version = self.version.clone();
2567                    let selection = if let Some(prefix_end) = prefix_end {
2568                        MessageAnchor {
2569                            id: MessageId(self.next_timestamp()),
2570                            start: self.buffer.read(cx).anchor_before(prefix_end),
2571                        }
2572                    } else {
2573                        self.buffer.update(cx, |buffer, cx| {
2574                            buffer.edit([(range.start..range.start, "\n")], None, cx)
2575                        });
2576                        edited_buffer = true;
2577                        MessageAnchor {
2578                            id: MessageId(self.next_timestamp()),
2579                            start: self.buffer.read(cx).anchor_before(range.end + 1),
2580                        }
2581                    };
2582
2583                    let selection_metadata = MessageMetadata {
2584                        role,
2585                        status: MessageStatus::Done,
2586                        timestamp: selection.id.0,
2587                        cache: None,
2588                    };
2589                    self.insert_message(selection.clone(), selection_metadata.clone(), cx);
2590                    self.push_op(
2591                        TextThreadOperation::InsertMessage {
2592                            anchor: selection.clone(),
2593                            metadata: selection_metadata,
2594                            version,
2595                        },
2596                        cx,
2597                    );
2598
2599                    (Some(selection), Some(suffix))
2600                };
2601
2602            if !edited_buffer {
2603                cx.emit(TextThreadEvent::MessagesEdited);
2604            }
2605            new_messages
2606        } else {
2607            (None, None)
2608        }
2609    }
2610
2611    fn insert_message(
2612        &mut self,
2613        new_anchor: MessageAnchor,
2614        new_metadata: MessageMetadata,
2615        cx: &mut Context<Self>,
2616    ) {
2617        cx.emit(TextThreadEvent::MessagesEdited);
2618
2619        self.messages_metadata.insert(new_anchor.id, new_metadata);
2620
2621        let buffer = self.buffer.read(cx);
2622        let insertion_ix = self
2623            .message_anchors
2624            .iter()
2625            .position(|anchor| {
2626                let comparison = new_anchor.start.cmp(&anchor.start, buffer);
2627                comparison.is_lt() || (comparison.is_eq() && new_anchor.id > anchor.id)
2628            })
2629            .unwrap_or(self.message_anchors.len());
2630        self.message_anchors.insert(insertion_ix, new_anchor);
2631    }
2632
2633    pub fn summarize(&mut self, mut replace_old: bool, cx: &mut Context<Self>) {
2634        let Some(model) = LanguageModelRegistry::read_global(cx).thread_summary_model() else {
2635            return;
2636        };
2637
2638        if replace_old || (self.message_anchors.len() >= 2 && self.summary.is_pending()) {
2639            if !model.provider.is_authenticated(cx) {
2640                return;
2641            }
2642
2643            let mut request = self.to_completion_request(Some(&model.model), cx);
2644            request.messages.push(LanguageModelRequestMessage {
2645                role: Role::User,
2646                content: vec![SUMMARIZE_THREAD_PROMPT.into()],
2647                cache: false,
2648                reasoning_details: None,
2649            });
2650
2651            // If there is no summary, it is set with `done: false` so that "Loading Summary…" can
2652            // be displayed.
2653            match self.summary {
2654                TextThreadSummary::Pending | TextThreadSummary::Error => {
2655                    self.summary = TextThreadSummary::Content(TextThreadSummaryContent {
2656                        text: "".to_string(),
2657                        done: false,
2658                        timestamp: clock::Lamport::MIN,
2659                    });
2660                    replace_old = true;
2661                }
2662                TextThreadSummary::Content(_) => {}
2663            }
2664
2665            self.summary_task = cx.spawn(async move |this, cx| {
2666                let result = async {
2667                    let stream = model.model.stream_completion_text(request, cx);
2668                    let mut messages = stream.await?;
2669
2670                    let mut replaced = !replace_old;
2671                    while let Some(message) = messages.stream.next().await {
2672                        let text = message?;
2673                        let mut lines = text.lines();
2674                        this.update(cx, |this, cx| {
2675                            let version = this.version.clone();
2676                            let timestamp = this.next_timestamp();
2677                            let summary = this.summary.content_or_set_empty();
2678                            if !replaced && replace_old {
2679                                summary.text.clear();
2680                                replaced = true;
2681                            }
2682                            summary.text.extend(lines.next());
2683                            summary.timestamp = timestamp;
2684                            let operation = TextThreadOperation::UpdateSummary {
2685                                summary: summary.clone(),
2686                                version,
2687                            };
2688                            this.push_op(operation, cx);
2689                            cx.emit(TextThreadEvent::SummaryChanged);
2690                            cx.emit(TextThreadEvent::SummaryGenerated);
2691                        })?;
2692
2693                        // Stop if the LLM generated multiple lines.
2694                        if lines.next().is_some() {
2695                            break;
2696                        }
2697                    }
2698
2699                    this.read_with(cx, |this, _cx| {
2700                        if let Some(summary) = this.summary.content()
2701                            && summary.text.is_empty()
2702                        {
2703                            bail!("Model generated an empty summary");
2704                        }
2705                        Ok(())
2706                    })??;
2707
2708                    this.update(cx, |this, cx| {
2709                        let version = this.version.clone();
2710                        let timestamp = this.next_timestamp();
2711                        if let Some(summary) = this.summary.content_as_mut() {
2712                            summary.done = true;
2713                            summary.timestamp = timestamp;
2714                            let operation = TextThreadOperation::UpdateSummary {
2715                                summary: summary.clone(),
2716                                version,
2717                            };
2718                            this.push_op(operation, cx);
2719                            cx.emit(TextThreadEvent::SummaryChanged);
2720                            cx.emit(TextThreadEvent::SummaryGenerated);
2721                        }
2722                    })?;
2723
2724                    anyhow::Ok(())
2725                }
2726                .await;
2727
2728                if let Err(err) = result {
2729                    this.update(cx, |this, cx| {
2730                        this.summary = TextThreadSummary::Error;
2731                        cx.emit(TextThreadEvent::SummaryChanged);
2732                    })
2733                    .log_err();
2734                    log::error!("Error generating context summary: {}", err);
2735                }
2736
2737                Some(())
2738            });
2739        }
2740    }
2741
2742    fn message_for_offset(&self, offset: usize, cx: &App) -> Option<Message> {
2743        self.messages_for_offsets([offset], cx).pop()
2744    }
2745
2746    pub fn messages_for_offsets(
2747        &self,
2748        offsets: impl IntoIterator<Item = usize>,
2749        cx: &App,
2750    ) -> Vec<Message> {
2751        let mut result = Vec::new();
2752
2753        let mut messages = self.messages(cx).peekable();
2754        let mut offsets = offsets.into_iter().peekable();
2755        let mut current_message = messages.next();
2756        while let Some(offset) = offsets.next() {
2757            // Locate the message that contains the offset.
2758            while current_message.as_ref().is_some_and(|message| {
2759                !message.offset_range.contains(&offset) && messages.peek().is_some()
2760            }) {
2761                current_message = messages.next();
2762            }
2763            let Some(message) = current_message.as_ref() else {
2764                break;
2765            };
2766
2767            // Skip offsets that are in the same message.
2768            while offsets.peek().is_some_and(|offset| {
2769                message.offset_range.contains(offset) || messages.peek().is_none()
2770            }) {
2771                offsets.next();
2772            }
2773
2774            result.push(message.clone());
2775        }
2776        result
2777    }
2778
2779    fn messages_from_anchors<'a>(
2780        &'a self,
2781        message_anchors: impl Iterator<Item = &'a MessageAnchor> + 'a,
2782        cx: &'a App,
2783    ) -> impl 'a + Iterator<Item = Message> {
2784        let buffer = self.buffer.read(cx);
2785
2786        Self::messages_from_iters(buffer, &self.messages_metadata, message_anchors.enumerate())
2787    }
2788
2789    pub fn messages<'a>(&'a self, cx: &'a App) -> impl 'a + Iterator<Item = Message> {
2790        self.messages_from_anchors(self.message_anchors.iter(), cx)
2791    }
2792
2793    pub fn messages_from_iters<'a>(
2794        buffer: &'a Buffer,
2795        metadata: &'a HashMap<MessageId, MessageMetadata>,
2796        messages: impl Iterator<Item = (usize, &'a MessageAnchor)> + 'a,
2797    ) -> impl 'a + Iterator<Item = Message> {
2798        let mut messages = messages.peekable();
2799
2800        iter::from_fn(move || {
2801            if let Some((start_ix, message_anchor)) = messages.next() {
2802                let metadata = metadata.get(&message_anchor.id)?;
2803
2804                let message_start = message_anchor.start.to_offset(buffer);
2805                let mut message_end = None;
2806                let mut end_ix = start_ix;
2807                while let Some((_, next_message)) = messages.peek() {
2808                    if next_message.start.is_valid(buffer) {
2809                        message_end = Some(next_message.start);
2810                        break;
2811                    } else {
2812                        end_ix += 1;
2813                        messages.next();
2814                    }
2815                }
2816                let message_end_anchor =
2817                    message_end.unwrap_or(language::Anchor::max_for_buffer(buffer.remote_id()));
2818                let message_end = message_end_anchor.to_offset(buffer);
2819
2820                return Some(Message {
2821                    index_range: start_ix..end_ix,
2822                    offset_range: message_start..message_end,
2823                    anchor_range: message_anchor.start..message_end_anchor,
2824                    id: message_anchor.id,
2825                    role: metadata.role,
2826                    status: metadata.status.clone(),
2827                    cache: metadata.cache.clone(),
2828                });
2829            }
2830            None
2831        })
2832    }
2833
2834    pub fn save(
2835        &mut self,
2836        debounce: Option<Duration>,
2837        fs: Arc<dyn Fs>,
2838        cx: &mut Context<TextThread>,
2839    ) {
2840        if self.replica_id() != ReplicaId::default() {
2841            // Prevent saving a remote context for now.
2842            return;
2843        }
2844
2845        self.pending_save = cx.spawn(async move |this, cx| {
2846            if let Some(debounce) = debounce {
2847                cx.background_executor().timer(debounce).await;
2848            }
2849
2850            let (old_path, summary) = this.read_with(cx, |this, _| {
2851                let path = this.path.clone();
2852                let summary = if let Some(summary) = this.summary.content() {
2853                    if summary.done {
2854                        Some(summary.text.clone())
2855                    } else {
2856                        None
2857                    }
2858                } else {
2859                    None
2860                };
2861                (path, summary)
2862            })?;
2863
2864            if let Some(summary) = summary {
2865                let context = this.read_with(cx, |this, cx| this.serialize(cx))?;
2866                let mut discriminant = 1;
2867                let mut new_path;
2868                loop {
2869                    new_path = text_threads_dir().join(&format!(
2870                        "{} - {}.zed.json",
2871                        summary.trim(),
2872                        discriminant
2873                    ));
2874                    if fs.is_file(&new_path).await {
2875                        discriminant += 1;
2876                    } else {
2877                        break;
2878                    }
2879                }
2880
2881                fs.create_dir(text_threads_dir().as_ref()).await?;
2882
2883                // rename before write ensures that only one file exists
2884                if let Some(old_path) = old_path.as_ref()
2885                    && new_path.as_path() != old_path.as_ref()
2886                {
2887                    fs.rename(
2888                        old_path,
2889                        &new_path,
2890                        RenameOptions {
2891                            overwrite: true,
2892                            ignore_if_exists: true,
2893                            create_parents: false,
2894                        },
2895                    )
2896                    .await?;
2897                }
2898
2899                // update path before write in case it fails
2900                this.update(cx, {
2901                    let new_path: Arc<Path> = new_path.clone().into();
2902                    move |this, cx| {
2903                        this.path = Some(new_path.clone());
2904                        cx.emit(TextThreadEvent::PathChanged { old_path, new_path });
2905                    }
2906                })
2907                .ok();
2908
2909                fs.atomic_write(new_path, serde_json::to_string(&context).unwrap())
2910                    .await?;
2911            }
2912
2913            Ok(())
2914        });
2915    }
2916
2917    pub fn set_custom_summary(&mut self, custom_summary: String, cx: &mut Context<Self>) {
2918        let timestamp = self.next_timestamp();
2919        let summary = self.summary.content_or_set_empty();
2920        summary.timestamp = timestamp;
2921        summary.done = true;
2922        summary.text = custom_summary;
2923        cx.emit(TextThreadEvent::SummaryChanged);
2924    }
2925}
2926
2927#[derive(Debug, Default)]
2928pub struct TextThreadVersion {
2929    text_thread: clock::Global,
2930    buffer: clock::Global,
2931}
2932
2933impl TextThreadVersion {
2934    pub fn from_proto(proto: &proto::ContextVersion) -> Self {
2935        Self {
2936            text_thread: language::proto::deserialize_version(&proto.context_version),
2937            buffer: language::proto::deserialize_version(&proto.buffer_version),
2938        }
2939    }
2940
2941    pub fn to_proto(&self, context_id: TextThreadId) -> proto::ContextVersion {
2942        proto::ContextVersion {
2943            context_id: context_id.to_proto(),
2944            context_version: language::proto::serialize_version(&self.text_thread),
2945            buffer_version: language::proto::serialize_version(&self.buffer),
2946        }
2947    }
2948}
2949
2950#[derive(Debug, Clone)]
2951pub struct ParsedSlashCommand {
2952    pub name: String,
2953    pub arguments: SmallVec<[String; 3]>,
2954    pub status: PendingSlashCommandStatus,
2955    pub source_range: Range<language::Anchor>,
2956}
2957
2958#[derive(Debug)]
2959pub struct InvokedSlashCommand {
2960    pub name: SharedString,
2961    pub range: Range<language::Anchor>,
2962    pub run_commands_in_ranges: Vec<Range<language::Anchor>>,
2963    pub status: InvokedSlashCommandStatus,
2964    pub transaction: Option<language::TransactionId>,
2965    timestamp: clock::Lamport,
2966}
2967
2968#[derive(Debug)]
2969pub enum InvokedSlashCommandStatus {
2970    Running(Task<()>),
2971    Error(SharedString),
2972    Finished,
2973}
2974
2975#[derive(Debug, Clone)]
2976pub enum PendingSlashCommandStatus {
2977    Idle,
2978    Running { _task: Shared<Task<()>> },
2979    Error(String),
2980}
2981
2982#[derive(Debug, Clone)]
2983pub struct PendingToolUse {
2984    pub id: LanguageModelToolUseId,
2985    pub name: String,
2986    pub input: serde_json::Value,
2987    pub status: PendingToolUseStatus,
2988    pub source_range: Range<language::Anchor>,
2989}
2990
2991#[derive(Debug, Clone)]
2992pub enum PendingToolUseStatus {
2993    Idle,
2994    Running { _task: Shared<Task<()>> },
2995    Error(String),
2996}
2997
2998impl PendingToolUseStatus {
2999    pub fn is_idle(&self) -> bool {
3000        matches!(self, PendingToolUseStatus::Idle)
3001    }
3002}
3003
3004#[derive(Serialize, Deserialize)]
3005pub struct SavedMessage {
3006    pub id: MessageId,
3007    pub start: usize,
3008    pub metadata: MessageMetadata,
3009}
3010
3011#[derive(Serialize, Deserialize)]
3012pub struct SavedTextThread {
3013    pub id: Option<TextThreadId>,
3014    pub zed: String,
3015    pub version: String,
3016    pub text: String,
3017    pub messages: Vec<SavedMessage>,
3018    pub summary: String,
3019    pub slash_command_output_sections:
3020        Vec<assistant_slash_command::SlashCommandOutputSection<usize>>,
3021    #[serde(default)]
3022    pub thought_process_output_sections: Vec<ThoughtProcessOutputSection<usize>>,
3023}
3024
3025impl SavedTextThread {
3026    pub const VERSION: &'static str = "0.4.0";
3027
3028    pub fn from_json(json: &str) -> Result<Self> {
3029        let saved_context_json = serde_json::from_str::<serde_json::Value>(json)?;
3030        match saved_context_json
3031            .get("version")
3032            .context("version not found")?
3033        {
3034            serde_json::Value::String(version) => match version.as_str() {
3035                SavedTextThread::VERSION => Ok(serde_json::from_value::<SavedTextThread>(
3036                    saved_context_json,
3037                )?),
3038                SavedContextV0_3_0::VERSION => {
3039                    let saved_context =
3040                        serde_json::from_value::<SavedContextV0_3_0>(saved_context_json)?;
3041                    Ok(saved_context.upgrade())
3042                }
3043                SavedContextV0_2_0::VERSION => {
3044                    let saved_context =
3045                        serde_json::from_value::<SavedContextV0_2_0>(saved_context_json)?;
3046                    Ok(saved_context.upgrade())
3047                }
3048                SavedContextV0_1_0::VERSION => {
3049                    let saved_context =
3050                        serde_json::from_value::<SavedContextV0_1_0>(saved_context_json)?;
3051                    Ok(saved_context.upgrade())
3052                }
3053                _ => anyhow::bail!("unrecognized saved context version: {version:?}"),
3054            },
3055            _ => anyhow::bail!("version not found on saved context"),
3056        }
3057    }
3058
3059    fn into_ops(
3060        self,
3061        buffer: &Entity<Buffer>,
3062        cx: &mut Context<TextThread>,
3063    ) -> Vec<TextThreadOperation> {
3064        let mut operations = Vec::new();
3065        let mut version = clock::Global::new();
3066        let mut next_timestamp = clock::Lamport::new(ReplicaId::default());
3067
3068        let mut first_message_metadata = None;
3069        for message in self.messages {
3070            if message.id == MessageId(clock::Lamport::MIN) {
3071                first_message_metadata = Some(message.metadata);
3072            } else {
3073                operations.push(TextThreadOperation::InsertMessage {
3074                    anchor: MessageAnchor {
3075                        id: message.id,
3076                        start: buffer.read(cx).anchor_before(message.start),
3077                    },
3078                    metadata: MessageMetadata {
3079                        role: message.metadata.role,
3080                        status: message.metadata.status,
3081                        timestamp: message.metadata.timestamp,
3082                        cache: None,
3083                    },
3084                    version: version.clone(),
3085                });
3086                version.observe(message.id.0);
3087                next_timestamp.observe(message.id.0);
3088            }
3089        }
3090
3091        if let Some(metadata) = first_message_metadata {
3092            let timestamp = next_timestamp.tick();
3093            operations.push(TextThreadOperation::UpdateMessage {
3094                message_id: MessageId(clock::Lamport::MIN),
3095                metadata: MessageMetadata {
3096                    role: metadata.role,
3097                    status: metadata.status,
3098                    timestamp,
3099                    cache: None,
3100                },
3101                version: version.clone(),
3102            });
3103            version.observe(timestamp);
3104        }
3105
3106        let buffer = buffer.read(cx);
3107        for section in self.slash_command_output_sections {
3108            let timestamp = next_timestamp.tick();
3109            operations.push(TextThreadOperation::SlashCommandOutputSectionAdded {
3110                timestamp,
3111                section: SlashCommandOutputSection {
3112                    range: buffer.anchor_after(section.range.start)
3113                        ..buffer.anchor_before(section.range.end),
3114                    icon: section.icon,
3115                    label: section.label,
3116                    metadata: section.metadata,
3117                },
3118                version: version.clone(),
3119            });
3120
3121            version.observe(timestamp);
3122        }
3123
3124        for section in self.thought_process_output_sections {
3125            let timestamp = next_timestamp.tick();
3126            operations.push(TextThreadOperation::ThoughtProcessOutputSectionAdded {
3127                timestamp,
3128                section: ThoughtProcessOutputSection {
3129                    range: buffer.anchor_after(section.range.start)
3130                        ..buffer.anchor_before(section.range.end),
3131                },
3132                version: version.clone(),
3133            });
3134
3135            version.observe(timestamp);
3136        }
3137
3138        let timestamp = next_timestamp.tick();
3139        operations.push(TextThreadOperation::UpdateSummary {
3140            summary: TextThreadSummaryContent {
3141                text: self.summary,
3142                done: true,
3143                timestamp,
3144            },
3145            version: version.clone(),
3146        });
3147        version.observe(timestamp);
3148
3149        operations
3150    }
3151}
3152
3153#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
3154struct SavedMessageIdPreV0_4_0(usize);
3155
3156#[derive(Serialize, Deserialize)]
3157struct SavedMessagePreV0_4_0 {
3158    id: SavedMessageIdPreV0_4_0,
3159    start: usize,
3160}
3161
3162#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
3163struct SavedMessageMetadataPreV0_4_0 {
3164    role: Role,
3165    status: MessageStatus,
3166}
3167
3168#[derive(Serialize, Deserialize)]
3169struct SavedContextV0_3_0 {
3170    id: Option<TextThreadId>,
3171    zed: String,
3172    version: String,
3173    text: String,
3174    messages: Vec<SavedMessagePreV0_4_0>,
3175    message_metadata: HashMap<SavedMessageIdPreV0_4_0, SavedMessageMetadataPreV0_4_0>,
3176    summary: String,
3177    slash_command_output_sections: Vec<assistant_slash_command::SlashCommandOutputSection<usize>>,
3178}
3179
3180impl SavedContextV0_3_0 {
3181    const VERSION: &'static str = "0.3.0";
3182
3183    fn upgrade(self) -> SavedTextThread {
3184        SavedTextThread {
3185            id: self.id,
3186            zed: self.zed,
3187            version: SavedTextThread::VERSION.into(),
3188            text: self.text,
3189            messages: self
3190                .messages
3191                .into_iter()
3192                .filter_map(|message| {
3193                    let metadata = self.message_metadata.get(&message.id)?;
3194                    let timestamp = clock::Lamport {
3195                        replica_id: ReplicaId::default(),
3196                        value: message.id.0 as u32,
3197                    };
3198                    Some(SavedMessage {
3199                        id: MessageId(timestamp),
3200                        start: message.start,
3201                        metadata: MessageMetadata {
3202                            role: metadata.role,
3203                            status: metadata.status.clone(),
3204                            timestamp,
3205                            cache: None,
3206                        },
3207                    })
3208                })
3209                .collect(),
3210            summary: self.summary,
3211            slash_command_output_sections: self.slash_command_output_sections,
3212            thought_process_output_sections: Vec::new(),
3213        }
3214    }
3215}
3216
3217#[derive(Serialize, Deserialize)]
3218struct SavedContextV0_2_0 {
3219    id: Option<TextThreadId>,
3220    zed: String,
3221    version: String,
3222    text: String,
3223    messages: Vec<SavedMessagePreV0_4_0>,
3224    message_metadata: HashMap<SavedMessageIdPreV0_4_0, SavedMessageMetadataPreV0_4_0>,
3225    summary: String,
3226}
3227
3228impl SavedContextV0_2_0 {
3229    const VERSION: &'static str = "0.2.0";
3230
3231    fn upgrade(self) -> SavedTextThread {
3232        SavedContextV0_3_0 {
3233            id: self.id,
3234            zed: self.zed,
3235            version: SavedContextV0_3_0::VERSION.to_string(),
3236            text: self.text,
3237            messages: self.messages,
3238            message_metadata: self.message_metadata,
3239            summary: self.summary,
3240            slash_command_output_sections: Vec::new(),
3241        }
3242        .upgrade()
3243    }
3244}
3245
3246#[derive(Serialize, Deserialize)]
3247struct SavedContextV0_1_0 {
3248    id: Option<TextThreadId>,
3249    zed: String,
3250    version: String,
3251    text: String,
3252    messages: Vec<SavedMessagePreV0_4_0>,
3253    message_metadata: HashMap<SavedMessageIdPreV0_4_0, SavedMessageMetadataPreV0_4_0>,
3254    summary: String,
3255    api_url: Option<String>,
3256    model: OpenAiModel,
3257}
3258
3259impl SavedContextV0_1_0 {
3260    const VERSION: &'static str = "0.1.0";
3261
3262    fn upgrade(self) -> SavedTextThread {
3263        SavedContextV0_2_0 {
3264            id: self.id,
3265            zed: self.zed,
3266            version: SavedContextV0_2_0::VERSION.to_string(),
3267            text: self.text,
3268            messages: self.messages,
3269            message_metadata: self.message_metadata,
3270            summary: self.summary,
3271        }
3272        .upgrade()
3273    }
3274}
3275
3276#[derive(Debug, Clone)]
3277pub struct SavedTextThreadMetadata {
3278    pub title: SharedString,
3279    pub path: Arc<Path>,
3280    pub mtime: chrono::DateTime<chrono::Local>,
3281}