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