1use crate::{
2 language_model_selector::{LanguageModelSelector, language_model_selector},
3 ui::{BurnModeTooltip, ModelSelectorTooltip},
4};
5use agent_settings::CompletionMode;
6use anyhow::Result;
7use assistant_slash_command::{SlashCommand, SlashCommandOutputSection, SlashCommandWorkingSet};
8use assistant_slash_commands::{DefaultSlashCommand, FileSlashCommand, selections_creases};
9use client::{proto, zed_urls};
10use collections::{BTreeSet, HashMap, HashSet, hash_map};
11use editor::{
12 Anchor, Editor, EditorEvent, MenuEditPredictionsPolicy, MultiBuffer, MultiBufferOffset,
13 MultiBufferSnapshot, RowExt, ToOffset as _, ToPoint,
14 actions::{MoveToEndOfLine, Newline, ShowCompletions},
15 display_map::{
16 BlockPlacement, BlockProperties, BlockStyle, Crease, CreaseMetadata, CustomBlockId, FoldId,
17 RenderBlock, ToDisplayPoint,
18 },
19 scroll::ScrollOffset,
20};
21use editor::{FoldPlaceholder, display_map::CreaseId};
22use fs::Fs;
23use futures::FutureExt;
24use gpui::{
25 Action, Animation, AnimationExt, AnyElement, App, ClipboardEntry, ClipboardItem, Empty, Entity,
26 EventEmitter, FocusHandle, Focusable, FontWeight, Global, InteractiveElement, IntoElement,
27 ParentElement, Pixels, Render, RenderImage, SharedString, Size, StatefulInteractiveElement,
28 Styled, Subscription, Task, WeakEntity, actions, div, img, point, prelude::*,
29 pulsating_between, size,
30};
31use language::{
32 BufferSnapshot, LspAdapterDelegate, ToOffset,
33 language_settings::{SoftWrap, all_language_settings},
34};
35use language_model::{
36 ConfigurationError, IconOrSvg, LanguageModelExt, LanguageModelImage, LanguageModelRegistry,
37 Role,
38};
39use multi_buffer::MultiBufferRow;
40use picker::{Picker, popover_menu::PickerPopoverMenu};
41use project::{Project, Worktree};
42use project::{ProjectPath, lsp_store::LocalLspAdapterDelegate};
43use rope::Point;
44use serde::{Deserialize, Serialize};
45use settings::{
46 LanguageModelProviderSetting, LanguageModelSelection, Settings, SettingsStore,
47 update_settings_file,
48};
49use std::{
50 any::TypeId,
51 cmp,
52 ops::Range,
53 path::{Path, PathBuf},
54 rc::Rc,
55 sync::Arc,
56 time::Duration,
57};
58use text::SelectionGoal;
59use ui::{
60 ButtonLike, CommonAnimationExt, Disclosure, ElevationIndex, KeyBinding, PopoverMenuHandle,
61 TintColor, Tooltip, prelude::*,
62};
63use util::{ResultExt, maybe};
64use workspace::{
65 CollaboratorId,
66 searchable::{Direction, SearchableItemHandle},
67};
68use workspace::{
69 Save, Toast, Workspace,
70 item::{self, FollowableItem, Item},
71 notifications::NotificationId,
72 pane,
73 searchable::{SearchEvent, SearchableItem},
74};
75use zed_actions::agent::{AddSelectionToThread, PasteRaw, ToggleModelSelector};
76
77use crate::CycleFavoriteModels;
78
79use crate::{slash_command::SlashCommandCompletionProvider, slash_command_picker};
80use assistant_text_thread::{
81 CacheStatus, Content, InvokedSlashCommandId, InvokedSlashCommandStatus, Message, MessageId,
82 MessageMetadata, MessageStatus, PendingSlashCommandStatus, TextThread, TextThreadEvent,
83 TextThreadId, ThoughtProcessOutputSection,
84};
85
86actions!(
87 assistant,
88 [
89 /// Sends the current message to the assistant.
90 Assist,
91 /// Confirms and executes the entered slash command.
92 ConfirmCommand,
93 /// Copies code from the assistant's response to the clipboard.
94 CopyCode,
95 /// Cycles between user and assistant message roles.
96 CycleMessageRole,
97 /// Inserts the selected text into the active editor.
98 InsertIntoEditor,
99 /// Splits the conversation at the current cursor position.
100 Split,
101 ]
102);
103
104/// Inserts files that were dragged and dropped into the assistant conversation.
105#[derive(PartialEq, Clone, Action)]
106#[action(namespace = assistant, no_json, no_register)]
107pub enum InsertDraggedFiles {
108 ProjectPaths(Vec<ProjectPath>),
109 ExternalFiles(Vec<PathBuf>),
110}
111
112#[derive(Copy, Clone, Debug, PartialEq)]
113struct ScrollPosition {
114 offset_before_cursor: gpui::Point<ScrollOffset>,
115 cursor: Anchor,
116}
117
118type MessageHeader = MessageMetadata;
119
120#[derive(Clone)]
121enum AssistError {
122 PaymentRequired,
123 Message(SharedString),
124}
125
126pub enum ThoughtProcessStatus {
127 Pending,
128 Completed,
129}
130
131pub trait AgentPanelDelegate {
132 fn active_text_thread_editor(
133 &self,
134 workspace: &mut Workspace,
135 window: &mut Window,
136 cx: &mut Context<Workspace>,
137 ) -> Option<Entity<TextThreadEditor>>;
138
139 fn open_local_text_thread(
140 &self,
141 workspace: &mut Workspace,
142 path: Arc<Path>,
143 window: &mut Window,
144 cx: &mut Context<Workspace>,
145 ) -> Task<Result<()>>;
146
147 fn open_remote_text_thread(
148 &self,
149 workspace: &mut Workspace,
150 text_thread_id: TextThreadId,
151 window: &mut Window,
152 cx: &mut Context<Workspace>,
153 ) -> Task<Result<Entity<TextThreadEditor>>>;
154
155 fn quote_selection(
156 &self,
157 workspace: &mut Workspace,
158 selection_ranges: Vec<Range<Anchor>>,
159 buffer: Entity<MultiBuffer>,
160 window: &mut Window,
161 cx: &mut Context<Workspace>,
162 );
163}
164
165impl dyn AgentPanelDelegate {
166 /// Returns the global [`AssistantPanelDelegate`], if it exists.
167 pub fn try_global(cx: &App) -> Option<Arc<Self>> {
168 cx.try_global::<GlobalAssistantPanelDelegate>()
169 .map(|global| global.0.clone())
170 }
171
172 /// Sets the global [`AssistantPanelDelegate`].
173 pub fn set_global(delegate: Arc<Self>, cx: &mut App) {
174 cx.set_global(GlobalAssistantPanelDelegate(delegate));
175 }
176}
177
178struct GlobalAssistantPanelDelegate(Arc<dyn AgentPanelDelegate>);
179
180impl Global for GlobalAssistantPanelDelegate {}
181
182pub struct TextThreadEditor {
183 text_thread: Entity<TextThread>,
184 fs: Arc<dyn Fs>,
185 slash_commands: Arc<SlashCommandWorkingSet>,
186 workspace: WeakEntity<Workspace>,
187 project: Entity<Project>,
188 lsp_adapter_delegate: Option<Arc<dyn LspAdapterDelegate>>,
189 editor: Entity<Editor>,
190 pending_thought_process: Option<(CreaseId, language::Anchor)>,
191 blocks: HashMap<MessageId, (MessageHeader, CustomBlockId)>,
192 image_blocks: HashSet<CustomBlockId>,
193 scroll_position: Option<ScrollPosition>,
194 remote_id: Option<workspace::ViewId>,
195 pending_slash_command_creases: HashMap<Range<language::Anchor>, CreaseId>,
196 invoked_slash_command_creases: HashMap<InvokedSlashCommandId, CreaseId>,
197 _subscriptions: Vec<Subscription>,
198 last_error: Option<AssistError>,
199 pub(crate) slash_menu_handle:
200 PopoverMenuHandle<Picker<slash_command_picker::SlashCommandDelegate>>,
201 // dragged_file_worktrees is used to keep references to worktrees that were added
202 // when the user drag/dropped an external file onto the context editor. Since
203 // the worktree is not part of the project panel, it would be dropped as soon as
204 // the file is opened. In order to keep the worktree alive for the duration of the
205 // context editor, we keep a reference here.
206 dragged_file_worktrees: Vec<Entity<Worktree>>,
207 language_model_selector: Entity<LanguageModelSelector>,
208 language_model_selector_menu_handle: PopoverMenuHandle<LanguageModelSelector>,
209}
210
211const MAX_TAB_TITLE_LEN: usize = 16;
212
213impl TextThreadEditor {
214 pub fn init(cx: &mut App) {
215 workspace::FollowableViewRegistry::register::<TextThreadEditor>(cx);
216
217 cx.observe_new(
218 |workspace: &mut Workspace, _window, _cx: &mut Context<Workspace>| {
219 workspace
220 .register_action(TextThreadEditor::quote_selection)
221 .register_action(TextThreadEditor::insert_selection)
222 .register_action(TextThreadEditor::copy_code)
223 .register_action(TextThreadEditor::handle_insert_dragged_files);
224 },
225 )
226 .detach();
227 }
228
229 pub fn for_text_thread(
230 text_thread: Entity<TextThread>,
231 fs: Arc<dyn Fs>,
232 workspace: WeakEntity<Workspace>,
233 project: Entity<Project>,
234 lsp_adapter_delegate: Option<Arc<dyn LspAdapterDelegate>>,
235 window: &mut Window,
236 cx: &mut Context<Self>,
237 ) -> Self {
238 let completion_provider = SlashCommandCompletionProvider::new(
239 text_thread.read(cx).slash_commands().clone(),
240 Some(cx.entity().downgrade()),
241 Some(workspace.clone()),
242 );
243
244 let editor = cx.new(|cx| {
245 let mut editor =
246 Editor::for_buffer(text_thread.read(cx).buffer().clone(), None, window, cx);
247 editor.disable_scrollbars_and_minimap(window, cx);
248 editor.set_soft_wrap_mode(SoftWrap::EditorWidth, cx);
249 editor.set_show_line_numbers(false, cx);
250 editor.set_show_git_diff_gutter(false, cx);
251 editor.set_show_code_actions(false, cx);
252 editor.set_show_runnables(false, cx);
253 editor.set_show_breakpoints(false, cx);
254 editor.set_show_wrap_guides(false, cx);
255 editor.set_show_indent_guides(false, cx);
256 editor.set_completion_provider(Some(Rc::new(completion_provider)));
257 editor.set_menu_edit_predictions_policy(MenuEditPredictionsPolicy::Never);
258 editor.set_collaboration_hub(Box::new(project.clone()));
259
260 let show_edit_predictions = all_language_settings(None, cx)
261 .edit_predictions
262 .enabled_in_text_threads;
263
264 editor.set_show_edit_predictions(Some(show_edit_predictions), window, cx);
265
266 editor
267 });
268
269 let _subscriptions = vec![
270 cx.observe(&text_thread, |_, _, cx| cx.notify()),
271 cx.subscribe_in(&text_thread, window, Self::handle_text_thread_event),
272 cx.subscribe_in(&editor, window, Self::handle_editor_event),
273 cx.subscribe_in(&editor, window, Self::handle_editor_search_event),
274 cx.observe_global_in::<SettingsStore>(window, Self::settings_changed),
275 ];
276
277 let slash_command_sections = text_thread
278 .read(cx)
279 .slash_command_output_sections()
280 .to_vec();
281 let thought_process_sections = text_thread
282 .read(cx)
283 .thought_process_output_sections()
284 .to_vec();
285 let slash_commands = text_thread.read(cx).slash_commands().clone();
286 let focus_handle = editor.read(cx).focus_handle(cx);
287
288 let mut this = Self {
289 text_thread,
290 slash_commands,
291 editor,
292 lsp_adapter_delegate,
293 blocks: Default::default(),
294 image_blocks: Default::default(),
295 scroll_position: None,
296 remote_id: None,
297 pending_thought_process: None,
298 fs: fs.clone(),
299 workspace,
300 project,
301 pending_slash_command_creases: HashMap::default(),
302 invoked_slash_command_creases: HashMap::default(),
303 _subscriptions,
304 last_error: None,
305 slash_menu_handle: Default::default(),
306 dragged_file_worktrees: Vec::new(),
307 language_model_selector: cx.new(|cx| {
308 language_model_selector(
309 |cx| LanguageModelRegistry::read_global(cx).default_model(),
310 {
311 let fs = fs.clone();
312 move |model, cx| {
313 update_settings_file(fs.clone(), cx, move |settings, _| {
314 let provider = model.provider_id().0.to_string();
315 let model = model.id().0.to_string();
316 settings.agent.get_or_insert_default().set_model(
317 LanguageModelSelection {
318 provider: LanguageModelProviderSetting(provider),
319 model,
320 },
321 )
322 });
323 }
324 },
325 {
326 let fs = fs.clone();
327 move |model, should_be_favorite, cx| {
328 crate::favorite_models::toggle_in_settings(
329 model,
330 should_be_favorite,
331 fs.clone(),
332 cx,
333 );
334 }
335 },
336 true, // Use popover styles for picker
337 focus_handle,
338 window,
339 cx,
340 )
341 }),
342 language_model_selector_menu_handle: PopoverMenuHandle::default(),
343 };
344 this.update_message_headers(cx);
345 this.update_image_blocks(cx);
346 this.insert_slash_command_output_sections(slash_command_sections, false, window, cx);
347 this.insert_thought_process_output_sections(
348 thought_process_sections
349 .into_iter()
350 .map(|section| (section, ThoughtProcessStatus::Completed)),
351 window,
352 cx,
353 );
354 this
355 }
356
357 fn settings_changed(&mut self, window: &mut Window, cx: &mut Context<Self>) {
358 self.editor.update(cx, |editor, cx| {
359 let show_edit_predictions = all_language_settings(None, cx)
360 .edit_predictions
361 .enabled_in_text_threads;
362
363 editor.set_show_edit_predictions(Some(show_edit_predictions), window, cx);
364 });
365 }
366
367 pub fn text_thread(&self) -> &Entity<TextThread> {
368 &self.text_thread
369 }
370
371 pub fn editor(&self) -> &Entity<Editor> {
372 &self.editor
373 }
374
375 pub fn insert_default_prompt(&mut self, window: &mut Window, cx: &mut Context<Self>) {
376 let command_name = DefaultSlashCommand.name();
377 self.editor.update(cx, |editor, cx| {
378 editor.insert(&format!("/{command_name}\n\n"), window, cx)
379 });
380 let command = self.text_thread.update(cx, |text_thread, cx| {
381 text_thread.reparse(cx);
382 text_thread.parsed_slash_commands()[0].clone()
383 });
384 self.run_command(
385 command.source_range,
386 &command.name,
387 &command.arguments,
388 false,
389 self.workspace.clone(),
390 window,
391 cx,
392 );
393 }
394
395 fn assist(&mut self, _: &Assist, window: &mut Window, cx: &mut Context<Self>) {
396 if self.sending_disabled(cx) {
397 return;
398 }
399 telemetry::event!("Agent Message Sent", agent = "zed-text");
400 self.send_to_model(window, cx);
401 }
402
403 fn send_to_model(&mut self, window: &mut Window, cx: &mut Context<Self>) {
404 self.last_error = None;
405 if let Some(user_message) = self
406 .text_thread
407 .update(cx, |text_thread, cx| text_thread.assist(cx))
408 {
409 let new_selection = {
410 let cursor = user_message
411 .start
412 .to_offset(self.text_thread.read(cx).buffer().read(cx));
413 MultiBufferOffset(cursor)..MultiBufferOffset(cursor)
414 };
415 self.editor.update(cx, |editor, cx| {
416 editor.change_selections(Default::default(), window, cx, |selections| {
417 selections.select_ranges([new_selection])
418 });
419 });
420 // Avoid scrolling to the new cursor position so the assistant's output is stable.
421 cx.defer_in(window, |this, _, _| this.scroll_position = None);
422 }
423
424 cx.notify();
425 }
426
427 fn cancel(
428 &mut self,
429 _: &editor::actions::Cancel,
430 _window: &mut Window,
431 cx: &mut Context<Self>,
432 ) {
433 self.last_error = None;
434
435 if self
436 .text_thread
437 .update(cx, |text_thread, cx| text_thread.cancel_last_assist(cx))
438 {
439 return;
440 }
441
442 cx.propagate();
443 }
444
445 fn cycle_message_role(
446 &mut self,
447 _: &CycleMessageRole,
448 _window: &mut Window,
449 cx: &mut Context<Self>,
450 ) {
451 let cursors = self.cursors(cx);
452 self.text_thread.update(cx, |text_thread, cx| {
453 let messages = text_thread
454 .messages_for_offsets(cursors.into_iter().map(|cursor| cursor.0), cx)
455 .into_iter()
456 .map(|message| message.id)
457 .collect();
458 text_thread.cycle_message_roles(messages, cx)
459 });
460 }
461
462 fn cursors(&self, cx: &mut App) -> Vec<MultiBufferOffset> {
463 let selections = self.editor.update(cx, |editor, cx| {
464 editor
465 .selections
466 .all::<MultiBufferOffset>(&editor.display_snapshot(cx))
467 });
468 selections
469 .into_iter()
470 .map(|selection| selection.head())
471 .collect()
472 }
473
474 pub fn insert_command(&mut self, name: &str, window: &mut Window, cx: &mut Context<Self>) {
475 if let Some(command) = self.slash_commands.command(name, cx) {
476 self.editor.update(cx, |editor, cx| {
477 editor.transact(window, cx, |editor, window, cx| {
478 editor.change_selections(Default::default(), window, cx, |s| s.try_cancel());
479 let snapshot = editor.buffer().read(cx).snapshot(cx);
480 let newest_cursor = editor
481 .selections
482 .newest::<Point>(&editor.display_snapshot(cx))
483 .head();
484 if newest_cursor.column > 0
485 || snapshot
486 .chars_at(newest_cursor)
487 .next()
488 .is_some_and(|ch| ch != '\n')
489 {
490 editor.move_to_end_of_line(
491 &MoveToEndOfLine {
492 stop_at_soft_wraps: false,
493 },
494 window,
495 cx,
496 );
497 editor.newline(&Newline, window, cx);
498 }
499
500 editor.insert(&format!("/{name}"), window, cx);
501 if command.accepts_arguments() {
502 editor.insert(" ", window, cx);
503 editor.show_completions(&ShowCompletions, window, cx);
504 }
505 });
506 });
507 if !command.requires_argument() {
508 self.confirm_command(&ConfirmCommand, window, cx);
509 }
510 }
511 }
512
513 pub fn confirm_command(
514 &mut self,
515 _: &ConfirmCommand,
516 window: &mut Window,
517 cx: &mut Context<Self>,
518 ) {
519 if self.editor.read(cx).has_visible_completions_menu() {
520 return;
521 }
522
523 let selections = self.editor.read(cx).selections.disjoint_anchors_arc();
524 let mut commands_by_range = HashMap::default();
525 let workspace = self.workspace.clone();
526 self.text_thread.update(cx, |text_thread, cx| {
527 text_thread.reparse(cx);
528 for selection in selections.iter() {
529 if let Some(command) =
530 text_thread.pending_command_for_position(selection.head().text_anchor, cx)
531 {
532 commands_by_range
533 .entry(command.source_range.clone())
534 .or_insert_with(|| command.clone());
535 }
536 }
537 });
538
539 if commands_by_range.is_empty() {
540 cx.propagate();
541 } else {
542 for command in commands_by_range.into_values() {
543 self.run_command(
544 command.source_range,
545 &command.name,
546 &command.arguments,
547 true,
548 workspace.clone(),
549 window,
550 cx,
551 );
552 }
553 cx.stop_propagation();
554 }
555 }
556
557 pub fn run_command(
558 &mut self,
559 command_range: Range<language::Anchor>,
560 name: &str,
561 arguments: &[String],
562 ensure_trailing_newline: bool,
563 workspace: WeakEntity<Workspace>,
564 window: &mut Window,
565 cx: &mut Context<Self>,
566 ) {
567 if let Some(command) = self.slash_commands.command(name, cx) {
568 let text_thread = self.text_thread.read(cx);
569 let sections = text_thread
570 .slash_command_output_sections()
571 .iter()
572 .filter(|section| section.is_valid(text_thread.buffer().read(cx)))
573 .cloned()
574 .collect::<Vec<_>>();
575 let snapshot = text_thread.buffer().read(cx).snapshot();
576 let output = command.run(
577 arguments,
578 §ions,
579 snapshot,
580 workspace,
581 self.lsp_adapter_delegate.clone(),
582 window,
583 cx,
584 );
585 self.text_thread.update(cx, |text_thread, cx| {
586 text_thread.insert_command_output(
587 command_range,
588 name,
589 output,
590 ensure_trailing_newline,
591 cx,
592 )
593 });
594 }
595 }
596
597 fn handle_text_thread_event(
598 &mut self,
599 _: &Entity<TextThread>,
600 event: &TextThreadEvent,
601 window: &mut Window,
602 cx: &mut Context<Self>,
603 ) {
604 let text_thread_editor = cx.entity().downgrade();
605
606 match event {
607 TextThreadEvent::MessagesEdited => {
608 self.update_message_headers(cx);
609 self.update_image_blocks(cx);
610 self.text_thread.update(cx, |text_thread, cx| {
611 text_thread.save(Some(Duration::from_millis(500)), self.fs.clone(), cx);
612 });
613 }
614 TextThreadEvent::SummaryChanged => {
615 cx.emit(EditorEvent::TitleChanged);
616 self.text_thread.update(cx, |text_thread, cx| {
617 text_thread.save(Some(Duration::from_millis(500)), self.fs.clone(), cx);
618 });
619 }
620 TextThreadEvent::SummaryGenerated => {}
621 TextThreadEvent::PathChanged { .. } => {}
622 TextThreadEvent::StartedThoughtProcess(range) => {
623 let creases = self.insert_thought_process_output_sections(
624 [(
625 ThoughtProcessOutputSection {
626 range: range.clone(),
627 },
628 ThoughtProcessStatus::Pending,
629 )],
630 window,
631 cx,
632 );
633 self.pending_thought_process = Some((creases[0], range.start));
634 }
635 TextThreadEvent::EndedThoughtProcess(end) => {
636 if let Some((crease_id, start)) = self.pending_thought_process.take() {
637 self.editor.update(cx, |editor, cx| {
638 let multi_buffer_snapshot = editor.buffer().read(cx).snapshot(cx);
639 let start_anchor =
640 multi_buffer_snapshot.as_singleton_anchor(start).unwrap();
641
642 editor.display_map.update(cx, |display_map, cx| {
643 display_map.unfold_intersecting(
644 vec![start_anchor..start_anchor],
645 true,
646 cx,
647 );
648 });
649 editor.remove_creases(vec![crease_id], cx);
650 });
651 self.insert_thought_process_output_sections(
652 [(
653 ThoughtProcessOutputSection { range: start..*end },
654 ThoughtProcessStatus::Completed,
655 )],
656 window,
657 cx,
658 );
659 }
660 }
661 TextThreadEvent::StreamedCompletion => {
662 self.editor.update(cx, |editor, cx| {
663 if let Some(scroll_position) = self.scroll_position {
664 let snapshot = editor.snapshot(window, cx);
665 let cursor_point = scroll_position.cursor.to_display_point(&snapshot);
666 let scroll_top =
667 cursor_point.row().as_f64() - scroll_position.offset_before_cursor.y;
668 editor.set_scroll_position(
669 point(scroll_position.offset_before_cursor.x, scroll_top),
670 window,
671 cx,
672 );
673 }
674 });
675 }
676 TextThreadEvent::ParsedSlashCommandsUpdated { removed, updated } => {
677 self.editor.update(cx, |editor, cx| {
678 let buffer = editor.buffer().read(cx).snapshot(cx);
679 let (&excerpt_id, _, _) = buffer.as_singleton().unwrap();
680
681 editor.remove_creases(
682 removed
683 .iter()
684 .filter_map(|range| self.pending_slash_command_creases.remove(range)),
685 cx,
686 );
687
688 let crease_ids = editor.insert_creases(
689 updated.iter().map(|command| {
690 let workspace = self.workspace.clone();
691 let confirm_command = Arc::new({
692 let text_thread_editor = text_thread_editor.clone();
693 let command = command.clone();
694 move |window: &mut Window, cx: &mut App| {
695 text_thread_editor
696 .update(cx, |text_thread_editor, cx| {
697 text_thread_editor.run_command(
698 command.source_range.clone(),
699 &command.name,
700 &command.arguments,
701 false,
702 workspace.clone(),
703 window,
704 cx,
705 );
706 })
707 .ok();
708 }
709 });
710 let placeholder = FoldPlaceholder {
711 render: Arc::new(move |_, _, _| Empty.into_any()),
712 ..Default::default()
713 };
714 let render_toggle = {
715 let confirm_command = confirm_command.clone();
716 let command = command.clone();
717 move |row, _, _, _window: &mut Window, _cx: &mut App| {
718 render_pending_slash_command_gutter_decoration(
719 row,
720 &command.status,
721 confirm_command.clone(),
722 )
723 }
724 };
725 let render_trailer = {
726 move |_row, _unfold, _window: &mut Window, _cx: &mut App| {
727 Empty.into_any()
728 }
729 };
730
731 let range = buffer
732 .anchor_range_in_excerpt(excerpt_id, command.source_range.clone())
733 .unwrap();
734 Crease::inline(range, placeholder, render_toggle, render_trailer)
735 }),
736 cx,
737 );
738
739 self.pending_slash_command_creases.extend(
740 updated
741 .iter()
742 .map(|command| command.source_range.clone())
743 .zip(crease_ids),
744 );
745 })
746 }
747 TextThreadEvent::InvokedSlashCommandChanged { command_id } => {
748 self.update_invoked_slash_command(*command_id, window, cx);
749 }
750 TextThreadEvent::SlashCommandOutputSectionAdded { section } => {
751 self.insert_slash_command_output_sections([section.clone()], false, window, cx);
752 }
753 TextThreadEvent::Operation(_) => {}
754 TextThreadEvent::ShowAssistError(error_message) => {
755 self.last_error = Some(AssistError::Message(error_message.clone()));
756 }
757 TextThreadEvent::ShowPaymentRequiredError => {
758 self.last_error = Some(AssistError::PaymentRequired);
759 }
760 }
761 }
762
763 fn update_invoked_slash_command(
764 &mut self,
765 command_id: InvokedSlashCommandId,
766 window: &mut Window,
767 cx: &mut Context<Self>,
768 ) {
769 if let Some(invoked_slash_command) =
770 self.text_thread.read(cx).invoked_slash_command(&command_id)
771 && let InvokedSlashCommandStatus::Finished = invoked_slash_command.status
772 {
773 let run_commands_in_ranges = invoked_slash_command.run_commands_in_ranges.clone();
774 for range in run_commands_in_ranges {
775 let commands = self.text_thread.update(cx, |text_thread, cx| {
776 text_thread.reparse(cx);
777 text_thread
778 .pending_commands_for_range(range.clone(), cx)
779 .to_vec()
780 });
781
782 for command in commands {
783 self.run_command(
784 command.source_range,
785 &command.name,
786 &command.arguments,
787 false,
788 self.workspace.clone(),
789 window,
790 cx,
791 );
792 }
793 }
794 }
795
796 self.editor.update(cx, |editor, cx| {
797 if let Some(invoked_slash_command) =
798 self.text_thread.read(cx).invoked_slash_command(&command_id)
799 {
800 if let InvokedSlashCommandStatus::Finished = invoked_slash_command.status {
801 let buffer = editor.buffer().read(cx).snapshot(cx);
802 let (&excerpt_id, _buffer_id, _buffer_snapshot) =
803 buffer.as_singleton().unwrap();
804
805 let range = buffer
806 .anchor_range_in_excerpt(excerpt_id, invoked_slash_command.range.clone())
807 .unwrap();
808 editor.remove_folds_with_type(
809 &[range],
810 TypeId::of::<PendingSlashCommand>(),
811 false,
812 cx,
813 );
814
815 editor.remove_creases(
816 HashSet::from_iter(self.invoked_slash_command_creases.remove(&command_id)),
817 cx,
818 );
819 } else if let hash_map::Entry::Vacant(entry) =
820 self.invoked_slash_command_creases.entry(command_id)
821 {
822 let buffer = editor.buffer().read(cx).snapshot(cx);
823 let (&excerpt_id, _buffer_id, _buffer_snapshot) =
824 buffer.as_singleton().unwrap();
825 let context = self.text_thread.downgrade();
826 let range = buffer
827 .anchor_range_in_excerpt(excerpt_id, invoked_slash_command.range.clone())
828 .unwrap();
829 let crease = Crease::inline(
830 range,
831 invoked_slash_command_fold_placeholder(command_id, context),
832 fold_toggle("invoked-slash-command"),
833 |_row, _folded, _window, _cx| Empty.into_any(),
834 );
835 let crease_ids = editor.insert_creases([crease.clone()], cx);
836 editor.fold_creases(vec![crease], false, window, cx);
837 entry.insert(crease_ids[0]);
838 } else {
839 cx.notify()
840 }
841 } else {
842 editor.remove_creases(
843 HashSet::from_iter(self.invoked_slash_command_creases.remove(&command_id)),
844 cx,
845 );
846 cx.notify();
847 };
848 });
849 }
850
851 fn insert_thought_process_output_sections(
852 &mut self,
853 sections: impl IntoIterator<
854 Item = (
855 ThoughtProcessOutputSection<language::Anchor>,
856 ThoughtProcessStatus,
857 ),
858 >,
859 window: &mut Window,
860 cx: &mut Context<Self>,
861 ) -> Vec<CreaseId> {
862 self.editor.update(cx, |editor, cx| {
863 let buffer = editor.buffer().read(cx).snapshot(cx);
864 let excerpt_id = *buffer.as_singleton().unwrap().0;
865 let mut buffer_rows_to_fold = BTreeSet::new();
866 let mut creases = Vec::new();
867 for (section, status) in sections {
868 let range = buffer
869 .anchor_range_in_excerpt(excerpt_id, section.range)
870 .unwrap();
871 let buffer_row = MultiBufferRow(range.start.to_point(&buffer).row);
872 buffer_rows_to_fold.insert(buffer_row);
873 creases.push(
874 Crease::inline(
875 range,
876 FoldPlaceholder {
877 render: render_thought_process_fold_icon_button(
878 cx.entity().downgrade(),
879 status,
880 ),
881 merge_adjacent: false,
882 ..Default::default()
883 },
884 render_slash_command_output_toggle,
885 |_, _, _, _| Empty.into_any_element(),
886 )
887 .with_metadata(CreaseMetadata {
888 icon_path: SharedString::from(IconName::Ai.path()),
889 label: "Thinking Process".into(),
890 }),
891 );
892 }
893
894 let creases = editor.insert_creases(creases, cx);
895
896 for buffer_row in buffer_rows_to_fold.into_iter().rev() {
897 editor.fold_at(buffer_row, window, cx);
898 }
899
900 creases
901 })
902 }
903
904 fn insert_slash_command_output_sections(
905 &mut self,
906 sections: impl IntoIterator<Item = SlashCommandOutputSection<language::Anchor>>,
907 expand_result: bool,
908 window: &mut Window,
909 cx: &mut Context<Self>,
910 ) {
911 self.editor.update(cx, |editor, cx| {
912 let buffer = editor.buffer().read(cx).snapshot(cx);
913 let excerpt_id = *buffer.as_singleton().unwrap().0;
914 let mut buffer_rows_to_fold = BTreeSet::new();
915 let mut creases = Vec::new();
916 for section in sections {
917 let range = buffer
918 .anchor_range_in_excerpt(excerpt_id, section.range)
919 .unwrap();
920 let buffer_row = MultiBufferRow(range.start.to_point(&buffer).row);
921 buffer_rows_to_fold.insert(buffer_row);
922 creases.push(
923 Crease::inline(
924 range,
925 FoldPlaceholder {
926 render: render_fold_icon_button(
927 cx.entity().downgrade(),
928 section.icon.path().into(),
929 section.label.clone(),
930 ),
931 merge_adjacent: false,
932 ..Default::default()
933 },
934 render_slash_command_output_toggle,
935 |_, _, _, _| Empty.into_any_element(),
936 )
937 .with_metadata(CreaseMetadata {
938 icon_path: section.icon.path().into(),
939 label: section.label,
940 }),
941 );
942 }
943
944 editor.insert_creases(creases, cx);
945
946 if expand_result {
947 buffer_rows_to_fold.clear();
948 }
949 for buffer_row in buffer_rows_to_fold.into_iter().rev() {
950 editor.fold_at(buffer_row, window, cx);
951 }
952 });
953 }
954
955 fn handle_editor_event(
956 &mut self,
957 _: &Entity<Editor>,
958 event: &EditorEvent,
959 window: &mut Window,
960 cx: &mut Context<Self>,
961 ) {
962 match event {
963 EditorEvent::ScrollPositionChanged { autoscroll, .. } => {
964 let cursor_scroll_position = self.cursor_scroll_position(window, cx);
965 if *autoscroll {
966 self.scroll_position = cursor_scroll_position;
967 } else if self.scroll_position != cursor_scroll_position {
968 self.scroll_position = None;
969 }
970 }
971 EditorEvent::SelectionsChanged { .. } => {
972 self.scroll_position = self.cursor_scroll_position(window, cx);
973 }
974 _ => {}
975 }
976 cx.emit(event.clone());
977 }
978
979 fn handle_editor_search_event(
980 &mut self,
981 _: &Entity<Editor>,
982 event: &SearchEvent,
983 _window: &mut Window,
984 cx: &mut Context<Self>,
985 ) {
986 cx.emit(event.clone());
987 }
988
989 fn cursor_scroll_position(
990 &self,
991 window: &mut Window,
992 cx: &mut Context<Self>,
993 ) -> Option<ScrollPosition> {
994 self.editor.update(cx, |editor, cx| {
995 let snapshot = editor.snapshot(window, cx);
996 let cursor = editor.selections.newest_anchor().head();
997 let cursor_row = cursor
998 .to_display_point(&snapshot.display_snapshot)
999 .row()
1000 .as_f64();
1001 let scroll_position = editor
1002 .scroll_manager
1003 .anchor()
1004 .scroll_position(&snapshot.display_snapshot);
1005
1006 let scroll_bottom = scroll_position.y + editor.visible_line_count().unwrap_or(0.);
1007 if (scroll_position.y..scroll_bottom).contains(&cursor_row) {
1008 Some(ScrollPosition {
1009 cursor,
1010 offset_before_cursor: point(scroll_position.x, cursor_row - scroll_position.y),
1011 })
1012 } else {
1013 None
1014 }
1015 })
1016 }
1017
1018 fn esc_kbd(cx: &App) -> Div {
1019 let colors = cx.theme().colors().clone();
1020
1021 h_flex()
1022 .items_center()
1023 .gap_1()
1024 .font(theme::ThemeSettings::get_global(cx).buffer_font.clone())
1025 .text_size(TextSize::XSmall.rems(cx))
1026 .text_color(colors.text_muted)
1027 .child("Press")
1028 .child(
1029 h_flex()
1030 .rounded_sm()
1031 .px_1()
1032 .mr_0p5()
1033 .border_1()
1034 .border_color(colors.border_variant.alpha(0.6))
1035 .bg(colors.element_background.alpha(0.6))
1036 .child("esc"),
1037 )
1038 .child("to cancel")
1039 }
1040
1041 fn update_message_headers(&mut self, cx: &mut Context<Self>) {
1042 self.editor.update(cx, |editor, cx| {
1043 let buffer = editor.buffer().read(cx).snapshot(cx);
1044
1045 let excerpt_id = *buffer.as_singleton().unwrap().0;
1046 let mut old_blocks = std::mem::take(&mut self.blocks);
1047 let mut blocks_to_remove: HashMap<_, _> = old_blocks
1048 .iter()
1049 .map(|(message_id, (_, block_id))| (*message_id, *block_id))
1050 .collect();
1051 let mut blocks_to_replace: HashMap<_, RenderBlock> = Default::default();
1052
1053 let render_block = |message: MessageMetadata| -> RenderBlock {
1054 Arc::new({
1055 let text_thread = self.text_thread.clone();
1056
1057 move |cx| {
1058 let message_id = MessageId(message.timestamp);
1059 let llm_loading = message.role == Role::Assistant
1060 && message.status == MessageStatus::Pending;
1061
1062 let (label, spinner, note) = match message.role {
1063 Role::User => (
1064 Label::new("You").color(Color::Default).into_any_element(),
1065 None,
1066 None,
1067 ),
1068 Role::Assistant => {
1069 let base_label = Label::new("Agent").color(Color::Info);
1070 let mut spinner = None;
1071 let mut note = None;
1072 let animated_label = if llm_loading {
1073 base_label
1074 .with_animation(
1075 "pulsating-label",
1076 Animation::new(Duration::from_secs(2))
1077 .repeat()
1078 .with_easing(pulsating_between(0.4, 0.8)),
1079 |label, delta| label.alpha(delta),
1080 )
1081 .into_any_element()
1082 } else {
1083 base_label.into_any_element()
1084 };
1085 if llm_loading {
1086 spinner = Some(
1087 Icon::new(IconName::ArrowCircle)
1088 .size(IconSize::XSmall)
1089 .color(Color::Info)
1090 .with_rotate_animation(2)
1091 .into_any_element(),
1092 );
1093 note = Some(Self::esc_kbd(cx).into_any_element());
1094 }
1095 (animated_label, spinner, note)
1096 }
1097 Role::System => (
1098 Label::new("System")
1099 .color(Color::Warning)
1100 .into_any_element(),
1101 None,
1102 None,
1103 ),
1104 };
1105
1106 let sender = h_flex()
1107 .items_center()
1108 .gap_2p5()
1109 .child(
1110 ButtonLike::new("role")
1111 .style(ButtonStyle::Filled)
1112 .child(
1113 h_flex()
1114 .items_center()
1115 .gap_1p5()
1116 .child(label)
1117 .children(spinner),
1118 )
1119 .tooltip(|_window, cx| {
1120 Tooltip::with_meta(
1121 "Toggle message role",
1122 None,
1123 "Available roles: You (User), Agent, System",
1124 cx,
1125 )
1126 })
1127 .on_click({
1128 let text_thread = text_thread.clone();
1129 move |_, _window, cx| {
1130 text_thread.update(cx, |text_thread, cx| {
1131 text_thread.cycle_message_roles(
1132 HashSet::from_iter(Some(message_id)),
1133 cx,
1134 )
1135 })
1136 }
1137 }),
1138 )
1139 .children(note);
1140
1141 h_flex()
1142 .id(("message_header", message_id.as_u64()))
1143 .pl(cx.margins.gutter.full_width())
1144 .h_11()
1145 .w_full()
1146 .relative()
1147 .gap_1p5()
1148 .child(sender)
1149 .children(match &message.cache {
1150 Some(cache) if cache.is_final_anchor => match cache.status {
1151 CacheStatus::Cached => Some(
1152 div()
1153 .id("cached")
1154 .child(
1155 Icon::new(IconName::DatabaseZap)
1156 .size(IconSize::XSmall)
1157 .color(Color::Hint),
1158 )
1159 .tooltip(|_window, cx| {
1160 Tooltip::with_meta(
1161 "Context Cached",
1162 None,
1163 "Large messages cached to optimize performance",
1164 cx,
1165 )
1166 })
1167 .into_any_element(),
1168 ),
1169 CacheStatus::Pending => Some(
1170 div()
1171 .child(
1172 Icon::new(IconName::Ellipsis)
1173 .size(IconSize::XSmall)
1174 .color(Color::Hint),
1175 )
1176 .into_any_element(),
1177 ),
1178 },
1179 _ => None,
1180 })
1181 .children(match &message.status {
1182 MessageStatus::Error(error) => Some(
1183 Button::new("show-error", "Error")
1184 .color(Color::Error)
1185 .selected_label_color(Color::Error)
1186 .selected_icon_color(Color::Error)
1187 .icon(IconName::XCircle)
1188 .icon_color(Color::Error)
1189 .icon_size(IconSize::XSmall)
1190 .icon_position(IconPosition::Start)
1191 .tooltip(Tooltip::text("View Details"))
1192 .on_click({
1193 let text_thread = text_thread.clone();
1194 let error = error.clone();
1195 move |_, _window, cx| {
1196 text_thread.update(cx, |_, cx| {
1197 cx.emit(TextThreadEvent::ShowAssistError(
1198 error.clone(),
1199 ));
1200 });
1201 }
1202 })
1203 .into_any_element(),
1204 ),
1205 MessageStatus::Canceled => Some(
1206 h_flex()
1207 .gap_1()
1208 .items_center()
1209 .child(
1210 Icon::new(IconName::XCircle)
1211 .color(Color::Disabled)
1212 .size(IconSize::XSmall),
1213 )
1214 .child(
1215 Label::new("Canceled")
1216 .size(LabelSize::Small)
1217 .color(Color::Disabled),
1218 )
1219 .into_any_element(),
1220 ),
1221 _ => None,
1222 })
1223 .into_any_element()
1224 }
1225 })
1226 };
1227 let create_block_properties = |message: &Message| BlockProperties {
1228 height: Some(2),
1229 style: BlockStyle::Sticky,
1230 placement: BlockPlacement::Above(
1231 buffer
1232 .anchor_in_excerpt(excerpt_id, message.anchor_range.start)
1233 .unwrap(),
1234 ),
1235 priority: usize::MAX,
1236 render: render_block(MessageMetadata::from(message)),
1237 };
1238 let mut new_blocks = vec![];
1239 let mut block_index_to_message = vec![];
1240 for message in self.text_thread.read(cx).messages(cx) {
1241 if blocks_to_remove.remove(&message.id).is_some() {
1242 // This is an old message that we might modify.
1243 let Some((meta, block_id)) = old_blocks.get_mut(&message.id) else {
1244 debug_assert!(
1245 false,
1246 "old_blocks should contain a message_id we've just removed."
1247 );
1248 continue;
1249 };
1250 // Should we modify it?
1251 let message_meta = MessageMetadata::from(&message);
1252 if meta != &message_meta {
1253 blocks_to_replace.insert(*block_id, render_block(message_meta.clone()));
1254 *meta = message_meta;
1255 }
1256 } else {
1257 // This is a new message.
1258 new_blocks.push(create_block_properties(&message));
1259 block_index_to_message.push((message.id, MessageMetadata::from(&message)));
1260 }
1261 }
1262 editor.replace_blocks(blocks_to_replace, None, cx);
1263 editor.remove_blocks(blocks_to_remove.into_values().collect(), None, cx);
1264
1265 let ids = editor.insert_blocks(new_blocks, None, cx);
1266 old_blocks.extend(ids.into_iter().zip(block_index_to_message).map(
1267 |(block_id, (message_id, message_meta))| (message_id, (message_meta, block_id)),
1268 ));
1269 self.blocks = old_blocks;
1270 });
1271 }
1272
1273 /// Returns either the selected text, or the content of the Markdown code
1274 /// block surrounding the cursor.
1275 fn get_selection_or_code_block(
1276 context_editor_view: &Entity<TextThreadEditor>,
1277 cx: &mut Context<Workspace>,
1278 ) -> Option<(String, bool)> {
1279 const CODE_FENCE_DELIMITER: &str = "```";
1280
1281 let text_thread_editor = context_editor_view.read(cx).editor.clone();
1282 text_thread_editor.update(cx, |text_thread_editor, cx| {
1283 let display_map = text_thread_editor.display_snapshot(cx);
1284 if text_thread_editor
1285 .selections
1286 .newest::<Point>(&display_map)
1287 .is_empty()
1288 {
1289 let snapshot = text_thread_editor.buffer().read(cx).snapshot(cx);
1290 let (_, _, snapshot) = snapshot.as_singleton()?;
1291
1292 let head = text_thread_editor
1293 .selections
1294 .newest::<Point>(&display_map)
1295 .head();
1296 let offset = snapshot.point_to_offset(head);
1297
1298 let surrounding_code_block_range = find_surrounding_code_block(snapshot, offset)?;
1299 let mut text = snapshot
1300 .text_for_range(surrounding_code_block_range)
1301 .collect::<String>();
1302
1303 // If there is no newline trailing the closing three-backticks, then
1304 // tree-sitter-md extends the range of the content node to include
1305 // the backticks.
1306 if text.ends_with(CODE_FENCE_DELIMITER) {
1307 text.drain((text.len() - CODE_FENCE_DELIMITER.len())..);
1308 }
1309
1310 (!text.is_empty()).then_some((text, true))
1311 } else {
1312 let selection = text_thread_editor.selections.newest_adjusted(&display_map);
1313 let buffer = text_thread_editor.buffer().read(cx).snapshot(cx);
1314 let selected_text = buffer.text_for_range(selection.range()).collect::<String>();
1315
1316 (!selected_text.is_empty()).then_some((selected_text, false))
1317 }
1318 })
1319 }
1320
1321 pub fn insert_selection(
1322 workspace: &mut Workspace,
1323 _: &InsertIntoEditor,
1324 window: &mut Window,
1325 cx: &mut Context<Workspace>,
1326 ) {
1327 let Some(agent_panel_delegate) = <dyn AgentPanelDelegate>::try_global(cx) else {
1328 return;
1329 };
1330 let Some(context_editor_view) =
1331 agent_panel_delegate.active_text_thread_editor(workspace, window, cx)
1332 else {
1333 return;
1334 };
1335 let Some(active_editor_view) = workspace
1336 .active_item(cx)
1337 .and_then(|item| item.act_as::<Editor>(cx))
1338 else {
1339 return;
1340 };
1341
1342 if let Some((text, _)) = Self::get_selection_or_code_block(&context_editor_view, cx) {
1343 active_editor_view.update(cx, |editor, cx| {
1344 editor.insert(&text, window, cx);
1345 editor.focus_handle(cx).focus(window, cx);
1346 })
1347 }
1348 }
1349
1350 pub fn copy_code(
1351 workspace: &mut Workspace,
1352 _: &CopyCode,
1353 window: &mut Window,
1354 cx: &mut Context<Workspace>,
1355 ) {
1356 let result = maybe!({
1357 let agent_panel_delegate = <dyn AgentPanelDelegate>::try_global(cx)?;
1358 let context_editor_view =
1359 agent_panel_delegate.active_text_thread_editor(workspace, window, cx)?;
1360 Self::get_selection_or_code_block(&context_editor_view, cx)
1361 });
1362 let Some((text, is_code_block)) = result else {
1363 return;
1364 };
1365
1366 cx.write_to_clipboard(ClipboardItem::new_string(text));
1367
1368 struct CopyToClipboardToast;
1369 workspace.show_toast(
1370 Toast::new(
1371 NotificationId::unique::<CopyToClipboardToast>(),
1372 format!(
1373 "{} copied to clipboard.",
1374 if is_code_block {
1375 "Code block"
1376 } else {
1377 "Selection"
1378 }
1379 ),
1380 )
1381 .autohide(),
1382 cx,
1383 );
1384 }
1385
1386 pub fn handle_insert_dragged_files(
1387 workspace: &mut Workspace,
1388 action: &InsertDraggedFiles,
1389 window: &mut Window,
1390 cx: &mut Context<Workspace>,
1391 ) {
1392 let Some(agent_panel_delegate) = <dyn AgentPanelDelegate>::try_global(cx) else {
1393 return;
1394 };
1395 let Some(context_editor_view) =
1396 agent_panel_delegate.active_text_thread_editor(workspace, window, cx)
1397 else {
1398 return;
1399 };
1400
1401 let project = context_editor_view.read(cx).project.clone();
1402
1403 let paths = match action {
1404 InsertDraggedFiles::ProjectPaths(paths) => Task::ready((paths.clone(), vec![])),
1405 InsertDraggedFiles::ExternalFiles(paths) => {
1406 let tasks = paths
1407 .clone()
1408 .into_iter()
1409 .map(|path| Workspace::project_path_for_path(project.clone(), &path, false, cx))
1410 .collect::<Vec<_>>();
1411
1412 cx.background_spawn(async move {
1413 let mut paths = vec![];
1414 let mut worktrees = vec![];
1415
1416 let opened_paths = futures::future::join_all(tasks).await;
1417
1418 for entry in opened_paths {
1419 if let Some((worktree, project_path)) = entry.log_err() {
1420 worktrees.push(worktree);
1421 paths.push(project_path);
1422 }
1423 }
1424
1425 (paths, worktrees)
1426 })
1427 }
1428 };
1429
1430 context_editor_view.update(cx, |_, cx| {
1431 cx.spawn_in(window, async move |this, cx| {
1432 let (paths, dragged_file_worktrees) = paths.await;
1433 this.update_in(cx, |this, window, cx| {
1434 this.insert_dragged_files(paths, dragged_file_worktrees, window, cx);
1435 })
1436 .ok();
1437 })
1438 .detach();
1439 })
1440 }
1441
1442 pub fn insert_dragged_files(
1443 &mut self,
1444 opened_paths: Vec<ProjectPath>,
1445 added_worktrees: Vec<Entity<Worktree>>,
1446 window: &mut Window,
1447 cx: &mut Context<Self>,
1448 ) {
1449 let mut file_slash_command_args = vec![];
1450 for project_path in opened_paths.into_iter() {
1451 let Some(worktree) = self
1452 .project
1453 .read(cx)
1454 .worktree_for_id(project_path.worktree_id, cx)
1455 else {
1456 continue;
1457 };
1458 let path_style = worktree.read(cx).path_style();
1459 let full_path = worktree
1460 .read(cx)
1461 .root_name()
1462 .join(&project_path.path)
1463 .display(path_style)
1464 .into_owned();
1465 file_slash_command_args.push(full_path);
1466 }
1467
1468 let cmd_name = FileSlashCommand.name();
1469
1470 let file_argument = file_slash_command_args.join(" ");
1471
1472 self.editor.update(cx, |editor, cx| {
1473 editor.insert("\n", window, cx);
1474 editor.insert(&format!("/{} {}", cmd_name, file_argument), window, cx);
1475 });
1476 self.confirm_command(&ConfirmCommand, window, cx);
1477 self.dragged_file_worktrees.extend(added_worktrees);
1478 }
1479
1480 pub fn quote_selection(
1481 workspace: &mut Workspace,
1482 _: &AddSelectionToThread,
1483 window: &mut Window,
1484 cx: &mut Context<Workspace>,
1485 ) {
1486 let Some(agent_panel_delegate) = <dyn AgentPanelDelegate>::try_global(cx) else {
1487 return;
1488 };
1489
1490 let Some((selections, buffer)) = maybe!({
1491 let editor = workspace
1492 .active_item(cx)
1493 .and_then(|item| item.act_as::<Editor>(cx))?;
1494
1495 let buffer = editor.read(cx).buffer().clone();
1496 let snapshot = buffer.read(cx).snapshot(cx);
1497 let selections = editor.update(cx, |editor, cx| {
1498 editor
1499 .selections
1500 .all_adjusted(&editor.display_snapshot(cx))
1501 .into_iter()
1502 .filter_map(|s| {
1503 (!s.is_empty())
1504 .then(|| snapshot.anchor_after(s.start)..snapshot.anchor_before(s.end))
1505 })
1506 .collect::<Vec<_>>()
1507 });
1508 Some((selections, buffer))
1509 }) else {
1510 return;
1511 };
1512
1513 if selections.is_empty() {
1514 return;
1515 }
1516
1517 agent_panel_delegate.quote_selection(workspace, selections, buffer, window, cx);
1518 }
1519
1520 pub fn quote_ranges(
1521 &mut self,
1522 ranges: Vec<Range<Point>>,
1523 snapshot: MultiBufferSnapshot,
1524 window: &mut Window,
1525 cx: &mut Context<Self>,
1526 ) {
1527 let creases = selections_creases(ranges, snapshot, cx);
1528
1529 self.editor.update(cx, |editor, cx| {
1530 editor.insert("\n", window, cx);
1531 for (text, crease_title) in creases {
1532 let point = editor
1533 .selections
1534 .newest::<Point>(&editor.display_snapshot(cx))
1535 .head();
1536 let start_row = MultiBufferRow(point.row);
1537
1538 editor.insert(&text, window, cx);
1539
1540 let snapshot = editor.buffer().read(cx).snapshot(cx);
1541 let anchor_before = snapshot.anchor_after(point);
1542 let anchor_after = editor
1543 .selections
1544 .newest_anchor()
1545 .head()
1546 .bias_left(&snapshot);
1547
1548 editor.insert("\n", window, cx);
1549
1550 let fold_placeholder =
1551 quote_selection_fold_placeholder(crease_title, cx.entity().downgrade());
1552 let crease = Crease::inline(
1553 anchor_before..anchor_after,
1554 fold_placeholder,
1555 render_quote_selection_output_toggle,
1556 |_, _, _, _| Empty.into_any(),
1557 );
1558 editor.insert_creases(vec![crease], cx);
1559 editor.fold_at(start_row, window, cx);
1560 }
1561 })
1562 }
1563
1564 fn copy(&mut self, _: &editor::actions::Copy, _window: &mut Window, cx: &mut Context<Self>) {
1565 if self.editor.read(cx).selections.count() == 1 {
1566 let (copied_text, metadata, _) = self.get_clipboard_contents(cx);
1567 cx.write_to_clipboard(ClipboardItem::new_string_with_json_metadata(
1568 copied_text,
1569 metadata,
1570 ));
1571 cx.stop_propagation();
1572 return;
1573 }
1574
1575 cx.propagate();
1576 }
1577
1578 fn cut(&mut self, _: &editor::actions::Cut, window: &mut Window, cx: &mut Context<Self>) {
1579 if self.editor.read(cx).selections.count() == 1 {
1580 let (copied_text, metadata, selections) = self.get_clipboard_contents(cx);
1581
1582 self.editor.update(cx, |editor, cx| {
1583 editor.transact(window, cx, |this, window, cx| {
1584 this.change_selections(Default::default(), window, cx, |s| {
1585 s.select(selections);
1586 });
1587 this.insert("", window, cx);
1588 cx.write_to_clipboard(ClipboardItem::new_string_with_json_metadata(
1589 copied_text,
1590 metadata,
1591 ));
1592 });
1593 });
1594
1595 cx.stop_propagation();
1596 return;
1597 }
1598
1599 cx.propagate();
1600 }
1601
1602 fn get_clipboard_contents(
1603 &mut self,
1604 cx: &mut Context<Self>,
1605 ) -> (
1606 String,
1607 CopyMetadata,
1608 Vec<text::Selection<MultiBufferOffset>>,
1609 ) {
1610 let (mut selection, creases) = self.editor.update(cx, |editor, cx| {
1611 let mut selection = editor
1612 .selections
1613 .newest_adjusted(&editor.display_snapshot(cx));
1614 let snapshot = editor.buffer().read(cx).snapshot(cx);
1615
1616 selection.goal = SelectionGoal::None;
1617
1618 let selection_start = snapshot.point_to_offset(selection.start);
1619
1620 (
1621 selection.map(|point| snapshot.point_to_offset(point)),
1622 editor.display_map.update(cx, |display_map, cx| {
1623 display_map
1624 .snapshot(cx)
1625 .crease_snapshot
1626 .creases_in_range(
1627 MultiBufferRow(selection.start.row)
1628 ..MultiBufferRow(selection.end.row + 1),
1629 &snapshot,
1630 )
1631 .filter_map(|crease| {
1632 if let Crease::Inline {
1633 range, metadata, ..
1634 } = &crease
1635 {
1636 let metadata = metadata.as_ref()?;
1637 let start = range
1638 .start
1639 .to_offset(&snapshot)
1640 .saturating_sub(selection_start);
1641 let end = range
1642 .end
1643 .to_offset(&snapshot)
1644 .saturating_sub(selection_start);
1645
1646 let range_relative_to_selection = start..end;
1647 if !range_relative_to_selection.is_empty() {
1648 return Some(SelectedCreaseMetadata {
1649 range_relative_to_selection,
1650 crease: metadata.clone(),
1651 });
1652 }
1653 }
1654 None
1655 })
1656 .collect::<Vec<_>>()
1657 }),
1658 )
1659 });
1660
1661 let text_thread = self.text_thread.read(cx);
1662
1663 let mut text = String::new();
1664
1665 // If selection is empty, we want to copy the entire line
1666 if selection.range().is_empty() {
1667 let snapshot = self.editor.read(cx).buffer().read(cx).snapshot(cx);
1668 let point = snapshot.offset_to_point(selection.range().start);
1669 selection.start = snapshot.point_to_offset(Point::new(point.row, 0));
1670 selection.end = snapshot
1671 .point_to_offset(cmp::min(Point::new(point.row + 1, 0), snapshot.max_point()));
1672 for chunk in snapshot.text_for_range(selection.range()) {
1673 text.push_str(chunk);
1674 }
1675 } else {
1676 for message in text_thread.messages(cx) {
1677 if message.offset_range.start >= selection.range().end.0 {
1678 break;
1679 } else if message.offset_range.end >= selection.range().start.0 {
1680 let range = cmp::max(message.offset_range.start, selection.range().start.0)
1681 ..cmp::min(message.offset_range.end, selection.range().end.0);
1682 if !range.is_empty() {
1683 for chunk in text_thread.buffer().read(cx).text_for_range(range) {
1684 text.push_str(chunk);
1685 }
1686 if message.offset_range.end < selection.range().end.0 {
1687 text.push('\n');
1688 }
1689 }
1690 }
1691 }
1692 }
1693 (text, CopyMetadata { creases }, vec![selection])
1694 }
1695
1696 fn paste(
1697 &mut self,
1698 action: &editor::actions::Paste,
1699 window: &mut Window,
1700 cx: &mut Context<Self>,
1701 ) {
1702 let Some(workspace) = self.workspace.upgrade() else {
1703 return;
1704 };
1705 let editor_clipboard_selections = cx
1706 .read_from_clipboard()
1707 .and_then(|item| item.entries().first().cloned())
1708 .and_then(|entry| match entry {
1709 ClipboardEntry::String(text) => {
1710 text.metadata_json::<Vec<editor::ClipboardSelection>>()
1711 }
1712 _ => None,
1713 });
1714
1715 // Insert creases for pasted clipboard selections that:
1716 // 1. Contain exactly one selection
1717 // 2. Have an associated file path
1718 // 3. Span multiple lines (not single-line selections)
1719 // 4. Belong to a file that exists in the current project
1720 let should_insert_creases = util::maybe!({
1721 let selections = editor_clipboard_selections.as_ref()?;
1722 if selections.len() > 1 {
1723 return Some(false);
1724 }
1725 let selection = selections.first()?;
1726 let file_path = selection.file_path.as_ref()?;
1727 let line_range = selection.line_range.as_ref()?;
1728
1729 if line_range.start() == line_range.end() {
1730 return Some(false);
1731 }
1732
1733 Some(
1734 workspace
1735 .read(cx)
1736 .project()
1737 .read(cx)
1738 .project_path_for_absolute_path(file_path, cx)
1739 .is_some(),
1740 )
1741 })
1742 .unwrap_or(false);
1743
1744 if should_insert_creases && let Some(clipboard_item) = cx.read_from_clipboard() {
1745 if let Some(ClipboardEntry::String(clipboard_text)) = clipboard_item.entries().first() {
1746 if let Some(selections) = editor_clipboard_selections {
1747 cx.stop_propagation();
1748
1749 let text = clipboard_text.text();
1750 self.editor.update(cx, |editor, cx| {
1751 let mut current_offset = 0;
1752 let weak_editor = cx.entity().downgrade();
1753
1754 for selection in selections {
1755 if let (Some(file_path), Some(line_range)) =
1756 (selection.file_path, selection.line_range)
1757 {
1758 let selected_text =
1759 &text[current_offset..current_offset + selection.len];
1760 let fence = assistant_slash_commands::codeblock_fence_for_path(
1761 file_path.to_str(),
1762 Some(line_range.clone()),
1763 );
1764 let formatted_text = format!("{fence}{selected_text}\n```");
1765
1766 let insert_point = editor
1767 .selections
1768 .newest::<Point>(&editor.display_snapshot(cx))
1769 .head();
1770 let start_row = MultiBufferRow(insert_point.row);
1771
1772 editor.insert(&formatted_text, window, cx);
1773
1774 let snapshot = editor.buffer().read(cx).snapshot(cx);
1775 let anchor_before = snapshot.anchor_after(insert_point);
1776 let anchor_after = editor
1777 .selections
1778 .newest_anchor()
1779 .head()
1780 .bias_left(&snapshot);
1781
1782 editor.insert("\n", window, cx);
1783
1784 let crease_text = acp_thread::selection_name(
1785 Some(file_path.as_ref()),
1786 &line_range,
1787 );
1788
1789 let fold_placeholder = quote_selection_fold_placeholder(
1790 crease_text,
1791 weak_editor.clone(),
1792 );
1793 let crease = Crease::inline(
1794 anchor_before..anchor_after,
1795 fold_placeholder,
1796 render_quote_selection_output_toggle,
1797 |_, _, _, _| Empty.into_any(),
1798 );
1799 editor.insert_creases(vec![crease], cx);
1800 editor.fold_at(start_row, window, cx);
1801
1802 current_offset += selection.len;
1803 if !selection.is_entire_line && current_offset < text.len() {
1804 current_offset += 1;
1805 }
1806 }
1807 }
1808 });
1809 return;
1810 }
1811 }
1812 }
1813
1814 cx.stop_propagation();
1815
1816 let mut images = if let Some(item) = cx.read_from_clipboard() {
1817 item.into_entries()
1818 .filter_map(|entry| {
1819 if let ClipboardEntry::Image(image) = entry {
1820 Some(image)
1821 } else {
1822 None
1823 }
1824 })
1825 .collect()
1826 } else {
1827 Vec::new()
1828 };
1829
1830 if let Some(paths) = cx.read_from_clipboard() {
1831 for path in paths
1832 .into_entries()
1833 .filter_map(|entry| {
1834 if let ClipboardEntry::ExternalPaths(paths) = entry {
1835 Some(paths.paths().to_owned())
1836 } else {
1837 None
1838 }
1839 })
1840 .flatten()
1841 {
1842 let Ok(content) = std::fs::read(path) else {
1843 continue;
1844 };
1845 let Ok(format) = image::guess_format(&content) else {
1846 continue;
1847 };
1848 images.push(gpui::Image::from_bytes(
1849 match format {
1850 image::ImageFormat::Png => gpui::ImageFormat::Png,
1851 image::ImageFormat::Jpeg => gpui::ImageFormat::Jpeg,
1852 image::ImageFormat::WebP => gpui::ImageFormat::Webp,
1853 image::ImageFormat::Gif => gpui::ImageFormat::Gif,
1854 image::ImageFormat::Bmp => gpui::ImageFormat::Bmp,
1855 image::ImageFormat::Tiff => gpui::ImageFormat::Tiff,
1856 image::ImageFormat::Ico => gpui::ImageFormat::Ico,
1857 _ => continue,
1858 },
1859 content,
1860 ));
1861 }
1862 }
1863
1864 let metadata = if let Some(item) = cx.read_from_clipboard() {
1865 item.entries().first().and_then(|entry| {
1866 if let ClipboardEntry::String(text) = entry {
1867 text.metadata_json::<CopyMetadata>()
1868 } else {
1869 None
1870 }
1871 })
1872 } else {
1873 None
1874 };
1875
1876 if images.is_empty() {
1877 self.editor.update(cx, |editor, cx| {
1878 let paste_position = editor
1879 .selections
1880 .newest::<MultiBufferOffset>(&editor.display_snapshot(cx))
1881 .head();
1882 editor.paste(action, window, cx);
1883
1884 if let Some(metadata) = metadata {
1885 let buffer = editor.buffer().read(cx).snapshot(cx);
1886
1887 let mut buffer_rows_to_fold = BTreeSet::new();
1888 let weak_editor = cx.entity().downgrade();
1889 editor.insert_creases(
1890 metadata.creases.into_iter().map(|metadata| {
1891 let start = buffer.anchor_after(
1892 paste_position + metadata.range_relative_to_selection.start,
1893 );
1894 let end = buffer.anchor_before(
1895 paste_position + metadata.range_relative_to_selection.end,
1896 );
1897
1898 let buffer_row = MultiBufferRow(start.to_point(&buffer).row);
1899 buffer_rows_to_fold.insert(buffer_row);
1900 Crease::inline(
1901 start..end,
1902 FoldPlaceholder {
1903 render: render_fold_icon_button(
1904 weak_editor.clone(),
1905 metadata.crease.icon_path.clone(),
1906 metadata.crease.label.clone(),
1907 ),
1908 ..Default::default()
1909 },
1910 render_slash_command_output_toggle,
1911 |_, _, _, _| Empty.into_any(),
1912 )
1913 .with_metadata(metadata.crease)
1914 }),
1915 cx,
1916 );
1917 for buffer_row in buffer_rows_to_fold.into_iter().rev() {
1918 editor.fold_at(buffer_row, window, cx);
1919 }
1920 }
1921 });
1922 } else {
1923 let mut image_positions = Vec::new();
1924 self.editor.update(cx, |editor, cx| {
1925 editor.transact(window, cx, |editor, _window, cx| {
1926 let edits = editor
1927 .selections
1928 .all::<MultiBufferOffset>(&editor.display_snapshot(cx))
1929 .into_iter()
1930 .map(|selection| (selection.start..selection.end, "\n"));
1931 editor.edit(edits, cx);
1932
1933 let snapshot = editor.buffer().read(cx).snapshot(cx);
1934 for selection in editor
1935 .selections
1936 .all::<MultiBufferOffset>(&editor.display_snapshot(cx))
1937 {
1938 image_positions.push(snapshot.anchor_before(selection.end));
1939 }
1940 });
1941 });
1942
1943 self.text_thread.update(cx, |text_thread, cx| {
1944 for image in images {
1945 let Some(render_image) = image.to_image_data(cx.svg_renderer()).log_err()
1946 else {
1947 continue;
1948 };
1949 let image_id = image.id();
1950 let image_task = LanguageModelImage::from_image(Arc::new(image), cx).shared();
1951
1952 for image_position in image_positions.iter() {
1953 text_thread.insert_content(
1954 Content::Image {
1955 anchor: image_position.text_anchor,
1956 image_id,
1957 image: image_task.clone(),
1958 render_image: render_image.clone(),
1959 },
1960 cx,
1961 );
1962 }
1963 }
1964 });
1965 }
1966 }
1967
1968 fn paste_raw(&mut self, _: &PasteRaw, window: &mut Window, cx: &mut Context<Self>) {
1969 self.editor.update(cx, |editor, cx| {
1970 editor.paste(&editor::actions::Paste, window, cx);
1971 });
1972 }
1973
1974 fn update_image_blocks(&mut self, cx: &mut Context<Self>) {
1975 self.editor.update(cx, |editor, cx| {
1976 let buffer = editor.buffer().read(cx).snapshot(cx);
1977 let excerpt_id = *buffer.as_singleton().unwrap().0;
1978 let old_blocks = std::mem::take(&mut self.image_blocks);
1979 let new_blocks = self
1980 .text_thread
1981 .read(cx)
1982 .contents(cx)
1983 .map(
1984 |Content::Image {
1985 anchor,
1986 render_image,
1987 ..
1988 }| (anchor, render_image),
1989 )
1990 .filter_map(|(anchor, render_image)| {
1991 const MAX_HEIGHT_IN_LINES: u32 = 8;
1992 let anchor = buffer.anchor_in_excerpt(excerpt_id, anchor).unwrap();
1993 let image = render_image;
1994 anchor.is_valid(&buffer).then(|| BlockProperties {
1995 placement: BlockPlacement::Above(anchor),
1996 height: Some(MAX_HEIGHT_IN_LINES),
1997 style: BlockStyle::Sticky,
1998 render: Arc::new(move |cx| {
1999 let image_size = size_for_image(
2000 &image,
2001 size(
2002 cx.max_width - cx.margins.gutter.full_width(),
2003 MAX_HEIGHT_IN_LINES as f32 * cx.line_height,
2004 ),
2005 );
2006 h_flex()
2007 .pl(cx.margins.gutter.full_width())
2008 .child(
2009 img(image.clone())
2010 .object_fit(gpui::ObjectFit::ScaleDown)
2011 .w(image_size.width)
2012 .h(image_size.height),
2013 )
2014 .into_any_element()
2015 }),
2016 priority: 0,
2017 })
2018 })
2019 .collect::<Vec<_>>();
2020
2021 editor.remove_blocks(old_blocks, None, cx);
2022 let ids = editor.insert_blocks(new_blocks, None, cx);
2023 self.image_blocks = HashSet::from_iter(ids);
2024 });
2025 }
2026
2027 fn split(&mut self, _: &Split, _window: &mut Window, cx: &mut Context<Self>) {
2028 self.text_thread.update(cx, |text_thread, cx| {
2029 let selections = self.editor.read(cx).selections.disjoint_anchors_arc();
2030 for selection in selections.as_ref() {
2031 let buffer = self.editor.read(cx).buffer().read(cx).snapshot(cx);
2032 let range = selection
2033 .map(|endpoint| endpoint.to_offset(&buffer))
2034 .range();
2035 text_thread.split_message(range.start.0..range.end.0, cx);
2036 }
2037 });
2038 }
2039
2040 fn save(&mut self, _: &Save, _window: &mut Window, cx: &mut Context<Self>) {
2041 self.text_thread.update(cx, |text_thread, cx| {
2042 text_thread.save(Some(Duration::from_millis(500)), self.fs.clone(), cx)
2043 });
2044 }
2045
2046 pub fn title(&self, cx: &App) -> SharedString {
2047 self.text_thread.read(cx).summary().or_default()
2048 }
2049
2050 pub fn regenerate_summary(&mut self, cx: &mut Context<Self>) {
2051 self.text_thread
2052 .update(cx, |text_thread, cx| text_thread.summarize(true, cx));
2053 }
2054
2055 fn render_remaining_tokens(&self, cx: &App) -> Option<impl IntoElement + use<>> {
2056 let (token_count_color, token_count, max_token_count, tooltip) =
2057 match token_state(&self.text_thread, cx)? {
2058 TokenState::NoTokensLeft {
2059 max_token_count,
2060 token_count,
2061 } => (
2062 Color::Error,
2063 token_count,
2064 max_token_count,
2065 Some("Token Limit Reached"),
2066 ),
2067 TokenState::HasMoreTokens {
2068 max_token_count,
2069 token_count,
2070 over_warn_threshold,
2071 } => {
2072 let (color, tooltip) = if over_warn_threshold {
2073 (Color::Warning, Some("Token Limit is Close to Exhaustion"))
2074 } else {
2075 (Color::Muted, None)
2076 };
2077 (color, token_count, max_token_count, tooltip)
2078 }
2079 };
2080
2081 Some(
2082 h_flex()
2083 .id("token-count")
2084 .gap_0p5()
2085 .child(
2086 Label::new(humanize_token_count(token_count))
2087 .size(LabelSize::Small)
2088 .color(token_count_color),
2089 )
2090 .child(Label::new("/").size(LabelSize::Small).color(Color::Muted))
2091 .child(
2092 Label::new(humanize_token_count(max_token_count))
2093 .size(LabelSize::Small)
2094 .color(Color::Muted),
2095 )
2096 .when_some(tooltip, |element, tooltip| {
2097 element.tooltip(Tooltip::text(tooltip))
2098 }),
2099 )
2100 }
2101
2102 fn render_send_button(&self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
2103 let focus_handle = self.focus_handle(cx);
2104
2105 let (style, tooltip) = match token_state(&self.text_thread, cx) {
2106 Some(TokenState::NoTokensLeft { .. }) => (
2107 ButtonStyle::Tinted(TintColor::Error),
2108 Some(Tooltip::text("Token limit reached")(window, cx)),
2109 ),
2110 Some(TokenState::HasMoreTokens {
2111 over_warn_threshold,
2112 ..
2113 }) => {
2114 let (style, tooltip) = if over_warn_threshold {
2115 (
2116 ButtonStyle::Tinted(TintColor::Warning),
2117 Some(Tooltip::text("Token limit is close to exhaustion")(
2118 window, cx,
2119 )),
2120 )
2121 } else {
2122 (ButtonStyle::Filled, None)
2123 };
2124 (style, tooltip)
2125 }
2126 None => (ButtonStyle::Filled, None),
2127 };
2128
2129 Button::new("send_button", "Send")
2130 .label_size(LabelSize::Small)
2131 .disabled(self.sending_disabled(cx))
2132 .style(style)
2133 .when_some(tooltip, |button, tooltip| {
2134 button.tooltip(move |_, _| tooltip.clone())
2135 })
2136 .layer(ElevationIndex::ModalSurface)
2137 .key_binding(
2138 KeyBinding::for_action_in(&Assist, &focus_handle, cx)
2139 .map(|kb| kb.size(rems_from_px(12.))),
2140 )
2141 .on_click(move |_event, window, cx| {
2142 focus_handle.dispatch_action(&Assist, window, cx);
2143 })
2144 }
2145
2146 /// Whether or not we should allow messages to be sent.
2147 /// Will return false if the selected provided has a configuration error or
2148 /// if the user has not accepted the terms of service for this provider.
2149 fn sending_disabled(&self, cx: &mut Context<'_, TextThreadEditor>) -> bool {
2150 let model_registry = LanguageModelRegistry::read_global(cx);
2151 let Some(configuration_error) =
2152 model_registry.configuration_error(model_registry.default_model(), cx)
2153 else {
2154 return false;
2155 };
2156
2157 match configuration_error {
2158 ConfigurationError::NoProvider
2159 | ConfigurationError::ModelNotFound
2160 | ConfigurationError::ProviderNotAuthenticated(_) => true,
2161 }
2162 }
2163
2164 fn render_inject_context_menu(&self, cx: &mut Context<Self>) -> impl IntoElement {
2165 slash_command_picker::SlashCommandSelector::new(
2166 self.slash_commands.clone(),
2167 cx.entity().downgrade(),
2168 IconButton::new("trigger", IconName::Plus)
2169 .icon_size(IconSize::Small)
2170 .icon_color(Color::Muted)
2171 .selected_icon_color(Color::Accent)
2172 .selected_style(ButtonStyle::Filled),
2173 move |_window, cx| {
2174 Tooltip::with_meta("Add Context", None, "Type / to insert via keyboard", cx)
2175 },
2176 )
2177 }
2178
2179 fn render_burn_mode_toggle(&self, cx: &mut Context<Self>) -> Option<AnyElement> {
2180 let text_thread = self.text_thread().read(cx);
2181 let active_model = LanguageModelRegistry::read_global(cx)
2182 .default_model()
2183 .map(|default| default.model)?;
2184 if !active_model.supports_burn_mode() {
2185 return None;
2186 }
2187
2188 let active_completion_mode = text_thread.completion_mode();
2189 let burn_mode_enabled = active_completion_mode == CompletionMode::Burn;
2190 let icon = if burn_mode_enabled {
2191 IconName::ZedBurnModeOn
2192 } else {
2193 IconName::ZedBurnMode
2194 };
2195
2196 Some(
2197 IconButton::new("burn-mode", icon)
2198 .icon_size(IconSize::Small)
2199 .icon_color(Color::Muted)
2200 .toggle_state(burn_mode_enabled)
2201 .selected_icon_color(Color::Error)
2202 .on_click(cx.listener(move |this, _event, _window, cx| {
2203 this.text_thread().update(cx, |text_thread, _cx| {
2204 text_thread.set_completion_mode(match active_completion_mode {
2205 CompletionMode::Burn => CompletionMode::Normal,
2206 CompletionMode::Normal => CompletionMode::Burn,
2207 });
2208 });
2209 }))
2210 .tooltip(move |_window, cx| {
2211 cx.new(|_| BurnModeTooltip::new().selected(burn_mode_enabled))
2212 .into()
2213 })
2214 .into_any_element(),
2215 )
2216 }
2217
2218 fn render_language_model_selector(
2219 &self,
2220 window: &mut Window,
2221 cx: &mut Context<Self>,
2222 ) -> impl IntoElement {
2223 let active_model = LanguageModelRegistry::read_global(cx)
2224 .default_model()
2225 .map(|default| default.model);
2226 let model_name = match active_model {
2227 Some(model) => model.name().0,
2228 None => SharedString::from("Select Model"),
2229 };
2230
2231 let active_provider = LanguageModelRegistry::read_global(cx)
2232 .default_model()
2233 .map(|default| default.provider);
2234
2235 let provider_icon = active_provider
2236 .as_ref()
2237 .map(|p| p.icon())
2238 .unwrap_or(IconOrSvg::Icon(IconName::Ai));
2239
2240 let focus_handle = self.editor().focus_handle(cx);
2241
2242 let (color, icon) = if self.language_model_selector_menu_handle.is_deployed() {
2243 (Color::Accent, IconName::ChevronUp)
2244 } else {
2245 (Color::Muted, IconName::ChevronDown)
2246 };
2247
2248 let provider_icon_element = match provider_icon {
2249 IconOrSvg::Svg(path) => Icon::from_external_svg(path),
2250 IconOrSvg::Icon(name) => Icon::new(name),
2251 }
2252 .color(color)
2253 .size(IconSize::XSmall);
2254
2255 let show_cycle_row = self
2256 .language_model_selector
2257 .read(cx)
2258 .delegate
2259 .favorites_count()
2260 > 1;
2261
2262 let tooltip = Tooltip::element({
2263 move |_, _cx| {
2264 ModelSelectorTooltip::new(focus_handle.clone())
2265 .show_cycle_row(show_cycle_row)
2266 .into_any_element()
2267 }
2268 });
2269
2270 PickerPopoverMenu::new(
2271 self.language_model_selector.clone(),
2272 ButtonLike::new("active-model")
2273 .selected_style(ButtonStyle::Tinted(TintColor::Accent))
2274 .child(
2275 h_flex()
2276 .gap_0p5()
2277 .child(provider_icon_element)
2278 .child(
2279 Label::new(model_name)
2280 .color(color)
2281 .size(LabelSize::Small)
2282 .ml_0p5(),
2283 )
2284 .child(Icon::new(icon).color(color).size(IconSize::XSmall)),
2285 ),
2286 tooltip,
2287 gpui::Corner::BottomRight,
2288 cx,
2289 )
2290 .with_handle(self.language_model_selector_menu_handle.clone())
2291 .render(window, cx)
2292 }
2293
2294 fn render_last_error(&self, cx: &mut Context<Self>) -> Option<AnyElement> {
2295 let last_error = self.last_error.as_ref()?;
2296
2297 Some(
2298 div()
2299 .absolute()
2300 .right_3()
2301 .bottom_12()
2302 .max_w_96()
2303 .py_2()
2304 .px_3()
2305 .elevation_2(cx)
2306 .occlude()
2307 .child(match last_error {
2308 AssistError::PaymentRequired => self.render_payment_required_error(cx),
2309 AssistError::Message(error_message) => {
2310 self.render_assist_error(error_message, cx)
2311 }
2312 })
2313 .into_any(),
2314 )
2315 }
2316
2317 fn render_payment_required_error(&self, cx: &mut Context<Self>) -> AnyElement {
2318 const ERROR_MESSAGE: &str = "Free tier exceeded. Subscribe and add payment to continue using Zed LLMs. You'll be billed at cost for tokens used.";
2319
2320 v_flex()
2321 .gap_0p5()
2322 .child(
2323 h_flex()
2324 .gap_1p5()
2325 .items_center()
2326 .child(Icon::new(IconName::XCircle).color(Color::Error))
2327 .child(Label::new("Free Usage Exceeded").weight(FontWeight::MEDIUM)),
2328 )
2329 .child(
2330 div()
2331 .id("error-message")
2332 .max_h_24()
2333 .overflow_y_scroll()
2334 .child(Label::new(ERROR_MESSAGE)),
2335 )
2336 .child(
2337 h_flex()
2338 .justify_end()
2339 .mt_1()
2340 .child(Button::new("subscribe", "Subscribe").on_click(cx.listener(
2341 |this, _, _window, cx| {
2342 this.last_error = None;
2343 cx.open_url(&zed_urls::account_url(cx));
2344 cx.notify();
2345 },
2346 )))
2347 .child(Button::new("dismiss", "Dismiss").on_click(cx.listener(
2348 |this, _, _window, cx| {
2349 this.last_error = None;
2350 cx.notify();
2351 },
2352 ))),
2353 )
2354 .into_any()
2355 }
2356
2357 fn render_assist_error(
2358 &self,
2359 error_message: &SharedString,
2360 cx: &mut Context<Self>,
2361 ) -> AnyElement {
2362 v_flex()
2363 .gap_0p5()
2364 .child(
2365 h_flex()
2366 .gap_1p5()
2367 .items_center()
2368 .child(Icon::new(IconName::XCircle).color(Color::Error))
2369 .child(
2370 Label::new("Error interacting with language model")
2371 .weight(FontWeight::MEDIUM),
2372 ),
2373 )
2374 .child(
2375 div()
2376 .id("error-message")
2377 .max_h_32()
2378 .overflow_y_scroll()
2379 .child(Label::new(error_message.clone())),
2380 )
2381 .child(
2382 h_flex()
2383 .justify_end()
2384 .mt_1()
2385 .child(Button::new("dismiss", "Dismiss").on_click(cx.listener(
2386 |this, _, _window, cx| {
2387 this.last_error = None;
2388 cx.notify();
2389 },
2390 ))),
2391 )
2392 .into_any()
2393 }
2394}
2395
2396/// Returns the contents of the *outermost* fenced code block that contains the given offset.
2397fn find_surrounding_code_block(snapshot: &BufferSnapshot, offset: usize) -> Option<Range<usize>> {
2398 const CODE_BLOCK_NODE: &str = "fenced_code_block";
2399 const CODE_BLOCK_CONTENT: &str = "code_fence_content";
2400
2401 let layer = snapshot.syntax_layers().next()?;
2402
2403 let root_node = layer.node();
2404 let mut cursor = root_node.walk();
2405
2406 // Go to the first child for the given offset
2407 while cursor.goto_first_child_for_byte(offset).is_some() {
2408 // If we're at the end of the node, go to the next one.
2409 // Example: if you have a fenced-code-block, and you're on the start of the line
2410 // right after the closing ```, you want to skip the fenced-code-block and
2411 // go to the next sibling.
2412 if cursor.node().end_byte() == offset {
2413 cursor.goto_next_sibling();
2414 }
2415
2416 if cursor.node().start_byte() > offset {
2417 break;
2418 }
2419
2420 // We found the fenced code block.
2421 if cursor.node().kind() == CODE_BLOCK_NODE {
2422 // Now we need to find the child node that contains the code.
2423 cursor.goto_first_child();
2424 loop {
2425 if cursor.node().kind() == CODE_BLOCK_CONTENT {
2426 return Some(cursor.node().byte_range());
2427 }
2428 if !cursor.goto_next_sibling() {
2429 break;
2430 }
2431 }
2432 }
2433 }
2434
2435 None
2436}
2437
2438fn render_thought_process_fold_icon_button(
2439 editor: WeakEntity<Editor>,
2440 status: ThoughtProcessStatus,
2441) -> Arc<dyn Send + Sync + Fn(FoldId, Range<Anchor>, &mut App) -> AnyElement> {
2442 Arc::new(move |fold_id, fold_range, _cx| {
2443 let editor = editor.clone();
2444
2445 let button = ButtonLike::new(fold_id).layer(ElevationIndex::ElevatedSurface);
2446 let button = match status {
2447 ThoughtProcessStatus::Pending => button
2448 .child(
2449 Icon::new(IconName::ToolThink)
2450 .size(IconSize::Small)
2451 .color(Color::Muted),
2452 )
2453 .child(
2454 Label::new("Thinking…").color(Color::Muted).with_animation(
2455 "pulsating-label",
2456 Animation::new(Duration::from_secs(2))
2457 .repeat()
2458 .with_easing(pulsating_between(0.4, 0.8)),
2459 |label, delta| label.alpha(delta),
2460 ),
2461 ),
2462 ThoughtProcessStatus::Completed => button
2463 .style(ButtonStyle::Filled)
2464 .child(Icon::new(IconName::ToolThink).size(IconSize::Small))
2465 .child(Label::new("Thought Process").single_line()),
2466 };
2467
2468 button
2469 .on_click(move |_, window, cx| {
2470 editor
2471 .update(cx, |editor, cx| {
2472 let buffer_start = fold_range
2473 .start
2474 .to_point(&editor.buffer().read(cx).read(cx));
2475 let buffer_row = MultiBufferRow(buffer_start.row);
2476 editor.unfold_at(buffer_row, window, cx);
2477 })
2478 .ok();
2479 })
2480 .into_any_element()
2481 })
2482}
2483
2484fn render_fold_icon_button(
2485 editor: WeakEntity<Editor>,
2486 icon_path: SharedString,
2487 label: SharedString,
2488) -> Arc<dyn Send + Sync + Fn(FoldId, Range<Anchor>, &mut App) -> AnyElement> {
2489 Arc::new(move |fold_id, fold_range, _cx| {
2490 let editor = editor.clone();
2491 ButtonLike::new(fold_id)
2492 .style(ButtonStyle::Filled)
2493 .layer(ElevationIndex::ElevatedSurface)
2494 .child(Icon::from_path(icon_path.clone()))
2495 .child(Label::new(label.clone()).single_line())
2496 .on_click(move |_, window, cx| {
2497 editor
2498 .update(cx, |editor, cx| {
2499 let buffer_start = fold_range
2500 .start
2501 .to_point(&editor.buffer().read(cx).read(cx));
2502 let buffer_row = MultiBufferRow(buffer_start.row);
2503 editor.unfold_at(buffer_row, window, cx);
2504 })
2505 .ok();
2506 })
2507 .into_any_element()
2508 })
2509}
2510
2511type ToggleFold = Arc<dyn Fn(bool, &mut Window, &mut App) + Send + Sync>;
2512
2513fn render_slash_command_output_toggle(
2514 row: MultiBufferRow,
2515 is_folded: bool,
2516 fold: ToggleFold,
2517 _window: &mut Window,
2518 _cx: &mut App,
2519) -> AnyElement {
2520 Disclosure::new(
2521 ("slash-command-output-fold-indicator", row.0 as u64),
2522 !is_folded,
2523 )
2524 .toggle_state(is_folded)
2525 .on_click(move |_e, window, cx| fold(!is_folded, window, cx))
2526 .into_any_element()
2527}
2528
2529pub fn fold_toggle(
2530 name: &'static str,
2531) -> impl Fn(
2532 MultiBufferRow,
2533 bool,
2534 Arc<dyn Fn(bool, &mut Window, &mut App) + Send + Sync>,
2535 &mut Window,
2536 &mut App,
2537) -> AnyElement {
2538 move |row, is_folded, fold, _window, _cx| {
2539 Disclosure::new((name, row.0 as u64), !is_folded)
2540 .toggle_state(is_folded)
2541 .on_click(move |_e, window, cx| fold(!is_folded, window, cx))
2542 .into_any_element()
2543 }
2544}
2545
2546fn quote_selection_fold_placeholder(title: String, editor: WeakEntity<Editor>) -> FoldPlaceholder {
2547 FoldPlaceholder {
2548 render: Arc::new({
2549 move |fold_id, fold_range, _cx| {
2550 let editor = editor.clone();
2551 ButtonLike::new(fold_id)
2552 .style(ButtonStyle::Filled)
2553 .layer(ElevationIndex::ElevatedSurface)
2554 .child(Icon::new(IconName::TextSnippet))
2555 .child(Label::new(title.clone()).single_line())
2556 .on_click(move |_, window, cx| {
2557 editor
2558 .update(cx, |editor, cx| {
2559 let buffer_start = fold_range
2560 .start
2561 .to_point(&editor.buffer().read(cx).read(cx));
2562 let buffer_row = MultiBufferRow(buffer_start.row);
2563 editor.unfold_at(buffer_row, window, cx);
2564 })
2565 .ok();
2566 })
2567 .into_any_element()
2568 }
2569 }),
2570 merge_adjacent: false,
2571 ..Default::default()
2572 }
2573}
2574
2575fn render_quote_selection_output_toggle(
2576 row: MultiBufferRow,
2577 is_folded: bool,
2578 fold: ToggleFold,
2579 _window: &mut Window,
2580 _cx: &mut App,
2581) -> AnyElement {
2582 Disclosure::new(("quote-selection-indicator", row.0 as u64), !is_folded)
2583 .toggle_state(is_folded)
2584 .on_click(move |_e, window, cx| fold(!is_folded, window, cx))
2585 .into_any_element()
2586}
2587
2588fn render_pending_slash_command_gutter_decoration(
2589 row: MultiBufferRow,
2590 status: &PendingSlashCommandStatus,
2591 confirm_command: Arc<dyn Fn(&mut Window, &mut App)>,
2592) -> AnyElement {
2593 let mut icon = IconButton::new(
2594 ("slash-command-gutter-decoration", row.0),
2595 ui::IconName::TriangleRight,
2596 )
2597 .on_click(move |_e, window, cx| confirm_command(window, cx))
2598 .icon_size(ui::IconSize::Small)
2599 .size(ui::ButtonSize::None);
2600
2601 match status {
2602 PendingSlashCommandStatus::Idle => {
2603 icon = icon.icon_color(Color::Muted);
2604 }
2605 PendingSlashCommandStatus::Running { .. } => {
2606 icon = icon.toggle_state(true);
2607 }
2608 PendingSlashCommandStatus::Error(_) => icon = icon.icon_color(Color::Error),
2609 }
2610
2611 icon.into_any_element()
2612}
2613
2614#[derive(Debug, Clone, Serialize, Deserialize)]
2615struct CopyMetadata {
2616 creases: Vec<SelectedCreaseMetadata>,
2617}
2618
2619#[derive(Debug, Clone, Serialize, Deserialize)]
2620struct SelectedCreaseMetadata {
2621 range_relative_to_selection: Range<usize>,
2622 crease: CreaseMetadata,
2623}
2624
2625impl EventEmitter<EditorEvent> for TextThreadEditor {}
2626impl EventEmitter<SearchEvent> for TextThreadEditor {}
2627
2628impl Render for TextThreadEditor {
2629 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
2630 let language_model_selector = self.language_model_selector_menu_handle.clone();
2631
2632 v_flex()
2633 .key_context("ContextEditor")
2634 .capture_action(cx.listener(TextThreadEditor::cancel))
2635 .capture_action(cx.listener(TextThreadEditor::save))
2636 .capture_action(cx.listener(TextThreadEditor::copy))
2637 .capture_action(cx.listener(TextThreadEditor::cut))
2638 .capture_action(cx.listener(TextThreadEditor::paste))
2639 .on_action(cx.listener(TextThreadEditor::paste_raw))
2640 .capture_action(cx.listener(TextThreadEditor::cycle_message_role))
2641 .capture_action(cx.listener(TextThreadEditor::confirm_command))
2642 .on_action(cx.listener(TextThreadEditor::assist))
2643 .on_action(cx.listener(TextThreadEditor::split))
2644 .on_action(move |_: &ToggleModelSelector, window, cx| {
2645 language_model_selector.toggle(window, cx);
2646 })
2647 .on_action(cx.listener(|this, _: &CycleFavoriteModels, window, cx| {
2648 this.language_model_selector.update(cx, |selector, cx| {
2649 selector.delegate.cycle_favorite_models(window, cx);
2650 });
2651 }))
2652 .size_full()
2653 .child(
2654 div()
2655 .flex_grow()
2656 .bg(cx.theme().colors().editor_background)
2657 .child(self.editor.clone()),
2658 )
2659 .children(self.render_last_error(cx))
2660 .child(
2661 h_flex()
2662 .relative()
2663 .py_2()
2664 .pl_1p5()
2665 .pr_2()
2666 .w_full()
2667 .justify_between()
2668 .border_t_1()
2669 .border_color(cx.theme().colors().border_variant)
2670 .bg(cx.theme().colors().editor_background)
2671 .child(
2672 h_flex()
2673 .gap_0p5()
2674 .child(self.render_inject_context_menu(cx))
2675 .children(self.render_burn_mode_toggle(cx)),
2676 )
2677 .child(
2678 h_flex()
2679 .gap_2p5()
2680 .children(self.render_remaining_tokens(cx))
2681 .child(
2682 h_flex()
2683 .gap_1()
2684 .child(self.render_language_model_selector(window, cx))
2685 .child(self.render_send_button(window, cx)),
2686 ),
2687 ),
2688 )
2689 }
2690}
2691
2692impl Focusable for TextThreadEditor {
2693 fn focus_handle(&self, cx: &App) -> FocusHandle {
2694 self.editor.focus_handle(cx)
2695 }
2696}
2697
2698impl Item for TextThreadEditor {
2699 type Event = editor::EditorEvent;
2700
2701 fn tab_content_text(&self, _detail: usize, cx: &App) -> SharedString {
2702 util::truncate_and_trailoff(&self.title(cx), MAX_TAB_TITLE_LEN).into()
2703 }
2704
2705 fn to_item_events(event: &Self::Event, mut f: impl FnMut(item::ItemEvent)) {
2706 match event {
2707 EditorEvent::Edited { .. } => {
2708 f(item::ItemEvent::Edit);
2709 }
2710 EditorEvent::TitleChanged => {
2711 f(item::ItemEvent::UpdateTab);
2712 }
2713 _ => {}
2714 }
2715 }
2716
2717 fn tab_tooltip_text(&self, cx: &App) -> Option<SharedString> {
2718 Some(self.title(cx).to_string().into())
2719 }
2720
2721 fn as_searchable(
2722 &self,
2723 handle: &Entity<Self>,
2724 _: &App,
2725 ) -> Option<Box<dyn SearchableItemHandle>> {
2726 Some(Box::new(handle.clone()))
2727 }
2728
2729 fn set_nav_history(
2730 &mut self,
2731 nav_history: pane::ItemNavHistory,
2732 window: &mut Window,
2733 cx: &mut Context<Self>,
2734 ) {
2735 self.editor.update(cx, |editor, cx| {
2736 Item::set_nav_history(editor, nav_history, window, cx)
2737 })
2738 }
2739
2740 fn navigate(
2741 &mut self,
2742 data: Box<dyn std::any::Any>,
2743 window: &mut Window,
2744 cx: &mut Context<Self>,
2745 ) -> bool {
2746 self.editor
2747 .update(cx, |editor, cx| Item::navigate(editor, data, window, cx))
2748 }
2749
2750 fn deactivated(&mut self, window: &mut Window, cx: &mut Context<Self>) {
2751 self.editor
2752 .update(cx, |editor, cx| Item::deactivated(editor, window, cx))
2753 }
2754
2755 fn act_as_type<'a>(
2756 &'a self,
2757 type_id: TypeId,
2758 self_handle: &'a Entity<Self>,
2759 _: &'a App,
2760 ) -> Option<gpui::AnyEntity> {
2761 if type_id == TypeId::of::<Self>() {
2762 Some(self_handle.clone().into())
2763 } else if type_id == TypeId::of::<Editor>() {
2764 Some(self.editor.clone().into())
2765 } else {
2766 None
2767 }
2768 }
2769
2770 fn include_in_nav_history() -> bool {
2771 false
2772 }
2773}
2774
2775impl SearchableItem for TextThreadEditor {
2776 type Match = <Editor as SearchableItem>::Match;
2777
2778 fn clear_matches(&mut self, window: &mut Window, cx: &mut Context<Self>) {
2779 self.editor.update(cx, |editor, cx| {
2780 editor.clear_matches(window, cx);
2781 });
2782 }
2783
2784 fn update_matches(
2785 &mut self,
2786 matches: &[Self::Match],
2787 active_match_index: Option<usize>,
2788 window: &mut Window,
2789 cx: &mut Context<Self>,
2790 ) {
2791 self.editor.update(cx, |editor, cx| {
2792 editor.update_matches(matches, active_match_index, window, cx)
2793 });
2794 }
2795
2796 fn query_suggestion(&mut self, window: &mut Window, cx: &mut Context<Self>) -> String {
2797 self.editor
2798 .update(cx, |editor, cx| editor.query_suggestion(window, cx))
2799 }
2800
2801 fn activate_match(
2802 &mut self,
2803 index: usize,
2804 matches: &[Self::Match],
2805 window: &mut Window,
2806 cx: &mut Context<Self>,
2807 ) {
2808 self.editor.update(cx, |editor, cx| {
2809 editor.activate_match(index, matches, window, cx);
2810 });
2811 }
2812
2813 fn select_matches(
2814 &mut self,
2815 matches: &[Self::Match],
2816 window: &mut Window,
2817 cx: &mut Context<Self>,
2818 ) {
2819 self.editor
2820 .update(cx, |editor, cx| editor.select_matches(matches, window, cx));
2821 }
2822
2823 fn replace(
2824 &mut self,
2825 identifier: &Self::Match,
2826 query: &project::search::SearchQuery,
2827 window: &mut Window,
2828 cx: &mut Context<Self>,
2829 ) {
2830 self.editor.update(cx, |editor, cx| {
2831 editor.replace(identifier, query, window, cx)
2832 });
2833 }
2834
2835 fn find_matches(
2836 &mut self,
2837 query: Arc<project::search::SearchQuery>,
2838 window: &mut Window,
2839 cx: &mut Context<Self>,
2840 ) -> Task<Vec<Self::Match>> {
2841 self.editor
2842 .update(cx, |editor, cx| editor.find_matches(query, window, cx))
2843 }
2844
2845 fn active_match_index(
2846 &mut self,
2847 direction: Direction,
2848 matches: &[Self::Match],
2849 window: &mut Window,
2850 cx: &mut Context<Self>,
2851 ) -> Option<usize> {
2852 self.editor.update(cx, |editor, cx| {
2853 editor.active_match_index(direction, matches, window, cx)
2854 })
2855 }
2856}
2857
2858impl FollowableItem for TextThreadEditor {
2859 fn remote_id(&self) -> Option<workspace::ViewId> {
2860 self.remote_id
2861 }
2862
2863 fn to_state_proto(&self, window: &Window, cx: &App) -> Option<proto::view::Variant> {
2864 let text_thread = self.text_thread.read(cx);
2865 Some(proto::view::Variant::ContextEditor(
2866 proto::view::ContextEditor {
2867 context_id: text_thread.id().to_proto(),
2868 editor: if let Some(proto::view::Variant::Editor(proto)) =
2869 self.editor.read(cx).to_state_proto(window, cx)
2870 {
2871 Some(proto)
2872 } else {
2873 None
2874 },
2875 },
2876 ))
2877 }
2878
2879 fn from_state_proto(
2880 workspace: Entity<Workspace>,
2881 id: workspace::ViewId,
2882 state: &mut Option<proto::view::Variant>,
2883 window: &mut Window,
2884 cx: &mut App,
2885 ) -> Option<Task<Result<Entity<Self>>>> {
2886 let proto::view::Variant::ContextEditor(_) = state.as_ref()? else {
2887 return None;
2888 };
2889 let Some(proto::view::Variant::ContextEditor(state)) = state.take() else {
2890 unreachable!()
2891 };
2892
2893 let text_thread_id = TextThreadId::from_proto(state.context_id);
2894 let editor_state = state.editor?;
2895
2896 let project = workspace.read(cx).project().clone();
2897 let agent_panel_delegate = <dyn AgentPanelDelegate>::try_global(cx)?;
2898
2899 let text_thread_editor_task = workspace.update(cx, |workspace, cx| {
2900 agent_panel_delegate.open_remote_text_thread(workspace, text_thread_id, window, cx)
2901 });
2902
2903 Some(window.spawn(cx, async move |cx| {
2904 let text_thread_editor = text_thread_editor_task.await?;
2905 text_thread_editor
2906 .update_in(cx, |text_thread_editor, window, cx| {
2907 text_thread_editor.remote_id = Some(id);
2908 text_thread_editor.editor.update(cx, |editor, cx| {
2909 editor.apply_update_proto(
2910 &project,
2911 proto::update_view::Variant::Editor(proto::update_view::Editor {
2912 selections: editor_state.selections,
2913 pending_selection: editor_state.pending_selection,
2914 scroll_top_anchor: editor_state.scroll_top_anchor,
2915 scroll_x: editor_state.scroll_y,
2916 scroll_y: editor_state.scroll_y,
2917 ..Default::default()
2918 }),
2919 window,
2920 cx,
2921 )
2922 })
2923 })?
2924 .await?;
2925 Ok(text_thread_editor)
2926 }))
2927 }
2928
2929 fn to_follow_event(event: &Self::Event) -> Option<item::FollowEvent> {
2930 Editor::to_follow_event(event)
2931 }
2932
2933 fn add_event_to_update_proto(
2934 &self,
2935 event: &Self::Event,
2936 update: &mut Option<proto::update_view::Variant>,
2937 window: &Window,
2938 cx: &App,
2939 ) -> bool {
2940 self.editor
2941 .read(cx)
2942 .add_event_to_update_proto(event, update, window, cx)
2943 }
2944
2945 fn apply_update_proto(
2946 &mut self,
2947 project: &Entity<Project>,
2948 message: proto::update_view::Variant,
2949 window: &mut Window,
2950 cx: &mut Context<Self>,
2951 ) -> Task<Result<()>> {
2952 self.editor.update(cx, |editor, cx| {
2953 editor.apply_update_proto(project, message, window, cx)
2954 })
2955 }
2956
2957 fn is_project_item(&self, _window: &Window, _cx: &App) -> bool {
2958 true
2959 }
2960
2961 fn set_leader_id(
2962 &mut self,
2963 leader_id: Option<CollaboratorId>,
2964 window: &mut Window,
2965 cx: &mut Context<Self>,
2966 ) {
2967 self.editor
2968 .update(cx, |editor, cx| editor.set_leader_id(leader_id, window, cx))
2969 }
2970
2971 fn dedup(&self, existing: &Self, _window: &Window, cx: &App) -> Option<item::Dedup> {
2972 if existing.text_thread.read(cx).id() == self.text_thread.read(cx).id() {
2973 Some(item::Dedup::KeepExisting)
2974 } else {
2975 None
2976 }
2977 }
2978}
2979
2980enum PendingSlashCommand {}
2981
2982fn invoked_slash_command_fold_placeholder(
2983 command_id: InvokedSlashCommandId,
2984 text_thread: WeakEntity<TextThread>,
2985) -> FoldPlaceholder {
2986 FoldPlaceholder {
2987 constrain_width: false,
2988 merge_adjacent: false,
2989 render: Arc::new(move |fold_id, _, cx| {
2990 let Some(text_thread) = text_thread.upgrade() else {
2991 return Empty.into_any();
2992 };
2993
2994 let Some(command) = text_thread.read(cx).invoked_slash_command(&command_id) else {
2995 return Empty.into_any();
2996 };
2997
2998 h_flex()
2999 .id(fold_id)
3000 .px_1()
3001 .ml_6()
3002 .gap_2()
3003 .bg(cx.theme().colors().surface_background)
3004 .rounded_sm()
3005 .child(Label::new(format!("/{}", command.name)))
3006 .map(|parent| match &command.status {
3007 InvokedSlashCommandStatus::Running(_) => {
3008 parent.child(Icon::new(IconName::ArrowCircle).with_rotate_animation(4))
3009 }
3010 InvokedSlashCommandStatus::Error(message) => parent.child(
3011 Label::new(format!("error: {message}"))
3012 .single_line()
3013 .color(Color::Error),
3014 ),
3015 InvokedSlashCommandStatus::Finished => parent,
3016 })
3017 .into_any_element()
3018 }),
3019 type_tag: Some(TypeId::of::<PendingSlashCommand>()),
3020 }
3021}
3022
3023enum TokenState {
3024 NoTokensLeft {
3025 max_token_count: u64,
3026 token_count: u64,
3027 },
3028 HasMoreTokens {
3029 max_token_count: u64,
3030 token_count: u64,
3031 over_warn_threshold: bool,
3032 },
3033}
3034
3035fn token_state(text_thread: &Entity<TextThread>, cx: &App) -> Option<TokenState> {
3036 const WARNING_TOKEN_THRESHOLD: f32 = 0.8;
3037
3038 let model = LanguageModelRegistry::read_global(cx)
3039 .default_model()?
3040 .model;
3041 let token_count = text_thread.read(cx).token_count()?;
3042 let max_token_count =
3043 model.max_token_count_for_mode(text_thread.read(cx).completion_mode().into());
3044 let token_state = if max_token_count.saturating_sub(token_count) == 0 {
3045 TokenState::NoTokensLeft {
3046 max_token_count,
3047 token_count,
3048 }
3049 } else {
3050 let over_warn_threshold =
3051 token_count as f32 / max_token_count as f32 >= WARNING_TOKEN_THRESHOLD;
3052 TokenState::HasMoreTokens {
3053 max_token_count,
3054 token_count,
3055 over_warn_threshold,
3056 }
3057 };
3058 Some(token_state)
3059}
3060
3061fn size_for_image(data: &RenderImage, max_size: Size<Pixels>) -> Size<Pixels> {
3062 let image_size = data
3063 .size(0)
3064 .map(|dimension| Pixels::from(u32::from(dimension)));
3065 let image_ratio = image_size.width / image_size.height;
3066 let bounds_ratio = max_size.width / max_size.height;
3067
3068 if image_size.width > max_size.width || image_size.height > max_size.height {
3069 if bounds_ratio > image_ratio {
3070 size(
3071 image_size.width * (max_size.height / image_size.height),
3072 max_size.height,
3073 )
3074 } else {
3075 size(
3076 max_size.width,
3077 image_size.height * (max_size.width / image_size.width),
3078 )
3079 }
3080 } else {
3081 size(image_size.width, image_size.height)
3082 }
3083}
3084
3085pub fn humanize_token_count(count: u64) -> String {
3086 match count {
3087 0..=999 => count.to_string(),
3088 1000..=9999 => {
3089 let thousands = count / 1000;
3090 let hundreds = (count % 1000 + 50) / 100;
3091 if hundreds == 0 {
3092 format!("{}k", thousands)
3093 } else if hundreds == 10 {
3094 format!("{}k", thousands + 1)
3095 } else {
3096 format!("{}.{}k", thousands, hundreds)
3097 }
3098 }
3099 1_000_000..=9_999_999 => {
3100 let millions = count / 1_000_000;
3101 let hundred_thousands = (count % 1_000_000 + 50_000) / 100_000;
3102 if hundred_thousands == 0 {
3103 format!("{}M", millions)
3104 } else if hundred_thousands == 10 {
3105 format!("{}M", millions + 1)
3106 } else {
3107 format!("{}.{}M", millions, hundred_thousands)
3108 }
3109 }
3110 10_000_000.. => format!("{}M", (count + 500_000) / 1_000_000),
3111 _ => format!("{}k", (count + 500) / 1000),
3112 }
3113}
3114
3115pub fn make_lsp_adapter_delegate(
3116 project: &Entity<Project>,
3117 cx: &mut App,
3118) -> Result<Option<Arc<dyn LspAdapterDelegate>>> {
3119 project.update(cx, |project, cx| {
3120 // TODO: Find the right worktree.
3121 let Some(worktree) = project.worktrees(cx).next() else {
3122 return Ok(None::<Arc<dyn LspAdapterDelegate>>);
3123 };
3124 let http_client = project.client().http_client();
3125 project.lsp_store().update(cx, |_, cx| {
3126 Ok(Some(LocalLspAdapterDelegate::new(
3127 project.languages().clone(),
3128 project.environment(),
3129 cx.weak_entity(),
3130 &worktree,
3131 http_client,
3132 project.fs().clone(),
3133 cx,
3134 ) as Arc<dyn LspAdapterDelegate>))
3135 })
3136 })
3137}
3138
3139#[cfg(test)]
3140mod tests {
3141 use super::*;
3142 use editor::{MultiBufferOffset, SelectionEffects};
3143 use fs::FakeFs;
3144 use gpui::{App, TestAppContext, VisualTestContext};
3145 use indoc::indoc;
3146 use language::{Buffer, LanguageRegistry};
3147 use pretty_assertions::assert_eq;
3148 use prompt_store::PromptBuilder;
3149 use text::OffsetRangeExt;
3150 use unindent::Unindent;
3151 use util::path;
3152
3153 #[gpui::test]
3154 async fn test_copy_paste_whole_message(cx: &mut TestAppContext) {
3155 let (context, text_thread_editor, mut cx) = setup_text_thread_editor_text(vec![
3156 (Role::User, "What is the Zed editor?"),
3157 (
3158 Role::Assistant,
3159 "Zed is a modern, high-performance code editor designed from the ground up for speed and collaboration.",
3160 ),
3161 (Role::User, ""),
3162 ],cx).await;
3163
3164 // Select & Copy whole user message
3165 assert_copy_paste_text_thread_editor(
3166 &text_thread_editor,
3167 message_range(&context, 0, &mut cx),
3168 indoc! {"
3169 What is the Zed editor?
3170 Zed is a modern, high-performance code editor designed from the ground up for speed and collaboration.
3171 What is the Zed editor?
3172 "},
3173 &mut cx,
3174 );
3175
3176 // Select & Copy whole assistant message
3177 assert_copy_paste_text_thread_editor(
3178 &text_thread_editor,
3179 message_range(&context, 1, &mut cx),
3180 indoc! {"
3181 What is the Zed editor?
3182 Zed is a modern, high-performance code editor designed from the ground up for speed and collaboration.
3183 What is the Zed editor?
3184 Zed is a modern, high-performance code editor designed from the ground up for speed and collaboration.
3185 "},
3186 &mut cx,
3187 );
3188 }
3189
3190 #[gpui::test]
3191 async fn test_copy_paste_no_selection(cx: &mut TestAppContext) {
3192 let (context, text_thread_editor, mut cx) = setup_text_thread_editor_text(
3193 vec![
3194 (Role::User, "user1"),
3195 (Role::Assistant, "assistant1"),
3196 (Role::Assistant, "assistant2"),
3197 (Role::User, ""),
3198 ],
3199 cx,
3200 )
3201 .await;
3202
3203 // Copy and paste first assistant message
3204 let message_2_range = message_range(&context, 1, &mut cx);
3205 assert_copy_paste_text_thread_editor(
3206 &text_thread_editor,
3207 message_2_range.start..message_2_range.start,
3208 indoc! {"
3209 user1
3210 assistant1
3211 assistant2
3212 assistant1
3213 "},
3214 &mut cx,
3215 );
3216
3217 // Copy and cut second assistant message
3218 let message_3_range = message_range(&context, 2, &mut cx);
3219 assert_copy_paste_text_thread_editor(
3220 &text_thread_editor,
3221 message_3_range.start..message_3_range.start,
3222 indoc! {"
3223 user1
3224 assistant1
3225 assistant2
3226 assistant1
3227 assistant2
3228 "},
3229 &mut cx,
3230 );
3231 }
3232
3233 #[gpui::test]
3234 fn test_find_code_blocks(cx: &mut App) {
3235 let markdown = languages::language("markdown", tree_sitter_md::LANGUAGE.into());
3236
3237 let buffer = cx.new(|cx| {
3238 let text = r#"
3239 line 0
3240 line 1
3241 ```rust
3242 fn main() {}
3243 ```
3244 line 5
3245 line 6
3246 line 7
3247 ```go
3248 func main() {}
3249 ```
3250 line 11
3251 ```
3252 this is plain text code block
3253 ```
3254
3255 ```go
3256 func another() {}
3257 ```
3258 line 19
3259 "#
3260 .unindent();
3261 let mut buffer = Buffer::local(text, cx);
3262 buffer.set_language(Some(markdown.clone()), cx);
3263 buffer
3264 });
3265 let snapshot = buffer.read(cx).snapshot();
3266
3267 let code_blocks = vec![
3268 Point::new(3, 0)..Point::new(4, 0),
3269 Point::new(9, 0)..Point::new(10, 0),
3270 Point::new(13, 0)..Point::new(14, 0),
3271 Point::new(17, 0)..Point::new(18, 0),
3272 ]
3273 .into_iter()
3274 .map(|range| snapshot.point_to_offset(range.start)..snapshot.point_to_offset(range.end))
3275 .collect::<Vec<_>>();
3276
3277 let expected_results = vec![
3278 (0, None),
3279 (1, None),
3280 (2, Some(code_blocks[0].clone())),
3281 (3, Some(code_blocks[0].clone())),
3282 (4, Some(code_blocks[0].clone())),
3283 (5, None),
3284 (6, None),
3285 (7, None),
3286 (8, Some(code_blocks[1].clone())),
3287 (9, Some(code_blocks[1].clone())),
3288 (10, Some(code_blocks[1].clone())),
3289 (11, None),
3290 (12, Some(code_blocks[2].clone())),
3291 (13, Some(code_blocks[2].clone())),
3292 (14, Some(code_blocks[2].clone())),
3293 (15, None),
3294 (16, Some(code_blocks[3].clone())),
3295 (17, Some(code_blocks[3].clone())),
3296 (18, Some(code_blocks[3].clone())),
3297 (19, None),
3298 ];
3299
3300 for (row, expected) in expected_results {
3301 let offset = snapshot.point_to_offset(Point::new(row, 0));
3302 let range = find_surrounding_code_block(&snapshot, offset);
3303 assert_eq!(range, expected, "unexpected result on row {:?}", row);
3304 }
3305 }
3306
3307 async fn setup_text_thread_editor_text(
3308 messages: Vec<(Role, &str)>,
3309 cx: &mut TestAppContext,
3310 ) -> (
3311 Entity<TextThread>,
3312 Entity<TextThreadEditor>,
3313 VisualTestContext,
3314 ) {
3315 cx.update(init_test);
3316
3317 let fs = FakeFs::new(cx.executor());
3318 let text_thread = create_text_thread_with_messages(messages, cx);
3319
3320 let project = Project::test(fs.clone(), [path!("/test").as_ref()], cx).await;
3321 let window = cx.add_window(|window, cx| Workspace::test_new(project.clone(), window, cx));
3322 let workspace = window.root(cx).unwrap();
3323 let mut cx = VisualTestContext::from_window(*window, cx);
3324
3325 let text_thread_editor = window
3326 .update(&mut cx, |_, window, cx| {
3327 cx.new(|cx| {
3328 TextThreadEditor::for_text_thread(
3329 text_thread.clone(),
3330 fs,
3331 workspace.downgrade(),
3332 project,
3333 None,
3334 window,
3335 cx,
3336 )
3337 })
3338 })
3339 .unwrap();
3340
3341 (text_thread, text_thread_editor, cx)
3342 }
3343
3344 fn message_range(
3345 text_thread: &Entity<TextThread>,
3346 message_ix: usize,
3347 cx: &mut TestAppContext,
3348 ) -> Range<MultiBufferOffset> {
3349 let range = text_thread.update(cx, |text_thread, cx| {
3350 text_thread
3351 .messages(cx)
3352 .nth(message_ix)
3353 .unwrap()
3354 .anchor_range
3355 .to_offset(&text_thread.buffer().read(cx).snapshot())
3356 });
3357 MultiBufferOffset(range.start)..MultiBufferOffset(range.end)
3358 }
3359
3360 fn assert_copy_paste_text_thread_editor<T: editor::ToOffset>(
3361 text_thread_editor: &Entity<TextThreadEditor>,
3362 range: Range<T>,
3363 expected_text: &str,
3364 cx: &mut VisualTestContext,
3365 ) {
3366 text_thread_editor.update_in(cx, |text_thread_editor, window, cx| {
3367 text_thread_editor.editor.update(cx, |editor, cx| {
3368 editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
3369 s.select_ranges([range])
3370 });
3371 });
3372
3373 text_thread_editor.copy(&Default::default(), window, cx);
3374
3375 text_thread_editor.editor.update(cx, |editor, cx| {
3376 editor.move_to_end(&Default::default(), window, cx);
3377 });
3378
3379 text_thread_editor.paste(&Default::default(), window, cx);
3380
3381 text_thread_editor.editor.update(cx, |editor, cx| {
3382 assert_eq!(editor.text(cx), expected_text);
3383 });
3384 });
3385 }
3386
3387 fn create_text_thread_with_messages(
3388 mut messages: Vec<(Role, &str)>,
3389 cx: &mut TestAppContext,
3390 ) -> Entity<TextThread> {
3391 let registry = Arc::new(LanguageRegistry::test(cx.executor()));
3392 let prompt_builder = Arc::new(PromptBuilder::new(None).unwrap());
3393 cx.new(|cx| {
3394 let mut text_thread = TextThread::local(
3395 registry,
3396 None,
3397 prompt_builder.clone(),
3398 Arc::new(SlashCommandWorkingSet::default()),
3399 cx,
3400 );
3401 let mut message_1 = text_thread.messages(cx).next().unwrap();
3402 let (role, text) = messages.remove(0);
3403
3404 loop {
3405 if role == message_1.role {
3406 text_thread.buffer().update(cx, |buffer, cx| {
3407 buffer.edit([(message_1.offset_range, text)], None, cx);
3408 });
3409 break;
3410 }
3411 let mut ids = HashSet::default();
3412 ids.insert(message_1.id);
3413 text_thread.cycle_message_roles(ids, cx);
3414 message_1 = text_thread.messages(cx).next().unwrap();
3415 }
3416
3417 let mut last_message_id = message_1.id;
3418 for (role, text) in messages {
3419 text_thread.insert_message_after(last_message_id, role, MessageStatus::Done, cx);
3420 let message = text_thread.messages(cx).last().unwrap();
3421 last_message_id = message.id;
3422 text_thread.buffer().update(cx, |buffer, cx| {
3423 buffer.edit([(message.offset_range, text)], None, cx);
3424 })
3425 }
3426
3427 text_thread
3428 })
3429 }
3430
3431 fn init_test(cx: &mut App) {
3432 let settings_store = SettingsStore::test(cx);
3433 prompt_store::init(cx);
3434 LanguageModelRegistry::test(cx);
3435 cx.set_global(settings_store);
3436
3437 theme::init(theme::LoadThemes::JustBase, cx);
3438 }
3439}