thread.rs

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