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