copilot.rs

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