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