copilot.rs

  1mod request;
  2mod sign_in;
  3
  4use anyhow::{anyhow, Context, Result};
  5use async_compression::futures::bufread::GzipDecoder;
  6use async_tar::Archive;
  7use client::Client;
  8use collections::HashMap;
  9use futures::{future::Shared, Future, FutureExt, TryFutureExt};
 10use gpui::{
 11    actions, AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle, MutableAppContext,
 12    Task,
 13};
 14use language::{point_from_lsp, point_to_lsp, Anchor, Bias, Buffer, Language, ToPointUtf16};
 15use log::{debug, error};
 16use lsp::LanguageServer;
 17use node_runtime::NodeRuntime;
 18use request::{LogMessage, StatusNotification};
 19use settings::Settings;
 20use smol::{fs, io::BufReader, stream::StreamExt};
 21use std::{
 22    ffi::OsString,
 23    ops::Range,
 24    path::{Path, PathBuf},
 25    sync::Arc,
 26};
 27use util::{
 28    fs::remove_matching, github::latest_github_release, http::HttpClient, paths, ResultExt,
 29};
 30
 31const COPILOT_AUTH_NAMESPACE: &'static str = "copilot_auth";
 32actions!(copilot_auth, [SignIn, SignOut]);
 33
 34const COPILOT_NAMESPACE: &'static str = "copilot";
 35actions!(copilot, [NextSuggestion, PreviousSuggestion, Reinstall]);
 36
 37pub fn init(client: Arc<Client>, node_runtime: Arc<NodeRuntime>, cx: &mut MutableAppContext) {
 38    let copilot = cx.add_model(|cx| Copilot::start(client.http_client(), node_runtime, cx));
 39    cx.set_global(copilot.clone());
 40    cx.add_global_action(|_: &SignIn, cx| {
 41        let copilot = Copilot::global(cx).unwrap();
 42        copilot
 43            .update(cx, |copilot, cx| copilot.sign_in(cx))
 44            .detach_and_log_err(cx);
 45    });
 46    cx.add_global_action(|_: &SignOut, cx| {
 47        let copilot = Copilot::global(cx).unwrap();
 48        copilot
 49            .update(cx, |copilot, cx| copilot.sign_out(cx))
 50            .detach_and_log_err(cx);
 51    });
 52
 53    cx.add_global_action(|_: &Reinstall, cx| {
 54        let copilot = Copilot::global(cx).unwrap();
 55        copilot
 56            .update(cx, |copilot, cx| copilot.reinstall(cx))
 57            .detach();
 58    });
 59
 60    cx.observe(&copilot, |handle, cx| {
 61        let status = handle.read(cx).status();
 62        cx.update_global::<collections::CommandPaletteFilter, _, _>(
 63            move |filter, _cx| match status {
 64                Status::Disabled => {
 65                    filter.filtered_namespaces.insert(COPILOT_NAMESPACE);
 66                    filter.filtered_namespaces.insert(COPILOT_AUTH_NAMESPACE);
 67                }
 68                Status::Authorized => {
 69                    filter.filtered_namespaces.remove(COPILOT_NAMESPACE);
 70                    filter.filtered_namespaces.remove(COPILOT_AUTH_NAMESPACE);
 71                }
 72                _ => {
 73                    filter.filtered_namespaces.insert(COPILOT_NAMESPACE);
 74                    filter.filtered_namespaces.remove(COPILOT_AUTH_NAMESPACE);
 75                }
 76            },
 77        );
 78    })
 79    .detach();
 80
 81    sign_in::init(cx);
 82}
 83
 84enum CopilotServer {
 85    Disabled,
 86    Starting {
 87        task: Shared<Task<()>>,
 88    },
 89    Error(Arc<str>),
 90    Started {
 91        server: Arc<LanguageServer>,
 92        status: SignInStatus,
 93        subscriptions_by_buffer_id: HashMap<usize, gpui::Subscription>,
 94    },
 95}
 96
 97#[derive(Clone, Debug)]
 98enum SignInStatus {
 99    Authorized {
100        _user: String,
101    },
102    Unauthorized {
103        _user: String,
104    },
105    SigningIn {
106        prompt: Option<request::PromptUserDeviceFlow>,
107        task: Shared<Task<Result<(), Arc<anyhow::Error>>>>,
108    },
109    SignedOut,
110}
111
112#[derive(Debug, Clone)]
113pub enum Status {
114    Starting {
115        task: Shared<Task<()>>,
116    },
117    Error(Arc<str>),
118    Disabled,
119    SignedOut,
120    SigningIn {
121        prompt: Option<request::PromptUserDeviceFlow>,
122    },
123    Unauthorized,
124    Authorized,
125}
126
127impl Status {
128    pub fn is_authorized(&self) -> bool {
129        matches!(self, Status::Authorized)
130    }
131}
132
133#[derive(Debug, PartialEq, Eq)]
134pub struct Completion {
135    pub range: Range<Anchor>,
136    pub text: String,
137}
138
139pub struct Copilot {
140    http: Arc<dyn HttpClient>,
141    node_runtime: Arc<NodeRuntime>,
142    server: CopilotServer,
143}
144
145impl Entity for Copilot {
146    type Event = ();
147}
148
149impl Copilot {
150    pub fn starting_task(&self) -> Option<Shared<Task<()>>> {
151        match self.server {
152            CopilotServer::Starting { ref task } => Some(task.clone()),
153            _ => None,
154        }
155    }
156
157    pub fn global(cx: &AppContext) -> Option<ModelHandle<Self>> {
158        if cx.has_global::<ModelHandle<Self>>() {
159            Some(cx.global::<ModelHandle<Self>>().clone())
160        } else {
161            None
162        }
163    }
164
165    fn start(
166        http: Arc<dyn HttpClient>,
167        node_runtime: Arc<NodeRuntime>,
168        cx: &mut ModelContext<Self>,
169    ) -> Self {
170        cx.observe_global::<Settings, _>({
171            let http = http.clone();
172            let node_runtime = node_runtime.clone();
173            move |this, cx| {
174                if cx.global::<Settings>().enable_copilot_integration {
175                    if matches!(this.server, CopilotServer::Disabled) {
176                        let start_task = cx
177                            .spawn({
178                                let http = http.clone();
179                                let node_runtime = node_runtime.clone();
180                                move |this, cx| {
181                                    Self::start_language_server(http, node_runtime, this, cx)
182                                }
183                            })
184                            .shared();
185                        this.server = CopilotServer::Starting { task: start_task };
186                        cx.notify();
187                    }
188                } else {
189                    this.server = CopilotServer::Disabled;
190                    cx.notify();
191                }
192            }
193        })
194        .detach();
195
196        if cx.global::<Settings>().enable_copilot_integration {
197            let start_task = cx
198                .spawn({
199                    let http = http.clone();
200                    let node_runtime = node_runtime.clone();
201                    move |this, cx| Self::start_language_server(http, node_runtime, this, cx)
202                })
203                .shared();
204
205            Self {
206                http,
207                node_runtime,
208                server: CopilotServer::Starting { task: start_task },
209            }
210        } else {
211            Self {
212                http,
213                node_runtime,
214                server: CopilotServer::Disabled,
215            }
216        }
217    }
218
219    fn start_language_server(
220        http: Arc<dyn HttpClient>,
221        node_runtime: Arc<NodeRuntime>,
222        this: ModelHandle<Self>,
223        mut cx: AsyncAppContext,
224    ) -> impl Future<Output = ()> {
225        async move {
226            let start_language_server = async {
227                let server_path = get_copilot_lsp(http).await?;
228                let node_path = node_runtime.binary_path().await?;
229                let arguments: &[OsString] = &[server_path.into(), "--stdio".into()];
230                let server = LanguageServer::new(
231                    0,
232                    &node_path,
233                    arguments,
234                    Path::new("/"),
235                    None,
236                    cx.clone(),
237                )?;
238
239                let server = server.initialize(Default::default()).await?;
240                let status = server
241                    .request::<request::CheckStatus>(request::CheckStatusParams {
242                        local_checks_only: false,
243                    })
244                    .await?;
245
246                server
247                    .on_notification::<LogMessage, _>(|params, _cx| {
248                        match params.level {
249                            // Copilot is pretty agressive about logging
250                            0 => debug!("copilot: {}", params.message),
251                            1 => debug!("copilot: {}", params.message),
252                            _ => error!("copilot: {}", params.message),
253                        }
254
255                        debug!("copilot metadata: {}", params.metadata_str);
256                        debug!("copilot extra: {:?}", params.extra);
257                    })
258                    .detach();
259
260                server
261                    .on_notification::<StatusNotification, _>(
262                        |_, _| { /* Silence the notification */ },
263                    )
264                    .detach();
265
266                anyhow::Ok((server, status))
267            };
268
269            let server = start_language_server.await;
270            this.update(&mut cx, |this, cx| {
271                cx.notify();
272                match server {
273                    Ok((server, status)) => {
274                        this.server = CopilotServer::Started {
275                            server,
276                            status: SignInStatus::SignedOut,
277                            subscriptions_by_buffer_id: Default::default(),
278                        };
279                        this.update_sign_in_status(status, cx);
280                    }
281                    Err(error) => {
282                        this.server = CopilotServer::Error(error.to_string().into());
283                        cx.notify()
284                    }
285                }
286            })
287        }
288    }
289
290    fn sign_in(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> {
291        if let CopilotServer::Started { server, status, .. } = &mut self.server {
292            let task = match status {
293                SignInStatus::Authorized { .. } | SignInStatus::Unauthorized { .. } => {
294                    Task::ready(Ok(())).shared()
295                }
296                SignInStatus::SigningIn { task, .. } => {
297                    cx.notify();
298                    task.clone()
299                }
300                SignInStatus::SignedOut => {
301                    let server = server.clone();
302                    let task = cx
303                        .spawn(|this, mut cx| async move {
304                            let sign_in = async {
305                                let sign_in = server
306                                    .request::<request::SignInInitiate>(
307                                        request::SignInInitiateParams {},
308                                    )
309                                    .await?;
310                                match sign_in {
311                                    request::SignInInitiateResult::AlreadySignedIn { user } => {
312                                        Ok(request::SignInStatus::Ok { user })
313                                    }
314                                    request::SignInInitiateResult::PromptUserDeviceFlow(flow) => {
315                                        this.update(&mut cx, |this, cx| {
316                                            if let CopilotServer::Started { status, .. } =
317                                                &mut this.server
318                                            {
319                                                if let SignInStatus::SigningIn {
320                                                    prompt: prompt_flow,
321                                                    ..
322                                                } = status
323                                                {
324                                                    *prompt_flow = Some(flow.clone());
325                                                    cx.notify();
326                                                }
327                                            }
328                                        });
329                                        let response = server
330                                            .request::<request::SignInConfirm>(
331                                                request::SignInConfirmParams {
332                                                    user_code: flow.user_code,
333                                                },
334                                            )
335                                            .await?;
336                                        Ok(response)
337                                    }
338                                }
339                            };
340
341                            let sign_in = sign_in.await;
342                            this.update(&mut cx, |this, cx| match sign_in {
343                                Ok(status) => {
344                                    this.update_sign_in_status(status, cx);
345                                    Ok(())
346                                }
347                                Err(error) => {
348                                    this.update_sign_in_status(
349                                        request::SignInStatus::NotSignedIn,
350                                        cx,
351                                    );
352                                    Err(Arc::new(error))
353                                }
354                            })
355                        })
356                        .shared();
357                    *status = SignInStatus::SigningIn {
358                        prompt: None,
359                        task: task.clone(),
360                    };
361                    cx.notify();
362                    task
363                }
364            };
365
366            cx.foreground()
367                .spawn(task.map_err(|err| anyhow!("{:?}", err)))
368        } else {
369            // If we're downloading, wait until download is finished
370            // If we're in a stuck state, display to the user
371            Task::ready(Err(anyhow!("copilot hasn't started yet")))
372        }
373    }
374
375    fn sign_out(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> {
376        if let CopilotServer::Started { server, status, .. } = &mut self.server {
377            *status = SignInStatus::SignedOut;
378            cx.notify();
379
380            let server = server.clone();
381            cx.background().spawn(async move {
382                server
383                    .request::<request::SignOut>(request::SignOutParams {})
384                    .await?;
385                anyhow::Ok(())
386            })
387        } else {
388            Task::ready(Err(anyhow!("copilot hasn't started yet")))
389        }
390    }
391
392    fn reinstall(&mut self, cx: &mut ModelContext<Self>) -> Task<()> {
393        let start_task = cx
394            .spawn({
395                let http = self.http.clone();
396                let node_runtime = self.node_runtime.clone();
397                move |this, cx| async move {
398                    clear_copilot_dir().await;
399                    Self::start_language_server(http, node_runtime, this, cx).await
400                }
401            })
402            .shared();
403
404        self.server = CopilotServer::Starting {
405            task: start_task.clone(),
406        };
407
408        cx.notify();
409
410        cx.foreground().spawn(start_task)
411    }
412
413    pub fn completions<T>(
414        &mut self,
415        buffer: &ModelHandle<Buffer>,
416        position: T,
417        cx: &mut ModelContext<Self>,
418    ) -> Task<Result<Vec<Completion>>>
419    where
420        T: ToPointUtf16,
421    {
422        self.request_completions::<request::GetCompletions, _>(buffer, position, cx)
423    }
424
425    pub fn completions_cycling<T>(
426        &mut self,
427        buffer: &ModelHandle<Buffer>,
428        position: T,
429        cx: &mut ModelContext<Self>,
430    ) -> Task<Result<Vec<Completion>>>
431    where
432        T: ToPointUtf16,
433    {
434        self.request_completions::<request::GetCompletionsCycling, _>(buffer, position, cx)
435    }
436
437    fn request_completions<R, T>(
438        &mut self,
439        buffer: &ModelHandle<Buffer>,
440        position: T,
441        cx: &mut ModelContext<Self>,
442    ) -> Task<Result<Vec<Completion>>>
443    where
444        R: lsp::request::Request<
445            Params = request::GetCompletionsParams,
446            Result = request::GetCompletionsResult,
447        >,
448        T: ToPointUtf16,
449    {
450        let buffer_id = buffer.id();
451        let uri: lsp::Url = format!("buffer://{}", buffer_id).parse().unwrap();
452        let snapshot = buffer.read(cx).snapshot();
453        let server = match &mut self.server {
454            CopilotServer::Starting { .. } => {
455                return Task::ready(Err(anyhow!("copilot is still starting")))
456            }
457            CopilotServer::Disabled => return Task::ready(Err(anyhow!("copilot is disabled"))),
458            CopilotServer::Error(error) => {
459                return Task::ready(Err(anyhow!(
460                    "copilot was not started because of an error: {}",
461                    error
462                )))
463            }
464            CopilotServer::Started {
465                server,
466                status,
467                subscriptions_by_buffer_id,
468            } => {
469                if matches!(status, SignInStatus::Authorized { .. }) {
470                    subscriptions_by_buffer_id
471                        .entry(buffer_id)
472                        .or_insert_with(|| {
473                            server
474                                .notify::<lsp::notification::DidOpenTextDocument>(
475                                    lsp::DidOpenTextDocumentParams {
476                                        text_document: lsp::TextDocumentItem {
477                                            uri: uri.clone(),
478                                            language_id: id_for_language(
479                                                buffer.read(cx).language(),
480                                            ),
481                                            version: 0,
482                                            text: snapshot.text(),
483                                        },
484                                    },
485                                )
486                                .log_err();
487
488                            let uri = uri.clone();
489                            cx.observe_release(buffer, move |this, _, _| {
490                                if let CopilotServer::Started {
491                                    server,
492                                    subscriptions_by_buffer_id,
493                                    ..
494                                } = &mut this.server
495                                {
496                                    server
497                                        .notify::<lsp::notification::DidCloseTextDocument>(
498                                            lsp::DidCloseTextDocumentParams {
499                                                text_document: lsp::TextDocumentIdentifier::new(
500                                                    uri.clone(),
501                                                ),
502                                            },
503                                        )
504                                        .log_err();
505                                    subscriptions_by_buffer_id.remove(&buffer_id);
506                                }
507                            })
508                        });
509
510                    server.clone()
511                } else {
512                    return Task::ready(Err(anyhow!("must sign in before using copilot")));
513                }
514            }
515        };
516
517        let settings = cx.global::<Settings>();
518        let position = position.to_point_utf16(&snapshot);
519        let language = snapshot.language_at(position);
520        let language_name = language.map(|language| language.name());
521        let language_name = language_name.as_deref();
522        let tab_size = settings.tab_size(language_name);
523        let hard_tabs = settings.hard_tabs(language_name);
524        let language_id = id_for_language(language);
525
526        let path;
527        let relative_path;
528        if let Some(file) = snapshot.file() {
529            if let Some(file) = file.as_local() {
530                path = file.abs_path(cx);
531            } else {
532                path = file.full_path(cx);
533            }
534            relative_path = file.path().to_path_buf();
535        } else {
536            path = PathBuf::new();
537            relative_path = PathBuf::new();
538        }
539
540        cx.background().spawn(async move {
541            let result = server
542                .request::<R>(request::GetCompletionsParams {
543                    doc: request::GetCompletionsDocument {
544                        source: snapshot.text(),
545                        tab_size: tab_size.into(),
546                        indent_size: 1,
547                        insert_spaces: !hard_tabs,
548                        uri,
549                        path: path.to_string_lossy().into(),
550                        relative_path: relative_path.to_string_lossy().into(),
551                        language_id,
552                        position: point_to_lsp(position),
553                        version: 0,
554                    },
555                })
556                .await?;
557            let completions = result
558                .completions
559                .into_iter()
560                .map(|completion| {
561                    let start = snapshot
562                        .clip_point_utf16(point_from_lsp(completion.range.start), Bias::Left);
563                    let end =
564                        snapshot.clip_point_utf16(point_from_lsp(completion.range.end), Bias::Left);
565                    Completion {
566                        range: snapshot.anchor_before(start)..snapshot.anchor_after(end),
567                        text: completion.text,
568                    }
569                })
570                .collect();
571            anyhow::Ok(completions)
572        })
573    }
574
575    pub fn status(&self) -> Status {
576        match &self.server {
577            CopilotServer::Starting { task } => Status::Starting { task: task.clone() },
578            CopilotServer::Disabled => Status::Disabled,
579            CopilotServer::Error(error) => Status::Error(error.clone()),
580            CopilotServer::Started { status, .. } => match status {
581                SignInStatus::Authorized { .. } => Status::Authorized,
582                SignInStatus::Unauthorized { .. } => Status::Unauthorized,
583                SignInStatus::SigningIn { prompt, .. } => Status::SigningIn {
584                    prompt: prompt.clone(),
585                },
586                SignInStatus::SignedOut => Status::SignedOut,
587            },
588        }
589    }
590
591    fn update_sign_in_status(
592        &mut self,
593        lsp_status: request::SignInStatus,
594        cx: &mut ModelContext<Self>,
595    ) {
596        if let CopilotServer::Started { status, .. } = &mut self.server {
597            *status = match lsp_status {
598                request::SignInStatus::Ok { user }
599                | request::SignInStatus::MaybeOk { user }
600                | request::SignInStatus::AlreadySignedIn { user } => {
601                    SignInStatus::Authorized { _user: user }
602                }
603                request::SignInStatus::NotAuthorized { user } => {
604                    SignInStatus::Unauthorized { _user: user }
605                }
606                request::SignInStatus::NotSignedIn => SignInStatus::SignedOut,
607            };
608            cx.notify();
609        }
610    }
611}
612
613fn id_for_language(language: Option<&Arc<Language>>) -> String {
614    let language_name = language.map(|language| language.name());
615    match language_name.as_deref() {
616        Some("Plain Text") => "plaintext".to_string(),
617        Some(language_name) => language_name.to_lowercase(),
618        None => "plaintext".to_string(),
619    }
620}
621
622async fn clear_copilot_dir() {
623    remove_matching(&paths::COPILOT_DIR, |_| true).await
624}
625
626async fn get_copilot_lsp(http: Arc<dyn HttpClient>) -> anyhow::Result<PathBuf> {
627    const SERVER_PATH: &'static str = "dist/agent.js";
628
629    ///Check for the latest copilot language server and download it if we haven't already
630    async fn fetch_latest(http: Arc<dyn HttpClient>) -> anyhow::Result<PathBuf> {
631        let release = latest_github_release("zed-industries/copilot", http.clone()).await?;
632
633        let version_dir = &*paths::COPILOT_DIR.join(format!("copilot-{}", release.name));
634
635        fs::create_dir_all(version_dir).await?;
636        let server_path = version_dir.join(SERVER_PATH);
637
638        if fs::metadata(&server_path).await.is_err() {
639            // Copilot LSP looks for this dist dir specifcially, so lets add it in.
640            let dist_dir = version_dir.join("dist");
641            fs::create_dir_all(dist_dir.as_path()).await?;
642
643            let url = &release
644                .assets
645                .get(0)
646                .context("Github release for copilot contained no assets")?
647                .browser_download_url;
648
649            let mut response = http
650                .get(&url, Default::default(), true)
651                .await
652                .map_err(|err| anyhow!("error downloading copilot release: {}", err))?;
653            let decompressed_bytes = GzipDecoder::new(BufReader::new(response.body_mut()));
654            let archive = Archive::new(decompressed_bytes);
655            archive.unpack(dist_dir).await?;
656
657            remove_matching(&paths::COPILOT_DIR, |entry| entry != version_dir).await;
658        }
659
660        Ok(server_path)
661    }
662
663    match fetch_latest(http).await {
664        ok @ Result::Ok(..) => ok,
665        e @ Err(..) => {
666            e.log_err();
667            // Fetch a cached binary, if it exists
668            (|| async move {
669                let mut last_version_dir = None;
670                let mut entries = fs::read_dir(paths::COPILOT_DIR.as_path()).await?;
671                while let Some(entry) = entries.next().await {
672                    let entry = entry?;
673                    if entry.file_type().await?.is_dir() {
674                        last_version_dir = Some(entry.path());
675                    }
676                }
677                let last_version_dir =
678                    last_version_dir.ok_or_else(|| anyhow!("no cached binary"))?;
679                let server_path = last_version_dir.join(SERVER_PATH);
680                if server_path.exists() {
681                    Ok(server_path)
682                } else {
683                    Err(anyhow!(
684                        "missing executable in directory {:?}",
685                        last_version_dir
686                    ))
687                }
688            })()
689            .await
690        }
691    }
692}