copilot.rs

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