1use std::path::PathBuf;
2use std::sync::Arc;
3use std::sync::OnceLock;
4
5use anyhow::{anyhow, Result};
6use chrono::DateTime;
7use collections::HashSet;
8use fs::Fs;
9use futures::{io::BufReader, stream::BoxStream, AsyncBufReadExt, AsyncReadExt, StreamExt};
10use gpui::{prelude::*, App, AsyncApp, Global};
11use http_client::{AsyncBody, HttpClient, Method, Request as HttpRequest};
12use paths::home_dir;
13use serde::{Deserialize, Serialize};
14use settings::watch_config_dir;
15use strum::EnumIter;
16
17pub const COPILOT_CHAT_COMPLETION_URL: &str = "https://api.githubcopilot.com/chat/completions";
18pub const COPILOT_CHAT_AUTH_URL: &str = "https://api.github.com/copilot_internal/v2/token";
19
20#[derive(Clone, Copy, Serialize, Deserialize, Debug, Eq, PartialEq)]
21#[serde(rename_all = "lowercase")]
22pub enum Role {
23 User,
24 Assistant,
25 System,
26}
27
28#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
29#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, EnumIter)]
30pub enum Model {
31 #[default]
32 #[serde(alias = "gpt-4o", rename = "gpt-4o-2024-05-13")]
33 Gpt4o,
34 #[serde(alias = "gpt-4", rename = "gpt-4")]
35 Gpt4,
36 #[serde(alias = "gpt-3.5-turbo", rename = "gpt-3.5-turbo")]
37 Gpt3_5Turbo,
38 #[serde(alias = "o1", rename = "o1")]
39 O1,
40 #[serde(alias = "o1-mini", rename = "o3-mini")]
41 O3Mini,
42 #[serde(alias = "claude-3-5-sonnet", rename = "claude-3.5-sonnet")]
43 Claude3_5Sonnet,
44 #[serde(alias = "claude-3-7-sonnet", rename = "claude-3.7-sonnet")]
45 Claude3_7Sonnet,
46 #[serde(alias = "gemini-2.0-flash", rename = "gemini-2.0-flash-001")]
47 Gemini20Flash,
48}
49
50impl Model {
51 pub fn uses_streaming(&self) -> bool {
52 match self {
53 Self::Gpt4o
54 | Self::Gpt4
55 | Self::Gpt3_5Turbo
56 | Self::Claude3_5Sonnet
57 | Self::Claude3_7Sonnet => true,
58 Self::O3Mini | Self::O1 | Self::Gemini20Flash => false,
59 }
60 }
61
62 pub fn from_id(id: &str) -> Result<Self> {
63 match id {
64 "gpt-4o" => Ok(Self::Gpt4o),
65 "gpt-4" => Ok(Self::Gpt4),
66 "gpt-3.5-turbo" => Ok(Self::Gpt3_5Turbo),
67 "o1" => Ok(Self::O1),
68 "o3-mini" => Ok(Self::O3Mini),
69 "claude-3-5-sonnet" => Ok(Self::Claude3_5Sonnet),
70 "claude-3-7-sonnet" => Ok(Self::Claude3_7Sonnet),
71 "gemini-2.0-flash-001" => Ok(Self::Gemini20Flash),
72 _ => Err(anyhow!("Invalid model id: {}", id)),
73 }
74 }
75
76 pub fn id(&self) -> &'static str {
77 match self {
78 Self::Gpt3_5Turbo => "gpt-3.5-turbo",
79 Self::Gpt4 => "gpt-4",
80 Self::Gpt4o => "gpt-4o",
81 Self::O3Mini => "o3-mini",
82 Self::O1 => "o1",
83 Self::Claude3_5Sonnet => "claude-3-5-sonnet",
84 Self::Claude3_7Sonnet => "claude-3-7-sonnet",
85 Self::Gemini20Flash => "gemini-2.0-flash-001",
86 }
87 }
88
89 pub fn display_name(&self) -> &'static str {
90 match self {
91 Self::Gpt3_5Turbo => "GPT-3.5",
92 Self::Gpt4 => "GPT-4",
93 Self::Gpt4o => "GPT-4o",
94 Self::O3Mini => "o3-mini",
95 Self::O1 => "o1",
96 Self::Claude3_5Sonnet => "Claude 3.5 Sonnet",
97 Self::Claude3_7Sonnet => "Claude 3.7 Sonnet",
98 Self::Gemini20Flash => "Gemini 2.0 Flash",
99 }
100 }
101
102 pub fn max_token_count(&self) -> usize {
103 match self {
104 Self::Gpt4o => 64_000,
105 Self::Gpt4 => 32_768,
106 Self::Gpt3_5Turbo => 12_288,
107 Self::O3Mini => 64_000,
108 Self::O1 => 20_000,
109 Self::Claude3_5Sonnet => 200_000,
110 Self::Claude3_7Sonnet => 90_000,
111 Model::Gemini20Flash => 128_000,
112 }
113 }
114}
115
116#[derive(Serialize, Deserialize)]
117pub struct Request {
118 pub intent: bool,
119 pub n: usize,
120 pub stream: bool,
121 pub temperature: f32,
122 pub model: Model,
123 pub messages: Vec<ChatMessage>,
124}
125
126impl Request {
127 pub fn new(model: Model, messages: Vec<ChatMessage>) -> Self {
128 Self {
129 intent: true,
130 n: 1,
131 stream: model.uses_streaming(),
132 temperature: 0.1,
133 model,
134 messages,
135 }
136 }
137}
138
139#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
140pub struct ChatMessage {
141 pub role: Role,
142 pub content: String,
143}
144
145#[derive(Deserialize, Debug)]
146#[serde(tag = "type", rename_all = "snake_case")]
147pub struct ResponseEvent {
148 pub choices: Vec<ResponseChoice>,
149 pub created: u64,
150 pub id: String,
151}
152
153#[derive(Debug, Deserialize)]
154pub struct ResponseChoice {
155 pub index: usize,
156 pub finish_reason: Option<String>,
157 pub delta: Option<ResponseDelta>,
158 pub message: Option<ResponseDelta>,
159}
160
161#[derive(Debug, Deserialize)]
162pub struct ResponseDelta {
163 pub content: Option<String>,
164 pub role: Option<Role>,
165}
166
167#[derive(Deserialize)]
168struct ApiTokenResponse {
169 token: String,
170 expires_at: i64,
171}
172
173#[derive(Clone)]
174struct ApiToken {
175 api_key: String,
176 expires_at: DateTime<chrono::Utc>,
177}
178
179impl ApiToken {
180 pub fn remaining_seconds(&self) -> i64 {
181 self.expires_at
182 .timestamp()
183 .saturating_sub(chrono::Utc::now().timestamp())
184 }
185}
186
187impl TryFrom<ApiTokenResponse> for ApiToken {
188 type Error = anyhow::Error;
189
190 fn try_from(response: ApiTokenResponse) -> Result<Self, Self::Error> {
191 let expires_at = DateTime::from_timestamp(response.expires_at, 0)
192 .ok_or_else(|| anyhow!("invalid expires_at"))?;
193
194 Ok(Self {
195 api_key: response.token,
196 expires_at,
197 })
198 }
199}
200
201struct GlobalCopilotChat(gpui::Entity<CopilotChat>);
202
203impl Global for GlobalCopilotChat {}
204
205pub struct CopilotChat {
206 oauth_token: Option<String>,
207 api_token: Option<ApiToken>,
208 client: Arc<dyn HttpClient>,
209}
210
211pub fn init(fs: Arc<dyn Fs>, client: Arc<dyn HttpClient>, cx: &mut App) {
212 let copilot_chat = cx.new(|cx| CopilotChat::new(fs, client, cx));
213 cx.set_global(GlobalCopilotChat(copilot_chat));
214}
215
216pub fn copilot_chat_config_dir() -> &'static PathBuf {
217 static COPILOT_CHAT_CONFIG_DIR: OnceLock<PathBuf> = OnceLock::new();
218
219 COPILOT_CHAT_CONFIG_DIR.get_or_init(|| {
220 if cfg!(target_os = "windows") {
221 home_dir().join("AppData").join("Local")
222 } else {
223 home_dir().join(".config")
224 }
225 .join("github-copilot")
226 })
227}
228
229fn copilot_chat_config_paths() -> [PathBuf; 2] {
230 let base_dir = copilot_chat_config_dir();
231 [base_dir.join("hosts.json"), base_dir.join("apps.json")]
232}
233
234impl CopilotChat {
235 pub fn global(cx: &App) -> Option<gpui::Entity<Self>> {
236 cx.try_global::<GlobalCopilotChat>()
237 .map(|model| model.0.clone())
238 }
239
240 pub fn new(fs: Arc<dyn Fs>, client: Arc<dyn HttpClient>, cx: &App) -> Self {
241 let config_paths: HashSet<PathBuf> = copilot_chat_config_paths().into_iter().collect();
242 let dir_path = copilot_chat_config_dir();
243
244 cx.spawn(async move |cx| {
245 let mut parent_watch_rx = watch_config_dir(
246 cx.background_executor(),
247 fs.clone(),
248 dir_path.clone(),
249 config_paths,
250 );
251 while let Some(contents) = parent_watch_rx.next().await {
252 let oauth_token = extract_oauth_token(contents);
253 cx.update(|cx| {
254 if let Some(this) = Self::global(cx).as_ref() {
255 this.update(cx, |this, cx| {
256 this.oauth_token = oauth_token;
257 cx.notify();
258 });
259 }
260 })?;
261 }
262 anyhow::Ok(())
263 })
264 .detach_and_log_err(cx);
265
266 Self {
267 oauth_token: None,
268 api_token: None,
269 client,
270 }
271 }
272
273 pub fn is_authenticated(&self) -> bool {
274 self.oauth_token.is_some()
275 }
276
277 pub async fn stream_completion(
278 request: Request,
279 mut cx: AsyncApp,
280 ) -> Result<BoxStream<'static, Result<ResponseEvent>>> {
281 let Some(this) = cx.update(|cx| Self::global(cx)).ok().flatten() else {
282 return Err(anyhow!("Copilot chat is not enabled"));
283 };
284
285 let (oauth_token, api_token, client) = this.read_with(&cx, |this, _| {
286 (
287 this.oauth_token.clone(),
288 this.api_token.clone(),
289 this.client.clone(),
290 )
291 })?;
292
293 let oauth_token = oauth_token.ok_or_else(|| anyhow!("No OAuth token available"))?;
294
295 let token = match api_token {
296 Some(api_token) if api_token.remaining_seconds() > 5 * 60 => api_token.clone(),
297 _ => {
298 let token = request_api_token(&oauth_token, client.clone()).await?;
299 this.update(&mut cx, |this, cx| {
300 this.api_token = Some(token.clone());
301 cx.notify();
302 })?;
303 token
304 }
305 };
306
307 stream_completion(client.clone(), token.api_key, request).await
308 }
309}
310
311async fn request_api_token(oauth_token: &str, client: Arc<dyn HttpClient>) -> Result<ApiToken> {
312 let request_builder = HttpRequest::builder()
313 .method(Method::GET)
314 .uri(COPILOT_CHAT_AUTH_URL)
315 .header("Authorization", format!("token {}", oauth_token))
316 .header("Accept", "application/json");
317
318 let request = request_builder.body(AsyncBody::empty())?;
319
320 let mut response = client.send(request).await?;
321
322 if response.status().is_success() {
323 let mut body = Vec::new();
324 response.body_mut().read_to_end(&mut body).await?;
325
326 let body_str = std::str::from_utf8(&body)?;
327
328 let parsed: ApiTokenResponse = serde_json::from_str(body_str)?;
329 ApiToken::try_from(parsed)
330 } else {
331 let mut body = Vec::new();
332 response.body_mut().read_to_end(&mut body).await?;
333
334 let body_str = std::str::from_utf8(&body)?;
335
336 Err(anyhow!("Failed to request API token: {}", body_str))
337 }
338}
339
340fn extract_oauth_token(contents: String) -> Option<String> {
341 serde_json::from_str::<serde_json::Value>(&contents)
342 .map(|v| {
343 v.as_object().and_then(|obj| {
344 obj.iter().find_map(|(key, value)| {
345 if key.starts_with("github.com") {
346 value["oauth_token"].as_str().map(|v| v.to_string())
347 } else {
348 None
349 }
350 })
351 })
352 })
353 .ok()
354 .flatten()
355}
356
357async fn stream_completion(
358 client: Arc<dyn HttpClient>,
359 api_key: String,
360 request: Request,
361) -> Result<BoxStream<'static, Result<ResponseEvent>>> {
362 let request_builder = HttpRequest::builder()
363 .method(Method::POST)
364 .uri(COPILOT_CHAT_COMPLETION_URL)
365 .header(
366 "Editor-Version",
367 format!(
368 "Zed/{}",
369 option_env!("CARGO_PKG_VERSION").unwrap_or("unknown")
370 ),
371 )
372 .header("Authorization", format!("Bearer {}", api_key))
373 .header("Content-Type", "application/json")
374 .header("Copilot-Integration-Id", "vscode-chat");
375
376 let is_streaming = request.stream;
377
378 let request = request_builder.body(AsyncBody::from(serde_json::to_string(&request)?))?;
379 let mut response = client.send(request).await?;
380
381 if !response.status().is_success() {
382 let mut body = Vec::new();
383 response.body_mut().read_to_end(&mut body).await?;
384 let body_str = std::str::from_utf8(&body)?;
385 return Err(anyhow!(
386 "Failed to connect to API: {} {}",
387 response.status(),
388 body_str
389 ));
390 }
391
392 if is_streaming {
393 let reader = BufReader::new(response.into_body());
394 Ok(reader
395 .lines()
396 .filter_map(|line| async move {
397 match line {
398 Ok(line) => {
399 let line = line.strip_prefix("data: ")?;
400 if line.starts_with("[DONE]") {
401 return None;
402 }
403
404 match serde_json::from_str::<ResponseEvent>(line) {
405 Ok(response) => {
406 if response.choices.is_empty()
407 || response.choices.first().unwrap().finish_reason.is_some()
408 {
409 None
410 } else {
411 Some(Ok(response))
412 }
413 }
414 Err(error) => Some(Err(anyhow!(error))),
415 }
416 }
417 Err(error) => Some(Err(anyhow!(error))),
418 }
419 })
420 .boxed())
421 } else {
422 let mut body = Vec::new();
423 response.body_mut().read_to_end(&mut body).await?;
424 let body_str = std::str::from_utf8(&body)?;
425 let response: ResponseEvent = serde_json::from_str(body_str)?;
426
427 Ok(futures::stream::once(async move { Ok(response) }).boxed())
428 }
429}