zeta.rs

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