thread.rs

   1use std::fmt::Write as _;
   2use std::io::Write;
   3use std::ops::Range;
   4use std::sync::Arc;
   5
   6use anyhow::{Context as _, Result};
   7use assistant_settings::AssistantSettings;
   8use assistant_tool::{ActionLog, Tool, ToolWorkingSet};
   9use chrono::{DateTime, Utc};
  10use collections::{BTreeMap, HashMap, HashSet};
  11use fs::Fs;
  12use futures::future::Shared;
  13use futures::{FutureExt, StreamExt as _};
  14use git;
  15use gpui::{App, AppContext, Context, Entity, EventEmitter, SharedString, Task, WeakEntity};
  16use language_model::{
  17    LanguageModel, LanguageModelCompletionEvent, LanguageModelRegistry, LanguageModelRequest,
  18    LanguageModelRequestMessage, LanguageModelRequestTool, LanguageModelToolResult,
  19    LanguageModelToolUseId, MaxMonthlySpendReachedError, MessageContent, PaymentRequiredError,
  20    Role, StopReason, TokenUsage,
  21};
  22use project::git_store::{GitStore, GitStoreCheckpoint};
  23use project::{Project, Worktree};
  24use prompt_store::{
  25    AssistantSystemPromptContext, PromptBuilder, RulesFile, WorktreeInfoForSystemPrompt,
  26};
  27use serde::{Deserialize, Serialize};
  28use settings::Settings;
  29use util::{ResultExt as _, TryFutureExt as _, maybe, post_inc};
  30use uuid::Uuid;
  31
  32use crate::context::{AssistantContext, ContextId, attach_context_to_message};
  33use crate::thread_store::{
  34    SerializedMessage, SerializedMessageSegment, SerializedThread, SerializedToolResult,
  35    SerializedToolUse,
  36};
  37use crate::tool_use::{PendingToolUse, ToolUse, ToolUseState};
  38
  39#[derive(Debug, Clone, Copy)]
  40pub enum RequestKind {
  41    Chat,
  42    /// Used when summarizing a thread.
  43    Summarize,
  44}
  45
  46#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Serialize, Deserialize)]
  47pub struct ThreadId(Arc<str>);
  48
  49impl ThreadId {
  50    pub fn new() -> Self {
  51        Self(Uuid::new_v4().to_string().into())
  52    }
  53}
  54
  55impl std::fmt::Display for ThreadId {
  56    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  57        write!(f, "{}", self.0)
  58    }
  59}
  60
  61impl From<&str> for ThreadId {
  62    fn from(value: &str) -> Self {
  63        Self(value.into())
  64    }
  65}
  66
  67#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Serialize, Deserialize)]
  68pub struct MessageId(pub(crate) usize);
  69
  70impl MessageId {
  71    fn post_inc(&mut self) -> Self {
  72        Self(post_inc(&mut self.0))
  73    }
  74}
  75
  76/// A message in a [`Thread`].
  77#[derive(Debug, Clone)]
  78pub struct Message {
  79    pub id: MessageId,
  80    pub role: Role,
  81    pub segments: Vec<MessageSegment>,
  82}
  83
  84impl Message {
  85    pub fn push_thinking(&mut self, text: &str) {
  86        if let Some(MessageSegment::Thinking(segment)) = self.segments.last_mut() {
  87            segment.push_str(text);
  88        } else {
  89            self.segments
  90                .push(MessageSegment::Thinking(text.to_string()));
  91        }
  92    }
  93
  94    pub fn push_text(&mut self, text: &str) {
  95        if let Some(MessageSegment::Text(segment)) = self.segments.last_mut() {
  96            segment.push_str(text);
  97        } else {
  98            self.segments.push(MessageSegment::Text(text.to_string()));
  99        }
 100    }
 101
 102    pub fn to_string(&self) -> String {
 103        let mut result = String::new();
 104        for segment in &self.segments {
 105            match segment {
 106                MessageSegment::Text(text) => result.push_str(text),
 107                MessageSegment::Thinking(text) => {
 108                    result.push_str("<think>");
 109                    result.push_str(text);
 110                    result.push_str("</think>");
 111                }
 112            }
 113        }
 114        result
 115    }
 116}
 117
 118#[derive(Debug, Clone)]
 119pub enum MessageSegment {
 120    Text(String),
 121    Thinking(String),
 122}
 123
 124#[derive(Debug, Clone, Serialize, Deserialize)]
 125pub struct ProjectSnapshot {
 126    pub worktree_snapshots: Vec<WorktreeSnapshot>,
 127    pub unsaved_buffer_paths: Vec<String>,
 128    pub timestamp: DateTime<Utc>,
 129}
 130
 131#[derive(Debug, Clone, Serialize, Deserialize)]
 132pub struct WorktreeSnapshot {
 133    pub worktree_path: String,
 134    pub git_state: Option<GitState>,
 135}
 136
 137#[derive(Debug, Clone, Serialize, Deserialize)]
 138pub struct GitState {
 139    pub remote_url: Option<String>,
 140    pub head_sha: Option<String>,
 141    pub current_branch: Option<String>,
 142    pub diff: Option<String>,
 143}
 144
 145#[derive(Clone)]
 146pub struct ThreadCheckpoint {
 147    message_id: MessageId,
 148    git_checkpoint: GitStoreCheckpoint,
 149}
 150
 151#[derive(Copy, Clone, Debug)]
 152pub enum ThreadFeedback {
 153    Positive,
 154    Negative,
 155}
 156
 157pub enum LastRestoreCheckpoint {
 158    Pending {
 159        message_id: MessageId,
 160    },
 161    Error {
 162        message_id: MessageId,
 163        error: String,
 164    },
 165}
 166
 167impl LastRestoreCheckpoint {
 168    pub fn message_id(&self) -> MessageId {
 169        match self {
 170            LastRestoreCheckpoint::Pending { message_id } => *message_id,
 171            LastRestoreCheckpoint::Error { message_id, .. } => *message_id,
 172        }
 173    }
 174}
 175
 176/// A thread of conversation with the LLM.
 177pub struct Thread {
 178    id: ThreadId,
 179    updated_at: DateTime<Utc>,
 180    summary: Option<SharedString>,
 181    pending_summary: Task<Option<()>>,
 182    messages: Vec<Message>,
 183    next_message_id: MessageId,
 184    context: BTreeMap<ContextId, AssistantContext>,
 185    context_by_message: HashMap<MessageId, Vec<ContextId>>,
 186    system_prompt_context: Option<AssistantSystemPromptContext>,
 187    checkpoints_by_message: HashMap<MessageId, ThreadCheckpoint>,
 188    completion_count: usize,
 189    pending_completions: Vec<PendingCompletion>,
 190    project: Entity<Project>,
 191    prompt_builder: Arc<PromptBuilder>,
 192    tools: Arc<ToolWorkingSet>,
 193    tool_use: ToolUseState,
 194    action_log: Entity<ActionLog>,
 195    last_restore_checkpoint: Option<LastRestoreCheckpoint>,
 196    pending_checkpoint: Option<ThreadCheckpoint>,
 197    initial_project_snapshot: Shared<Task<Option<Arc<ProjectSnapshot>>>>,
 198    cumulative_token_usage: TokenUsage,
 199    feedback: Option<ThreadFeedback>,
 200}
 201
 202impl Thread {
 203    pub fn new(
 204        project: Entity<Project>,
 205        tools: Arc<ToolWorkingSet>,
 206        prompt_builder: Arc<PromptBuilder>,
 207        cx: &mut Context<Self>,
 208    ) -> Self {
 209        Self {
 210            id: ThreadId::new(),
 211            updated_at: Utc::now(),
 212            summary: None,
 213            pending_summary: Task::ready(None),
 214            messages: Vec::new(),
 215            next_message_id: MessageId(0),
 216            context: BTreeMap::default(),
 217            context_by_message: HashMap::default(),
 218            system_prompt_context: None,
 219            checkpoints_by_message: HashMap::default(),
 220            completion_count: 0,
 221            pending_completions: Vec::new(),
 222            project: project.clone(),
 223            prompt_builder,
 224            tools: tools.clone(),
 225            last_restore_checkpoint: None,
 226            pending_checkpoint: None,
 227            tool_use: ToolUseState::new(tools.clone()),
 228            action_log: cx.new(|_| ActionLog::new()),
 229            initial_project_snapshot: {
 230                let project_snapshot = Self::project_snapshot(project, cx);
 231                cx.foreground_executor()
 232                    .spawn(async move { Some(project_snapshot.await) })
 233                    .shared()
 234            },
 235            cumulative_token_usage: TokenUsage::default(),
 236            feedback: None,
 237        }
 238    }
 239
 240    pub fn deserialize(
 241        id: ThreadId,
 242        serialized: SerializedThread,
 243        project: Entity<Project>,
 244        tools: Arc<ToolWorkingSet>,
 245        prompt_builder: Arc<PromptBuilder>,
 246        cx: &mut Context<Self>,
 247    ) -> Self {
 248        let next_message_id = MessageId(
 249            serialized
 250                .messages
 251                .last()
 252                .map(|message| message.id.0 + 1)
 253                .unwrap_or(0),
 254        );
 255        let tool_use =
 256            ToolUseState::from_serialized_messages(tools.clone(), &serialized.messages, |_| true);
 257
 258        Self {
 259            id,
 260            updated_at: serialized.updated_at,
 261            summary: Some(serialized.summary),
 262            pending_summary: Task::ready(None),
 263            messages: serialized
 264                .messages
 265                .into_iter()
 266                .map(|message| Message {
 267                    id: message.id,
 268                    role: message.role,
 269                    segments: message
 270                        .segments
 271                        .into_iter()
 272                        .map(|segment| match segment {
 273                            SerializedMessageSegment::Text { text } => MessageSegment::Text(text),
 274                            SerializedMessageSegment::Thinking { text } => {
 275                                MessageSegment::Thinking(text)
 276                            }
 277                        })
 278                        .collect(),
 279                })
 280                .collect(),
 281            next_message_id,
 282            context: BTreeMap::default(),
 283            context_by_message: HashMap::default(),
 284            system_prompt_context: None,
 285            checkpoints_by_message: HashMap::default(),
 286            completion_count: 0,
 287            pending_completions: Vec::new(),
 288            last_restore_checkpoint: None,
 289            pending_checkpoint: None,
 290            project,
 291            prompt_builder,
 292            tools,
 293            tool_use,
 294            action_log: cx.new(|_| ActionLog::new()),
 295            initial_project_snapshot: Task::ready(serialized.initial_project_snapshot).shared(),
 296            cumulative_token_usage: serialized.cumulative_token_usage,
 297            feedback: None,
 298        }
 299    }
 300
 301    pub fn id(&self) -> &ThreadId {
 302        &self.id
 303    }
 304
 305    pub fn is_empty(&self) -> bool {
 306        self.messages.is_empty()
 307    }
 308
 309    pub fn updated_at(&self) -> DateTime<Utc> {
 310        self.updated_at
 311    }
 312
 313    pub fn touch_updated_at(&mut self) {
 314        self.updated_at = Utc::now();
 315    }
 316
 317    pub fn summary(&self) -> Option<SharedString> {
 318        self.summary.clone()
 319    }
 320
 321    pub fn summary_or_default(&self) -> SharedString {
 322        const DEFAULT: SharedString = SharedString::new_static("New Thread");
 323        self.summary.clone().unwrap_or(DEFAULT)
 324    }
 325
 326    pub fn set_summary(&mut self, summary: impl Into<SharedString>, cx: &mut Context<Self>) {
 327        self.summary = Some(summary.into());
 328        cx.emit(ThreadEvent::SummaryChanged);
 329    }
 330
 331    pub fn message(&self, id: MessageId) -> Option<&Message> {
 332        self.messages.iter().find(|message| message.id == id)
 333    }
 334
 335    pub fn messages(&self) -> impl Iterator<Item = &Message> {
 336        self.messages.iter()
 337    }
 338
 339    pub fn is_generating(&self) -> bool {
 340        !self.pending_completions.is_empty() || !self.all_tools_finished()
 341    }
 342
 343    pub fn tools(&self) -> &Arc<ToolWorkingSet> {
 344        &self.tools
 345    }
 346
 347    pub fn pending_tool(&self, id: &LanguageModelToolUseId) -> Option<&PendingToolUse> {
 348        self.tool_use
 349            .pending_tool_uses()
 350            .into_iter()
 351            .find(|tool_use| &tool_use.id == id)
 352    }
 353
 354    pub fn tools_needing_confirmation(&self) -> impl Iterator<Item = &PendingToolUse> {
 355        self.tool_use
 356            .pending_tool_uses()
 357            .into_iter()
 358            .filter(|tool_use| tool_use.status.needs_confirmation())
 359    }
 360
 361    pub fn has_pending_tool_uses(&self) -> bool {
 362        !self.tool_use.pending_tool_uses().is_empty()
 363    }
 364
 365    pub fn checkpoint_for_message(&self, id: MessageId) -> Option<ThreadCheckpoint> {
 366        self.checkpoints_by_message.get(&id).cloned()
 367    }
 368
 369    pub fn restore_checkpoint(
 370        &mut self,
 371        checkpoint: ThreadCheckpoint,
 372        cx: &mut Context<Self>,
 373    ) -> Task<Result<()>> {
 374        self.last_restore_checkpoint = Some(LastRestoreCheckpoint::Pending {
 375            message_id: checkpoint.message_id,
 376        });
 377        cx.emit(ThreadEvent::CheckpointChanged);
 378        cx.notify();
 379
 380        let project = self.project.read(cx);
 381        let restore = project
 382            .git_store()
 383            .read(cx)
 384            .restore_checkpoint(checkpoint.git_checkpoint.clone(), cx);
 385        cx.spawn(async move |this, cx| {
 386            let result = restore.await;
 387            this.update(cx, |this, cx| {
 388                if let Err(err) = result.as_ref() {
 389                    this.last_restore_checkpoint = Some(LastRestoreCheckpoint::Error {
 390                        message_id: checkpoint.message_id,
 391                        error: err.to_string(),
 392                    });
 393                } else {
 394                    this.truncate(checkpoint.message_id, cx);
 395                    this.last_restore_checkpoint = None;
 396                }
 397                this.pending_checkpoint = None;
 398                cx.emit(ThreadEvent::CheckpointChanged);
 399                cx.notify();
 400            })?;
 401            result
 402        })
 403    }
 404
 405    fn finalize_pending_checkpoint(&mut self, cx: &mut Context<Self>) {
 406        let pending_checkpoint = if self.is_generating() {
 407            return;
 408        } else if let Some(checkpoint) = self.pending_checkpoint.take() {
 409            checkpoint
 410        } else {
 411            return;
 412        };
 413
 414        let git_store = self.project.read(cx).git_store().clone();
 415        let final_checkpoint = git_store.read(cx).checkpoint(cx);
 416        cx.spawn(async move |this, cx| match final_checkpoint.await {
 417            Ok(final_checkpoint) => {
 418                let equal = git_store
 419                    .read_with(cx, |store, cx| {
 420                        store.compare_checkpoints(
 421                            pending_checkpoint.git_checkpoint.clone(),
 422                            final_checkpoint.clone(),
 423                            cx,
 424                        )
 425                    })?
 426                    .await
 427                    .unwrap_or(false);
 428
 429                if equal {
 430                    git_store
 431                        .read_with(cx, |store, cx| {
 432                            store.delete_checkpoint(pending_checkpoint.git_checkpoint, cx)
 433                        })?
 434                        .detach();
 435                } else {
 436                    this.update(cx, |this, cx| {
 437                        this.insert_checkpoint(pending_checkpoint, cx)
 438                    })?;
 439                }
 440
 441                git_store
 442                    .read_with(cx, |store, cx| {
 443                        store.delete_checkpoint(final_checkpoint, cx)
 444                    })?
 445                    .detach();
 446
 447                Ok(())
 448            }
 449            Err(_) => this.update(cx, |this, cx| {
 450                this.insert_checkpoint(pending_checkpoint, cx)
 451            }),
 452        })
 453        .detach();
 454    }
 455
 456    fn insert_checkpoint(&mut self, checkpoint: ThreadCheckpoint, cx: &mut Context<Self>) {
 457        self.checkpoints_by_message
 458            .insert(checkpoint.message_id, checkpoint);
 459        cx.emit(ThreadEvent::CheckpointChanged);
 460        cx.notify();
 461    }
 462
 463    pub fn last_restore_checkpoint(&self) -> Option<&LastRestoreCheckpoint> {
 464        self.last_restore_checkpoint.as_ref()
 465    }
 466
 467    pub fn truncate(&mut self, message_id: MessageId, cx: &mut Context<Self>) {
 468        let Some(message_ix) = self
 469            .messages
 470            .iter()
 471            .rposition(|message| message.id == message_id)
 472        else {
 473            return;
 474        };
 475        for deleted_message in self.messages.drain(message_ix..) {
 476            self.context_by_message.remove(&deleted_message.id);
 477            self.checkpoints_by_message.remove(&deleted_message.id);
 478        }
 479        cx.notify();
 480    }
 481
 482    pub fn context_for_message(&self, id: MessageId) -> impl Iterator<Item = &AssistantContext> {
 483        self.context_by_message
 484            .get(&id)
 485            .into_iter()
 486            .flat_map(|context| {
 487                context
 488                    .iter()
 489                    .filter_map(|context_id| self.context.get(&context_id))
 490            })
 491    }
 492
 493    /// Returns whether all of the tool uses have finished running.
 494    pub fn all_tools_finished(&self) -> bool {
 495        // If the only pending tool uses left are the ones with errors, then
 496        // that means that we've finished running all of the pending tools.
 497        self.tool_use
 498            .pending_tool_uses()
 499            .iter()
 500            .all(|tool_use| tool_use.status.is_error())
 501    }
 502
 503    pub fn tool_uses_for_message(&self, id: MessageId, cx: &App) -> Vec<ToolUse> {
 504        self.tool_use.tool_uses_for_message(id, cx)
 505    }
 506
 507    pub fn tool_results_for_message(&self, id: MessageId) -> Vec<&LanguageModelToolResult> {
 508        self.tool_use.tool_results_for_message(id)
 509    }
 510
 511    pub fn tool_result(&self, id: &LanguageModelToolUseId) -> Option<&LanguageModelToolResult> {
 512        self.tool_use.tool_result(id)
 513    }
 514
 515    pub fn message_has_tool_results(&self, message_id: MessageId) -> bool {
 516        self.tool_use.message_has_tool_results(message_id)
 517    }
 518
 519    pub fn insert_user_message(
 520        &mut self,
 521        text: impl Into<String>,
 522        context: Vec<AssistantContext>,
 523        git_checkpoint: Option<GitStoreCheckpoint>,
 524        cx: &mut Context<Self>,
 525    ) -> MessageId {
 526        let message_id =
 527            self.insert_message(Role::User, vec![MessageSegment::Text(text.into())], cx);
 528        let context_ids = context
 529            .iter()
 530            .map(|context| context.id())
 531            .collect::<Vec<_>>();
 532        self.context
 533            .extend(context.into_iter().map(|context| (context.id(), context)));
 534        self.context_by_message.insert(message_id, context_ids);
 535        if let Some(git_checkpoint) = git_checkpoint {
 536            self.pending_checkpoint = Some(ThreadCheckpoint {
 537                message_id,
 538                git_checkpoint,
 539            });
 540        }
 541        message_id
 542    }
 543
 544    pub fn insert_message(
 545        &mut self,
 546        role: Role,
 547        segments: Vec<MessageSegment>,
 548        cx: &mut Context<Self>,
 549    ) -> MessageId {
 550        let id = self.next_message_id.post_inc();
 551        self.messages.push(Message { id, role, segments });
 552        self.touch_updated_at();
 553        cx.emit(ThreadEvent::MessageAdded(id));
 554        id
 555    }
 556
 557    pub fn edit_message(
 558        &mut self,
 559        id: MessageId,
 560        new_role: Role,
 561        new_segments: Vec<MessageSegment>,
 562        cx: &mut Context<Self>,
 563    ) -> bool {
 564        let Some(message) = self.messages.iter_mut().find(|message| message.id == id) else {
 565            return false;
 566        };
 567        message.role = new_role;
 568        message.segments = new_segments;
 569        self.touch_updated_at();
 570        cx.emit(ThreadEvent::MessageEdited(id));
 571        true
 572    }
 573
 574    pub fn delete_message(&mut self, id: MessageId, cx: &mut Context<Self>) -> bool {
 575        let Some(index) = self.messages.iter().position(|message| message.id == id) else {
 576            return false;
 577        };
 578        self.messages.remove(index);
 579        self.context_by_message.remove(&id);
 580        self.touch_updated_at();
 581        cx.emit(ThreadEvent::MessageDeleted(id));
 582        true
 583    }
 584
 585    /// Returns the representation of this [`Thread`] in a textual form.
 586    ///
 587    /// This is the representation we use when attaching a thread as context to another thread.
 588    pub fn text(&self) -> String {
 589        let mut text = String::new();
 590
 591        for message in &self.messages {
 592            text.push_str(match message.role {
 593                language_model::Role::User => "User:",
 594                language_model::Role::Assistant => "Assistant:",
 595                language_model::Role::System => "System:",
 596            });
 597            text.push('\n');
 598
 599            for segment in &message.segments {
 600                match segment {
 601                    MessageSegment::Text(content) => text.push_str(content),
 602                    MessageSegment::Thinking(content) => {
 603                        text.push_str(&format!("<think>{}</think>", content))
 604                    }
 605                }
 606            }
 607            text.push('\n');
 608        }
 609
 610        text
 611    }
 612
 613    /// Serializes this thread into a format for storage or telemetry.
 614    pub fn serialize(&self, cx: &mut Context<Self>) -> Task<Result<SerializedThread>> {
 615        let initial_project_snapshot = self.initial_project_snapshot.clone();
 616        cx.spawn(async move |this, cx| {
 617            let initial_project_snapshot = initial_project_snapshot.await;
 618            this.read_with(cx, |this, cx| SerializedThread {
 619                version: SerializedThread::VERSION.to_string(),
 620                summary: this.summary_or_default(),
 621                updated_at: this.updated_at(),
 622                messages: this
 623                    .messages()
 624                    .map(|message| SerializedMessage {
 625                        id: message.id,
 626                        role: message.role,
 627                        segments: message
 628                            .segments
 629                            .iter()
 630                            .map(|segment| match segment {
 631                                MessageSegment::Text(text) => {
 632                                    SerializedMessageSegment::Text { text: text.clone() }
 633                                }
 634                                MessageSegment::Thinking(text) => {
 635                                    SerializedMessageSegment::Thinking { text: text.clone() }
 636                                }
 637                            })
 638                            .collect(),
 639                        tool_uses: this
 640                            .tool_uses_for_message(message.id, cx)
 641                            .into_iter()
 642                            .map(|tool_use| SerializedToolUse {
 643                                id: tool_use.id,
 644                                name: tool_use.name,
 645                                input: tool_use.input,
 646                            })
 647                            .collect(),
 648                        tool_results: this
 649                            .tool_results_for_message(message.id)
 650                            .into_iter()
 651                            .map(|tool_result| SerializedToolResult {
 652                                tool_use_id: tool_result.tool_use_id.clone(),
 653                                is_error: tool_result.is_error,
 654                                content: tool_result.content.clone(),
 655                            })
 656                            .collect(),
 657                    })
 658                    .collect(),
 659                initial_project_snapshot,
 660                cumulative_token_usage: this.cumulative_token_usage.clone(),
 661            })
 662        })
 663    }
 664
 665    pub fn set_system_prompt_context(&mut self, context: AssistantSystemPromptContext) {
 666        self.system_prompt_context = Some(context);
 667    }
 668
 669    pub fn system_prompt_context(&self) -> &Option<AssistantSystemPromptContext> {
 670        &self.system_prompt_context
 671    }
 672
 673    pub fn load_system_prompt_context(
 674        &self,
 675        cx: &App,
 676    ) -> Task<(AssistantSystemPromptContext, Option<ThreadError>)> {
 677        let project = self.project.read(cx);
 678        let tasks = project
 679            .visible_worktrees(cx)
 680            .map(|worktree| {
 681                Self::load_worktree_info_for_system_prompt(
 682                    project.fs().clone(),
 683                    worktree.read(cx),
 684                    cx,
 685                )
 686            })
 687            .collect::<Vec<_>>();
 688
 689        cx.spawn(async |_cx| {
 690            let results = futures::future::join_all(tasks).await;
 691            let mut first_err = None;
 692            let worktrees = results
 693                .into_iter()
 694                .map(|(worktree, err)| {
 695                    if first_err.is_none() && err.is_some() {
 696                        first_err = err;
 697                    }
 698                    worktree
 699                })
 700                .collect::<Vec<_>>();
 701            (AssistantSystemPromptContext::new(worktrees), first_err)
 702        })
 703    }
 704
 705    fn load_worktree_info_for_system_prompt(
 706        fs: Arc<dyn Fs>,
 707        worktree: &Worktree,
 708        cx: &App,
 709    ) -> Task<(WorktreeInfoForSystemPrompt, Option<ThreadError>)> {
 710        let root_name = worktree.root_name().into();
 711        let abs_path = worktree.abs_path();
 712
 713        // Note that Cline supports `.clinerules` being a directory, but that is not currently
 714        // supported. This doesn't seem to occur often in GitHub repositories.
 715        const RULES_FILE_NAMES: [&'static str; 6] = [
 716            ".rules",
 717            ".cursorrules",
 718            ".windsurfrules",
 719            ".clinerules",
 720            ".github/copilot-instructions.md",
 721            "CLAUDE.md",
 722        ];
 723        let selected_rules_file = RULES_FILE_NAMES
 724            .into_iter()
 725            .filter_map(|name| {
 726                worktree
 727                    .entry_for_path(name)
 728                    .filter(|entry| entry.is_file())
 729                    .map(|entry| (entry.path.clone(), worktree.absolutize(&entry.path)))
 730            })
 731            .next();
 732
 733        if let Some((rel_rules_path, abs_rules_path)) = selected_rules_file {
 734            cx.spawn(async move |_| {
 735                let rules_file_result = maybe!(async move {
 736                    let abs_rules_path = abs_rules_path?;
 737                    let text = fs.load(&abs_rules_path).await.with_context(|| {
 738                        format!("Failed to load assistant rules file {:?}", abs_rules_path)
 739                    })?;
 740                    anyhow::Ok(RulesFile {
 741                        rel_path: rel_rules_path,
 742                        abs_path: abs_rules_path.into(),
 743                        text: text.trim().to_string(),
 744                    })
 745                })
 746                .await;
 747                let (rules_file, rules_file_error) = match rules_file_result {
 748                    Ok(rules_file) => (Some(rules_file), None),
 749                    Err(err) => (
 750                        None,
 751                        Some(ThreadError::Message {
 752                            header: "Error loading rules file".into(),
 753                            message: format!("{err}").into(),
 754                        }),
 755                    ),
 756                };
 757                let worktree_info = WorktreeInfoForSystemPrompt {
 758                    root_name,
 759                    abs_path,
 760                    rules_file,
 761                };
 762                (worktree_info, rules_file_error)
 763            })
 764        } else {
 765            Task::ready((
 766                WorktreeInfoForSystemPrompt {
 767                    root_name,
 768                    abs_path,
 769                    rules_file: None,
 770                },
 771                None,
 772            ))
 773        }
 774    }
 775
 776    pub fn send_to_model(
 777        &mut self,
 778        model: Arc<dyn LanguageModel>,
 779        request_kind: RequestKind,
 780        cx: &mut Context<Self>,
 781    ) {
 782        let mut request = self.to_completion_request(request_kind, cx);
 783        if model.supports_tools() {
 784            request.tools = {
 785                let mut tools = Vec::new();
 786                tools.extend(self.tools().enabled_tools(cx).into_iter().map(|tool| {
 787                    LanguageModelRequestTool {
 788                        name: tool.name(),
 789                        description: tool.description(),
 790                        input_schema: tool.input_schema(model.tool_input_format()),
 791                    }
 792                }));
 793
 794                tools
 795            };
 796        }
 797
 798        self.stream_completion(request, model, cx);
 799    }
 800
 801    pub fn used_tools_since_last_user_message(&self) -> bool {
 802        for message in self.messages.iter().rev() {
 803            if self.tool_use.message_has_tool_results(message.id) {
 804                return true;
 805            } else if message.role == Role::User {
 806                return false;
 807            }
 808        }
 809
 810        false
 811    }
 812
 813    pub fn to_completion_request(
 814        &self,
 815        request_kind: RequestKind,
 816        cx: &App,
 817    ) -> LanguageModelRequest {
 818        let mut request = LanguageModelRequest {
 819            messages: vec![],
 820            tools: Vec::new(),
 821            stop: Vec::new(),
 822            temperature: None,
 823        };
 824
 825        if let Some(system_prompt_context) = self.system_prompt_context.as_ref() {
 826            if let Some(system_prompt) = self
 827                .prompt_builder
 828                .generate_assistant_system_prompt(system_prompt_context)
 829                .context("failed to generate assistant system prompt")
 830                .log_err()
 831            {
 832                request.messages.push(LanguageModelRequestMessage {
 833                    role: Role::System,
 834                    content: vec![MessageContent::Text(system_prompt)],
 835                    cache: true,
 836                });
 837            }
 838        } else {
 839            log::error!("system_prompt_context not set.")
 840        }
 841
 842        let mut referenced_context_ids = HashSet::default();
 843
 844        for message in &self.messages {
 845            if let Some(context_ids) = self.context_by_message.get(&message.id) {
 846                referenced_context_ids.extend(context_ids);
 847            }
 848
 849            let mut request_message = LanguageModelRequestMessage {
 850                role: message.role,
 851                content: Vec::new(),
 852                cache: false,
 853            };
 854
 855            match request_kind {
 856                RequestKind::Chat => {
 857                    self.tool_use
 858                        .attach_tool_results(message.id, &mut request_message);
 859                }
 860                RequestKind::Summarize => {
 861                    // We don't care about tool use during summarization.
 862                    if self.tool_use.message_has_tool_results(message.id) {
 863                        continue;
 864                    }
 865                }
 866            }
 867
 868            if !message.segments.is_empty() {
 869                request_message
 870                    .content
 871                    .push(MessageContent::Text(message.to_string()));
 872            }
 873
 874            match request_kind {
 875                RequestKind::Chat => {
 876                    self.tool_use
 877                        .attach_tool_uses(message.id, &mut request_message);
 878                }
 879                RequestKind::Summarize => {
 880                    // We don't care about tool use during summarization.
 881                }
 882            };
 883
 884            request.messages.push(request_message);
 885        }
 886
 887        // Set a cache breakpoint at the second-to-last message.
 888        // https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching
 889        let breakpoint_index = request.messages.len() - 2;
 890        for (index, message) in request.messages.iter_mut().enumerate() {
 891            message.cache = index == breakpoint_index;
 892        }
 893
 894        if !referenced_context_ids.is_empty() {
 895            let mut context_message = LanguageModelRequestMessage {
 896                role: Role::User,
 897                content: Vec::new(),
 898                cache: false,
 899            };
 900
 901            let referenced_context = referenced_context_ids
 902                .into_iter()
 903                .filter_map(|context_id| self.context.get(context_id));
 904            attach_context_to_message(&mut context_message, referenced_context, cx);
 905
 906            request.messages.push(context_message);
 907        }
 908
 909        self.attached_tracked_files_state(&mut request.messages, cx);
 910
 911        request
 912    }
 913
 914    fn attached_tracked_files_state(
 915        &self,
 916        messages: &mut Vec<LanguageModelRequestMessage>,
 917        cx: &App,
 918    ) {
 919        const STALE_FILES_HEADER: &str = "These files changed since last read:";
 920
 921        let mut stale_message = String::new();
 922
 923        let action_log = self.action_log.read(cx);
 924
 925        for stale_file in action_log.stale_buffers(cx) {
 926            let Some(file) = stale_file.read(cx).file() else {
 927                continue;
 928            };
 929
 930            if stale_message.is_empty() {
 931                write!(&mut stale_message, "{}", STALE_FILES_HEADER).ok();
 932            }
 933
 934            writeln!(&mut stale_message, "- {}", file.path().display()).ok();
 935        }
 936
 937        let mut content = Vec::with_capacity(2);
 938
 939        if !stale_message.is_empty() {
 940            content.push(stale_message.into());
 941        }
 942
 943        if action_log.has_edited_files_since_project_diagnostics_check() {
 944            content.push(
 945                "\n\nWhen you're done making changes, make sure to check project diagnostics \
 946                and fix all errors AND warnings you introduced! \
 947                DO NOT mention you're going to do this until you're done."
 948                    .into(),
 949            );
 950        }
 951
 952        if !content.is_empty() {
 953            let context_message = LanguageModelRequestMessage {
 954                role: Role::User,
 955                content,
 956                cache: false,
 957            };
 958
 959            messages.push(context_message);
 960        }
 961    }
 962
 963    pub fn stream_completion(
 964        &mut self,
 965        request: LanguageModelRequest,
 966        model: Arc<dyn LanguageModel>,
 967        cx: &mut Context<Self>,
 968    ) {
 969        let pending_completion_id = post_inc(&mut self.completion_count);
 970
 971        let task = cx.spawn(async move |thread, cx| {
 972            let stream = model.stream_completion(request, &cx);
 973            let initial_token_usage =
 974                thread.read_with(cx, |thread, _cx| thread.cumulative_token_usage.clone());
 975            let stream_completion = async {
 976                let mut events = stream.await?;
 977                let mut stop_reason = StopReason::EndTurn;
 978                let mut current_token_usage = TokenUsage::default();
 979
 980                while let Some(event) = events.next().await {
 981                    let event = event?;
 982
 983                    thread.update(cx, |thread, cx| {
 984                        match event {
 985                            LanguageModelCompletionEvent::StartMessage { .. } => {
 986                                thread.insert_message(
 987                                    Role::Assistant,
 988                                    vec![MessageSegment::Text(String::new())],
 989                                    cx,
 990                                );
 991                            }
 992                            LanguageModelCompletionEvent::Stop(reason) => {
 993                                stop_reason = reason;
 994                            }
 995                            LanguageModelCompletionEvent::UsageUpdate(token_usage) => {
 996                                thread.cumulative_token_usage =
 997                                    thread.cumulative_token_usage.clone() + token_usage.clone()
 998                                        - current_token_usage.clone();
 999                                current_token_usage = token_usage;
1000                            }
1001                            LanguageModelCompletionEvent::Text(chunk) => {
1002                                if let Some(last_message) = thread.messages.last_mut() {
1003                                    if last_message.role == Role::Assistant {
1004                                        last_message.push_text(&chunk);
1005                                        cx.emit(ThreadEvent::StreamedAssistantText(
1006                                            last_message.id,
1007                                            chunk,
1008                                        ));
1009                                    } else {
1010                                        // If we won't have an Assistant message yet, assume this chunk marks the beginning
1011                                        // of a new Assistant response.
1012                                        //
1013                                        // Importantly: We do *not* want to emit a `StreamedAssistantText` event here, as it
1014                                        // will result in duplicating the text of the chunk in the rendered Markdown.
1015                                        thread.insert_message(
1016                                            Role::Assistant,
1017                                            vec![MessageSegment::Text(chunk.to_string())],
1018                                            cx,
1019                                        );
1020                                    };
1021                                }
1022                            }
1023                            LanguageModelCompletionEvent::Thinking(chunk) => {
1024                                if let Some(last_message) = thread.messages.last_mut() {
1025                                    if last_message.role == Role::Assistant {
1026                                        last_message.push_thinking(&chunk);
1027                                        cx.emit(ThreadEvent::StreamedAssistantThinking(
1028                                            last_message.id,
1029                                            chunk,
1030                                        ));
1031                                    } else {
1032                                        // If we won't have an Assistant message yet, assume this chunk marks the beginning
1033                                        // of a new Assistant response.
1034                                        //
1035                                        // Importantly: We do *not* want to emit a `StreamedAssistantText` event here, as it
1036                                        // will result in duplicating the text of the chunk in the rendered Markdown.
1037                                        thread.insert_message(
1038                                            Role::Assistant,
1039                                            vec![MessageSegment::Thinking(chunk.to_string())],
1040                                            cx,
1041                                        );
1042                                    };
1043                                }
1044                            }
1045                            LanguageModelCompletionEvent::ToolUse(tool_use) => {
1046                                let last_assistant_message_id = thread
1047                                    .messages
1048                                    .iter()
1049                                    .rfind(|message| message.role == Role::Assistant)
1050                                    .map(|message| message.id)
1051                                    .unwrap_or_else(|| {
1052                                        thread.insert_message(
1053                                            Role::Assistant,
1054                                            vec![MessageSegment::Text("Using tool...".to_string())],
1055                                            cx,
1056                                        )
1057                                    });
1058                                thread.tool_use.request_tool_use(
1059                                    last_assistant_message_id,
1060                                    tool_use,
1061                                    cx,
1062                                );
1063                            }
1064                        }
1065
1066                        thread.touch_updated_at();
1067                        cx.emit(ThreadEvent::StreamedCompletion);
1068                        cx.notify();
1069                    })?;
1070
1071                    smol::future::yield_now().await;
1072                }
1073
1074                thread.update(cx, |thread, cx| {
1075                    thread
1076                        .pending_completions
1077                        .retain(|completion| completion.id != pending_completion_id);
1078
1079                    if thread.summary.is_none() && thread.messages.len() >= 2 {
1080                        thread.summarize(cx);
1081                    }
1082                })?;
1083
1084                anyhow::Ok(stop_reason)
1085            };
1086
1087            let result = stream_completion.await;
1088
1089            thread
1090                .update(cx, |thread, cx| {
1091                    thread.finalize_pending_checkpoint(cx);
1092                    match result.as_ref() {
1093                        Ok(stop_reason) => match stop_reason {
1094                            StopReason::ToolUse => {
1095                                cx.emit(ThreadEvent::UsePendingTools);
1096                            }
1097                            StopReason::EndTurn => {}
1098                            StopReason::MaxTokens => {}
1099                        },
1100                        Err(error) => {
1101                            if error.is::<PaymentRequiredError>() {
1102                                cx.emit(ThreadEvent::ShowError(ThreadError::PaymentRequired));
1103                            } else if error.is::<MaxMonthlySpendReachedError>() {
1104                                cx.emit(ThreadEvent::ShowError(
1105                                    ThreadError::MaxMonthlySpendReached,
1106                                ));
1107                            } else {
1108                                let error_message = error
1109                                    .chain()
1110                                    .map(|err| err.to_string())
1111                                    .collect::<Vec<_>>()
1112                                    .join("\n");
1113                                cx.emit(ThreadEvent::ShowError(ThreadError::Message {
1114                                    header: "Error interacting with language model".into(),
1115                                    message: SharedString::from(error_message.clone()),
1116                                }));
1117                            }
1118
1119                            thread.cancel_last_completion(cx);
1120                        }
1121                    }
1122                    cx.emit(ThreadEvent::DoneStreaming);
1123
1124                    if let Ok(initial_usage) = initial_token_usage {
1125                        let usage = thread.cumulative_token_usage.clone() - initial_usage;
1126
1127                        telemetry::event!(
1128                            "Assistant Thread Completion",
1129                            thread_id = thread.id().to_string(),
1130                            model = model.telemetry_id(),
1131                            model_provider = model.provider_id().to_string(),
1132                            input_tokens = usage.input_tokens,
1133                            output_tokens = usage.output_tokens,
1134                            cache_creation_input_tokens = usage.cache_creation_input_tokens,
1135                            cache_read_input_tokens = usage.cache_read_input_tokens,
1136                        );
1137                    }
1138                })
1139                .ok();
1140        });
1141
1142        self.pending_completions.push(PendingCompletion {
1143            id: pending_completion_id,
1144            _task: task,
1145        });
1146    }
1147
1148    pub fn summarize(&mut self, cx: &mut Context<Self>) {
1149        let Some(provider) = LanguageModelRegistry::read_global(cx).active_provider() else {
1150            return;
1151        };
1152        let Some(model) = LanguageModelRegistry::read_global(cx).active_model() else {
1153            return;
1154        };
1155
1156        if !provider.is_authenticated(cx) {
1157            return;
1158        }
1159
1160        let mut request = self.to_completion_request(RequestKind::Summarize, cx);
1161        request.messages.push(LanguageModelRequestMessage {
1162            role: Role::User,
1163            content: vec![
1164                "Generate a concise 3-7 word title for this conversation, omitting punctuation. \
1165                 Go straight to the title, without any preamble and prefix like `Here's a concise suggestion:...` or `Title:`. \
1166                 If the conversation is about a specific subject, include it in the title. \
1167                 Be descriptive. DO NOT speak in the first person."
1168                    .into(),
1169            ],
1170            cache: false,
1171        });
1172
1173        self.pending_summary = cx.spawn(async move |this, cx| {
1174            async move {
1175                let stream = model.stream_completion_text(request, &cx);
1176                let mut messages = stream.await?;
1177
1178                let mut new_summary = String::new();
1179                while let Some(message) = messages.stream.next().await {
1180                    let text = message?;
1181                    let mut lines = text.lines();
1182                    new_summary.extend(lines.next());
1183
1184                    // Stop if the LLM generated multiple lines.
1185                    if lines.next().is_some() {
1186                        break;
1187                    }
1188                }
1189
1190                this.update(cx, |this, cx| {
1191                    if !new_summary.is_empty() {
1192                        this.summary = Some(new_summary.into());
1193                    }
1194
1195                    cx.emit(ThreadEvent::SummaryChanged);
1196                })?;
1197
1198                anyhow::Ok(())
1199            }
1200            .log_err()
1201            .await
1202        });
1203    }
1204
1205    pub fn use_pending_tools(
1206        &mut self,
1207        cx: &mut Context<Self>,
1208    ) -> impl IntoIterator<Item = PendingToolUse> + use<> {
1209        let request = self.to_completion_request(RequestKind::Chat, cx);
1210        let messages = Arc::new(request.messages);
1211        let pending_tool_uses = self
1212            .tool_use
1213            .pending_tool_uses()
1214            .into_iter()
1215            .filter(|tool_use| tool_use.status.is_idle())
1216            .cloned()
1217            .collect::<Vec<_>>();
1218
1219        for tool_use in pending_tool_uses.iter() {
1220            if let Some(tool) = self.tools.tool(&tool_use.name, cx) {
1221                if tool.needs_confirmation()
1222                    && !AssistantSettings::get_global(cx).always_allow_tool_actions
1223                {
1224                    self.tool_use.confirm_tool_use(
1225                        tool_use.id.clone(),
1226                        tool_use.ui_text.clone(),
1227                        tool_use.input.clone(),
1228                        messages.clone(),
1229                        tool,
1230                    );
1231                    cx.emit(ThreadEvent::ToolConfirmationNeeded);
1232                } else {
1233                    self.run_tool(
1234                        tool_use.id.clone(),
1235                        tool_use.ui_text.clone(),
1236                        tool_use.input.clone(),
1237                        &messages,
1238                        tool,
1239                        cx,
1240                    );
1241                }
1242            } else if let Some(tool) = self.tools.tool(&tool_use.name, cx) {
1243                self.run_tool(
1244                    tool_use.id.clone(),
1245                    tool_use.ui_text.clone(),
1246                    tool_use.input.clone(),
1247                    &messages,
1248                    tool,
1249                    cx,
1250                );
1251            }
1252        }
1253
1254        pending_tool_uses
1255    }
1256
1257    pub fn run_tool(
1258        &mut self,
1259        tool_use_id: LanguageModelToolUseId,
1260        ui_text: impl Into<SharedString>,
1261        input: serde_json::Value,
1262        messages: &[LanguageModelRequestMessage],
1263        tool: Arc<dyn Tool>,
1264        cx: &mut Context<Thread>,
1265    ) {
1266        let task = self.spawn_tool_use(tool_use_id.clone(), messages, input, tool, cx);
1267        self.tool_use
1268            .run_pending_tool(tool_use_id, ui_text.into(), task);
1269    }
1270
1271    fn spawn_tool_use(
1272        &mut self,
1273        tool_use_id: LanguageModelToolUseId,
1274        messages: &[LanguageModelRequestMessage],
1275        input: serde_json::Value,
1276        tool: Arc<dyn Tool>,
1277        cx: &mut Context<Thread>,
1278    ) -> Task<()> {
1279        let tool_name: Arc<str> = tool.name().into();
1280        let run_tool = tool.run(
1281            input,
1282            messages,
1283            self.project.clone(),
1284            self.action_log.clone(),
1285            cx,
1286        );
1287
1288        cx.spawn({
1289            async move |thread: WeakEntity<Thread>, cx| {
1290                let output = run_tool.await;
1291
1292                thread
1293                    .update(cx, |thread, cx| {
1294                        let pending_tool_use = thread.tool_use.insert_tool_output(
1295                            tool_use_id.clone(),
1296                            tool_name,
1297                            output,
1298                        );
1299
1300                        cx.emit(ThreadEvent::ToolFinished {
1301                            tool_use_id,
1302                            pending_tool_use,
1303                            canceled: false,
1304                        });
1305                    })
1306                    .ok();
1307            }
1308        })
1309    }
1310
1311    pub fn attach_tool_results(
1312        &mut self,
1313        updated_context: Vec<AssistantContext>,
1314        cx: &mut Context<Self>,
1315    ) {
1316        self.context.extend(
1317            updated_context
1318                .into_iter()
1319                .map(|context| (context.id(), context)),
1320        );
1321
1322        // Insert a user message to contain the tool results.
1323        self.insert_user_message(
1324            // TODO: Sending up a user message without any content results in the model sending back
1325            // responses that also don't have any content. We currently don't handle this case well,
1326            // so for now we provide some text to keep the model on track.
1327            "Here are the tool results.",
1328            Vec::new(),
1329            None,
1330            cx,
1331        );
1332    }
1333
1334    /// Cancels the last pending completion, if there are any pending.
1335    ///
1336    /// Returns whether a completion was canceled.
1337    pub fn cancel_last_completion(&mut self, cx: &mut Context<Self>) -> bool {
1338        let canceled = if self.pending_completions.pop().is_some() {
1339            true
1340        } else {
1341            let mut canceled = false;
1342            for pending_tool_use in self.tool_use.cancel_pending() {
1343                canceled = true;
1344                cx.emit(ThreadEvent::ToolFinished {
1345                    tool_use_id: pending_tool_use.id.clone(),
1346                    pending_tool_use: Some(pending_tool_use),
1347                    canceled: true,
1348                });
1349            }
1350            canceled
1351        };
1352        self.finalize_pending_checkpoint(cx);
1353        canceled
1354    }
1355
1356    /// Returns the feedback given to the thread, if any.
1357    pub fn feedback(&self) -> Option<ThreadFeedback> {
1358        self.feedback
1359    }
1360
1361    /// Reports feedback about the thread and stores it in our telemetry backend.
1362    pub fn report_feedback(
1363        &mut self,
1364        feedback: ThreadFeedback,
1365        cx: &mut Context<Self>,
1366    ) -> Task<Result<()>> {
1367        let final_project_snapshot = Self::project_snapshot(self.project.clone(), cx);
1368        let serialized_thread = self.serialize(cx);
1369        let thread_id = self.id().clone();
1370        let client = self.project.read(cx).client();
1371        self.feedback = Some(feedback);
1372        cx.notify();
1373
1374        cx.background_spawn(async move {
1375            let final_project_snapshot = final_project_snapshot.await;
1376            let serialized_thread = serialized_thread.await?;
1377            let thread_data =
1378                serde_json::to_value(serialized_thread).unwrap_or_else(|_| serde_json::Value::Null);
1379
1380            let rating = match feedback {
1381                ThreadFeedback::Positive => "positive",
1382                ThreadFeedback::Negative => "negative",
1383            };
1384            telemetry::event!(
1385                "Assistant Thread Rated",
1386                rating,
1387                thread_id,
1388                thread_data,
1389                final_project_snapshot
1390            );
1391            client.telemetry().flush_events();
1392
1393            Ok(())
1394        })
1395    }
1396
1397    /// Create a snapshot of the current project state including git information and unsaved buffers.
1398    fn project_snapshot(
1399        project: Entity<Project>,
1400        cx: &mut Context<Self>,
1401    ) -> Task<Arc<ProjectSnapshot>> {
1402        let git_store = project.read(cx).git_store().clone();
1403        let worktree_snapshots: Vec<_> = project
1404            .read(cx)
1405            .visible_worktrees(cx)
1406            .map(|worktree| Self::worktree_snapshot(worktree, git_store.clone(), cx))
1407            .collect();
1408
1409        cx.spawn(async move |_, cx| {
1410            let worktree_snapshots = futures::future::join_all(worktree_snapshots).await;
1411
1412            let mut unsaved_buffers = Vec::new();
1413            cx.update(|app_cx| {
1414                let buffer_store = project.read(app_cx).buffer_store();
1415                for buffer_handle in buffer_store.read(app_cx).buffers() {
1416                    let buffer = buffer_handle.read(app_cx);
1417                    if buffer.is_dirty() {
1418                        if let Some(file) = buffer.file() {
1419                            let path = file.path().to_string_lossy().to_string();
1420                            unsaved_buffers.push(path);
1421                        }
1422                    }
1423                }
1424            })
1425            .ok();
1426
1427            Arc::new(ProjectSnapshot {
1428                worktree_snapshots,
1429                unsaved_buffer_paths: unsaved_buffers,
1430                timestamp: Utc::now(),
1431            })
1432        })
1433    }
1434
1435    fn worktree_snapshot(
1436        worktree: Entity<project::Worktree>,
1437        git_store: Entity<GitStore>,
1438        cx: &App,
1439    ) -> Task<WorktreeSnapshot> {
1440        cx.spawn(async move |cx| {
1441            // Get worktree path and snapshot
1442            let worktree_info = cx.update(|app_cx| {
1443                let worktree = worktree.read(app_cx);
1444                let path = worktree.abs_path().to_string_lossy().to_string();
1445                let snapshot = worktree.snapshot();
1446                (path, snapshot)
1447            });
1448
1449            let Ok((worktree_path, snapshot)) = worktree_info else {
1450                return WorktreeSnapshot {
1451                    worktree_path: String::new(),
1452                    git_state: None,
1453                };
1454            };
1455
1456            let repo_info = git_store
1457                .update(cx, |git_store, cx| {
1458                    git_store
1459                        .repositories()
1460                        .values()
1461                        .find(|repo| repo.read(cx).worktree_id == Some(snapshot.id()))
1462                        .and_then(|repo| {
1463                            let repo = repo.read(cx);
1464                            Some((repo.branch().cloned(), repo.local_repository()?))
1465                        })
1466                })
1467                .ok()
1468                .flatten();
1469
1470            // Extract git information
1471            let git_state = match repo_info {
1472                None => None,
1473                Some((branch, repo)) => {
1474                    let current_branch = branch.map(|branch| branch.name.to_string());
1475                    let remote_url = repo.remote_url("origin");
1476                    let head_sha = repo.head_sha();
1477
1478                    // Get diff asynchronously
1479                    let diff = repo
1480                        .diff(git::repository::DiffType::HeadToWorktree)
1481                        .await
1482                        .ok();
1483
1484                    Some(GitState {
1485                        remote_url,
1486                        head_sha,
1487                        current_branch,
1488                        diff,
1489                    })
1490                }
1491            };
1492
1493            WorktreeSnapshot {
1494                worktree_path,
1495                git_state,
1496            }
1497        })
1498    }
1499
1500    pub fn to_markdown(&self, cx: &App) -> Result<String> {
1501        let mut markdown = Vec::new();
1502
1503        if let Some(summary) = self.summary() {
1504            writeln!(markdown, "# {summary}\n")?;
1505        };
1506
1507        for message in self.messages() {
1508            writeln!(
1509                markdown,
1510                "## {role}\n",
1511                role = match message.role {
1512                    Role::User => "User",
1513                    Role::Assistant => "Assistant",
1514                    Role::System => "System",
1515                }
1516            )?;
1517            for segment in &message.segments {
1518                match segment {
1519                    MessageSegment::Text(text) => writeln!(markdown, "{}\n", text)?,
1520                    MessageSegment::Thinking(text) => {
1521                        writeln!(markdown, "<think>{}</think>\n", text)?
1522                    }
1523                }
1524            }
1525
1526            for tool_use in self.tool_uses_for_message(message.id, cx) {
1527                writeln!(
1528                    markdown,
1529                    "**Use Tool: {} ({})**",
1530                    tool_use.name, tool_use.id
1531                )?;
1532                writeln!(markdown, "```json")?;
1533                writeln!(
1534                    markdown,
1535                    "{}",
1536                    serde_json::to_string_pretty(&tool_use.input)?
1537                )?;
1538                writeln!(markdown, "```")?;
1539            }
1540
1541            for tool_result in self.tool_results_for_message(message.id) {
1542                write!(markdown, "**Tool Results: {}", tool_result.tool_use_id)?;
1543                if tool_result.is_error {
1544                    write!(markdown, " (Error)")?;
1545                }
1546
1547                writeln!(markdown, "**\n")?;
1548                writeln!(markdown, "{}", tool_result.content)?;
1549            }
1550        }
1551
1552        Ok(String::from_utf8_lossy(&markdown).to_string())
1553    }
1554
1555    pub fn review_edits_in_range(
1556        &mut self,
1557        buffer: Entity<language::Buffer>,
1558        buffer_range: Range<language::Anchor>,
1559        accept: bool,
1560        cx: &mut Context<Self>,
1561    ) {
1562        self.action_log.update(cx, |action_log, cx| {
1563            action_log.review_edits_in_range(buffer, buffer_range, accept, cx)
1564        });
1565    }
1566
1567    /// Keeps all edits across all buffers at once.
1568    /// This provides a more performant alternative to calling review_edits_in_range for each buffer.
1569    pub fn keep_all_edits(&mut self, cx: &mut Context<Self>) {
1570        self.action_log
1571            .update(cx, |action_log, _cx| action_log.keep_all_edits());
1572    }
1573
1574    pub fn action_log(&self) -> &Entity<ActionLog> {
1575        &self.action_log
1576    }
1577
1578    pub fn project(&self) -> &Entity<Project> {
1579        &self.project
1580    }
1581
1582    pub fn cumulative_token_usage(&self) -> TokenUsage {
1583        self.cumulative_token_usage.clone()
1584    }
1585
1586    pub fn deny_tool_use(
1587        &mut self,
1588        tool_use_id: LanguageModelToolUseId,
1589        tool_name: Arc<str>,
1590        cx: &mut Context<Self>,
1591    ) {
1592        let err = Err(anyhow::anyhow!(
1593            "Permission to run tool action denied by user"
1594        ));
1595
1596        self.tool_use
1597            .insert_tool_output(tool_use_id.clone(), tool_name, err);
1598
1599        cx.emit(ThreadEvent::ToolFinished {
1600            tool_use_id,
1601            pending_tool_use: None,
1602            canceled: true,
1603        });
1604    }
1605}
1606
1607#[derive(Debug, Clone)]
1608pub enum ThreadError {
1609    PaymentRequired,
1610    MaxMonthlySpendReached,
1611    Message {
1612        header: SharedString,
1613        message: SharedString,
1614    },
1615}
1616
1617#[derive(Debug, Clone)]
1618pub enum ThreadEvent {
1619    ShowError(ThreadError),
1620    StreamedCompletion,
1621    StreamedAssistantText(MessageId, String),
1622    StreamedAssistantThinking(MessageId, String),
1623    DoneStreaming,
1624    MessageAdded(MessageId),
1625    MessageEdited(MessageId),
1626    MessageDeleted(MessageId),
1627    SummaryChanged,
1628    UsePendingTools,
1629    ToolFinished {
1630        #[allow(unused)]
1631        tool_use_id: LanguageModelToolUseId,
1632        /// The pending tool use that corresponds to this tool.
1633        pending_tool_use: Option<PendingToolUse>,
1634        /// Whether the tool was canceled by the user.
1635        canceled: bool,
1636    },
1637    CheckpointChanged,
1638    ToolConfirmationNeeded,
1639}
1640
1641impl EventEmitter<ThreadEvent> for Thread {}
1642
1643struct PendingCompletion {
1644    id: usize,
1645    _task: Task<()>,
1646}