thread.rs

   1use std::fmt::Write as _;
   2use std::io::Write;
   3use std::ops::Range;
   4use std::sync::Arc;
   5use std::time::Instant;
   6
   7use agent_settings::{AgentProfileId, AgentSettings, CompletionMode};
   8use anyhow::{Result, anyhow};
   9use assistant_tool::{ActionLog, AnyToolCard, Tool, ToolWorkingSet};
  10use chrono::{DateTime, Utc};
  11use collections::HashMap;
  12use editor::display_map::CreaseMetadata;
  13use feature_flags::{self, FeatureFlagAppExt};
  14use futures::future::Shared;
  15use futures::{FutureExt, StreamExt as _};
  16use git::repository::DiffType;
  17use gpui::{
  18    AnyWindowHandle, App, AppContext, AsyncApp, Context, Entity, EventEmitter, SharedString, Task,
  19    WeakEntity,
  20};
  21use language_model::{
  22    ConfiguredModel, LanguageModel, LanguageModelCompletionError, LanguageModelCompletionEvent,
  23    LanguageModelId, LanguageModelKnownError, LanguageModelRegistry, LanguageModelRequest,
  24    LanguageModelRequestMessage, LanguageModelRequestTool, LanguageModelToolResult,
  25    LanguageModelToolResultContent, LanguageModelToolUseId, MessageContent,
  26    ModelRequestLimitReachedError, PaymentRequiredError, RequestUsage, Role, SelectedModel,
  27    StopReason, TokenUsage,
  28};
  29use postage::stream::Stream as _;
  30use project::Project;
  31use project::git_store::{GitStore, GitStoreCheckpoint, RepositoryState};
  32use prompt_store::{ModelContext, PromptBuilder};
  33use proto::Plan;
  34use schemars::JsonSchema;
  35use serde::{Deserialize, Serialize};
  36use settings::Settings;
  37use thiserror::Error;
  38use ui::Window;
  39use util::{ResultExt as _, post_inc};
  40use uuid::Uuid;
  41use zed_llm_client::{CompletionIntent, CompletionRequestStatus};
  42
  43use crate::ThreadStore;
  44use crate::agent_profile::AgentProfile;
  45use crate::context::{AgentContext, AgentContextHandle, ContextLoadResult, LoadedContext};
  46use crate::thread_store::{
  47    SerializedCrease, SerializedLanguageModel, SerializedMessage, SerializedMessageSegment,
  48    SerializedThread, SerializedToolResult, SerializedToolUse, SharedProjectContext,
  49};
  50use crate::tool_use::{PendingToolUse, ToolUse, ToolUseMetadata, ToolUseState};
  51
  52#[derive(
  53    Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Serialize, Deserialize, JsonSchema,
  54)]
  55pub struct ThreadId(Arc<str>);
  56
  57impl ThreadId {
  58    pub fn new() -> Self {
  59        Self(Uuid::new_v4().to_string().into())
  60    }
  61}
  62
  63impl std::fmt::Display for ThreadId {
  64    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  65        write!(f, "{}", self.0)
  66    }
  67}
  68
  69impl From<&str> for ThreadId {
  70    fn from(value: &str) -> Self {
  71        Self(value.into())
  72    }
  73}
  74
  75/// The ID of the user prompt that initiated a request.
  76///
  77/// This equates to the user physically submitting a message to the model (e.g., by pressing the Enter key).
  78#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Serialize, Deserialize)]
  79pub struct PromptId(Arc<str>);
  80
  81impl PromptId {
  82    pub fn new() -> Self {
  83        Self(Uuid::new_v4().to_string().into())
  84    }
  85}
  86
  87impl std::fmt::Display for PromptId {
  88    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  89        write!(f, "{}", self.0)
  90    }
  91}
  92
  93#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Serialize, Deserialize)]
  94pub struct MessageId(pub(crate) usize);
  95
  96impl MessageId {
  97    fn post_inc(&mut self) -> Self {
  98        Self(post_inc(&mut self.0))
  99    }
 100}
 101
 102/// Stored information that can be used to resurrect a context crease when creating an editor for a past message.
 103#[derive(Clone, Debug)]
 104pub struct MessageCrease {
 105    pub range: Range<usize>,
 106    pub metadata: CreaseMetadata,
 107    /// None for a deserialized message, Some otherwise.
 108    pub context: Option<AgentContextHandle>,
 109}
 110
 111/// A message in a [`Thread`].
 112#[derive(Debug, Clone)]
 113pub struct Message {
 114    pub id: MessageId,
 115    pub role: Role,
 116    pub segments: Vec<MessageSegment>,
 117    pub loaded_context: LoadedContext,
 118    pub creases: Vec<MessageCrease>,
 119    pub is_hidden: bool,
 120}
 121
 122impl Message {
 123    /// Returns whether the message contains any meaningful text that should be displayed
 124    /// The model sometimes runs tool without producing any text or just a marker ([`USING_TOOL_MARKER`])
 125    pub fn should_display_content(&self) -> bool {
 126        self.segments.iter().all(|segment| segment.should_display())
 127    }
 128
 129    pub fn push_thinking(&mut self, text: &str, signature: Option<String>) {
 130        if let Some(MessageSegment::Thinking {
 131            text: segment,
 132            signature: current_signature,
 133        }) = self.segments.last_mut()
 134        {
 135            if let Some(signature) = signature {
 136                *current_signature = Some(signature);
 137            }
 138            segment.push_str(text);
 139        } else {
 140            self.segments.push(MessageSegment::Thinking {
 141                text: text.to_string(),
 142                signature,
 143            });
 144        }
 145    }
 146
 147    pub fn push_text(&mut self, text: &str) {
 148        if let Some(MessageSegment::Text(segment)) = self.segments.last_mut() {
 149            segment.push_str(text);
 150        } else {
 151            self.segments.push(MessageSegment::Text(text.to_string()));
 152        }
 153    }
 154
 155    pub fn to_string(&self) -> String {
 156        let mut result = String::new();
 157
 158        if !self.loaded_context.text.is_empty() {
 159            result.push_str(&self.loaded_context.text);
 160        }
 161
 162        for segment in &self.segments {
 163            match segment {
 164                MessageSegment::Text(text) => result.push_str(text),
 165                MessageSegment::Thinking { text, .. } => {
 166                    result.push_str("<think>\n");
 167                    result.push_str(text);
 168                    result.push_str("\n</think>");
 169                }
 170                MessageSegment::RedactedThinking(_) => {}
 171            }
 172        }
 173
 174        result
 175    }
 176}
 177
 178#[derive(Debug, Clone, PartialEq, Eq)]
 179pub enum MessageSegment {
 180    Text(String),
 181    Thinking {
 182        text: String,
 183        signature: Option<String>,
 184    },
 185    RedactedThinking(Vec<u8>),
 186}
 187
 188impl MessageSegment {
 189    pub fn should_display(&self) -> bool {
 190        match self {
 191            Self::Text(text) => text.is_empty(),
 192            Self::Thinking { text, .. } => text.is_empty(),
 193            Self::RedactedThinking(_) => false,
 194        }
 195    }
 196}
 197
 198#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
 199pub struct ProjectSnapshot {
 200    pub worktree_snapshots: Vec<WorktreeSnapshot>,
 201    pub unsaved_buffer_paths: Vec<String>,
 202    pub timestamp: DateTime<Utc>,
 203}
 204
 205#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
 206pub struct WorktreeSnapshot {
 207    pub worktree_path: String,
 208    pub git_state: Option<GitState>,
 209}
 210
 211#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
 212pub struct GitState {
 213    pub remote_url: Option<String>,
 214    pub head_sha: Option<String>,
 215    pub current_branch: Option<String>,
 216    pub diff: Option<String>,
 217}
 218
 219#[derive(Clone, Debug)]
 220pub struct ThreadCheckpoint {
 221    message_id: MessageId,
 222    git_checkpoint: GitStoreCheckpoint,
 223}
 224
 225#[derive(Copy, Clone, Debug, PartialEq, Eq)]
 226pub enum ThreadFeedback {
 227    Positive,
 228    Negative,
 229}
 230
 231pub enum LastRestoreCheckpoint {
 232    Pending {
 233        message_id: MessageId,
 234    },
 235    Error {
 236        message_id: MessageId,
 237        error: String,
 238    },
 239}
 240
 241impl LastRestoreCheckpoint {
 242    pub fn message_id(&self) -> MessageId {
 243        match self {
 244            LastRestoreCheckpoint::Pending { message_id } => *message_id,
 245            LastRestoreCheckpoint::Error { message_id, .. } => *message_id,
 246        }
 247    }
 248}
 249
 250#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
 251pub enum DetailedSummaryState {
 252    #[default]
 253    NotGenerated,
 254    Generating {
 255        message_id: MessageId,
 256    },
 257    Generated {
 258        text: SharedString,
 259        message_id: MessageId,
 260    },
 261}
 262
 263impl DetailedSummaryState {
 264    fn text(&self) -> Option<SharedString> {
 265        if let Self::Generated { text, .. } = self {
 266            Some(text.clone())
 267        } else {
 268            None
 269        }
 270    }
 271}
 272
 273#[derive(Default, Debug)]
 274pub struct TotalTokenUsage {
 275    pub total: usize,
 276    pub max: usize,
 277}
 278
 279impl TotalTokenUsage {
 280    pub fn ratio(&self) -> TokenUsageRatio {
 281        #[cfg(debug_assertions)]
 282        let warning_threshold: f32 = std::env::var("ZED_THREAD_WARNING_THRESHOLD")
 283            .unwrap_or("0.8".to_string())
 284            .parse()
 285            .unwrap();
 286        #[cfg(not(debug_assertions))]
 287        let warning_threshold: f32 = 0.8;
 288
 289        // When the maximum is unknown because there is no selected model,
 290        // avoid showing the token limit warning.
 291        if self.max == 0 {
 292            TokenUsageRatio::Normal
 293        } else if self.total >= self.max {
 294            TokenUsageRatio::Exceeded
 295        } else if self.total as f32 / self.max as f32 >= warning_threshold {
 296            TokenUsageRatio::Warning
 297        } else {
 298            TokenUsageRatio::Normal
 299        }
 300    }
 301
 302    pub fn add(&self, tokens: usize) -> TotalTokenUsage {
 303        TotalTokenUsage {
 304            total: self.total + tokens,
 305            max: self.max,
 306        }
 307    }
 308}
 309
 310#[derive(Debug, Default, PartialEq, Eq)]
 311pub enum TokenUsageRatio {
 312    #[default]
 313    Normal,
 314    Warning,
 315    Exceeded,
 316}
 317
 318#[derive(Debug, Clone, Copy)]
 319pub enum QueueState {
 320    Sending,
 321    Queued { position: usize },
 322    Started,
 323}
 324
 325/// A thread of conversation with the LLM.
 326pub struct Thread {
 327    id: ThreadId,
 328    updated_at: DateTime<Utc>,
 329    summary: ThreadSummary,
 330    pending_summary: Task<Option<()>>,
 331    detailed_summary_task: Task<Option<()>>,
 332    detailed_summary_tx: postage::watch::Sender<DetailedSummaryState>,
 333    detailed_summary_rx: postage::watch::Receiver<DetailedSummaryState>,
 334    completion_mode: agent_settings::CompletionMode,
 335    messages: Vec<Message>,
 336    next_message_id: MessageId,
 337    last_prompt_id: PromptId,
 338    project_context: SharedProjectContext,
 339    checkpoints_by_message: HashMap<MessageId, ThreadCheckpoint>,
 340    completion_count: usize,
 341    pending_completions: Vec<PendingCompletion>,
 342    project: Entity<Project>,
 343    prompt_builder: Arc<PromptBuilder>,
 344    tools: Entity<ToolWorkingSet>,
 345    tool_use: ToolUseState,
 346    action_log: Entity<ActionLog>,
 347    last_restore_checkpoint: Option<LastRestoreCheckpoint>,
 348    pending_checkpoint: Option<ThreadCheckpoint>,
 349    initial_project_snapshot: Shared<Task<Option<Arc<ProjectSnapshot>>>>,
 350    request_token_usage: Vec<TokenUsage>,
 351    cumulative_token_usage: TokenUsage,
 352    exceeded_window_error: Option<ExceededWindowError>,
 353    last_usage: Option<RequestUsage>,
 354    tool_use_limit_reached: bool,
 355    feedback: Option<ThreadFeedback>,
 356    message_feedback: HashMap<MessageId, ThreadFeedback>,
 357    last_auto_capture_at: Option<Instant>,
 358    last_received_chunk_at: Option<Instant>,
 359    request_callback: Option<
 360        Box<dyn FnMut(&LanguageModelRequest, &[Result<LanguageModelCompletionEvent, String>])>,
 361    >,
 362    remaining_turns: u32,
 363    configured_model: Option<ConfiguredModel>,
 364    profile: AgentProfile,
 365}
 366
 367#[derive(Clone, Debug, PartialEq, Eq)]
 368pub enum ThreadSummary {
 369    Pending,
 370    Generating,
 371    Ready(SharedString),
 372    Error,
 373}
 374
 375impl ThreadSummary {
 376    pub const DEFAULT: SharedString = SharedString::new_static("New Thread");
 377
 378    pub fn or_default(&self) -> SharedString {
 379        self.unwrap_or(Self::DEFAULT)
 380    }
 381
 382    pub fn unwrap_or(&self, message: impl Into<SharedString>) -> SharedString {
 383        self.ready().unwrap_or_else(|| message.into())
 384    }
 385
 386    pub fn ready(&self) -> Option<SharedString> {
 387        match self {
 388            ThreadSummary::Ready(summary) => Some(summary.clone()),
 389            ThreadSummary::Pending | ThreadSummary::Generating | ThreadSummary::Error => None,
 390        }
 391    }
 392}
 393
 394#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
 395pub struct ExceededWindowError {
 396    /// Model used when last message exceeded context window
 397    model_id: LanguageModelId,
 398    /// Token count including last message
 399    token_count: usize,
 400}
 401
 402impl Thread {
 403    pub fn new(
 404        project: Entity<Project>,
 405        tools: Entity<ToolWorkingSet>,
 406        prompt_builder: Arc<PromptBuilder>,
 407        system_prompt: SharedProjectContext,
 408        cx: &mut Context<Self>,
 409    ) -> Self {
 410        let (detailed_summary_tx, detailed_summary_rx) = postage::watch::channel();
 411        let configured_model = LanguageModelRegistry::read_global(cx).default_model();
 412        let profile_id = AgentSettings::get_global(cx).default_profile.clone();
 413
 414        Self {
 415            id: ThreadId::new(),
 416            updated_at: Utc::now(),
 417            summary: ThreadSummary::Pending,
 418            pending_summary: Task::ready(None),
 419            detailed_summary_task: Task::ready(None),
 420            detailed_summary_tx,
 421            detailed_summary_rx,
 422            completion_mode: AgentSettings::get_global(cx).preferred_completion_mode,
 423            messages: Vec::new(),
 424            next_message_id: MessageId(0),
 425            last_prompt_id: PromptId::new(),
 426            project_context: system_prompt,
 427            checkpoints_by_message: HashMap::default(),
 428            completion_count: 0,
 429            pending_completions: Vec::new(),
 430            project: project.clone(),
 431            prompt_builder,
 432            tools: tools.clone(),
 433            last_restore_checkpoint: None,
 434            pending_checkpoint: None,
 435            tool_use: ToolUseState::new(tools.clone()),
 436            action_log: cx.new(|_| ActionLog::new(project.clone())),
 437            initial_project_snapshot: {
 438                let project_snapshot = Self::project_snapshot(project, cx);
 439                cx.foreground_executor()
 440                    .spawn(async move { Some(project_snapshot.await) })
 441                    .shared()
 442            },
 443            request_token_usage: Vec::new(),
 444            cumulative_token_usage: TokenUsage::default(),
 445            exceeded_window_error: None,
 446            last_usage: None,
 447            tool_use_limit_reached: false,
 448            feedback: None,
 449            message_feedback: HashMap::default(),
 450            last_auto_capture_at: None,
 451            last_received_chunk_at: None,
 452            request_callback: None,
 453            remaining_turns: u32::MAX,
 454            configured_model,
 455            profile: AgentProfile::new(profile_id, tools),
 456        }
 457    }
 458
 459    pub fn deserialize(
 460        id: ThreadId,
 461        serialized: SerializedThread,
 462        project: Entity<Project>,
 463        tools: Entity<ToolWorkingSet>,
 464        prompt_builder: Arc<PromptBuilder>,
 465        project_context: SharedProjectContext,
 466        window: Option<&mut Window>, // None in headless mode
 467        cx: &mut Context<Self>,
 468    ) -> Self {
 469        let next_message_id = MessageId(
 470            serialized
 471                .messages
 472                .last()
 473                .map(|message| message.id.0 + 1)
 474                .unwrap_or(0),
 475        );
 476        let tool_use = ToolUseState::from_serialized_messages(
 477            tools.clone(),
 478            &serialized.messages,
 479            project.clone(),
 480            window,
 481            cx,
 482        );
 483        let (detailed_summary_tx, detailed_summary_rx) =
 484            postage::watch::channel_with(serialized.detailed_summary_state);
 485
 486        let configured_model = LanguageModelRegistry::global(cx).update(cx, |registry, cx| {
 487            serialized
 488                .model
 489                .and_then(|model| {
 490                    let model = SelectedModel {
 491                        provider: model.provider.clone().into(),
 492                        model: model.model.clone().into(),
 493                    };
 494                    registry.select_model(&model, cx)
 495                })
 496                .or_else(|| registry.default_model())
 497        });
 498
 499        let completion_mode = serialized
 500            .completion_mode
 501            .unwrap_or_else(|| AgentSettings::get_global(cx).preferred_completion_mode);
 502        let profile_id = serialized
 503            .profile
 504            .unwrap_or_else(|| AgentSettings::get_global(cx).default_profile.clone());
 505
 506        Self {
 507            id,
 508            updated_at: serialized.updated_at,
 509            summary: ThreadSummary::Ready(serialized.summary),
 510            pending_summary: Task::ready(None),
 511            detailed_summary_task: Task::ready(None),
 512            detailed_summary_tx,
 513            detailed_summary_rx,
 514            completion_mode,
 515            messages: serialized
 516                .messages
 517                .into_iter()
 518                .map(|message| Message {
 519                    id: message.id,
 520                    role: message.role,
 521                    segments: message
 522                        .segments
 523                        .into_iter()
 524                        .map(|segment| match segment {
 525                            SerializedMessageSegment::Text { text } => MessageSegment::Text(text),
 526                            SerializedMessageSegment::Thinking { text, signature } => {
 527                                MessageSegment::Thinking { text, signature }
 528                            }
 529                            SerializedMessageSegment::RedactedThinking { data } => {
 530                                MessageSegment::RedactedThinking(data)
 531                            }
 532                        })
 533                        .collect(),
 534                    loaded_context: LoadedContext {
 535                        contexts: Vec::new(),
 536                        text: message.context,
 537                        images: Vec::new(),
 538                    },
 539                    creases: message
 540                        .creases
 541                        .into_iter()
 542                        .map(|crease| MessageCrease {
 543                            range: crease.start..crease.end,
 544                            metadata: CreaseMetadata {
 545                                icon_path: crease.icon_path,
 546                                label: crease.label,
 547                            },
 548                            context: None,
 549                        })
 550                        .collect(),
 551                    is_hidden: message.is_hidden,
 552                })
 553                .collect(),
 554            next_message_id,
 555            last_prompt_id: PromptId::new(),
 556            project_context,
 557            checkpoints_by_message: HashMap::default(),
 558            completion_count: 0,
 559            pending_completions: Vec::new(),
 560            last_restore_checkpoint: None,
 561            pending_checkpoint: None,
 562            project: project.clone(),
 563            prompt_builder,
 564            tools: tools.clone(),
 565            tool_use,
 566            action_log: cx.new(|_| ActionLog::new(project)),
 567            initial_project_snapshot: Task::ready(serialized.initial_project_snapshot).shared(),
 568            request_token_usage: serialized.request_token_usage,
 569            cumulative_token_usage: serialized.cumulative_token_usage,
 570            exceeded_window_error: None,
 571            last_usage: None,
 572            tool_use_limit_reached: serialized.tool_use_limit_reached,
 573            feedback: None,
 574            message_feedback: HashMap::default(),
 575            last_auto_capture_at: None,
 576            last_received_chunk_at: None,
 577            request_callback: None,
 578            remaining_turns: u32::MAX,
 579            configured_model,
 580            profile: AgentProfile::new(profile_id, tools),
 581        }
 582    }
 583
 584    pub fn set_request_callback(
 585        &mut self,
 586        callback: impl 'static
 587        + FnMut(&LanguageModelRequest, &[Result<LanguageModelCompletionEvent, String>]),
 588    ) {
 589        self.request_callback = Some(Box::new(callback));
 590    }
 591
 592    pub fn id(&self) -> &ThreadId {
 593        &self.id
 594    }
 595
 596    pub fn profile(&self) -> &AgentProfile {
 597        &self.profile
 598    }
 599
 600    pub fn set_profile(&mut self, id: AgentProfileId, cx: &mut Context<Self>) {
 601        if &id != self.profile.id() {
 602            self.profile = AgentProfile::new(id, self.tools.clone());
 603            cx.emit(ThreadEvent::ProfileChanged);
 604        }
 605    }
 606
 607    pub fn is_empty(&self) -> bool {
 608        self.messages.is_empty()
 609    }
 610
 611    pub fn updated_at(&self) -> DateTime<Utc> {
 612        self.updated_at
 613    }
 614
 615    pub fn touch_updated_at(&mut self) {
 616        self.updated_at = Utc::now();
 617    }
 618
 619    pub fn advance_prompt_id(&mut self) {
 620        self.last_prompt_id = PromptId::new();
 621    }
 622
 623    pub fn project_context(&self) -> SharedProjectContext {
 624        self.project_context.clone()
 625    }
 626
 627    pub fn get_or_init_configured_model(&mut self, cx: &App) -> Option<ConfiguredModel> {
 628        if self.configured_model.is_none() {
 629            self.configured_model = LanguageModelRegistry::read_global(cx).default_model();
 630        }
 631        self.configured_model.clone()
 632    }
 633
 634    pub fn configured_model(&self) -> Option<ConfiguredModel> {
 635        self.configured_model.clone()
 636    }
 637
 638    pub fn set_configured_model(&mut self, model: Option<ConfiguredModel>, cx: &mut Context<Self>) {
 639        self.configured_model = model;
 640        cx.notify();
 641    }
 642
 643    pub fn summary(&self) -> &ThreadSummary {
 644        &self.summary
 645    }
 646
 647    pub fn set_summary(&mut self, new_summary: impl Into<SharedString>, cx: &mut Context<Self>) {
 648        let current_summary = match &self.summary {
 649            ThreadSummary::Pending | ThreadSummary::Generating => return,
 650            ThreadSummary::Ready(summary) => summary,
 651            ThreadSummary::Error => &ThreadSummary::DEFAULT,
 652        };
 653
 654        let mut new_summary = new_summary.into();
 655
 656        if new_summary.is_empty() {
 657            new_summary = ThreadSummary::DEFAULT;
 658        }
 659
 660        if current_summary != &new_summary {
 661            self.summary = ThreadSummary::Ready(new_summary);
 662            cx.emit(ThreadEvent::SummaryChanged);
 663        }
 664    }
 665
 666    pub fn completion_mode(&self) -> CompletionMode {
 667        self.completion_mode
 668    }
 669
 670    pub fn set_completion_mode(&mut self, mode: CompletionMode) {
 671        self.completion_mode = mode;
 672    }
 673
 674    pub fn message(&self, id: MessageId) -> Option<&Message> {
 675        let index = self
 676            .messages
 677            .binary_search_by(|message| message.id.cmp(&id))
 678            .ok()?;
 679
 680        self.messages.get(index)
 681    }
 682
 683    pub fn messages(&self) -> impl ExactSizeIterator<Item = &Message> {
 684        self.messages.iter()
 685    }
 686
 687    pub fn is_generating(&self) -> bool {
 688        !self.pending_completions.is_empty() || !self.all_tools_finished()
 689    }
 690
 691    /// Indicates whether streaming of language model events is stale.
 692    /// When `is_generating()` is false, this method returns `None`.
 693    pub fn is_generation_stale(&self) -> Option<bool> {
 694        const STALE_THRESHOLD: u128 = 250;
 695
 696        self.last_received_chunk_at
 697            .map(|instant| instant.elapsed().as_millis() > STALE_THRESHOLD)
 698    }
 699
 700    fn received_chunk(&mut self) {
 701        self.last_received_chunk_at = Some(Instant::now());
 702    }
 703
 704    pub fn queue_state(&self) -> Option<QueueState> {
 705        self.pending_completions
 706            .first()
 707            .map(|pending_completion| pending_completion.queue_state)
 708    }
 709
 710    pub fn tools(&self) -> &Entity<ToolWorkingSet> {
 711        &self.tools
 712    }
 713
 714    pub fn pending_tool(&self, id: &LanguageModelToolUseId) -> Option<&PendingToolUse> {
 715        self.tool_use
 716            .pending_tool_uses()
 717            .into_iter()
 718            .find(|tool_use| &tool_use.id == id)
 719    }
 720
 721    pub fn tools_needing_confirmation(&self) -> impl Iterator<Item = &PendingToolUse> {
 722        self.tool_use
 723            .pending_tool_uses()
 724            .into_iter()
 725            .filter(|tool_use| tool_use.status.needs_confirmation())
 726    }
 727
 728    pub fn has_pending_tool_uses(&self) -> bool {
 729        !self.tool_use.pending_tool_uses().is_empty()
 730    }
 731
 732    pub fn checkpoint_for_message(&self, id: MessageId) -> Option<ThreadCheckpoint> {
 733        self.checkpoints_by_message.get(&id).cloned()
 734    }
 735
 736    pub fn restore_checkpoint(
 737        &mut self,
 738        checkpoint: ThreadCheckpoint,
 739        cx: &mut Context<Self>,
 740    ) -> Task<Result<()>> {
 741        self.last_restore_checkpoint = Some(LastRestoreCheckpoint::Pending {
 742            message_id: checkpoint.message_id,
 743        });
 744        cx.emit(ThreadEvent::CheckpointChanged);
 745        cx.notify();
 746
 747        let git_store = self.project().read(cx).git_store().clone();
 748        let restore = git_store.update(cx, |git_store, cx| {
 749            git_store.restore_checkpoint(checkpoint.git_checkpoint.clone(), cx)
 750        });
 751
 752        cx.spawn(async move |this, cx| {
 753            let result = restore.await;
 754            this.update(cx, |this, cx| {
 755                if let Err(err) = result.as_ref() {
 756                    this.last_restore_checkpoint = Some(LastRestoreCheckpoint::Error {
 757                        message_id: checkpoint.message_id,
 758                        error: err.to_string(),
 759                    });
 760                } else {
 761                    this.truncate(checkpoint.message_id, cx);
 762                    this.last_restore_checkpoint = None;
 763                }
 764                this.pending_checkpoint = None;
 765                cx.emit(ThreadEvent::CheckpointChanged);
 766                cx.notify();
 767            })?;
 768            result
 769        })
 770    }
 771
 772    fn finalize_pending_checkpoint(&mut self, cx: &mut Context<Self>) {
 773        let pending_checkpoint = if self.is_generating() {
 774            return;
 775        } else if let Some(checkpoint) = self.pending_checkpoint.take() {
 776            checkpoint
 777        } else {
 778            return;
 779        };
 780
 781        self.finalize_checkpoint(pending_checkpoint, cx);
 782    }
 783
 784    fn finalize_checkpoint(
 785        &mut self,
 786        pending_checkpoint: ThreadCheckpoint,
 787        cx: &mut Context<Self>,
 788    ) {
 789        let git_store = self.project.read(cx).git_store().clone();
 790        let final_checkpoint = git_store.update(cx, |git_store, cx| git_store.checkpoint(cx));
 791        cx.spawn(async move |this, cx| match final_checkpoint.await {
 792            Ok(final_checkpoint) => {
 793                let equal = git_store
 794                    .update(cx, |store, cx| {
 795                        store.compare_checkpoints(
 796                            pending_checkpoint.git_checkpoint.clone(),
 797                            final_checkpoint.clone(),
 798                            cx,
 799                        )
 800                    })?
 801                    .await
 802                    .unwrap_or(false);
 803
 804                if !equal {
 805                    this.update(cx, |this, cx| {
 806                        this.insert_checkpoint(pending_checkpoint, cx)
 807                    })?;
 808                }
 809
 810                Ok(())
 811            }
 812            Err(_) => this.update(cx, |this, cx| {
 813                this.insert_checkpoint(pending_checkpoint, cx)
 814            }),
 815        })
 816        .detach();
 817    }
 818
 819    fn insert_checkpoint(&mut self, checkpoint: ThreadCheckpoint, cx: &mut Context<Self>) {
 820        self.checkpoints_by_message
 821            .insert(checkpoint.message_id, checkpoint);
 822        cx.emit(ThreadEvent::CheckpointChanged);
 823        cx.notify();
 824    }
 825
 826    pub fn last_restore_checkpoint(&self) -> Option<&LastRestoreCheckpoint> {
 827        self.last_restore_checkpoint.as_ref()
 828    }
 829
 830    pub fn truncate(&mut self, message_id: MessageId, cx: &mut Context<Self>) {
 831        let Some(message_ix) = self
 832            .messages
 833            .iter()
 834            .rposition(|message| message.id == message_id)
 835        else {
 836            return;
 837        };
 838        for deleted_message in self.messages.drain(message_ix..) {
 839            self.checkpoints_by_message.remove(&deleted_message.id);
 840        }
 841        cx.notify();
 842    }
 843
 844    pub fn context_for_message(&self, id: MessageId) -> impl Iterator<Item = &AgentContext> {
 845        self.messages
 846            .iter()
 847            .find(|message| message.id == id)
 848            .into_iter()
 849            .flat_map(|message| message.loaded_context.contexts.iter())
 850    }
 851
 852    pub fn is_turn_end(&self, ix: usize) -> bool {
 853        if self.messages.is_empty() {
 854            return false;
 855        }
 856
 857        if !self.is_generating() && ix == self.messages.len() - 1 {
 858            return true;
 859        }
 860
 861        let Some(message) = self.messages.get(ix) else {
 862            return false;
 863        };
 864
 865        if message.role != Role::Assistant {
 866            return false;
 867        }
 868
 869        self.messages
 870            .get(ix + 1)
 871            .and_then(|message| {
 872                self.message(message.id)
 873                    .map(|next_message| next_message.role == Role::User && !next_message.is_hidden)
 874            })
 875            .unwrap_or(false)
 876    }
 877
 878    pub fn last_usage(&self) -> Option<RequestUsage> {
 879        self.last_usage
 880    }
 881
 882    pub fn tool_use_limit_reached(&self) -> bool {
 883        self.tool_use_limit_reached
 884    }
 885
 886    /// Returns whether all of the tool uses have finished running.
 887    pub fn all_tools_finished(&self) -> bool {
 888        // If the only pending tool uses left are the ones with errors, then
 889        // that means that we've finished running all of the pending tools.
 890        self.tool_use
 891            .pending_tool_uses()
 892            .iter()
 893            .all(|pending_tool_use| pending_tool_use.status.is_error())
 894    }
 895
 896    /// Returns whether any pending tool uses may perform edits
 897    pub fn has_pending_edit_tool_uses(&self) -> bool {
 898        self.tool_use
 899            .pending_tool_uses()
 900            .iter()
 901            .filter(|pending_tool_use| !pending_tool_use.status.is_error())
 902            .any(|pending_tool_use| pending_tool_use.may_perform_edits)
 903    }
 904
 905    pub fn tool_uses_for_message(&self, id: MessageId, cx: &App) -> Vec<ToolUse> {
 906        self.tool_use.tool_uses_for_message(id, cx)
 907    }
 908
 909    pub fn tool_results_for_message(
 910        &self,
 911        assistant_message_id: MessageId,
 912    ) -> Vec<&LanguageModelToolResult> {
 913        self.tool_use.tool_results_for_message(assistant_message_id)
 914    }
 915
 916    pub fn tool_result(&self, id: &LanguageModelToolUseId) -> Option<&LanguageModelToolResult> {
 917        self.tool_use.tool_result(id)
 918    }
 919
 920    pub fn output_for_tool(&self, id: &LanguageModelToolUseId) -> Option<&Arc<str>> {
 921        match &self.tool_use.tool_result(id)?.content {
 922            LanguageModelToolResultContent::Text(text) => Some(text),
 923            LanguageModelToolResultContent::Image(_) => {
 924                // TODO: We should display image
 925                None
 926            }
 927        }
 928    }
 929
 930    pub fn card_for_tool(&self, id: &LanguageModelToolUseId) -> Option<AnyToolCard> {
 931        self.tool_use.tool_result_card(id).cloned()
 932    }
 933
 934    /// Return tools that are both enabled and supported by the model
 935    pub fn available_tools(
 936        &self,
 937        cx: &App,
 938        model: Arc<dyn LanguageModel>,
 939    ) -> Vec<LanguageModelRequestTool> {
 940        if model.supports_tools() {
 941            self.profile
 942                .enabled_tools(cx)
 943                .into_iter()
 944                .filter_map(|tool| {
 945                    // Skip tools that cannot be supported
 946                    let input_schema = tool.input_schema(model.tool_input_format()).ok()?;
 947                    Some(LanguageModelRequestTool {
 948                        name: tool.name(),
 949                        description: tool.description(),
 950                        input_schema,
 951                    })
 952                })
 953                .collect()
 954        } else {
 955            Vec::default()
 956        }
 957    }
 958
 959    pub fn insert_user_message(
 960        &mut self,
 961        text: impl Into<String>,
 962        loaded_context: ContextLoadResult,
 963        git_checkpoint: Option<GitStoreCheckpoint>,
 964        creases: Vec<MessageCrease>,
 965        cx: &mut Context<Self>,
 966    ) -> MessageId {
 967        if !loaded_context.referenced_buffers.is_empty() {
 968            self.action_log.update(cx, |log, cx| {
 969                for buffer in loaded_context.referenced_buffers {
 970                    log.buffer_read(buffer, cx);
 971                }
 972            });
 973        }
 974
 975        let message_id = self.insert_message(
 976            Role::User,
 977            vec![MessageSegment::Text(text.into())],
 978            loaded_context.loaded_context,
 979            creases,
 980            false,
 981            cx,
 982        );
 983
 984        if let Some(git_checkpoint) = git_checkpoint {
 985            self.pending_checkpoint = Some(ThreadCheckpoint {
 986                message_id,
 987                git_checkpoint,
 988            });
 989        }
 990
 991        self.auto_capture_telemetry(cx);
 992
 993        message_id
 994    }
 995
 996    pub fn insert_invisible_continue_message(&mut self, cx: &mut Context<Self>) -> MessageId {
 997        let id = self.insert_message(
 998            Role::User,
 999            vec![MessageSegment::Text("Continue where you left off".into())],
1000            LoadedContext::default(),
1001            vec![],
1002            true,
1003            cx,
1004        );
1005        self.pending_checkpoint = None;
1006
1007        id
1008    }
1009
1010    pub fn insert_assistant_message(
1011        &mut self,
1012        segments: Vec<MessageSegment>,
1013        cx: &mut Context<Self>,
1014    ) -> MessageId {
1015        self.insert_message(
1016            Role::Assistant,
1017            segments,
1018            LoadedContext::default(),
1019            Vec::new(),
1020            false,
1021            cx,
1022        )
1023    }
1024
1025    pub fn insert_message(
1026        &mut self,
1027        role: Role,
1028        segments: Vec<MessageSegment>,
1029        loaded_context: LoadedContext,
1030        creases: Vec<MessageCrease>,
1031        is_hidden: bool,
1032        cx: &mut Context<Self>,
1033    ) -> MessageId {
1034        let id = self.next_message_id.post_inc();
1035        self.messages.push(Message {
1036            id,
1037            role,
1038            segments,
1039            loaded_context,
1040            creases,
1041            is_hidden,
1042        });
1043        self.touch_updated_at();
1044        cx.emit(ThreadEvent::MessageAdded(id));
1045        id
1046    }
1047
1048    pub fn edit_message(
1049        &mut self,
1050        id: MessageId,
1051        new_role: Role,
1052        new_segments: Vec<MessageSegment>,
1053        creases: Vec<MessageCrease>,
1054        loaded_context: Option<LoadedContext>,
1055        checkpoint: Option<GitStoreCheckpoint>,
1056        cx: &mut Context<Self>,
1057    ) -> bool {
1058        let Some(message) = self.messages.iter_mut().find(|message| message.id == id) else {
1059            return false;
1060        };
1061        message.role = new_role;
1062        message.segments = new_segments;
1063        message.creases = creases;
1064        if let Some(context) = loaded_context {
1065            message.loaded_context = context;
1066        }
1067        if let Some(git_checkpoint) = checkpoint {
1068            self.checkpoints_by_message.insert(
1069                id,
1070                ThreadCheckpoint {
1071                    message_id: id,
1072                    git_checkpoint,
1073                },
1074            );
1075        }
1076        self.touch_updated_at();
1077        cx.emit(ThreadEvent::MessageEdited(id));
1078        true
1079    }
1080
1081    pub fn delete_message(&mut self, id: MessageId, cx: &mut Context<Self>) -> bool {
1082        let Some(index) = self.messages.iter().position(|message| message.id == id) else {
1083            return false;
1084        };
1085        self.messages.remove(index);
1086        self.touch_updated_at();
1087        cx.emit(ThreadEvent::MessageDeleted(id));
1088        true
1089    }
1090
1091    /// Returns the representation of this [`Thread`] in a textual form.
1092    ///
1093    /// This is the representation we use when attaching a thread as context to another thread.
1094    pub fn text(&self) -> String {
1095        let mut text = String::new();
1096
1097        for message in &self.messages {
1098            text.push_str(match message.role {
1099                language_model::Role::User => "User:",
1100                language_model::Role::Assistant => "Agent:",
1101                language_model::Role::System => "System:",
1102            });
1103            text.push('\n');
1104
1105            for segment in &message.segments {
1106                match segment {
1107                    MessageSegment::Text(content) => text.push_str(content),
1108                    MessageSegment::Thinking { text: content, .. } => {
1109                        text.push_str(&format!("<think>{}</think>", content))
1110                    }
1111                    MessageSegment::RedactedThinking(_) => {}
1112                }
1113            }
1114            text.push('\n');
1115        }
1116
1117        text
1118    }
1119
1120    /// Serializes this thread into a format for storage or telemetry.
1121    pub fn serialize(&self, cx: &mut Context<Self>) -> Task<Result<SerializedThread>> {
1122        let initial_project_snapshot = self.initial_project_snapshot.clone();
1123        cx.spawn(async move |this, cx| {
1124            let initial_project_snapshot = initial_project_snapshot.await;
1125            this.read_with(cx, |this, cx| SerializedThread {
1126                version: SerializedThread::VERSION.to_string(),
1127                summary: this.summary().or_default(),
1128                updated_at: this.updated_at(),
1129                messages: this
1130                    .messages()
1131                    .map(|message| SerializedMessage {
1132                        id: message.id,
1133                        role: message.role,
1134                        segments: message
1135                            .segments
1136                            .iter()
1137                            .map(|segment| match segment {
1138                                MessageSegment::Text(text) => {
1139                                    SerializedMessageSegment::Text { text: text.clone() }
1140                                }
1141                                MessageSegment::Thinking { text, signature } => {
1142                                    SerializedMessageSegment::Thinking {
1143                                        text: text.clone(),
1144                                        signature: signature.clone(),
1145                                    }
1146                                }
1147                                MessageSegment::RedactedThinking(data) => {
1148                                    SerializedMessageSegment::RedactedThinking {
1149                                        data: data.clone(),
1150                                    }
1151                                }
1152                            })
1153                            .collect(),
1154                        tool_uses: this
1155                            .tool_uses_for_message(message.id, cx)
1156                            .into_iter()
1157                            .map(|tool_use| SerializedToolUse {
1158                                id: tool_use.id,
1159                                name: tool_use.name,
1160                                input: tool_use.input,
1161                            })
1162                            .collect(),
1163                        tool_results: this
1164                            .tool_results_for_message(message.id)
1165                            .into_iter()
1166                            .map(|tool_result| SerializedToolResult {
1167                                tool_use_id: tool_result.tool_use_id.clone(),
1168                                is_error: tool_result.is_error,
1169                                content: tool_result.content.clone(),
1170                                output: tool_result.output.clone(),
1171                            })
1172                            .collect(),
1173                        context: message.loaded_context.text.clone(),
1174                        creases: message
1175                            .creases
1176                            .iter()
1177                            .map(|crease| SerializedCrease {
1178                                start: crease.range.start,
1179                                end: crease.range.end,
1180                                icon_path: crease.metadata.icon_path.clone(),
1181                                label: crease.metadata.label.clone(),
1182                            })
1183                            .collect(),
1184                        is_hidden: message.is_hidden,
1185                    })
1186                    .collect(),
1187                initial_project_snapshot,
1188                cumulative_token_usage: this.cumulative_token_usage,
1189                request_token_usage: this.request_token_usage.clone(),
1190                detailed_summary_state: this.detailed_summary_rx.borrow().clone(),
1191                exceeded_window_error: this.exceeded_window_error.clone(),
1192                model: this
1193                    .configured_model
1194                    .as_ref()
1195                    .map(|model| SerializedLanguageModel {
1196                        provider: model.provider.id().0.to_string(),
1197                        model: model.model.id().0.to_string(),
1198                    }),
1199                completion_mode: Some(this.completion_mode),
1200                tool_use_limit_reached: this.tool_use_limit_reached,
1201                profile: Some(this.profile.id().clone()),
1202            })
1203        })
1204    }
1205
1206    pub fn remaining_turns(&self) -> u32 {
1207        self.remaining_turns
1208    }
1209
1210    pub fn set_remaining_turns(&mut self, remaining_turns: u32) {
1211        self.remaining_turns = remaining_turns;
1212    }
1213
1214    pub fn send_to_model(
1215        &mut self,
1216        model: Arc<dyn LanguageModel>,
1217        intent: CompletionIntent,
1218        window: Option<AnyWindowHandle>,
1219        cx: &mut Context<Self>,
1220    ) {
1221        if self.remaining_turns == 0 {
1222            return;
1223        }
1224
1225        self.remaining_turns -= 1;
1226
1227        let request = self.to_completion_request(model.clone(), intent, cx);
1228
1229        self.stream_completion(request, model, window, cx);
1230    }
1231
1232    pub fn used_tools_since_last_user_message(&self) -> bool {
1233        for message in self.messages.iter().rev() {
1234            if self.tool_use.message_has_tool_results(message.id) {
1235                return true;
1236            } else if message.role == Role::User {
1237                return false;
1238            }
1239        }
1240
1241        false
1242    }
1243
1244    pub fn to_completion_request(
1245        &self,
1246        model: Arc<dyn LanguageModel>,
1247        intent: CompletionIntent,
1248        cx: &mut Context<Self>,
1249    ) -> LanguageModelRequest {
1250        let mut request = LanguageModelRequest {
1251            thread_id: Some(self.id.to_string()),
1252            prompt_id: Some(self.last_prompt_id.to_string()),
1253            intent: Some(intent),
1254            mode: None,
1255            messages: vec![],
1256            tools: Vec::new(),
1257            tool_choice: None,
1258            stop: Vec::new(),
1259            temperature: AgentSettings::temperature_for_model(&model, cx),
1260        };
1261
1262        let available_tools = self.available_tools(cx, model.clone());
1263        let available_tool_names = available_tools
1264            .iter()
1265            .map(|tool| tool.name.clone())
1266            .collect();
1267
1268        let model_context = &ModelContext {
1269            available_tools: available_tool_names,
1270        };
1271
1272        if let Some(project_context) = self.project_context.borrow().as_ref() {
1273            match self
1274                .prompt_builder
1275                .generate_assistant_system_prompt(project_context, model_context)
1276            {
1277                Err(err) => {
1278                    let message = format!("{err:?}").into();
1279                    log::error!("{message}");
1280                    cx.emit(ThreadEvent::ShowError(ThreadError::Message {
1281                        header: "Error generating system prompt".into(),
1282                        message,
1283                    }));
1284                }
1285                Ok(system_prompt) => {
1286                    request.messages.push(LanguageModelRequestMessage {
1287                        role: Role::System,
1288                        content: vec![MessageContent::Text(system_prompt)],
1289                        cache: true,
1290                    });
1291                }
1292            }
1293        } else {
1294            let message = "Context for system prompt unexpectedly not ready.".into();
1295            log::error!("{message}");
1296            cx.emit(ThreadEvent::ShowError(ThreadError::Message {
1297                header: "Error generating system prompt".into(),
1298                message,
1299            }));
1300        }
1301
1302        let mut message_ix_to_cache = None;
1303        for message in &self.messages {
1304            let mut request_message = LanguageModelRequestMessage {
1305                role: message.role,
1306                content: Vec::new(),
1307                cache: false,
1308            };
1309
1310            message
1311                .loaded_context
1312                .add_to_request_message(&mut request_message);
1313
1314            for segment in &message.segments {
1315                match segment {
1316                    MessageSegment::Text(text) => {
1317                        if !text.is_empty() {
1318                            request_message
1319                                .content
1320                                .push(MessageContent::Text(text.into()));
1321                        }
1322                    }
1323                    MessageSegment::Thinking { text, signature } => {
1324                        if !text.is_empty() {
1325                            request_message.content.push(MessageContent::Thinking {
1326                                text: text.into(),
1327                                signature: signature.clone(),
1328                            });
1329                        }
1330                    }
1331                    MessageSegment::RedactedThinking(data) => {
1332                        request_message
1333                            .content
1334                            .push(MessageContent::RedactedThinking(data.clone()));
1335                    }
1336                };
1337            }
1338
1339            let mut cache_message = true;
1340            let mut tool_results_message = LanguageModelRequestMessage {
1341                role: Role::User,
1342                content: Vec::new(),
1343                cache: false,
1344            };
1345            for (tool_use, tool_result) in self.tool_use.tool_results(message.id) {
1346                if let Some(tool_result) = tool_result {
1347                    request_message
1348                        .content
1349                        .push(MessageContent::ToolUse(tool_use.clone()));
1350                    tool_results_message
1351                        .content
1352                        .push(MessageContent::ToolResult(LanguageModelToolResult {
1353                            tool_use_id: tool_use.id.clone(),
1354                            tool_name: tool_result.tool_name.clone(),
1355                            is_error: tool_result.is_error,
1356                            content: if tool_result.content.is_empty() {
1357                                // Surprisingly, the API fails if we return an empty string here.
1358                                // It thinks we are sending a tool use without a tool result.
1359                                "<Tool returned an empty string>".into()
1360                            } else {
1361                                tool_result.content.clone()
1362                            },
1363                            output: None,
1364                        }));
1365                } else {
1366                    cache_message = false;
1367                    log::debug!(
1368                        "skipped tool use {:?} because it is still pending",
1369                        tool_use
1370                    );
1371                }
1372            }
1373
1374            if cache_message {
1375                message_ix_to_cache = Some(request.messages.len());
1376            }
1377            request.messages.push(request_message);
1378
1379            if !tool_results_message.content.is_empty() {
1380                if cache_message {
1381                    message_ix_to_cache = Some(request.messages.len());
1382                }
1383                request.messages.push(tool_results_message);
1384            }
1385        }
1386
1387        // https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching
1388        if let Some(message_ix_to_cache) = message_ix_to_cache {
1389            request.messages[message_ix_to_cache].cache = true;
1390        }
1391
1392        self.attached_tracked_files_state(&mut request.messages, cx);
1393
1394        request.tools = available_tools;
1395        request.mode = if model.supports_max_mode() {
1396            Some(self.completion_mode.into())
1397        } else {
1398            Some(CompletionMode::Normal.into())
1399        };
1400
1401        request
1402    }
1403
1404    fn to_summarize_request(
1405        &self,
1406        model: &Arc<dyn LanguageModel>,
1407        intent: CompletionIntent,
1408        added_user_message: String,
1409        cx: &App,
1410    ) -> LanguageModelRequest {
1411        let mut request = LanguageModelRequest {
1412            thread_id: None,
1413            prompt_id: None,
1414            intent: Some(intent),
1415            mode: None,
1416            messages: vec![],
1417            tools: Vec::new(),
1418            tool_choice: None,
1419            stop: Vec::new(),
1420            temperature: AgentSettings::temperature_for_model(model, cx),
1421        };
1422
1423        for message in &self.messages {
1424            let mut request_message = LanguageModelRequestMessage {
1425                role: message.role,
1426                content: Vec::new(),
1427                cache: false,
1428            };
1429
1430            for segment in &message.segments {
1431                match segment {
1432                    MessageSegment::Text(text) => request_message
1433                        .content
1434                        .push(MessageContent::Text(text.clone())),
1435                    MessageSegment::Thinking { .. } => {}
1436                    MessageSegment::RedactedThinking(_) => {}
1437                }
1438            }
1439
1440            if request_message.content.is_empty() {
1441                continue;
1442            }
1443
1444            request.messages.push(request_message);
1445        }
1446
1447        request.messages.push(LanguageModelRequestMessage {
1448            role: Role::User,
1449            content: vec![MessageContent::Text(added_user_message)],
1450            cache: false,
1451        });
1452
1453        request
1454    }
1455
1456    fn attached_tracked_files_state(
1457        &self,
1458        messages: &mut Vec<LanguageModelRequestMessage>,
1459        cx: &App,
1460    ) {
1461        const STALE_FILES_HEADER: &str = include_str!("./prompts/stale_files_prompt_header.txt");
1462
1463        let mut stale_message = String::new();
1464
1465        let action_log = self.action_log.read(cx);
1466
1467        for stale_file in action_log.stale_buffers(cx) {
1468            let Some(file) = stale_file.read(cx).file() else {
1469                continue;
1470            };
1471
1472            if stale_message.is_empty() {
1473                write!(&mut stale_message, "{}\n", STALE_FILES_HEADER.trim()).ok();
1474            }
1475
1476            writeln!(&mut stale_message, "- {}", file.path().display()).ok();
1477        }
1478
1479        let mut content = Vec::with_capacity(2);
1480
1481        if !stale_message.is_empty() {
1482            content.push(stale_message.into());
1483        }
1484
1485        if !content.is_empty() {
1486            let context_message = LanguageModelRequestMessage {
1487                role: Role::User,
1488                content,
1489                cache: false,
1490            };
1491
1492            messages.push(context_message);
1493        }
1494    }
1495
1496    pub fn stream_completion(
1497        &mut self,
1498        request: LanguageModelRequest,
1499        model: Arc<dyn LanguageModel>,
1500        window: Option<AnyWindowHandle>,
1501        cx: &mut Context<Self>,
1502    ) {
1503        self.tool_use_limit_reached = false;
1504
1505        let pending_completion_id = post_inc(&mut self.completion_count);
1506        let mut request_callback_parameters = if self.request_callback.is_some() {
1507            Some((request.clone(), Vec::new()))
1508        } else {
1509            None
1510        };
1511        let prompt_id = self.last_prompt_id.clone();
1512        let tool_use_metadata = ToolUseMetadata {
1513            model: model.clone(),
1514            thread_id: self.id.clone(),
1515            prompt_id: prompt_id.clone(),
1516        };
1517
1518        self.last_received_chunk_at = Some(Instant::now());
1519
1520        let task = cx.spawn(async move |thread, cx| {
1521            let stream_completion_future = model.stream_completion(request, &cx);
1522            let initial_token_usage =
1523                thread.read_with(cx, |thread, _cx| thread.cumulative_token_usage);
1524            let stream_completion = async {
1525                let mut events = stream_completion_future.await?;
1526
1527                let mut stop_reason = StopReason::EndTurn;
1528                let mut current_token_usage = TokenUsage::default();
1529
1530                thread
1531                    .update(cx, |_thread, cx| {
1532                        cx.emit(ThreadEvent::NewRequest);
1533                    })
1534                    .ok();
1535
1536                let mut request_assistant_message_id = None;
1537
1538                while let Some(event) = events.next().await {
1539                    if let Some((_, response_events)) = request_callback_parameters.as_mut() {
1540                        response_events
1541                            .push(event.as_ref().map_err(|error| error.to_string()).cloned());
1542                    }
1543
1544                    thread.update(cx, |thread, cx| {
1545                        let event = match event {
1546                            Ok(event) => event,
1547                            Err(LanguageModelCompletionError::BadInputJson {
1548                                id,
1549                                tool_name,
1550                                raw_input: invalid_input_json,
1551                                json_parse_error,
1552                            }) => {
1553                                thread.receive_invalid_tool_json(
1554                                    id,
1555                                    tool_name,
1556                                    invalid_input_json,
1557                                    json_parse_error,
1558                                    window,
1559                                    cx,
1560                                );
1561                                return Ok(());
1562                            }
1563                            Err(LanguageModelCompletionError::Other(error)) => {
1564                                return Err(error);
1565                            }
1566                            Err(err @ LanguageModelCompletionError::RateLimit(..)) => {
1567                                return Err(err.into());
1568                            }
1569                        };
1570
1571                        match event {
1572                            LanguageModelCompletionEvent::StartMessage { .. } => {
1573                                request_assistant_message_id =
1574                                    Some(thread.insert_assistant_message(
1575                                        vec![MessageSegment::Text(String::new())],
1576                                        cx,
1577                                    ));
1578                            }
1579                            LanguageModelCompletionEvent::Stop(reason) => {
1580                                stop_reason = reason;
1581                            }
1582                            LanguageModelCompletionEvent::UsageUpdate(token_usage) => {
1583                                thread.update_token_usage_at_last_message(token_usage);
1584                                thread.cumulative_token_usage = thread.cumulative_token_usage
1585                                    + token_usage
1586                                    - current_token_usage;
1587                                current_token_usage = token_usage;
1588                            }
1589                            LanguageModelCompletionEvent::Text(chunk) => {
1590                                thread.received_chunk();
1591
1592                                cx.emit(ThreadEvent::ReceivedTextChunk);
1593                                if let Some(last_message) = thread.messages.last_mut() {
1594                                    if last_message.role == Role::Assistant
1595                                        && !thread.tool_use.has_tool_results(last_message.id)
1596                                    {
1597                                        last_message.push_text(&chunk);
1598                                        cx.emit(ThreadEvent::StreamedAssistantText(
1599                                            last_message.id,
1600                                            chunk,
1601                                        ));
1602                                    } else {
1603                                        // If we won't have an Assistant message yet, assume this chunk marks the beginning
1604                                        // of a new Assistant response.
1605                                        //
1606                                        // Importantly: We do *not* want to emit a `StreamedAssistantText` event here, as it
1607                                        // will result in duplicating the text of the chunk in the rendered Markdown.
1608                                        request_assistant_message_id =
1609                                            Some(thread.insert_assistant_message(
1610                                                vec![MessageSegment::Text(chunk.to_string())],
1611                                                cx,
1612                                            ));
1613                                    };
1614                                }
1615                            }
1616                            LanguageModelCompletionEvent::Thinking {
1617                                text: chunk,
1618                                signature,
1619                            } => {
1620                                thread.received_chunk();
1621
1622                                if let Some(last_message) = thread.messages.last_mut() {
1623                                    if last_message.role == Role::Assistant
1624                                        && !thread.tool_use.has_tool_results(last_message.id)
1625                                    {
1626                                        last_message.push_thinking(&chunk, signature);
1627                                        cx.emit(ThreadEvent::StreamedAssistantThinking(
1628                                            last_message.id,
1629                                            chunk,
1630                                        ));
1631                                    } else {
1632                                        // If we won't have an Assistant message yet, assume this chunk marks the beginning
1633                                        // of a new Assistant response.
1634                                        //
1635                                        // Importantly: We do *not* want to emit a `StreamedAssistantText` event here, as it
1636                                        // will result in duplicating the text of the chunk in the rendered Markdown.
1637                                        request_assistant_message_id =
1638                                            Some(thread.insert_assistant_message(
1639                                                vec![MessageSegment::Thinking {
1640                                                    text: chunk.to_string(),
1641                                                    signature,
1642                                                }],
1643                                                cx,
1644                                            ));
1645                                    };
1646                                }
1647                            }
1648                            LanguageModelCompletionEvent::ToolUse(tool_use) => {
1649                                let last_assistant_message_id = request_assistant_message_id
1650                                    .unwrap_or_else(|| {
1651                                        let new_assistant_message_id =
1652                                            thread.insert_assistant_message(vec![], cx);
1653                                        request_assistant_message_id =
1654                                            Some(new_assistant_message_id);
1655                                        new_assistant_message_id
1656                                    });
1657
1658                                let tool_use_id = tool_use.id.clone();
1659                                let streamed_input = if tool_use.is_input_complete {
1660                                    None
1661                                } else {
1662                                    Some((&tool_use.input).clone())
1663                                };
1664
1665                                let ui_text = thread.tool_use.request_tool_use(
1666                                    last_assistant_message_id,
1667                                    tool_use,
1668                                    tool_use_metadata.clone(),
1669                                    cx,
1670                                );
1671
1672                                if let Some(input) = streamed_input {
1673                                    cx.emit(ThreadEvent::StreamedToolUse {
1674                                        tool_use_id,
1675                                        ui_text,
1676                                        input,
1677                                    });
1678                                }
1679                            }
1680                            LanguageModelCompletionEvent::StatusUpdate(status_update) => {
1681                                if let Some(completion) = thread
1682                                    .pending_completions
1683                                    .iter_mut()
1684                                    .find(|completion| completion.id == pending_completion_id)
1685                                {
1686                                    match status_update {
1687                                        CompletionRequestStatus::Queued {
1688                                            position,
1689                                        } => {
1690                                            completion.queue_state = QueueState::Queued { position };
1691                                        }
1692                                        CompletionRequestStatus::Started => {
1693                                            completion.queue_state =  QueueState::Started;
1694                                        }
1695                                        CompletionRequestStatus::Failed {
1696                                            code, message, request_id
1697                                        } => {
1698                                            anyhow::bail!("completion request failed. request_id: {request_id}, code: {code}, message: {message}");
1699                                        }
1700                                        CompletionRequestStatus::UsageUpdated {
1701                                            amount, limit
1702                                        } => {
1703                                            let usage = RequestUsage { limit, amount: amount as i32 };
1704
1705                                            thread.last_usage = Some(usage);
1706                                        }
1707                                        CompletionRequestStatus::ToolUseLimitReached => {
1708                                            thread.tool_use_limit_reached = true;
1709                                            cx.emit(ThreadEvent::ToolUseLimitReached);
1710                                        }
1711                                    }
1712                                }
1713                            }
1714                        }
1715
1716                        thread.touch_updated_at();
1717                        cx.emit(ThreadEvent::StreamedCompletion);
1718                        cx.notify();
1719
1720                        thread.auto_capture_telemetry(cx);
1721                        Ok(())
1722                    })??;
1723
1724                    smol::future::yield_now().await;
1725                }
1726
1727                thread.update(cx, |thread, cx| {
1728                    thread.last_received_chunk_at = None;
1729                    thread
1730                        .pending_completions
1731                        .retain(|completion| completion.id != pending_completion_id);
1732
1733                    // If there is a response without tool use, summarize the message. Otherwise,
1734                    // allow two tool uses before summarizing.
1735                    if matches!(thread.summary, ThreadSummary::Pending)
1736                        && thread.messages.len() >= 2
1737                        && (!thread.has_pending_tool_uses() || thread.messages.len() >= 6)
1738                    {
1739                        thread.summarize(cx);
1740                    }
1741                })?;
1742
1743                anyhow::Ok(stop_reason)
1744            };
1745
1746            let result = stream_completion.await;
1747
1748            thread
1749                .update(cx, |thread, cx| {
1750                    thread.finalize_pending_checkpoint(cx);
1751                    match result.as_ref() {
1752                        Ok(stop_reason) => match stop_reason {
1753                            StopReason::ToolUse => {
1754                                let tool_uses = thread.use_pending_tools(window, cx, model.clone());
1755                                cx.emit(ThreadEvent::UsePendingTools { tool_uses });
1756                            }
1757                            StopReason::EndTurn | StopReason::MaxTokens  => {
1758                                thread.project.update(cx, |project, cx| {
1759                                    project.set_agent_location(None, cx);
1760                                });
1761                            }
1762                            StopReason::Refusal => {
1763                                thread.project.update(cx, |project, cx| {
1764                                    project.set_agent_location(None, cx);
1765                                });
1766
1767                                // Remove the turn that was refused.
1768                                //
1769                                // https://docs.anthropic.com/en/docs/test-and-evaluate/strengthen-guardrails/handle-streaming-refusals#reset-context-after-refusal
1770                                {
1771                                    let mut messages_to_remove = Vec::new();
1772
1773                                    for (ix, message) in thread.messages.iter().enumerate().rev() {
1774                                        messages_to_remove.push(message.id);
1775
1776                                        if message.role == Role::User {
1777                                            if ix == 0 {
1778                                                break;
1779                                            }
1780
1781                                            if let Some(prev_message) = thread.messages.get(ix - 1) {
1782                                                if prev_message.role == Role::Assistant {
1783                                                    break;
1784                                                }
1785                                            }
1786                                        }
1787                                    }
1788
1789                                    for message_id in messages_to_remove {
1790                                        thread.delete_message(message_id, cx);
1791                                    }
1792                                }
1793
1794                                cx.emit(ThreadEvent::ShowError(ThreadError::Message {
1795                                    header: "Language model refusal".into(),
1796                                    message: "Model refused to generate content for safety reasons.".into(),
1797                                }));
1798                            }
1799                        },
1800                        Err(error) => {
1801                            thread.project.update(cx, |project, cx| {
1802                                project.set_agent_location(None, cx);
1803                            });
1804
1805                            if error.is::<PaymentRequiredError>() {
1806                                cx.emit(ThreadEvent::ShowError(ThreadError::PaymentRequired));
1807                            } else if let Some(error) =
1808                                error.downcast_ref::<ModelRequestLimitReachedError>()
1809                            {
1810                                cx.emit(ThreadEvent::ShowError(
1811                                    ThreadError::ModelRequestLimitReached { plan: error.plan },
1812                                ));
1813                            } else if let Some(known_error) =
1814                                error.downcast_ref::<LanguageModelKnownError>()
1815                            {
1816                                match known_error {
1817                                    LanguageModelKnownError::ContextWindowLimitExceeded {
1818                                        tokens,
1819                                    } => {
1820                                        thread.exceeded_window_error = Some(ExceededWindowError {
1821                                            model_id: model.id(),
1822                                            token_count: *tokens,
1823                                        });
1824                                        cx.notify();
1825                                    }
1826                                }
1827                            } else {
1828                                let error_message = error
1829                                    .chain()
1830                                    .map(|err| err.to_string())
1831                                    .collect::<Vec<_>>()
1832                                    .join("\n");
1833                                cx.emit(ThreadEvent::ShowError(ThreadError::Message {
1834                                    header: "Error interacting with language model".into(),
1835                                    message: SharedString::from(error_message.clone()),
1836                                }));
1837                            }
1838
1839                            thread.cancel_last_completion(window, cx);
1840                        }
1841                    }
1842
1843                    cx.emit(ThreadEvent::Stopped(result.map_err(Arc::new)));
1844
1845                    if let Some((request_callback, (request, response_events))) = thread
1846                        .request_callback
1847                        .as_mut()
1848                        .zip(request_callback_parameters.as_ref())
1849                    {
1850                        request_callback(request, response_events);
1851                    }
1852
1853                    thread.auto_capture_telemetry(cx);
1854
1855                    if let Ok(initial_usage) = initial_token_usage {
1856                        let usage = thread.cumulative_token_usage - initial_usage;
1857
1858                        telemetry::event!(
1859                            "Assistant Thread Completion",
1860                            thread_id = thread.id().to_string(),
1861                            prompt_id = prompt_id,
1862                            model = model.telemetry_id(),
1863                            model_provider = model.provider_id().to_string(),
1864                            input_tokens = usage.input_tokens,
1865                            output_tokens = usage.output_tokens,
1866                            cache_creation_input_tokens = usage.cache_creation_input_tokens,
1867                            cache_read_input_tokens = usage.cache_read_input_tokens,
1868                        );
1869                    }
1870                })
1871                .ok();
1872        });
1873
1874        self.pending_completions.push(PendingCompletion {
1875            id: pending_completion_id,
1876            queue_state: QueueState::Sending,
1877            _task: task,
1878        });
1879    }
1880
1881    pub fn summarize(&mut self, cx: &mut Context<Self>) {
1882        let Some(model) = LanguageModelRegistry::read_global(cx).thread_summary_model() else {
1883            println!("No thread summary model");
1884            return;
1885        };
1886
1887        if !model.provider.is_authenticated(cx) {
1888            return;
1889        }
1890
1891        let added_user_message = include_str!("./prompts/summarize_thread_prompt.txt");
1892
1893        let request = self.to_summarize_request(
1894            &model.model,
1895            CompletionIntent::ThreadSummarization,
1896            added_user_message.into(),
1897            cx,
1898        );
1899
1900        self.summary = ThreadSummary::Generating;
1901
1902        self.pending_summary = cx.spawn(async move |this, cx| {
1903            let result = async {
1904                let mut messages = model.model.stream_completion(request, &cx).await?;
1905
1906                let mut new_summary = String::new();
1907                while let Some(event) = messages.next().await {
1908                    let Ok(event) = event else {
1909                        continue;
1910                    };
1911                    let text = match event {
1912                        LanguageModelCompletionEvent::Text(text) => text,
1913                        LanguageModelCompletionEvent::StatusUpdate(
1914                            CompletionRequestStatus::UsageUpdated { amount, limit },
1915                        ) => {
1916                            this.update(cx, |thread, _cx| {
1917                                thread.last_usage = Some(RequestUsage {
1918                                    limit,
1919                                    amount: amount as i32,
1920                                });
1921                            })?;
1922                            continue;
1923                        }
1924                        _ => continue,
1925                    };
1926
1927                    let mut lines = text.lines();
1928                    new_summary.extend(lines.next());
1929
1930                    // Stop if the LLM generated multiple lines.
1931                    if lines.next().is_some() {
1932                        break;
1933                    }
1934                }
1935
1936                anyhow::Ok(new_summary)
1937            }
1938            .await;
1939
1940            this.update(cx, |this, cx| {
1941                match result {
1942                    Ok(new_summary) => {
1943                        if new_summary.is_empty() {
1944                            this.summary = ThreadSummary::Error;
1945                        } else {
1946                            this.summary = ThreadSummary::Ready(new_summary.into());
1947                        }
1948                    }
1949                    Err(err) => {
1950                        this.summary = ThreadSummary::Error;
1951                        log::error!("Failed to generate thread summary: {}", err);
1952                    }
1953                }
1954                cx.emit(ThreadEvent::SummaryGenerated);
1955            })
1956            .log_err()?;
1957
1958            Some(())
1959        });
1960    }
1961
1962    pub fn start_generating_detailed_summary_if_needed(
1963        &mut self,
1964        thread_store: WeakEntity<ThreadStore>,
1965        cx: &mut Context<Self>,
1966    ) {
1967        let Some(last_message_id) = self.messages.last().map(|message| message.id) else {
1968            return;
1969        };
1970
1971        match &*self.detailed_summary_rx.borrow() {
1972            DetailedSummaryState::Generating { message_id, .. }
1973            | DetailedSummaryState::Generated { message_id, .. }
1974                if *message_id == last_message_id =>
1975            {
1976                // Already up-to-date
1977                return;
1978            }
1979            _ => {}
1980        }
1981
1982        let Some(ConfiguredModel { model, provider }) =
1983            LanguageModelRegistry::read_global(cx).thread_summary_model()
1984        else {
1985            return;
1986        };
1987
1988        if !provider.is_authenticated(cx) {
1989            return;
1990        }
1991
1992        let added_user_message = include_str!("./prompts/summarize_thread_detailed_prompt.txt");
1993
1994        let request = self.to_summarize_request(
1995            &model,
1996            CompletionIntent::ThreadContextSummarization,
1997            added_user_message.into(),
1998            cx,
1999        );
2000
2001        *self.detailed_summary_tx.borrow_mut() = DetailedSummaryState::Generating {
2002            message_id: last_message_id,
2003        };
2004
2005        // Replace the detailed summarization task if there is one, cancelling it. It would probably
2006        // be better to allow the old task to complete, but this would require logic for choosing
2007        // which result to prefer (the old task could complete after the new one, resulting in a
2008        // stale summary).
2009        self.detailed_summary_task = cx.spawn(async move |thread, cx| {
2010            let stream = model.stream_completion_text(request, &cx);
2011            let Some(mut messages) = stream.await.log_err() else {
2012                thread
2013                    .update(cx, |thread, _cx| {
2014                        *thread.detailed_summary_tx.borrow_mut() =
2015                            DetailedSummaryState::NotGenerated;
2016                    })
2017                    .ok()?;
2018                return None;
2019            };
2020
2021            let mut new_detailed_summary = String::new();
2022
2023            while let Some(chunk) = messages.stream.next().await {
2024                if let Some(chunk) = chunk.log_err() {
2025                    new_detailed_summary.push_str(&chunk);
2026                }
2027            }
2028
2029            thread
2030                .update(cx, |thread, _cx| {
2031                    *thread.detailed_summary_tx.borrow_mut() = DetailedSummaryState::Generated {
2032                        text: new_detailed_summary.into(),
2033                        message_id: last_message_id,
2034                    };
2035                })
2036                .ok()?;
2037
2038            // Save thread so its summary can be reused later
2039            if let Some(thread) = thread.upgrade() {
2040                if let Ok(Ok(save_task)) = cx.update(|cx| {
2041                    thread_store
2042                        .update(cx, |thread_store, cx| thread_store.save_thread(&thread, cx))
2043                }) {
2044                    save_task.await.log_err();
2045                }
2046            }
2047
2048            Some(())
2049        });
2050    }
2051
2052    pub async fn wait_for_detailed_summary_or_text(
2053        this: &Entity<Self>,
2054        cx: &mut AsyncApp,
2055    ) -> Option<SharedString> {
2056        let mut detailed_summary_rx = this
2057            .read_with(cx, |this, _cx| this.detailed_summary_rx.clone())
2058            .ok()?;
2059        loop {
2060            match detailed_summary_rx.recv().await? {
2061                DetailedSummaryState::Generating { .. } => {}
2062                DetailedSummaryState::NotGenerated => {
2063                    return this.read_with(cx, |this, _cx| this.text().into()).ok();
2064                }
2065                DetailedSummaryState::Generated { text, .. } => return Some(text),
2066            }
2067        }
2068    }
2069
2070    pub fn latest_detailed_summary_or_text(&self) -> SharedString {
2071        self.detailed_summary_rx
2072            .borrow()
2073            .text()
2074            .unwrap_or_else(|| self.text().into())
2075    }
2076
2077    pub fn is_generating_detailed_summary(&self) -> bool {
2078        matches!(
2079            &*self.detailed_summary_rx.borrow(),
2080            DetailedSummaryState::Generating { .. }
2081        )
2082    }
2083
2084    pub fn use_pending_tools(
2085        &mut self,
2086        window: Option<AnyWindowHandle>,
2087        cx: &mut Context<Self>,
2088        model: Arc<dyn LanguageModel>,
2089    ) -> Vec<PendingToolUse> {
2090        self.auto_capture_telemetry(cx);
2091        let request =
2092            Arc::new(self.to_completion_request(model.clone(), CompletionIntent::ToolResults, cx));
2093        let pending_tool_uses = self
2094            .tool_use
2095            .pending_tool_uses()
2096            .into_iter()
2097            .filter(|tool_use| tool_use.status.is_idle())
2098            .cloned()
2099            .collect::<Vec<_>>();
2100
2101        for tool_use in pending_tool_uses.iter() {
2102            if let Some(tool) = self.tools.read(cx).tool(&tool_use.name, cx) {
2103                if tool.needs_confirmation(&tool_use.input, cx)
2104                    && !AgentSettings::get_global(cx).always_allow_tool_actions
2105                {
2106                    self.tool_use.confirm_tool_use(
2107                        tool_use.id.clone(),
2108                        tool_use.ui_text.clone(),
2109                        tool_use.input.clone(),
2110                        request.clone(),
2111                        tool,
2112                    );
2113                    cx.emit(ThreadEvent::ToolConfirmationNeeded);
2114                } else {
2115                    self.run_tool(
2116                        tool_use.id.clone(),
2117                        tool_use.ui_text.clone(),
2118                        tool_use.input.clone(),
2119                        request.clone(),
2120                        tool,
2121                        model.clone(),
2122                        window,
2123                        cx,
2124                    );
2125                }
2126            } else {
2127                self.handle_hallucinated_tool_use(
2128                    tool_use.id.clone(),
2129                    tool_use.name.clone(),
2130                    window,
2131                    cx,
2132                );
2133            }
2134        }
2135
2136        pending_tool_uses
2137    }
2138
2139    pub fn handle_hallucinated_tool_use(
2140        &mut self,
2141        tool_use_id: LanguageModelToolUseId,
2142        hallucinated_tool_name: Arc<str>,
2143        window: Option<AnyWindowHandle>,
2144        cx: &mut Context<Thread>,
2145    ) {
2146        let available_tools = self.profile.enabled_tools(cx);
2147
2148        let tool_list = available_tools
2149            .iter()
2150            .map(|tool| format!("- {}: {}", tool.name(), tool.description()))
2151            .collect::<Vec<_>>()
2152            .join("\n");
2153
2154        let error_message = format!(
2155            "The tool '{}' doesn't exist or is not enabled. Available tools:\n{}",
2156            hallucinated_tool_name, tool_list
2157        );
2158
2159        let pending_tool_use = self.tool_use.insert_tool_output(
2160            tool_use_id.clone(),
2161            hallucinated_tool_name,
2162            Err(anyhow!("Missing tool call: {error_message}")),
2163            self.configured_model.as_ref(),
2164        );
2165
2166        cx.emit(ThreadEvent::MissingToolUse {
2167            tool_use_id: tool_use_id.clone(),
2168            ui_text: error_message.into(),
2169        });
2170
2171        self.tool_finished(tool_use_id, pending_tool_use, false, window, cx);
2172    }
2173
2174    pub fn receive_invalid_tool_json(
2175        &mut self,
2176        tool_use_id: LanguageModelToolUseId,
2177        tool_name: Arc<str>,
2178        invalid_json: Arc<str>,
2179        error: String,
2180        window: Option<AnyWindowHandle>,
2181        cx: &mut Context<Thread>,
2182    ) {
2183        log::error!("The model returned invalid input JSON: {invalid_json}");
2184
2185        let pending_tool_use = self.tool_use.insert_tool_output(
2186            tool_use_id.clone(),
2187            tool_name,
2188            Err(anyhow!("Error parsing input JSON: {error}")),
2189            self.configured_model.as_ref(),
2190        );
2191        let ui_text = if let Some(pending_tool_use) = &pending_tool_use {
2192            pending_tool_use.ui_text.clone()
2193        } else {
2194            log::error!(
2195                "There was no pending tool use for tool use {tool_use_id}, even though it finished (with invalid input JSON)."
2196            );
2197            format!("Unknown tool {}", tool_use_id).into()
2198        };
2199
2200        cx.emit(ThreadEvent::InvalidToolInput {
2201            tool_use_id: tool_use_id.clone(),
2202            ui_text,
2203            invalid_input_json: invalid_json,
2204        });
2205
2206        self.tool_finished(tool_use_id, pending_tool_use, false, window, cx);
2207    }
2208
2209    pub fn run_tool(
2210        &mut self,
2211        tool_use_id: LanguageModelToolUseId,
2212        ui_text: impl Into<SharedString>,
2213        input: serde_json::Value,
2214        request: Arc<LanguageModelRequest>,
2215        tool: Arc<dyn Tool>,
2216        model: Arc<dyn LanguageModel>,
2217        window: Option<AnyWindowHandle>,
2218        cx: &mut Context<Thread>,
2219    ) {
2220        let task =
2221            self.spawn_tool_use(tool_use_id.clone(), request, input, tool, model, window, cx);
2222        self.tool_use
2223            .run_pending_tool(tool_use_id, ui_text.into(), task);
2224    }
2225
2226    fn spawn_tool_use(
2227        &mut self,
2228        tool_use_id: LanguageModelToolUseId,
2229        request: Arc<LanguageModelRequest>,
2230        input: serde_json::Value,
2231        tool: Arc<dyn Tool>,
2232        model: Arc<dyn LanguageModel>,
2233        window: Option<AnyWindowHandle>,
2234        cx: &mut Context<Thread>,
2235    ) -> Task<()> {
2236        let tool_name: Arc<str> = tool.name().into();
2237
2238        let tool_result = tool.run(
2239            input,
2240            request,
2241            self.project.clone(),
2242            self.action_log.clone(),
2243            model,
2244            window,
2245            cx,
2246        );
2247
2248        // Store the card separately if it exists
2249        if let Some(card) = tool_result.card.clone() {
2250            self.tool_use
2251                .insert_tool_result_card(tool_use_id.clone(), card);
2252        }
2253
2254        cx.spawn({
2255            async move |thread: WeakEntity<Thread>, cx| {
2256                let output = tool_result.output.await;
2257
2258                thread
2259                    .update(cx, |thread, cx| {
2260                        let pending_tool_use = thread.tool_use.insert_tool_output(
2261                            tool_use_id.clone(),
2262                            tool_name,
2263                            output,
2264                            thread.configured_model.as_ref(),
2265                        );
2266                        thread.tool_finished(tool_use_id, pending_tool_use, false, window, cx);
2267                    })
2268                    .ok();
2269            }
2270        })
2271    }
2272
2273    fn tool_finished(
2274        &mut self,
2275        tool_use_id: LanguageModelToolUseId,
2276        pending_tool_use: Option<PendingToolUse>,
2277        canceled: bool,
2278        window: Option<AnyWindowHandle>,
2279        cx: &mut Context<Self>,
2280    ) {
2281        if self.all_tools_finished() {
2282            if let Some(ConfiguredModel { model, .. }) = self.configured_model.as_ref() {
2283                if !canceled {
2284                    self.send_to_model(model.clone(), CompletionIntent::ToolResults, window, cx);
2285                }
2286                self.auto_capture_telemetry(cx);
2287            }
2288        }
2289
2290        cx.emit(ThreadEvent::ToolFinished {
2291            tool_use_id,
2292            pending_tool_use,
2293        });
2294    }
2295
2296    /// Cancels the last pending completion, if there are any pending.
2297    ///
2298    /// Returns whether a completion was canceled.
2299    pub fn cancel_last_completion(
2300        &mut self,
2301        window: Option<AnyWindowHandle>,
2302        cx: &mut Context<Self>,
2303    ) -> bool {
2304        let mut canceled = self.pending_completions.pop().is_some();
2305
2306        for pending_tool_use in self.tool_use.cancel_pending() {
2307            canceled = true;
2308            self.tool_finished(
2309                pending_tool_use.id.clone(),
2310                Some(pending_tool_use),
2311                true,
2312                window,
2313                cx,
2314            );
2315        }
2316
2317        if canceled {
2318            cx.emit(ThreadEvent::CompletionCanceled);
2319
2320            // When canceled, we always want to insert the checkpoint.
2321            // (We skip over finalize_pending_checkpoint, because it
2322            // would conclude we didn't have anything to insert here.)
2323            if let Some(checkpoint) = self.pending_checkpoint.take() {
2324                self.insert_checkpoint(checkpoint, cx);
2325            }
2326        } else {
2327            self.finalize_pending_checkpoint(cx);
2328        }
2329
2330        canceled
2331    }
2332
2333    /// Signals that any in-progress editing should be canceled.
2334    ///
2335    /// This method is used to notify listeners (like ActiveThread) that
2336    /// they should cancel any editing operations.
2337    pub fn cancel_editing(&mut self, cx: &mut Context<Self>) {
2338        cx.emit(ThreadEvent::CancelEditing);
2339    }
2340
2341    pub fn feedback(&self) -> Option<ThreadFeedback> {
2342        self.feedback
2343    }
2344
2345    pub fn message_feedback(&self, message_id: MessageId) -> Option<ThreadFeedback> {
2346        self.message_feedback.get(&message_id).copied()
2347    }
2348
2349    pub fn report_message_feedback(
2350        &mut self,
2351        message_id: MessageId,
2352        feedback: ThreadFeedback,
2353        cx: &mut Context<Self>,
2354    ) -> Task<Result<()>> {
2355        if self.message_feedback.get(&message_id) == Some(&feedback) {
2356            return Task::ready(Ok(()));
2357        }
2358
2359        let final_project_snapshot = Self::project_snapshot(self.project.clone(), cx);
2360        let serialized_thread = self.serialize(cx);
2361        let thread_id = self.id().clone();
2362        let client = self.project.read(cx).client();
2363
2364        let enabled_tool_names: Vec<String> = self
2365            .profile
2366            .enabled_tools(cx)
2367            .iter()
2368            .map(|tool| tool.name())
2369            .collect();
2370
2371        self.message_feedback.insert(message_id, feedback);
2372
2373        cx.notify();
2374
2375        let message_content = self
2376            .message(message_id)
2377            .map(|msg| msg.to_string())
2378            .unwrap_or_default();
2379
2380        cx.background_spawn(async move {
2381            let final_project_snapshot = final_project_snapshot.await;
2382            let serialized_thread = serialized_thread.await?;
2383            let thread_data =
2384                serde_json::to_value(serialized_thread).unwrap_or_else(|_| serde_json::Value::Null);
2385
2386            let rating = match feedback {
2387                ThreadFeedback::Positive => "positive",
2388                ThreadFeedback::Negative => "negative",
2389            };
2390            telemetry::event!(
2391                "Assistant Thread Rated",
2392                rating,
2393                thread_id,
2394                enabled_tool_names,
2395                message_id = message_id.0,
2396                message_content,
2397                thread_data,
2398                final_project_snapshot
2399            );
2400            client.telemetry().flush_events().await;
2401
2402            Ok(())
2403        })
2404    }
2405
2406    pub fn report_feedback(
2407        &mut self,
2408        feedback: ThreadFeedback,
2409        cx: &mut Context<Self>,
2410    ) -> Task<Result<()>> {
2411        let last_assistant_message_id = self
2412            .messages
2413            .iter()
2414            .rev()
2415            .find(|msg| msg.role == Role::Assistant)
2416            .map(|msg| msg.id);
2417
2418        if let Some(message_id) = last_assistant_message_id {
2419            self.report_message_feedback(message_id, feedback, cx)
2420        } else {
2421            let final_project_snapshot = Self::project_snapshot(self.project.clone(), cx);
2422            let serialized_thread = self.serialize(cx);
2423            let thread_id = self.id().clone();
2424            let client = self.project.read(cx).client();
2425            self.feedback = Some(feedback);
2426            cx.notify();
2427
2428            cx.background_spawn(async move {
2429                let final_project_snapshot = final_project_snapshot.await;
2430                let serialized_thread = serialized_thread.await?;
2431                let thread_data = serde_json::to_value(serialized_thread)
2432                    .unwrap_or_else(|_| serde_json::Value::Null);
2433
2434                let rating = match feedback {
2435                    ThreadFeedback::Positive => "positive",
2436                    ThreadFeedback::Negative => "negative",
2437                };
2438                telemetry::event!(
2439                    "Assistant Thread Rated",
2440                    rating,
2441                    thread_id,
2442                    thread_data,
2443                    final_project_snapshot
2444                );
2445                client.telemetry().flush_events().await;
2446
2447                Ok(())
2448            })
2449        }
2450    }
2451
2452    /// Create a snapshot of the current project state including git information and unsaved buffers.
2453    fn project_snapshot(
2454        project: Entity<Project>,
2455        cx: &mut Context<Self>,
2456    ) -> Task<Arc<ProjectSnapshot>> {
2457        let git_store = project.read(cx).git_store().clone();
2458        let worktree_snapshots: Vec<_> = project
2459            .read(cx)
2460            .visible_worktrees(cx)
2461            .map(|worktree| Self::worktree_snapshot(worktree, git_store.clone(), cx))
2462            .collect();
2463
2464        cx.spawn(async move |_, cx| {
2465            let worktree_snapshots = futures::future::join_all(worktree_snapshots).await;
2466
2467            let mut unsaved_buffers = Vec::new();
2468            cx.update(|app_cx| {
2469                let buffer_store = project.read(app_cx).buffer_store();
2470                for buffer_handle in buffer_store.read(app_cx).buffers() {
2471                    let buffer = buffer_handle.read(app_cx);
2472                    if buffer.is_dirty() {
2473                        if let Some(file) = buffer.file() {
2474                            let path = file.path().to_string_lossy().to_string();
2475                            unsaved_buffers.push(path);
2476                        }
2477                    }
2478                }
2479            })
2480            .ok();
2481
2482            Arc::new(ProjectSnapshot {
2483                worktree_snapshots,
2484                unsaved_buffer_paths: unsaved_buffers,
2485                timestamp: Utc::now(),
2486            })
2487        })
2488    }
2489
2490    fn worktree_snapshot(
2491        worktree: Entity<project::Worktree>,
2492        git_store: Entity<GitStore>,
2493        cx: &App,
2494    ) -> Task<WorktreeSnapshot> {
2495        cx.spawn(async move |cx| {
2496            // Get worktree path and snapshot
2497            let worktree_info = cx.update(|app_cx| {
2498                let worktree = worktree.read(app_cx);
2499                let path = worktree.abs_path().to_string_lossy().to_string();
2500                let snapshot = worktree.snapshot();
2501                (path, snapshot)
2502            });
2503
2504            let Ok((worktree_path, _snapshot)) = worktree_info else {
2505                return WorktreeSnapshot {
2506                    worktree_path: String::new(),
2507                    git_state: None,
2508                };
2509            };
2510
2511            let git_state = git_store
2512                .update(cx, |git_store, cx| {
2513                    git_store
2514                        .repositories()
2515                        .values()
2516                        .find(|repo| {
2517                            repo.read(cx)
2518                                .abs_path_to_repo_path(&worktree.read(cx).abs_path())
2519                                .is_some()
2520                        })
2521                        .cloned()
2522                })
2523                .ok()
2524                .flatten()
2525                .map(|repo| {
2526                    repo.update(cx, |repo, _| {
2527                        let current_branch =
2528                            repo.branch.as_ref().map(|branch| branch.name().to_owned());
2529                        repo.send_job(None, |state, _| async move {
2530                            let RepositoryState::Local { backend, .. } = state else {
2531                                return GitState {
2532                                    remote_url: None,
2533                                    head_sha: None,
2534                                    current_branch,
2535                                    diff: None,
2536                                };
2537                            };
2538
2539                            let remote_url = backend.remote_url("origin");
2540                            let head_sha = backend.head_sha().await;
2541                            let diff = backend.diff(DiffType::HeadToWorktree).await.ok();
2542
2543                            GitState {
2544                                remote_url,
2545                                head_sha,
2546                                current_branch,
2547                                diff,
2548                            }
2549                        })
2550                    })
2551                });
2552
2553            let git_state = match git_state {
2554                Some(git_state) => match git_state.ok() {
2555                    Some(git_state) => git_state.await.ok(),
2556                    None => None,
2557                },
2558                None => None,
2559            };
2560
2561            WorktreeSnapshot {
2562                worktree_path,
2563                git_state,
2564            }
2565        })
2566    }
2567
2568    pub fn to_markdown(&self, cx: &App) -> Result<String> {
2569        let mut markdown = Vec::new();
2570
2571        let summary = self.summary().or_default();
2572        writeln!(markdown, "# {summary}\n")?;
2573
2574        for message in self.messages() {
2575            writeln!(
2576                markdown,
2577                "## {role}\n",
2578                role = match message.role {
2579                    Role::User => "User",
2580                    Role::Assistant => "Agent",
2581                    Role::System => "System",
2582                }
2583            )?;
2584
2585            if !message.loaded_context.text.is_empty() {
2586                writeln!(markdown, "{}", message.loaded_context.text)?;
2587            }
2588
2589            if !message.loaded_context.images.is_empty() {
2590                writeln!(
2591                    markdown,
2592                    "\n{} images attached as context.\n",
2593                    message.loaded_context.images.len()
2594                )?;
2595            }
2596
2597            for segment in &message.segments {
2598                match segment {
2599                    MessageSegment::Text(text) => writeln!(markdown, "{}\n", text)?,
2600                    MessageSegment::Thinking { text, .. } => {
2601                        writeln!(markdown, "<think>\n{}\n</think>\n", text)?
2602                    }
2603                    MessageSegment::RedactedThinking(_) => {}
2604                }
2605            }
2606
2607            for tool_use in self.tool_uses_for_message(message.id, cx) {
2608                writeln!(
2609                    markdown,
2610                    "**Use Tool: {} ({})**",
2611                    tool_use.name, tool_use.id
2612                )?;
2613                writeln!(markdown, "```json")?;
2614                writeln!(
2615                    markdown,
2616                    "{}",
2617                    serde_json::to_string_pretty(&tool_use.input)?
2618                )?;
2619                writeln!(markdown, "```")?;
2620            }
2621
2622            for tool_result in self.tool_results_for_message(message.id) {
2623                write!(markdown, "\n**Tool Results: {}", tool_result.tool_use_id)?;
2624                if tool_result.is_error {
2625                    write!(markdown, " (Error)")?;
2626                }
2627
2628                writeln!(markdown, "**\n")?;
2629                match &tool_result.content {
2630                    LanguageModelToolResultContent::Text(text) => {
2631                        writeln!(markdown, "{text}")?;
2632                    }
2633                    LanguageModelToolResultContent::Image(image) => {
2634                        writeln!(markdown, "![Image](data:base64,{})", image.source)?;
2635                    }
2636                }
2637
2638                if let Some(output) = tool_result.output.as_ref() {
2639                    writeln!(
2640                        markdown,
2641                        "\n\nDebug Output:\n\n```json\n{}\n```\n",
2642                        serde_json::to_string_pretty(output)?
2643                    )?;
2644                }
2645            }
2646        }
2647
2648        Ok(String::from_utf8_lossy(&markdown).to_string())
2649    }
2650
2651    pub fn keep_edits_in_range(
2652        &mut self,
2653        buffer: Entity<language::Buffer>,
2654        buffer_range: Range<language::Anchor>,
2655        cx: &mut Context<Self>,
2656    ) {
2657        self.action_log.update(cx, |action_log, cx| {
2658            action_log.keep_edits_in_range(buffer, buffer_range, cx)
2659        });
2660    }
2661
2662    pub fn keep_all_edits(&mut self, cx: &mut Context<Self>) {
2663        self.action_log
2664            .update(cx, |action_log, cx| action_log.keep_all_edits(cx));
2665    }
2666
2667    pub fn reject_edits_in_ranges(
2668        &mut self,
2669        buffer: Entity<language::Buffer>,
2670        buffer_ranges: Vec<Range<language::Anchor>>,
2671        cx: &mut Context<Self>,
2672    ) -> Task<Result<()>> {
2673        self.action_log.update(cx, |action_log, cx| {
2674            action_log.reject_edits_in_ranges(buffer, buffer_ranges, cx)
2675        })
2676    }
2677
2678    pub fn action_log(&self) -> &Entity<ActionLog> {
2679        &self.action_log
2680    }
2681
2682    pub fn project(&self) -> &Entity<Project> {
2683        &self.project
2684    }
2685
2686    pub fn auto_capture_telemetry(&mut self, cx: &mut Context<Self>) {
2687        if !cx.has_flag::<feature_flags::ThreadAutoCaptureFeatureFlag>() {
2688            return;
2689        }
2690
2691        let now = Instant::now();
2692        if let Some(last) = self.last_auto_capture_at {
2693            if now.duration_since(last).as_secs() < 10 {
2694                return;
2695            }
2696        }
2697
2698        self.last_auto_capture_at = Some(now);
2699
2700        let thread_id = self.id().clone();
2701        let github_login = self
2702            .project
2703            .read(cx)
2704            .user_store()
2705            .read(cx)
2706            .current_user()
2707            .map(|user| user.github_login.clone());
2708        let client = self.project.read(cx).client();
2709        let serialize_task = self.serialize(cx);
2710
2711        cx.background_executor()
2712            .spawn(async move {
2713                if let Ok(serialized_thread) = serialize_task.await {
2714                    if let Ok(thread_data) = serde_json::to_value(serialized_thread) {
2715                        telemetry::event!(
2716                            "Agent Thread Auto-Captured",
2717                            thread_id = thread_id.to_string(),
2718                            thread_data = thread_data,
2719                            auto_capture_reason = "tracked_user",
2720                            github_login = github_login
2721                        );
2722
2723                        client.telemetry().flush_events().await;
2724                    }
2725                }
2726            })
2727            .detach();
2728    }
2729
2730    pub fn cumulative_token_usage(&self) -> TokenUsage {
2731        self.cumulative_token_usage
2732    }
2733
2734    pub fn token_usage_up_to_message(&self, message_id: MessageId) -> TotalTokenUsage {
2735        let Some(model) = self.configured_model.as_ref() else {
2736            return TotalTokenUsage::default();
2737        };
2738
2739        let max = model.model.max_token_count();
2740
2741        let index = self
2742            .messages
2743            .iter()
2744            .position(|msg| msg.id == message_id)
2745            .unwrap_or(0);
2746
2747        if index == 0 {
2748            return TotalTokenUsage { total: 0, max };
2749        }
2750
2751        let token_usage = &self
2752            .request_token_usage
2753            .get(index - 1)
2754            .cloned()
2755            .unwrap_or_default();
2756
2757        TotalTokenUsage {
2758            total: token_usage.total_tokens() as usize,
2759            max,
2760        }
2761    }
2762
2763    pub fn total_token_usage(&self) -> Option<TotalTokenUsage> {
2764        let model = self.configured_model.as_ref()?;
2765
2766        let max = model.model.max_token_count();
2767
2768        if let Some(exceeded_error) = &self.exceeded_window_error {
2769            if model.model.id() == exceeded_error.model_id {
2770                return Some(TotalTokenUsage {
2771                    total: exceeded_error.token_count,
2772                    max,
2773                });
2774            }
2775        }
2776
2777        let total = self
2778            .token_usage_at_last_message()
2779            .unwrap_or_default()
2780            .total_tokens() as usize;
2781
2782        Some(TotalTokenUsage { total, max })
2783    }
2784
2785    fn token_usage_at_last_message(&self) -> Option<TokenUsage> {
2786        self.request_token_usage
2787            .get(self.messages.len().saturating_sub(1))
2788            .or_else(|| self.request_token_usage.last())
2789            .cloned()
2790    }
2791
2792    fn update_token_usage_at_last_message(&mut self, token_usage: TokenUsage) {
2793        let placeholder = self.token_usage_at_last_message().unwrap_or_default();
2794        self.request_token_usage
2795            .resize(self.messages.len(), placeholder);
2796
2797        if let Some(last) = self.request_token_usage.last_mut() {
2798            *last = token_usage;
2799        }
2800    }
2801
2802    pub fn deny_tool_use(
2803        &mut self,
2804        tool_use_id: LanguageModelToolUseId,
2805        tool_name: Arc<str>,
2806        window: Option<AnyWindowHandle>,
2807        cx: &mut Context<Self>,
2808    ) {
2809        let err = Err(anyhow::anyhow!(
2810            "Permission to run tool action denied by user"
2811        ));
2812
2813        self.tool_use.insert_tool_output(
2814            tool_use_id.clone(),
2815            tool_name,
2816            err,
2817            self.configured_model.as_ref(),
2818        );
2819        self.tool_finished(tool_use_id.clone(), None, true, window, cx);
2820    }
2821}
2822
2823#[derive(Debug, Clone, Error)]
2824pub enum ThreadError {
2825    #[error("Payment required")]
2826    PaymentRequired,
2827    #[error("Model request limit reached")]
2828    ModelRequestLimitReached { plan: Plan },
2829    #[error("Message {header}: {message}")]
2830    Message {
2831        header: SharedString,
2832        message: SharedString,
2833    },
2834}
2835
2836#[derive(Debug, Clone)]
2837pub enum ThreadEvent {
2838    ShowError(ThreadError),
2839    StreamedCompletion,
2840    ReceivedTextChunk,
2841    NewRequest,
2842    StreamedAssistantText(MessageId, String),
2843    StreamedAssistantThinking(MessageId, String),
2844    StreamedToolUse {
2845        tool_use_id: LanguageModelToolUseId,
2846        ui_text: Arc<str>,
2847        input: serde_json::Value,
2848    },
2849    MissingToolUse {
2850        tool_use_id: LanguageModelToolUseId,
2851        ui_text: Arc<str>,
2852    },
2853    InvalidToolInput {
2854        tool_use_id: LanguageModelToolUseId,
2855        ui_text: Arc<str>,
2856        invalid_input_json: Arc<str>,
2857    },
2858    Stopped(Result<StopReason, Arc<anyhow::Error>>),
2859    MessageAdded(MessageId),
2860    MessageEdited(MessageId),
2861    MessageDeleted(MessageId),
2862    SummaryGenerated,
2863    SummaryChanged,
2864    UsePendingTools {
2865        tool_uses: Vec<PendingToolUse>,
2866    },
2867    ToolFinished {
2868        #[allow(unused)]
2869        tool_use_id: LanguageModelToolUseId,
2870        /// The pending tool use that corresponds to this tool.
2871        pending_tool_use: Option<PendingToolUse>,
2872    },
2873    CheckpointChanged,
2874    ToolConfirmationNeeded,
2875    ToolUseLimitReached,
2876    CancelEditing,
2877    CompletionCanceled,
2878    ProfileChanged,
2879}
2880
2881impl EventEmitter<ThreadEvent> for Thread {}
2882
2883struct PendingCompletion {
2884    id: usize,
2885    queue_state: QueueState,
2886    _task: Task<()>,
2887}
2888
2889#[cfg(test)]
2890mod tests {
2891    use super::*;
2892    use crate::{ThreadStore, context::load_context, context_store::ContextStore, thread_store};
2893    use agent_settings::{AgentProfileId, AgentSettings, LanguageModelParameters};
2894    use assistant_tool::ToolRegistry;
2895    use editor::EditorSettings;
2896    use gpui::TestAppContext;
2897    use language_model::fake_provider::{FakeLanguageModel, FakeLanguageModelProvider};
2898    use project::{FakeFs, Project};
2899    use prompt_store::PromptBuilder;
2900    use serde_json::json;
2901    use settings::{Settings, SettingsStore};
2902    use std::sync::Arc;
2903    use theme::ThemeSettings;
2904    use util::path;
2905    use workspace::Workspace;
2906
2907    #[gpui::test]
2908    async fn test_message_with_context(cx: &mut TestAppContext) {
2909        init_test_settings(cx);
2910
2911        let project = create_test_project(
2912            cx,
2913            json!({"code.rs": "fn main() {\n    println!(\"Hello, world!\");\n}"}),
2914        )
2915        .await;
2916
2917        let (_workspace, _thread_store, thread, context_store, model) =
2918            setup_test_environment(cx, project.clone()).await;
2919
2920        add_file_to_context(&project, &context_store, "test/code.rs", cx)
2921            .await
2922            .unwrap();
2923
2924        let context =
2925            context_store.read_with(cx, |store, _| store.context().next().cloned().unwrap());
2926        let loaded_context = cx
2927            .update(|cx| load_context(vec![context], &project, &None, cx))
2928            .await;
2929
2930        // Insert user message with context
2931        let message_id = thread.update(cx, |thread, cx| {
2932            thread.insert_user_message(
2933                "Please explain this code",
2934                loaded_context,
2935                None,
2936                Vec::new(),
2937                cx,
2938            )
2939        });
2940
2941        // Check content and context in message object
2942        let message = thread.read_with(cx, |thread, _| thread.message(message_id).unwrap().clone());
2943
2944        // Use different path format strings based on platform for the test
2945        #[cfg(windows)]
2946        let path_part = r"test\code.rs";
2947        #[cfg(not(windows))]
2948        let path_part = "test/code.rs";
2949
2950        let expected_context = format!(
2951            r#"
2952<context>
2953The following items were attached by the user. They are up-to-date and don't need to be re-read.
2954
2955<files>
2956```rs {path_part}
2957fn main() {{
2958    println!("Hello, world!");
2959}}
2960```
2961</files>
2962</context>
2963"#
2964        );
2965
2966        assert_eq!(message.role, Role::User);
2967        assert_eq!(message.segments.len(), 1);
2968        assert_eq!(
2969            message.segments[0],
2970            MessageSegment::Text("Please explain this code".to_string())
2971        );
2972        assert_eq!(message.loaded_context.text, expected_context);
2973
2974        // Check message in request
2975        let request = thread.update(cx, |thread, cx| {
2976            thread.to_completion_request(model.clone(), CompletionIntent::UserPrompt, cx)
2977        });
2978
2979        assert_eq!(request.messages.len(), 2);
2980        let expected_full_message = format!("{}Please explain this code", expected_context);
2981        assert_eq!(request.messages[1].string_contents(), expected_full_message);
2982    }
2983
2984    #[gpui::test]
2985    async fn test_only_include_new_contexts(cx: &mut TestAppContext) {
2986        init_test_settings(cx);
2987
2988        let project = create_test_project(
2989            cx,
2990            json!({
2991                "file1.rs": "fn function1() {}\n",
2992                "file2.rs": "fn function2() {}\n",
2993                "file3.rs": "fn function3() {}\n",
2994                "file4.rs": "fn function4() {}\n",
2995            }),
2996        )
2997        .await;
2998
2999        let (_, _thread_store, thread, context_store, model) =
3000            setup_test_environment(cx, project.clone()).await;
3001
3002        // First message with context 1
3003        add_file_to_context(&project, &context_store, "test/file1.rs", cx)
3004            .await
3005            .unwrap();
3006        let new_contexts = context_store.update(cx, |store, cx| {
3007            store.new_context_for_thread(thread.read(cx), None)
3008        });
3009        assert_eq!(new_contexts.len(), 1);
3010        let loaded_context = cx
3011            .update(|cx| load_context(new_contexts, &project, &None, cx))
3012            .await;
3013        let message1_id = thread.update(cx, |thread, cx| {
3014            thread.insert_user_message("Message 1", loaded_context, None, Vec::new(), cx)
3015        });
3016
3017        // Second message with contexts 1 and 2 (context 1 should be skipped as it's already included)
3018        add_file_to_context(&project, &context_store, "test/file2.rs", cx)
3019            .await
3020            .unwrap();
3021        let new_contexts = context_store.update(cx, |store, cx| {
3022            store.new_context_for_thread(thread.read(cx), None)
3023        });
3024        assert_eq!(new_contexts.len(), 1);
3025        let loaded_context = cx
3026            .update(|cx| load_context(new_contexts, &project, &None, cx))
3027            .await;
3028        let message2_id = thread.update(cx, |thread, cx| {
3029            thread.insert_user_message("Message 2", loaded_context, None, Vec::new(), cx)
3030        });
3031
3032        // Third message with all three contexts (contexts 1 and 2 should be skipped)
3033        //
3034        add_file_to_context(&project, &context_store, "test/file3.rs", cx)
3035            .await
3036            .unwrap();
3037        let new_contexts = context_store.update(cx, |store, cx| {
3038            store.new_context_for_thread(thread.read(cx), None)
3039        });
3040        assert_eq!(new_contexts.len(), 1);
3041        let loaded_context = cx
3042            .update(|cx| load_context(new_contexts, &project, &None, cx))
3043            .await;
3044        let message3_id = thread.update(cx, |thread, cx| {
3045            thread.insert_user_message("Message 3", loaded_context, None, Vec::new(), cx)
3046        });
3047
3048        // Check what contexts are included in each message
3049        let (message1, message2, message3) = thread.read_with(cx, |thread, _| {
3050            (
3051                thread.message(message1_id).unwrap().clone(),
3052                thread.message(message2_id).unwrap().clone(),
3053                thread.message(message3_id).unwrap().clone(),
3054            )
3055        });
3056
3057        // First message should include context 1
3058        assert!(message1.loaded_context.text.contains("file1.rs"));
3059
3060        // Second message should include only context 2 (not 1)
3061        assert!(!message2.loaded_context.text.contains("file1.rs"));
3062        assert!(message2.loaded_context.text.contains("file2.rs"));
3063
3064        // Third message should include only context 3 (not 1 or 2)
3065        assert!(!message3.loaded_context.text.contains("file1.rs"));
3066        assert!(!message3.loaded_context.text.contains("file2.rs"));
3067        assert!(message3.loaded_context.text.contains("file3.rs"));
3068
3069        // Check entire request to make sure all contexts are properly included
3070        let request = thread.update(cx, |thread, cx| {
3071            thread.to_completion_request(model.clone(), CompletionIntent::UserPrompt, cx)
3072        });
3073
3074        // The request should contain all 3 messages
3075        assert_eq!(request.messages.len(), 4);
3076
3077        // Check that the contexts are properly formatted in each message
3078        assert!(request.messages[1].string_contents().contains("file1.rs"));
3079        assert!(!request.messages[1].string_contents().contains("file2.rs"));
3080        assert!(!request.messages[1].string_contents().contains("file3.rs"));
3081
3082        assert!(!request.messages[2].string_contents().contains("file1.rs"));
3083        assert!(request.messages[2].string_contents().contains("file2.rs"));
3084        assert!(!request.messages[2].string_contents().contains("file3.rs"));
3085
3086        assert!(!request.messages[3].string_contents().contains("file1.rs"));
3087        assert!(!request.messages[3].string_contents().contains("file2.rs"));
3088        assert!(request.messages[3].string_contents().contains("file3.rs"));
3089
3090        add_file_to_context(&project, &context_store, "test/file4.rs", cx)
3091            .await
3092            .unwrap();
3093        let new_contexts = context_store.update(cx, |store, cx| {
3094            store.new_context_for_thread(thread.read(cx), Some(message2_id))
3095        });
3096        assert_eq!(new_contexts.len(), 3);
3097        let loaded_context = cx
3098            .update(|cx| load_context(new_contexts, &project, &None, cx))
3099            .await
3100            .loaded_context;
3101
3102        assert!(!loaded_context.text.contains("file1.rs"));
3103        assert!(loaded_context.text.contains("file2.rs"));
3104        assert!(loaded_context.text.contains("file3.rs"));
3105        assert!(loaded_context.text.contains("file4.rs"));
3106
3107        let new_contexts = context_store.update(cx, |store, cx| {
3108            // Remove file4.rs
3109            store.remove_context(&loaded_context.contexts[2].handle(), cx);
3110            store.new_context_for_thread(thread.read(cx), Some(message2_id))
3111        });
3112        assert_eq!(new_contexts.len(), 2);
3113        let loaded_context = cx
3114            .update(|cx| load_context(new_contexts, &project, &None, cx))
3115            .await
3116            .loaded_context;
3117
3118        assert!(!loaded_context.text.contains("file1.rs"));
3119        assert!(loaded_context.text.contains("file2.rs"));
3120        assert!(loaded_context.text.contains("file3.rs"));
3121        assert!(!loaded_context.text.contains("file4.rs"));
3122
3123        let new_contexts = context_store.update(cx, |store, cx| {
3124            // Remove file3.rs
3125            store.remove_context(&loaded_context.contexts[1].handle(), cx);
3126            store.new_context_for_thread(thread.read(cx), Some(message2_id))
3127        });
3128        assert_eq!(new_contexts.len(), 1);
3129        let loaded_context = cx
3130            .update(|cx| load_context(new_contexts, &project, &None, cx))
3131            .await
3132            .loaded_context;
3133
3134        assert!(!loaded_context.text.contains("file1.rs"));
3135        assert!(loaded_context.text.contains("file2.rs"));
3136        assert!(!loaded_context.text.contains("file3.rs"));
3137        assert!(!loaded_context.text.contains("file4.rs"));
3138    }
3139
3140    #[gpui::test]
3141    async fn test_message_without_files(cx: &mut TestAppContext) {
3142        init_test_settings(cx);
3143
3144        let project = create_test_project(
3145            cx,
3146            json!({"code.rs": "fn main() {\n    println!(\"Hello, world!\");\n}"}),
3147        )
3148        .await;
3149
3150        let (_, _thread_store, thread, _context_store, model) =
3151            setup_test_environment(cx, project.clone()).await;
3152
3153        // Insert user message without any context (empty context vector)
3154        let message_id = thread.update(cx, |thread, cx| {
3155            thread.insert_user_message(
3156                "What is the best way to learn Rust?",
3157                ContextLoadResult::default(),
3158                None,
3159                Vec::new(),
3160                cx,
3161            )
3162        });
3163
3164        // Check content and context in message object
3165        let message = thread.read_with(cx, |thread, _| thread.message(message_id).unwrap().clone());
3166
3167        // Context should be empty when no files are included
3168        assert_eq!(message.role, Role::User);
3169        assert_eq!(message.segments.len(), 1);
3170        assert_eq!(
3171            message.segments[0],
3172            MessageSegment::Text("What is the best way to learn Rust?".to_string())
3173        );
3174        assert_eq!(message.loaded_context.text, "");
3175
3176        // Check message in request
3177        let request = thread.update(cx, |thread, cx| {
3178            thread.to_completion_request(model.clone(), CompletionIntent::UserPrompt, cx)
3179        });
3180
3181        assert_eq!(request.messages.len(), 2);
3182        assert_eq!(
3183            request.messages[1].string_contents(),
3184            "What is the best way to learn Rust?"
3185        );
3186
3187        // Add second message, also without context
3188        let message2_id = thread.update(cx, |thread, cx| {
3189            thread.insert_user_message(
3190                "Are there any good books?",
3191                ContextLoadResult::default(),
3192                None,
3193                Vec::new(),
3194                cx,
3195            )
3196        });
3197
3198        let message2 =
3199            thread.read_with(cx, |thread, _| thread.message(message2_id).unwrap().clone());
3200        assert_eq!(message2.loaded_context.text, "");
3201
3202        // Check that both messages appear in the request
3203        let request = thread.update(cx, |thread, cx| {
3204            thread.to_completion_request(model.clone(), CompletionIntent::UserPrompt, cx)
3205        });
3206
3207        assert_eq!(request.messages.len(), 3);
3208        assert_eq!(
3209            request.messages[1].string_contents(),
3210            "What is the best way to learn Rust?"
3211        );
3212        assert_eq!(
3213            request.messages[2].string_contents(),
3214            "Are there any good books?"
3215        );
3216    }
3217
3218    #[gpui::test]
3219    async fn test_stale_buffer_notification(cx: &mut TestAppContext) {
3220        init_test_settings(cx);
3221
3222        let project = create_test_project(
3223            cx,
3224            json!({"code.rs": "fn main() {\n    println!(\"Hello, world!\");\n}"}),
3225        )
3226        .await;
3227
3228        let (_workspace, _thread_store, thread, context_store, model) =
3229            setup_test_environment(cx, project.clone()).await;
3230
3231        // Open buffer and add it to context
3232        let buffer = add_file_to_context(&project, &context_store, "test/code.rs", cx)
3233            .await
3234            .unwrap();
3235
3236        let context =
3237            context_store.read_with(cx, |store, _| store.context().next().cloned().unwrap());
3238        let loaded_context = cx
3239            .update(|cx| load_context(vec![context], &project, &None, cx))
3240            .await;
3241
3242        // Insert user message with the buffer as context
3243        thread.update(cx, |thread, cx| {
3244            thread.insert_user_message("Explain this code", loaded_context, None, Vec::new(), cx)
3245        });
3246
3247        // Create a request and check that it doesn't have a stale buffer warning yet
3248        let initial_request = thread.update(cx, |thread, cx| {
3249            thread.to_completion_request(model.clone(), CompletionIntent::UserPrompt, cx)
3250        });
3251
3252        // Make sure we don't have a stale file warning yet
3253        let has_stale_warning = initial_request.messages.iter().any(|msg| {
3254            msg.string_contents()
3255                .contains("These files changed since last read:")
3256        });
3257        assert!(
3258            !has_stale_warning,
3259            "Should not have stale buffer warning before buffer is modified"
3260        );
3261
3262        // Modify the buffer
3263        buffer.update(cx, |buffer, cx| {
3264            // Find a position at the end of line 1
3265            buffer.edit(
3266                [(1..1, "\n    println!(\"Added a new line\");\n")],
3267                None,
3268                cx,
3269            );
3270        });
3271
3272        // Insert another user message without context
3273        thread.update(cx, |thread, cx| {
3274            thread.insert_user_message(
3275                "What does the code do now?",
3276                ContextLoadResult::default(),
3277                None,
3278                Vec::new(),
3279                cx,
3280            )
3281        });
3282
3283        // Create a new request and check for the stale buffer warning
3284        let new_request = thread.update(cx, |thread, cx| {
3285            thread.to_completion_request(model.clone(), CompletionIntent::UserPrompt, cx)
3286        });
3287
3288        // We should have a stale file warning as the last message
3289        let last_message = new_request
3290            .messages
3291            .last()
3292            .expect("Request should have messages");
3293
3294        // The last message should be the stale buffer notification
3295        assert_eq!(last_message.role, Role::User);
3296
3297        // Check the exact content of the message
3298        let expected_content = "These files changed since last read:\n- code.rs\n";
3299        assert_eq!(
3300            last_message.string_contents(),
3301            expected_content,
3302            "Last message should be exactly the stale buffer notification"
3303        );
3304    }
3305
3306    #[gpui::test]
3307    async fn test_storing_profile_setting_per_thread(cx: &mut TestAppContext) {
3308        init_test_settings(cx);
3309
3310        let project = create_test_project(
3311            cx,
3312            json!({"code.rs": "fn main() {\n    println!(\"Hello, world!\");\n}"}),
3313        )
3314        .await;
3315
3316        let (_workspace, thread_store, thread, _context_store, _model) =
3317            setup_test_environment(cx, project.clone()).await;
3318
3319        // Check that we are starting with the default profile
3320        let profile = cx.read(|cx| thread.read(cx).profile.clone());
3321        let tool_set = cx.read(|cx| thread_store.read(cx).tools());
3322        assert_eq!(
3323            profile,
3324            AgentProfile::new(AgentProfileId::default(), tool_set)
3325        );
3326    }
3327
3328    #[gpui::test]
3329    async fn test_serializing_thread_profile(cx: &mut TestAppContext) {
3330        init_test_settings(cx);
3331
3332        let project = create_test_project(
3333            cx,
3334            json!({"code.rs": "fn main() {\n    println!(\"Hello, world!\");\n}"}),
3335        )
3336        .await;
3337
3338        let (_workspace, thread_store, thread, _context_store, _model) =
3339            setup_test_environment(cx, project.clone()).await;
3340
3341        // Profile gets serialized with default values
3342        let serialized = thread
3343            .update(cx, |thread, cx| thread.serialize(cx))
3344            .await
3345            .unwrap();
3346
3347        assert_eq!(serialized.profile, Some(AgentProfileId::default()));
3348
3349        let deserialized = cx.update(|cx| {
3350            thread.update(cx, |thread, cx| {
3351                Thread::deserialize(
3352                    thread.id.clone(),
3353                    serialized,
3354                    thread.project.clone(),
3355                    thread.tools.clone(),
3356                    thread.prompt_builder.clone(),
3357                    thread.project_context.clone(),
3358                    None,
3359                    cx,
3360                )
3361            })
3362        });
3363        let tool_set = cx.read(|cx| thread_store.read(cx).tools());
3364
3365        assert_eq!(
3366            deserialized.profile,
3367            AgentProfile::new(AgentProfileId::default(), tool_set)
3368        );
3369    }
3370
3371    #[gpui::test]
3372    async fn test_temperature_setting(cx: &mut TestAppContext) {
3373        init_test_settings(cx);
3374
3375        let project = create_test_project(
3376            cx,
3377            json!({"code.rs": "fn main() {\n    println!(\"Hello, world!\");\n}"}),
3378        )
3379        .await;
3380
3381        let (_workspace, _thread_store, thread, _context_store, model) =
3382            setup_test_environment(cx, project.clone()).await;
3383
3384        // Both model and provider
3385        cx.update(|cx| {
3386            AgentSettings::override_global(
3387                AgentSettings {
3388                    model_parameters: vec![LanguageModelParameters {
3389                        provider: Some(model.provider_id().0.to_string().into()),
3390                        model: Some(model.id().0.clone()),
3391                        temperature: Some(0.66),
3392                    }],
3393                    ..AgentSettings::get_global(cx).clone()
3394                },
3395                cx,
3396            );
3397        });
3398
3399        let request = thread.update(cx, |thread, cx| {
3400            thread.to_completion_request(model.clone(), CompletionIntent::UserPrompt, cx)
3401        });
3402        assert_eq!(request.temperature, Some(0.66));
3403
3404        // Only model
3405        cx.update(|cx| {
3406            AgentSettings::override_global(
3407                AgentSettings {
3408                    model_parameters: vec![LanguageModelParameters {
3409                        provider: None,
3410                        model: Some(model.id().0.clone()),
3411                        temperature: Some(0.66),
3412                    }],
3413                    ..AgentSettings::get_global(cx).clone()
3414                },
3415                cx,
3416            );
3417        });
3418
3419        let request = thread.update(cx, |thread, cx| {
3420            thread.to_completion_request(model.clone(), CompletionIntent::UserPrompt, cx)
3421        });
3422        assert_eq!(request.temperature, Some(0.66));
3423
3424        // Only provider
3425        cx.update(|cx| {
3426            AgentSettings::override_global(
3427                AgentSettings {
3428                    model_parameters: vec![LanguageModelParameters {
3429                        provider: Some(model.provider_id().0.to_string().into()),
3430                        model: None,
3431                        temperature: Some(0.66),
3432                    }],
3433                    ..AgentSettings::get_global(cx).clone()
3434                },
3435                cx,
3436            );
3437        });
3438
3439        let request = thread.update(cx, |thread, cx| {
3440            thread.to_completion_request(model.clone(), CompletionIntent::UserPrompt, cx)
3441        });
3442        assert_eq!(request.temperature, Some(0.66));
3443
3444        // Same model name, different provider
3445        cx.update(|cx| {
3446            AgentSettings::override_global(
3447                AgentSettings {
3448                    model_parameters: vec![LanguageModelParameters {
3449                        provider: Some("anthropic".into()),
3450                        model: Some(model.id().0.clone()),
3451                        temperature: Some(0.66),
3452                    }],
3453                    ..AgentSettings::get_global(cx).clone()
3454                },
3455                cx,
3456            );
3457        });
3458
3459        let request = thread.update(cx, |thread, cx| {
3460            thread.to_completion_request(model.clone(), CompletionIntent::UserPrompt, cx)
3461        });
3462        assert_eq!(request.temperature, None);
3463    }
3464
3465    #[gpui::test]
3466    async fn test_thread_summary(cx: &mut TestAppContext) {
3467        init_test_settings(cx);
3468
3469        let project = create_test_project(cx, json!({})).await;
3470
3471        let (_, _thread_store, thread, _context_store, model) =
3472            setup_test_environment(cx, project.clone()).await;
3473
3474        // Initial state should be pending
3475        thread.read_with(cx, |thread, _| {
3476            assert!(matches!(thread.summary(), ThreadSummary::Pending));
3477            assert_eq!(thread.summary().or_default(), ThreadSummary::DEFAULT);
3478        });
3479
3480        // Manually setting the summary should not be allowed in this state
3481        thread.update(cx, |thread, cx| {
3482            thread.set_summary("This should not work", cx);
3483        });
3484
3485        thread.read_with(cx, |thread, _| {
3486            assert!(matches!(thread.summary(), ThreadSummary::Pending));
3487        });
3488
3489        // Send a message
3490        thread.update(cx, |thread, cx| {
3491            thread.insert_user_message("Hi!", ContextLoadResult::default(), None, vec![], cx);
3492            thread.send_to_model(
3493                model.clone(),
3494                CompletionIntent::ThreadSummarization,
3495                None,
3496                cx,
3497            );
3498        });
3499
3500        let fake_model = model.as_fake();
3501        simulate_successful_response(&fake_model, cx);
3502
3503        // Should start generating summary when there are >= 2 messages
3504        thread.read_with(cx, |thread, _| {
3505            assert_eq!(*thread.summary(), ThreadSummary::Generating);
3506        });
3507
3508        // Should not be able to set the summary while generating
3509        thread.update(cx, |thread, cx| {
3510            thread.set_summary("This should not work either", cx);
3511        });
3512
3513        thread.read_with(cx, |thread, _| {
3514            assert!(matches!(thread.summary(), ThreadSummary::Generating));
3515            assert_eq!(thread.summary().or_default(), ThreadSummary::DEFAULT);
3516        });
3517
3518        cx.run_until_parked();
3519        fake_model.stream_last_completion_response("Brief");
3520        fake_model.stream_last_completion_response(" Introduction");
3521        fake_model.end_last_completion_stream();
3522        cx.run_until_parked();
3523
3524        // Summary should be set
3525        thread.read_with(cx, |thread, _| {
3526            assert!(matches!(thread.summary(), ThreadSummary::Ready(_)));
3527            assert_eq!(thread.summary().or_default(), "Brief Introduction");
3528        });
3529
3530        // Now we should be able to set a summary
3531        thread.update(cx, |thread, cx| {
3532            thread.set_summary("Brief Intro", cx);
3533        });
3534
3535        thread.read_with(cx, |thread, _| {
3536            assert_eq!(thread.summary().or_default(), "Brief Intro");
3537        });
3538
3539        // Test setting an empty summary (should default to DEFAULT)
3540        thread.update(cx, |thread, cx| {
3541            thread.set_summary("", cx);
3542        });
3543
3544        thread.read_with(cx, |thread, _| {
3545            assert!(matches!(thread.summary(), ThreadSummary::Ready(_)));
3546            assert_eq!(thread.summary().or_default(), ThreadSummary::DEFAULT);
3547        });
3548    }
3549
3550    #[gpui::test]
3551    async fn test_thread_summary_error_set_manually(cx: &mut TestAppContext) {
3552        init_test_settings(cx);
3553
3554        let project = create_test_project(cx, json!({})).await;
3555
3556        let (_, _thread_store, thread, _context_store, model) =
3557            setup_test_environment(cx, project.clone()).await;
3558
3559        test_summarize_error(&model, &thread, cx);
3560
3561        // Now we should be able to set a summary
3562        thread.update(cx, |thread, cx| {
3563            thread.set_summary("Brief Intro", cx);
3564        });
3565
3566        thread.read_with(cx, |thread, _| {
3567            assert!(matches!(thread.summary(), ThreadSummary::Ready(_)));
3568            assert_eq!(thread.summary().or_default(), "Brief Intro");
3569        });
3570    }
3571
3572    #[gpui::test]
3573    async fn test_thread_summary_error_retry(cx: &mut TestAppContext) {
3574        init_test_settings(cx);
3575
3576        let project = create_test_project(cx, json!({})).await;
3577
3578        let (_, _thread_store, thread, _context_store, model) =
3579            setup_test_environment(cx, project.clone()).await;
3580
3581        test_summarize_error(&model, &thread, cx);
3582
3583        // Sending another message should not trigger another summarize request
3584        thread.update(cx, |thread, cx| {
3585            thread.insert_user_message(
3586                "How are you?",
3587                ContextLoadResult::default(),
3588                None,
3589                vec![],
3590                cx,
3591            );
3592            thread.send_to_model(model.clone(), CompletionIntent::UserPrompt, None, cx);
3593        });
3594
3595        let fake_model = model.as_fake();
3596        simulate_successful_response(&fake_model, cx);
3597
3598        thread.read_with(cx, |thread, _| {
3599            // State is still Error, not Generating
3600            assert!(matches!(thread.summary(), ThreadSummary::Error));
3601        });
3602
3603        // But the summarize request can be invoked manually
3604        thread.update(cx, |thread, cx| {
3605            thread.summarize(cx);
3606        });
3607
3608        thread.read_with(cx, |thread, _| {
3609            assert!(matches!(thread.summary(), ThreadSummary::Generating));
3610        });
3611
3612        cx.run_until_parked();
3613        fake_model.stream_last_completion_response("A successful summary");
3614        fake_model.end_last_completion_stream();
3615        cx.run_until_parked();
3616
3617        thread.read_with(cx, |thread, _| {
3618            assert!(matches!(thread.summary(), ThreadSummary::Ready(_)));
3619            assert_eq!(thread.summary().or_default(), "A successful summary");
3620        });
3621    }
3622
3623    fn test_summarize_error(
3624        model: &Arc<dyn LanguageModel>,
3625        thread: &Entity<Thread>,
3626        cx: &mut TestAppContext,
3627    ) {
3628        thread.update(cx, |thread, cx| {
3629            thread.insert_user_message("Hi!", ContextLoadResult::default(), None, vec![], cx);
3630            thread.send_to_model(
3631                model.clone(),
3632                CompletionIntent::ThreadSummarization,
3633                None,
3634                cx,
3635            );
3636        });
3637
3638        let fake_model = model.as_fake();
3639        simulate_successful_response(&fake_model, cx);
3640
3641        thread.read_with(cx, |thread, _| {
3642            assert!(matches!(thread.summary(), ThreadSummary::Generating));
3643            assert_eq!(thread.summary().or_default(), ThreadSummary::DEFAULT);
3644        });
3645
3646        // Simulate summary request ending
3647        cx.run_until_parked();
3648        fake_model.end_last_completion_stream();
3649        cx.run_until_parked();
3650
3651        // State is set to Error and default message
3652        thread.read_with(cx, |thread, _| {
3653            assert!(matches!(thread.summary(), ThreadSummary::Error));
3654            assert_eq!(thread.summary().or_default(), ThreadSummary::DEFAULT);
3655        });
3656    }
3657
3658    fn simulate_successful_response(fake_model: &FakeLanguageModel, cx: &mut TestAppContext) {
3659        cx.run_until_parked();
3660        fake_model.stream_last_completion_response("Assistant response");
3661        fake_model.end_last_completion_stream();
3662        cx.run_until_parked();
3663    }
3664
3665    fn init_test_settings(cx: &mut TestAppContext) {
3666        cx.update(|cx| {
3667            let settings_store = SettingsStore::test(cx);
3668            cx.set_global(settings_store);
3669            language::init(cx);
3670            Project::init_settings(cx);
3671            AgentSettings::register(cx);
3672            prompt_store::init(cx);
3673            thread_store::init(cx);
3674            workspace::init_settings(cx);
3675            language_model::init_settings(cx);
3676            ThemeSettings::register(cx);
3677            EditorSettings::register(cx);
3678            ToolRegistry::default_global(cx);
3679        });
3680    }
3681
3682    // Helper to create a test project with test files
3683    async fn create_test_project(
3684        cx: &mut TestAppContext,
3685        files: serde_json::Value,
3686    ) -> Entity<Project> {
3687        let fs = FakeFs::new(cx.executor());
3688        fs.insert_tree(path!("/test"), files).await;
3689        Project::test(fs, [path!("/test").as_ref()], cx).await
3690    }
3691
3692    async fn setup_test_environment(
3693        cx: &mut TestAppContext,
3694        project: Entity<Project>,
3695    ) -> (
3696        Entity<Workspace>,
3697        Entity<ThreadStore>,
3698        Entity<Thread>,
3699        Entity<ContextStore>,
3700        Arc<dyn LanguageModel>,
3701    ) {
3702        let (workspace, cx) =
3703            cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));
3704
3705        let thread_store = cx
3706            .update(|_, cx| {
3707                ThreadStore::load(
3708                    project.clone(),
3709                    cx.new(|_| ToolWorkingSet::default()),
3710                    None,
3711                    Arc::new(PromptBuilder::new(None).unwrap()),
3712                    cx,
3713                )
3714            })
3715            .await
3716            .unwrap();
3717
3718        let thread = thread_store.update(cx, |store, cx| store.create_thread(cx));
3719        let context_store = cx.new(|_cx| ContextStore::new(project.downgrade(), None));
3720
3721        let provider = Arc::new(FakeLanguageModelProvider);
3722        let model = provider.test_model();
3723        let model: Arc<dyn LanguageModel> = Arc::new(model);
3724
3725        cx.update(|_, cx| {
3726            LanguageModelRegistry::global(cx).update(cx, |registry, cx| {
3727                registry.set_default_model(
3728                    Some(ConfiguredModel {
3729                        provider: provider.clone(),
3730                        model: model.clone(),
3731                    }),
3732                    cx,
3733                );
3734                registry.set_thread_summary_model(
3735                    Some(ConfiguredModel {
3736                        provider,
3737                        model: model.clone(),
3738                    }),
3739                    cx,
3740                );
3741            })
3742        });
3743
3744        (workspace, thread_store, thread, context_store, model)
3745    }
3746
3747    async fn add_file_to_context(
3748        project: &Entity<Project>,
3749        context_store: &Entity<ContextStore>,
3750        path: &str,
3751        cx: &mut TestAppContext,
3752    ) -> Result<Entity<language::Buffer>> {
3753        let buffer_path = project
3754            .read_with(cx, |project, cx| project.find_project_path(path, cx))
3755            .unwrap();
3756
3757        let buffer = project
3758            .update(cx, |project, cx| {
3759                project.open_buffer(buffer_path.clone(), cx)
3760            })
3761            .await
3762            .unwrap();
3763
3764        context_store.update(cx, |context_store, cx| {
3765            context_store.add_file_from_buffer(&buffer_path, buffer.clone(), false, cx);
3766        });
3767
3768        Ok(buffer)
3769    }
3770}