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, 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 self.to_copilot_chat_request(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
426impl CopilotChatLanguageModel {
427    pub fn to_copilot_chat_request(
428        &self,
429        request: LanguageModelRequest,
430    ) -> Result<CopilotChatRequest> {
431        let mut request_messages: Vec<LanguageModelRequestMessage> = Vec::new();
432        for message in request.messages {
433            if let Some(last_message) = request_messages.last_mut() {
434                if last_message.role == message.role {
435                    last_message.content.extend(message.content);
436                } else {
437                    request_messages.push(message);
438                }
439            } else {
440                request_messages.push(message);
441            }
442        }
443
444        let mut tool_called = false;
445        let mut messages: Vec<ChatMessage> = Vec::new();
446        for message in request_messages {
447            let text_content = {
448                let mut buffer = String::new();
449                for string in message.content.iter().filter_map(|content| match content {
450                    MessageContent::Text(text) | MessageContent::Thinking { text, .. } => {
451                        Some(text.as_str())
452                    }
453                    MessageContent::ToolUse(_)
454                    | MessageContent::RedactedThinking(_)
455                    | MessageContent::ToolResult(_)
456                    | MessageContent::Image(_) => None,
457                }) {
458                    buffer.push_str(string);
459                }
460
461                buffer
462            };
463
464            match message.role {
465                Role::User => {
466                    for content in &message.content {
467                        if let MessageContent::ToolResult(tool_result) = content {
468                            messages.push(ChatMessage::Tool {
469                                tool_call_id: tool_result.tool_use_id.to_string(),
470                                content: tool_result.content.to_string(),
471                            });
472                        }
473                    }
474
475                    if !text_content.is_empty() {
476                        messages.push(ChatMessage::User {
477                            content: text_content,
478                        });
479                    }
480                }
481                Role::Assistant => {
482                    let mut tool_calls = Vec::new();
483                    for content in &message.content {
484                        if let MessageContent::ToolUse(tool_use) = content {
485                            tool_called = true;
486                            tool_calls.push(ToolCall {
487                                id: tool_use.id.to_string(),
488                                content: copilot::copilot_chat::ToolCallContent::Function {
489                                    function: copilot::copilot_chat::FunctionContent {
490                                        name: tool_use.name.to_string(),
491                                        arguments: serde_json::to_string(&tool_use.input)?,
492                                    },
493                                },
494                            });
495                        }
496                    }
497
498                    messages.push(ChatMessage::Assistant {
499                        content: if text_content.is_empty() {
500                            None
501                        } else {
502                            Some(text_content)
503                        },
504                        tool_calls,
505                    });
506                }
507                Role::System => messages.push(ChatMessage::System {
508                    content: message.string_contents(),
509                }),
510            }
511        }
512
513        let mut tools = request
514            .tools
515            .iter()
516            .map(|tool| Tool::Function {
517                function: copilot::copilot_chat::Function {
518                    name: tool.name.clone(),
519                    description: tool.description.clone(),
520                    parameters: tool.input_schema.clone(),
521                },
522            })
523            .collect::<Vec<_>>();
524
525        // The API will return a Bad Request (with no error message) when tools
526        // were used previously in the conversation but no tools are provided as
527        // part of this request. Inserting a dummy tool seems to circumvent this
528        // error.
529        if tool_called && tools.is_empty() {
530            tools.push(Tool::Function {
531                function: copilot::copilot_chat::Function {
532                    name: "noop".to_string(),
533                    description: "No operation".to_string(),
534                    parameters: serde_json::json!({
535                        "type": "object"
536                    }),
537                },
538            });
539        }
540
541        Ok(CopilotChatRequest {
542            intent: true,
543            n: 1,
544            stream: self.model.uses_streaming(),
545            temperature: 0.1,
546            model: self.model.id().to_string(),
547            messages,
548            tools,
549            tool_choice: request.tool_choice.map(|choice| match choice {
550                LanguageModelToolChoice::Auto => copilot::copilot_chat::ToolChoice::Auto,
551                LanguageModelToolChoice::Any => copilot::copilot_chat::ToolChoice::Any,
552                LanguageModelToolChoice::None => copilot::copilot_chat::ToolChoice::None,
553            }),
554        })
555    }
556}
557
558struct ConfigurationView {
559    copilot_status: Option<copilot::Status>,
560    state: Entity<State>,
561    _subscription: Option<Subscription>,
562}
563
564impl ConfigurationView {
565    pub fn new(state: Entity<State>, cx: &mut Context<Self>) -> Self {
566        let copilot = Copilot::global(cx);
567
568        Self {
569            copilot_status: copilot.as_ref().map(|copilot| copilot.read(cx).status()),
570            state,
571            _subscription: copilot.as_ref().map(|copilot| {
572                cx.observe(copilot, |this, model, cx| {
573                    this.copilot_status = Some(model.read(cx).status());
574                    cx.notify();
575                })
576            }),
577        }
578    }
579}
580
581impl Render for ConfigurationView {
582    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
583        if self.state.read(cx).is_authenticated(cx) {
584            h_flex()
585                .mt_1()
586                .p_1()
587                .justify_between()
588                .rounded_md()
589                .border_1()
590                .border_color(cx.theme().colors().border)
591                .bg(cx.theme().colors().background)
592                .child(
593                    h_flex()
594                        .gap_1()
595                        .child(Icon::new(IconName::Check).color(Color::Success))
596                        .child(Label::new("Authorized")),
597                )
598                .child(
599                    Button::new("sign_out", "Sign Out")
600                        .label_size(LabelSize::Small)
601                        .on_click(|_, window, cx| {
602                            window.dispatch_action(copilot::SignOut.boxed_clone(), cx);
603                        }),
604                )
605        } else {
606            let loading_icon = Icon::new(IconName::ArrowCircle).with_animation(
607                "arrow-circle",
608                Animation::new(Duration::from_secs(4)).repeat(),
609                |icon, delta| icon.transform(Transformation::rotate(percentage(delta))),
610            );
611
612            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.";
613
614            match &self.copilot_status {
615                Some(status) => match status {
616                    Status::Starting { task: _ } => h_flex()
617                        .gap_2()
618                        .child(loading_icon)
619                        .child(Label::new("Starting Copilot…")),
620                    Status::SigningIn { prompt: _ }
621                    | Status::SignedOut {
622                        awaiting_signing_in: true,
623                    } => h_flex()
624                        .gap_2()
625                        .child(loading_icon)
626                        .child(Label::new("Signing into Copilot…")),
627                    Status::Error(_) => {
628                        const LABEL: &str = "Copilot had issues starting. Please try restarting it. If the issue persists, try reinstalling Copilot.";
629                        v_flex()
630                            .gap_6()
631                            .child(Label::new(LABEL))
632                            .child(svg().size_8().path(IconName::CopilotError.path()))
633                    }
634                    _ => {
635                        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.";
636                        v_flex().gap_2().child(Label::new(LABEL)).child(
637                            Button::new("sign_in", "Sign in to use GitHub Copilot")
638                                .icon_color(Color::Muted)
639                                .icon(IconName::Github)
640                                .icon_position(IconPosition::Start)
641                                .icon_size(IconSize::Medium)
642                                .full_width()
643                                .on_click(|_, window, cx| copilot::initiate_sign_in(window, cx)),
644                        )
645                    }
646                },
647                None => v_flex().gap_6().child(Label::new(ERROR_LABEL)),
648            }
649        }
650    }
651}