copilot_chat.rs

  1use std::pin::Pin;
  2use std::str::FromStr as _;
  3use std::sync::Arc;
  4
  5use anyhow::{Result, anyhow};
  6use collections::HashMap;
  7use copilot::copilot_chat::{
  8    ChatMessage, CopilotChat, Model as CopilotChatModel, Request as CopilotChatRequest,
  9    ResponseEvent, Tool, ToolCall,
 10};
 11use copilot::{Copilot, Status};
 12use futures::future::BoxFuture;
 13use futures::stream::BoxStream;
 14use futures::{FutureExt, Stream, StreamExt};
 15use gpui::{
 16    Action, Animation, AnimationExt, AnyView, App, AsyncApp, Entity, Render, Subscription, Task,
 17    Transformation, percentage, svg,
 18};
 19use language_model::{
 20    AuthenticateError, LanguageModel, LanguageModelCompletionError, LanguageModelCompletionEvent,
 21    LanguageModelId, LanguageModelName, LanguageModelProvider, LanguageModelProviderId,
 22    LanguageModelProviderName, LanguageModelProviderState, LanguageModelRequest,
 23    LanguageModelRequestMessage, LanguageModelToolUse, MessageContent, RateLimiter, Role,
 24    StopReason,
 25};
 26use settings::SettingsStore;
 27use std::time::Duration;
 28use strum::IntoEnumIterator;
 29use ui::prelude::*;
 30
 31use super::anthropic::count_anthropic_tokens;
 32use super::google::count_google_tokens;
 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
 41pub struct CopilotChatLanguageModelProvider {
 42    state: Entity<State>,
 43}
 44
 45pub struct State {
 46    _copilot_chat_subscription: Option<Subscription>,
 47    _settings_subscription: Subscription,
 48}
 49
 50impl State {
 51    fn is_authenticated(&self, cx: &App) -> bool {
 52        CopilotChat::global(cx)
 53            .map(|m| m.read(cx).is_authenticated())
 54            .unwrap_or(false)
 55    }
 56}
 57
 58impl CopilotChatLanguageModelProvider {
 59    pub fn new(cx: &mut App) -> Self {
 60        let state = cx.new(|cx| {
 61            let _copilot_chat_subscription = CopilotChat::global(cx)
 62                .map(|copilot_chat| cx.observe(&copilot_chat, |_, _, cx| cx.notify()));
 63            State {
 64                _copilot_chat_subscription,
 65                _settings_subscription: cx.observe_global::<SettingsStore>(|_, cx| {
 66                    cx.notify();
 67                }),
 68            }
 69        });
 70
 71        Self { state }
 72    }
 73
 74    fn create_language_model(&self, model: CopilotChatModel) -> Arc<dyn LanguageModel> {
 75        Arc::new(CopilotChatLanguageModel {
 76            model,
 77            request_limiter: RateLimiter::new(4),
 78        })
 79    }
 80}
 81
 82impl LanguageModelProviderState for CopilotChatLanguageModelProvider {
 83    type ObservableEntity = State;
 84
 85    fn observable_entity(&self) -> Option<gpui::Entity<Self::ObservableEntity>> {
 86        Some(self.state.clone())
 87    }
 88}
 89
 90impl LanguageModelProvider for CopilotChatLanguageModelProvider {
 91    fn id(&self) -> LanguageModelProviderId {
 92        LanguageModelProviderId(PROVIDER_ID.into())
 93    }
 94
 95    fn name(&self) -> LanguageModelProviderName {
 96        LanguageModelProviderName(PROVIDER_NAME.into())
 97    }
 98
 99    fn icon(&self) -> IconName {
100        IconName::Copilot
101    }
102
103    fn default_model(&self, _cx: &App) -> Option<Arc<dyn LanguageModel>> {
104        Some(self.create_language_model(CopilotChatModel::default()))
105    }
106
107    fn default_fast_model(&self, _cx: &App) -> Option<Arc<dyn LanguageModel>> {
108        Some(self.create_language_model(CopilotChatModel::default_fast()))
109    }
110
111    fn provided_models(&self, _cx: &App) -> Vec<Arc<dyn LanguageModel>> {
112        CopilotChatModel::iter()
113            .map(|model| self.create_language_model(model))
114            .collect()
115    }
116
117    fn is_authenticated(&self, cx: &App) -> bool {
118        self.state.read(cx).is_authenticated(cx)
119    }
120
121    fn authenticate(&self, cx: &mut App) -> Task<Result<(), AuthenticateError>> {
122        if self.is_authenticated(cx) {
123            return Task::ready(Ok(()));
124        };
125
126        let Some(copilot) = Copilot::global(cx) else {
127            return Task::ready( Err(anyhow!(
128                "Copilot must be enabled for Copilot Chat to work. Please enable Copilot and try again."
129            ).into()));
130        };
131
132        let err = match copilot.read(cx).status() {
133            Status::Authorized => return Task::ready(Ok(())),
134            Status::Disabled => anyhow!(
135                "Copilot must be enabled for Copilot Chat to work. Please enable Copilot and try again."
136            ),
137            Status::Error(err) => anyhow!(format!(
138                "Received the following error while signing into Copilot: {err}"
139            )),
140            Status::Starting { task: _ } => anyhow!(
141                "Copilot is still starting, please wait for Copilot to start then try again"
142            ),
143            Status::Unauthorized => anyhow!(
144                "Unable to authorize with Copilot. Please make sure that you have an active Copilot and Copilot Chat subscription."
145            ),
146            Status::SignedOut { .. } => {
147                anyhow!("You have signed out of Copilot. Please sign in to Copilot and try again.")
148            }
149            Status::SigningIn { prompt: _ } => anyhow!("Still signing into Copilot..."),
150        };
151
152        Task::ready(Err(err.into()))
153    }
154
155    fn configuration_view(&self, _: &mut Window, cx: &mut App) -> AnyView {
156        let state = self.state.clone();
157        cx.new(|cx| ConfigurationView::new(state, cx)).into()
158    }
159
160    fn reset_credentials(&self, _cx: &mut App) -> Task<Result<()>> {
161        Task::ready(Err(anyhow!(
162            "Signing out of GitHub Copilot Chat is currently not supported."
163        )))
164    }
165}
166
167pub struct CopilotChatLanguageModel {
168    model: CopilotChatModel,
169    request_limiter: RateLimiter,
170}
171
172impl LanguageModel for CopilotChatLanguageModel {
173    fn id(&self) -> LanguageModelId {
174        LanguageModelId::from(self.model.id().to_string())
175    }
176
177    fn name(&self) -> LanguageModelName {
178        LanguageModelName::from(self.model.display_name().to_string())
179    }
180
181    fn provider_id(&self) -> LanguageModelProviderId {
182        LanguageModelProviderId(PROVIDER_ID.into())
183    }
184
185    fn provider_name(&self) -> LanguageModelProviderName {
186        LanguageModelProviderName(PROVIDER_NAME.into())
187    }
188
189    fn supports_tools(&self) -> bool {
190        match self.model {
191            CopilotChatModel::Gpt4o
192            | CopilotChatModel::Gpt4_1
193            | CopilotChatModel::O4Mini
194            | CopilotChatModel::Claude3_5Sonnet
195            | CopilotChatModel::Claude3_7Sonnet
196            | CopilotChatModel::Claude3_7SonnetThinking => true,
197            _ => false,
198        }
199    }
200
201    fn telemetry_id(&self) -> String {
202        format!("copilot_chat/{}", self.model.id())
203    }
204
205    fn max_token_count(&self) -> usize {
206        self.model.max_token_count()
207    }
208
209    fn count_tokens(
210        &self,
211        request: LanguageModelRequest,
212        cx: &App,
213    ) -> BoxFuture<'static, Result<usize>> {
214        match self.model {
215            CopilotChatModel::Claude3_5Sonnet => count_anthropic_tokens(request, cx),
216            CopilotChatModel::Claude3_7Sonnet => count_anthropic_tokens(request, cx),
217            CopilotChatModel::Claude3_7SonnetThinking => count_anthropic_tokens(request, cx),
218            CopilotChatModel::Gemini20Flash | CopilotChatModel::Gemini25Pro => {
219                count_google_tokens(request, cx)
220            }
221            _ => {
222                let model = match self.model {
223                    CopilotChatModel::Gpt4o => open_ai::Model::FourOmni,
224                    CopilotChatModel::Gpt4 => open_ai::Model::Four,
225                    CopilotChatModel::Gpt4_1 => open_ai::Model::FourPointOne,
226                    CopilotChatModel::Gpt3_5Turbo => open_ai::Model::ThreePointFiveTurbo,
227                    CopilotChatModel::O1 => open_ai::Model::O1,
228                    CopilotChatModel::O3Mini => open_ai::Model::O3Mini,
229                    CopilotChatModel::O3 => open_ai::Model::O3,
230                    CopilotChatModel::O4Mini => open_ai::Model::O4Mini,
231                    CopilotChatModel::Claude3_5Sonnet
232                    | CopilotChatModel::Claude3_7Sonnet
233                    | CopilotChatModel::Claude3_7SonnetThinking
234                    | CopilotChatModel::Gemini20Flash
235                    | CopilotChatModel::Gemini25Pro => {
236                        unreachable!()
237                    }
238                };
239                count_open_ai_tokens(request, model, cx)
240            }
241        }
242    }
243
244    fn stream_completion(
245        &self,
246        request: LanguageModelRequest,
247        cx: &AsyncApp,
248    ) -> BoxFuture<
249        'static,
250        Result<
251            BoxStream<'static, Result<LanguageModelCompletionEvent, LanguageModelCompletionError>>,
252        >,
253    > {
254        if let Some(message) = request.messages.last() {
255            if message.contents_empty() {
256                const EMPTY_PROMPT_MSG: &str =
257                    "Empty prompts aren't allowed. Please provide a non-empty prompt.";
258                return futures::future::ready(Err(anyhow::anyhow!(EMPTY_PROMPT_MSG))).boxed();
259            }
260
261            // Copilot Chat has a restriction that the final message must be from the user.
262            // While their API does return an error message for this, we can catch it earlier
263            // and provide a more helpful error message.
264            if !matches!(message.role, Role::User) {
265                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.";
266                return futures::future::ready(Err(anyhow::anyhow!(USER_ROLE_MSG))).boxed();
267            }
268        }
269
270        let copilot_request = match self.to_copilot_chat_request(request) {
271            Ok(request) => request,
272            Err(err) => return futures::future::ready(Err(err)).boxed(),
273        };
274        let is_streaming = copilot_request.stream;
275
276        let request_limiter = self.request_limiter.clone();
277        let future = cx.spawn(async move |cx| {
278            let request = CopilotChat::stream_completion(copilot_request, cx.clone());
279            request_limiter
280                .stream(async move {
281                    let response = request.await?;
282                    Ok(map_to_language_model_completion_events(
283                        response,
284                        is_streaming,
285                    ))
286                })
287                .await
288        });
289        async move { Ok(future.await?.boxed()) }.boxed()
290    }
291}
292
293pub fn map_to_language_model_completion_events(
294    events: Pin<Box<dyn Send + Stream<Item = Result<ResponseEvent>>>>,
295    is_streaming: bool,
296) -> impl Stream<Item = Result<LanguageModelCompletionEvent, LanguageModelCompletionError>> {
297    #[derive(Default)]
298    struct RawToolCall {
299        id: String,
300        name: String,
301        arguments: String,
302    }
303
304    struct State {
305        events: Pin<Box<dyn Send + Stream<Item = Result<ResponseEvent>>>>,
306        tool_calls_by_index: HashMap<usize, RawToolCall>,
307    }
308
309    futures::stream::unfold(
310        State {
311            events,
312            tool_calls_by_index: HashMap::default(),
313        },
314        move |mut state| async move {
315            if let Some(event) = state.events.next().await {
316                match event {
317                    Ok(event) => {
318                        let Some(choice) = event.choices.first() else {
319                            return Some((
320                                vec![Err(anyhow!("Response contained no choices").into())],
321                                state,
322                            ));
323                        };
324
325                        let delta = if is_streaming {
326                            choice.delta.as_ref()
327                        } else {
328                            choice.message.as_ref()
329                        };
330
331                        let Some(delta) = delta else {
332                            return Some((
333                                vec![Err(anyhow!("Response contained no delta").into())],
334                                state,
335                            ));
336                        };
337
338                        let mut events = Vec::new();
339                        if let Some(content) = delta.content.clone() {
340                            events.push(Ok(LanguageModelCompletionEvent::Text(content)));
341                        }
342
343                        for tool_call in &delta.tool_calls {
344                            let entry = state
345                                .tool_calls_by_index
346                                .entry(tool_call.index)
347                                .or_default();
348
349                            if let Some(tool_id) = tool_call.id.clone() {
350                                entry.id = tool_id;
351                            }
352
353                            if let Some(function) = tool_call.function.as_ref() {
354                                if let Some(name) = function.name.clone() {
355                                    entry.name = name;
356                                }
357
358                                if let Some(arguments) = function.arguments.clone() {
359                                    entry.arguments.push_str(&arguments);
360                                }
361                            }
362                        }
363
364                        match choice.finish_reason.as_deref() {
365                            Some("stop") => {
366                                events.push(Ok(LanguageModelCompletionEvent::Stop(
367                                    StopReason::EndTurn,
368                                )));
369                            }
370                            Some("tool_calls") => {
371                                events.extend(state.tool_calls_by_index.drain().map(
372                                    |(_, tool_call)| match serde_json::Value::from_str(
373                                        &tool_call.arguments,
374                                    ) {
375                                        Ok(input) => Ok(LanguageModelCompletionEvent::ToolUse(
376                                            LanguageModelToolUse {
377                                                id: tool_call.id.clone().into(),
378                                                name: tool_call.name.as_str().into(),
379                                                is_input_complete: true,
380                                                input,
381                                                raw_input: tool_call.arguments.clone(),
382                                            },
383                                        )),
384                                        Err(error) => {
385                                            Err(LanguageModelCompletionError::BadInputJson {
386                                                id: tool_call.id.into(),
387                                                tool_name: tool_call.name.as_str().into(),
388                                                raw_input: tool_call.arguments.into(),
389                                                json_parse_error: error.to_string(),
390                                            })
391                                        }
392                                    },
393                                ));
394
395                                events.push(Ok(LanguageModelCompletionEvent::Stop(
396                                    StopReason::ToolUse,
397                                )));
398                            }
399                            Some(stop_reason) => {
400                                log::error!("Unexpected Copilot Chat stop_reason: {stop_reason:?}");
401                                events.push(Ok(LanguageModelCompletionEvent::Stop(
402                                    StopReason::EndTurn,
403                                )));
404                            }
405                            None => {}
406                        }
407
408                        return Some((events, state));
409                    }
410                    Err(err) => return Some((vec![Err(anyhow!(err).into())], state)),
411                }
412            }
413
414            None
415        },
416    )
417    .flat_map(futures::stream::iter)
418}
419
420impl CopilotChatLanguageModel {
421    pub fn to_copilot_chat_request(
422        &self,
423        request: LanguageModelRequest,
424    ) -> Result<CopilotChatRequest> {
425        let model = self.model.clone();
426
427        let mut request_messages: Vec<LanguageModelRequestMessage> = Vec::new();
428        for message in request.messages {
429            if let Some(last_message) = request_messages.last_mut() {
430                if last_message.role == message.role {
431                    last_message.content.extend(message.content);
432                } else {
433                    request_messages.push(message);
434                }
435            } else {
436                request_messages.push(message);
437            }
438        }
439
440        let mut messages: Vec<ChatMessage> = Vec::new();
441        for message in request_messages {
442            let text_content = {
443                let mut buffer = String::new();
444                for string in message.content.iter().filter_map(|content| match content {
445                    MessageContent::Text(text) | MessageContent::Thinking { text, .. } => {
446                        Some(text.as_str())
447                    }
448                    MessageContent::ToolUse(_)
449                    | MessageContent::RedactedThinking(_)
450                    | MessageContent::ToolResult(_)
451                    | MessageContent::Image(_) => None,
452                }) {
453                    buffer.push_str(string);
454                }
455
456                buffer
457            };
458
459            match message.role {
460                Role::User => {
461                    for content in &message.content {
462                        if let MessageContent::ToolResult(tool_result) = content {
463                            messages.push(ChatMessage::Tool {
464                                tool_call_id: tool_result.tool_use_id.to_string(),
465                                content: tool_result.content.to_string(),
466                            });
467                        }
468                    }
469
470                    if !text_content.is_empty() {
471                        messages.push(ChatMessage::User {
472                            content: text_content,
473                        });
474                    }
475                }
476                Role::Assistant => {
477                    let mut tool_calls = Vec::new();
478                    for content in &message.content {
479                        if let MessageContent::ToolUse(tool_use) = content {
480                            tool_calls.push(ToolCall {
481                                id: tool_use.id.to_string(),
482                                content: copilot::copilot_chat::ToolCallContent::Function {
483                                    function: copilot::copilot_chat::FunctionContent {
484                                        name: tool_use.name.to_string(),
485                                        arguments: serde_json::to_string(&tool_use.input)?,
486                                    },
487                                },
488                            });
489                        }
490                    }
491
492                    messages.push(ChatMessage::Assistant {
493                        content: if text_content.is_empty() {
494                            None
495                        } else {
496                            Some(text_content)
497                        },
498                        tool_calls,
499                    });
500                }
501                Role::System => messages.push(ChatMessage::System {
502                    content: message.string_contents(),
503                }),
504            }
505        }
506
507        let tools = request
508            .tools
509            .iter()
510            .map(|tool| Tool::Function {
511                function: copilot::copilot_chat::Function {
512                    name: tool.name.clone(),
513                    description: tool.description.clone(),
514                    parameters: tool.input_schema.clone(),
515                },
516            })
517            .collect();
518
519        Ok(CopilotChatRequest {
520            intent: true,
521            n: 1,
522            stream: model.uses_streaming(),
523            temperature: 0.1,
524            model,
525            messages,
526            tools,
527            tool_choice: None,
528        })
529    }
530}
531
532struct ConfigurationView {
533    copilot_status: Option<copilot::Status>,
534    state: Entity<State>,
535    _subscription: Option<Subscription>,
536}
537
538impl ConfigurationView {
539    pub fn new(state: Entity<State>, cx: &mut Context<Self>) -> Self {
540        let copilot = Copilot::global(cx);
541
542        Self {
543            copilot_status: copilot.as_ref().map(|copilot| copilot.read(cx).status()),
544            state,
545            _subscription: copilot.as_ref().map(|copilot| {
546                cx.observe(copilot, |this, model, cx| {
547                    this.copilot_status = Some(model.read(cx).status());
548                    cx.notify();
549                })
550            }),
551        }
552    }
553}
554
555impl Render for ConfigurationView {
556    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
557        if self.state.read(cx).is_authenticated(cx) {
558            h_flex()
559                .mt_1()
560                .p_1()
561                .justify_between()
562                .rounded_md()
563                .border_1()
564                .border_color(cx.theme().colors().border)
565                .bg(cx.theme().colors().background)
566                .child(
567                    h_flex()
568                        .gap_1()
569                        .child(Icon::new(IconName::Check).color(Color::Success))
570                        .child(Label::new("Authorized")),
571                )
572                .child(
573                    Button::new("sign_out", "Sign Out")
574                        .label_size(LabelSize::Small)
575                        .on_click(|_, window, cx| {
576                            window.dispatch_action(copilot::SignOut.boxed_clone(), cx);
577                        }),
578                )
579        } else {
580            let loading_icon = Icon::new(IconName::ArrowCircle).with_animation(
581                "arrow-circle",
582                Animation::new(Duration::from_secs(4)).repeat(),
583                |icon, delta| icon.transform(Transformation::rotate(percentage(delta))),
584            );
585
586            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.";
587
588            match &self.copilot_status {
589                Some(status) => match status {
590                    Status::Starting { task: _ } => h_flex()
591                        .gap_2()
592                        .child(loading_icon)
593                        .child(Label::new("Starting Copilot…")),
594                    Status::SigningIn { prompt: _ }
595                    | Status::SignedOut {
596                        awaiting_signing_in: true,
597                    } => h_flex()
598                        .gap_2()
599                        .child(loading_icon)
600                        .child(Label::new("Signing into Copilot…")),
601                    Status::Error(_) => {
602                        const LABEL: &str = "Copilot had issues starting. Please try restarting it. If the issue persists, try reinstalling Copilot.";
603                        v_flex()
604                            .gap_6()
605                            .child(Label::new(LABEL))
606                            .child(svg().size_8().path(IconName::CopilotError.path()))
607                    }
608                    _ => {
609                        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.";
610                        v_flex().gap_2().child(Label::new(LABEL)).child(
611                            Button::new("sign_in", "Sign in to use GitHub Copilot")
612                                .icon_color(Color::Muted)
613                                .icon(IconName::Github)
614                                .icon_position(IconPosition::Start)
615                                .icon_size(IconSize::Medium)
616                                .full_width()
617                                .on_click(|_, window, cx| copilot::initiate_sign_in(window, cx)),
618                        )
619                    }
620                },
621                None => v_flex().gap_6().child(Label::new(ERROR_LABEL)),
622            }
623        }
624    }
625}