1use std::future;
2use std::sync::Arc;
3
4use anyhow::{anyhow, Result};
5use copilot::copilot_chat::{
6 ChatMessage, CopilotChat, Model as CopilotChatModel, Request as CopilotChatRequest,
7 Role as CopilotChatRole,
8};
9use copilot::{Copilot, Status};
10use futures::future::BoxFuture;
11use futures::stream::BoxStream;
12use futures::{FutureExt, StreamExt};
13use gpui::{
14 percentage, svg, Animation, AnimationExt, AnyView, AppContext, AsyncAppContext, Model, Render,
15 Subscription, Task, Transformation,
16};
17use settings::SettingsStore;
18use std::time::Duration;
19use strum::IntoEnumIterator;
20use ui::{
21 div, h_flex, v_flex, Button, ButtonCommon, Clickable, Color, Context, FixedWidth, Icon,
22 IconName, IconPosition, IconSize, IntoElement, Label, LabelCommon, ParentElement, Styled,
23 ViewContext, VisualContext, WindowContext,
24};
25
26use crate::{
27 LanguageModel, LanguageModelId, LanguageModelName, LanguageModelProvider,
28 LanguageModelProviderId, LanguageModelProviderName, LanguageModelRequest, RateLimiter, Role,
29};
30use crate::{LanguageModelCompletionEvent, LanguageModelProviderState};
31
32use super::anthropic::count_anthropic_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: Model<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: &AppContext) -> 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 AppContext) -> Self {
60 let state = cx.new_model(|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::Model<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 provided_models(&self, _cx: &AppContext) -> Vec<Arc<dyn LanguageModel>> {
97 CopilotChatModel::iter()
98 .map(|model| {
99 Arc::new(CopilotChatLanguageModel {
100 model,
101 request_limiter: RateLimiter::new(4),
102 }) as Arc<dyn LanguageModel>
103 })
104 .collect()
105 }
106
107 fn is_authenticated(&self, cx: &AppContext) -> bool {
108 self.state.read(cx).is_authenticated(cx)
109 }
110
111 fn authenticate(&self, cx: &mut AppContext) -> Task<Result<()>> {
112 let result = if self.is_authenticated(cx) {
113 Ok(())
114 } else if let Some(copilot) = Copilot::global(cx) {
115 let error_msg = match copilot.read(cx).status() {
116 Status::Disabled => anyhow::anyhow!("Copilot must be enabled for Copilot Chat to work. Please enable Copilot and try again."),
117 Status::Error(e) => anyhow::anyhow!(format!("Received the following error while signing into Copilot: {e}")),
118 Status::Starting { task: _ } => anyhow::anyhow!("Copilot is still starting, please wait for Copilot to start then try again"),
119 Status::Unauthorized => anyhow::anyhow!("Unable to authorize with Copilot. Please make sure that you have an active Copilot and Copilot Chat subscription."),
120 Status::Authorized => return Task::ready(Ok(())),
121 Status::SignedOut => anyhow::anyhow!("You have signed out of Copilot. Please sign in to Copilot and try again."),
122 Status::SigningIn { prompt: _ } => anyhow::anyhow!("Still signing into Copilot..."),
123 };
124 Err(error_msg)
125 } else {
126 Err(anyhow::anyhow!(
127 "Copilot must be enabled for Copilot Chat to work. Please enable Copilot and try again."
128 ))
129 };
130 Task::ready(result)
131 }
132
133 fn configuration_view(&self, cx: &mut WindowContext) -> AnyView {
134 let state = self.state.clone();
135 cx.new_view(|cx| ConfigurationView::new(state, cx)).into()
136 }
137
138 fn reset_credentials(&self, _cx: &mut AppContext) -> Task<Result<()>> {
139 Task::ready(Err(anyhow!(
140 "Signing out of GitHub Copilot Chat is currently not supported."
141 )))
142 }
143}
144
145pub struct CopilotChatLanguageModel {
146 model: CopilotChatModel,
147 request_limiter: RateLimiter,
148}
149
150impl LanguageModel for CopilotChatLanguageModel {
151 fn id(&self) -> LanguageModelId {
152 LanguageModelId::from(self.model.id().to_string())
153 }
154
155 fn name(&self) -> LanguageModelName {
156 LanguageModelName::from(self.model.display_name().to_string())
157 }
158
159 fn provider_id(&self) -> LanguageModelProviderId {
160 LanguageModelProviderId(PROVIDER_ID.into())
161 }
162
163 fn provider_name(&self) -> LanguageModelProviderName {
164 LanguageModelProviderName(PROVIDER_NAME.into())
165 }
166
167 fn telemetry_id(&self) -> String {
168 format!("copilot_chat/{}", self.model.id())
169 }
170
171 fn max_token_count(&self) -> usize {
172 self.model.max_token_count()
173 }
174
175 fn count_tokens(
176 &self,
177 request: LanguageModelRequest,
178 cx: &AppContext,
179 ) -> BoxFuture<'static, Result<usize>> {
180 match self.model {
181 CopilotChatModel::Claude3_5Sonnet => count_anthropic_tokens(request, cx),
182 _ => {
183 let model = match self.model {
184 CopilotChatModel::Gpt4o => open_ai::Model::FourOmni,
185 CopilotChatModel::Gpt4 => open_ai::Model::Four,
186 CopilotChatModel::Gpt3_5Turbo => open_ai::Model::ThreePointFiveTurbo,
187 CopilotChatModel::O1Preview | CopilotChatModel::O1Mini => open_ai::Model::Four,
188 CopilotChatModel::Claude3_5Sonnet => unreachable!(),
189 };
190 count_open_ai_tokens(request, model, cx)
191 }
192 }
193 }
194
195 fn stream_completion(
196 &self,
197 request: LanguageModelRequest,
198 cx: &AsyncAppContext,
199 ) -> BoxFuture<'static, Result<BoxStream<'static, Result<LanguageModelCompletionEvent>>>> {
200 if let Some(message) = request.messages.last() {
201 if message.contents_empty() {
202 const EMPTY_PROMPT_MSG: &str =
203 "Empty prompts aren't allowed. Please provide a non-empty prompt.";
204 return futures::future::ready(Err(anyhow::anyhow!(EMPTY_PROMPT_MSG))).boxed();
205 }
206
207 // Copilot Chat has a restriction that the final message must be from the user.
208 // While their API does return an error message for this, we can catch it earlier
209 // and provide a more helpful error message.
210 if !matches!(message.role, Role::User) {
211 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.";
212 return futures::future::ready(Err(anyhow::anyhow!(USER_ROLE_MSG))).boxed();
213 }
214 }
215
216 let copilot_request = self.to_copilot_chat_request(request);
217 let is_streaming = copilot_request.stream;
218
219 let request_limiter = self.request_limiter.clone();
220 let future = cx.spawn(|cx| async move {
221 let response = CopilotChat::stream_completion(copilot_request, cx);
222 request_limiter.stream(async move {
223 let response = response.await?;
224 let stream = response
225 .filter_map(move |response| async move {
226 match response {
227 Ok(result) => {
228 let choice = result.choices.first();
229 match choice {
230 Some(choice) if !is_streaming => {
231 match &choice.message {
232 Some(msg) => Some(Ok(msg.content.clone().unwrap_or_default())),
233 None => Some(Err(anyhow::anyhow!(
234 "The Copilot Chat API returned a response with no message content"
235 ))),
236 }
237 },
238 Some(choice) => {
239 match &choice.delta {
240 Some(delta) => Some(Ok(delta.content.clone().unwrap_or_default())),
241 None => Some(Err(anyhow::anyhow!(
242 "The Copilot Chat API returned a response with no delta content"
243 ))),
244 }
245 },
246 None => Some(Err(anyhow::anyhow!(
247 "The Copilot Chat API returned a response with no choices, but hadn't finished the message yet. Please try again."
248 ))),
249 }
250 }
251 Err(err) => Some(Err(err)),
252 }
253 })
254 .boxed();
255 Ok(stream)
256 }).await
257 });
258
259 async move {
260 Ok(future
261 .await?
262 .map(|result| result.map(LanguageModelCompletionEvent::Text))
263 .boxed())
264 }
265 .boxed()
266 }
267
268 fn use_any_tool(
269 &self,
270 _request: LanguageModelRequest,
271 _name: String,
272 _description: String,
273 _schema: serde_json::Value,
274 _cx: &AsyncAppContext,
275 ) -> BoxFuture<'static, Result<BoxStream<'static, Result<String>>>> {
276 future::ready(Err(anyhow!("not implemented"))).boxed()
277 }
278}
279
280impl CopilotChatLanguageModel {
281 pub fn to_copilot_chat_request(&self, request: LanguageModelRequest) -> CopilotChatRequest {
282 CopilotChatRequest::new(
283 self.model.clone(),
284 request
285 .messages
286 .into_iter()
287 .map(|msg| ChatMessage {
288 role: match msg.role {
289 Role::User => CopilotChatRole::User,
290 Role::Assistant => CopilotChatRole::Assistant,
291 Role::System => CopilotChatRole::System,
292 },
293 content: msg.string_contents(),
294 })
295 .collect(),
296 )
297 }
298}
299
300struct ConfigurationView {
301 copilot_status: Option<copilot::Status>,
302 state: Model<State>,
303 _subscription: Option<Subscription>,
304}
305
306impl ConfigurationView {
307 pub fn new(state: Model<State>, cx: &mut ViewContext<Self>) -> Self {
308 let copilot = Copilot::global(cx);
309
310 Self {
311 copilot_status: copilot.as_ref().map(|copilot| copilot.read(cx).status()),
312 state,
313 _subscription: copilot.as_ref().map(|copilot| {
314 cx.observe(copilot, |this, model, cx| {
315 this.copilot_status = Some(model.read(cx).status());
316 cx.notify();
317 })
318 }),
319 }
320 }
321}
322
323impl Render for ConfigurationView {
324 fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
325 if self.state.read(cx).is_authenticated(cx) {
326 const LABEL: &str = "Authorized.";
327 h_flex()
328 .gap_1()
329 .child(Icon::new(IconName::Check).color(Color::Success))
330 .child(Label::new(LABEL))
331 } else {
332 let loading_icon = svg()
333 .size_8()
334 .path(IconName::ArrowCircle.path())
335 .text_color(cx.text_style().color)
336 .with_animation(
337 "icon_circle_arrow",
338 Animation::new(Duration::from_secs(2)).repeat(),
339 |svg, delta| svg.with_transformation(Transformation::rotate(percentage(delta))),
340 );
341
342 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.";
343
344 match &self.copilot_status {
345 Some(status) => match status {
346 Status::Disabled => v_flex().gap_6().p_4().child(Label::new(ERROR_LABEL)),
347 Status::Starting { task: _ } => {
348 const LABEL: &str = "Starting Copilot...";
349 v_flex()
350 .gap_6()
351 .justify_center()
352 .items_center()
353 .child(Label::new(LABEL))
354 .child(loading_icon)
355 }
356 Status::SigningIn { prompt: _ } => {
357 const LABEL: &str = "Signing in to Copilot...";
358 v_flex()
359 .gap_6()
360 .justify_center()
361 .items_center()
362 .child(Label::new(LABEL))
363 .child(loading_icon)
364 }
365 Status::Error(_) => {
366 const LABEL: &str = "Copilot had issues starting. Please try restarting it. If the issue persists, try reinstalling Copilot.";
367 v_flex()
368 .gap_6()
369 .child(Label::new(LABEL))
370 .child(svg().size_8().path(IconName::CopilotError.path()))
371 }
372 _ => {
373 const LABEL: &str =
374 "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.";
375 v_flex().gap_6().child(Label::new(LABEL)).child(
376 v_flex()
377 .gap_2()
378 .child(
379 Button::new("sign_in", "Sign In")
380 .icon_color(Color::Muted)
381 .icon(IconName::Github)
382 .icon_position(IconPosition::Start)
383 .icon_size(IconSize::Medium)
384 .style(ui::ButtonStyle::Filled)
385 .full_width()
386 .on_click(|_, cx| {
387 inline_completion_button::initiate_sign_in(cx)
388 }),
389 )
390 .child(
391 div().flex().w_full().items_center().child(
392 Label::new("Sign in to start using Github Copilot Chat.")
393 .color(Color::Muted)
394 .size(ui::LabelSize::Small),
395 ),
396 ),
397 )
398 }
399 },
400 None => v_flex().gap_6().child(Label::new(ERROR_LABEL)),
401 }
402 }
403 }
404}