zeta.rs

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