zeta.rs

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