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