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