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