copilot_chat.rs

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