1use crate::{
2 humanize_token_count, prompts::PromptBuilder, AssistantPanel, AssistantPanelEvent,
3 ModelSelector, DEFAULT_CONTEXT_LINES,
4};
5use anyhow::{Context as _, Result};
6use client::telemetry::Telemetry;
7use collections::{HashMap, VecDeque};
8use editor::{
9 actions::{MoveDown, MoveUp, SelectAll},
10 Editor, EditorElement, EditorEvent, EditorMode, EditorStyle, MultiBuffer,
11};
12use fs::Fs;
13use futures::{channel::mpsc, SinkExt, StreamExt};
14use gpui::{
15 AppContext, Context, EventEmitter, FocusHandle, FocusableView, Global, Model, ModelContext,
16 Subscription, Task, TextStyle, UpdateGlobal, View, WeakView,
17};
18use language::Buffer;
19use language_model::{
20 LanguageModelRegistry, LanguageModelRequest, LanguageModelRequestMessage, Role,
21};
22use settings::Settings;
23use std::{
24 cmp,
25 sync::Arc,
26 time::{Duration, Instant},
27};
28use terminal::Terminal;
29use terminal_view::TerminalView;
30use theme::ThemeSettings;
31use ui::{prelude::*, IconButtonShape, Tooltip};
32use util::ResultExt;
33use workspace::{notifications::NotificationId, Toast, Workspace};
34
35pub fn init(
36 fs: Arc<dyn Fs>,
37 prompt_builder: Arc<PromptBuilder>,
38 telemetry: Arc<Telemetry>,
39 cx: &mut AppContext,
40) {
41 cx.set_global(TerminalInlineAssistant::new(fs, prompt_builder, telemetry));
42}
43
44const PROMPT_HISTORY_MAX_LEN: usize = 20;
45
46#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, Hash)]
47struct TerminalInlineAssistId(usize);
48
49impl TerminalInlineAssistId {
50 fn post_inc(&mut self) -> TerminalInlineAssistId {
51 let id = *self;
52 self.0 += 1;
53 id
54 }
55}
56
57pub struct TerminalInlineAssistant {
58 next_assist_id: TerminalInlineAssistId,
59 assists: HashMap<TerminalInlineAssistId, TerminalInlineAssist>,
60 prompt_history: VecDeque<String>,
61 telemetry: Option<Arc<Telemetry>>,
62 fs: Arc<dyn Fs>,
63 prompt_builder: Arc<PromptBuilder>,
64}
65
66impl Global for TerminalInlineAssistant {}
67
68impl TerminalInlineAssistant {
69 pub fn new(
70 fs: Arc<dyn Fs>,
71 prompt_builder: Arc<PromptBuilder>,
72 telemetry: Arc<Telemetry>,
73 ) -> Self {
74 Self {
75 next_assist_id: TerminalInlineAssistId::default(),
76 assists: HashMap::default(),
77 prompt_history: VecDeque::default(),
78 telemetry: Some(telemetry),
79 fs,
80 prompt_builder,
81 }
82 }
83
84 pub fn assist(
85 &mut self,
86 terminal_view: &View<TerminalView>,
87 workspace: Option<WeakView<Workspace>>,
88 assistant_panel: Option<&View<AssistantPanel>>,
89 initial_prompt: Option<String>,
90 cx: &mut WindowContext,
91 ) {
92 let terminal = terminal_view.read(cx).terminal().clone();
93 let assist_id = self.next_assist_id.post_inc();
94 let prompt_buffer =
95 cx.new_model(|cx| Buffer::local(initial_prompt.unwrap_or_default(), cx));
96 let prompt_buffer = cx.new_model(|cx| MultiBuffer::singleton(prompt_buffer, cx));
97 let codegen = cx.new_model(|_| Codegen::new(terminal, self.telemetry.clone()));
98
99 let prompt_editor = cx.new_view(|cx| {
100 PromptEditor::new(
101 assist_id,
102 self.prompt_history.clone(),
103 prompt_buffer.clone(),
104 codegen,
105 assistant_panel,
106 workspace.clone(),
107 self.fs.clone(),
108 cx,
109 )
110 });
111 let prompt_editor_render = prompt_editor.clone();
112 let block = terminal_view::BlockProperties {
113 height: 2,
114 render: Box::new(move |_| prompt_editor_render.clone().into_any_element()),
115 };
116 terminal_view.update(cx, |terminal_view, cx| {
117 terminal_view.set_block_below_cursor(block, cx);
118 });
119
120 let terminal_assistant = TerminalInlineAssist::new(
121 assist_id,
122 terminal_view,
123 assistant_panel.is_some(),
124 prompt_editor,
125 workspace.clone(),
126 cx,
127 );
128
129 self.assists.insert(assist_id, terminal_assistant);
130
131 self.focus_assist(assist_id, cx);
132 }
133
134 fn focus_assist(&mut self, assist_id: TerminalInlineAssistId, cx: &mut WindowContext) {
135 let assist = &self.assists[&assist_id];
136 if let Some(prompt_editor) = assist.prompt_editor.as_ref() {
137 prompt_editor.update(cx, |this, cx| {
138 this.editor.update(cx, |editor, cx| {
139 editor.focus(cx);
140 editor.select_all(&SelectAll, cx);
141 });
142 });
143 }
144 }
145
146 fn handle_prompt_editor_event(
147 &mut self,
148 prompt_editor: View<PromptEditor>,
149 event: &PromptEditorEvent,
150 cx: &mut WindowContext,
151 ) {
152 let assist_id = prompt_editor.read(cx).id;
153 match event {
154 PromptEditorEvent::StartRequested => {
155 self.start_assist(assist_id, cx);
156 }
157 PromptEditorEvent::StopRequested => {
158 self.stop_assist(assist_id, cx);
159 }
160 PromptEditorEvent::ConfirmRequested => {
161 self.finish_assist(assist_id, false, cx);
162 }
163 PromptEditorEvent::CancelRequested => {
164 self.finish_assist(assist_id, true, cx);
165 }
166 PromptEditorEvent::DismissRequested => {
167 self.dismiss_assist(assist_id, cx);
168 }
169 PromptEditorEvent::Resized { height_in_lines } => {
170 self.insert_prompt_editor_into_terminal(assist_id, *height_in_lines, cx);
171 }
172 }
173 }
174
175 fn start_assist(&mut self, assist_id: TerminalInlineAssistId, cx: &mut WindowContext) {
176 let assist = if let Some(assist) = self.assists.get_mut(&assist_id) {
177 assist
178 } else {
179 return;
180 };
181
182 let Some(user_prompt) = assist
183 .prompt_editor
184 .as_ref()
185 .map(|editor| editor.read(cx).prompt(cx))
186 else {
187 return;
188 };
189
190 self.prompt_history.retain(|prompt| *prompt != user_prompt);
191 self.prompt_history.push_back(user_prompt.clone());
192 if self.prompt_history.len() > PROMPT_HISTORY_MAX_LEN {
193 self.prompt_history.pop_front();
194 }
195
196 assist
197 .terminal
198 .update(cx, |terminal, cx| {
199 terminal
200 .terminal()
201 .update(cx, |terminal, _| terminal.input(CLEAR_INPUT.to_string()));
202 })
203 .log_err();
204
205 let codegen = assist.codegen.clone();
206 let Some(request) = self.request_for_inline_assist(assist_id, cx).log_err() else {
207 return;
208 };
209
210 codegen.update(cx, |codegen, cx| codegen.start(request, cx));
211 }
212
213 fn stop_assist(&mut self, assist_id: TerminalInlineAssistId, cx: &mut WindowContext) {
214 let assist = if let Some(assist) = self.assists.get_mut(&assist_id) {
215 assist
216 } else {
217 return;
218 };
219
220 assist.codegen.update(cx, |codegen, cx| codegen.stop(cx));
221 }
222
223 fn request_for_inline_assist(
224 &self,
225 assist_id: TerminalInlineAssistId,
226 cx: &mut WindowContext,
227 ) -> Result<LanguageModelRequest> {
228 let assist = self.assists.get(&assist_id).context("invalid assist")?;
229
230 let shell = std::env::var("SHELL").ok();
231 let (latest_output, working_directory) = assist
232 .terminal
233 .update(cx, |terminal, cx| {
234 let terminal = terminal.model().read(cx);
235 let latest_output = terminal.last_n_non_empty_lines(DEFAULT_CONTEXT_LINES);
236 let working_directory = terminal
237 .working_directory()
238 .map(|path| path.to_string_lossy().to_string());
239 (latest_output, working_directory)
240 })
241 .ok()
242 .unwrap_or_default();
243
244 let context_request = if assist.include_context {
245 assist.workspace.as_ref().and_then(|workspace| {
246 let workspace = workspace.upgrade()?.read(cx);
247 let assistant_panel = workspace.panel::<AssistantPanel>(cx)?;
248 Some(
249 assistant_panel
250 .read(cx)
251 .active_context(cx)?
252 .read(cx)
253 .to_completion_request(cx),
254 )
255 })
256 } else {
257 None
258 };
259
260 let prompt = self.prompt_builder.generate_terminal_assistant_prompt(
261 &assist
262 .prompt_editor
263 .clone()
264 .context("invalid assist")?
265 .read(cx)
266 .prompt(cx),
267 shell.as_deref(),
268 working_directory.as_deref(),
269 &latest_output,
270 )?;
271
272 let mut messages = Vec::new();
273 if let Some(context_request) = context_request {
274 messages = context_request.messages;
275 }
276
277 messages.push(LanguageModelRequestMessage {
278 role: Role::User,
279 content: vec![prompt.into()],
280 cache: false,
281 });
282
283 Ok(LanguageModelRequest {
284 messages,
285 tools: Vec::new(),
286 stop: Vec::new(),
287 temperature: 1.0,
288 })
289 }
290
291 fn finish_assist(
292 &mut self,
293 assist_id: TerminalInlineAssistId,
294 undo: bool,
295 cx: &mut WindowContext,
296 ) {
297 self.dismiss_assist(assist_id, cx);
298
299 if let Some(assist) = self.assists.remove(&assist_id) {
300 assist
301 .terminal
302 .update(cx, |this, cx| {
303 this.clear_block_below_cursor(cx);
304 this.focus_handle(cx).focus(cx);
305 })
306 .log_err();
307 assist.codegen.update(cx, |codegen, cx| {
308 if undo {
309 codegen.undo(cx);
310 } else {
311 codegen.complete(cx);
312 }
313 });
314 }
315 }
316
317 fn dismiss_assist(
318 &mut self,
319 assist_id: TerminalInlineAssistId,
320 cx: &mut WindowContext,
321 ) -> bool {
322 let Some(assist) = self.assists.get_mut(&assist_id) else {
323 return false;
324 };
325 if assist.prompt_editor.is_none() {
326 return false;
327 }
328 assist.prompt_editor = None;
329 assist
330 .terminal
331 .update(cx, |this, cx| {
332 this.clear_block_below_cursor(cx);
333 this.focus_handle(cx).focus(cx);
334 })
335 .is_ok()
336 }
337
338 fn insert_prompt_editor_into_terminal(
339 &mut self,
340 assist_id: TerminalInlineAssistId,
341 height: u8,
342 cx: &mut WindowContext,
343 ) {
344 if let Some(assist) = self.assists.get_mut(&assist_id) {
345 if let Some(prompt_editor) = assist.prompt_editor.as_ref().cloned() {
346 assist
347 .terminal
348 .update(cx, |terminal, cx| {
349 terminal.clear_block_below_cursor(cx);
350 let block = terminal_view::BlockProperties {
351 height,
352 render: Box::new(move |_| prompt_editor.clone().into_any_element()),
353 };
354 terminal.set_block_below_cursor(block, cx);
355 })
356 .log_err();
357 }
358 }
359 }
360}
361
362struct TerminalInlineAssist {
363 terminal: WeakView<TerminalView>,
364 prompt_editor: Option<View<PromptEditor>>,
365 codegen: Model<Codegen>,
366 workspace: Option<WeakView<Workspace>>,
367 include_context: bool,
368 _subscriptions: Vec<Subscription>,
369}
370
371impl TerminalInlineAssist {
372 pub fn new(
373 assist_id: TerminalInlineAssistId,
374 terminal: &View<TerminalView>,
375 include_context: bool,
376 prompt_editor: View<PromptEditor>,
377 workspace: Option<WeakView<Workspace>>,
378 cx: &mut WindowContext,
379 ) -> Self {
380 let codegen = prompt_editor.read(cx).codegen.clone();
381 Self {
382 terminal: terminal.downgrade(),
383 prompt_editor: Some(prompt_editor.clone()),
384 codegen: codegen.clone(),
385 workspace: workspace.clone(),
386 include_context,
387 _subscriptions: vec![
388 cx.subscribe(&prompt_editor, |prompt_editor, event, cx| {
389 TerminalInlineAssistant::update_global(cx, |this, cx| {
390 this.handle_prompt_editor_event(prompt_editor, event, cx)
391 })
392 }),
393 cx.subscribe(&codegen, move |codegen, event, cx| {
394 TerminalInlineAssistant::update_global(cx, |this, cx| match event {
395 CodegenEvent::Finished => {
396 let assist = if let Some(assist) = this.assists.get(&assist_id) {
397 assist
398 } else {
399 return;
400 };
401
402 if let CodegenStatus::Error(error) = &codegen.read(cx).status {
403 if assist.prompt_editor.is_none() {
404 if let Some(workspace) = assist
405 .workspace
406 .as_ref()
407 .and_then(|workspace| workspace.upgrade())
408 {
409 let error =
410 format!("Terminal inline assistant error: {}", error);
411 workspace.update(cx, |workspace, cx| {
412 struct InlineAssistantError;
413
414 let id =
415 NotificationId::identified::<InlineAssistantError>(
416 assist_id.0,
417 );
418
419 workspace.show_toast(Toast::new(id, error), cx);
420 })
421 }
422 }
423 }
424
425 if assist.prompt_editor.is_none() {
426 this.finish_assist(assist_id, false, cx);
427 }
428 }
429 })
430 }),
431 ],
432 }
433 }
434}
435
436enum PromptEditorEvent {
437 StartRequested,
438 StopRequested,
439 ConfirmRequested,
440 CancelRequested,
441 DismissRequested,
442 Resized { height_in_lines: u8 },
443}
444
445struct PromptEditor {
446 id: TerminalInlineAssistId,
447 fs: Arc<dyn Fs>,
448 height_in_lines: u8,
449 editor: View<Editor>,
450 edited_since_done: bool,
451 prompt_history: VecDeque<String>,
452 prompt_history_ix: Option<usize>,
453 pending_prompt: String,
454 codegen: Model<Codegen>,
455 _codegen_subscription: Subscription,
456 editor_subscriptions: Vec<Subscription>,
457 pending_token_count: Task<Result<()>>,
458 token_count: Option<usize>,
459 _token_count_subscriptions: Vec<Subscription>,
460 workspace: Option<WeakView<Workspace>>,
461}
462
463impl EventEmitter<PromptEditorEvent> for PromptEditor {}
464
465impl Render for PromptEditor {
466 fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
467 let buttons = match &self.codegen.read(cx).status {
468 CodegenStatus::Idle => {
469 vec![
470 IconButton::new("cancel", IconName::Close)
471 .icon_color(Color::Muted)
472 .shape(IconButtonShape::Square)
473 .tooltip(|cx| Tooltip::for_action("Cancel Assist", &menu::Cancel, cx))
474 .on_click(
475 cx.listener(|_, _, cx| cx.emit(PromptEditorEvent::CancelRequested)),
476 ),
477 IconButton::new("start", IconName::SparkleAlt)
478 .icon_color(Color::Muted)
479 .shape(IconButtonShape::Square)
480 .tooltip(|cx| Tooltip::for_action("Generate", &menu::Confirm, cx))
481 .on_click(
482 cx.listener(|_, _, cx| cx.emit(PromptEditorEvent::StartRequested)),
483 ),
484 ]
485 }
486 CodegenStatus::Pending => {
487 vec![
488 IconButton::new("cancel", IconName::Close)
489 .icon_color(Color::Muted)
490 .shape(IconButtonShape::Square)
491 .tooltip(|cx| Tooltip::text("Cancel Assist", cx))
492 .on_click(
493 cx.listener(|_, _, cx| cx.emit(PromptEditorEvent::CancelRequested)),
494 ),
495 IconButton::new("stop", IconName::Stop)
496 .icon_color(Color::Error)
497 .shape(IconButtonShape::Square)
498 .tooltip(|cx| {
499 Tooltip::with_meta(
500 "Interrupt Generation",
501 Some(&menu::Cancel),
502 "Changes won't be discarded",
503 cx,
504 )
505 })
506 .on_click(
507 cx.listener(|_, _, cx| cx.emit(PromptEditorEvent::StopRequested)),
508 ),
509 ]
510 }
511 CodegenStatus::Error(_) | CodegenStatus::Done => {
512 vec![
513 IconButton::new("cancel", IconName::Close)
514 .icon_color(Color::Muted)
515 .shape(IconButtonShape::Square)
516 .tooltip(|cx| Tooltip::for_action("Cancel Assist", &menu::Cancel, cx))
517 .on_click(
518 cx.listener(|_, _, cx| cx.emit(PromptEditorEvent::CancelRequested)),
519 ),
520 if self.edited_since_done {
521 IconButton::new("restart", IconName::RotateCw)
522 .icon_color(Color::Info)
523 .shape(IconButtonShape::Square)
524 .tooltip(|cx| {
525 Tooltip::with_meta(
526 "Restart Generation",
527 Some(&menu::Confirm),
528 "Changes will be discarded",
529 cx,
530 )
531 })
532 .on_click(cx.listener(|_, _, cx| {
533 cx.emit(PromptEditorEvent::StartRequested);
534 }))
535 } else {
536 IconButton::new("confirm", IconName::Play)
537 .icon_color(Color::Info)
538 .shape(IconButtonShape::Square)
539 .tooltip(|cx| {
540 Tooltip::for_action("Execute generated command", &menu::Confirm, cx)
541 })
542 .on_click(cx.listener(|_, _, cx| {
543 cx.emit(PromptEditorEvent::ConfirmRequested);
544 }))
545 },
546 ]
547 }
548 };
549
550 h_flex()
551 .bg(cx.theme().colors().editor_background)
552 .border_y_1()
553 .border_color(cx.theme().status().info_border)
554 .py_1p5()
555 .h_full()
556 .w_full()
557 .on_action(cx.listener(Self::confirm))
558 .on_action(cx.listener(Self::cancel))
559 .on_action(cx.listener(Self::move_up))
560 .on_action(cx.listener(Self::move_down))
561 .child(
562 h_flex()
563 .w_12()
564 .justify_center()
565 .gap_2()
566 .child(ModelSelector::new(
567 self.fs.clone(),
568 IconButton::new("context", IconName::SlidersAlt)
569 .shape(IconButtonShape::Square)
570 .icon_size(IconSize::Small)
571 .icon_color(Color::Muted)
572 .tooltip(move |cx| {
573 Tooltip::with_meta(
574 format!(
575 "Using {}",
576 LanguageModelRegistry::read_global(cx)
577 .active_model()
578 .map(|model| model.name().0)
579 .unwrap_or_else(|| "No model selected".into()),
580 ),
581 None,
582 "Change Model",
583 cx,
584 )
585 }),
586 ))
587 .children(
588 if let CodegenStatus::Error(error) = &self.codegen.read(cx).status {
589 let error_message = SharedString::from(error.to_string());
590 Some(
591 div()
592 .id("error")
593 .tooltip(move |cx| Tooltip::text(error_message.clone(), cx))
594 .child(
595 Icon::new(IconName::XCircle)
596 .size(IconSize::Small)
597 .color(Color::Error),
598 ),
599 )
600 } else {
601 None
602 },
603 ),
604 )
605 .child(div().flex_1().child(self.render_prompt_editor(cx)))
606 .child(
607 h_flex()
608 .gap_2()
609 .pr_4()
610 .children(self.render_token_count(cx))
611 .children(buttons),
612 )
613 }
614}
615
616impl FocusableView for PromptEditor {
617 fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
618 self.editor.focus_handle(cx)
619 }
620}
621
622impl PromptEditor {
623 const MAX_LINES: u8 = 8;
624
625 #[allow(clippy::too_many_arguments)]
626 fn new(
627 id: TerminalInlineAssistId,
628 prompt_history: VecDeque<String>,
629 prompt_buffer: Model<MultiBuffer>,
630 codegen: Model<Codegen>,
631 assistant_panel: Option<&View<AssistantPanel>>,
632 workspace: Option<WeakView<Workspace>>,
633 fs: Arc<dyn Fs>,
634 cx: &mut ViewContext<Self>,
635 ) -> Self {
636 let prompt_editor = cx.new_view(|cx| {
637 let mut editor = Editor::new(
638 EditorMode::AutoHeight {
639 max_lines: Self::MAX_LINES as usize,
640 },
641 prompt_buffer,
642 None,
643 false,
644 cx,
645 );
646 editor.set_soft_wrap_mode(language::language_settings::SoftWrap::EditorWidth, cx);
647 editor.set_placeholder_text("Add a prompt…", cx);
648 editor
649 });
650
651 let mut token_count_subscriptions = Vec::new();
652 if let Some(assistant_panel) = assistant_panel {
653 token_count_subscriptions
654 .push(cx.subscribe(assistant_panel, Self::handle_assistant_panel_event));
655 }
656
657 let mut this = Self {
658 id,
659 height_in_lines: 1,
660 editor: prompt_editor,
661 edited_since_done: false,
662 prompt_history,
663 prompt_history_ix: None,
664 pending_prompt: String::new(),
665 _codegen_subscription: cx.observe(&codegen, Self::handle_codegen_changed),
666 editor_subscriptions: Vec::new(),
667 codegen,
668 fs,
669 pending_token_count: Task::ready(Ok(())),
670 token_count: None,
671 _token_count_subscriptions: token_count_subscriptions,
672 workspace,
673 };
674 this.count_lines(cx);
675 this.count_tokens(cx);
676 this.subscribe_to_editor(cx);
677 this
678 }
679
680 fn subscribe_to_editor(&mut self, cx: &mut ViewContext<Self>) {
681 self.editor_subscriptions.clear();
682 self.editor_subscriptions
683 .push(cx.observe(&self.editor, Self::handle_prompt_editor_changed));
684 self.editor_subscriptions
685 .push(cx.subscribe(&self.editor, Self::handle_prompt_editor_events));
686 }
687
688 fn prompt(&self, cx: &AppContext) -> String {
689 self.editor.read(cx).text(cx)
690 }
691
692 fn count_lines(&mut self, cx: &mut ViewContext<Self>) {
693 let height_in_lines = cmp::max(
694 2, // Make the editor at least two lines tall, to account for padding and buttons.
695 cmp::min(
696 self.editor
697 .update(cx, |editor, cx| editor.max_point(cx).row().0 + 1),
698 Self::MAX_LINES as u32,
699 ),
700 ) as u8;
701
702 if height_in_lines != self.height_in_lines {
703 self.height_in_lines = height_in_lines;
704 cx.emit(PromptEditorEvent::Resized { height_in_lines });
705 }
706 }
707
708 fn handle_assistant_panel_event(
709 &mut self,
710 _: View<AssistantPanel>,
711 event: &AssistantPanelEvent,
712 cx: &mut ViewContext<Self>,
713 ) {
714 let AssistantPanelEvent::ContextEdited { .. } = event;
715 self.count_tokens(cx);
716 }
717
718 fn count_tokens(&mut self, cx: &mut ViewContext<Self>) {
719 let assist_id = self.id;
720 let Some(model) = LanguageModelRegistry::read_global(cx).active_model() else {
721 return;
722 };
723 self.pending_token_count = cx.spawn(|this, mut cx| async move {
724 cx.background_executor().timer(Duration::from_secs(1)).await;
725 let request =
726 cx.update_global(|inline_assistant: &mut TerminalInlineAssistant, cx| {
727 inline_assistant.request_for_inline_assist(assist_id, cx)
728 })??;
729
730 let token_count = cx.update(|cx| model.count_tokens(request, cx))?.await?;
731 this.update(&mut cx, |this, cx| {
732 this.token_count = Some(token_count);
733 cx.notify();
734 })
735 })
736 }
737
738 fn handle_prompt_editor_changed(&mut self, _: View<Editor>, cx: &mut ViewContext<Self>) {
739 self.count_lines(cx);
740 }
741
742 fn handle_prompt_editor_events(
743 &mut self,
744 _: View<Editor>,
745 event: &EditorEvent,
746 cx: &mut ViewContext<Self>,
747 ) {
748 match event {
749 EditorEvent::Edited { .. } => {
750 let prompt = self.editor.read(cx).text(cx);
751 if self
752 .prompt_history_ix
753 .map_or(true, |ix| self.prompt_history[ix] != prompt)
754 {
755 self.prompt_history_ix.take();
756 self.pending_prompt = prompt;
757 }
758
759 self.edited_since_done = true;
760 cx.notify();
761 }
762 EditorEvent::BufferEdited => {
763 self.count_tokens(cx);
764 }
765 _ => {}
766 }
767 }
768
769 fn handle_codegen_changed(&mut self, _: Model<Codegen>, cx: &mut ViewContext<Self>) {
770 match &self.codegen.read(cx).status {
771 CodegenStatus::Idle => {
772 self.editor
773 .update(cx, |editor, _| editor.set_read_only(false));
774 }
775 CodegenStatus::Pending => {
776 self.editor
777 .update(cx, |editor, _| editor.set_read_only(true));
778 }
779 CodegenStatus::Done | CodegenStatus::Error(_) => {
780 self.edited_since_done = false;
781 self.editor
782 .update(cx, |editor, _| editor.set_read_only(false));
783 }
784 }
785 }
786
787 fn cancel(&mut self, _: &editor::actions::Cancel, cx: &mut ViewContext<Self>) {
788 match &self.codegen.read(cx).status {
789 CodegenStatus::Idle | CodegenStatus::Done | CodegenStatus::Error(_) => {
790 cx.emit(PromptEditorEvent::CancelRequested);
791 }
792 CodegenStatus::Pending => {
793 cx.emit(PromptEditorEvent::StopRequested);
794 }
795 }
796 }
797
798 fn confirm(&mut self, _: &menu::Confirm, cx: &mut ViewContext<Self>) {
799 match &self.codegen.read(cx).status {
800 CodegenStatus::Idle => {
801 if !self.editor.read(cx).text(cx).trim().is_empty() {
802 cx.emit(PromptEditorEvent::StartRequested);
803 }
804 }
805 CodegenStatus::Pending => {
806 cx.emit(PromptEditorEvent::DismissRequested);
807 }
808 CodegenStatus::Done | CodegenStatus::Error(_) => {
809 if self.edited_since_done {
810 cx.emit(PromptEditorEvent::StartRequested);
811 } else {
812 cx.emit(PromptEditorEvent::ConfirmRequested);
813 }
814 }
815 }
816 }
817
818 fn move_up(&mut self, _: &MoveUp, cx: &mut ViewContext<Self>) {
819 if let Some(ix) = self.prompt_history_ix {
820 if ix > 0 {
821 self.prompt_history_ix = Some(ix - 1);
822 let prompt = self.prompt_history[ix - 1].as_str();
823 self.editor.update(cx, |editor, cx| {
824 editor.set_text(prompt, cx);
825 editor.move_to_beginning(&Default::default(), cx);
826 });
827 }
828 } else if !self.prompt_history.is_empty() {
829 self.prompt_history_ix = Some(self.prompt_history.len() - 1);
830 let prompt = self.prompt_history[self.prompt_history.len() - 1].as_str();
831 self.editor.update(cx, |editor, cx| {
832 editor.set_text(prompt, cx);
833 editor.move_to_beginning(&Default::default(), cx);
834 });
835 }
836 }
837
838 fn move_down(&mut self, _: &MoveDown, cx: &mut ViewContext<Self>) {
839 if let Some(ix) = self.prompt_history_ix {
840 if ix < self.prompt_history.len() - 1 {
841 self.prompt_history_ix = Some(ix + 1);
842 let prompt = self.prompt_history[ix + 1].as_str();
843 self.editor.update(cx, |editor, cx| {
844 editor.set_text(prompt, cx);
845 editor.move_to_end(&Default::default(), cx)
846 });
847 } else {
848 self.prompt_history_ix = None;
849 let prompt = self.pending_prompt.as_str();
850 self.editor.update(cx, |editor, cx| {
851 editor.set_text(prompt, cx);
852 editor.move_to_end(&Default::default(), cx)
853 });
854 }
855 }
856 }
857
858 fn render_token_count(&self, cx: &mut ViewContext<Self>) -> Option<impl IntoElement> {
859 let model = LanguageModelRegistry::read_global(cx).active_model()?;
860 let token_count = self.token_count?;
861 let max_token_count = model.max_token_count();
862
863 let remaining_tokens = max_token_count as isize - token_count as isize;
864 let token_count_color = if remaining_tokens <= 0 {
865 Color::Error
866 } else if token_count as f32 / max_token_count as f32 >= 0.8 {
867 Color::Warning
868 } else {
869 Color::Muted
870 };
871
872 let mut token_count = h_flex()
873 .id("token_count")
874 .gap_0p5()
875 .child(
876 Label::new(humanize_token_count(token_count))
877 .size(LabelSize::Small)
878 .color(token_count_color),
879 )
880 .child(Label::new("/").size(LabelSize::Small).color(Color::Muted))
881 .child(
882 Label::new(humanize_token_count(max_token_count))
883 .size(LabelSize::Small)
884 .color(Color::Muted),
885 );
886 if let Some(workspace) = self.workspace.clone() {
887 token_count = token_count
888 .tooltip(|cx| {
889 Tooltip::with_meta(
890 "Tokens Used by Inline Assistant",
891 None,
892 "Click to Open Assistant Panel",
893 cx,
894 )
895 })
896 .cursor_pointer()
897 .on_mouse_down(gpui::MouseButton::Left, |_, cx| cx.stop_propagation())
898 .on_click(move |_, cx| {
899 cx.stop_propagation();
900 workspace
901 .update(cx, |workspace, cx| {
902 workspace.focus_panel::<AssistantPanel>(cx)
903 })
904 .ok();
905 });
906 } else {
907 token_count = token_count
908 .cursor_default()
909 .tooltip(|cx| Tooltip::text("Tokens Used by Inline Assistant", cx));
910 }
911
912 Some(token_count)
913 }
914
915 fn render_prompt_editor(&self, cx: &mut ViewContext<Self>) -> impl IntoElement {
916 let settings = ThemeSettings::get_global(cx);
917 let text_style = TextStyle {
918 color: if self.editor.read(cx).read_only(cx) {
919 cx.theme().colors().text_disabled
920 } else {
921 cx.theme().colors().text
922 },
923 font_family: settings.ui_font.family.clone(),
924 font_features: settings.ui_font.features.clone(),
925 font_fallbacks: settings.ui_font.fallbacks.clone(),
926 font_size: rems(0.875).into(),
927 font_weight: settings.ui_font.weight,
928 line_height: relative(1.3),
929 ..Default::default()
930 };
931 EditorElement::new(
932 &self.editor,
933 EditorStyle {
934 background: cx.theme().colors().editor_background,
935 local_player: cx.theme().players().local(),
936 text: text_style,
937 ..Default::default()
938 },
939 )
940 }
941}
942
943#[derive(Debug)]
944pub enum CodegenEvent {
945 Finished,
946}
947
948impl EventEmitter<CodegenEvent> for Codegen {}
949
950const CLEAR_INPUT: &str = "\x15";
951const CARRIAGE_RETURN: &str = "\x0d";
952
953struct TerminalTransaction {
954 terminal: Model<Terminal>,
955}
956
957impl TerminalTransaction {
958 pub fn start(terminal: Model<Terminal>) -> Self {
959 Self { terminal }
960 }
961
962 pub fn push(&mut self, hunk: String, cx: &mut AppContext) {
963 // Ensure that the assistant cannot accidentally execute commands that are streamed into the terminal
964 let input = hunk.replace(CARRIAGE_RETURN, " ");
965 self.terminal
966 .update(cx, |terminal, _| terminal.input(input));
967 }
968
969 pub fn undo(&self, cx: &mut AppContext) {
970 self.terminal
971 .update(cx, |terminal, _| terminal.input(CLEAR_INPUT.to_string()));
972 }
973
974 pub fn complete(&self, cx: &mut AppContext) {
975 self.terminal.update(cx, |terminal, _| {
976 terminal.input(CARRIAGE_RETURN.to_string())
977 });
978 }
979}
980
981pub struct Codegen {
982 status: CodegenStatus,
983 telemetry: Option<Arc<Telemetry>>,
984 terminal: Model<Terminal>,
985 generation: Task<()>,
986 transaction: Option<TerminalTransaction>,
987}
988
989impl Codegen {
990 pub fn new(terminal: Model<Terminal>, telemetry: Option<Arc<Telemetry>>) -> Self {
991 Self {
992 terminal,
993 telemetry,
994 status: CodegenStatus::Idle,
995 generation: Task::ready(()),
996 transaction: None,
997 }
998 }
999
1000 pub fn start(&mut self, prompt: LanguageModelRequest, cx: &mut ModelContext<Self>) {
1001 let Some(model) = LanguageModelRegistry::read_global(cx).active_model() else {
1002 return;
1003 };
1004
1005 let telemetry = self.telemetry.clone();
1006 self.status = CodegenStatus::Pending;
1007 self.transaction = Some(TerminalTransaction::start(self.terminal.clone()));
1008 self.generation = cx.spawn(|this, mut cx| async move {
1009 let model_telemetry_id = model.telemetry_id();
1010 let response = model.stream_completion(prompt, &cx).await;
1011 let generate = async {
1012 let (mut hunks_tx, mut hunks_rx) = mpsc::channel(1);
1013
1014 let task = cx.background_executor().spawn(async move {
1015 let mut response_latency = None;
1016 let request_start = Instant::now();
1017 let task = async {
1018 let mut chunks = response?;
1019 while let Some(chunk) = chunks.next().await {
1020 if response_latency.is_none() {
1021 response_latency = Some(request_start.elapsed());
1022 }
1023 let chunk = chunk?;
1024 hunks_tx.send(chunk).await?;
1025 }
1026
1027 anyhow::Ok(())
1028 };
1029
1030 let result = task.await;
1031
1032 let error_message = result.as_ref().err().map(|error| error.to_string());
1033 if let Some(telemetry) = telemetry {
1034 telemetry.report_assistant_event(
1035 None,
1036 telemetry_events::AssistantKind::Inline,
1037 model_telemetry_id,
1038 response_latency,
1039 error_message,
1040 );
1041 }
1042
1043 result?;
1044 anyhow::Ok(())
1045 });
1046
1047 while let Some(hunk) = hunks_rx.next().await {
1048 this.update(&mut cx, |this, cx| {
1049 if let Some(transaction) = &mut this.transaction {
1050 transaction.push(hunk, cx);
1051 cx.notify();
1052 }
1053 })?;
1054 }
1055
1056 task.await?;
1057 anyhow::Ok(())
1058 };
1059
1060 let result = generate.await;
1061
1062 this.update(&mut cx, |this, cx| {
1063 if let Err(error) = result {
1064 this.status = CodegenStatus::Error(error);
1065 } else {
1066 this.status = CodegenStatus::Done;
1067 }
1068 cx.emit(CodegenEvent::Finished);
1069 cx.notify();
1070 })
1071 .ok();
1072 });
1073 cx.notify();
1074 }
1075
1076 pub fn stop(&mut self, cx: &mut ModelContext<Self>) {
1077 self.status = CodegenStatus::Done;
1078 self.generation = Task::ready(());
1079 cx.emit(CodegenEvent::Finished);
1080 cx.notify();
1081 }
1082
1083 pub fn complete(&mut self, cx: &mut ModelContext<Self>) {
1084 if let Some(transaction) = self.transaction.take() {
1085 transaction.complete(cx);
1086 }
1087 }
1088
1089 pub fn undo(&mut self, cx: &mut ModelContext<Self>) {
1090 if let Some(transaction) = self.transaction.take() {
1091 transaction.undo(cx);
1092 }
1093 }
1094}
1095
1096enum CodegenStatus {
1097 Idle,
1098 Pending,
1099 Done,
1100 Error(anyhow::Error),
1101}