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