thread.rs

   1use crate::{
   2    agent_profile::AgentProfile,
   3    context::{AgentContext, AgentContextHandle, ContextLoadResult, LoadedContext},
   4    thread_store::{
   5        SerializedCrease, SerializedLanguageModel, SerializedMessage, SerializedMessageSegment,
   6        SerializedThread, SerializedToolResult, SerializedToolUse, SharedProjectContext,
   7        ThreadStore,
   8    },
   9    tool_use::{PendingToolUse, ToolUse, ToolUseMetadata, ToolUseState},
  10};
  11use action_log::ActionLog;
  12use agent_settings::{AgentProfileId, AgentSettings, CompletionMode, SUMMARIZE_THREAD_PROMPT};
  13use anyhow::{Result, anyhow};
  14use assistant_tool::{AnyToolCard, Tool, ToolWorkingSet};
  15use chrono::{DateTime, Utc};
  16use client::{ModelRequestUsage, RequestUsage};
  17use cloud_llm_client::{CompletionIntent, CompletionRequestStatus, Plan, UsageLimit};
  18use collections::HashMap;
  19use feature_flags::{self, FeatureFlagAppExt};
  20use futures::{FutureExt, StreamExt as _, future::Shared};
  21use git::repository::DiffType;
  22use gpui::{
  23    AnyWindowHandle, App, AppContext, AsyncApp, Context, Entity, EventEmitter, SharedString, Task,
  24    WeakEntity, Window,
  25};
  26use http_client::StatusCode;
  27use language_model::{
  28    ConfiguredModel, LanguageModel, LanguageModelCompletionError, LanguageModelCompletionEvent,
  29    LanguageModelExt as _, LanguageModelId, LanguageModelRegistry, LanguageModelRequest,
  30    LanguageModelRequestMessage, LanguageModelRequestTool, LanguageModelToolResult,
  31    LanguageModelToolResultContent, LanguageModelToolUse, LanguageModelToolUseId, MessageContent,
  32    ModelRequestLimitReachedError, PaymentRequiredError, Role, SelectedModel, StopReason,
  33    TokenUsage,
  34};
  35use postage::stream::Stream as _;
  36use project::{
  37    Project,
  38    git_store::{GitStore, GitStoreCheckpoint, RepositoryState},
  39};
  40use prompt_store::{ModelContext, PromptBuilder};
  41use schemars::JsonSchema;
  42use serde::{Deserialize, Serialize};
  43use settings::Settings;
  44use std::{
  45    io::Write,
  46    ops::Range,
  47    sync::Arc,
  48    time::{Duration, Instant},
  49};
  50use thiserror::Error;
  51use util::{ResultExt as _, post_inc};
  52use uuid::Uuid;
  53
  54const MAX_RETRY_ATTEMPTS: u8 = 4;
  55const BASE_RETRY_DELAY: Duration = Duration::from_secs(5);
  56
  57#[derive(Debug, Clone)]
  58enum RetryStrategy {
  59    ExponentialBackoff {
  60        initial_delay: Duration,
  61        max_attempts: u8,
  62    },
  63    Fixed {
  64        delay: Duration,
  65        max_attempts: u8,
  66    },
  67}
  68
  69#[derive(
  70    Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Serialize, Deserialize, JsonSchema,
  71)]
  72pub struct ThreadId(Arc<str>);
  73
  74impl ThreadId {
  75    pub fn new() -> Self {
  76        Self(Uuid::new_v4().to_string().into())
  77    }
  78}
  79
  80impl std::fmt::Display for ThreadId {
  81    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  82        write!(f, "{}", self.0)
  83    }
  84}
  85
  86impl From<&str> for ThreadId {
  87    fn from(value: &str) -> Self {
  88        Self(value.into())
  89    }
  90}
  91
  92/// The ID of the user prompt that initiated a request.
  93///
  94/// This equates to the user physically submitting a message to the model (e.g., by pressing the Enter key).
  95#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Serialize, Deserialize)]
  96pub struct PromptId(Arc<str>);
  97
  98impl PromptId {
  99    pub fn new() -> Self {
 100        Self(Uuid::new_v4().to_string().into())
 101    }
 102}
 103
 104impl std::fmt::Display for PromptId {
 105    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 106        write!(f, "{}", self.0)
 107    }
 108}
 109
 110#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Serialize, Deserialize)]
 111pub struct MessageId(pub(crate) usize);
 112
 113impl MessageId {
 114    fn post_inc(&mut self) -> Self {
 115        Self(post_inc(&mut self.0))
 116    }
 117
 118    pub fn as_usize(&self) -> usize {
 119        self.0
 120    }
 121}
 122
 123/// Stored information that can be used to resurrect a context crease when creating an editor for a past message.
 124#[derive(Clone, Debug)]
 125pub struct MessageCrease {
 126    pub range: Range<usize>,
 127    pub icon_path: SharedString,
 128    pub label: SharedString,
 129    /// None for a deserialized message, Some otherwise.
 130    pub context: Option<AgentContextHandle>,
 131}
 132
 133/// A message in a [`Thread`].
 134#[derive(Debug, Clone)]
 135pub struct Message {
 136    pub id: MessageId,
 137    pub role: Role,
 138    pub segments: Vec<MessageSegment>,
 139    pub loaded_context: LoadedContext,
 140    pub creases: Vec<MessageCrease>,
 141    pub is_hidden: bool,
 142    pub ui_only: bool,
 143}
 144
 145impl Message {
 146    /// Returns whether the message contains any meaningful text that should be displayed
 147    /// The model sometimes runs tool without producing any text or just a marker ([`USING_TOOL_MARKER`])
 148    pub fn should_display_content(&self) -> bool {
 149        self.segments.iter().all(|segment| segment.should_display())
 150    }
 151
 152    pub fn push_thinking(&mut self, text: &str, signature: Option<String>) {
 153        if let Some(MessageSegment::Thinking {
 154            text: segment,
 155            signature: current_signature,
 156        }) = self.segments.last_mut()
 157        {
 158            if let Some(signature) = signature {
 159                *current_signature = Some(signature);
 160            }
 161            segment.push_str(text);
 162        } else {
 163            self.segments.push(MessageSegment::Thinking {
 164                text: text.to_string(),
 165                signature,
 166            });
 167        }
 168    }
 169
 170    pub fn push_redacted_thinking(&mut self, data: String) {
 171        self.segments.push(MessageSegment::RedactedThinking(data));
 172    }
 173
 174    pub fn push_text(&mut self, text: &str) {
 175        if let Some(MessageSegment::Text(segment)) = self.segments.last_mut() {
 176            segment.push_str(text);
 177        } else {
 178            self.segments.push(MessageSegment::Text(text.to_string()));
 179        }
 180    }
 181
 182    pub fn to_string(&self) -> String {
 183        let mut result = String::new();
 184
 185        if !self.loaded_context.text.is_empty() {
 186            result.push_str(&self.loaded_context.text);
 187        }
 188
 189        for segment in &self.segments {
 190            match segment {
 191                MessageSegment::Text(text) => result.push_str(text),
 192                MessageSegment::Thinking { text, .. } => {
 193                    result.push_str("<think>\n");
 194                    result.push_str(text);
 195                    result.push_str("\n</think>");
 196                }
 197                MessageSegment::RedactedThinking(_) => {}
 198            }
 199        }
 200
 201        result
 202    }
 203}
 204
 205#[derive(Debug, Clone, PartialEq, Eq)]
 206pub enum MessageSegment {
 207    Text(String),
 208    Thinking {
 209        text: String,
 210        signature: Option<String>,
 211    },
 212    RedactedThinking(String),
 213}
 214
 215impl MessageSegment {
 216    pub fn should_display(&self) -> bool {
 217        match self {
 218            Self::Text(text) => text.is_empty(),
 219            Self::Thinking { text, .. } => text.is_empty(),
 220            Self::RedactedThinking(_) => false,
 221        }
 222    }
 223
 224    pub fn text(&self) -> Option<&str> {
 225        match self {
 226            MessageSegment::Text(text) => Some(text),
 227            _ => None,
 228        }
 229    }
 230}
 231
 232#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
 233pub struct ProjectSnapshot {
 234    pub worktree_snapshots: Vec<WorktreeSnapshot>,
 235    pub unsaved_buffer_paths: Vec<String>,
 236    pub timestamp: DateTime<Utc>,
 237}
 238
 239#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
 240pub struct WorktreeSnapshot {
 241    pub worktree_path: String,
 242    pub git_state: Option<GitState>,
 243}
 244
 245#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
 246pub struct GitState {
 247    pub remote_url: Option<String>,
 248    pub head_sha: Option<String>,
 249    pub current_branch: Option<String>,
 250    pub diff: Option<String>,
 251}
 252
 253#[derive(Clone, Debug)]
 254pub struct ThreadCheckpoint {
 255    message_id: MessageId,
 256    git_checkpoint: GitStoreCheckpoint,
 257}
 258
 259#[derive(Copy, Clone, Debug, PartialEq, Eq)]
 260pub enum ThreadFeedback {
 261    Positive,
 262    Negative,
 263}
 264
 265pub enum LastRestoreCheckpoint {
 266    Pending {
 267        message_id: MessageId,
 268    },
 269    Error {
 270        message_id: MessageId,
 271        error: String,
 272    },
 273}
 274
 275impl LastRestoreCheckpoint {
 276    pub fn message_id(&self) -> MessageId {
 277        match self {
 278            LastRestoreCheckpoint::Pending { message_id } => *message_id,
 279            LastRestoreCheckpoint::Error { message_id, .. } => *message_id,
 280        }
 281    }
 282}
 283
 284#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
 285pub enum DetailedSummaryState {
 286    #[default]
 287    NotGenerated,
 288    Generating {
 289        message_id: MessageId,
 290    },
 291    Generated {
 292        text: SharedString,
 293        message_id: MessageId,
 294    },
 295}
 296
 297impl DetailedSummaryState {
 298    fn text(&self) -> Option<SharedString> {
 299        if let Self::Generated { text, .. } = self {
 300            Some(text.clone())
 301        } else {
 302            None
 303        }
 304    }
 305}
 306
 307#[derive(Default, Debug)]
 308pub struct TotalTokenUsage {
 309    pub total: u64,
 310    pub max: u64,
 311}
 312
 313impl TotalTokenUsage {
 314    pub fn ratio(&self) -> TokenUsageRatio {
 315        #[cfg(debug_assertions)]
 316        let warning_threshold: f32 = std::env::var("ZED_THREAD_WARNING_THRESHOLD")
 317            .unwrap_or("0.8".to_string())
 318            .parse()
 319            .unwrap();
 320        #[cfg(not(debug_assertions))]
 321        let warning_threshold: f32 = 0.8;
 322
 323        // When the maximum is unknown because there is no selected model,
 324        // avoid showing the token limit warning.
 325        if self.max == 0 {
 326            TokenUsageRatio::Normal
 327        } else if self.total >= self.max {
 328            TokenUsageRatio::Exceeded
 329        } else if self.total as f32 / self.max as f32 >= warning_threshold {
 330            TokenUsageRatio::Warning
 331        } else {
 332            TokenUsageRatio::Normal
 333        }
 334    }
 335
 336    pub fn add(&self, tokens: u64) -> TotalTokenUsage {
 337        TotalTokenUsage {
 338            total: self.total + tokens,
 339            max: self.max,
 340        }
 341    }
 342}
 343
 344#[derive(Debug, Default, PartialEq, Eq)]
 345pub enum TokenUsageRatio {
 346    #[default]
 347    Normal,
 348    Warning,
 349    Exceeded,
 350}
 351
 352#[derive(Debug, Clone, Copy)]
 353pub enum QueueState {
 354    Sending,
 355    Queued { position: usize },
 356    Started,
 357}
 358
 359/// A thread of conversation with the LLM.
 360pub struct Thread {
 361    id: ThreadId,
 362    updated_at: DateTime<Utc>,
 363    summary: ThreadSummary,
 364    pending_summary: Task<Option<()>>,
 365    detailed_summary_task: Task<Option<()>>,
 366    detailed_summary_tx: postage::watch::Sender<DetailedSummaryState>,
 367    detailed_summary_rx: postage::watch::Receiver<DetailedSummaryState>,
 368    completion_mode: agent_settings::CompletionMode,
 369    messages: Vec<Message>,
 370    next_message_id: MessageId,
 371    last_prompt_id: PromptId,
 372    project_context: SharedProjectContext,
 373    checkpoints_by_message: HashMap<MessageId, ThreadCheckpoint>,
 374    completion_count: usize,
 375    pending_completions: Vec<PendingCompletion>,
 376    project: Entity<Project>,
 377    prompt_builder: Arc<PromptBuilder>,
 378    tools: Entity<ToolWorkingSet>,
 379    tool_use: ToolUseState,
 380    action_log: Entity<ActionLog>,
 381    last_restore_checkpoint: Option<LastRestoreCheckpoint>,
 382    pending_checkpoint: Option<ThreadCheckpoint>,
 383    initial_project_snapshot: Shared<Task<Option<Arc<ProjectSnapshot>>>>,
 384    request_token_usage: Vec<TokenUsage>,
 385    cumulative_token_usage: TokenUsage,
 386    exceeded_window_error: Option<ExceededWindowError>,
 387    tool_use_limit_reached: bool,
 388    feedback: Option<ThreadFeedback>,
 389    retry_state: Option<RetryState>,
 390    message_feedback: HashMap<MessageId, ThreadFeedback>,
 391    last_auto_capture_at: Option<Instant>,
 392    last_received_chunk_at: Option<Instant>,
 393    request_callback: Option<
 394        Box<dyn FnMut(&LanguageModelRequest, &[Result<LanguageModelCompletionEvent, String>])>,
 395    >,
 396    remaining_turns: u32,
 397    configured_model: Option<ConfiguredModel>,
 398    profile: AgentProfile,
 399    last_error_context: Option<(Arc<dyn LanguageModel>, CompletionIntent)>,
 400}
 401
 402#[derive(Clone, Debug)]
 403struct RetryState {
 404    attempt: u8,
 405    max_attempts: u8,
 406    intent: CompletionIntent,
 407}
 408
 409#[derive(Clone, Debug, PartialEq, Eq)]
 410pub enum ThreadSummary {
 411    Pending,
 412    Generating,
 413    Ready(SharedString),
 414    Error,
 415}
 416
 417impl ThreadSummary {
 418    pub const DEFAULT: SharedString = SharedString::new_static("New Thread");
 419
 420    pub fn or_default(&self) -> SharedString {
 421        self.unwrap_or(Self::DEFAULT)
 422    }
 423
 424    pub fn unwrap_or(&self, message: impl Into<SharedString>) -> SharedString {
 425        self.ready().unwrap_or_else(|| message.into())
 426    }
 427
 428    pub fn ready(&self) -> Option<SharedString> {
 429        match self {
 430            ThreadSummary::Ready(summary) => Some(summary.clone()),
 431            ThreadSummary::Pending | ThreadSummary::Generating | ThreadSummary::Error => None,
 432        }
 433    }
 434}
 435
 436#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
 437pub struct ExceededWindowError {
 438    /// Model used when last message exceeded context window
 439    model_id: LanguageModelId,
 440    /// Token count including last message
 441    token_count: u64,
 442}
 443
 444impl Thread {
 445    pub fn new(
 446        project: Entity<Project>,
 447        tools: Entity<ToolWorkingSet>,
 448        prompt_builder: Arc<PromptBuilder>,
 449        system_prompt: SharedProjectContext,
 450        cx: &mut Context<Self>,
 451    ) -> Self {
 452        let (detailed_summary_tx, detailed_summary_rx) = postage::watch::channel();
 453        let configured_model = LanguageModelRegistry::read_global(cx).default_model();
 454        let profile_id = AgentSettings::get_global(cx).default_profile.clone();
 455
 456        Self {
 457            id: ThreadId::new(),
 458            updated_at: Utc::now(),
 459            summary: ThreadSummary::Pending,
 460            pending_summary: Task::ready(None),
 461            detailed_summary_task: Task::ready(None),
 462            detailed_summary_tx,
 463            detailed_summary_rx,
 464            completion_mode: AgentSettings::get_global(cx).preferred_completion_mode,
 465            messages: Vec::new(),
 466            next_message_id: MessageId(0),
 467            last_prompt_id: PromptId::new(),
 468            project_context: system_prompt,
 469            checkpoints_by_message: HashMap::default(),
 470            completion_count: 0,
 471            pending_completions: Vec::new(),
 472            project: project.clone(),
 473            prompt_builder,
 474            tools: tools.clone(),
 475            last_restore_checkpoint: None,
 476            pending_checkpoint: None,
 477            tool_use: ToolUseState::new(tools.clone()),
 478            action_log: cx.new(|_| ActionLog::new(project.clone())),
 479            initial_project_snapshot: {
 480                let project_snapshot = Self::project_snapshot(project, cx);
 481                cx.foreground_executor()
 482                    .spawn(async move { Some(project_snapshot.await) })
 483                    .shared()
 484            },
 485            request_token_usage: Vec::new(),
 486            cumulative_token_usage: TokenUsage::default(),
 487            exceeded_window_error: None,
 488            tool_use_limit_reached: false,
 489            feedback: None,
 490            retry_state: None,
 491            message_feedback: HashMap::default(),
 492            last_auto_capture_at: None,
 493            last_error_context: None,
 494            last_received_chunk_at: None,
 495            request_callback: None,
 496            remaining_turns: u32::MAX,
 497            configured_model: configured_model.clone(),
 498            profile: AgentProfile::new(profile_id, tools),
 499        }
 500    }
 501
 502    pub fn deserialize(
 503        id: ThreadId,
 504        serialized: SerializedThread,
 505        project: Entity<Project>,
 506        tools: Entity<ToolWorkingSet>,
 507        prompt_builder: Arc<PromptBuilder>,
 508        project_context: SharedProjectContext,
 509        window: Option<&mut Window>, // None in headless mode
 510        cx: &mut Context<Self>,
 511    ) -> Self {
 512        let next_message_id = MessageId(
 513            serialized
 514                .messages
 515                .last()
 516                .map(|message| message.id.0 + 1)
 517                .unwrap_or(0),
 518        );
 519        let tool_use = ToolUseState::from_serialized_messages(
 520            tools.clone(),
 521            &serialized.messages,
 522            project.clone(),
 523            window,
 524            cx,
 525        );
 526        let (detailed_summary_tx, detailed_summary_rx) =
 527            postage::watch::channel_with(serialized.detailed_summary_state);
 528
 529        let configured_model = LanguageModelRegistry::global(cx).update(cx, |registry, cx| {
 530            serialized
 531                .model
 532                .and_then(|model| {
 533                    let model = SelectedModel {
 534                        provider: model.provider.clone().into(),
 535                        model: model.model.clone().into(),
 536                    };
 537                    registry.select_model(&model, cx)
 538                })
 539                .or_else(|| registry.default_model())
 540        });
 541
 542        let completion_mode = serialized
 543            .completion_mode
 544            .unwrap_or_else(|| AgentSettings::get_global(cx).preferred_completion_mode);
 545        let profile_id = serialized
 546            .profile
 547            .unwrap_or_else(|| AgentSettings::get_global(cx).default_profile.clone());
 548
 549        Self {
 550            id,
 551            updated_at: serialized.updated_at,
 552            summary: ThreadSummary::Ready(serialized.summary),
 553            pending_summary: Task::ready(None),
 554            detailed_summary_task: Task::ready(None),
 555            detailed_summary_tx,
 556            detailed_summary_rx,
 557            completion_mode,
 558            retry_state: None,
 559            messages: serialized
 560                .messages
 561                .into_iter()
 562                .map(|message| Message {
 563                    id: message.id,
 564                    role: message.role,
 565                    segments: message
 566                        .segments
 567                        .into_iter()
 568                        .map(|segment| match segment {
 569                            SerializedMessageSegment::Text { text } => MessageSegment::Text(text),
 570                            SerializedMessageSegment::Thinking { text, signature } => {
 571                                MessageSegment::Thinking { text, signature }
 572                            }
 573                            SerializedMessageSegment::RedactedThinking { data } => {
 574                                MessageSegment::RedactedThinking(data)
 575                            }
 576                        })
 577                        .collect(),
 578                    loaded_context: LoadedContext {
 579                        contexts: Vec::new(),
 580                        text: message.context,
 581                        images: Vec::new(),
 582                    },
 583                    creases: message
 584                        .creases
 585                        .into_iter()
 586                        .map(|crease| MessageCrease {
 587                            range: crease.start..crease.end,
 588                            icon_path: crease.icon_path,
 589                            label: crease.label,
 590                            context: None,
 591                        })
 592                        .collect(),
 593                    is_hidden: message.is_hidden,
 594                    ui_only: false, // UI-only messages are not persisted
 595                })
 596                .collect(),
 597            next_message_id,
 598            last_prompt_id: PromptId::new(),
 599            project_context,
 600            checkpoints_by_message: HashMap::default(),
 601            completion_count: 0,
 602            pending_completions: Vec::new(),
 603            last_restore_checkpoint: None,
 604            pending_checkpoint: None,
 605            project: project.clone(),
 606            prompt_builder,
 607            tools: tools.clone(),
 608            tool_use,
 609            action_log: cx.new(|_| ActionLog::new(project)),
 610            initial_project_snapshot: Task::ready(serialized.initial_project_snapshot).shared(),
 611            request_token_usage: serialized.request_token_usage,
 612            cumulative_token_usage: serialized.cumulative_token_usage,
 613            exceeded_window_error: None,
 614            tool_use_limit_reached: serialized.tool_use_limit_reached,
 615            feedback: None,
 616            message_feedback: HashMap::default(),
 617            last_auto_capture_at: None,
 618            last_error_context: None,
 619            last_received_chunk_at: None,
 620            request_callback: None,
 621            remaining_turns: u32::MAX,
 622            configured_model,
 623            profile: AgentProfile::new(profile_id, tools),
 624        }
 625    }
 626
 627    pub fn set_request_callback(
 628        &mut self,
 629        callback: impl 'static
 630        + FnMut(&LanguageModelRequest, &[Result<LanguageModelCompletionEvent, String>]),
 631    ) {
 632        self.request_callback = Some(Box::new(callback));
 633    }
 634
 635    pub fn id(&self) -> &ThreadId {
 636        &self.id
 637    }
 638
 639    pub fn profile(&self) -> &AgentProfile {
 640        &self.profile
 641    }
 642
 643    pub fn set_profile(&mut self, id: AgentProfileId, cx: &mut Context<Self>) {
 644        if &id != self.profile.id() {
 645            self.profile = AgentProfile::new(id, self.tools.clone());
 646            cx.emit(ThreadEvent::ProfileChanged);
 647        }
 648    }
 649
 650    pub fn is_empty(&self) -> bool {
 651        self.messages.is_empty()
 652    }
 653
 654    pub fn updated_at(&self) -> DateTime<Utc> {
 655        self.updated_at
 656    }
 657
 658    pub fn touch_updated_at(&mut self) {
 659        self.updated_at = Utc::now();
 660    }
 661
 662    pub fn advance_prompt_id(&mut self) {
 663        self.last_prompt_id = PromptId::new();
 664    }
 665
 666    pub fn project_context(&self) -> SharedProjectContext {
 667        self.project_context.clone()
 668    }
 669
 670    pub fn get_or_init_configured_model(&mut self, cx: &App) -> Option<ConfiguredModel> {
 671        if self.configured_model.is_none() {
 672            self.configured_model = LanguageModelRegistry::read_global(cx).default_model();
 673        }
 674        self.configured_model.clone()
 675    }
 676
 677    pub fn configured_model(&self) -> Option<ConfiguredModel> {
 678        self.configured_model.clone()
 679    }
 680
 681    pub fn set_configured_model(&mut self, model: Option<ConfiguredModel>, cx: &mut Context<Self>) {
 682        self.configured_model = model;
 683        cx.notify();
 684    }
 685
 686    pub fn summary(&self) -> &ThreadSummary {
 687        &self.summary
 688    }
 689
 690    pub fn set_summary(&mut self, new_summary: impl Into<SharedString>, cx: &mut Context<Self>) {
 691        let current_summary = match &self.summary {
 692            ThreadSummary::Pending | ThreadSummary::Generating => return,
 693            ThreadSummary::Ready(summary) => summary,
 694            ThreadSummary::Error => &ThreadSummary::DEFAULT,
 695        };
 696
 697        let mut new_summary = new_summary.into();
 698
 699        if new_summary.is_empty() {
 700            new_summary = ThreadSummary::DEFAULT;
 701        }
 702
 703        if current_summary != &new_summary {
 704            self.summary = ThreadSummary::Ready(new_summary);
 705            cx.emit(ThreadEvent::SummaryChanged);
 706        }
 707    }
 708
 709    pub fn completion_mode(&self) -> CompletionMode {
 710        self.completion_mode
 711    }
 712
 713    pub fn set_completion_mode(&mut self, mode: CompletionMode) {
 714        self.completion_mode = mode;
 715    }
 716
 717    pub fn message(&self, id: MessageId) -> Option<&Message> {
 718        let index = self
 719            .messages
 720            .binary_search_by(|message| message.id.cmp(&id))
 721            .ok()?;
 722
 723        self.messages.get(index)
 724    }
 725
 726    pub fn messages(&self) -> impl ExactSizeIterator<Item = &Message> {
 727        self.messages.iter()
 728    }
 729
 730    pub fn is_generating(&self) -> bool {
 731        !self.pending_completions.is_empty() || !self.all_tools_finished()
 732    }
 733
 734    /// Indicates whether streaming of language model events is stale.
 735    /// When `is_generating()` is false, this method returns `None`.
 736    pub fn is_generation_stale(&self) -> Option<bool> {
 737        const STALE_THRESHOLD: u128 = 250;
 738
 739        self.last_received_chunk_at
 740            .map(|instant| instant.elapsed().as_millis() > STALE_THRESHOLD)
 741    }
 742
 743    fn received_chunk(&mut self) {
 744        self.last_received_chunk_at = Some(Instant::now());
 745    }
 746
 747    pub fn queue_state(&self) -> Option<QueueState> {
 748        self.pending_completions
 749            .first()
 750            .map(|pending_completion| pending_completion.queue_state)
 751    }
 752
 753    pub fn tools(&self) -> &Entity<ToolWorkingSet> {
 754        &self.tools
 755    }
 756
 757    pub fn pending_tool(&self, id: &LanguageModelToolUseId) -> Option<&PendingToolUse> {
 758        self.tool_use
 759            .pending_tool_uses()
 760            .into_iter()
 761            .find(|tool_use| &tool_use.id == id)
 762    }
 763
 764    pub fn tools_needing_confirmation(&self) -> impl Iterator<Item = &PendingToolUse> {
 765        self.tool_use
 766            .pending_tool_uses()
 767            .into_iter()
 768            .filter(|tool_use| tool_use.status.needs_confirmation())
 769    }
 770
 771    pub fn has_pending_tool_uses(&self) -> bool {
 772        !self.tool_use.pending_tool_uses().is_empty()
 773    }
 774
 775    pub fn checkpoint_for_message(&self, id: MessageId) -> Option<ThreadCheckpoint> {
 776        self.checkpoints_by_message.get(&id).cloned()
 777    }
 778
 779    pub fn restore_checkpoint(
 780        &mut self,
 781        checkpoint: ThreadCheckpoint,
 782        cx: &mut Context<Self>,
 783    ) -> Task<Result<()>> {
 784        self.last_restore_checkpoint = Some(LastRestoreCheckpoint::Pending {
 785            message_id: checkpoint.message_id,
 786        });
 787        cx.emit(ThreadEvent::CheckpointChanged);
 788        cx.notify();
 789
 790        let git_store = self.project().read(cx).git_store().clone();
 791        let restore = git_store.update(cx, |git_store, cx| {
 792            git_store.restore_checkpoint(checkpoint.git_checkpoint.clone(), cx)
 793        });
 794
 795        cx.spawn(async move |this, cx| {
 796            let result = restore.await;
 797            this.update(cx, |this, cx| {
 798                if let Err(err) = result.as_ref() {
 799                    this.last_restore_checkpoint = Some(LastRestoreCheckpoint::Error {
 800                        message_id: checkpoint.message_id,
 801                        error: err.to_string(),
 802                    });
 803                } else {
 804                    this.truncate(checkpoint.message_id, cx);
 805                    this.last_restore_checkpoint = None;
 806                }
 807                this.pending_checkpoint = None;
 808                cx.emit(ThreadEvent::CheckpointChanged);
 809                cx.notify();
 810            })?;
 811            result
 812        })
 813    }
 814
 815    fn finalize_pending_checkpoint(&mut self, cx: &mut Context<Self>) {
 816        dbg!("finalize_pending_checkpoint");
 817        let pending_checkpoint = if self.is_generating() {
 818            return;
 819        } else if let Some(checkpoint) = self.pending_checkpoint.take() {
 820            checkpoint
 821        } else {
 822            return;
 823        };
 824
 825        self.finalize_checkpoint(pending_checkpoint, cx);
 826    }
 827
 828    fn finalize_checkpoint(
 829        &mut self,
 830        pending_checkpoint: ThreadCheckpoint,
 831        cx: &mut Context<Self>,
 832    ) {
 833        dbg!("finalize_checkpoint");
 834        let git_store = self.project.read(cx).git_store().clone();
 835        let final_checkpoint = git_store.update(cx, |git_store, cx| git_store.checkpoint(cx));
 836        cx.spawn(async move |this, cx| match final_checkpoint.await {
 837            Ok(final_checkpoint) => {
 838                dbg!(&pending_checkpoint.git_checkpoint);
 839                dbg!(&final_checkpoint);
 840                let equal = git_store
 841                    .update(cx, |store, cx| {
 842                        store.compare_checkpoints(
 843                            pending_checkpoint.git_checkpoint.clone(),
 844                            final_checkpoint.clone(),
 845                            cx,
 846                        )
 847                    })?
 848                    .await
 849                    .unwrap_or(false);
 850
 851                if dbg!(!equal) {
 852                    this.update(cx, |this, cx| {
 853                        this.insert_checkpoint(pending_checkpoint, cx)
 854                    })?;
 855                }
 856
 857                Ok(())
 858            }
 859            Err(_) => this.update(cx, |this, cx| {
 860                this.insert_checkpoint(pending_checkpoint, cx)
 861            }),
 862        })
 863        .detach();
 864    }
 865
 866    fn insert_checkpoint(&mut self, checkpoint: ThreadCheckpoint, cx: &mut Context<Self>) {
 867        dbg!("insert_checkpoint");
 868        self.checkpoints_by_message
 869            .insert(checkpoint.message_id, checkpoint);
 870        cx.emit(ThreadEvent::CheckpointChanged);
 871        cx.notify();
 872    }
 873
 874    pub fn last_restore_checkpoint(&self) -> Option<&LastRestoreCheckpoint> {
 875        dbg!();
 876        self.last_restore_checkpoint.as_ref()
 877    }
 878
 879    pub fn truncate(&mut self, message_id: MessageId, cx: &mut Context<Self>) {
 880        let Some(message_ix) = self
 881            .messages
 882            .iter()
 883            .rposition(|message| message.id == message_id)
 884        else {
 885            return;
 886        };
 887        for deleted_message in self.messages.drain(message_ix..) {
 888            self.checkpoints_by_message.remove(&deleted_message.id);
 889        }
 890        cx.notify();
 891    }
 892
 893    pub fn context_for_message(&self, id: MessageId) -> impl Iterator<Item = &AgentContext> {
 894        self.messages
 895            .iter()
 896            .find(|message| message.id == id)
 897            .into_iter()
 898            .flat_map(|message| message.loaded_context.contexts.iter())
 899    }
 900
 901    pub fn is_turn_end(&self, ix: usize) -> bool {
 902        if self.messages.is_empty() {
 903            return false;
 904        }
 905
 906        if !self.is_generating() && ix == self.messages.len() - 1 {
 907            return true;
 908        }
 909
 910        let Some(message) = self.messages.get(ix) else {
 911            return false;
 912        };
 913
 914        if message.role != Role::Assistant {
 915            return false;
 916        }
 917
 918        self.messages
 919            .get(ix + 1)
 920            .and_then(|message| {
 921                self.message(message.id)
 922                    .map(|next_message| next_message.role == Role::User && !next_message.is_hidden)
 923            })
 924            .unwrap_or(false)
 925    }
 926
 927    pub fn tool_use_limit_reached(&self) -> bool {
 928        self.tool_use_limit_reached
 929    }
 930
 931    /// Returns whether all of the tool uses have finished running.
 932    pub fn all_tools_finished(&self) -> bool {
 933        // If the only pending tool uses left are the ones with errors, then
 934        // that means that we've finished running all of the pending tools.
 935        self.tool_use
 936            .pending_tool_uses()
 937            .iter()
 938            .all(|pending_tool_use| pending_tool_use.status.is_error())
 939    }
 940
 941    /// Returns whether any pending tool uses may perform edits
 942    pub fn has_pending_edit_tool_uses(&self) -> bool {
 943        self.tool_use
 944            .pending_tool_uses()
 945            .iter()
 946            .filter(|pending_tool_use| !pending_tool_use.status.is_error())
 947            .any(|pending_tool_use| pending_tool_use.may_perform_edits)
 948    }
 949
 950    pub fn tool_uses_for_message(&self, id: MessageId, cx: &App) -> Vec<ToolUse> {
 951        self.tool_use.tool_uses_for_message(id, &self.project, cx)
 952    }
 953
 954    pub fn tool_results_for_message(
 955        &self,
 956        assistant_message_id: MessageId,
 957    ) -> Vec<&LanguageModelToolResult> {
 958        self.tool_use.tool_results_for_message(assistant_message_id)
 959    }
 960
 961    pub fn tool_result(&self, id: &LanguageModelToolUseId) -> Option<&LanguageModelToolResult> {
 962        self.tool_use.tool_result(id)
 963    }
 964
 965    pub fn output_for_tool(&self, id: &LanguageModelToolUseId) -> Option<&Arc<str>> {
 966        match &self.tool_use.tool_result(id)?.content {
 967            LanguageModelToolResultContent::Text(text) => Some(text),
 968            LanguageModelToolResultContent::Image(_) => {
 969                // TODO: We should display image
 970                None
 971            }
 972        }
 973    }
 974
 975    pub fn card_for_tool(&self, id: &LanguageModelToolUseId) -> Option<AnyToolCard> {
 976        self.tool_use.tool_result_card(id).cloned()
 977    }
 978
 979    /// Return tools that are both enabled and supported by the model
 980    pub fn available_tools(
 981        &self,
 982        cx: &App,
 983        model: Arc<dyn LanguageModel>,
 984    ) -> Vec<LanguageModelRequestTool> {
 985        if model.supports_tools() {
 986            self.profile
 987                .enabled_tools(cx)
 988                .into_iter()
 989                .filter_map(|(name, tool)| {
 990                    // Skip tools that cannot be supported
 991                    let input_schema = tool.input_schema(model.tool_input_format()).ok()?;
 992                    Some(LanguageModelRequestTool {
 993                        name: name.into(),
 994                        description: tool.description(),
 995                        input_schema,
 996                    })
 997                })
 998                .collect()
 999        } else {
1000            Vec::default()
1001        }
1002    }
1003
1004    pub fn insert_user_message(
1005        &mut self,
1006        text: impl Into<String>,
1007        loaded_context: ContextLoadResult,
1008        git_checkpoint: Option<GitStoreCheckpoint>,
1009        creases: Vec<MessageCrease>,
1010        cx: &mut Context<Self>,
1011    ) -> MessageId {
1012        if !loaded_context.referenced_buffers.is_empty() {
1013            self.action_log.update(cx, |log, cx| {
1014                for buffer in loaded_context.referenced_buffers {
1015                    log.buffer_read(buffer, cx);
1016                }
1017            });
1018        }
1019
1020        let message_id = self.insert_message(
1021            Role::User,
1022            vec![MessageSegment::Text(text.into())],
1023            loaded_context.loaded_context,
1024            creases,
1025            false,
1026            cx,
1027        );
1028
1029        if let Some(git_checkpoint) = git_checkpoint {
1030            self.pending_checkpoint = Some(ThreadCheckpoint {
1031                message_id,
1032                git_checkpoint,
1033            });
1034        }
1035
1036        self.auto_capture_telemetry(cx);
1037
1038        message_id
1039    }
1040
1041    pub fn insert_invisible_continue_message(&mut self, cx: &mut Context<Self>) -> MessageId {
1042        let id = self.insert_message(
1043            Role::User,
1044            vec![MessageSegment::Text("Continue where you left off".into())],
1045            LoadedContext::default(),
1046            vec![],
1047            true,
1048            cx,
1049        );
1050        self.pending_checkpoint = None;
1051
1052        id
1053    }
1054
1055    pub fn insert_assistant_message(
1056        &mut self,
1057        segments: Vec<MessageSegment>,
1058        cx: &mut Context<Self>,
1059    ) -> MessageId {
1060        self.insert_message(
1061            Role::Assistant,
1062            segments,
1063            LoadedContext::default(),
1064            Vec::new(),
1065            false,
1066            cx,
1067        )
1068    }
1069
1070    pub fn insert_message(
1071        &mut self,
1072        role: Role,
1073        segments: Vec<MessageSegment>,
1074        loaded_context: LoadedContext,
1075        creases: Vec<MessageCrease>,
1076        is_hidden: bool,
1077        cx: &mut Context<Self>,
1078    ) -> MessageId {
1079        let id = self.next_message_id.post_inc();
1080        self.messages.push(Message {
1081            id,
1082            role,
1083            segments,
1084            loaded_context,
1085            creases,
1086            is_hidden,
1087            ui_only: false,
1088        });
1089        self.touch_updated_at();
1090        cx.emit(ThreadEvent::MessageAdded(id));
1091        id
1092    }
1093
1094    pub fn edit_message(
1095        &mut self,
1096        id: MessageId,
1097        new_role: Role,
1098        new_segments: Vec<MessageSegment>,
1099        creases: Vec<MessageCrease>,
1100        loaded_context: Option<LoadedContext>,
1101        checkpoint: Option<GitStoreCheckpoint>,
1102        cx: &mut Context<Self>,
1103    ) -> bool {
1104        let Some(message) = self.messages.iter_mut().find(|message| message.id == id) else {
1105            return false;
1106        };
1107        message.role = new_role;
1108        message.segments = new_segments;
1109        message.creases = creases;
1110        if let Some(context) = loaded_context {
1111            message.loaded_context = context;
1112        }
1113        if let Some(git_checkpoint) = checkpoint {
1114            self.checkpoints_by_message.insert(
1115                id,
1116                ThreadCheckpoint {
1117                    message_id: id,
1118                    git_checkpoint,
1119                },
1120            );
1121        }
1122        self.touch_updated_at();
1123        cx.emit(ThreadEvent::MessageEdited(id));
1124        true
1125    }
1126
1127    pub fn delete_message(&mut self, id: MessageId, cx: &mut Context<Self>) -> bool {
1128        let Some(index) = self.messages.iter().position(|message| message.id == id) else {
1129            return false;
1130        };
1131        self.messages.remove(index);
1132        self.touch_updated_at();
1133        cx.emit(ThreadEvent::MessageDeleted(id));
1134        true
1135    }
1136
1137    /// Returns the representation of this [`Thread`] in a textual form.
1138    ///
1139    /// This is the representation we use when attaching a thread as context to another thread.
1140    pub fn text(&self) -> String {
1141        let mut text = String::new();
1142
1143        for message in &self.messages {
1144            text.push_str(match message.role {
1145                language_model::Role::User => "User:",
1146                language_model::Role::Assistant => "Agent:",
1147                language_model::Role::System => "System:",
1148            });
1149            text.push('\n');
1150
1151            for segment in &message.segments {
1152                match segment {
1153                    MessageSegment::Text(content) => text.push_str(content),
1154                    MessageSegment::Thinking { text: content, .. } => {
1155                        text.push_str(&format!("<think>{}</think>", content))
1156                    }
1157                    MessageSegment::RedactedThinking(_) => {}
1158                }
1159            }
1160            text.push('\n');
1161        }
1162
1163        text
1164    }
1165
1166    /// Serializes this thread into a format for storage or telemetry.
1167    pub fn serialize(&self, cx: &mut Context<Self>) -> Task<Result<SerializedThread>> {
1168        let initial_project_snapshot = self.initial_project_snapshot.clone();
1169        cx.spawn(async move |this, cx| {
1170            let initial_project_snapshot = initial_project_snapshot.await;
1171            this.read_with(cx, |this, cx| SerializedThread {
1172                version: SerializedThread::VERSION.to_string(),
1173                summary: this.summary().or_default(),
1174                updated_at: this.updated_at(),
1175                messages: this
1176                    .messages()
1177                    .filter(|message| !message.ui_only)
1178                    .map(|message| SerializedMessage {
1179                        id: message.id,
1180                        role: message.role,
1181                        segments: message
1182                            .segments
1183                            .iter()
1184                            .map(|segment| match segment {
1185                                MessageSegment::Text(text) => {
1186                                    SerializedMessageSegment::Text { text: text.clone() }
1187                                }
1188                                MessageSegment::Thinking { text, signature } => {
1189                                    SerializedMessageSegment::Thinking {
1190                                        text: text.clone(),
1191                                        signature: signature.clone(),
1192                                    }
1193                                }
1194                                MessageSegment::RedactedThinking(data) => {
1195                                    SerializedMessageSegment::RedactedThinking {
1196                                        data: data.clone(),
1197                                    }
1198                                }
1199                            })
1200                            .collect(),
1201                        tool_uses: this
1202                            .tool_uses_for_message(message.id, cx)
1203                            .into_iter()
1204                            .map(|tool_use| SerializedToolUse {
1205                                id: tool_use.id,
1206                                name: tool_use.name,
1207                                input: tool_use.input,
1208                            })
1209                            .collect(),
1210                        tool_results: this
1211                            .tool_results_for_message(message.id)
1212                            .into_iter()
1213                            .map(|tool_result| SerializedToolResult {
1214                                tool_use_id: tool_result.tool_use_id.clone(),
1215                                is_error: tool_result.is_error,
1216                                content: tool_result.content.clone(),
1217                                output: tool_result.output.clone(),
1218                            })
1219                            .collect(),
1220                        context: message.loaded_context.text.clone(),
1221                        creases: message
1222                            .creases
1223                            .iter()
1224                            .map(|crease| SerializedCrease {
1225                                start: crease.range.start,
1226                                end: crease.range.end,
1227                                icon_path: crease.icon_path.clone(),
1228                                label: crease.label.clone(),
1229                            })
1230                            .collect(),
1231                        is_hidden: message.is_hidden,
1232                    })
1233                    .collect(),
1234                initial_project_snapshot,
1235                cumulative_token_usage: this.cumulative_token_usage,
1236                request_token_usage: this.request_token_usage.clone(),
1237                detailed_summary_state: this.detailed_summary_rx.borrow().clone(),
1238                exceeded_window_error: this.exceeded_window_error.clone(),
1239                model: this
1240                    .configured_model
1241                    .as_ref()
1242                    .map(|model| SerializedLanguageModel {
1243                        provider: model.provider.id().0.to_string(),
1244                        model: model.model.id().0.to_string(),
1245                    }),
1246                completion_mode: Some(this.completion_mode),
1247                tool_use_limit_reached: this.tool_use_limit_reached,
1248                profile: Some(this.profile.id().clone()),
1249            })
1250        })
1251    }
1252
1253    pub fn remaining_turns(&self) -> u32 {
1254        self.remaining_turns
1255    }
1256
1257    pub fn set_remaining_turns(&mut self, remaining_turns: u32) {
1258        self.remaining_turns = remaining_turns;
1259    }
1260
1261    pub fn send_to_model(
1262        &mut self,
1263        model: Arc<dyn LanguageModel>,
1264        intent: CompletionIntent,
1265        window: Option<AnyWindowHandle>,
1266        cx: &mut Context<Self>,
1267    ) {
1268        if self.remaining_turns == 0 {
1269            return;
1270        }
1271
1272        self.remaining_turns -= 1;
1273
1274        self.flush_notifications(model.clone(), intent, cx);
1275
1276        let _checkpoint = self.finalize_pending_checkpoint(cx);
1277        self.stream_completion(
1278            self.to_completion_request(model.clone(), intent, cx),
1279            model,
1280            intent,
1281            window,
1282            cx,
1283        );
1284    }
1285
1286    pub fn retry_last_completion(
1287        &mut self,
1288        window: Option<AnyWindowHandle>,
1289        cx: &mut Context<Self>,
1290    ) {
1291        // Clear any existing error state
1292        self.retry_state = None;
1293
1294        // Use the last error context if available, otherwise fall back to configured model
1295        let (model, intent) = if let Some((model, intent)) = self.last_error_context.take() {
1296            (model, intent)
1297        } else if let Some(configured_model) = self.configured_model.as_ref() {
1298            let model = configured_model.model.clone();
1299            let intent = if self.has_pending_tool_uses() {
1300                CompletionIntent::ToolResults
1301            } else {
1302                CompletionIntent::UserPrompt
1303            };
1304            (model, intent)
1305        } else if let Some(configured_model) = self.get_or_init_configured_model(cx) {
1306            let model = configured_model.model.clone();
1307            let intent = if self.has_pending_tool_uses() {
1308                CompletionIntent::ToolResults
1309            } else {
1310                CompletionIntent::UserPrompt
1311            };
1312            (model, intent)
1313        } else {
1314            return;
1315        };
1316
1317        self.send_to_model(model, intent, window, cx);
1318    }
1319
1320    pub fn enable_burn_mode_and_retry(
1321        &mut self,
1322        window: Option<AnyWindowHandle>,
1323        cx: &mut Context<Self>,
1324    ) {
1325        self.completion_mode = CompletionMode::Burn;
1326        cx.emit(ThreadEvent::ProfileChanged);
1327        self.retry_last_completion(window, cx);
1328    }
1329
1330    pub fn used_tools_since_last_user_message(&self) -> bool {
1331        for message in self.messages.iter().rev() {
1332            if self.tool_use.message_has_tool_results(message.id) {
1333                return true;
1334            } else if message.role == Role::User {
1335                return false;
1336            }
1337        }
1338
1339        false
1340    }
1341
1342    pub fn to_completion_request(
1343        &self,
1344        model: Arc<dyn LanguageModel>,
1345        intent: CompletionIntent,
1346        cx: &mut Context<Self>,
1347    ) -> LanguageModelRequest {
1348        let mut request = LanguageModelRequest {
1349            thread_id: Some(self.id.to_string()),
1350            prompt_id: Some(self.last_prompt_id.to_string()),
1351            intent: Some(intent),
1352            mode: None,
1353            messages: vec![],
1354            tools: Vec::new(),
1355            tool_choice: None,
1356            stop: Vec::new(),
1357            temperature: AgentSettings::temperature_for_model(&model, cx),
1358            thinking_allowed: true,
1359        };
1360
1361        let available_tools = self.available_tools(cx, model.clone());
1362        let available_tool_names = available_tools
1363            .iter()
1364            .map(|tool| tool.name.clone())
1365            .collect();
1366
1367        let model_context = &ModelContext {
1368            available_tools: available_tool_names,
1369        };
1370
1371        if let Some(project_context) = self.project_context.borrow().as_ref() {
1372            match self
1373                .prompt_builder
1374                .generate_assistant_system_prompt(project_context, model_context)
1375            {
1376                Err(err) => {
1377                    let message = format!("{err:?}").into();
1378                    log::error!("{message}");
1379                    cx.emit(ThreadEvent::ShowError(ThreadError::Message {
1380                        header: "Error generating system prompt".into(),
1381                        message,
1382                    }));
1383                }
1384                Ok(system_prompt) => {
1385                    request.messages.push(LanguageModelRequestMessage {
1386                        role: Role::System,
1387                        content: vec![MessageContent::Text(system_prompt)],
1388                        cache: true,
1389                    });
1390                }
1391            }
1392        } else {
1393            let message = "Context for system prompt unexpectedly not ready.".into();
1394            log::error!("{message}");
1395            cx.emit(ThreadEvent::ShowError(ThreadError::Message {
1396                header: "Error generating system prompt".into(),
1397                message,
1398            }));
1399        }
1400
1401        let mut message_ix_to_cache = None;
1402        for message in &self.messages {
1403            // ui_only messages are for the UI only, not for the model
1404            if message.ui_only {
1405                continue;
1406            }
1407
1408            let mut request_message = LanguageModelRequestMessage {
1409                role: message.role,
1410                content: Vec::new(),
1411                cache: false,
1412            };
1413
1414            message
1415                .loaded_context
1416                .add_to_request_message(&mut request_message);
1417
1418            for segment in &message.segments {
1419                match segment {
1420                    MessageSegment::Text(text) => {
1421                        let text = text.trim_end();
1422                        if !text.is_empty() {
1423                            request_message
1424                                .content
1425                                .push(MessageContent::Text(text.into()));
1426                        }
1427                    }
1428                    MessageSegment::Thinking { text, signature } => {
1429                        if !text.is_empty() {
1430                            request_message.content.push(MessageContent::Thinking {
1431                                text: text.into(),
1432                                signature: signature.clone(),
1433                            });
1434                        }
1435                    }
1436                    MessageSegment::RedactedThinking(data) => {
1437                        request_message
1438                            .content
1439                            .push(MessageContent::RedactedThinking(data.clone()));
1440                    }
1441                };
1442            }
1443
1444            let mut cache_message = true;
1445            let mut tool_results_message = LanguageModelRequestMessage {
1446                role: Role::User,
1447                content: Vec::new(),
1448                cache: false,
1449            };
1450            for (tool_use, tool_result) in self.tool_use.tool_results(message.id) {
1451                if let Some(tool_result) = tool_result {
1452                    request_message
1453                        .content
1454                        .push(MessageContent::ToolUse(tool_use.clone()));
1455                    tool_results_message
1456                        .content
1457                        .push(MessageContent::ToolResult(LanguageModelToolResult {
1458                            tool_use_id: tool_use.id.clone(),
1459                            tool_name: tool_result.tool_name.clone(),
1460                            is_error: tool_result.is_error,
1461                            content: if tool_result.content.is_empty() {
1462                                // Surprisingly, the API fails if we return an empty string here.
1463                                // It thinks we are sending a tool use without a tool result.
1464                                "<Tool returned an empty string>".into()
1465                            } else {
1466                                tool_result.content.clone()
1467                            },
1468                            output: None,
1469                        }));
1470                } else {
1471                    cache_message = false;
1472                    log::debug!(
1473                        "skipped tool use {:?} because it is still pending",
1474                        tool_use
1475                    );
1476                }
1477            }
1478
1479            if cache_message {
1480                message_ix_to_cache = Some(request.messages.len());
1481            }
1482            request.messages.push(request_message);
1483
1484            if !tool_results_message.content.is_empty() {
1485                if cache_message {
1486                    message_ix_to_cache = Some(request.messages.len());
1487                }
1488                request.messages.push(tool_results_message);
1489            }
1490        }
1491
1492        // https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching
1493        if let Some(message_ix_to_cache) = message_ix_to_cache {
1494            request.messages[message_ix_to_cache].cache = true;
1495        }
1496
1497        request.tools = available_tools;
1498        request.mode = if model.supports_burn_mode() {
1499            Some(self.completion_mode.into())
1500        } else {
1501            Some(CompletionMode::Normal.into())
1502        };
1503
1504        request
1505    }
1506
1507    fn to_summarize_request(
1508        &self,
1509        model: &Arc<dyn LanguageModel>,
1510        intent: CompletionIntent,
1511        added_user_message: String,
1512        cx: &App,
1513    ) -> LanguageModelRequest {
1514        let mut request = LanguageModelRequest {
1515            thread_id: None,
1516            prompt_id: None,
1517            intent: Some(intent),
1518            mode: None,
1519            messages: vec![],
1520            tools: Vec::new(),
1521            tool_choice: None,
1522            stop: Vec::new(),
1523            temperature: AgentSettings::temperature_for_model(model, cx),
1524            thinking_allowed: false,
1525        };
1526
1527        for message in &self.messages {
1528            let mut request_message = LanguageModelRequestMessage {
1529                role: message.role,
1530                content: Vec::new(),
1531                cache: false,
1532            };
1533
1534            for segment in &message.segments {
1535                match segment {
1536                    MessageSegment::Text(text) => request_message
1537                        .content
1538                        .push(MessageContent::Text(text.clone())),
1539                    MessageSegment::Thinking { .. } => {}
1540                    MessageSegment::RedactedThinking(_) => {}
1541                }
1542            }
1543
1544            if request_message.content.is_empty() {
1545                continue;
1546            }
1547
1548            request.messages.push(request_message);
1549        }
1550
1551        request.messages.push(LanguageModelRequestMessage {
1552            role: Role::User,
1553            content: vec![MessageContent::Text(added_user_message)],
1554            cache: false,
1555        });
1556
1557        request
1558    }
1559
1560    /// Insert auto-generated notifications (if any) to the thread
1561    fn flush_notifications(
1562        &mut self,
1563        model: Arc<dyn LanguageModel>,
1564        intent: CompletionIntent,
1565        cx: &mut Context<Self>,
1566    ) {
1567        match intent {
1568            CompletionIntent::UserPrompt | CompletionIntent::ToolResults => {
1569                if let Some(pending_tool_use) = self.attach_tracked_files_state(model, cx) {
1570                    cx.emit(ThreadEvent::ToolFinished {
1571                        tool_use_id: pending_tool_use.id.clone(),
1572                        pending_tool_use: Some(pending_tool_use),
1573                    });
1574                }
1575            }
1576            CompletionIntent::ThreadSummarization
1577            | CompletionIntent::ThreadContextSummarization
1578            | CompletionIntent::CreateFile
1579            | CompletionIntent::EditFile
1580            | CompletionIntent::InlineAssist
1581            | CompletionIntent::TerminalInlineAssist
1582            | CompletionIntent::GenerateGitCommitMessage => {}
1583        };
1584    }
1585
1586    fn attach_tracked_files_state(
1587        &mut self,
1588        model: Arc<dyn LanguageModel>,
1589        cx: &mut App,
1590    ) -> Option<PendingToolUse> {
1591        // Represent notification as a simulated `project_notifications` tool call
1592        let tool_name = Arc::from("project_notifications");
1593        let tool = self.tools.read(cx).tool(&tool_name, cx)?;
1594
1595        if !self.profile.is_tool_enabled(tool.source(), tool.name(), cx) {
1596            return None;
1597        }
1598
1599        if self
1600            .action_log
1601            .update(cx, |log, cx| log.unnotified_user_edits(cx).is_none())
1602        {
1603            return None;
1604        }
1605
1606        let input = serde_json::json!({});
1607        let request = Arc::new(LanguageModelRequest::default()); // unused
1608        let window = None;
1609        let tool_result = tool.run(
1610            input,
1611            request,
1612            self.project.clone(),
1613            self.action_log.clone(),
1614            model.clone(),
1615            window,
1616            cx,
1617        );
1618
1619        let tool_use_id =
1620            LanguageModelToolUseId::from(format!("project_notifications_{}", self.messages.len()));
1621
1622        let tool_use = LanguageModelToolUse {
1623            id: tool_use_id.clone(),
1624            name: tool_name.clone(),
1625            raw_input: "{}".to_string(),
1626            input: serde_json::json!({}),
1627            is_input_complete: true,
1628        };
1629
1630        let tool_output = cx.background_executor().block(tool_result.output);
1631
1632        // Attach a project_notification tool call to the latest existing
1633        // Assistant message. We cannot create a new Assistant message
1634        // because thinking models require a `thinking` block that we
1635        // cannot mock. We cannot send a notification as a normal
1636        // (non-tool-use) User message because this distracts Agent
1637        // too much.
1638        let tool_message_id = self
1639            .messages
1640            .iter()
1641            .enumerate()
1642            .rfind(|(_, message)| message.role == Role::Assistant)
1643            .map(|(_, message)| message.id)?;
1644
1645        let tool_use_metadata = ToolUseMetadata {
1646            model: model.clone(),
1647            thread_id: self.id.clone(),
1648            prompt_id: self.last_prompt_id.clone(),
1649        };
1650
1651        self.tool_use
1652            .request_tool_use(tool_message_id, tool_use, tool_use_metadata.clone(), cx);
1653
1654        let pending_tool_use = self.tool_use.insert_tool_output(
1655            tool_use_id.clone(),
1656            tool_name,
1657            tool_output,
1658            self.configured_model.as_ref(),
1659            self.completion_mode,
1660        );
1661
1662        pending_tool_use
1663    }
1664
1665    pub fn stream_completion(
1666        &mut self,
1667        request: LanguageModelRequest,
1668        model: Arc<dyn LanguageModel>,
1669        intent: CompletionIntent,
1670        window: Option<AnyWindowHandle>,
1671        cx: &mut Context<Self>,
1672    ) {
1673        self.tool_use_limit_reached = false;
1674
1675        let pending_completion_id = post_inc(&mut self.completion_count);
1676        let mut request_callback_parameters = if self.request_callback.is_some() {
1677            Some((request.clone(), Vec::new()))
1678        } else {
1679            None
1680        };
1681        let prompt_id = self.last_prompt_id.clone();
1682        let tool_use_metadata = ToolUseMetadata {
1683            model: model.clone(),
1684            thread_id: self.id.clone(),
1685            prompt_id: prompt_id.clone(),
1686        };
1687
1688        let completion_mode = request
1689            .mode
1690            .unwrap_or(cloud_llm_client::CompletionMode::Normal);
1691
1692        self.last_received_chunk_at = Some(Instant::now());
1693
1694        let task = cx.spawn(async move |thread, cx| {
1695            let stream_completion_future = model.stream_completion(request, &cx);
1696            let initial_token_usage =
1697                thread.read_with(cx, |thread, _cx| thread.cumulative_token_usage);
1698            let stream_completion = async {
1699                let mut events = stream_completion_future.await?;
1700
1701                let mut stop_reason = StopReason::EndTurn;
1702                let mut current_token_usage = TokenUsage::default();
1703
1704                thread
1705                    .update(cx, |_thread, cx| {
1706                        cx.emit(ThreadEvent::NewRequest);
1707                    })
1708                    .ok();
1709
1710                let mut request_assistant_message_id = None;
1711
1712                while let Some(event) = events.next().await {
1713                    if let Some((_, response_events)) = request_callback_parameters.as_mut() {
1714                        response_events
1715                            .push(event.as_ref().map_err(|error| error.to_string()).cloned());
1716                    }
1717
1718                    thread.update(cx, |thread, cx| {
1719                        match event? {
1720                            LanguageModelCompletionEvent::StartMessage { .. } => {
1721                                request_assistant_message_id =
1722                                    Some(thread.insert_assistant_message(
1723                                        vec![MessageSegment::Text(String::new())],
1724                                        cx,
1725                                    ));
1726                            }
1727                            LanguageModelCompletionEvent::Stop(reason) => {
1728                                stop_reason = reason;
1729                            }
1730                            LanguageModelCompletionEvent::UsageUpdate(token_usage) => {
1731                                thread.update_token_usage_at_last_message(token_usage);
1732                                thread.cumulative_token_usage = thread.cumulative_token_usage
1733                                    + token_usage
1734                                    - current_token_usage;
1735                                current_token_usage = token_usage;
1736                            }
1737                            LanguageModelCompletionEvent::Text(chunk) => {
1738                                thread.received_chunk();
1739
1740                                cx.emit(ThreadEvent::ReceivedTextChunk);
1741                                if let Some(last_message) = thread.messages.last_mut() {
1742                                    if last_message.role == Role::Assistant
1743                                        && !thread.tool_use.has_tool_results(last_message.id)
1744                                    {
1745                                        last_message.push_text(&chunk);
1746                                        cx.emit(ThreadEvent::StreamedAssistantText(
1747                                            last_message.id,
1748                                            chunk,
1749                                        ));
1750                                    } else {
1751                                        // If we won't have an Assistant message yet, assume this chunk marks the beginning
1752                                        // of a new Assistant response.
1753                                        //
1754                                        // Importantly: We do *not* want to emit a `StreamedAssistantText` event here, as it
1755                                        // will result in duplicating the text of the chunk in the rendered Markdown.
1756                                        request_assistant_message_id =
1757                                            Some(thread.insert_assistant_message(
1758                                                vec![MessageSegment::Text(chunk.to_string())],
1759                                                cx,
1760                                            ));
1761                                    };
1762                                }
1763                            }
1764                            LanguageModelCompletionEvent::Thinking {
1765                                text: chunk,
1766                                signature,
1767                            } => {
1768                                thread.received_chunk();
1769
1770                                if let Some(last_message) = thread.messages.last_mut() {
1771                                    if last_message.role == Role::Assistant
1772                                        && !thread.tool_use.has_tool_results(last_message.id)
1773                                    {
1774                                        last_message.push_thinking(&chunk, signature);
1775                                        cx.emit(ThreadEvent::StreamedAssistantThinking(
1776                                            last_message.id,
1777                                            chunk,
1778                                        ));
1779                                    } else {
1780                                        // If we won't have an Assistant message yet, assume this chunk marks the beginning
1781                                        // of a new Assistant response.
1782                                        //
1783                                        // Importantly: We do *not* want to emit a `StreamedAssistantText` event here, as it
1784                                        // will result in duplicating the text of the chunk in the rendered Markdown.
1785                                        request_assistant_message_id =
1786                                            Some(thread.insert_assistant_message(
1787                                                vec![MessageSegment::Thinking {
1788                                                    text: chunk.to_string(),
1789                                                    signature,
1790                                                }],
1791                                                cx,
1792                                            ));
1793                                    };
1794                                }
1795                            }
1796                            LanguageModelCompletionEvent::RedactedThinking { data } => {
1797                                thread.received_chunk();
1798
1799                                if let Some(last_message) = thread.messages.last_mut() {
1800                                    if last_message.role == Role::Assistant
1801                                        && !thread.tool_use.has_tool_results(last_message.id)
1802                                    {
1803                                        last_message.push_redacted_thinking(data);
1804                                    } else {
1805                                        request_assistant_message_id =
1806                                            Some(thread.insert_assistant_message(
1807                                                vec![MessageSegment::RedactedThinking(data)],
1808                                                cx,
1809                                            ));
1810                                    };
1811                                }
1812                            }
1813                            LanguageModelCompletionEvent::ToolUse(tool_use) => {
1814                                let last_assistant_message_id = request_assistant_message_id
1815                                    .unwrap_or_else(|| {
1816                                        let new_assistant_message_id =
1817                                            thread.insert_assistant_message(vec![], cx);
1818                                        request_assistant_message_id =
1819                                            Some(new_assistant_message_id);
1820                                        new_assistant_message_id
1821                                    });
1822
1823                                let tool_use_id = tool_use.id.clone();
1824                                let streamed_input = if tool_use.is_input_complete {
1825                                    None
1826                                } else {
1827                                    Some((&tool_use.input).clone())
1828                                };
1829
1830                                let ui_text = thread.tool_use.request_tool_use(
1831                                    last_assistant_message_id,
1832                                    tool_use,
1833                                    tool_use_metadata.clone(),
1834                                    cx,
1835                                );
1836
1837                                if let Some(input) = streamed_input {
1838                                    cx.emit(ThreadEvent::StreamedToolUse {
1839                                        tool_use_id,
1840                                        ui_text,
1841                                        input,
1842                                    });
1843                                }
1844                            }
1845                            LanguageModelCompletionEvent::ToolUseJsonParseError {
1846                                id,
1847                                tool_name,
1848                                raw_input: invalid_input_json,
1849                                json_parse_error,
1850                            } => {
1851                                thread.receive_invalid_tool_json(
1852                                    id,
1853                                    tool_name,
1854                                    invalid_input_json,
1855                                    json_parse_error,
1856                                    window,
1857                                    cx,
1858                                );
1859                            }
1860                            LanguageModelCompletionEvent::StatusUpdate(status_update) => {
1861                                if let Some(completion) = thread
1862                                    .pending_completions
1863                                    .iter_mut()
1864                                    .find(|completion| completion.id == pending_completion_id)
1865                                {
1866                                    match status_update {
1867                                        CompletionRequestStatus::Queued { position } => {
1868                                            completion.queue_state =
1869                                                QueueState::Queued { position };
1870                                        }
1871                                        CompletionRequestStatus::Started => {
1872                                            completion.queue_state = QueueState::Started;
1873                                        }
1874                                        CompletionRequestStatus::Failed {
1875                                            code,
1876                                            message,
1877                                            request_id: _,
1878                                            retry_after,
1879                                        } => {
1880                                            return Err(
1881                                                LanguageModelCompletionError::from_cloud_failure(
1882                                                    model.upstream_provider_name(),
1883                                                    code,
1884                                                    message,
1885                                                    retry_after.map(Duration::from_secs_f64),
1886                                                ),
1887                                            );
1888                                        }
1889                                        CompletionRequestStatus::UsageUpdated { amount, limit } => {
1890                                            thread.update_model_request_usage(
1891                                                amount as u32,
1892                                                limit,
1893                                                cx,
1894                                            );
1895                                        }
1896                                        CompletionRequestStatus::ToolUseLimitReached => {
1897                                            thread.tool_use_limit_reached = true;
1898                                            cx.emit(ThreadEvent::ToolUseLimitReached);
1899                                        }
1900                                    }
1901                                }
1902                            }
1903                        }
1904
1905                        thread.touch_updated_at();
1906                        cx.emit(ThreadEvent::StreamedCompletion);
1907                        cx.notify();
1908
1909                        thread.auto_capture_telemetry(cx);
1910                        Ok(())
1911                    })??;
1912
1913                    smol::future::yield_now().await;
1914                }
1915
1916                thread.update(cx, |thread, cx| {
1917                    thread.last_received_chunk_at = None;
1918                    thread
1919                        .pending_completions
1920                        .retain(|completion| completion.id != pending_completion_id);
1921
1922                    // If there is a response without tool use, summarize the message. Otherwise,
1923                    // allow two tool uses before summarizing.
1924                    if matches!(thread.summary, ThreadSummary::Pending)
1925                        && thread.messages.len() >= 2
1926                        && (!thread.has_pending_tool_uses() || thread.messages.len() >= 6)
1927                    {
1928                        thread.summarize(cx);
1929                    }
1930                })?;
1931
1932                anyhow::Ok(stop_reason)
1933            };
1934
1935            let result = stream_completion.await;
1936            let mut retry_scheduled = false;
1937
1938            thread
1939                .update(cx, |thread, cx| {
1940                    thread.finalize_pending_checkpoint(cx);
1941                    match result.as_ref() {
1942                        Ok(stop_reason) => {
1943                            match stop_reason {
1944                                StopReason::ToolUse => {
1945                                    let tool_uses =
1946                                        thread.use_pending_tools(window, model.clone(), cx);
1947                                    cx.emit(ThreadEvent::UsePendingTools { tool_uses });
1948                                }
1949                                StopReason::EndTurn | StopReason::MaxTokens => {
1950                                    thread.project.update(cx, |project, cx| {
1951                                        project.set_agent_location(None, cx);
1952                                    });
1953                                }
1954                                StopReason::Refusal => {
1955                                    thread.project.update(cx, |project, cx| {
1956                                        project.set_agent_location(None, cx);
1957                                    });
1958
1959                                    // Remove the turn that was refused.
1960                                    //
1961                                    // https://docs.anthropic.com/en/docs/test-and-evaluate/strengthen-guardrails/handle-streaming-refusals#reset-context-after-refusal
1962                                    {
1963                                        let mut messages_to_remove = Vec::new();
1964
1965                                        for (ix, message) in
1966                                            thread.messages.iter().enumerate().rev()
1967                                        {
1968                                            messages_to_remove.push(message.id);
1969
1970                                            if message.role == Role::User {
1971                                                if ix == 0 {
1972                                                    break;
1973                                                }
1974
1975                                                if let Some(prev_message) =
1976                                                    thread.messages.get(ix - 1)
1977                                                {
1978                                                    if prev_message.role == Role::Assistant {
1979                                                        break;
1980                                                    }
1981                                                }
1982                                            }
1983                                        }
1984
1985                                        for message_id in messages_to_remove {
1986                                            thread.delete_message(message_id, cx);
1987                                        }
1988                                    }
1989
1990                                    cx.emit(ThreadEvent::ShowError(ThreadError::Message {
1991                                        header: "Language model refusal".into(),
1992                                        message:
1993                                            "Model refused to generate content for safety reasons."
1994                                                .into(),
1995                                    }));
1996                                }
1997                            }
1998
1999                            // We successfully completed, so cancel any remaining retries.
2000                            thread.retry_state = None;
2001                        }
2002                        Err(error) => {
2003                            thread.project.update(cx, |project, cx| {
2004                                project.set_agent_location(None, cx);
2005                            });
2006
2007                            if error.is::<PaymentRequiredError>() {
2008                                cx.emit(ThreadEvent::ShowError(ThreadError::PaymentRequired));
2009                            } else if let Some(error) =
2010                                error.downcast_ref::<ModelRequestLimitReachedError>()
2011                            {
2012                                cx.emit(ThreadEvent::ShowError(
2013                                    ThreadError::ModelRequestLimitReached { plan: error.plan },
2014                                ));
2015                            } else if let Some(completion_error) =
2016                                error.downcast_ref::<LanguageModelCompletionError>()
2017                            {
2018                                match &completion_error {
2019                                    LanguageModelCompletionError::PromptTooLarge {
2020                                        tokens, ..
2021                                    } => {
2022                                        let tokens = tokens.unwrap_or_else(|| {
2023                                            // We didn't get an exact token count from the API, so fall back on our estimate.
2024                                            thread
2025                                                .total_token_usage()
2026                                                .map(|usage| usage.total)
2027                                                .unwrap_or(0)
2028                                                // We know the context window was exceeded in practice, so if our estimate was
2029                                                // lower than max tokens, the estimate was wrong; return that we exceeded by 1.
2030                                                .max(
2031                                                    model
2032                                                        .max_token_count_for_mode(completion_mode)
2033                                                        .saturating_add(1),
2034                                                )
2035                                        });
2036                                        thread.exceeded_window_error = Some(ExceededWindowError {
2037                                            model_id: model.id(),
2038                                            token_count: tokens,
2039                                        });
2040                                        cx.notify();
2041                                    }
2042                                    _ => {
2043                                        if let Some(retry_strategy) =
2044                                            Thread::get_retry_strategy(completion_error)
2045                                        {
2046                                            log::info!(
2047                                                "Retrying with {:?} for language model completion error {:?}",
2048                                                retry_strategy,
2049                                                completion_error
2050                                            );
2051
2052                                            retry_scheduled = thread
2053                                                .handle_retryable_error_with_delay(
2054                                                    &completion_error,
2055                                                    Some(retry_strategy),
2056                                                    model.clone(),
2057                                                    intent,
2058                                                    window,
2059                                                    cx,
2060                                                );
2061                                        }
2062                                    }
2063                                }
2064                            }
2065
2066                            if !retry_scheduled {
2067                                thread.cancel_last_completion(window, cx);
2068                            }
2069                        }
2070                    }
2071
2072                    if !retry_scheduled {
2073                        cx.emit(ThreadEvent::Stopped(result.map_err(Arc::new)));
2074                    }
2075
2076                    if let Some((request_callback, (request, response_events))) = thread
2077                        .request_callback
2078                        .as_mut()
2079                        .zip(request_callback_parameters.as_ref())
2080                    {
2081                        request_callback(request, response_events);
2082                    }
2083
2084                    thread.auto_capture_telemetry(cx);
2085
2086                    if let Ok(initial_usage) = initial_token_usage {
2087                        let usage = thread.cumulative_token_usage - initial_usage;
2088
2089                        telemetry::event!(
2090                            "Assistant Thread Completion",
2091                            thread_id = thread.id().to_string(),
2092                            prompt_id = prompt_id,
2093                            model = model.telemetry_id(),
2094                            model_provider = model.provider_id().to_string(),
2095                            input_tokens = usage.input_tokens,
2096                            output_tokens = usage.output_tokens,
2097                            cache_creation_input_tokens = usage.cache_creation_input_tokens,
2098                            cache_read_input_tokens = usage.cache_read_input_tokens,
2099                        );
2100                    }
2101                })
2102                .ok();
2103        });
2104
2105        self.pending_completions.push(PendingCompletion {
2106            id: pending_completion_id,
2107            queue_state: QueueState::Sending,
2108            _task: task,
2109        });
2110    }
2111
2112    pub fn summarize(&mut self, cx: &mut Context<Self>) {
2113        let Some(model) = LanguageModelRegistry::read_global(cx).thread_summary_model() else {
2114            println!("No thread summary model");
2115            return;
2116        };
2117
2118        if !model.provider.is_authenticated(cx) {
2119            return;
2120        }
2121
2122        let request = self.to_summarize_request(
2123            &model.model,
2124            CompletionIntent::ThreadSummarization,
2125            SUMMARIZE_THREAD_PROMPT.into(),
2126            cx,
2127        );
2128
2129        self.summary = ThreadSummary::Generating;
2130
2131        self.pending_summary = cx.spawn(async move |this, cx| {
2132            let result = async {
2133                let mut messages = model.model.stream_completion(request, &cx).await?;
2134
2135                let mut new_summary = String::new();
2136                while let Some(event) = messages.next().await {
2137                    let Ok(event) = event else {
2138                        continue;
2139                    };
2140                    let text = match event {
2141                        LanguageModelCompletionEvent::Text(text) => text,
2142                        LanguageModelCompletionEvent::StatusUpdate(
2143                            CompletionRequestStatus::UsageUpdated { amount, limit },
2144                        ) => {
2145                            this.update(cx, |thread, cx| {
2146                                thread.update_model_request_usage(amount as u32, limit, cx);
2147                            })?;
2148                            continue;
2149                        }
2150                        _ => continue,
2151                    };
2152
2153                    let mut lines = text.lines();
2154                    new_summary.extend(lines.next());
2155
2156                    // Stop if the LLM generated multiple lines.
2157                    if lines.next().is_some() {
2158                        break;
2159                    }
2160                }
2161
2162                anyhow::Ok(new_summary)
2163            }
2164            .await;
2165
2166            this.update(cx, |this, cx| {
2167                match result {
2168                    Ok(new_summary) => {
2169                        if new_summary.is_empty() {
2170                            this.summary = ThreadSummary::Error;
2171                        } else {
2172                            this.summary = ThreadSummary::Ready(new_summary.into());
2173                        }
2174                    }
2175                    Err(err) => {
2176                        this.summary = ThreadSummary::Error;
2177                        log::error!("Failed to generate thread summary: {}", err);
2178                    }
2179                }
2180                cx.emit(ThreadEvent::SummaryGenerated);
2181            })
2182            .log_err()?;
2183
2184            Some(())
2185        });
2186    }
2187
2188    fn get_retry_strategy(error: &LanguageModelCompletionError) -> Option<RetryStrategy> {
2189        use LanguageModelCompletionError::*;
2190
2191        // General strategy here:
2192        // - If retrying won't help (e.g. invalid API key or payload too large), return None so we don't retry at all.
2193        // - If it's a time-based issue (e.g. server overloaded, rate limit exceeded), retry up to 4 times with exponential backoff.
2194        // - If it's an issue that *might* be fixed by retrying (e.g. internal server error), retry up to 3 times.
2195        match error {
2196            HttpResponseError {
2197                status_code: StatusCode::TOO_MANY_REQUESTS,
2198                ..
2199            } => Some(RetryStrategy::ExponentialBackoff {
2200                initial_delay: BASE_RETRY_DELAY,
2201                max_attempts: MAX_RETRY_ATTEMPTS,
2202            }),
2203            ServerOverloaded { retry_after, .. } | RateLimitExceeded { retry_after, .. } => {
2204                Some(RetryStrategy::Fixed {
2205                    delay: retry_after.unwrap_or(BASE_RETRY_DELAY),
2206                    max_attempts: MAX_RETRY_ATTEMPTS,
2207                })
2208            }
2209            UpstreamProviderError {
2210                status,
2211                retry_after,
2212                ..
2213            } => match *status {
2214                StatusCode::TOO_MANY_REQUESTS | StatusCode::SERVICE_UNAVAILABLE => {
2215                    Some(RetryStrategy::Fixed {
2216                        delay: retry_after.unwrap_or(BASE_RETRY_DELAY),
2217                        max_attempts: MAX_RETRY_ATTEMPTS,
2218                    })
2219                }
2220                StatusCode::INTERNAL_SERVER_ERROR => Some(RetryStrategy::Fixed {
2221                    delay: retry_after.unwrap_or(BASE_RETRY_DELAY),
2222                    // Internal Server Error could be anything, retry up to 3 times.
2223                    max_attempts: 3,
2224                }),
2225                status => {
2226                    // There is no StatusCode variant for the unofficial HTTP 529 ("The service is overloaded"),
2227                    // but we frequently get them in practice. See https://http.dev/529
2228                    if status.as_u16() == 529 {
2229                        Some(RetryStrategy::Fixed {
2230                            delay: retry_after.unwrap_or(BASE_RETRY_DELAY),
2231                            max_attempts: MAX_RETRY_ATTEMPTS,
2232                        })
2233                    } else {
2234                        Some(RetryStrategy::Fixed {
2235                            delay: retry_after.unwrap_or(BASE_RETRY_DELAY),
2236                            max_attempts: 2,
2237                        })
2238                    }
2239                }
2240            },
2241            ApiInternalServerError { .. } => Some(RetryStrategy::Fixed {
2242                delay: BASE_RETRY_DELAY,
2243                max_attempts: 3,
2244            }),
2245            ApiReadResponseError { .. }
2246            | HttpSend { .. }
2247            | DeserializeResponse { .. }
2248            | BadRequestFormat { .. } => Some(RetryStrategy::Fixed {
2249                delay: BASE_RETRY_DELAY,
2250                max_attempts: 3,
2251            }),
2252            // Retrying these errors definitely shouldn't help.
2253            HttpResponseError {
2254                status_code:
2255                    StatusCode::PAYLOAD_TOO_LARGE | StatusCode::FORBIDDEN | StatusCode::UNAUTHORIZED,
2256                ..
2257            }
2258            | AuthenticationError { .. }
2259            | PermissionError { .. }
2260            | NoApiKey { .. }
2261            | ApiEndpointNotFound { .. }
2262            | PromptTooLarge { .. } => None,
2263            // These errors might be transient, so retry them
2264            SerializeRequest { .. } | BuildRequestBody { .. } => Some(RetryStrategy::Fixed {
2265                delay: BASE_RETRY_DELAY,
2266                max_attempts: 1,
2267            }),
2268            // Retry all other 4xx and 5xx errors once.
2269            HttpResponseError { status_code, .. }
2270                if status_code.is_client_error() || status_code.is_server_error() =>
2271            {
2272                Some(RetryStrategy::Fixed {
2273                    delay: BASE_RETRY_DELAY,
2274                    max_attempts: 3,
2275                })
2276            }
2277            // Conservatively assume that any other errors are non-retryable
2278            HttpResponseError { .. } | Other(..) => Some(RetryStrategy::Fixed {
2279                delay: BASE_RETRY_DELAY,
2280                max_attempts: 2,
2281            }),
2282        }
2283    }
2284
2285    fn handle_retryable_error_with_delay(
2286        &mut self,
2287        error: &LanguageModelCompletionError,
2288        strategy: Option<RetryStrategy>,
2289        model: Arc<dyn LanguageModel>,
2290        intent: CompletionIntent,
2291        window: Option<AnyWindowHandle>,
2292        cx: &mut Context<Self>,
2293    ) -> bool {
2294        // Store context for the Retry button
2295        self.last_error_context = Some((model.clone(), intent));
2296
2297        // Only auto-retry if Burn Mode is enabled
2298        if self.completion_mode != CompletionMode::Burn {
2299            // Show error with retry options
2300            cx.emit(ThreadEvent::ShowError(ThreadError::RetryableError {
2301                message: format!(
2302                    "{}\n\nTo automatically retry when similar errors happen, enable Burn Mode.",
2303                    error
2304                )
2305                .into(),
2306                can_enable_burn_mode: true,
2307            }));
2308            return false;
2309        }
2310
2311        let Some(strategy) = strategy.or_else(|| Self::get_retry_strategy(error)) else {
2312            return false;
2313        };
2314
2315        let max_attempts = match &strategy {
2316            RetryStrategy::ExponentialBackoff { max_attempts, .. } => *max_attempts,
2317            RetryStrategy::Fixed { max_attempts, .. } => *max_attempts,
2318        };
2319
2320        let retry_state = self.retry_state.get_or_insert(RetryState {
2321            attempt: 0,
2322            max_attempts,
2323            intent,
2324        });
2325
2326        retry_state.attempt += 1;
2327        let attempt = retry_state.attempt;
2328        let max_attempts = retry_state.max_attempts;
2329        let intent = retry_state.intent;
2330
2331        if attempt <= max_attempts {
2332            let delay = match &strategy {
2333                RetryStrategy::ExponentialBackoff { initial_delay, .. } => {
2334                    let delay_secs = initial_delay.as_secs() * 2u64.pow((attempt - 1) as u32);
2335                    Duration::from_secs(delay_secs)
2336                }
2337                RetryStrategy::Fixed { delay, .. } => *delay,
2338            };
2339
2340            // Add a transient message to inform the user
2341            let delay_secs = delay.as_secs();
2342            let retry_message = if max_attempts == 1 {
2343                format!("{error}. Retrying in {delay_secs} seconds...")
2344            } else {
2345                format!(
2346                    "{error}. Retrying (attempt {attempt} of {max_attempts}) \
2347                    in {delay_secs} seconds..."
2348                )
2349            };
2350            log::warn!(
2351                "Retrying completion request (attempt {attempt} of {max_attempts}) \
2352                in {delay_secs} seconds: {error:?}",
2353            );
2354
2355            // Add a UI-only message instead of a regular message
2356            let id = self.next_message_id.post_inc();
2357            self.messages.push(Message {
2358                id,
2359                role: Role::System,
2360                segments: vec![MessageSegment::Text(retry_message)],
2361                loaded_context: LoadedContext::default(),
2362                creases: Vec::new(),
2363                is_hidden: false,
2364                ui_only: true,
2365            });
2366            cx.emit(ThreadEvent::MessageAdded(id));
2367
2368            // Schedule the retry
2369            let thread_handle = cx.entity().downgrade();
2370
2371            cx.spawn(async move |_thread, cx| {
2372                cx.background_executor().timer(delay).await;
2373
2374                thread_handle
2375                    .update(cx, |thread, cx| {
2376                        // Retry the completion
2377                        thread.send_to_model(model, intent, window, cx);
2378                    })
2379                    .log_err();
2380            })
2381            .detach();
2382
2383            true
2384        } else {
2385            // Max retries exceeded
2386            self.retry_state = None;
2387
2388            // Stop generating since we're giving up on retrying.
2389            self.pending_completions.clear();
2390
2391            // Show error alongside a Retry button, but no
2392            // Enable Burn Mode button (since it's already enabled)
2393            cx.emit(ThreadEvent::ShowError(ThreadError::RetryableError {
2394                message: format!("Failed after retrying: {}", error).into(),
2395                can_enable_burn_mode: false,
2396            }));
2397
2398            false
2399        }
2400    }
2401
2402    pub fn start_generating_detailed_summary_if_needed(
2403        &mut self,
2404        thread_store: WeakEntity<ThreadStore>,
2405        cx: &mut Context<Self>,
2406    ) {
2407        let Some(last_message_id) = self.messages.last().map(|message| message.id) else {
2408            return;
2409        };
2410
2411        match &*self.detailed_summary_rx.borrow() {
2412            DetailedSummaryState::Generating { message_id, .. }
2413            | DetailedSummaryState::Generated { message_id, .. }
2414                if *message_id == last_message_id =>
2415            {
2416                // Already up-to-date
2417                return;
2418            }
2419            _ => {}
2420        }
2421
2422        let Some(ConfiguredModel { model, provider }) =
2423            LanguageModelRegistry::read_global(cx).thread_summary_model()
2424        else {
2425            return;
2426        };
2427
2428        if !provider.is_authenticated(cx) {
2429            return;
2430        }
2431
2432        let added_user_message = include_str!("./prompts/summarize_thread_detailed_prompt.txt");
2433
2434        let request = self.to_summarize_request(
2435            &model,
2436            CompletionIntent::ThreadContextSummarization,
2437            added_user_message.into(),
2438            cx,
2439        );
2440
2441        *self.detailed_summary_tx.borrow_mut() = DetailedSummaryState::Generating {
2442            message_id: last_message_id,
2443        };
2444
2445        // Replace the detailed summarization task if there is one, cancelling it. It would probably
2446        // be better to allow the old task to complete, but this would require logic for choosing
2447        // which result to prefer (the old task could complete after the new one, resulting in a
2448        // stale summary).
2449        self.detailed_summary_task = cx.spawn(async move |thread, cx| {
2450            let stream = model.stream_completion_text(request, &cx);
2451            let Some(mut messages) = stream.await.log_err() else {
2452                thread
2453                    .update(cx, |thread, _cx| {
2454                        *thread.detailed_summary_tx.borrow_mut() =
2455                            DetailedSummaryState::NotGenerated;
2456                    })
2457                    .ok()?;
2458                return None;
2459            };
2460
2461            let mut new_detailed_summary = String::new();
2462
2463            while let Some(chunk) = messages.stream.next().await {
2464                if let Some(chunk) = chunk.log_err() {
2465                    new_detailed_summary.push_str(&chunk);
2466                }
2467            }
2468
2469            thread
2470                .update(cx, |thread, _cx| {
2471                    *thread.detailed_summary_tx.borrow_mut() = DetailedSummaryState::Generated {
2472                        text: new_detailed_summary.into(),
2473                        message_id: last_message_id,
2474                    };
2475                })
2476                .ok()?;
2477
2478            // Save thread so its summary can be reused later
2479            if let Some(thread) = thread.upgrade() {
2480                if let Ok(Ok(save_task)) = cx.update(|cx| {
2481                    thread_store
2482                        .update(cx, |thread_store, cx| thread_store.save_thread(&thread, cx))
2483                }) {
2484                    save_task.await.log_err();
2485                }
2486            }
2487
2488            Some(())
2489        });
2490    }
2491
2492    pub async fn wait_for_detailed_summary_or_text(
2493        this: &Entity<Self>,
2494        cx: &mut AsyncApp,
2495    ) -> Option<SharedString> {
2496        let mut detailed_summary_rx = this
2497            .read_with(cx, |this, _cx| this.detailed_summary_rx.clone())
2498            .ok()?;
2499        loop {
2500            match detailed_summary_rx.recv().await? {
2501                DetailedSummaryState::Generating { .. } => {}
2502                DetailedSummaryState::NotGenerated => {
2503                    return this.read_with(cx, |this, _cx| this.text().into()).ok();
2504                }
2505                DetailedSummaryState::Generated { text, .. } => return Some(text),
2506            }
2507        }
2508    }
2509
2510    pub fn latest_detailed_summary_or_text(&self) -> SharedString {
2511        self.detailed_summary_rx
2512            .borrow()
2513            .text()
2514            .unwrap_or_else(|| self.text().into())
2515    }
2516
2517    pub fn is_generating_detailed_summary(&self) -> bool {
2518        matches!(
2519            &*self.detailed_summary_rx.borrow(),
2520            DetailedSummaryState::Generating { .. }
2521        )
2522    }
2523
2524    pub fn use_pending_tools(
2525        &mut self,
2526        window: Option<AnyWindowHandle>,
2527        model: Arc<dyn LanguageModel>,
2528        cx: &mut Context<Self>,
2529    ) -> Vec<PendingToolUse> {
2530        self.auto_capture_telemetry(cx);
2531        let request =
2532            Arc::new(self.to_completion_request(model.clone(), CompletionIntent::ToolResults, cx));
2533        let pending_tool_uses = self
2534            .tool_use
2535            .pending_tool_uses()
2536            .into_iter()
2537            .filter(|tool_use| tool_use.status.is_idle())
2538            .cloned()
2539            .collect::<Vec<_>>();
2540
2541        for tool_use in pending_tool_uses.iter() {
2542            self.use_pending_tool(tool_use.clone(), request.clone(), model.clone(), window, cx);
2543        }
2544
2545        pending_tool_uses
2546    }
2547
2548    fn use_pending_tool(
2549        &mut self,
2550        tool_use: PendingToolUse,
2551        request: Arc<LanguageModelRequest>,
2552        model: Arc<dyn LanguageModel>,
2553        window: Option<AnyWindowHandle>,
2554        cx: &mut Context<Self>,
2555    ) {
2556        let Some(tool) = self.tools.read(cx).tool(&tool_use.name, cx) else {
2557            return self.handle_hallucinated_tool_use(tool_use.id, tool_use.name, window, cx);
2558        };
2559
2560        if !self.profile.is_tool_enabled(tool.source(), tool.name(), cx) {
2561            return self.handle_hallucinated_tool_use(tool_use.id, tool_use.name, window, cx);
2562        }
2563
2564        if tool.needs_confirmation(&tool_use.input, &self.project, cx)
2565            && !AgentSettings::get_global(cx).always_allow_tool_actions
2566        {
2567            self.tool_use.confirm_tool_use(
2568                tool_use.id,
2569                tool_use.ui_text,
2570                tool_use.input,
2571                request,
2572                tool,
2573            );
2574            cx.emit(ThreadEvent::ToolConfirmationNeeded);
2575        } else {
2576            self.run_tool(
2577                tool_use.id,
2578                tool_use.ui_text,
2579                tool_use.input,
2580                request,
2581                tool,
2582                model,
2583                window,
2584                cx,
2585            );
2586        }
2587    }
2588
2589    pub fn handle_hallucinated_tool_use(
2590        &mut self,
2591        tool_use_id: LanguageModelToolUseId,
2592        hallucinated_tool_name: Arc<str>,
2593        window: Option<AnyWindowHandle>,
2594        cx: &mut Context<Thread>,
2595    ) {
2596        let available_tools = self.profile.enabled_tools(cx);
2597
2598        let tool_list = available_tools
2599            .iter()
2600            .map(|(name, tool)| format!("- {}: {}", name, tool.description()))
2601            .collect::<Vec<_>>()
2602            .join("\n");
2603
2604        let error_message = format!(
2605            "The tool '{}' doesn't exist or is not enabled. Available tools:\n{}",
2606            hallucinated_tool_name, tool_list
2607        );
2608
2609        let pending_tool_use = self.tool_use.insert_tool_output(
2610            tool_use_id.clone(),
2611            hallucinated_tool_name,
2612            Err(anyhow!("Missing tool call: {error_message}")),
2613            self.configured_model.as_ref(),
2614            self.completion_mode,
2615        );
2616
2617        cx.emit(ThreadEvent::MissingToolUse {
2618            tool_use_id: tool_use_id.clone(),
2619            ui_text: error_message.into(),
2620        });
2621
2622        self.tool_finished(tool_use_id, pending_tool_use, false, window, cx);
2623    }
2624
2625    pub fn receive_invalid_tool_json(
2626        &mut self,
2627        tool_use_id: LanguageModelToolUseId,
2628        tool_name: Arc<str>,
2629        invalid_json: Arc<str>,
2630        error: String,
2631        window: Option<AnyWindowHandle>,
2632        cx: &mut Context<Thread>,
2633    ) {
2634        log::error!("The model returned invalid input JSON: {invalid_json}");
2635
2636        let pending_tool_use = self.tool_use.insert_tool_output(
2637            tool_use_id.clone(),
2638            tool_name,
2639            Err(anyhow!("Error parsing input JSON: {error}")),
2640            self.configured_model.as_ref(),
2641            self.completion_mode,
2642        );
2643        let ui_text = if let Some(pending_tool_use) = &pending_tool_use {
2644            pending_tool_use.ui_text.clone()
2645        } else {
2646            log::error!(
2647                "There was no pending tool use for tool use {tool_use_id}, even though it finished (with invalid input JSON)."
2648            );
2649            format!("Unknown tool {}", tool_use_id).into()
2650        };
2651
2652        cx.emit(ThreadEvent::InvalidToolInput {
2653            tool_use_id: tool_use_id.clone(),
2654            ui_text,
2655            invalid_input_json: invalid_json,
2656        });
2657
2658        self.tool_finished(tool_use_id, pending_tool_use, false, window, cx);
2659    }
2660
2661    pub fn run_tool(
2662        &mut self,
2663        tool_use_id: LanguageModelToolUseId,
2664        ui_text: impl Into<SharedString>,
2665        input: serde_json::Value,
2666        request: Arc<LanguageModelRequest>,
2667        tool: Arc<dyn Tool>,
2668        model: Arc<dyn LanguageModel>,
2669        window: Option<AnyWindowHandle>,
2670        cx: &mut Context<Thread>,
2671    ) {
2672        let task =
2673            self.spawn_tool_use(tool_use_id.clone(), request, input, tool, model, window, cx);
2674        self.tool_use
2675            .run_pending_tool(tool_use_id, ui_text.into(), task);
2676    }
2677
2678    fn spawn_tool_use(
2679        &mut self,
2680        tool_use_id: LanguageModelToolUseId,
2681        request: Arc<LanguageModelRequest>,
2682        input: serde_json::Value,
2683        tool: Arc<dyn Tool>,
2684        model: Arc<dyn LanguageModel>,
2685        window: Option<AnyWindowHandle>,
2686        cx: &mut Context<Thread>,
2687    ) -> Task<()> {
2688        let tool_name: Arc<str> = tool.name().into();
2689
2690        let tool_result = tool.run(
2691            input,
2692            request,
2693            self.project.clone(),
2694            self.action_log.clone(),
2695            model,
2696            window,
2697            cx,
2698        );
2699
2700        // Store the card separately if it exists
2701        if let Some(card) = tool_result.card.clone() {
2702            self.tool_use
2703                .insert_tool_result_card(tool_use_id.clone(), card);
2704        }
2705
2706        cx.spawn({
2707            async move |thread: WeakEntity<Thread>, cx| {
2708                let output = tool_result.output.await;
2709
2710                thread
2711                    .update(cx, |thread, cx| {
2712                        let pending_tool_use = thread.tool_use.insert_tool_output(
2713                            tool_use_id.clone(),
2714                            tool_name,
2715                            output,
2716                            thread.configured_model.as_ref(),
2717                            thread.completion_mode,
2718                        );
2719                        thread.tool_finished(tool_use_id, pending_tool_use, false, window, cx);
2720                    })
2721                    .ok();
2722            }
2723        })
2724    }
2725
2726    fn tool_finished(
2727        &mut self,
2728        tool_use_id: LanguageModelToolUseId,
2729        pending_tool_use: Option<PendingToolUse>,
2730        canceled: bool,
2731        window: Option<AnyWindowHandle>,
2732        cx: &mut Context<Self>,
2733    ) {
2734        if self.all_tools_finished() {
2735            if let Some(ConfiguredModel { model, .. }) = self.configured_model.as_ref() {
2736                if !canceled {
2737                    self.send_to_model(model.clone(), CompletionIntent::ToolResults, window, cx);
2738                }
2739                self.auto_capture_telemetry(cx);
2740            }
2741        }
2742
2743        cx.emit(ThreadEvent::ToolFinished {
2744            tool_use_id,
2745            pending_tool_use,
2746        });
2747    }
2748
2749    /// Cancels the last pending completion, if there are any pending.
2750    ///
2751    /// Returns whether a completion was canceled.
2752    pub fn cancel_last_completion(
2753        &mut self,
2754        window: Option<AnyWindowHandle>,
2755        cx: &mut Context<Self>,
2756    ) -> bool {
2757        let mut canceled = self.pending_completions.pop().is_some() || self.retry_state.is_some();
2758
2759        self.retry_state = None;
2760
2761        for pending_tool_use in self.tool_use.cancel_pending() {
2762            canceled = true;
2763            self.tool_finished(
2764                pending_tool_use.id.clone(),
2765                Some(pending_tool_use),
2766                true,
2767                window,
2768                cx,
2769            );
2770        }
2771
2772        if canceled {
2773            cx.emit(ThreadEvent::CompletionCanceled);
2774
2775            // When canceled, we always want to insert the checkpoint.
2776            // (We skip over finalize_pending_checkpoint, because it
2777            // would conclude we didn't have anything to insert here.)
2778            if let Some(checkpoint) = self.pending_checkpoint.take() {
2779                self.insert_checkpoint(checkpoint, cx);
2780            }
2781        } else {
2782            self.finalize_pending_checkpoint(cx);
2783        }
2784
2785        canceled
2786    }
2787
2788    /// Signals that any in-progress editing should be canceled.
2789    ///
2790    /// This method is used to notify listeners (like ActiveThread) that
2791    /// they should cancel any editing operations.
2792    pub fn cancel_editing(&mut self, cx: &mut Context<Self>) {
2793        cx.emit(ThreadEvent::CancelEditing);
2794    }
2795
2796    pub fn feedback(&self) -> Option<ThreadFeedback> {
2797        self.feedback
2798    }
2799
2800    pub fn message_feedback(&self, message_id: MessageId) -> Option<ThreadFeedback> {
2801        self.message_feedback.get(&message_id).copied()
2802    }
2803
2804    pub fn report_message_feedback(
2805        &mut self,
2806        message_id: MessageId,
2807        feedback: ThreadFeedback,
2808        cx: &mut Context<Self>,
2809    ) -> Task<Result<()>> {
2810        if self.message_feedback.get(&message_id) == Some(&feedback) {
2811            return Task::ready(Ok(()));
2812        }
2813
2814        let final_project_snapshot = Self::project_snapshot(self.project.clone(), cx);
2815        let serialized_thread = self.serialize(cx);
2816        let thread_id = self.id().clone();
2817        let client = self.project.read(cx).client();
2818
2819        let enabled_tool_names: Vec<String> = self
2820            .profile
2821            .enabled_tools(cx)
2822            .iter()
2823            .map(|(name, _)| name.clone().into())
2824            .collect();
2825
2826        self.message_feedback.insert(message_id, feedback);
2827
2828        cx.notify();
2829
2830        let message_content = self
2831            .message(message_id)
2832            .map(|msg| msg.to_string())
2833            .unwrap_or_default();
2834
2835        cx.background_spawn(async move {
2836            let final_project_snapshot = final_project_snapshot.await;
2837            let serialized_thread = serialized_thread.await?;
2838            let thread_data =
2839                serde_json::to_value(serialized_thread).unwrap_or_else(|_| serde_json::Value::Null);
2840
2841            let rating = match feedback {
2842                ThreadFeedback::Positive => "positive",
2843                ThreadFeedback::Negative => "negative",
2844            };
2845            telemetry::event!(
2846                "Assistant Thread Rated",
2847                rating,
2848                thread_id,
2849                enabled_tool_names,
2850                message_id = message_id.0,
2851                message_content,
2852                thread_data,
2853                final_project_snapshot
2854            );
2855            client.telemetry().flush_events().await;
2856
2857            Ok(())
2858        })
2859    }
2860
2861    pub fn report_feedback(
2862        &mut self,
2863        feedback: ThreadFeedback,
2864        cx: &mut Context<Self>,
2865    ) -> Task<Result<()>> {
2866        let last_assistant_message_id = self
2867            .messages
2868            .iter()
2869            .rev()
2870            .find(|msg| msg.role == Role::Assistant)
2871            .map(|msg| msg.id);
2872
2873        if let Some(message_id) = last_assistant_message_id {
2874            self.report_message_feedback(message_id, feedback, cx)
2875        } else {
2876            let final_project_snapshot = Self::project_snapshot(self.project.clone(), cx);
2877            let serialized_thread = self.serialize(cx);
2878            let thread_id = self.id().clone();
2879            let client = self.project.read(cx).client();
2880            self.feedback = Some(feedback);
2881            cx.notify();
2882
2883            cx.background_spawn(async move {
2884                let final_project_snapshot = final_project_snapshot.await;
2885                let serialized_thread = serialized_thread.await?;
2886                let thread_data = serde_json::to_value(serialized_thread)
2887                    .unwrap_or_else(|_| serde_json::Value::Null);
2888
2889                let rating = match feedback {
2890                    ThreadFeedback::Positive => "positive",
2891                    ThreadFeedback::Negative => "negative",
2892                };
2893                telemetry::event!(
2894                    "Assistant Thread Rated",
2895                    rating,
2896                    thread_id,
2897                    thread_data,
2898                    final_project_snapshot
2899                );
2900                client.telemetry().flush_events().await;
2901
2902                Ok(())
2903            })
2904        }
2905    }
2906
2907    /// Create a snapshot of the current project state including git information and unsaved buffers.
2908    fn project_snapshot(
2909        project: Entity<Project>,
2910        cx: &mut Context<Self>,
2911    ) -> Task<Arc<ProjectSnapshot>> {
2912        let git_store = project.read(cx).git_store().clone();
2913        let worktree_snapshots: Vec<_> = project
2914            .read(cx)
2915            .visible_worktrees(cx)
2916            .map(|worktree| Self::worktree_snapshot(worktree, git_store.clone(), cx))
2917            .collect();
2918
2919        cx.spawn(async move |_, cx| {
2920            let worktree_snapshots = futures::future::join_all(worktree_snapshots).await;
2921
2922            let mut unsaved_buffers = Vec::new();
2923            cx.update(|app_cx| {
2924                let buffer_store = project.read(app_cx).buffer_store();
2925                for buffer_handle in buffer_store.read(app_cx).buffers() {
2926                    let buffer = buffer_handle.read(app_cx);
2927                    if buffer.is_dirty() {
2928                        if let Some(file) = buffer.file() {
2929                            let path = file.path().to_string_lossy().to_string();
2930                            unsaved_buffers.push(path);
2931                        }
2932                    }
2933                }
2934            })
2935            .ok();
2936
2937            Arc::new(ProjectSnapshot {
2938                worktree_snapshots,
2939                unsaved_buffer_paths: unsaved_buffers,
2940                timestamp: Utc::now(),
2941            })
2942        })
2943    }
2944
2945    fn worktree_snapshot(
2946        worktree: Entity<project::Worktree>,
2947        git_store: Entity<GitStore>,
2948        cx: &App,
2949    ) -> Task<WorktreeSnapshot> {
2950        cx.spawn(async move |cx| {
2951            // Get worktree path and snapshot
2952            let worktree_info = cx.update(|app_cx| {
2953                let worktree = worktree.read(app_cx);
2954                let path = worktree.abs_path().to_string_lossy().to_string();
2955                let snapshot = worktree.snapshot();
2956                (path, snapshot)
2957            });
2958
2959            let Ok((worktree_path, _snapshot)) = worktree_info else {
2960                return WorktreeSnapshot {
2961                    worktree_path: String::new(),
2962                    git_state: None,
2963                };
2964            };
2965
2966            let git_state = git_store
2967                .update(cx, |git_store, cx| {
2968                    git_store
2969                        .repositories()
2970                        .values()
2971                        .find(|repo| {
2972                            repo.read(cx)
2973                                .abs_path_to_repo_path(&worktree.read(cx).abs_path())
2974                                .is_some()
2975                        })
2976                        .cloned()
2977                })
2978                .ok()
2979                .flatten()
2980                .map(|repo| {
2981                    repo.update(cx, |repo, _| {
2982                        let current_branch =
2983                            repo.branch.as_ref().map(|branch| branch.name().to_owned());
2984                        repo.send_job(None, |state, _| async move {
2985                            let RepositoryState::Local { backend, .. } = state else {
2986                                return GitState {
2987                                    remote_url: None,
2988                                    head_sha: None,
2989                                    current_branch,
2990                                    diff: None,
2991                                };
2992                            };
2993
2994                            let remote_url = backend.remote_url("origin");
2995                            let head_sha = backend.head_sha().await;
2996                            let diff = backend.diff(DiffType::HeadToWorktree).await.ok();
2997
2998                            GitState {
2999                                remote_url,
3000                                head_sha,
3001                                current_branch,
3002                                diff,
3003                            }
3004                        })
3005                    })
3006                });
3007
3008            let git_state = match git_state {
3009                Some(git_state) => match git_state.ok() {
3010                    Some(git_state) => git_state.await.ok(),
3011                    None => None,
3012                },
3013                None => None,
3014            };
3015
3016            WorktreeSnapshot {
3017                worktree_path,
3018                git_state,
3019            }
3020        })
3021    }
3022
3023    pub fn to_markdown(&self, cx: &App) -> Result<String> {
3024        let mut markdown = Vec::new();
3025
3026        let summary = self.summary().or_default();
3027        writeln!(markdown, "# {summary}\n")?;
3028
3029        for message in self.messages() {
3030            writeln!(
3031                markdown,
3032                "## {role}\n",
3033                role = match message.role {
3034                    Role::User => "User",
3035                    Role::Assistant => "Agent",
3036                    Role::System => "System",
3037                }
3038            )?;
3039
3040            if !message.loaded_context.text.is_empty() {
3041                writeln!(markdown, "{}", message.loaded_context.text)?;
3042            }
3043
3044            if !message.loaded_context.images.is_empty() {
3045                writeln!(
3046                    markdown,
3047                    "\n{} images attached as context.\n",
3048                    message.loaded_context.images.len()
3049                )?;
3050            }
3051
3052            for segment in &message.segments {
3053                match segment {
3054                    MessageSegment::Text(text) => writeln!(markdown, "{}\n", text)?,
3055                    MessageSegment::Thinking { text, .. } => {
3056                        writeln!(markdown, "<think>\n{}\n</think>\n", text)?
3057                    }
3058                    MessageSegment::RedactedThinking(_) => {}
3059                }
3060            }
3061
3062            for tool_use in self.tool_uses_for_message(message.id, cx) {
3063                writeln!(
3064                    markdown,
3065                    "**Use Tool: {} ({})**",
3066                    tool_use.name, tool_use.id
3067                )?;
3068                writeln!(markdown, "```json")?;
3069                writeln!(
3070                    markdown,
3071                    "{}",
3072                    serde_json::to_string_pretty(&tool_use.input)?
3073                )?;
3074                writeln!(markdown, "```")?;
3075            }
3076
3077            for tool_result in self.tool_results_for_message(message.id) {
3078                write!(markdown, "\n**Tool Results: {}", tool_result.tool_use_id)?;
3079                if tool_result.is_error {
3080                    write!(markdown, " (Error)")?;
3081                }
3082
3083                writeln!(markdown, "**\n")?;
3084                match &tool_result.content {
3085                    LanguageModelToolResultContent::Text(text) => {
3086                        writeln!(markdown, "{text}")?;
3087                    }
3088                    LanguageModelToolResultContent::Image(image) => {
3089                        writeln!(markdown, "![Image](data:base64,{})", image.source)?;
3090                    }
3091                }
3092
3093                if let Some(output) = tool_result.output.as_ref() {
3094                    writeln!(
3095                        markdown,
3096                        "\n\nDebug Output:\n\n```json\n{}\n```\n",
3097                        serde_json::to_string_pretty(output)?
3098                    )?;
3099                }
3100            }
3101        }
3102
3103        Ok(String::from_utf8_lossy(&markdown).to_string())
3104    }
3105
3106    pub fn keep_edits_in_range(
3107        &mut self,
3108        buffer: Entity<language::Buffer>,
3109        buffer_range: Range<language::Anchor>,
3110        cx: &mut Context<Self>,
3111    ) {
3112        self.action_log.update(cx, |action_log, cx| {
3113            action_log.keep_edits_in_range(buffer, buffer_range, cx)
3114        });
3115    }
3116
3117    pub fn keep_all_edits(&mut self, cx: &mut Context<Self>) {
3118        self.action_log
3119            .update(cx, |action_log, cx| action_log.keep_all_edits(cx));
3120    }
3121
3122    pub fn reject_edits_in_ranges(
3123        &mut self,
3124        buffer: Entity<language::Buffer>,
3125        buffer_ranges: Vec<Range<language::Anchor>>,
3126        cx: &mut Context<Self>,
3127    ) -> Task<Result<()>> {
3128        self.action_log.update(cx, |action_log, cx| {
3129            action_log.reject_edits_in_ranges(buffer, buffer_ranges, cx)
3130        })
3131    }
3132
3133    pub fn action_log(&self) -> &Entity<ActionLog> {
3134        &self.action_log
3135    }
3136
3137    pub fn project(&self) -> &Entity<Project> {
3138        &self.project
3139    }
3140
3141    pub fn auto_capture_telemetry(&mut self, cx: &mut Context<Self>) {
3142        if !cx.has_flag::<feature_flags::ThreadAutoCaptureFeatureFlag>() {
3143            return;
3144        }
3145
3146        let now = Instant::now();
3147        if let Some(last) = self.last_auto_capture_at {
3148            if now.duration_since(last).as_secs() < 10 {
3149                return;
3150            }
3151        }
3152
3153        self.last_auto_capture_at = Some(now);
3154
3155        let thread_id = self.id().clone();
3156        let github_login = self
3157            .project
3158            .read(cx)
3159            .user_store()
3160            .read(cx)
3161            .current_user()
3162            .map(|user| user.github_login.clone());
3163        let client = self.project.read(cx).client();
3164        let serialize_task = self.serialize(cx);
3165
3166        cx.background_executor()
3167            .spawn(async move {
3168                if let Ok(serialized_thread) = serialize_task.await {
3169                    if let Ok(thread_data) = serde_json::to_value(serialized_thread) {
3170                        telemetry::event!(
3171                            "Agent Thread Auto-Captured",
3172                            thread_id = thread_id.to_string(),
3173                            thread_data = thread_data,
3174                            auto_capture_reason = "tracked_user",
3175                            github_login = github_login
3176                        );
3177
3178                        client.telemetry().flush_events().await;
3179                    }
3180                }
3181            })
3182            .detach();
3183    }
3184
3185    pub fn cumulative_token_usage(&self) -> TokenUsage {
3186        self.cumulative_token_usage
3187    }
3188
3189    pub fn token_usage_up_to_message(&self, message_id: MessageId) -> TotalTokenUsage {
3190        let Some(model) = self.configured_model.as_ref() else {
3191            return TotalTokenUsage::default();
3192        };
3193
3194        let max = model
3195            .model
3196            .max_token_count_for_mode(self.completion_mode().into());
3197
3198        let index = self
3199            .messages
3200            .iter()
3201            .position(|msg| msg.id == message_id)
3202            .unwrap_or(0);
3203
3204        if index == 0 {
3205            return TotalTokenUsage { total: 0, max };
3206        }
3207
3208        let token_usage = &self
3209            .request_token_usage
3210            .get(index - 1)
3211            .cloned()
3212            .unwrap_or_default();
3213
3214        TotalTokenUsage {
3215            total: token_usage.total_tokens(),
3216            max,
3217        }
3218    }
3219
3220    pub fn total_token_usage(&self) -> Option<TotalTokenUsage> {
3221        let model = self.configured_model.as_ref()?;
3222
3223        let max = model
3224            .model
3225            .max_token_count_for_mode(self.completion_mode().into());
3226
3227        if let Some(exceeded_error) = &self.exceeded_window_error {
3228            if model.model.id() == exceeded_error.model_id {
3229                return Some(TotalTokenUsage {
3230                    total: exceeded_error.token_count,
3231                    max,
3232                });
3233            }
3234        }
3235
3236        let total = self
3237            .token_usage_at_last_message()
3238            .unwrap_or_default()
3239            .total_tokens();
3240
3241        Some(TotalTokenUsage { total, max })
3242    }
3243
3244    fn token_usage_at_last_message(&self) -> Option<TokenUsage> {
3245        self.request_token_usage
3246            .get(self.messages.len().saturating_sub(1))
3247            .or_else(|| self.request_token_usage.last())
3248            .cloned()
3249    }
3250
3251    fn update_token_usage_at_last_message(&mut self, token_usage: TokenUsage) {
3252        let placeholder = self.token_usage_at_last_message().unwrap_or_default();
3253        self.request_token_usage
3254            .resize(self.messages.len(), placeholder);
3255
3256        if let Some(last) = self.request_token_usage.last_mut() {
3257            *last = token_usage;
3258        }
3259    }
3260
3261    fn update_model_request_usage(&self, amount: u32, limit: UsageLimit, cx: &mut Context<Self>) {
3262        self.project
3263            .read(cx)
3264            .user_store()
3265            .update(cx, |user_store, cx| {
3266                user_store.update_model_request_usage(
3267                    ModelRequestUsage(RequestUsage {
3268                        amount: amount as i32,
3269                        limit,
3270                    }),
3271                    cx,
3272                )
3273            });
3274    }
3275
3276    pub fn deny_tool_use(
3277        &mut self,
3278        tool_use_id: LanguageModelToolUseId,
3279        tool_name: Arc<str>,
3280        window: Option<AnyWindowHandle>,
3281        cx: &mut Context<Self>,
3282    ) {
3283        let err = Err(anyhow::anyhow!(
3284            "Permission to run tool action denied by user"
3285        ));
3286
3287        self.tool_use.insert_tool_output(
3288            tool_use_id.clone(),
3289            tool_name,
3290            err,
3291            self.configured_model.as_ref(),
3292            self.completion_mode,
3293        );
3294        self.tool_finished(tool_use_id.clone(), None, true, window, cx);
3295    }
3296}
3297
3298#[derive(Debug, Clone, Error)]
3299pub enum ThreadError {
3300    #[error("Payment required")]
3301    PaymentRequired,
3302    #[error("Model request limit reached")]
3303    ModelRequestLimitReached { plan: Plan },
3304    #[error("Message {header}: {message}")]
3305    Message {
3306        header: SharedString,
3307        message: SharedString,
3308    },
3309    #[error("Retryable error: {message}")]
3310    RetryableError {
3311        message: SharedString,
3312        can_enable_burn_mode: bool,
3313    },
3314}
3315
3316#[derive(Debug, Clone)]
3317pub enum ThreadEvent {
3318    ShowError(ThreadError),
3319    StreamedCompletion,
3320    ReceivedTextChunk,
3321    NewRequest,
3322    StreamedAssistantText(MessageId, String),
3323    StreamedAssistantThinking(MessageId, String),
3324    StreamedToolUse {
3325        tool_use_id: LanguageModelToolUseId,
3326        ui_text: Arc<str>,
3327        input: serde_json::Value,
3328    },
3329    MissingToolUse {
3330        tool_use_id: LanguageModelToolUseId,
3331        ui_text: Arc<str>,
3332    },
3333    InvalidToolInput {
3334        tool_use_id: LanguageModelToolUseId,
3335        ui_text: Arc<str>,
3336        invalid_input_json: Arc<str>,
3337    },
3338    Stopped(Result<StopReason, Arc<anyhow::Error>>),
3339    MessageAdded(MessageId),
3340    MessageEdited(MessageId),
3341    MessageDeleted(MessageId),
3342    SummaryGenerated,
3343    SummaryChanged,
3344    UsePendingTools {
3345        tool_uses: Vec<PendingToolUse>,
3346    },
3347    ToolFinished {
3348        #[allow(unused)]
3349        tool_use_id: LanguageModelToolUseId,
3350        /// The pending tool use that corresponds to this tool.
3351        pending_tool_use: Option<PendingToolUse>,
3352    },
3353    CheckpointChanged,
3354    ToolConfirmationNeeded,
3355    ToolUseLimitReached,
3356    CancelEditing,
3357    CompletionCanceled,
3358    ProfileChanged,
3359}
3360
3361impl EventEmitter<ThreadEvent> for Thread {}
3362
3363struct PendingCompletion {
3364    id: usize,
3365    queue_state: QueueState,
3366    _task: Task<()>,
3367}
3368
3369#[cfg(test)]
3370mod tests {
3371    use super::*;
3372    use crate::{
3373        context::load_context, context_store::ContextStore, thread_store, thread_store::ThreadStore,
3374    };
3375
3376    // Test-specific constants
3377    const TEST_RATE_LIMIT_RETRY_SECS: u64 = 30;
3378    use agent_settings::{AgentProfileId, AgentSettings, LanguageModelParameters};
3379    use assistant_tool::ToolRegistry;
3380    use assistant_tools;
3381    use futures::StreamExt;
3382    use futures::future::BoxFuture;
3383    use futures::stream::BoxStream;
3384    use gpui::TestAppContext;
3385    use http_client;
3386    use language_model::fake_provider::{FakeLanguageModel, FakeLanguageModelProvider};
3387    use language_model::{
3388        LanguageModelCompletionError, LanguageModelName, LanguageModelProviderId,
3389        LanguageModelProviderName, LanguageModelToolChoice,
3390    };
3391    use parking_lot::Mutex;
3392    use project::{FakeFs, Project};
3393    use prompt_store::PromptBuilder;
3394    use serde_json::json;
3395    use settings::{Settings, SettingsStore};
3396    use std::sync::Arc;
3397    use std::time::Duration;
3398    use theme::ThemeSettings;
3399    use util::path;
3400    use workspace::Workspace;
3401
3402    #[gpui::test]
3403    async fn test_message_with_context(cx: &mut TestAppContext) {
3404        init_test_settings(cx);
3405
3406        let project = create_test_project(
3407            cx,
3408            json!({"code.rs": "fn main() {\n    println!(\"Hello, world!\");\n}"}),
3409        )
3410        .await;
3411
3412        let (_workspace, _thread_store, thread, context_store, model) =
3413            setup_test_environment(cx, project.clone()).await;
3414
3415        add_file_to_context(&project, &context_store, "test/code.rs", cx)
3416            .await
3417            .unwrap();
3418
3419        let context =
3420            context_store.read_with(cx, |store, _| store.context().next().cloned().unwrap());
3421        let loaded_context = cx
3422            .update(|cx| load_context(vec![context], &project, &None, cx))
3423            .await;
3424
3425        // Insert user message with context
3426        let message_id = thread.update(cx, |thread, cx| {
3427            thread.insert_user_message(
3428                "Please explain this code",
3429                loaded_context,
3430                None,
3431                Vec::new(),
3432                cx,
3433            )
3434        });
3435
3436        // Check content and context in message object
3437        let message = thread.read_with(cx, |thread, _| thread.message(message_id).unwrap().clone());
3438
3439        // Use different path format strings based on platform for the test
3440        #[cfg(windows)]
3441        let path_part = r"test\code.rs";
3442        #[cfg(not(windows))]
3443        let path_part = "test/code.rs";
3444
3445        let expected_context = format!(
3446            r#"
3447<context>
3448The following items were attached by the user. They are up-to-date and don't need to be re-read.
3449
3450<files>
3451```rs {path_part}
3452fn main() {{
3453    println!("Hello, world!");
3454}}
3455```
3456</files>
3457</context>
3458"#
3459        );
3460
3461        assert_eq!(message.role, Role::User);
3462        assert_eq!(message.segments.len(), 1);
3463        assert_eq!(
3464            message.segments[0],
3465            MessageSegment::Text("Please explain this code".to_string())
3466        );
3467        assert_eq!(message.loaded_context.text, expected_context);
3468
3469        // Check message in request
3470        let request = thread.update(cx, |thread, cx| {
3471            thread.to_completion_request(model.clone(), CompletionIntent::UserPrompt, cx)
3472        });
3473
3474        assert_eq!(request.messages.len(), 2);
3475        let expected_full_message = format!("{}Please explain this code", expected_context);
3476        assert_eq!(request.messages[1].string_contents(), expected_full_message);
3477    }
3478
3479    #[gpui::test]
3480    async fn test_only_include_new_contexts(cx: &mut TestAppContext) {
3481        init_test_settings(cx);
3482
3483        let project = create_test_project(
3484            cx,
3485            json!({
3486                "file1.rs": "fn function1() {}\n",
3487                "file2.rs": "fn function2() {}\n",
3488                "file3.rs": "fn function3() {}\n",
3489                "file4.rs": "fn function4() {}\n",
3490            }),
3491        )
3492        .await;
3493
3494        let (_, _thread_store, thread, context_store, model) =
3495            setup_test_environment(cx, project.clone()).await;
3496
3497        // First message with context 1
3498        add_file_to_context(&project, &context_store, "test/file1.rs", cx)
3499            .await
3500            .unwrap();
3501        let new_contexts = context_store.update(cx, |store, cx| {
3502            store.new_context_for_thread(thread.read(cx), None)
3503        });
3504        assert_eq!(new_contexts.len(), 1);
3505        let loaded_context = cx
3506            .update(|cx| load_context(new_contexts, &project, &None, cx))
3507            .await;
3508        let message1_id = thread.update(cx, |thread, cx| {
3509            thread.insert_user_message("Message 1", loaded_context, None, Vec::new(), cx)
3510        });
3511
3512        // Second message with contexts 1 and 2 (context 1 should be skipped as it's already included)
3513        add_file_to_context(&project, &context_store, "test/file2.rs", cx)
3514            .await
3515            .unwrap();
3516        let new_contexts = context_store.update(cx, |store, cx| {
3517            store.new_context_for_thread(thread.read(cx), None)
3518        });
3519        assert_eq!(new_contexts.len(), 1);
3520        let loaded_context = cx
3521            .update(|cx| load_context(new_contexts, &project, &None, cx))
3522            .await;
3523        let message2_id = thread.update(cx, |thread, cx| {
3524            thread.insert_user_message("Message 2", loaded_context, None, Vec::new(), cx)
3525        });
3526
3527        // Third message with all three contexts (contexts 1 and 2 should be skipped)
3528        //
3529        add_file_to_context(&project, &context_store, "test/file3.rs", cx)
3530            .await
3531            .unwrap();
3532        let new_contexts = context_store.update(cx, |store, cx| {
3533            store.new_context_for_thread(thread.read(cx), None)
3534        });
3535        assert_eq!(new_contexts.len(), 1);
3536        let loaded_context = cx
3537            .update(|cx| load_context(new_contexts, &project, &None, cx))
3538            .await;
3539        let message3_id = thread.update(cx, |thread, cx| {
3540            thread.insert_user_message("Message 3", loaded_context, None, Vec::new(), cx)
3541        });
3542
3543        // Check what contexts are included in each message
3544        let (message1, message2, message3) = thread.read_with(cx, |thread, _| {
3545            (
3546                thread.message(message1_id).unwrap().clone(),
3547                thread.message(message2_id).unwrap().clone(),
3548                thread.message(message3_id).unwrap().clone(),
3549            )
3550        });
3551
3552        // First message should include context 1
3553        assert!(message1.loaded_context.text.contains("file1.rs"));
3554
3555        // Second message should include only context 2 (not 1)
3556        assert!(!message2.loaded_context.text.contains("file1.rs"));
3557        assert!(message2.loaded_context.text.contains("file2.rs"));
3558
3559        // Third message should include only context 3 (not 1 or 2)
3560        assert!(!message3.loaded_context.text.contains("file1.rs"));
3561        assert!(!message3.loaded_context.text.contains("file2.rs"));
3562        assert!(message3.loaded_context.text.contains("file3.rs"));
3563
3564        // Check entire request to make sure all contexts are properly included
3565        let request = thread.update(cx, |thread, cx| {
3566            thread.to_completion_request(model.clone(), CompletionIntent::UserPrompt, cx)
3567        });
3568
3569        // The request should contain all 3 messages
3570        assert_eq!(request.messages.len(), 4);
3571
3572        // Check that the contexts are properly formatted in each message
3573        assert!(request.messages[1].string_contents().contains("file1.rs"));
3574        assert!(!request.messages[1].string_contents().contains("file2.rs"));
3575        assert!(!request.messages[1].string_contents().contains("file3.rs"));
3576
3577        assert!(!request.messages[2].string_contents().contains("file1.rs"));
3578        assert!(request.messages[2].string_contents().contains("file2.rs"));
3579        assert!(!request.messages[2].string_contents().contains("file3.rs"));
3580
3581        assert!(!request.messages[3].string_contents().contains("file1.rs"));
3582        assert!(!request.messages[3].string_contents().contains("file2.rs"));
3583        assert!(request.messages[3].string_contents().contains("file3.rs"));
3584
3585        add_file_to_context(&project, &context_store, "test/file4.rs", cx)
3586            .await
3587            .unwrap();
3588        let new_contexts = context_store.update(cx, |store, cx| {
3589            store.new_context_for_thread(thread.read(cx), Some(message2_id))
3590        });
3591        assert_eq!(new_contexts.len(), 3);
3592        let loaded_context = cx
3593            .update(|cx| load_context(new_contexts, &project, &None, cx))
3594            .await
3595            .loaded_context;
3596
3597        assert!(!loaded_context.text.contains("file1.rs"));
3598        assert!(loaded_context.text.contains("file2.rs"));
3599        assert!(loaded_context.text.contains("file3.rs"));
3600        assert!(loaded_context.text.contains("file4.rs"));
3601
3602        let new_contexts = context_store.update(cx, |store, cx| {
3603            // Remove file4.rs
3604            store.remove_context(&loaded_context.contexts[2].handle(), cx);
3605            store.new_context_for_thread(thread.read(cx), Some(message2_id))
3606        });
3607        assert_eq!(new_contexts.len(), 2);
3608        let loaded_context = cx
3609            .update(|cx| load_context(new_contexts, &project, &None, cx))
3610            .await
3611            .loaded_context;
3612
3613        assert!(!loaded_context.text.contains("file1.rs"));
3614        assert!(loaded_context.text.contains("file2.rs"));
3615        assert!(loaded_context.text.contains("file3.rs"));
3616        assert!(!loaded_context.text.contains("file4.rs"));
3617
3618        let new_contexts = context_store.update(cx, |store, cx| {
3619            // Remove file3.rs
3620            store.remove_context(&loaded_context.contexts[1].handle(), cx);
3621            store.new_context_for_thread(thread.read(cx), Some(message2_id))
3622        });
3623        assert_eq!(new_contexts.len(), 1);
3624        let loaded_context = cx
3625            .update(|cx| load_context(new_contexts, &project, &None, cx))
3626            .await
3627            .loaded_context;
3628
3629        assert!(!loaded_context.text.contains("file1.rs"));
3630        assert!(loaded_context.text.contains("file2.rs"));
3631        assert!(!loaded_context.text.contains("file3.rs"));
3632        assert!(!loaded_context.text.contains("file4.rs"));
3633    }
3634
3635    #[gpui::test]
3636    async fn test_message_without_files(cx: &mut TestAppContext) {
3637        init_test_settings(cx);
3638
3639        let project = create_test_project(
3640            cx,
3641            json!({"code.rs": "fn main() {\n    println!(\"Hello, world!\");\n}"}),
3642        )
3643        .await;
3644
3645        let (_, _thread_store, thread, _context_store, model) =
3646            setup_test_environment(cx, project.clone()).await;
3647
3648        // Insert user message without any context (empty context vector)
3649        let message_id = thread.update(cx, |thread, cx| {
3650            thread.insert_user_message(
3651                "What is the best way to learn Rust?",
3652                ContextLoadResult::default(),
3653                None,
3654                Vec::new(),
3655                cx,
3656            )
3657        });
3658
3659        // Check content and context in message object
3660        let message = thread.read_with(cx, |thread, _| thread.message(message_id).unwrap().clone());
3661
3662        // Context should be empty when no files are included
3663        assert_eq!(message.role, Role::User);
3664        assert_eq!(message.segments.len(), 1);
3665        assert_eq!(
3666            message.segments[0],
3667            MessageSegment::Text("What is the best way to learn Rust?".to_string())
3668        );
3669        assert_eq!(message.loaded_context.text, "");
3670
3671        // Check message in request
3672        let request = thread.update(cx, |thread, cx| {
3673            thread.to_completion_request(model.clone(), CompletionIntent::UserPrompt, cx)
3674        });
3675
3676        assert_eq!(request.messages.len(), 2);
3677        assert_eq!(
3678            request.messages[1].string_contents(),
3679            "What is the best way to learn Rust?"
3680        );
3681
3682        // Add second message, also without context
3683        let message2_id = thread.update(cx, |thread, cx| {
3684            thread.insert_user_message(
3685                "Are there any good books?",
3686                ContextLoadResult::default(),
3687                None,
3688                Vec::new(),
3689                cx,
3690            )
3691        });
3692
3693        let message2 =
3694            thread.read_with(cx, |thread, _| thread.message(message2_id).unwrap().clone());
3695        assert_eq!(message2.loaded_context.text, "");
3696
3697        // Check that both messages appear in the request
3698        let request = thread.update(cx, |thread, cx| {
3699            thread.to_completion_request(model.clone(), CompletionIntent::UserPrompt, cx)
3700        });
3701
3702        assert_eq!(request.messages.len(), 3);
3703        assert_eq!(
3704            request.messages[1].string_contents(),
3705            "What is the best way to learn Rust?"
3706        );
3707        assert_eq!(
3708            request.messages[2].string_contents(),
3709            "Are there any good books?"
3710        );
3711    }
3712
3713    #[gpui::test]
3714    #[ignore] // turn this test on when project_notifications tool is re-enabled
3715    async fn test_stale_buffer_notification(cx: &mut TestAppContext) {
3716        init_test_settings(cx);
3717
3718        let project = create_test_project(
3719            cx,
3720            json!({"code.rs": "fn main() {\n    println!(\"Hello, world!\");\n}"}),
3721        )
3722        .await;
3723
3724        let (_workspace, _thread_store, thread, context_store, model) =
3725            setup_test_environment(cx, project.clone()).await;
3726
3727        // Add a buffer to the context. This will be a tracked buffer
3728        let buffer = add_file_to_context(&project, &context_store, "test/code.rs", cx)
3729            .await
3730            .unwrap();
3731
3732        let context = context_store
3733            .read_with(cx, |store, _| store.context().next().cloned())
3734            .unwrap();
3735        let loaded_context = cx
3736            .update(|cx| load_context(vec![context], &project, &None, cx))
3737            .await;
3738
3739        // Insert user message and assistant response
3740        thread.update(cx, |thread, cx| {
3741            thread.insert_user_message("Explain this code", loaded_context, None, Vec::new(), cx);
3742            thread.insert_assistant_message(
3743                vec![MessageSegment::Text("This code prints 42.".into())],
3744                cx,
3745            );
3746        });
3747        cx.run_until_parked();
3748
3749        // We shouldn't have a stale buffer notification yet
3750        let notifications = thread.read_with(cx, |thread, _| {
3751            find_tool_uses(thread, "project_notifications")
3752        });
3753        assert!(
3754            notifications.is_empty(),
3755            "Should not have stale buffer notification before buffer is modified"
3756        );
3757
3758        // Modify the buffer
3759        buffer.update(cx, |buffer, cx| {
3760            buffer.edit(
3761                [(1..1, "\n    println!(\"Added a new line\");\n")],
3762                None,
3763                cx,
3764            );
3765        });
3766
3767        // Insert another user message
3768        thread.update(cx, |thread, cx| {
3769            thread.insert_user_message(
3770                "What does the code do now?",
3771                ContextLoadResult::default(),
3772                None,
3773                Vec::new(),
3774                cx,
3775            )
3776        });
3777        cx.run_until_parked();
3778
3779        // Check for the stale buffer warning
3780        thread.update(cx, |thread, cx| {
3781            thread.flush_notifications(model.clone(), CompletionIntent::UserPrompt, cx)
3782        });
3783        cx.run_until_parked();
3784
3785        let notifications = thread.read_with(cx, |thread, _cx| {
3786            find_tool_uses(thread, "project_notifications")
3787        });
3788
3789        let [notification] = notifications.as_slice() else {
3790            panic!("Should have a `project_notifications` tool use");
3791        };
3792
3793        let Some(notification_content) = notification.content.to_str() else {
3794            panic!("`project_notifications` should return text");
3795        };
3796
3797        assert!(notification_content.contains("These files have changed since the last read:"));
3798        assert!(notification_content.contains("code.rs"));
3799
3800        // Insert another user message and flush notifications again
3801        thread.update(cx, |thread, cx| {
3802            thread.insert_user_message(
3803                "Can you tell me more?",
3804                ContextLoadResult::default(),
3805                None,
3806                Vec::new(),
3807                cx,
3808            )
3809        });
3810
3811        thread.update(cx, |thread, cx| {
3812            thread.flush_notifications(model.clone(), CompletionIntent::UserPrompt, cx)
3813        });
3814        cx.run_until_parked();
3815
3816        // There should be no new notifications (we already flushed one)
3817        let notifications = thread.read_with(cx, |thread, _cx| {
3818            find_tool_uses(thread, "project_notifications")
3819        });
3820
3821        assert_eq!(
3822            notifications.len(),
3823            1,
3824            "Should still have only one notification after second flush - no duplicates"
3825        );
3826    }
3827
3828    fn find_tool_uses(thread: &Thread, tool_name: &str) -> Vec<LanguageModelToolResult> {
3829        thread
3830            .messages()
3831            .flat_map(|message| {
3832                thread
3833                    .tool_results_for_message(message.id)
3834                    .into_iter()
3835                    .filter(|result| result.tool_name == tool_name.into())
3836                    .cloned()
3837                    .collect::<Vec<_>>()
3838            })
3839            .collect()
3840    }
3841
3842    #[gpui::test]
3843    async fn test_storing_profile_setting_per_thread(cx: &mut TestAppContext) {
3844        init_test_settings(cx);
3845
3846        let project = create_test_project(
3847            cx,
3848            json!({"code.rs": "fn main() {\n    println!(\"Hello, world!\");\n}"}),
3849        )
3850        .await;
3851
3852        let (_workspace, thread_store, thread, _context_store, _model) =
3853            setup_test_environment(cx, project.clone()).await;
3854
3855        // Check that we are starting with the default profile
3856        let profile = cx.read(|cx| thread.read(cx).profile.clone());
3857        let tool_set = cx.read(|cx| thread_store.read(cx).tools());
3858        assert_eq!(
3859            profile,
3860            AgentProfile::new(AgentProfileId::default(), tool_set)
3861        );
3862    }
3863
3864    #[gpui::test]
3865    async fn test_serializing_thread_profile(cx: &mut TestAppContext) {
3866        init_test_settings(cx);
3867
3868        let project = create_test_project(
3869            cx,
3870            json!({"code.rs": "fn main() {\n    println!(\"Hello, world!\");\n}"}),
3871        )
3872        .await;
3873
3874        let (_workspace, thread_store, thread, _context_store, _model) =
3875            setup_test_environment(cx, project.clone()).await;
3876
3877        // Profile gets serialized with default values
3878        let serialized = thread
3879            .update(cx, |thread, cx| thread.serialize(cx))
3880            .await
3881            .unwrap();
3882
3883        assert_eq!(serialized.profile, Some(AgentProfileId::default()));
3884
3885        let deserialized = cx.update(|cx| {
3886            thread.update(cx, |thread, cx| {
3887                Thread::deserialize(
3888                    thread.id.clone(),
3889                    serialized,
3890                    thread.project.clone(),
3891                    thread.tools.clone(),
3892                    thread.prompt_builder.clone(),
3893                    thread.project_context.clone(),
3894                    None,
3895                    cx,
3896                )
3897            })
3898        });
3899        let tool_set = cx.read(|cx| thread_store.read(cx).tools());
3900
3901        assert_eq!(
3902            deserialized.profile,
3903            AgentProfile::new(AgentProfileId::default(), tool_set)
3904        );
3905    }
3906
3907    #[gpui::test]
3908    async fn test_temperature_setting(cx: &mut TestAppContext) {
3909        init_test_settings(cx);
3910
3911        let project = create_test_project(
3912            cx,
3913            json!({"code.rs": "fn main() {\n    println!(\"Hello, world!\");\n}"}),
3914        )
3915        .await;
3916
3917        let (_workspace, _thread_store, thread, _context_store, model) =
3918            setup_test_environment(cx, project.clone()).await;
3919
3920        // Both model and provider
3921        cx.update(|cx| {
3922            AgentSettings::override_global(
3923                AgentSettings {
3924                    model_parameters: vec![LanguageModelParameters {
3925                        provider: Some(model.provider_id().0.to_string().into()),
3926                        model: Some(model.id().0.clone()),
3927                        temperature: Some(0.66),
3928                    }],
3929                    ..AgentSettings::get_global(cx).clone()
3930                },
3931                cx,
3932            );
3933        });
3934
3935        let request = thread.update(cx, |thread, cx| {
3936            thread.to_completion_request(model.clone(), CompletionIntent::UserPrompt, cx)
3937        });
3938        assert_eq!(request.temperature, Some(0.66));
3939
3940        // Only model
3941        cx.update(|cx| {
3942            AgentSettings::override_global(
3943                AgentSettings {
3944                    model_parameters: vec![LanguageModelParameters {
3945                        provider: None,
3946                        model: Some(model.id().0.clone()),
3947                        temperature: Some(0.66),
3948                    }],
3949                    ..AgentSettings::get_global(cx).clone()
3950                },
3951                cx,
3952            );
3953        });
3954
3955        let request = thread.update(cx, |thread, cx| {
3956            thread.to_completion_request(model.clone(), CompletionIntent::UserPrompt, cx)
3957        });
3958        assert_eq!(request.temperature, Some(0.66));
3959
3960        // Only provider
3961        cx.update(|cx| {
3962            AgentSettings::override_global(
3963                AgentSettings {
3964                    model_parameters: vec![LanguageModelParameters {
3965                        provider: Some(model.provider_id().0.to_string().into()),
3966                        model: None,
3967                        temperature: Some(0.66),
3968                    }],
3969                    ..AgentSettings::get_global(cx).clone()
3970                },
3971                cx,
3972            );
3973        });
3974
3975        let request = thread.update(cx, |thread, cx| {
3976            thread.to_completion_request(model.clone(), CompletionIntent::UserPrompt, cx)
3977        });
3978        assert_eq!(request.temperature, Some(0.66));
3979
3980        // Same model name, different provider
3981        cx.update(|cx| {
3982            AgentSettings::override_global(
3983                AgentSettings {
3984                    model_parameters: vec![LanguageModelParameters {
3985                        provider: Some("anthropic".into()),
3986                        model: Some(model.id().0.clone()),
3987                        temperature: Some(0.66),
3988                    }],
3989                    ..AgentSettings::get_global(cx).clone()
3990                },
3991                cx,
3992            );
3993        });
3994
3995        let request = thread.update(cx, |thread, cx| {
3996            thread.to_completion_request(model.clone(), CompletionIntent::UserPrompt, cx)
3997        });
3998        assert_eq!(request.temperature, None);
3999    }
4000
4001    #[gpui::test]
4002    async fn test_thread_summary(cx: &mut TestAppContext) {
4003        init_test_settings(cx);
4004
4005        let project = create_test_project(cx, json!({})).await;
4006
4007        let (_, _thread_store, thread, _context_store, model) =
4008            setup_test_environment(cx, project.clone()).await;
4009
4010        // Initial state should be pending
4011        thread.read_with(cx, |thread, _| {
4012            assert!(matches!(thread.summary(), ThreadSummary::Pending));
4013            assert_eq!(thread.summary().or_default(), ThreadSummary::DEFAULT);
4014        });
4015
4016        // Manually setting the summary should not be allowed in this state
4017        thread.update(cx, |thread, cx| {
4018            thread.set_summary("This should not work", cx);
4019        });
4020
4021        thread.read_with(cx, |thread, _| {
4022            assert!(matches!(thread.summary(), ThreadSummary::Pending));
4023        });
4024
4025        // Send a message
4026        thread.update(cx, |thread, cx| {
4027            thread.insert_user_message("Hi!", ContextLoadResult::default(), None, vec![], cx);
4028            thread.send_to_model(
4029                model.clone(),
4030                CompletionIntent::ThreadSummarization,
4031                None,
4032                cx,
4033            );
4034        });
4035
4036        let fake_model = model.as_fake();
4037        simulate_successful_response(&fake_model, cx);
4038
4039        // Should start generating summary when there are >= 2 messages
4040        thread.read_with(cx, |thread, _| {
4041            assert_eq!(*thread.summary(), ThreadSummary::Generating);
4042        });
4043
4044        // Should not be able to set the summary while generating
4045        thread.update(cx, |thread, cx| {
4046            thread.set_summary("This should not work either", cx);
4047        });
4048
4049        thread.read_with(cx, |thread, _| {
4050            assert!(matches!(thread.summary(), ThreadSummary::Generating));
4051            assert_eq!(thread.summary().or_default(), ThreadSummary::DEFAULT);
4052        });
4053
4054        cx.run_until_parked();
4055        fake_model.send_last_completion_stream_text_chunk("Brief");
4056        fake_model.send_last_completion_stream_text_chunk(" Introduction");
4057        fake_model.end_last_completion_stream();
4058        cx.run_until_parked();
4059
4060        // Summary should be set
4061        thread.read_with(cx, |thread, _| {
4062            assert!(matches!(thread.summary(), ThreadSummary::Ready(_)));
4063            assert_eq!(thread.summary().or_default(), "Brief Introduction");
4064        });
4065
4066        // Now we should be able to set a summary
4067        thread.update(cx, |thread, cx| {
4068            thread.set_summary("Brief Intro", cx);
4069        });
4070
4071        thread.read_with(cx, |thread, _| {
4072            assert_eq!(thread.summary().or_default(), "Brief Intro");
4073        });
4074
4075        // Test setting an empty summary (should default to DEFAULT)
4076        thread.update(cx, |thread, cx| {
4077            thread.set_summary("", cx);
4078        });
4079
4080        thread.read_with(cx, |thread, _| {
4081            assert!(matches!(thread.summary(), ThreadSummary::Ready(_)));
4082            assert_eq!(thread.summary().or_default(), ThreadSummary::DEFAULT);
4083        });
4084    }
4085
4086    #[gpui::test]
4087    async fn test_thread_summary_error_set_manually(cx: &mut TestAppContext) {
4088        init_test_settings(cx);
4089
4090        let project = create_test_project(cx, json!({})).await;
4091
4092        let (_, _thread_store, thread, _context_store, model) =
4093            setup_test_environment(cx, project.clone()).await;
4094
4095        test_summarize_error(&model, &thread, cx);
4096
4097        // Now we should be able to set a summary
4098        thread.update(cx, |thread, cx| {
4099            thread.set_summary("Brief Intro", cx);
4100        });
4101
4102        thread.read_with(cx, |thread, _| {
4103            assert!(matches!(thread.summary(), ThreadSummary::Ready(_)));
4104            assert_eq!(thread.summary().or_default(), "Brief Intro");
4105        });
4106    }
4107
4108    #[gpui::test]
4109    async fn test_thread_summary_error_retry(cx: &mut TestAppContext) {
4110        init_test_settings(cx);
4111
4112        let project = create_test_project(cx, json!({})).await;
4113
4114        let (_, _thread_store, thread, _context_store, model) =
4115            setup_test_environment(cx, project.clone()).await;
4116
4117        test_summarize_error(&model, &thread, cx);
4118
4119        // Sending another message should not trigger another summarize request
4120        thread.update(cx, |thread, cx| {
4121            thread.insert_user_message(
4122                "How are you?",
4123                ContextLoadResult::default(),
4124                None,
4125                vec![],
4126                cx,
4127            );
4128            thread.send_to_model(model.clone(), CompletionIntent::UserPrompt, None, cx);
4129        });
4130
4131        let fake_model = model.as_fake();
4132        simulate_successful_response(&fake_model, cx);
4133
4134        thread.read_with(cx, |thread, _| {
4135            // State is still Error, not Generating
4136            assert!(matches!(thread.summary(), ThreadSummary::Error));
4137        });
4138
4139        // But the summarize request can be invoked manually
4140        thread.update(cx, |thread, cx| {
4141            thread.summarize(cx);
4142        });
4143
4144        thread.read_with(cx, |thread, _| {
4145            assert!(matches!(thread.summary(), ThreadSummary::Generating));
4146        });
4147
4148        cx.run_until_parked();
4149        fake_model.send_last_completion_stream_text_chunk("A successful summary");
4150        fake_model.end_last_completion_stream();
4151        cx.run_until_parked();
4152
4153        thread.read_with(cx, |thread, _| {
4154            assert!(matches!(thread.summary(), ThreadSummary::Ready(_)));
4155            assert_eq!(thread.summary().or_default(), "A successful summary");
4156        });
4157    }
4158
4159    // Helper to create a model that returns errors
4160    enum TestError {
4161        Overloaded,
4162        InternalServerError,
4163    }
4164
4165    struct ErrorInjector {
4166        inner: Arc<FakeLanguageModel>,
4167        error_type: TestError,
4168    }
4169
4170    impl ErrorInjector {
4171        fn new(error_type: TestError) -> Self {
4172            Self {
4173                inner: Arc::new(FakeLanguageModel::default()),
4174                error_type,
4175            }
4176        }
4177    }
4178
4179    impl LanguageModel for ErrorInjector {
4180        fn id(&self) -> LanguageModelId {
4181            self.inner.id()
4182        }
4183
4184        fn name(&self) -> LanguageModelName {
4185            self.inner.name()
4186        }
4187
4188        fn provider_id(&self) -> LanguageModelProviderId {
4189            self.inner.provider_id()
4190        }
4191
4192        fn provider_name(&self) -> LanguageModelProviderName {
4193            self.inner.provider_name()
4194        }
4195
4196        fn supports_tools(&self) -> bool {
4197            self.inner.supports_tools()
4198        }
4199
4200        fn supports_tool_choice(&self, choice: LanguageModelToolChoice) -> bool {
4201            self.inner.supports_tool_choice(choice)
4202        }
4203
4204        fn supports_images(&self) -> bool {
4205            self.inner.supports_images()
4206        }
4207
4208        fn telemetry_id(&self) -> String {
4209            self.inner.telemetry_id()
4210        }
4211
4212        fn max_token_count(&self) -> u64 {
4213            self.inner.max_token_count()
4214        }
4215
4216        fn count_tokens(
4217            &self,
4218            request: LanguageModelRequest,
4219            cx: &App,
4220        ) -> BoxFuture<'static, Result<u64>> {
4221            self.inner.count_tokens(request, cx)
4222        }
4223
4224        fn stream_completion(
4225            &self,
4226            _request: LanguageModelRequest,
4227            _cx: &AsyncApp,
4228        ) -> BoxFuture<
4229            'static,
4230            Result<
4231                BoxStream<
4232                    'static,
4233                    Result<LanguageModelCompletionEvent, LanguageModelCompletionError>,
4234                >,
4235                LanguageModelCompletionError,
4236            >,
4237        > {
4238            let error = match self.error_type {
4239                TestError::Overloaded => LanguageModelCompletionError::ServerOverloaded {
4240                    provider: self.provider_name(),
4241                    retry_after: None,
4242                },
4243                TestError::InternalServerError => {
4244                    LanguageModelCompletionError::ApiInternalServerError {
4245                        provider: self.provider_name(),
4246                        message: "I'm a teapot orbiting the sun".to_string(),
4247                    }
4248                }
4249            };
4250            async move {
4251                let stream = futures::stream::once(async move { Err(error) });
4252                Ok(stream.boxed())
4253            }
4254            .boxed()
4255        }
4256
4257        fn as_fake(&self) -> &FakeLanguageModel {
4258            &self.inner
4259        }
4260    }
4261
4262    #[gpui::test]
4263    async fn test_retry_on_overloaded_error(cx: &mut TestAppContext) {
4264        init_test_settings(cx);
4265
4266        let project = create_test_project(cx, json!({})).await;
4267        let (_, _, thread, _, _base_model) = setup_test_environment(cx, project.clone()).await;
4268
4269        // Enable Burn Mode to allow retries
4270        thread.update(cx, |thread, _| {
4271            thread.set_completion_mode(CompletionMode::Burn);
4272        });
4273
4274        // Create model that returns overloaded error
4275        let model = Arc::new(ErrorInjector::new(TestError::Overloaded));
4276
4277        // Insert a user message
4278        thread.update(cx, |thread, cx| {
4279            thread.insert_user_message("Hello!", ContextLoadResult::default(), None, vec![], cx);
4280        });
4281
4282        // Start completion
4283        thread.update(cx, |thread, cx| {
4284            thread.send_to_model(model.clone(), CompletionIntent::UserPrompt, None, cx);
4285        });
4286
4287        cx.run_until_parked();
4288
4289        thread.read_with(cx, |thread, _| {
4290            assert!(thread.retry_state.is_some(), "Should have retry state");
4291            let retry_state = thread.retry_state.as_ref().unwrap();
4292            assert_eq!(retry_state.attempt, 1, "Should be first retry attempt");
4293            assert_eq!(
4294                retry_state.max_attempts, MAX_RETRY_ATTEMPTS,
4295                "Should retry MAX_RETRY_ATTEMPTS times for overloaded errors"
4296            );
4297        });
4298
4299        // Check that a retry message was added
4300        thread.read_with(cx, |thread, _| {
4301            let mut messages = thread.messages();
4302            assert!(
4303                messages.any(|msg| {
4304                    msg.role == Role::System
4305                        && msg.ui_only
4306                        && msg.segments.iter().any(|seg| {
4307                            if let MessageSegment::Text(text) = seg {
4308                                text.contains("overloaded")
4309                                    && text
4310                                        .contains(&format!("attempt 1 of {}", MAX_RETRY_ATTEMPTS))
4311                            } else {
4312                                false
4313                            }
4314                        })
4315                }),
4316                "Should have added a system retry message"
4317            );
4318        });
4319
4320        let retry_count = thread.update(cx, |thread, _| {
4321            thread
4322                .messages
4323                .iter()
4324                .filter(|m| {
4325                    m.ui_only
4326                        && m.segments.iter().any(|s| {
4327                            if let MessageSegment::Text(text) = s {
4328                                text.contains("Retrying") && text.contains("seconds")
4329                            } else {
4330                                false
4331                            }
4332                        })
4333                })
4334                .count()
4335        });
4336
4337        assert_eq!(retry_count, 1, "Should have one retry message");
4338    }
4339
4340    #[gpui::test]
4341    async fn test_retry_on_internal_server_error(cx: &mut TestAppContext) {
4342        init_test_settings(cx);
4343
4344        let project = create_test_project(cx, json!({})).await;
4345        let (_, _, thread, _, _base_model) = setup_test_environment(cx, project.clone()).await;
4346
4347        // Enable Burn Mode to allow retries
4348        thread.update(cx, |thread, _| {
4349            thread.set_completion_mode(CompletionMode::Burn);
4350        });
4351
4352        // Create model that returns internal server error
4353        let model = Arc::new(ErrorInjector::new(TestError::InternalServerError));
4354
4355        // Insert a user message
4356        thread.update(cx, |thread, cx| {
4357            thread.insert_user_message("Hello!", ContextLoadResult::default(), None, vec![], cx);
4358        });
4359
4360        // Start completion
4361        thread.update(cx, |thread, cx| {
4362            thread.send_to_model(model.clone(), CompletionIntent::UserPrompt, None, cx);
4363        });
4364
4365        cx.run_until_parked();
4366
4367        // Check retry state on thread
4368        thread.read_with(cx, |thread, _| {
4369            assert!(thread.retry_state.is_some(), "Should have retry state");
4370            let retry_state = thread.retry_state.as_ref().unwrap();
4371            assert_eq!(retry_state.attempt, 1, "Should be first retry attempt");
4372            assert_eq!(
4373                retry_state.max_attempts, 3,
4374                "Should have correct max attempts"
4375            );
4376        });
4377
4378        // Check that a retry message was added with provider name
4379        thread.read_with(cx, |thread, _| {
4380            let mut messages = thread.messages();
4381            assert!(
4382                messages.any(|msg| {
4383                    msg.role == Role::System
4384                        && msg.ui_only
4385                        && msg.segments.iter().any(|seg| {
4386                            if let MessageSegment::Text(text) = seg {
4387                                text.contains("internal")
4388                                    && text.contains("Fake")
4389                                    && text.contains("Retrying")
4390                                    && text.contains("attempt 1 of 3")
4391                                    && text.contains("seconds")
4392                            } else {
4393                                false
4394                            }
4395                        })
4396                }),
4397                "Should have added a system retry message with provider name"
4398            );
4399        });
4400
4401        // Count retry messages
4402        let retry_count = thread.update(cx, |thread, _| {
4403            thread
4404                .messages
4405                .iter()
4406                .filter(|m| {
4407                    m.ui_only
4408                        && m.segments.iter().any(|s| {
4409                            if let MessageSegment::Text(text) = s {
4410                                text.contains("Retrying") && text.contains("seconds")
4411                            } else {
4412                                false
4413                            }
4414                        })
4415                })
4416                .count()
4417        });
4418
4419        assert_eq!(retry_count, 1, "Should have one retry message");
4420    }
4421
4422    #[gpui::test]
4423    async fn test_exponential_backoff_on_retries(cx: &mut TestAppContext) {
4424        init_test_settings(cx);
4425
4426        let project = create_test_project(cx, json!({})).await;
4427        let (_, _, thread, _, _base_model) = setup_test_environment(cx, project.clone()).await;
4428
4429        // Enable Burn Mode to allow retries
4430        thread.update(cx, |thread, _| {
4431            thread.set_completion_mode(CompletionMode::Burn);
4432        });
4433
4434        // Create model that returns internal server error
4435        let model = Arc::new(ErrorInjector::new(TestError::InternalServerError));
4436
4437        // Insert a user message
4438        thread.update(cx, |thread, cx| {
4439            thread.insert_user_message("Hello!", ContextLoadResult::default(), None, vec![], cx);
4440        });
4441
4442        // Track retry events and completion count
4443        // Track completion events
4444        let completion_count = Arc::new(Mutex::new(0));
4445        let completion_count_clone = completion_count.clone();
4446
4447        let _subscription = thread.update(cx, |_, cx| {
4448            cx.subscribe(&thread, move |_, _, event: &ThreadEvent, _| {
4449                if let ThreadEvent::NewRequest = event {
4450                    *completion_count_clone.lock() += 1;
4451                }
4452            })
4453        });
4454
4455        // First attempt
4456        thread.update(cx, |thread, cx| {
4457            thread.send_to_model(model.clone(), CompletionIntent::UserPrompt, None, cx);
4458        });
4459        cx.run_until_parked();
4460
4461        // Should have scheduled first retry - count retry messages
4462        let retry_count = thread.update(cx, |thread, _| {
4463            thread
4464                .messages
4465                .iter()
4466                .filter(|m| {
4467                    m.ui_only
4468                        && m.segments.iter().any(|s| {
4469                            if let MessageSegment::Text(text) = s {
4470                                text.contains("Retrying") && text.contains("seconds")
4471                            } else {
4472                                false
4473                            }
4474                        })
4475                })
4476                .count()
4477        });
4478        assert_eq!(retry_count, 1, "Should have scheduled first retry");
4479
4480        // Check retry state
4481        thread.read_with(cx, |thread, _| {
4482            assert!(thread.retry_state.is_some(), "Should have retry state");
4483            let retry_state = thread.retry_state.as_ref().unwrap();
4484            assert_eq!(retry_state.attempt, 1, "Should be first retry attempt");
4485            assert_eq!(
4486                retry_state.max_attempts, 3,
4487                "Internal server errors should retry up to 3 times"
4488            );
4489        });
4490
4491        // Advance clock for first retry
4492        cx.executor().advance_clock(BASE_RETRY_DELAY);
4493        cx.run_until_parked();
4494
4495        // Advance clock for second retry
4496        cx.executor().advance_clock(BASE_RETRY_DELAY);
4497        cx.run_until_parked();
4498
4499        // Advance clock for third retry
4500        cx.executor().advance_clock(BASE_RETRY_DELAY);
4501        cx.run_until_parked();
4502
4503        // Should have completed all retries - count retry messages
4504        let retry_count = thread.update(cx, |thread, _| {
4505            thread
4506                .messages
4507                .iter()
4508                .filter(|m| {
4509                    m.ui_only
4510                        && m.segments.iter().any(|s| {
4511                            if let MessageSegment::Text(text) = s {
4512                                text.contains("Retrying") && text.contains("seconds")
4513                            } else {
4514                                false
4515                            }
4516                        })
4517                })
4518                .count()
4519        });
4520        assert_eq!(
4521            retry_count, 3,
4522            "Should have 3 retries for internal server errors"
4523        );
4524
4525        // For internal server errors, we retry 3 times and then give up
4526        // Check that retry_state is cleared after all retries
4527        thread.read_with(cx, |thread, _| {
4528            assert!(
4529                thread.retry_state.is_none(),
4530                "Retry state should be cleared after all retries"
4531            );
4532        });
4533
4534        // Verify total attempts (1 initial + 3 retries)
4535        assert_eq!(
4536            *completion_count.lock(),
4537            4,
4538            "Should have attempted once plus 3 retries"
4539        );
4540    }
4541
4542    #[gpui::test]
4543    async fn test_max_retries_exceeded(cx: &mut TestAppContext) {
4544        init_test_settings(cx);
4545
4546        let project = create_test_project(cx, json!({})).await;
4547        let (_, _, thread, _, _base_model) = setup_test_environment(cx, project.clone()).await;
4548
4549        // Enable Burn Mode to allow retries
4550        thread.update(cx, |thread, _| {
4551            thread.set_completion_mode(CompletionMode::Burn);
4552        });
4553
4554        // Create model that returns overloaded error
4555        let model = Arc::new(ErrorInjector::new(TestError::Overloaded));
4556
4557        // Insert a user message
4558        thread.update(cx, |thread, cx| {
4559            thread.insert_user_message("Hello!", ContextLoadResult::default(), None, vec![], cx);
4560        });
4561
4562        // Track events
4563        let stopped_with_error = Arc::new(Mutex::new(false));
4564        let stopped_with_error_clone = stopped_with_error.clone();
4565
4566        let _subscription = thread.update(cx, |_, cx| {
4567            cx.subscribe(&thread, move |_, _, event: &ThreadEvent, _| {
4568                if let ThreadEvent::Stopped(Err(_)) = event {
4569                    *stopped_with_error_clone.lock() = true;
4570                }
4571            })
4572        });
4573
4574        // Start initial completion
4575        thread.update(cx, |thread, cx| {
4576            thread.send_to_model(model.clone(), CompletionIntent::UserPrompt, None, cx);
4577        });
4578        cx.run_until_parked();
4579
4580        // Advance through all retries
4581        for _ in 0..MAX_RETRY_ATTEMPTS {
4582            cx.executor().advance_clock(BASE_RETRY_DELAY);
4583            cx.run_until_parked();
4584        }
4585
4586        let retry_count = thread.update(cx, |thread, _| {
4587            thread
4588                .messages
4589                .iter()
4590                .filter(|m| {
4591                    m.ui_only
4592                        && m.segments.iter().any(|s| {
4593                            if let MessageSegment::Text(text) = s {
4594                                text.contains("Retrying") && text.contains("seconds")
4595                            } else {
4596                                false
4597                            }
4598                        })
4599                })
4600                .count()
4601        });
4602
4603        // After max retries, should emit Stopped(Err(...)) event
4604        assert_eq!(
4605            retry_count, MAX_RETRY_ATTEMPTS as usize,
4606            "Should have attempted MAX_RETRY_ATTEMPTS retries for overloaded errors"
4607        );
4608        assert!(
4609            *stopped_with_error.lock(),
4610            "Should emit Stopped(Err(...)) event after max retries exceeded"
4611        );
4612
4613        // Retry state should be cleared
4614        thread.read_with(cx, |thread, _| {
4615            assert!(
4616                thread.retry_state.is_none(),
4617                "Retry state should be cleared after max retries"
4618            );
4619
4620            // Verify we have the expected number of retry messages
4621            let retry_messages = thread
4622                .messages
4623                .iter()
4624                .filter(|msg| msg.ui_only && msg.role == Role::System)
4625                .count();
4626            assert_eq!(
4627                retry_messages, MAX_RETRY_ATTEMPTS as usize,
4628                "Should have MAX_RETRY_ATTEMPTS retry messages for overloaded errors"
4629            );
4630        });
4631    }
4632
4633    #[gpui::test]
4634    async fn test_retry_message_removed_on_retry(cx: &mut TestAppContext) {
4635        init_test_settings(cx);
4636
4637        let project = create_test_project(cx, json!({})).await;
4638        let (_, _, thread, _, _base_model) = setup_test_environment(cx, project.clone()).await;
4639
4640        // Enable Burn Mode to allow retries
4641        thread.update(cx, |thread, _| {
4642            thread.set_completion_mode(CompletionMode::Burn);
4643        });
4644
4645        // We'll use a wrapper to switch behavior after first failure
4646        struct RetryTestModel {
4647            inner: Arc<FakeLanguageModel>,
4648            failed_once: Arc<Mutex<bool>>,
4649        }
4650
4651        impl LanguageModel for RetryTestModel {
4652            fn id(&self) -> LanguageModelId {
4653                self.inner.id()
4654            }
4655
4656            fn name(&self) -> LanguageModelName {
4657                self.inner.name()
4658            }
4659
4660            fn provider_id(&self) -> LanguageModelProviderId {
4661                self.inner.provider_id()
4662            }
4663
4664            fn provider_name(&self) -> LanguageModelProviderName {
4665                self.inner.provider_name()
4666            }
4667
4668            fn supports_tools(&self) -> bool {
4669                self.inner.supports_tools()
4670            }
4671
4672            fn supports_tool_choice(&self, choice: LanguageModelToolChoice) -> bool {
4673                self.inner.supports_tool_choice(choice)
4674            }
4675
4676            fn supports_images(&self) -> bool {
4677                self.inner.supports_images()
4678            }
4679
4680            fn telemetry_id(&self) -> String {
4681                self.inner.telemetry_id()
4682            }
4683
4684            fn max_token_count(&self) -> u64 {
4685                self.inner.max_token_count()
4686            }
4687
4688            fn count_tokens(
4689                &self,
4690                request: LanguageModelRequest,
4691                cx: &App,
4692            ) -> BoxFuture<'static, Result<u64>> {
4693                self.inner.count_tokens(request, cx)
4694            }
4695
4696            fn stream_completion(
4697                &self,
4698                request: LanguageModelRequest,
4699                cx: &AsyncApp,
4700            ) -> BoxFuture<
4701                'static,
4702                Result<
4703                    BoxStream<
4704                        'static,
4705                        Result<LanguageModelCompletionEvent, LanguageModelCompletionError>,
4706                    >,
4707                    LanguageModelCompletionError,
4708                >,
4709            > {
4710                if !*self.failed_once.lock() {
4711                    *self.failed_once.lock() = true;
4712                    let provider = self.provider_name();
4713                    // Return error on first attempt
4714                    let stream = futures::stream::once(async move {
4715                        Err(LanguageModelCompletionError::ServerOverloaded {
4716                            provider,
4717                            retry_after: None,
4718                        })
4719                    });
4720                    async move { Ok(stream.boxed()) }.boxed()
4721                } else {
4722                    // Succeed on retry
4723                    self.inner.stream_completion(request, cx)
4724                }
4725            }
4726
4727            fn as_fake(&self) -> &FakeLanguageModel {
4728                &self.inner
4729            }
4730        }
4731
4732        let model = Arc::new(RetryTestModel {
4733            inner: Arc::new(FakeLanguageModel::default()),
4734            failed_once: Arc::new(Mutex::new(false)),
4735        });
4736
4737        // Insert a user message
4738        thread.update(cx, |thread, cx| {
4739            thread.insert_user_message("Hello!", ContextLoadResult::default(), None, vec![], cx);
4740        });
4741
4742        // Track message deletions
4743        // Track when retry completes successfully
4744        let retry_completed = Arc::new(Mutex::new(false));
4745        let retry_completed_clone = retry_completed.clone();
4746
4747        let _subscription = thread.update(cx, |_, cx| {
4748            cx.subscribe(&thread, move |_, _, event: &ThreadEvent, _| {
4749                if let ThreadEvent::StreamedCompletion = event {
4750                    *retry_completed_clone.lock() = true;
4751                }
4752            })
4753        });
4754
4755        // Start completion
4756        thread.update(cx, |thread, cx| {
4757            thread.send_to_model(model.clone(), CompletionIntent::UserPrompt, None, cx);
4758        });
4759        cx.run_until_parked();
4760
4761        // Get the retry message ID
4762        let retry_message_id = thread.read_with(cx, |thread, _| {
4763            thread
4764                .messages()
4765                .find(|msg| msg.role == Role::System && msg.ui_only)
4766                .map(|msg| msg.id)
4767                .expect("Should have a retry message")
4768        });
4769
4770        // Wait for retry
4771        cx.executor().advance_clock(BASE_RETRY_DELAY);
4772        cx.run_until_parked();
4773
4774        // Stream some successful content
4775        let fake_model = model.as_fake();
4776        // After the retry, there should be a new pending completion
4777        let pending = fake_model.pending_completions();
4778        assert!(
4779            !pending.is_empty(),
4780            "Should have a pending completion after retry"
4781        );
4782        fake_model.send_completion_stream_text_chunk(&pending[0], "Success!");
4783        fake_model.end_completion_stream(&pending[0]);
4784        cx.run_until_parked();
4785
4786        // Check that the retry completed successfully
4787        assert!(
4788            *retry_completed.lock(),
4789            "Retry should have completed successfully"
4790        );
4791
4792        // Retry message should still exist but be marked as ui_only
4793        thread.read_with(cx, |thread, _| {
4794            let retry_msg = thread
4795                .message(retry_message_id)
4796                .expect("Retry message should still exist");
4797            assert!(retry_msg.ui_only, "Retry message should be ui_only");
4798            assert_eq!(
4799                retry_msg.role,
4800                Role::System,
4801                "Retry message should have System role"
4802            );
4803        });
4804    }
4805
4806    #[gpui::test]
4807    async fn test_successful_completion_clears_retry_state(cx: &mut TestAppContext) {
4808        init_test_settings(cx);
4809
4810        let project = create_test_project(cx, json!({})).await;
4811        let (_, _, thread, _, _base_model) = setup_test_environment(cx, project.clone()).await;
4812
4813        // Enable Burn Mode to allow retries
4814        thread.update(cx, |thread, _| {
4815            thread.set_completion_mode(CompletionMode::Burn);
4816        });
4817
4818        // Create a model that fails once then succeeds
4819        struct FailOnceModel {
4820            inner: Arc<FakeLanguageModel>,
4821            failed_once: Arc<Mutex<bool>>,
4822        }
4823
4824        impl LanguageModel for FailOnceModel {
4825            fn id(&self) -> LanguageModelId {
4826                self.inner.id()
4827            }
4828
4829            fn name(&self) -> LanguageModelName {
4830                self.inner.name()
4831            }
4832
4833            fn provider_id(&self) -> LanguageModelProviderId {
4834                self.inner.provider_id()
4835            }
4836
4837            fn provider_name(&self) -> LanguageModelProviderName {
4838                self.inner.provider_name()
4839            }
4840
4841            fn supports_tools(&self) -> bool {
4842                self.inner.supports_tools()
4843            }
4844
4845            fn supports_tool_choice(&self, choice: LanguageModelToolChoice) -> bool {
4846                self.inner.supports_tool_choice(choice)
4847            }
4848
4849            fn supports_images(&self) -> bool {
4850                self.inner.supports_images()
4851            }
4852
4853            fn telemetry_id(&self) -> String {
4854                self.inner.telemetry_id()
4855            }
4856
4857            fn max_token_count(&self) -> u64 {
4858                self.inner.max_token_count()
4859            }
4860
4861            fn count_tokens(
4862                &self,
4863                request: LanguageModelRequest,
4864                cx: &App,
4865            ) -> BoxFuture<'static, Result<u64>> {
4866                self.inner.count_tokens(request, cx)
4867            }
4868
4869            fn stream_completion(
4870                &self,
4871                request: LanguageModelRequest,
4872                cx: &AsyncApp,
4873            ) -> BoxFuture<
4874                'static,
4875                Result<
4876                    BoxStream<
4877                        'static,
4878                        Result<LanguageModelCompletionEvent, LanguageModelCompletionError>,
4879                    >,
4880                    LanguageModelCompletionError,
4881                >,
4882            > {
4883                if !*self.failed_once.lock() {
4884                    *self.failed_once.lock() = true;
4885                    let provider = self.provider_name();
4886                    // Return error on first attempt
4887                    let stream = futures::stream::once(async move {
4888                        Err(LanguageModelCompletionError::ServerOverloaded {
4889                            provider,
4890                            retry_after: None,
4891                        })
4892                    });
4893                    async move { Ok(stream.boxed()) }.boxed()
4894                } else {
4895                    // Succeed on retry
4896                    self.inner.stream_completion(request, cx)
4897                }
4898            }
4899        }
4900
4901        let fail_once_model = Arc::new(FailOnceModel {
4902            inner: Arc::new(FakeLanguageModel::default()),
4903            failed_once: Arc::new(Mutex::new(false)),
4904        });
4905
4906        // Insert a user message
4907        thread.update(cx, |thread, cx| {
4908            thread.insert_user_message(
4909                "Test message",
4910                ContextLoadResult::default(),
4911                None,
4912                vec![],
4913                cx,
4914            );
4915        });
4916
4917        // Start completion with fail-once model
4918        thread.update(cx, |thread, cx| {
4919            thread.send_to_model(
4920                fail_once_model.clone(),
4921                CompletionIntent::UserPrompt,
4922                None,
4923                cx,
4924            );
4925        });
4926
4927        cx.run_until_parked();
4928
4929        // Verify retry state exists after first failure
4930        thread.read_with(cx, |thread, _| {
4931            assert!(
4932                thread.retry_state.is_some(),
4933                "Should have retry state after failure"
4934            );
4935        });
4936
4937        // Wait for retry delay
4938        cx.executor().advance_clock(BASE_RETRY_DELAY);
4939        cx.run_until_parked();
4940
4941        // The retry should now use our FailOnceModel which should succeed
4942        // We need to help the FakeLanguageModel complete the stream
4943        let inner_fake = fail_once_model.inner.clone();
4944
4945        // Wait a bit for the retry to start
4946        cx.run_until_parked();
4947
4948        // Check for pending completions and complete them
4949        if let Some(pending) = inner_fake.pending_completions().first() {
4950            inner_fake.send_completion_stream_text_chunk(pending, "Success!");
4951            inner_fake.end_completion_stream(pending);
4952        }
4953        cx.run_until_parked();
4954
4955        thread.read_with(cx, |thread, _| {
4956            assert!(
4957                thread.retry_state.is_none(),
4958                "Retry state should be cleared after successful completion"
4959            );
4960
4961            let has_assistant_message = thread
4962                .messages
4963                .iter()
4964                .any(|msg| msg.role == Role::Assistant && !msg.ui_only);
4965            assert!(
4966                has_assistant_message,
4967                "Should have an assistant message after successful retry"
4968            );
4969        });
4970    }
4971
4972    #[gpui::test]
4973    async fn test_rate_limit_retry_single_attempt(cx: &mut TestAppContext) {
4974        init_test_settings(cx);
4975
4976        let project = create_test_project(cx, json!({})).await;
4977        let (_, _, thread, _, _base_model) = setup_test_environment(cx, project.clone()).await;
4978
4979        // Enable Burn Mode to allow retries
4980        thread.update(cx, |thread, _| {
4981            thread.set_completion_mode(CompletionMode::Burn);
4982        });
4983
4984        // Create a model that returns rate limit error with retry_after
4985        struct RateLimitModel {
4986            inner: Arc<FakeLanguageModel>,
4987        }
4988
4989        impl LanguageModel for RateLimitModel {
4990            fn id(&self) -> LanguageModelId {
4991                self.inner.id()
4992            }
4993
4994            fn name(&self) -> LanguageModelName {
4995                self.inner.name()
4996            }
4997
4998            fn provider_id(&self) -> LanguageModelProviderId {
4999                self.inner.provider_id()
5000            }
5001
5002            fn provider_name(&self) -> LanguageModelProviderName {
5003                self.inner.provider_name()
5004            }
5005
5006            fn supports_tools(&self) -> bool {
5007                self.inner.supports_tools()
5008            }
5009
5010            fn supports_tool_choice(&self, choice: LanguageModelToolChoice) -> bool {
5011                self.inner.supports_tool_choice(choice)
5012            }
5013
5014            fn supports_images(&self) -> bool {
5015                self.inner.supports_images()
5016            }
5017
5018            fn telemetry_id(&self) -> String {
5019                self.inner.telemetry_id()
5020            }
5021
5022            fn max_token_count(&self) -> u64 {
5023                self.inner.max_token_count()
5024            }
5025
5026            fn count_tokens(
5027                &self,
5028                request: LanguageModelRequest,
5029                cx: &App,
5030            ) -> BoxFuture<'static, Result<u64>> {
5031                self.inner.count_tokens(request, cx)
5032            }
5033
5034            fn stream_completion(
5035                &self,
5036                _request: LanguageModelRequest,
5037                _cx: &AsyncApp,
5038            ) -> BoxFuture<
5039                'static,
5040                Result<
5041                    BoxStream<
5042                        'static,
5043                        Result<LanguageModelCompletionEvent, LanguageModelCompletionError>,
5044                    >,
5045                    LanguageModelCompletionError,
5046                >,
5047            > {
5048                let provider = self.provider_name();
5049                async move {
5050                    let stream = futures::stream::once(async move {
5051                        Err(LanguageModelCompletionError::RateLimitExceeded {
5052                            provider,
5053                            retry_after: Some(Duration::from_secs(TEST_RATE_LIMIT_RETRY_SECS)),
5054                        })
5055                    });
5056                    Ok(stream.boxed())
5057                }
5058                .boxed()
5059            }
5060
5061            fn as_fake(&self) -> &FakeLanguageModel {
5062                &self.inner
5063            }
5064        }
5065
5066        let model = Arc::new(RateLimitModel {
5067            inner: Arc::new(FakeLanguageModel::default()),
5068        });
5069
5070        // Insert a user message
5071        thread.update(cx, |thread, cx| {
5072            thread.insert_user_message("Hello!", ContextLoadResult::default(), None, vec![], cx);
5073        });
5074
5075        // Start completion
5076        thread.update(cx, |thread, cx| {
5077            thread.send_to_model(model.clone(), CompletionIntent::UserPrompt, None, cx);
5078        });
5079
5080        cx.run_until_parked();
5081
5082        let retry_count = thread.update(cx, |thread, _| {
5083            thread
5084                .messages
5085                .iter()
5086                .filter(|m| {
5087                    m.ui_only
5088                        && m.segments.iter().any(|s| {
5089                            if let MessageSegment::Text(text) = s {
5090                                text.contains("rate limit exceeded")
5091                            } else {
5092                                false
5093                            }
5094                        })
5095                })
5096                .count()
5097        });
5098        assert_eq!(retry_count, 1, "Should have scheduled one retry");
5099
5100        thread.read_with(cx, |thread, _| {
5101            assert!(
5102                thread.retry_state.is_some(),
5103                "Rate limit errors should set retry_state"
5104            );
5105            if let Some(retry_state) = &thread.retry_state {
5106                assert_eq!(
5107                    retry_state.max_attempts, MAX_RETRY_ATTEMPTS,
5108                    "Rate limit errors should use MAX_RETRY_ATTEMPTS"
5109                );
5110            }
5111        });
5112
5113        // Verify we have one retry message
5114        thread.read_with(cx, |thread, _| {
5115            let retry_messages = thread
5116                .messages
5117                .iter()
5118                .filter(|msg| {
5119                    msg.ui_only
5120                        && msg.segments.iter().any(|seg| {
5121                            if let MessageSegment::Text(text) = seg {
5122                                text.contains("rate limit exceeded")
5123                            } else {
5124                                false
5125                            }
5126                        })
5127                })
5128                .count();
5129            assert_eq!(
5130                retry_messages, 1,
5131                "Should have one rate limit retry message"
5132            );
5133        });
5134
5135        // Check that retry message doesn't include attempt count
5136        thread.read_with(cx, |thread, _| {
5137            let retry_message = thread
5138                .messages
5139                .iter()
5140                .find(|msg| msg.role == Role::System && msg.ui_only)
5141                .expect("Should have a retry message");
5142
5143            // Check that the message contains attempt count since we use retry_state
5144            if let Some(MessageSegment::Text(text)) = retry_message.segments.first() {
5145                assert!(
5146                    text.contains(&format!("attempt 1 of {}", MAX_RETRY_ATTEMPTS)),
5147                    "Rate limit retry message should contain attempt count with MAX_RETRY_ATTEMPTS"
5148                );
5149                assert!(
5150                    text.contains("Retrying"),
5151                    "Rate limit retry message should contain retry text"
5152                );
5153            }
5154        });
5155    }
5156
5157    #[gpui::test]
5158    async fn test_ui_only_messages_not_sent_to_model(cx: &mut TestAppContext) {
5159        init_test_settings(cx);
5160
5161        let project = create_test_project(cx, json!({})).await;
5162        let (_, _, thread, _, model) = setup_test_environment(cx, project.clone()).await;
5163
5164        // Insert a regular user message
5165        thread.update(cx, |thread, cx| {
5166            thread.insert_user_message("Hello!", ContextLoadResult::default(), None, vec![], cx);
5167        });
5168
5169        // Insert a UI-only message (like our retry notifications)
5170        thread.update(cx, |thread, cx| {
5171            let id = thread.next_message_id.post_inc();
5172            thread.messages.push(Message {
5173                id,
5174                role: Role::System,
5175                segments: vec![MessageSegment::Text(
5176                    "This is a UI-only message that should not be sent to the model".to_string(),
5177                )],
5178                loaded_context: LoadedContext::default(),
5179                creases: Vec::new(),
5180                is_hidden: true,
5181                ui_only: true,
5182            });
5183            cx.emit(ThreadEvent::MessageAdded(id));
5184        });
5185
5186        // Insert another regular message
5187        thread.update(cx, |thread, cx| {
5188            thread.insert_user_message(
5189                "How are you?",
5190                ContextLoadResult::default(),
5191                None,
5192                vec![],
5193                cx,
5194            );
5195        });
5196
5197        // Generate the completion request
5198        let request = thread.update(cx, |thread, cx| {
5199            thread.to_completion_request(model.clone(), CompletionIntent::UserPrompt, cx)
5200        });
5201
5202        // Verify that the request only contains non-UI-only messages
5203        // Should have system prompt + 2 user messages, but not the UI-only message
5204        let user_messages: Vec<_> = request
5205            .messages
5206            .iter()
5207            .filter(|msg| msg.role == Role::User)
5208            .collect();
5209        assert_eq!(
5210            user_messages.len(),
5211            2,
5212            "Should have exactly 2 user messages"
5213        );
5214
5215        // Verify the UI-only content is not present anywhere in the request
5216        let request_text = request
5217            .messages
5218            .iter()
5219            .flat_map(|msg| &msg.content)
5220            .filter_map(|content| match content {
5221                MessageContent::Text(text) => Some(text.as_str()),
5222                _ => None,
5223            })
5224            .collect::<String>();
5225
5226        assert!(
5227            !request_text.contains("UI-only message"),
5228            "UI-only message content should not be in the request"
5229        );
5230
5231        // Verify the thread still has all 3 messages (including UI-only)
5232        thread.read_with(cx, |thread, _| {
5233            assert_eq!(
5234                thread.messages().count(),
5235                3,
5236                "Thread should have 3 messages"
5237            );
5238            assert_eq!(
5239                thread.messages().filter(|m| m.ui_only).count(),
5240                1,
5241                "Thread should have 1 UI-only message"
5242            );
5243        });
5244
5245        // Verify that UI-only messages are not serialized
5246        let serialized = thread
5247            .update(cx, |thread, cx| thread.serialize(cx))
5248            .await
5249            .unwrap();
5250        assert_eq!(
5251            serialized.messages.len(),
5252            2,
5253            "Serialized thread should only have 2 messages (no UI-only)"
5254        );
5255    }
5256
5257    #[gpui::test]
5258    async fn test_no_retry_without_burn_mode(cx: &mut TestAppContext) {
5259        init_test_settings(cx);
5260
5261        let project = create_test_project(cx, json!({})).await;
5262        let (_, _, thread, _, _base_model) = setup_test_environment(cx, project.clone()).await;
5263
5264        // Ensure we're in Normal mode (not Burn mode)
5265        thread.update(cx, |thread, _| {
5266            thread.set_completion_mode(CompletionMode::Normal);
5267        });
5268
5269        // Track error events
5270        let error_events = Arc::new(Mutex::new(Vec::new()));
5271        let error_events_clone = error_events.clone();
5272
5273        let _subscription = thread.update(cx, |_, cx| {
5274            cx.subscribe(&thread, move |_, _, event: &ThreadEvent, _| {
5275                if let ThreadEvent::ShowError(error) = event {
5276                    error_events_clone.lock().push(error.clone());
5277                }
5278            })
5279        });
5280
5281        // Create model that returns overloaded error
5282        let model = Arc::new(ErrorInjector::new(TestError::Overloaded));
5283
5284        // Insert a user message
5285        thread.update(cx, |thread, cx| {
5286            thread.insert_user_message("Hello!", ContextLoadResult::default(), None, vec![], cx);
5287        });
5288
5289        // Start completion
5290        thread.update(cx, |thread, cx| {
5291            thread.send_to_model(model.clone(), CompletionIntent::UserPrompt, None, cx);
5292        });
5293
5294        cx.run_until_parked();
5295
5296        // Verify no retry state was created
5297        thread.read_with(cx, |thread, _| {
5298            assert!(
5299                thread.retry_state.is_none(),
5300                "Should not have retry state in Normal mode"
5301            );
5302        });
5303
5304        // Check that a retryable error was reported
5305        let errors = error_events.lock();
5306        assert!(!errors.is_empty(), "Should have received an error event");
5307
5308        if let ThreadError::RetryableError {
5309            message: _,
5310            can_enable_burn_mode,
5311        } = &errors[0]
5312        {
5313            assert!(
5314                *can_enable_burn_mode,
5315                "Error should indicate burn mode can be enabled"
5316            );
5317        } else {
5318            panic!("Expected RetryableError, got {:?}", errors[0]);
5319        }
5320
5321        // Verify the thread is no longer generating
5322        thread.read_with(cx, |thread, _| {
5323            assert!(
5324                !thread.is_generating(),
5325                "Should not be generating after error without retry"
5326            );
5327        });
5328    }
5329
5330    #[gpui::test]
5331    async fn test_retry_cancelled_on_stop(cx: &mut TestAppContext) {
5332        init_test_settings(cx);
5333
5334        let project = create_test_project(cx, json!({})).await;
5335        let (_, _, thread, _, _base_model) = setup_test_environment(cx, project.clone()).await;
5336
5337        // Enable Burn Mode to allow retries
5338        thread.update(cx, |thread, _| {
5339            thread.set_completion_mode(CompletionMode::Burn);
5340        });
5341
5342        // Create model that returns overloaded error
5343        let model = Arc::new(ErrorInjector::new(TestError::Overloaded));
5344
5345        // Insert a user message
5346        thread.update(cx, |thread, cx| {
5347            thread.insert_user_message("Hello!", ContextLoadResult::default(), None, vec![], cx);
5348        });
5349
5350        // Start completion
5351        thread.update(cx, |thread, cx| {
5352            thread.send_to_model(model.clone(), CompletionIntent::UserPrompt, None, cx);
5353        });
5354
5355        cx.run_until_parked();
5356
5357        // Verify retry was scheduled by checking for retry message
5358        let has_retry_message = thread.read_with(cx, |thread, _| {
5359            thread.messages.iter().any(|m| {
5360                m.ui_only
5361                    && m.segments.iter().any(|s| {
5362                        if let MessageSegment::Text(text) = s {
5363                            text.contains("Retrying") && text.contains("seconds")
5364                        } else {
5365                            false
5366                        }
5367                    })
5368            })
5369        });
5370        assert!(has_retry_message, "Should have scheduled a retry");
5371
5372        // Cancel the completion before the retry happens
5373        thread.update(cx, |thread, cx| {
5374            thread.cancel_last_completion(None, cx);
5375        });
5376
5377        cx.run_until_parked();
5378
5379        // The retry should not have happened - no pending completions
5380        let fake_model = model.as_fake();
5381        assert_eq!(
5382            fake_model.pending_completions().len(),
5383            0,
5384            "Should have no pending completions after cancellation"
5385        );
5386
5387        // Verify the retry was cancelled by checking retry state
5388        thread.read_with(cx, |thread, _| {
5389            if let Some(retry_state) = &thread.retry_state {
5390                panic!(
5391                    "retry_state should be cleared after cancellation, but found: attempt={}, max_attempts={}, intent={:?}",
5392                    retry_state.attempt, retry_state.max_attempts, retry_state.intent
5393                );
5394            }
5395        });
5396    }
5397
5398    fn test_summarize_error(
5399        model: &Arc<dyn LanguageModel>,
5400        thread: &Entity<Thread>,
5401        cx: &mut TestAppContext,
5402    ) {
5403        thread.update(cx, |thread, cx| {
5404            thread.insert_user_message("Hi!", ContextLoadResult::default(), None, vec![], cx);
5405            thread.send_to_model(
5406                model.clone(),
5407                CompletionIntent::ThreadSummarization,
5408                None,
5409                cx,
5410            );
5411        });
5412
5413        let fake_model = model.as_fake();
5414        simulate_successful_response(&fake_model, cx);
5415
5416        thread.read_with(cx, |thread, _| {
5417            assert!(matches!(thread.summary(), ThreadSummary::Generating));
5418            assert_eq!(thread.summary().or_default(), ThreadSummary::DEFAULT);
5419        });
5420
5421        // Simulate summary request ending
5422        cx.run_until_parked();
5423        fake_model.end_last_completion_stream();
5424        cx.run_until_parked();
5425
5426        // State is set to Error and default message
5427        thread.read_with(cx, |thread, _| {
5428            assert!(matches!(thread.summary(), ThreadSummary::Error));
5429            assert_eq!(thread.summary().or_default(), ThreadSummary::DEFAULT);
5430        });
5431    }
5432
5433    fn simulate_successful_response(fake_model: &FakeLanguageModel, cx: &mut TestAppContext) {
5434        cx.run_until_parked();
5435        fake_model.send_last_completion_stream_text_chunk("Assistant response");
5436        fake_model.end_last_completion_stream();
5437        cx.run_until_parked();
5438    }
5439
5440    fn init_test_settings(cx: &mut TestAppContext) {
5441        cx.update(|cx| {
5442            let settings_store = SettingsStore::test(cx);
5443            cx.set_global(settings_store);
5444            language::init(cx);
5445            Project::init_settings(cx);
5446            AgentSettings::register(cx);
5447            prompt_store::init(cx);
5448            thread_store::init(cx);
5449            workspace::init_settings(cx);
5450            language_model::init_settings(cx);
5451            ThemeSettings::register(cx);
5452            ToolRegistry::default_global(cx);
5453            assistant_tool::init(cx);
5454
5455            let http_client = Arc::new(http_client::HttpClientWithUrl::new(
5456                http_client::FakeHttpClient::with_200_response(),
5457                "http://localhost".to_string(),
5458                None,
5459            ));
5460            assistant_tools::init(http_client, cx);
5461        });
5462    }
5463
5464    // Helper to create a test project with test files
5465    async fn create_test_project(
5466        cx: &mut TestAppContext,
5467        files: serde_json::Value,
5468    ) -> Entity<Project> {
5469        let fs = FakeFs::new(cx.executor());
5470        fs.insert_tree(path!("/test"), files).await;
5471        Project::test(fs, [path!("/test").as_ref()], cx).await
5472    }
5473
5474    async fn setup_test_environment(
5475        cx: &mut TestAppContext,
5476        project: Entity<Project>,
5477    ) -> (
5478        Entity<Workspace>,
5479        Entity<ThreadStore>,
5480        Entity<Thread>,
5481        Entity<ContextStore>,
5482        Arc<dyn LanguageModel>,
5483    ) {
5484        let (workspace, cx) =
5485            cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));
5486
5487        let thread_store = cx
5488            .update(|_, cx| {
5489                ThreadStore::load(
5490                    project.clone(),
5491                    cx.new(|_| ToolWorkingSet::default()),
5492                    None,
5493                    Arc::new(PromptBuilder::new(None).unwrap()),
5494                    cx,
5495                )
5496            })
5497            .await
5498            .unwrap();
5499
5500        let thread = thread_store.update(cx, |store, cx| store.create_thread(cx));
5501        let context_store = cx.new(|_cx| ContextStore::new(project.downgrade(), None));
5502
5503        let provider = Arc::new(FakeLanguageModelProvider::default());
5504        let model = provider.test_model();
5505        let model: Arc<dyn LanguageModel> = Arc::new(model);
5506
5507        cx.update(|_, cx| {
5508            LanguageModelRegistry::global(cx).update(cx, |registry, cx| {
5509                registry.set_default_model(
5510                    Some(ConfiguredModel {
5511                        provider: provider.clone(),
5512                        model: model.clone(),
5513                    }),
5514                    cx,
5515                );
5516                registry.set_thread_summary_model(
5517                    Some(ConfiguredModel {
5518                        provider,
5519                        model: model.clone(),
5520                    }),
5521                    cx,
5522                );
5523            })
5524        });
5525
5526        (workspace, thread_store, thread, context_store, model)
5527    }
5528
5529    async fn add_file_to_context(
5530        project: &Entity<Project>,
5531        context_store: &Entity<ContextStore>,
5532        path: &str,
5533        cx: &mut TestAppContext,
5534    ) -> Result<Entity<language::Buffer>> {
5535        let buffer_path = project
5536            .read_with(cx, |project, cx| project.find_project_path(path, cx))
5537            .unwrap();
5538
5539        let buffer = project
5540            .update(cx, |project, cx| {
5541                project.open_buffer(buffer_path.clone(), cx)
5542            })
5543            .await
5544            .unwrap();
5545
5546        context_store.update(cx, |context_store, cx| {
5547            context_store.add_file_from_buffer(&buffer_path, buffer.clone(), false, cx);
5548        });
5549
5550        Ok(buffer)
5551    }
5552}