thread.rs

   1use crate::{
   2    ContextServerRegistry, CopyPathTool, CreateDirectoryTool, DbLanguageModel, DbThread,
   3    DeletePathTool, DiagnosticsTool, EditFileTool, FetchTool, FindPathTool, GrepTool,
   4    ListDirectoryTool, MovePathTool, NowTool, OpenTool, ReadFileTool, SystemPromptTemplate,
   5    Template, Templates, TerminalTool, ThinkingTool, WebSearchTool,
   6};
   7use acp_thread::{MentionUri, UserMessageId};
   8use action_log::ActionLog;
   9use agent::thread::{GitState, ProjectSnapshot, WorktreeSnapshot};
  10use agent_client_protocol as acp;
  11use agent_settings::{
  12    AgentProfileId, AgentProfileSettings, AgentSettings, CompletionMode,
  13    SUMMARIZE_THREAD_DETAILED_PROMPT, SUMMARIZE_THREAD_PROMPT,
  14};
  15use anyhow::{Context as _, Result, anyhow};
  16use assistant_tool::adapt_schema_to_format;
  17use chrono::{DateTime, Utc};
  18use client::{ModelRequestUsage, RequestUsage};
  19use cloud_llm_client::{CompletionIntent, CompletionRequestStatus, UsageLimit};
  20use collections::{HashMap, HashSet, IndexMap};
  21use fs::Fs;
  22use futures::{
  23    FutureExt,
  24    channel::{mpsc, oneshot},
  25    future::Shared,
  26    stream::FuturesUnordered,
  27};
  28use git::repository::DiffType;
  29use gpui::{
  30    App, AppContext, AsyncApp, Context, Entity, EventEmitter, SharedString, Task, WeakEntity,
  31};
  32use language_model::{
  33    LanguageModel, LanguageModelCompletionError, LanguageModelCompletionEvent, LanguageModelExt,
  34    LanguageModelImage, LanguageModelProviderId, LanguageModelRegistry, LanguageModelRequest,
  35    LanguageModelRequestMessage, LanguageModelRequestTool, LanguageModelToolResult,
  36    LanguageModelToolResultContent, LanguageModelToolSchemaFormat, LanguageModelToolUse,
  37    LanguageModelToolUseId, Role, SelectedModel, StopReason, TokenUsage,
  38};
  39use project::{
  40    Project,
  41    git_store::{GitStore, RepositoryState},
  42};
  43use prompt_store::ProjectContext;
  44use schemars::{JsonSchema, Schema};
  45use serde::{Deserialize, Serialize};
  46use settings::{Settings, update_settings_file};
  47use smol::stream::StreamExt;
  48use std::fmt::Write;
  49use std::{
  50    collections::BTreeMap,
  51    ops::RangeInclusive,
  52    path::Path,
  53    sync::Arc,
  54    time::{Duration, Instant},
  55};
  56use util::{ResultExt, debug_panic, markdown::MarkdownCodeBlock};
  57use uuid::Uuid;
  58
  59const TOOL_CANCELED_MESSAGE: &str = "Tool canceled by user";
  60pub const MAX_TOOL_NAME_LENGTH: usize = 64;
  61
  62/// The ID of the user prompt that initiated a request.
  63///
  64/// This equates to the user physically submitting a message to the model (e.g., by pressing the Enter key).
  65#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Serialize, Deserialize)]
  66pub struct PromptId(Arc<str>);
  67
  68impl PromptId {
  69    pub fn new() -> Self {
  70        Self(Uuid::new_v4().to_string().into())
  71    }
  72}
  73
  74impl std::fmt::Display for PromptId {
  75    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  76        write!(f, "{}", self.0)
  77    }
  78}
  79
  80pub(crate) const MAX_RETRY_ATTEMPTS: u8 = 4;
  81pub(crate) const BASE_RETRY_DELAY: Duration = Duration::from_secs(5);
  82
  83#[derive(Debug, Clone)]
  84enum RetryStrategy {
  85    ExponentialBackoff {
  86        initial_delay: Duration,
  87        max_attempts: u8,
  88    },
  89    Fixed {
  90        delay: Duration,
  91        max_attempts: u8,
  92    },
  93}
  94
  95#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
  96pub enum Message {
  97    User(UserMessage),
  98    Agent(AgentMessage),
  99    Resume,
 100}
 101
 102impl Message {
 103    pub fn as_agent_message(&self) -> Option<&AgentMessage> {
 104        match self {
 105            Message::Agent(agent_message) => Some(agent_message),
 106            _ => None,
 107        }
 108    }
 109
 110    pub fn to_request(&self) -> Vec<LanguageModelRequestMessage> {
 111        match self {
 112            Message::User(message) => vec![message.to_request()],
 113            Message::Agent(message) => message.to_request(),
 114            Message::Resume => vec![LanguageModelRequestMessage {
 115                role: Role::User,
 116                content: vec!["Continue where you left off".into()],
 117                cache: false,
 118            }],
 119        }
 120    }
 121
 122    pub fn to_markdown(&self) -> String {
 123        match self {
 124            Message::User(message) => message.to_markdown(),
 125            Message::Agent(message) => message.to_markdown(),
 126            Message::Resume => "[resume]\n".into(),
 127        }
 128    }
 129
 130    pub fn role(&self) -> Role {
 131        match self {
 132            Message::User(_) | Message::Resume => Role::User,
 133            Message::Agent(_) => Role::Assistant,
 134        }
 135    }
 136}
 137
 138#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
 139pub struct UserMessage {
 140    pub id: UserMessageId,
 141    pub content: Vec<UserMessageContent>,
 142}
 143
 144#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
 145pub enum UserMessageContent {
 146    Text(String),
 147    Mention { uri: MentionUri, content: String },
 148    Image(LanguageModelImage),
 149}
 150
 151impl UserMessage {
 152    pub fn to_markdown(&self) -> String {
 153        let mut markdown = String::from("## User\n\n");
 154
 155        for content in &self.content {
 156            match content {
 157                UserMessageContent::Text(text) => {
 158                    markdown.push_str(text);
 159                    markdown.push('\n');
 160                }
 161                UserMessageContent::Image(_) => {
 162                    markdown.push_str("<image />\n");
 163                }
 164                UserMessageContent::Mention { uri, content } => {
 165                    if !content.is_empty() {
 166                        let _ = writeln!(&mut markdown, "{}\n\n{}", uri.as_link(), content);
 167                    } else {
 168                        let _ = writeln!(&mut markdown, "{}", uri.as_link());
 169                    }
 170                }
 171            }
 172        }
 173
 174        markdown
 175    }
 176
 177    fn to_request(&self) -> LanguageModelRequestMessage {
 178        let mut message = LanguageModelRequestMessage {
 179            role: Role::User,
 180            content: Vec::with_capacity(self.content.len()),
 181            cache: false,
 182        };
 183
 184        const OPEN_CONTEXT: &str = "<context>\n\
 185            The following items were attached by the user. \
 186            They are up-to-date and don't need to be re-read.\n\n";
 187
 188        const OPEN_FILES_TAG: &str = "<files>";
 189        const OPEN_DIRECTORIES_TAG: &str = "<directories>";
 190        const OPEN_SYMBOLS_TAG: &str = "<symbols>";
 191        const OPEN_SELECTIONS_TAG: &str = "<selections>";
 192        const OPEN_THREADS_TAG: &str = "<threads>";
 193        const OPEN_FETCH_TAG: &str = "<fetched_urls>";
 194        const OPEN_RULES_TAG: &str =
 195            "<rules>\nThe user has specified the following rules that should be applied:\n";
 196
 197        let mut file_context = OPEN_FILES_TAG.to_string();
 198        let mut directory_context = OPEN_DIRECTORIES_TAG.to_string();
 199        let mut symbol_context = OPEN_SYMBOLS_TAG.to_string();
 200        let mut selection_context = OPEN_SELECTIONS_TAG.to_string();
 201        let mut thread_context = OPEN_THREADS_TAG.to_string();
 202        let mut fetch_context = OPEN_FETCH_TAG.to_string();
 203        let mut rules_context = OPEN_RULES_TAG.to_string();
 204
 205        for chunk in &self.content {
 206            let chunk = match chunk {
 207                UserMessageContent::Text(text) => {
 208                    language_model::MessageContent::Text(text.clone())
 209                }
 210                UserMessageContent::Image(value) => {
 211                    language_model::MessageContent::Image(value.clone())
 212                }
 213                UserMessageContent::Mention { uri, content } => {
 214                    match uri {
 215                        MentionUri::File { abs_path } => {
 216                            write!(
 217                                &mut file_context,
 218                                "\n{}",
 219                                MarkdownCodeBlock {
 220                                    tag: &codeblock_tag(abs_path, None),
 221                                    text: &content.to_string(),
 222                                }
 223                            )
 224                            .ok();
 225                        }
 226                        MentionUri::PastedImage => {
 227                            debug_panic!("pasted image URI should not be used in mention content")
 228                        }
 229                        MentionUri::Directory { .. } => {
 230                            write!(&mut directory_context, "\n{}\n", content).ok();
 231                        }
 232                        MentionUri::Symbol {
 233                            abs_path: path,
 234                            line_range,
 235                            ..
 236                        } => {
 237                            write!(
 238                                &mut symbol_context,
 239                                "\n{}",
 240                                MarkdownCodeBlock {
 241                                    tag: &codeblock_tag(path, Some(line_range)),
 242                                    text: content
 243                                }
 244                            )
 245                            .ok();
 246                        }
 247                        MentionUri::Selection {
 248                            abs_path: path,
 249                            line_range,
 250                            ..
 251                        } => {
 252                            write!(
 253                                &mut selection_context,
 254                                "\n{}",
 255                                MarkdownCodeBlock {
 256                                    tag: &codeblock_tag(
 257                                        path.as_deref().unwrap_or("Untitled".as_ref()),
 258                                        Some(line_range)
 259                                    ),
 260                                    text: content
 261                                }
 262                            )
 263                            .ok();
 264                        }
 265                        MentionUri::Thread { .. } => {
 266                            write!(&mut thread_context, "\n{}\n", content).ok();
 267                        }
 268                        MentionUri::TextThread { .. } => {
 269                            write!(&mut thread_context, "\n{}\n", content).ok();
 270                        }
 271                        MentionUri::Rule { .. } => {
 272                            write!(
 273                                &mut rules_context,
 274                                "\n{}",
 275                                MarkdownCodeBlock {
 276                                    tag: "",
 277                                    text: content
 278                                }
 279                            )
 280                            .ok();
 281                        }
 282                        MentionUri::Fetch { url } => {
 283                            write!(&mut fetch_context, "\nFetch: {}\n\n{}", url, content).ok();
 284                        }
 285                    }
 286
 287                    language_model::MessageContent::Text(uri.as_link().to_string())
 288                }
 289            };
 290
 291            message.content.push(chunk);
 292        }
 293
 294        let len_before_context = message.content.len();
 295
 296        if file_context.len() > OPEN_FILES_TAG.len() {
 297            file_context.push_str("</files>\n");
 298            message
 299                .content
 300                .push(language_model::MessageContent::Text(file_context));
 301        }
 302
 303        if directory_context.len() > OPEN_DIRECTORIES_TAG.len() {
 304            directory_context.push_str("</directories>\n");
 305            message
 306                .content
 307                .push(language_model::MessageContent::Text(directory_context));
 308        }
 309
 310        if symbol_context.len() > OPEN_SYMBOLS_TAG.len() {
 311            symbol_context.push_str("</symbols>\n");
 312            message
 313                .content
 314                .push(language_model::MessageContent::Text(symbol_context));
 315        }
 316
 317        if selection_context.len() > OPEN_SELECTIONS_TAG.len() {
 318            selection_context.push_str("</selections>\n");
 319            message
 320                .content
 321                .push(language_model::MessageContent::Text(selection_context));
 322        }
 323
 324        if thread_context.len() > OPEN_THREADS_TAG.len() {
 325            thread_context.push_str("</threads>\n");
 326            message
 327                .content
 328                .push(language_model::MessageContent::Text(thread_context));
 329        }
 330
 331        if fetch_context.len() > OPEN_FETCH_TAG.len() {
 332            fetch_context.push_str("</fetched_urls>\n");
 333            message
 334                .content
 335                .push(language_model::MessageContent::Text(fetch_context));
 336        }
 337
 338        if rules_context.len() > OPEN_RULES_TAG.len() {
 339            rules_context.push_str("</user_rules>\n");
 340            message
 341                .content
 342                .push(language_model::MessageContent::Text(rules_context));
 343        }
 344
 345        if message.content.len() > len_before_context {
 346            message.content.insert(
 347                len_before_context,
 348                language_model::MessageContent::Text(OPEN_CONTEXT.into()),
 349            );
 350            message
 351                .content
 352                .push(language_model::MessageContent::Text("</context>".into()));
 353        }
 354
 355        message
 356    }
 357}
 358
 359fn codeblock_tag(full_path: &Path, line_range: Option<&RangeInclusive<u32>>) -> String {
 360    let mut result = String::new();
 361
 362    if let Some(extension) = full_path.extension().and_then(|ext| ext.to_str()) {
 363        let _ = write!(result, "{} ", extension);
 364    }
 365
 366    let _ = write!(result, "{}", full_path.display());
 367
 368    if let Some(range) = line_range {
 369        if range.start() == range.end() {
 370            let _ = write!(result, ":{}", range.start() + 1);
 371        } else {
 372            let _ = write!(result, ":{}-{}", range.start() + 1, range.end() + 1);
 373        }
 374    }
 375
 376    result
 377}
 378
 379impl AgentMessage {
 380    pub fn to_markdown(&self) -> String {
 381        let mut markdown = String::from("## Assistant\n\n");
 382
 383        for content in &self.content {
 384            match content {
 385                AgentMessageContent::Text(text) => {
 386                    markdown.push_str(text);
 387                    markdown.push('\n');
 388                }
 389                AgentMessageContent::Thinking { text, .. } => {
 390                    markdown.push_str("<think>");
 391                    markdown.push_str(text);
 392                    markdown.push_str("</think>\n");
 393                }
 394                AgentMessageContent::RedactedThinking(_) => {
 395                    markdown.push_str("<redacted_thinking />\n")
 396                }
 397                AgentMessageContent::ToolUse(tool_use) => {
 398                    markdown.push_str(&format!(
 399                        "**Tool Use**: {} (ID: {})\n",
 400                        tool_use.name, tool_use.id
 401                    ));
 402                    markdown.push_str(&format!(
 403                        "{}\n",
 404                        MarkdownCodeBlock {
 405                            tag: "json",
 406                            text: &format!("{:#}", tool_use.input)
 407                        }
 408                    ));
 409                }
 410            }
 411        }
 412
 413        for tool_result in self.tool_results.values() {
 414            markdown.push_str(&format!(
 415                "**Tool Result**: {} (ID: {})\n\n",
 416                tool_result.tool_name, tool_result.tool_use_id
 417            ));
 418            if tool_result.is_error {
 419                markdown.push_str("**ERROR:**\n");
 420            }
 421
 422            match &tool_result.content {
 423                LanguageModelToolResultContent::Text(text) => {
 424                    writeln!(markdown, "{text}\n").ok();
 425                }
 426                LanguageModelToolResultContent::Image(_) => {
 427                    writeln!(markdown, "<image />\n").ok();
 428                }
 429            }
 430
 431            if let Some(output) = tool_result.output.as_ref() {
 432                writeln!(
 433                    markdown,
 434                    "**Debug Output**:\n\n```json\n{}\n```\n",
 435                    serde_json::to_string_pretty(output).unwrap()
 436                )
 437                .unwrap();
 438            }
 439        }
 440
 441        markdown
 442    }
 443
 444    pub fn to_request(&self) -> Vec<LanguageModelRequestMessage> {
 445        let mut assistant_message = LanguageModelRequestMessage {
 446            role: Role::Assistant,
 447            content: Vec::with_capacity(self.content.len()),
 448            cache: false,
 449        };
 450        for chunk in &self.content {
 451            match chunk {
 452                AgentMessageContent::Text(text) => {
 453                    assistant_message
 454                        .content
 455                        .push(language_model::MessageContent::Text(text.clone()));
 456                }
 457                AgentMessageContent::Thinking { text, signature } => {
 458                    assistant_message
 459                        .content
 460                        .push(language_model::MessageContent::Thinking {
 461                            text: text.clone(),
 462                            signature: signature.clone(),
 463                        });
 464                }
 465                AgentMessageContent::RedactedThinking(value) => {
 466                    assistant_message.content.push(
 467                        language_model::MessageContent::RedactedThinking(value.clone()),
 468                    );
 469                }
 470                AgentMessageContent::ToolUse(tool_use) => {
 471                    if self.tool_results.contains_key(&tool_use.id) {
 472                        assistant_message
 473                            .content
 474                            .push(language_model::MessageContent::ToolUse(tool_use.clone()));
 475                    }
 476                }
 477            };
 478        }
 479
 480        let mut user_message = LanguageModelRequestMessage {
 481            role: Role::User,
 482            content: Vec::new(),
 483            cache: false,
 484        };
 485
 486        for tool_result in self.tool_results.values() {
 487            user_message
 488                .content
 489                .push(language_model::MessageContent::ToolResult(
 490                    tool_result.clone(),
 491                ));
 492        }
 493
 494        let mut messages = Vec::new();
 495        if !assistant_message.content.is_empty() {
 496            messages.push(assistant_message);
 497        }
 498        if !user_message.content.is_empty() {
 499            messages.push(user_message);
 500        }
 501        messages
 502    }
 503}
 504
 505#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
 506pub struct AgentMessage {
 507    pub content: Vec<AgentMessageContent>,
 508    pub tool_results: IndexMap<LanguageModelToolUseId, LanguageModelToolResult>,
 509}
 510
 511#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
 512pub enum AgentMessageContent {
 513    Text(String),
 514    Thinking {
 515        text: String,
 516        signature: Option<String>,
 517    },
 518    RedactedThinking(String),
 519    ToolUse(LanguageModelToolUse),
 520}
 521
 522#[derive(Debug)]
 523pub enum ThreadEvent {
 524    UserMessage(UserMessage),
 525    AgentText(String),
 526    AgentThinking(String),
 527    ToolCall(acp::ToolCall),
 528    ToolCallUpdate(acp_thread::ToolCallUpdate),
 529    ToolCallAuthorization(ToolCallAuthorization),
 530    Retry(acp_thread::RetryStatus),
 531    Stop(acp::StopReason),
 532}
 533
 534#[derive(Debug)]
 535pub struct ToolCallAuthorization {
 536    pub tool_call: acp::ToolCallUpdate,
 537    pub options: Vec<acp::PermissionOption>,
 538    pub response: oneshot::Sender<acp::PermissionOptionId>,
 539}
 540
 541#[derive(Debug, thiserror::Error)]
 542enum CompletionError {
 543    #[error("max tokens")]
 544    MaxTokens,
 545    #[error("refusal")]
 546    Refusal,
 547    #[error(transparent)]
 548    Other(#[from] anyhow::Error),
 549}
 550
 551pub struct Thread {
 552    id: acp::SessionId,
 553    prompt_id: PromptId,
 554    updated_at: DateTime<Utc>,
 555    title: Option<SharedString>,
 556    pending_title_generation: Option<Task<()>>,
 557    summary: Option<SharedString>,
 558    messages: Vec<Message>,
 559    completion_mode: CompletionMode,
 560    /// Holds the task that handles agent interaction until the end of the turn.
 561    /// Survives across multiple requests as the model performs tool calls and
 562    /// we run tools, report their results.
 563    running_turn: Option<RunningTurn>,
 564    pending_message: Option<AgentMessage>,
 565    tools: BTreeMap<SharedString, Arc<dyn AnyAgentTool>>,
 566    tool_use_limit_reached: bool,
 567    request_token_usage: HashMap<UserMessageId, language_model::TokenUsage>,
 568    #[allow(unused)]
 569    cumulative_token_usage: TokenUsage,
 570    #[allow(unused)]
 571    initial_project_snapshot: Shared<Task<Option<Arc<ProjectSnapshot>>>>,
 572    context_server_registry: Entity<ContextServerRegistry>,
 573    profile_id: AgentProfileId,
 574    project_context: Entity<ProjectContext>,
 575    templates: Arc<Templates>,
 576    model: Option<Arc<dyn LanguageModel>>,
 577    summarization_model: Option<Arc<dyn LanguageModel>>,
 578    prompt_capabilities_tx: watch::Sender<acp::PromptCapabilities>,
 579    pub(crate) prompt_capabilities_rx: watch::Receiver<acp::PromptCapabilities>,
 580    pub(crate) project: Entity<Project>,
 581    pub(crate) action_log: Entity<ActionLog>,
 582}
 583
 584impl Thread {
 585    fn prompt_capabilities(model: Option<&dyn LanguageModel>) -> acp::PromptCapabilities {
 586        let image = model.map_or(true, |model| model.supports_images());
 587        acp::PromptCapabilities {
 588            image,
 589            audio: false,
 590            embedded_context: true,
 591        }
 592    }
 593
 594    pub fn new(
 595        project: Entity<Project>,
 596        project_context: Entity<ProjectContext>,
 597        context_server_registry: Entity<ContextServerRegistry>,
 598        templates: Arc<Templates>,
 599        model: Option<Arc<dyn LanguageModel>>,
 600        cx: &mut Context<Self>,
 601    ) -> Self {
 602        let profile_id = AgentSettings::get_global(cx).default_profile.clone();
 603        let action_log = cx.new(|_cx| ActionLog::new(project.clone()));
 604        let (prompt_capabilities_tx, prompt_capabilities_rx) =
 605            watch::channel(Self::prompt_capabilities(model.as_deref()));
 606        Self {
 607            id: acp::SessionId(uuid::Uuid::new_v4().to_string().into()),
 608            prompt_id: PromptId::new(),
 609            updated_at: Utc::now(),
 610            title: None,
 611            pending_title_generation: None,
 612            summary: None,
 613            messages: Vec::new(),
 614            completion_mode: AgentSettings::get_global(cx).preferred_completion_mode,
 615            running_turn: None,
 616            pending_message: None,
 617            tools: BTreeMap::default(),
 618            tool_use_limit_reached: false,
 619            request_token_usage: HashMap::default(),
 620            cumulative_token_usage: TokenUsage::default(),
 621            initial_project_snapshot: {
 622                let project_snapshot = Self::project_snapshot(project.clone(), cx);
 623                cx.foreground_executor()
 624                    .spawn(async move { Some(project_snapshot.await) })
 625                    .shared()
 626            },
 627            context_server_registry,
 628            profile_id,
 629            project_context,
 630            templates,
 631            model,
 632            summarization_model: None,
 633            prompt_capabilities_tx,
 634            prompt_capabilities_rx,
 635            project,
 636            action_log,
 637        }
 638    }
 639
 640    pub fn id(&self) -> &acp::SessionId {
 641        &self.id
 642    }
 643
 644    pub fn replay(
 645        &mut self,
 646        cx: &mut Context<Self>,
 647    ) -> mpsc::UnboundedReceiver<Result<ThreadEvent>> {
 648        let (tx, rx) = mpsc::unbounded();
 649        let stream = ThreadEventStream(tx);
 650        for message in &self.messages {
 651            match message {
 652                Message::User(user_message) => stream.send_user_message(user_message),
 653                Message::Agent(assistant_message) => {
 654                    for content in &assistant_message.content {
 655                        match content {
 656                            AgentMessageContent::Text(text) => stream.send_text(text),
 657                            AgentMessageContent::Thinking { text, .. } => {
 658                                stream.send_thinking(text)
 659                            }
 660                            AgentMessageContent::RedactedThinking(_) => {}
 661                            AgentMessageContent::ToolUse(tool_use) => {
 662                                self.replay_tool_call(
 663                                    tool_use,
 664                                    assistant_message.tool_results.get(&tool_use.id),
 665                                    &stream,
 666                                    cx,
 667                                );
 668                            }
 669                        }
 670                    }
 671                }
 672                Message::Resume => {}
 673            }
 674        }
 675        rx
 676    }
 677
 678    fn replay_tool_call(
 679        &self,
 680        tool_use: &LanguageModelToolUse,
 681        tool_result: Option<&LanguageModelToolResult>,
 682        stream: &ThreadEventStream,
 683        cx: &mut Context<Self>,
 684    ) {
 685        let tool = self.tools.get(tool_use.name.as_ref()).cloned().or_else(|| {
 686            self.context_server_registry
 687                .read(cx)
 688                .servers()
 689                .find_map(|(_, tools)| {
 690                    if let Some(tool) = tools.get(tool_use.name.as_ref()) {
 691                        Some(tool.clone())
 692                    } else {
 693                        None
 694                    }
 695                })
 696        });
 697
 698        let Some(tool) = tool else {
 699            stream
 700                .0
 701                .unbounded_send(Ok(ThreadEvent::ToolCall(acp::ToolCall {
 702                    id: acp::ToolCallId(tool_use.id.to_string().into()),
 703                    title: tool_use.name.to_string(),
 704                    kind: acp::ToolKind::Other,
 705                    status: acp::ToolCallStatus::Failed,
 706                    content: Vec::new(),
 707                    locations: Vec::new(),
 708                    raw_input: Some(tool_use.input.clone()),
 709                    raw_output: None,
 710                })))
 711                .ok();
 712            return;
 713        };
 714
 715        let title = tool.initial_title(tool_use.input.clone());
 716        let kind = tool.kind();
 717        stream.send_tool_call(&tool_use.id, title, kind, tool_use.input.clone());
 718
 719        let output = tool_result
 720            .as_ref()
 721            .and_then(|result| result.output.clone());
 722        if let Some(output) = output.clone() {
 723            let tool_event_stream = ToolCallEventStream::new(
 724                tool_use.id.clone(),
 725                stream.clone(),
 726                Some(self.project.read(cx).fs().clone()),
 727            );
 728            tool.replay(tool_use.input.clone(), output, tool_event_stream, cx)
 729                .log_err();
 730        }
 731
 732        stream.update_tool_call_fields(
 733            &tool_use.id,
 734            acp::ToolCallUpdateFields {
 735                status: Some(acp::ToolCallStatus::Completed),
 736                raw_output: output,
 737                ..Default::default()
 738            },
 739        );
 740    }
 741
 742    pub fn from_db(
 743        id: acp::SessionId,
 744        db_thread: DbThread,
 745        project: Entity<Project>,
 746        project_context: Entity<ProjectContext>,
 747        context_server_registry: Entity<ContextServerRegistry>,
 748        action_log: Entity<ActionLog>,
 749        templates: Arc<Templates>,
 750        cx: &mut Context<Self>,
 751    ) -> Self {
 752        let profile_id = db_thread
 753            .profile
 754            .unwrap_or_else(|| AgentSettings::get_global(cx).default_profile.clone());
 755        let model = LanguageModelRegistry::global(cx).update(cx, |registry, cx| {
 756            db_thread
 757                .model
 758                .and_then(|model| {
 759                    let model = SelectedModel {
 760                        provider: model.provider.clone().into(),
 761                        model: model.model.into(),
 762                    };
 763                    registry.select_model(&model, cx)
 764                })
 765                .or_else(|| registry.default_model())
 766                .map(|model| model.model)
 767        });
 768        let (prompt_capabilities_tx, prompt_capabilities_rx) =
 769            watch::channel(Self::prompt_capabilities(model.as_deref()));
 770
 771        Self {
 772            id,
 773            prompt_id: PromptId::new(),
 774            title: if db_thread.title.is_empty() {
 775                None
 776            } else {
 777                Some(db_thread.title.clone())
 778            },
 779            pending_title_generation: None,
 780            summary: db_thread.detailed_summary,
 781            messages: db_thread.messages,
 782            completion_mode: db_thread.completion_mode.unwrap_or_default(),
 783            running_turn: None,
 784            pending_message: None,
 785            tools: BTreeMap::default(),
 786            tool_use_limit_reached: false,
 787            request_token_usage: db_thread.request_token_usage.clone(),
 788            cumulative_token_usage: db_thread.cumulative_token_usage,
 789            initial_project_snapshot: Task::ready(db_thread.initial_project_snapshot).shared(),
 790            context_server_registry,
 791            profile_id,
 792            project_context,
 793            templates,
 794            model,
 795            summarization_model: None,
 796            project,
 797            action_log,
 798            updated_at: db_thread.updated_at,
 799            prompt_capabilities_tx,
 800            prompt_capabilities_rx,
 801        }
 802    }
 803
 804    pub fn to_db(&self, cx: &App) -> Task<DbThread> {
 805        let initial_project_snapshot = self.initial_project_snapshot.clone();
 806        let mut thread = DbThread {
 807            title: self.title(),
 808            messages: self.messages.clone(),
 809            updated_at: self.updated_at,
 810            detailed_summary: self.summary.clone(),
 811            initial_project_snapshot: None,
 812            cumulative_token_usage: self.cumulative_token_usage,
 813            request_token_usage: self.request_token_usage.clone(),
 814            model: self.model.as_ref().map(|model| DbLanguageModel {
 815                provider: model.provider_id().to_string(),
 816                model: model.name().0.to_string(),
 817            }),
 818            completion_mode: Some(self.completion_mode),
 819            profile: Some(self.profile_id.clone()),
 820        };
 821
 822        cx.background_spawn(async move {
 823            let initial_project_snapshot = initial_project_snapshot.await;
 824            thread.initial_project_snapshot = initial_project_snapshot;
 825            thread
 826        })
 827    }
 828
 829    /// Create a snapshot of the current project state including git information and unsaved buffers.
 830    fn project_snapshot(
 831        project: Entity<Project>,
 832        cx: &mut Context<Self>,
 833    ) -> Task<Arc<agent::thread::ProjectSnapshot>> {
 834        let git_store = project.read(cx).git_store().clone();
 835        let worktree_snapshots: Vec<_> = project
 836            .read(cx)
 837            .visible_worktrees(cx)
 838            .map(|worktree| Self::worktree_snapshot(worktree, git_store.clone(), cx))
 839            .collect();
 840
 841        cx.spawn(async move |_, cx| {
 842            let worktree_snapshots = futures::future::join_all(worktree_snapshots).await;
 843
 844            let mut unsaved_buffers = Vec::new();
 845            cx.update(|app_cx| {
 846                let buffer_store = project.read(app_cx).buffer_store();
 847                for buffer_handle in buffer_store.read(app_cx).buffers() {
 848                    let buffer = buffer_handle.read(app_cx);
 849                    if buffer.is_dirty()
 850                        && let Some(file) = buffer.file()
 851                    {
 852                        let path = file.path().to_string_lossy().to_string();
 853                        unsaved_buffers.push(path);
 854                    }
 855                }
 856            })
 857            .ok();
 858
 859            Arc::new(ProjectSnapshot {
 860                worktree_snapshots,
 861                unsaved_buffer_paths: unsaved_buffers,
 862                timestamp: Utc::now(),
 863            })
 864        })
 865    }
 866
 867    fn worktree_snapshot(
 868        worktree: Entity<project::Worktree>,
 869        git_store: Entity<GitStore>,
 870        cx: &App,
 871    ) -> Task<agent::thread::WorktreeSnapshot> {
 872        cx.spawn(async move |cx| {
 873            // Get worktree path and snapshot
 874            let worktree_info = cx.update(|app_cx| {
 875                let worktree = worktree.read(app_cx);
 876                let path = worktree.abs_path().to_string_lossy().to_string();
 877                let snapshot = worktree.snapshot();
 878                (path, snapshot)
 879            });
 880
 881            let Ok((worktree_path, _snapshot)) = worktree_info else {
 882                return WorktreeSnapshot {
 883                    worktree_path: String::new(),
 884                    git_state: None,
 885                };
 886            };
 887
 888            let git_state = git_store
 889                .update(cx, |git_store, cx| {
 890                    git_store
 891                        .repositories()
 892                        .values()
 893                        .find(|repo| {
 894                            repo.read(cx)
 895                                .abs_path_to_repo_path(&worktree.read(cx).abs_path())
 896                                .is_some()
 897                        })
 898                        .cloned()
 899                })
 900                .ok()
 901                .flatten()
 902                .map(|repo| {
 903                    repo.update(cx, |repo, _| {
 904                        let current_branch =
 905                            repo.branch.as_ref().map(|branch| branch.name().to_owned());
 906                        repo.send_job(None, |state, _| async move {
 907                            let RepositoryState::Local { backend, .. } = state else {
 908                                return GitState {
 909                                    remote_url: None,
 910                                    head_sha: None,
 911                                    current_branch,
 912                                    diff: None,
 913                                };
 914                            };
 915
 916                            let remote_url = backend.remote_url("origin");
 917                            let head_sha = backend.head_sha().await;
 918                            let diff = backend.diff(DiffType::HeadToWorktree).await.ok();
 919
 920                            GitState {
 921                                remote_url,
 922                                head_sha,
 923                                current_branch,
 924                                diff,
 925                            }
 926                        })
 927                    })
 928                });
 929
 930            let git_state = match git_state {
 931                Some(git_state) => match git_state.ok() {
 932                    Some(git_state) => git_state.await.ok(),
 933                    None => None,
 934                },
 935                None => None,
 936            };
 937
 938            WorktreeSnapshot {
 939                worktree_path,
 940                git_state,
 941            }
 942        })
 943    }
 944
 945    pub fn project_context(&self) -> &Entity<ProjectContext> {
 946        &self.project_context
 947    }
 948
 949    pub fn project(&self) -> &Entity<Project> {
 950        &self.project
 951    }
 952
 953    pub fn action_log(&self) -> &Entity<ActionLog> {
 954        &self.action_log
 955    }
 956
 957    pub fn is_empty(&self) -> bool {
 958        self.messages.is_empty() && self.title.is_none()
 959    }
 960
 961    pub fn model(&self) -> Option<&Arc<dyn LanguageModel>> {
 962        self.model.as_ref()
 963    }
 964
 965    pub fn set_model(&mut self, model: Arc<dyn LanguageModel>, cx: &mut Context<Self>) {
 966        let old_usage = self.latest_token_usage();
 967        self.model = Some(model);
 968        let new_caps = Self::prompt_capabilities(self.model.as_deref());
 969        let new_usage = self.latest_token_usage();
 970        if old_usage != new_usage {
 971            cx.emit(TokenUsageUpdated(new_usage));
 972        }
 973        self.prompt_capabilities_tx.send(new_caps).log_err();
 974        cx.notify()
 975    }
 976
 977    pub fn summarization_model(&self) -> Option<&Arc<dyn LanguageModel>> {
 978        self.summarization_model.as_ref()
 979    }
 980
 981    pub fn set_summarization_model(
 982        &mut self,
 983        model: Option<Arc<dyn LanguageModel>>,
 984        cx: &mut Context<Self>,
 985    ) {
 986        self.summarization_model = model;
 987        cx.notify()
 988    }
 989
 990    pub fn completion_mode(&self) -> CompletionMode {
 991        self.completion_mode
 992    }
 993
 994    pub fn set_completion_mode(&mut self, mode: CompletionMode, cx: &mut Context<Self>) {
 995        let old_usage = self.latest_token_usage();
 996        self.completion_mode = mode;
 997        let new_usage = self.latest_token_usage();
 998        if old_usage != new_usage {
 999            cx.emit(TokenUsageUpdated(new_usage));
1000        }
1001        cx.notify()
1002    }
1003
1004    #[cfg(any(test, feature = "test-support"))]
1005    pub fn last_message(&self) -> Option<Message> {
1006        if let Some(message) = self.pending_message.clone() {
1007            Some(Message::Agent(message))
1008        } else {
1009            self.messages.last().cloned()
1010        }
1011    }
1012
1013    pub fn add_default_tools(&mut self, cx: &mut Context<Self>) {
1014        let language_registry = self.project.read(cx).languages().clone();
1015        self.add_tool(CopyPathTool::new(self.project.clone()));
1016        self.add_tool(CreateDirectoryTool::new(self.project.clone()));
1017        self.add_tool(DeletePathTool::new(
1018            self.project.clone(),
1019            self.action_log.clone(),
1020        ));
1021        self.add_tool(DiagnosticsTool::new(self.project.clone()));
1022        self.add_tool(EditFileTool::new(cx.weak_entity(), language_registry));
1023        self.add_tool(FetchTool::new(self.project.read(cx).client().http_client()));
1024        self.add_tool(FindPathTool::new(self.project.clone()));
1025        self.add_tool(GrepTool::new(self.project.clone()));
1026        self.add_tool(ListDirectoryTool::new(self.project.clone()));
1027        self.add_tool(MovePathTool::new(self.project.clone()));
1028        self.add_tool(NowTool);
1029        self.add_tool(OpenTool::new(self.project.clone()));
1030        self.add_tool(ReadFileTool::new(
1031            self.project.clone(),
1032            self.action_log.clone(),
1033        ));
1034        self.add_tool(TerminalTool::new(self.project.clone(), cx));
1035        self.add_tool(ThinkingTool);
1036        self.add_tool(WebSearchTool);
1037    }
1038
1039    pub fn add_tool<T: AgentTool>(&mut self, tool: T) {
1040        self.tools.insert(T::name().into(), tool.erase());
1041    }
1042
1043    pub fn remove_tool(&mut self, name: &str) -> bool {
1044        self.tools.remove(name).is_some()
1045    }
1046
1047    pub fn profile(&self) -> &AgentProfileId {
1048        &self.profile_id
1049    }
1050
1051    pub fn set_profile(&mut self, profile_id: AgentProfileId) {
1052        self.profile_id = profile_id;
1053    }
1054
1055    pub fn cancel(&mut self, cx: &mut Context<Self>) {
1056        if let Some(running_turn) = self.running_turn.take() {
1057            running_turn.cancel();
1058        }
1059        self.flush_pending_message(cx);
1060    }
1061
1062    fn update_token_usage(&mut self, update: language_model::TokenUsage, cx: &mut Context<Self>) {
1063        let Some(last_user_message) = self.last_user_message() else {
1064            return;
1065        };
1066
1067        self.request_token_usage
1068            .insert(last_user_message.id.clone(), update);
1069        cx.emit(TokenUsageUpdated(self.latest_token_usage()));
1070        cx.notify();
1071    }
1072
1073    pub fn truncate(&mut self, message_id: UserMessageId, cx: &mut Context<Self>) -> Result<()> {
1074        self.cancel(cx);
1075        let Some(position) = self.messages.iter().position(
1076            |msg| matches!(msg, Message::User(UserMessage { id, .. }) if id == &message_id),
1077        ) else {
1078            return Err(anyhow!("Message not found"));
1079        };
1080
1081        for message in self.messages.drain(position..) {
1082            match message {
1083                Message::User(message) => {
1084                    self.request_token_usage.remove(&message.id);
1085                }
1086                Message::Agent(_) | Message::Resume => {}
1087            }
1088        }
1089        self.summary = None;
1090        cx.notify();
1091        Ok(())
1092    }
1093
1094    pub fn latest_token_usage(&self) -> Option<acp_thread::TokenUsage> {
1095        let last_user_message = self.last_user_message()?;
1096        let tokens = self.request_token_usage.get(&last_user_message.id)?;
1097        let model = self.model.clone()?;
1098
1099        Some(acp_thread::TokenUsage {
1100            max_tokens: model.max_token_count_for_mode(self.completion_mode.into()),
1101            used_tokens: tokens.total_tokens(),
1102        })
1103    }
1104
1105    pub fn resume(
1106        &mut self,
1107        cx: &mut Context<Self>,
1108    ) -> Result<mpsc::UnboundedReceiver<Result<ThreadEvent>>> {
1109        self.messages.push(Message::Resume);
1110        cx.notify();
1111
1112        log::debug!("Total messages in thread: {}", self.messages.len());
1113        self.run_turn(cx)
1114    }
1115
1116    /// Sending a message results in the model streaming a response, which could include tool calls.
1117    /// After calling tools, the model will stops and waits for any outstanding tool calls to be completed and their results sent.
1118    /// The returned channel will report all the occurrences in which the model stops before erroring or ending its turn.
1119    pub fn send<T>(
1120        &mut self,
1121        id: UserMessageId,
1122        content: impl IntoIterator<Item = T>,
1123        cx: &mut Context<Self>,
1124    ) -> Result<mpsc::UnboundedReceiver<Result<ThreadEvent>>>
1125    where
1126        T: Into<UserMessageContent>,
1127    {
1128        let model = self.model().context("No language model configured")?;
1129
1130        log::info!("Thread::send called with model: {}", model.name().0);
1131        self.advance_prompt_id();
1132
1133        let content = content.into_iter().map(Into::into).collect::<Vec<_>>();
1134        log::debug!("Thread::send content: {:?}", content);
1135
1136        self.messages
1137            .push(Message::User(UserMessage { id, content }));
1138        cx.notify();
1139
1140        log::debug!("Total messages in thread: {}", self.messages.len());
1141        self.run_turn(cx)
1142    }
1143
1144    fn run_turn(
1145        &mut self,
1146        cx: &mut Context<Self>,
1147    ) -> Result<mpsc::UnboundedReceiver<Result<ThreadEvent>>> {
1148        self.cancel(cx);
1149
1150        let model = self.model.clone().context("No language model configured")?;
1151        let profile = AgentSettings::get_global(cx)
1152            .profiles
1153            .get(&self.profile_id)
1154            .context("Profile not found")?;
1155        let (events_tx, events_rx) = mpsc::unbounded::<Result<ThreadEvent>>();
1156        let event_stream = ThreadEventStream(events_tx);
1157        let message_ix = self.messages.len().saturating_sub(1);
1158        self.tool_use_limit_reached = false;
1159        self.summary = None;
1160        self.running_turn = Some(RunningTurn {
1161            event_stream: event_stream.clone(),
1162            tools: self.enabled_tools(profile, &model, cx),
1163            _task: cx.spawn(async move |this, cx| {
1164                log::debug!("Starting agent turn execution");
1165
1166                let turn_result = Self::run_turn_internal(&this, model, &event_stream, cx).await;
1167                _ = this.update(cx, |this, cx| this.flush_pending_message(cx));
1168
1169                match turn_result {
1170                    Ok(()) => {
1171                        log::debug!("Turn execution completed");
1172                        event_stream.send_stop(acp::StopReason::EndTurn);
1173                    }
1174                    Err(error) => {
1175                        log::error!("Turn execution failed: {:?}", error);
1176                        match error.downcast::<CompletionError>() {
1177                            Ok(CompletionError::Refusal) => {
1178                                event_stream.send_stop(acp::StopReason::Refusal);
1179                                _ = this.update(cx, |this, _| this.messages.truncate(message_ix));
1180                            }
1181                            Ok(CompletionError::MaxTokens) => {
1182                                event_stream.send_stop(acp::StopReason::MaxTokens);
1183                            }
1184                            Ok(CompletionError::Other(error)) | Err(error) => {
1185                                event_stream.send_error(error);
1186                            }
1187                        }
1188                    }
1189                }
1190
1191                _ = this.update(cx, |this, _| this.running_turn.take());
1192            }),
1193        });
1194        Ok(events_rx)
1195    }
1196
1197    async fn run_turn_internal(
1198        this: &WeakEntity<Self>,
1199        model: Arc<dyn LanguageModel>,
1200        event_stream: &ThreadEventStream,
1201        cx: &mut AsyncApp,
1202    ) -> Result<()> {
1203        let mut attempt = 0;
1204        let mut intent = CompletionIntent::UserPrompt;
1205        loop {
1206            let request =
1207                this.update(cx, |this, cx| this.build_completion_request(intent, cx))??;
1208
1209            telemetry::event!(
1210                "Agent Thread Completion",
1211                thread_id = this.read_with(cx, |this, _| this.id.to_string())?,
1212                prompt_id = this.read_with(cx, |this, _| this.prompt_id.to_string())?,
1213                model = model.telemetry_id(),
1214                model_provider = model.provider_id().to_string(),
1215                attempt
1216            );
1217
1218            log::debug!("Calling model.stream_completion, attempt {}", attempt);
1219            let mut events = model
1220                .stream_completion(request, cx)
1221                .await
1222                .map_err(|error| anyhow!(error))?;
1223            let mut tool_results = FuturesUnordered::new();
1224            let mut error = None;
1225            while let Some(event) = events.next().await {
1226                log::trace!("Received completion event: {:?}", event);
1227                match event {
1228                    Ok(event) => {
1229                        tool_results.extend(this.update(cx, |this, cx| {
1230                            this.handle_completion_event(event, event_stream, cx)
1231                        })??);
1232                    }
1233                    Err(err) => {
1234                        error = Some(err);
1235                        break;
1236                    }
1237                }
1238            }
1239
1240            let end_turn = tool_results.is_empty();
1241            while let Some(tool_result) = tool_results.next().await {
1242                log::debug!("Tool finished {:?}", tool_result);
1243
1244                event_stream.update_tool_call_fields(
1245                    &tool_result.tool_use_id,
1246                    acp::ToolCallUpdateFields {
1247                        status: Some(if tool_result.is_error {
1248                            acp::ToolCallStatus::Failed
1249                        } else {
1250                            acp::ToolCallStatus::Completed
1251                        }),
1252                        raw_output: tool_result.output.clone(),
1253                        ..Default::default()
1254                    },
1255                );
1256                this.update(cx, |this, _cx| {
1257                    this.pending_message()
1258                        .tool_results
1259                        .insert(tool_result.tool_use_id.clone(), tool_result);
1260                })?;
1261            }
1262
1263            this.update(cx, |this, cx| {
1264                this.flush_pending_message(cx);
1265                if this.title.is_none() && this.pending_title_generation.is_none() {
1266                    this.generate_title(cx);
1267                }
1268            })?;
1269
1270            if let Some(error) = error {
1271                attempt += 1;
1272                let retry =
1273                    this.update(cx, |this, _| this.handle_completion_error(error, attempt))??;
1274                let timer = cx.background_executor().timer(retry.duration);
1275                event_stream.send_retry(retry);
1276                timer.await;
1277                this.update(cx, |this, _cx| {
1278                    if let Some(Message::Agent(message)) = this.messages.last() {
1279                        if message.tool_results.is_empty() {
1280                            intent = CompletionIntent::UserPrompt;
1281                            this.messages.push(Message::Resume);
1282                        }
1283                    }
1284                })?;
1285            } else if this.read_with(cx, |this, _| this.tool_use_limit_reached)? {
1286                return Err(language_model::ToolUseLimitReachedError.into());
1287            } else if end_turn {
1288                return Ok(());
1289            } else {
1290                intent = CompletionIntent::ToolResults;
1291                attempt = 0;
1292            }
1293        }
1294    }
1295
1296    fn handle_completion_error(
1297        &mut self,
1298        error: LanguageModelCompletionError,
1299        attempt: u8,
1300    ) -> Result<acp_thread::RetryStatus> {
1301        if self.completion_mode == CompletionMode::Normal {
1302            return Err(anyhow!(error));
1303        }
1304
1305        let Some(strategy) = Self::retry_strategy_for(&error) else {
1306            return Err(anyhow!(error));
1307        };
1308
1309        let max_attempts = match &strategy {
1310            RetryStrategy::ExponentialBackoff { max_attempts, .. } => *max_attempts,
1311            RetryStrategy::Fixed { max_attempts, .. } => *max_attempts,
1312        };
1313
1314        if attempt > max_attempts {
1315            return Err(anyhow!(error));
1316        }
1317
1318        let delay = match &strategy {
1319            RetryStrategy::ExponentialBackoff { initial_delay, .. } => {
1320                let delay_secs = initial_delay.as_secs() * 2u64.pow((attempt - 1) as u32);
1321                Duration::from_secs(delay_secs)
1322            }
1323            RetryStrategy::Fixed { delay, .. } => *delay,
1324        };
1325        log::debug!("Retry attempt {attempt} with delay {delay:?}");
1326
1327        Ok(acp_thread::RetryStatus {
1328            last_error: error.to_string().into(),
1329            attempt: attempt as usize,
1330            max_attempts: max_attempts as usize,
1331            started_at: Instant::now(),
1332            duration: delay,
1333        })
1334    }
1335
1336    /// A helper method that's called on every streamed completion event.
1337    /// Returns an optional tool result task, which the main agentic loop will
1338    /// send back to the model when it resolves.
1339    fn handle_completion_event(
1340        &mut self,
1341        event: LanguageModelCompletionEvent,
1342        event_stream: &ThreadEventStream,
1343        cx: &mut Context<Self>,
1344    ) -> Result<Option<Task<LanguageModelToolResult>>> {
1345        log::trace!("Handling streamed completion event: {:?}", event);
1346        use LanguageModelCompletionEvent::*;
1347
1348        match event {
1349            StartMessage { .. } => {
1350                self.flush_pending_message(cx);
1351                self.pending_message = Some(AgentMessage::default());
1352            }
1353            Text(new_text) => self.handle_text_event(new_text, event_stream, cx),
1354            Thinking { text, signature } => {
1355                self.handle_thinking_event(text, signature, event_stream, cx)
1356            }
1357            RedactedThinking { data } => self.handle_redacted_thinking_event(data, cx),
1358            ToolUse(tool_use) => {
1359                return Ok(self.handle_tool_use_event(tool_use, event_stream, cx));
1360            }
1361            ToolUseJsonParseError {
1362                id,
1363                tool_name,
1364                raw_input,
1365                json_parse_error,
1366            } => {
1367                return Ok(Some(Task::ready(
1368                    self.handle_tool_use_json_parse_error_event(
1369                        id,
1370                        tool_name,
1371                        raw_input,
1372                        json_parse_error,
1373                    ),
1374                )));
1375            }
1376            UsageUpdate(usage) => {
1377                telemetry::event!(
1378                    "Agent Thread Completion Usage Updated",
1379                    thread_id = self.id.to_string(),
1380                    prompt_id = self.prompt_id.to_string(),
1381                    model = self.model.as_ref().map(|m| m.telemetry_id()),
1382                    model_provider = self.model.as_ref().map(|m| m.provider_id().to_string()),
1383                    input_tokens = usage.input_tokens,
1384                    output_tokens = usage.output_tokens,
1385                    cache_creation_input_tokens = usage.cache_creation_input_tokens,
1386                    cache_read_input_tokens = usage.cache_read_input_tokens,
1387                );
1388                self.update_token_usage(usage, cx);
1389            }
1390            StatusUpdate(CompletionRequestStatus::UsageUpdated { amount, limit }) => {
1391                self.update_model_request_usage(amount, limit, cx);
1392            }
1393            StatusUpdate(
1394                CompletionRequestStatus::Started
1395                | CompletionRequestStatus::Queued { .. }
1396                | CompletionRequestStatus::Failed { .. },
1397            ) => {}
1398            StatusUpdate(CompletionRequestStatus::ToolUseLimitReached) => {
1399                self.tool_use_limit_reached = true;
1400            }
1401            Stop(StopReason::Refusal) => return Err(CompletionError::Refusal.into()),
1402            Stop(StopReason::MaxTokens) => return Err(CompletionError::MaxTokens.into()),
1403            Stop(StopReason::ToolUse | StopReason::EndTurn) => {}
1404        }
1405
1406        Ok(None)
1407    }
1408
1409    fn handle_text_event(
1410        &mut self,
1411        new_text: String,
1412        event_stream: &ThreadEventStream,
1413        cx: &mut Context<Self>,
1414    ) {
1415        event_stream.send_text(&new_text);
1416
1417        let last_message = self.pending_message();
1418        if let Some(AgentMessageContent::Text(text)) = last_message.content.last_mut() {
1419            text.push_str(&new_text);
1420        } else {
1421            last_message
1422                .content
1423                .push(AgentMessageContent::Text(new_text));
1424        }
1425
1426        cx.notify();
1427    }
1428
1429    fn handle_thinking_event(
1430        &mut self,
1431        new_text: String,
1432        new_signature: Option<String>,
1433        event_stream: &ThreadEventStream,
1434        cx: &mut Context<Self>,
1435    ) {
1436        event_stream.send_thinking(&new_text);
1437
1438        let last_message = self.pending_message();
1439        if let Some(AgentMessageContent::Thinking { text, signature }) =
1440            last_message.content.last_mut()
1441        {
1442            text.push_str(&new_text);
1443            *signature = new_signature.or(signature.take());
1444        } else {
1445            last_message.content.push(AgentMessageContent::Thinking {
1446                text: new_text,
1447                signature: new_signature,
1448            });
1449        }
1450
1451        cx.notify();
1452    }
1453
1454    fn handle_redacted_thinking_event(&mut self, data: String, cx: &mut Context<Self>) {
1455        let last_message = self.pending_message();
1456        last_message
1457            .content
1458            .push(AgentMessageContent::RedactedThinking(data));
1459        cx.notify();
1460    }
1461
1462    fn handle_tool_use_event(
1463        &mut self,
1464        tool_use: LanguageModelToolUse,
1465        event_stream: &ThreadEventStream,
1466        cx: &mut Context<Self>,
1467    ) -> Option<Task<LanguageModelToolResult>> {
1468        cx.notify();
1469
1470        let tool = self.tool(tool_use.name.as_ref());
1471        let mut title = SharedString::from(&tool_use.name);
1472        let mut kind = acp::ToolKind::Other;
1473        if let Some(tool) = tool.as_ref() {
1474            title = tool.initial_title(tool_use.input.clone());
1475            kind = tool.kind();
1476        }
1477
1478        // Ensure the last message ends in the current tool use
1479        let last_message = self.pending_message();
1480        let push_new_tool_use = last_message.content.last_mut().is_none_or(|content| {
1481            if let AgentMessageContent::ToolUse(last_tool_use) = content {
1482                if last_tool_use.id == tool_use.id {
1483                    *last_tool_use = tool_use.clone();
1484                    false
1485                } else {
1486                    true
1487                }
1488            } else {
1489                true
1490            }
1491        });
1492
1493        if push_new_tool_use {
1494            event_stream.send_tool_call(&tool_use.id, title, kind, tool_use.input.clone());
1495            last_message
1496                .content
1497                .push(AgentMessageContent::ToolUse(tool_use.clone()));
1498        } else {
1499            event_stream.update_tool_call_fields(
1500                &tool_use.id,
1501                acp::ToolCallUpdateFields {
1502                    title: Some(title.into()),
1503                    kind: Some(kind),
1504                    raw_input: Some(tool_use.input.clone()),
1505                    ..Default::default()
1506                },
1507            );
1508        }
1509
1510        if !tool_use.is_input_complete {
1511            return None;
1512        }
1513
1514        let Some(tool) = tool else {
1515            let content = format!("No tool named {} exists", tool_use.name);
1516            return Some(Task::ready(LanguageModelToolResult {
1517                content: LanguageModelToolResultContent::Text(Arc::from(content)),
1518                tool_use_id: tool_use.id,
1519                tool_name: tool_use.name,
1520                is_error: true,
1521                output: None,
1522            }));
1523        };
1524
1525        let fs = self.project.read(cx).fs().clone();
1526        let tool_event_stream =
1527            ToolCallEventStream::new(tool_use.id.clone(), event_stream.clone(), Some(fs));
1528        tool_event_stream.update_fields(acp::ToolCallUpdateFields {
1529            status: Some(acp::ToolCallStatus::InProgress),
1530            ..Default::default()
1531        });
1532        let supports_images = self.model().is_some_and(|model| model.supports_images());
1533        let tool_result = tool.run(tool_use.input, tool_event_stream, cx);
1534        log::debug!("Running tool {}", tool_use.name);
1535        Some(cx.foreground_executor().spawn(async move {
1536            let tool_result = tool_result.await.and_then(|output| {
1537                if let LanguageModelToolResultContent::Image(_) = &output.llm_output
1538                    && !supports_images
1539                {
1540                    return Err(anyhow!(
1541                        "Attempted to read an image, but this model doesn't support it.",
1542                    ));
1543                }
1544                Ok(output)
1545            });
1546
1547            match tool_result {
1548                Ok(output) => LanguageModelToolResult {
1549                    tool_use_id: tool_use.id,
1550                    tool_name: tool_use.name,
1551                    is_error: false,
1552                    content: output.llm_output,
1553                    output: Some(output.raw_output),
1554                },
1555                Err(error) => LanguageModelToolResult {
1556                    tool_use_id: tool_use.id,
1557                    tool_name: tool_use.name,
1558                    is_error: true,
1559                    content: LanguageModelToolResultContent::Text(Arc::from(error.to_string())),
1560                    output: None,
1561                },
1562            }
1563        }))
1564    }
1565
1566    fn handle_tool_use_json_parse_error_event(
1567        &mut self,
1568        tool_use_id: LanguageModelToolUseId,
1569        tool_name: Arc<str>,
1570        raw_input: Arc<str>,
1571        json_parse_error: String,
1572    ) -> LanguageModelToolResult {
1573        let tool_output = format!("Error parsing input JSON: {json_parse_error}");
1574        LanguageModelToolResult {
1575            tool_use_id,
1576            tool_name,
1577            is_error: true,
1578            content: LanguageModelToolResultContent::Text(tool_output.into()),
1579            output: Some(serde_json::Value::String(raw_input.to_string())),
1580        }
1581    }
1582
1583    fn update_model_request_usage(&self, amount: usize, limit: UsageLimit, cx: &mut Context<Self>) {
1584        self.project
1585            .read(cx)
1586            .user_store()
1587            .update(cx, |user_store, cx| {
1588                user_store.update_model_request_usage(
1589                    ModelRequestUsage(RequestUsage {
1590                        amount: amount as i32,
1591                        limit,
1592                    }),
1593                    cx,
1594                )
1595            });
1596    }
1597
1598    pub fn title(&self) -> SharedString {
1599        self.title.clone().unwrap_or("New Thread".into())
1600    }
1601
1602    pub fn summary(&mut self, cx: &mut Context<Self>) -> Task<Result<SharedString>> {
1603        if let Some(summary) = self.summary.as_ref() {
1604            return Task::ready(Ok(summary.clone()));
1605        }
1606        let Some(model) = self.summarization_model.clone() else {
1607            return Task::ready(Err(anyhow!("No summarization model available")));
1608        };
1609        let mut request = LanguageModelRequest {
1610            intent: Some(CompletionIntent::ThreadContextSummarization),
1611            temperature: AgentSettings::temperature_for_model(&model, cx),
1612            ..Default::default()
1613        };
1614
1615        for message in &self.messages {
1616            request.messages.extend(message.to_request());
1617        }
1618
1619        request.messages.push(LanguageModelRequestMessage {
1620            role: Role::User,
1621            content: vec![SUMMARIZE_THREAD_DETAILED_PROMPT.into()],
1622            cache: false,
1623        });
1624        cx.spawn(async move |this, cx| {
1625            let mut summary = String::new();
1626            let mut messages = model.stream_completion(request, cx).await?;
1627            while let Some(event) = messages.next().await {
1628                let event = event?;
1629                let text = match event {
1630                    LanguageModelCompletionEvent::Text(text) => text,
1631                    LanguageModelCompletionEvent::StatusUpdate(
1632                        CompletionRequestStatus::UsageUpdated { amount, limit },
1633                    ) => {
1634                        this.update(cx, |thread, cx| {
1635                            thread.update_model_request_usage(amount, limit, cx);
1636                        })?;
1637                        continue;
1638                    }
1639                    _ => continue,
1640                };
1641
1642                let mut lines = text.lines();
1643                summary.extend(lines.next());
1644            }
1645
1646            log::debug!("Setting summary: {}", summary);
1647            let summary = SharedString::from(summary);
1648
1649            this.update(cx, |this, cx| {
1650                this.summary = Some(summary.clone());
1651                cx.notify()
1652            })?;
1653
1654            Ok(summary)
1655        })
1656    }
1657
1658    fn generate_title(&mut self, cx: &mut Context<Self>) {
1659        let Some(model) = self.summarization_model.clone() else {
1660            return;
1661        };
1662
1663        log::debug!(
1664            "Generating title with model: {:?}",
1665            self.summarization_model.as_ref().map(|model| model.name())
1666        );
1667        let mut request = LanguageModelRequest {
1668            intent: Some(CompletionIntent::ThreadSummarization),
1669            temperature: AgentSettings::temperature_for_model(&model, cx),
1670            ..Default::default()
1671        };
1672
1673        for message in &self.messages {
1674            request.messages.extend(message.to_request());
1675        }
1676
1677        request.messages.push(LanguageModelRequestMessage {
1678            role: Role::User,
1679            content: vec![SUMMARIZE_THREAD_PROMPT.into()],
1680            cache: false,
1681        });
1682        self.pending_title_generation = Some(cx.spawn(async move |this, cx| {
1683            let mut title = String::new();
1684
1685            let generate = async {
1686                let mut messages = model.stream_completion(request, cx).await?;
1687                while let Some(event) = messages.next().await {
1688                    let event = event?;
1689                    let text = match event {
1690                        LanguageModelCompletionEvent::Text(text) => text,
1691                        LanguageModelCompletionEvent::StatusUpdate(
1692                            CompletionRequestStatus::UsageUpdated { amount, limit },
1693                        ) => {
1694                            this.update(cx, |thread, cx| {
1695                                thread.update_model_request_usage(amount, limit, cx);
1696                            })?;
1697                            continue;
1698                        }
1699                        _ => continue,
1700                    };
1701
1702                    let mut lines = text.lines();
1703                    title.extend(lines.next());
1704
1705                    // Stop if the LLM generated multiple lines.
1706                    if lines.next().is_some() {
1707                        break;
1708                    }
1709                }
1710                anyhow::Ok(())
1711            };
1712
1713            if generate.await.context("failed to generate title").is_ok() {
1714                _ = this.update(cx, |this, cx| this.set_title(title.into(), cx));
1715            }
1716            _ = this.update(cx, |this, _| this.pending_title_generation = None);
1717        }));
1718    }
1719
1720    pub fn set_title(&mut self, title: SharedString, cx: &mut Context<Self>) {
1721        self.pending_title_generation = None;
1722        if Some(&title) != self.title.as_ref() {
1723            self.title = Some(title);
1724            cx.emit(TitleUpdated);
1725            cx.notify();
1726        }
1727    }
1728
1729    fn last_user_message(&self) -> Option<&UserMessage> {
1730        self.messages
1731            .iter()
1732            .rev()
1733            .find_map(|message| match message {
1734                Message::User(user_message) => Some(user_message),
1735                Message::Agent(_) => None,
1736                Message::Resume => None,
1737            })
1738    }
1739
1740    fn pending_message(&mut self) -> &mut AgentMessage {
1741        self.pending_message.get_or_insert_default()
1742    }
1743
1744    fn flush_pending_message(&mut self, cx: &mut Context<Self>) {
1745        let Some(mut message) = self.pending_message.take() else {
1746            return;
1747        };
1748
1749        if message.content.is_empty() {
1750            return;
1751        }
1752
1753        for content in &message.content {
1754            let AgentMessageContent::ToolUse(tool_use) = content else {
1755                continue;
1756            };
1757
1758            if !message.tool_results.contains_key(&tool_use.id) {
1759                message.tool_results.insert(
1760                    tool_use.id.clone(),
1761                    LanguageModelToolResult {
1762                        tool_use_id: tool_use.id.clone(),
1763                        tool_name: tool_use.name.clone(),
1764                        is_error: true,
1765                        content: LanguageModelToolResultContent::Text(TOOL_CANCELED_MESSAGE.into()),
1766                        output: None,
1767                    },
1768                );
1769            }
1770        }
1771
1772        self.messages.push(Message::Agent(message));
1773        self.updated_at = Utc::now();
1774        self.summary = None;
1775        cx.notify()
1776    }
1777
1778    pub(crate) fn build_completion_request(
1779        &self,
1780        completion_intent: CompletionIntent,
1781        cx: &App,
1782    ) -> Result<LanguageModelRequest> {
1783        let model = self.model().context("No language model configured")?;
1784        let tools = if let Some(turn) = self.running_turn.as_ref() {
1785            turn.tools
1786                .iter()
1787                .filter_map(|(tool_name, tool)| {
1788                    log::trace!("Including tool: {}", tool_name);
1789                    Some(LanguageModelRequestTool {
1790                        name: tool_name.to_string(),
1791                        description: tool.description().to_string(),
1792                        input_schema: tool.input_schema(model.tool_input_format()).log_err()?,
1793                    })
1794                })
1795                .collect::<Vec<_>>()
1796        } else {
1797            Vec::new()
1798        };
1799
1800        log::debug!("Building completion request");
1801        log::debug!("Completion intent: {:?}", completion_intent);
1802        log::debug!("Completion mode: {:?}", self.completion_mode);
1803
1804        let messages = self.build_request_messages(cx);
1805        log::debug!("Request will include {} messages", messages.len());
1806        log::debug!("Request includes {} tools", tools.len());
1807
1808        let request = LanguageModelRequest {
1809            thread_id: Some(self.id.to_string()),
1810            prompt_id: Some(self.prompt_id.to_string()),
1811            intent: Some(completion_intent),
1812            mode: Some(self.completion_mode.into()),
1813            messages,
1814            tools,
1815            tool_choice: None,
1816            stop: Vec::new(),
1817            temperature: AgentSettings::temperature_for_model(model, cx),
1818            thinking_allowed: true,
1819        };
1820
1821        log::debug!("Completion request built successfully");
1822        Ok(request)
1823    }
1824
1825    fn enabled_tools(
1826        &self,
1827        profile: &AgentProfileSettings,
1828        model: &Arc<dyn LanguageModel>,
1829        cx: &App,
1830    ) -> BTreeMap<SharedString, Arc<dyn AnyAgentTool>> {
1831        fn truncate(tool_name: &SharedString) -> SharedString {
1832            if tool_name.len() > MAX_TOOL_NAME_LENGTH {
1833                let mut truncated = tool_name.to_string();
1834                truncated.truncate(MAX_TOOL_NAME_LENGTH);
1835                truncated.into()
1836            } else {
1837                tool_name.clone()
1838            }
1839        }
1840
1841        let mut tools = self
1842            .tools
1843            .iter()
1844            .filter_map(|(tool_name, tool)| {
1845                if tool.supported_provider(&model.provider_id())
1846                    && profile.is_tool_enabled(tool_name)
1847                {
1848                    Some((truncate(tool_name), tool.clone()))
1849                } else {
1850                    None
1851                }
1852            })
1853            .collect::<BTreeMap<_, _>>();
1854
1855        let mut context_server_tools = Vec::new();
1856        let mut seen_tools = tools.keys().cloned().collect::<HashSet<_>>();
1857        let mut duplicate_tool_names = HashSet::default();
1858        for (server_id, server_tools) in self.context_server_registry.read(cx).servers() {
1859            for (tool_name, tool) in server_tools {
1860                if profile.is_context_server_tool_enabled(&server_id.0, &tool_name) {
1861                    let tool_name = truncate(tool_name);
1862                    if !seen_tools.insert(tool_name.clone()) {
1863                        duplicate_tool_names.insert(tool_name.clone());
1864                    }
1865                    context_server_tools.push((server_id.clone(), tool_name, tool.clone()));
1866                }
1867            }
1868        }
1869
1870        // When there are duplicate tool names, disambiguate by prefixing them
1871        // with the server ID. In the rare case there isn't enough space for the
1872        // disambiguated tool name, keep only the last tool with this name.
1873        for (server_id, tool_name, tool) in context_server_tools {
1874            if duplicate_tool_names.contains(&tool_name) {
1875                let available = MAX_TOOL_NAME_LENGTH.saturating_sub(tool_name.len());
1876                if available >= 2 {
1877                    let mut disambiguated = server_id.0.to_string();
1878                    disambiguated.truncate(available - 1);
1879                    disambiguated.push('_');
1880                    disambiguated.push_str(&tool_name);
1881                    tools.insert(disambiguated.into(), tool.clone());
1882                } else {
1883                    tools.insert(tool_name, tool.clone());
1884                }
1885            } else {
1886                tools.insert(tool_name, tool.clone());
1887            }
1888        }
1889
1890        tools
1891    }
1892
1893    fn tool(&self, name: &str) -> Option<Arc<dyn AnyAgentTool>> {
1894        self.running_turn.as_ref()?.tools.get(name).cloned()
1895    }
1896
1897    fn build_request_messages(&self, cx: &App) -> Vec<LanguageModelRequestMessage> {
1898        log::trace!(
1899            "Building request messages from {} thread messages",
1900            self.messages.len()
1901        );
1902
1903        let system_prompt = SystemPromptTemplate {
1904            project: self.project_context.read(cx),
1905            available_tools: self.tools.keys().cloned().collect(),
1906        }
1907        .render(&self.templates)
1908        .context("failed to build system prompt")
1909        .expect("Invalid template");
1910        let mut messages = vec![LanguageModelRequestMessage {
1911            role: Role::System,
1912            content: vec![system_prompt.into()],
1913            cache: false,
1914        }];
1915        for message in &self.messages {
1916            messages.extend(message.to_request());
1917        }
1918
1919        if let Some(last_message) = messages.last_mut() {
1920            last_message.cache = true;
1921        }
1922
1923        if let Some(message) = self.pending_message.as_ref() {
1924            messages.extend(message.to_request());
1925        }
1926
1927        messages
1928    }
1929
1930    pub fn to_markdown(&self) -> String {
1931        let mut markdown = String::new();
1932        for (ix, message) in self.messages.iter().enumerate() {
1933            if ix > 0 {
1934                markdown.push('\n');
1935            }
1936            markdown.push_str(&message.to_markdown());
1937        }
1938
1939        if let Some(message) = self.pending_message.as_ref() {
1940            markdown.push('\n');
1941            markdown.push_str(&message.to_markdown());
1942        }
1943
1944        markdown
1945    }
1946
1947    fn advance_prompt_id(&mut self) {
1948        self.prompt_id = PromptId::new();
1949    }
1950
1951    fn retry_strategy_for(error: &LanguageModelCompletionError) -> Option<RetryStrategy> {
1952        use LanguageModelCompletionError::*;
1953        use http_client::StatusCode;
1954
1955        // General strategy here:
1956        // - If retrying won't help (e.g. invalid API key or payload too large), return None so we don't retry at all.
1957        // - If it's a time-based issue (e.g. server overloaded, rate limit exceeded), retry up to 4 times with exponential backoff.
1958        // - If it's an issue that *might* be fixed by retrying (e.g. internal server error), retry up to 3 times.
1959        match error {
1960            HttpResponseError {
1961                status_code: StatusCode::TOO_MANY_REQUESTS,
1962                ..
1963            } => Some(RetryStrategy::ExponentialBackoff {
1964                initial_delay: BASE_RETRY_DELAY,
1965                max_attempts: MAX_RETRY_ATTEMPTS,
1966            }),
1967            ServerOverloaded { retry_after, .. } | RateLimitExceeded { retry_after, .. } => {
1968                Some(RetryStrategy::Fixed {
1969                    delay: retry_after.unwrap_or(BASE_RETRY_DELAY),
1970                    max_attempts: MAX_RETRY_ATTEMPTS,
1971                })
1972            }
1973            UpstreamProviderError {
1974                status,
1975                retry_after,
1976                ..
1977            } => match *status {
1978                StatusCode::TOO_MANY_REQUESTS | StatusCode::SERVICE_UNAVAILABLE => {
1979                    Some(RetryStrategy::Fixed {
1980                        delay: retry_after.unwrap_or(BASE_RETRY_DELAY),
1981                        max_attempts: MAX_RETRY_ATTEMPTS,
1982                    })
1983                }
1984                StatusCode::INTERNAL_SERVER_ERROR => Some(RetryStrategy::Fixed {
1985                    delay: retry_after.unwrap_or(BASE_RETRY_DELAY),
1986                    // Internal Server Error could be anything, retry up to 3 times.
1987                    max_attempts: 3,
1988                }),
1989                status => {
1990                    // There is no StatusCode variant for the unofficial HTTP 529 ("The service is overloaded"),
1991                    // but we frequently get them in practice. See https://http.dev/529
1992                    if status.as_u16() == 529 {
1993                        Some(RetryStrategy::Fixed {
1994                            delay: retry_after.unwrap_or(BASE_RETRY_DELAY),
1995                            max_attempts: MAX_RETRY_ATTEMPTS,
1996                        })
1997                    } else {
1998                        Some(RetryStrategy::Fixed {
1999                            delay: retry_after.unwrap_or(BASE_RETRY_DELAY),
2000                            max_attempts: 2,
2001                        })
2002                    }
2003                }
2004            },
2005            ApiInternalServerError { .. } => Some(RetryStrategy::Fixed {
2006                delay: BASE_RETRY_DELAY,
2007                max_attempts: 3,
2008            }),
2009            ApiReadResponseError { .. }
2010            | HttpSend { .. }
2011            | DeserializeResponse { .. }
2012            | BadRequestFormat { .. } => Some(RetryStrategy::Fixed {
2013                delay: BASE_RETRY_DELAY,
2014                max_attempts: 3,
2015            }),
2016            // Retrying these errors definitely shouldn't help.
2017            HttpResponseError {
2018                status_code:
2019                    StatusCode::PAYLOAD_TOO_LARGE | StatusCode::FORBIDDEN | StatusCode::UNAUTHORIZED,
2020                ..
2021            }
2022            | AuthenticationError { .. }
2023            | PermissionError { .. }
2024            | NoApiKey { .. }
2025            | ApiEndpointNotFound { .. }
2026            | PromptTooLarge { .. } => None,
2027            // These errors might be transient, so retry them
2028            SerializeRequest { .. } | BuildRequestBody { .. } => Some(RetryStrategy::Fixed {
2029                delay: BASE_RETRY_DELAY,
2030                max_attempts: 1,
2031            }),
2032            // Retry all other 4xx and 5xx errors once.
2033            HttpResponseError { status_code, .. }
2034                if status_code.is_client_error() || status_code.is_server_error() =>
2035            {
2036                Some(RetryStrategy::Fixed {
2037                    delay: BASE_RETRY_DELAY,
2038                    max_attempts: 3,
2039                })
2040            }
2041            Other(err)
2042                if err.is::<language_model::PaymentRequiredError>()
2043                    || err.is::<language_model::ModelRequestLimitReachedError>() =>
2044            {
2045                // Retrying won't help for Payment Required or Model Request Limit errors (where
2046                // the user must upgrade to usage-based billing to get more requests, or else wait
2047                // for a significant amount of time for the request limit to reset).
2048                None
2049            }
2050            // Conservatively assume that any other errors are non-retryable
2051            HttpResponseError { .. } | Other(..) => Some(RetryStrategy::Fixed {
2052                delay: BASE_RETRY_DELAY,
2053                max_attempts: 2,
2054            }),
2055        }
2056    }
2057}
2058
2059struct RunningTurn {
2060    /// Holds the task that handles agent interaction until the end of the turn.
2061    /// Survives across multiple requests as the model performs tool calls and
2062    /// we run tools, report their results.
2063    _task: Task<()>,
2064    /// The current event stream for the running turn. Used to report a final
2065    /// cancellation event if we cancel the turn.
2066    event_stream: ThreadEventStream,
2067    /// The tools that were enabled for this turn.
2068    tools: BTreeMap<SharedString, Arc<dyn AnyAgentTool>>,
2069}
2070
2071impl RunningTurn {
2072    fn cancel(self) {
2073        log::debug!("Cancelling in progress turn");
2074        self.event_stream.send_canceled();
2075    }
2076}
2077
2078pub struct TokenUsageUpdated(pub Option<acp_thread::TokenUsage>);
2079
2080impl EventEmitter<TokenUsageUpdated> for Thread {}
2081
2082pub struct TitleUpdated;
2083
2084impl EventEmitter<TitleUpdated> for Thread {}
2085
2086pub trait AgentTool
2087where
2088    Self: 'static + Sized,
2089{
2090    type Input: for<'de> Deserialize<'de> + Serialize + JsonSchema;
2091    type Output: for<'de> Deserialize<'de> + Serialize + Into<LanguageModelToolResultContent>;
2092
2093    fn name() -> &'static str;
2094
2095    fn description(&self) -> SharedString {
2096        let schema = schemars::schema_for!(Self::Input);
2097        SharedString::new(
2098            schema
2099                .get("description")
2100                .and_then(|description| description.as_str())
2101                .unwrap_or_default(),
2102        )
2103    }
2104
2105    fn kind() -> acp::ToolKind;
2106
2107    /// The initial tool title to display. Can be updated during the tool run.
2108    fn initial_title(&self, input: Result<Self::Input, serde_json::Value>) -> SharedString;
2109
2110    /// Returns the JSON schema that describes the tool's input.
2111    fn input_schema(&self, format: LanguageModelToolSchemaFormat) -> Schema {
2112        crate::tool_schema::root_schema_for::<Self::Input>(format)
2113    }
2114
2115    /// Some tools rely on a provider for the underlying billing or other reasons.
2116    /// Allow the tool to check if they are compatible, or should be filtered out.
2117    fn supported_provider(&self, _provider: &LanguageModelProviderId) -> bool {
2118        true
2119    }
2120
2121    /// Runs the tool with the provided input.
2122    fn run(
2123        self: Arc<Self>,
2124        input: Self::Input,
2125        event_stream: ToolCallEventStream,
2126        cx: &mut App,
2127    ) -> Task<Result<Self::Output>>;
2128
2129    /// Emits events for a previous execution of the tool.
2130    fn replay(
2131        &self,
2132        _input: Self::Input,
2133        _output: Self::Output,
2134        _event_stream: ToolCallEventStream,
2135        _cx: &mut App,
2136    ) -> Result<()> {
2137        Ok(())
2138    }
2139
2140    fn erase(self) -> Arc<dyn AnyAgentTool> {
2141        Arc::new(Erased(Arc::new(self)))
2142    }
2143}
2144
2145pub struct Erased<T>(T);
2146
2147pub struct AgentToolOutput {
2148    pub llm_output: LanguageModelToolResultContent,
2149    pub raw_output: serde_json::Value,
2150}
2151
2152pub trait AnyAgentTool {
2153    fn name(&self) -> SharedString;
2154    fn description(&self) -> SharedString;
2155    fn kind(&self) -> acp::ToolKind;
2156    fn initial_title(&self, input: serde_json::Value) -> SharedString;
2157    fn input_schema(&self, format: LanguageModelToolSchemaFormat) -> Result<serde_json::Value>;
2158    fn supported_provider(&self, _provider: &LanguageModelProviderId) -> bool {
2159        true
2160    }
2161    fn run(
2162        self: Arc<Self>,
2163        input: serde_json::Value,
2164        event_stream: ToolCallEventStream,
2165        cx: &mut App,
2166    ) -> Task<Result<AgentToolOutput>>;
2167    fn replay(
2168        &self,
2169        input: serde_json::Value,
2170        output: serde_json::Value,
2171        event_stream: ToolCallEventStream,
2172        cx: &mut App,
2173    ) -> Result<()>;
2174}
2175
2176impl<T> AnyAgentTool for Erased<Arc<T>>
2177where
2178    T: AgentTool,
2179{
2180    fn name(&self) -> SharedString {
2181        T::name().into()
2182    }
2183
2184    fn description(&self) -> SharedString {
2185        self.0.description()
2186    }
2187
2188    fn kind(&self) -> agent_client_protocol::ToolKind {
2189        T::kind()
2190    }
2191
2192    fn initial_title(&self, input: serde_json::Value) -> SharedString {
2193        let parsed_input = serde_json::from_value(input.clone()).map_err(|_| input);
2194        self.0.initial_title(parsed_input)
2195    }
2196
2197    fn input_schema(&self, format: LanguageModelToolSchemaFormat) -> Result<serde_json::Value> {
2198        let mut json = serde_json::to_value(self.0.input_schema(format))?;
2199        adapt_schema_to_format(&mut json, format)?;
2200        Ok(json)
2201    }
2202
2203    fn supported_provider(&self, provider: &LanguageModelProviderId) -> bool {
2204        self.0.supported_provider(provider)
2205    }
2206
2207    fn run(
2208        self: Arc<Self>,
2209        input: serde_json::Value,
2210        event_stream: ToolCallEventStream,
2211        cx: &mut App,
2212    ) -> Task<Result<AgentToolOutput>> {
2213        cx.spawn(async move |cx| {
2214            let input = serde_json::from_value(input)?;
2215            let output = cx
2216                .update(|cx| self.0.clone().run(input, event_stream, cx))?
2217                .await?;
2218            let raw_output = serde_json::to_value(&output)?;
2219            Ok(AgentToolOutput {
2220                llm_output: output.into(),
2221                raw_output,
2222            })
2223        })
2224    }
2225
2226    fn replay(
2227        &self,
2228        input: serde_json::Value,
2229        output: serde_json::Value,
2230        event_stream: ToolCallEventStream,
2231        cx: &mut App,
2232    ) -> Result<()> {
2233        let input = serde_json::from_value(input)?;
2234        let output = serde_json::from_value(output)?;
2235        self.0.replay(input, output, event_stream, cx)
2236    }
2237}
2238
2239#[derive(Clone)]
2240struct ThreadEventStream(mpsc::UnboundedSender<Result<ThreadEvent>>);
2241
2242impl ThreadEventStream {
2243    fn send_user_message(&self, message: &UserMessage) {
2244        self.0
2245            .unbounded_send(Ok(ThreadEvent::UserMessage(message.clone())))
2246            .ok();
2247    }
2248
2249    fn send_text(&self, text: &str) {
2250        self.0
2251            .unbounded_send(Ok(ThreadEvent::AgentText(text.to_string())))
2252            .ok();
2253    }
2254
2255    fn send_thinking(&self, text: &str) {
2256        self.0
2257            .unbounded_send(Ok(ThreadEvent::AgentThinking(text.to_string())))
2258            .ok();
2259    }
2260
2261    fn send_tool_call(
2262        &self,
2263        id: &LanguageModelToolUseId,
2264        title: SharedString,
2265        kind: acp::ToolKind,
2266        input: serde_json::Value,
2267    ) {
2268        self.0
2269            .unbounded_send(Ok(ThreadEvent::ToolCall(Self::initial_tool_call(
2270                id,
2271                title.to_string(),
2272                kind,
2273                input,
2274            ))))
2275            .ok();
2276    }
2277
2278    fn initial_tool_call(
2279        id: &LanguageModelToolUseId,
2280        title: String,
2281        kind: acp::ToolKind,
2282        input: serde_json::Value,
2283    ) -> acp::ToolCall {
2284        acp::ToolCall {
2285            id: acp::ToolCallId(id.to_string().into()),
2286            title,
2287            kind,
2288            status: acp::ToolCallStatus::Pending,
2289            content: vec![],
2290            locations: vec![],
2291            raw_input: Some(input),
2292            raw_output: None,
2293        }
2294    }
2295
2296    fn update_tool_call_fields(
2297        &self,
2298        tool_use_id: &LanguageModelToolUseId,
2299        fields: acp::ToolCallUpdateFields,
2300    ) {
2301        self.0
2302            .unbounded_send(Ok(ThreadEvent::ToolCallUpdate(
2303                acp::ToolCallUpdate {
2304                    id: acp::ToolCallId(tool_use_id.to_string().into()),
2305                    fields,
2306                }
2307                .into(),
2308            )))
2309            .ok();
2310    }
2311
2312    fn send_retry(&self, status: acp_thread::RetryStatus) {
2313        self.0.unbounded_send(Ok(ThreadEvent::Retry(status))).ok();
2314    }
2315
2316    fn send_stop(&self, reason: acp::StopReason) {
2317        self.0.unbounded_send(Ok(ThreadEvent::Stop(reason))).ok();
2318    }
2319
2320    fn send_canceled(&self) {
2321        self.0
2322            .unbounded_send(Ok(ThreadEvent::Stop(acp::StopReason::Cancelled)))
2323            .ok();
2324    }
2325
2326    fn send_error(&self, error: impl Into<anyhow::Error>) {
2327        self.0.unbounded_send(Err(error.into())).ok();
2328    }
2329}
2330
2331#[derive(Clone)]
2332pub struct ToolCallEventStream {
2333    tool_use_id: LanguageModelToolUseId,
2334    stream: ThreadEventStream,
2335    fs: Option<Arc<dyn Fs>>,
2336}
2337
2338impl ToolCallEventStream {
2339    #[cfg(test)]
2340    pub fn test() -> (Self, ToolCallEventStreamReceiver) {
2341        let (events_tx, events_rx) = mpsc::unbounded::<Result<ThreadEvent>>();
2342
2343        let stream = ToolCallEventStream::new("test_id".into(), ThreadEventStream(events_tx), None);
2344
2345        (stream, ToolCallEventStreamReceiver(events_rx))
2346    }
2347
2348    fn new(
2349        tool_use_id: LanguageModelToolUseId,
2350        stream: ThreadEventStream,
2351        fs: Option<Arc<dyn Fs>>,
2352    ) -> Self {
2353        Self {
2354            tool_use_id,
2355            stream,
2356            fs,
2357        }
2358    }
2359
2360    pub fn update_fields(&self, fields: acp::ToolCallUpdateFields) {
2361        self.stream
2362            .update_tool_call_fields(&self.tool_use_id, fields);
2363    }
2364
2365    pub fn update_diff(&self, diff: Entity<acp_thread::Diff>) {
2366        self.stream
2367            .0
2368            .unbounded_send(Ok(ThreadEvent::ToolCallUpdate(
2369                acp_thread::ToolCallUpdateDiff {
2370                    id: acp::ToolCallId(self.tool_use_id.to_string().into()),
2371                    diff,
2372                }
2373                .into(),
2374            )))
2375            .ok();
2376    }
2377
2378    pub fn update_terminal(&self, terminal: Entity<acp_thread::Terminal>) {
2379        self.stream
2380            .0
2381            .unbounded_send(Ok(ThreadEvent::ToolCallUpdate(
2382                acp_thread::ToolCallUpdateTerminal {
2383                    id: acp::ToolCallId(self.tool_use_id.to_string().into()),
2384                    terminal,
2385                }
2386                .into(),
2387            )))
2388            .ok();
2389    }
2390
2391    pub fn authorize(&self, title: impl Into<String>, cx: &mut App) -> Task<Result<()>> {
2392        if agent_settings::AgentSettings::get_global(cx).always_allow_tool_actions {
2393            return Task::ready(Ok(()));
2394        }
2395
2396        let (response_tx, response_rx) = oneshot::channel();
2397        self.stream
2398            .0
2399            .unbounded_send(Ok(ThreadEvent::ToolCallAuthorization(
2400                ToolCallAuthorization {
2401                    tool_call: acp::ToolCallUpdate {
2402                        id: acp::ToolCallId(self.tool_use_id.to_string().into()),
2403                        fields: acp::ToolCallUpdateFields {
2404                            title: Some(title.into()),
2405                            ..Default::default()
2406                        },
2407                    },
2408                    options: vec![
2409                        acp::PermissionOption {
2410                            id: acp::PermissionOptionId("always_allow".into()),
2411                            name: "Always Allow".into(),
2412                            kind: acp::PermissionOptionKind::AllowAlways,
2413                        },
2414                        acp::PermissionOption {
2415                            id: acp::PermissionOptionId("allow".into()),
2416                            name: "Allow".into(),
2417                            kind: acp::PermissionOptionKind::AllowOnce,
2418                        },
2419                        acp::PermissionOption {
2420                            id: acp::PermissionOptionId("deny".into()),
2421                            name: "Deny".into(),
2422                            kind: acp::PermissionOptionKind::RejectOnce,
2423                        },
2424                    ],
2425                    response: response_tx,
2426                },
2427            )))
2428            .ok();
2429        let fs = self.fs.clone();
2430        cx.spawn(async move |cx| match response_rx.await?.0.as_ref() {
2431            "always_allow" => {
2432                if let Some(fs) = fs.clone() {
2433                    cx.update(|cx| {
2434                        update_settings_file::<AgentSettings>(fs, cx, |settings, _| {
2435                            settings.set_always_allow_tool_actions(true);
2436                        });
2437                    })?;
2438                }
2439
2440                Ok(())
2441            }
2442            "allow" => Ok(()),
2443            _ => Err(anyhow!("Permission to run tool denied by user")),
2444        })
2445    }
2446}
2447
2448#[cfg(test)]
2449pub struct ToolCallEventStreamReceiver(mpsc::UnboundedReceiver<Result<ThreadEvent>>);
2450
2451#[cfg(test)]
2452impl ToolCallEventStreamReceiver {
2453    pub async fn expect_authorization(&mut self) -> ToolCallAuthorization {
2454        let event = self.0.next().await;
2455        if let Some(Ok(ThreadEvent::ToolCallAuthorization(auth))) = event {
2456            auth
2457        } else {
2458            panic!("Expected ToolCallAuthorization but got: {:?}", event);
2459        }
2460    }
2461
2462    pub async fn expect_terminal(&mut self) -> Entity<acp_thread::Terminal> {
2463        let event = self.0.next().await;
2464        if let Some(Ok(ThreadEvent::ToolCallUpdate(acp_thread::ToolCallUpdate::UpdateTerminal(
2465            update,
2466        )))) = event
2467        {
2468            update.terminal
2469        } else {
2470            panic!("Expected terminal but got: {:?}", event);
2471        }
2472    }
2473}
2474
2475#[cfg(test)]
2476impl std::ops::Deref for ToolCallEventStreamReceiver {
2477    type Target = mpsc::UnboundedReceiver<Result<ThreadEvent>>;
2478
2479    fn deref(&self) -> &Self::Target {
2480        &self.0
2481    }
2482}
2483
2484#[cfg(test)]
2485impl std::ops::DerefMut for ToolCallEventStreamReceiver {
2486    fn deref_mut(&mut self) -> &mut Self::Target {
2487        &mut self.0
2488    }
2489}
2490
2491impl From<&str> for UserMessageContent {
2492    fn from(text: &str) -> Self {
2493        Self::Text(text.into())
2494    }
2495}
2496
2497impl From<acp::ContentBlock> for UserMessageContent {
2498    fn from(value: acp::ContentBlock) -> Self {
2499        match value {
2500            acp::ContentBlock::Text(text_content) => Self::Text(text_content.text),
2501            acp::ContentBlock::Image(image_content) => Self::Image(convert_image(image_content)),
2502            acp::ContentBlock::Audio(_) => {
2503                // TODO
2504                Self::Text("[audio]".to_string())
2505            }
2506            acp::ContentBlock::ResourceLink(resource_link) => {
2507                match MentionUri::parse(&resource_link.uri) {
2508                    Ok(uri) => Self::Mention {
2509                        uri,
2510                        content: String::new(),
2511                    },
2512                    Err(err) => {
2513                        log::error!("Failed to parse mention link: {}", err);
2514                        Self::Text(format!("[{}]({})", resource_link.name, resource_link.uri))
2515                    }
2516                }
2517            }
2518            acp::ContentBlock::Resource(resource) => match resource.resource {
2519                acp::EmbeddedResourceResource::TextResourceContents(resource) => {
2520                    match MentionUri::parse(&resource.uri) {
2521                        Ok(uri) => Self::Mention {
2522                            uri,
2523                            content: resource.text,
2524                        },
2525                        Err(err) => {
2526                            log::error!("Failed to parse mention link: {}", err);
2527                            Self::Text(
2528                                MarkdownCodeBlock {
2529                                    tag: &resource.uri,
2530                                    text: &resource.text,
2531                                }
2532                                .to_string(),
2533                            )
2534                        }
2535                    }
2536                }
2537                acp::EmbeddedResourceResource::BlobResourceContents(_) => {
2538                    // TODO
2539                    Self::Text("[blob]".to_string())
2540                }
2541            },
2542        }
2543    }
2544}
2545
2546impl From<UserMessageContent> for acp::ContentBlock {
2547    fn from(content: UserMessageContent) -> Self {
2548        match content {
2549            UserMessageContent::Text(text) => acp::ContentBlock::Text(acp::TextContent {
2550                text,
2551                annotations: None,
2552            }),
2553            UserMessageContent::Image(image) => acp::ContentBlock::Image(acp::ImageContent {
2554                data: image.source.to_string(),
2555                mime_type: "image/png".to_string(),
2556                annotations: None,
2557                uri: None,
2558            }),
2559            UserMessageContent::Mention { uri, content } => {
2560                acp::ContentBlock::Resource(acp::EmbeddedResource {
2561                    resource: acp::EmbeddedResourceResource::TextResourceContents(
2562                        acp::TextResourceContents {
2563                            mime_type: None,
2564                            text: content,
2565                            uri: uri.to_uri().to_string(),
2566                        },
2567                    ),
2568                    annotations: None,
2569                })
2570            }
2571        }
2572    }
2573}
2574
2575fn convert_image(image_content: acp::ImageContent) -> LanguageModelImage {
2576    LanguageModelImage {
2577        source: image_content.data.into(),
2578        // TODO: make this optional?
2579        size: gpui::Size::new(0.into(), 0.into()),
2580    }
2581}