copilot.rs

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