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