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