agent.rs

   1use crate::{
   2    ContextServerRegistry, Thread, ThreadEvent, ThreadsDatabase, ToolCallAuthorization,
   3    UserMessageContent, templates::Templates,
   4};
   5use crate::{HistoryStore, TokenUsageUpdated};
   6use acp_thread::{AcpThread, AgentModelSelector};
   7use action_log::ActionLog;
   8use agent_client_protocol as acp;
   9use agent_settings::AgentSettings;
  10use anyhow::{Context as _, Result, anyhow};
  11use collections::{HashSet, IndexMap};
  12use fs::Fs;
  13use futures::channel::mpsc;
  14use futures::{StreamExt, future};
  15use gpui::{
  16    App, AppContext, AsyncApp, Context, Entity, SharedString, Subscription, Task, WeakEntity,
  17};
  18use language_model::{LanguageModel, LanguageModelProvider, LanguageModelRegistry};
  19use project::{Project, ProjectItem, ProjectPath, Worktree};
  20use prompt_store::{
  21    ProjectContext, PromptId, PromptStore, RulesFileContext, UserRulesContext, WorktreeContext,
  22};
  23use settings::update_settings_file;
  24use std::any::Any;
  25use std::collections::HashMap;
  26use std::path::Path;
  27use std::rc::Rc;
  28use std::sync::Arc;
  29use util::ResultExt;
  30
  31const RULES_FILE_NAMES: [&str; 9] = [
  32    ".rules",
  33    ".cursorrules",
  34    ".windsurfrules",
  35    ".clinerules",
  36    ".github/copilot-instructions.md",
  37    "CLAUDE.md",
  38    "AGENT.md",
  39    "AGENTS.md",
  40    "GEMINI.md",
  41];
  42
  43pub struct RulesLoadingError {
  44    pub message: SharedString,
  45}
  46
  47/// Holds both the internal Thread and the AcpThread for a session
  48struct Session {
  49    /// The internal thread that processes messages
  50    thread: Entity<Thread>,
  51    /// The ACP thread that handles protocol communication
  52    acp_thread: WeakEntity<acp_thread::AcpThread>,
  53    pending_save: Task<()>,
  54    _subscriptions: Vec<Subscription>,
  55}
  56
  57pub struct LanguageModels {
  58    /// Access language model by ID
  59    models: HashMap<acp_thread::AgentModelId, Arc<dyn LanguageModel>>,
  60    /// Cached list for returning language model information
  61    model_list: acp_thread::AgentModelList,
  62    refresh_models_rx: watch::Receiver<()>,
  63    refresh_models_tx: watch::Sender<()>,
  64}
  65
  66impl LanguageModels {
  67    fn new(cx: &App) -> Self {
  68        let (refresh_models_tx, refresh_models_rx) = watch::channel(());
  69        let mut this = Self {
  70            models: HashMap::default(),
  71            model_list: acp_thread::AgentModelList::Grouped(IndexMap::default()),
  72            refresh_models_rx,
  73            refresh_models_tx,
  74        };
  75        this.refresh_list(cx);
  76        this
  77    }
  78
  79    fn refresh_list(&mut self, cx: &App) {
  80        let providers = LanguageModelRegistry::global(cx)
  81            .read(cx)
  82            .providers()
  83            .into_iter()
  84            .filter(|provider| provider.is_authenticated(cx))
  85            .collect::<Vec<_>>();
  86
  87        let mut language_model_list = IndexMap::default();
  88        let mut recommended_models = HashSet::default();
  89
  90        let mut recommended = Vec::new();
  91        for provider in &providers {
  92            for model in provider.recommended_models(cx) {
  93                recommended_models.insert(model.id());
  94                recommended.push(Self::map_language_model_to_info(&model, provider));
  95            }
  96        }
  97        if !recommended.is_empty() {
  98            language_model_list.insert(
  99                acp_thread::AgentModelGroupName("Recommended".into()),
 100                recommended,
 101            );
 102        }
 103
 104        let mut models = HashMap::default();
 105        for provider in providers {
 106            let mut provider_models = Vec::new();
 107            for model in provider.provided_models(cx) {
 108                let model_info = Self::map_language_model_to_info(&model, &provider);
 109                let model_id = model_info.id.clone();
 110                if !recommended_models.contains(&model.id()) {
 111                    provider_models.push(model_info);
 112                }
 113                models.insert(model_id, model);
 114            }
 115            if !provider_models.is_empty() {
 116                language_model_list.insert(
 117                    acp_thread::AgentModelGroupName(provider.name().0.clone()),
 118                    provider_models,
 119                );
 120            }
 121        }
 122
 123        self.models = models;
 124        self.model_list = acp_thread::AgentModelList::Grouped(language_model_list);
 125        self.refresh_models_tx.send(()).ok();
 126    }
 127
 128    fn watch(&self) -> watch::Receiver<()> {
 129        self.refresh_models_rx.clone()
 130    }
 131
 132    pub fn model_from_id(
 133        &self,
 134        model_id: &acp_thread::AgentModelId,
 135    ) -> Option<Arc<dyn LanguageModel>> {
 136        self.models.get(model_id).cloned()
 137    }
 138
 139    fn map_language_model_to_info(
 140        model: &Arc<dyn LanguageModel>,
 141        provider: &Arc<dyn LanguageModelProvider>,
 142    ) -> acp_thread::AgentModelInfo {
 143        acp_thread::AgentModelInfo {
 144            id: Self::model_id(model),
 145            name: model.name().0,
 146            icon: Some(provider.icon()),
 147        }
 148    }
 149
 150    fn model_id(model: &Arc<dyn LanguageModel>) -> acp_thread::AgentModelId {
 151        acp_thread::AgentModelId(format!("{}/{}", model.provider_id().0, model.id().0).into())
 152    }
 153}
 154
 155pub struct NativeAgent {
 156    /// Session ID -> Session mapping
 157    sessions: HashMap<acp::SessionId, Session>,
 158    history: Entity<HistoryStore>,
 159    /// Shared project context for all threads
 160    project_context: Entity<ProjectContext>,
 161    project_context_needs_refresh: watch::Sender<()>,
 162    _maintain_project_context: Task<Result<()>>,
 163    context_server_registry: Entity<ContextServerRegistry>,
 164    /// Shared templates for all threads
 165    templates: Arc<Templates>,
 166    /// Cached model information
 167    models: LanguageModels,
 168    project: Entity<Project>,
 169    prompt_store: Option<Entity<PromptStore>>,
 170    fs: Arc<dyn Fs>,
 171    _subscriptions: Vec<Subscription>,
 172}
 173
 174impl NativeAgent {
 175    pub async fn new(
 176        project: Entity<Project>,
 177        history: Entity<HistoryStore>,
 178        templates: Arc<Templates>,
 179        prompt_store: Option<Entity<PromptStore>>,
 180        fs: Arc<dyn Fs>,
 181        cx: &mut AsyncApp,
 182    ) -> Result<Entity<NativeAgent>> {
 183        log::info!("Creating new NativeAgent");
 184
 185        let project_context = cx
 186            .update(|cx| Self::build_project_context(&project, prompt_store.as_ref(), cx))?
 187            .await;
 188
 189        cx.new(|cx| {
 190            let mut subscriptions = vec![
 191                cx.subscribe(&project, Self::handle_project_event),
 192                cx.subscribe(
 193                    &LanguageModelRegistry::global(cx),
 194                    Self::handle_models_updated_event,
 195                ),
 196            ];
 197            if let Some(prompt_store) = prompt_store.as_ref() {
 198                subscriptions.push(cx.subscribe(prompt_store, Self::handle_prompts_updated_event))
 199            }
 200
 201            let (project_context_needs_refresh_tx, project_context_needs_refresh_rx) =
 202                watch::channel(());
 203            Self {
 204                sessions: HashMap::new(),
 205                history,
 206                project_context: cx.new(|_| project_context),
 207                project_context_needs_refresh: project_context_needs_refresh_tx,
 208                _maintain_project_context: cx.spawn(async move |this, cx| {
 209                    Self::maintain_project_context(this, project_context_needs_refresh_rx, cx).await
 210                }),
 211                context_server_registry: cx.new(|cx| {
 212                    ContextServerRegistry::new(project.read(cx).context_server_store(), cx)
 213                }),
 214                templates,
 215                models: LanguageModels::new(cx),
 216                project,
 217                prompt_store,
 218                fs,
 219                _subscriptions: subscriptions,
 220            }
 221        })
 222    }
 223
 224    fn register_session(
 225        &mut self,
 226        thread_handle: Entity<Thread>,
 227        cx: &mut Context<Self>,
 228    ) -> Entity<AcpThread> {
 229        let connection = Rc::new(NativeAgentConnection(cx.entity()));
 230        let registry = LanguageModelRegistry::read_global(cx);
 231        let summarization_model = registry.thread_summary_model().map(|c| c.model);
 232
 233        thread_handle.update(cx, |thread, cx| {
 234            thread.set_summarization_model(summarization_model, cx);
 235            thread.add_default_tools(cx)
 236        });
 237
 238        let thread = thread_handle.read(cx);
 239        let session_id = thread.id().clone();
 240        let title = thread.title();
 241        let project = thread.project.clone();
 242        let action_log = thread.action_log.clone();
 243        let acp_thread = cx.new(|_cx| {
 244            acp_thread::AcpThread::new(
 245                title,
 246                connection,
 247                project.clone(),
 248                action_log.clone(),
 249                session_id.clone(),
 250            )
 251        });
 252        let subscriptions = vec![
 253            cx.observe_release(&acp_thread, |this, acp_thread, _cx| {
 254                this.sessions.remove(acp_thread.session_id());
 255            }),
 256            cx.subscribe(&thread_handle, Self::handle_thread_token_usage_updated),
 257            cx.observe(&thread_handle, move |this, thread, cx| {
 258                this.save_thread(thread, cx)
 259            }),
 260        ];
 261
 262        self.sessions.insert(
 263            session_id,
 264            Session {
 265                thread: thread_handle,
 266                acp_thread: acp_thread.downgrade(),
 267                _subscriptions: subscriptions,
 268                pending_save: Task::ready(()),
 269            },
 270        );
 271        acp_thread
 272    }
 273
 274    pub fn models(&self) -> &LanguageModels {
 275        &self.models
 276    }
 277
 278    async fn maintain_project_context(
 279        this: WeakEntity<Self>,
 280        mut needs_refresh: watch::Receiver<()>,
 281        cx: &mut AsyncApp,
 282    ) -> Result<()> {
 283        while needs_refresh.changed().await.is_ok() {
 284            let project_context = this
 285                .update(cx, |this, cx| {
 286                    Self::build_project_context(&this.project, this.prompt_store.as_ref(), cx)
 287                })?
 288                .await;
 289            this.update(cx, |this, cx| {
 290                this.project_context = cx.new(|_| project_context);
 291            })?;
 292        }
 293
 294        Ok(())
 295    }
 296
 297    fn build_project_context(
 298        project: &Entity<Project>,
 299        prompt_store: Option<&Entity<PromptStore>>,
 300        cx: &mut App,
 301    ) -> Task<ProjectContext> {
 302        let worktrees = project.read(cx).visible_worktrees(cx).collect::<Vec<_>>();
 303        let worktree_tasks = worktrees
 304            .into_iter()
 305            .map(|worktree| {
 306                Self::load_worktree_info_for_system_prompt(worktree, project.clone(), cx)
 307            })
 308            .collect::<Vec<_>>();
 309        let default_user_rules_task = if let Some(prompt_store) = prompt_store.as_ref() {
 310            prompt_store.read_with(cx, |prompt_store, cx| {
 311                let prompts = prompt_store.default_prompt_metadata();
 312                let load_tasks = prompts.into_iter().map(|prompt_metadata| {
 313                    let contents = prompt_store.load(prompt_metadata.id, cx);
 314                    async move { (contents.await, prompt_metadata) }
 315                });
 316                cx.background_spawn(future::join_all(load_tasks))
 317            })
 318        } else {
 319            Task::ready(vec![])
 320        };
 321
 322        cx.spawn(async move |_cx| {
 323            let (worktrees, default_user_rules) =
 324                future::join(future::join_all(worktree_tasks), default_user_rules_task).await;
 325
 326            let worktrees = worktrees
 327                .into_iter()
 328                .map(|(worktree, _rules_error)| {
 329                    // TODO: show error message
 330                    // if let Some(rules_error) = rules_error {
 331                    //     this.update(cx, |_, cx| cx.emit(rules_error)).ok();
 332                    // }
 333                    worktree
 334                })
 335                .collect::<Vec<_>>();
 336
 337            let default_user_rules = default_user_rules
 338                .into_iter()
 339                .flat_map(|(contents, prompt_metadata)| match contents {
 340                    Ok(contents) => Some(UserRulesContext {
 341                        uuid: match prompt_metadata.id {
 342                            PromptId::User { uuid } => uuid,
 343                            PromptId::EditWorkflow => return None,
 344                        },
 345                        title: prompt_metadata.title.map(|title| title.to_string()),
 346                        contents,
 347                    }),
 348                    Err(_err) => {
 349                        // TODO: show error message
 350                        // this.update(cx, |_, cx| {
 351                        //     cx.emit(RulesLoadingError {
 352                        //         message: format!("{err:?}").into(),
 353                        //     });
 354                        // })
 355                        // .ok();
 356                        None
 357                    }
 358                })
 359                .collect::<Vec<_>>();
 360
 361            ProjectContext::new(worktrees, default_user_rules)
 362        })
 363    }
 364
 365    fn load_worktree_info_for_system_prompt(
 366        worktree: Entity<Worktree>,
 367        project: Entity<Project>,
 368        cx: &mut App,
 369    ) -> Task<(WorktreeContext, Option<RulesLoadingError>)> {
 370        let tree = worktree.read(cx);
 371        let root_name = tree.root_name().into();
 372        let abs_path = tree.abs_path();
 373
 374        let mut context = WorktreeContext {
 375            root_name,
 376            abs_path,
 377            rules_file: None,
 378        };
 379
 380        let rules_task = Self::load_worktree_rules_file(worktree, project, cx);
 381        let Some(rules_task) = rules_task else {
 382            return Task::ready((context, None));
 383        };
 384
 385        cx.spawn(async move |_| {
 386            let (rules_file, rules_file_error) = match rules_task.await {
 387                Ok(rules_file) => (Some(rules_file), None),
 388                Err(err) => (
 389                    None,
 390                    Some(RulesLoadingError {
 391                        message: format!("{err}").into(),
 392                    }),
 393                ),
 394            };
 395            context.rules_file = rules_file;
 396            (context, rules_file_error)
 397        })
 398    }
 399
 400    fn load_worktree_rules_file(
 401        worktree: Entity<Worktree>,
 402        project: Entity<Project>,
 403        cx: &mut App,
 404    ) -> Option<Task<Result<RulesFileContext>>> {
 405        let worktree = worktree.read(cx);
 406        let worktree_id = worktree.id();
 407        let selected_rules_file = RULES_FILE_NAMES
 408            .into_iter()
 409            .filter_map(|name| {
 410                worktree
 411                    .entry_for_path(name)
 412                    .filter(|entry| entry.is_file())
 413                    .map(|entry| entry.path.clone())
 414            })
 415            .next();
 416
 417        // Note that Cline supports `.clinerules` being a directory, but that is not currently
 418        // supported. This doesn't seem to occur often in GitHub repositories.
 419        selected_rules_file.map(|path_in_worktree| {
 420            let project_path = ProjectPath {
 421                worktree_id,
 422                path: path_in_worktree.clone(),
 423            };
 424            let buffer_task =
 425                project.update(cx, |project, cx| project.open_buffer(project_path, cx));
 426            let rope_task = cx.spawn(async move |cx| {
 427                buffer_task.await?.read_with(cx, |buffer, cx| {
 428                    let project_entry_id = buffer.entry_id(cx).context("buffer has no file")?;
 429                    anyhow::Ok((project_entry_id, buffer.as_rope().clone()))
 430                })?
 431            });
 432            // Build a string from the rope on a background thread.
 433            cx.background_spawn(async move {
 434                let (project_entry_id, rope) = rope_task.await?;
 435                anyhow::Ok(RulesFileContext {
 436                    path_in_worktree,
 437                    text: rope.to_string().trim().to_string(),
 438                    project_entry_id: project_entry_id.to_usize(),
 439                })
 440            })
 441        })
 442    }
 443
 444    fn handle_thread_token_usage_updated(
 445        &mut self,
 446        thread: Entity<Thread>,
 447        usage: &TokenUsageUpdated,
 448        cx: &mut Context<Self>,
 449    ) {
 450        let Some(session) = self.sessions.get(thread.read(cx).id()) else {
 451            return;
 452        };
 453        session
 454            .acp_thread
 455            .update(cx, |acp_thread, cx| {
 456                acp_thread.update_token_usage(usage.0.clone(), cx);
 457            })
 458            .ok();
 459    }
 460
 461    fn handle_project_event(
 462        &mut self,
 463        _project: Entity<Project>,
 464        event: &project::Event,
 465        _cx: &mut Context<Self>,
 466    ) {
 467        match event {
 468            project::Event::WorktreeAdded(_) | project::Event::WorktreeRemoved(_) => {
 469                self.project_context_needs_refresh.send(()).ok();
 470            }
 471            project::Event::WorktreeUpdatedEntries(_, items) => {
 472                if items.iter().any(|(path, _, _)| {
 473                    RULES_FILE_NAMES
 474                        .iter()
 475                        .any(|name| path.as_ref() == Path::new(name))
 476                }) {
 477                    self.project_context_needs_refresh.send(()).ok();
 478                }
 479            }
 480            _ => {}
 481        }
 482    }
 483
 484    fn handle_prompts_updated_event(
 485        &mut self,
 486        _prompt_store: Entity<PromptStore>,
 487        _event: &prompt_store::PromptsUpdatedEvent,
 488        _cx: &mut Context<Self>,
 489    ) {
 490        self.project_context_needs_refresh.send(()).ok();
 491    }
 492
 493    fn handle_models_updated_event(
 494        &mut self,
 495        _registry: Entity<LanguageModelRegistry>,
 496        _event: &language_model::Event,
 497        cx: &mut Context<Self>,
 498    ) {
 499        self.models.refresh_list(cx);
 500
 501        let registry = LanguageModelRegistry::read_global(cx);
 502        let default_model = registry.default_model().map(|m| m.model);
 503        let summarization_model = registry.thread_summary_model().map(|m| m.model);
 504
 505        for session in self.sessions.values_mut() {
 506            session.thread.update(cx, |thread, cx| {
 507                if thread.model().is_none()
 508                    && let Some(model) = default_model.clone()
 509                {
 510                    thread.set_model(model, cx);
 511                    cx.notify();
 512                }
 513                thread.set_summarization_model(summarization_model.clone(), cx);
 514            });
 515        }
 516    }
 517
 518    pub fn open_thread(
 519        &mut self,
 520        id: acp::SessionId,
 521        cx: &mut Context<Self>,
 522    ) -> Task<Result<Entity<AcpThread>>> {
 523        let database_future = ThreadsDatabase::connect(cx);
 524        cx.spawn(async move |this, cx| {
 525            let database = database_future.await.map_err(|err| anyhow!(err))?;
 526            let db_thread = database
 527                .load_thread(id.clone())
 528                .await?
 529                .with_context(|| format!("no thread found with ID: {id:?}"))?;
 530
 531            let thread = this.update(cx, |this, cx| {
 532                let action_log = cx.new(|_cx| ActionLog::new(this.project.clone()));
 533                cx.new(|cx| {
 534                    Thread::from_db(
 535                        id.clone(),
 536                        db_thread,
 537                        this.project.clone(),
 538                        this.project_context.clone(),
 539                        this.context_server_registry.clone(),
 540                        action_log.clone(),
 541                        this.templates.clone(),
 542                        cx,
 543                    )
 544                })
 545            })?;
 546            let acp_thread =
 547                this.update(cx, |this, cx| this.register_session(thread.clone(), cx))?;
 548            let events = thread.update(cx, |thread, cx| thread.replay(cx))?;
 549            cx.update(|cx| {
 550                NativeAgentConnection::handle_thread_events(events, acp_thread.downgrade(), cx)
 551            })?
 552            .await?;
 553            Ok(acp_thread)
 554        })
 555    }
 556
 557    pub fn thread_summary(
 558        &mut self,
 559        id: acp::SessionId,
 560        cx: &mut Context<Self>,
 561    ) -> Task<Result<SharedString>> {
 562        let thread = self.open_thread(id.clone(), cx);
 563        cx.spawn(async move |this, cx| {
 564            let acp_thread = thread.await?;
 565            let result = this
 566                .update(cx, |this, cx| {
 567                    this.sessions
 568                        .get(&id)
 569                        .unwrap()
 570                        .thread
 571                        .update(cx, |thread, cx| thread.summary(cx))
 572                })?
 573                .await?;
 574            drop(acp_thread);
 575            Ok(result)
 576        })
 577    }
 578
 579    fn save_thread(&mut self, thread: Entity<Thread>, cx: &mut Context<Self>) {
 580        if thread.read(cx).is_empty() {
 581            return;
 582        }
 583
 584        let database_future = ThreadsDatabase::connect(cx);
 585        let (id, db_thread) =
 586            thread.update(cx, |thread, cx| (thread.id().clone(), thread.to_db(cx)));
 587        let Some(session) = self.sessions.get_mut(&id) else {
 588            return;
 589        };
 590        let history = self.history.clone();
 591        session.pending_save = cx.spawn(async move |_, cx| {
 592            let Some(database) = database_future.await.map_err(|err| anyhow!(err)).log_err() else {
 593                return;
 594            };
 595            let db_thread = db_thread.await;
 596            database.save_thread(id, db_thread).await.log_err();
 597            history.update(cx, |history, cx| history.reload(cx)).ok();
 598        });
 599    }
 600}
 601
 602/// Wrapper struct that implements the AgentConnection trait
 603#[derive(Clone)]
 604pub struct NativeAgentConnection(pub Entity<NativeAgent>);
 605
 606impl NativeAgentConnection {
 607    pub fn thread(&self, session_id: &acp::SessionId, cx: &App) -> Option<Entity<Thread>> {
 608        self.0
 609            .read(cx)
 610            .sessions
 611            .get(session_id)
 612            .map(|session| session.thread.clone())
 613    }
 614
 615    fn run_turn(
 616        &self,
 617        session_id: acp::SessionId,
 618        cx: &mut App,
 619        f: impl 'static
 620        + FnOnce(Entity<Thread>, &mut App) -> Result<mpsc::UnboundedReceiver<Result<ThreadEvent>>>,
 621    ) -> Task<Result<acp::PromptResponse>> {
 622        let Some((thread, acp_thread)) = self.0.update(cx, |agent, _cx| {
 623            agent
 624                .sessions
 625                .get_mut(&session_id)
 626                .map(|s| (s.thread.clone(), s.acp_thread.clone()))
 627        }) else {
 628            return Task::ready(Err(anyhow!("Session not found")));
 629        };
 630        log::debug!("Found session for: {}", session_id);
 631
 632        let response_stream = match f(thread, cx) {
 633            Ok(stream) => stream,
 634            Err(err) => return Task::ready(Err(err)),
 635        };
 636        Self::handle_thread_events(response_stream, acp_thread, cx)
 637    }
 638
 639    fn handle_thread_events(
 640        mut events: mpsc::UnboundedReceiver<Result<ThreadEvent>>,
 641        acp_thread: WeakEntity<AcpThread>,
 642        cx: &App,
 643    ) -> Task<Result<acp::PromptResponse>> {
 644        cx.spawn(async move |cx| {
 645            // Handle response stream and forward to session.acp_thread
 646            while let Some(result) = events.next().await {
 647                match result {
 648                    Ok(event) => {
 649                        log::trace!("Received completion event: {:?}", event);
 650
 651                        match event {
 652                            ThreadEvent::UserMessage(message) => {
 653                                acp_thread.update(cx, |thread, cx| {
 654                                    for content in message.content {
 655                                        thread.push_user_content_block(
 656                                            Some(message.id.clone()),
 657                                            content.into(),
 658                                            cx,
 659                                        );
 660                                    }
 661                                })?;
 662                            }
 663                            ThreadEvent::AgentText(text) => {
 664                                acp_thread.update(cx, |thread, cx| {
 665                                    thread.push_assistant_content_block(
 666                                        acp::ContentBlock::Text(acp::TextContent {
 667                                            text,
 668                                            annotations: None,
 669                                        }),
 670                                        false,
 671                                        cx,
 672                                    )
 673                                })?;
 674                            }
 675                            ThreadEvent::AgentThinking(text) => {
 676                                acp_thread.update(cx, |thread, cx| {
 677                                    thread.push_assistant_content_block(
 678                                        acp::ContentBlock::Text(acp::TextContent {
 679                                            text,
 680                                            annotations: None,
 681                                        }),
 682                                        true,
 683                                        cx,
 684                                    )
 685                                })?;
 686                            }
 687                            ThreadEvent::ToolCallAuthorization(ToolCallAuthorization {
 688                                tool_call,
 689                                options,
 690                                response,
 691                            }) => {
 692                                let recv = acp_thread.update(cx, |thread, cx| {
 693                                    thread.request_tool_call_authorization(tool_call, options, cx)
 694                                })?;
 695                                cx.background_spawn(async move {
 696                                    if let Some(recv) = recv.log_err()
 697                                        && let Some(option) = recv
 698                                            .await
 699                                            .context("authorization sender was dropped")
 700                                            .log_err()
 701                                    {
 702                                        response
 703                                            .send(option)
 704                                            .map(|_| anyhow!("authorization receiver was dropped"))
 705                                            .log_err();
 706                                    }
 707                                })
 708                                .detach();
 709                            }
 710                            ThreadEvent::ToolCall(tool_call) => {
 711                                acp_thread.update(cx, |thread, cx| {
 712                                    thread.upsert_tool_call(tool_call, cx)
 713                                })??;
 714                            }
 715                            ThreadEvent::ToolCallUpdate(update) => {
 716                                acp_thread.update(cx, |thread, cx| {
 717                                    thread.update_tool_call(update, cx)
 718                                })??;
 719                            }
 720                            ThreadEvent::TitleUpdate(title) => {
 721                                acp_thread
 722                                    .update(cx, |thread, cx| thread.update_title(title, cx))??;
 723                            }
 724                            ThreadEvent::Retry(status) => {
 725                                acp_thread.update(cx, |thread, cx| {
 726                                    thread.update_retry_status(status, cx)
 727                                })?;
 728                            }
 729                            ThreadEvent::Stop(stop_reason) => {
 730                                log::debug!("Assistant message complete: {:?}", stop_reason);
 731                                return Ok(acp::PromptResponse { stop_reason });
 732                            }
 733                        }
 734                    }
 735                    Err(e) => {
 736                        log::error!("Error in model response stream: {:?}", e);
 737                        return Err(e);
 738                    }
 739                }
 740            }
 741
 742            log::info!("Response stream completed");
 743            anyhow::Ok(acp::PromptResponse {
 744                stop_reason: acp::StopReason::EndTurn,
 745            })
 746        })
 747    }
 748}
 749
 750impl AgentModelSelector for NativeAgentConnection {
 751    fn list_models(&self, cx: &mut App) -> Task<Result<acp_thread::AgentModelList>> {
 752        log::debug!("NativeAgentConnection::list_models called");
 753        let list = self.0.read(cx).models.model_list.clone();
 754        Task::ready(if list.is_empty() {
 755            Err(anyhow::anyhow!("No models available"))
 756        } else {
 757            Ok(list)
 758        })
 759    }
 760
 761    fn select_model(
 762        &self,
 763        session_id: acp::SessionId,
 764        model_id: acp_thread::AgentModelId,
 765        cx: &mut App,
 766    ) -> Task<Result<()>> {
 767        log::info!("Setting model for session {}: {}", session_id, model_id);
 768        let Some(thread) = self
 769            .0
 770            .read(cx)
 771            .sessions
 772            .get(&session_id)
 773            .map(|session| session.thread.clone())
 774        else {
 775            return Task::ready(Err(anyhow!("Session not found")));
 776        };
 777
 778        let Some(model) = self.0.read(cx).models.model_from_id(&model_id) else {
 779            return Task::ready(Err(anyhow!("Invalid model ID {}", model_id)));
 780        };
 781
 782        thread.update(cx, |thread, cx| {
 783            thread.set_model(model.clone(), cx);
 784        });
 785
 786        update_settings_file::<AgentSettings>(
 787            self.0.read(cx).fs.clone(),
 788            cx,
 789            move |settings, _cx| {
 790                settings.set_model(model);
 791            },
 792        );
 793
 794        Task::ready(Ok(()))
 795    }
 796
 797    fn selected_model(
 798        &self,
 799        session_id: &acp::SessionId,
 800        cx: &mut App,
 801    ) -> Task<Result<acp_thread::AgentModelInfo>> {
 802        let session_id = session_id.clone();
 803
 804        let Some(thread) = self
 805            .0
 806            .read(cx)
 807            .sessions
 808            .get(&session_id)
 809            .map(|session| session.thread.clone())
 810        else {
 811            return Task::ready(Err(anyhow!("Session not found")));
 812        };
 813        let Some(model) = thread.read(cx).model() else {
 814            return Task::ready(Err(anyhow!("Model not found")));
 815        };
 816        let Some(provider) = LanguageModelRegistry::read_global(cx).provider(&model.provider_id())
 817        else {
 818            return Task::ready(Err(anyhow!("Provider not found")));
 819        };
 820        Task::ready(Ok(LanguageModels::map_language_model_to_info(
 821            model, &provider,
 822        )))
 823    }
 824
 825    fn watch(&self, cx: &mut App) -> watch::Receiver<()> {
 826        self.0.read(cx).models.watch()
 827    }
 828}
 829
 830impl acp_thread::AgentConnection for NativeAgentConnection {
 831    fn new_thread(
 832        self: Rc<Self>,
 833        project: Entity<Project>,
 834        cwd: &Path,
 835        cx: &mut App,
 836    ) -> Task<Result<Entity<acp_thread::AcpThread>>> {
 837        let agent = self.0.clone();
 838        log::info!("Creating new thread for project at: {:?}", cwd);
 839
 840        cx.spawn(async move |cx| {
 841            log::debug!("Starting thread creation in async context");
 842
 843            let action_log = cx.new(|_cx| ActionLog::new(project.clone()))?;
 844            // Create Thread
 845            let thread = agent.update(
 846                cx,
 847                |agent, cx: &mut gpui::Context<NativeAgent>| -> Result<_> {
 848                    // Fetch default model from registry settings
 849                    let registry = LanguageModelRegistry::read_global(cx);
 850                    // Log available models for debugging
 851                    let available_count = registry.available_models(cx).count();
 852                    log::debug!("Total available models: {}", available_count);
 853
 854                    let default_model = registry.default_model().and_then(|default_model| {
 855                        agent
 856                            .models
 857                            .model_from_id(&LanguageModels::model_id(&default_model.model))
 858                    });
 859
 860                    let thread = cx.new(|cx| {
 861                        Thread::new(
 862                            project.clone(),
 863                            agent.project_context.clone(),
 864                            agent.context_server_registry.clone(),
 865                            action_log.clone(),
 866                            agent.templates.clone(),
 867                            default_model,
 868                            cx,
 869                        )
 870                    });
 871
 872                    Ok(thread)
 873                },
 874            )??;
 875            agent.update(cx, |agent, cx| agent.register_session(thread, cx))
 876        })
 877    }
 878
 879    fn auth_methods(&self) -> &[acp::AuthMethod] {
 880        &[] // No auth for in-process
 881    }
 882
 883    fn authenticate(&self, _method: acp::AuthMethodId, _cx: &mut App) -> Task<Result<()>> {
 884        Task::ready(Ok(()))
 885    }
 886
 887    fn model_selector(&self) -> Option<Rc<dyn AgentModelSelector>> {
 888        Some(Rc::new(self.clone()) as Rc<dyn AgentModelSelector>)
 889    }
 890
 891    fn prompt(
 892        &self,
 893        id: Option<acp_thread::UserMessageId>,
 894        params: acp::PromptRequest,
 895        cx: &mut App,
 896    ) -> Task<Result<acp::PromptResponse>> {
 897        let id = id.expect("UserMessageId is required");
 898        let session_id = params.session_id.clone();
 899        log::info!("Received prompt request for session: {}", session_id);
 900        log::debug!("Prompt blocks count: {}", params.prompt.len());
 901
 902        self.run_turn(session_id, cx, |thread, cx| {
 903            let content: Vec<UserMessageContent> = params
 904                .prompt
 905                .into_iter()
 906                .map(Into::into)
 907                .collect::<Vec<_>>();
 908            log::info!("Converted prompt to message: {} chars", content.len());
 909            log::debug!("Message id: {:?}", id);
 910            log::debug!("Message content: {:?}", content);
 911
 912            thread.update(cx, |thread, cx| thread.send(id, content, cx))
 913        })
 914    }
 915
 916    fn resume(
 917        &self,
 918        session_id: &acp::SessionId,
 919        _cx: &mut App,
 920    ) -> Option<Rc<dyn acp_thread::AgentSessionResume>> {
 921        Some(Rc::new(NativeAgentSessionResume {
 922            connection: self.clone(),
 923            session_id: session_id.clone(),
 924        }) as _)
 925    }
 926
 927    fn cancel(&self, session_id: &acp::SessionId, cx: &mut App) {
 928        log::info!("Cancelling on session: {}", session_id);
 929        self.0.update(cx, |agent, cx| {
 930            if let Some(agent) = agent.sessions.get(session_id) {
 931                agent.thread.update(cx, |thread, cx| thread.cancel(cx));
 932            }
 933        });
 934    }
 935
 936    fn session_editor(
 937        &self,
 938        session_id: &agent_client_protocol::SessionId,
 939        cx: &mut App,
 940    ) -> Option<Rc<dyn acp_thread::AgentSessionEditor>> {
 941        self.0.update(cx, |agent, _cx| {
 942            agent.sessions.get(session_id).map(|session| {
 943                Rc::new(NativeAgentSessionEditor {
 944                    thread: session.thread.clone(),
 945                    acp_thread: session.acp_thread.clone(),
 946                }) as _
 947            })
 948        })
 949    }
 950
 951    fn into_any(self: Rc<Self>) -> Rc<dyn Any> {
 952        self
 953    }
 954}
 955
 956struct NativeAgentSessionEditor {
 957    thread: Entity<Thread>,
 958    acp_thread: WeakEntity<AcpThread>,
 959}
 960
 961impl acp_thread::AgentSessionEditor for NativeAgentSessionEditor {
 962    fn truncate(&self, message_id: acp_thread::UserMessageId, cx: &mut App) -> Task<Result<()>> {
 963        match self.thread.update(cx, |thread, cx| {
 964            thread.truncate(message_id.clone(), cx)?;
 965            Ok(thread.latest_token_usage())
 966        }) {
 967            Ok(usage) => {
 968                self.acp_thread
 969                    .update(cx, |thread, cx| {
 970                        thread.update_token_usage(usage, cx);
 971                    })
 972                    .ok();
 973                Task::ready(Ok(()))
 974            }
 975            Err(error) => Task::ready(Err(error)),
 976        }
 977    }
 978}
 979
 980struct NativeAgentSessionResume {
 981    connection: NativeAgentConnection,
 982    session_id: acp::SessionId,
 983}
 984
 985impl acp_thread::AgentSessionResume for NativeAgentSessionResume {
 986    fn run(&self, cx: &mut App) -> Task<Result<acp::PromptResponse>> {
 987        self.connection
 988            .run_turn(self.session_id.clone(), cx, |thread, cx| {
 989                thread.update(cx, |thread, cx| thread.resume(cx))
 990            })
 991    }
 992}
 993
 994#[cfg(test)]
 995mod tests {
 996    use crate::HistoryEntryId;
 997
 998    use super::*;
 999    use acp_thread::{
1000        AgentConnection, AgentModelGroupName, AgentModelId, AgentModelInfo, MentionUri,
1001    };
1002    use fs::FakeFs;
1003    use gpui::TestAppContext;
1004    use indoc::indoc;
1005    use language_model::fake_provider::FakeLanguageModel;
1006    use serde_json::json;
1007    use settings::SettingsStore;
1008    use util::path;
1009
1010    #[gpui::test]
1011    async fn test_maintaining_project_context(cx: &mut TestAppContext) {
1012        init_test(cx);
1013        let fs = FakeFs::new(cx.executor());
1014        fs.insert_tree(
1015            "/",
1016            json!({
1017                "a": {}
1018            }),
1019        )
1020        .await;
1021        let project = Project::test(fs.clone(), [], cx).await;
1022        let context_store = cx.new(|cx| assistant_context::ContextStore::fake(project.clone(), cx));
1023        let history_store = cx.new(|cx| HistoryStore::new(context_store, cx));
1024        let agent = NativeAgent::new(
1025            project.clone(),
1026            history_store,
1027            Templates::new(),
1028            None,
1029            fs.clone(),
1030            &mut cx.to_async(),
1031        )
1032        .await
1033        .unwrap();
1034        agent.read_with(cx, |agent, cx| {
1035            assert_eq!(agent.project_context.read(cx).worktrees, vec![])
1036        });
1037
1038        let worktree = project
1039            .update(cx, |project, cx| project.create_worktree("/a", true, cx))
1040            .await
1041            .unwrap();
1042        cx.run_until_parked();
1043        agent.read_with(cx, |agent, cx| {
1044            assert_eq!(
1045                agent.project_context.read(cx).worktrees,
1046                vec![WorktreeContext {
1047                    root_name: "a".into(),
1048                    abs_path: Path::new("/a").into(),
1049                    rules_file: None
1050                }]
1051            )
1052        });
1053
1054        // Creating `/a/.rules` updates the project context.
1055        fs.insert_file("/a/.rules", Vec::new()).await;
1056        cx.run_until_parked();
1057        agent.read_with(cx, |agent, cx| {
1058            let rules_entry = worktree.read(cx).entry_for_path(".rules").unwrap();
1059            assert_eq!(
1060                agent.project_context.read(cx).worktrees,
1061                vec![WorktreeContext {
1062                    root_name: "a".into(),
1063                    abs_path: Path::new("/a").into(),
1064                    rules_file: Some(RulesFileContext {
1065                        path_in_worktree: Path::new(".rules").into(),
1066                        text: "".into(),
1067                        project_entry_id: rules_entry.id.to_usize()
1068                    })
1069                }]
1070            )
1071        });
1072    }
1073
1074    #[gpui::test]
1075    async fn test_listing_models(cx: &mut TestAppContext) {
1076        init_test(cx);
1077        let fs = FakeFs::new(cx.executor());
1078        fs.insert_tree("/", json!({ "a": {}  })).await;
1079        let project = Project::test(fs.clone(), [], cx).await;
1080        let context_store = cx.new(|cx| assistant_context::ContextStore::fake(project.clone(), cx));
1081        let history_store = cx.new(|cx| HistoryStore::new(context_store, cx));
1082        let connection = NativeAgentConnection(
1083            NativeAgent::new(
1084                project.clone(),
1085                history_store,
1086                Templates::new(),
1087                None,
1088                fs.clone(),
1089                &mut cx.to_async(),
1090            )
1091            .await
1092            .unwrap(),
1093        );
1094
1095        let models = cx.update(|cx| connection.list_models(cx)).await.unwrap();
1096
1097        let acp_thread::AgentModelList::Grouped(models) = models else {
1098            panic!("Unexpected model group");
1099        };
1100        assert_eq!(
1101            models,
1102            IndexMap::from_iter([(
1103                AgentModelGroupName("Fake".into()),
1104                vec![AgentModelInfo {
1105                    id: AgentModelId("fake/fake".into()),
1106                    name: "Fake".into(),
1107                    icon: Some(ui::IconName::ZedAssistant),
1108                }]
1109            )])
1110        );
1111    }
1112
1113    #[gpui::test]
1114    async fn test_model_selection_persists_to_settings(cx: &mut TestAppContext) {
1115        init_test(cx);
1116        let fs = FakeFs::new(cx.executor());
1117        fs.create_dir(paths::settings_file().parent().unwrap())
1118            .await
1119            .unwrap();
1120        fs.insert_file(
1121            paths::settings_file(),
1122            json!({
1123                "agent": {
1124                    "default_model": {
1125                        "provider": "foo",
1126                        "model": "bar"
1127                    }
1128                }
1129            })
1130            .to_string()
1131            .into_bytes(),
1132        )
1133        .await;
1134        let project = Project::test(fs.clone(), [], cx).await;
1135
1136        let context_store = cx.new(|cx| assistant_context::ContextStore::fake(project.clone(), cx));
1137        let history_store = cx.new(|cx| HistoryStore::new(context_store, cx));
1138
1139        // Create the agent and connection
1140        let agent = NativeAgent::new(
1141            project.clone(),
1142            history_store,
1143            Templates::new(),
1144            None,
1145            fs.clone(),
1146            &mut cx.to_async(),
1147        )
1148        .await
1149        .unwrap();
1150        let connection = NativeAgentConnection(agent.clone());
1151
1152        // Create a thread/session
1153        let acp_thread = cx
1154            .update(|cx| {
1155                Rc::new(connection.clone()).new_thread(project.clone(), Path::new("/a"), cx)
1156            })
1157            .await
1158            .unwrap();
1159
1160        let session_id = cx.update(|cx| acp_thread.read(cx).session_id().clone());
1161
1162        // Select a model
1163        let model_id = AgentModelId("fake/fake".into());
1164        cx.update(|cx| connection.select_model(session_id.clone(), model_id.clone(), cx))
1165            .await
1166            .unwrap();
1167
1168        // Verify the thread has the selected model
1169        agent.read_with(cx, |agent, _| {
1170            let session = agent.sessions.get(&session_id).unwrap();
1171            session.thread.read_with(cx, |thread, _| {
1172                assert_eq!(thread.model().unwrap().id().0, "fake");
1173            });
1174        });
1175
1176        cx.run_until_parked();
1177
1178        // Verify settings file was updated
1179        let settings_content = fs.load(paths::settings_file()).await.unwrap();
1180        let settings_json: serde_json::Value = serde_json::from_str(&settings_content).unwrap();
1181
1182        // Check that the agent settings contain the selected model
1183        assert_eq!(
1184            settings_json["agent"]["default_model"]["model"],
1185            json!("fake")
1186        );
1187        assert_eq!(
1188            settings_json["agent"]["default_model"]["provider"],
1189            json!("fake")
1190        );
1191    }
1192
1193    #[gpui::test]
1194    #[cfg_attr(target_os = "windows", ignore)] // TODO: Fix this test on Windows
1195    async fn test_save_load_thread(cx: &mut TestAppContext) {
1196        init_test(cx);
1197        let fs = FakeFs::new(cx.executor());
1198        fs.insert_tree(
1199            "/",
1200            json!({
1201                "a": {
1202                    "b.md": "Lorem"
1203                }
1204            }),
1205        )
1206        .await;
1207        let project = Project::test(fs.clone(), [path!("/a").as_ref()], cx).await;
1208        let context_store = cx.new(|cx| assistant_context::ContextStore::fake(project.clone(), cx));
1209        let history_store = cx.new(|cx| HistoryStore::new(context_store, cx));
1210        let agent = NativeAgent::new(
1211            project.clone(),
1212            history_store.clone(),
1213            Templates::new(),
1214            None,
1215            fs.clone(),
1216            &mut cx.to_async(),
1217        )
1218        .await
1219        .unwrap();
1220        let connection = Rc::new(NativeAgentConnection(agent.clone()));
1221
1222        let acp_thread = cx
1223            .update(|cx| {
1224                connection
1225                    .clone()
1226                    .new_thread(project.clone(), Path::new(""), cx)
1227            })
1228            .await
1229            .unwrap();
1230        let session_id = acp_thread.read_with(cx, |thread, _| thread.session_id().clone());
1231        let thread = agent.read_with(cx, |agent, _| {
1232            agent.sessions.get(&session_id).unwrap().thread.clone()
1233        });
1234
1235        // Ensure empty threads are not saved, even if they get mutated.
1236        let model = Arc::new(FakeLanguageModel::default());
1237        let summary_model = Arc::new(FakeLanguageModel::default());
1238        thread.update(cx, |thread, cx| {
1239            thread.set_model(model, cx);
1240            thread.set_summarization_model(Some(summary_model), cx);
1241        });
1242        cx.run_until_parked();
1243        assert_eq!(history_entries(&history_store, cx), vec![]);
1244
1245        let model = thread.read_with(cx, |thread, _| thread.model().unwrap().clone());
1246        let model = model.as_fake();
1247        let summary_model = thread.read_with(cx, |thread, _| {
1248            thread.summarization_model().unwrap().clone()
1249        });
1250        let summary_model = summary_model.as_fake();
1251        let send = acp_thread.update(cx, |thread, cx| {
1252            thread.send(
1253                vec![
1254                    "What does ".into(),
1255                    acp::ContentBlock::ResourceLink(acp::ResourceLink {
1256                        name: "b.md".into(),
1257                        uri: MentionUri::File {
1258                            abs_path: path!("/a/b.md").into(),
1259                        }
1260                        .to_uri()
1261                        .to_string(),
1262                        annotations: None,
1263                        description: None,
1264                        mime_type: None,
1265                        size: None,
1266                        title: None,
1267                    }),
1268                    " mean?".into(),
1269                ],
1270                cx,
1271            )
1272        });
1273        let send = cx.foreground_executor().spawn(send);
1274        cx.run_until_parked();
1275
1276        model.send_last_completion_stream_text_chunk("Lorem.");
1277        model.end_last_completion_stream();
1278        cx.run_until_parked();
1279        summary_model.send_last_completion_stream_text_chunk("Explaining /a/b.md");
1280        summary_model.end_last_completion_stream();
1281
1282        send.await.unwrap();
1283        acp_thread.read_with(cx, |thread, cx| {
1284            assert_eq!(
1285                thread.to_markdown(cx),
1286                indoc! {"
1287                    ## User
1288
1289                    What does [@b.md](file:///a/b.md) mean?
1290
1291                    ## Assistant
1292
1293                    Lorem.
1294
1295                "}
1296            )
1297        });
1298
1299        // Drop the ACP thread, which should cause the session to be dropped as well.
1300        cx.update(|_| {
1301            drop(thread);
1302            drop(acp_thread);
1303        });
1304        agent.read_with(cx, |agent, _| {
1305            assert_eq!(agent.sessions.keys().cloned().collect::<Vec<_>>(), []);
1306        });
1307
1308        // Ensure the thread can be reloaded from disk.
1309        assert_eq!(
1310            history_entries(&history_store, cx),
1311            vec![(
1312                HistoryEntryId::AcpThread(session_id.clone()),
1313                "Explaining /a/b.md".into()
1314            )]
1315        );
1316        let acp_thread = agent
1317            .update(cx, |agent, cx| agent.open_thread(session_id.clone(), cx))
1318            .await
1319            .unwrap();
1320        acp_thread.read_with(cx, |thread, cx| {
1321            assert_eq!(
1322                thread.to_markdown(cx),
1323                indoc! {"
1324                    ## User
1325
1326                    What does [@b.md](file:///a/b.md) mean?
1327
1328                    ## Assistant
1329
1330                    Lorem.
1331
1332                "}
1333            )
1334        });
1335    }
1336
1337    fn history_entries(
1338        history: &Entity<HistoryStore>,
1339        cx: &mut TestAppContext,
1340    ) -> Vec<(HistoryEntryId, String)> {
1341        history.read_with(cx, |history, cx| {
1342            history
1343                .entries(cx)
1344                .iter()
1345                .map(|e| (e.id(), e.title().to_string()))
1346                .collect::<Vec<_>>()
1347        })
1348    }
1349
1350    fn init_test(cx: &mut TestAppContext) {
1351        env_logger::try_init().ok();
1352        cx.update(|cx| {
1353            let settings_store = SettingsStore::test(cx);
1354            cx.set_global(settings_store);
1355            Project::init_settings(cx);
1356            agent_settings::init(cx);
1357            language::init(cx);
1358            LanguageModelRegistry::test(cx);
1359        });
1360    }
1361}