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