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