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