inline_assistant.rs

   1use language_model::AnthropicEventData;
   2use language_model::report_anthropic_event;
   3use std::cmp;
   4use std::mem;
   5use std::ops::Range;
   6use std::rc::Rc;
   7use std::sync::Arc;
   8use uuid::Uuid;
   9
  10use crate::ThreadHistory;
  11use crate::context::load_context;
  12use crate::mention_set::MentionSet;
  13use crate::{
  14    AgentPanel,
  15    buffer_codegen::{BufferCodegen, CodegenAlternative, CodegenEvent},
  16    inline_prompt_editor::{CodegenStatus, InlineAssistId, PromptEditor, PromptEditorEvent},
  17    terminal_inline_assistant::TerminalInlineAssistant,
  18};
  19use agent::ThreadStore;
  20use agent_settings::AgentSettings;
  21use anyhow::{Context as _, Result};
  22use collections::{HashMap, HashSet, VecDeque, hash_map};
  23use editor::EditorSnapshot;
  24use editor::MultiBufferOffset;
  25use editor::RowExt;
  26use editor::SelectionEffects;
  27use editor::scroll::ScrollOffset;
  28use editor::{
  29    Anchor, AnchorRangeExt, CodeActionProvider, Editor, EditorEvent, ExcerptId, HighlightKey,
  30    MultiBuffer, MultiBufferSnapshot, ToOffset as _, ToPoint,
  31    actions::SelectAll,
  32    display_map::{
  33        BlockContext, BlockPlacement, BlockProperties, BlockStyle, CustomBlockId, EditorMargins,
  34        RenderBlock, ToDisplayPoint,
  35    },
  36};
  37use fs::Fs;
  38use futures::{FutureExt, channel::mpsc};
  39use gpui::{
  40    App, Context, Entity, Focusable, Global, HighlightStyle, Subscription, Task, UpdateGlobal,
  41    WeakEntity, Window, point,
  42};
  43use language::{Buffer, Point, Selection, TransactionId};
  44use language_model::{ConfigurationError, ConfiguredModel, LanguageModelRegistry};
  45use multi_buffer::MultiBufferRow;
  46use parking_lot::Mutex;
  47use project::{CodeAction, DisableAiSettings, LspAction, Project, ProjectTransaction};
  48use prompt_store::{PromptBuilder, PromptStore};
  49use settings::{Settings, SettingsStore};
  50
  51use terminal_view::{TerminalView, terminal_panel::TerminalPanel};
  52use text::{OffsetRangeExt, ToPoint as _};
  53use ui::prelude::*;
  54use util::{RangeExt, ResultExt, maybe};
  55use workspace::{ItemHandle, Toast, Workspace, dock::Panel, notifications::NotificationId};
  56use zed_actions::agent::OpenSettings;
  57
  58pub fn init(fs: Arc<dyn Fs>, prompt_builder: Arc<PromptBuilder>, cx: &mut App) {
  59    cx.set_global(InlineAssistant::new(fs, prompt_builder));
  60
  61    cx.observe_global::<SettingsStore>(|cx| {
  62        if DisableAiSettings::get_global(cx).disable_ai {
  63            // Hide any active inline assist UI when AI is disabled
  64            InlineAssistant::update_global(cx, |assistant, cx| {
  65                assistant.cancel_all_active_completions(cx);
  66            });
  67        }
  68    })
  69    .detach();
  70
  71    cx.observe_new(|_workspace: &mut Workspace, window, cx| {
  72        let Some(window) = window else {
  73            return;
  74        };
  75        let workspace = cx.entity();
  76        InlineAssistant::update_global(cx, |inline_assistant, cx| {
  77            inline_assistant.register_workspace(&workspace, window, cx)
  78        });
  79    })
  80    .detach();
  81}
  82
  83const PROMPT_HISTORY_MAX_LEN: usize = 20;
  84
  85enum InlineAssistTarget {
  86    Editor(Entity<Editor>),
  87    Terminal(Entity<TerminalView>),
  88}
  89
  90pub struct InlineAssistant {
  91    next_assist_id: InlineAssistId,
  92    next_assist_group_id: InlineAssistGroupId,
  93    assists: HashMap<InlineAssistId, InlineAssist>,
  94    assists_by_editor: HashMap<WeakEntity<Editor>, EditorInlineAssists>,
  95    assist_groups: HashMap<InlineAssistGroupId, InlineAssistGroup>,
  96    confirmed_assists: HashMap<InlineAssistId, Entity<CodegenAlternative>>,
  97    prompt_history: VecDeque<String>,
  98    prompt_builder: Arc<PromptBuilder>,
  99    fs: Arc<dyn Fs>,
 100    _inline_assistant_completions: Option<mpsc::UnboundedSender<anyhow::Result<InlineAssistId>>>,
 101}
 102
 103impl Global for InlineAssistant {}
 104
 105impl InlineAssistant {
 106    pub fn new(fs: Arc<dyn Fs>, prompt_builder: Arc<PromptBuilder>) -> Self {
 107        Self {
 108            next_assist_id: InlineAssistId::default(),
 109            next_assist_group_id: InlineAssistGroupId::default(),
 110            assists: HashMap::default(),
 111            assists_by_editor: HashMap::default(),
 112            assist_groups: HashMap::default(),
 113            confirmed_assists: HashMap::default(),
 114            prompt_history: VecDeque::default(),
 115            prompt_builder,
 116            fs,
 117            _inline_assistant_completions: None,
 118        }
 119    }
 120
 121    pub fn register_workspace(
 122        &mut self,
 123        workspace: &Entity<Workspace>,
 124        window: &mut Window,
 125        cx: &mut App,
 126    ) {
 127        window
 128            .subscribe(workspace, cx, |workspace, event, window, cx| {
 129                Self::update_global(cx, |this, cx| {
 130                    this.handle_workspace_event(workspace, event, window, cx)
 131                });
 132            })
 133            .detach();
 134
 135        let workspace_weak = workspace.downgrade();
 136        cx.observe_global::<SettingsStore>(move |cx| {
 137            let Some(workspace) = workspace_weak.upgrade() else {
 138                return;
 139            };
 140            let Some(terminal_panel) = workspace.read(cx).panel::<TerminalPanel>(cx) else {
 141                return;
 142            };
 143            let enabled = AgentSettings::get_global(cx).enabled(cx);
 144            terminal_panel.update(cx, |terminal_panel, cx| {
 145                terminal_panel.set_assistant_enabled(enabled, cx)
 146            });
 147        })
 148        .detach();
 149
 150        cx.observe(workspace, |workspace, cx| {
 151            let Some(terminal_panel) = workspace.read(cx).panel::<TerminalPanel>(cx) else {
 152                return;
 153            };
 154            let enabled = AgentSettings::get_global(cx).enabled(cx);
 155            if terminal_panel.read(cx).assistant_enabled() != enabled {
 156                terminal_panel.update(cx, |terminal_panel, cx| {
 157                    terminal_panel.set_assistant_enabled(enabled, cx)
 158                });
 159            }
 160        })
 161        .detach();
 162    }
 163
 164    /// Hides all active inline assists when AI is disabled
 165    pub fn cancel_all_active_completions(&mut self, cx: &mut App) {
 166        // Cancel all active completions in editors
 167        for (editor_handle, _) in self.assists_by_editor.iter() {
 168            if let Some(editor) = editor_handle.upgrade() {
 169                let windows = cx.windows();
 170                if !windows.is_empty() {
 171                    let window = windows[0];
 172                    let _ = window.update(cx, |_, window, cx| {
 173                        editor.update(cx, |editor, cx| {
 174                            if editor.has_active_edit_prediction() {
 175                                editor.cancel(&Default::default(), window, cx);
 176                            }
 177                        });
 178                    });
 179                }
 180            }
 181        }
 182    }
 183
 184    fn handle_workspace_event(
 185        &mut self,
 186        workspace: Entity<Workspace>,
 187        event: &workspace::Event,
 188        window: &mut Window,
 189        cx: &mut App,
 190    ) {
 191        match event {
 192            workspace::Event::UserSavedItem { item, .. } => {
 193                // When the user manually saves an editor, automatically accepts all finished transformations.
 194                if let Some(editor) = item.upgrade().and_then(|item| item.act_as::<Editor>(cx))
 195                    && let Some(editor_assists) = self.assists_by_editor.get(&editor.downgrade())
 196                {
 197                    for assist_id in editor_assists.assist_ids.clone() {
 198                        let assist = &self.assists[&assist_id];
 199                        if let CodegenStatus::Done = assist.codegen.read(cx).status(cx) {
 200                            self.finish_assist(assist_id, false, window, cx)
 201                        }
 202                    }
 203                }
 204            }
 205            workspace::Event::ItemAdded { item } => {
 206                self.register_workspace_item(&workspace, item.as_ref(), window, cx);
 207            }
 208            _ => (),
 209        }
 210    }
 211
 212    fn register_workspace_item(
 213        &mut self,
 214        workspace: &Entity<Workspace>,
 215        item: &dyn ItemHandle,
 216        window: &mut Window,
 217        cx: &mut App,
 218    ) {
 219        let is_ai_enabled = !DisableAiSettings::get_global(cx).disable_ai;
 220
 221        if let Some(editor) = item.act_as::<Editor>(cx) {
 222            editor.update(cx, |editor, cx| {
 223                if is_ai_enabled {
 224                    editor.add_code_action_provider(
 225                        Rc::new(AssistantCodeActionProvider {
 226                            editor: cx.entity().downgrade(),
 227                            workspace: workspace.downgrade(),
 228                        }),
 229                        window,
 230                        cx,
 231                    );
 232
 233                    if DisableAiSettings::get_global(cx).disable_ai {
 234                        // Cancel any active edit predictions
 235                        if editor.has_active_edit_prediction() {
 236                            editor.cancel(&Default::default(), window, cx);
 237                        }
 238                    }
 239                } else {
 240                    editor.remove_code_action_provider(
 241                        ASSISTANT_CODE_ACTION_PROVIDER_ID.into(),
 242                        window,
 243                        cx,
 244                    );
 245                }
 246            });
 247        }
 248    }
 249
 250    pub fn inline_assist(
 251        workspace: &mut Workspace,
 252        action: &zed_actions::assistant::InlineAssist,
 253        window: &mut Window,
 254        cx: &mut Context<Workspace>,
 255    ) {
 256        if !AgentSettings::get_global(cx).enabled(cx) {
 257            return;
 258        }
 259
 260        let Some(inline_assist_target) = Self::resolve_inline_assist_target(
 261            workspace,
 262            workspace.panel::<AgentPanel>(cx),
 263            window,
 264            cx,
 265        ) else {
 266            return;
 267        };
 268
 269        let configuration_error = || {
 270            let model_registry = LanguageModelRegistry::read_global(cx);
 271            model_registry.configuration_error(model_registry.inline_assistant_model(), cx)
 272        };
 273
 274        let Some(agent_panel) = workspace.panel::<AgentPanel>(cx) else {
 275            return;
 276        };
 277        let agent_panel = agent_panel.read(cx);
 278
 279        let prompt_store = agent_panel.prompt_store().as_ref().cloned();
 280        let thread_store = agent_panel.thread_store().clone();
 281        let history = agent_panel.history().downgrade();
 282
 283        let handle_assist =
 284            |window: &mut Window, cx: &mut Context<Workspace>| match inline_assist_target {
 285                InlineAssistTarget::Editor(active_editor) => {
 286                    InlineAssistant::update_global(cx, |assistant, cx| {
 287                        assistant.assist(
 288                            &active_editor,
 289                            cx.entity().downgrade(),
 290                            workspace.project().downgrade(),
 291                            thread_store,
 292                            prompt_store,
 293                            history,
 294                            action.prompt.clone(),
 295                            window,
 296                            cx,
 297                        );
 298                    })
 299                }
 300                InlineAssistTarget::Terminal(active_terminal) => {
 301                    TerminalInlineAssistant::update_global(cx, |assistant, cx| {
 302                        assistant.assist(
 303                            &active_terminal,
 304                            cx.entity().downgrade(),
 305                            workspace.project().downgrade(),
 306                            thread_store,
 307                            prompt_store,
 308                            history,
 309                            action.prompt.clone(),
 310                            window,
 311                            cx,
 312                        );
 313                    });
 314                }
 315            };
 316
 317        if let Some(error) = configuration_error() {
 318            if let ConfigurationError::ProviderNotAuthenticated(provider) = error {
 319                cx.spawn(async move |_, cx| {
 320                    cx.update(|cx| provider.authenticate(cx)).await?;
 321                    anyhow::Ok(())
 322                })
 323                .detach_and_log_err(cx);
 324
 325                if configuration_error().is_none() {
 326                    handle_assist(window, cx);
 327                }
 328            } else {
 329                cx.spawn_in(window, async move |_, cx| {
 330                    let answer = cx
 331                        .prompt(
 332                            gpui::PromptLevel::Warning,
 333                            &error.to_string(),
 334                            None,
 335                            &["Configure", "Cancel"],
 336                        )
 337                        .await
 338                        .ok();
 339                    if let Some(answer) = answer
 340                        && answer == 0
 341                    {
 342                        cx.update(|window, cx| window.dispatch_action(Box::new(OpenSettings), cx))
 343                            .ok();
 344                    }
 345                    anyhow::Ok(())
 346                })
 347                .detach_and_log_err(cx);
 348            }
 349        } else {
 350            handle_assist(window, cx);
 351        }
 352    }
 353
 354    fn codegen_ranges(
 355        &mut self,
 356        editor: &Entity<Editor>,
 357        snapshot: &EditorSnapshot,
 358        window: &mut Window,
 359        cx: &mut App,
 360    ) -> Option<(Vec<Range<Anchor>>, Selection<Point>)> {
 361        let (initial_selections, newest_selection) = editor.update(cx, |editor, _| {
 362            (
 363                editor.selections.all::<Point>(&snapshot.display_snapshot),
 364                editor
 365                    .selections
 366                    .newest::<Point>(&snapshot.display_snapshot),
 367            )
 368        });
 369
 370        // Check if there is already an inline assistant that contains the
 371        // newest selection, if there is, focus it
 372        if let Some(editor_assists) = self.assists_by_editor.get(&editor.downgrade()) {
 373            for assist_id in &editor_assists.assist_ids {
 374                let assist = &self.assists[assist_id];
 375                let range = assist.range.to_point(&snapshot.buffer_snapshot());
 376                if range.start.row <= newest_selection.start.row
 377                    && newest_selection.end.row <= range.end.row
 378                {
 379                    self.focus_assist(*assist_id, window, cx);
 380                    return None;
 381                }
 382            }
 383        }
 384
 385        let mut selections = Vec::<Selection<Point>>::new();
 386        let mut newest_selection = None;
 387        for mut selection in initial_selections {
 388            if selection.end == selection.start
 389                && let Some(fold) =
 390                    snapshot.crease_for_buffer_row(MultiBufferRow(selection.end.row))
 391            {
 392                selection.start = fold.range().start;
 393                selection.end = fold.range().end;
 394                if MultiBufferRow(selection.end.row) < snapshot.buffer_snapshot().max_row() {
 395                    let chars = snapshot
 396                        .buffer_snapshot()
 397                        .chars_at(Point::new(selection.end.row + 1, 0));
 398
 399                    for c in chars {
 400                        if c == '\n' {
 401                            break;
 402                        }
 403                        if c.is_whitespace() {
 404                            continue;
 405                        }
 406                        if snapshot
 407                            .language_at(selection.end)
 408                            .is_some_and(|language| language.config().brackets.is_closing_brace(c))
 409                        {
 410                            selection.end.row += 1;
 411                            selection.end.column = snapshot
 412                                .buffer_snapshot()
 413                                .line_len(MultiBufferRow(selection.end.row));
 414                        }
 415                    }
 416                }
 417            } else {
 418                selection.start.column = 0;
 419                // If the selection ends at the start of the line, we don't want to include it.
 420                if selection.end.column == 0 && selection.start.row != selection.end.row {
 421                    selection.end.row -= 1;
 422                }
 423                selection.end.column = snapshot
 424                    .buffer_snapshot()
 425                    .line_len(MultiBufferRow(selection.end.row));
 426            }
 427
 428            if let Some(prev_selection) = selections.last_mut()
 429                && selection.start <= prev_selection.end
 430            {
 431                prev_selection.end = selection.end;
 432                continue;
 433            }
 434
 435            let latest_selection = newest_selection.get_or_insert_with(|| selection.clone());
 436            if selection.id > latest_selection.id {
 437                *latest_selection = selection.clone();
 438            }
 439            selections.push(selection);
 440        }
 441        let snapshot = &snapshot.buffer_snapshot();
 442        let newest_selection = newest_selection.unwrap();
 443
 444        let mut codegen_ranges = Vec::new();
 445        for (buffer, buffer_range, excerpt_id) in selections
 446            .iter()
 447            .map(|selection| selection.range_to_buffer_range(selection.start..selection.end))
 448        {
 449            let anchor_range = Anchor::range_in_buffer(
 450                excerpt_id,
 451                buffer.anchor_before(buffer_range.start)..buffer.anchor_after(buffer_range.end),
 452            );
 453
 454            codegen_ranges.push(anchor_range);
 455
 456            if let Some(model) = LanguageModelRegistry::read_global(cx).inline_assistant_model() {
 457                telemetry::event!(
 458                    "Assistant Invoked",
 459                    kind = "inline",
 460                    phase = "invoked",
 461                    model = model.model.telemetry_id(),
 462                    model_provider = model.provider.id().to_string(),
 463                    language_name = buffer.language().map(|language| language.name().to_proto())
 464                );
 465
 466                report_anthropic_event(
 467                    &model.model,
 468                    AnthropicEventData {
 469                        completion_type: language_model::AnthropicCompletionType::Editor,
 470                        event: language_model::AnthropicEventType::Invoked,
 471                        language_name: buffer.language().map(|language| language.name().to_proto()),
 472                        message_id: None,
 473                    },
 474                    cx,
 475                );
 476            }
 477        }
 478
 479        Some((codegen_ranges, newest_selection))
 480    }
 481
 482    fn batch_assist(
 483        &mut self,
 484        editor: &Entity<Editor>,
 485        workspace: WeakEntity<Workspace>,
 486        project: WeakEntity<Project>,
 487        thread_store: Entity<ThreadStore>,
 488        prompt_store: Option<Entity<PromptStore>>,
 489        history: WeakEntity<ThreadHistory>,
 490        initial_prompt: Option<String>,
 491        window: &mut Window,
 492        codegen_ranges: &[Range<Anchor>],
 493        newest_selection: Option<Selection<Point>>,
 494        initial_transaction_id: Option<TransactionId>,
 495        cx: &mut App,
 496    ) -> Option<InlineAssistId> {
 497        let snapshot = editor.update(cx, |editor, cx| editor.snapshot(window, cx));
 498
 499        let assist_group_id = self.next_assist_group_id.post_inc();
 500        let session_id = Uuid::new_v4();
 501        let prompt_buffer = cx.new(|cx| {
 502            MultiBuffer::singleton(
 503                cx.new(|cx| Buffer::local(initial_prompt.unwrap_or_default(), cx)),
 504                cx,
 505            )
 506        });
 507
 508        let mut assists = Vec::new();
 509        let mut assist_to_focus = None;
 510
 511        for range in codegen_ranges {
 512            let assist_id = self.next_assist_id.post_inc();
 513            let codegen = cx.new(|cx| {
 514                BufferCodegen::new(
 515                    editor.read(cx).buffer().clone(),
 516                    range.clone(),
 517                    initial_transaction_id,
 518                    session_id,
 519                    self.prompt_builder.clone(),
 520                    cx,
 521                )
 522            });
 523
 524            let editor_margins = Arc::new(Mutex::new(EditorMargins::default()));
 525            let prompt_editor = cx.new(|cx| {
 526                PromptEditor::new_buffer(
 527                    assist_id,
 528                    editor_margins,
 529                    self.prompt_history.clone(),
 530                    prompt_buffer.clone(),
 531                    codegen.clone(),
 532                    session_id,
 533                    self.fs.clone(),
 534                    thread_store.clone(),
 535                    prompt_store.clone(),
 536                    history.clone(),
 537                    project.clone(),
 538                    workspace.clone(),
 539                    window,
 540                    cx,
 541                )
 542            });
 543
 544            if let Some(newest_selection) = newest_selection.as_ref()
 545                && assist_to_focus.is_none()
 546            {
 547                let focus_assist = if newest_selection.reversed {
 548                    range.start.to_point(&snapshot) == newest_selection.start
 549                } else {
 550                    range.end.to_point(&snapshot) == newest_selection.end
 551                };
 552                if focus_assist {
 553                    assist_to_focus = Some(assist_id);
 554                }
 555            }
 556
 557            let [prompt_block_id, tool_description_block_id, end_block_id] =
 558                self.insert_assist_blocks(&editor, &range, &prompt_editor, cx);
 559
 560            assists.push((
 561                assist_id,
 562                range.clone(),
 563                prompt_editor,
 564                prompt_block_id,
 565                tool_description_block_id,
 566                end_block_id,
 567            ));
 568        }
 569
 570        let editor_assists = self
 571            .assists_by_editor
 572            .entry(editor.downgrade())
 573            .or_insert_with(|| EditorInlineAssists::new(editor, window, cx));
 574
 575        let assist_to_focus = if let Some(focus_id) = assist_to_focus {
 576            Some(focus_id)
 577        } else if assists.len() >= 1 {
 578            Some(assists[0].0)
 579        } else {
 580            None
 581        };
 582
 583        let mut assist_group = InlineAssistGroup::new();
 584        for (
 585            assist_id,
 586            range,
 587            prompt_editor,
 588            prompt_block_id,
 589            tool_description_block_id,
 590            end_block_id,
 591        ) in assists
 592        {
 593            let codegen = prompt_editor.read(cx).codegen().clone();
 594
 595            self.assists.insert(
 596                assist_id,
 597                InlineAssist::new(
 598                    assist_id,
 599                    assist_group_id,
 600                    editor,
 601                    &prompt_editor,
 602                    prompt_block_id,
 603                    tool_description_block_id,
 604                    end_block_id,
 605                    range,
 606                    codegen,
 607                    workspace.clone(),
 608                    window,
 609                    cx,
 610                ),
 611            );
 612            assist_group.assist_ids.push(assist_id);
 613            editor_assists.assist_ids.push(assist_id);
 614        }
 615
 616        self.assist_groups.insert(assist_group_id, assist_group);
 617
 618        assist_to_focus
 619    }
 620
 621    pub fn assist(
 622        &mut self,
 623        editor: &Entity<Editor>,
 624        workspace: WeakEntity<Workspace>,
 625        project: WeakEntity<Project>,
 626        thread_store: Entity<ThreadStore>,
 627        prompt_store: Option<Entity<PromptStore>>,
 628        history: WeakEntity<ThreadHistory>,
 629        initial_prompt: Option<String>,
 630        window: &mut Window,
 631        cx: &mut App,
 632    ) -> Option<InlineAssistId> {
 633        let snapshot = editor.update(cx, |editor, cx| editor.snapshot(window, cx));
 634
 635        let Some((codegen_ranges, newest_selection)) =
 636            self.codegen_ranges(editor, &snapshot, window, cx)
 637        else {
 638            return None;
 639        };
 640
 641        let assist_to_focus = self.batch_assist(
 642            editor,
 643            workspace,
 644            project,
 645            thread_store,
 646            prompt_store,
 647            history,
 648            initial_prompt,
 649            window,
 650            &codegen_ranges,
 651            Some(newest_selection),
 652            None,
 653            cx,
 654        );
 655
 656        if let Some(assist_id) = assist_to_focus {
 657            self.focus_assist(assist_id, window, cx);
 658        }
 659
 660        assist_to_focus
 661    }
 662
 663    pub fn suggest_assist(
 664        &mut self,
 665        editor: &Entity<Editor>,
 666        mut range: Range<Anchor>,
 667        initial_prompt: String,
 668        initial_transaction_id: Option<TransactionId>,
 669        focus: bool,
 670        workspace: Entity<Workspace>,
 671        thread_store: Entity<ThreadStore>,
 672        prompt_store: Option<Entity<PromptStore>>,
 673        history: WeakEntity<ThreadHistory>,
 674        window: &mut Window,
 675        cx: &mut App,
 676    ) -> InlineAssistId {
 677        let buffer = editor.read(cx).buffer().clone();
 678        {
 679            let snapshot = buffer.read(cx).read(cx);
 680            range.start = range.start.bias_left(&snapshot);
 681            range.end = range.end.bias_right(&snapshot);
 682        }
 683
 684        let project = workspace.read(cx).project().downgrade();
 685
 686        let assist_id = self
 687            .batch_assist(
 688                editor,
 689                workspace.downgrade(),
 690                project,
 691                thread_store,
 692                prompt_store,
 693                history,
 694                Some(initial_prompt),
 695                window,
 696                &[range],
 697                None,
 698                initial_transaction_id,
 699                cx,
 700            )
 701            .expect("batch_assist returns an id if there's only one range");
 702
 703        if focus {
 704            self.focus_assist(assist_id, window, cx);
 705        }
 706
 707        assist_id
 708    }
 709
 710    fn insert_assist_blocks(
 711        &self,
 712        editor: &Entity<Editor>,
 713        range: &Range<Anchor>,
 714        prompt_editor: &Entity<PromptEditor<BufferCodegen>>,
 715        cx: &mut App,
 716    ) -> [CustomBlockId; 3] {
 717        let prompt_editor_height = prompt_editor.update(cx, |prompt_editor, cx| {
 718            prompt_editor
 719                .editor
 720                .update(cx, |editor, cx| editor.max_point(cx).row().0 + 1 + 2)
 721        });
 722        let assist_blocks = vec![
 723            BlockProperties {
 724                style: BlockStyle::Sticky,
 725                placement: BlockPlacement::Above(range.start),
 726                height: Some(prompt_editor_height),
 727                render: build_assist_editor_renderer(prompt_editor),
 728                priority: 0,
 729            },
 730            // Placeholder for tool description - will be updated dynamically
 731            BlockProperties {
 732                style: BlockStyle::Flex,
 733                placement: BlockPlacement::Below(range.end),
 734                height: Some(0),
 735                render: Arc::new(|_cx| div().into_any_element()),
 736                priority: 0,
 737            },
 738            BlockProperties {
 739                style: BlockStyle::Sticky,
 740                placement: BlockPlacement::Below(range.end),
 741                height: None,
 742                render: Arc::new(|cx| {
 743                    v_flex()
 744                        .h_full()
 745                        .w_full()
 746                        .border_t_1()
 747                        .border_color(cx.theme().status().info_border)
 748                        .into_any_element()
 749                }),
 750                priority: 0,
 751            },
 752        ];
 753
 754        editor.update(cx, |editor, cx| {
 755            let block_ids = editor.insert_blocks(assist_blocks, None, cx);
 756            [block_ids[0], block_ids[1], block_ids[2]]
 757        })
 758    }
 759
 760    fn handle_prompt_editor_focus_in(&mut self, assist_id: InlineAssistId, cx: &mut App) {
 761        let assist = &self.assists[&assist_id];
 762        let Some(decorations) = assist.decorations.as_ref() else {
 763            return;
 764        };
 765        let assist_group = self.assist_groups.get_mut(&assist.group_id).unwrap();
 766        let editor_assists = self.assists_by_editor.get_mut(&assist.editor).unwrap();
 767
 768        assist_group.active_assist_id = Some(assist_id);
 769        if assist_group.linked {
 770            for assist_id in &assist_group.assist_ids {
 771                if let Some(decorations) = self.assists[assist_id].decorations.as_ref() {
 772                    decorations.prompt_editor.update(cx, |prompt_editor, cx| {
 773                        prompt_editor.set_show_cursor_when_unfocused(true, cx)
 774                    });
 775                }
 776            }
 777        }
 778
 779        assist
 780            .editor
 781            .update(cx, |editor, cx| {
 782                let scroll_top = editor.scroll_position(cx).y;
 783                let scroll_bottom = scroll_top + editor.visible_line_count().unwrap_or(0.);
 784                editor_assists.scroll_lock = editor
 785                    .row_for_block(decorations.prompt_block_id, cx)
 786                    .map(|row| row.as_f64())
 787                    .filter(|prompt_row| (scroll_top..scroll_bottom).contains(&prompt_row))
 788                    .map(|prompt_row| InlineAssistScrollLock {
 789                        assist_id,
 790                        distance_from_top: prompt_row - scroll_top,
 791                    });
 792            })
 793            .ok();
 794    }
 795
 796    fn handle_prompt_editor_focus_out(&mut self, assist_id: InlineAssistId, cx: &mut App) {
 797        let assist = &self.assists[&assist_id];
 798        let assist_group = self.assist_groups.get_mut(&assist.group_id).unwrap();
 799        if assist_group.active_assist_id == Some(assist_id) {
 800            assist_group.active_assist_id = None;
 801            if assist_group.linked {
 802                for assist_id in &assist_group.assist_ids {
 803                    if let Some(decorations) = self.assists[assist_id].decorations.as_ref() {
 804                        decorations.prompt_editor.update(cx, |prompt_editor, cx| {
 805                            prompt_editor.set_show_cursor_when_unfocused(false, cx)
 806                        });
 807                    }
 808                }
 809            }
 810        }
 811    }
 812
 813    fn handle_prompt_editor_event(
 814        &mut self,
 815        prompt_editor: Entity<PromptEditor<BufferCodegen>>,
 816        event: &PromptEditorEvent,
 817        window: &mut Window,
 818        cx: &mut App,
 819    ) {
 820        let assist_id = prompt_editor.read(cx).id();
 821        match event {
 822            PromptEditorEvent::StartRequested => {
 823                self.start_assist(assist_id, window, cx);
 824            }
 825            PromptEditorEvent::StopRequested => {
 826                self.stop_assist(assist_id, cx);
 827            }
 828            PromptEditorEvent::ConfirmRequested { execute: _ } => {
 829                self.finish_assist(assist_id, false, window, cx);
 830            }
 831            PromptEditorEvent::CancelRequested => {
 832                self.finish_assist(assist_id, true, window, cx);
 833            }
 834            PromptEditorEvent::Resized { .. } => {
 835                // This only matters for the terminal inline assistant
 836            }
 837        }
 838    }
 839
 840    fn handle_editor_newline(&mut self, editor: Entity<Editor>, window: &mut Window, cx: &mut App) {
 841        let Some(editor_assists) = self.assists_by_editor.get(&editor.downgrade()) else {
 842            return;
 843        };
 844
 845        if editor.read(cx).selections.count() == 1 {
 846            let (selection, buffer) = editor.update(cx, |editor, cx| {
 847                (
 848                    editor
 849                        .selections
 850                        .newest::<MultiBufferOffset>(&editor.display_snapshot(cx)),
 851                    editor.buffer().read(cx).snapshot(cx),
 852                )
 853            });
 854            for assist_id in &editor_assists.assist_ids {
 855                let assist = &self.assists[assist_id];
 856                let assist_range = assist.range.to_offset(&buffer);
 857                if assist_range.contains(&selection.start) && assist_range.contains(&selection.end)
 858                {
 859                    if matches!(assist.codegen.read(cx).status(cx), CodegenStatus::Pending) {
 860                        self.dismiss_assist(*assist_id, window, cx);
 861                    } else {
 862                        self.finish_assist(*assist_id, false, window, cx);
 863                    }
 864
 865                    return;
 866                }
 867            }
 868        }
 869
 870        cx.propagate();
 871    }
 872
 873    fn handle_editor_cancel(&mut self, editor: Entity<Editor>, window: &mut Window, cx: &mut App) {
 874        let Some(editor_assists) = self.assists_by_editor.get(&editor.downgrade()) else {
 875            return;
 876        };
 877
 878        if editor.read(cx).selections.count() == 1 {
 879            let (selection, buffer) = editor.update(cx, |editor, cx| {
 880                (
 881                    editor
 882                        .selections
 883                        .newest::<MultiBufferOffset>(&editor.display_snapshot(cx)),
 884                    editor.buffer().read(cx).snapshot(cx),
 885                )
 886            });
 887            let mut closest_assist_fallback = None;
 888            for assist_id in &editor_assists.assist_ids {
 889                let assist = &self.assists[assist_id];
 890                let assist_range = assist.range.to_offset(&buffer);
 891                if assist.decorations.is_some() {
 892                    if assist_range.contains(&selection.start)
 893                        && assist_range.contains(&selection.end)
 894                    {
 895                        self.focus_assist(*assist_id, window, cx);
 896                        return;
 897                    } else {
 898                        let distance_from_selection = assist_range
 899                            .start
 900                            .0
 901                            .abs_diff(selection.start.0)
 902                            .min(assist_range.start.0.abs_diff(selection.end.0))
 903                            + assist_range
 904                                .end
 905                                .0
 906                                .abs_diff(selection.start.0)
 907                                .min(assist_range.end.0.abs_diff(selection.end.0));
 908                        match closest_assist_fallback {
 909                            Some((_, old_distance)) => {
 910                                if distance_from_selection < old_distance {
 911                                    closest_assist_fallback =
 912                                        Some((assist_id, distance_from_selection));
 913                                }
 914                            }
 915                            None => {
 916                                closest_assist_fallback = Some((assist_id, distance_from_selection))
 917                            }
 918                        }
 919                    }
 920                }
 921            }
 922
 923            if let Some((&assist_id, _)) = closest_assist_fallback {
 924                self.focus_assist(assist_id, window, cx);
 925            }
 926        }
 927
 928        cx.propagate();
 929    }
 930
 931    fn handle_editor_release(
 932        &mut self,
 933        editor: WeakEntity<Editor>,
 934        window: &mut Window,
 935        cx: &mut App,
 936    ) {
 937        if let Some(editor_assists) = self.assists_by_editor.get_mut(&editor) {
 938            for assist_id in editor_assists.assist_ids.clone() {
 939                self.finish_assist(assist_id, true, window, cx);
 940            }
 941        }
 942    }
 943
 944    fn handle_editor_change(&mut self, editor: Entity<Editor>, window: &mut Window, cx: &mut App) {
 945        let Some(editor_assists) = self.assists_by_editor.get(&editor.downgrade()) else {
 946            return;
 947        };
 948        let Some(scroll_lock) = editor_assists.scroll_lock.as_ref() else {
 949            return;
 950        };
 951        let assist = &self.assists[&scroll_lock.assist_id];
 952        let Some(decorations) = assist.decorations.as_ref() else {
 953            return;
 954        };
 955
 956        editor.update(cx, |editor, cx| {
 957            let scroll_position = editor.scroll_position(cx);
 958            let target_scroll_top = editor
 959                .row_for_block(decorations.prompt_block_id, cx)?
 960                .as_f64()
 961                - scroll_lock.distance_from_top;
 962            if target_scroll_top != scroll_position.y {
 963                editor.set_scroll_position(point(scroll_position.x, target_scroll_top), window, cx);
 964            }
 965            Some(())
 966        });
 967    }
 968
 969    fn handle_editor_event(
 970        &mut self,
 971        editor: Entity<Editor>,
 972        event: &EditorEvent,
 973        window: &mut Window,
 974        cx: &mut App,
 975    ) {
 976        let Some(editor_assists) = self.assists_by_editor.get_mut(&editor.downgrade()) else {
 977            return;
 978        };
 979
 980        match event {
 981            EditorEvent::Edited { transaction_id } => {
 982                let buffer = editor.read(cx).buffer().read(cx);
 983                let edited_ranges =
 984                    buffer.edited_ranges_for_transaction::<MultiBufferOffset>(*transaction_id, cx);
 985                let snapshot = buffer.snapshot(cx);
 986
 987                for assist_id in editor_assists.assist_ids.clone() {
 988                    let assist = &self.assists[&assist_id];
 989                    if matches!(
 990                        assist.codegen.read(cx).status(cx),
 991                        CodegenStatus::Error(_) | CodegenStatus::Done
 992                    ) {
 993                        let assist_range = assist.range.to_offset(&snapshot);
 994                        if edited_ranges
 995                            .iter()
 996                            .any(|range| range.overlaps(&assist_range))
 997                        {
 998                            self.finish_assist(assist_id, false, window, cx);
 999                        }
1000                    }
1001                }
1002            }
1003            EditorEvent::ScrollPositionChanged { .. } => {
1004                if let Some(scroll_lock) = editor_assists.scroll_lock.as_ref() {
1005                    let assist = &self.assists[&scroll_lock.assist_id];
1006                    if let Some(decorations) = assist.decorations.as_ref() {
1007                        let distance_from_top = editor.update(cx, |editor, cx| {
1008                            let scroll_top = editor.scroll_position(cx).y;
1009                            let prompt_row = editor
1010                                .row_for_block(decorations.prompt_block_id, cx)?
1011                                .0 as ScrollOffset;
1012                            Some(prompt_row - scroll_top)
1013                        });
1014
1015                        if distance_from_top.is_none_or(|distance_from_top| {
1016                            distance_from_top != scroll_lock.distance_from_top
1017                        }) {
1018                            editor_assists.scroll_lock = None;
1019                        }
1020                    }
1021                }
1022            }
1023            EditorEvent::SelectionsChanged { .. } => {
1024                for assist_id in editor_assists.assist_ids.clone() {
1025                    let assist = &self.assists[&assist_id];
1026                    if let Some(decorations) = assist.decorations.as_ref()
1027                        && decorations
1028                            .prompt_editor
1029                            .focus_handle(cx)
1030                            .is_focused(window)
1031                    {
1032                        return;
1033                    }
1034                }
1035
1036                editor_assists.scroll_lock = None;
1037            }
1038            _ => {}
1039        }
1040    }
1041
1042    pub fn finish_assist(
1043        &mut self,
1044        assist_id: InlineAssistId,
1045        undo: bool,
1046        window: &mut Window,
1047        cx: &mut App,
1048    ) {
1049        if let Some(assist) = self.assists.get(&assist_id) {
1050            let assist_group_id = assist.group_id;
1051            if self.assist_groups[&assist_group_id].linked {
1052                for assist_id in self.unlink_assist_group(assist_group_id, window, cx) {
1053                    self.finish_assist(assist_id, undo, window, cx);
1054                }
1055                return;
1056            }
1057        }
1058
1059        self.dismiss_assist(assist_id, window, cx);
1060
1061        if let Some(assist) = self.assists.remove(&assist_id) {
1062            if let hash_map::Entry::Occupied(mut entry) = self.assist_groups.entry(assist.group_id)
1063            {
1064                entry.get_mut().assist_ids.retain(|id| *id != assist_id);
1065                if entry.get().assist_ids.is_empty() {
1066                    entry.remove();
1067                }
1068            }
1069
1070            if let hash_map::Entry::Occupied(mut entry) =
1071                self.assists_by_editor.entry(assist.editor.clone())
1072            {
1073                entry.get_mut().assist_ids.retain(|id| *id != assist_id);
1074                if entry.get().assist_ids.is_empty() {
1075                    entry.remove();
1076                    if let Some(editor) = assist.editor.upgrade() {
1077                        self.update_editor_highlights(&editor, cx);
1078                    }
1079                } else {
1080                    entry.get_mut().highlight_updates.send(()).ok();
1081                }
1082            }
1083
1084            let active_alternative = assist.codegen.read(cx).active_alternative().clone();
1085            if let Some(model) = LanguageModelRegistry::read_global(cx).inline_assistant_model() {
1086                let language_name = assist.editor.upgrade().and_then(|editor| {
1087                    let multibuffer = editor.read(cx).buffer().read(cx);
1088                    let snapshot = multibuffer.snapshot(cx);
1089                    let ranges =
1090                        snapshot.range_to_buffer_ranges(assist.range.start..=assist.range.end);
1091                    ranges
1092                        .first()
1093                        .and_then(|(buffer, _, _)| buffer.language())
1094                        .map(|language| language.name().0.to_string())
1095                });
1096
1097                let codegen = assist.codegen.read(cx);
1098                let session_id = codegen.session_id();
1099                let message_id = active_alternative.read(cx).message_id.clone();
1100                let model_telemetry_id = model.model.telemetry_id();
1101                let model_provider_id = model.model.provider_id().to_string();
1102
1103                let (phase, event_type, anthropic_event_type) = if undo {
1104                    (
1105                        "rejected",
1106                        "Assistant Response Rejected",
1107                        language_model::AnthropicEventType::Reject,
1108                    )
1109                } else {
1110                    (
1111                        "accepted",
1112                        "Assistant Response Accepted",
1113                        language_model::AnthropicEventType::Accept,
1114                    )
1115                };
1116
1117                telemetry::event!(
1118                    event_type,
1119                    phase,
1120                    session_id = session_id.to_string(),
1121                    kind = "inline",
1122                    model = model_telemetry_id,
1123                    model_provider = model_provider_id,
1124                    language_name = language_name,
1125                    message_id = message_id.as_deref(),
1126                );
1127
1128                report_anthropic_event(
1129                    &model.model,
1130                    language_model::AnthropicEventData {
1131                        completion_type: language_model::AnthropicCompletionType::Editor,
1132                        event: anthropic_event_type,
1133                        language_name,
1134                        message_id,
1135                    },
1136                    cx,
1137                );
1138            }
1139
1140            if undo {
1141                assist.codegen.update(cx, |codegen, cx| codegen.undo(cx));
1142            } else {
1143                self.confirmed_assists.insert(assist_id, active_alternative);
1144            }
1145        }
1146    }
1147
1148    fn dismiss_assist(
1149        &mut self,
1150        assist_id: InlineAssistId,
1151        window: &mut Window,
1152        cx: &mut App,
1153    ) -> bool {
1154        let Some(assist) = self.assists.get_mut(&assist_id) else {
1155            return false;
1156        };
1157        let Some(editor) = assist.editor.upgrade() else {
1158            return false;
1159        };
1160        let Some(decorations) = assist.decorations.take() else {
1161            return false;
1162        };
1163
1164        editor.update(cx, |editor, cx| {
1165            let mut to_remove = decorations.removed_line_block_ids;
1166            to_remove.insert(decorations.prompt_block_id);
1167            to_remove.insert(decorations.end_block_id);
1168            if let Some(tool_description_block_id) = decorations.model_explanation {
1169                to_remove.insert(tool_description_block_id);
1170            }
1171            editor.remove_blocks(to_remove, None, cx);
1172        });
1173
1174        if decorations
1175            .prompt_editor
1176            .focus_handle(cx)
1177            .contains_focused(window, cx)
1178        {
1179            self.focus_next_assist(assist_id, window, cx);
1180        }
1181
1182        if let Some(editor_assists) = self.assists_by_editor.get_mut(&editor.downgrade()) {
1183            if editor_assists
1184                .scroll_lock
1185                .as_ref()
1186                .is_some_and(|lock| lock.assist_id == assist_id)
1187            {
1188                editor_assists.scroll_lock = None;
1189            }
1190            editor_assists.highlight_updates.send(()).ok();
1191        }
1192
1193        true
1194    }
1195
1196    fn focus_next_assist(&mut self, assist_id: InlineAssistId, window: &mut Window, cx: &mut App) {
1197        let Some(assist) = self.assists.get(&assist_id) else {
1198            return;
1199        };
1200
1201        let assist_group = &self.assist_groups[&assist.group_id];
1202        let assist_ix = assist_group
1203            .assist_ids
1204            .iter()
1205            .position(|id| *id == assist_id)
1206            .unwrap();
1207        let assist_ids = assist_group
1208            .assist_ids
1209            .iter()
1210            .skip(assist_ix + 1)
1211            .chain(assist_group.assist_ids.iter().take(assist_ix));
1212
1213        for assist_id in assist_ids {
1214            let assist = &self.assists[assist_id];
1215            if assist.decorations.is_some() {
1216                self.focus_assist(*assist_id, window, cx);
1217                return;
1218            }
1219        }
1220
1221        assist
1222            .editor
1223            .update(cx, |editor, cx| window.focus(&editor.focus_handle(cx), cx))
1224            .ok();
1225    }
1226
1227    fn focus_assist(&mut self, assist_id: InlineAssistId, window: &mut Window, cx: &mut App) {
1228        let Some(assist) = self.assists.get(&assist_id) else {
1229            return;
1230        };
1231
1232        if let Some(decorations) = assist.decorations.as_ref() {
1233            decorations.prompt_editor.update(cx, |prompt_editor, cx| {
1234                prompt_editor.editor.update(cx, |editor, cx| {
1235                    window.focus(&editor.focus_handle(cx), cx);
1236                    editor.select_all(&SelectAll, window, cx);
1237                })
1238            });
1239        }
1240
1241        self.scroll_to_assist(assist_id, window, cx);
1242    }
1243
1244    pub fn scroll_to_assist(
1245        &mut self,
1246        assist_id: InlineAssistId,
1247        window: &mut Window,
1248        cx: &mut App,
1249    ) {
1250        let Some(assist) = self.assists.get(&assist_id) else {
1251            return;
1252        };
1253        let Some(editor) = assist.editor.upgrade() else {
1254            return;
1255        };
1256
1257        let position = assist.range.start;
1258        editor.update(cx, |editor, cx| {
1259            editor.change_selections(SelectionEffects::no_scroll(), window, cx, |selections| {
1260                selections.select_anchor_ranges([position..position])
1261            });
1262
1263            let mut scroll_target_range = None;
1264            if let Some(decorations) = assist.decorations.as_ref() {
1265                scroll_target_range = maybe!({
1266                    let top = editor.row_for_block(decorations.prompt_block_id, cx)?.0 as f64;
1267                    let bottom = editor.row_for_block(decorations.end_block_id, cx)?.0 as f64;
1268                    Some((top, bottom))
1269                });
1270                if scroll_target_range.is_none() {
1271                    log::error!("bug: failed to find blocks for scrolling to inline assist");
1272                }
1273            }
1274            let scroll_target_range = scroll_target_range.unwrap_or_else(|| {
1275                let snapshot = editor.snapshot(window, cx);
1276                let start_row = assist
1277                    .range
1278                    .start
1279                    .to_display_point(&snapshot.display_snapshot)
1280                    .row();
1281                let top = start_row.0 as ScrollOffset;
1282                let bottom = top + 1.0;
1283                (top, bottom)
1284            });
1285            let height_in_lines = editor.visible_line_count().unwrap_or(0.);
1286            let vertical_scroll_margin = editor.vertical_scroll_margin() as ScrollOffset;
1287            let scroll_target_top = (scroll_target_range.0 - vertical_scroll_margin)
1288                // Don't scroll up too far in the case of a large vertical_scroll_margin.
1289                .max(scroll_target_range.0 - height_in_lines / 2.0);
1290            let scroll_target_bottom = (scroll_target_range.1 + vertical_scroll_margin)
1291                // Don't scroll down past where the top would still be visible.
1292                .min(scroll_target_top + height_in_lines);
1293
1294            let scroll_top = editor.scroll_position(cx).y;
1295            let scroll_bottom = scroll_top + height_in_lines;
1296
1297            if scroll_target_top < scroll_top {
1298                editor.set_scroll_position(point(0., scroll_target_top), window, cx);
1299            } else if scroll_target_bottom > scroll_bottom {
1300                editor.set_scroll_position(
1301                    point(0., scroll_target_bottom - height_in_lines),
1302                    window,
1303                    cx,
1304                );
1305            }
1306        });
1307    }
1308
1309    fn unlink_assist_group(
1310        &mut self,
1311        assist_group_id: InlineAssistGroupId,
1312        window: &mut Window,
1313        cx: &mut App,
1314    ) -> Vec<InlineAssistId> {
1315        let assist_group = self.assist_groups.get_mut(&assist_group_id).unwrap();
1316        assist_group.linked = false;
1317
1318        for assist_id in &assist_group.assist_ids {
1319            let assist = self.assists.get_mut(assist_id).unwrap();
1320            if let Some(editor_decorations) = assist.decorations.as_ref() {
1321                editor_decorations
1322                    .prompt_editor
1323                    .update(cx, |prompt_editor, cx| prompt_editor.unlink(window, cx));
1324            }
1325        }
1326        assist_group.assist_ids.clone()
1327    }
1328
1329    pub fn start_assist(&mut self, assist_id: InlineAssistId, window: &mut Window, cx: &mut App) {
1330        let assist = if let Some(assist) = self.assists.get_mut(&assist_id) {
1331            assist
1332        } else {
1333            return;
1334        };
1335
1336        let assist_group_id = assist.group_id;
1337        if self.assist_groups[&assist_group_id].linked {
1338            for assist_id in self.unlink_assist_group(assist_group_id, window, cx) {
1339                self.start_assist(assist_id, window, cx);
1340            }
1341            return;
1342        }
1343
1344        let Some((user_prompt, mention_set)) = assist.user_prompt(cx).zip(assist.mention_set(cx))
1345        else {
1346            return;
1347        };
1348
1349        self.prompt_history.retain(|prompt| *prompt != user_prompt);
1350        self.prompt_history.push_back(user_prompt.clone());
1351        if self.prompt_history.len() > PROMPT_HISTORY_MAX_LEN {
1352            self.prompt_history.pop_front();
1353        }
1354
1355        let Some(ConfiguredModel { model, .. }) =
1356            LanguageModelRegistry::read_global(cx).inline_assistant_model()
1357        else {
1358            return;
1359        };
1360
1361        let context_task = load_context(&mention_set, cx).shared();
1362        assist
1363            .codegen
1364            .update(cx, |codegen, cx| {
1365                codegen.start(model, user_prompt, context_task, cx)
1366            })
1367            .log_err();
1368    }
1369
1370    pub fn stop_assist(&mut self, assist_id: InlineAssistId, cx: &mut App) {
1371        let assist = if let Some(assist) = self.assists.get_mut(&assist_id) {
1372            assist
1373        } else {
1374            return;
1375        };
1376
1377        assist.codegen.update(cx, |codegen, cx| codegen.stop(cx));
1378    }
1379
1380    fn update_editor_highlights(&self, editor: &Entity<Editor>, cx: &mut App) {
1381        let mut gutter_pending_ranges = Vec::new();
1382        let mut gutter_transformed_ranges = Vec::new();
1383        let mut foreground_ranges = Vec::new();
1384        let mut inserted_row_ranges = Vec::new();
1385        let empty_assist_ids = Vec::new();
1386        let assist_ids = self
1387            .assists_by_editor
1388            .get(&editor.downgrade())
1389            .map_or(&empty_assist_ids, |editor_assists| {
1390                &editor_assists.assist_ids
1391            });
1392
1393        for assist_id in assist_ids {
1394            if let Some(assist) = self.assists.get(assist_id) {
1395                let codegen = assist.codegen.read(cx);
1396                let buffer = codegen.buffer(cx).read(cx).read(cx);
1397                foreground_ranges.extend(codegen.last_equal_ranges(cx).iter().cloned());
1398
1399                let pending_range =
1400                    codegen.edit_position(cx).unwrap_or(assist.range.start)..assist.range.end;
1401                if pending_range.end.to_offset(&buffer) > pending_range.start.to_offset(&buffer) {
1402                    gutter_pending_ranges.push(pending_range);
1403                }
1404
1405                if let Some(edit_position) = codegen.edit_position(cx) {
1406                    let edited_range = assist.range.start..edit_position;
1407                    if edited_range.end.to_offset(&buffer) > edited_range.start.to_offset(&buffer) {
1408                        gutter_transformed_ranges.push(edited_range);
1409                    }
1410                }
1411
1412                if assist.decorations.is_some() {
1413                    inserted_row_ranges
1414                        .extend(codegen.diff(cx).inserted_row_ranges.iter().cloned());
1415                }
1416            }
1417        }
1418
1419        let snapshot = editor.read(cx).buffer().read(cx).snapshot(cx);
1420        merge_ranges(&mut foreground_ranges, &snapshot);
1421        merge_ranges(&mut gutter_pending_ranges, &snapshot);
1422        merge_ranges(&mut gutter_transformed_ranges, &snapshot);
1423        editor.update(cx, |editor, cx| {
1424            enum GutterPendingRange {}
1425            if gutter_pending_ranges.is_empty() {
1426                editor.clear_gutter_highlights::<GutterPendingRange>(cx);
1427            } else {
1428                editor.highlight_gutter::<GutterPendingRange>(
1429                    gutter_pending_ranges,
1430                    |cx| cx.theme().status().info_background,
1431                    cx,
1432                )
1433            }
1434
1435            enum GutterTransformedRange {}
1436            if gutter_transformed_ranges.is_empty() {
1437                editor.clear_gutter_highlights::<GutterTransformedRange>(cx);
1438            } else {
1439                editor.highlight_gutter::<GutterTransformedRange>(
1440                    gutter_transformed_ranges,
1441                    |cx| cx.theme().status().info,
1442                    cx,
1443                )
1444            }
1445
1446            if foreground_ranges.is_empty() {
1447                editor.clear_highlights(HighlightKey::InlineAssist, cx);
1448            } else {
1449                editor.highlight_text(
1450                    HighlightKey::InlineAssist,
1451                    foreground_ranges,
1452                    HighlightStyle {
1453                        fade_out: Some(0.6),
1454                        ..Default::default()
1455                    },
1456                    cx,
1457                );
1458            }
1459
1460            editor.clear_row_highlights::<InlineAssist>();
1461            for row_range in inserted_row_ranges {
1462                editor.highlight_rows::<InlineAssist>(
1463                    row_range,
1464                    cx.theme().status().info_background,
1465                    Default::default(),
1466                    cx,
1467                );
1468            }
1469        });
1470    }
1471
1472    fn update_editor_blocks(
1473        &mut self,
1474        editor: &Entity<Editor>,
1475        assist_id: InlineAssistId,
1476        window: &mut Window,
1477        cx: &mut App,
1478    ) {
1479        let Some(assist) = self.assists.get_mut(&assist_id) else {
1480            return;
1481        };
1482        let Some(decorations) = assist.decorations.as_mut() else {
1483            return;
1484        };
1485
1486        let codegen = assist.codegen.read(cx);
1487        let old_snapshot = codegen.snapshot(cx);
1488        let old_buffer = codegen.old_buffer(cx);
1489        let deleted_row_ranges = codegen.diff(cx).deleted_row_ranges.clone();
1490
1491        editor.update(cx, |editor, cx| {
1492            let old_blocks = mem::take(&mut decorations.removed_line_block_ids);
1493            editor.remove_blocks(old_blocks, None, cx);
1494
1495            let mut new_blocks = Vec::new();
1496            for (new_row, old_row_range) in deleted_row_ranges {
1497                let (_, start, _) = old_snapshot
1498                    .point_to_buffer_point(Point::new(*old_row_range.start(), 0))
1499                    .unwrap();
1500                let (_, end, _) = old_snapshot
1501                    .point_to_buffer_point(Point::new(
1502                        *old_row_range.end(),
1503                        old_snapshot.line_len(MultiBufferRow(*old_row_range.end())),
1504                    ))
1505                    .unwrap();
1506
1507                let deleted_lines_editor = cx.new(|cx| {
1508                    let multi_buffer =
1509                        cx.new(|_| MultiBuffer::without_headers(language::Capability::ReadOnly));
1510                    multi_buffer.update(cx, |multi_buffer, cx| {
1511                        multi_buffer.set_excerpts_for_buffer(
1512                            old_buffer.clone(),
1513                            // todo(lw): start and end might come from different snapshots!
1514                            [start..end],
1515                            0,
1516                            cx,
1517                        );
1518                    });
1519
1520                    enum DeletedLines {}
1521                    let mut editor = Editor::for_multibuffer(multi_buffer, None, window, cx);
1522                    editor.disable_scrollbars_and_minimap(window, cx);
1523                    editor.set_soft_wrap_mode(language::language_settings::SoftWrap::None, cx);
1524                    editor.set_show_wrap_guides(false, cx);
1525                    editor.set_show_gutter(false, cx);
1526                    editor.set_offset_content(false, cx);
1527                    editor.scroll_manager.set_forbid_vertical_scroll(true);
1528                    editor.set_read_only(true);
1529                    editor.set_show_edit_predictions(Some(false), window, cx);
1530                    editor.highlight_rows::<DeletedLines>(
1531                        Anchor::min()..Anchor::max(),
1532                        cx.theme().status().deleted_background,
1533                        Default::default(),
1534                        cx,
1535                    );
1536                    editor
1537                });
1538
1539                let height =
1540                    deleted_lines_editor.update(cx, |editor, cx| editor.max_point(cx).row().0 + 1);
1541                new_blocks.push(BlockProperties {
1542                    placement: BlockPlacement::Above(new_row),
1543                    height: Some(height),
1544                    style: BlockStyle::Flex,
1545                    render: Arc::new(move |cx| {
1546                        div()
1547                            .block_mouse_except_scroll()
1548                            .bg(cx.theme().status().deleted_background)
1549                            .size_full()
1550                            .h(height as f32 * cx.window.line_height())
1551                            .pl(cx.margins.gutter.full_width())
1552                            .child(deleted_lines_editor.clone())
1553                            .into_any_element()
1554                    }),
1555                    priority: 0,
1556                });
1557            }
1558
1559            decorations.removed_line_block_ids = editor
1560                .insert_blocks(new_blocks, None, cx)
1561                .into_iter()
1562                .collect();
1563        })
1564    }
1565
1566    fn resolve_inline_assist_target(
1567        workspace: &mut Workspace,
1568        agent_panel: Option<Entity<AgentPanel>>,
1569        window: &mut Window,
1570        cx: &mut App,
1571    ) -> Option<InlineAssistTarget> {
1572        if let Some(terminal_panel) = workspace.panel::<TerminalPanel>(cx)
1573            && terminal_panel
1574                .read(cx)
1575                .focus_handle(cx)
1576                .contains_focused(window, cx)
1577            && let Some(terminal_view) = terminal_panel.read(cx).pane().and_then(|pane| {
1578                pane.read(cx)
1579                    .active_item()
1580                    .and_then(|t| t.downcast::<TerminalView>())
1581            })
1582        {
1583            return Some(InlineAssistTarget::Terminal(terminal_view));
1584        }
1585
1586        let text_thread_editor = agent_panel
1587            .and_then(|panel| panel.read(cx).active_text_thread_editor())
1588            .and_then(|editor| {
1589                let editor = &editor.read(cx).editor().clone();
1590                if editor.read(cx).is_focused(window) {
1591                    Some(editor.clone())
1592                } else {
1593                    None
1594                }
1595            });
1596
1597        if let Some(text_thread_editor) = text_thread_editor {
1598            Some(InlineAssistTarget::Editor(text_thread_editor))
1599        } else if let Some(workspace_editor) = workspace
1600            .active_item(cx)
1601            .and_then(|item| item.act_as::<Editor>(cx))
1602        {
1603            Some(InlineAssistTarget::Editor(workspace_editor))
1604        } else {
1605            workspace
1606                .active_item(cx)
1607                .and_then(|item| item.act_as::<TerminalView>(cx))
1608                .map(InlineAssistTarget::Terminal)
1609        }
1610    }
1611
1612    #[cfg(any(test, feature = "test-support"))]
1613    pub fn set_completion_receiver(
1614        &mut self,
1615        sender: mpsc::UnboundedSender<anyhow::Result<InlineAssistId>>,
1616    ) {
1617        self._inline_assistant_completions = Some(sender);
1618    }
1619
1620    #[cfg(any(test, feature = "test-support"))]
1621    pub fn get_codegen(
1622        &mut self,
1623        assist_id: InlineAssistId,
1624        cx: &mut App,
1625    ) -> Option<Entity<CodegenAlternative>> {
1626        self.assists.get(&assist_id).map(|inline_assist| {
1627            inline_assist
1628                .codegen
1629                .update(cx, |codegen, _cx| codegen.active_alternative().clone())
1630        })
1631    }
1632}
1633
1634struct EditorInlineAssists {
1635    assist_ids: Vec<InlineAssistId>,
1636    scroll_lock: Option<InlineAssistScrollLock>,
1637    highlight_updates: watch::Sender<()>,
1638    _update_highlights: Task<Result<()>>,
1639    _subscriptions: Vec<gpui::Subscription>,
1640}
1641
1642struct InlineAssistScrollLock {
1643    assist_id: InlineAssistId,
1644    distance_from_top: ScrollOffset,
1645}
1646
1647impl EditorInlineAssists {
1648    fn new(editor: &Entity<Editor>, window: &mut Window, cx: &mut App) -> Self {
1649        let (highlight_updates_tx, mut highlight_updates_rx) = watch::channel(());
1650        Self {
1651            assist_ids: Vec::new(),
1652            scroll_lock: None,
1653            highlight_updates: highlight_updates_tx,
1654            _update_highlights: cx.spawn({
1655                let editor = editor.downgrade();
1656                async move |cx| {
1657                    while let Ok(()) = highlight_updates_rx.changed().await {
1658                        let editor = editor.upgrade().context("editor was dropped")?;
1659                        cx.update_global(|assistant: &mut InlineAssistant, cx| {
1660                            assistant.update_editor_highlights(&editor, cx);
1661                        });
1662                    }
1663                    Ok(())
1664                }
1665            }),
1666            _subscriptions: vec![
1667                cx.observe_release_in(editor, window, {
1668                    let editor = editor.downgrade();
1669                    |_, window, cx| {
1670                        InlineAssistant::update_global(cx, |this, cx| {
1671                            this.handle_editor_release(editor, window, cx);
1672                        })
1673                    }
1674                }),
1675                window.observe(editor, cx, move |editor, window, cx| {
1676                    InlineAssistant::update_global(cx, |this, cx| {
1677                        this.handle_editor_change(editor, window, cx)
1678                    })
1679                }),
1680                window.subscribe(editor, cx, move |editor, event, window, cx| {
1681                    InlineAssistant::update_global(cx, |this, cx| {
1682                        this.handle_editor_event(editor, event, window, cx)
1683                    })
1684                }),
1685                editor.update(cx, |editor, cx| {
1686                    let editor_handle = cx.entity().downgrade();
1687                    editor.register_action(move |_: &editor::actions::Newline, window, cx| {
1688                        InlineAssistant::update_global(cx, |this, cx| {
1689                            if let Some(editor) = editor_handle.upgrade() {
1690                                this.handle_editor_newline(editor, window, cx)
1691                            }
1692                        })
1693                    })
1694                }),
1695                editor.update(cx, |editor, cx| {
1696                    let editor_handle = cx.entity().downgrade();
1697                    editor.register_action(move |_: &editor::actions::Cancel, window, cx| {
1698                        InlineAssistant::update_global(cx, |this, cx| {
1699                            if let Some(editor) = editor_handle.upgrade() {
1700                                this.handle_editor_cancel(editor, window, cx)
1701                            }
1702                        })
1703                    })
1704                }),
1705            ],
1706        }
1707    }
1708}
1709
1710struct InlineAssistGroup {
1711    assist_ids: Vec<InlineAssistId>,
1712    linked: bool,
1713    active_assist_id: Option<InlineAssistId>,
1714}
1715
1716impl InlineAssistGroup {
1717    fn new() -> Self {
1718        Self {
1719            assist_ids: Vec::new(),
1720            linked: true,
1721            active_assist_id: None,
1722        }
1723    }
1724}
1725
1726fn build_assist_editor_renderer(editor: &Entity<PromptEditor<BufferCodegen>>) -> RenderBlock {
1727    let editor = editor.clone();
1728
1729    Arc::new(move |cx: &mut BlockContext| {
1730        let editor_margins = editor.read(cx).editor_margins();
1731
1732        *editor_margins.lock() = *cx.margins;
1733        editor.clone().into_any_element()
1734    })
1735}
1736
1737#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, Hash)]
1738struct InlineAssistGroupId(usize);
1739
1740impl InlineAssistGroupId {
1741    fn post_inc(&mut self) -> InlineAssistGroupId {
1742        let id = *self;
1743        self.0 += 1;
1744        id
1745    }
1746}
1747
1748pub struct InlineAssist {
1749    group_id: InlineAssistGroupId,
1750    range: Range<Anchor>,
1751    editor: WeakEntity<Editor>,
1752    decorations: Option<InlineAssistDecorations>,
1753    codegen: Entity<BufferCodegen>,
1754    _subscriptions: Vec<Subscription>,
1755    workspace: WeakEntity<Workspace>,
1756}
1757
1758impl InlineAssist {
1759    fn new(
1760        assist_id: InlineAssistId,
1761        group_id: InlineAssistGroupId,
1762        editor: &Entity<Editor>,
1763        prompt_editor: &Entity<PromptEditor<BufferCodegen>>,
1764        prompt_block_id: CustomBlockId,
1765        tool_description_block_id: CustomBlockId,
1766        end_block_id: CustomBlockId,
1767        range: Range<Anchor>,
1768        codegen: Entity<BufferCodegen>,
1769        workspace: WeakEntity<Workspace>,
1770        window: &mut Window,
1771        cx: &mut App,
1772    ) -> Self {
1773        let prompt_editor_focus_handle = prompt_editor.focus_handle(cx);
1774        InlineAssist {
1775            group_id,
1776            editor: editor.downgrade(),
1777            decorations: Some(InlineAssistDecorations {
1778                prompt_block_id,
1779                prompt_editor: prompt_editor.clone(),
1780                removed_line_block_ids: Default::default(),
1781                model_explanation: Some(tool_description_block_id),
1782                end_block_id,
1783            }),
1784            range,
1785            codegen: codegen.clone(),
1786            workspace,
1787            _subscriptions: vec![
1788                window.on_focus_in(&prompt_editor_focus_handle, cx, move |_, cx| {
1789                    InlineAssistant::update_global(cx, |this, cx| {
1790                        this.handle_prompt_editor_focus_in(assist_id, cx)
1791                    })
1792                }),
1793                window.on_focus_out(&prompt_editor_focus_handle, cx, move |_, _, cx| {
1794                    InlineAssistant::update_global(cx, |this, cx| {
1795                        this.handle_prompt_editor_focus_out(assist_id, cx)
1796                    })
1797                }),
1798                window.subscribe(prompt_editor, cx, |prompt_editor, event, window, cx| {
1799                    InlineAssistant::update_global(cx, |this, cx| {
1800                        this.handle_prompt_editor_event(prompt_editor, event, window, cx)
1801                    })
1802                }),
1803                window.observe(&codegen, cx, {
1804                    let editor = editor.downgrade();
1805                    move |_, window, cx| {
1806                        if let Some(editor) = editor.upgrade() {
1807                            InlineAssistant::update_global(cx, |this, cx| {
1808                                if let Some(editor_assists) =
1809                                    this.assists_by_editor.get_mut(&editor.downgrade())
1810                                {
1811                                    editor_assists.highlight_updates.send(()).ok();
1812                                }
1813
1814                                this.update_editor_blocks(&editor, assist_id, window, cx);
1815                            })
1816                        }
1817                    }
1818                }),
1819                window.subscribe(&codegen, cx, move |codegen, event, window, cx| {
1820                    InlineAssistant::update_global(cx, |this, cx| match event {
1821                        CodegenEvent::Undone => this.finish_assist(assist_id, false, window, cx),
1822                        CodegenEvent::Finished => {
1823                            let assist = if let Some(assist) = this.assists.get(&assist_id) {
1824                                assist
1825                            } else {
1826                                return;
1827                            };
1828
1829                            if let CodegenStatus::Error(error) = codegen.read(cx).status(cx)
1830                                && assist.decorations.is_none()
1831                                && let Some(workspace) = assist.workspace.upgrade()
1832                            {
1833                                #[cfg(any(test, feature = "test-support"))]
1834                                if let Some(sender) = &mut this._inline_assistant_completions {
1835                                    sender
1836                                        .unbounded_send(Err(anyhow::anyhow!(
1837                                            "Inline assistant error: {}",
1838                                            error
1839                                        )))
1840                                        .ok();
1841                                }
1842
1843                                let error = format!("Inline assistant error: {}", error);
1844                                workspace.update(cx, |workspace, cx| {
1845                                    struct InlineAssistantError;
1846
1847                                    let id = NotificationId::composite::<InlineAssistantError>(
1848                                        assist_id.0,
1849                                    );
1850
1851                                    workspace.show_toast(Toast::new(id, error), cx);
1852                                })
1853                            } else {
1854                                #[cfg(any(test, feature = "test-support"))]
1855                                if let Some(sender) = &mut this._inline_assistant_completions {
1856                                    sender.unbounded_send(Ok(assist_id)).ok();
1857                                }
1858                            }
1859
1860                            if assist.decorations.is_none() {
1861                                this.finish_assist(assist_id, false, window, cx);
1862                            }
1863                        }
1864                    })
1865                }),
1866            ],
1867        }
1868    }
1869
1870    fn user_prompt(&self, cx: &App) -> Option<String> {
1871        let decorations = self.decorations.as_ref()?;
1872        Some(decorations.prompt_editor.read(cx).prompt(cx))
1873    }
1874
1875    fn mention_set(&self, cx: &App) -> Option<Entity<MentionSet>> {
1876        let decorations = self.decorations.as_ref()?;
1877        Some(decorations.prompt_editor.read(cx).mention_set().clone())
1878    }
1879}
1880
1881struct InlineAssistDecorations {
1882    prompt_block_id: CustomBlockId,
1883    prompt_editor: Entity<PromptEditor<BufferCodegen>>,
1884    removed_line_block_ids: HashSet<CustomBlockId>,
1885    model_explanation: Option<CustomBlockId>,
1886    end_block_id: CustomBlockId,
1887}
1888
1889struct AssistantCodeActionProvider {
1890    editor: WeakEntity<Editor>,
1891    workspace: WeakEntity<Workspace>,
1892}
1893
1894const ASSISTANT_CODE_ACTION_PROVIDER_ID: &str = "assistant";
1895
1896impl CodeActionProvider for AssistantCodeActionProvider {
1897    fn id(&self) -> Arc<str> {
1898        ASSISTANT_CODE_ACTION_PROVIDER_ID.into()
1899    }
1900
1901    fn code_actions(
1902        &self,
1903        buffer: &Entity<Buffer>,
1904        range: Range<text::Anchor>,
1905        _: &mut Window,
1906        cx: &mut App,
1907    ) -> Task<Result<Vec<CodeAction>>> {
1908        if !AgentSettings::get_global(cx).enabled(cx) {
1909            return Task::ready(Ok(Vec::new()));
1910        }
1911
1912        let snapshot = buffer.read(cx).snapshot();
1913        let mut range = range.to_point(&snapshot);
1914
1915        // Expand the range to line boundaries.
1916        range.start.column = 0;
1917        range.end.column = snapshot.line_len(range.end.row);
1918
1919        let mut has_diagnostics = false;
1920        for diagnostic in snapshot.diagnostics_in_range::<_, Point>(range.clone(), false) {
1921            range.start = cmp::min(range.start, diagnostic.range.start);
1922            range.end = cmp::max(range.end, diagnostic.range.end);
1923            has_diagnostics = true;
1924        }
1925        if has_diagnostics {
1926            let symbols_containing_start = snapshot.symbols_containing(range.start, None);
1927            if let Some(symbol) = symbols_containing_start.last() {
1928                range.start = cmp::min(range.start, symbol.range.start.to_point(&snapshot));
1929                range.end = cmp::max(range.end, symbol.range.end.to_point(&snapshot));
1930            }
1931            let symbols_containing_end = snapshot.symbols_containing(range.end, None);
1932            if let Some(symbol) = symbols_containing_end.last() {
1933                range.start = cmp::min(range.start, symbol.range.start.to_point(&snapshot));
1934                range.end = cmp::max(range.end, symbol.range.end.to_point(&snapshot));
1935            }
1936
1937            Task::ready(Ok(vec![CodeAction {
1938                server_id: language::LanguageServerId(0),
1939                range: snapshot.anchor_before(range.start)..snapshot.anchor_after(range.end),
1940                lsp_action: LspAction::Action(Box::new(lsp::CodeAction {
1941                    title: "Fix with Assistant".into(),
1942                    ..Default::default()
1943                })),
1944                resolved: true,
1945            }]))
1946        } else {
1947            Task::ready(Ok(Vec::new()))
1948        }
1949    }
1950
1951    fn apply_code_action(
1952        &self,
1953        buffer: Entity<Buffer>,
1954        action: CodeAction,
1955        excerpt_id: ExcerptId,
1956        _push_to_history: bool,
1957        window: &mut Window,
1958        cx: &mut App,
1959    ) -> Task<Result<ProjectTransaction>> {
1960        let editor = self.editor.clone();
1961        let workspace = self.workspace.clone();
1962        let prompt_store = PromptStore::global(cx);
1963        window.spawn(cx, async move |cx| {
1964            let workspace = workspace.upgrade().context("workspace was released")?;
1965            let (thread_store, history) = cx.update(|_window, cx| {
1966                let panel = workspace
1967                    .read(cx)
1968                    .panel::<AgentPanel>(cx)
1969                    .context("missing agent panel")?
1970                    .read(cx);
1971                anyhow::Ok((panel.thread_store().clone(), panel.history().downgrade()))
1972            })??;
1973            let editor = editor.upgrade().context("editor was released")?;
1974            let range = editor
1975                .update(cx, |editor, cx| {
1976                    editor.buffer().update(cx, |multibuffer, cx| {
1977                        let buffer = buffer.read(cx);
1978                        let multibuffer_snapshot = multibuffer.read(cx);
1979
1980                        let old_context_range =
1981                            multibuffer_snapshot.context_range_for_excerpt(excerpt_id)?;
1982                        let mut new_context_range = old_context_range.clone();
1983                        if action
1984                            .range
1985                            .start
1986                            .cmp(&old_context_range.start, buffer)
1987                            .is_lt()
1988                        {
1989                            new_context_range.start = action.range.start;
1990                        }
1991                        if action.range.end.cmp(&old_context_range.end, buffer).is_gt() {
1992                            new_context_range.end = action.range.end;
1993                        }
1994                        drop(multibuffer_snapshot);
1995
1996                        if new_context_range != old_context_range {
1997                            multibuffer.resize_excerpt(excerpt_id, new_context_range, cx);
1998                        }
1999
2000                        let multibuffer_snapshot = multibuffer.read(cx);
2001                        multibuffer_snapshot.anchor_range_in_buffer(excerpt_id, action.range)
2002                    })
2003                })
2004                .context("invalid range")?;
2005
2006            let prompt_store = prompt_store.await.ok();
2007            cx.update_global(|assistant: &mut InlineAssistant, window, cx| {
2008                let assist_id = assistant.suggest_assist(
2009                    &editor,
2010                    range,
2011                    "Fix Diagnostics".into(),
2012                    None,
2013                    true,
2014                    workspace,
2015                    thread_store,
2016                    prompt_store,
2017                    history,
2018                    window,
2019                    cx,
2020                );
2021                assistant.start_assist(assist_id, window, cx);
2022            })?;
2023
2024            Ok(ProjectTransaction::default())
2025        })
2026    }
2027}
2028
2029fn merge_ranges(ranges: &mut Vec<Range<Anchor>>, buffer: &MultiBufferSnapshot) {
2030    ranges.sort_unstable_by(|a, b| {
2031        a.start
2032            .cmp(&b.start, buffer)
2033            .then_with(|| b.end.cmp(&a.end, buffer))
2034    });
2035
2036    let mut ix = 0;
2037    while ix + 1 < ranges.len() {
2038        let b = ranges[ix + 1].clone();
2039        let a = &mut ranges[ix];
2040        if a.end.cmp(&b.start, buffer).is_gt() {
2041            if a.end.cmp(&b.end, buffer).is_lt() {
2042                a.end = b.end;
2043            }
2044            ranges.remove(ix + 1);
2045        } else {
2046            ix += 1;
2047        }
2048    }
2049}
2050
2051#[cfg(any(test, feature = "unit-eval"))]
2052#[cfg_attr(not(test), allow(dead_code))]
2053pub mod test {
2054
2055    use std::sync::Arc;
2056
2057    use agent::ThreadStore;
2058    use client::{Client, UserStore};
2059    use editor::{Editor, MultiBuffer, MultiBufferOffset};
2060    use fs::FakeFs;
2061    use futures::channel::mpsc;
2062    use gpui::{AppContext, TestAppContext, UpdateGlobal as _};
2063    use language::Buffer;
2064    use project::Project;
2065    use prompt_store::PromptBuilder;
2066    use smol::stream::StreamExt as _;
2067    use util::test::marked_text_ranges;
2068    use workspace::Workspace;
2069
2070    use crate::InlineAssistant;
2071
2072    #[derive(Debug)]
2073    pub enum InlineAssistantOutput {
2074        Success {
2075            completion: Option<String>,
2076            description: Option<String>,
2077            full_buffer_text: String,
2078        },
2079        Failure {
2080            failure: String,
2081        },
2082        // These fields are used for logging
2083        #[allow(unused)]
2084        Malformed {
2085            completion: Option<String>,
2086            description: Option<String>,
2087            failure: Option<String>,
2088        },
2089    }
2090
2091    pub fn run_inline_assistant_test<SetupF, TestF>(
2092        base_buffer: String,
2093        prompt: String,
2094        setup: SetupF,
2095        test: TestF,
2096        cx: &mut TestAppContext,
2097    ) -> InlineAssistantOutput
2098    where
2099        SetupF: FnOnce(&mut gpui::VisualTestContext),
2100        TestF: FnOnce(&mut gpui::VisualTestContext),
2101    {
2102        let fs = FakeFs::new(cx.executor());
2103        let app_state = cx.update(|cx| workspace::AppState::test(cx));
2104        let prompt_builder = Arc::new(PromptBuilder::new(None).unwrap());
2105        let http = Arc::new(reqwest_client::ReqwestClient::user_agent("agent tests").unwrap());
2106        let client = cx.update(|cx| {
2107            cx.set_http_client(http);
2108            Client::production(cx)
2109        });
2110        let mut inline_assistant = InlineAssistant::new(fs.clone(), prompt_builder);
2111
2112        let (tx, mut completion_rx) = mpsc::unbounded();
2113        inline_assistant.set_completion_receiver(tx);
2114
2115        // Initialize settings and client
2116        cx.update(|cx| {
2117            gpui_tokio::init(cx);
2118            settings::init(cx);
2119            client::init(&client, cx);
2120            workspace::init(app_state.clone(), cx);
2121            let user_store = cx.new(|cx| UserStore::new(client.clone(), cx));
2122            language_model::init(client.clone(), cx);
2123            language_models::init(user_store, client.clone(), cx);
2124
2125            cx.set_global(inline_assistant);
2126        });
2127
2128        let foreground_executor = cx.foreground_executor().clone();
2129        let project =
2130            foreground_executor.block_test(async { Project::test(fs.clone(), [], cx).await });
2131
2132        // Create workspace with window
2133        let (workspace, cx) = cx.add_window_view(|window, cx| {
2134            window.activate_window();
2135            Workspace::new(None, project.clone(), app_state.clone(), window, cx)
2136        });
2137
2138        setup(cx);
2139
2140        let (_editor, buffer, _history) = cx.update(|window, cx| {
2141            let buffer = cx.new(|cx| Buffer::local("", cx));
2142            let multibuffer = cx.new(|cx| MultiBuffer::singleton(buffer.clone(), cx));
2143            let editor = cx.new(|cx| Editor::for_multibuffer(multibuffer, None, window, cx));
2144            editor.update(cx, |editor, cx| {
2145                let (unmarked_text, selection_ranges) = marked_text_ranges(&base_buffer, true);
2146                editor.set_text(unmarked_text, window, cx);
2147                editor.change_selections(Default::default(), window, cx, |s| {
2148                    s.select_ranges(
2149                        selection_ranges.into_iter().map(|range| {
2150                            MultiBufferOffset(range.start)..MultiBufferOffset(range.end)
2151                        }),
2152                    )
2153                })
2154            });
2155
2156            let thread_store = cx.new(|cx| ThreadStore::new(cx));
2157            let history = cx.new(|cx| crate::ThreadHistory::new(None, window, cx));
2158
2159            // Add editor to workspace
2160            workspace.update(cx, |workspace, cx| {
2161                workspace.add_item_to_active_pane(Box::new(editor.clone()), None, true, window, cx);
2162            });
2163
2164            // Call assist method
2165            InlineAssistant::update_global(cx, |inline_assistant, cx| {
2166                let assist_id = inline_assistant
2167                    .assist(
2168                        &editor,
2169                        workspace.downgrade(),
2170                        project.downgrade(),
2171                        thread_store,
2172                        None,
2173                        history.downgrade(),
2174                        Some(prompt),
2175                        window,
2176                        cx,
2177                    )
2178                    .unwrap();
2179
2180                inline_assistant.start_assist(assist_id, window, cx);
2181            });
2182
2183            (editor, buffer, history)
2184        });
2185
2186        cx.run_until_parked();
2187
2188        test(cx);
2189
2190        let assist_id = foreground_executor
2191            .block_test(async { completion_rx.next().await })
2192            .unwrap()
2193            .unwrap();
2194
2195        let (completion, description, failure) = cx.update(|_, cx| {
2196            InlineAssistant::update_global(cx, |inline_assistant, cx| {
2197                let codegen = inline_assistant.get_codegen(assist_id, cx).unwrap();
2198
2199                let completion = codegen.read(cx).current_completion();
2200                let description = codegen.read(cx).current_description();
2201                let failure = codegen.read(cx).current_failure();
2202
2203                (completion, description, failure)
2204            })
2205        });
2206
2207        if failure.is_some() && (completion.is_some() || description.is_some()) {
2208            InlineAssistantOutput::Malformed {
2209                completion,
2210                description,
2211                failure,
2212            }
2213        } else if let Some(failure) = failure {
2214            InlineAssistantOutput::Failure { failure }
2215        } else {
2216            InlineAssistantOutput::Success {
2217                completion,
2218                description,
2219                full_buffer_text: buffer.read_with(cx, |buffer, _| buffer.text()),
2220            }
2221        }
2222    }
2223}
2224
2225#[cfg(any(test, feature = "unit-eval"))]
2226#[cfg_attr(not(test), allow(dead_code))]
2227pub mod evals {
2228    use std::str::FromStr;
2229
2230    use eval_utils::{EvalOutput, NoProcessor};
2231    use gpui::TestAppContext;
2232    use language_model::{LanguageModelRegistry, SelectedModel};
2233
2234    use crate::inline_assistant::test::{InlineAssistantOutput, run_inline_assistant_test};
2235
2236    #[test]
2237    #[cfg_attr(not(feature = "unit-eval"), ignore)]
2238    fn eval_single_cursor_edit() {
2239        run_eval(
2240            20,
2241            1.0,
2242            "Rename this variable to buffer_text".to_string(),
2243            indoc::indoc! {"
2244                struct EvalExampleStruct {
2245                    text: Strˇing,
2246                    prompt: String,
2247                }
2248            "}
2249            .to_string(),
2250            exact_buffer_match(indoc::indoc! {"
2251                struct EvalExampleStruct {
2252                    buffer_text: String,
2253                    prompt: String,
2254                }
2255            "}),
2256        );
2257    }
2258
2259    #[test]
2260    #[cfg_attr(not(feature = "unit-eval"), ignore)]
2261    fn eval_cant_do() {
2262        run_eval(
2263            20,
2264            0.95,
2265            "Rename the struct to EvalExampleStructNope",
2266            indoc::indoc! {"
2267                struct EvalExampleStruct {
2268                    text: Strˇing,
2269                    prompt: String,
2270                }
2271            "},
2272            uncertain_output,
2273        );
2274    }
2275
2276    #[test]
2277    #[cfg_attr(not(feature = "unit-eval"), ignore)]
2278    fn eval_unclear() {
2279        run_eval(
2280            20,
2281            0.95,
2282            "Make exactly the change I want you to make",
2283            indoc::indoc! {"
2284                struct EvalExampleStruct {
2285                    text: Strˇing,
2286                    prompt: String,
2287                }
2288            "},
2289            uncertain_output,
2290        );
2291    }
2292
2293    #[test]
2294    #[cfg_attr(not(feature = "unit-eval"), ignore)]
2295    fn eval_empty_buffer() {
2296        run_eval(
2297            20,
2298            1.0,
2299            "Write a Python hello, world program".to_string(),
2300            "ˇ".to_string(),
2301            |output| match output {
2302                InlineAssistantOutput::Success {
2303                    full_buffer_text, ..
2304                } => {
2305                    if full_buffer_text.is_empty() {
2306                        EvalOutput::failed("expected some output".to_string())
2307                    } else {
2308                        EvalOutput::passed(format!("Produced {full_buffer_text}"))
2309                    }
2310                }
2311                o @ InlineAssistantOutput::Failure { .. } => EvalOutput::failed(format!(
2312                    "Assistant output does not match expected output: {:?}",
2313                    o
2314                )),
2315                o @ InlineAssistantOutput::Malformed { .. } => EvalOutput::failed(format!(
2316                    "Assistant output does not match expected output: {:?}",
2317                    o
2318                )),
2319            },
2320        );
2321    }
2322
2323    fn run_eval(
2324        iterations: usize,
2325        expected_pass_ratio: f32,
2326        prompt: impl Into<String>,
2327        buffer: impl Into<String>,
2328        judge: impl Fn(InlineAssistantOutput) -> eval_utils::EvalOutput<()> + Send + Sync + 'static,
2329    ) {
2330        let buffer = buffer.into();
2331        let prompt = prompt.into();
2332
2333        eval_utils::eval(iterations, expected_pass_ratio, NoProcessor, move || {
2334            let dispatcher = gpui::TestDispatcher::new(rand::random());
2335            let mut cx = TestAppContext::build(dispatcher, None);
2336            cx.skip_drawing();
2337
2338            let output = run_inline_assistant_test(
2339                buffer.clone(),
2340                prompt.clone(),
2341                |cx| {
2342                    // Reconfigure to use a real model instead of the fake one
2343                    let model_name = std::env::var("ZED_AGENT_MODEL")
2344                        .unwrap_or("anthropic/claude-sonnet-4-latest".into());
2345
2346                    let selected_model = SelectedModel::from_str(&model_name)
2347                        .expect("Invalid model format. Use 'provider/model-id'");
2348
2349                    log::info!("Selected model: {selected_model:?}");
2350
2351                    cx.update(|_, cx| {
2352                        LanguageModelRegistry::global(cx).update(cx, |registry, cx| {
2353                            registry.select_inline_assistant_model(Some(&selected_model), cx);
2354                        });
2355                    });
2356                },
2357                |_cx| {
2358                    log::info!("Waiting for actual response from the LLM...");
2359                },
2360                &mut cx,
2361            );
2362
2363            cx.quit();
2364
2365            judge(output)
2366        });
2367    }
2368
2369    fn uncertain_output(output: InlineAssistantOutput) -> EvalOutput<()> {
2370        match &output {
2371            o @ InlineAssistantOutput::Success {
2372                completion,
2373                description,
2374                ..
2375            } => {
2376                if description.is_some() && completion.is_none() {
2377                    EvalOutput::passed(format!(
2378                        "Assistant produced no completion, but a description:\n{}",
2379                        description.as_ref().unwrap()
2380                    ))
2381                } else {
2382                    EvalOutput::failed(format!("Assistant produced a completion:\n{:?}", o))
2383                }
2384            }
2385            InlineAssistantOutput::Failure {
2386                failure: error_message,
2387            } => EvalOutput::passed(format!(
2388                "Assistant produced a failure message: {}",
2389                error_message
2390            )),
2391            o @ InlineAssistantOutput::Malformed { .. } => {
2392                EvalOutput::failed(format!("Assistant produced a malformed response:\n{:?}", o))
2393            }
2394        }
2395    }
2396
2397    fn exact_buffer_match(
2398        correct_output: impl Into<String>,
2399    ) -> impl Fn(InlineAssistantOutput) -> EvalOutput<()> {
2400        let correct_output = correct_output.into();
2401        move |output| match output {
2402            InlineAssistantOutput::Success {
2403                description,
2404                full_buffer_text,
2405                ..
2406            } => {
2407                if full_buffer_text == correct_output && description.is_none() {
2408                    EvalOutput::passed("Assistant output matches")
2409                } else if full_buffer_text == correct_output {
2410                    EvalOutput::failed(format!(
2411                        "Assistant output produced an unescessary description description:\n{:?}",
2412                        description
2413                    ))
2414                } else {
2415                    EvalOutput::failed(format!(
2416                        "Assistant output does not match expected output:\n{:?}\ndescription:\n{:?}",
2417                        full_buffer_text, description
2418                    ))
2419                }
2420            }
2421            o @ InlineAssistantOutput::Failure { .. } => EvalOutput::failed(format!(
2422                "Assistant output does not match expected output: {:?}",
2423                o
2424            )),
2425            o @ InlineAssistantOutput::Malformed { .. } => EvalOutput::failed(format!(
2426                "Assistant output does not match expected output: {:?}",
2427                o
2428            )),
2429        }
2430    }
2431}