1mod completion_diff_element;
2mod init;
3mod input_excerpt;
4mod license_detection;
5mod onboarding_modal;
6mod onboarding_telemetry;
7mod rate_completion_modal;
8
9pub(crate) use completion_diff_element::*;
10use db::kvp::KEY_VALUE_STORE;
11pub use init::*;
12use inline_completion::{DataCollectionState, EditPredictionUsage};
13use license_detection::LICENSE_FILES_TO_CHECK;
14pub use license_detection::is_license_eligible_for_data_collection;
15pub use rate_completion_modal::*;
16
17use anyhow::{Context as _, Result, anyhow};
18use arrayvec::ArrayVec;
19use client::{Client, UserStore};
20use collections::{HashMap, HashSet, VecDeque};
21use futures::AsyncReadExt;
22use gpui::{
23 App, AppContext as _, AsyncApp, Context, Entity, EntityId, Global, SemanticVersion,
24 Subscription, Task, WeakEntity, actions,
25};
26use http_client::{AsyncBody, HttpClient, Method, Request, Response};
27use input_excerpt::excerpt_for_cursor_position;
28use language::{
29 Anchor, Buffer, BufferSnapshot, EditPreview, OffsetRangeExt, ToOffset, ToPoint, text_diff,
30};
31use language_model::{LlmApiToken, RefreshLlmTokenListener};
32use postage::watch;
33use project::Project;
34use release_channel::AppVersion;
35use settings::WorktreeId;
36use std::str::FromStr;
37use std::{
38 borrow::Cow,
39 cmp,
40 fmt::Write,
41 future::Future,
42 mem,
43 ops::Range,
44 path::Path,
45 rc::Rc,
46 sync::Arc,
47 time::{Duration, Instant},
48};
49use telemetry_events::InlineCompletionRating;
50use thiserror::Error;
51use util::{ResultExt, maybe};
52use uuid::Uuid;
53use workspace::Workspace;
54use workspace::notifications::{ErrorMessagePrompt, NotificationId};
55use worktree::Worktree;
56use zed_llm_client::{
57 AcceptEditPredictionBody, EXPIRED_LLM_TOKEN_HEADER_NAME, MINIMUM_REQUIRED_VERSION_HEADER_NAME,
58 PredictEditsBody, PredictEditsResponse, ZED_VERSION_HEADER_NAME,
59};
60
61const CURSOR_MARKER: &'static str = "<|user_cursor_is_here|>";
62const START_OF_FILE_MARKER: &'static str = "<|start_of_file|>";
63const EDITABLE_REGION_START_MARKER: &'static str = "<|editable_region_start|>";
64const EDITABLE_REGION_END_MARKER: &'static str = "<|editable_region_end|>";
65const BUFFER_CHANGE_GROUPING_INTERVAL: Duration = Duration::from_secs(1);
66const ZED_PREDICT_DATA_COLLECTION_CHOICE: &str = "zed_predict_data_collection_choice";
67
68const MAX_CONTEXT_TOKENS: usize = 150;
69const MAX_REWRITE_TOKENS: usize = 350;
70const MAX_EVENT_TOKENS: usize = 500;
71
72/// Maximum number of events to track.
73const MAX_EVENT_COUNT: usize = 16;
74
75actions!(edit_prediction, [ClearHistory]);
76
77#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, Hash)]
78pub struct InlineCompletionId(Uuid);
79
80impl From<InlineCompletionId> for gpui::ElementId {
81 fn from(value: InlineCompletionId) -> Self {
82 gpui::ElementId::Uuid(value.0)
83 }
84}
85
86impl std::fmt::Display for InlineCompletionId {
87 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
88 write!(f, "{}", self.0)
89 }
90}
91
92#[derive(Clone)]
93struct ZetaGlobal(Entity<Zeta>);
94
95impl Global for ZetaGlobal {}
96
97#[derive(Clone)]
98pub struct InlineCompletion {
99 id: InlineCompletionId,
100 path: Arc<Path>,
101 excerpt_range: Range<usize>,
102 cursor_offset: usize,
103 edits: Arc<[(Range<Anchor>, String)]>,
104 snapshot: BufferSnapshot,
105 edit_preview: EditPreview,
106 input_outline: Arc<str>,
107 input_events: Arc<str>,
108 input_excerpt: Arc<str>,
109 output_excerpt: Arc<str>,
110 request_sent_at: Instant,
111 response_received_at: Instant,
112}
113
114impl InlineCompletion {
115 fn latency(&self) -> Duration {
116 self.response_received_at
117 .duration_since(self.request_sent_at)
118 }
119
120 fn interpolate(&self, new_snapshot: &BufferSnapshot) -> Option<Vec<(Range<Anchor>, String)>> {
121 interpolate(&self.snapshot, new_snapshot, self.edits.clone())
122 }
123}
124
125fn interpolate(
126 old_snapshot: &BufferSnapshot,
127 new_snapshot: &BufferSnapshot,
128 current_edits: Arc<[(Range<Anchor>, String)]>,
129) -> Option<Vec<(Range<Anchor>, String)>> {
130 let mut edits = Vec::new();
131
132 let mut model_edits = current_edits.into_iter().peekable();
133 for user_edit in new_snapshot.edits_since::<usize>(&old_snapshot.version) {
134 while let Some((model_old_range, _)) = model_edits.peek() {
135 let model_old_range = model_old_range.to_offset(old_snapshot);
136 if model_old_range.end < user_edit.old.start {
137 let (model_old_range, model_new_text) = model_edits.next().unwrap();
138 edits.push((model_old_range.clone(), model_new_text.clone()));
139 } else {
140 break;
141 }
142 }
143
144 if let Some((model_old_range, model_new_text)) = model_edits.peek() {
145 let model_old_offset_range = model_old_range.to_offset(old_snapshot);
146 if user_edit.old == model_old_offset_range {
147 let user_new_text = new_snapshot
148 .text_for_range(user_edit.new.clone())
149 .collect::<String>();
150
151 if let Some(model_suffix) = model_new_text.strip_prefix(&user_new_text) {
152 if !model_suffix.is_empty() {
153 let anchor = old_snapshot.anchor_after(user_edit.old.end);
154 edits.push((anchor..anchor, model_suffix.to_string()));
155 }
156
157 model_edits.next();
158 continue;
159 }
160 }
161 }
162
163 return None;
164 }
165
166 edits.extend(model_edits.cloned());
167
168 if edits.is_empty() { None } else { Some(edits) }
169}
170
171impl std::fmt::Debug for InlineCompletion {
172 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
173 f.debug_struct("InlineCompletion")
174 .field("id", &self.id)
175 .field("path", &self.path)
176 .field("edits", &self.edits)
177 .finish_non_exhaustive()
178 }
179}
180
181pub struct Zeta {
182 workspace: Option<WeakEntity<Workspace>>,
183 client: Arc<Client>,
184 events: VecDeque<Event>,
185 registered_buffers: HashMap<gpui::EntityId, RegisteredBuffer>,
186 shown_completions: VecDeque<InlineCompletion>,
187 rated_completions: HashSet<InlineCompletionId>,
188 data_collection_choice: Entity<DataCollectionChoice>,
189 llm_token: LlmApiToken,
190 _llm_token_subscription: Subscription,
191 last_usage: Option<EditPredictionUsage>,
192 /// Whether the terms of service have been accepted.
193 tos_accepted: bool,
194 /// Whether an update to a newer version of Zed is required to continue using Zeta.
195 update_required: bool,
196 user_store: Entity<UserStore>,
197 _user_store_subscription: Subscription,
198 license_detection_watchers: HashMap<WorktreeId, Rc<LicenseDetectionWatcher>>,
199}
200
201impl Zeta {
202 pub fn global(cx: &mut App) -> Option<Entity<Self>> {
203 cx.try_global::<ZetaGlobal>().map(|global| global.0.clone())
204 }
205
206 pub fn register(
207 workspace: Option<WeakEntity<Workspace>>,
208 worktree: Option<Entity<Worktree>>,
209 client: Arc<Client>,
210 user_store: Entity<UserStore>,
211 cx: &mut App,
212 ) -> Entity<Self> {
213 let this = Self::global(cx).unwrap_or_else(|| {
214 let entity = cx.new(|cx| Self::new(workspace, client, user_store, cx));
215 cx.set_global(ZetaGlobal(entity.clone()));
216 entity
217 });
218
219 this.update(cx, move |this, cx| {
220 if let Some(worktree) = worktree {
221 worktree.update(cx, |worktree, cx| {
222 this.license_detection_watchers
223 .entry(worktree.id())
224 .or_insert_with(|| Rc::new(LicenseDetectionWatcher::new(worktree, cx)));
225 });
226 }
227 });
228
229 this
230 }
231
232 pub fn clear_history(&mut self) {
233 self.events.clear();
234 }
235
236 pub fn usage(&self, cx: &App) -> Option<EditPredictionUsage> {
237 self.last_usage.or_else(|| {
238 let user_store = self.user_store.read(cx);
239 maybe!({
240 let amount = user_store.edit_predictions_usage_amount()?;
241 let limit = user_store.edit_predictions_usage_limit()?.variant?;
242
243 Some(EditPredictionUsage {
244 amount: amount as i32,
245 limit: match limit {
246 proto::usage_limit::Variant::Limited(limited) => {
247 zed_llm_client::UsageLimit::Limited(limited.limit as i32)
248 }
249 proto::usage_limit::Variant::Unlimited(_) => {
250 zed_llm_client::UsageLimit::Unlimited
251 }
252 },
253 })
254 })
255 })
256 }
257
258 fn new(
259 workspace: Option<WeakEntity<Workspace>>,
260 client: Arc<Client>,
261 user_store: Entity<UserStore>,
262 cx: &mut Context<Self>,
263 ) -> Self {
264 let refresh_llm_token_listener = RefreshLlmTokenListener::global(cx);
265
266 let data_collection_choice = Self::load_data_collection_choices();
267 let data_collection_choice = cx.new(|_| data_collection_choice);
268
269 Self {
270 workspace,
271 client,
272 events: VecDeque::new(),
273 shown_completions: VecDeque::new(),
274 rated_completions: HashSet::default(),
275 registered_buffers: HashMap::default(),
276 data_collection_choice,
277 llm_token: LlmApiToken::default(),
278 _llm_token_subscription: cx.subscribe(
279 &refresh_llm_token_listener,
280 |this, _listener, _event, cx| {
281 let client = this.client.clone();
282 let llm_token = this.llm_token.clone();
283 cx.spawn(async move |_this, _cx| {
284 llm_token.refresh(&client).await?;
285 anyhow::Ok(())
286 })
287 .detach_and_log_err(cx);
288 },
289 ),
290 last_usage: None,
291 tos_accepted: user_store
292 .read(cx)
293 .current_user_has_accepted_terms()
294 .unwrap_or(false),
295 update_required: false,
296 _user_store_subscription: cx.subscribe(&user_store, |this, user_store, event, cx| {
297 match event {
298 client::user::Event::PrivateUserInfoUpdated => {
299 this.tos_accepted = user_store
300 .read(cx)
301 .current_user_has_accepted_terms()
302 .unwrap_or(false);
303 }
304 _ => {}
305 }
306 }),
307 license_detection_watchers: HashMap::default(),
308 user_store,
309 }
310 }
311
312 fn push_event(&mut self, event: Event) {
313 if let Some(Event::BufferChange {
314 new_snapshot: last_new_snapshot,
315 timestamp: last_timestamp,
316 ..
317 }) = self.events.back_mut()
318 {
319 // Coalesce edits for the same buffer when they happen one after the other.
320 let Event::BufferChange {
321 old_snapshot,
322 new_snapshot,
323 timestamp,
324 } = &event;
325
326 if timestamp.duration_since(*last_timestamp) <= BUFFER_CHANGE_GROUPING_INTERVAL
327 && old_snapshot.remote_id() == last_new_snapshot.remote_id()
328 && old_snapshot.version == last_new_snapshot.version
329 {
330 *last_new_snapshot = new_snapshot.clone();
331 *last_timestamp = *timestamp;
332 return;
333 }
334 }
335
336 self.events.push_back(event);
337 if self.events.len() >= MAX_EVENT_COUNT {
338 self.events.drain(..MAX_EVENT_COUNT / 2);
339 }
340 }
341
342 pub fn register_buffer(&mut self, buffer: &Entity<Buffer>, cx: &mut Context<Self>) {
343 let buffer_id = buffer.entity_id();
344 let weak_buffer = buffer.downgrade();
345
346 if let std::collections::hash_map::Entry::Vacant(entry) =
347 self.registered_buffers.entry(buffer_id)
348 {
349 let snapshot = buffer.read(cx).snapshot();
350
351 entry.insert(RegisteredBuffer {
352 snapshot,
353 _subscriptions: [
354 cx.subscribe(buffer, move |this, buffer, event, cx| {
355 this.handle_buffer_event(buffer, event, cx);
356 }),
357 cx.observe_release(buffer, move |this, _buffer, _cx| {
358 this.registered_buffers.remove(&weak_buffer.entity_id());
359 }),
360 ],
361 });
362 };
363 }
364
365 fn handle_buffer_event(
366 &mut self,
367 buffer: Entity<Buffer>,
368 event: &language::BufferEvent,
369 cx: &mut Context<Self>,
370 ) {
371 if let language::BufferEvent::Edited = event {
372 self.report_changes_for_buffer(&buffer, cx);
373 }
374 }
375
376 fn request_completion_impl<F, R>(
377 &mut self,
378 workspace: Option<Entity<Workspace>>,
379 project: Option<&Entity<Project>>,
380 buffer: &Entity<Buffer>,
381 cursor: language::Anchor,
382 can_collect_data: bool,
383 cx: &mut Context<Self>,
384 perform_predict_edits: F,
385 ) -> Task<Result<Option<InlineCompletion>>>
386 where
387 F: FnOnce(PerformPredictEditsParams) -> R + 'static,
388 R: Future<Output = Result<(PredictEditsResponse, Option<EditPredictionUsage>)>>
389 + Send
390 + 'static,
391 {
392 let snapshot = self.report_changes_for_buffer(&buffer, cx);
393 let diagnostic_groups = snapshot.diagnostic_groups(None);
394 let cursor_point = cursor.to_point(&snapshot);
395 let cursor_offset = cursor_point.to_offset(&snapshot);
396 let events = self.events.clone();
397 let path: Arc<Path> = snapshot
398 .file()
399 .map(|f| Arc::from(f.full_path(cx).as_path()))
400 .unwrap_or_else(|| Arc::from(Path::new("untitled")));
401
402 let zeta = cx.entity();
403 let client = self.client.clone();
404 let llm_token = self.llm_token.clone();
405 let app_version = AppVersion::global(cx);
406
407 let buffer = buffer.clone();
408
409 let local_lsp_store =
410 project.and_then(|project| project.read(cx).lsp_store().read(cx).as_local());
411 let diagnostic_groups = if let Some(local_lsp_store) = local_lsp_store {
412 Some(
413 diagnostic_groups
414 .into_iter()
415 .filter_map(|(language_server_id, diagnostic_group)| {
416 let language_server =
417 local_lsp_store.running_language_server_for_id(language_server_id)?;
418
419 Some((
420 language_server.name(),
421 diagnostic_group.resolve::<usize>(&snapshot),
422 ))
423 })
424 .collect::<Vec<_>>(),
425 )
426 } else {
427 None
428 };
429
430 cx.spawn(async move |this, cx| {
431 let request_sent_at = Instant::now();
432
433 struct BackgroundValues {
434 input_events: String,
435 input_excerpt: String,
436 speculated_output: String,
437 editable_range: Range<usize>,
438 input_outline: String,
439 }
440
441 let values = cx
442 .background_spawn({
443 let snapshot = snapshot.clone();
444 let path = path.clone();
445 async move {
446 let path = path.to_string_lossy();
447 let input_excerpt = excerpt_for_cursor_position(
448 cursor_point,
449 &path,
450 &snapshot,
451 MAX_REWRITE_TOKENS,
452 MAX_CONTEXT_TOKENS,
453 );
454 let input_events = prompt_for_events(&events, MAX_EVENT_TOKENS);
455 let input_outline = prompt_for_outline(&snapshot);
456
457 anyhow::Ok(BackgroundValues {
458 input_events,
459 input_excerpt: input_excerpt.prompt,
460 speculated_output: input_excerpt.speculated_output,
461 editable_range: input_excerpt.editable_range.to_offset(&snapshot),
462 input_outline,
463 })
464 }
465 })
466 .await?;
467
468 log::debug!(
469 "Events:\n{}\nExcerpt:\n{:?}",
470 values.input_events,
471 values.input_excerpt
472 );
473
474 let body = PredictEditsBody {
475 input_events: values.input_events.clone(),
476 input_excerpt: values.input_excerpt.clone(),
477 speculated_output: Some(values.speculated_output),
478 outline: Some(values.input_outline.clone()),
479 can_collect_data,
480 diagnostic_groups: diagnostic_groups.and_then(|diagnostic_groups| {
481 diagnostic_groups
482 .into_iter()
483 .map(|(name, diagnostic_group)| {
484 Ok((name.to_string(), serde_json::to_value(diagnostic_group)?))
485 })
486 .collect::<Result<Vec<_>>>()
487 .log_err()
488 }),
489 };
490
491 let response = perform_predict_edits(PerformPredictEditsParams {
492 client,
493 llm_token,
494 app_version,
495 body,
496 })
497 .await;
498 let (response, usage) = match response {
499 Ok(response) => response,
500 Err(err) => {
501 if err.is::<ZedUpdateRequiredError>() {
502 cx.update(|cx| {
503 zeta.update(cx, |zeta, _cx| {
504 zeta.update_required = true;
505 });
506
507 if let Some(workspace) = workspace {
508 workspace.update(cx, |workspace, cx| {
509 workspace.show_notification(
510 NotificationId::unique::<ZedUpdateRequiredError>(),
511 cx,
512 |cx| {
513 cx.new(|cx| {
514 ErrorMessagePrompt::new(err.to_string(), cx)
515 .with_link_button(
516 "Update Zed",
517 "https://zed.dev/releases",
518 )
519 })
520 },
521 );
522 });
523 }
524 })
525 .ok();
526 }
527
528 return Err(err);
529 }
530 };
531
532 log::debug!("completion response: {}", &response.output_excerpt);
533
534 if let Some(usage) = usage {
535 this.update(cx, |this, _cx| {
536 this.last_usage = Some(usage);
537 })
538 .ok();
539 }
540
541 Self::process_completion_response(
542 response,
543 buffer,
544 &snapshot,
545 values.editable_range,
546 cursor_offset,
547 path,
548 values.input_outline,
549 values.input_events,
550 values.input_excerpt,
551 request_sent_at,
552 &cx,
553 )
554 .await
555 })
556 }
557
558 // Generates several example completions of various states to fill the Zeta completion modal
559 #[cfg(any(test, feature = "test-support"))]
560 pub fn fill_with_fake_completions(&mut self, cx: &mut Context<Self>) -> Task<()> {
561 use language::Point;
562
563 let test_buffer_text = indoc::indoc! {r#"a longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg line
564 And maybe a short line
565
566 Then a few lines
567
568 and then another
569 "#};
570
571 let project = None;
572 let buffer = cx.new(|cx| Buffer::local(test_buffer_text, cx));
573 let position = buffer.read(cx).anchor_before(Point::new(1, 0));
574
575 let completion_tasks = vec![
576 self.fake_completion(
577 project,
578 &buffer,
579 position,
580 PredictEditsResponse {
581 request_id: Uuid::parse_str("e7861db5-0cea-4761-b1c5-ad083ac53a80").unwrap(),
582 output_excerpt: format!("{EDITABLE_REGION_START_MARKER}
583a longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg line
584[here's an edit]
585And maybe a short line
586Then a few lines
587and then another
588{EDITABLE_REGION_END_MARKER}
589 ", ),
590 },
591 cx,
592 ),
593 self.fake_completion(
594 project,
595 &buffer,
596 position,
597 PredictEditsResponse {
598 request_id: Uuid::parse_str("077c556a-2c49-44e2-bbc6-dafc09032a5e").unwrap(),
599 output_excerpt: format!(r#"{EDITABLE_REGION_START_MARKER}
600a longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg line
601And maybe a short line
602[and another edit]
603Then a few lines
604and then another
605{EDITABLE_REGION_END_MARKER}
606 "#),
607 },
608 cx,
609 ),
610 self.fake_completion(
611 project,
612 &buffer,
613 position,
614 PredictEditsResponse {
615 request_id: Uuid::parse_str("df8c7b23-3d1d-4f99-a306-1f6264a41277").unwrap(),
616 output_excerpt: format!(r#"{EDITABLE_REGION_START_MARKER}
617a longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg line
618And maybe a short line
619
620Then a few lines
621
622and then another
623{EDITABLE_REGION_END_MARKER}
624 "#),
625 },
626 cx,
627 ),
628 self.fake_completion(
629 project,
630 &buffer,
631 position,
632 PredictEditsResponse {
633 request_id: Uuid::parse_str("c743958d-e4d8-44a8-aa5b-eb1e305c5f5c").unwrap(),
634 output_excerpt: format!(r#"{EDITABLE_REGION_START_MARKER}
635a longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg line
636And maybe a short line
637
638Then a few lines
639
640and then another
641{EDITABLE_REGION_END_MARKER}
642 "#),
643 },
644 cx,
645 ),
646 self.fake_completion(
647 project,
648 &buffer,
649 position,
650 PredictEditsResponse {
651 request_id: Uuid::parse_str("ff5cd7ab-ad06-4808-986e-d3391e7b8355").unwrap(),
652 output_excerpt: format!(r#"{EDITABLE_REGION_START_MARKER}
653a longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg line
654And maybe a short line
655Then a few lines
656[a third completion]
657and then another
658{EDITABLE_REGION_END_MARKER}
659 "#),
660 },
661 cx,
662 ),
663 self.fake_completion(
664 project,
665 &buffer,
666 position,
667 PredictEditsResponse {
668 request_id: Uuid::parse_str("83cafa55-cdba-4b27-8474-1865ea06be94").unwrap(),
669 output_excerpt: format!(r#"{EDITABLE_REGION_START_MARKER}
670a longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg line
671And maybe a short line
672and then another
673[fourth completion example]
674{EDITABLE_REGION_END_MARKER}
675 "#),
676 },
677 cx,
678 ),
679 self.fake_completion(
680 project,
681 &buffer,
682 position,
683 PredictEditsResponse {
684 request_id: Uuid::parse_str("d5bd3afd-8723-47c7-bd77-15a3a926867b").unwrap(),
685 output_excerpt: format!(r#"{EDITABLE_REGION_START_MARKER}
686a longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg line
687And maybe a short line
688Then a few lines
689and then another
690[fifth and final completion]
691{EDITABLE_REGION_END_MARKER}
692 "#),
693 },
694 cx,
695 ),
696 ];
697
698 cx.spawn(async move |zeta, cx| {
699 for task in completion_tasks {
700 task.await.unwrap();
701 }
702
703 zeta.update(cx, |zeta, _cx| {
704 zeta.shown_completions.get_mut(2).unwrap().edits = Arc::new([]);
705 zeta.shown_completions.get_mut(3).unwrap().edits = Arc::new([]);
706 })
707 .ok();
708 })
709 }
710
711 #[cfg(any(test, feature = "test-support"))]
712 pub fn fake_completion(
713 &mut self,
714 project: Option<&Entity<Project>>,
715 buffer: &Entity<Buffer>,
716 position: language::Anchor,
717 response: PredictEditsResponse,
718 cx: &mut Context<Self>,
719 ) -> Task<Result<Option<InlineCompletion>>> {
720 use std::future::ready;
721
722 self.request_completion_impl(None, project, buffer, position, false, cx, |_params| {
723 ready(Ok((response, None)))
724 })
725 }
726
727 pub fn request_completion(
728 &mut self,
729 project: Option<&Entity<Project>>,
730 buffer: &Entity<Buffer>,
731 position: language::Anchor,
732 can_collect_data: bool,
733 cx: &mut Context<Self>,
734 ) -> Task<Result<Option<InlineCompletion>>> {
735 let workspace = self
736 .workspace
737 .as_ref()
738 .and_then(|workspace| workspace.upgrade());
739 self.request_completion_impl(
740 workspace,
741 project,
742 buffer,
743 position,
744 can_collect_data,
745 cx,
746 Self::perform_predict_edits,
747 )
748 }
749
750 fn perform_predict_edits(
751 params: PerformPredictEditsParams,
752 ) -> impl Future<Output = Result<(PredictEditsResponse, Option<EditPredictionUsage>)>> {
753 async move {
754 let PerformPredictEditsParams {
755 client,
756 llm_token,
757 app_version,
758 body,
759 ..
760 } = params;
761
762 let http_client = client.http_client();
763 let mut token = llm_token.acquire(&client).await?;
764 let mut did_retry = false;
765
766 loop {
767 let request_builder = http_client::Request::builder().method(Method::POST);
768 let request_builder =
769 if let Ok(predict_edits_url) = std::env::var("ZED_PREDICT_EDITS_URL") {
770 request_builder.uri(predict_edits_url)
771 } else {
772 request_builder.uri(
773 http_client
774 .build_zed_llm_url("/predict_edits/v2", &[])?
775 .as_ref(),
776 )
777 };
778 let request = request_builder
779 .header("Content-Type", "application/json")
780 .header("Authorization", format!("Bearer {}", token))
781 .header(ZED_VERSION_HEADER_NAME, app_version.to_string())
782 .body(serde_json::to_string(&body)?.into())?;
783
784 let mut response = http_client.send(request).await?;
785
786 if let Some(minimum_required_version) = response
787 .headers()
788 .get(MINIMUM_REQUIRED_VERSION_HEADER_NAME)
789 .and_then(|version| SemanticVersion::from_str(version.to_str().ok()?).ok())
790 {
791 anyhow::ensure!(
792 app_version >= minimum_required_version,
793 ZedUpdateRequiredError {
794 minimum_version: minimum_required_version
795 }
796 );
797 }
798
799 if response.status().is_success() {
800 let usage = EditPredictionUsage::from_headers(response.headers()).ok();
801
802 let mut body = String::new();
803 response.body_mut().read_to_string(&mut body).await?;
804 return Ok((serde_json::from_str(&body)?, usage));
805 } else if !did_retry
806 && response
807 .headers()
808 .get(EXPIRED_LLM_TOKEN_HEADER_NAME)
809 .is_some()
810 {
811 did_retry = true;
812 token = llm_token.refresh(&client).await?;
813 } else {
814 let mut body = String::new();
815 response.body_mut().read_to_string(&mut body).await?;
816 anyhow::bail!(
817 "error predicting edits.\nStatus: {:?}\nBody: {}",
818 response.status(),
819 body
820 );
821 }
822 }
823 }
824 }
825
826 fn accept_edit_prediction(
827 &mut self,
828 request_id: InlineCompletionId,
829 cx: &mut Context<Self>,
830 ) -> Task<Result<()>> {
831 let client = self.client.clone();
832 let llm_token = self.llm_token.clone();
833 let app_version = AppVersion::global(cx);
834 cx.spawn(async move |this, cx| {
835 let http_client = client.http_client();
836 let mut response = llm_token_retry(&llm_token, &client, |token| {
837 let request_builder = http_client::Request::builder().method(Method::POST);
838 let request_builder =
839 if let Ok(accept_prediction_url) = std::env::var("ZED_ACCEPT_PREDICTION_URL") {
840 request_builder.uri(accept_prediction_url)
841 } else {
842 request_builder.uri(
843 http_client
844 .build_zed_llm_url("/predict_edits/accept", &[])?
845 .as_ref(),
846 )
847 };
848 Ok(request_builder
849 .header("Content-Type", "application/json")
850 .header("Authorization", format!("Bearer {}", token))
851 .header(ZED_VERSION_HEADER_NAME, app_version.to_string())
852 .body(
853 serde_json::to_string(&AcceptEditPredictionBody {
854 request_id: request_id.0,
855 })?
856 .into(),
857 )?)
858 })
859 .await?;
860
861 if let Some(minimum_required_version) = response
862 .headers()
863 .get(MINIMUM_REQUIRED_VERSION_HEADER_NAME)
864 .and_then(|version| SemanticVersion::from_str(version.to_str().ok()?).ok())
865 {
866 if app_version < minimum_required_version {
867 return Err(anyhow!(ZedUpdateRequiredError {
868 minimum_version: minimum_required_version
869 }));
870 }
871 }
872
873 if response.status().is_success() {
874 if let Some(usage) = EditPredictionUsage::from_headers(response.headers()).ok() {
875 this.update(cx, |this, cx| {
876 this.last_usage = Some(usage);
877 cx.notify();
878 })?;
879 }
880
881 Ok(())
882 } else {
883 let mut body = String::new();
884 response.body_mut().read_to_string(&mut body).await?;
885 Err(anyhow!(
886 "error accepting edit prediction.\nStatus: {:?}\nBody: {}",
887 response.status(),
888 body
889 ))
890 }
891 })
892 }
893
894 fn process_completion_response(
895 prediction_response: PredictEditsResponse,
896 buffer: Entity<Buffer>,
897 snapshot: &BufferSnapshot,
898 editable_range: Range<usize>,
899 cursor_offset: usize,
900 path: Arc<Path>,
901 input_outline: String,
902 input_events: String,
903 input_excerpt: String,
904 request_sent_at: Instant,
905 cx: &AsyncApp,
906 ) -> Task<Result<Option<InlineCompletion>>> {
907 let snapshot = snapshot.clone();
908 let request_id = prediction_response.request_id;
909 let output_excerpt = prediction_response.output_excerpt;
910 cx.spawn(async move |cx| {
911 let output_excerpt: Arc<str> = output_excerpt.into();
912
913 let edits: Arc<[(Range<Anchor>, String)]> = cx
914 .background_spawn({
915 let output_excerpt = output_excerpt.clone();
916 let editable_range = editable_range.clone();
917 let snapshot = snapshot.clone();
918 async move { Self::parse_edits(output_excerpt, editable_range, &snapshot) }
919 })
920 .await?
921 .into();
922
923 let Some((edits, snapshot, edit_preview)) = buffer.read_with(cx, {
924 let edits = edits.clone();
925 |buffer, cx| {
926 let new_snapshot = buffer.snapshot();
927 let edits: Arc<[(Range<Anchor>, String)]> =
928 interpolate(&snapshot, &new_snapshot, edits)?.into();
929 Some((edits.clone(), new_snapshot, buffer.preview_edits(edits, cx)))
930 }
931 })?
932 else {
933 return anyhow::Ok(None);
934 };
935
936 let edit_preview = edit_preview.await;
937
938 Ok(Some(InlineCompletion {
939 id: InlineCompletionId(request_id),
940 path,
941 excerpt_range: editable_range,
942 cursor_offset,
943 edits,
944 edit_preview,
945 snapshot,
946 input_outline: input_outline.into(),
947 input_events: input_events.into(),
948 input_excerpt: input_excerpt.into(),
949 output_excerpt,
950 request_sent_at,
951 response_received_at: Instant::now(),
952 }))
953 })
954 }
955
956 fn parse_edits(
957 output_excerpt: Arc<str>,
958 editable_range: Range<usize>,
959 snapshot: &BufferSnapshot,
960 ) -> Result<Vec<(Range<Anchor>, String)>> {
961 let content = output_excerpt.replace(CURSOR_MARKER, "");
962
963 let start_markers = content
964 .match_indices(EDITABLE_REGION_START_MARKER)
965 .collect::<Vec<_>>();
966 anyhow::ensure!(
967 start_markers.len() == 1,
968 "expected exactly one start marker, found {}",
969 start_markers.len()
970 );
971
972 let end_markers = content
973 .match_indices(EDITABLE_REGION_END_MARKER)
974 .collect::<Vec<_>>();
975 anyhow::ensure!(
976 end_markers.len() == 1,
977 "expected exactly one end marker, found {}",
978 end_markers.len()
979 );
980
981 let sof_markers = content
982 .match_indices(START_OF_FILE_MARKER)
983 .collect::<Vec<_>>();
984 anyhow::ensure!(
985 sof_markers.len() <= 1,
986 "expected at most one start-of-file marker, found {}",
987 sof_markers.len()
988 );
989
990 let codefence_start = start_markers[0].0;
991 let content = &content[codefence_start..];
992
993 let newline_ix = content.find('\n').context("could not find newline")?;
994 let content = &content[newline_ix + 1..];
995
996 let codefence_end = content
997 .rfind(&format!("\n{EDITABLE_REGION_END_MARKER}"))
998 .context("could not find end marker")?;
999 let new_text = &content[..codefence_end];
1000
1001 let old_text = snapshot
1002 .text_for_range(editable_range.clone())
1003 .collect::<String>();
1004
1005 Ok(Self::compute_edits(
1006 old_text,
1007 new_text,
1008 editable_range.start,
1009 &snapshot,
1010 ))
1011 }
1012
1013 pub fn compute_edits(
1014 old_text: String,
1015 new_text: &str,
1016 offset: usize,
1017 snapshot: &BufferSnapshot,
1018 ) -> Vec<(Range<Anchor>, String)> {
1019 text_diff(&old_text, &new_text)
1020 .into_iter()
1021 .map(|(mut old_range, new_text)| {
1022 old_range.start += offset;
1023 old_range.end += offset;
1024
1025 let prefix_len = common_prefix(
1026 snapshot.chars_for_range(old_range.clone()),
1027 new_text.chars(),
1028 );
1029 old_range.start += prefix_len;
1030
1031 let suffix_len = common_prefix(
1032 snapshot.reversed_chars_for_range(old_range.clone()),
1033 new_text[prefix_len..].chars().rev(),
1034 );
1035 old_range.end = old_range.end.saturating_sub(suffix_len);
1036
1037 let new_text = new_text[prefix_len..new_text.len() - suffix_len].to_string();
1038 let range = if old_range.is_empty() {
1039 let anchor = snapshot.anchor_after(old_range.start);
1040 anchor..anchor
1041 } else {
1042 snapshot.anchor_after(old_range.start)..snapshot.anchor_before(old_range.end)
1043 };
1044 (range, new_text)
1045 })
1046 .collect()
1047 }
1048
1049 pub fn is_completion_rated(&self, completion_id: InlineCompletionId) -> bool {
1050 self.rated_completions.contains(&completion_id)
1051 }
1052
1053 pub fn completion_shown(&mut self, completion: &InlineCompletion, cx: &mut Context<Self>) {
1054 self.shown_completions.push_front(completion.clone());
1055 if self.shown_completions.len() > 50 {
1056 let completion = self.shown_completions.pop_back().unwrap();
1057 self.rated_completions.remove(&completion.id);
1058 }
1059 cx.notify();
1060 }
1061
1062 pub fn rate_completion(
1063 &mut self,
1064 completion: &InlineCompletion,
1065 rating: InlineCompletionRating,
1066 feedback: String,
1067 cx: &mut Context<Self>,
1068 ) {
1069 self.rated_completions.insert(completion.id);
1070 telemetry::event!(
1071 "Edit Prediction Rated",
1072 rating,
1073 input_events = completion.input_events,
1074 input_excerpt = completion.input_excerpt,
1075 input_outline = completion.input_outline,
1076 output_excerpt = completion.output_excerpt,
1077 feedback
1078 );
1079 self.client.telemetry().flush_events().detach();
1080 cx.notify();
1081 }
1082
1083 pub fn shown_completions(&self) -> impl DoubleEndedIterator<Item = &InlineCompletion> {
1084 self.shown_completions.iter()
1085 }
1086
1087 pub fn shown_completions_len(&self) -> usize {
1088 self.shown_completions.len()
1089 }
1090
1091 fn report_changes_for_buffer(
1092 &mut self,
1093 buffer: &Entity<Buffer>,
1094 cx: &mut Context<Self>,
1095 ) -> BufferSnapshot {
1096 self.register_buffer(buffer, cx);
1097
1098 let registered_buffer = self
1099 .registered_buffers
1100 .get_mut(&buffer.entity_id())
1101 .unwrap();
1102 let new_snapshot = buffer.read(cx).snapshot();
1103
1104 if new_snapshot.version != registered_buffer.snapshot.version {
1105 let old_snapshot = mem::replace(&mut registered_buffer.snapshot, new_snapshot.clone());
1106 self.push_event(Event::BufferChange {
1107 old_snapshot,
1108 new_snapshot: new_snapshot.clone(),
1109 timestamp: Instant::now(),
1110 });
1111 }
1112
1113 new_snapshot
1114 }
1115
1116 fn load_data_collection_choices() -> DataCollectionChoice {
1117 let choice = KEY_VALUE_STORE
1118 .read_kvp(ZED_PREDICT_DATA_COLLECTION_CHOICE)
1119 .log_err()
1120 .flatten();
1121
1122 match choice.as_deref() {
1123 Some("true") => DataCollectionChoice::Enabled,
1124 Some("false") => DataCollectionChoice::Disabled,
1125 Some(_) => {
1126 log::error!("unknown value in '{ZED_PREDICT_DATA_COLLECTION_CHOICE}'");
1127 DataCollectionChoice::NotAnswered
1128 }
1129 None => DataCollectionChoice::NotAnswered,
1130 }
1131 }
1132}
1133
1134struct PerformPredictEditsParams {
1135 pub client: Arc<Client>,
1136 pub llm_token: LlmApiToken,
1137 pub app_version: SemanticVersion,
1138 pub body: PredictEditsBody,
1139}
1140
1141#[derive(Error, Debug)]
1142#[error(
1143 "You must update to Zed version {minimum_version} or higher to continue using edit predictions."
1144)]
1145pub struct ZedUpdateRequiredError {
1146 minimum_version: SemanticVersion,
1147}
1148
1149struct LicenseDetectionWatcher {
1150 is_open_source_rx: watch::Receiver<bool>,
1151 _is_open_source_task: Task<()>,
1152}
1153
1154impl LicenseDetectionWatcher {
1155 pub fn new(worktree: &Worktree, cx: &mut Context<Worktree>) -> Self {
1156 let (mut is_open_source_tx, is_open_source_rx) = watch::channel_with::<bool>(false);
1157
1158 // Check if worktree is a single file, if so we do not need to check for a LICENSE file
1159 let task = if worktree.abs_path().is_file() {
1160 Task::ready(())
1161 } else {
1162 let loaded_files = LICENSE_FILES_TO_CHECK
1163 .iter()
1164 .map(Path::new)
1165 .map(|file| worktree.load_file(file, cx))
1166 .collect::<ArrayVec<_, { LICENSE_FILES_TO_CHECK.len() }>>();
1167
1168 cx.background_spawn(async move {
1169 for loaded_file in loaded_files.into_iter() {
1170 let Ok(loaded_file) = loaded_file.await else {
1171 continue;
1172 };
1173
1174 let path = &loaded_file.file.path;
1175 if is_license_eligible_for_data_collection(&loaded_file.text) {
1176 log::info!("detected '{path:?}' as open source license");
1177 *is_open_source_tx.borrow_mut() = true;
1178 } else {
1179 log::info!("didn't detect '{path:?}' as open source license");
1180 }
1181
1182 // stop on the first license that successfully read
1183 return;
1184 }
1185
1186 log::debug!("didn't find a license file to check, assuming closed source");
1187 })
1188 };
1189
1190 Self {
1191 is_open_source_rx,
1192 _is_open_source_task: task,
1193 }
1194 }
1195
1196 /// Answers false until we find out it's open source
1197 pub fn is_project_open_source(&self) -> bool {
1198 *self.is_open_source_rx.borrow()
1199 }
1200}
1201
1202fn common_prefix<T1: Iterator<Item = char>, T2: Iterator<Item = char>>(a: T1, b: T2) -> usize {
1203 a.zip(b)
1204 .take_while(|(a, b)| a == b)
1205 .map(|(a, _)| a.len_utf8())
1206 .sum()
1207}
1208
1209fn prompt_for_outline(snapshot: &BufferSnapshot) -> String {
1210 let mut input_outline = String::new();
1211
1212 writeln!(
1213 input_outline,
1214 "```{}",
1215 snapshot
1216 .file()
1217 .map_or(Cow::Borrowed("untitled"), |file| file
1218 .path()
1219 .to_string_lossy())
1220 )
1221 .unwrap();
1222
1223 if let Some(outline) = snapshot.outline(None) {
1224 for item in &outline.items {
1225 let spacing = " ".repeat(item.depth);
1226 writeln!(input_outline, "{}{}", spacing, item.text).unwrap();
1227 }
1228 }
1229
1230 writeln!(input_outline, "```").unwrap();
1231
1232 input_outline
1233}
1234
1235fn prompt_for_events(events: &VecDeque<Event>, mut remaining_tokens: usize) -> String {
1236 let mut result = String::new();
1237 for event in events.iter().rev() {
1238 let event_string = event.to_prompt();
1239 let event_tokens = tokens_for_bytes(event_string.len());
1240 if event_tokens > remaining_tokens {
1241 break;
1242 }
1243
1244 if !result.is_empty() {
1245 result.insert_str(0, "\n\n");
1246 }
1247 result.insert_str(0, &event_string);
1248 remaining_tokens -= event_tokens;
1249 }
1250 result
1251}
1252
1253struct RegisteredBuffer {
1254 snapshot: BufferSnapshot,
1255 _subscriptions: [gpui::Subscription; 2],
1256}
1257
1258#[derive(Clone)]
1259enum Event {
1260 BufferChange {
1261 old_snapshot: BufferSnapshot,
1262 new_snapshot: BufferSnapshot,
1263 timestamp: Instant,
1264 },
1265}
1266
1267impl Event {
1268 fn to_prompt(&self) -> String {
1269 match self {
1270 Event::BufferChange {
1271 old_snapshot,
1272 new_snapshot,
1273 ..
1274 } => {
1275 let mut prompt = String::new();
1276
1277 let old_path = old_snapshot
1278 .file()
1279 .map(|f| f.path().as_ref())
1280 .unwrap_or(Path::new("untitled"));
1281 let new_path = new_snapshot
1282 .file()
1283 .map(|f| f.path().as_ref())
1284 .unwrap_or(Path::new("untitled"));
1285 if old_path != new_path {
1286 writeln!(prompt, "User renamed {:?} to {:?}\n", old_path, new_path).unwrap();
1287 }
1288
1289 let diff = language::unified_diff(&old_snapshot.text(), &new_snapshot.text());
1290 if !diff.is_empty() {
1291 write!(
1292 prompt,
1293 "User edited {:?}:\n```diff\n{}\n```",
1294 new_path, diff
1295 )
1296 .unwrap();
1297 }
1298
1299 prompt
1300 }
1301 }
1302 }
1303}
1304
1305#[derive(Debug, Clone)]
1306struct CurrentInlineCompletion {
1307 buffer_id: EntityId,
1308 completion: InlineCompletion,
1309}
1310
1311impl CurrentInlineCompletion {
1312 fn should_replace_completion(&self, old_completion: &Self, snapshot: &BufferSnapshot) -> bool {
1313 if self.buffer_id != old_completion.buffer_id {
1314 return true;
1315 }
1316
1317 let Some(old_edits) = old_completion.completion.interpolate(&snapshot) else {
1318 return true;
1319 };
1320 let Some(new_edits) = self.completion.interpolate(&snapshot) else {
1321 return false;
1322 };
1323
1324 if old_edits.len() == 1 && new_edits.len() == 1 {
1325 let (old_range, old_text) = &old_edits[0];
1326 let (new_range, new_text) = &new_edits[0];
1327 new_range == old_range && new_text.starts_with(old_text)
1328 } else {
1329 true
1330 }
1331 }
1332}
1333
1334struct PendingCompletion {
1335 id: usize,
1336 _task: Task<()>,
1337}
1338
1339#[derive(Debug, Clone, Copy)]
1340pub enum DataCollectionChoice {
1341 NotAnswered,
1342 Enabled,
1343 Disabled,
1344}
1345
1346impl DataCollectionChoice {
1347 pub fn is_enabled(self) -> bool {
1348 match self {
1349 Self::Enabled => true,
1350 Self::NotAnswered | Self::Disabled => false,
1351 }
1352 }
1353
1354 pub fn is_answered(self) -> bool {
1355 match self {
1356 Self::Enabled | Self::Disabled => true,
1357 Self::NotAnswered => false,
1358 }
1359 }
1360
1361 pub fn toggle(&self) -> DataCollectionChoice {
1362 match self {
1363 Self::Enabled => Self::Disabled,
1364 Self::Disabled => Self::Enabled,
1365 Self::NotAnswered => Self::Enabled,
1366 }
1367 }
1368}
1369
1370impl From<bool> for DataCollectionChoice {
1371 fn from(value: bool) -> Self {
1372 match value {
1373 true => DataCollectionChoice::Enabled,
1374 false => DataCollectionChoice::Disabled,
1375 }
1376 }
1377}
1378
1379pub struct ProviderDataCollection {
1380 /// When set to None, data collection is not possible in the provider buffer
1381 choice: Option<Entity<DataCollectionChoice>>,
1382 license_detection_watcher: Option<Rc<LicenseDetectionWatcher>>,
1383}
1384
1385impl ProviderDataCollection {
1386 pub fn new(zeta: Entity<Zeta>, buffer: Option<Entity<Buffer>>, cx: &mut App) -> Self {
1387 let choice_and_watcher = buffer.and_then(|buffer| {
1388 let file = buffer.read(cx).file()?;
1389
1390 if !file.is_local() || file.is_private() {
1391 return None;
1392 }
1393
1394 let zeta = zeta.read(cx);
1395 let choice = zeta.data_collection_choice.clone();
1396
1397 let license_detection_watcher = zeta
1398 .license_detection_watchers
1399 .get(&file.worktree_id(cx))
1400 .cloned()?;
1401
1402 Some((choice, license_detection_watcher))
1403 });
1404
1405 if let Some((choice, watcher)) = choice_and_watcher {
1406 ProviderDataCollection {
1407 choice: Some(choice),
1408 license_detection_watcher: Some(watcher),
1409 }
1410 } else {
1411 ProviderDataCollection {
1412 choice: None,
1413 license_detection_watcher: None,
1414 }
1415 }
1416 }
1417
1418 pub fn can_collect_data(&self, cx: &App) -> bool {
1419 self.is_data_collection_enabled(cx) && self.is_project_open_source()
1420 }
1421
1422 pub fn is_data_collection_enabled(&self, cx: &App) -> bool {
1423 self.choice
1424 .as_ref()
1425 .is_some_and(|choice| choice.read(cx).is_enabled())
1426 }
1427
1428 fn is_project_open_source(&self) -> bool {
1429 self.license_detection_watcher
1430 .as_ref()
1431 .is_some_and(|watcher| watcher.is_project_open_source())
1432 }
1433
1434 pub fn toggle(&mut self, cx: &mut App) {
1435 if let Some(choice) = self.choice.as_mut() {
1436 let new_choice = choice.update(cx, |choice, _cx| {
1437 let new_choice = choice.toggle();
1438 *choice = new_choice;
1439 new_choice
1440 });
1441
1442 db::write_and_log(cx, move || {
1443 KEY_VALUE_STORE.write_kvp(
1444 ZED_PREDICT_DATA_COLLECTION_CHOICE.into(),
1445 new_choice.is_enabled().to_string(),
1446 )
1447 });
1448 }
1449 }
1450}
1451
1452async fn llm_token_retry(
1453 llm_token: &LlmApiToken,
1454 client: &Arc<Client>,
1455 build_request: impl Fn(String) -> Result<Request<AsyncBody>>,
1456) -> Result<Response<AsyncBody>> {
1457 let mut did_retry = false;
1458 let http_client = client.http_client();
1459 let mut token = llm_token.acquire(client).await?;
1460 loop {
1461 let request = build_request(token.clone())?;
1462 let response = http_client.send(request).await?;
1463
1464 if !did_retry
1465 && !response.status().is_success()
1466 && response
1467 .headers()
1468 .get(EXPIRED_LLM_TOKEN_HEADER_NAME)
1469 .is_some()
1470 {
1471 did_retry = true;
1472 token = llm_token.refresh(client).await?;
1473 continue;
1474 }
1475
1476 return Ok(response);
1477 }
1478}
1479
1480pub struct ZetaInlineCompletionProvider {
1481 zeta: Entity<Zeta>,
1482 pending_completions: ArrayVec<PendingCompletion, 2>,
1483 next_pending_completion_id: usize,
1484 current_completion: Option<CurrentInlineCompletion>,
1485 /// None if this is entirely disabled for this provider
1486 provider_data_collection: ProviderDataCollection,
1487 last_request_timestamp: Instant,
1488}
1489
1490impl ZetaInlineCompletionProvider {
1491 pub const THROTTLE_TIMEOUT: Duration = Duration::from_millis(300);
1492
1493 pub fn new(zeta: Entity<Zeta>, provider_data_collection: ProviderDataCollection) -> Self {
1494 Self {
1495 zeta,
1496 pending_completions: ArrayVec::new(),
1497 next_pending_completion_id: 0,
1498 current_completion: None,
1499 provider_data_collection,
1500 last_request_timestamp: Instant::now(),
1501 }
1502 }
1503}
1504
1505impl inline_completion::EditPredictionProvider for ZetaInlineCompletionProvider {
1506 fn name() -> &'static str {
1507 "zed-predict"
1508 }
1509
1510 fn display_name() -> &'static str {
1511 "Zed's Edit Predictions"
1512 }
1513
1514 fn show_completions_in_menu() -> bool {
1515 true
1516 }
1517
1518 fn show_tab_accept_marker() -> bool {
1519 true
1520 }
1521
1522 fn data_collection_state(&self, cx: &App) -> DataCollectionState {
1523 let is_project_open_source = self.provider_data_collection.is_project_open_source();
1524
1525 if self.provider_data_collection.is_data_collection_enabled(cx) {
1526 DataCollectionState::Enabled {
1527 is_project_open_source,
1528 }
1529 } else {
1530 DataCollectionState::Disabled {
1531 is_project_open_source,
1532 }
1533 }
1534 }
1535
1536 fn toggle_data_collection(&mut self, cx: &mut App) {
1537 self.provider_data_collection.toggle(cx);
1538 }
1539
1540 fn usage(&self, cx: &App) -> Option<EditPredictionUsage> {
1541 self.zeta.read(cx).usage(cx)
1542 }
1543
1544 fn is_enabled(
1545 &self,
1546 _buffer: &Entity<Buffer>,
1547 _cursor_position: language::Anchor,
1548 _cx: &App,
1549 ) -> bool {
1550 true
1551 }
1552
1553 fn needs_terms_acceptance(&self, cx: &App) -> bool {
1554 !self.zeta.read(cx).tos_accepted
1555 }
1556
1557 fn is_refreshing(&self) -> bool {
1558 !self.pending_completions.is_empty()
1559 }
1560
1561 fn refresh(
1562 &mut self,
1563 project: Option<Entity<Project>>,
1564 buffer: Entity<Buffer>,
1565 position: language::Anchor,
1566 _debounce: bool,
1567 cx: &mut Context<Self>,
1568 ) {
1569 if !self.zeta.read(cx).tos_accepted {
1570 return;
1571 }
1572
1573 if self.zeta.read(cx).update_required {
1574 return;
1575 }
1576
1577 if self
1578 .zeta
1579 .read(cx)
1580 .user_store
1581 .read_with(cx, |user_store, _| {
1582 user_store.account_too_young() || user_store.has_overdue_invoices()
1583 })
1584 {
1585 return;
1586 }
1587
1588 if let Some(current_completion) = self.current_completion.as_ref() {
1589 let snapshot = buffer.read(cx).snapshot();
1590 if current_completion
1591 .completion
1592 .interpolate(&snapshot)
1593 .is_some()
1594 {
1595 return;
1596 }
1597 }
1598
1599 let pending_completion_id = self.next_pending_completion_id;
1600 self.next_pending_completion_id += 1;
1601 let can_collect_data = self.provider_data_collection.can_collect_data(cx);
1602 let last_request_timestamp = self.last_request_timestamp;
1603
1604 let task = cx.spawn(async move |this, cx| {
1605 if let Some(timeout) = (last_request_timestamp + Self::THROTTLE_TIMEOUT)
1606 .checked_duration_since(Instant::now())
1607 {
1608 cx.background_executor().timer(timeout).await;
1609 }
1610
1611 let completion_request = this.update(cx, |this, cx| {
1612 this.last_request_timestamp = Instant::now();
1613 this.zeta.update(cx, |zeta, cx| {
1614 zeta.request_completion(
1615 project.as_ref(),
1616 &buffer,
1617 position,
1618 can_collect_data,
1619 cx,
1620 )
1621 })
1622 });
1623
1624 let completion = match completion_request {
1625 Ok(completion_request) => {
1626 let completion_request = completion_request.await;
1627 completion_request.map(|c| {
1628 c.map(|completion| CurrentInlineCompletion {
1629 buffer_id: buffer.entity_id(),
1630 completion,
1631 })
1632 })
1633 }
1634 Err(error) => Err(error),
1635 };
1636 let Some(new_completion) = completion
1637 .context("edit prediction failed")
1638 .log_err()
1639 .flatten()
1640 else {
1641 this.update(cx, |this, cx| {
1642 if this.pending_completions[0].id == pending_completion_id {
1643 this.pending_completions.remove(0);
1644 } else {
1645 this.pending_completions.clear();
1646 }
1647
1648 cx.notify();
1649 })
1650 .ok();
1651 return;
1652 };
1653
1654 this.update(cx, |this, cx| {
1655 if this.pending_completions[0].id == pending_completion_id {
1656 this.pending_completions.remove(0);
1657 } else {
1658 this.pending_completions.clear();
1659 }
1660
1661 if let Some(old_completion) = this.current_completion.as_ref() {
1662 let snapshot = buffer.read(cx).snapshot();
1663 if new_completion.should_replace_completion(&old_completion, &snapshot) {
1664 this.zeta.update(cx, |zeta, cx| {
1665 zeta.completion_shown(&new_completion.completion, cx);
1666 });
1667 this.current_completion = Some(new_completion);
1668 }
1669 } else {
1670 this.zeta.update(cx, |zeta, cx| {
1671 zeta.completion_shown(&new_completion.completion, cx);
1672 });
1673 this.current_completion = Some(new_completion);
1674 }
1675
1676 cx.notify();
1677 })
1678 .ok();
1679 });
1680
1681 // We always maintain at most two pending completions. When we already
1682 // have two, we replace the newest one.
1683 if self.pending_completions.len() <= 1 {
1684 self.pending_completions.push(PendingCompletion {
1685 id: pending_completion_id,
1686 _task: task,
1687 });
1688 } else if self.pending_completions.len() == 2 {
1689 self.pending_completions.pop();
1690 self.pending_completions.push(PendingCompletion {
1691 id: pending_completion_id,
1692 _task: task,
1693 });
1694 }
1695 }
1696
1697 fn cycle(
1698 &mut self,
1699 _buffer: Entity<Buffer>,
1700 _cursor_position: language::Anchor,
1701 _direction: inline_completion::Direction,
1702 _cx: &mut Context<Self>,
1703 ) {
1704 // Right now we don't support cycling.
1705 }
1706
1707 fn accept(&mut self, cx: &mut Context<Self>) {
1708 let completion_id = self
1709 .current_completion
1710 .as_ref()
1711 .map(|completion| completion.completion.id);
1712 if let Some(completion_id) = completion_id {
1713 self.zeta
1714 .update(cx, |zeta, cx| {
1715 zeta.accept_edit_prediction(completion_id, cx)
1716 })
1717 .detach();
1718 }
1719 self.pending_completions.clear();
1720 }
1721
1722 fn discard(&mut self, _cx: &mut Context<Self>) {
1723 self.pending_completions.clear();
1724 self.current_completion.take();
1725 }
1726
1727 fn suggest(
1728 &mut self,
1729 buffer: &Entity<Buffer>,
1730 cursor_position: language::Anchor,
1731 cx: &mut Context<Self>,
1732 ) -> Option<inline_completion::InlineCompletion> {
1733 let CurrentInlineCompletion {
1734 buffer_id,
1735 completion,
1736 ..
1737 } = self.current_completion.as_mut()?;
1738
1739 // Invalidate previous completion if it was generated for a different buffer.
1740 if *buffer_id != buffer.entity_id() {
1741 self.current_completion.take();
1742 return None;
1743 }
1744
1745 let buffer = buffer.read(cx);
1746 let Some(edits) = completion.interpolate(&buffer.snapshot()) else {
1747 self.current_completion.take();
1748 return None;
1749 };
1750
1751 let cursor_row = cursor_position.to_point(buffer).row;
1752 let (closest_edit_ix, (closest_edit_range, _)) =
1753 edits.iter().enumerate().min_by_key(|(_, (range, _))| {
1754 let distance_from_start = cursor_row.abs_diff(range.start.to_point(buffer).row);
1755 let distance_from_end = cursor_row.abs_diff(range.end.to_point(buffer).row);
1756 cmp::min(distance_from_start, distance_from_end)
1757 })?;
1758
1759 let mut edit_start_ix = closest_edit_ix;
1760 for (range, _) in edits[..edit_start_ix].iter().rev() {
1761 let distance_from_closest_edit =
1762 closest_edit_range.start.to_point(buffer).row - range.end.to_point(buffer).row;
1763 if distance_from_closest_edit <= 1 {
1764 edit_start_ix -= 1;
1765 } else {
1766 break;
1767 }
1768 }
1769
1770 let mut edit_end_ix = closest_edit_ix + 1;
1771 for (range, _) in &edits[edit_end_ix..] {
1772 let distance_from_closest_edit =
1773 range.start.to_point(buffer).row - closest_edit_range.end.to_point(buffer).row;
1774 if distance_from_closest_edit <= 1 {
1775 edit_end_ix += 1;
1776 } else {
1777 break;
1778 }
1779 }
1780
1781 Some(inline_completion::InlineCompletion {
1782 id: Some(completion.id.to_string().into()),
1783 edits: edits[edit_start_ix..edit_end_ix].to_vec(),
1784 edit_preview: Some(completion.edit_preview.clone()),
1785 })
1786 }
1787}
1788
1789fn tokens_for_bytes(bytes: usize) -> usize {
1790 /// Typical number of string bytes per token for the purposes of limiting model input. This is
1791 /// intentionally low to err on the side of underestimating limits.
1792 const BYTES_PER_TOKEN_GUESS: usize = 3;
1793 bytes / BYTES_PER_TOKEN_GUESS
1794}
1795
1796#[cfg(test)]
1797mod tests {
1798 use client::test::FakeServer;
1799 use clock::FakeSystemClock;
1800 use gpui::TestAppContext;
1801 use http_client::FakeHttpClient;
1802 use indoc::indoc;
1803 use language::Point;
1804 use rpc::proto;
1805 use settings::SettingsStore;
1806
1807 use super::*;
1808
1809 #[gpui::test]
1810 async fn test_inline_completion_basic_interpolation(cx: &mut TestAppContext) {
1811 let buffer = cx.new(|cx| Buffer::local("Lorem ipsum dolor", cx));
1812 let edits: Arc<[(Range<Anchor>, String)]> = cx.update(|cx| {
1813 to_completion_edits(
1814 [(2..5, "REM".to_string()), (9..11, "".to_string())],
1815 &buffer,
1816 cx,
1817 )
1818 .into()
1819 });
1820
1821 let edit_preview = cx
1822 .read(|cx| buffer.read(cx).preview_edits(edits.clone(), cx))
1823 .await;
1824
1825 let completion = InlineCompletion {
1826 edits,
1827 edit_preview,
1828 path: Path::new("").into(),
1829 snapshot: cx.read(|cx| buffer.read(cx).snapshot()),
1830 id: InlineCompletionId(Uuid::new_v4()),
1831 excerpt_range: 0..0,
1832 cursor_offset: 0,
1833 input_outline: "".into(),
1834 input_events: "".into(),
1835 input_excerpt: "".into(),
1836 output_excerpt: "".into(),
1837 request_sent_at: Instant::now(),
1838 response_received_at: Instant::now(),
1839 };
1840
1841 cx.update(|cx| {
1842 assert_eq!(
1843 from_completion_edits(
1844 &completion.interpolate(&buffer.read(cx).snapshot()).unwrap(),
1845 &buffer,
1846 cx
1847 ),
1848 vec![(2..5, "REM".to_string()), (9..11, "".to_string())]
1849 );
1850
1851 buffer.update(cx, |buffer, cx| buffer.edit([(2..5, "")], None, cx));
1852 assert_eq!(
1853 from_completion_edits(
1854 &completion.interpolate(&buffer.read(cx).snapshot()).unwrap(),
1855 &buffer,
1856 cx
1857 ),
1858 vec![(2..2, "REM".to_string()), (6..8, "".to_string())]
1859 );
1860
1861 buffer.update(cx, |buffer, cx| buffer.undo(cx));
1862 assert_eq!(
1863 from_completion_edits(
1864 &completion.interpolate(&buffer.read(cx).snapshot()).unwrap(),
1865 &buffer,
1866 cx
1867 ),
1868 vec![(2..5, "REM".to_string()), (9..11, "".to_string())]
1869 );
1870
1871 buffer.update(cx, |buffer, cx| buffer.edit([(2..5, "R")], None, cx));
1872 assert_eq!(
1873 from_completion_edits(
1874 &completion.interpolate(&buffer.read(cx).snapshot()).unwrap(),
1875 &buffer,
1876 cx
1877 ),
1878 vec![(3..3, "EM".to_string()), (7..9, "".to_string())]
1879 );
1880
1881 buffer.update(cx, |buffer, cx| buffer.edit([(3..3, "E")], None, cx));
1882 assert_eq!(
1883 from_completion_edits(
1884 &completion.interpolate(&buffer.read(cx).snapshot()).unwrap(),
1885 &buffer,
1886 cx
1887 ),
1888 vec![(4..4, "M".to_string()), (8..10, "".to_string())]
1889 );
1890
1891 buffer.update(cx, |buffer, cx| buffer.edit([(4..4, "M")], None, cx));
1892 assert_eq!(
1893 from_completion_edits(
1894 &completion.interpolate(&buffer.read(cx).snapshot()).unwrap(),
1895 &buffer,
1896 cx
1897 ),
1898 vec![(9..11, "".to_string())]
1899 );
1900
1901 buffer.update(cx, |buffer, cx| buffer.edit([(4..5, "")], None, cx));
1902 assert_eq!(
1903 from_completion_edits(
1904 &completion.interpolate(&buffer.read(cx).snapshot()).unwrap(),
1905 &buffer,
1906 cx
1907 ),
1908 vec![(4..4, "M".to_string()), (8..10, "".to_string())]
1909 );
1910
1911 buffer.update(cx, |buffer, cx| buffer.edit([(8..10, "")], None, cx));
1912 assert_eq!(
1913 from_completion_edits(
1914 &completion.interpolate(&buffer.read(cx).snapshot()).unwrap(),
1915 &buffer,
1916 cx
1917 ),
1918 vec![(4..4, "M".to_string())]
1919 );
1920
1921 buffer.update(cx, |buffer, cx| buffer.edit([(4..6, "")], None, cx));
1922 assert_eq!(completion.interpolate(&buffer.read(cx).snapshot()), None);
1923 })
1924 }
1925
1926 #[gpui::test]
1927 async fn test_clean_up_diff(cx: &mut TestAppContext) {
1928 cx.update(|cx| {
1929 let settings_store = SettingsStore::test(cx);
1930 cx.set_global(settings_store);
1931 client::init_settings(cx);
1932 });
1933
1934 let edits = edits_for_prediction(
1935 indoc! {"
1936 fn main() {
1937 let word_1 = \"lorem\";
1938 let range = word.len()..word.len();
1939 }
1940 "},
1941 indoc! {"
1942 <|editable_region_start|>
1943 fn main() {
1944 let word_1 = \"lorem\";
1945 let range = word_1.len()..word_1.len();
1946 }
1947
1948 <|editable_region_end|>
1949 "},
1950 cx,
1951 )
1952 .await;
1953 assert_eq!(
1954 edits,
1955 [
1956 (Point::new(2, 20)..Point::new(2, 20), "_1".to_string()),
1957 (Point::new(2, 32)..Point::new(2, 32), "_1".to_string()),
1958 ]
1959 );
1960
1961 let edits = edits_for_prediction(
1962 indoc! {"
1963 fn main() {
1964 let story = \"the quick\"
1965 }
1966 "},
1967 indoc! {"
1968 <|editable_region_start|>
1969 fn main() {
1970 let story = \"the quick brown fox jumps over the lazy dog\";
1971 }
1972
1973 <|editable_region_end|>
1974 "},
1975 cx,
1976 )
1977 .await;
1978 assert_eq!(
1979 edits,
1980 [
1981 (
1982 Point::new(1, 26)..Point::new(1, 26),
1983 " brown fox jumps over the lazy dog".to_string()
1984 ),
1985 (Point::new(1, 27)..Point::new(1, 27), ";".to_string()),
1986 ]
1987 );
1988 }
1989
1990 #[gpui::test]
1991 async fn test_inline_completion_end_of_buffer(cx: &mut TestAppContext) {
1992 cx.update(|cx| {
1993 let settings_store = SettingsStore::test(cx);
1994 cx.set_global(settings_store);
1995 client::init_settings(cx);
1996 });
1997
1998 let buffer_content = "lorem\n";
1999 let completion_response = indoc! {"
2000 ```animals.js
2001 <|start_of_file|>
2002 <|editable_region_start|>
2003 lorem
2004 ipsum
2005 <|editable_region_end|>
2006 ```"};
2007
2008 let http_client = FakeHttpClient::create(move |_| async move {
2009 Ok(http_client::Response::builder()
2010 .status(200)
2011 .body(
2012 serde_json::to_string(&PredictEditsResponse {
2013 request_id: Uuid::parse_str("7e86480f-3536-4d2c-9334-8213e3445d45")
2014 .unwrap(),
2015 output_excerpt: completion_response.to_string(),
2016 })
2017 .unwrap()
2018 .into(),
2019 )
2020 .unwrap())
2021 });
2022
2023 let client = cx.update(|cx| Client::new(Arc::new(FakeSystemClock::new()), http_client, cx));
2024 cx.update(|cx| {
2025 RefreshLlmTokenListener::register(client.clone(), cx);
2026 });
2027 let server = FakeServer::for_client(42, &client, cx).await;
2028 let user_store = cx.new(|cx| UserStore::new(client.clone(), cx));
2029 let zeta = cx.new(|cx| Zeta::new(None, client, user_store, cx));
2030
2031 let buffer = cx.new(|cx| Buffer::local(buffer_content, cx));
2032 let cursor = buffer.read_with(cx, |buffer, _| buffer.anchor_before(Point::new(1, 0)));
2033 let completion_task = zeta.update(cx, |zeta, cx| {
2034 zeta.request_completion(None, &buffer, cursor, false, cx)
2035 });
2036
2037 server.receive::<proto::GetUsers>().await.unwrap();
2038 let token_request = server.receive::<proto::GetLlmToken>().await.unwrap();
2039 server.respond(
2040 token_request.receipt(),
2041 proto::GetLlmTokenResponse { token: "".into() },
2042 );
2043
2044 let completion = completion_task.await.unwrap().unwrap();
2045 buffer.update(cx, |buffer, cx| {
2046 buffer.edit(completion.edits.iter().cloned(), None, cx)
2047 });
2048 assert_eq!(
2049 buffer.read_with(cx, |buffer, _| buffer.text()),
2050 "lorem\nipsum"
2051 );
2052 }
2053
2054 async fn edits_for_prediction(
2055 buffer_content: &str,
2056 completion_response: &str,
2057 cx: &mut TestAppContext,
2058 ) -> Vec<(Range<Point>, String)> {
2059 let completion_response = completion_response.to_string();
2060 let http_client = FakeHttpClient::create(move |_| {
2061 let completion = completion_response.clone();
2062 async move {
2063 Ok(http_client::Response::builder()
2064 .status(200)
2065 .body(
2066 serde_json::to_string(&PredictEditsResponse {
2067 request_id: Uuid::new_v4(),
2068 output_excerpt: completion,
2069 })
2070 .unwrap()
2071 .into(),
2072 )
2073 .unwrap())
2074 }
2075 });
2076
2077 let client = cx.update(|cx| Client::new(Arc::new(FakeSystemClock::new()), http_client, cx));
2078 cx.update(|cx| {
2079 RefreshLlmTokenListener::register(client.clone(), cx);
2080 });
2081 let server = FakeServer::for_client(42, &client, cx).await;
2082 let user_store = cx.new(|cx| UserStore::new(client.clone(), cx));
2083 let zeta = cx.new(|cx| Zeta::new(None, client, user_store, cx));
2084
2085 let buffer = cx.new(|cx| Buffer::local(buffer_content, cx));
2086 let snapshot = buffer.read_with(cx, |buffer, _| buffer.snapshot());
2087 let cursor = buffer.read_with(cx, |buffer, _| buffer.anchor_before(Point::new(1, 0)));
2088 let completion_task = zeta.update(cx, |zeta, cx| {
2089 zeta.request_completion(None, &buffer, cursor, false, cx)
2090 });
2091
2092 server.receive::<proto::GetUsers>().await.unwrap();
2093 let token_request = server.receive::<proto::GetLlmToken>().await.unwrap();
2094 server.respond(
2095 token_request.receipt(),
2096 proto::GetLlmTokenResponse { token: "".into() },
2097 );
2098
2099 let completion = completion_task.await.unwrap().unwrap();
2100 completion
2101 .edits
2102 .into_iter()
2103 .map(|(old_range, new_text)| (old_range.to_point(&snapshot), new_text.clone()))
2104 .collect::<Vec<_>>()
2105 }
2106
2107 fn to_completion_edits(
2108 iterator: impl IntoIterator<Item = (Range<usize>, String)>,
2109 buffer: &Entity<Buffer>,
2110 cx: &App,
2111 ) -> Vec<(Range<Anchor>, String)> {
2112 let buffer = buffer.read(cx);
2113 iterator
2114 .into_iter()
2115 .map(|(range, text)| {
2116 (
2117 buffer.anchor_after(range.start)..buffer.anchor_before(range.end),
2118 text,
2119 )
2120 })
2121 .collect()
2122 }
2123
2124 fn from_completion_edits(
2125 editor_edits: &[(Range<Anchor>, String)],
2126 buffer: &Entity<Buffer>,
2127 cx: &App,
2128 ) -> Vec<(Range<usize>, String)> {
2129 let buffer = buffer.read(cx);
2130 editor_edits
2131 .iter()
2132 .map(|(range, text)| {
2133 (
2134 range.start.to_offset(buffer)..range.end.to_offset(buffer),
2135 text.clone(),
2136 )
2137 })
2138 .collect()
2139 }
2140
2141 #[ctor::ctor]
2142 fn init_logger() {
2143 zlog::init_test();
2144 }
2145}