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