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