copilot_chat.rs

  1use std::future;
  2use std::sync::Arc;
  3
  4use anyhow::{Result, anyhow};
  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    Action, Animation, AnimationExt, AnyView, App, AsyncApp, Entity, Render, Subscription, Task,
 15    Transformation, percentage, svg,
 16};
 17use language_model::{
 18    AuthenticateError, LanguageModel, LanguageModelCompletionEvent, LanguageModelId,
 19    LanguageModelName, 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 default_model(&self, _cx: &App) -> Option<Arc<dyn LanguageModel>> {
 93        let model = CopilotChatModel::default();
 94        Some(Arc::new(CopilotChatLanguageModel {
 95            model,
 96            request_limiter: RateLimiter::new(4),
 97        }) as Arc<dyn LanguageModel>)
 98    }
 99
100    fn provided_models(&self, _cx: &App) -> Vec<Arc<dyn LanguageModel>> {
101        CopilotChatModel::iter()
102            .map(|model| {
103                Arc::new(CopilotChatLanguageModel {
104                    model,
105                    request_limiter: RateLimiter::new(4),
106                }) as Arc<dyn LanguageModel>
107            })
108            .collect()
109    }
110
111    fn is_authenticated(&self, cx: &App) -> bool {
112        self.state.read(cx).is_authenticated(cx)
113    }
114
115    fn authenticate(&self, cx: &mut App) -> Task<Result<(), AuthenticateError>> {
116        if self.is_authenticated(cx) {
117            return Task::ready(Ok(()));
118        };
119
120        let Some(copilot) = Copilot::global(cx) else {
121            return Task::ready( Err(anyhow!(
122                "Copilot must be enabled for Copilot Chat to work. Please enable Copilot and try again."
123            ).into()));
124        };
125
126        let err = match copilot.read(cx).status() {
127            Status::Authorized => return Task::ready(Ok(())),
128            Status::Disabled => anyhow!(
129                "Copilot must be enabled for Copilot Chat to work. Please enable Copilot and try again."
130            ),
131            Status::Error(err) => anyhow!(format!(
132                "Received the following error while signing into Copilot: {err}"
133            )),
134            Status::Starting { task: _ } => anyhow!(
135                "Copilot is still starting, please wait for Copilot to start then try again"
136            ),
137            Status::Unauthorized => anyhow!(
138                "Unable to authorize with Copilot. Please make sure that you have an active Copilot and Copilot Chat subscription."
139            ),
140            Status::SignedOut { .. } => {
141                anyhow!("You have signed out of Copilot. Please sign in to Copilot and try again.")
142            }
143            Status::SigningIn { prompt: _ } => anyhow!("Still signing into Copilot..."),
144        };
145
146        Task::ready(Err(err.into()))
147    }
148
149    fn configuration_view(&self, _: &mut Window, cx: &mut App) -> AnyView {
150        let state = self.state.clone();
151        cx.new(|cx| ConfigurationView::new(state, cx)).into()
152    }
153
154    fn reset_credentials(&self, _cx: &mut App) -> Task<Result<()>> {
155        Task::ready(Err(anyhow!(
156            "Signing out of GitHub Copilot Chat is currently not supported."
157        )))
158    }
159}
160
161pub struct CopilotChatLanguageModel {
162    model: CopilotChatModel,
163    request_limiter: RateLimiter,
164}
165
166impl LanguageModel for CopilotChatLanguageModel {
167    fn id(&self) -> LanguageModelId {
168        LanguageModelId::from(self.model.id().to_string())
169    }
170
171    fn name(&self) -> LanguageModelName {
172        LanguageModelName::from(self.model.display_name().to_string())
173    }
174
175    fn provider_id(&self) -> LanguageModelProviderId {
176        LanguageModelProviderId(PROVIDER_ID.into())
177    }
178
179    fn provider_name(&self) -> LanguageModelProviderName {
180        LanguageModelProviderName(PROVIDER_NAME.into())
181    }
182
183    fn supports_tools(&self) -> bool {
184        match self.model {
185            CopilotChatModel::Claude3_5Sonnet
186            | CopilotChatModel::Claude3_7Sonnet
187            | CopilotChatModel::Claude3_7SonnetThinking => true,
188            _ => false,
189        }
190    }
191
192    fn telemetry_id(&self) -> String {
193        format!("copilot_chat/{}", self.model.id())
194    }
195
196    fn max_token_count(&self) -> usize {
197        self.model.max_token_count()
198    }
199
200    fn count_tokens(
201        &self,
202        request: LanguageModelRequest,
203        cx: &App,
204    ) -> BoxFuture<'static, Result<usize>> {
205        match self.model {
206            CopilotChatModel::Claude3_5Sonnet => count_anthropic_tokens(request, cx),
207            CopilotChatModel::Claude3_7Sonnet => count_anthropic_tokens(request, cx),
208            CopilotChatModel::Claude3_7SonnetThinking => count_anthropic_tokens(request, cx),
209            CopilotChatModel::Gemini20Flash => count_google_tokens(request, cx),
210            _ => {
211                let model = match self.model {
212                    CopilotChatModel::Gpt4o => open_ai::Model::FourOmni,
213                    CopilotChatModel::Gpt4 => open_ai::Model::Four,
214                    CopilotChatModel::Gpt3_5Turbo => open_ai::Model::ThreePointFiveTurbo,
215                    CopilotChatModel::O1 | CopilotChatModel::O3Mini => open_ai::Model::Four,
216                    CopilotChatModel::Claude3_5Sonnet
217                    | CopilotChatModel::Claude3_7Sonnet
218                    | CopilotChatModel::Claude3_7SonnetThinking
219                    | CopilotChatModel::Gemini20Flash => {
220                        unreachable!()
221                    }
222                };
223                count_open_ai_tokens(request, model, cx)
224            }
225        }
226    }
227
228    fn stream_completion(
229        &self,
230        request: LanguageModelRequest,
231        cx: &AsyncApp,
232    ) -> BoxFuture<'static, Result<BoxStream<'static, Result<LanguageModelCompletionEvent>>>> {
233        if let Some(message) = request.messages.last() {
234            if message.contents_empty() {
235                const EMPTY_PROMPT_MSG: &str =
236                    "Empty prompts aren't allowed. Please provide a non-empty prompt.";
237                return futures::future::ready(Err(anyhow::anyhow!(EMPTY_PROMPT_MSG))).boxed();
238            }
239
240            // Copilot Chat has a restriction that the final message must be from the user.
241            // While their API does return an error message for this, we can catch it earlier
242            // and provide a more helpful error message.
243            if !matches!(message.role, Role::User) {
244                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.";
245                return futures::future::ready(Err(anyhow::anyhow!(USER_ROLE_MSG))).boxed();
246            }
247        }
248
249        let copilot_request = self.to_copilot_chat_request(request);
250        let is_streaming = copilot_request.stream;
251
252        let request_limiter = self.request_limiter.clone();
253        let future = cx.spawn(async move |cx| {
254            let response = CopilotChat::stream_completion(copilot_request, cx.clone());
255            request_limiter.stream(async move {
256                let response = response.await?;
257                let stream = response
258                    .filter_map(move |response| async move {
259                        match response {
260                            Ok(result) => {
261                                let choice = result.choices.first();
262                                match choice {
263                                    Some(choice) if !is_streaming => {
264                                        match &choice.message {
265                                            Some(msg) => Some(Ok(msg.content.clone().unwrap_or_default())),
266                                            None => Some(Err(anyhow::anyhow!(
267                                                "The Copilot Chat API returned a response with no message content"
268                                            ))),
269                                        }
270                                    },
271                                    Some(choice) => {
272                                        match &choice.delta {
273                                            Some(delta) => Some(Ok(delta.content.clone().unwrap_or_default())),
274                                            None => Some(Err(anyhow::anyhow!(
275                                                "The Copilot Chat API returned a response with no delta content"
276                                            ))),
277                                        }
278                                    },
279                                    None => Some(Err(anyhow::anyhow!(
280                                        "The Copilot Chat API returned a response with no choices, but hadn't finished the message yet. Please try again."
281                                    ))),
282                                }
283                            }
284                            Err(err) => Some(Err(err)),
285                        }
286                    })
287                    .boxed();
288
289                Ok(stream)
290            }).await
291        });
292
293        async move {
294            Ok(future
295                .await?
296                .map(|result| result.map(LanguageModelCompletionEvent::Text))
297                .boxed())
298        }
299        .boxed()
300    }
301
302    fn use_any_tool(
303        &self,
304        _request: LanguageModelRequest,
305        _name: String,
306        _description: String,
307        _schema: serde_json::Value,
308        _cx: &AsyncApp,
309    ) -> BoxFuture<'static, Result<BoxStream<'static, Result<String>>>> {
310        future::ready(Err(anyhow!("not implemented"))).boxed()
311    }
312}
313
314impl CopilotChatLanguageModel {
315    pub fn to_copilot_chat_request(&self, request: LanguageModelRequest) -> CopilotChatRequest {
316        CopilotChatRequest::new(
317            self.model.clone(),
318            request
319                .messages
320                .into_iter()
321                .map(|msg| ChatMessage {
322                    role: match msg.role {
323                        Role::User => CopilotChatRole::User,
324                        Role::Assistant => CopilotChatRole::Assistant,
325                        Role::System => CopilotChatRole::System,
326                    },
327                    content: msg.string_contents(),
328                })
329                .collect(),
330        )
331    }
332}
333
334struct ConfigurationView {
335    copilot_status: Option<copilot::Status>,
336    state: Entity<State>,
337    _subscription: Option<Subscription>,
338}
339
340impl ConfigurationView {
341    pub fn new(state: Entity<State>, cx: &mut Context<Self>) -> Self {
342        let copilot = Copilot::global(cx);
343
344        Self {
345            copilot_status: copilot.as_ref().map(|copilot| copilot.read(cx).status()),
346            state,
347            _subscription: copilot.as_ref().map(|copilot| {
348                cx.observe(copilot, |this, model, cx| {
349                    this.copilot_status = Some(model.read(cx).status());
350                    cx.notify();
351                })
352            }),
353        }
354    }
355}
356
357impl Render for ConfigurationView {
358    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
359        if self.state.read(cx).is_authenticated(cx) {
360            const LABEL: &str = "Authorized.";
361            h_flex()
362                .justify_between()
363                .child(
364                    h_flex()
365                        .gap_1()
366                        .child(Icon::new(IconName::Check).color(Color::Success))
367                        .child(Label::new(LABEL)),
368                )
369                .child(
370                    Button::new("sign_out", "Sign Out")
371                        .style(ui::ButtonStyle::Filled)
372                        .on_click(|_, window, cx| {
373                            window.dispatch_action(copilot::SignOut.boxed_clone(), cx);
374                        }),
375                )
376        } else {
377            let loading_icon = svg()
378                .size_8()
379                .path(IconName::ArrowCircle.path())
380                .text_color(window.text_style().color)
381                .with_animation(
382                    "icon_circle_arrow",
383                    Animation::new(Duration::from_secs(2)).repeat(),
384                    |svg, delta| svg.with_transformation(Transformation::rotate(percentage(delta))),
385                );
386
387            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.";
388
389            match &self.copilot_status {
390                Some(status) => match status {
391                    Status::Starting { task: _ } => {
392                        const LABEL: &str = "Starting Copilot...";
393                        v_flex()
394                            .gap_6()
395                            .justify_center()
396                            .items_center()
397                            .child(Label::new(LABEL))
398                            .child(loading_icon)
399                    }
400                    Status::SigningIn { prompt: _ }
401                    | Status::SignedOut {
402                        awaiting_signing_in: true,
403                    } => {
404                        const LABEL: &str = "Signing in to Copilot...";
405                        v_flex()
406                            .gap_6()
407                            .justify_center()
408                            .items_center()
409                            .child(Label::new(LABEL))
410                            .child(loading_icon)
411                    }
412                    Status::Error(_) => {
413                        const LABEL: &str = "Copilot had issues starting. Please try restarting it. If the issue persists, try reinstalling Copilot.";
414                        v_flex()
415                            .gap_6()
416                            .child(Label::new(LABEL))
417                            .child(svg().size_8().path(IconName::CopilotError.path()))
418                    }
419                    _ => {
420                        const LABEL: &str = "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.";
421                        v_flex().gap_6().child(Label::new(LABEL)).child(
422                            v_flex()
423                                .gap_2()
424                                .child(
425                                    Button::new("sign_in", "Sign In")
426                                        .icon_color(Color::Muted)
427                                        .icon(IconName::Github)
428                                        .icon_position(IconPosition::Start)
429                                        .icon_size(IconSize::Medium)
430                                        .style(ui::ButtonStyle::Filled)
431                                        .full_width()
432                                        .on_click(|_, window, cx| {
433                                            copilot::initiate_sign_in(window, cx)
434                                        }),
435                                )
436                                .child(
437                                    div().flex().w_full().items_center().child(
438                                        Label::new("Sign in to start using Github Copilot Chat.")
439                                            .color(Color::Muted)
440                                            .size(ui::LabelSize::Small),
441                                    ),
442                                ),
443                        )
444                    }
445                },
446                None => v_flex().gap_6().child(Label::new(ERROR_LABEL)),
447            }
448        }
449    }
450}