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