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