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