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::Gemini20Flash => count_google_tokens(request, cx),
179            _ => {
180                let model = match self.model {
181                    CopilotChatModel::Gpt4o => open_ai::Model::FourOmni,
182                    CopilotChatModel::Gpt4 => open_ai::Model::Four,
183                    CopilotChatModel::Gpt3_5Turbo => open_ai::Model::ThreePointFiveTurbo,
184                    CopilotChatModel::O1 | CopilotChatModel::O3Mini => open_ai::Model::Four,
185                    CopilotChatModel::Claude3_5Sonnet | CopilotChatModel::Gemini20Flash => {
186                        unreachable!()
187                    }
188                };
189                count_open_ai_tokens(request, model, cx)
190            }
191        }
192    }
193
194    fn stream_completion(
195        &self,
196        request: LanguageModelRequest,
197        cx: &AsyncApp,
198    ) -> BoxFuture<'static, Result<BoxStream<'static, Result<LanguageModelCompletionEvent>>>> {
199        if let Some(message) = request.messages.last() {
200            if message.contents_empty() {
201                const EMPTY_PROMPT_MSG: &str =
202                    "Empty prompts aren't allowed. Please provide a non-empty prompt.";
203                return futures::future::ready(Err(anyhow::anyhow!(EMPTY_PROMPT_MSG))).boxed();
204            }
205
206            // Copilot Chat has a restriction that the final message must be from the user.
207            // While their API does return an error message for this, we can catch it earlier
208            // and provide a more helpful error message.
209            if !matches!(message.role, Role::User) {
210                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.";
211                return futures::future::ready(Err(anyhow::anyhow!(USER_ROLE_MSG))).boxed();
212            }
213        }
214
215        let copilot_request = self.to_copilot_chat_request(request);
216        let is_streaming = copilot_request.stream;
217
218        let request_limiter = self.request_limiter.clone();
219        let future = cx.spawn(|cx| async move {
220            let response = CopilotChat::stream_completion(copilot_request, cx);
221            request_limiter.stream(async move {
222                let response = response.await?;
223                let stream = response
224                    .filter_map(move |response| async move {
225                        match response {
226                            Ok(result) => {
227                                let choice = result.choices.first();
228                                match choice {
229                                    Some(choice) if !is_streaming => {
230                                        match &choice.message {
231                                            Some(msg) => Some(Ok(msg.content.clone().unwrap_or_default())),
232                                            None => Some(Err(anyhow::anyhow!(
233                                                "The Copilot Chat API returned a response with no message content"
234                                            ))),
235                                        }
236                                    },
237                                    Some(choice) => {
238                                        match &choice.delta {
239                                            Some(delta) => Some(Ok(delta.content.clone().unwrap_or_default())),
240                                            None => Some(Err(anyhow::anyhow!(
241                                                "The Copilot Chat API returned a response with no delta content"
242                                            ))),
243                                        }
244                                    },
245                                    None => Some(Err(anyhow::anyhow!(
246                                        "The Copilot Chat API returned a response with no choices, but hadn't finished the message yet. Please try again."
247                                    ))),
248                                }
249                            }
250                            Err(err) => Some(Err(err)),
251                        }
252                    })
253                    .boxed();
254                Ok(stream)
255            }).await
256        });
257
258        async move {
259            Ok(future
260                .await?
261                .map(|result| result.map(LanguageModelCompletionEvent::Text))
262                .boxed())
263        }
264        .boxed()
265    }
266
267    fn use_any_tool(
268        &self,
269        _request: LanguageModelRequest,
270        _name: String,
271        _description: String,
272        _schema: serde_json::Value,
273        _cx: &AsyncApp,
274    ) -> BoxFuture<'static, Result<BoxStream<'static, Result<String>>>> {
275        future::ready(Err(anyhow!("not implemented"))).boxed()
276    }
277}
278
279impl CopilotChatLanguageModel {
280    pub fn to_copilot_chat_request(&self, request: LanguageModelRequest) -> CopilotChatRequest {
281        CopilotChatRequest::new(
282            self.model.clone(),
283            request
284                .messages
285                .into_iter()
286                .map(|msg| ChatMessage {
287                    role: match msg.role {
288                        Role::User => CopilotChatRole::User,
289                        Role::Assistant => CopilotChatRole::Assistant,
290                        Role::System => CopilotChatRole::System,
291                    },
292                    content: msg.string_contents(),
293                })
294                .collect(),
295        )
296    }
297}
298
299struct ConfigurationView {
300    copilot_status: Option<copilot::Status>,
301    state: Entity<State>,
302    _subscription: Option<Subscription>,
303}
304
305impl ConfigurationView {
306    pub fn new(state: Entity<State>, cx: &mut Context<Self>) -> Self {
307        let copilot = Copilot::global(cx);
308
309        Self {
310            copilot_status: copilot.as_ref().map(|copilot| copilot.read(cx).status()),
311            state,
312            _subscription: copilot.as_ref().map(|copilot| {
313                cx.observe(copilot, |this, model, cx| {
314                    this.copilot_status = Some(model.read(cx).status());
315                    cx.notify();
316                })
317            }),
318        }
319    }
320}
321
322impl Render for ConfigurationView {
323    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
324        if self.state.read(cx).is_authenticated(cx) {
325            const LABEL: &str = "Authorized.";
326            h_flex()
327                .gap_1()
328                .child(Icon::new(IconName::Check).color(Color::Success))
329                .child(Label::new(LABEL))
330        } else {
331            let loading_icon = svg()
332                .size_8()
333                .path(IconName::ArrowCircle.path())
334                .text_color(window.text_style().color)
335                .with_animation(
336                    "icon_circle_arrow",
337                    Animation::new(Duration::from_secs(2)).repeat(),
338                    |svg, delta| svg.with_transformation(Transformation::rotate(percentage(delta))),
339                );
340
341            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.";
342
343            match &self.copilot_status {
344                Some(status) => match status {
345                    Status::Disabled => v_flex().gap_6().p_4().child(Label::new(ERROR_LABEL)),
346                    Status::Starting { task: _ } => {
347                        const LABEL: &str = "Starting Copilot...";
348                        v_flex()
349                            .gap_6()
350                            .justify_center()
351                            .items_center()
352                            .child(Label::new(LABEL))
353                            .child(loading_icon)
354                    }
355                    Status::SigningIn { prompt: _ } => {
356                        const LABEL: &str = "Signing in to Copilot...";
357                        v_flex()
358                            .gap_6()
359                            .justify_center()
360                            .items_center()
361                            .child(Label::new(LABEL))
362                            .child(loading_icon)
363                    }
364                    Status::Error(_) => {
365                        const LABEL: &str = "Copilot had issues starting. Please try restarting it. If the issue persists, try reinstalling Copilot.";
366                        v_flex()
367                            .gap_6()
368                            .child(Label::new(LABEL))
369                            .child(svg().size_8().path(IconName::CopilotError.path()))
370                    }
371                    _ => {
372                        const LABEL: &str =
373                    "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.";
374                        v_flex().gap_6().child(Label::new(LABEL)).child(
375                            v_flex()
376                                .gap_2()
377                                .child(
378                                    Button::new("sign_in", "Sign In")
379                                        .icon_color(Color::Muted)
380                                        .icon(IconName::Github)
381                                        .icon_position(IconPosition::Start)
382                                        .icon_size(IconSize::Medium)
383                                        .style(ui::ButtonStyle::Filled)
384                                        .full_width()
385                                        .on_click(|_, window, cx| {
386                                            copilot::initiate_sign_in(window, cx)
387                                        }),
388                                )
389                                .child(
390                                    div().flex().w_full().items_center().child(
391                                        Label::new("Sign in to start using Github Copilot Chat.")
392                                            .color(Color::Muted)
393                                            .size(ui::LabelSize::Small),
394                                    ),
395                                ),
396                        )
397                    }
398                },
399                None => v_flex().gap_6().child(Label::new(ERROR_LABEL)),
400            }
401        }
402    }
403}