copilot.rs

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