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