copilot_chat.rs

  1use std::path::PathBuf;
  2use std::sync::Arc;
  3use std::sync::OnceLock;
  4
  5use anyhow::Context as _;
  6use anyhow::{Result, anyhow};
  7use chrono::DateTime;
  8use collections::HashSet;
  9use fs::Fs;
 10use futures::{AsyncBufReadExt, AsyncReadExt, StreamExt, io::BufReader, stream::BoxStream};
 11use gpui::WeakEntity;
 12use gpui::{App, AsyncApp, Global, prelude::*};
 13use http_client::{AsyncBody, HttpClient, Method, Request as HttpRequest};
 14use itertools::Itertools;
 15use paths::home_dir;
 16use serde::{Deserialize, Serialize};
 17use settings::watch_config_dir;
 18
 19pub const COPILOT_OAUTH_ENV_VAR: &str = "GH_COPILOT_TOKEN";
 20
 21#[derive(Default, Clone, Debug, PartialEq)]
 22pub struct CopilotChatConfiguration {
 23    pub enterprise_uri: Option<String>,
 24}
 25
 26impl CopilotChatConfiguration {
 27    pub fn token_url(&self) -> String {
 28        if let Some(enterprise_uri) = &self.enterprise_uri {
 29            let domain = Self::parse_domain(enterprise_uri);
 30            format!("https://api.{}/copilot_internal/v2/token", domain)
 31        } else {
 32            "https://api.github.com/copilot_internal/v2/token".to_string()
 33        }
 34    }
 35
 36    pub fn oauth_domain(&self) -> String {
 37        if let Some(enterprise_uri) = &self.enterprise_uri {
 38            Self::parse_domain(enterprise_uri)
 39        } else {
 40            "github.com".to_string()
 41        }
 42    }
 43
 44    pub fn api_url_from_endpoint(&self, endpoint: &str) -> String {
 45        format!("{}/chat/completions", endpoint)
 46    }
 47
 48    pub fn models_url_from_endpoint(&self, endpoint: &str) -> String {
 49        format!("{}/models", endpoint)
 50    }
 51
 52    fn parse_domain(enterprise_uri: &str) -> String {
 53        let uri = enterprise_uri.trim_end_matches('/');
 54
 55        if let Some(domain) = uri.strip_prefix("https://") {
 56            domain.split('/').next().unwrap_or(domain).to_string()
 57        } else if let Some(domain) = uri.strip_prefix("http://") {
 58            domain.split('/').next().unwrap_or(domain).to_string()
 59        } else {
 60            uri.split('/').next().unwrap_or(uri).to_string()
 61        }
 62    }
 63}
 64
 65// Copilot's base model; defined by Microsoft in premium requests table
 66// This will be moved to the front of the Copilot model list, and will be used for
 67// 'fast' requests (e.g. title generation)
 68// https://docs.github.com/en/copilot/managing-copilot/monitoring-usage-and-entitlements/about-premium-requests
 69const DEFAULT_MODEL_ID: &str = "gpt-4.1";
 70
 71#[derive(Clone, Copy, Serialize, Deserialize, Debug, Eq, PartialEq)]
 72#[serde(rename_all = "lowercase")]
 73pub enum Role {
 74    User,
 75    Assistant,
 76    System,
 77}
 78
 79#[derive(Deserialize)]
 80struct ModelSchema {
 81    #[serde(deserialize_with = "deserialize_models_skip_errors")]
 82    data: Vec<Model>,
 83}
 84
 85fn deserialize_models_skip_errors<'de, D>(deserializer: D) -> Result<Vec<Model>, D::Error>
 86where
 87    D: serde::Deserializer<'de>,
 88{
 89    let raw_values = Vec::<serde_json::Value>::deserialize(deserializer)?;
 90    let models = raw_values
 91        .into_iter()
 92        .filter_map(|value| match serde_json::from_value::<Model>(value) {
 93            Ok(model) => Some(model),
 94            Err(err) => {
 95                log::warn!("GitHub Copilot Chat model failed to deserialize: {:?}", err);
 96                None
 97            }
 98        })
 99        .collect();
100
101    Ok(models)
102}
103
104#[derive(Clone, Serialize, Deserialize, Debug, Eq, PartialEq)]
105pub struct Model {
106    capabilities: ModelCapabilities,
107    id: String,
108    name: String,
109    policy: Option<ModelPolicy>,
110    vendor: ModelVendor,
111    model_picker_enabled: bool,
112}
113
114#[derive(Clone, Serialize, Deserialize, Debug, Eq, PartialEq)]
115struct ModelCapabilities {
116    family: String,
117    #[serde(default)]
118    limits: ModelLimits,
119    supports: ModelSupportedFeatures,
120}
121
122#[derive(Default, Clone, Serialize, Deserialize, Debug, Eq, PartialEq)]
123struct ModelLimits {
124    #[serde(default)]
125    max_context_window_tokens: usize,
126    #[serde(default)]
127    max_output_tokens: usize,
128    #[serde(default)]
129    max_prompt_tokens: u64,
130}
131
132#[derive(Clone, Serialize, Deserialize, Debug, Eq, PartialEq)]
133struct ModelPolicy {
134    state: String,
135}
136
137#[derive(Clone, Serialize, Deserialize, Debug, Eq, PartialEq)]
138struct ModelSupportedFeatures {
139    #[serde(default)]
140    streaming: bool,
141    #[serde(default)]
142    tool_calls: bool,
143    #[serde(default)]
144    parallel_tool_calls: bool,
145    #[serde(default)]
146    vision: bool,
147}
148
149#[derive(Clone, Copy, Serialize, Deserialize, Debug, Eq, PartialEq)]
150pub enum ModelVendor {
151    // Azure OpenAI should have no functional difference from OpenAI in Copilot Chat
152    #[serde(alias = "Azure OpenAI")]
153    OpenAI,
154    Google,
155    Anthropic,
156    #[serde(rename = "xAI")]
157    XAI,
158}
159
160#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
161#[serde(tag = "type")]
162pub enum ChatMessagePart {
163    #[serde(rename = "text")]
164    Text { text: String },
165    #[serde(rename = "image_url")]
166    Image { image_url: ImageUrl },
167}
168
169#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
170pub struct ImageUrl {
171    pub url: String,
172}
173
174impl Model {
175    pub fn uses_streaming(&self) -> bool {
176        self.capabilities.supports.streaming
177    }
178
179    pub fn id(&self) -> &str {
180        self.id.as_str()
181    }
182
183    pub fn display_name(&self) -> &str {
184        self.name.as_str()
185    }
186
187    pub fn max_token_count(&self) -> u64 {
188        self.capabilities.limits.max_prompt_tokens
189    }
190
191    pub fn supports_tools(&self) -> bool {
192        self.capabilities.supports.tool_calls
193    }
194
195    pub fn vendor(&self) -> ModelVendor {
196        self.vendor
197    }
198
199    pub fn supports_vision(&self) -> bool {
200        self.capabilities.supports.vision
201    }
202
203    pub fn supports_parallel_tool_calls(&self) -> bool {
204        self.capabilities.supports.parallel_tool_calls
205    }
206}
207
208#[derive(Serialize, Deserialize)]
209pub struct Request {
210    pub intent: bool,
211    pub n: usize,
212    pub stream: bool,
213    pub temperature: f32,
214    pub model: String,
215    pub messages: Vec<ChatMessage>,
216    #[serde(default, skip_serializing_if = "Vec::is_empty")]
217    pub tools: Vec<Tool>,
218    #[serde(default, skip_serializing_if = "Option::is_none")]
219    pub tool_choice: Option<ToolChoice>,
220}
221
222#[derive(Serialize, Deserialize)]
223pub struct Function {
224    pub name: String,
225    pub description: String,
226    pub parameters: serde_json::Value,
227}
228
229#[derive(Serialize, Deserialize)]
230#[serde(tag = "type", rename_all = "snake_case")]
231pub enum Tool {
232    Function { function: Function },
233}
234
235#[derive(Serialize, Deserialize)]
236#[serde(rename_all = "lowercase")]
237pub enum ToolChoice {
238    Auto,
239    Any,
240    None,
241}
242
243#[derive(Serialize, Deserialize, Debug)]
244#[serde(tag = "role", rename_all = "lowercase")]
245pub enum ChatMessage {
246    Assistant {
247        content: ChatMessageContent,
248        #[serde(default, skip_serializing_if = "Vec::is_empty")]
249        tool_calls: Vec<ToolCall>,
250    },
251    User {
252        content: ChatMessageContent,
253    },
254    System {
255        content: String,
256    },
257    Tool {
258        content: ChatMessageContent,
259        tool_call_id: String,
260    },
261}
262
263#[derive(Debug, Serialize, Deserialize)]
264#[serde(untagged)]
265pub enum ChatMessageContent {
266    Plain(String),
267    Multipart(Vec<ChatMessagePart>),
268}
269
270impl ChatMessageContent {
271    pub fn empty() -> Self {
272        ChatMessageContent::Multipart(vec![])
273    }
274}
275
276impl From<Vec<ChatMessagePart>> for ChatMessageContent {
277    fn from(mut parts: Vec<ChatMessagePart>) -> Self {
278        if let [ChatMessagePart::Text { text }] = parts.as_mut_slice() {
279            ChatMessageContent::Plain(std::mem::take(text))
280        } else {
281            ChatMessageContent::Multipart(parts)
282        }
283    }
284}
285
286impl From<String> for ChatMessageContent {
287    fn from(text: String) -> Self {
288        ChatMessageContent::Plain(text)
289    }
290}
291
292#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
293pub struct ToolCall {
294    pub id: String,
295    #[serde(flatten)]
296    pub content: ToolCallContent,
297}
298
299#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
300#[serde(tag = "type", rename_all = "lowercase")]
301pub enum ToolCallContent {
302    Function { function: FunctionContent },
303}
304
305#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
306pub struct FunctionContent {
307    pub name: String,
308    pub arguments: String,
309}
310
311#[derive(Deserialize, Debug)]
312#[serde(tag = "type", rename_all = "snake_case")]
313pub struct ResponseEvent {
314    pub choices: Vec<ResponseChoice>,
315    pub id: String,
316    pub usage: Option<Usage>,
317}
318
319#[derive(Deserialize, Debug)]
320pub struct Usage {
321    pub completion_tokens: u64,
322    pub prompt_tokens: u64,
323    pub total_tokens: u64,
324}
325
326#[derive(Debug, Deserialize)]
327pub struct ResponseChoice {
328    pub index: usize,
329    pub finish_reason: Option<String>,
330    pub delta: Option<ResponseDelta>,
331    pub message: Option<ResponseDelta>,
332}
333
334#[derive(Debug, Deserialize)]
335pub struct ResponseDelta {
336    pub content: Option<String>,
337    pub role: Option<Role>,
338    #[serde(default)]
339    pub tool_calls: Vec<ToolCallChunk>,
340}
341
342#[derive(Deserialize, Debug, Eq, PartialEq)]
343pub struct ToolCallChunk {
344    pub index: usize,
345    pub id: Option<String>,
346    pub function: Option<FunctionChunk>,
347}
348
349#[derive(Deserialize, Debug, Eq, PartialEq)]
350pub struct FunctionChunk {
351    pub name: Option<String>,
352    pub arguments: Option<String>,
353}
354
355#[derive(Deserialize)]
356struct ApiTokenResponse {
357    token: String,
358    expires_at: i64,
359    endpoints: ApiTokenResponseEndpoints,
360}
361
362#[derive(Deserialize)]
363struct ApiTokenResponseEndpoints {
364    api: String,
365}
366
367#[derive(Clone)]
368struct ApiToken {
369    api_key: String,
370    expires_at: DateTime<chrono::Utc>,
371    api_endpoint: String,
372}
373
374impl ApiToken {
375    pub fn remaining_seconds(&self) -> i64 {
376        self.expires_at
377            .timestamp()
378            .saturating_sub(chrono::Utc::now().timestamp())
379    }
380}
381
382impl TryFrom<ApiTokenResponse> for ApiToken {
383    type Error = anyhow::Error;
384
385    fn try_from(response: ApiTokenResponse) -> Result<Self, Self::Error> {
386        let expires_at =
387            DateTime::from_timestamp(response.expires_at, 0).context("invalid expires_at")?;
388
389        Ok(Self {
390            api_key: response.token,
391            expires_at,
392            api_endpoint: response.endpoints.api,
393        })
394    }
395}
396
397struct GlobalCopilotChat(gpui::Entity<CopilotChat>);
398
399impl Global for GlobalCopilotChat {}
400
401pub struct CopilotChat {
402    oauth_token: Option<String>,
403    api_token: Option<ApiToken>,
404    configuration: CopilotChatConfiguration,
405    models: Option<Vec<Model>>,
406    client: Arc<dyn HttpClient>,
407}
408
409pub fn init(
410    fs: Arc<dyn Fs>,
411    client: Arc<dyn HttpClient>,
412    configuration: CopilotChatConfiguration,
413    cx: &mut App,
414) {
415    let copilot_chat = cx.new(|cx| CopilotChat::new(fs, client, configuration, cx));
416    cx.set_global(GlobalCopilotChat(copilot_chat));
417}
418
419pub fn copilot_chat_config_dir() -> &'static PathBuf {
420    static COPILOT_CHAT_CONFIG_DIR: OnceLock<PathBuf> = OnceLock::new();
421
422    COPILOT_CHAT_CONFIG_DIR.get_or_init(|| {
423        let config_dir = if cfg!(target_os = "windows") {
424            dirs::data_local_dir().expect("failed to determine LocalAppData directory")
425        } else {
426            std::env::var("XDG_CONFIG_HOME")
427                .map(PathBuf::from)
428                .unwrap_or_else(|_| home_dir().join(".config"))
429        };
430
431        config_dir.join("github-copilot")
432    })
433}
434
435fn copilot_chat_config_paths() -> [PathBuf; 2] {
436    let base_dir = copilot_chat_config_dir();
437    [base_dir.join("hosts.json"), base_dir.join("apps.json")]
438}
439
440impl CopilotChat {
441    pub fn global(cx: &App) -> Option<gpui::Entity<Self>> {
442        cx.try_global::<GlobalCopilotChat>()
443            .map(|model| model.0.clone())
444    }
445
446    fn new(
447        fs: Arc<dyn Fs>,
448        client: Arc<dyn HttpClient>,
449        configuration: CopilotChatConfiguration,
450        cx: &mut Context<Self>,
451    ) -> Self {
452        let config_paths: HashSet<PathBuf> = copilot_chat_config_paths().into_iter().collect();
453        let dir_path = copilot_chat_config_dir();
454
455        cx.spawn(async move |this, cx| {
456            let mut parent_watch_rx = watch_config_dir(
457                cx.background_executor(),
458                fs.clone(),
459                dir_path.clone(),
460                config_paths,
461            );
462            while let Some(contents) = parent_watch_rx.next().await {
463                let oauth_domain =
464                    this.read_with(cx, |this, _| this.configuration.oauth_domain())?;
465                let oauth_token = extract_oauth_token(contents, &oauth_domain);
466
467                this.update(cx, |this, cx| {
468                    this.oauth_token = oauth_token.clone();
469                    cx.notify();
470                })?;
471
472                if oauth_token.is_some() {
473                    Self::update_models(&this, cx).await?;
474                }
475            }
476            anyhow::Ok(())
477        })
478        .detach_and_log_err(cx);
479
480        let this = Self {
481            oauth_token: std::env::var(COPILOT_OAUTH_ENV_VAR).ok(),
482            api_token: None,
483            models: None,
484            configuration,
485            client,
486        };
487
488        if this.oauth_token.is_some() {
489            cx.spawn(async move |this, cx| Self::update_models(&this, cx).await)
490                .detach_and_log_err(cx);
491        }
492
493        this
494    }
495
496    async fn update_models(this: &WeakEntity<Self>, cx: &mut AsyncApp) -> Result<()> {
497        let (oauth_token, client, configuration) = this.read_with(cx, |this, _| {
498            (
499                this.oauth_token.clone(),
500                this.client.clone(),
501                this.configuration.clone(),
502            )
503        })?;
504
505        let oauth_token = oauth_token
506            .ok_or_else(|| anyhow!("OAuth token is missing while updating Copilot Chat models"))?;
507
508        let token_url = configuration.token_url();
509        let api_token = request_api_token(&oauth_token, token_url.into(), client.clone()).await?;
510
511        let models_url = configuration.models_url_from_endpoint(&api_token.api_endpoint);
512        let models =
513            get_models(models_url.into(), api_token.api_key.clone(), client.clone()).await?;
514
515        this.update(cx, |this, cx| {
516            this.api_token = Some(api_token);
517            this.models = Some(models);
518            cx.notify();
519        })?;
520        anyhow::Ok(())
521    }
522
523    pub fn is_authenticated(&self) -> bool {
524        self.oauth_token.is_some()
525    }
526
527    pub fn models(&self) -> Option<&[Model]> {
528        self.models.as_deref()
529    }
530
531    pub async fn stream_completion(
532        request: Request,
533        is_user_initiated: bool,
534        mut cx: AsyncApp,
535    ) -> Result<BoxStream<'static, Result<ResponseEvent>>> {
536        let this = cx
537            .update(|cx| Self::global(cx))
538            .ok()
539            .flatten()
540            .context("Copilot chat is not enabled")?;
541
542        let (oauth_token, api_token, client, configuration) = this.read_with(&cx, |this, _| {
543            (
544                this.oauth_token.clone(),
545                this.api_token.clone(),
546                this.client.clone(),
547                this.configuration.clone(),
548            )
549        })?;
550
551        let oauth_token = oauth_token.context("No OAuth token available")?;
552
553        let token = match api_token {
554            Some(api_token) if api_token.remaining_seconds() > 5 * 60 => api_token.clone(),
555            _ => {
556                let token_url = configuration.token_url();
557                let token =
558                    request_api_token(&oauth_token, token_url.into(), client.clone()).await?;
559                this.update(&mut cx, |this, cx| {
560                    this.api_token = Some(token.clone());
561                    cx.notify();
562                })?;
563                token
564            }
565        };
566
567        let api_url = configuration.api_url_from_endpoint(&token.api_endpoint);
568        stream_completion(
569            client.clone(),
570            token.api_key,
571            api_url.into(),
572            request,
573            is_user_initiated,
574        )
575        .await
576    }
577
578    pub fn set_configuration(
579        &mut self,
580        configuration: CopilotChatConfiguration,
581        cx: &mut Context<Self>,
582    ) {
583        let same_configuration = self.configuration == configuration;
584        self.configuration = configuration;
585        if !same_configuration {
586            self.api_token = None;
587            cx.spawn(async move |this, cx| {
588                Self::update_models(&this, cx).await?;
589                Ok::<_, anyhow::Error>(())
590            })
591            .detach();
592        }
593    }
594}
595
596async fn get_models(
597    models_url: Arc<str>,
598    api_token: String,
599    client: Arc<dyn HttpClient>,
600) -> Result<Vec<Model>> {
601    let all_models = request_models(models_url, api_token, client).await?;
602
603    let mut models: Vec<Model> = all_models
604        .into_iter()
605        .filter(|model| {
606            model.model_picker_enabled
607                && model
608                    .policy
609                    .as_ref()
610                    .is_none_or(|policy| policy.state == "enabled")
611        })
612        .dedup_by(|a, b| a.capabilities.family == b.capabilities.family)
613        .collect();
614
615    if let Some(default_model_position) =
616        models.iter().position(|model| model.id == DEFAULT_MODEL_ID)
617    {
618        let default_model = models.remove(default_model_position);
619        models.insert(0, default_model);
620    }
621
622    Ok(models)
623}
624
625async fn request_models(
626    models_url: Arc<str>,
627    api_token: String,
628    client: Arc<dyn HttpClient>,
629) -> Result<Vec<Model>> {
630    let request_builder = HttpRequest::builder()
631        .method(Method::GET)
632        .uri(models_url.as_ref())
633        .header("Authorization", format!("Bearer {}", api_token))
634        .header("Content-Type", "application/json")
635        .header("Copilot-Integration-Id", "vscode-chat");
636
637    let request = request_builder.body(AsyncBody::empty())?;
638
639    let mut response = client.send(request).await?;
640
641    anyhow::ensure!(
642        response.status().is_success(),
643        "Failed to request models: {}",
644        response.status()
645    );
646    let mut body = Vec::new();
647    response.body_mut().read_to_end(&mut body).await?;
648
649    let body_str = std::str::from_utf8(&body)?;
650
651    let models = serde_json::from_str::<ModelSchema>(body_str)?.data;
652
653    Ok(models)
654}
655
656async fn request_api_token(
657    oauth_token: &str,
658    auth_url: Arc<str>,
659    client: Arc<dyn HttpClient>,
660) -> Result<ApiToken> {
661    let request_builder = HttpRequest::builder()
662        .method(Method::GET)
663        .uri(auth_url.as_ref())
664        .header("Authorization", format!("token {}", oauth_token))
665        .header("Accept", "application/json");
666
667    let request = request_builder.body(AsyncBody::empty())?;
668
669    let mut response = client.send(request).await?;
670
671    if response.status().is_success() {
672        let mut body = Vec::new();
673        response.body_mut().read_to_end(&mut body).await?;
674
675        let body_str = std::str::from_utf8(&body)?;
676
677        let parsed: ApiTokenResponse = serde_json::from_str(body_str)?;
678        ApiToken::try_from(parsed)
679    } else {
680        let mut body = Vec::new();
681        response.body_mut().read_to_end(&mut body).await?;
682
683        let body_str = std::str::from_utf8(&body)?;
684        anyhow::bail!("Failed to request API token: {body_str}");
685    }
686}
687
688fn extract_oauth_token(contents: String, domain: &str) -> Option<String> {
689    serde_json::from_str::<serde_json::Value>(&contents)
690        .map(|v| {
691            v.as_object().and_then(|obj| {
692                obj.iter().find_map(|(key, value)| {
693                    if key.starts_with(domain) {
694                        value["oauth_token"].as_str().map(|v| v.to_string())
695                    } else {
696                        None
697                    }
698                })
699            })
700        })
701        .ok()
702        .flatten()
703}
704
705async fn stream_completion(
706    client: Arc<dyn HttpClient>,
707    api_key: String,
708    completion_url: Arc<str>,
709    request: Request,
710    is_user_initiated: bool,
711) -> Result<BoxStream<'static, Result<ResponseEvent>>> {
712    let is_vision_request = request.messages.iter().any(|message| match message {
713      ChatMessage::User { content }
714      | ChatMessage::Assistant { content, .. }
715      | ChatMessage::Tool { content, .. } => {
716          matches!(content, ChatMessageContent::Multipart(parts) if parts.iter().any(|part| matches!(part, ChatMessagePart::Image { .. })))
717      }
718      _ => false,
719  });
720
721    let request_initiator = if is_user_initiated { "user" } else { "agent" };
722
723    let mut request_builder = HttpRequest::builder()
724        .method(Method::POST)
725        .uri(completion_url.as_ref())
726        .header(
727            "Editor-Version",
728            format!(
729                "Zed/{}",
730                option_env!("CARGO_PKG_VERSION").unwrap_or("unknown")
731            ),
732        )
733        .header("Authorization", format!("Bearer {}", api_key))
734        .header("Content-Type", "application/json")
735        .header("Copilot-Integration-Id", "vscode-chat")
736        .header("X-Initiator", request_initiator);
737
738    if is_vision_request {
739        request_builder =
740            request_builder.header("Copilot-Vision-Request", is_vision_request.to_string());
741    }
742
743    let is_streaming = request.stream;
744
745    let json = serde_json::to_string(&request)?;
746    let request = request_builder.body(AsyncBody::from(json))?;
747    let mut response = client.send(request).await?;
748
749    if !response.status().is_success() {
750        let mut body = Vec::new();
751        response.body_mut().read_to_end(&mut body).await?;
752        let body_str = std::str::from_utf8(&body)?;
753        anyhow::bail!(
754            "Failed to connect to API: {} {}",
755            response.status(),
756            body_str
757        );
758    }
759
760    if is_streaming {
761        let reader = BufReader::new(response.into_body());
762        Ok(reader
763            .lines()
764            .filter_map(|line| async move {
765                match line {
766                    Ok(line) => {
767                        let line = line.strip_prefix("data: ")?;
768                        if line.starts_with("[DONE]") {
769                            return None;
770                        }
771
772                        match serde_json::from_str::<ResponseEvent>(line) {
773                            Ok(response) => {
774                                if response.choices.is_empty() {
775                                    None
776                                } else {
777                                    Some(Ok(response))
778                                }
779                            }
780                            Err(error) => Some(Err(anyhow!(error))),
781                        }
782                    }
783                    Err(error) => Some(Err(anyhow!(error))),
784                }
785            })
786            .boxed())
787    } else {
788        let mut body = Vec::new();
789        response.body_mut().read_to_end(&mut body).await?;
790        let body_str = std::str::from_utf8(&body)?;
791        let response: ResponseEvent = serde_json::from_str(body_str)?;
792
793        Ok(futures::stream::once(async move { Ok(response) }).boxed())
794    }
795}
796
797#[cfg(test)]
798mod tests {
799    use super::*;
800
801    #[test]
802    fn test_resilient_model_schema_deserialize() {
803        let json = r#"{
804              "data": [
805                {
806                  "capabilities": {
807                    "family": "gpt-4",
808                    "limits": {
809                      "max_context_window_tokens": 32768,
810                      "max_output_tokens": 4096,
811                      "max_prompt_tokens": 32768
812                    },
813                    "object": "model_capabilities",
814                    "supports": { "streaming": true, "tool_calls": true },
815                    "tokenizer": "cl100k_base",
816                    "type": "chat"
817                  },
818                  "id": "gpt-4",
819                  "model_picker_enabled": false,
820                  "name": "GPT 4",
821                  "object": "model",
822                  "preview": false,
823                  "vendor": "Azure OpenAI",
824                  "version": "gpt-4-0613"
825                },
826                {
827                    "some-unknown-field": 123
828                },
829                {
830                  "capabilities": {
831                    "family": "claude-3.7-sonnet",
832                    "limits": {
833                      "max_context_window_tokens": 200000,
834                      "max_output_tokens": 16384,
835                      "max_prompt_tokens": 90000,
836                      "vision": {
837                        "max_prompt_image_size": 3145728,
838                        "max_prompt_images": 1,
839                        "supported_media_types": ["image/jpeg", "image/png", "image/webp"]
840                      }
841                    },
842                    "object": "model_capabilities",
843                    "supports": {
844                      "parallel_tool_calls": true,
845                      "streaming": true,
846                      "tool_calls": true,
847                      "vision": true
848                    },
849                    "tokenizer": "o200k_base",
850                    "type": "chat"
851                  },
852                  "id": "claude-3.7-sonnet",
853                  "model_picker_enabled": true,
854                  "name": "Claude 3.7 Sonnet",
855                  "object": "model",
856                  "policy": {
857                    "state": "enabled",
858                    "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)."
859                  },
860                  "preview": false,
861                  "vendor": "Anthropic",
862                  "version": "claude-3.7-sonnet"
863                }
864              ],
865              "object": "list"
866            }"#;
867
868        let schema: ModelSchema = serde_json::from_str(json).unwrap();
869
870        assert_eq!(schema.data.len(), 2);
871        assert_eq!(schema.data[0].id, "gpt-4");
872        assert_eq!(schema.data[1].id, "claude-3.7-sonnet");
873    }
874}