copilot.rs

   1pub mod copilot_chat;
   2mod copilot_completion_provider;
   3pub mod request;
   4mod sign_in;
   5
   6use crate::sign_in::initiate_sign_in_within_workspace;
   7use ::fs::Fs;
   8use anyhow::{Context as _, Result, anyhow};
   9use collections::{HashMap, HashSet};
  10use command_palette_hooks::CommandPaletteFilter;
  11use futures::{Future, FutureExt, TryFutureExt, channel::oneshot, future::Shared};
  12use gpui::{
  13    App, AppContext as _, AsyncApp, Context, Entity, EntityId, EventEmitter, Global, Task,
  14    WeakEntity, actions,
  15};
  16use http_client::HttpClient;
  17use language::language_settings::CopilotSettings;
  18use language::{
  19    Anchor, Bias, Buffer, BufferSnapshot, Language, PointUtf16, ToPointUtf16,
  20    language_settings::{EditPredictionProvider, all_language_settings, language_settings},
  21    point_from_lsp, point_to_lsp,
  22};
  23use lsp::{LanguageServer, LanguageServerBinary, LanguageServerId, LanguageServerName};
  24use node_runtime::NodeRuntime;
  25use parking_lot::Mutex;
  26use request::StatusNotification;
  27use settings::SettingsStore;
  28use sign_in::{reinstall_and_sign_in_within_workspace, sign_out_within_workspace};
  29use std::collections::hash_map::Entry;
  30use std::{
  31    any::TypeId,
  32    env,
  33    ffi::OsString,
  34    mem,
  35    ops::Range,
  36    path::{Path, PathBuf},
  37    sync::Arc,
  38};
  39use util::{ResultExt, fs::remove_matching};
  40use workspace::Workspace;
  41
  42pub use crate::copilot_completion_provider::CopilotCompletionProvider;
  43pub use crate::sign_in::{CopilotCodeVerification, initiate_sign_in, reinstall_and_sign_in};
  44
  45actions!(
  46    copilot,
  47    [
  48        Suggest,
  49        NextSuggestion,
  50        PreviousSuggestion,
  51        Reinstall,
  52        SignIn,
  53        SignOut
  54    ]
  55);
  56
  57pub fn init(
  58    new_server_id: LanguageServerId,
  59    fs: Arc<dyn Fs>,
  60    http: Arc<dyn HttpClient>,
  61    node_runtime: NodeRuntime,
  62    cx: &mut App,
  63) {
  64    copilot_chat::init(fs.clone(), http.clone(), cx);
  65
  66    let copilot = cx.new({
  67        let node_runtime = node_runtime.clone();
  68        move |cx| Copilot::start(new_server_id, fs, node_runtime, cx)
  69    });
  70    Copilot::set_global(copilot.clone(), cx);
  71    cx.observe(&copilot, |handle, cx| {
  72        let copilot_action_types = [
  73            TypeId::of::<Suggest>(),
  74            TypeId::of::<NextSuggestion>(),
  75            TypeId::of::<PreviousSuggestion>(),
  76            TypeId::of::<Reinstall>(),
  77        ];
  78        let copilot_auth_action_types = [TypeId::of::<SignOut>()];
  79        let copilot_no_auth_action_types = [TypeId::of::<SignIn>()];
  80        let status = handle.read(cx).status();
  81        let filter = CommandPaletteFilter::global_mut(cx);
  82
  83        match status {
  84            Status::Disabled => {
  85                filter.hide_action_types(&copilot_action_types);
  86                filter.hide_action_types(&copilot_auth_action_types);
  87                filter.hide_action_types(&copilot_no_auth_action_types);
  88            }
  89            Status::Authorized => {
  90                filter.hide_action_types(&copilot_no_auth_action_types);
  91                filter.show_action_types(
  92                    copilot_action_types
  93                        .iter()
  94                        .chain(&copilot_auth_action_types),
  95                );
  96            }
  97            _ => {
  98                filter.hide_action_types(&copilot_action_types);
  99                filter.hide_action_types(&copilot_auth_action_types);
 100                filter.show_action_types(copilot_no_auth_action_types.iter());
 101            }
 102        }
 103    })
 104    .detach();
 105
 106    cx.observe_new(|workspace: &mut Workspace, _window, _cx| {
 107        workspace.register_action(|workspace, _: &SignIn, window, cx| {
 108            if let Some(copilot) = Copilot::global(cx) {
 109                let is_reinstall = false;
 110                initiate_sign_in_within_workspace(workspace, copilot, is_reinstall, window, cx);
 111            }
 112        });
 113        workspace.register_action(|workspace, _: &Reinstall, window, cx| {
 114            if let Some(copilot) = Copilot::global(cx) {
 115                reinstall_and_sign_in_within_workspace(workspace, copilot, window, cx);
 116            }
 117        });
 118        workspace.register_action(|workspace, _: &SignOut, _window, cx| {
 119            if let Some(copilot) = Copilot::global(cx) {
 120                sign_out_within_workspace(workspace, copilot, cx);
 121            }
 122        });
 123    })
 124    .detach();
 125}
 126
 127enum CopilotServer {
 128    Disabled,
 129    Starting { task: Shared<Task<()>> },
 130    Error(Arc<str>),
 131    Running(RunningCopilotServer),
 132}
 133
 134impl CopilotServer {
 135    fn as_authenticated(&mut self) -> Result<&mut RunningCopilotServer> {
 136        let server = self.as_running()?;
 137        anyhow::ensure!(
 138            matches!(server.sign_in_status, SignInStatus::Authorized { .. }),
 139            "must sign in before using copilot"
 140        );
 141        Ok(server)
 142    }
 143
 144    fn as_running(&mut self) -> Result<&mut RunningCopilotServer> {
 145        match self {
 146            CopilotServer::Starting { .. } => anyhow::bail!("copilot is still starting"),
 147            CopilotServer::Disabled => anyhow::bail!("copilot is disabled"),
 148            CopilotServer::Error(error) => {
 149                anyhow::bail!("copilot was not started because of an error: {error}")
 150            }
 151            CopilotServer::Running(server) => Ok(server),
 152        }
 153    }
 154}
 155
 156struct RunningCopilotServer {
 157    lsp: Arc<LanguageServer>,
 158    sign_in_status: SignInStatus,
 159    registered_buffers: HashMap<EntityId, RegisteredBuffer>,
 160}
 161
 162#[derive(Clone, Debug)]
 163enum SignInStatus {
 164    Authorized,
 165    Unauthorized,
 166    SigningIn {
 167        prompt: Option<request::PromptUserDeviceFlow>,
 168        task: Shared<Task<Result<(), Arc<anyhow::Error>>>>,
 169    },
 170    SignedOut {
 171        awaiting_signing_in: bool,
 172    },
 173}
 174
 175#[derive(Debug, Clone)]
 176pub enum Status {
 177    Starting {
 178        task: Shared<Task<()>>,
 179    },
 180    Error(Arc<str>),
 181    Disabled,
 182    SignedOut {
 183        awaiting_signing_in: bool,
 184    },
 185    SigningIn {
 186        prompt: Option<request::PromptUserDeviceFlow>,
 187    },
 188    Unauthorized,
 189    Authorized,
 190}
 191
 192impl Status {
 193    pub fn is_authorized(&self) -> bool {
 194        matches!(self, Status::Authorized)
 195    }
 196
 197    pub fn is_disabled(&self) -> bool {
 198        matches!(self, Status::Disabled)
 199    }
 200}
 201
 202struct RegisteredBuffer {
 203    uri: lsp::Url,
 204    language_id: String,
 205    snapshot: BufferSnapshot,
 206    snapshot_version: i32,
 207    _subscriptions: [gpui::Subscription; 2],
 208    pending_buffer_change: Task<Option<()>>,
 209}
 210
 211impl RegisteredBuffer {
 212    fn report_changes(
 213        &mut self,
 214        buffer: &Entity<Buffer>,
 215        cx: &mut Context<Copilot>,
 216    ) -> oneshot::Receiver<(i32, BufferSnapshot)> {
 217        let (done_tx, done_rx) = oneshot::channel();
 218
 219        if buffer.read(cx).version() == self.snapshot.version {
 220            let _ = done_tx.send((self.snapshot_version, self.snapshot.clone()));
 221        } else {
 222            let buffer = buffer.downgrade();
 223            let id = buffer.entity_id();
 224            let prev_pending_change =
 225                mem::replace(&mut self.pending_buffer_change, Task::ready(None));
 226            self.pending_buffer_change = cx.spawn(async move |copilot, cx| {
 227                prev_pending_change.await;
 228
 229                let old_version = copilot
 230                    .update(cx, |copilot, _| {
 231                        let server = copilot.server.as_authenticated().log_err()?;
 232                        let buffer = server.registered_buffers.get_mut(&id)?;
 233                        Some(buffer.snapshot.version.clone())
 234                    })
 235                    .ok()??;
 236                let new_snapshot = buffer.read_with(cx, |buffer, _| buffer.snapshot()).ok()?;
 237
 238                let content_changes = cx
 239                    .background_spawn({
 240                        let new_snapshot = new_snapshot.clone();
 241                        async move {
 242                            new_snapshot
 243                                .edits_since::<(PointUtf16, usize)>(&old_version)
 244                                .map(|edit| {
 245                                    let edit_start = edit.new.start.0;
 246                                    let edit_end = edit_start + (edit.old.end.0 - edit.old.start.0);
 247                                    let new_text = new_snapshot
 248                                        .text_for_range(edit.new.start.1..edit.new.end.1)
 249                                        .collect();
 250                                    lsp::TextDocumentContentChangeEvent {
 251                                        range: Some(lsp::Range::new(
 252                                            point_to_lsp(edit_start),
 253                                            point_to_lsp(edit_end),
 254                                        )),
 255                                        range_length: None,
 256                                        text: new_text,
 257                                    }
 258                                })
 259                                .collect::<Vec<_>>()
 260                        }
 261                    })
 262                    .await;
 263
 264                copilot
 265                    .update(cx, |copilot, _| {
 266                        let server = copilot.server.as_authenticated().log_err()?;
 267                        let buffer = server.registered_buffers.get_mut(&id)?;
 268                        if !content_changes.is_empty() {
 269                            buffer.snapshot_version += 1;
 270                            buffer.snapshot = new_snapshot;
 271                            server
 272                                .lsp
 273                                .notify::<lsp::notification::DidChangeTextDocument>(
 274                                    &lsp::DidChangeTextDocumentParams {
 275                                        text_document: lsp::VersionedTextDocumentIdentifier::new(
 276                                            buffer.uri.clone(),
 277                                            buffer.snapshot_version,
 278                                        ),
 279                                        content_changes,
 280                                    },
 281                                )
 282                                .ok();
 283                        }
 284                        let _ = done_tx.send((buffer.snapshot_version, buffer.snapshot.clone()));
 285                        Some(())
 286                    })
 287                    .ok()?;
 288
 289                Some(())
 290            });
 291        }
 292
 293        done_rx
 294    }
 295}
 296
 297#[derive(Debug)]
 298pub struct Completion {
 299    pub uuid: String,
 300    pub range: Range<Anchor>,
 301    pub text: String,
 302}
 303
 304pub struct Copilot {
 305    fs: Arc<dyn Fs>,
 306    node_runtime: NodeRuntime,
 307    server: CopilotServer,
 308    buffers: HashSet<WeakEntity<Buffer>>,
 309    server_id: LanguageServerId,
 310    _subscription: gpui::Subscription,
 311}
 312
 313pub enum Event {
 314    CopilotLanguageServerStarted,
 315    CopilotAuthSignedIn,
 316    CopilotAuthSignedOut,
 317}
 318
 319impl EventEmitter<Event> for Copilot {}
 320
 321struct GlobalCopilot(Entity<Copilot>);
 322
 323impl Global for GlobalCopilot {}
 324
 325impl Copilot {
 326    pub fn global(cx: &App) -> Option<Entity<Self>> {
 327        cx.try_global::<GlobalCopilot>()
 328            .map(|model| model.0.clone())
 329    }
 330
 331    pub fn set_global(copilot: Entity<Self>, cx: &mut App) {
 332        cx.set_global(GlobalCopilot(copilot));
 333    }
 334
 335    fn start(
 336        new_server_id: LanguageServerId,
 337        fs: Arc<dyn Fs>,
 338        node_runtime: NodeRuntime,
 339        cx: &mut Context<Self>,
 340    ) -> Self {
 341        let mut this = Self {
 342            server_id: new_server_id,
 343            fs,
 344            node_runtime,
 345            server: CopilotServer::Disabled,
 346            buffers: Default::default(),
 347            _subscription: cx.on_app_quit(Self::shutdown_language_server),
 348        };
 349        this.start_copilot(true, false, cx);
 350        cx.observe_global::<SettingsStore>(move |this, cx| this.start_copilot(true, false, cx))
 351            .detach();
 352        this
 353    }
 354
 355    fn shutdown_language_server(
 356        &mut self,
 357        _cx: &mut Context<Self>,
 358    ) -> impl Future<Output = ()> + use<> {
 359        let shutdown = match mem::replace(&mut self.server, CopilotServer::Disabled) {
 360            CopilotServer::Running(server) => Some(Box::pin(async move { server.lsp.shutdown() })),
 361            _ => None,
 362        };
 363
 364        async move {
 365            if let Some(shutdown) = shutdown {
 366                shutdown.await;
 367            }
 368        }
 369    }
 370
 371    fn start_copilot(
 372        &mut self,
 373        check_edit_prediction_provider: bool,
 374        awaiting_sign_in_after_start: bool,
 375        cx: &mut Context<Self>,
 376    ) {
 377        if !matches!(self.server, CopilotServer::Disabled) {
 378            return;
 379        }
 380        let language_settings = all_language_settings(None, cx);
 381        if check_edit_prediction_provider
 382            && language_settings.edit_predictions.provider != EditPredictionProvider::Copilot
 383        {
 384            return;
 385        }
 386        let server_id = self.server_id;
 387        let fs = self.fs.clone();
 388        let node_runtime = self.node_runtime.clone();
 389        let env = self.build_env(&language_settings.edit_predictions.copilot);
 390        let start_task = cx
 391            .spawn(async move |this, cx| {
 392                Self::start_language_server(
 393                    server_id,
 394                    fs,
 395                    node_runtime,
 396                    env,
 397                    this,
 398                    awaiting_sign_in_after_start,
 399                    cx,
 400                )
 401                .await
 402            })
 403            .shared();
 404        self.server = CopilotServer::Starting { task: start_task };
 405        cx.notify();
 406    }
 407
 408    fn build_env(&self, copilot_settings: &CopilotSettings) -> Option<HashMap<String, String>> {
 409        let proxy_url = copilot_settings.proxy.clone()?;
 410        let no_verify = copilot_settings.proxy_no_verify;
 411        let http_or_https_proxy = if proxy_url.starts_with("http:") {
 412            Some("HTTP_PROXY")
 413        } else if proxy_url.starts_with("https:") {
 414            Some("HTTPS_PROXY")
 415        } else {
 416            log::error!(
 417                "Unsupported protocol scheme for language server proxy (must be http or https)"
 418            );
 419            None
 420        };
 421
 422        let mut env = HashMap::default();
 423
 424        if let Some(proxy_type) = http_or_https_proxy {
 425            env.insert(proxy_type.to_string(), proxy_url);
 426            if let Some(true) = no_verify {
 427                env.insert("NODE_TLS_REJECT_UNAUTHORIZED".to_string(), "0".to_string());
 428            };
 429        }
 430
 431        if let Ok(oauth_token) = env::var(copilot_chat::COPILOT_OAUTH_ENV_VAR) {
 432            env.insert(copilot_chat::COPILOT_OAUTH_ENV_VAR.to_string(), oauth_token);
 433        }
 434
 435        if env.is_empty() { None } else { Some(env) }
 436    }
 437
 438    #[cfg(any(test, feature = "test-support"))]
 439    pub fn fake(cx: &mut gpui::TestAppContext) -> (Entity<Self>, lsp::FakeLanguageServer) {
 440        use fs::FakeFs;
 441        use lsp::FakeLanguageServer;
 442        use node_runtime::NodeRuntime;
 443
 444        let (server, fake_server) = FakeLanguageServer::new(
 445            LanguageServerId(0),
 446            LanguageServerBinary {
 447                path: "path/to/copilot".into(),
 448                arguments: vec![],
 449                env: None,
 450            },
 451            "copilot".into(),
 452            Default::default(),
 453            &mut cx.to_async(),
 454        );
 455        let node_runtime = NodeRuntime::unavailable();
 456        let this = cx.new(|cx| Self {
 457            server_id: LanguageServerId(0),
 458            fs: FakeFs::new(cx.background_executor().clone()),
 459            node_runtime,
 460            server: CopilotServer::Running(RunningCopilotServer {
 461                lsp: Arc::new(server),
 462                sign_in_status: SignInStatus::Authorized,
 463                registered_buffers: Default::default(),
 464            }),
 465            _subscription: cx.on_app_quit(Self::shutdown_language_server),
 466            buffers: Default::default(),
 467        });
 468        (this, fake_server)
 469    }
 470
 471    async fn start_language_server(
 472        new_server_id: LanguageServerId,
 473        fs: Arc<dyn Fs>,
 474        node_runtime: NodeRuntime,
 475        env: Option<HashMap<String, String>>,
 476        this: WeakEntity<Self>,
 477        awaiting_sign_in_after_start: bool,
 478        cx: &mut AsyncApp,
 479    ) {
 480        let start_language_server = async {
 481            let server_path = get_copilot_lsp(fs, node_runtime.clone()).await?;
 482            let node_path = node_runtime.binary_path().await?;
 483            let arguments: Vec<OsString> = vec![server_path.into(), "--stdio".into()];
 484            let binary = LanguageServerBinary {
 485                path: node_path,
 486                arguments,
 487                env,
 488            };
 489
 490            let root_path = if cfg!(target_os = "windows") {
 491                Path::new("C:/")
 492            } else {
 493                Path::new("/")
 494            };
 495
 496            let server_name = LanguageServerName("copilot".into());
 497            let server = LanguageServer::new(
 498                Arc::new(Mutex::new(None)),
 499                new_server_id,
 500                server_name,
 501                binary,
 502                root_path,
 503                None,
 504                Default::default(),
 505                cx,
 506            )?;
 507
 508            server
 509                .on_notification::<StatusNotification, _>(|_, _| { /* Silence the notification */ })
 510                .detach();
 511
 512            let configuration = lsp::DidChangeConfigurationParams {
 513                settings: Default::default(),
 514            };
 515
 516            let editor_info = request::SetEditorInfoParams {
 517                editor_info: request::EditorInfo {
 518                    name: "zed".into(),
 519                    version: env!("CARGO_PKG_VERSION").into(),
 520                },
 521                editor_plugin_info: request::EditorPluginInfo {
 522                    name: "zed-copilot".into(),
 523                    version: "0.0.1".into(),
 524                },
 525            };
 526            let editor_info_json = serde_json::to_value(&editor_info)?;
 527
 528            let server = cx
 529                .update(|cx| {
 530                    let mut params = server.default_initialize_params(false, cx);
 531                    params.initialization_options = Some(editor_info_json);
 532                    server.initialize(params, configuration.into(), cx)
 533                })?
 534                .await?;
 535
 536            let status = server
 537                .request::<request::CheckStatus>(request::CheckStatusParams {
 538                    local_checks_only: false,
 539                })
 540                .await
 541                .into_response()
 542                .context("copilot: check status")?;
 543
 544            server
 545                .request::<request::SetEditorInfo>(editor_info)
 546                .await
 547                .into_response()
 548                .context("copilot: set editor info")?;
 549
 550            anyhow::Ok((server, status))
 551        };
 552
 553        let server = start_language_server.await;
 554        this.update(cx, |this, cx| {
 555            cx.notify();
 556            match server {
 557                Ok((server, status)) => {
 558                    this.server = CopilotServer::Running(RunningCopilotServer {
 559                        lsp: server,
 560                        sign_in_status: SignInStatus::SignedOut {
 561                            awaiting_signing_in: awaiting_sign_in_after_start,
 562                        },
 563                        registered_buffers: Default::default(),
 564                    });
 565                    cx.emit(Event::CopilotLanguageServerStarted);
 566                    this.update_sign_in_status(status, cx);
 567                }
 568                Err(error) => {
 569                    this.server = CopilotServer::Error(error.to_string().into());
 570                    cx.notify()
 571                }
 572            }
 573        })
 574        .ok();
 575    }
 576
 577    pub(crate) fn sign_in(&mut self, cx: &mut Context<Self>) -> Task<Result<()>> {
 578        if let CopilotServer::Running(server) = &mut self.server {
 579            let task = match &server.sign_in_status {
 580                SignInStatus::Authorized { .. } => Task::ready(Ok(())).shared(),
 581                SignInStatus::SigningIn { task, .. } => {
 582                    cx.notify();
 583                    task.clone()
 584                }
 585                SignInStatus::SignedOut { .. } | SignInStatus::Unauthorized { .. } => {
 586                    let lsp = server.lsp.clone();
 587                    let task = cx
 588                        .spawn(async move |this, cx| {
 589                            let sign_in = async {
 590                                let sign_in = lsp
 591                                    .request::<request::SignInInitiate>(
 592                                        request::SignInInitiateParams {},
 593                                    )
 594                                    .await
 595                                    .into_response()
 596                                    .context("copilot sign-in")?;
 597                                match sign_in {
 598                                    request::SignInInitiateResult::AlreadySignedIn { user } => {
 599                                        Ok(request::SignInStatus::Ok { user: Some(user) })
 600                                    }
 601                                    request::SignInInitiateResult::PromptUserDeviceFlow(flow) => {
 602                                        this.update(cx, |this, cx| {
 603                                            if let CopilotServer::Running(RunningCopilotServer {
 604                                                sign_in_status: status,
 605                                                ..
 606                                            }) = &mut this.server
 607                                            {
 608                                                if let SignInStatus::SigningIn {
 609                                                    prompt: prompt_flow,
 610                                                    ..
 611                                                } = status
 612                                                {
 613                                                    *prompt_flow = Some(flow.clone());
 614                                                    cx.notify();
 615                                                }
 616                                            }
 617                                        })?;
 618                                        let response = lsp
 619                                            .request::<request::SignInConfirm>(
 620                                                request::SignInConfirmParams {
 621                                                    user_code: flow.user_code,
 622                                                },
 623                                            )
 624                                            .await
 625                                            .into_response()
 626                                            .context("copilot: sign in confirm")?;
 627                                        Ok(response)
 628                                    }
 629                                }
 630                            };
 631
 632                            let sign_in = sign_in.await;
 633                            this.update(cx, |this, cx| match sign_in {
 634                                Ok(status) => {
 635                                    this.update_sign_in_status(status, cx);
 636                                    Ok(())
 637                                }
 638                                Err(error) => {
 639                                    this.update_sign_in_status(
 640                                        request::SignInStatus::NotSignedIn,
 641                                        cx,
 642                                    );
 643                                    Err(Arc::new(error))
 644                                }
 645                            })?
 646                        })
 647                        .shared();
 648                    server.sign_in_status = SignInStatus::SigningIn {
 649                        prompt: None,
 650                        task: task.clone(),
 651                    };
 652                    cx.notify();
 653                    task
 654                }
 655            };
 656
 657            cx.background_spawn(task.map_err(|err| anyhow!("{err:?}")))
 658        } else {
 659            // If we're downloading, wait until download is finished
 660            // If we're in a stuck state, display to the user
 661            Task::ready(Err(anyhow!("copilot hasn't started yet")))
 662        }
 663    }
 664
 665    pub(crate) fn sign_out(&mut self, cx: &mut Context<Self>) -> Task<Result<()>> {
 666        self.update_sign_in_status(request::SignInStatus::NotSignedIn, cx);
 667        match &self.server {
 668            CopilotServer::Running(RunningCopilotServer { lsp: server, .. }) => {
 669                let server = server.clone();
 670                cx.background_spawn(async move {
 671                    server
 672                        .request::<request::SignOut>(request::SignOutParams {})
 673                        .await
 674                        .into_response()
 675                        .context("copilot: sign in confirm")?;
 676                    anyhow::Ok(())
 677                })
 678            }
 679            CopilotServer::Disabled => cx.background_spawn(async {
 680                clear_copilot_config_dir().await;
 681                anyhow::Ok(())
 682            }),
 683            _ => Task::ready(Err(anyhow!("copilot hasn't started yet"))),
 684        }
 685    }
 686
 687    pub(crate) fn reinstall(&mut self, cx: &mut Context<Self>) -> Shared<Task<()>> {
 688        let language_settings = all_language_settings(None, cx);
 689        let env = self.build_env(&language_settings.edit_predictions.copilot);
 690        let start_task = cx
 691            .spawn({
 692                let fs = self.fs.clone();
 693                let node_runtime = self.node_runtime.clone();
 694                let server_id = self.server_id;
 695                async move |this, cx| {
 696                    clear_copilot_dir().await;
 697                    Self::start_language_server(server_id, fs, node_runtime, env, this, false, cx)
 698                        .await
 699                }
 700            })
 701            .shared();
 702
 703        self.server = CopilotServer::Starting {
 704            task: start_task.clone(),
 705        };
 706
 707        cx.notify();
 708
 709        start_task
 710    }
 711
 712    pub fn language_server(&self) -> Option<&Arc<LanguageServer>> {
 713        if let CopilotServer::Running(server) = &self.server {
 714            Some(&server.lsp)
 715        } else {
 716            None
 717        }
 718    }
 719
 720    pub fn register_buffer(&mut self, buffer: &Entity<Buffer>, cx: &mut Context<Self>) {
 721        let weak_buffer = buffer.downgrade();
 722        self.buffers.insert(weak_buffer.clone());
 723
 724        if let CopilotServer::Running(RunningCopilotServer {
 725            lsp: server,
 726            sign_in_status: status,
 727            registered_buffers,
 728            ..
 729        }) = &mut self.server
 730        {
 731            if !matches!(status, SignInStatus::Authorized { .. }) {
 732                return;
 733            }
 734
 735            let entry = registered_buffers.entry(buffer.entity_id());
 736            if let Entry::Vacant(e) = entry {
 737                let Ok(uri) = uri_for_buffer(buffer, cx) else {
 738                    return;
 739                };
 740                let language_id = id_for_language(buffer.read(cx).language());
 741                let snapshot = buffer.read(cx).snapshot();
 742                server
 743                    .notify::<lsp::notification::DidOpenTextDocument>(
 744                        &lsp::DidOpenTextDocumentParams {
 745                            text_document: lsp::TextDocumentItem {
 746                                uri: uri.clone(),
 747                                language_id: language_id.clone(),
 748                                version: 0,
 749                                text: snapshot.text(),
 750                            },
 751                        },
 752                    )
 753                    .ok();
 754
 755                e.insert(RegisteredBuffer {
 756                    uri,
 757                    language_id,
 758                    snapshot,
 759                    snapshot_version: 0,
 760                    pending_buffer_change: Task::ready(Some(())),
 761                    _subscriptions: [
 762                        cx.subscribe(buffer, |this, buffer, event, cx| {
 763                            this.handle_buffer_event(buffer, event, cx).log_err();
 764                        }),
 765                        cx.observe_release(buffer, move |this, _buffer, _cx| {
 766                            this.buffers.remove(&weak_buffer);
 767                            this.unregister_buffer(&weak_buffer);
 768                        }),
 769                    ],
 770                });
 771            }
 772        }
 773    }
 774
 775    fn handle_buffer_event(
 776        &mut self,
 777        buffer: Entity<Buffer>,
 778        event: &language::BufferEvent,
 779        cx: &mut Context<Self>,
 780    ) -> Result<()> {
 781        if let Ok(server) = self.server.as_running() {
 782            if let Some(registered_buffer) = server.registered_buffers.get_mut(&buffer.entity_id())
 783            {
 784                match event {
 785                    language::BufferEvent::Edited => {
 786                        drop(registered_buffer.report_changes(&buffer, cx));
 787                    }
 788                    language::BufferEvent::Saved => {
 789                        server
 790                            .lsp
 791                            .notify::<lsp::notification::DidSaveTextDocument>(
 792                                &lsp::DidSaveTextDocumentParams {
 793                                    text_document: lsp::TextDocumentIdentifier::new(
 794                                        registered_buffer.uri.clone(),
 795                                    ),
 796                                    text: None,
 797                                },
 798                            )?;
 799                    }
 800                    language::BufferEvent::FileHandleChanged
 801                    | language::BufferEvent::LanguageChanged => {
 802                        let new_language_id = id_for_language(buffer.read(cx).language());
 803                        let Ok(new_uri) = uri_for_buffer(&buffer, cx) else {
 804                            return Ok(());
 805                        };
 806                        if new_uri != registered_buffer.uri
 807                            || new_language_id != registered_buffer.language_id
 808                        {
 809                            let old_uri = mem::replace(&mut registered_buffer.uri, new_uri);
 810                            registered_buffer.language_id = new_language_id;
 811                            server
 812                                .lsp
 813                                .notify::<lsp::notification::DidCloseTextDocument>(
 814                                    &lsp::DidCloseTextDocumentParams {
 815                                        text_document: lsp::TextDocumentIdentifier::new(old_uri),
 816                                    },
 817                                )?;
 818                            server
 819                                .lsp
 820                                .notify::<lsp::notification::DidOpenTextDocument>(
 821                                    &lsp::DidOpenTextDocumentParams {
 822                                        text_document: lsp::TextDocumentItem::new(
 823                                            registered_buffer.uri.clone(),
 824                                            registered_buffer.language_id.clone(),
 825                                            registered_buffer.snapshot_version,
 826                                            registered_buffer.snapshot.text(),
 827                                        ),
 828                                    },
 829                                )?;
 830                        }
 831                    }
 832                    _ => {}
 833                }
 834            }
 835        }
 836
 837        Ok(())
 838    }
 839
 840    fn unregister_buffer(&mut self, buffer: &WeakEntity<Buffer>) {
 841        if let Ok(server) = self.server.as_running() {
 842            if let Some(buffer) = server.registered_buffers.remove(&buffer.entity_id()) {
 843                server
 844                    .lsp
 845                    .notify::<lsp::notification::DidCloseTextDocument>(
 846                        &lsp::DidCloseTextDocumentParams {
 847                            text_document: lsp::TextDocumentIdentifier::new(buffer.uri),
 848                        },
 849                    )
 850                    .ok();
 851            }
 852        }
 853    }
 854
 855    pub fn completions<T>(
 856        &mut self,
 857        buffer: &Entity<Buffer>,
 858        position: T,
 859        cx: &mut Context<Self>,
 860    ) -> Task<Result<Vec<Completion>>>
 861    where
 862        T: ToPointUtf16,
 863    {
 864        self.request_completions::<request::GetCompletions, _>(buffer, position, cx)
 865    }
 866
 867    pub fn completions_cycling<T>(
 868        &mut self,
 869        buffer: &Entity<Buffer>,
 870        position: T,
 871        cx: &mut Context<Self>,
 872    ) -> Task<Result<Vec<Completion>>>
 873    where
 874        T: ToPointUtf16,
 875    {
 876        self.request_completions::<request::GetCompletionsCycling, _>(buffer, position, cx)
 877    }
 878
 879    pub fn accept_completion(
 880        &mut self,
 881        completion: &Completion,
 882        cx: &mut Context<Self>,
 883    ) -> Task<Result<()>> {
 884        let server = match self.server.as_authenticated() {
 885            Ok(server) => server,
 886            Err(error) => return Task::ready(Err(error)),
 887        };
 888        let request =
 889            server
 890                .lsp
 891                .request::<request::NotifyAccepted>(request::NotifyAcceptedParams {
 892                    uuid: completion.uuid.clone(),
 893                });
 894        cx.background_spawn(async move {
 895            request
 896                .await
 897                .into_response()
 898                .context("copilot: notify accepted")?;
 899            Ok(())
 900        })
 901    }
 902
 903    pub fn discard_completions(
 904        &mut self,
 905        completions: &[Completion],
 906        cx: &mut Context<Self>,
 907    ) -> Task<Result<()>> {
 908        let server = match self.server.as_authenticated() {
 909            Ok(server) => server,
 910            Err(_) => return Task::ready(Ok(())),
 911        };
 912        let request =
 913            server
 914                .lsp
 915                .request::<request::NotifyRejected>(request::NotifyRejectedParams {
 916                    uuids: completions
 917                        .iter()
 918                        .map(|completion| completion.uuid.clone())
 919                        .collect(),
 920                });
 921        cx.background_spawn(async move {
 922            request
 923                .await
 924                .into_response()
 925                .context("copilot: notify rejected")?;
 926            Ok(())
 927        })
 928    }
 929
 930    fn request_completions<R, T>(
 931        &mut self,
 932        buffer: &Entity<Buffer>,
 933        position: T,
 934        cx: &mut Context<Self>,
 935    ) -> Task<Result<Vec<Completion>>>
 936    where
 937        R: 'static
 938            + lsp::request::Request<
 939                Params = request::GetCompletionsParams,
 940                Result = request::GetCompletionsResult,
 941            >,
 942        T: ToPointUtf16,
 943    {
 944        self.register_buffer(buffer, cx);
 945
 946        let server = match self.server.as_authenticated() {
 947            Ok(server) => server,
 948            Err(error) => return Task::ready(Err(error)),
 949        };
 950        let lsp = server.lsp.clone();
 951        let registered_buffer = server
 952            .registered_buffers
 953            .get_mut(&buffer.entity_id())
 954            .unwrap();
 955        let snapshot = registered_buffer.report_changes(buffer, cx);
 956        let buffer = buffer.read(cx);
 957        let uri = registered_buffer.uri.clone();
 958        let position = position.to_point_utf16(buffer);
 959        let settings = language_settings(
 960            buffer.language_at(position).map(|l| l.name()),
 961            buffer.file(),
 962            cx,
 963        );
 964        let tab_size = settings.tab_size;
 965        let hard_tabs = settings.hard_tabs;
 966        let relative_path = buffer
 967            .file()
 968            .map(|file| file.path().to_path_buf())
 969            .unwrap_or_default();
 970
 971        cx.background_spawn(async move {
 972            let (version, snapshot) = snapshot.await?;
 973            let result = lsp
 974                .request::<R>(request::GetCompletionsParams {
 975                    doc: request::GetCompletionsDocument {
 976                        uri,
 977                        tab_size: tab_size.into(),
 978                        indent_size: 1,
 979                        insert_spaces: !hard_tabs,
 980                        relative_path: relative_path.to_string_lossy().into(),
 981                        position: point_to_lsp(position),
 982                        version: version.try_into().unwrap(),
 983                    },
 984                })
 985                .await
 986                .into_response()
 987                .context("copilot: get completions")?;
 988            let completions = result
 989                .completions
 990                .into_iter()
 991                .map(|completion| {
 992                    let start = snapshot
 993                        .clip_point_utf16(point_from_lsp(completion.range.start), Bias::Left);
 994                    let end =
 995                        snapshot.clip_point_utf16(point_from_lsp(completion.range.end), Bias::Left);
 996                    Completion {
 997                        uuid: completion.uuid,
 998                        range: snapshot.anchor_before(start)..snapshot.anchor_after(end),
 999                        text: completion.text,
1000                    }
1001                })
1002                .collect();
1003            anyhow::Ok(completions)
1004        })
1005    }
1006
1007    pub fn status(&self) -> Status {
1008        match &self.server {
1009            CopilotServer::Starting { task } => Status::Starting { task: task.clone() },
1010            CopilotServer::Disabled => Status::Disabled,
1011            CopilotServer::Error(error) => Status::Error(error.clone()),
1012            CopilotServer::Running(RunningCopilotServer { sign_in_status, .. }) => {
1013                match sign_in_status {
1014                    SignInStatus::Authorized { .. } => Status::Authorized,
1015                    SignInStatus::Unauthorized { .. } => Status::Unauthorized,
1016                    SignInStatus::SigningIn { prompt, .. } => Status::SigningIn {
1017                        prompt: prompt.clone(),
1018                    },
1019                    SignInStatus::SignedOut {
1020                        awaiting_signing_in,
1021                    } => Status::SignedOut {
1022                        awaiting_signing_in: *awaiting_signing_in,
1023                    },
1024                }
1025            }
1026        }
1027    }
1028
1029    fn update_sign_in_status(&mut self, lsp_status: request::SignInStatus, cx: &mut Context<Self>) {
1030        self.buffers.retain(|buffer| buffer.is_upgradable());
1031
1032        if let Ok(server) = self.server.as_running() {
1033            match lsp_status {
1034                request::SignInStatus::Ok { user: Some(_) }
1035                | request::SignInStatus::MaybeOk { .. }
1036                | request::SignInStatus::AlreadySignedIn { .. } => {
1037                    server.sign_in_status = SignInStatus::Authorized;
1038                    cx.emit(Event::CopilotAuthSignedIn);
1039                    for buffer in self.buffers.iter().cloned().collect::<Vec<_>>() {
1040                        if let Some(buffer) = buffer.upgrade() {
1041                            self.register_buffer(&buffer, cx);
1042                        }
1043                    }
1044                }
1045                request::SignInStatus::NotAuthorized { .. } => {
1046                    server.sign_in_status = SignInStatus::Unauthorized;
1047                    for buffer in self.buffers.iter().cloned().collect::<Vec<_>>() {
1048                        self.unregister_buffer(&buffer);
1049                    }
1050                }
1051                request::SignInStatus::Ok { user: None } | request::SignInStatus::NotSignedIn => {
1052                    if !matches!(server.sign_in_status, SignInStatus::SignedOut { .. }) {
1053                        server.sign_in_status = SignInStatus::SignedOut {
1054                            awaiting_signing_in: false,
1055                        };
1056                    }
1057                    cx.emit(Event::CopilotAuthSignedOut);
1058                    for buffer in self.buffers.iter().cloned().collect::<Vec<_>>() {
1059                        self.unregister_buffer(&buffer);
1060                    }
1061                }
1062            }
1063
1064            cx.notify();
1065        }
1066    }
1067}
1068
1069fn id_for_language(language: Option<&Arc<Language>>) -> String {
1070    language
1071        .map(|language| language.lsp_id())
1072        .unwrap_or_else(|| "plaintext".to_string())
1073}
1074
1075fn uri_for_buffer(buffer: &Entity<Buffer>, cx: &App) -> Result<lsp::Url, ()> {
1076    if let Some(file) = buffer.read(cx).file().and_then(|file| file.as_local()) {
1077        lsp::Url::from_file_path(file.abs_path(cx))
1078    } else {
1079        format!("buffer://{}", buffer.entity_id())
1080            .parse()
1081            .map_err(|_| ())
1082    }
1083}
1084
1085async fn clear_copilot_dir() {
1086    remove_matching(paths::copilot_dir(), |_| true).await
1087}
1088
1089async fn clear_copilot_config_dir() {
1090    remove_matching(copilot_chat::copilot_chat_config_dir(), |_| true).await
1091}
1092
1093async fn get_copilot_lsp(fs: Arc<dyn Fs>, node_runtime: NodeRuntime) -> anyhow::Result<PathBuf> {
1094    const PACKAGE_NAME: &str = "@github/copilot-language-server";
1095    const SERVER_PATH: &str =
1096        "node_modules/@github/copilot-language-server/dist/language-server.js";
1097
1098    let latest_version = node_runtime
1099        .npm_package_latest_version(PACKAGE_NAME)
1100        .await?;
1101    let server_path = paths::copilot_dir().join(SERVER_PATH);
1102
1103    fs.create_dir(paths::copilot_dir()).await?;
1104
1105    let should_install = node_runtime
1106        .should_install_npm_package(
1107            PACKAGE_NAME,
1108            &server_path,
1109            paths::copilot_dir(),
1110            &latest_version,
1111        )
1112        .await;
1113    if should_install {
1114        node_runtime
1115            .npm_install_packages(paths::copilot_dir(), &[(PACKAGE_NAME, &latest_version)])
1116            .await?;
1117    }
1118
1119    Ok(server_path)
1120}
1121
1122#[cfg(test)]
1123mod tests {
1124    use super::*;
1125    use gpui::TestAppContext;
1126    use util::path;
1127
1128    #[gpui::test(iterations = 10)]
1129    async fn test_buffer_management(cx: &mut TestAppContext) {
1130        let (copilot, mut lsp) = Copilot::fake(cx);
1131
1132        let buffer_1 = cx.new(|cx| Buffer::local("Hello", cx));
1133        let buffer_1_uri: lsp::Url = format!("buffer://{}", buffer_1.entity_id().as_u64())
1134            .parse()
1135            .unwrap();
1136        copilot.update(cx, |copilot, cx| copilot.register_buffer(&buffer_1, cx));
1137        assert_eq!(
1138            lsp.receive_notification::<lsp::notification::DidOpenTextDocument>()
1139                .await,
1140            lsp::DidOpenTextDocumentParams {
1141                text_document: lsp::TextDocumentItem::new(
1142                    buffer_1_uri.clone(),
1143                    "plaintext".into(),
1144                    0,
1145                    "Hello".into()
1146                ),
1147            }
1148        );
1149
1150        let buffer_2 = cx.new(|cx| Buffer::local("Goodbye", cx));
1151        let buffer_2_uri: lsp::Url = format!("buffer://{}", buffer_2.entity_id().as_u64())
1152            .parse()
1153            .unwrap();
1154        copilot.update(cx, |copilot, cx| copilot.register_buffer(&buffer_2, cx));
1155        assert_eq!(
1156            lsp.receive_notification::<lsp::notification::DidOpenTextDocument>()
1157                .await,
1158            lsp::DidOpenTextDocumentParams {
1159                text_document: lsp::TextDocumentItem::new(
1160                    buffer_2_uri.clone(),
1161                    "plaintext".into(),
1162                    0,
1163                    "Goodbye".into()
1164                ),
1165            }
1166        );
1167
1168        buffer_1.update(cx, |buffer, cx| buffer.edit([(5..5, " world")], None, cx));
1169        assert_eq!(
1170            lsp.receive_notification::<lsp::notification::DidChangeTextDocument>()
1171                .await,
1172            lsp::DidChangeTextDocumentParams {
1173                text_document: lsp::VersionedTextDocumentIdentifier::new(buffer_1_uri.clone(), 1),
1174                content_changes: vec![lsp::TextDocumentContentChangeEvent {
1175                    range: Some(lsp::Range::new(
1176                        lsp::Position::new(0, 5),
1177                        lsp::Position::new(0, 5)
1178                    )),
1179                    range_length: None,
1180                    text: " world".into(),
1181                }],
1182            }
1183        );
1184
1185        // Ensure updates to the file are reflected in the LSP.
1186        buffer_1.update(cx, |buffer, cx| {
1187            buffer.file_updated(
1188                Arc::new(File {
1189                    abs_path: path!("/root/child/buffer-1").into(),
1190                    path: Path::new("child/buffer-1").into(),
1191                }),
1192                cx,
1193            )
1194        });
1195        assert_eq!(
1196            lsp.receive_notification::<lsp::notification::DidCloseTextDocument>()
1197                .await,
1198            lsp::DidCloseTextDocumentParams {
1199                text_document: lsp::TextDocumentIdentifier::new(buffer_1_uri),
1200            }
1201        );
1202        let buffer_1_uri = lsp::Url::from_file_path(path!("/root/child/buffer-1")).unwrap();
1203        assert_eq!(
1204            lsp.receive_notification::<lsp::notification::DidOpenTextDocument>()
1205                .await,
1206            lsp::DidOpenTextDocumentParams {
1207                text_document: lsp::TextDocumentItem::new(
1208                    buffer_1_uri.clone(),
1209                    "plaintext".into(),
1210                    1,
1211                    "Hello world".into()
1212                ),
1213            }
1214        );
1215
1216        // Ensure all previously-registered buffers are closed when signing out.
1217        lsp.set_request_handler::<request::SignOut, _, _>(|_, _| async {
1218            Ok(request::SignOutResult {})
1219        });
1220        copilot
1221            .update(cx, |copilot, cx| copilot.sign_out(cx))
1222            .await
1223            .unwrap();
1224        assert_eq!(
1225            lsp.receive_notification::<lsp::notification::DidCloseTextDocument>()
1226                .await,
1227            lsp::DidCloseTextDocumentParams {
1228                text_document: lsp::TextDocumentIdentifier::new(buffer_1_uri.clone()),
1229            }
1230        );
1231        assert_eq!(
1232            lsp.receive_notification::<lsp::notification::DidCloseTextDocument>()
1233                .await,
1234            lsp::DidCloseTextDocumentParams {
1235                text_document: lsp::TextDocumentIdentifier::new(buffer_2_uri.clone()),
1236            }
1237        );
1238
1239        // Ensure all previously-registered buffers are re-opened when signing in.
1240        lsp.set_request_handler::<request::SignInInitiate, _, _>(|_, _| async {
1241            Ok(request::SignInInitiateResult::AlreadySignedIn {
1242                user: "user-1".into(),
1243            })
1244        });
1245        copilot
1246            .update(cx, |copilot, cx| copilot.sign_in(cx))
1247            .await
1248            .unwrap();
1249
1250        assert_eq!(
1251            lsp.receive_notification::<lsp::notification::DidOpenTextDocument>()
1252                .await,
1253            lsp::DidOpenTextDocumentParams {
1254                text_document: lsp::TextDocumentItem::new(
1255                    buffer_1_uri.clone(),
1256                    "plaintext".into(),
1257                    0,
1258                    "Hello world".into()
1259                ),
1260            }
1261        );
1262        assert_eq!(
1263            lsp.receive_notification::<lsp::notification::DidOpenTextDocument>()
1264                .await,
1265            lsp::DidOpenTextDocumentParams {
1266                text_document: lsp::TextDocumentItem::new(
1267                    buffer_2_uri.clone(),
1268                    "plaintext".into(),
1269                    0,
1270                    "Goodbye".into()
1271                ),
1272            }
1273        );
1274        // Dropping a buffer causes it to be closed on the LSP side as well.
1275        cx.update(|_| drop(buffer_2));
1276        assert_eq!(
1277            lsp.receive_notification::<lsp::notification::DidCloseTextDocument>()
1278                .await,
1279            lsp::DidCloseTextDocumentParams {
1280                text_document: lsp::TextDocumentIdentifier::new(buffer_2_uri),
1281            }
1282        );
1283    }
1284
1285    struct File {
1286        abs_path: PathBuf,
1287        path: Arc<Path>,
1288    }
1289
1290    impl language::File for File {
1291        fn as_local(&self) -> Option<&dyn language::LocalFile> {
1292            Some(self)
1293        }
1294
1295        fn disk_state(&self) -> language::DiskState {
1296            language::DiskState::Present {
1297                mtime: ::fs::MTime::from_seconds_and_nanos(100, 42),
1298            }
1299        }
1300
1301        fn path(&self) -> &Arc<Path> {
1302            &self.path
1303        }
1304
1305        fn full_path(&self, _: &App) -> PathBuf {
1306            unimplemented!()
1307        }
1308
1309        fn file_name<'a>(&'a self, _: &'a App) -> &'a std::ffi::OsStr {
1310            unimplemented!()
1311        }
1312
1313        fn to_proto(&self, _: &App) -> rpc::proto::File {
1314            unimplemented!()
1315        }
1316
1317        fn worktree_id(&self, _: &App) -> settings::WorktreeId {
1318            settings::WorktreeId::from_usize(0)
1319        }
1320
1321        fn is_private(&self) -> bool {
1322            false
1323        }
1324    }
1325
1326    impl language::LocalFile for File {
1327        fn abs_path(&self, _: &App) -> PathBuf {
1328            self.abs_path.clone()
1329        }
1330
1331        fn load(&self, _: &App) -> Task<Result<String>> {
1332            unimplemented!()
1333        }
1334
1335        fn load_bytes(&self, _cx: &App) -> Task<Result<Vec<u8>>> {
1336            unimplemented!()
1337        }
1338    }
1339}
1340
1341#[cfg(test)]
1342#[ctor::ctor]
1343fn init_logger() {
1344    zlog::init_test();
1345}