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