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