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