context.rs

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