copilot_chat.rs

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