thread.rs

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