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 | CopilotChatModel::Gemini25Pro => {
214                count_google_tokens(request, cx)
215            }
216            _ => {
217                let model = match self.model {
218                    CopilotChatModel::Gpt4o => open_ai::Model::FourOmni,
219                    CopilotChatModel::Gpt4 => open_ai::Model::Four,
220                    CopilotChatModel::Gpt4_1 => open_ai::Model::FourPointOne,
221                    CopilotChatModel::Gpt3_5Turbo => open_ai::Model::ThreePointFiveTurbo,
222                    CopilotChatModel::O1 => open_ai::Model::O1,
223                    CopilotChatModel::O3Mini => open_ai::Model::O3Mini,
224                    CopilotChatModel::O3 => open_ai::Model::O3,
225                    CopilotChatModel::O4Mini => open_ai::Model::O4Mini,
226                    CopilotChatModel::Claude3_5Sonnet
227                    | CopilotChatModel::Claude3_7Sonnet
228                    | CopilotChatModel::Claude3_7SonnetThinking
229                    | CopilotChatModel::Gemini20Flash
230                    | CopilotChatModel::Gemini25Pro => {
231                        unreachable!()
232                    }
233                };
234                count_open_ai_tokens(request, model, cx)
235            }
236        }
237    }
238
239    fn stream_completion(
240        &self,
241        request: LanguageModelRequest,
242        cx: &AsyncApp,
243    ) -> BoxFuture<'static, Result<BoxStream<'static, Result<LanguageModelCompletionEvent>>>> {
244        if let Some(message) = request.messages.last() {
245            if message.contents_empty() {
246                const EMPTY_PROMPT_MSG: &str =
247                    "Empty prompts aren't allowed. Please provide a non-empty prompt.";
248                return futures::future::ready(Err(anyhow::anyhow!(EMPTY_PROMPT_MSG))).boxed();
249            }
250
251            // Copilot Chat has a restriction that the final message must be from the user.
252            // While their API does return an error message for this, we can catch it earlier
253            // and provide a more helpful error message.
254            if !matches!(message.role, Role::User) {
255                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.";
256                return futures::future::ready(Err(anyhow::anyhow!(USER_ROLE_MSG))).boxed();
257            }
258        }
259
260        let copilot_request = match self.to_copilot_chat_request(request) {
261            Ok(request) => request,
262            Err(err) => return futures::future::ready(Err(err)).boxed(),
263        };
264        let is_streaming = copilot_request.stream;
265
266        let request_limiter = self.request_limiter.clone();
267        let future = cx.spawn(async move |cx| {
268            let request = CopilotChat::stream_completion(copilot_request, cx.clone());
269            request_limiter
270                .stream(async move {
271                    let response = request.await?;
272                    Ok(map_to_language_model_completion_events(
273                        response,
274                        is_streaming,
275                    ))
276                })
277                .await
278        });
279        async move { Ok(future.await?.boxed()) }.boxed()
280    }
281}
282
283pub fn map_to_language_model_completion_events(
284    events: Pin<Box<dyn Send + Stream<Item = Result<ResponseEvent>>>>,
285    is_streaming: bool,
286) -> impl Stream<Item = Result<LanguageModelCompletionEvent>> {
287    #[derive(Default)]
288    struct RawToolCall {
289        id: String,
290        name: String,
291        arguments: String,
292    }
293
294    struct State {
295        events: Pin<Box<dyn Send + Stream<Item = Result<ResponseEvent>>>>,
296        tool_calls_by_index: HashMap<usize, RawToolCall>,
297    }
298
299    futures::stream::unfold(
300        State {
301            events,
302            tool_calls_by_index: HashMap::default(),
303        },
304        move |mut state| async move {
305            if let Some(event) = state.events.next().await {
306                match event {
307                    Ok(event) => {
308                        let Some(choice) = event.choices.first() else {
309                            return Some((
310                                vec![Err(anyhow!("Response contained no choices"))],
311                                state,
312                            ));
313                        };
314
315                        let delta = if is_streaming {
316                            choice.delta.as_ref()
317                        } else {
318                            choice.message.as_ref()
319                        };
320
321                        let Some(delta) = delta else {
322                            return Some((
323                                vec![Err(anyhow!("Response contained no delta"))],
324                                state,
325                            ));
326                        };
327
328                        let mut events = Vec::new();
329                        if let Some(content) = delta.content.clone() {
330                            events.push(Ok(LanguageModelCompletionEvent::Text(content)));
331                        }
332
333                        for tool_call in &delta.tool_calls {
334                            let entry = state
335                                .tool_calls_by_index
336                                .entry(tool_call.index)
337                                .or_default();
338
339                            if let Some(tool_id) = tool_call.id.clone() {
340                                entry.id = tool_id;
341                            }
342
343                            if let Some(function) = tool_call.function.as_ref() {
344                                if let Some(name) = function.name.clone() {
345                                    entry.name = name;
346                                }
347
348                                if let Some(arguments) = function.arguments.clone() {
349                                    entry.arguments.push_str(&arguments);
350                                }
351                            }
352                        }
353
354                        match choice.finish_reason.as_deref() {
355                            Some("stop") => {
356                                events.push(Ok(LanguageModelCompletionEvent::Stop(
357                                    StopReason::EndTurn,
358                                )));
359                            }
360                            Some("tool_calls") => {
361                                events.extend(state.tool_calls_by_index.drain().map(
362                                    |(_, tool_call)| {
363                                        maybe!({
364                                            Ok(LanguageModelCompletionEvent::ToolUse(
365                                                LanguageModelToolUse {
366                                                    id: tool_call.id.into(),
367                                                    name: tool_call.name.as_str().into(),
368                                                    input: serde_json::Value::from_str(
369                                                        &tool_call.arguments,
370                                                    )?,
371                                                },
372                                            ))
373                                        })
374                                    },
375                                ));
376
377                                events.push(Ok(LanguageModelCompletionEvent::Stop(
378                                    StopReason::ToolUse,
379                                )));
380                            }
381                            Some(stop_reason) => {
382                                log::error!("Unexpected Copilot Chat stop_reason: {stop_reason:?}");
383                                events.push(Ok(LanguageModelCompletionEvent::Stop(
384                                    StopReason::EndTurn,
385                                )));
386                            }
387                            None => {}
388                        }
389
390                        return Some((events, state));
391                    }
392                    Err(err) => return Some((vec![Err(err)], state)),
393                }
394            }
395
396            None
397        },
398    )
399    .flat_map(futures::stream::iter)
400}
401
402impl CopilotChatLanguageModel {
403    pub fn to_copilot_chat_request(
404        &self,
405        request: LanguageModelRequest,
406    ) -> Result<CopilotChatRequest> {
407        let model = self.model.clone();
408
409        let mut request_messages: Vec<LanguageModelRequestMessage> = Vec::new();
410        for message in request.messages {
411            if let Some(last_message) = request_messages.last_mut() {
412                if last_message.role == message.role {
413                    last_message.content.extend(message.content);
414                } else {
415                    request_messages.push(message);
416                }
417            } else {
418                request_messages.push(message);
419            }
420        }
421
422        let mut messages: Vec<ChatMessage> = Vec::new();
423        for message in request_messages {
424            let text_content = {
425                let mut buffer = String::new();
426                for string in message.content.iter().filter_map(|content| match content {
427                    MessageContent::Text(text) => Some(text.as_str()),
428                    MessageContent::ToolUse(_)
429                    | MessageContent::ToolResult(_)
430                    | MessageContent::Image(_) => None,
431                }) {
432                    buffer.push_str(string);
433                }
434
435                buffer
436            };
437
438            match message.role {
439                Role::User => {
440                    for content in &message.content {
441                        if let MessageContent::ToolResult(tool_result) = content {
442                            messages.push(ChatMessage::Tool {
443                                tool_call_id: tool_result.tool_use_id.to_string(),
444                                content: tool_result.content.to_string(),
445                            });
446                        }
447                    }
448
449                    messages.push(ChatMessage::User {
450                        content: text_content,
451                    });
452                }
453                Role::Assistant => {
454                    let mut tool_calls = Vec::new();
455                    for content in &message.content {
456                        if let MessageContent::ToolUse(tool_use) = content {
457                            tool_calls.push(ToolCall {
458                                id: tool_use.id.to_string(),
459                                content: copilot::copilot_chat::ToolCallContent::Function {
460                                    function: copilot::copilot_chat::FunctionContent {
461                                        name: tool_use.name.to_string(),
462                                        arguments: serde_json::to_string(&tool_use.input)?,
463                                    },
464                                },
465                            });
466                        }
467                    }
468
469                    messages.push(ChatMessage::Assistant {
470                        content: if text_content.is_empty() {
471                            None
472                        } else {
473                            Some(text_content)
474                        },
475                        tool_calls,
476                    });
477                }
478                Role::System => messages.push(ChatMessage::System {
479                    content: message.string_contents(),
480                }),
481            }
482        }
483
484        let tools = request
485            .tools
486            .iter()
487            .map(|tool| Tool::Function {
488                function: copilot::copilot_chat::Function {
489                    name: tool.name.clone(),
490                    description: tool.description.clone(),
491                    parameters: tool.input_schema.clone(),
492                },
493            })
494            .collect();
495
496        Ok(CopilotChatRequest {
497            intent: true,
498            n: 1,
499            stream: model.uses_streaming(),
500            temperature: 0.1,
501            model,
502            messages,
503            tools,
504            tool_choice: None,
505        })
506    }
507}
508
509struct ConfigurationView {
510    copilot_status: Option<copilot::Status>,
511    state: Entity<State>,
512    _subscription: Option<Subscription>,
513}
514
515impl ConfigurationView {
516    pub fn new(state: Entity<State>, cx: &mut Context<Self>) -> Self {
517        let copilot = Copilot::global(cx);
518
519        Self {
520            copilot_status: copilot.as_ref().map(|copilot| copilot.read(cx).status()),
521            state,
522            _subscription: copilot.as_ref().map(|copilot| {
523                cx.observe(copilot, |this, model, cx| {
524                    this.copilot_status = Some(model.read(cx).status());
525                    cx.notify();
526                })
527            }),
528        }
529    }
530}
531
532impl Render for ConfigurationView {
533    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
534        if self.state.read(cx).is_authenticated(cx) {
535            h_flex()
536                .mt_1()
537                .p_1()
538                .justify_between()
539                .rounded_md()
540                .border_1()
541                .border_color(cx.theme().colors().border)
542                .bg(cx.theme().colors().background)
543                .child(
544                    h_flex()
545                        .gap_1()
546                        .child(Icon::new(IconName::Check).color(Color::Success))
547                        .child(Label::new("Authorized")),
548                )
549                .child(
550                    Button::new("sign_out", "Sign Out")
551                        .label_size(LabelSize::Small)
552                        .on_click(|_, window, cx| {
553                            window.dispatch_action(copilot::SignOut.boxed_clone(), cx);
554                        }),
555                )
556        } else {
557            let loading_icon = Icon::new(IconName::ArrowCircle).with_animation(
558                "arrow-circle",
559                Animation::new(Duration::from_secs(4)).repeat(),
560                |icon, delta| icon.transform(Transformation::rotate(percentage(delta))),
561            );
562
563            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.";
564
565            match &self.copilot_status {
566                Some(status) => match status {
567                    Status::Starting { task: _ } => h_flex()
568                        .gap_2()
569                        .child(loading_icon)
570                        .child(Label::new("Starting Copilot…")),
571                    Status::SigningIn { prompt: _ }
572                    | Status::SignedOut {
573                        awaiting_signing_in: true,
574                    } => h_flex()
575                        .gap_2()
576                        .child(loading_icon)
577                        .child(Label::new("Signing into Copilot…")),
578                    Status::Error(_) => {
579                        const LABEL: &str = "Copilot had issues starting. Please try restarting it. If the issue persists, try reinstalling Copilot.";
580                        v_flex()
581                            .gap_6()
582                            .child(Label::new(LABEL))
583                            .child(svg().size_8().path(IconName::CopilotError.path()))
584                    }
585                    _ => {
586                        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.";
587                        v_flex().gap_2().child(Label::new(LABEL)).child(
588                            Button::new("sign_in", "Sign in to use GitHub Copilot")
589                                .icon_color(Color::Muted)
590                                .icon(IconName::Github)
591                                .icon_position(IconPosition::Start)
592                                .icon_size(IconSize::Medium)
593                                .full_width()
594                                .on_click(|_, window, cx| copilot::initiate_sign_in(window, cx)),
595                        )
596                    }
597                },
598                None => v_flex().gap_6().child(Label::new(ERROR_LABEL)),
599            }
600        }
601    }
602}