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, LanguageModelToolChoice, LanguageModelToolUse, MessageContent,
 24    RateLimiter, Role, 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 => true,
196            _ => false,
197        }
198    }
199
200    fn supports_tool_choice(&self, choice: LanguageModelToolChoice) -> bool {
201        match choice {
202            LanguageModelToolChoice::Auto
203            | LanguageModelToolChoice::Any
204            | LanguageModelToolChoice::None => self.supports_tools(),
205        }
206    }
207
208    fn telemetry_id(&self) -> String {
209        format!("copilot_chat/{}", self.model.id())
210    }
211
212    fn max_token_count(&self) -> usize {
213        self.model.max_token_count()
214    }
215
216    fn count_tokens(
217        &self,
218        request: LanguageModelRequest,
219        cx: &App,
220    ) -> BoxFuture<'static, Result<usize>> {
221        match self.model {
222            CopilotChatModel::Claude3_5Sonnet
223            | CopilotChatModel::Claude3_7Sonnet
224            | CopilotChatModel::Claude3_7SonnetThinking => count_anthropic_tokens(request, cx),
225            CopilotChatModel::Gemini20Flash | CopilotChatModel::Gemini25Pro => {
226                count_google_tokens(request, cx)
227            }
228            CopilotChatModel::Gpt4o => count_open_ai_tokens(request, open_ai::Model::FourOmni, cx),
229            CopilotChatModel::Gpt4 => count_open_ai_tokens(request, open_ai::Model::Four, cx),
230            CopilotChatModel::Gpt4_1 => {
231                count_open_ai_tokens(request, open_ai::Model::FourPointOne, cx)
232            }
233            CopilotChatModel::Gpt3_5Turbo => {
234                count_open_ai_tokens(request, open_ai::Model::ThreePointFiveTurbo, cx)
235            }
236            CopilotChatModel::O1 => count_open_ai_tokens(request, open_ai::Model::O1, cx),
237            CopilotChatModel::O3Mini => count_open_ai_tokens(request, open_ai::Model::O3Mini, cx),
238            CopilotChatModel::O3 => count_open_ai_tokens(request, open_ai::Model::O3, cx),
239            CopilotChatModel::O4Mini => count_open_ai_tokens(request, open_ai::Model::O4Mini, cx),
240        }
241    }
242
243    fn stream_completion(
244        &self,
245        request: LanguageModelRequest,
246        cx: &AsyncApp,
247    ) -> BoxFuture<
248        'static,
249        Result<
250            BoxStream<'static, Result<LanguageModelCompletionEvent, LanguageModelCompletionError>>,
251        >,
252    > {
253        if let Some(message) = request.messages.last() {
254            if message.contents_empty() {
255                const EMPTY_PROMPT_MSG: &str =
256                    "Empty prompts aren't allowed. Please provide a non-empty prompt.";
257                return futures::future::ready(Err(anyhow::anyhow!(EMPTY_PROMPT_MSG))).boxed();
258            }
259
260            // Copilot Chat has a restriction that the final message must be from the user.
261            // While their API does return an error message for this, we can catch it earlier
262            // and provide a more helpful error message.
263            if !matches!(message.role, Role::User) {
264                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.";
265                return futures::future::ready(Err(anyhow::anyhow!(USER_ROLE_MSG))).boxed();
266            }
267        }
268
269        let copilot_request = match self.to_copilot_chat_request(request) {
270            Ok(request) => request,
271            Err(err) => return futures::future::ready(Err(err)).boxed(),
272        };
273        let is_streaming = copilot_request.stream;
274
275        let request_limiter = self.request_limiter.clone();
276        let future = cx.spawn(async move |cx| {
277            let request = CopilotChat::stream_completion(copilot_request, cx.clone());
278            request_limiter
279                .stream(async move {
280                    let response = request.await?;
281                    Ok(map_to_language_model_completion_events(
282                        response,
283                        is_streaming,
284                    ))
285                })
286                .await
287        });
288        async move { Ok(future.await?.boxed()) }.boxed()
289    }
290}
291
292pub fn map_to_language_model_completion_events(
293    events: Pin<Box<dyn Send + Stream<Item = Result<ResponseEvent>>>>,
294    is_streaming: bool,
295) -> impl Stream<Item = Result<LanguageModelCompletionEvent, LanguageModelCompletionError>> {
296    #[derive(Default)]
297    struct RawToolCall {
298        id: String,
299        name: String,
300        arguments: String,
301    }
302
303    struct State {
304        events: Pin<Box<dyn Send + Stream<Item = Result<ResponseEvent>>>>,
305        tool_calls_by_index: HashMap<usize, RawToolCall>,
306    }
307
308    futures::stream::unfold(
309        State {
310            events,
311            tool_calls_by_index: HashMap::default(),
312        },
313        move |mut state| async move {
314            if let Some(event) = state.events.next().await {
315                match event {
316                    Ok(event) => {
317                        let Some(choice) = event.choices.first() else {
318                            return Some((
319                                vec![Err(anyhow!("Response contained no choices").into())],
320                                state,
321                            ));
322                        };
323
324                        let delta = if is_streaming {
325                            choice.delta.as_ref()
326                        } else {
327                            choice.message.as_ref()
328                        };
329
330                        let Some(delta) = delta else {
331                            return Some((
332                                vec![Err(anyhow!("Response contained no delta").into())],
333                                state,
334                            ));
335                        };
336
337                        let mut events = Vec::new();
338                        if let Some(content) = delta.content.clone() {
339                            events.push(Ok(LanguageModelCompletionEvent::Text(content)));
340                        }
341
342                        for tool_call in &delta.tool_calls {
343                            let entry = state
344                                .tool_calls_by_index
345                                .entry(tool_call.index)
346                                .or_default();
347
348                            if let Some(tool_id) = tool_call.id.clone() {
349                                entry.id = tool_id;
350                            }
351
352                            if let Some(function) = tool_call.function.as_ref() {
353                                if let Some(name) = function.name.clone() {
354                                    entry.name = name;
355                                }
356
357                                if let Some(arguments) = function.arguments.clone() {
358                                    entry.arguments.push_str(&arguments);
359                                }
360                            }
361                        }
362
363                        match choice.finish_reason.as_deref() {
364                            Some("stop") => {
365                                events.push(Ok(LanguageModelCompletionEvent::Stop(
366                                    StopReason::EndTurn,
367                                )));
368                            }
369                            Some("tool_calls") => {
370                                events.extend(state.tool_calls_by_index.drain().map(
371                                    |(_, tool_call)| {
372                                        // The model can output an empty string
373                                        // to indicate the absence of arguments.
374                                        // When that happens, create an empty
375                                        // object instead.
376                                        let arguments = if tool_call.arguments.is_empty() {
377                                            Ok(serde_json::Value::Object(Default::default()))
378                                        } else {
379                                            serde_json::Value::from_str(&tool_call.arguments)
380                                        };
381                                        match arguments {
382                                            Ok(input) => Ok(LanguageModelCompletionEvent::ToolUse(
383                                                LanguageModelToolUse {
384                                                    id: tool_call.id.clone().into(),
385                                                    name: tool_call.name.as_str().into(),
386                                                    is_input_complete: true,
387                                                    input,
388                                                    raw_input: tool_call.arguments.clone(),
389                                                },
390                                            )),
391                                            Err(error) => {
392                                                Err(LanguageModelCompletionError::BadInputJson {
393                                                    id: tool_call.id.into(),
394                                                    tool_name: tool_call.name.as_str().into(),
395                                                    raw_input: tool_call.arguments.into(),
396                                                    json_parse_error: error.to_string(),
397                                                })
398                                            }
399                                        }
400                                    },
401                                ));
402
403                                events.push(Ok(LanguageModelCompletionEvent::Stop(
404                                    StopReason::ToolUse,
405                                )));
406                            }
407                            Some(stop_reason) => {
408                                log::error!("Unexpected Copilot Chat stop_reason: {stop_reason:?}");
409                                events.push(Ok(LanguageModelCompletionEvent::Stop(
410                                    StopReason::EndTurn,
411                                )));
412                            }
413                            None => {}
414                        }
415
416                        return Some((events, state));
417                    }
418                    Err(err) => return Some((vec![Err(anyhow!(err).into())], state)),
419                }
420            }
421
422            None
423        },
424    )
425    .flat_map(futures::stream::iter)
426}
427
428impl CopilotChatLanguageModel {
429    pub fn to_copilot_chat_request(
430        &self,
431        request: LanguageModelRequest,
432    ) -> Result<CopilotChatRequest> {
433        let model = self.model.clone();
434
435        let mut request_messages: Vec<LanguageModelRequestMessage> = Vec::new();
436        for message in request.messages {
437            if let Some(last_message) = request_messages.last_mut() {
438                if last_message.role == message.role {
439                    last_message.content.extend(message.content);
440                } else {
441                    request_messages.push(message);
442                }
443            } else {
444                request_messages.push(message);
445            }
446        }
447
448        let mut tool_called = false;
449        let mut messages: Vec<ChatMessage> = Vec::new();
450        for message in request_messages {
451            let text_content = {
452                let mut buffer = String::new();
453                for string in message.content.iter().filter_map(|content| match content {
454                    MessageContent::Text(text) | MessageContent::Thinking { text, .. } => {
455                        Some(text.as_str())
456                    }
457                    MessageContent::ToolUse(_)
458                    | MessageContent::RedactedThinking(_)
459                    | MessageContent::ToolResult(_)
460                    | MessageContent::Image(_) => None,
461                }) {
462                    buffer.push_str(string);
463                }
464
465                buffer
466            };
467
468            match message.role {
469                Role::User => {
470                    for content in &message.content {
471                        if let MessageContent::ToolResult(tool_result) = content {
472                            messages.push(ChatMessage::Tool {
473                                tool_call_id: tool_result.tool_use_id.to_string(),
474                                content: tool_result.content.to_string(),
475                            });
476                        }
477                    }
478
479                    if !text_content.is_empty() {
480                        messages.push(ChatMessage::User {
481                            content: text_content,
482                        });
483                    }
484                }
485                Role::Assistant => {
486                    let mut tool_calls = Vec::new();
487                    for content in &message.content {
488                        if let MessageContent::ToolUse(tool_use) = content {
489                            tool_called = true;
490                            tool_calls.push(ToolCall {
491                                id: tool_use.id.to_string(),
492                                content: copilot::copilot_chat::ToolCallContent::Function {
493                                    function: copilot::copilot_chat::FunctionContent {
494                                        name: tool_use.name.to_string(),
495                                        arguments: serde_json::to_string(&tool_use.input)?,
496                                    },
497                                },
498                            });
499                        }
500                    }
501
502                    messages.push(ChatMessage::Assistant {
503                        content: if text_content.is_empty() {
504                            None
505                        } else {
506                            Some(text_content)
507                        },
508                        tool_calls,
509                    });
510                }
511                Role::System => messages.push(ChatMessage::System {
512                    content: message.string_contents(),
513                }),
514            }
515        }
516
517        let mut tools = request
518            .tools
519            .iter()
520            .map(|tool| Tool::Function {
521                function: copilot::copilot_chat::Function {
522                    name: tool.name.clone(),
523                    description: tool.description.clone(),
524                    parameters: tool.input_schema.clone(),
525                },
526            })
527            .collect::<Vec<_>>();
528
529        // The API will return a Bad Request (with no error message) when tools
530        // were used previously in the conversation but no tools are provided as
531        // part of this request. Inserting a dummy tool seems to circumvent this
532        // error.
533        if tool_called && tools.is_empty() {
534            tools.push(Tool::Function {
535                function: copilot::copilot_chat::Function {
536                    name: "noop".to_string(),
537                    description: "No operation".to_string(),
538                    parameters: serde_json::json!({
539                        "type": "object"
540                    }),
541                },
542            });
543        }
544
545        Ok(CopilotChatRequest {
546            intent: true,
547            n: 1,
548            stream: model.uses_streaming(),
549            temperature: 0.1,
550            model,
551            messages,
552            tools,
553            tool_choice: request.tool_choice.map(|choice| match choice {
554                LanguageModelToolChoice::Auto => copilot::copilot_chat::ToolChoice::Auto,
555                LanguageModelToolChoice::Any => copilot::copilot_chat::ToolChoice::Any,
556                LanguageModelToolChoice::None => copilot::copilot_chat::ToolChoice::None,
557            }),
558        })
559    }
560}
561
562struct ConfigurationView {
563    copilot_status: Option<copilot::Status>,
564    state: Entity<State>,
565    _subscription: Option<Subscription>,
566}
567
568impl ConfigurationView {
569    pub fn new(state: Entity<State>, cx: &mut Context<Self>) -> Self {
570        let copilot = Copilot::global(cx);
571
572        Self {
573            copilot_status: copilot.as_ref().map(|copilot| copilot.read(cx).status()),
574            state,
575            _subscription: copilot.as_ref().map(|copilot| {
576                cx.observe(copilot, |this, model, cx| {
577                    this.copilot_status = Some(model.read(cx).status());
578                    cx.notify();
579                })
580            }),
581        }
582    }
583}
584
585impl Render for ConfigurationView {
586    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
587        if self.state.read(cx).is_authenticated(cx) {
588            h_flex()
589                .mt_1()
590                .p_1()
591                .justify_between()
592                .rounded_md()
593                .border_1()
594                .border_color(cx.theme().colors().border)
595                .bg(cx.theme().colors().background)
596                .child(
597                    h_flex()
598                        .gap_1()
599                        .child(Icon::new(IconName::Check).color(Color::Success))
600                        .child(Label::new("Authorized")),
601                )
602                .child(
603                    Button::new("sign_out", "Sign Out")
604                        .label_size(LabelSize::Small)
605                        .on_click(|_, window, cx| {
606                            window.dispatch_action(copilot::SignOut.boxed_clone(), cx);
607                        }),
608                )
609        } else {
610            let loading_icon = Icon::new(IconName::ArrowCircle).with_animation(
611                "arrow-circle",
612                Animation::new(Duration::from_secs(4)).repeat(),
613                |icon, delta| icon.transform(Transformation::rotate(percentage(delta))),
614            );
615
616            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.";
617
618            match &self.copilot_status {
619                Some(status) => match status {
620                    Status::Starting { task: _ } => h_flex()
621                        .gap_2()
622                        .child(loading_icon)
623                        .child(Label::new("Starting Copilot…")),
624                    Status::SigningIn { prompt: _ }
625                    | Status::SignedOut {
626                        awaiting_signing_in: true,
627                    } => h_flex()
628                        .gap_2()
629                        .child(loading_icon)
630                        .child(Label::new("Signing into Copilot…")),
631                    Status::Error(_) => {
632                        const LABEL: &str = "Copilot had issues starting. Please try restarting it. If the issue persists, try reinstalling Copilot.";
633                        v_flex()
634                            .gap_6()
635                            .child(Label::new(LABEL))
636                            .child(svg().size_8().path(IconName::CopilotError.path()))
637                    }
638                    _ => {
639                        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.";
640                        v_flex().gap_2().child(Label::new(LABEL)).child(
641                            Button::new("sign_in", "Sign in to use GitHub Copilot")
642                                .icon_color(Color::Muted)
643                                .icon(IconName::Github)
644                                .icon_position(IconPosition::Start)
645                                .icon_size(IconSize::Medium)
646                                .full_width()
647                                .on_click(|_, window, cx| copilot::initiate_sign_in(window, cx)),
648                        )
649                    }
650                },
651                None => v_flex().gap_6().child(Label::new(ERROR_LABEL)),
652            }
653        }
654    }
655}