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