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