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