inline_assistant.rs

   1use crate::{
   2    humanize_token_count, prompts::PromptBuilder, AssistantPanel, AssistantPanelEvent,
   3    CharOperation, LineDiff, LineOperation, ModelSelector, StreamingDiff,
   4};
   5use anyhow::{anyhow, Context as _, Result};
   6use client::{telemetry::Telemetry, ErrorExt};
   7use collections::{hash_map, HashMap, HashSet, VecDeque};
   8use editor::{
   9    actions::{MoveDown, MoveUp, SelectAll},
  10    display_map::{
  11        BlockContext, BlockDisposition, BlockProperties, BlockStyle, CustomBlockId, RenderBlock,
  12        ToDisplayPoint,
  13    },
  14    Anchor, AnchorRangeExt, Editor, EditorElement, EditorEvent, EditorMode, EditorStyle,
  15    ExcerptRange, GutterDimensions, MultiBuffer, MultiBufferSnapshot, ToOffset, ToPoint,
  16};
  17use feature_flags::{FeatureFlagAppExt as _, ZedPro};
  18use fs::Fs;
  19use futures::{
  20    channel::mpsc,
  21    future::{BoxFuture, LocalBoxFuture},
  22    stream::{self, BoxStream},
  23    SinkExt, Stream, StreamExt,
  24};
  25use gpui::{
  26    anchored, deferred, point, AppContext, ClickEvent, EventEmitter, FocusHandle, FocusableView,
  27    FontWeight, Global, HighlightStyle, Model, ModelContext, Subscription, Task, TextStyle,
  28    UpdateGlobal, View, ViewContext, WeakView, WindowContext,
  29};
  30use language::{Buffer, IndentKind, Point, TransactionId};
  31use language_model::{
  32    LanguageModelRegistry, LanguageModelRequest, LanguageModelRequestMessage, Role,
  33};
  34use multi_buffer::MultiBufferRow;
  35use parking_lot::Mutex;
  36use rope::Rope;
  37use settings::Settings;
  38use smol::future::FutureExt;
  39use std::{
  40    future::{self, Future},
  41    mem,
  42    ops::{Range, RangeInclusive},
  43    pin::Pin,
  44    sync::Arc,
  45    task::{self, Poll},
  46    time::{Duration, Instant},
  47};
  48use text::ToOffset as _;
  49use theme::ThemeSettings;
  50use ui::{prelude::*, CheckboxWithLabel, IconButtonShape, Popover, Tooltip};
  51use util::{RangeExt, ResultExt};
  52use workspace::{notifications::NotificationId, Toast, Workspace};
  53
  54pub fn init(
  55    fs: Arc<dyn Fs>,
  56    prompt_builder: Arc<PromptBuilder>,
  57    telemetry: Arc<Telemetry>,
  58    cx: &mut AppContext,
  59) {
  60    cx.set_global(InlineAssistant::new(fs, prompt_builder, telemetry));
  61    cx.observe_new_views(|_, cx| {
  62        let workspace = cx.view().clone();
  63        InlineAssistant::update_global(cx, |inline_assistant, cx| {
  64            inline_assistant.register_workspace(&workspace, cx)
  65        })
  66    })
  67    .detach();
  68}
  69
  70const PROMPT_HISTORY_MAX_LEN: usize = 20;
  71
  72pub struct InlineAssistant {
  73    next_assist_id: InlineAssistId,
  74    next_assist_group_id: InlineAssistGroupId,
  75    assists: HashMap<InlineAssistId, InlineAssist>,
  76    assists_by_editor: HashMap<WeakView<Editor>, EditorInlineAssists>,
  77    assist_groups: HashMap<InlineAssistGroupId, InlineAssistGroup>,
  78    assist_observations:
  79        HashMap<InlineAssistId, (async_watch::Sender<()>, async_watch::Receiver<()>)>,
  80    confirmed_assists: HashMap<InlineAssistId, Model<Codegen>>,
  81    prompt_history: VecDeque<String>,
  82    prompt_builder: Arc<PromptBuilder>,
  83    telemetry: Option<Arc<Telemetry>>,
  84    fs: Arc<dyn Fs>,
  85}
  86
  87impl Global for InlineAssistant {}
  88
  89impl InlineAssistant {
  90    pub fn new(
  91        fs: Arc<dyn Fs>,
  92        prompt_builder: Arc<PromptBuilder>,
  93        telemetry: Arc<Telemetry>,
  94    ) -> Self {
  95        Self {
  96            next_assist_id: InlineAssistId::default(),
  97            next_assist_group_id: InlineAssistGroupId::default(),
  98            assists: HashMap::default(),
  99            assists_by_editor: HashMap::default(),
 100            assist_groups: HashMap::default(),
 101            assist_observations: HashMap::default(),
 102            confirmed_assists: HashMap::default(),
 103            prompt_history: VecDeque::default(),
 104            prompt_builder,
 105            telemetry: Some(telemetry),
 106            fs,
 107        }
 108    }
 109
 110    pub fn register_workspace(&mut self, workspace: &View<Workspace>, cx: &mut WindowContext) {
 111        cx.subscribe(workspace, |_, event, cx| {
 112            Self::update_global(cx, |this, cx| this.handle_workspace_event(event, cx));
 113        })
 114        .detach();
 115    }
 116
 117    fn handle_workspace_event(&mut self, event: &workspace::Event, cx: &mut WindowContext) {
 118        // When the user manually saves an editor, automatically accepts all finished transformations.
 119        if let workspace::Event::UserSavedItem { item, .. } = event {
 120            if let Some(editor) = item.upgrade().and_then(|item| item.act_as::<Editor>(cx)) {
 121                if let Some(editor_assists) = self.assists_by_editor.get(&editor.downgrade()) {
 122                    for assist_id in editor_assists.assist_ids.clone() {
 123                        let assist = &self.assists[&assist_id];
 124                        if let CodegenStatus::Done = &assist.codegen.read(cx).status {
 125                            self.finish_assist(assist_id, false, cx)
 126                        }
 127                    }
 128                }
 129            }
 130        }
 131    }
 132
 133    pub fn assist(
 134        &mut self,
 135        editor: &View<Editor>,
 136        workspace: Option<WeakView<Workspace>>,
 137        assistant_panel: Option<&View<AssistantPanel>>,
 138        initial_prompt: Option<String>,
 139        cx: &mut WindowContext,
 140    ) {
 141        let snapshot = editor.read(cx).buffer().read(cx).snapshot(cx);
 142
 143        struct CodegenRange {
 144            transform_range: Range<Point>,
 145            selection_ranges: Vec<Range<Point>>,
 146            focus_assist: bool,
 147        }
 148
 149        let newest_selection = editor.read(cx).selections.newest::<Point>(cx);
 150        let mut codegen_ranges: Vec<CodegenRange> = Vec::new();
 151        for selection in editor.read(cx).selections.all::<Point>(cx) {
 152            let selection_is_newest = selection.id == newest_selection.id;
 153            let mut transform_range = selection.start..selection.end;
 154
 155            // Expand the transform range to start/end of lines.
 156            // If a non-empty selection ends at the start of the last line, clip at the end of the penultimate line.
 157            transform_range.start.column = 0;
 158            if transform_range.end.column == 0 && transform_range.end > transform_range.start {
 159                transform_range.end.row -= 1;
 160            }
 161            transform_range.end.column = snapshot.line_len(MultiBufferRow(transform_range.end.row));
 162            let selection_range = selection.start..selection.end.min(transform_range.end);
 163
 164            // If we intersect the previous transform range,
 165            if let Some(CodegenRange {
 166                transform_range: prev_transform_range,
 167                selection_ranges,
 168                focus_assist,
 169            }) = codegen_ranges.last_mut()
 170            {
 171                if transform_range.start <= prev_transform_range.end {
 172                    prev_transform_range.end = transform_range.end;
 173                    selection_ranges.push(selection_range);
 174                    *focus_assist |= selection_is_newest;
 175                    continue;
 176                }
 177            }
 178
 179            codegen_ranges.push(CodegenRange {
 180                transform_range,
 181                selection_ranges: vec![selection_range],
 182                focus_assist: selection_is_newest,
 183            })
 184        }
 185
 186        let assist_group_id = self.next_assist_group_id.post_inc();
 187        let prompt_buffer =
 188            cx.new_model(|cx| Buffer::local(initial_prompt.unwrap_or_default(), cx));
 189        let prompt_buffer = cx.new_model(|cx| MultiBuffer::singleton(prompt_buffer, cx));
 190        let mut assists = Vec::new();
 191        let mut assist_to_focus = None;
 192
 193        for CodegenRange {
 194            transform_range,
 195            selection_ranges,
 196            focus_assist,
 197        } in codegen_ranges
 198        {
 199            let transform_range = snapshot.anchor_before(transform_range.start)
 200                ..snapshot.anchor_after(transform_range.end);
 201            let selection_ranges = selection_ranges
 202                .iter()
 203                .map(|range| snapshot.anchor_before(range.start)..snapshot.anchor_after(range.end))
 204                .collect::<Vec<_>>();
 205
 206            let codegen = cx.new_model(|cx| {
 207                Codegen::new(
 208                    editor.read(cx).buffer().clone(),
 209                    transform_range.clone(),
 210                    selection_ranges,
 211                    None,
 212                    self.telemetry.clone(),
 213                    self.prompt_builder.clone(),
 214                    cx,
 215                )
 216            });
 217
 218            let assist_id = self.next_assist_id.post_inc();
 219            let gutter_dimensions = Arc::new(Mutex::new(GutterDimensions::default()));
 220            let prompt_editor = cx.new_view(|cx| {
 221                PromptEditor::new(
 222                    assist_id,
 223                    gutter_dimensions.clone(),
 224                    self.prompt_history.clone(),
 225                    prompt_buffer.clone(),
 226                    codegen.clone(),
 227                    editor,
 228                    assistant_panel,
 229                    workspace.clone(),
 230                    self.fs.clone(),
 231                    cx,
 232                )
 233            });
 234
 235            if focus_assist {
 236                assist_to_focus = Some(assist_id);
 237            }
 238
 239            let [prompt_block_id, end_block_id] =
 240                self.insert_assist_blocks(editor, &transform_range, &prompt_editor, cx);
 241
 242            assists.push((
 243                assist_id,
 244                transform_range,
 245                prompt_editor,
 246                prompt_block_id,
 247                end_block_id,
 248            ));
 249        }
 250
 251        let editor_assists = self
 252            .assists_by_editor
 253            .entry(editor.downgrade())
 254            .or_insert_with(|| EditorInlineAssists::new(&editor, cx));
 255        let mut assist_group = InlineAssistGroup::new();
 256        for (assist_id, range, prompt_editor, prompt_block_id, end_block_id) in assists {
 257            self.assists.insert(
 258                assist_id,
 259                InlineAssist::new(
 260                    assist_id,
 261                    assist_group_id,
 262                    assistant_panel.is_some(),
 263                    editor,
 264                    &prompt_editor,
 265                    prompt_block_id,
 266                    end_block_id,
 267                    range,
 268                    prompt_editor.read(cx).codegen.clone(),
 269                    workspace.clone(),
 270                    cx,
 271                ),
 272            );
 273            assist_group.assist_ids.push(assist_id);
 274            editor_assists.assist_ids.push(assist_id);
 275        }
 276        self.assist_groups.insert(assist_group_id, assist_group);
 277
 278        if let Some(assist_id) = assist_to_focus {
 279            self.focus_assist(assist_id, cx);
 280        }
 281    }
 282
 283    #[allow(clippy::too_many_arguments)]
 284    pub fn suggest_assist(
 285        &mut self,
 286        editor: &View<Editor>,
 287        mut range: Range<Anchor>,
 288        initial_prompt: String,
 289        initial_transaction_id: Option<TransactionId>,
 290        workspace: Option<WeakView<Workspace>>,
 291        assistant_panel: Option<&View<AssistantPanel>>,
 292        cx: &mut WindowContext,
 293    ) -> InlineAssistId {
 294        let assist_group_id = self.next_assist_group_id.post_inc();
 295        let prompt_buffer = cx.new_model(|cx| Buffer::local(&initial_prompt, cx));
 296        let prompt_buffer = cx.new_model(|cx| MultiBuffer::singleton(prompt_buffer, cx));
 297
 298        let assist_id = self.next_assist_id.post_inc();
 299
 300        let buffer = editor.read(cx).buffer().clone();
 301        {
 302            let snapshot = buffer.read(cx).read(cx);
 303            range.start = range.start.bias_left(&snapshot);
 304            range.end = range.end.bias_right(&snapshot);
 305        }
 306
 307        let codegen = cx.new_model(|cx| {
 308            Codegen::new(
 309                editor.read(cx).buffer().clone(),
 310                range.clone(),
 311                vec![range.clone()],
 312                initial_transaction_id,
 313                self.telemetry.clone(),
 314                self.prompt_builder.clone(),
 315                cx,
 316            )
 317        });
 318
 319        let gutter_dimensions = Arc::new(Mutex::new(GutterDimensions::default()));
 320        let prompt_editor = cx.new_view(|cx| {
 321            PromptEditor::new(
 322                assist_id,
 323                gutter_dimensions.clone(),
 324                self.prompt_history.clone(),
 325                prompt_buffer.clone(),
 326                codegen.clone(),
 327                editor,
 328                assistant_panel,
 329                workspace.clone(),
 330                self.fs.clone(),
 331                cx,
 332            )
 333        });
 334
 335        let [prompt_block_id, end_block_id] =
 336            self.insert_assist_blocks(editor, &range, &prompt_editor, cx);
 337
 338        let editor_assists = self
 339            .assists_by_editor
 340            .entry(editor.downgrade())
 341            .or_insert_with(|| EditorInlineAssists::new(&editor, cx));
 342
 343        let mut assist_group = InlineAssistGroup::new();
 344        self.assists.insert(
 345            assist_id,
 346            InlineAssist::new(
 347                assist_id,
 348                assist_group_id,
 349                assistant_panel.is_some(),
 350                editor,
 351                &prompt_editor,
 352                prompt_block_id,
 353                end_block_id,
 354                range,
 355                prompt_editor.read(cx).codegen.clone(),
 356                workspace.clone(),
 357                cx,
 358            ),
 359        );
 360        assist_group.assist_ids.push(assist_id);
 361        editor_assists.assist_ids.push(assist_id);
 362        self.assist_groups.insert(assist_group_id, assist_group);
 363        assist_id
 364    }
 365
 366    fn insert_assist_blocks(
 367        &self,
 368        editor: &View<Editor>,
 369        range: &Range<Anchor>,
 370        prompt_editor: &View<PromptEditor>,
 371        cx: &mut WindowContext,
 372    ) -> [CustomBlockId; 2] {
 373        let prompt_editor_height = prompt_editor.update(cx, |prompt_editor, cx| {
 374            prompt_editor
 375                .editor
 376                .update(cx, |editor, cx| editor.max_point(cx).row().0 + 1 + 2)
 377        });
 378        let assist_blocks = vec![
 379            BlockProperties {
 380                style: BlockStyle::Sticky,
 381                position: range.start,
 382                height: prompt_editor_height,
 383                render: build_assist_editor_renderer(prompt_editor),
 384                disposition: BlockDisposition::Above,
 385                priority: 0,
 386            },
 387            BlockProperties {
 388                style: BlockStyle::Sticky,
 389                position: range.end,
 390                height: 0,
 391                render: Box::new(|cx| {
 392                    v_flex()
 393                        .h_full()
 394                        .w_full()
 395                        .border_t_1()
 396                        .border_color(cx.theme().status().info_border)
 397                        .into_any_element()
 398                }),
 399                disposition: BlockDisposition::Below,
 400                priority: 0,
 401            },
 402        ];
 403
 404        editor.update(cx, |editor, cx| {
 405            let block_ids = editor.insert_blocks(assist_blocks, None, cx);
 406            [block_ids[0], block_ids[1]]
 407        })
 408    }
 409
 410    fn handle_prompt_editor_focus_in(&mut self, assist_id: InlineAssistId, cx: &mut WindowContext) {
 411        let assist = &self.assists[&assist_id];
 412        let Some(decorations) = assist.decorations.as_ref() else {
 413            return;
 414        };
 415        let assist_group = self.assist_groups.get_mut(&assist.group_id).unwrap();
 416        let editor_assists = self.assists_by_editor.get_mut(&assist.editor).unwrap();
 417
 418        assist_group.active_assist_id = Some(assist_id);
 419        if assist_group.linked {
 420            for assist_id in &assist_group.assist_ids {
 421                if let Some(decorations) = self.assists[assist_id].decorations.as_ref() {
 422                    decorations.prompt_editor.update(cx, |prompt_editor, cx| {
 423                        prompt_editor.set_show_cursor_when_unfocused(true, cx)
 424                    });
 425                }
 426            }
 427        }
 428
 429        assist
 430            .editor
 431            .update(cx, |editor, cx| {
 432                let scroll_top = editor.scroll_position(cx).y;
 433                let scroll_bottom = scroll_top + editor.visible_line_count().unwrap_or(0.);
 434                let prompt_row = editor
 435                    .row_for_block(decorations.prompt_block_id, cx)
 436                    .unwrap()
 437                    .0 as f32;
 438
 439                if (scroll_top..scroll_bottom).contains(&prompt_row) {
 440                    editor_assists.scroll_lock = Some(InlineAssistScrollLock {
 441                        assist_id,
 442                        distance_from_top: prompt_row - scroll_top,
 443                    });
 444                } else {
 445                    editor_assists.scroll_lock = None;
 446                }
 447            })
 448            .ok();
 449    }
 450
 451    fn handle_prompt_editor_focus_out(
 452        &mut self,
 453        assist_id: InlineAssistId,
 454        cx: &mut WindowContext,
 455    ) {
 456        let assist = &self.assists[&assist_id];
 457        let assist_group = self.assist_groups.get_mut(&assist.group_id).unwrap();
 458        if assist_group.active_assist_id == Some(assist_id) {
 459            assist_group.active_assist_id = None;
 460            if assist_group.linked {
 461                for assist_id in &assist_group.assist_ids {
 462                    if let Some(decorations) = self.assists[assist_id].decorations.as_ref() {
 463                        decorations.prompt_editor.update(cx, |prompt_editor, cx| {
 464                            prompt_editor.set_show_cursor_when_unfocused(false, cx)
 465                        });
 466                    }
 467                }
 468            }
 469        }
 470    }
 471
 472    fn handle_prompt_editor_event(
 473        &mut self,
 474        prompt_editor: View<PromptEditor>,
 475        event: &PromptEditorEvent,
 476        cx: &mut WindowContext,
 477    ) {
 478        let assist_id = prompt_editor.read(cx).id;
 479        match event {
 480            PromptEditorEvent::StartRequested => {
 481                self.start_assist(assist_id, cx);
 482            }
 483            PromptEditorEvent::StopRequested => {
 484                self.stop_assist(assist_id, cx);
 485            }
 486            PromptEditorEvent::ConfirmRequested => {
 487                self.finish_assist(assist_id, false, cx);
 488            }
 489            PromptEditorEvent::CancelRequested => {
 490                self.finish_assist(assist_id, true, cx);
 491            }
 492            PromptEditorEvent::DismissRequested => {
 493                self.dismiss_assist(assist_id, cx);
 494            }
 495        }
 496    }
 497
 498    fn handle_editor_newline(&mut self, editor: View<Editor>, cx: &mut WindowContext) {
 499        let Some(editor_assists) = self.assists_by_editor.get(&editor.downgrade()) else {
 500            return;
 501        };
 502
 503        let editor = editor.read(cx);
 504        if editor.selections.count() == 1 {
 505            let selection = editor.selections.newest::<usize>(cx);
 506            let buffer = editor.buffer().read(cx).snapshot(cx);
 507            for assist_id in &editor_assists.assist_ids {
 508                let assist = &self.assists[assist_id];
 509                let assist_range = assist.range.to_offset(&buffer);
 510                if assist_range.contains(&selection.start) && assist_range.contains(&selection.end)
 511                {
 512                    if matches!(assist.codegen.read(cx).status, CodegenStatus::Pending) {
 513                        self.dismiss_assist(*assist_id, cx);
 514                    } else {
 515                        self.finish_assist(*assist_id, false, cx);
 516                    }
 517
 518                    return;
 519                }
 520            }
 521        }
 522
 523        cx.propagate();
 524    }
 525
 526    fn handle_editor_cancel(&mut self, editor: View<Editor>, cx: &mut WindowContext) {
 527        let Some(editor_assists) = self.assists_by_editor.get(&editor.downgrade()) else {
 528            return;
 529        };
 530
 531        let editor = editor.read(cx);
 532        if editor.selections.count() == 1 {
 533            let selection = editor.selections.newest::<usize>(cx);
 534            let buffer = editor.buffer().read(cx).snapshot(cx);
 535            for assist_id in &editor_assists.assist_ids {
 536                let assist = &self.assists[assist_id];
 537                let assist_range = assist.range.to_offset(&buffer);
 538                if assist.decorations.is_some()
 539                    && assist_range.contains(&selection.start)
 540                    && assist_range.contains(&selection.end)
 541                {
 542                    self.focus_assist(*assist_id, cx);
 543                    return;
 544                }
 545            }
 546        }
 547
 548        cx.propagate();
 549    }
 550
 551    fn handle_editor_release(&mut self, editor: WeakView<Editor>, cx: &mut WindowContext) {
 552        if let Some(editor_assists) = self.assists_by_editor.get_mut(&editor) {
 553            for assist_id in editor_assists.assist_ids.clone() {
 554                self.finish_assist(assist_id, true, cx);
 555            }
 556        }
 557    }
 558
 559    fn handle_editor_change(&mut self, editor: View<Editor>, cx: &mut WindowContext) {
 560        let Some(editor_assists) = self.assists_by_editor.get(&editor.downgrade()) else {
 561            return;
 562        };
 563        let Some(scroll_lock) = editor_assists.scroll_lock.as_ref() else {
 564            return;
 565        };
 566        let assist = &self.assists[&scroll_lock.assist_id];
 567        let Some(decorations) = assist.decorations.as_ref() else {
 568            return;
 569        };
 570
 571        editor.update(cx, |editor, cx| {
 572            let scroll_position = editor.scroll_position(cx);
 573            let target_scroll_top = editor
 574                .row_for_block(decorations.prompt_block_id, cx)
 575                .unwrap()
 576                .0 as f32
 577                - scroll_lock.distance_from_top;
 578            if target_scroll_top != scroll_position.y {
 579                editor.set_scroll_position(point(scroll_position.x, target_scroll_top), cx);
 580            }
 581        });
 582    }
 583
 584    fn handle_editor_event(
 585        &mut self,
 586        editor: View<Editor>,
 587        event: &EditorEvent,
 588        cx: &mut WindowContext,
 589    ) {
 590        let Some(editor_assists) = self.assists_by_editor.get_mut(&editor.downgrade()) else {
 591            return;
 592        };
 593
 594        match event {
 595            EditorEvent::Edited { transaction_id } => {
 596                let buffer = editor.read(cx).buffer().read(cx);
 597                let edited_ranges =
 598                    buffer.edited_ranges_for_transaction::<usize>(*transaction_id, cx);
 599                let snapshot = buffer.snapshot(cx);
 600
 601                for assist_id in editor_assists.assist_ids.clone() {
 602                    let assist = &self.assists[&assist_id];
 603                    if matches!(
 604                        assist.codegen.read(cx).status,
 605                        CodegenStatus::Error(_) | CodegenStatus::Done
 606                    ) {
 607                        let assist_range = assist.range.to_offset(&snapshot);
 608                        if edited_ranges
 609                            .iter()
 610                            .any(|range| range.overlaps(&assist_range))
 611                        {
 612                            self.finish_assist(assist_id, false, cx);
 613                        }
 614                    }
 615                }
 616            }
 617            EditorEvent::ScrollPositionChanged { .. } => {
 618                if let Some(scroll_lock) = editor_assists.scroll_lock.as_ref() {
 619                    let assist = &self.assists[&scroll_lock.assist_id];
 620                    if let Some(decorations) = assist.decorations.as_ref() {
 621                        let distance_from_top = editor.update(cx, |editor, cx| {
 622                            let scroll_top = editor.scroll_position(cx).y;
 623                            let prompt_row = editor
 624                                .row_for_block(decorations.prompt_block_id, cx)
 625                                .unwrap()
 626                                .0 as f32;
 627                            prompt_row - scroll_top
 628                        });
 629
 630                        if distance_from_top != scroll_lock.distance_from_top {
 631                            editor_assists.scroll_lock = None;
 632                        }
 633                    }
 634                }
 635            }
 636            EditorEvent::SelectionsChanged { .. } => {
 637                for assist_id in editor_assists.assist_ids.clone() {
 638                    let assist = &self.assists[&assist_id];
 639                    if let Some(decorations) = assist.decorations.as_ref() {
 640                        if decorations.prompt_editor.focus_handle(cx).is_focused(cx) {
 641                            return;
 642                        }
 643                    }
 644                }
 645
 646                editor_assists.scroll_lock = None;
 647            }
 648            _ => {}
 649        }
 650    }
 651
 652    pub fn finish_assist(&mut self, assist_id: InlineAssistId, undo: bool, cx: &mut WindowContext) {
 653        if let Some(assist) = self.assists.get(&assist_id) {
 654            let assist_group_id = assist.group_id;
 655            if self.assist_groups[&assist_group_id].linked {
 656                for assist_id in self.unlink_assist_group(assist_group_id, cx) {
 657                    self.finish_assist(assist_id, undo, cx);
 658                }
 659                return;
 660            }
 661        }
 662
 663        self.dismiss_assist(assist_id, cx);
 664
 665        if let Some(assist) = self.assists.remove(&assist_id) {
 666            if let hash_map::Entry::Occupied(mut entry) = self.assist_groups.entry(assist.group_id)
 667            {
 668                entry.get_mut().assist_ids.retain(|id| *id != assist_id);
 669                if entry.get().assist_ids.is_empty() {
 670                    entry.remove();
 671                }
 672            }
 673
 674            if let hash_map::Entry::Occupied(mut entry) =
 675                self.assists_by_editor.entry(assist.editor.clone())
 676            {
 677                entry.get_mut().assist_ids.retain(|id| *id != assist_id);
 678                if entry.get().assist_ids.is_empty() {
 679                    entry.remove();
 680                    if let Some(editor) = assist.editor.upgrade() {
 681                        self.update_editor_highlights(&editor, cx);
 682                    }
 683                } else {
 684                    entry.get().highlight_updates.send(()).ok();
 685                }
 686            }
 687
 688            if undo {
 689                assist.codegen.update(cx, |codegen, cx| codegen.undo(cx));
 690            } else {
 691                self.confirmed_assists.insert(assist_id, assist.codegen);
 692            }
 693        }
 694
 695        // Remove the assist from the status updates map
 696        self.assist_observations.remove(&assist_id);
 697    }
 698
 699    pub fn undo_assist(&mut self, assist_id: InlineAssistId, cx: &mut WindowContext) -> bool {
 700        let Some(codegen) = self.confirmed_assists.remove(&assist_id) else {
 701            return false;
 702        };
 703        codegen.update(cx, |this, cx| this.undo(cx));
 704        true
 705    }
 706
 707    fn dismiss_assist(&mut self, assist_id: InlineAssistId, cx: &mut WindowContext) -> bool {
 708        let Some(assist) = self.assists.get_mut(&assist_id) else {
 709            return false;
 710        };
 711        let Some(editor) = assist.editor.upgrade() else {
 712            return false;
 713        };
 714        let Some(decorations) = assist.decorations.take() else {
 715            return false;
 716        };
 717
 718        editor.update(cx, |editor, cx| {
 719            let mut to_remove = decorations.removed_line_block_ids;
 720            to_remove.insert(decorations.prompt_block_id);
 721            to_remove.insert(decorations.end_block_id);
 722            editor.remove_blocks(to_remove, None, cx);
 723        });
 724
 725        if decorations
 726            .prompt_editor
 727            .focus_handle(cx)
 728            .contains_focused(cx)
 729        {
 730            self.focus_next_assist(assist_id, cx);
 731        }
 732
 733        if let Some(editor_assists) = self.assists_by_editor.get_mut(&editor.downgrade()) {
 734            if editor_assists
 735                .scroll_lock
 736                .as_ref()
 737                .map_or(false, |lock| lock.assist_id == assist_id)
 738            {
 739                editor_assists.scroll_lock = None;
 740            }
 741            editor_assists.highlight_updates.send(()).ok();
 742        }
 743
 744        true
 745    }
 746
 747    fn focus_next_assist(&mut self, assist_id: InlineAssistId, cx: &mut WindowContext) {
 748        let Some(assist) = self.assists.get(&assist_id) else {
 749            return;
 750        };
 751
 752        let assist_group = &self.assist_groups[&assist.group_id];
 753        let assist_ix = assist_group
 754            .assist_ids
 755            .iter()
 756            .position(|id| *id == assist_id)
 757            .unwrap();
 758        let assist_ids = assist_group
 759            .assist_ids
 760            .iter()
 761            .skip(assist_ix + 1)
 762            .chain(assist_group.assist_ids.iter().take(assist_ix));
 763
 764        for assist_id in assist_ids {
 765            let assist = &self.assists[assist_id];
 766            if assist.decorations.is_some() {
 767                self.focus_assist(*assist_id, cx);
 768                return;
 769            }
 770        }
 771
 772        assist.editor.update(cx, |editor, cx| editor.focus(cx)).ok();
 773    }
 774
 775    fn focus_assist(&mut self, assist_id: InlineAssistId, cx: &mut WindowContext) {
 776        let Some(assist) = self.assists.get(&assist_id) else {
 777            return;
 778        };
 779
 780        if let Some(decorations) = assist.decorations.as_ref() {
 781            decorations.prompt_editor.update(cx, |prompt_editor, cx| {
 782                prompt_editor.editor.update(cx, |editor, cx| {
 783                    editor.focus(cx);
 784                    editor.select_all(&SelectAll, cx);
 785                })
 786            });
 787        }
 788
 789        self.scroll_to_assist(assist_id, cx);
 790    }
 791
 792    pub fn scroll_to_assist(&mut self, assist_id: InlineAssistId, cx: &mut WindowContext) {
 793        let Some(assist) = self.assists.get(&assist_id) else {
 794            return;
 795        };
 796        let Some(editor) = assist.editor.upgrade() else {
 797            return;
 798        };
 799
 800        let position = assist.range.start;
 801        editor.update(cx, |editor, cx| {
 802            editor.change_selections(None, cx, |selections| {
 803                selections.select_anchor_ranges([position..position])
 804            });
 805
 806            let mut scroll_target_top;
 807            let mut scroll_target_bottom;
 808            if let Some(decorations) = assist.decorations.as_ref() {
 809                scroll_target_top = editor
 810                    .row_for_block(decorations.prompt_block_id, cx)
 811                    .unwrap()
 812                    .0 as f32;
 813                scroll_target_bottom = editor
 814                    .row_for_block(decorations.end_block_id, cx)
 815                    .unwrap()
 816                    .0 as f32;
 817            } else {
 818                let snapshot = editor.snapshot(cx);
 819                let start_row = assist
 820                    .range
 821                    .start
 822                    .to_display_point(&snapshot.display_snapshot)
 823                    .row();
 824                scroll_target_top = start_row.0 as f32;
 825                scroll_target_bottom = scroll_target_top + 1.;
 826            }
 827            scroll_target_top -= editor.vertical_scroll_margin() as f32;
 828            scroll_target_bottom += editor.vertical_scroll_margin() as f32;
 829
 830            let height_in_lines = editor.visible_line_count().unwrap_or(0.);
 831            let scroll_top = editor.scroll_position(cx).y;
 832            let scroll_bottom = scroll_top + height_in_lines;
 833
 834            if scroll_target_top < scroll_top {
 835                editor.set_scroll_position(point(0., scroll_target_top), cx);
 836            } else if scroll_target_bottom > scroll_bottom {
 837                if (scroll_target_bottom - scroll_target_top) <= height_in_lines {
 838                    editor
 839                        .set_scroll_position(point(0., scroll_target_bottom - height_in_lines), cx);
 840                } else {
 841                    editor.set_scroll_position(point(0., scroll_target_top), cx);
 842                }
 843            }
 844        });
 845    }
 846
 847    fn unlink_assist_group(
 848        &mut self,
 849        assist_group_id: InlineAssistGroupId,
 850        cx: &mut WindowContext,
 851    ) -> Vec<InlineAssistId> {
 852        let assist_group = self.assist_groups.get_mut(&assist_group_id).unwrap();
 853        assist_group.linked = false;
 854        for assist_id in &assist_group.assist_ids {
 855            let assist = self.assists.get_mut(assist_id).unwrap();
 856            if let Some(editor_decorations) = assist.decorations.as_ref() {
 857                editor_decorations
 858                    .prompt_editor
 859                    .update(cx, |prompt_editor, cx| prompt_editor.unlink(cx));
 860            }
 861        }
 862        assist_group.assist_ids.clone()
 863    }
 864
 865    pub fn start_assist(&mut self, assist_id: InlineAssistId, cx: &mut WindowContext) {
 866        let assist = if let Some(assist) = self.assists.get_mut(&assist_id) {
 867            assist
 868        } else {
 869            return;
 870        };
 871
 872        let assist_group_id = assist.group_id;
 873        if self.assist_groups[&assist_group_id].linked {
 874            for assist_id in self.unlink_assist_group(assist_group_id, cx) {
 875                self.start_assist(assist_id, cx);
 876            }
 877            return;
 878        }
 879
 880        let Some(user_prompt) = assist.user_prompt(cx) else {
 881            return;
 882        };
 883
 884        self.prompt_history.retain(|prompt| *prompt != user_prompt);
 885        self.prompt_history.push_back(user_prompt.clone());
 886        if self.prompt_history.len() > PROMPT_HISTORY_MAX_LEN {
 887            self.prompt_history.pop_front();
 888        }
 889
 890        let assistant_panel_context = assist.assistant_panel_context(cx);
 891
 892        assist
 893            .codegen
 894            .update(cx, |codegen, cx| {
 895                codegen.start(user_prompt, assistant_panel_context, cx)
 896            })
 897            .log_err();
 898
 899        if let Some((tx, _)) = self.assist_observations.get(&assist_id) {
 900            tx.send(()).ok();
 901        }
 902    }
 903
 904    pub fn stop_assist(&mut self, assist_id: InlineAssistId, cx: &mut WindowContext) {
 905        let assist = if let Some(assist) = self.assists.get_mut(&assist_id) {
 906            assist
 907        } else {
 908            return;
 909        };
 910
 911        assist.codegen.update(cx, |codegen, cx| codegen.stop(cx));
 912
 913        if let Some((tx, _)) = self.assist_observations.get(&assist_id) {
 914            tx.send(()).ok();
 915        }
 916    }
 917
 918    pub fn assist_status(&self, assist_id: InlineAssistId, cx: &AppContext) -> InlineAssistStatus {
 919        if let Some(assist) = self.assists.get(&assist_id) {
 920            match &assist.codegen.read(cx).status {
 921                CodegenStatus::Idle => InlineAssistStatus::Idle,
 922                CodegenStatus::Pending => InlineAssistStatus::Pending,
 923                CodegenStatus::Done => InlineAssistStatus::Done,
 924                CodegenStatus::Error(_) => InlineAssistStatus::Error,
 925            }
 926        } else if self.confirmed_assists.contains_key(&assist_id) {
 927            InlineAssistStatus::Confirmed
 928        } else {
 929            InlineAssistStatus::Canceled
 930        }
 931    }
 932
 933    fn update_editor_highlights(&self, editor: &View<Editor>, cx: &mut WindowContext) {
 934        let mut gutter_pending_ranges = Vec::new();
 935        let mut gutter_transformed_ranges = Vec::new();
 936        let mut foreground_ranges = Vec::new();
 937        let mut inserted_row_ranges = Vec::new();
 938        let empty_assist_ids = Vec::new();
 939        let assist_ids = self
 940            .assists_by_editor
 941            .get(&editor.downgrade())
 942            .map_or(&empty_assist_ids, |editor_assists| {
 943                &editor_assists.assist_ids
 944            });
 945
 946        for assist_id in assist_ids {
 947            if let Some(assist) = self.assists.get(assist_id) {
 948                let codegen = assist.codegen.read(cx);
 949                let buffer = codegen.buffer.read(cx).read(cx);
 950                foreground_ranges.extend(codegen.last_equal_ranges().iter().cloned());
 951
 952                let pending_range =
 953                    codegen.edit_position.unwrap_or(assist.range.start)..assist.range.end;
 954                if pending_range.end.to_offset(&buffer) > pending_range.start.to_offset(&buffer) {
 955                    gutter_pending_ranges.push(pending_range);
 956                }
 957
 958                if let Some(edit_position) = codegen.edit_position {
 959                    let edited_range = assist.range.start..edit_position;
 960                    if edited_range.end.to_offset(&buffer) > edited_range.start.to_offset(&buffer) {
 961                        gutter_transformed_ranges.push(edited_range);
 962                    }
 963                }
 964
 965                if assist.decorations.is_some() {
 966                    inserted_row_ranges.extend(codegen.diff.inserted_row_ranges.iter().cloned());
 967                }
 968            }
 969        }
 970
 971        let snapshot = editor.read(cx).buffer().read(cx).snapshot(cx);
 972        merge_ranges(&mut foreground_ranges, &snapshot);
 973        merge_ranges(&mut gutter_pending_ranges, &snapshot);
 974        merge_ranges(&mut gutter_transformed_ranges, &snapshot);
 975        editor.update(cx, |editor, cx| {
 976            enum GutterPendingRange {}
 977            if gutter_pending_ranges.is_empty() {
 978                editor.clear_gutter_highlights::<GutterPendingRange>(cx);
 979            } else {
 980                editor.highlight_gutter::<GutterPendingRange>(
 981                    &gutter_pending_ranges,
 982                    |cx| cx.theme().status().info_background,
 983                    cx,
 984                )
 985            }
 986
 987            enum GutterTransformedRange {}
 988            if gutter_transformed_ranges.is_empty() {
 989                editor.clear_gutter_highlights::<GutterTransformedRange>(cx);
 990            } else {
 991                editor.highlight_gutter::<GutterTransformedRange>(
 992                    &gutter_transformed_ranges,
 993                    |cx| cx.theme().status().info,
 994                    cx,
 995                )
 996            }
 997
 998            if foreground_ranges.is_empty() {
 999                editor.clear_highlights::<InlineAssist>(cx);
1000            } else {
1001                editor.highlight_text::<InlineAssist>(
1002                    foreground_ranges,
1003                    HighlightStyle {
1004                        fade_out: Some(0.6),
1005                        ..Default::default()
1006                    },
1007                    cx,
1008                );
1009            }
1010
1011            editor.clear_row_highlights::<InlineAssist>();
1012            for row_range in inserted_row_ranges {
1013                editor.highlight_rows::<InlineAssist>(
1014                    row_range,
1015                    Some(cx.theme().status().info_background),
1016                    false,
1017                    cx,
1018                );
1019            }
1020        });
1021    }
1022
1023    fn update_editor_blocks(
1024        &mut self,
1025        editor: &View<Editor>,
1026        assist_id: InlineAssistId,
1027        cx: &mut WindowContext,
1028    ) {
1029        let Some(assist) = self.assists.get_mut(&assist_id) else {
1030            return;
1031        };
1032        let Some(decorations) = assist.decorations.as_mut() else {
1033            return;
1034        };
1035
1036        let codegen = assist.codegen.read(cx);
1037        let old_snapshot = codegen.snapshot.clone();
1038        let old_buffer = codegen.old_buffer.clone();
1039        let deleted_row_ranges = codegen.diff.deleted_row_ranges.clone();
1040
1041        editor.update(cx, |editor, cx| {
1042            let old_blocks = mem::take(&mut decorations.removed_line_block_ids);
1043            editor.remove_blocks(old_blocks, None, cx);
1044
1045            let mut new_blocks = Vec::new();
1046            for (new_row, old_row_range) in deleted_row_ranges {
1047                let (_, buffer_start) = old_snapshot
1048                    .point_to_buffer_offset(Point::new(*old_row_range.start(), 0))
1049                    .unwrap();
1050                let (_, buffer_end) = old_snapshot
1051                    .point_to_buffer_offset(Point::new(
1052                        *old_row_range.end(),
1053                        old_snapshot.line_len(MultiBufferRow(*old_row_range.end())),
1054                    ))
1055                    .unwrap();
1056
1057                let deleted_lines_editor = cx.new_view(|cx| {
1058                    let multi_buffer = cx.new_model(|_| {
1059                        MultiBuffer::without_headers(0, language::Capability::ReadOnly)
1060                    });
1061                    multi_buffer.update(cx, |multi_buffer, cx| {
1062                        multi_buffer.push_excerpts(
1063                            old_buffer.clone(),
1064                            Some(ExcerptRange {
1065                                context: buffer_start..buffer_end,
1066                                primary: None,
1067                            }),
1068                            cx,
1069                        );
1070                    });
1071
1072                    enum DeletedLines {}
1073                    let mut editor = Editor::for_multibuffer(multi_buffer, None, true, cx);
1074                    editor.set_soft_wrap_mode(language::language_settings::SoftWrap::None, cx);
1075                    editor.set_show_wrap_guides(false, cx);
1076                    editor.set_show_gutter(false, cx);
1077                    editor.scroll_manager.set_forbid_vertical_scroll(true);
1078                    editor.set_read_only(true);
1079                    editor.highlight_rows::<DeletedLines>(
1080                        Anchor::min()..=Anchor::max(),
1081                        Some(cx.theme().status().deleted_background),
1082                        false,
1083                        cx,
1084                    );
1085                    editor
1086                });
1087
1088                let height =
1089                    deleted_lines_editor.update(cx, |editor, cx| editor.max_point(cx).row().0 + 1);
1090                new_blocks.push(BlockProperties {
1091                    position: new_row,
1092                    height,
1093                    style: BlockStyle::Flex,
1094                    render: Box::new(move |cx| {
1095                        div()
1096                            .bg(cx.theme().status().deleted_background)
1097                            .size_full()
1098                            .h(height as f32 * cx.line_height())
1099                            .pl(cx.gutter_dimensions.full_width())
1100                            .child(deleted_lines_editor.clone())
1101                            .into_any_element()
1102                    }),
1103                    disposition: BlockDisposition::Above,
1104                    priority: 0,
1105                });
1106            }
1107
1108            decorations.removed_line_block_ids = editor
1109                .insert_blocks(new_blocks, None, cx)
1110                .into_iter()
1111                .collect();
1112        })
1113    }
1114
1115    pub fn observe_assist(&mut self, assist_id: InlineAssistId) -> async_watch::Receiver<()> {
1116        if let Some((_, rx)) = self.assist_observations.get(&assist_id) {
1117            rx.clone()
1118        } else {
1119            let (tx, rx) = async_watch::channel(());
1120            self.assist_observations.insert(assist_id, (tx, rx.clone()));
1121            rx
1122        }
1123    }
1124}
1125
1126pub enum InlineAssistStatus {
1127    Idle,
1128    Pending,
1129    Done,
1130    Error,
1131    Confirmed,
1132    Canceled,
1133}
1134
1135impl InlineAssistStatus {
1136    pub(crate) fn is_pending(&self) -> bool {
1137        matches!(self, Self::Pending)
1138    }
1139    pub(crate) fn is_confirmed(&self) -> bool {
1140        matches!(self, Self::Confirmed)
1141    }
1142    pub(crate) fn is_done(&self) -> bool {
1143        matches!(self, Self::Done)
1144    }
1145}
1146
1147struct EditorInlineAssists {
1148    assist_ids: Vec<InlineAssistId>,
1149    scroll_lock: Option<InlineAssistScrollLock>,
1150    highlight_updates: async_watch::Sender<()>,
1151    _update_highlights: Task<Result<()>>,
1152    _subscriptions: Vec<gpui::Subscription>,
1153}
1154
1155struct InlineAssistScrollLock {
1156    assist_id: InlineAssistId,
1157    distance_from_top: f32,
1158}
1159
1160impl EditorInlineAssists {
1161    #[allow(clippy::too_many_arguments)]
1162    fn new(editor: &View<Editor>, cx: &mut WindowContext) -> Self {
1163        let (highlight_updates_tx, mut highlight_updates_rx) = async_watch::channel(());
1164        Self {
1165            assist_ids: Vec::new(),
1166            scroll_lock: None,
1167            highlight_updates: highlight_updates_tx,
1168            _update_highlights: cx.spawn(|mut cx| {
1169                let editor = editor.downgrade();
1170                async move {
1171                    while let Ok(()) = highlight_updates_rx.changed().await {
1172                        let editor = editor.upgrade().context("editor was dropped")?;
1173                        cx.update_global(|assistant: &mut InlineAssistant, cx| {
1174                            assistant.update_editor_highlights(&editor, cx);
1175                        })?;
1176                    }
1177                    Ok(())
1178                }
1179            }),
1180            _subscriptions: vec![
1181                cx.observe_release(editor, {
1182                    let editor = editor.downgrade();
1183                    |_, cx| {
1184                        InlineAssistant::update_global(cx, |this, cx| {
1185                            this.handle_editor_release(editor, cx);
1186                        })
1187                    }
1188                }),
1189                cx.observe(editor, move |editor, cx| {
1190                    InlineAssistant::update_global(cx, |this, cx| {
1191                        this.handle_editor_change(editor, cx)
1192                    })
1193                }),
1194                cx.subscribe(editor, move |editor, event, cx| {
1195                    InlineAssistant::update_global(cx, |this, cx| {
1196                        this.handle_editor_event(editor, event, cx)
1197                    })
1198                }),
1199                editor.update(cx, |editor, cx| {
1200                    let editor_handle = cx.view().downgrade();
1201                    editor.register_action(
1202                        move |_: &editor::actions::Newline, cx: &mut WindowContext| {
1203                            InlineAssistant::update_global(cx, |this, cx| {
1204                                if let Some(editor) = editor_handle.upgrade() {
1205                                    this.handle_editor_newline(editor, cx)
1206                                }
1207                            })
1208                        },
1209                    )
1210                }),
1211                editor.update(cx, |editor, cx| {
1212                    let editor_handle = cx.view().downgrade();
1213                    editor.register_action(
1214                        move |_: &editor::actions::Cancel, cx: &mut WindowContext| {
1215                            InlineAssistant::update_global(cx, |this, cx| {
1216                                if let Some(editor) = editor_handle.upgrade() {
1217                                    this.handle_editor_cancel(editor, cx)
1218                                }
1219                            })
1220                        },
1221                    )
1222                }),
1223            ],
1224        }
1225    }
1226}
1227
1228struct InlineAssistGroup {
1229    assist_ids: Vec<InlineAssistId>,
1230    linked: bool,
1231    active_assist_id: Option<InlineAssistId>,
1232}
1233
1234impl InlineAssistGroup {
1235    fn new() -> Self {
1236        Self {
1237            assist_ids: Vec::new(),
1238            linked: true,
1239            active_assist_id: None,
1240        }
1241    }
1242}
1243
1244fn build_assist_editor_renderer(editor: &View<PromptEditor>) -> RenderBlock {
1245    let editor = editor.clone();
1246    Box::new(move |cx: &mut BlockContext| {
1247        *editor.read(cx).gutter_dimensions.lock() = *cx.gutter_dimensions;
1248        editor.clone().into_any_element()
1249    })
1250}
1251
1252#[derive(Copy, Clone, Debug, Eq, PartialEq)]
1253pub enum InitialInsertion {
1254    NewlineBefore,
1255    NewlineAfter,
1256}
1257
1258#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, Hash)]
1259pub struct InlineAssistId(usize);
1260
1261impl InlineAssistId {
1262    fn post_inc(&mut self) -> InlineAssistId {
1263        let id = *self;
1264        self.0 += 1;
1265        id
1266    }
1267}
1268
1269#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, Hash)]
1270struct InlineAssistGroupId(usize);
1271
1272impl InlineAssistGroupId {
1273    fn post_inc(&mut self) -> InlineAssistGroupId {
1274        let id = *self;
1275        self.0 += 1;
1276        id
1277    }
1278}
1279
1280enum PromptEditorEvent {
1281    StartRequested,
1282    StopRequested,
1283    ConfirmRequested,
1284    CancelRequested,
1285    DismissRequested,
1286}
1287
1288struct PromptEditor {
1289    id: InlineAssistId,
1290    fs: Arc<dyn Fs>,
1291    editor: View<Editor>,
1292    edited_since_done: bool,
1293    gutter_dimensions: Arc<Mutex<GutterDimensions>>,
1294    prompt_history: VecDeque<String>,
1295    prompt_history_ix: Option<usize>,
1296    pending_prompt: String,
1297    codegen: Model<Codegen>,
1298    _codegen_subscription: Subscription,
1299    editor_subscriptions: Vec<Subscription>,
1300    pending_token_count: Task<Result<()>>,
1301    token_counts: Option<TokenCounts>,
1302    _token_count_subscriptions: Vec<Subscription>,
1303    workspace: Option<WeakView<Workspace>>,
1304    show_rate_limit_notice: bool,
1305}
1306
1307#[derive(Copy, Clone)]
1308pub struct TokenCounts {
1309    total: usize,
1310    assistant_panel: usize,
1311}
1312
1313impl EventEmitter<PromptEditorEvent> for PromptEditor {}
1314
1315impl Render for PromptEditor {
1316    fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
1317        let gutter_dimensions = *self.gutter_dimensions.lock();
1318        let status = &self.codegen.read(cx).status;
1319        let buttons = match status {
1320            CodegenStatus::Idle => {
1321                vec![
1322                    IconButton::new("cancel", IconName::Close)
1323                        .icon_color(Color::Muted)
1324                        .shape(IconButtonShape::Square)
1325                        .tooltip(|cx| Tooltip::for_action("Cancel Assist", &menu::Cancel, cx))
1326                        .on_click(
1327                            cx.listener(|_, _, cx| cx.emit(PromptEditorEvent::CancelRequested)),
1328                        ),
1329                    IconButton::new("start", IconName::SparkleAlt)
1330                        .icon_color(Color::Muted)
1331                        .shape(IconButtonShape::Square)
1332                        .tooltip(|cx| Tooltip::for_action("Transform", &menu::Confirm, cx))
1333                        .on_click(
1334                            cx.listener(|_, _, cx| cx.emit(PromptEditorEvent::StartRequested)),
1335                        ),
1336                ]
1337            }
1338            CodegenStatus::Pending => {
1339                vec![
1340                    IconButton::new("cancel", IconName::Close)
1341                        .icon_color(Color::Muted)
1342                        .shape(IconButtonShape::Square)
1343                        .tooltip(|cx| Tooltip::text("Cancel Assist", cx))
1344                        .on_click(
1345                            cx.listener(|_, _, cx| cx.emit(PromptEditorEvent::CancelRequested)),
1346                        ),
1347                    IconButton::new("stop", IconName::Stop)
1348                        .icon_color(Color::Error)
1349                        .shape(IconButtonShape::Square)
1350                        .tooltip(|cx| {
1351                            Tooltip::with_meta(
1352                                "Interrupt Transformation",
1353                                Some(&menu::Cancel),
1354                                "Changes won't be discarded",
1355                                cx,
1356                            )
1357                        })
1358                        .on_click(
1359                            cx.listener(|_, _, cx| cx.emit(PromptEditorEvent::StopRequested)),
1360                        ),
1361                ]
1362            }
1363            CodegenStatus::Error(_) | CodegenStatus::Done => {
1364                vec![
1365                    IconButton::new("cancel", IconName::Close)
1366                        .icon_color(Color::Muted)
1367                        .shape(IconButtonShape::Square)
1368                        .tooltip(|cx| Tooltip::for_action("Cancel Assist", &menu::Cancel, cx))
1369                        .on_click(
1370                            cx.listener(|_, _, cx| cx.emit(PromptEditorEvent::CancelRequested)),
1371                        ),
1372                    if self.edited_since_done || matches!(status, CodegenStatus::Error(_)) {
1373                        IconButton::new("restart", IconName::RotateCw)
1374                            .icon_color(Color::Info)
1375                            .shape(IconButtonShape::Square)
1376                            .tooltip(|cx| {
1377                                Tooltip::with_meta(
1378                                    "Restart Transformation",
1379                                    Some(&menu::Confirm),
1380                                    "Changes will be discarded",
1381                                    cx,
1382                                )
1383                            })
1384                            .on_click(cx.listener(|_, _, cx| {
1385                                cx.emit(PromptEditorEvent::StartRequested);
1386                            }))
1387                    } else {
1388                        IconButton::new("confirm", IconName::Check)
1389                            .icon_color(Color::Info)
1390                            .shape(IconButtonShape::Square)
1391                            .tooltip(|cx| Tooltip::for_action("Confirm Assist", &menu::Confirm, cx))
1392                            .on_click(cx.listener(|_, _, cx| {
1393                                cx.emit(PromptEditorEvent::ConfirmRequested);
1394                            }))
1395                    },
1396                ]
1397            }
1398        };
1399
1400        h_flex()
1401            .bg(cx.theme().colors().editor_background)
1402            .border_y_1()
1403            .border_color(cx.theme().status().info_border)
1404            .size_full()
1405            .py(cx.line_height() / 2.)
1406            .on_action(cx.listener(Self::confirm))
1407            .on_action(cx.listener(Self::cancel))
1408            .on_action(cx.listener(Self::move_up))
1409            .on_action(cx.listener(Self::move_down))
1410            .child(
1411                h_flex()
1412                    .w(gutter_dimensions.full_width() + (gutter_dimensions.margin / 2.0))
1413                    .justify_center()
1414                    .gap_2()
1415                    .child(
1416                        ModelSelector::new(
1417                            self.fs.clone(),
1418                            IconButton::new("context", IconName::SlidersAlt)
1419                                .shape(IconButtonShape::Square)
1420                                .icon_size(IconSize::Small)
1421                                .icon_color(Color::Muted)
1422                                .tooltip(move |cx| {
1423                                    Tooltip::with_meta(
1424                                        format!(
1425                                            "Using {}",
1426                                            LanguageModelRegistry::read_global(cx)
1427                                                .active_model()
1428                                                .map(|model| model.name().0)
1429                                                .unwrap_or_else(|| "No model selected".into()),
1430                                        ),
1431                                        None,
1432                                        "Change Model",
1433                                        cx,
1434                                    )
1435                                }),
1436                        )
1437                        .with_info_text(
1438                            "Inline edits use context\n\
1439                            from the currently selected\n\
1440                            assistant panel tab.",
1441                        ),
1442                    )
1443                    .map(|el| {
1444                        let CodegenStatus::Error(error) = &self.codegen.read(cx).status else {
1445                            return el;
1446                        };
1447
1448                        let error_message = SharedString::from(error.to_string());
1449                        if error.error_code() == proto::ErrorCode::RateLimitExceeded
1450                            && cx.has_flag::<ZedPro>()
1451                        {
1452                            el.child(
1453                                v_flex()
1454                                    .child(
1455                                        IconButton::new("rate-limit-error", IconName::XCircle)
1456                                            .selected(self.show_rate_limit_notice)
1457                                            .shape(IconButtonShape::Square)
1458                                            .icon_size(IconSize::Small)
1459                                            .on_click(cx.listener(Self::toggle_rate_limit_notice)),
1460                                    )
1461                                    .children(self.show_rate_limit_notice.then(|| {
1462                                        deferred(
1463                                            anchored()
1464                                                .position_mode(gpui::AnchoredPositionMode::Local)
1465                                                .position(point(px(0.), px(24.)))
1466                                                .anchor(gpui::AnchorCorner::TopLeft)
1467                                                .child(self.render_rate_limit_notice(cx)),
1468                                        )
1469                                    })),
1470                            )
1471                        } else {
1472                            el.child(
1473                                div()
1474                                    .id("error")
1475                                    .tooltip(move |cx| Tooltip::text(error_message.clone(), cx))
1476                                    .child(
1477                                        Icon::new(IconName::XCircle)
1478                                            .size(IconSize::Small)
1479                                            .color(Color::Error),
1480                                    ),
1481                            )
1482                        }
1483                    }),
1484            )
1485            .child(div().flex_1().child(self.render_prompt_editor(cx)))
1486            .child(
1487                h_flex()
1488                    .gap_2()
1489                    .pr_6()
1490                    .children(self.render_token_count(cx))
1491                    .children(buttons),
1492            )
1493    }
1494}
1495
1496impl FocusableView for PromptEditor {
1497    fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
1498        self.editor.focus_handle(cx)
1499    }
1500}
1501
1502impl PromptEditor {
1503    const MAX_LINES: u8 = 8;
1504
1505    #[allow(clippy::too_many_arguments)]
1506    fn new(
1507        id: InlineAssistId,
1508        gutter_dimensions: Arc<Mutex<GutterDimensions>>,
1509        prompt_history: VecDeque<String>,
1510        prompt_buffer: Model<MultiBuffer>,
1511        codegen: Model<Codegen>,
1512        parent_editor: &View<Editor>,
1513        assistant_panel: Option<&View<AssistantPanel>>,
1514        workspace: Option<WeakView<Workspace>>,
1515        fs: Arc<dyn Fs>,
1516        cx: &mut ViewContext<Self>,
1517    ) -> Self {
1518        let prompt_editor = cx.new_view(|cx| {
1519            let mut editor = Editor::new(
1520                EditorMode::AutoHeight {
1521                    max_lines: Self::MAX_LINES as usize,
1522                },
1523                prompt_buffer,
1524                None,
1525                false,
1526                cx,
1527            );
1528            editor.set_soft_wrap_mode(language::language_settings::SoftWrap::EditorWidth, cx);
1529            // Since the prompt editors for all inline assistants are linked,
1530            // always show the cursor (even when it isn't focused) because
1531            // typing in one will make what you typed appear in all of them.
1532            editor.set_show_cursor_when_unfocused(true, cx);
1533            editor.set_placeholder_text("Add a prompt…", cx);
1534            editor
1535        });
1536
1537        let mut token_count_subscriptions = Vec::new();
1538        token_count_subscriptions
1539            .push(cx.subscribe(parent_editor, Self::handle_parent_editor_event));
1540        if let Some(assistant_panel) = assistant_panel {
1541            token_count_subscriptions
1542                .push(cx.subscribe(assistant_panel, Self::handle_assistant_panel_event));
1543        }
1544
1545        let mut this = Self {
1546            id,
1547            editor: prompt_editor,
1548            edited_since_done: false,
1549            gutter_dimensions,
1550            prompt_history,
1551            prompt_history_ix: None,
1552            pending_prompt: String::new(),
1553            _codegen_subscription: cx.observe(&codegen, Self::handle_codegen_changed),
1554            editor_subscriptions: Vec::new(),
1555            codegen,
1556            fs,
1557            pending_token_count: Task::ready(Ok(())),
1558            token_counts: None,
1559            _token_count_subscriptions: token_count_subscriptions,
1560            workspace,
1561            show_rate_limit_notice: false,
1562        };
1563        this.count_tokens(cx);
1564        this.subscribe_to_editor(cx);
1565        this
1566    }
1567
1568    fn subscribe_to_editor(&mut self, cx: &mut ViewContext<Self>) {
1569        self.editor_subscriptions.clear();
1570        self.editor_subscriptions
1571            .push(cx.subscribe(&self.editor, Self::handle_prompt_editor_events));
1572    }
1573
1574    fn set_show_cursor_when_unfocused(
1575        &mut self,
1576        show_cursor_when_unfocused: bool,
1577        cx: &mut ViewContext<Self>,
1578    ) {
1579        self.editor.update(cx, |editor, cx| {
1580            editor.set_show_cursor_when_unfocused(show_cursor_when_unfocused, cx)
1581        });
1582    }
1583
1584    fn unlink(&mut self, cx: &mut ViewContext<Self>) {
1585        let prompt = self.prompt(cx);
1586        let focus = self.editor.focus_handle(cx).contains_focused(cx);
1587        self.editor = cx.new_view(|cx| {
1588            let mut editor = Editor::auto_height(Self::MAX_LINES as usize, cx);
1589            editor.set_soft_wrap_mode(language::language_settings::SoftWrap::EditorWidth, cx);
1590            editor.set_placeholder_text("Add a prompt…", cx);
1591            editor.set_text(prompt, cx);
1592            if focus {
1593                editor.focus(cx);
1594            }
1595            editor
1596        });
1597        self.subscribe_to_editor(cx);
1598    }
1599
1600    fn prompt(&self, cx: &AppContext) -> String {
1601        self.editor.read(cx).text(cx)
1602    }
1603
1604    fn toggle_rate_limit_notice(&mut self, _: &ClickEvent, cx: &mut ViewContext<Self>) {
1605        self.show_rate_limit_notice = !self.show_rate_limit_notice;
1606        if self.show_rate_limit_notice {
1607            cx.focus_view(&self.editor);
1608        }
1609        cx.notify();
1610    }
1611
1612    fn handle_parent_editor_event(
1613        &mut self,
1614        _: View<Editor>,
1615        event: &EditorEvent,
1616        cx: &mut ViewContext<Self>,
1617    ) {
1618        if let EditorEvent::BufferEdited { .. } = event {
1619            self.count_tokens(cx);
1620        }
1621    }
1622
1623    fn handle_assistant_panel_event(
1624        &mut self,
1625        _: View<AssistantPanel>,
1626        event: &AssistantPanelEvent,
1627        cx: &mut ViewContext<Self>,
1628    ) {
1629        let AssistantPanelEvent::ContextEdited { .. } = event;
1630        self.count_tokens(cx);
1631    }
1632
1633    fn count_tokens(&mut self, cx: &mut ViewContext<Self>) {
1634        let assist_id = self.id;
1635        self.pending_token_count = cx.spawn(|this, mut cx| async move {
1636            cx.background_executor().timer(Duration::from_secs(1)).await;
1637            let token_count = cx
1638                .update_global(|inline_assistant: &mut InlineAssistant, cx| {
1639                    let assist = inline_assistant
1640                        .assists
1641                        .get(&assist_id)
1642                        .context("assist not found")?;
1643                    anyhow::Ok(assist.count_tokens(cx))
1644                })??
1645                .await?;
1646
1647            this.update(&mut cx, |this, cx| {
1648                this.token_counts = Some(token_count);
1649                cx.notify();
1650            })
1651        })
1652    }
1653
1654    fn handle_prompt_editor_events(
1655        &mut self,
1656        _: View<Editor>,
1657        event: &EditorEvent,
1658        cx: &mut ViewContext<Self>,
1659    ) {
1660        match event {
1661            EditorEvent::Edited { .. } => {
1662                let prompt = self.editor.read(cx).text(cx);
1663                if self
1664                    .prompt_history_ix
1665                    .map_or(true, |ix| self.prompt_history[ix] != prompt)
1666                {
1667                    self.prompt_history_ix.take();
1668                    self.pending_prompt = prompt;
1669                }
1670
1671                self.edited_since_done = true;
1672                cx.notify();
1673            }
1674            EditorEvent::BufferEdited => {
1675                self.count_tokens(cx);
1676            }
1677            EditorEvent::Blurred => {
1678                if self.show_rate_limit_notice {
1679                    self.show_rate_limit_notice = false;
1680                    cx.notify();
1681                }
1682            }
1683            _ => {}
1684        }
1685    }
1686
1687    fn handle_codegen_changed(&mut self, _: Model<Codegen>, cx: &mut ViewContext<Self>) {
1688        match &self.codegen.read(cx).status {
1689            CodegenStatus::Idle => {
1690                self.editor
1691                    .update(cx, |editor, _| editor.set_read_only(false));
1692            }
1693            CodegenStatus::Pending => {
1694                self.editor
1695                    .update(cx, |editor, _| editor.set_read_only(true));
1696            }
1697            CodegenStatus::Done => {
1698                self.edited_since_done = false;
1699                self.editor
1700                    .update(cx, |editor, _| editor.set_read_only(false));
1701            }
1702            CodegenStatus::Error(error) => {
1703                if cx.has_flag::<ZedPro>()
1704                    && error.error_code() == proto::ErrorCode::RateLimitExceeded
1705                    && !dismissed_rate_limit_notice()
1706                {
1707                    self.show_rate_limit_notice = true;
1708                    cx.notify();
1709                }
1710
1711                self.edited_since_done = false;
1712                self.editor
1713                    .update(cx, |editor, _| editor.set_read_only(false));
1714            }
1715        }
1716    }
1717
1718    fn cancel(&mut self, _: &editor::actions::Cancel, cx: &mut ViewContext<Self>) {
1719        match &self.codegen.read(cx).status {
1720            CodegenStatus::Idle | CodegenStatus::Done | CodegenStatus::Error(_) => {
1721                cx.emit(PromptEditorEvent::CancelRequested);
1722            }
1723            CodegenStatus::Pending => {
1724                cx.emit(PromptEditorEvent::StopRequested);
1725            }
1726        }
1727    }
1728
1729    fn confirm(&mut self, _: &menu::Confirm, cx: &mut ViewContext<Self>) {
1730        match &self.codegen.read(cx).status {
1731            CodegenStatus::Idle => {
1732                cx.emit(PromptEditorEvent::StartRequested);
1733            }
1734            CodegenStatus::Pending => {
1735                cx.emit(PromptEditorEvent::DismissRequested);
1736            }
1737            CodegenStatus::Done | CodegenStatus::Error(_) => {
1738                if self.edited_since_done {
1739                    cx.emit(PromptEditorEvent::StartRequested);
1740                } else {
1741                    cx.emit(PromptEditorEvent::ConfirmRequested);
1742                }
1743            }
1744        }
1745    }
1746
1747    fn move_up(&mut self, _: &MoveUp, cx: &mut ViewContext<Self>) {
1748        if let Some(ix) = self.prompt_history_ix {
1749            if ix > 0 {
1750                self.prompt_history_ix = Some(ix - 1);
1751                let prompt = self.prompt_history[ix - 1].as_str();
1752                self.editor.update(cx, |editor, cx| {
1753                    editor.set_text(prompt, cx);
1754                    editor.move_to_beginning(&Default::default(), cx);
1755                });
1756            }
1757        } else if !self.prompt_history.is_empty() {
1758            self.prompt_history_ix = Some(self.prompt_history.len() - 1);
1759            let prompt = self.prompt_history[self.prompt_history.len() - 1].as_str();
1760            self.editor.update(cx, |editor, cx| {
1761                editor.set_text(prompt, cx);
1762                editor.move_to_beginning(&Default::default(), cx);
1763            });
1764        }
1765    }
1766
1767    fn move_down(&mut self, _: &MoveDown, cx: &mut ViewContext<Self>) {
1768        if let Some(ix) = self.prompt_history_ix {
1769            if ix < self.prompt_history.len() - 1 {
1770                self.prompt_history_ix = Some(ix + 1);
1771                let prompt = self.prompt_history[ix + 1].as_str();
1772                self.editor.update(cx, |editor, cx| {
1773                    editor.set_text(prompt, cx);
1774                    editor.move_to_end(&Default::default(), cx)
1775                });
1776            } else {
1777                self.prompt_history_ix = None;
1778                let prompt = self.pending_prompt.as_str();
1779                self.editor.update(cx, |editor, cx| {
1780                    editor.set_text(prompt, cx);
1781                    editor.move_to_end(&Default::default(), cx)
1782                });
1783            }
1784        }
1785    }
1786
1787    fn render_token_count(&self, cx: &mut ViewContext<Self>) -> Option<impl IntoElement> {
1788        let model = LanguageModelRegistry::read_global(cx).active_model()?;
1789        let token_counts = self.token_counts?;
1790        let max_token_count = model.max_token_count();
1791
1792        let remaining_tokens = max_token_count as isize - token_counts.total as isize;
1793        let token_count_color = if remaining_tokens <= 0 {
1794            Color::Error
1795        } else if token_counts.total as f32 / max_token_count as f32 >= 0.8 {
1796            Color::Warning
1797        } else {
1798            Color::Muted
1799        };
1800
1801        let mut token_count = h_flex()
1802            .id("token_count")
1803            .gap_0p5()
1804            .child(
1805                Label::new(humanize_token_count(token_counts.total))
1806                    .size(LabelSize::Small)
1807                    .color(token_count_color),
1808            )
1809            .child(Label::new("/").size(LabelSize::Small).color(Color::Muted))
1810            .child(
1811                Label::new(humanize_token_count(max_token_count))
1812                    .size(LabelSize::Small)
1813                    .color(Color::Muted),
1814            );
1815        if let Some(workspace) = self.workspace.clone() {
1816            token_count = token_count
1817                .tooltip(move |cx| {
1818                    Tooltip::with_meta(
1819                        format!(
1820                            "Tokens Used ({} from the Assistant Panel)",
1821                            humanize_token_count(token_counts.assistant_panel)
1822                        ),
1823                        None,
1824                        "Click to open the Assistant Panel",
1825                        cx,
1826                    )
1827                })
1828                .cursor_pointer()
1829                .on_mouse_down(gpui::MouseButton::Left, |_, cx| cx.stop_propagation())
1830                .on_click(move |_, cx| {
1831                    cx.stop_propagation();
1832                    workspace
1833                        .update(cx, |workspace, cx| {
1834                            workspace.focus_panel::<AssistantPanel>(cx)
1835                        })
1836                        .ok();
1837                });
1838        } else {
1839            token_count = token_count
1840                .cursor_default()
1841                .tooltip(|cx| Tooltip::text("Tokens used", cx));
1842        }
1843
1844        Some(token_count)
1845    }
1846
1847    fn render_prompt_editor(&self, cx: &mut ViewContext<Self>) -> impl IntoElement {
1848        let settings = ThemeSettings::get_global(cx);
1849        let text_style = TextStyle {
1850            color: if self.editor.read(cx).read_only(cx) {
1851                cx.theme().colors().text_disabled
1852            } else {
1853                cx.theme().colors().text
1854            },
1855            font_family: settings.ui_font.family.clone(),
1856            font_features: settings.ui_font.features.clone(),
1857            font_fallbacks: settings.ui_font.fallbacks.clone(),
1858            font_size: rems(0.875).into(),
1859            font_weight: settings.ui_font.weight,
1860            line_height: relative(1.3),
1861            ..Default::default()
1862        };
1863        EditorElement::new(
1864            &self.editor,
1865            EditorStyle {
1866                background: cx.theme().colors().editor_background,
1867                local_player: cx.theme().players().local(),
1868                text: text_style,
1869                ..Default::default()
1870            },
1871        )
1872    }
1873
1874    fn render_rate_limit_notice(&self, cx: &mut ViewContext<Self>) -> impl IntoElement {
1875        Popover::new().child(
1876            v_flex()
1877                .occlude()
1878                .p_2()
1879                .child(
1880                    Label::new("Out of Tokens")
1881                        .size(LabelSize::Small)
1882                        .weight(FontWeight::BOLD),
1883                )
1884                .child(Label::new(
1885                    "Try Zed Pro for higher limits, a wider range of models, and more.",
1886                ))
1887                .child(
1888                    h_flex()
1889                        .justify_between()
1890                        .child(CheckboxWithLabel::new(
1891                            "dont-show-again",
1892                            Label::new("Don't show again"),
1893                            if dismissed_rate_limit_notice() {
1894                                ui::Selection::Selected
1895                            } else {
1896                                ui::Selection::Unselected
1897                            },
1898                            |selection, cx| {
1899                                let is_dismissed = match selection {
1900                                    ui::Selection::Unselected => false,
1901                                    ui::Selection::Indeterminate => return,
1902                                    ui::Selection::Selected => true,
1903                                };
1904
1905                                set_rate_limit_notice_dismissed(is_dismissed, cx)
1906                            },
1907                        ))
1908                        .child(
1909                            h_flex()
1910                                .gap_2()
1911                                .child(
1912                                    Button::new("dismiss", "Dismiss")
1913                                        .style(ButtonStyle::Transparent)
1914                                        .on_click(cx.listener(Self::toggle_rate_limit_notice)),
1915                                )
1916                                .child(Button::new("more-info", "More Info").on_click(
1917                                    |_event, cx| {
1918                                        cx.dispatch_action(Box::new(
1919                                            zed_actions::OpenAccountSettings,
1920                                        ))
1921                                    },
1922                                )),
1923                        ),
1924                ),
1925        )
1926    }
1927}
1928
1929const DISMISSED_RATE_LIMIT_NOTICE_KEY: &str = "dismissed-rate-limit-notice";
1930
1931fn dismissed_rate_limit_notice() -> bool {
1932    db::kvp::KEY_VALUE_STORE
1933        .read_kvp(DISMISSED_RATE_LIMIT_NOTICE_KEY)
1934        .log_err()
1935        .map_or(false, |s| s.is_some())
1936}
1937
1938fn set_rate_limit_notice_dismissed(is_dismissed: bool, cx: &mut AppContext) {
1939    db::write_and_log(cx, move || async move {
1940        if is_dismissed {
1941            db::kvp::KEY_VALUE_STORE
1942                .write_kvp(DISMISSED_RATE_LIMIT_NOTICE_KEY.into(), "1".into())
1943                .await
1944        } else {
1945            db::kvp::KEY_VALUE_STORE
1946                .delete_kvp(DISMISSED_RATE_LIMIT_NOTICE_KEY.into())
1947                .await
1948        }
1949    })
1950}
1951
1952struct InlineAssist {
1953    group_id: InlineAssistGroupId,
1954    range: Range<Anchor>,
1955    editor: WeakView<Editor>,
1956    decorations: Option<InlineAssistDecorations>,
1957    codegen: Model<Codegen>,
1958    _subscriptions: Vec<Subscription>,
1959    workspace: Option<WeakView<Workspace>>,
1960    include_context: bool,
1961}
1962
1963impl InlineAssist {
1964    #[allow(clippy::too_many_arguments)]
1965    fn new(
1966        assist_id: InlineAssistId,
1967        group_id: InlineAssistGroupId,
1968        include_context: bool,
1969        editor: &View<Editor>,
1970        prompt_editor: &View<PromptEditor>,
1971        prompt_block_id: CustomBlockId,
1972        end_block_id: CustomBlockId,
1973        range: Range<Anchor>,
1974        codegen: Model<Codegen>,
1975        workspace: Option<WeakView<Workspace>>,
1976        cx: &mut WindowContext,
1977    ) -> Self {
1978        let prompt_editor_focus_handle = prompt_editor.focus_handle(cx);
1979        InlineAssist {
1980            group_id,
1981            include_context,
1982            editor: editor.downgrade(),
1983            decorations: Some(InlineAssistDecorations {
1984                prompt_block_id,
1985                prompt_editor: prompt_editor.clone(),
1986                removed_line_block_ids: HashSet::default(),
1987                end_block_id,
1988            }),
1989            range,
1990            codegen: codegen.clone(),
1991            workspace: workspace.clone(),
1992            _subscriptions: vec![
1993                cx.on_focus_in(&prompt_editor_focus_handle, move |cx| {
1994                    InlineAssistant::update_global(cx, |this, cx| {
1995                        this.handle_prompt_editor_focus_in(assist_id, cx)
1996                    })
1997                }),
1998                cx.on_focus_out(&prompt_editor_focus_handle, move |_, cx| {
1999                    InlineAssistant::update_global(cx, |this, cx| {
2000                        this.handle_prompt_editor_focus_out(assist_id, cx)
2001                    })
2002                }),
2003                cx.subscribe(prompt_editor, |prompt_editor, event, cx| {
2004                    InlineAssistant::update_global(cx, |this, cx| {
2005                        this.handle_prompt_editor_event(prompt_editor, event, cx)
2006                    })
2007                }),
2008                cx.observe(&codegen, {
2009                    let editor = editor.downgrade();
2010                    move |_, cx| {
2011                        if let Some(editor) = editor.upgrade() {
2012                            InlineAssistant::update_global(cx, |this, cx| {
2013                                if let Some(editor_assists) =
2014                                    this.assists_by_editor.get(&editor.downgrade())
2015                                {
2016                                    editor_assists.highlight_updates.send(()).ok();
2017                                }
2018
2019                                this.update_editor_blocks(&editor, assist_id, cx);
2020                            })
2021                        }
2022                    }
2023                }),
2024                cx.subscribe(&codegen, move |codegen, event, cx| {
2025                    InlineAssistant::update_global(cx, |this, cx| match event {
2026                        CodegenEvent::Undone => this.finish_assist(assist_id, false, cx),
2027                        CodegenEvent::Finished => {
2028                            let assist = if let Some(assist) = this.assists.get(&assist_id) {
2029                                assist
2030                            } else {
2031                                return;
2032                            };
2033
2034                            if let CodegenStatus::Error(error) = &codegen.read(cx).status {
2035                                if assist.decorations.is_none() {
2036                                    if let Some(workspace) = assist
2037                                        .workspace
2038                                        .as_ref()
2039                                        .and_then(|workspace| workspace.upgrade())
2040                                    {
2041                                        let error = format!("Inline assistant error: {}", error);
2042                                        workspace.update(cx, |workspace, cx| {
2043                                            struct InlineAssistantError;
2044
2045                                            let id =
2046                                                NotificationId::identified::<InlineAssistantError>(
2047                                                    assist_id.0,
2048                                                );
2049
2050                                            workspace.show_toast(Toast::new(id, error), cx);
2051                                        })
2052                                    }
2053                                }
2054                            }
2055
2056                            if assist.decorations.is_none() {
2057                                this.finish_assist(assist_id, false, cx);
2058                            } else if let Some(tx) = this.assist_observations.get(&assist_id) {
2059                                tx.0.send(()).ok();
2060                            }
2061                        }
2062                    })
2063                }),
2064            ],
2065        }
2066    }
2067
2068    fn user_prompt(&self, cx: &AppContext) -> Option<String> {
2069        let decorations = self.decorations.as_ref()?;
2070        Some(decorations.prompt_editor.read(cx).prompt(cx))
2071    }
2072
2073    fn assistant_panel_context(&self, cx: &WindowContext) -> Option<LanguageModelRequest> {
2074        if self.include_context {
2075            let workspace = self.workspace.as_ref()?;
2076            let workspace = workspace.upgrade()?.read(cx);
2077            let assistant_panel = workspace.panel::<AssistantPanel>(cx)?;
2078            Some(
2079                assistant_panel
2080                    .read(cx)
2081                    .active_context(cx)?
2082                    .read(cx)
2083                    .to_completion_request(cx),
2084            )
2085        } else {
2086            None
2087        }
2088    }
2089
2090    pub fn count_tokens(&self, cx: &WindowContext) -> BoxFuture<'static, Result<TokenCounts>> {
2091        let Some(user_prompt) = self.user_prompt(cx) else {
2092            return future::ready(Err(anyhow!("no user prompt"))).boxed();
2093        };
2094        let assistant_panel_context = self.assistant_panel_context(cx);
2095        self.codegen
2096            .read(cx)
2097            .count_tokens(user_prompt, assistant_panel_context, cx)
2098    }
2099}
2100
2101struct InlineAssistDecorations {
2102    prompt_block_id: CustomBlockId,
2103    prompt_editor: View<PromptEditor>,
2104    removed_line_block_ids: HashSet<CustomBlockId>,
2105    end_block_id: CustomBlockId,
2106}
2107
2108#[derive(Debug)]
2109pub enum CodegenEvent {
2110    Finished,
2111    Undone,
2112}
2113
2114pub struct Codegen {
2115    buffer: Model<MultiBuffer>,
2116    old_buffer: Model<Buffer>,
2117    snapshot: MultiBufferSnapshot,
2118    transform_range: Range<Anchor>,
2119    selected_ranges: Vec<Range<Anchor>>,
2120    edit_position: Option<Anchor>,
2121    last_equal_ranges: Vec<Range<Anchor>>,
2122    initial_transaction_id: Option<TransactionId>,
2123    transformation_transaction_id: Option<TransactionId>,
2124    status: CodegenStatus,
2125    generation: Task<()>,
2126    diff: Diff,
2127    telemetry: Option<Arc<Telemetry>>,
2128    _subscription: gpui::Subscription,
2129    prompt_builder: Arc<PromptBuilder>,
2130}
2131
2132enum CodegenStatus {
2133    Idle,
2134    Pending,
2135    Done,
2136    Error(anyhow::Error),
2137}
2138
2139#[derive(Default)]
2140struct Diff {
2141    deleted_row_ranges: Vec<(Anchor, RangeInclusive<u32>)>,
2142    inserted_row_ranges: Vec<RangeInclusive<Anchor>>,
2143}
2144
2145impl Diff {
2146    fn is_empty(&self) -> bool {
2147        self.deleted_row_ranges.is_empty() && self.inserted_row_ranges.is_empty()
2148    }
2149}
2150
2151impl EventEmitter<CodegenEvent> for Codegen {}
2152
2153impl Codegen {
2154    pub fn new(
2155        buffer: Model<MultiBuffer>,
2156        transform_range: Range<Anchor>,
2157        selected_ranges: Vec<Range<Anchor>>,
2158        initial_transaction_id: Option<TransactionId>,
2159        telemetry: Option<Arc<Telemetry>>,
2160        builder: Arc<PromptBuilder>,
2161        cx: &mut ModelContext<Self>,
2162    ) -> Self {
2163        let snapshot = buffer.read(cx).snapshot(cx);
2164
2165        let (old_buffer, _, _) = buffer
2166            .read(cx)
2167            .range_to_buffer_ranges(transform_range.clone(), cx)
2168            .pop()
2169            .unwrap();
2170        let old_buffer = cx.new_model(|cx| {
2171            let old_buffer = old_buffer.read(cx);
2172            let text = old_buffer.as_rope().clone();
2173            let line_ending = old_buffer.line_ending();
2174            let language = old_buffer.language().cloned();
2175            let language_registry = old_buffer.language_registry();
2176
2177            let mut buffer = Buffer::local_normalized(text, line_ending, cx);
2178            buffer.set_language(language, cx);
2179            if let Some(language_registry) = language_registry {
2180                buffer.set_language_registry(language_registry)
2181            }
2182            buffer
2183        });
2184
2185        Self {
2186            buffer: buffer.clone(),
2187            old_buffer,
2188            edit_position: None,
2189            snapshot,
2190            last_equal_ranges: Default::default(),
2191            transformation_transaction_id: None,
2192            status: CodegenStatus::Idle,
2193            generation: Task::ready(()),
2194            diff: Diff::default(),
2195            telemetry,
2196            _subscription: cx.subscribe(&buffer, Self::handle_buffer_event),
2197            initial_transaction_id,
2198            prompt_builder: builder,
2199            transform_range,
2200            selected_ranges,
2201        }
2202    }
2203
2204    fn handle_buffer_event(
2205        &mut self,
2206        _buffer: Model<MultiBuffer>,
2207        event: &multi_buffer::Event,
2208        cx: &mut ModelContext<Self>,
2209    ) {
2210        if let multi_buffer::Event::TransactionUndone { transaction_id } = event {
2211            if self.transformation_transaction_id == Some(*transaction_id) {
2212                self.transformation_transaction_id = None;
2213                self.generation = Task::ready(());
2214                cx.emit(CodegenEvent::Undone);
2215            }
2216        }
2217    }
2218
2219    pub fn last_equal_ranges(&self) -> &[Range<Anchor>] {
2220        &self.last_equal_ranges
2221    }
2222
2223    pub fn count_tokens(
2224        &self,
2225        user_prompt: String,
2226        assistant_panel_context: Option<LanguageModelRequest>,
2227        cx: &AppContext,
2228    ) -> BoxFuture<'static, Result<TokenCounts>> {
2229        if let Some(model) = LanguageModelRegistry::read_global(cx).active_model() {
2230            let request = self.build_request(user_prompt, assistant_panel_context.clone(), cx);
2231            match request {
2232                Ok(request) => {
2233                    let total_count = model.count_tokens(request.clone(), cx);
2234                    let assistant_panel_count = assistant_panel_context
2235                        .map(|context| model.count_tokens(context, cx))
2236                        .unwrap_or_else(|| future::ready(Ok(0)).boxed());
2237
2238                    async move {
2239                        Ok(TokenCounts {
2240                            total: total_count.await?,
2241                            assistant_panel: assistant_panel_count.await?,
2242                        })
2243                    }
2244                    .boxed()
2245                }
2246                Err(error) => futures::future::ready(Err(error)).boxed(),
2247            }
2248        } else {
2249            future::ready(Err(anyhow!("no active model"))).boxed()
2250        }
2251    }
2252
2253    pub fn start(
2254        &mut self,
2255        user_prompt: String,
2256        assistant_panel_context: Option<LanguageModelRequest>,
2257        cx: &mut ModelContext<Self>,
2258    ) -> Result<()> {
2259        let model = LanguageModelRegistry::read_global(cx)
2260            .active_model()
2261            .context("no active model")?;
2262
2263        if let Some(transformation_transaction_id) = self.transformation_transaction_id.take() {
2264            self.buffer.update(cx, |buffer, cx| {
2265                buffer.undo_transaction(transformation_transaction_id, cx);
2266            });
2267        }
2268
2269        self.edit_position = Some(self.transform_range.start.bias_right(&self.snapshot));
2270
2271        let telemetry_id = model.telemetry_id();
2272        let chunks: LocalBoxFuture<Result<BoxStream<Result<String>>>> =
2273            if user_prompt.trim().to_lowercase() == "delete" {
2274                async { Ok(stream::empty().boxed()) }.boxed_local()
2275            } else {
2276                let request = self.build_request(user_prompt, assistant_panel_context, cx)?;
2277
2278                let chunks =
2279                    cx.spawn(|_, cx| async move { model.stream_completion(request, &cx).await });
2280                async move { Ok(chunks.await?.boxed()) }.boxed_local()
2281            };
2282        self.handle_stream(telemetry_id, self.transform_range.clone(), chunks, cx);
2283        Ok(())
2284    }
2285
2286    fn build_request(
2287        &self,
2288        user_prompt: String,
2289        assistant_panel_context: Option<LanguageModelRequest>,
2290        cx: &AppContext,
2291    ) -> Result<LanguageModelRequest> {
2292        let buffer = self.buffer.read(cx).snapshot(cx);
2293        let language = buffer.language_at(self.transform_range.start);
2294        let language_name = if let Some(language) = language.as_ref() {
2295            if Arc::ptr_eq(language, &language::PLAIN_TEXT) {
2296                None
2297            } else {
2298                Some(language.name())
2299            }
2300        } else {
2301            None
2302        };
2303
2304        // Higher Temperature increases the randomness of model outputs.
2305        // If Markdown or No Language is Known, increase the randomness for more creative output
2306        // If Code, decrease temperature to get more deterministic outputs
2307        let temperature = if let Some(language) = language_name.clone() {
2308            if language.as_ref() == "Markdown" {
2309                1.0
2310            } else {
2311                0.5
2312            }
2313        } else {
2314            1.0
2315        };
2316
2317        let language_name = language_name.as_deref();
2318        let start = buffer.point_to_buffer_offset(self.transform_range.start);
2319        let end = buffer.point_to_buffer_offset(self.transform_range.end);
2320        let (buffer, range) = if let Some((start, end)) = start.zip(end) {
2321            let (start_buffer, start_buffer_offset) = start;
2322            let (end_buffer, end_buffer_offset) = end;
2323            if start_buffer.remote_id() == end_buffer.remote_id() {
2324                (start_buffer.clone(), start_buffer_offset..end_buffer_offset)
2325            } else {
2326                return Err(anyhow::anyhow!("invalid transformation range"));
2327            }
2328        } else {
2329            return Err(anyhow::anyhow!("invalid transformation range"));
2330        };
2331
2332        let selected_ranges = self
2333            .selected_ranges
2334            .iter()
2335            .map(|range| {
2336                let start = range.start.text_anchor.to_offset(&buffer);
2337                let end = range.end.text_anchor.to_offset(&buffer);
2338                start..end
2339            })
2340            .collect::<Vec<_>>();
2341
2342        let prompt = self
2343            .prompt_builder
2344            .generate_content_prompt(user_prompt, language_name, buffer, range, selected_ranges)
2345            .map_err(|e| anyhow::anyhow!("Failed to generate content prompt: {}", e))?;
2346
2347        let mut messages = Vec::new();
2348        if let Some(context_request) = assistant_panel_context {
2349            messages = context_request.messages;
2350        }
2351
2352        messages.push(LanguageModelRequestMessage {
2353            role: Role::User,
2354            content: prompt,
2355        });
2356
2357        Ok(LanguageModelRequest {
2358            messages,
2359            stop: vec!["|END|>".to_string()],
2360            temperature,
2361        })
2362    }
2363
2364    pub fn handle_stream(
2365        &mut self,
2366        model_telemetry_id: String,
2367        edit_range: Range<Anchor>,
2368        stream: impl 'static + Future<Output = Result<BoxStream<'static, Result<String>>>>,
2369        cx: &mut ModelContext<Self>,
2370    ) {
2371        let snapshot = self.snapshot.clone();
2372        let selected_text = snapshot
2373            .text_for_range(edit_range.start..edit_range.end)
2374            .collect::<Rope>();
2375
2376        let selection_start = edit_range.start.to_point(&snapshot);
2377
2378        // Start with the indentation of the first line in the selection
2379        let mut suggested_line_indent = snapshot
2380            .suggested_indents(selection_start.row..=selection_start.row, cx)
2381            .into_values()
2382            .next()
2383            .unwrap_or_else(|| snapshot.indent_size_for_line(MultiBufferRow(selection_start.row)));
2384
2385        // If the first line in the selection does not have indentation, check the following lines
2386        if suggested_line_indent.len == 0 && suggested_line_indent.kind == IndentKind::Space {
2387            for row in selection_start.row..=edit_range.end.to_point(&snapshot).row {
2388                let line_indent = snapshot.indent_size_for_line(MultiBufferRow(row));
2389                // Prefer tabs if a line in the selection uses tabs as indentation
2390                if line_indent.kind == IndentKind::Tab {
2391                    suggested_line_indent.kind = IndentKind::Tab;
2392                    break;
2393                }
2394            }
2395        }
2396
2397        let telemetry = self.telemetry.clone();
2398        self.diff = Diff::default();
2399        self.status = CodegenStatus::Pending;
2400        let mut edit_start = edit_range.start.to_offset(&snapshot);
2401        self.generation = cx.spawn(|this, mut cx| {
2402            async move {
2403                let chunks = stream.await;
2404                let generate = async {
2405                    let (mut diff_tx, mut diff_rx) = mpsc::channel(1);
2406                    let diff: Task<anyhow::Result<()>> =
2407                        cx.background_executor().spawn(async move {
2408                            let mut response_latency = None;
2409                            let request_start = Instant::now();
2410                            let diff = async {
2411                                let chunks = StripInvalidSpans::new(chunks?);
2412                                futures::pin_mut!(chunks);
2413                                let mut diff = StreamingDiff::new(selected_text.to_string());
2414                                let mut line_diff = LineDiff::default();
2415
2416                                while let Some(chunk) = chunks.next().await {
2417                                    if response_latency.is_none() {
2418                                        response_latency = Some(request_start.elapsed());
2419                                    }
2420                                    let chunk = chunk?;
2421                                    let char_ops = diff.push_new(&chunk);
2422                                    line_diff.push_char_operations(&char_ops, &selected_text);
2423                                    diff_tx
2424                                        .send((char_ops, line_diff.line_operations()))
2425                                        .await?;
2426                                }
2427
2428                                let char_ops = diff.finish();
2429                                line_diff.push_char_operations(&char_ops, &selected_text);
2430                                line_diff.finish(&selected_text);
2431                                diff_tx
2432                                    .send((char_ops, line_diff.line_operations()))
2433                                    .await?;
2434
2435                                anyhow::Ok(())
2436                            };
2437
2438                            let result = diff.await;
2439
2440                            let error_message =
2441                                result.as_ref().err().map(|error| error.to_string());
2442                            if let Some(telemetry) = telemetry {
2443                                telemetry.report_assistant_event(
2444                                    None,
2445                                    telemetry_events::AssistantKind::Inline,
2446                                    model_telemetry_id,
2447                                    response_latency,
2448                                    error_message,
2449                                );
2450                            }
2451
2452                            result?;
2453                            Ok(())
2454                        });
2455
2456                    while let Some((char_ops, line_diff)) = diff_rx.next().await {
2457                        this.update(&mut cx, |this, cx| {
2458                            this.last_equal_ranges.clear();
2459
2460                            let transaction = this.buffer.update(cx, |buffer, cx| {
2461                                // Avoid grouping assistant edits with user edits.
2462                                buffer.finalize_last_transaction(cx);
2463
2464                                buffer.start_transaction(cx);
2465                                buffer.edit(
2466                                    char_ops
2467                                        .into_iter()
2468                                        .filter_map(|operation| match operation {
2469                                            CharOperation::Insert { text } => {
2470                                                let edit_start = snapshot.anchor_after(edit_start);
2471                                                Some((edit_start..edit_start, text))
2472                                            }
2473                                            CharOperation::Delete { bytes } => {
2474                                                let edit_end = edit_start + bytes;
2475                                                let edit_range = snapshot.anchor_after(edit_start)
2476                                                    ..snapshot.anchor_before(edit_end);
2477                                                edit_start = edit_end;
2478                                                Some((edit_range, String::new()))
2479                                            }
2480                                            CharOperation::Keep { bytes } => {
2481                                                let edit_end = edit_start + bytes;
2482                                                let edit_range = snapshot.anchor_after(edit_start)
2483                                                    ..snapshot.anchor_before(edit_end);
2484                                                edit_start = edit_end;
2485                                                this.last_equal_ranges.push(edit_range);
2486                                                None
2487                                            }
2488                                        }),
2489                                    None,
2490                                    cx,
2491                                );
2492                                this.edit_position = Some(snapshot.anchor_after(edit_start));
2493
2494                                buffer.end_transaction(cx)
2495                            });
2496
2497                            if let Some(transaction) = transaction {
2498                                if let Some(first_transaction) = this.transformation_transaction_id
2499                                {
2500                                    // Group all assistant edits into the first transaction.
2501                                    this.buffer.update(cx, |buffer, cx| {
2502                                        buffer.merge_transactions(
2503                                            transaction,
2504                                            first_transaction,
2505                                            cx,
2506                                        )
2507                                    });
2508                                } else {
2509                                    this.transformation_transaction_id = Some(transaction);
2510                                    this.buffer.update(cx, |buffer, cx| {
2511                                        buffer.finalize_last_transaction(cx)
2512                                    });
2513                                }
2514                            }
2515
2516                            this.update_diff(edit_range.clone(), line_diff, cx);
2517
2518                            cx.notify();
2519                        })?;
2520                    }
2521
2522                    diff.await?;
2523
2524                    anyhow::Ok(())
2525                };
2526
2527                let result = generate.await;
2528                this.update(&mut cx, |this, cx| {
2529                    this.last_equal_ranges.clear();
2530                    if let Err(error) = result {
2531                        this.status = CodegenStatus::Error(error);
2532                    } else {
2533                        this.status = CodegenStatus::Done;
2534                    }
2535                    cx.emit(CodegenEvent::Finished);
2536                    cx.notify();
2537                })
2538                .ok();
2539            }
2540        });
2541        cx.notify();
2542    }
2543
2544    pub fn stop(&mut self, cx: &mut ModelContext<Self>) {
2545        self.last_equal_ranges.clear();
2546        if self.diff.is_empty() {
2547            self.status = CodegenStatus::Idle;
2548        } else {
2549            self.status = CodegenStatus::Done;
2550        }
2551        self.generation = Task::ready(());
2552        cx.emit(CodegenEvent::Finished);
2553        cx.notify();
2554    }
2555
2556    pub fn undo(&mut self, cx: &mut ModelContext<Self>) {
2557        self.buffer.update(cx, |buffer, cx| {
2558            if let Some(transaction_id) = self.transformation_transaction_id.take() {
2559                buffer.undo_transaction(transaction_id, cx);
2560                buffer.refresh_preview(cx);
2561            }
2562
2563            if let Some(transaction_id) = self.initial_transaction_id.take() {
2564                buffer.undo_transaction(transaction_id, cx);
2565                buffer.refresh_preview(cx);
2566            }
2567        });
2568    }
2569
2570    fn update_diff(
2571        &mut self,
2572        edit_range: Range<Anchor>,
2573        line_operations: Vec<LineOperation>,
2574        cx: &mut ModelContext<Self>,
2575    ) {
2576        let old_snapshot = self.snapshot.clone();
2577        let old_range = edit_range.to_point(&old_snapshot);
2578        let new_snapshot = self.buffer.read(cx).snapshot(cx);
2579        let new_range = edit_range.to_point(&new_snapshot);
2580
2581        let mut old_row = old_range.start.row;
2582        let mut new_row = new_range.start.row;
2583
2584        self.diff.deleted_row_ranges.clear();
2585        self.diff.inserted_row_ranges.clear();
2586        for operation in line_operations {
2587            match operation {
2588                LineOperation::Keep { lines } => {
2589                    old_row += lines;
2590                    new_row += lines;
2591                }
2592                LineOperation::Delete { lines } => {
2593                    let old_end_row = old_row + lines - 1;
2594                    let new_row = new_snapshot.anchor_before(Point::new(new_row, 0));
2595
2596                    if let Some((_, last_deleted_row_range)) =
2597                        self.diff.deleted_row_ranges.last_mut()
2598                    {
2599                        if *last_deleted_row_range.end() + 1 == old_row {
2600                            *last_deleted_row_range = *last_deleted_row_range.start()..=old_end_row;
2601                        } else {
2602                            self.diff
2603                                .deleted_row_ranges
2604                                .push((new_row, old_row..=old_end_row));
2605                        }
2606                    } else {
2607                        self.diff
2608                            .deleted_row_ranges
2609                            .push((new_row, old_row..=old_end_row));
2610                    }
2611
2612                    old_row += lines;
2613                }
2614                LineOperation::Insert { lines } => {
2615                    let new_end_row = new_row + lines - 1;
2616                    let start = new_snapshot.anchor_before(Point::new(new_row, 0));
2617                    let end = new_snapshot.anchor_before(Point::new(
2618                        new_end_row,
2619                        new_snapshot.line_len(MultiBufferRow(new_end_row)),
2620                    ));
2621                    self.diff.inserted_row_ranges.push(start..=end);
2622                    new_row += lines;
2623                }
2624            }
2625
2626            cx.notify();
2627        }
2628    }
2629}
2630
2631struct StripInvalidSpans<T> {
2632    stream: T,
2633    stream_done: bool,
2634    buffer: String,
2635    first_line: bool,
2636    line_end: bool,
2637    starts_with_code_block: bool,
2638}
2639
2640impl<T> StripInvalidSpans<T>
2641where
2642    T: Stream<Item = Result<String>>,
2643{
2644    fn new(stream: T) -> Self {
2645        Self {
2646            stream,
2647            stream_done: false,
2648            buffer: String::new(),
2649            first_line: true,
2650            line_end: false,
2651            starts_with_code_block: false,
2652        }
2653    }
2654}
2655
2656impl<T> Stream for StripInvalidSpans<T>
2657where
2658    T: Stream<Item = Result<String>>,
2659{
2660    type Item = Result<String>;
2661
2662    fn poll_next(self: Pin<&mut Self>, cx: &mut task::Context) -> Poll<Option<Self::Item>> {
2663        const CODE_BLOCK_DELIMITER: &str = "```";
2664        const CURSOR_SPAN: &str = "<|CURSOR|>";
2665
2666        let this = unsafe { self.get_unchecked_mut() };
2667        loop {
2668            if !this.stream_done {
2669                let mut stream = unsafe { Pin::new_unchecked(&mut this.stream) };
2670                match stream.as_mut().poll_next(cx) {
2671                    Poll::Ready(Some(Ok(chunk))) => {
2672                        this.buffer.push_str(&chunk);
2673                    }
2674                    Poll::Ready(Some(Err(error))) => return Poll::Ready(Some(Err(error))),
2675                    Poll::Ready(None) => {
2676                        this.stream_done = true;
2677                    }
2678                    Poll::Pending => return Poll::Pending,
2679                }
2680            }
2681
2682            let mut chunk = String::new();
2683            let mut consumed = 0;
2684            if !this.buffer.is_empty() {
2685                let mut lines = this.buffer.split('\n').enumerate().peekable();
2686                while let Some((line_ix, line)) = lines.next() {
2687                    if line_ix > 0 {
2688                        this.first_line = false;
2689                    }
2690
2691                    if this.first_line {
2692                        let trimmed_line = line.trim();
2693                        if lines.peek().is_some() {
2694                            if trimmed_line.starts_with(CODE_BLOCK_DELIMITER) {
2695                                consumed += line.len() + 1;
2696                                this.starts_with_code_block = true;
2697                                continue;
2698                            }
2699                        } else if trimmed_line.is_empty()
2700                            || prefixes(CODE_BLOCK_DELIMITER)
2701                                .any(|prefix| trimmed_line.starts_with(prefix))
2702                        {
2703                            break;
2704                        }
2705                    }
2706
2707                    let line_without_cursor = line.replace(CURSOR_SPAN, "");
2708                    if lines.peek().is_some() {
2709                        if this.line_end {
2710                            chunk.push('\n');
2711                        }
2712
2713                        chunk.push_str(&line_without_cursor);
2714                        this.line_end = true;
2715                        consumed += line.len() + 1;
2716                    } else if this.stream_done {
2717                        if !this.starts_with_code_block
2718                            || !line_without_cursor.trim().ends_with(CODE_BLOCK_DELIMITER)
2719                        {
2720                            if this.line_end {
2721                                chunk.push('\n');
2722                            }
2723
2724                            chunk.push_str(&line);
2725                        }
2726
2727                        consumed += line.len();
2728                    } else {
2729                        let trimmed_line = line.trim();
2730                        if trimmed_line.is_empty()
2731                            || prefixes(CURSOR_SPAN).any(|prefix| trimmed_line.ends_with(prefix))
2732                            || prefixes(CODE_BLOCK_DELIMITER)
2733                                .any(|prefix| trimmed_line.ends_with(prefix))
2734                        {
2735                            break;
2736                        } else {
2737                            if this.line_end {
2738                                chunk.push('\n');
2739                                this.line_end = false;
2740                            }
2741
2742                            chunk.push_str(&line_without_cursor);
2743                            consumed += line.len();
2744                        }
2745                    }
2746                }
2747            }
2748
2749            this.buffer = this.buffer.split_off(consumed);
2750            if !chunk.is_empty() {
2751                return Poll::Ready(Some(Ok(chunk)));
2752            } else if this.stream_done {
2753                return Poll::Ready(None);
2754            }
2755        }
2756    }
2757}
2758
2759fn prefixes(text: &str) -> impl Iterator<Item = &str> {
2760    (0..text.len() - 1).map(|ix| &text[..ix + 1])
2761}
2762
2763fn merge_ranges(ranges: &mut Vec<Range<Anchor>>, buffer: &MultiBufferSnapshot) {
2764    ranges.sort_unstable_by(|a, b| {
2765        a.start
2766            .cmp(&b.start, buffer)
2767            .then_with(|| b.end.cmp(&a.end, buffer))
2768    });
2769
2770    let mut ix = 0;
2771    while ix + 1 < ranges.len() {
2772        let b = ranges[ix + 1].clone();
2773        let a = &mut ranges[ix];
2774        if a.end.cmp(&b.start, buffer).is_gt() {
2775            if a.end.cmp(&b.end, buffer).is_lt() {
2776                a.end = b.end;
2777            }
2778            ranges.remove(ix + 1);
2779        } else {
2780            ix += 1;
2781        }
2782    }
2783}
2784
2785#[cfg(test)]
2786mod tests {
2787    use super::*;
2788    use futures::stream::{self};
2789    use serde::Serialize;
2790
2791    #[derive(Serialize)]
2792    pub struct DummyCompletionRequest {
2793        pub name: String,
2794    }
2795
2796    #[gpui::test]
2797    async fn test_strip_invalid_spans_from_codeblock() {
2798        assert_chunks("Lorem ipsum dolor", "Lorem ipsum dolor").await;
2799        assert_chunks("```\nLorem ipsum dolor", "Lorem ipsum dolor").await;
2800        assert_chunks("```\nLorem ipsum dolor\n```", "Lorem ipsum dolor").await;
2801        assert_chunks(
2802            "```html\n```js\nLorem ipsum dolor\n```\n```",
2803            "```js\nLorem ipsum dolor\n```",
2804        )
2805        .await;
2806        assert_chunks("``\nLorem ipsum dolor\n```", "``\nLorem ipsum dolor\n```").await;
2807        assert_chunks("Lorem<|CURSOR|> ipsum", "Lorem ipsum").await;
2808        assert_chunks("Lorem ipsum", "Lorem ipsum").await;
2809        assert_chunks("```\n<|CURSOR|>Lorem ipsum\n```", "Lorem ipsum").await;
2810
2811        async fn assert_chunks(text: &str, expected_text: &str) {
2812            for chunk_size in 1..=text.len() {
2813                let actual_text = StripInvalidSpans::new(chunks(text, chunk_size))
2814                    .map(|chunk| chunk.unwrap())
2815                    .collect::<String>()
2816                    .await;
2817                assert_eq!(
2818                    actual_text, expected_text,
2819                    "failed to strip invalid spans, chunk size: {}",
2820                    chunk_size
2821                );
2822            }
2823        }
2824
2825        fn chunks(text: &str, size: usize) -> impl Stream<Item = Result<String>> {
2826            stream::iter(
2827                text.chars()
2828                    .collect::<Vec<_>>()
2829                    .chunks(size)
2830                    .map(|chunk| Ok(chunk.iter().collect::<String>()))
2831                    .collect::<Vec<_>>(),
2832            )
2833        }
2834    }
2835}