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, mut cx| Self::update_models(&this, &mut 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 mut cx: AsyncApp,
532 ) -> Result<BoxStream<'static, Result<ResponseEvent>>> {
533 let this = cx
534 .update(|cx| Self::global(cx))
535 .ok()
536 .flatten()
537 .context("Copilot chat is not enabled")?;
538
539 let (oauth_token, api_token, client, configuration) = this.read_with(&cx, |this, _| {
540 (
541 this.oauth_token.clone(),
542 this.api_token.clone(),
543 this.client.clone(),
544 this.configuration.clone(),
545 )
546 })?;
547
548 let oauth_token = oauth_token.context("No OAuth token available")?;
549
550 let token = match api_token {
551 Some(api_token) if api_token.remaining_seconds() > 5 * 60 => api_token.clone(),
552 _ => {
553 let token_url = configuration.token_url();
554 let token =
555 request_api_token(&oauth_token, token_url.into(), client.clone()).await?;
556 this.update(&mut cx, |this, cx| {
557 this.api_token = Some(token.clone());
558 cx.notify();
559 })?;
560 token
561 }
562 };
563
564 let api_url = configuration.api_url_from_endpoint(&token.api_endpoint);
565 stream_completion(client.clone(), token.api_key, api_url.into(), request).await
566 }
567
568 pub fn set_configuration(
569 &mut self,
570 configuration: CopilotChatConfiguration,
571 cx: &mut Context<Self>,
572 ) {
573 let same_configuration = self.configuration == configuration;
574 self.configuration = configuration;
575 if !same_configuration {
576 self.api_token = None;
577 cx.spawn(async move |this, cx| {
578 Self::update_models(&this, cx).await?;
579 Ok::<_, anyhow::Error>(())
580 })
581 .detach();
582 }
583 }
584}
585
586async fn get_models(
587 models_url: Arc<str>,
588 api_token: String,
589 client: Arc<dyn HttpClient>,
590) -> Result<Vec<Model>> {
591 let all_models = request_models(models_url, api_token, client).await?;
592
593 let mut models: Vec<Model> = all_models
594 .into_iter()
595 .filter(|model| {
596 model.model_picker_enabled
597 && model
598 .policy
599 .as_ref()
600 .is_none_or(|policy| policy.state == "enabled")
601 })
602 .dedup_by(|a, b| a.capabilities.family == b.capabilities.family)
603 .collect();
604
605 if let Some(default_model_position) =
606 models.iter().position(|model| model.id == DEFAULT_MODEL_ID)
607 {
608 let default_model = models.remove(default_model_position);
609 models.insert(0, default_model);
610 }
611
612 Ok(models)
613}
614
615async fn request_models(
616 models_url: Arc<str>,
617 api_token: String,
618 client: Arc<dyn HttpClient>,
619) -> Result<Vec<Model>> {
620 let request_builder = HttpRequest::builder()
621 .method(Method::GET)
622 .uri(models_url.as_ref())
623 .header("Authorization", format!("Bearer {}", api_token))
624 .header("Content-Type", "application/json")
625 .header("Copilot-Integration-Id", "vscode-chat");
626
627 let request = request_builder.body(AsyncBody::empty())?;
628
629 let mut response = client.send(request).await?;
630
631 anyhow::ensure!(
632 response.status().is_success(),
633 "Failed to request models: {}",
634 response.status()
635 );
636 let mut body = Vec::new();
637 response.body_mut().read_to_end(&mut body).await?;
638
639 let body_str = std::str::from_utf8(&body)?;
640
641 let models = serde_json::from_str::<ModelSchema>(body_str)?.data;
642
643 Ok(models)
644}
645
646async fn request_api_token(
647 oauth_token: &str,
648 auth_url: Arc<str>,
649 client: Arc<dyn HttpClient>,
650) -> Result<ApiToken> {
651 let request_builder = HttpRequest::builder()
652 .method(Method::GET)
653 .uri(auth_url.as_ref())
654 .header("Authorization", format!("token {}", oauth_token))
655 .header("Accept", "application/json");
656
657 let request = request_builder.body(AsyncBody::empty())?;
658
659 let mut response = client.send(request).await?;
660
661 if response.status().is_success() {
662 let mut body = Vec::new();
663 response.body_mut().read_to_end(&mut body).await?;
664
665 let body_str = std::str::from_utf8(&body)?;
666
667 let parsed: ApiTokenResponse = serde_json::from_str(body_str)?;
668 ApiToken::try_from(parsed)
669 } else {
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 anyhow::bail!("Failed to request API token: {body_str}");
675 }
676}
677
678fn extract_oauth_token(contents: String, domain: &str) -> Option<String> {
679 serde_json::from_str::<serde_json::Value>(&contents)
680 .map(|v| {
681 v.as_object().and_then(|obj| {
682 obj.iter().find_map(|(key, value)| {
683 if key.starts_with(domain) {
684 value["oauth_token"].as_str().map(|v| v.to_string())
685 } else {
686 None
687 }
688 })
689 })
690 })
691 .ok()
692 .flatten()
693}
694
695async fn stream_completion(
696 client: Arc<dyn HttpClient>,
697 api_key: String,
698 completion_url: Arc<str>,
699 request: Request,
700) -> Result<BoxStream<'static, Result<ResponseEvent>>> {
701 let is_vision_request = request.messages.iter().any(|message| match message {
702 ChatMessage::User { content }
703 | ChatMessage::Assistant { content, .. }
704 | ChatMessage::Tool { content, .. } => {
705 matches!(content, ChatMessageContent::Multipart(parts) if parts.iter().any(|part| matches!(part, ChatMessagePart::Image { .. })))
706 }
707 _ => false,
708 });
709
710 let mut request_builder = HttpRequest::builder()
711 .method(Method::POST)
712 .uri(completion_url.as_ref())
713 .header(
714 "Editor-Version",
715 format!(
716 "Zed/{}",
717 option_env!("CARGO_PKG_VERSION").unwrap_or("unknown")
718 ),
719 )
720 .header("Authorization", format!("Bearer {}", api_key))
721 .header("Content-Type", "application/json")
722 .header("Copilot-Integration-Id", "vscode-chat");
723
724 if is_vision_request {
725 request_builder =
726 request_builder.header("Copilot-Vision-Request", is_vision_request.to_string());
727 }
728
729 let is_streaming = request.stream;
730
731 let json = serde_json::to_string(&request)?;
732 let request = request_builder.body(AsyncBody::from(json))?;
733 let mut response = client.send(request).await?;
734
735 if !response.status().is_success() {
736 let mut body = Vec::new();
737 response.body_mut().read_to_end(&mut body).await?;
738 let body_str = std::str::from_utf8(&body)?;
739 anyhow::bail!(
740 "Failed to connect to API: {} {}",
741 response.status(),
742 body_str
743 );
744 }
745
746 if is_streaming {
747 let reader = BufReader::new(response.into_body());
748 Ok(reader
749 .lines()
750 .filter_map(|line| async move {
751 match line {
752 Ok(line) => {
753 let line = line.strip_prefix("data: ")?;
754 if line.starts_with("[DONE]") {
755 return None;
756 }
757
758 match serde_json::from_str::<ResponseEvent>(line) {
759 Ok(response) => {
760 if response.choices.is_empty() {
761 None
762 } else {
763 Some(Ok(response))
764 }
765 }
766 Err(error) => Some(Err(anyhow!(error))),
767 }
768 }
769 Err(error) => Some(Err(anyhow!(error))),
770 }
771 })
772 .boxed())
773 } else {
774 let mut body = Vec::new();
775 response.body_mut().read_to_end(&mut body).await?;
776 let body_str = std::str::from_utf8(&body)?;
777 let response: ResponseEvent = serde_json::from_str(body_str)?;
778
779 Ok(futures::stream::once(async move { Ok(response) }).boxed())
780 }
781}
782
783#[cfg(test)]
784mod tests {
785 use super::*;
786
787 #[test]
788 fn test_resilient_model_schema_deserialize() {
789 let json = r#"{
790 "data": [
791 {
792 "capabilities": {
793 "family": "gpt-4",
794 "limits": {
795 "max_context_window_tokens": 32768,
796 "max_output_tokens": 4096,
797 "max_prompt_tokens": 32768
798 },
799 "object": "model_capabilities",
800 "supports": { "streaming": true, "tool_calls": true },
801 "tokenizer": "cl100k_base",
802 "type": "chat"
803 },
804 "id": "gpt-4",
805 "model_picker_enabled": false,
806 "name": "GPT 4",
807 "object": "model",
808 "preview": false,
809 "vendor": "Azure OpenAI",
810 "version": "gpt-4-0613"
811 },
812 {
813 "some-unknown-field": 123
814 },
815 {
816 "capabilities": {
817 "family": "claude-3.7-sonnet",
818 "limits": {
819 "max_context_window_tokens": 200000,
820 "max_output_tokens": 16384,
821 "max_prompt_tokens": 90000,
822 "vision": {
823 "max_prompt_image_size": 3145728,
824 "max_prompt_images": 1,
825 "supported_media_types": ["image/jpeg", "image/png", "image/webp"]
826 }
827 },
828 "object": "model_capabilities",
829 "supports": {
830 "parallel_tool_calls": true,
831 "streaming": true,
832 "tool_calls": true,
833 "vision": true
834 },
835 "tokenizer": "o200k_base",
836 "type": "chat"
837 },
838 "id": "claude-3.7-sonnet",
839 "model_picker_enabled": true,
840 "name": "Claude 3.7 Sonnet",
841 "object": "model",
842 "policy": {
843 "state": "enabled",
844 "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)."
845 },
846 "preview": false,
847 "vendor": "Anthropic",
848 "version": "claude-3.7-sonnet"
849 }
850 ],
851 "object": "list"
852 }"#;
853
854 let schema: ModelSchema = serde_json::from_str(&json).unwrap();
855
856 assert_eq!(schema.data.len(), 2);
857 assert_eq!(schema.data[0].id, "gpt-4");
858 assert_eq!(schema.data[1].id, "claude-3.7-sonnet");
859 }
860}