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