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