copilot_chat.rs

  1use std::future;
  2use std::sync::Arc;
  3
  4use anyhow::{anyhow, Result};
  5use copilot::copilot_chat::{
  6    ChatMessage, CopilotChat, Model as CopilotChatModel, Request as CopilotChatRequest,
  7    Role as CopilotChatRole,
  8};
  9use copilot::{Copilot, Status};
 10use futures::future::BoxFuture;
 11use futures::stream::BoxStream;
 12use futures::{FutureExt, StreamExt};
 13use gpui::{
 14    percentage, svg, Animation, AnimationExt, AnyView, App, AsyncApp, Entity, Render, Subscription,
 15    Task, Transformation,
 16};
 17use language_model::{
 18    LanguageModel, LanguageModelCompletionEvent, LanguageModelId, LanguageModelName,
 19    LanguageModelProvider, LanguageModelProviderId, LanguageModelProviderName,
 20    LanguageModelProviderState, LanguageModelRequest, RateLimiter, Role,
 21};
 22use settings::SettingsStore;
 23use std::time::Duration;
 24use strum::IntoEnumIterator;
 25use ui::prelude::*;
 26
 27use super::anthropic::count_anthropic_tokens;
 28use super::google::count_google_tokens;
 29use super::open_ai::count_open_ai_tokens;
 30
 31const PROVIDER_ID: &str = "copilot_chat";
 32const PROVIDER_NAME: &str = "GitHub Copilot Chat";
 33
 34#[derive(Default, Clone, Debug, PartialEq)]
 35pub struct CopilotChatSettings {}
 36
 37pub struct CopilotChatLanguageModelProvider {
 38    state: Entity<State>,
 39}
 40
 41pub struct State {
 42    _copilot_chat_subscription: Option<Subscription>,
 43    _settings_subscription: Subscription,
 44}
 45
 46impl State {
 47    fn is_authenticated(&self, cx: &App) -> bool {
 48        CopilotChat::global(cx)
 49            .map(|m| m.read(cx).is_authenticated())
 50            .unwrap_or(false)
 51    }
 52}
 53
 54impl CopilotChatLanguageModelProvider {
 55    pub fn new(cx: &mut App) -> Self {
 56        let state = cx.new(|cx| {
 57            let _copilot_chat_subscription = CopilotChat::global(cx)
 58                .map(|copilot_chat| cx.observe(&copilot_chat, |_, _, cx| cx.notify()));
 59            State {
 60                _copilot_chat_subscription,
 61                _settings_subscription: cx.observe_global::<SettingsStore>(|_, cx| {
 62                    cx.notify();
 63                }),
 64            }
 65        });
 66
 67        Self { state }
 68    }
 69}
 70
 71impl LanguageModelProviderState for CopilotChatLanguageModelProvider {
 72    type ObservableEntity = State;
 73
 74    fn observable_entity(&self) -> Option<gpui::Entity<Self::ObservableEntity>> {
 75        Some(self.state.clone())
 76    }
 77}
 78
 79impl LanguageModelProvider for CopilotChatLanguageModelProvider {
 80    fn id(&self) -> LanguageModelProviderId {
 81        LanguageModelProviderId(PROVIDER_ID.into())
 82    }
 83
 84    fn name(&self) -> LanguageModelProviderName {
 85        LanguageModelProviderName(PROVIDER_NAME.into())
 86    }
 87
 88    fn icon(&self) -> IconName {
 89        IconName::Copilot
 90    }
 91
 92    fn provided_models(&self, _cx: &App) -> Vec<Arc<dyn LanguageModel>> {
 93        CopilotChatModel::iter()
 94            .map(|model| {
 95                Arc::new(CopilotChatLanguageModel {
 96                    model,
 97                    request_limiter: RateLimiter::new(4),
 98                }) as Arc<dyn LanguageModel>
 99            })
100            .collect()
101    }
102
103    fn is_authenticated(&self, cx: &App) -> bool {
104        self.state.read(cx).is_authenticated(cx)
105    }
106
107    fn authenticate(&self, cx: &mut App) -> Task<Result<()>> {
108        let result = if self.is_authenticated(cx) {
109            Ok(())
110        } else if let Some(copilot) = Copilot::global(cx) {
111            let error_msg = match copilot.read(cx).status() {
112                Status::Disabled => anyhow::anyhow!("Copilot must be enabled for Copilot Chat to work. Please enable Copilot and try again."),
113                Status::Error(e) => anyhow::anyhow!(format!("Received the following error while signing into Copilot: {e}")),
114                Status::Starting { task: _ } => anyhow::anyhow!("Copilot is still starting, please wait for Copilot to start then try again"),
115                Status::Unauthorized => anyhow::anyhow!("Unable to authorize with Copilot. Please make sure that you have an active Copilot and Copilot Chat subscription."),
116                Status::Authorized => return Task::ready(Ok(())),
117                Status::SignedOut => anyhow::anyhow!("You have signed out of Copilot. Please sign in to Copilot and try again."),
118                Status::SigningIn { prompt: _ } => anyhow::anyhow!("Still signing into Copilot..."),
119            };
120            Err(error_msg)
121        } else {
122            Err(anyhow::anyhow!(
123                "Copilot must be enabled for Copilot Chat to work. Please enable Copilot and try again."
124            ))
125        };
126        Task::ready(result)
127    }
128
129    fn configuration_view(&self, _: &mut Window, cx: &mut App) -> AnyView {
130        let state = self.state.clone();
131        cx.new(|cx| ConfigurationView::new(state, cx)).into()
132    }
133
134    fn reset_credentials(&self, _cx: &mut App) -> Task<Result<()>> {
135        Task::ready(Err(anyhow!(
136            "Signing out of GitHub Copilot Chat is currently not supported."
137        )))
138    }
139}
140
141pub struct CopilotChatLanguageModel {
142    model: CopilotChatModel,
143    request_limiter: RateLimiter,
144}
145
146impl LanguageModel for CopilotChatLanguageModel {
147    fn id(&self) -> LanguageModelId {
148        LanguageModelId::from(self.model.id().to_string())
149    }
150
151    fn name(&self) -> LanguageModelName {
152        LanguageModelName::from(self.model.display_name().to_string())
153    }
154
155    fn provider_id(&self) -> LanguageModelProviderId {
156        LanguageModelProviderId(PROVIDER_ID.into())
157    }
158
159    fn provider_name(&self) -> LanguageModelProviderName {
160        LanguageModelProviderName(PROVIDER_NAME.into())
161    }
162
163    fn telemetry_id(&self) -> String {
164        format!("copilot_chat/{}", self.model.id())
165    }
166
167    fn max_token_count(&self) -> usize {
168        self.model.max_token_count()
169    }
170
171    fn count_tokens(
172        &self,
173        request: LanguageModelRequest,
174        cx: &App,
175    ) -> BoxFuture<'static, Result<usize>> {
176        match self.model {
177            CopilotChatModel::Claude3_5Sonnet => count_anthropic_tokens(request, cx),
178            CopilotChatModel::Claude3_7Sonnet => count_anthropic_tokens(request, cx),
179            CopilotChatModel::Gemini20Flash => count_google_tokens(request, cx),
180            _ => {
181                let model = match self.model {
182                    CopilotChatModel::Gpt4o => open_ai::Model::FourOmni,
183                    CopilotChatModel::Gpt4 => open_ai::Model::Four,
184                    CopilotChatModel::Gpt3_5Turbo => open_ai::Model::ThreePointFiveTurbo,
185                    CopilotChatModel::O1 | CopilotChatModel::O3Mini => open_ai::Model::Four,
186                    CopilotChatModel::Claude3_5Sonnet
187                    | CopilotChatModel::Claude3_7Sonnet
188                    | CopilotChatModel::Gemini20Flash => {
189                        unreachable!()
190                    }
191                };
192                count_open_ai_tokens(request, model, cx)
193            }
194        }
195    }
196
197    fn stream_completion(
198        &self,
199        request: LanguageModelRequest,
200        cx: &AsyncApp,
201    ) -> BoxFuture<'static, Result<BoxStream<'static, Result<LanguageModelCompletionEvent>>>> {
202        if let Some(message) = request.messages.last() {
203            if message.contents_empty() {
204                const EMPTY_PROMPT_MSG: &str =
205                    "Empty prompts aren't allowed. Please provide a non-empty prompt.";
206                return futures::future::ready(Err(anyhow::anyhow!(EMPTY_PROMPT_MSG))).boxed();
207            }
208
209            // Copilot Chat has a restriction that the final message must be from the user.
210            // While their API does return an error message for this, we can catch it earlier
211            // and provide a more helpful error message.
212            if !matches!(message.role, Role::User) {
213                const USER_ROLE_MSG: &str = "The final message must be from the user. To provide a system prompt, you must provide the system prompt followed by a user prompt.";
214                return futures::future::ready(Err(anyhow::anyhow!(USER_ROLE_MSG))).boxed();
215            }
216        }
217
218        let copilot_request = self.to_copilot_chat_request(request);
219        let is_streaming = copilot_request.stream;
220
221        let request_limiter = self.request_limiter.clone();
222        let future = cx.spawn(|cx| async move {
223            let response = CopilotChat::stream_completion(copilot_request, cx);
224            request_limiter.stream(async move {
225                let response = response.await?;
226                let stream = response
227                    .filter_map(move |response| async move {
228                        match response {
229                            Ok(result) => {
230                                let choice = result.choices.first();
231                                match choice {
232                                    Some(choice) if !is_streaming => {
233                                        match &choice.message {
234                                            Some(msg) => Some(Ok(msg.content.clone().unwrap_or_default())),
235                                            None => Some(Err(anyhow::anyhow!(
236                                                "The Copilot Chat API returned a response with no message content"
237                                            ))),
238                                        }
239                                    },
240                                    Some(choice) => {
241                                        match &choice.delta {
242                                            Some(delta) => Some(Ok(delta.content.clone().unwrap_or_default())),
243                                            None => Some(Err(anyhow::anyhow!(
244                                                "The Copilot Chat API returned a response with no delta content"
245                                            ))),
246                                        }
247                                    },
248                                    None => Some(Err(anyhow::anyhow!(
249                                        "The Copilot Chat API returned a response with no choices, but hadn't finished the message yet. Please try again."
250                                    ))),
251                                }
252                            }
253                            Err(err) => Some(Err(err)),
254                        }
255                    })
256                    .boxed();
257                Ok(stream)
258            }).await
259        });
260
261        async move {
262            Ok(future
263                .await?
264                .map(|result| result.map(LanguageModelCompletionEvent::Text))
265                .boxed())
266        }
267        .boxed()
268    }
269
270    fn use_any_tool(
271        &self,
272        _request: LanguageModelRequest,
273        _name: String,
274        _description: String,
275        _schema: serde_json::Value,
276        _cx: &AsyncApp,
277    ) -> BoxFuture<'static, Result<BoxStream<'static, Result<String>>>> {
278        future::ready(Err(anyhow!("not implemented"))).boxed()
279    }
280}
281
282impl CopilotChatLanguageModel {
283    pub fn to_copilot_chat_request(&self, request: LanguageModelRequest) -> CopilotChatRequest {
284        CopilotChatRequest::new(
285            self.model.clone(),
286            request
287                .messages
288                .into_iter()
289                .map(|msg| ChatMessage {
290                    role: match msg.role {
291                        Role::User => CopilotChatRole::User,
292                        Role::Assistant => CopilotChatRole::Assistant,
293                        Role::System => CopilotChatRole::System,
294                    },
295                    content: msg.string_contents(),
296                })
297                .collect(),
298        )
299    }
300}
301
302struct ConfigurationView {
303    copilot_status: Option<copilot::Status>,
304    state: Entity<State>,
305    _subscription: Option<Subscription>,
306}
307
308impl ConfigurationView {
309    pub fn new(state: Entity<State>, cx: &mut Context<Self>) -> Self {
310        let copilot = Copilot::global(cx);
311
312        Self {
313            copilot_status: copilot.as_ref().map(|copilot| copilot.read(cx).status()),
314            state,
315            _subscription: copilot.as_ref().map(|copilot| {
316                cx.observe(copilot, |this, model, cx| {
317                    this.copilot_status = Some(model.read(cx).status());
318                    cx.notify();
319                })
320            }),
321        }
322    }
323}
324
325impl Render for ConfigurationView {
326    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
327        if self.state.read(cx).is_authenticated(cx) {
328            const LABEL: &str = "Authorized.";
329            h_flex()
330                .gap_1()
331                .child(Icon::new(IconName::Check).color(Color::Success))
332                .child(Label::new(LABEL))
333        } else {
334            let loading_icon = svg()
335                .size_8()
336                .path(IconName::ArrowCircle.path())
337                .text_color(window.text_style().color)
338                .with_animation(
339                    "icon_circle_arrow",
340                    Animation::new(Duration::from_secs(2)).repeat(),
341                    |svg, delta| svg.with_transformation(Transformation::rotate(percentage(delta))),
342                );
343
344            const ERROR_LABEL: &str = "Copilot Chat requires an active GitHub Copilot subscription. Please ensure Copilot is configured and try again, or use a different Assistant provider.";
345
346            match &self.copilot_status {
347                Some(status) => match status {
348                    Status::Disabled => v_flex().gap_6().p_4().child(Label::new(ERROR_LABEL)),
349                    Status::Starting { task: _ } => {
350                        const LABEL: &str = "Starting Copilot...";
351                        v_flex()
352                            .gap_6()
353                            .justify_center()
354                            .items_center()
355                            .child(Label::new(LABEL))
356                            .child(loading_icon)
357                    }
358                    Status::SigningIn { prompt: _ } => {
359                        const LABEL: &str = "Signing in to Copilot...";
360                        v_flex()
361                            .gap_6()
362                            .justify_center()
363                            .items_center()
364                            .child(Label::new(LABEL))
365                            .child(loading_icon)
366                    }
367                    Status::Error(_) => {
368                        const LABEL: &str = "Copilot had issues starting. Please try restarting it. If the issue persists, try reinstalling Copilot.";
369                        v_flex()
370                            .gap_6()
371                            .child(Label::new(LABEL))
372                            .child(svg().size_8().path(IconName::CopilotError.path()))
373                    }
374                    _ => {
375                        const LABEL: &str =
376                    "To use Zed's assistant with GitHub Copilot, you need to be logged in to GitHub. Note that your GitHub account must have an active Copilot Chat subscription.";
377                        v_flex().gap_6().child(Label::new(LABEL)).child(
378                            v_flex()
379                                .gap_2()
380                                .child(
381                                    Button::new("sign_in", "Sign In")
382                                        .icon_color(Color::Muted)
383                                        .icon(IconName::Github)
384                                        .icon_position(IconPosition::Start)
385                                        .icon_size(IconSize::Medium)
386                                        .style(ui::ButtonStyle::Filled)
387                                        .full_width()
388                                        .on_click(|_, window, cx| {
389                                            copilot::initiate_sign_in(window, cx)
390                                        }),
391                                )
392                                .child(
393                                    div().flex().w_full().items_center().child(
394                                        Label::new("Sign in to start using Github Copilot Chat.")
395                                            .color(Color::Muted)
396                                            .size(ui::LabelSize::Small),
397                                    ),
398                                ),
399                        )
400                    }
401                },
402                None => v_flex().gap_6().child(Label::new(ERROR_LABEL)),
403            }
404        }
405    }
406}