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