copilot_chat.rs

  1use std::path::PathBuf;
  2use std::sync::Arc;
  3use std::sync::OnceLock;
  4
  5use anyhow::{Result, anyhow};
  6use chrono::DateTime;
  7use collections::HashSet;
  8use fs::Fs;
  9use futures::{AsyncBufReadExt, AsyncReadExt, StreamExt, io::BufReader, stream::BoxStream};
 10use gpui::{App, AsyncApp, Global, prelude::*};
 11use http_client::{AsyncBody, HttpClient, Method, Request as HttpRequest};
 12use itertools::Itertools;
 13use paths::home_dir;
 14use serde::{Deserialize, Serialize};
 15use settings::watch_config_dir;
 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";
 19pub const COPILOT_CHAT_MODELS_URL: &str = "https://api.githubcopilot.com/models";
 20
 21// Copilot's base model; defined by Microsoft in premium requests table
 22// This will be moved to the front of the Copilot model list, and will be used for
 23// 'fast' requests (e.g. title generation)
 24// https://docs.github.com/en/copilot/managing-copilot/monitoring-usage-and-entitlements/about-premium-requests
 25const DEFAULT_MODEL_ID: &str = "gpt-4.1";
 26
 27#[derive(Clone, Copy, Serialize, Deserialize, Debug, Eq, PartialEq)]
 28#[serde(rename_all = "lowercase")]
 29pub enum Role {
 30    User,
 31    Assistant,
 32    System,
 33}
 34
 35#[derive(Deserialize)]
 36struct ModelSchema {
 37    #[serde(deserialize_with = "deserialize_models_skip_errors")]
 38    data: Vec<Model>,
 39}
 40
 41fn deserialize_models_skip_errors<'de, D>(deserializer: D) -> Result<Vec<Model>, D::Error>
 42where
 43    D: serde::Deserializer<'de>,
 44{
 45    let raw_values = Vec::<serde_json::Value>::deserialize(deserializer)?;
 46    let models = raw_values
 47        .into_iter()
 48        .filter_map(|value| match serde_json::from_value::<Model>(value) {
 49            Ok(model) => Some(model),
 50            Err(err) => {
 51                log::warn!("GitHub Copilot Chat model failed to deserialize: {:?}", err);
 52                None
 53            }
 54        })
 55        .collect();
 56
 57    Ok(models)
 58}
 59
 60#[derive(Clone, Serialize, Deserialize, Debug, Eq, PartialEq)]
 61pub struct Model {
 62    capabilities: ModelCapabilities,
 63    id: String,
 64    name: String,
 65    policy: Option<ModelPolicy>,
 66    vendor: ModelVendor,
 67    model_picker_enabled: bool,
 68}
 69
 70#[derive(Clone, Serialize, Deserialize, Debug, Eq, PartialEq)]
 71struct ModelCapabilities {
 72    family: String,
 73    #[serde(default)]
 74    limits: ModelLimits,
 75    supports: ModelSupportedFeatures,
 76}
 77
 78#[derive(Default, Clone, Serialize, Deserialize, Debug, Eq, PartialEq)]
 79struct ModelLimits {
 80    #[serde(default)]
 81    max_context_window_tokens: usize,
 82    #[serde(default)]
 83    max_output_tokens: usize,
 84    #[serde(default)]
 85    max_prompt_tokens: usize,
 86}
 87
 88#[derive(Clone, Serialize, Deserialize, Debug, Eq, PartialEq)]
 89struct ModelPolicy {
 90    state: String,
 91}
 92
 93#[derive(Clone, Serialize, Deserialize, Debug, Eq, PartialEq)]
 94struct ModelSupportedFeatures {
 95    #[serde(default)]
 96    streaming: bool,
 97    #[serde(default)]
 98    tool_calls: bool,
 99    #[serde(default)]
100    parallel_tool_calls: bool,
101    #[serde(default)]
102    vision: bool,
103}
104
105#[derive(Clone, Copy, Serialize, Deserialize, Debug, Eq, PartialEq)]
106pub enum ModelVendor {
107    // Azure OpenAI should have no functional difference from OpenAI in Copilot Chat
108    #[serde(alias = "Azure OpenAI")]
109    OpenAI,
110    Google,
111    Anthropic,
112}
113
114#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
115#[serde(tag = "type")]
116pub enum ChatMessageContent {
117    #[serde(rename = "text")]
118    Text { text: String },
119    #[serde(rename = "image_url")]
120    Image { image_url: ImageUrl },
121}
122
123#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
124pub struct ImageUrl {
125    pub url: String,
126}
127
128impl Model {
129    pub fn uses_streaming(&self) -> bool {
130        self.capabilities.supports.streaming
131    }
132
133    pub fn id(&self) -> &str {
134        self.id.as_str()
135    }
136
137    pub fn display_name(&self) -> &str {
138        self.name.as_str()
139    }
140
141    pub fn max_token_count(&self) -> usize {
142        self.capabilities.limits.max_prompt_tokens
143    }
144
145    pub fn supports_tools(&self) -> bool {
146        self.capabilities.supports.tool_calls
147    }
148
149    pub fn vendor(&self) -> ModelVendor {
150        self.vendor
151    }
152
153    pub fn supports_vision(&self) -> bool {
154        self.capabilities.supports.vision
155    }
156
157    pub fn supports_parallel_tool_calls(&self) -> bool {
158        self.capabilities.supports.parallel_tool_calls
159    }
160}
161
162#[derive(Serialize, Deserialize)]
163pub struct Request {
164    pub intent: bool,
165    pub n: usize,
166    pub stream: bool,
167    pub temperature: f32,
168    pub model: String,
169    pub messages: Vec<ChatMessage>,
170    #[serde(default, skip_serializing_if = "Vec::is_empty")]
171    pub tools: Vec<Tool>,
172    #[serde(default, skip_serializing_if = "Option::is_none")]
173    pub tool_choice: Option<ToolChoice>,
174}
175
176#[derive(Serialize, Deserialize)]
177pub struct Function {
178    pub name: String,
179    pub description: String,
180    pub parameters: serde_json::Value,
181}
182
183#[derive(Serialize, Deserialize)]
184#[serde(tag = "type", rename_all = "snake_case")]
185pub enum Tool {
186    Function { function: Function },
187}
188
189#[derive(Serialize, Deserialize)]
190#[serde(rename_all = "lowercase")]
191pub enum ToolChoice {
192    Auto,
193    Any,
194    None,
195}
196
197#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
198#[serde(tag = "role", rename_all = "lowercase")]
199pub enum ChatMessage {
200    Assistant {
201        content: Option<String>,
202        #[serde(default, skip_serializing_if = "Vec::is_empty")]
203        tool_calls: Vec<ToolCall>,
204    },
205    User {
206        content: Vec<ChatMessageContent>,
207    },
208    System {
209        content: String,
210    },
211    Tool {
212        content: String,
213        tool_call_id: String,
214    },
215}
216
217#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
218pub struct ToolCall {
219    pub id: String,
220    #[serde(flatten)]
221    pub content: ToolCallContent,
222}
223
224#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
225#[serde(tag = "type", rename_all = "lowercase")]
226pub enum ToolCallContent {
227    Function { function: FunctionContent },
228}
229
230#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
231pub struct FunctionContent {
232    pub name: String,
233    pub arguments: String,
234}
235
236#[derive(Deserialize, Debug)]
237#[serde(tag = "type", rename_all = "snake_case")]
238pub struct ResponseEvent {
239    pub choices: Vec<ResponseChoice>,
240    pub created: u64,
241    pub id: String,
242}
243
244#[derive(Debug, Deserialize)]
245pub struct ResponseChoice {
246    pub index: usize,
247    pub finish_reason: Option<String>,
248    pub delta: Option<ResponseDelta>,
249    pub message: Option<ResponseDelta>,
250}
251
252#[derive(Debug, Deserialize)]
253pub struct ResponseDelta {
254    pub content: Option<String>,
255    pub role: Option<Role>,
256    #[serde(default)]
257    pub tool_calls: Vec<ToolCallChunk>,
258}
259
260#[derive(Deserialize, Debug, Eq, PartialEq)]
261pub struct ToolCallChunk {
262    pub index: usize,
263    pub id: Option<String>,
264    pub function: Option<FunctionChunk>,
265}
266
267#[derive(Deserialize, Debug, Eq, PartialEq)]
268pub struct FunctionChunk {
269    pub name: Option<String>,
270    pub arguments: Option<String>,
271}
272
273#[derive(Deserialize)]
274struct ApiTokenResponse {
275    token: String,
276    expires_at: i64,
277}
278
279#[derive(Clone)]
280struct ApiToken {
281    api_key: String,
282    expires_at: DateTime<chrono::Utc>,
283}
284
285impl ApiToken {
286    pub fn remaining_seconds(&self) -> i64 {
287        self.expires_at
288            .timestamp()
289            .saturating_sub(chrono::Utc::now().timestamp())
290    }
291}
292
293impl TryFrom<ApiTokenResponse> for ApiToken {
294    type Error = anyhow::Error;
295
296    fn try_from(response: ApiTokenResponse) -> Result<Self, Self::Error> {
297        let expires_at = DateTime::from_timestamp(response.expires_at, 0)
298            .ok_or_else(|| anyhow!("invalid expires_at"))?;
299
300        Ok(Self {
301            api_key: response.token,
302            expires_at,
303        })
304    }
305}
306
307struct GlobalCopilotChat(gpui::Entity<CopilotChat>);
308
309impl Global for GlobalCopilotChat {}
310
311pub struct CopilotChat {
312    oauth_token: Option<String>,
313    api_token: Option<ApiToken>,
314    models: Option<Vec<Model>>,
315    client: Arc<dyn HttpClient>,
316}
317
318pub fn init(fs: Arc<dyn Fs>, client: Arc<dyn HttpClient>, cx: &mut App) {
319    let copilot_chat = cx.new(|cx| CopilotChat::new(fs, client, cx));
320    cx.set_global(GlobalCopilotChat(copilot_chat));
321}
322
323pub fn copilot_chat_config_dir() -> &'static PathBuf {
324    static COPILOT_CHAT_CONFIG_DIR: OnceLock<PathBuf> = OnceLock::new();
325
326    COPILOT_CHAT_CONFIG_DIR.get_or_init(|| {
327        if cfg!(target_os = "windows") {
328            home_dir().join("AppData").join("Local")
329        } else {
330            home_dir().join(".config")
331        }
332        .join("github-copilot")
333    })
334}
335
336fn copilot_chat_config_paths() -> [PathBuf; 2] {
337    let base_dir = copilot_chat_config_dir();
338    [base_dir.join("hosts.json"), base_dir.join("apps.json")]
339}
340
341impl CopilotChat {
342    pub fn global(cx: &App) -> Option<gpui::Entity<Self>> {
343        cx.try_global::<GlobalCopilotChat>()
344            .map(|model| model.0.clone())
345    }
346
347    pub fn new(fs: Arc<dyn Fs>, client: Arc<dyn HttpClient>, cx: &App) -> Self {
348        let config_paths: HashSet<PathBuf> = copilot_chat_config_paths().into_iter().collect();
349        let dir_path = copilot_chat_config_dir();
350
351        cx.spawn({
352            let client = client.clone();
353            async move |cx| {
354                let mut parent_watch_rx = watch_config_dir(
355                    cx.background_executor(),
356                    fs.clone(),
357                    dir_path.clone(),
358                    config_paths,
359                );
360                while let Some(contents) = parent_watch_rx.next().await {
361                    let oauth_token = extract_oauth_token(contents);
362                    cx.update(|cx| {
363                        if let Some(this) = Self::global(cx).as_ref() {
364                            this.update(cx, |this, cx| {
365                                this.oauth_token = oauth_token.clone();
366                                cx.notify();
367                            });
368                        }
369                    })?;
370
371                    if let Some(ref oauth_token) = oauth_token {
372                        let api_token = request_api_token(oauth_token, client.clone()).await?;
373                        cx.update(|cx| {
374                            if let Some(this) = Self::global(cx).as_ref() {
375                                this.update(cx, |this, cx| {
376                                    this.api_token = Some(api_token.clone());
377                                    cx.notify();
378                                });
379                            }
380                        })?;
381                        let models = get_models(api_token.api_key, client.clone()).await?;
382                        cx.update(|cx| {
383                            if let Some(this) = Self::global(cx).as_ref() {
384                                this.update(cx, |this, cx| {
385                                    this.models = Some(models);
386                                    cx.notify();
387                                });
388                            }
389                        })?;
390                    }
391                }
392                anyhow::Ok(())
393            }
394        })
395        .detach_and_log_err(cx);
396
397        Self {
398            oauth_token: None,
399            api_token: None,
400            models: None,
401            client,
402        }
403    }
404
405    pub fn is_authenticated(&self) -> bool {
406        self.oauth_token.is_some()
407    }
408
409    pub fn models(&self) -> Option<&[Model]> {
410        self.models.as_deref()
411    }
412
413    pub async fn stream_completion(
414        request: Request,
415        mut cx: AsyncApp,
416    ) -> Result<BoxStream<'static, Result<ResponseEvent>>> {
417        let Some(this) = cx.update(|cx| Self::global(cx)).ok().flatten() else {
418            return Err(anyhow!("Copilot chat is not enabled"));
419        };
420
421        let (oauth_token, api_token, client) = this.read_with(&cx, |this, _| {
422            (
423                this.oauth_token.clone(),
424                this.api_token.clone(),
425                this.client.clone(),
426            )
427        })?;
428
429        let oauth_token = oauth_token.ok_or_else(|| anyhow!("No OAuth token available"))?;
430
431        let token = match api_token {
432            Some(api_token) if api_token.remaining_seconds() > 5 * 60 => api_token.clone(),
433            _ => {
434                let token = request_api_token(&oauth_token, client.clone()).await?;
435                this.update(&mut cx, |this, cx| {
436                    this.api_token = Some(token.clone());
437                    cx.notify();
438                })?;
439                token
440            }
441        };
442
443        stream_completion(client.clone(), token.api_key, request).await
444    }
445}
446
447async fn get_models(api_token: String, client: Arc<dyn HttpClient>) -> Result<Vec<Model>> {
448    let all_models = request_models(api_token, client).await?;
449
450    let mut models: Vec<Model> = all_models
451        .into_iter()
452        .filter(|model| {
453            // Ensure user has access to the model; Policy is present only for models that must be
454            // enabled in the GitHub dashboard
455            model.model_picker_enabled
456                && model
457                    .policy
458                    .as_ref()
459                    .is_none_or(|policy| policy.state == "enabled")
460        })
461        // The first model from the API response, in any given family, appear to be the non-tagged
462        // models, which are likely the best choice (e.g. gpt-4o rather than gpt-4o-2024-11-20)
463        .dedup_by(|a, b| a.capabilities.family == b.capabilities.family)
464        .collect();
465
466    if let Some(default_model_position) =
467        models.iter().position(|model| model.id == DEFAULT_MODEL_ID)
468    {
469        let default_model = models.remove(default_model_position);
470        models.insert(0, default_model);
471    }
472
473    Ok(models)
474}
475
476async fn request_models(api_token: String, client: Arc<dyn HttpClient>) -> Result<Vec<Model>> {
477    let request_builder = HttpRequest::builder()
478        .method(Method::GET)
479        .uri(COPILOT_CHAT_MODELS_URL)
480        .header("Authorization", format!("Bearer {}", api_token))
481        .header("Content-Type", "application/json")
482        .header("Copilot-Integration-Id", "vscode-chat");
483
484    let request = request_builder.body(AsyncBody::empty())?;
485
486    let mut response = client.send(request).await?;
487
488    if response.status().is_success() {
489        let mut body = Vec::new();
490        response.body_mut().read_to_end(&mut body).await?;
491
492        let body_str = std::str::from_utf8(&body)?;
493
494        let models = serde_json::from_str::<ModelSchema>(body_str)?.data;
495
496        Ok(models)
497    } else {
498        Err(anyhow!("Failed to request models: {}", response.status()))
499    }
500}
501
502async fn request_api_token(oauth_token: &str, client: Arc<dyn HttpClient>) -> Result<ApiToken> {
503    let request_builder = HttpRequest::builder()
504        .method(Method::GET)
505        .uri(COPILOT_CHAT_AUTH_URL)
506        .header("Authorization", format!("token {}", oauth_token))
507        .header("Accept", "application/json");
508
509    let request = request_builder.body(AsyncBody::empty())?;
510
511    let mut response = client.send(request).await?;
512
513    if response.status().is_success() {
514        let mut body = Vec::new();
515        response.body_mut().read_to_end(&mut body).await?;
516
517        let body_str = std::str::from_utf8(&body)?;
518
519        let parsed: ApiTokenResponse = serde_json::from_str(body_str)?;
520        ApiToken::try_from(parsed)
521    } else {
522        let mut body = Vec::new();
523        response.body_mut().read_to_end(&mut body).await?;
524
525        let body_str = std::str::from_utf8(&body)?;
526
527        Err(anyhow!("Failed to request API token: {}", body_str))
528    }
529}
530
531fn extract_oauth_token(contents: String) -> Option<String> {
532    serde_json::from_str::<serde_json::Value>(&contents)
533        .map(|v| {
534            v.as_object().and_then(|obj| {
535                obj.iter().find_map(|(key, value)| {
536                    if key.starts_with("github.com") {
537                        value["oauth_token"].as_str().map(|v| v.to_string())
538                    } else {
539                        None
540                    }
541                })
542            })
543        })
544        .ok()
545        .flatten()
546}
547
548async fn stream_completion(
549    client: Arc<dyn HttpClient>,
550    api_key: String,
551    request: Request,
552) -> Result<BoxStream<'static, Result<ResponseEvent>>> {
553    let request_builder = HttpRequest::builder()
554        .method(Method::POST)
555        .uri(COPILOT_CHAT_COMPLETION_URL)
556        .header(
557            "Editor-Version",
558            format!(
559                "Zed/{}",
560                option_env!("CARGO_PKG_VERSION").unwrap_or("unknown")
561            ),
562        )
563        .header("Authorization", format!("Bearer {}", api_key))
564        .header("Content-Type", "application/json")
565        .header("Copilot-Integration-Id", "vscode-chat")
566        .header("Copilot-Vision-Request", "true");
567
568    let is_streaming = request.stream;
569
570    let json = serde_json::to_string(&request)?;
571    let request = request_builder.body(AsyncBody::from(json))?;
572    let mut response = client.send(request).await?;
573
574    if !response.status().is_success() {
575        let mut body = Vec::new();
576        response.body_mut().read_to_end(&mut body).await?;
577        let body_str = std::str::from_utf8(&body)?;
578        return Err(anyhow!(
579            "Failed to connect to API: {} {}",
580            response.status(),
581            body_str
582        ));
583    }
584
585    if is_streaming {
586        let reader = BufReader::new(response.into_body());
587        Ok(reader
588            .lines()
589            .filter_map(|line| async move {
590                match line {
591                    Ok(line) => {
592                        let line = line.strip_prefix("data: ")?;
593                        if line.starts_with("[DONE]") {
594                            return None;
595                        }
596
597                        match serde_json::from_str::<ResponseEvent>(line) {
598                            Ok(response) => {
599                                if response.choices.is_empty() {
600                                    None
601                                } else {
602                                    Some(Ok(response))
603                                }
604                            }
605                            Err(error) => Some(Err(anyhow!(error))),
606                        }
607                    }
608                    Err(error) => Some(Err(anyhow!(error))),
609                }
610            })
611            .boxed())
612    } else {
613        let mut body = Vec::new();
614        response.body_mut().read_to_end(&mut body).await?;
615        let body_str = std::str::from_utf8(&body)?;
616        let response: ResponseEvent = serde_json::from_str(body_str)?;
617
618        Ok(futures::stream::once(async move { Ok(response) }).boxed())
619    }
620}
621
622#[cfg(test)]
623mod tests {
624    use super::*;
625
626    #[test]
627    fn test_resilient_model_schema_deserialize() {
628        let json = r#"{
629              "data": [
630                {
631                  "capabilities": {
632                    "family": "gpt-4",
633                    "limits": {
634                      "max_context_window_tokens": 32768,
635                      "max_output_tokens": 4096,
636                      "max_prompt_tokens": 32768
637                    },
638                    "object": "model_capabilities",
639                    "supports": { "streaming": true, "tool_calls": true },
640                    "tokenizer": "cl100k_base",
641                    "type": "chat"
642                  },
643                  "id": "gpt-4",
644                  "model_picker_enabled": false,
645                  "name": "GPT 4",
646                  "object": "model",
647                  "preview": false,
648                  "vendor": "Azure OpenAI",
649                  "version": "gpt-4-0613"
650                },
651                {
652                    "some-unknown-field": 123
653                },
654                {
655                  "capabilities": {
656                    "family": "claude-3.7-sonnet",
657                    "limits": {
658                      "max_context_window_tokens": 200000,
659                      "max_output_tokens": 16384,
660                      "max_prompt_tokens": 90000,
661                      "vision": {
662                        "max_prompt_image_size": 3145728,
663                        "max_prompt_images": 1,
664                        "supported_media_types": ["image/jpeg", "image/png", "image/webp"]
665                      }
666                    },
667                    "object": "model_capabilities",
668                    "supports": {
669                      "parallel_tool_calls": true,
670                      "streaming": true,
671                      "tool_calls": true,
672                      "vision": true
673                    },
674                    "tokenizer": "o200k_base",
675                    "type": "chat"
676                  },
677                  "id": "claude-3.7-sonnet",
678                  "model_picker_enabled": true,
679                  "name": "Claude 3.7 Sonnet",
680                  "object": "model",
681                  "policy": {
682                    "state": "enabled",
683                    "terms": "Enable access to the latest Claude 3.7 Sonnet model from Anthropic. [Learn more about how GitHub Copilot serves Claude 3.7 Sonnet](https://docs.github.com/copilot/using-github-copilot/using-claude-sonnet-in-github-copilot)."
684                  },
685                  "preview": false,
686                  "vendor": "Anthropic",
687                  "version": "claude-3.7-sonnet"
688                }
689              ],
690              "object": "list"
691            }"#;
692
693        let schema: ModelSchema = serde_json::from_str(&json).unwrap();
694
695        assert_eq!(schema.data.len(), 2);
696        assert_eq!(schema.data[0].id, "gpt-4");
697        assert_eq!(schema.data[1].id, "claude-3.7-sonnet");
698    }
699}