copilot_chat.rs

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