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