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, AppContext, AsyncAppContext, Model, Render,
 15    Subscription, 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::{
 26    div, h_flex, v_flex, Button, ButtonCommon, Clickable, Color, Context, FixedWidth, Icon,
 27    IconName, IconPosition, IconSize, IntoElement, Label, LabelCommon, ParentElement, Styled,
 28    ViewContext, VisualContext, WindowContext,
 29};
 30
 31use super::anthropic::count_anthropic_tokens;
 32use super::open_ai::count_open_ai_tokens;
 33
 34const PROVIDER_ID: &str = "copilot_chat";
 35const PROVIDER_NAME: &str = "GitHub Copilot Chat";
 36
 37#[derive(Default, Clone, Debug, PartialEq)]
 38pub struct CopilotChatSettings {}
 39
 40pub struct CopilotChatLanguageModelProvider {
 41    state: Model<State>,
 42}
 43
 44pub struct State {
 45    _copilot_chat_subscription: Option<Subscription>,
 46    _settings_subscription: Subscription,
 47}
 48
 49impl State {
 50    fn is_authenticated(&self, cx: &AppContext) -> bool {
 51        CopilotChat::global(cx)
 52            .map(|m| m.read(cx).is_authenticated())
 53            .unwrap_or(false)
 54    }
 55}
 56
 57impl CopilotChatLanguageModelProvider {
 58    pub fn new(cx: &mut AppContext) -> Self {
 59        let state = cx.new_model(|cx| {
 60            let _copilot_chat_subscription = CopilotChat::global(cx)
 61                .map(|copilot_chat| cx.observe(&copilot_chat, |_, _, cx| cx.notify()));
 62            State {
 63                _copilot_chat_subscription,
 64                _settings_subscription: cx.observe_global::<SettingsStore>(|_, cx| {
 65                    cx.notify();
 66                }),
 67            }
 68        });
 69
 70        Self { state }
 71    }
 72}
 73
 74impl LanguageModelProviderState for CopilotChatLanguageModelProvider {
 75    type ObservableEntity = State;
 76
 77    fn observable_entity(&self) -> Option<gpui::Model<Self::ObservableEntity>> {
 78        Some(self.state.clone())
 79    }
 80}
 81
 82impl LanguageModelProvider for CopilotChatLanguageModelProvider {
 83    fn id(&self) -> LanguageModelProviderId {
 84        LanguageModelProviderId(PROVIDER_ID.into())
 85    }
 86
 87    fn name(&self) -> LanguageModelProviderName {
 88        LanguageModelProviderName(PROVIDER_NAME.into())
 89    }
 90
 91    fn icon(&self) -> IconName {
 92        IconName::Copilot
 93    }
 94
 95    fn provided_models(&self, _cx: &AppContext) -> Vec<Arc<dyn LanguageModel>> {
 96        CopilotChatModel::iter()
 97            .map(|model| {
 98                Arc::new(CopilotChatLanguageModel {
 99                    model,
100                    request_limiter: RateLimiter::new(4),
101                }) as Arc<dyn LanguageModel>
102            })
103            .collect()
104    }
105
106    fn is_authenticated(&self, cx: &AppContext) -> bool {
107        self.state.read(cx).is_authenticated(cx)
108    }
109
110    fn authenticate(&self, cx: &mut AppContext) -> Task<Result<()>> {
111        let result = if self.is_authenticated(cx) {
112            Ok(())
113        } else if let Some(copilot) = Copilot::global(cx) {
114            let error_msg = match copilot.read(cx).status() {
115                Status::Disabled => anyhow::anyhow!("Copilot must be enabled for Copilot Chat to work. Please enable Copilot and try again."),
116                Status::Error(e) => anyhow::anyhow!(format!("Received the following error while signing into Copilot: {e}")),
117                Status::Starting { task: _ } => anyhow::anyhow!("Copilot is still starting, please wait for Copilot to start then try again"),
118                Status::Unauthorized => anyhow::anyhow!("Unable to authorize with Copilot. Please make sure that you have an active Copilot and Copilot Chat subscription."),
119                Status::Authorized => return Task::ready(Ok(())),
120                Status::SignedOut => anyhow::anyhow!("You have signed out of Copilot. Please sign in to Copilot and try again."),
121                Status::SigningIn { prompt: _ } => anyhow::anyhow!("Still signing into Copilot..."),
122            };
123            Err(error_msg)
124        } else {
125            Err(anyhow::anyhow!(
126                "Copilot must be enabled for Copilot Chat to work. Please enable Copilot and try again."
127            ))
128        };
129        Task::ready(result)
130    }
131
132    fn configuration_view(&self, cx: &mut WindowContext) -> AnyView {
133        let state = self.state.clone();
134        cx.new_view(|cx| ConfigurationView::new(state, cx)).into()
135    }
136
137    fn reset_credentials(&self, _cx: &mut AppContext) -> Task<Result<()>> {
138        Task::ready(Err(anyhow!(
139            "Signing out of GitHub Copilot Chat is currently not supported."
140        )))
141    }
142}
143
144pub struct CopilotChatLanguageModel {
145    model: CopilotChatModel,
146    request_limiter: RateLimiter,
147}
148
149impl LanguageModel for CopilotChatLanguageModel {
150    fn id(&self) -> LanguageModelId {
151        LanguageModelId::from(self.model.id().to_string())
152    }
153
154    fn name(&self) -> LanguageModelName {
155        LanguageModelName::from(self.model.display_name().to_string())
156    }
157
158    fn provider_id(&self) -> LanguageModelProviderId {
159        LanguageModelProviderId(PROVIDER_ID.into())
160    }
161
162    fn provider_name(&self) -> LanguageModelProviderName {
163        LanguageModelProviderName(PROVIDER_NAME.into())
164    }
165
166    fn telemetry_id(&self) -> String {
167        format!("copilot_chat/{}", self.model.id())
168    }
169
170    fn max_token_count(&self) -> usize {
171        self.model.max_token_count()
172    }
173
174    fn count_tokens(
175        &self,
176        request: LanguageModelRequest,
177        cx: &AppContext,
178    ) -> BoxFuture<'static, Result<usize>> {
179        match self.model {
180            CopilotChatModel::Claude3_5Sonnet => count_anthropic_tokens(request, cx),
181            _ => {
182                let model = match self.model {
183                    CopilotChatModel::Gpt4o => open_ai::Model::FourOmni,
184                    CopilotChatModel::Gpt4 => open_ai::Model::Four,
185                    CopilotChatModel::Gpt3_5Turbo => open_ai::Model::ThreePointFiveTurbo,
186                    CopilotChatModel::O1Preview | CopilotChatModel::O1Mini => open_ai::Model::Four,
187                    CopilotChatModel::Claude3_5Sonnet => unreachable!(),
188                };
189                count_open_ai_tokens(request, model, cx)
190            }
191        }
192    }
193
194    fn stream_completion(
195        &self,
196        request: LanguageModelRequest,
197        cx: &AsyncAppContext,
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: &AsyncAppContext,
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: Model<State>,
302    _subscription: Option<Subscription>,
303}
304
305impl ConfigurationView {
306    pub fn new(state: Model<State>, cx: &mut ViewContext<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, cx: &mut ViewContext<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(cx.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(|_, cx| copilot::initiate_sign_in(cx)),
386                                )
387                                .child(
388                                    div().flex().w_full().items_center().child(
389                                        Label::new("Sign in to start using Github Copilot Chat.")
390                                            .color(Color::Muted)
391                                            .size(ui::LabelSize::Small),
392                                    ),
393                                ),
394                        )
395                    }
396                },
397                None => v_flex().gap_6().child(Label::new(ERROR_LABEL)),
398            }
399        }
400    }
401}