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, TokenUsage,
 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) -> u64 {
241        self.model.max_token_count()
242    }
243
244    fn count_tokens(
245        &self,
246        request: LanguageModelRequest,
247        cx: &App,
248    ) -> BoxFuture<'static, Result<u64>> {
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        let copilot_request = match into_copilot_chat(&self.model, request) {
271            Ok(request) => request,
272            Err(err) => return futures::future::ready(Err(err.into())).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                        if let Some(usage) = event.usage {
365                            events.push(Ok(LanguageModelCompletionEvent::UsageUpdate(
366                                TokenUsage {
367                                    input_tokens: usage.prompt_tokens,
368                                    output_tokens: usage.completion_tokens,
369                                    cache_creation_input_tokens: 0,
370                                    cache_read_input_tokens: 0,
371                                },
372                            )));
373                        }
374
375                        match choice.finish_reason.as_deref() {
376                            Some("stop") => {
377                                events.push(Ok(LanguageModelCompletionEvent::Stop(
378                                    StopReason::EndTurn,
379                                )));
380                            }
381                            Some("tool_calls") => {
382                                events.extend(state.tool_calls_by_index.drain().map(
383                                    |(_, tool_call)| {
384                                        // The model can output an empty string
385                                        // to indicate the absence of arguments.
386                                        // When that happens, create an empty
387                                        // object instead.
388                                        let arguments = if tool_call.arguments.is_empty() {
389                                            Ok(serde_json::Value::Object(Default::default()))
390                                        } else {
391                                            serde_json::Value::from_str(&tool_call.arguments)
392                                        };
393                                        match arguments {
394                                            Ok(input) => Ok(LanguageModelCompletionEvent::ToolUse(
395                                                LanguageModelToolUse {
396                                                    id: tool_call.id.clone().into(),
397                                                    name: tool_call.name.as_str().into(),
398                                                    is_input_complete: true,
399                                                    input,
400                                                    raw_input: tool_call.arguments.clone(),
401                                                },
402                                            )),
403                                            Err(error) => {
404                                                Err(LanguageModelCompletionError::BadInputJson {
405                                                    id: tool_call.id.into(),
406                                                    tool_name: tool_call.name.as_str().into(),
407                                                    raw_input: tool_call.arguments.into(),
408                                                    json_parse_error: error.to_string(),
409                                                })
410                                            }
411                                        }
412                                    },
413                                ));
414
415                                events.push(Ok(LanguageModelCompletionEvent::Stop(
416                                    StopReason::ToolUse,
417                                )));
418                            }
419                            Some(stop_reason) => {
420                                log::error!("Unexpected Copilot Chat stop_reason: {stop_reason:?}");
421                                events.push(Ok(LanguageModelCompletionEvent::Stop(
422                                    StopReason::EndTurn,
423                                )));
424                            }
425                            None => {}
426                        }
427
428                        return Some((events, state));
429                    }
430                    Err(err) => return Some((vec![Err(anyhow!(err).into())], state)),
431                }
432            }
433
434            None
435        },
436    )
437    .flat_map(futures::stream::iter)
438}
439
440fn into_copilot_chat(
441    model: &copilot::copilot_chat::Model,
442    request: LanguageModelRequest,
443) -> Result<CopilotChatRequest> {
444    let mut request_messages: Vec<LanguageModelRequestMessage> = Vec::new();
445    for message in request.messages {
446        if let Some(last_message) = request_messages.last_mut() {
447            if last_message.role == message.role {
448                last_message.content.extend(message.content);
449            } else {
450                request_messages.push(message);
451            }
452        } else {
453            request_messages.push(message);
454        }
455    }
456
457    let mut tool_called = false;
458    let mut messages: Vec<ChatMessage> = Vec::new();
459    for message in request_messages {
460        match message.role {
461            Role::User => {
462                for content in &message.content {
463                    if let MessageContent::ToolResult(tool_result) = content {
464                        let content = match &tool_result.content {
465                            LanguageModelToolResultContent::Text(text) => text.to_string().into(),
466                            LanguageModelToolResultContent::Image(image) => {
467                                if model.supports_vision() {
468                                    ChatMessageContent::Multipart(vec![ChatMessagePart::Image {
469                                        image_url: ImageUrl {
470                                            url: image.to_base64_url(),
471                                        },
472                                    }])
473                                } else {
474                                    debug_panic!(
475                                        "This should be caught at {} level",
476                                        tool_result.tool_name
477                                    );
478                                    "[Tool responded with an image, but this model does not support vision]".to_string().into()
479                                }
480                            }
481                        };
482
483                        messages.push(ChatMessage::Tool {
484                            tool_call_id: tool_result.tool_use_id.to_string(),
485                            content,
486                        });
487                    }
488                }
489
490                let mut content_parts = Vec::new();
491                for content in &message.content {
492                    match content {
493                        MessageContent::Text(text) | MessageContent::Thinking { text, .. }
494                            if !text.is_empty() =>
495                        {
496                            if let Some(ChatMessagePart::Text { text: text_content }) =
497                                content_parts.last_mut()
498                            {
499                                text_content.push_str(text);
500                            } else {
501                                content_parts.push(ChatMessagePart::Text {
502                                    text: text.to_string(),
503                                });
504                            }
505                        }
506                        MessageContent::Image(image) if model.supports_vision() => {
507                            content_parts.push(ChatMessagePart::Image {
508                                image_url: ImageUrl {
509                                    url: image.to_base64_url(),
510                                },
511                            });
512                        }
513                        _ => {}
514                    }
515                }
516
517                if !content_parts.is_empty() {
518                    messages.push(ChatMessage::User {
519                        content: content_parts.into(),
520                    });
521                }
522            }
523            Role::Assistant => {
524                let mut tool_calls = Vec::new();
525                for content in &message.content {
526                    if let MessageContent::ToolUse(tool_use) = content {
527                        tool_called = true;
528                        tool_calls.push(ToolCall {
529                            id: tool_use.id.to_string(),
530                            content: copilot::copilot_chat::ToolCallContent::Function {
531                                function: copilot::copilot_chat::FunctionContent {
532                                    name: tool_use.name.to_string(),
533                                    arguments: serde_json::to_string(&tool_use.input)?,
534                                },
535                            },
536                        });
537                    }
538                }
539
540                let text_content = {
541                    let mut buffer = String::new();
542                    for string in message.content.iter().filter_map(|content| match content {
543                        MessageContent::Text(text) | MessageContent::Thinking { text, .. } => {
544                            Some(text.as_str())
545                        }
546                        MessageContent::ToolUse(_)
547                        | MessageContent::RedactedThinking(_)
548                        | MessageContent::ToolResult(_)
549                        | MessageContent::Image(_) => None,
550                    }) {
551                        buffer.push_str(string);
552                    }
553
554                    buffer
555                };
556
557                messages.push(ChatMessage::Assistant {
558                    content: if text_content.is_empty() {
559                        ChatMessageContent::empty()
560                    } else {
561                        text_content.into()
562                    },
563                    tool_calls,
564                });
565            }
566            Role::System => messages.push(ChatMessage::System {
567                content: message.string_contents(),
568            }),
569        }
570    }
571
572    let mut tools = request
573        .tools
574        .iter()
575        .map(|tool| Tool::Function {
576            function: copilot::copilot_chat::Function {
577                name: tool.name.clone(),
578                description: tool.description.clone(),
579                parameters: tool.input_schema.clone(),
580            },
581        })
582        .collect::<Vec<_>>();
583
584    // The API will return a Bad Request (with no error message) when tools
585    // were used previously in the conversation but no tools are provided as
586    // part of this request. Inserting a dummy tool seems to circumvent this
587    // error.
588    if tool_called && tools.is_empty() {
589        tools.push(Tool::Function {
590            function: copilot::copilot_chat::Function {
591                name: "noop".to_string(),
592                description: "No operation".to_string(),
593                parameters: serde_json::json!({
594                    "type": "object"
595                }),
596            },
597        });
598    }
599
600    Ok(CopilotChatRequest {
601        intent: true,
602        n: 1,
603        stream: model.uses_streaming(),
604        temperature: 0.1,
605        model: model.id().to_string(),
606        messages,
607        tools,
608        tool_choice: request.tool_choice.map(|choice| match choice {
609            LanguageModelToolChoice::Auto => copilot::copilot_chat::ToolChoice::Auto,
610            LanguageModelToolChoice::Any => copilot::copilot_chat::ToolChoice::Any,
611            LanguageModelToolChoice::None => copilot::copilot_chat::ToolChoice::None,
612        }),
613    })
614}
615
616struct ConfigurationView {
617    copilot_status: Option<copilot::Status>,
618    state: Entity<State>,
619    _subscription: Option<Subscription>,
620}
621
622impl ConfigurationView {
623    pub fn new(state: Entity<State>, cx: &mut Context<Self>) -> Self {
624        let copilot = Copilot::global(cx);
625
626        Self {
627            copilot_status: copilot.as_ref().map(|copilot| copilot.read(cx).status()),
628            state,
629            _subscription: copilot.as_ref().map(|copilot| {
630                cx.observe(copilot, |this, model, cx| {
631                    this.copilot_status = Some(model.read(cx).status());
632                    cx.notify();
633                })
634            }),
635        }
636    }
637}
638
639impl Render for ConfigurationView {
640    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
641        if self.state.read(cx).is_authenticated(cx) {
642            h_flex()
643                .mt_1()
644                .p_1()
645                .justify_between()
646                .rounded_md()
647                .border_1()
648                .border_color(cx.theme().colors().border)
649                .bg(cx.theme().colors().background)
650                .child(
651                    h_flex()
652                        .gap_1()
653                        .child(Icon::new(IconName::Check).color(Color::Success))
654                        .child(Label::new("Authorized")),
655                )
656                .child(
657                    Button::new("sign_out", "Sign Out")
658                        .label_size(LabelSize::Small)
659                        .on_click(|_, window, cx| {
660                            window.dispatch_action(copilot::SignOut.boxed_clone(), cx);
661                        }),
662                )
663        } else {
664            let loading_icon = Icon::new(IconName::ArrowCircle).with_animation(
665                "arrow-circle",
666                Animation::new(Duration::from_secs(4)).repeat(),
667                |icon, delta| icon.transform(Transformation::rotate(percentage(delta))),
668            );
669
670            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.";
671
672            match &self.copilot_status {
673                Some(status) => match status {
674                    Status::Starting { task: _ } => h_flex()
675                        .gap_2()
676                        .child(loading_icon)
677                        .child(Label::new("Starting Copilot…")),
678                    Status::SigningIn { prompt: _ }
679                    | Status::SignedOut {
680                        awaiting_signing_in: true,
681                    } => h_flex()
682                        .gap_2()
683                        .child(loading_icon)
684                        .child(Label::new("Signing into Copilot…")),
685                    Status::Error(_) => {
686                        const LABEL: &str = "Copilot had issues starting. Please try restarting it. If the issue persists, try reinstalling Copilot.";
687                        v_flex()
688                            .gap_6()
689                            .child(Label::new(LABEL))
690                            .child(svg().size_8().path(IconName::CopilotError.path()))
691                    }
692                    _ => {
693                        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.";
694                        v_flex().gap_2().child(Label::new(LABEL)).child(
695                            Button::new("sign_in", "Sign in to use GitHub Copilot")
696                                .icon_color(Color::Muted)
697                                .icon(IconName::Github)
698                                .icon_position(IconPosition::Start)
699                                .icon_size(IconSize::Medium)
700                                .full_width()
701                                .on_click(|_, window, cx| copilot::initiate_sign_in(window, cx)),
702                        )
703                    }
704                },
705                None => v_flex().gap_6().child(Label::new(ERROR_LABEL)),
706            }
707        }
708    }
709}