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        request.tools = {
 784            let mut tools = Vec::new();
 785            tools.extend(self.tools().enabled_tools(cx).into_iter().map(|tool| {
 786                LanguageModelRequestTool {
 787                    name: tool.name(),
 788                    description: tool.description(),
 789                    input_schema: tool.input_schema(model.tool_input_format()),
 790                }
 791            }));
 792
 793            tools
 794        };
 795
 796        self.stream_completion(request, model, cx);
 797    }
 798
 799    pub fn used_tools_since_last_user_message(&self) -> bool {
 800        for message in self.messages.iter().rev() {
 801            if self.tool_use.message_has_tool_results(message.id) {
 802                return true;
 803            } else if message.role == Role::User {
 804                return false;
 805            }
 806        }
 807
 808        false
 809    }
 810
 811    pub fn to_completion_request(
 812        &self,
 813        request_kind: RequestKind,
 814        cx: &App,
 815    ) -> LanguageModelRequest {
 816        let mut request = LanguageModelRequest {
 817            messages: vec![],
 818            tools: Vec::new(),
 819            stop: Vec::new(),
 820            temperature: None,
 821        };
 822
 823        if let Some(system_prompt_context) = self.system_prompt_context.as_ref() {
 824            if let Some(system_prompt) = self
 825                .prompt_builder
 826                .generate_assistant_system_prompt(system_prompt_context)
 827                .context("failed to generate assistant system prompt")
 828                .log_err()
 829            {
 830                request.messages.push(LanguageModelRequestMessage {
 831                    role: Role::System,
 832                    content: vec![MessageContent::Text(system_prompt)],
 833                    cache: true,
 834                });
 835            }
 836        } else {
 837            log::error!("system_prompt_context not set.")
 838        }
 839
 840        let mut referenced_context_ids = HashSet::default();
 841
 842        for message in &self.messages {
 843            if let Some(context_ids) = self.context_by_message.get(&message.id) {
 844                referenced_context_ids.extend(context_ids);
 845            }
 846
 847            let mut request_message = LanguageModelRequestMessage {
 848                role: message.role,
 849                content: Vec::new(),
 850                cache: false,
 851            };
 852
 853            match request_kind {
 854                RequestKind::Chat => {
 855                    self.tool_use
 856                        .attach_tool_results(message.id, &mut request_message);
 857                }
 858                RequestKind::Summarize => {
 859                    // We don't care about tool use during summarization.
 860                    if self.tool_use.message_has_tool_results(message.id) {
 861                        continue;
 862                    }
 863                }
 864            }
 865
 866            if !message.segments.is_empty() {
 867                request_message
 868                    .content
 869                    .push(MessageContent::Text(message.to_string()));
 870            }
 871
 872            match request_kind {
 873                RequestKind::Chat => {
 874                    self.tool_use
 875                        .attach_tool_uses(message.id, &mut request_message);
 876                }
 877                RequestKind::Summarize => {
 878                    // We don't care about tool use during summarization.
 879                }
 880            };
 881
 882            request.messages.push(request_message);
 883        }
 884
 885        // Set a cache breakpoint at the second-to-last message.
 886        // https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching
 887        let breakpoint_index = request.messages.len() - 2;
 888        for (index, message) in request.messages.iter_mut().enumerate() {
 889            message.cache = index == breakpoint_index;
 890        }
 891
 892        if !referenced_context_ids.is_empty() {
 893            let mut context_message = LanguageModelRequestMessage {
 894                role: Role::User,
 895                content: Vec::new(),
 896                cache: false,
 897            };
 898
 899            let referenced_context = referenced_context_ids
 900                .into_iter()
 901                .filter_map(|context_id| self.context.get(context_id));
 902            attach_context_to_message(&mut context_message, referenced_context, cx);
 903
 904            request.messages.push(context_message);
 905        }
 906
 907        self.attached_tracked_files_state(&mut request.messages, cx);
 908
 909        request
 910    }
 911
 912    fn attached_tracked_files_state(
 913        &self,
 914        messages: &mut Vec<LanguageModelRequestMessage>,
 915        cx: &App,
 916    ) {
 917        const STALE_FILES_HEADER: &str = "These files changed since last read:";
 918
 919        let mut stale_message = String::new();
 920
 921        let action_log = self.action_log.read(cx);
 922
 923        for stale_file in action_log.stale_buffers(cx) {
 924            let Some(file) = stale_file.read(cx).file() else {
 925                continue;
 926            };
 927
 928            if stale_message.is_empty() {
 929                write!(&mut stale_message, "{}", STALE_FILES_HEADER).ok();
 930            }
 931
 932            writeln!(&mut stale_message, "- {}", file.path().display()).ok();
 933        }
 934
 935        let mut content = Vec::with_capacity(2);
 936
 937        if !stale_message.is_empty() {
 938            content.push(stale_message.into());
 939        }
 940
 941        if action_log.has_edited_files_since_project_diagnostics_check() {
 942            content.push(
 943                "\n\nWhen you're done making changes, make sure to check project diagnostics \
 944                and fix all errors AND warnings you introduced! \
 945                DO NOT mention you're going to do this until you're done."
 946                    .into(),
 947            );
 948        }
 949
 950        if !content.is_empty() {
 951            let context_message = LanguageModelRequestMessage {
 952                role: Role::User,
 953                content,
 954                cache: false,
 955            };
 956
 957            messages.push(context_message);
 958        }
 959    }
 960
 961    pub fn stream_completion(
 962        &mut self,
 963        request: LanguageModelRequest,
 964        model: Arc<dyn LanguageModel>,
 965        cx: &mut Context<Self>,
 966    ) {
 967        let pending_completion_id = post_inc(&mut self.completion_count);
 968
 969        let task = cx.spawn(async move |thread, cx| {
 970            let stream = model.stream_completion(request, &cx);
 971            let initial_token_usage =
 972                thread.read_with(cx, |thread, _cx| thread.cumulative_token_usage.clone());
 973            let stream_completion = async {
 974                let mut events = stream.await?;
 975                let mut stop_reason = StopReason::EndTurn;
 976                let mut current_token_usage = TokenUsage::default();
 977
 978                while let Some(event) = events.next().await {
 979                    let event = event?;
 980
 981                    thread.update(cx, |thread, cx| {
 982                        match event {
 983                            LanguageModelCompletionEvent::StartMessage { .. } => {
 984                                thread.insert_message(
 985                                    Role::Assistant,
 986                                    vec![MessageSegment::Text(String::new())],
 987                                    cx,
 988                                );
 989                            }
 990                            LanguageModelCompletionEvent::Stop(reason) => {
 991                                stop_reason = reason;
 992                            }
 993                            LanguageModelCompletionEvent::UsageUpdate(token_usage) => {
 994                                thread.cumulative_token_usage =
 995                                    thread.cumulative_token_usage.clone() + token_usage.clone()
 996                                        - current_token_usage.clone();
 997                                current_token_usage = token_usage;
 998                            }
 999                            LanguageModelCompletionEvent::Text(chunk) => {
1000                                if let Some(last_message) = thread.messages.last_mut() {
1001                                    if last_message.role == Role::Assistant {
1002                                        last_message.push_text(&chunk);
1003                                        cx.emit(ThreadEvent::StreamedAssistantText(
1004                                            last_message.id,
1005                                            chunk,
1006                                        ));
1007                                    } else {
1008                                        // If we won't have an Assistant message yet, assume this chunk marks the beginning
1009                                        // of a new Assistant response.
1010                                        //
1011                                        // Importantly: We do *not* want to emit a `StreamedAssistantText` event here, as it
1012                                        // will result in duplicating the text of the chunk in the rendered Markdown.
1013                                        thread.insert_message(
1014                                            Role::Assistant,
1015                                            vec![MessageSegment::Text(chunk.to_string())],
1016                                            cx,
1017                                        );
1018                                    };
1019                                }
1020                            }
1021                            LanguageModelCompletionEvent::Thinking(chunk) => {
1022                                if let Some(last_message) = thread.messages.last_mut() {
1023                                    if last_message.role == Role::Assistant {
1024                                        last_message.push_thinking(&chunk);
1025                                        cx.emit(ThreadEvent::StreamedAssistantThinking(
1026                                            last_message.id,
1027                                            chunk,
1028                                        ));
1029                                    } else {
1030                                        // If we won't have an Assistant message yet, assume this chunk marks the beginning
1031                                        // of a new Assistant response.
1032                                        //
1033                                        // Importantly: We do *not* want to emit a `StreamedAssistantText` event here, as it
1034                                        // will result in duplicating the text of the chunk in the rendered Markdown.
1035                                        thread.insert_message(
1036                                            Role::Assistant,
1037                                            vec![MessageSegment::Thinking(chunk.to_string())],
1038                                            cx,
1039                                        );
1040                                    };
1041                                }
1042                            }
1043                            LanguageModelCompletionEvent::ToolUse(tool_use) => {
1044                                let last_assistant_message_id = thread
1045                                    .messages
1046                                    .iter()
1047                                    .rfind(|message| message.role == Role::Assistant)
1048                                    .map(|message| message.id)
1049                                    .unwrap_or_else(|| {
1050                                        thread.insert_message(
1051                                            Role::Assistant,
1052                                            vec![MessageSegment::Text("Using tool...".to_string())],
1053                                            cx,
1054                                        )
1055                                    });
1056                                thread.tool_use.request_tool_use(
1057                                    last_assistant_message_id,
1058                                    tool_use,
1059                                    cx,
1060                                );
1061                            }
1062                        }
1063
1064                        thread.touch_updated_at();
1065                        cx.emit(ThreadEvent::StreamedCompletion);
1066                        cx.notify();
1067                    })?;
1068
1069                    smol::future::yield_now().await;
1070                }
1071
1072                thread.update(cx, |thread, cx| {
1073                    thread
1074                        .pending_completions
1075                        .retain(|completion| completion.id != pending_completion_id);
1076
1077                    if thread.summary.is_none() && thread.messages.len() >= 2 {
1078                        thread.summarize(cx);
1079                    }
1080                })?;
1081
1082                anyhow::Ok(stop_reason)
1083            };
1084
1085            let result = stream_completion.await;
1086
1087            thread
1088                .update(cx, |thread, cx| {
1089                    thread.finalize_pending_checkpoint(cx);
1090                    match result.as_ref() {
1091                        Ok(stop_reason) => match stop_reason {
1092                            StopReason::ToolUse => {
1093                                cx.emit(ThreadEvent::UsePendingTools);
1094                            }
1095                            StopReason::EndTurn => {}
1096                            StopReason::MaxTokens => {}
1097                        },
1098                        Err(error) => {
1099                            if error.is::<PaymentRequiredError>() {
1100                                cx.emit(ThreadEvent::ShowError(ThreadError::PaymentRequired));
1101                            } else if error.is::<MaxMonthlySpendReachedError>() {
1102                                cx.emit(ThreadEvent::ShowError(
1103                                    ThreadError::MaxMonthlySpendReached,
1104                                ));
1105                            } else {
1106                                let error_message = error
1107                                    .chain()
1108                                    .map(|err| err.to_string())
1109                                    .collect::<Vec<_>>()
1110                                    .join("\n");
1111                                cx.emit(ThreadEvent::ShowError(ThreadError::Message {
1112                                    header: "Error interacting with language model".into(),
1113                                    message: SharedString::from(error_message.clone()),
1114                                }));
1115                            }
1116
1117                            thread.cancel_last_completion(cx);
1118                        }
1119                    }
1120                    cx.emit(ThreadEvent::DoneStreaming);
1121
1122                    if let Ok(initial_usage) = initial_token_usage {
1123                        let usage = thread.cumulative_token_usage.clone() - initial_usage;
1124
1125                        telemetry::event!(
1126                            "Assistant Thread Completion",
1127                            thread_id = thread.id().to_string(),
1128                            model = model.telemetry_id(),
1129                            model_provider = model.provider_id().to_string(),
1130                            input_tokens = usage.input_tokens,
1131                            output_tokens = usage.output_tokens,
1132                            cache_creation_input_tokens = usage.cache_creation_input_tokens,
1133                            cache_read_input_tokens = usage.cache_read_input_tokens,
1134                        );
1135                    }
1136                })
1137                .ok();
1138        });
1139
1140        self.pending_completions.push(PendingCompletion {
1141            id: pending_completion_id,
1142            _task: task,
1143        });
1144    }
1145
1146    pub fn summarize(&mut self, cx: &mut Context<Self>) {
1147        let Some(provider) = LanguageModelRegistry::read_global(cx).active_provider() else {
1148            return;
1149        };
1150        let Some(model) = LanguageModelRegistry::read_global(cx).active_model() else {
1151            return;
1152        };
1153
1154        if !provider.is_authenticated(cx) {
1155            return;
1156        }
1157
1158        let mut request = self.to_completion_request(RequestKind::Summarize, cx);
1159        request.messages.push(LanguageModelRequestMessage {
1160            role: Role::User,
1161            content: vec![
1162                "Generate a concise 3-7 word title for this conversation, omitting punctuation. \
1163                 Go straight to the title, without any preamble and prefix like `Here's a concise suggestion:...` or `Title:`. \
1164                 If the conversation is about a specific subject, include it in the title. \
1165                 Be descriptive. DO NOT speak in the first person."
1166                    .into(),
1167            ],
1168            cache: false,
1169        });
1170
1171        self.pending_summary = cx.spawn(async move |this, cx| {
1172            async move {
1173                let stream = model.stream_completion_text(request, &cx);
1174                let mut messages = stream.await?;
1175
1176                let mut new_summary = String::new();
1177                while let Some(message) = messages.stream.next().await {
1178                    let text = message?;
1179                    let mut lines = text.lines();
1180                    new_summary.extend(lines.next());
1181
1182                    // Stop if the LLM generated multiple lines.
1183                    if lines.next().is_some() {
1184                        break;
1185                    }
1186                }
1187
1188                this.update(cx, |this, cx| {
1189                    if !new_summary.is_empty() {
1190                        this.summary = Some(new_summary.into());
1191                    }
1192
1193                    cx.emit(ThreadEvent::SummaryChanged);
1194                })?;
1195
1196                anyhow::Ok(())
1197            }
1198            .log_err()
1199            .await
1200        });
1201    }
1202
1203    pub fn use_pending_tools(
1204        &mut self,
1205        cx: &mut Context<Self>,
1206    ) -> impl IntoIterator<Item = PendingToolUse> + use<> {
1207        let request = self.to_completion_request(RequestKind::Chat, cx);
1208        let messages = Arc::new(request.messages);
1209        let pending_tool_uses = self
1210            .tool_use
1211            .pending_tool_uses()
1212            .into_iter()
1213            .filter(|tool_use| tool_use.status.is_idle())
1214            .cloned()
1215            .collect::<Vec<_>>();
1216
1217        for tool_use in pending_tool_uses.iter() {
1218            if let Some(tool) = self.tools.tool(&tool_use.name, cx) {
1219                if tool.needs_confirmation()
1220                    && !AssistantSettings::get_global(cx).always_allow_tool_actions
1221                {
1222                    self.tool_use.confirm_tool_use(
1223                        tool_use.id.clone(),
1224                        tool_use.ui_text.clone(),
1225                        tool_use.input.clone(),
1226                        messages.clone(),
1227                        tool,
1228                    );
1229                    cx.emit(ThreadEvent::ToolConfirmationNeeded);
1230                } else {
1231                    self.run_tool(
1232                        tool_use.id.clone(),
1233                        tool_use.ui_text.clone(),
1234                        tool_use.input.clone(),
1235                        &messages,
1236                        tool,
1237                        cx,
1238                    );
1239                }
1240            } else if let Some(tool) = self.tools.tool(&tool_use.name, cx) {
1241                self.run_tool(
1242                    tool_use.id.clone(),
1243                    tool_use.ui_text.clone(),
1244                    tool_use.input.clone(),
1245                    &messages,
1246                    tool,
1247                    cx,
1248                );
1249            }
1250        }
1251
1252        pending_tool_uses
1253    }
1254
1255    pub fn run_tool(
1256        &mut self,
1257        tool_use_id: LanguageModelToolUseId,
1258        ui_text: impl Into<SharedString>,
1259        input: serde_json::Value,
1260        messages: &[LanguageModelRequestMessage],
1261        tool: Arc<dyn Tool>,
1262        cx: &mut Context<Thread>,
1263    ) {
1264        let task = self.spawn_tool_use(tool_use_id.clone(), messages, input, tool, cx);
1265        self.tool_use
1266            .run_pending_tool(tool_use_id, ui_text.into(), task);
1267    }
1268
1269    fn spawn_tool_use(
1270        &mut self,
1271        tool_use_id: LanguageModelToolUseId,
1272        messages: &[LanguageModelRequestMessage],
1273        input: serde_json::Value,
1274        tool: Arc<dyn Tool>,
1275        cx: &mut Context<Thread>,
1276    ) -> Task<()> {
1277        let tool_name: Arc<str> = tool.name().into();
1278        let run_tool = tool.run(
1279            input,
1280            messages,
1281            self.project.clone(),
1282            self.action_log.clone(),
1283            cx,
1284        );
1285
1286        cx.spawn({
1287            async move |thread: WeakEntity<Thread>, cx| {
1288                let output = run_tool.await;
1289
1290                thread
1291                    .update(cx, |thread, cx| {
1292                        let pending_tool_use = thread.tool_use.insert_tool_output(
1293                            tool_use_id.clone(),
1294                            tool_name,
1295                            output,
1296                        );
1297
1298                        cx.emit(ThreadEvent::ToolFinished {
1299                            tool_use_id,
1300                            pending_tool_use,
1301                            canceled: false,
1302                        });
1303                    })
1304                    .ok();
1305            }
1306        })
1307    }
1308
1309    pub fn attach_tool_results(
1310        &mut self,
1311        updated_context: Vec<AssistantContext>,
1312        cx: &mut Context<Self>,
1313    ) {
1314        self.context.extend(
1315            updated_context
1316                .into_iter()
1317                .map(|context| (context.id(), context)),
1318        );
1319
1320        // Insert a user message to contain the tool results.
1321        self.insert_user_message(
1322            // TODO: Sending up a user message without any content results in the model sending back
1323            // responses that also don't have any content. We currently don't handle this case well,
1324            // so for now we provide some text to keep the model on track.
1325            "Here are the tool results.",
1326            Vec::new(),
1327            None,
1328            cx,
1329        );
1330    }
1331
1332    /// Cancels the last pending completion, if there are any pending.
1333    ///
1334    /// Returns whether a completion was canceled.
1335    pub fn cancel_last_completion(&mut self, cx: &mut Context<Self>) -> bool {
1336        let canceled = if self.pending_completions.pop().is_some() {
1337            true
1338        } else {
1339            let mut canceled = false;
1340            for pending_tool_use in self.tool_use.cancel_pending() {
1341                canceled = true;
1342                cx.emit(ThreadEvent::ToolFinished {
1343                    tool_use_id: pending_tool_use.id.clone(),
1344                    pending_tool_use: Some(pending_tool_use),
1345                    canceled: true,
1346                });
1347            }
1348            canceled
1349        };
1350        self.finalize_pending_checkpoint(cx);
1351        canceled
1352    }
1353
1354    /// Returns the feedback given to the thread, if any.
1355    pub fn feedback(&self) -> Option<ThreadFeedback> {
1356        self.feedback
1357    }
1358
1359    /// Reports feedback about the thread and stores it in our telemetry backend.
1360    pub fn report_feedback(
1361        &mut self,
1362        feedback: ThreadFeedback,
1363        cx: &mut Context<Self>,
1364    ) -> Task<Result<()>> {
1365        let final_project_snapshot = Self::project_snapshot(self.project.clone(), cx);
1366        let serialized_thread = self.serialize(cx);
1367        let thread_id = self.id().clone();
1368        let client = self.project.read(cx).client();
1369        self.feedback = Some(feedback);
1370        cx.notify();
1371
1372        cx.background_spawn(async move {
1373            let final_project_snapshot = final_project_snapshot.await;
1374            let serialized_thread = serialized_thread.await?;
1375            let thread_data =
1376                serde_json::to_value(serialized_thread).unwrap_or_else(|_| serde_json::Value::Null);
1377
1378            let rating = match feedback {
1379                ThreadFeedback::Positive => "positive",
1380                ThreadFeedback::Negative => "negative",
1381            };
1382            telemetry::event!(
1383                "Assistant Thread Rated",
1384                rating,
1385                thread_id,
1386                thread_data,
1387                final_project_snapshot
1388            );
1389            client.telemetry().flush_events();
1390
1391            Ok(())
1392        })
1393    }
1394
1395    /// Create a snapshot of the current project state including git information and unsaved buffers.
1396    fn project_snapshot(
1397        project: Entity<Project>,
1398        cx: &mut Context<Self>,
1399    ) -> Task<Arc<ProjectSnapshot>> {
1400        let git_store = project.read(cx).git_store().clone();
1401        let worktree_snapshots: Vec<_> = project
1402            .read(cx)
1403            .visible_worktrees(cx)
1404            .map(|worktree| Self::worktree_snapshot(worktree, git_store.clone(), cx))
1405            .collect();
1406
1407        cx.spawn(async move |_, cx| {
1408            let worktree_snapshots = futures::future::join_all(worktree_snapshots).await;
1409
1410            let mut unsaved_buffers = Vec::new();
1411            cx.update(|app_cx| {
1412                let buffer_store = project.read(app_cx).buffer_store();
1413                for buffer_handle in buffer_store.read(app_cx).buffers() {
1414                    let buffer = buffer_handle.read(app_cx);
1415                    if buffer.is_dirty() {
1416                        if let Some(file) = buffer.file() {
1417                            let path = file.path().to_string_lossy().to_string();
1418                            unsaved_buffers.push(path);
1419                        }
1420                    }
1421                }
1422            })
1423            .ok();
1424
1425            Arc::new(ProjectSnapshot {
1426                worktree_snapshots,
1427                unsaved_buffer_paths: unsaved_buffers,
1428                timestamp: Utc::now(),
1429            })
1430        })
1431    }
1432
1433    fn worktree_snapshot(
1434        worktree: Entity<project::Worktree>,
1435        git_store: Entity<GitStore>,
1436        cx: &App,
1437    ) -> Task<WorktreeSnapshot> {
1438        cx.spawn(async move |cx| {
1439            // Get worktree path and snapshot
1440            let worktree_info = cx.update(|app_cx| {
1441                let worktree = worktree.read(app_cx);
1442                let path = worktree.abs_path().to_string_lossy().to_string();
1443                let snapshot = worktree.snapshot();
1444                (path, snapshot)
1445            });
1446
1447            let Ok((worktree_path, snapshot)) = worktree_info else {
1448                return WorktreeSnapshot {
1449                    worktree_path: String::new(),
1450                    git_state: None,
1451                };
1452            };
1453
1454            let repo_info = git_store
1455                .update(cx, |git_store, cx| {
1456                    git_store
1457                        .repositories()
1458                        .values()
1459                        .find(|repo| repo.read(cx).worktree_id == Some(snapshot.id()))
1460                        .and_then(|repo| {
1461                            let repo = repo.read(cx);
1462                            Some((repo.branch().cloned(), repo.local_repository()?))
1463                        })
1464                })
1465                .ok()
1466                .flatten();
1467
1468            // Extract git information
1469            let git_state = match repo_info {
1470                None => None,
1471                Some((branch, repo)) => {
1472                    let current_branch = branch.map(|branch| branch.name.to_string());
1473                    let remote_url = repo.remote_url("origin");
1474                    let head_sha = repo.head_sha();
1475
1476                    // Get diff asynchronously
1477                    let diff = repo
1478                        .diff(git::repository::DiffType::HeadToWorktree)
1479                        .await
1480                        .ok();
1481
1482                    Some(GitState {
1483                        remote_url,
1484                        head_sha,
1485                        current_branch,
1486                        diff,
1487                    })
1488                }
1489            };
1490
1491            WorktreeSnapshot {
1492                worktree_path,
1493                git_state,
1494            }
1495        })
1496    }
1497
1498    pub fn to_markdown(&self, cx: &App) -> Result<String> {
1499        let mut markdown = Vec::new();
1500
1501        if let Some(summary) = self.summary() {
1502            writeln!(markdown, "# {summary}\n")?;
1503        };
1504
1505        for message in self.messages() {
1506            writeln!(
1507                markdown,
1508                "## {role}\n",
1509                role = match message.role {
1510                    Role::User => "User",
1511                    Role::Assistant => "Assistant",
1512                    Role::System => "System",
1513                }
1514            )?;
1515            for segment in &message.segments {
1516                match segment {
1517                    MessageSegment::Text(text) => writeln!(markdown, "{}\n", text)?,
1518                    MessageSegment::Thinking(text) => {
1519                        writeln!(markdown, "<think>{}</think>\n", text)?
1520                    }
1521                }
1522            }
1523
1524            for tool_use in self.tool_uses_for_message(message.id, cx) {
1525                writeln!(
1526                    markdown,
1527                    "**Use Tool: {} ({})**",
1528                    tool_use.name, tool_use.id
1529                )?;
1530                writeln!(markdown, "```json")?;
1531                writeln!(
1532                    markdown,
1533                    "{}",
1534                    serde_json::to_string_pretty(&tool_use.input)?
1535                )?;
1536                writeln!(markdown, "```")?;
1537            }
1538
1539            for tool_result in self.tool_results_for_message(message.id) {
1540                write!(markdown, "**Tool Results: {}", tool_result.tool_use_id)?;
1541                if tool_result.is_error {
1542                    write!(markdown, " (Error)")?;
1543                }
1544
1545                writeln!(markdown, "**\n")?;
1546                writeln!(markdown, "{}", tool_result.content)?;
1547            }
1548        }
1549
1550        Ok(String::from_utf8_lossy(&markdown).to_string())
1551    }
1552
1553    pub fn review_edits_in_range(
1554        &mut self,
1555        buffer: Entity<language::Buffer>,
1556        buffer_range: Range<language::Anchor>,
1557        accept: bool,
1558        cx: &mut Context<Self>,
1559    ) {
1560        self.action_log.update(cx, |action_log, cx| {
1561            action_log.review_edits_in_range(buffer, buffer_range, accept, cx)
1562        });
1563    }
1564
1565    /// Keeps all edits across all buffers at once.
1566    /// This provides a more performant alternative to calling review_edits_in_range for each buffer.
1567    pub fn keep_all_edits(&mut self, cx: &mut Context<Self>) {
1568        self.action_log
1569            .update(cx, |action_log, _cx| action_log.keep_all_edits());
1570    }
1571
1572    pub fn action_log(&self) -> &Entity<ActionLog> {
1573        &self.action_log
1574    }
1575
1576    pub fn project(&self) -> &Entity<Project> {
1577        &self.project
1578    }
1579
1580    pub fn cumulative_token_usage(&self) -> TokenUsage {
1581        self.cumulative_token_usage.clone()
1582    }
1583
1584    pub fn deny_tool_use(
1585        &mut self,
1586        tool_use_id: LanguageModelToolUseId,
1587        tool_name: Arc<str>,
1588        cx: &mut Context<Self>,
1589    ) {
1590        let err = Err(anyhow::anyhow!(
1591            "Permission to run tool action denied by user"
1592        ));
1593
1594        self.tool_use
1595            .insert_tool_output(tool_use_id.clone(), tool_name, err);
1596
1597        cx.emit(ThreadEvent::ToolFinished {
1598            tool_use_id,
1599            pending_tool_use: None,
1600            canceled: true,
1601        });
1602    }
1603}
1604
1605#[derive(Debug, Clone)]
1606pub enum ThreadError {
1607    PaymentRequired,
1608    MaxMonthlySpendReached,
1609    Message {
1610        header: SharedString,
1611        message: SharedString,
1612    },
1613}
1614
1615#[derive(Debug, Clone)]
1616pub enum ThreadEvent {
1617    ShowError(ThreadError),
1618    StreamedCompletion,
1619    StreamedAssistantText(MessageId, String),
1620    StreamedAssistantThinking(MessageId, String),
1621    DoneStreaming,
1622    MessageAdded(MessageId),
1623    MessageEdited(MessageId),
1624    MessageDeleted(MessageId),
1625    SummaryChanged,
1626    UsePendingTools,
1627    ToolFinished {
1628        #[allow(unused)]
1629        tool_use_id: LanguageModelToolUseId,
1630        /// The pending tool use that corresponds to this tool.
1631        pending_tool_use: Option<PendingToolUse>,
1632        /// Whether the tool was canceled by the user.
1633        canceled: bool,
1634    },
1635    CheckpointChanged,
1636    ToolConfirmationNeeded,
1637}
1638
1639impl EventEmitter<ThreadEvent> for Thread {}
1640
1641struct PendingCompletion {
1642    id: usize,
1643    _task: Task<()>,
1644}