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