context.rs

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