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