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) | MessageContent::Thinking { text, .. } => {
428 Some(text.as_str())
429 }
430 MessageContent::ToolUse(_)
431 | MessageContent::RedactedThinking(_)
432 | MessageContent::ToolResult(_)
433 | MessageContent::Image(_) => None,
434 }) {
435 buffer.push_str(string);
436 }
437
438 buffer
439 };
440
441 match message.role {
442 Role::User => {
443 for content in &message.content {
444 if let MessageContent::ToolResult(tool_result) = content {
445 messages.push(ChatMessage::Tool {
446 tool_call_id: tool_result.tool_use_id.to_string(),
447 content: tool_result.content.to_string(),
448 });
449 }
450 }
451
452 messages.push(ChatMessage::User {
453 content: text_content,
454 });
455 }
456 Role::Assistant => {
457 let mut tool_calls = Vec::new();
458 for content in &message.content {
459 if let MessageContent::ToolUse(tool_use) = content {
460 tool_calls.push(ToolCall {
461 id: tool_use.id.to_string(),
462 content: copilot::copilot_chat::ToolCallContent::Function {
463 function: copilot::copilot_chat::FunctionContent {
464 name: tool_use.name.to_string(),
465 arguments: serde_json::to_string(&tool_use.input)?,
466 },
467 },
468 });
469 }
470 }
471
472 messages.push(ChatMessage::Assistant {
473 content: if text_content.is_empty() {
474 None
475 } else {
476 Some(text_content)
477 },
478 tool_calls,
479 });
480 }
481 Role::System => messages.push(ChatMessage::System {
482 content: message.string_contents(),
483 }),
484 }
485 }
486
487 let tools = request
488 .tools
489 .iter()
490 .map(|tool| Tool::Function {
491 function: copilot::copilot_chat::Function {
492 name: tool.name.clone(),
493 description: tool.description.clone(),
494 parameters: tool.input_schema.clone(),
495 },
496 })
497 .collect();
498
499 Ok(CopilotChatRequest {
500 intent: true,
501 n: 1,
502 stream: model.uses_streaming(),
503 temperature: 0.1,
504 model,
505 messages,
506 tools,
507 tool_choice: None,
508 })
509 }
510}
511
512struct ConfigurationView {
513 copilot_status: Option<copilot::Status>,
514 state: Entity<State>,
515 _subscription: Option<Subscription>,
516}
517
518impl ConfigurationView {
519 pub fn new(state: Entity<State>, cx: &mut Context<Self>) -> Self {
520 let copilot = Copilot::global(cx);
521
522 Self {
523 copilot_status: copilot.as_ref().map(|copilot| copilot.read(cx).status()),
524 state,
525 _subscription: copilot.as_ref().map(|copilot| {
526 cx.observe(copilot, |this, model, cx| {
527 this.copilot_status = Some(model.read(cx).status());
528 cx.notify();
529 })
530 }),
531 }
532 }
533}
534
535impl Render for ConfigurationView {
536 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
537 if self.state.read(cx).is_authenticated(cx) {
538 h_flex()
539 .mt_1()
540 .p_1()
541 .justify_between()
542 .rounded_md()
543 .border_1()
544 .border_color(cx.theme().colors().border)
545 .bg(cx.theme().colors().background)
546 .child(
547 h_flex()
548 .gap_1()
549 .child(Icon::new(IconName::Check).color(Color::Success))
550 .child(Label::new("Authorized")),
551 )
552 .child(
553 Button::new("sign_out", "Sign Out")
554 .label_size(LabelSize::Small)
555 .on_click(|_, window, cx| {
556 window.dispatch_action(copilot::SignOut.boxed_clone(), cx);
557 }),
558 )
559 } else {
560 let loading_icon = Icon::new(IconName::ArrowCircle).with_animation(
561 "arrow-circle",
562 Animation::new(Duration::from_secs(4)).repeat(),
563 |icon, delta| icon.transform(Transformation::rotate(percentage(delta))),
564 );
565
566 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.";
567
568 match &self.copilot_status {
569 Some(status) => match status {
570 Status::Starting { task: _ } => h_flex()
571 .gap_2()
572 .child(loading_icon)
573 .child(Label::new("Starting Copilot…")),
574 Status::SigningIn { prompt: _ }
575 | Status::SignedOut {
576 awaiting_signing_in: true,
577 } => h_flex()
578 .gap_2()
579 .child(loading_icon)
580 .child(Label::new("Signing into Copilot…")),
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_2().child(Label::new(LABEL)).child(
591 Button::new("sign_in", "Sign in to use GitHub Copilot")
592 .icon_color(Color::Muted)
593 .icon(IconName::Github)
594 .icon_position(IconPosition::Start)
595 .icon_size(IconSize::Medium)
596 .full_width()
597 .on_click(|_, window, cx| copilot::initiate_sign_in(window, cx)),
598 )
599 }
600 },
601 None => v_flex().gap_6().child(Label::new(ERROR_LABEL)),
602 }
603 }
604 }
605}