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