1use crate::{
2 acp::completion_provider::{ContextPickerCompletionProvider, SlashCommandCompletion},
3 context_picker::{ContextPickerAction, fetch_context_picker::fetch_url_content},
4};
5use acp_thread::{MentionUri, selection_name};
6use agent::{HistoryStore, outline};
7use agent_client_protocol as acp;
8use agent_servers::{AgentServer, AgentServerDelegate};
9use anyhow::{Result, anyhow};
10use assistant_slash_commands::codeblock_fence_for_path;
11use collections::{HashMap, HashSet};
12use editor::{
13 Addon, Anchor, AnchorRangeExt, ContextMenuOptions, ContextMenuPlacement, Editor, EditorElement,
14 EditorEvent, EditorMode, EditorSnapshot, EditorStyle, ExcerptId, FoldPlaceholder, InlayId,
15 MultiBuffer, ToOffset,
16 actions::Paste,
17 display_map::{Crease, CreaseId, FoldId, Inlay},
18};
19use futures::{
20 FutureExt as _,
21 future::{Shared, join_all},
22};
23use gpui::{
24 Animation, AnimationExt as _, AppContext, ClipboardEntry, Context, Entity, EntityId,
25 EventEmitter, FocusHandle, Focusable, Image, ImageFormat, Img, KeyContext, SharedString,
26 Subscription, Task, TextStyle, WeakEntity, pulsating_between,
27};
28use language::{Buffer, Language, language_settings::InlayHintKind};
29use language_model::LanguageModelImage;
30use postage::stream::Stream as _;
31use project::{
32 CompletionIntent, InlayHint, InlayHintLabel, Project, ProjectItem, ProjectPath, Worktree,
33};
34use prompt_store::{PromptId, PromptStore};
35use rope::Point;
36use settings::Settings;
37use std::{
38 cell::RefCell,
39 ffi::OsStr,
40 fmt::Write,
41 ops::{Range, RangeInclusive},
42 path::{Path, PathBuf},
43 rc::Rc,
44 sync::Arc,
45 time::Duration,
46};
47use text::OffsetRangeExt;
48use theme::ThemeSettings;
49use ui::{ButtonLike, TintColor, Toggleable, prelude::*};
50use util::{ResultExt, debug_panic, rel_path::RelPath};
51use workspace::{Workspace, notifications::NotifyResultExt as _};
52use zed_actions::agent::Chat;
53
54pub struct MessageEditor {
55 mention_set: MentionSet,
56 editor: Entity<Editor>,
57 project: Entity<Project>,
58 workspace: WeakEntity<Workspace>,
59 history_store: Entity<HistoryStore>,
60 prompt_store: Option<Entity<PromptStore>>,
61 prompt_capabilities: Rc<RefCell<acp::PromptCapabilities>>,
62 available_commands: Rc<RefCell<Vec<acp::AvailableCommand>>>,
63 agent_name: SharedString,
64 _subscriptions: Vec<Subscription>,
65 _parse_slash_command_task: Task<()>,
66}
67
68#[derive(Clone, Copy, Debug)]
69pub enum MessageEditorEvent {
70 Send,
71 Cancel,
72 Focus,
73 LostFocus,
74}
75
76impl EventEmitter<MessageEditorEvent> for MessageEditor {}
77
78const COMMAND_HINT_INLAY_ID: u32 = 0;
79
80impl MessageEditor {
81 pub fn new(
82 workspace: WeakEntity<Workspace>,
83 project: Entity<Project>,
84 history_store: Entity<HistoryStore>,
85 prompt_store: Option<Entity<PromptStore>>,
86 prompt_capabilities: Rc<RefCell<acp::PromptCapabilities>>,
87 available_commands: Rc<RefCell<Vec<acp::AvailableCommand>>>,
88 agent_name: SharedString,
89 placeholder: &str,
90 mode: EditorMode,
91 window: &mut Window,
92 cx: &mut Context<Self>,
93 ) -> Self {
94 let language = Language::new(
95 language::LanguageConfig {
96 completion_query_characters: HashSet::from_iter(['.', '-', '_', '@']),
97 ..Default::default()
98 },
99 None,
100 );
101 let completion_provider = Rc::new(ContextPickerCompletionProvider::new(
102 cx.weak_entity(),
103 workspace.clone(),
104 history_store.clone(),
105 prompt_store.clone(),
106 prompt_capabilities.clone(),
107 available_commands.clone(),
108 ));
109 let mention_set = MentionSet::default();
110 let editor = cx.new(|cx| {
111 let buffer = cx.new(|cx| Buffer::local("", cx).with_language(Arc::new(language), cx));
112 let buffer = cx.new(|cx| MultiBuffer::singleton(buffer, cx));
113
114 let mut editor = Editor::new(mode, buffer, None, window, cx);
115 editor.set_placeholder_text(placeholder, window, cx);
116 editor.set_show_indent_guides(false, cx);
117 editor.set_soft_wrap();
118 editor.set_use_modal_editing(true);
119 editor.set_completion_provider(Some(completion_provider.clone()));
120 editor.set_context_menu_options(ContextMenuOptions {
121 min_entries_visible: 12,
122 max_entries_visible: 12,
123 placement: Some(ContextMenuPlacement::Above),
124 });
125 editor.register_addon(MessageEditorAddon::new());
126 editor
127 });
128
129 cx.on_focus_in(&editor.focus_handle(cx), window, |_, _, cx| {
130 cx.emit(MessageEditorEvent::Focus)
131 })
132 .detach();
133 cx.on_focus_out(&editor.focus_handle(cx), window, |_, _, _, cx| {
134 cx.emit(MessageEditorEvent::LostFocus)
135 })
136 .detach();
137
138 let mut has_hint = false;
139 let mut subscriptions = Vec::new();
140
141 subscriptions.push(cx.subscribe_in(&editor, window, {
142 move |this, editor, event, window, cx| {
143 if let EditorEvent::Edited { .. } = event
144 && !editor.read(cx).read_only(cx)
145 {
146 let snapshot = editor.update(cx, |editor, cx| {
147 let new_hints = this
148 .command_hint(editor.buffer(), cx)
149 .into_iter()
150 .collect::<Vec<_>>();
151 let has_new_hint = !new_hints.is_empty();
152 editor.splice_inlays(
153 if has_hint {
154 &[InlayId::Hint(COMMAND_HINT_INLAY_ID)]
155 } else {
156 &[]
157 },
158 new_hints,
159 cx,
160 );
161 has_hint = has_new_hint;
162
163 editor.snapshot(window, cx)
164 });
165 this.mention_set.remove_invalid(snapshot);
166
167 cx.notify();
168 }
169 }
170 }));
171
172 Self {
173 editor,
174 project,
175 mention_set,
176 workspace,
177 history_store,
178 prompt_store,
179 prompt_capabilities,
180 available_commands,
181 agent_name,
182 _subscriptions: subscriptions,
183 _parse_slash_command_task: Task::ready(()),
184 }
185 }
186
187 fn command_hint(&self, buffer: &Entity<MultiBuffer>, cx: &App) -> Option<Inlay> {
188 let available_commands = self.available_commands.borrow();
189 if available_commands.is_empty() {
190 return None;
191 }
192
193 let snapshot = buffer.read(cx).snapshot(cx);
194 let parsed_command = SlashCommandCompletion::try_parse(&snapshot.text(), 0)?;
195 if parsed_command.argument.is_some() {
196 return None;
197 }
198
199 let command_name = parsed_command.command?;
200 let available_command = available_commands
201 .iter()
202 .find(|command| command.name == command_name)?;
203
204 let acp::AvailableCommandInput::Unstructured { mut hint } =
205 available_command.input.clone()?;
206
207 let mut hint_pos = parsed_command.source_range.end + 1;
208 if hint_pos > snapshot.len() {
209 hint_pos = snapshot.len();
210 hint.insert(0, ' ');
211 }
212
213 let hint_pos = snapshot.anchor_after(hint_pos);
214
215 Some(Inlay::hint(
216 COMMAND_HINT_INLAY_ID,
217 hint_pos,
218 &InlayHint {
219 position: hint_pos.text_anchor,
220 label: InlayHintLabel::String(hint),
221 kind: Some(InlayHintKind::Parameter),
222 padding_left: false,
223 padding_right: false,
224 tooltip: None,
225 resolve_state: project::ResolveState::Resolved,
226 },
227 ))
228 }
229
230 pub fn insert_thread_summary(
231 &mut self,
232 thread: agent::DbThreadMetadata,
233 window: &mut Window,
234 cx: &mut Context<Self>,
235 ) {
236 let start = self.editor.update(cx, |editor, cx| {
237 editor.set_text(format!("{}\n", thread.title), window, cx);
238 editor
239 .buffer()
240 .read(cx)
241 .snapshot(cx)
242 .anchor_before(Point::zero())
243 .text_anchor
244 });
245
246 self.confirm_mention_completion(
247 thread.title.clone(),
248 start,
249 thread.title.len(),
250 MentionUri::Thread {
251 id: thread.id.clone(),
252 name: thread.title.to_string(),
253 },
254 window,
255 cx,
256 )
257 .detach();
258 }
259
260 #[cfg(test)]
261 pub(crate) fn editor(&self) -> &Entity<Editor> {
262 &self.editor
263 }
264
265 #[cfg(test)]
266 pub(crate) fn mention_set(&mut self) -> &mut MentionSet {
267 &mut self.mention_set
268 }
269
270 pub fn is_empty(&self, cx: &App) -> bool {
271 self.editor.read(cx).is_empty(cx)
272 }
273
274 pub fn mentions(&self) -> HashSet<MentionUri> {
275 self.mention_set
276 .mentions
277 .values()
278 .map(|(uri, _)| uri.clone())
279 .collect()
280 }
281
282 pub fn confirm_mention_completion(
283 &mut self,
284 crease_text: SharedString,
285 start: text::Anchor,
286 content_len: usize,
287 mention_uri: MentionUri,
288 window: &mut Window,
289 cx: &mut Context<Self>,
290 ) -> Task<()> {
291 let snapshot = self
292 .editor
293 .update(cx, |editor, cx| editor.snapshot(window, cx));
294 let Some(start_anchor) = snapshot.buffer_snapshot().as_singleton_anchor(start) else {
295 return Task::ready(());
296 };
297 let excerpt_id = start_anchor.excerpt_id;
298 let end_anchor = snapshot
299 .buffer_snapshot()
300 .anchor_before(start_anchor.to_offset(&snapshot.buffer_snapshot()) + content_len + 1);
301
302 let crease = if let MentionUri::File { abs_path } = &mention_uri
303 && let Some(extension) = abs_path.extension()
304 && let Some(extension) = extension.to_str()
305 && Img::extensions().contains(&extension)
306 && !extension.contains("svg")
307 {
308 let Some(project_path) = self
309 .project
310 .read(cx)
311 .project_path_for_absolute_path(&abs_path, cx)
312 else {
313 log::error!("project path not found");
314 return Task::ready(());
315 };
316 let image = self
317 .project
318 .update(cx, |project, cx| project.open_image(project_path, cx));
319 let image = cx
320 .spawn(async move |_, cx| {
321 let image = image.await.map_err(|e| e.to_string())?;
322 let image = image
323 .update(cx, |image, _| image.image.clone())
324 .map_err(|e| e.to_string())?;
325 Ok(image)
326 })
327 .shared();
328 insert_crease_for_mention(
329 excerpt_id,
330 start,
331 content_len,
332 mention_uri.name().into(),
333 IconName::Image.path().into(),
334 Some(image),
335 self.editor.clone(),
336 window,
337 cx,
338 )
339 } else {
340 insert_crease_for_mention(
341 excerpt_id,
342 start,
343 content_len,
344 crease_text,
345 mention_uri.icon_path(cx),
346 None,
347 self.editor.clone(),
348 window,
349 cx,
350 )
351 };
352 let Some((crease_id, tx)) = crease else {
353 return Task::ready(());
354 };
355
356 let task = match mention_uri.clone() {
357 MentionUri::Fetch { url } => self.confirm_mention_for_fetch(url, cx),
358 MentionUri::Directory { .. } => Task::ready(Ok(Mention::UriOnly)),
359 MentionUri::Thread { id, .. } => self.confirm_mention_for_thread(id, cx),
360 MentionUri::TextThread { path, .. } => self.confirm_mention_for_text_thread(path, cx),
361 MentionUri::File { abs_path } => self.confirm_mention_for_file(abs_path, cx),
362 MentionUri::Symbol {
363 abs_path,
364 line_range,
365 ..
366 } => self.confirm_mention_for_symbol(abs_path, line_range, cx),
367 MentionUri::Rule { id, .. } => self.confirm_mention_for_rule(id, cx),
368 MentionUri::PastedImage => {
369 debug_panic!("pasted image URI should not be included in completions");
370 Task::ready(Err(anyhow!(
371 "pasted imaged URI should not be included in completions"
372 )))
373 }
374 MentionUri::Selection { .. } => {
375 // Handled elsewhere
376 debug_panic!("unexpected selection URI");
377 Task::ready(Err(anyhow!("unexpected selection URI")))
378 }
379 };
380 let task = cx
381 .spawn(async move |_, _| task.await.map_err(|e| e.to_string()))
382 .shared();
383 self.mention_set
384 .mentions
385 .insert(crease_id, (mention_uri, task.clone()));
386
387 // Notify the user if we failed to load the mentioned context
388 cx.spawn_in(window, async move |this, cx| {
389 let result = task.await.notify_async_err(cx);
390 drop(tx);
391 if result.is_none() {
392 this.update(cx, |this, cx| {
393 this.editor.update(cx, |editor, cx| {
394 // Remove mention
395 editor.edit([(start_anchor..end_anchor, "")], cx);
396 });
397 this.mention_set.mentions.remove(&crease_id);
398 })
399 .ok();
400 }
401 })
402 }
403
404 fn confirm_mention_for_file(
405 &mut self,
406 abs_path: PathBuf,
407 cx: &mut Context<Self>,
408 ) -> Task<Result<Mention>> {
409 let Some(project_path) = self
410 .project
411 .read(cx)
412 .project_path_for_absolute_path(&abs_path, cx)
413 else {
414 return Task::ready(Err(anyhow!("project path not found")));
415 };
416 let extension = abs_path
417 .extension()
418 .and_then(OsStr::to_str)
419 .unwrap_or_default();
420
421 if Img::extensions().contains(&extension) && !extension.contains("svg") {
422 if !self.prompt_capabilities.borrow().image {
423 return Task::ready(Err(anyhow!("This model does not support images yet")));
424 }
425 let task = self
426 .project
427 .update(cx, |project, cx| project.open_image(project_path, cx));
428 return cx.spawn(async move |_, cx| {
429 let image = task.await?;
430 let image = image.update(cx, |image, _| image.image.clone())?;
431 let format = image.format;
432 let image = cx
433 .update(|cx| LanguageModelImage::from_image(image, cx))?
434 .await;
435 if let Some(image) = image {
436 Ok(Mention::Image(MentionImage {
437 data: image.source,
438 format,
439 }))
440 } else {
441 Err(anyhow!("Failed to convert image"))
442 }
443 });
444 }
445
446 let buffer = self
447 .project
448 .update(cx, |project, cx| project.open_buffer(project_path, cx));
449 cx.spawn(async move |_, cx| {
450 let buffer = buffer.await?;
451 let buffer_content = outline::get_buffer_content_or_outline(
452 buffer.clone(),
453 Some(&abs_path.to_string_lossy()),
454 &cx,
455 )
456 .await?;
457
458 Ok(Mention::Text {
459 content: buffer_content.text,
460 tracked_buffers: vec![buffer],
461 })
462 })
463 }
464
465 fn confirm_mention_for_fetch(
466 &mut self,
467 url: url::Url,
468 cx: &mut Context<Self>,
469 ) -> Task<Result<Mention>> {
470 let http_client = match self
471 .workspace
472 .update(cx, |workspace, _| workspace.client().http_client())
473 {
474 Ok(http_client) => http_client,
475 Err(e) => return Task::ready(Err(e)),
476 };
477 cx.background_executor().spawn(async move {
478 let content = fetch_url_content(http_client, url.to_string()).await?;
479 Ok(Mention::Text {
480 content,
481 tracked_buffers: Vec::new(),
482 })
483 })
484 }
485
486 fn confirm_mention_for_symbol(
487 &mut self,
488 abs_path: PathBuf,
489 line_range: RangeInclusive<u32>,
490 cx: &mut Context<Self>,
491 ) -> Task<Result<Mention>> {
492 let Some(project_path) = self
493 .project
494 .read(cx)
495 .project_path_for_absolute_path(&abs_path, cx)
496 else {
497 return Task::ready(Err(anyhow!("project path not found")));
498 };
499 let buffer = self
500 .project
501 .update(cx, |project, cx| project.open_buffer(project_path, cx));
502 cx.spawn(async move |_, cx| {
503 let buffer = buffer.await?;
504 let mention = buffer.update(cx, |buffer, cx| {
505 let start = Point::new(*line_range.start(), 0).min(buffer.max_point());
506 let end = Point::new(*line_range.end() + 1, 0).min(buffer.max_point());
507 let content = buffer.text_for_range(start..end).collect();
508 Mention::Text {
509 content,
510 tracked_buffers: vec![cx.entity()],
511 }
512 })?;
513 anyhow::Ok(mention)
514 })
515 }
516
517 fn confirm_mention_for_rule(
518 &mut self,
519 id: PromptId,
520 cx: &mut Context<Self>,
521 ) -> Task<Result<Mention>> {
522 let Some(prompt_store) = self.prompt_store.clone() else {
523 return Task::ready(Err(anyhow!("missing prompt store")));
524 };
525 let prompt = prompt_store.read(cx).load(id, cx);
526 cx.spawn(async move |_, _| {
527 let prompt = prompt.await?;
528 Ok(Mention::Text {
529 content: prompt,
530 tracked_buffers: Vec::new(),
531 })
532 })
533 }
534
535 pub fn confirm_mention_for_selection(
536 &mut self,
537 source_range: Range<text::Anchor>,
538 selections: Vec<(Entity<Buffer>, Range<text::Anchor>, Range<usize>)>,
539 window: &mut Window,
540 cx: &mut Context<Self>,
541 ) {
542 let snapshot = self.editor.read(cx).buffer().read(cx).snapshot(cx);
543 let Some(start) = snapshot.as_singleton_anchor(source_range.start) else {
544 return;
545 };
546
547 let offset = start.to_offset(&snapshot);
548
549 for (buffer, selection_range, range_to_fold) in selections {
550 let range = snapshot.anchor_after(offset + range_to_fold.start)
551 ..snapshot.anchor_after(offset + range_to_fold.end);
552
553 let abs_path = buffer
554 .read(cx)
555 .project_path(cx)
556 .and_then(|project_path| self.project.read(cx).absolute_path(&project_path, cx));
557 let snapshot = buffer.read(cx).snapshot();
558
559 let text = snapshot
560 .text_for_range(selection_range.clone())
561 .collect::<String>();
562 let point_range = selection_range.to_point(&snapshot);
563 let line_range = point_range.start.row..=point_range.end.row;
564
565 let uri = MentionUri::Selection {
566 abs_path: abs_path.clone(),
567 line_range: line_range.clone(),
568 };
569 let crease = crate::context_picker::crease_for_mention(
570 selection_name(abs_path.as_deref(), &line_range).into(),
571 uri.icon_path(cx),
572 range,
573 self.editor.downgrade(),
574 );
575
576 let crease_id = self.editor.update(cx, |editor, cx| {
577 let crease_ids = editor.insert_creases(vec![crease.clone()], cx);
578 editor.fold_creases(vec![crease], false, window, cx);
579 crease_ids.first().copied().unwrap()
580 });
581
582 self.mention_set.mentions.insert(
583 crease_id,
584 (
585 uri,
586 Task::ready(Ok(Mention::Text {
587 content: text,
588 tracked_buffers: vec![buffer],
589 }))
590 .shared(),
591 ),
592 );
593 }
594 }
595
596 fn confirm_mention_for_thread(
597 &mut self,
598 id: acp::SessionId,
599 cx: &mut Context<Self>,
600 ) -> Task<Result<Mention>> {
601 let server = Rc::new(agent::NativeAgentServer::new(
602 self.project.read(cx).fs().clone(),
603 self.history_store.clone(),
604 ));
605 let delegate = AgentServerDelegate::new(
606 self.project.read(cx).agent_server_store().clone(),
607 self.project.clone(),
608 None,
609 None,
610 );
611 let connection = server.connect(None, delegate, cx);
612 cx.spawn(async move |_, cx| {
613 let (agent, _) = connection.await?;
614 let agent = agent.downcast::<agent::NativeAgentConnection>().unwrap();
615 let summary = agent
616 .0
617 .update(cx, |agent, cx| agent.thread_summary(id, cx))?
618 .await?;
619 anyhow::Ok(Mention::Text {
620 content: summary.to_string(),
621 tracked_buffers: Vec::new(),
622 })
623 })
624 }
625
626 fn confirm_mention_for_text_thread(
627 &mut self,
628 path: PathBuf,
629 cx: &mut Context<Self>,
630 ) -> Task<Result<Mention>> {
631 let context = self.history_store.update(cx, |store, cx| {
632 store.load_text_thread(path.as_path().into(), cx)
633 });
634 cx.spawn(async move |_, cx| {
635 let context = context.await?;
636 let xml = context.update(cx, |context, cx| context.to_xml(cx))?;
637 Ok(Mention::Text {
638 content: xml,
639 tracked_buffers: Vec::new(),
640 })
641 })
642 }
643
644 fn validate_slash_commands(
645 text: &str,
646 available_commands: &[acp::AvailableCommand],
647 agent_name: &str,
648 ) -> Result<()> {
649 if let Some(parsed_command) = SlashCommandCompletion::try_parse(text, 0) {
650 if let Some(command_name) = parsed_command.command {
651 // Check if this command is in the list of available commands from the server
652 let is_supported = available_commands
653 .iter()
654 .any(|cmd| cmd.name == command_name);
655
656 if !is_supported {
657 return Err(anyhow!(
658 "The /{} command is not supported by {}.\n\nAvailable commands: {}",
659 command_name,
660 agent_name,
661 if available_commands.is_empty() {
662 "none".to_string()
663 } else {
664 available_commands
665 .iter()
666 .map(|cmd| format!("/{}", cmd.name))
667 .collect::<Vec<_>>()
668 .join(", ")
669 }
670 ));
671 }
672 }
673 }
674 Ok(())
675 }
676
677 pub fn contents(
678 &self,
679 full_mention_content: bool,
680 cx: &mut Context<Self>,
681 ) -> Task<Result<(Vec<acp::ContentBlock>, Vec<Entity<Buffer>>)>> {
682 // Check for unsupported slash commands before spawning async task
683 let text = self.editor.read(cx).text(cx);
684 let available_commands = self.available_commands.borrow().clone();
685 if let Err(err) =
686 Self::validate_slash_commands(&text, &available_commands, &self.agent_name)
687 {
688 return Task::ready(Err(err));
689 }
690
691 let contents = self.mention_set.contents(
692 &self.prompt_capabilities.borrow(),
693 full_mention_content,
694 self.project.clone(),
695 cx,
696 );
697 let editor = self.editor.clone();
698
699 cx.spawn(async move |_, cx| {
700 let contents = contents.await?;
701 let mut all_tracked_buffers = Vec::new();
702
703 let result = editor.update(cx, |editor, cx| {
704 let mut ix = 0;
705 let mut chunks: Vec<acp::ContentBlock> = Vec::new();
706 let text = editor.text(cx);
707 editor.display_map.update(cx, |map, cx| {
708 let snapshot = map.snapshot(cx);
709 for (crease_id, crease) in snapshot.crease_snapshot.creases() {
710 let Some((uri, mention)) = contents.get(&crease_id) else {
711 continue;
712 };
713
714 let crease_range = crease.range().to_offset(&snapshot.buffer_snapshot());
715 if crease_range.start > ix {
716 //todo(): Custom slash command ContentBlock?
717 // let chunk = if prevent_slash_commands
718 // && ix == 0
719 // && parse_slash_command(&text[ix..]).is_some()
720 // {
721 // format!(" {}", &text[ix..crease_range.start]).into()
722 // } else {
723 // text[ix..crease_range.start].into()
724 // };
725 let chunk = text[ix..crease_range.start].into();
726 chunks.push(chunk);
727 }
728 let chunk = match mention {
729 Mention::Text {
730 content,
731 tracked_buffers,
732 } => {
733 all_tracked_buffers.extend(tracked_buffers.iter().cloned());
734 acp::ContentBlock::Resource(acp::EmbeddedResource {
735 annotations: None,
736 resource: acp::EmbeddedResourceResource::TextResourceContents(
737 acp::TextResourceContents {
738 mime_type: None,
739 text: content.clone(),
740 uri: uri.to_uri().to_string(),
741 meta: None,
742 },
743 ),
744 meta: None,
745 })
746 }
747 Mention::Image(mention_image) => {
748 let uri = match uri {
749 MentionUri::File { .. } => Some(uri.to_uri().to_string()),
750 MentionUri::PastedImage => None,
751 other => {
752 debug_panic!(
753 "unexpected mention uri for image: {:?}",
754 other
755 );
756 None
757 }
758 };
759 acp::ContentBlock::Image(acp::ImageContent {
760 annotations: None,
761 data: mention_image.data.to_string(),
762 mime_type: mention_image.format.mime_type().into(),
763 uri,
764 meta: None,
765 })
766 }
767 Mention::UriOnly => {
768 acp::ContentBlock::ResourceLink(acp::ResourceLink {
769 name: uri.name(),
770 uri: uri.to_uri().to_string(),
771 annotations: None,
772 description: None,
773 mime_type: None,
774 size: None,
775 title: None,
776 meta: None,
777 })
778 }
779 };
780 chunks.push(chunk);
781 ix = crease_range.end;
782 }
783
784 if ix < text.len() {
785 //todo(): Custom slash command ContentBlock?
786 // let last_chunk = if prevent_slash_commands
787 // && ix == 0
788 // && parse_slash_command(&text[ix..]).is_some()
789 // {
790 // format!(" {}", text[ix..].trim_end())
791 // } else {
792 // text[ix..].trim_end().to_owned()
793 // };
794 let last_chunk = text[ix..].trim_end().to_owned();
795 if !last_chunk.is_empty() {
796 chunks.push(last_chunk.into());
797 }
798 }
799 });
800 Ok((chunks, all_tracked_buffers))
801 })?;
802 result
803 })
804 }
805
806 pub fn clear(&mut self, window: &mut Window, cx: &mut Context<Self>) {
807 self.editor.update(cx, |editor, cx| {
808 editor.clear(window, cx);
809 editor.remove_creases(
810 self.mention_set
811 .mentions
812 .drain()
813 .map(|(crease_id, _)| crease_id),
814 cx,
815 )
816 });
817 }
818
819 pub fn send(&mut self, cx: &mut Context<Self>) {
820 if self.is_empty(cx) {
821 return;
822 }
823 self.editor.update(cx, |editor, cx| {
824 editor.clear_inlay_hints(cx);
825 });
826 cx.emit(MessageEditorEvent::Send)
827 }
828
829 fn chat(&mut self, _: &Chat, _: &mut Window, cx: &mut Context<Self>) {
830 self.send(cx);
831 }
832
833 fn cancel(&mut self, _: &editor::actions::Cancel, _: &mut Window, cx: &mut Context<Self>) {
834 cx.emit(MessageEditorEvent::Cancel)
835 }
836
837 fn paste(&mut self, _: &Paste, window: &mut Window, cx: &mut Context<Self>) {
838 if !self.prompt_capabilities.borrow().image {
839 return;
840 }
841
842 let images = cx
843 .read_from_clipboard()
844 .map(|item| {
845 item.into_entries()
846 .filter_map(|entry| {
847 if let ClipboardEntry::Image(image) = entry {
848 Some(image)
849 } else {
850 None
851 }
852 })
853 .collect::<Vec<_>>()
854 })
855 .unwrap_or_default();
856
857 if images.is_empty() {
858 return;
859 }
860 cx.stop_propagation();
861
862 let replacement_text = MentionUri::PastedImage.as_link().to_string();
863 for image in images {
864 let (excerpt_id, text_anchor, multibuffer_anchor) =
865 self.editor.update(cx, |message_editor, cx| {
866 let snapshot = message_editor.snapshot(window, cx);
867 let (excerpt_id, _, buffer_snapshot) =
868 snapshot.buffer_snapshot().as_singleton().unwrap();
869
870 let text_anchor = buffer_snapshot.anchor_before(buffer_snapshot.len());
871 let multibuffer_anchor = snapshot
872 .buffer_snapshot()
873 .anchor_in_excerpt(*excerpt_id, text_anchor);
874 message_editor.edit(
875 [(
876 multi_buffer::Anchor::max()..multi_buffer::Anchor::max(),
877 format!("{replacement_text} "),
878 )],
879 cx,
880 );
881 (*excerpt_id, text_anchor, multibuffer_anchor)
882 });
883
884 let content_len = replacement_text.len();
885 let Some(start_anchor) = multibuffer_anchor else {
886 continue;
887 };
888 let end_anchor = self.editor.update(cx, |editor, cx| {
889 let snapshot = editor.buffer().read(cx).snapshot(cx);
890 snapshot.anchor_before(start_anchor.to_offset(&snapshot) + content_len)
891 });
892 let image = Arc::new(image);
893 let Some((crease_id, tx)) = insert_crease_for_mention(
894 excerpt_id,
895 text_anchor,
896 content_len,
897 MentionUri::PastedImage.name().into(),
898 IconName::Image.path().into(),
899 Some(Task::ready(Ok(image.clone())).shared()),
900 self.editor.clone(),
901 window,
902 cx,
903 ) else {
904 continue;
905 };
906 let task = cx
907 .spawn_in(window, {
908 async move |_, cx| {
909 let format = image.format;
910 let image = cx
911 .update(|_, cx| LanguageModelImage::from_image(image, cx))
912 .map_err(|e| e.to_string())?
913 .await;
914 drop(tx);
915 if let Some(image) = image {
916 Ok(Mention::Image(MentionImage {
917 data: image.source,
918 format,
919 }))
920 } else {
921 Err("Failed to convert image".into())
922 }
923 }
924 })
925 .shared();
926
927 self.mention_set
928 .mentions
929 .insert(crease_id, (MentionUri::PastedImage, task.clone()));
930
931 cx.spawn_in(window, async move |this, cx| {
932 if task.await.notify_async_err(cx).is_none() {
933 this.update(cx, |this, cx| {
934 this.editor.update(cx, |editor, cx| {
935 editor.edit([(start_anchor..end_anchor, "")], cx);
936 });
937 this.mention_set.mentions.remove(&crease_id);
938 })
939 .ok();
940 }
941 })
942 .detach();
943 }
944 }
945
946 pub fn insert_dragged_files(
947 &mut self,
948 paths: Vec<project::ProjectPath>,
949 added_worktrees: Vec<Entity<Worktree>>,
950 window: &mut Window,
951 cx: &mut Context<Self>,
952 ) {
953 let path_style = self.project.read(cx).path_style(cx);
954 let buffer = self.editor.read(cx).buffer().clone();
955 let Some(buffer) = buffer.read(cx).as_singleton() else {
956 return;
957 };
958 let mut tasks = Vec::new();
959 for path in paths {
960 let Some(entry) = self.project.read(cx).entry_for_path(&path, cx) else {
961 continue;
962 };
963 let Some(worktree) = self.project.read(cx).worktree_for_id(path.worktree_id, cx) else {
964 continue;
965 };
966 let abs_path = worktree.read(cx).absolutize(&path.path);
967 let (file_name, _) =
968 crate::context_picker::file_context_picker::extract_file_name_and_directory(
969 &path.path,
970 worktree.read(cx).root_name(),
971 path_style,
972 );
973
974 let uri = if entry.is_dir() {
975 MentionUri::Directory { abs_path }
976 } else {
977 MentionUri::File { abs_path }
978 };
979
980 let new_text = format!("{} ", uri.as_link());
981 let content_len = new_text.len() - 1;
982
983 let anchor = buffer.update(cx, |buffer, _cx| buffer.anchor_before(buffer.len()));
984
985 self.editor.update(cx, |message_editor, cx| {
986 message_editor.edit(
987 [(
988 multi_buffer::Anchor::max()..multi_buffer::Anchor::max(),
989 new_text,
990 )],
991 cx,
992 );
993 });
994 tasks.push(self.confirm_mention_completion(
995 file_name,
996 anchor,
997 content_len,
998 uri,
999 window,
1000 cx,
1001 ));
1002 }
1003 cx.spawn(async move |_, _| {
1004 join_all(tasks).await;
1005 drop(added_worktrees);
1006 })
1007 .detach();
1008 }
1009
1010 pub fn insert_selections(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1011 let editor = self.editor.read(cx);
1012 let editor_buffer = editor.buffer().read(cx);
1013 let Some(buffer) = editor_buffer.as_singleton() else {
1014 return;
1015 };
1016 let cursor_anchor = editor.selections.newest_anchor().head();
1017 let cursor_offset = cursor_anchor.to_offset(&editor_buffer.snapshot(cx));
1018 let anchor = buffer.update(cx, |buffer, _cx| {
1019 buffer.anchor_before(cursor_offset.min(buffer.len()))
1020 });
1021 let Some(workspace) = self.workspace.upgrade() else {
1022 return;
1023 };
1024 let Some(completion) = ContextPickerCompletionProvider::completion_for_action(
1025 ContextPickerAction::AddSelections,
1026 anchor..anchor,
1027 cx.weak_entity(),
1028 &workspace,
1029 cx,
1030 ) else {
1031 return;
1032 };
1033
1034 self.editor.update(cx, |message_editor, cx| {
1035 message_editor.edit([(cursor_anchor..cursor_anchor, completion.new_text)], cx);
1036 });
1037 if let Some(confirm) = completion.confirm {
1038 confirm(CompletionIntent::Complete, window, cx);
1039 }
1040 }
1041
1042 pub fn set_read_only(&mut self, read_only: bool, cx: &mut Context<Self>) {
1043 self.editor.update(cx, |message_editor, cx| {
1044 message_editor.set_read_only(read_only);
1045 cx.notify()
1046 })
1047 }
1048
1049 pub fn set_mode(&mut self, mode: EditorMode, cx: &mut Context<Self>) {
1050 self.editor.update(cx, |editor, cx| {
1051 editor.set_mode(mode);
1052 cx.notify()
1053 });
1054 }
1055
1056 pub fn set_message(
1057 &mut self,
1058 message: Vec<acp::ContentBlock>,
1059 window: &mut Window,
1060 cx: &mut Context<Self>,
1061 ) {
1062 self.clear(window, cx);
1063
1064 let path_style = self.project.read(cx).path_style(cx);
1065 let mut text = String::new();
1066 let mut mentions = Vec::new();
1067
1068 for chunk in message {
1069 match chunk {
1070 acp::ContentBlock::Text(text_content) => {
1071 text.push_str(&text_content.text);
1072 }
1073 acp::ContentBlock::Resource(acp::EmbeddedResource {
1074 resource: acp::EmbeddedResourceResource::TextResourceContents(resource),
1075 ..
1076 }) => {
1077 let Some(mention_uri) = MentionUri::parse(&resource.uri, path_style).log_err()
1078 else {
1079 continue;
1080 };
1081 let start = text.len();
1082 write!(&mut text, "{}", mention_uri.as_link()).ok();
1083 let end = text.len();
1084 mentions.push((
1085 start..end,
1086 mention_uri,
1087 Mention::Text {
1088 content: resource.text,
1089 tracked_buffers: Vec::new(),
1090 },
1091 ));
1092 }
1093 acp::ContentBlock::ResourceLink(resource) => {
1094 if let Some(mention_uri) =
1095 MentionUri::parse(&resource.uri, path_style).log_err()
1096 {
1097 let start = text.len();
1098 write!(&mut text, "{}", mention_uri.as_link()).ok();
1099 let end = text.len();
1100 mentions.push((start..end, mention_uri, Mention::UriOnly));
1101 }
1102 }
1103 acp::ContentBlock::Image(acp::ImageContent {
1104 uri,
1105 data,
1106 mime_type,
1107 annotations: _,
1108 meta: _,
1109 }) => {
1110 let mention_uri = if let Some(uri) = uri {
1111 MentionUri::parse(&uri, path_style)
1112 } else {
1113 Ok(MentionUri::PastedImage)
1114 };
1115 let Some(mention_uri) = mention_uri.log_err() else {
1116 continue;
1117 };
1118 let Some(format) = ImageFormat::from_mime_type(&mime_type) else {
1119 log::error!("failed to parse MIME type for image: {mime_type:?}");
1120 continue;
1121 };
1122 let start = text.len();
1123 write!(&mut text, "{}", mention_uri.as_link()).ok();
1124 let end = text.len();
1125 mentions.push((
1126 start..end,
1127 mention_uri,
1128 Mention::Image(MentionImage {
1129 data: data.into(),
1130 format,
1131 }),
1132 ));
1133 }
1134 acp::ContentBlock::Audio(_) | acp::ContentBlock::Resource(_) => {}
1135 }
1136 }
1137
1138 let snapshot = self.editor.update(cx, |editor, cx| {
1139 editor.set_text(text, window, cx);
1140 editor.buffer().read(cx).snapshot(cx)
1141 });
1142
1143 for (range, mention_uri, mention) in mentions {
1144 let anchor = snapshot.anchor_before(range.start);
1145 let Some((crease_id, tx)) = insert_crease_for_mention(
1146 anchor.excerpt_id,
1147 anchor.text_anchor,
1148 range.end - range.start,
1149 mention_uri.name().into(),
1150 mention_uri.icon_path(cx),
1151 None,
1152 self.editor.clone(),
1153 window,
1154 cx,
1155 ) else {
1156 continue;
1157 };
1158 drop(tx);
1159
1160 self.mention_set.mentions.insert(
1161 crease_id,
1162 (mention_uri.clone(), Task::ready(Ok(mention)).shared()),
1163 );
1164 }
1165 cx.notify();
1166 }
1167
1168 pub fn text(&self, cx: &App) -> String {
1169 self.editor.read(cx).text(cx)
1170 }
1171
1172 #[cfg(test)]
1173 pub fn set_text(&mut self, text: &str, window: &mut Window, cx: &mut Context<Self>) {
1174 self.editor.update(cx, |editor, cx| {
1175 editor.set_text(text, window, cx);
1176 });
1177 }
1178}
1179
1180fn full_mention_for_directory(
1181 project: &Entity<Project>,
1182 abs_path: &Path,
1183 cx: &mut App,
1184) -> Task<Result<Mention>> {
1185 fn collect_files_in_path(worktree: &Worktree, path: &RelPath) -> Vec<(Arc<RelPath>, String)> {
1186 let mut files = Vec::new();
1187
1188 for entry in worktree.child_entries(path) {
1189 if entry.is_dir() {
1190 files.extend(collect_files_in_path(worktree, &entry.path));
1191 } else if entry.is_file() {
1192 files.push((
1193 entry.path.clone(),
1194 worktree
1195 .full_path(&entry.path)
1196 .to_string_lossy()
1197 .to_string(),
1198 ));
1199 }
1200 }
1201
1202 files
1203 }
1204
1205 let Some(project_path) = project
1206 .read(cx)
1207 .project_path_for_absolute_path(&abs_path, cx)
1208 else {
1209 return Task::ready(Err(anyhow!("project path not found")));
1210 };
1211 let Some(entry) = project.read(cx).entry_for_path(&project_path, cx) else {
1212 return Task::ready(Err(anyhow!("project entry not found")));
1213 };
1214 let directory_path = entry.path.clone();
1215 let worktree_id = project_path.worktree_id;
1216 let Some(worktree) = project.read(cx).worktree_for_id(worktree_id, cx) else {
1217 return Task::ready(Err(anyhow!("worktree not found")));
1218 };
1219 let project = project.clone();
1220 cx.spawn(async move |cx| {
1221 let file_paths = worktree.read_with(cx, |worktree, _cx| {
1222 collect_files_in_path(worktree, &directory_path)
1223 })?;
1224 let descendants_future = cx.update(|cx| {
1225 join_all(file_paths.into_iter().map(|(worktree_path, full_path)| {
1226 let rel_path = worktree_path
1227 .strip_prefix(&directory_path)
1228 .log_err()
1229 .map_or_else(|| worktree_path.clone(), |rel_path| rel_path.into());
1230
1231 let open_task = project.update(cx, |project, cx| {
1232 project.buffer_store().update(cx, |buffer_store, cx| {
1233 let project_path = ProjectPath {
1234 worktree_id,
1235 path: worktree_path,
1236 };
1237 buffer_store.open_buffer(project_path, cx)
1238 })
1239 });
1240
1241 cx.spawn(async move |cx| {
1242 let buffer = open_task.await.log_err()?;
1243 let buffer_content = outline::get_buffer_content_or_outline(
1244 buffer.clone(),
1245 Some(&full_path),
1246 &cx,
1247 )
1248 .await
1249 .ok()?;
1250
1251 Some((rel_path, full_path, buffer_content.text, buffer))
1252 })
1253 }))
1254 })?;
1255
1256 let contents = cx
1257 .background_spawn(async move {
1258 let (contents, tracked_buffers) = descendants_future
1259 .await
1260 .into_iter()
1261 .flatten()
1262 .map(|(rel_path, full_path, rope, buffer)| {
1263 ((rel_path, full_path, rope), buffer)
1264 })
1265 .unzip();
1266 Mention::Text {
1267 content: render_directory_contents(contents),
1268 tracked_buffers,
1269 }
1270 })
1271 .await;
1272 anyhow::Ok(contents)
1273 })
1274}
1275
1276fn render_directory_contents(entries: Vec<(Arc<RelPath>, String, String)>) -> String {
1277 let mut output = String::new();
1278 for (_relative_path, full_path, content) in entries {
1279 let fence = codeblock_fence_for_path(Some(&full_path), None);
1280 write!(output, "\n{fence}\n{content}\n```").unwrap();
1281 }
1282 output
1283}
1284
1285impl Focusable for MessageEditor {
1286 fn focus_handle(&self, cx: &App) -> FocusHandle {
1287 self.editor.focus_handle(cx)
1288 }
1289}
1290
1291impl Render for MessageEditor {
1292 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
1293 div()
1294 .key_context("MessageEditor")
1295 .on_action(cx.listener(Self::chat))
1296 .on_action(cx.listener(Self::cancel))
1297 .capture_action(cx.listener(Self::paste))
1298 .flex_1()
1299 .child({
1300 let settings = ThemeSettings::get_global(cx);
1301
1302 let text_style = TextStyle {
1303 color: cx.theme().colors().text,
1304 font_family: settings.buffer_font.family.clone(),
1305 font_fallbacks: settings.buffer_font.fallbacks.clone(),
1306 font_features: settings.buffer_font.features.clone(),
1307 font_size: settings.agent_buffer_font_size(cx).into(),
1308 line_height: relative(settings.buffer_line_height.value()),
1309 ..Default::default()
1310 };
1311
1312 EditorElement::new(
1313 &self.editor,
1314 EditorStyle {
1315 background: cx.theme().colors().editor_background,
1316 local_player: cx.theme().players().local(),
1317 text: text_style,
1318 syntax: cx.theme().syntax().clone(),
1319 inlay_hints_style: editor::make_inlay_hints_style(cx),
1320 ..Default::default()
1321 },
1322 )
1323 })
1324 }
1325}
1326
1327pub(crate) fn insert_crease_for_mention(
1328 excerpt_id: ExcerptId,
1329 anchor: text::Anchor,
1330 content_len: usize,
1331 crease_label: SharedString,
1332 crease_icon: SharedString,
1333 // abs_path: Option<Arc<Path>>,
1334 image: Option<Shared<Task<Result<Arc<Image>, String>>>>,
1335 editor: Entity<Editor>,
1336 window: &mut Window,
1337 cx: &mut App,
1338) -> Option<(CreaseId, postage::barrier::Sender)> {
1339 let (tx, rx) = postage::barrier::channel();
1340
1341 let crease_id = editor.update(cx, |editor, cx| {
1342 let snapshot = editor.buffer().read(cx).snapshot(cx);
1343
1344 let start = snapshot.anchor_in_excerpt(excerpt_id, anchor)?;
1345
1346 let start = start.bias_right(&snapshot);
1347 let end = snapshot.anchor_before(start.to_offset(&snapshot) + content_len);
1348
1349 let placeholder = FoldPlaceholder {
1350 render: render_mention_fold_button(
1351 crease_label,
1352 crease_icon,
1353 start..end,
1354 rx,
1355 image,
1356 cx.weak_entity(),
1357 cx,
1358 ),
1359 merge_adjacent: false,
1360 ..Default::default()
1361 };
1362
1363 let crease = Crease::Inline {
1364 range: start..end,
1365 placeholder,
1366 render_toggle: None,
1367 render_trailer: None,
1368 metadata: None,
1369 };
1370
1371 let ids = editor.insert_creases(vec![crease.clone()], cx);
1372 editor.fold_creases(vec![crease], false, window, cx);
1373
1374 Some(ids[0])
1375 })?;
1376
1377 Some((crease_id, tx))
1378}
1379
1380fn render_mention_fold_button(
1381 label: SharedString,
1382 icon: SharedString,
1383 range: Range<Anchor>,
1384 mut loading_finished: postage::barrier::Receiver,
1385 image_task: Option<Shared<Task<Result<Arc<Image>, String>>>>,
1386 editor: WeakEntity<Editor>,
1387 cx: &mut App,
1388) -> Arc<dyn Send + Sync + Fn(FoldId, Range<Anchor>, &mut App) -> AnyElement> {
1389 let loading = cx.new(|cx| {
1390 let loading = cx.spawn(async move |this, cx| {
1391 loading_finished.recv().await;
1392 this.update(cx, |this: &mut LoadingContext, cx| {
1393 this.loading = None;
1394 cx.notify();
1395 })
1396 .ok();
1397 });
1398 LoadingContext {
1399 id: cx.entity_id(),
1400 label,
1401 icon,
1402 range,
1403 editor,
1404 loading: Some(loading),
1405 image: image_task.clone(),
1406 }
1407 });
1408 Arc::new(move |_fold_id, _fold_range, _cx| loading.clone().into_any_element())
1409}
1410
1411struct LoadingContext {
1412 id: EntityId,
1413 label: SharedString,
1414 icon: SharedString,
1415 range: Range<Anchor>,
1416 editor: WeakEntity<Editor>,
1417 loading: Option<Task<()>>,
1418 image: Option<Shared<Task<Result<Arc<Image>, String>>>>,
1419}
1420
1421impl Render for LoadingContext {
1422 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
1423 let is_in_text_selection = self
1424 .editor
1425 .update(cx, |editor, cx| editor.is_range_selected(&self.range, cx))
1426 .unwrap_or_default();
1427 ButtonLike::new(("loading-context", self.id))
1428 .style(ButtonStyle::Filled)
1429 .selected_style(ButtonStyle::Tinted(TintColor::Accent))
1430 .toggle_state(is_in_text_selection)
1431 .when_some(self.image.clone(), |el, image_task| {
1432 el.hoverable_tooltip(move |_, cx| {
1433 let image = image_task.peek().cloned().transpose().ok().flatten();
1434 let image_task = image_task.clone();
1435 cx.new::<ImageHover>(|cx| ImageHover {
1436 image,
1437 _task: cx.spawn(async move |this, cx| {
1438 if let Ok(image) = image_task.clone().await {
1439 this.update(cx, |this, cx| {
1440 if this.image.replace(image).is_none() {
1441 cx.notify();
1442 }
1443 })
1444 .ok();
1445 }
1446 }),
1447 })
1448 .into()
1449 })
1450 })
1451 .child(
1452 h_flex()
1453 .gap_1()
1454 .child(
1455 Icon::from_path(self.icon.clone())
1456 .size(IconSize::XSmall)
1457 .color(Color::Muted),
1458 )
1459 .child(
1460 Label::new(self.label.clone())
1461 .size(LabelSize::Small)
1462 .buffer_font(cx)
1463 .single_line(),
1464 )
1465 .map(|el| {
1466 if self.loading.is_some() {
1467 el.with_animation(
1468 "loading-context-crease",
1469 Animation::new(Duration::from_secs(2))
1470 .repeat()
1471 .with_easing(pulsating_between(0.4, 0.8)),
1472 |label, delta| label.opacity(delta),
1473 )
1474 .into_any()
1475 } else {
1476 el.into_any()
1477 }
1478 }),
1479 )
1480 }
1481}
1482
1483struct ImageHover {
1484 image: Option<Arc<Image>>,
1485 _task: Task<()>,
1486}
1487
1488impl Render for ImageHover {
1489 fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
1490 if let Some(image) = self.image.clone() {
1491 gpui::img(image).max_w_96().max_h_96().into_any_element()
1492 } else {
1493 gpui::Empty.into_any_element()
1494 }
1495 }
1496}
1497
1498#[derive(Debug, Clone, Eq, PartialEq)]
1499pub enum Mention {
1500 Text {
1501 content: String,
1502 tracked_buffers: Vec<Entity<Buffer>>,
1503 },
1504 Image(MentionImage),
1505 UriOnly,
1506}
1507
1508#[derive(Clone, Debug, Eq, PartialEq)]
1509pub struct MentionImage {
1510 pub data: SharedString,
1511 pub format: ImageFormat,
1512}
1513
1514#[derive(Default)]
1515pub struct MentionSet {
1516 mentions: HashMap<CreaseId, (MentionUri, Shared<Task<Result<Mention, String>>>)>,
1517}
1518
1519impl MentionSet {
1520 fn contents(
1521 &self,
1522 prompt_capabilities: &acp::PromptCapabilities,
1523 full_mention_content: bool,
1524 project: Entity<Project>,
1525 cx: &mut App,
1526 ) -> Task<Result<HashMap<CreaseId, (MentionUri, Mention)>>> {
1527 if !prompt_capabilities.embedded_context {
1528 let mentions = self
1529 .mentions
1530 .iter()
1531 .map(|(crease_id, (uri, _))| (*crease_id, (uri.clone(), Mention::UriOnly)))
1532 .collect();
1533
1534 return Task::ready(Ok(mentions));
1535 }
1536
1537 let mentions = self.mentions.clone();
1538 cx.spawn(async move |cx| {
1539 let mut contents = HashMap::default();
1540 for (crease_id, (mention_uri, task)) in mentions {
1541 let content = if full_mention_content
1542 && let MentionUri::Directory { abs_path } = &mention_uri
1543 {
1544 cx.update(|cx| full_mention_for_directory(&project, abs_path, cx))?
1545 .await?
1546 } else {
1547 task.await.map_err(|e| anyhow!("{e}"))?
1548 };
1549
1550 contents.insert(crease_id, (mention_uri, content));
1551 }
1552 Ok(contents)
1553 })
1554 }
1555
1556 fn remove_invalid(&mut self, snapshot: EditorSnapshot) {
1557 for (crease_id, crease) in snapshot.crease_snapshot.creases() {
1558 if !crease.range().start.is_valid(&snapshot.buffer_snapshot()) {
1559 self.mentions.remove(&crease_id);
1560 }
1561 }
1562 }
1563}
1564
1565pub struct MessageEditorAddon {}
1566
1567impl MessageEditorAddon {
1568 pub fn new() -> Self {
1569 Self {}
1570 }
1571}
1572
1573impl Addon for MessageEditorAddon {
1574 fn to_any(&self) -> &dyn std::any::Any {
1575 self
1576 }
1577
1578 fn to_any_mut(&mut self) -> Option<&mut dyn std::any::Any> {
1579 Some(self)
1580 }
1581
1582 fn extend_key_context(&self, key_context: &mut KeyContext, cx: &App) {
1583 let settings = agent_settings::AgentSettings::get_global(cx);
1584 if settings.use_modifier_to_send {
1585 key_context.add("use_modifier_to_send");
1586 }
1587 }
1588}
1589
1590#[cfg(test)]
1591mod tests {
1592 use std::{cell::RefCell, ops::Range, path::Path, rc::Rc, sync::Arc};
1593
1594 use acp_thread::MentionUri;
1595 use agent::{HistoryStore, outline};
1596 use agent_client_protocol as acp;
1597 use assistant_context::ContextStore;
1598 use editor::{AnchorRangeExt as _, Editor, EditorMode};
1599 use fs::FakeFs;
1600 use futures::StreamExt as _;
1601 use gpui::{
1602 AppContext, Entity, EventEmitter, FocusHandle, Focusable, TestAppContext, VisualTestContext,
1603 };
1604 use lsp::{CompletionContext, CompletionTriggerKind};
1605 use project::{CompletionIntent, Project, ProjectPath};
1606 use serde_json::json;
1607 use text::Point;
1608 use ui::{App, Context, IntoElement, Render, SharedString, Window};
1609 use util::{path, paths::PathStyle, rel_path::rel_path};
1610 use workspace::{AppState, Item, Workspace};
1611
1612 use crate::acp::{
1613 message_editor::{Mention, MessageEditor},
1614 thread_view::tests::init_test,
1615 };
1616
1617 #[gpui::test]
1618 async fn test_at_mention_removal(cx: &mut TestAppContext) {
1619 init_test(cx);
1620
1621 let fs = FakeFs::new(cx.executor());
1622 fs.insert_tree("/project", json!({"file": ""})).await;
1623 let project = Project::test(fs, [Path::new(path!("/project"))], cx).await;
1624
1625 let (workspace, cx) =
1626 cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));
1627
1628 let context_store = cx.new(|cx| ContextStore::fake(project.clone(), cx));
1629 let history_store = cx.new(|cx| HistoryStore::new(context_store, cx));
1630
1631 let message_editor = cx.update(|window, cx| {
1632 cx.new(|cx| {
1633 MessageEditor::new(
1634 workspace.downgrade(),
1635 project.clone(),
1636 history_store.clone(),
1637 None,
1638 Default::default(),
1639 Default::default(),
1640 "Test Agent".into(),
1641 "Test",
1642 EditorMode::AutoHeight {
1643 min_lines: 1,
1644 max_lines: None,
1645 },
1646 window,
1647 cx,
1648 )
1649 })
1650 });
1651 let editor = message_editor.update(cx, |message_editor, _| message_editor.editor.clone());
1652
1653 cx.run_until_parked();
1654
1655 let excerpt_id = editor.update(cx, |editor, cx| {
1656 editor
1657 .buffer()
1658 .read(cx)
1659 .excerpt_ids()
1660 .into_iter()
1661 .next()
1662 .unwrap()
1663 });
1664 let completions = editor.update_in(cx, |editor, window, cx| {
1665 editor.set_text("Hello @file ", window, cx);
1666 let buffer = editor.buffer().read(cx).as_singleton().unwrap();
1667 let completion_provider = editor.completion_provider().unwrap();
1668 completion_provider.completions(
1669 excerpt_id,
1670 &buffer,
1671 text::Anchor::MAX,
1672 CompletionContext {
1673 trigger_kind: CompletionTriggerKind::TRIGGER_CHARACTER,
1674 trigger_character: Some("@".into()),
1675 },
1676 window,
1677 cx,
1678 )
1679 });
1680 let [_, completion]: [_; 2] = completions
1681 .await
1682 .unwrap()
1683 .into_iter()
1684 .flat_map(|response| response.completions)
1685 .collect::<Vec<_>>()
1686 .try_into()
1687 .unwrap();
1688
1689 editor.update_in(cx, |editor, window, cx| {
1690 let snapshot = editor.buffer().read(cx).snapshot(cx);
1691 let range = snapshot
1692 .anchor_range_in_excerpt(excerpt_id, completion.replace_range)
1693 .unwrap();
1694 editor.edit([(range, completion.new_text)], cx);
1695 (completion.confirm.unwrap())(CompletionIntent::Complete, window, cx);
1696 });
1697
1698 cx.run_until_parked();
1699
1700 // Backspace over the inserted crease (and the following space).
1701 editor.update_in(cx, |editor, window, cx| {
1702 editor.backspace(&Default::default(), window, cx);
1703 editor.backspace(&Default::default(), window, cx);
1704 });
1705
1706 let (content, _) = message_editor
1707 .update(cx, |message_editor, cx| message_editor.contents(false, cx))
1708 .await
1709 .unwrap();
1710
1711 // We don't send a resource link for the deleted crease.
1712 pretty_assertions::assert_matches!(content.as_slice(), [acp::ContentBlock::Text { .. }]);
1713 }
1714
1715 #[gpui::test]
1716 async fn test_slash_command_validation(cx: &mut gpui::TestAppContext) {
1717 init_test(cx);
1718 let fs = FakeFs::new(cx.executor());
1719 fs.insert_tree(
1720 "/test",
1721 json!({
1722 ".zed": {
1723 "tasks.json": r#"[{"label": "test", "command": "echo"}]"#
1724 },
1725 "src": {
1726 "main.rs": "fn main() {}",
1727 },
1728 }),
1729 )
1730 .await;
1731
1732 let project = Project::test(fs.clone(), ["/test".as_ref()], cx).await;
1733 let context_store = cx.new(|cx| ContextStore::fake(project.clone(), cx));
1734 let history_store = cx.new(|cx| HistoryStore::new(context_store, cx));
1735 let prompt_capabilities = Rc::new(RefCell::new(acp::PromptCapabilities::default()));
1736 // Start with no available commands - simulating Claude which doesn't support slash commands
1737 let available_commands = Rc::new(RefCell::new(vec![]));
1738
1739 let (workspace, cx) =
1740 cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));
1741 let workspace_handle = workspace.downgrade();
1742 let message_editor = workspace.update_in(cx, |_, window, cx| {
1743 cx.new(|cx| {
1744 MessageEditor::new(
1745 workspace_handle.clone(),
1746 project.clone(),
1747 history_store.clone(),
1748 None,
1749 prompt_capabilities.clone(),
1750 available_commands.clone(),
1751 "Claude Code".into(),
1752 "Test",
1753 EditorMode::AutoHeight {
1754 min_lines: 1,
1755 max_lines: None,
1756 },
1757 window,
1758 cx,
1759 )
1760 })
1761 });
1762 let editor = message_editor.update(cx, |message_editor, _| message_editor.editor.clone());
1763
1764 // Test that slash commands fail when no available_commands are set (empty list means no commands supported)
1765 editor.update_in(cx, |editor, window, cx| {
1766 editor.set_text("/file test.txt", window, cx);
1767 });
1768
1769 let contents_result = message_editor
1770 .update(cx, |message_editor, cx| message_editor.contents(false, cx))
1771 .await;
1772
1773 // Should fail because available_commands is empty (no commands supported)
1774 assert!(contents_result.is_err());
1775 let error_message = contents_result.unwrap_err().to_string();
1776 assert!(error_message.contains("not supported by Claude Code"));
1777 assert!(error_message.contains("Available commands: none"));
1778
1779 // Now simulate Claude providing its list of available commands (which doesn't include file)
1780 available_commands.replace(vec![acp::AvailableCommand {
1781 name: "help".to_string(),
1782 description: "Get help".to_string(),
1783 input: None,
1784 meta: None,
1785 }]);
1786
1787 // Test that unsupported slash commands trigger an error when we have a list of available commands
1788 editor.update_in(cx, |editor, window, cx| {
1789 editor.set_text("/file test.txt", window, cx);
1790 });
1791
1792 let contents_result = message_editor
1793 .update(cx, |message_editor, cx| message_editor.contents(false, cx))
1794 .await;
1795
1796 assert!(contents_result.is_err());
1797 let error_message = contents_result.unwrap_err().to_string();
1798 assert!(error_message.contains("not supported by Claude Code"));
1799 assert!(error_message.contains("/file"));
1800 assert!(error_message.contains("Available commands: /help"));
1801
1802 // Test that supported commands work fine
1803 editor.update_in(cx, |editor, window, cx| {
1804 editor.set_text("/help", window, cx);
1805 });
1806
1807 let contents_result = message_editor
1808 .update(cx, |message_editor, cx| message_editor.contents(false, cx))
1809 .await;
1810
1811 // Should succeed because /help is in available_commands
1812 assert!(contents_result.is_ok());
1813
1814 // Test that regular text works fine
1815 editor.update_in(cx, |editor, window, cx| {
1816 editor.set_text("Hello Claude!", window, cx);
1817 });
1818
1819 let (content, _) = message_editor
1820 .update(cx, |message_editor, cx| message_editor.contents(false, cx))
1821 .await
1822 .unwrap();
1823
1824 assert_eq!(content.len(), 1);
1825 if let acp::ContentBlock::Text(text) = &content[0] {
1826 assert_eq!(text.text, "Hello Claude!");
1827 } else {
1828 panic!("Expected ContentBlock::Text");
1829 }
1830
1831 // Test that @ mentions still work
1832 editor.update_in(cx, |editor, window, cx| {
1833 editor.set_text("Check this @", window, cx);
1834 });
1835
1836 // The @ mention functionality should not be affected
1837 let (content, _) = message_editor
1838 .update(cx, |message_editor, cx| message_editor.contents(false, cx))
1839 .await
1840 .unwrap();
1841
1842 assert_eq!(content.len(), 1);
1843 if let acp::ContentBlock::Text(text) = &content[0] {
1844 assert_eq!(text.text, "Check this @");
1845 } else {
1846 panic!("Expected ContentBlock::Text");
1847 }
1848 }
1849
1850 struct MessageEditorItem(Entity<MessageEditor>);
1851
1852 impl Item for MessageEditorItem {
1853 type Event = ();
1854
1855 fn include_in_nav_history() -> bool {
1856 false
1857 }
1858
1859 fn tab_content_text(&self, _detail: usize, _cx: &App) -> SharedString {
1860 "Test".into()
1861 }
1862 }
1863
1864 impl EventEmitter<()> for MessageEditorItem {}
1865
1866 impl Focusable for MessageEditorItem {
1867 fn focus_handle(&self, cx: &App) -> FocusHandle {
1868 self.0.read(cx).focus_handle(cx)
1869 }
1870 }
1871
1872 impl Render for MessageEditorItem {
1873 fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
1874 self.0.clone().into_any_element()
1875 }
1876 }
1877
1878 #[gpui::test]
1879 async fn test_completion_provider_commands(cx: &mut TestAppContext) {
1880 init_test(cx);
1881
1882 let app_state = cx.update(AppState::test);
1883
1884 cx.update(|cx| {
1885 language::init(cx);
1886 editor::init(cx);
1887 workspace::init(app_state.clone(), cx);
1888 Project::init_settings(cx);
1889 });
1890
1891 let project = Project::test(app_state.fs.clone(), [path!("/dir").as_ref()], cx).await;
1892 let window = cx.add_window(|window, cx| Workspace::test_new(project.clone(), window, cx));
1893 let workspace = window.root(cx).unwrap();
1894
1895 let mut cx = VisualTestContext::from_window(*window, cx);
1896
1897 let context_store = cx.new(|cx| ContextStore::fake(project.clone(), cx));
1898 let history_store = cx.new(|cx| HistoryStore::new(context_store, cx));
1899 let prompt_capabilities = Rc::new(RefCell::new(acp::PromptCapabilities::default()));
1900 let available_commands = Rc::new(RefCell::new(vec![
1901 acp::AvailableCommand {
1902 name: "quick-math".to_string(),
1903 description: "2 + 2 = 4 - 1 = 3".to_string(),
1904 input: None,
1905 meta: None,
1906 },
1907 acp::AvailableCommand {
1908 name: "say-hello".to_string(),
1909 description: "Say hello to whoever you want".to_string(),
1910 input: Some(acp::AvailableCommandInput::Unstructured {
1911 hint: "<name>".to_string(),
1912 }),
1913 meta: None,
1914 },
1915 ]));
1916
1917 let editor = workspace.update_in(&mut cx, |workspace, window, cx| {
1918 let workspace_handle = cx.weak_entity();
1919 let message_editor = cx.new(|cx| {
1920 MessageEditor::new(
1921 workspace_handle,
1922 project.clone(),
1923 history_store.clone(),
1924 None,
1925 prompt_capabilities.clone(),
1926 available_commands.clone(),
1927 "Test Agent".into(),
1928 "Test",
1929 EditorMode::AutoHeight {
1930 max_lines: None,
1931 min_lines: 1,
1932 },
1933 window,
1934 cx,
1935 )
1936 });
1937 workspace.active_pane().update(cx, |pane, cx| {
1938 pane.add_item(
1939 Box::new(cx.new(|_| MessageEditorItem(message_editor.clone()))),
1940 true,
1941 true,
1942 None,
1943 window,
1944 cx,
1945 );
1946 });
1947 message_editor.read(cx).focus_handle(cx).focus(window);
1948 message_editor.read(cx).editor().clone()
1949 });
1950
1951 cx.simulate_input("/");
1952
1953 editor.update_in(&mut cx, |editor, window, cx| {
1954 assert_eq!(editor.text(cx), "/");
1955 assert!(editor.has_visible_completions_menu());
1956
1957 assert_eq!(
1958 current_completion_labels_with_documentation(editor),
1959 &[
1960 ("quick-math".into(), "2 + 2 = 4 - 1 = 3".into()),
1961 ("say-hello".into(), "Say hello to whoever you want".into())
1962 ]
1963 );
1964 editor.set_text("", window, cx);
1965 });
1966
1967 cx.simulate_input("/qui");
1968
1969 editor.update_in(&mut cx, |editor, window, cx| {
1970 assert_eq!(editor.text(cx), "/qui");
1971 assert!(editor.has_visible_completions_menu());
1972
1973 assert_eq!(
1974 current_completion_labels_with_documentation(editor),
1975 &[("quick-math".into(), "2 + 2 = 4 - 1 = 3".into())]
1976 );
1977 editor.set_text("", window, cx);
1978 });
1979
1980 editor.update_in(&mut cx, |editor, window, cx| {
1981 assert!(editor.has_visible_completions_menu());
1982 editor.confirm_completion(&editor::actions::ConfirmCompletion::default(), window, cx);
1983 });
1984
1985 cx.run_until_parked();
1986
1987 editor.update_in(&mut cx, |editor, window, cx| {
1988 assert_eq!(editor.display_text(cx), "/quick-math ");
1989 assert!(!editor.has_visible_completions_menu());
1990 editor.set_text("", window, cx);
1991 });
1992
1993 cx.simulate_input("/say");
1994
1995 editor.update_in(&mut cx, |editor, _window, cx| {
1996 assert_eq!(editor.display_text(cx), "/say");
1997 assert!(editor.has_visible_completions_menu());
1998
1999 assert_eq!(
2000 current_completion_labels_with_documentation(editor),
2001 &[("say-hello".into(), "Say hello to whoever you want".into())]
2002 );
2003 });
2004
2005 editor.update_in(&mut cx, |editor, window, cx| {
2006 assert!(editor.has_visible_completions_menu());
2007 editor.confirm_completion(&editor::actions::ConfirmCompletion::default(), window, cx);
2008 });
2009
2010 cx.run_until_parked();
2011
2012 editor.update_in(&mut cx, |editor, _window, cx| {
2013 assert_eq!(editor.text(cx), "/say-hello ");
2014 assert_eq!(editor.display_text(cx), "/say-hello <name>");
2015 assert!(!editor.has_visible_completions_menu());
2016 });
2017
2018 cx.simulate_input("GPT5");
2019
2020 cx.run_until_parked();
2021
2022 editor.update_in(&mut cx, |editor, window, cx| {
2023 assert_eq!(editor.text(cx), "/say-hello GPT5");
2024 assert_eq!(editor.display_text(cx), "/say-hello GPT5");
2025 assert!(!editor.has_visible_completions_menu());
2026
2027 // Delete argument
2028 for _ in 0..5 {
2029 editor.backspace(&editor::actions::Backspace, window, cx);
2030 }
2031 });
2032
2033 cx.run_until_parked();
2034
2035 editor.update_in(&mut cx, |editor, window, cx| {
2036 assert_eq!(editor.text(cx), "/say-hello");
2037 // Hint is visible because argument was deleted
2038 assert_eq!(editor.display_text(cx), "/say-hello <name>");
2039
2040 // Delete last command letter
2041 editor.backspace(&editor::actions::Backspace, window, cx);
2042 });
2043
2044 cx.run_until_parked();
2045
2046 editor.update_in(&mut cx, |editor, _window, cx| {
2047 // Hint goes away once command no longer matches an available one
2048 assert_eq!(editor.text(cx), "/say-hell");
2049 assert_eq!(editor.display_text(cx), "/say-hell");
2050 assert!(!editor.has_visible_completions_menu());
2051 });
2052 }
2053
2054 #[gpui::test]
2055 async fn test_context_completion_provider_mentions(cx: &mut TestAppContext) {
2056 init_test(cx);
2057
2058 let app_state = cx.update(AppState::test);
2059
2060 cx.update(|cx| {
2061 language::init(cx);
2062 editor::init(cx);
2063 workspace::init(app_state.clone(), cx);
2064 Project::init_settings(cx);
2065 });
2066
2067 app_state
2068 .fs
2069 .as_fake()
2070 .insert_tree(
2071 path!("/dir"),
2072 json!({
2073 "editor": "",
2074 "a": {
2075 "one.txt": "1",
2076 "two.txt": "2",
2077 "three.txt": "3",
2078 "four.txt": "4"
2079 },
2080 "b": {
2081 "five.txt": "5",
2082 "six.txt": "6",
2083 "seven.txt": "7",
2084 "eight.txt": "8",
2085 },
2086 "x.png": "",
2087 }),
2088 )
2089 .await;
2090
2091 let project = Project::test(app_state.fs.clone(), [path!("/dir").as_ref()], cx).await;
2092 let window = cx.add_window(|window, cx| Workspace::test_new(project.clone(), window, cx));
2093 let workspace = window.root(cx).unwrap();
2094
2095 let worktree = project.update(cx, |project, cx| {
2096 let mut worktrees = project.worktrees(cx).collect::<Vec<_>>();
2097 assert_eq!(worktrees.len(), 1);
2098 worktrees.pop().unwrap()
2099 });
2100 let worktree_id = worktree.read_with(cx, |worktree, _| worktree.id());
2101
2102 let mut cx = VisualTestContext::from_window(*window, cx);
2103
2104 let paths = vec![
2105 rel_path("a/one.txt"),
2106 rel_path("a/two.txt"),
2107 rel_path("a/three.txt"),
2108 rel_path("a/four.txt"),
2109 rel_path("b/five.txt"),
2110 rel_path("b/six.txt"),
2111 rel_path("b/seven.txt"),
2112 rel_path("b/eight.txt"),
2113 ];
2114
2115 let slash = PathStyle::local().separator();
2116
2117 let mut opened_editors = Vec::new();
2118 for path in paths {
2119 let buffer = workspace
2120 .update_in(&mut cx, |workspace, window, cx| {
2121 workspace.open_path(
2122 ProjectPath {
2123 worktree_id,
2124 path: path.into(),
2125 },
2126 None,
2127 false,
2128 window,
2129 cx,
2130 )
2131 })
2132 .await
2133 .unwrap();
2134 opened_editors.push(buffer);
2135 }
2136
2137 let context_store = cx.new(|cx| ContextStore::fake(project.clone(), cx));
2138 let history_store = cx.new(|cx| HistoryStore::new(context_store, cx));
2139 let prompt_capabilities = Rc::new(RefCell::new(acp::PromptCapabilities::default()));
2140
2141 let (message_editor, editor) = workspace.update_in(&mut cx, |workspace, window, cx| {
2142 let workspace_handle = cx.weak_entity();
2143 let message_editor = cx.new(|cx| {
2144 MessageEditor::new(
2145 workspace_handle,
2146 project.clone(),
2147 history_store.clone(),
2148 None,
2149 prompt_capabilities.clone(),
2150 Default::default(),
2151 "Test Agent".into(),
2152 "Test",
2153 EditorMode::AutoHeight {
2154 max_lines: None,
2155 min_lines: 1,
2156 },
2157 window,
2158 cx,
2159 )
2160 });
2161 workspace.active_pane().update(cx, |pane, cx| {
2162 pane.add_item(
2163 Box::new(cx.new(|_| MessageEditorItem(message_editor.clone()))),
2164 true,
2165 true,
2166 None,
2167 window,
2168 cx,
2169 );
2170 });
2171 message_editor.read(cx).focus_handle(cx).focus(window);
2172 let editor = message_editor.read(cx).editor().clone();
2173 (message_editor, editor)
2174 });
2175
2176 cx.simulate_input("Lorem @");
2177
2178 editor.update_in(&mut cx, |editor, window, cx| {
2179 assert_eq!(editor.text(cx), "Lorem @");
2180 assert!(editor.has_visible_completions_menu());
2181
2182 assert_eq!(
2183 current_completion_labels(editor),
2184 &[
2185 format!("eight.txt dir{slash}b{slash}"),
2186 format!("seven.txt dir{slash}b{slash}"),
2187 format!("six.txt dir{slash}b{slash}"),
2188 format!("five.txt dir{slash}b{slash}"),
2189 ]
2190 );
2191 editor.set_text("", window, cx);
2192 });
2193
2194 prompt_capabilities.replace(acp::PromptCapabilities {
2195 image: true,
2196 audio: true,
2197 embedded_context: true,
2198 meta: None,
2199 });
2200
2201 cx.simulate_input("Lorem ");
2202
2203 editor.update(&mut cx, |editor, cx| {
2204 assert_eq!(editor.text(cx), "Lorem ");
2205 assert!(!editor.has_visible_completions_menu());
2206 });
2207
2208 cx.simulate_input("@");
2209
2210 editor.update(&mut cx, |editor, cx| {
2211 assert_eq!(editor.text(cx), "Lorem @");
2212 assert!(editor.has_visible_completions_menu());
2213 assert_eq!(
2214 current_completion_labels(editor),
2215 &[
2216 format!("eight.txt dir{slash}b{slash}"),
2217 format!("seven.txt dir{slash}b{slash}"),
2218 format!("six.txt dir{slash}b{slash}"),
2219 format!("five.txt dir{slash}b{slash}"),
2220 "Files & Directories".into(),
2221 "Symbols".into(),
2222 "Threads".into(),
2223 "Fetch".into()
2224 ]
2225 );
2226 });
2227
2228 // Select and confirm "File"
2229 editor.update_in(&mut cx, |editor, window, cx| {
2230 assert!(editor.has_visible_completions_menu());
2231 editor.context_menu_next(&editor::actions::ContextMenuNext, window, cx);
2232 editor.context_menu_next(&editor::actions::ContextMenuNext, window, cx);
2233 editor.context_menu_next(&editor::actions::ContextMenuNext, window, cx);
2234 editor.context_menu_next(&editor::actions::ContextMenuNext, window, cx);
2235 editor.confirm_completion(&editor::actions::ConfirmCompletion::default(), window, cx);
2236 });
2237
2238 cx.run_until_parked();
2239
2240 editor.update(&mut cx, |editor, cx| {
2241 assert_eq!(editor.text(cx), "Lorem @file ");
2242 assert!(editor.has_visible_completions_menu());
2243 });
2244
2245 cx.simulate_input("one");
2246
2247 editor.update(&mut cx, |editor, cx| {
2248 assert_eq!(editor.text(cx), "Lorem @file one");
2249 assert!(editor.has_visible_completions_menu());
2250 assert_eq!(
2251 current_completion_labels(editor),
2252 vec![format!("one.txt dir{slash}a{slash}")]
2253 );
2254 });
2255
2256 editor.update_in(&mut cx, |editor, window, cx| {
2257 assert!(editor.has_visible_completions_menu());
2258 editor.confirm_completion(&editor::actions::ConfirmCompletion::default(), window, cx);
2259 });
2260
2261 let url_one = MentionUri::File {
2262 abs_path: path!("/dir/a/one.txt").into(),
2263 }
2264 .to_uri()
2265 .to_string();
2266 editor.update(&mut cx, |editor, cx| {
2267 let text = editor.text(cx);
2268 assert_eq!(text, format!("Lorem [@one.txt]({url_one}) "));
2269 assert!(!editor.has_visible_completions_menu());
2270 assert_eq!(fold_ranges(editor, cx).len(), 1);
2271 });
2272
2273 let all_prompt_capabilities = acp::PromptCapabilities {
2274 image: true,
2275 audio: true,
2276 embedded_context: true,
2277 meta: None,
2278 };
2279
2280 let contents = message_editor
2281 .update(&mut cx, |message_editor, cx| {
2282 message_editor.mention_set().contents(
2283 &all_prompt_capabilities,
2284 false,
2285 project.clone(),
2286 cx,
2287 )
2288 })
2289 .await
2290 .unwrap()
2291 .into_values()
2292 .collect::<Vec<_>>();
2293
2294 {
2295 let [(uri, Mention::Text { content, .. })] = contents.as_slice() else {
2296 panic!("Unexpected mentions");
2297 };
2298 pretty_assertions::assert_eq!(content, "1");
2299 pretty_assertions::assert_eq!(
2300 uri,
2301 &MentionUri::parse(&url_one, PathStyle::local()).unwrap()
2302 );
2303 }
2304
2305 let contents = message_editor
2306 .update(&mut cx, |message_editor, cx| {
2307 message_editor.mention_set().contents(
2308 &acp::PromptCapabilities::default(),
2309 false,
2310 project.clone(),
2311 cx,
2312 )
2313 })
2314 .await
2315 .unwrap()
2316 .into_values()
2317 .collect::<Vec<_>>();
2318
2319 {
2320 let [(uri, Mention::UriOnly)] = contents.as_slice() else {
2321 panic!("Unexpected mentions");
2322 };
2323 pretty_assertions::assert_eq!(
2324 uri,
2325 &MentionUri::parse(&url_one, PathStyle::local()).unwrap()
2326 );
2327 }
2328
2329 cx.simulate_input(" ");
2330
2331 editor.update(&mut cx, |editor, cx| {
2332 let text = editor.text(cx);
2333 assert_eq!(text, format!("Lorem [@one.txt]({url_one}) "));
2334 assert!(!editor.has_visible_completions_menu());
2335 assert_eq!(fold_ranges(editor, cx).len(), 1);
2336 });
2337
2338 cx.simulate_input("Ipsum ");
2339
2340 editor.update(&mut cx, |editor, cx| {
2341 let text = editor.text(cx);
2342 assert_eq!(text, format!("Lorem [@one.txt]({url_one}) Ipsum "),);
2343 assert!(!editor.has_visible_completions_menu());
2344 assert_eq!(fold_ranges(editor, cx).len(), 1);
2345 });
2346
2347 cx.simulate_input("@file ");
2348
2349 editor.update(&mut cx, |editor, cx| {
2350 let text = editor.text(cx);
2351 assert_eq!(text, format!("Lorem [@one.txt]({url_one}) Ipsum @file "),);
2352 assert!(editor.has_visible_completions_menu());
2353 assert_eq!(fold_ranges(editor, cx).len(), 1);
2354 });
2355
2356 editor.update_in(&mut cx, |editor, window, cx| {
2357 editor.confirm_completion(&editor::actions::ConfirmCompletion::default(), window, cx);
2358 });
2359
2360 cx.run_until_parked();
2361
2362 let contents = message_editor
2363 .update(&mut cx, |message_editor, cx| {
2364 message_editor.mention_set().contents(
2365 &all_prompt_capabilities,
2366 false,
2367 project.clone(),
2368 cx,
2369 )
2370 })
2371 .await
2372 .unwrap()
2373 .into_values()
2374 .collect::<Vec<_>>();
2375
2376 let url_eight = MentionUri::File {
2377 abs_path: path!("/dir/b/eight.txt").into(),
2378 }
2379 .to_uri()
2380 .to_string();
2381
2382 {
2383 let [_, (uri, Mention::Text { content, .. })] = contents.as_slice() else {
2384 panic!("Unexpected mentions");
2385 };
2386 pretty_assertions::assert_eq!(content, "8");
2387 pretty_assertions::assert_eq!(
2388 uri,
2389 &MentionUri::parse(&url_eight, PathStyle::local()).unwrap()
2390 );
2391 }
2392
2393 editor.update(&mut cx, |editor, cx| {
2394 assert_eq!(
2395 editor.text(cx),
2396 format!("Lorem [@one.txt]({url_one}) Ipsum [@eight.txt]({url_eight}) ")
2397 );
2398 assert!(!editor.has_visible_completions_menu());
2399 assert_eq!(fold_ranges(editor, cx).len(), 2);
2400 });
2401
2402 let plain_text_language = Arc::new(language::Language::new(
2403 language::LanguageConfig {
2404 name: "Plain Text".into(),
2405 matcher: language::LanguageMatcher {
2406 path_suffixes: vec!["txt".to_string()],
2407 ..Default::default()
2408 },
2409 ..Default::default()
2410 },
2411 None,
2412 ));
2413
2414 // Register the language and fake LSP
2415 let language_registry = project.read_with(&cx, |project, _| project.languages().clone());
2416 language_registry.add(plain_text_language);
2417
2418 let mut fake_language_servers = language_registry.register_fake_lsp(
2419 "Plain Text",
2420 language::FakeLspAdapter {
2421 capabilities: lsp::ServerCapabilities {
2422 workspace_symbol_provider: Some(lsp::OneOf::Left(true)),
2423 ..Default::default()
2424 },
2425 ..Default::default()
2426 },
2427 );
2428
2429 // Open the buffer to trigger LSP initialization
2430 let buffer = project
2431 .update(&mut cx, |project, cx| {
2432 project.open_local_buffer(path!("/dir/a/one.txt"), cx)
2433 })
2434 .await
2435 .unwrap();
2436
2437 // Register the buffer with language servers
2438 let _handle = project.update(&mut cx, |project, cx| {
2439 project.register_buffer_with_language_servers(&buffer, cx)
2440 });
2441
2442 cx.run_until_parked();
2443
2444 let fake_language_server = fake_language_servers.next().await.unwrap();
2445 fake_language_server.set_request_handler::<lsp::WorkspaceSymbolRequest, _, _>(
2446 move |_, _| async move {
2447 Ok(Some(lsp::WorkspaceSymbolResponse::Flat(vec![
2448 #[allow(deprecated)]
2449 lsp::SymbolInformation {
2450 name: "MySymbol".into(),
2451 location: lsp::Location {
2452 uri: lsp::Uri::from_file_path(path!("/dir/a/one.txt")).unwrap(),
2453 range: lsp::Range::new(
2454 lsp::Position::new(0, 0),
2455 lsp::Position::new(0, 1),
2456 ),
2457 },
2458 kind: lsp::SymbolKind::CONSTANT,
2459 tags: None,
2460 container_name: None,
2461 deprecated: None,
2462 },
2463 ])))
2464 },
2465 );
2466
2467 cx.simulate_input("@symbol ");
2468
2469 editor.update(&mut cx, |editor, cx| {
2470 assert_eq!(
2471 editor.text(cx),
2472 format!("Lorem [@one.txt]({url_one}) Ipsum [@eight.txt]({url_eight}) @symbol ")
2473 );
2474 assert!(editor.has_visible_completions_menu());
2475 assert_eq!(current_completion_labels(editor), &["MySymbol"]);
2476 });
2477
2478 editor.update_in(&mut cx, |editor, window, cx| {
2479 editor.confirm_completion(&editor::actions::ConfirmCompletion::default(), window, cx);
2480 });
2481
2482 let symbol = MentionUri::Symbol {
2483 abs_path: path!("/dir/a/one.txt").into(),
2484 name: "MySymbol".into(),
2485 line_range: 0..=0,
2486 };
2487
2488 let contents = message_editor
2489 .update(&mut cx, |message_editor, cx| {
2490 message_editor.mention_set().contents(
2491 &all_prompt_capabilities,
2492 false,
2493 project.clone(),
2494 cx,
2495 )
2496 })
2497 .await
2498 .unwrap()
2499 .into_values()
2500 .collect::<Vec<_>>();
2501
2502 {
2503 let [_, _, (uri, Mention::Text { content, .. })] = contents.as_slice() else {
2504 panic!("Unexpected mentions");
2505 };
2506 pretty_assertions::assert_eq!(content, "1");
2507 pretty_assertions::assert_eq!(uri, &symbol);
2508 }
2509
2510 cx.run_until_parked();
2511
2512 editor.read_with(&cx, |editor, cx| {
2513 assert_eq!(
2514 editor.text(cx),
2515 format!(
2516 "Lorem [@one.txt]({url_one}) Ipsum [@eight.txt]({url_eight}) [@MySymbol]({}) ",
2517 symbol.to_uri(),
2518 )
2519 );
2520 });
2521
2522 // Try to mention an "image" file that will fail to load
2523 cx.simulate_input("@file x.png");
2524
2525 editor.update(&mut cx, |editor, cx| {
2526 assert_eq!(
2527 editor.text(cx),
2528 format!("Lorem [@one.txt]({url_one}) Ipsum [@eight.txt]({url_eight}) [@MySymbol]({}) @file x.png", symbol.to_uri())
2529 );
2530 assert!(editor.has_visible_completions_menu());
2531 assert_eq!(current_completion_labels(editor), &[format!("x.png dir{slash}")]);
2532 });
2533
2534 editor.update_in(&mut cx, |editor, window, cx| {
2535 editor.confirm_completion(&editor::actions::ConfirmCompletion::default(), window, cx);
2536 });
2537
2538 // Getting the message contents fails
2539 message_editor
2540 .update(&mut cx, |message_editor, cx| {
2541 message_editor.mention_set().contents(
2542 &all_prompt_capabilities,
2543 false,
2544 project.clone(),
2545 cx,
2546 )
2547 })
2548 .await
2549 .expect_err("Should fail to load x.png");
2550
2551 cx.run_until_parked();
2552
2553 // Mention was removed
2554 editor.read_with(&cx, |editor, cx| {
2555 assert_eq!(
2556 editor.text(cx),
2557 format!(
2558 "Lorem [@one.txt]({url_one}) Ipsum [@eight.txt]({url_eight}) [@MySymbol]({}) ",
2559 symbol.to_uri()
2560 )
2561 );
2562 });
2563
2564 // Once more
2565 cx.simulate_input("@file x.png");
2566
2567 editor.update(&mut cx, |editor, cx| {
2568 assert_eq!(
2569 editor.text(cx),
2570 format!("Lorem [@one.txt]({url_one}) Ipsum [@eight.txt]({url_eight}) [@MySymbol]({}) @file x.png", symbol.to_uri())
2571 );
2572 assert!(editor.has_visible_completions_menu());
2573 assert_eq!(current_completion_labels(editor), &[format!("x.png dir{slash}")]);
2574 });
2575
2576 editor.update_in(&mut cx, |editor, window, cx| {
2577 editor.confirm_completion(&editor::actions::ConfirmCompletion::default(), window, cx);
2578 });
2579
2580 // This time don't immediately get the contents, just let the confirmed completion settle
2581 cx.run_until_parked();
2582
2583 // Mention was removed
2584 editor.read_with(&cx, |editor, cx| {
2585 assert_eq!(
2586 editor.text(cx),
2587 format!(
2588 "Lorem [@one.txt]({url_one}) Ipsum [@eight.txt]({url_eight}) [@MySymbol]({}) ",
2589 symbol.to_uri()
2590 )
2591 );
2592 });
2593
2594 // Now getting the contents succeeds, because the invalid mention was removed
2595 let contents = message_editor
2596 .update(&mut cx, |message_editor, cx| {
2597 message_editor.mention_set().contents(
2598 &all_prompt_capabilities,
2599 false,
2600 project.clone(),
2601 cx,
2602 )
2603 })
2604 .await
2605 .unwrap();
2606 assert_eq!(contents.len(), 3);
2607 }
2608
2609 fn fold_ranges(editor: &Editor, cx: &mut App) -> Vec<Range<Point>> {
2610 let snapshot = editor.buffer().read(cx).snapshot(cx);
2611 editor.display_map.update(cx, |display_map, cx| {
2612 display_map
2613 .snapshot(cx)
2614 .folds_in_range(0..snapshot.len())
2615 .map(|fold| fold.range.to_point(&snapshot))
2616 .collect()
2617 })
2618 }
2619
2620 fn current_completion_labels(editor: &Editor) -> Vec<String> {
2621 let completions = editor.current_completions().expect("Missing completions");
2622 completions
2623 .into_iter()
2624 .map(|completion| completion.label.text)
2625 .collect::<Vec<_>>()
2626 }
2627
2628 fn current_completion_labels_with_documentation(editor: &Editor) -> Vec<(String, String)> {
2629 let completions = editor.current_completions().expect("Missing completions");
2630 completions
2631 .into_iter()
2632 .map(|completion| {
2633 (
2634 completion.label.text,
2635 completion
2636 .documentation
2637 .map(|d| d.text().to_string())
2638 .unwrap_or_default(),
2639 )
2640 })
2641 .collect::<Vec<_>>()
2642 }
2643
2644 #[gpui::test]
2645 async fn test_large_file_mention_uses_outline(cx: &mut TestAppContext) {
2646 init_test(cx);
2647
2648 let fs = FakeFs::new(cx.executor());
2649
2650 // Create a large file that exceeds AUTO_OUTLINE_SIZE
2651 const LINE: &str = "fn example_function() { /* some code */ }\n";
2652 let large_content = LINE.repeat(2 * (outline::AUTO_OUTLINE_SIZE / LINE.len()));
2653 assert!(large_content.len() > outline::AUTO_OUTLINE_SIZE);
2654
2655 // Create a small file that doesn't exceed AUTO_OUTLINE_SIZE
2656 let small_content = "fn small_function() { /* small */ }\n";
2657 assert!(small_content.len() < outline::AUTO_OUTLINE_SIZE);
2658
2659 fs.insert_tree(
2660 "/project",
2661 json!({
2662 "large_file.rs": large_content.clone(),
2663 "small_file.rs": small_content,
2664 }),
2665 )
2666 .await;
2667
2668 let project = Project::test(fs, [Path::new(path!("/project"))], cx).await;
2669
2670 let (workspace, cx) =
2671 cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));
2672
2673 let context_store = cx.new(|cx| ContextStore::fake(project.clone(), cx));
2674 let history_store = cx.new(|cx| HistoryStore::new(context_store, cx));
2675
2676 let message_editor = cx.update(|window, cx| {
2677 cx.new(|cx| {
2678 let editor = MessageEditor::new(
2679 workspace.downgrade(),
2680 project.clone(),
2681 history_store.clone(),
2682 None,
2683 Default::default(),
2684 Default::default(),
2685 "Test Agent".into(),
2686 "Test",
2687 EditorMode::AutoHeight {
2688 min_lines: 1,
2689 max_lines: None,
2690 },
2691 window,
2692 cx,
2693 );
2694 // Enable embedded context so files are actually included
2695 editor.prompt_capabilities.replace(acp::PromptCapabilities {
2696 embedded_context: true,
2697 meta: None,
2698 ..Default::default()
2699 });
2700 editor
2701 })
2702 });
2703
2704 // Test large file mention
2705 // Get the absolute path using the project's worktree
2706 let large_file_abs_path = project.read_with(cx, |project, cx| {
2707 let worktree = project.worktrees(cx).next().unwrap();
2708 let worktree_root = worktree.read(cx).abs_path();
2709 worktree_root.join("large_file.rs")
2710 });
2711 let large_file_task = message_editor.update(cx, |editor, cx| {
2712 editor.confirm_mention_for_file(large_file_abs_path, cx)
2713 });
2714
2715 let large_file_mention = large_file_task.await.unwrap();
2716 match large_file_mention {
2717 Mention::Text { content, .. } => {
2718 // Should contain outline header for large files
2719 assert!(content.contains("File outline for"));
2720 assert!(content.contains("file too large to show full content"));
2721 // Should not contain the full repeated content
2722 assert!(!content.contains(&LINE.repeat(100)));
2723 }
2724 _ => panic!("Expected Text mention for large file"),
2725 }
2726
2727 // Test small file mention
2728 // Get the absolute path using the project's worktree
2729 let small_file_abs_path = project.read_with(cx, |project, cx| {
2730 let worktree = project.worktrees(cx).next().unwrap();
2731 let worktree_root = worktree.read(cx).abs_path();
2732 worktree_root.join("small_file.rs")
2733 });
2734 let small_file_task = message_editor.update(cx, |editor, cx| {
2735 editor.confirm_mention_for_file(small_file_abs_path, cx)
2736 });
2737
2738 let small_file_mention = small_file_task.await.unwrap();
2739 match small_file_mention {
2740 Mention::Text { content, .. } => {
2741 // Should contain the actual content
2742 assert_eq!(content, small_content);
2743 // Should not contain outline header
2744 assert!(!content.contains("File outline for"));
2745 }
2746 _ => panic!("Expected Text mention for small file"),
2747 }
2748 }
2749}