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 ChatMessageContent {
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, Eq, PartialEq)]
198#[serde(tag = "role", rename_all = "lowercase")]
199pub enum ChatMessage {
200 Assistant {
201 content: Option<String>,
202 #[serde(default, skip_serializing_if = "Vec::is_empty")]
203 tool_calls: Vec<ToolCall>,
204 },
205 User {
206 content: Vec<ChatMessageContent>,
207 },
208 System {
209 content: String,
210 },
211 Tool {
212 content: String,
213 tool_call_id: String,
214 },
215}
216
217#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
218pub struct ToolCall {
219 pub id: String,
220 #[serde(flatten)]
221 pub content: ToolCallContent,
222}
223
224#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
225#[serde(tag = "type", rename_all = "lowercase")]
226pub enum ToolCallContent {
227 Function { function: FunctionContent },
228}
229
230#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
231pub struct FunctionContent {
232 pub name: String,
233 pub arguments: String,
234}
235
236#[derive(Deserialize, Debug)]
237#[serde(tag = "type", rename_all = "snake_case")]
238pub struct ResponseEvent {
239 pub choices: Vec<ResponseChoice>,
240 pub id: String,
241}
242
243#[derive(Debug, Deserialize)]
244pub struct ResponseChoice {
245 pub index: usize,
246 pub finish_reason: Option<String>,
247 pub delta: Option<ResponseDelta>,
248 pub message: Option<ResponseDelta>,
249}
250
251#[derive(Debug, Deserialize)]
252pub struct ResponseDelta {
253 pub content: Option<String>,
254 pub role: Option<Role>,
255 #[serde(default)]
256 pub tool_calls: Vec<ToolCallChunk>,
257}
258
259#[derive(Deserialize, Debug, Eq, PartialEq)]
260pub struct ToolCallChunk {
261 pub index: usize,
262 pub id: Option<String>,
263 pub function: Option<FunctionChunk>,
264}
265
266#[derive(Deserialize, Debug, Eq, PartialEq)]
267pub struct FunctionChunk {
268 pub name: Option<String>,
269 pub arguments: Option<String>,
270}
271
272#[derive(Deserialize)]
273struct ApiTokenResponse {
274 token: String,
275 expires_at: i64,
276}
277
278#[derive(Clone)]
279struct ApiToken {
280 api_key: String,
281 expires_at: DateTime<chrono::Utc>,
282}
283
284impl ApiToken {
285 pub fn remaining_seconds(&self) -> i64 {
286 self.expires_at
287 .timestamp()
288 .saturating_sub(chrono::Utc::now().timestamp())
289 }
290}
291
292impl TryFrom<ApiTokenResponse> for ApiToken {
293 type Error = anyhow::Error;
294
295 fn try_from(response: ApiTokenResponse) -> Result<Self, Self::Error> {
296 let expires_at = DateTime::from_timestamp(response.expires_at, 0)
297 .ok_or_else(|| anyhow!("invalid expires_at"))?;
298
299 Ok(Self {
300 api_key: response.token,
301 expires_at,
302 })
303 }
304}
305
306struct GlobalCopilotChat(gpui::Entity<CopilotChat>);
307
308impl Global for GlobalCopilotChat {}
309
310pub struct CopilotChat {
311 oauth_token: Option<String>,
312 api_token: Option<ApiToken>,
313 models: Option<Vec<Model>>,
314 client: Arc<dyn HttpClient>,
315}
316
317pub fn init(fs: Arc<dyn Fs>, client: Arc<dyn HttpClient>, cx: &mut App) {
318 let copilot_chat = cx.new(|cx| CopilotChat::new(fs, client, cx));
319 cx.set_global(GlobalCopilotChat(copilot_chat));
320}
321
322pub fn copilot_chat_config_dir() -> &'static PathBuf {
323 static COPILOT_CHAT_CONFIG_DIR: OnceLock<PathBuf> = OnceLock::new();
324
325 COPILOT_CHAT_CONFIG_DIR.get_or_init(|| {
326 if cfg!(target_os = "windows") {
327 home_dir().join("AppData").join("Local")
328 } else {
329 home_dir().join(".config")
330 }
331 .join("github-copilot")
332 })
333}
334
335fn copilot_chat_config_paths() -> [PathBuf; 2] {
336 let base_dir = copilot_chat_config_dir();
337 [base_dir.join("hosts.json"), base_dir.join("apps.json")]
338}
339
340impl CopilotChat {
341 pub fn global(cx: &App) -> Option<gpui::Entity<Self>> {
342 cx.try_global::<GlobalCopilotChat>()
343 .map(|model| model.0.clone())
344 }
345
346 pub fn new(fs: Arc<dyn Fs>, client: Arc<dyn HttpClient>, cx: &App) -> Self {
347 let config_paths: HashSet<PathBuf> = copilot_chat_config_paths().into_iter().collect();
348 let dir_path = copilot_chat_config_dir();
349
350 cx.spawn({
351 let client = client.clone();
352 async move |cx| {
353 let mut parent_watch_rx = watch_config_dir(
354 cx.background_executor(),
355 fs.clone(),
356 dir_path.clone(),
357 config_paths,
358 );
359 while let Some(contents) = parent_watch_rx.next().await {
360 let oauth_token = extract_oauth_token(contents);
361 cx.update(|cx| {
362 if let Some(this) = Self::global(cx).as_ref() {
363 this.update(cx, |this, cx| {
364 this.oauth_token = oauth_token.clone();
365 cx.notify();
366 });
367 }
368 })?;
369
370 if let Some(ref oauth_token) = oauth_token {
371 let api_token = request_api_token(oauth_token, client.clone()).await?;
372 cx.update(|cx| {
373 if let Some(this) = Self::global(cx).as_ref() {
374 this.update(cx, |this, cx| {
375 this.api_token = Some(api_token.clone());
376 cx.notify();
377 });
378 }
379 })?;
380 let models = get_models(api_token.api_key, client.clone()).await?;
381 cx.update(|cx| {
382 if let Some(this) = Self::global(cx).as_ref() {
383 this.update(cx, |this, cx| {
384 this.models = Some(models);
385 cx.notify();
386 });
387 }
388 })?;
389 }
390 }
391 anyhow::Ok(())
392 }
393 })
394 .detach_and_log_err(cx);
395
396 Self {
397 oauth_token: None,
398 api_token: None,
399 models: None,
400 client,
401 }
402 }
403
404 pub fn is_authenticated(&self) -> bool {
405 self.oauth_token.is_some()
406 }
407
408 pub fn models(&self) -> Option<&[Model]> {
409 self.models.as_deref()
410 }
411
412 pub async fn stream_completion(
413 request: Request,
414 mut cx: AsyncApp,
415 ) -> Result<BoxStream<'static, Result<ResponseEvent>>> {
416 let Some(this) = cx.update(|cx| Self::global(cx)).ok().flatten() else {
417 return Err(anyhow!("Copilot chat is not enabled"));
418 };
419
420 let (oauth_token, api_token, client) = this.read_with(&cx, |this, _| {
421 (
422 this.oauth_token.clone(),
423 this.api_token.clone(),
424 this.client.clone(),
425 )
426 })?;
427
428 let oauth_token = oauth_token.ok_or_else(|| anyhow!("No OAuth token available"))?;
429
430 let token = match api_token {
431 Some(api_token) if api_token.remaining_seconds() > 5 * 60 => api_token.clone(),
432 _ => {
433 let token = request_api_token(&oauth_token, client.clone()).await?;
434 this.update(&mut cx, |this, cx| {
435 this.api_token = Some(token.clone());
436 cx.notify();
437 })?;
438 token
439 }
440 };
441
442 stream_completion(client.clone(), token.api_key, request).await
443 }
444}
445
446async fn get_models(api_token: String, client: Arc<dyn HttpClient>) -> Result<Vec<Model>> {
447 let all_models = request_models(api_token, client).await?;
448
449 let mut models: Vec<Model> = all_models
450 .into_iter()
451 .filter(|model| {
452 // Ensure user has access to the model; Policy is present only for models that must be
453 // enabled in the GitHub dashboard
454 model.model_picker_enabled
455 && model
456 .policy
457 .as_ref()
458 .is_none_or(|policy| policy.state == "enabled")
459 })
460 // The first model from the API response, in any given family, appear to be the non-tagged
461 // models, which are likely the best choice (e.g. gpt-4o rather than gpt-4o-2024-11-20)
462 .dedup_by(|a, b| a.capabilities.family == b.capabilities.family)
463 .collect();
464
465 if let Some(default_model_position) =
466 models.iter().position(|model| model.id == DEFAULT_MODEL_ID)
467 {
468 let default_model = models.remove(default_model_position);
469 models.insert(0, default_model);
470 }
471
472 Ok(models)
473}
474
475async fn request_models(api_token: String, client: Arc<dyn HttpClient>) -> Result<Vec<Model>> {
476 let request_builder = HttpRequest::builder()
477 .method(Method::GET)
478 .uri(COPILOT_CHAT_MODELS_URL)
479 .header("Authorization", format!("Bearer {}", api_token))
480 .header("Content-Type", "application/json")
481 .header("Copilot-Integration-Id", "vscode-chat");
482
483 let request = request_builder.body(AsyncBody::empty())?;
484
485 let mut response = client.send(request).await?;
486
487 if response.status().is_success() {
488 let mut body = Vec::new();
489 response.body_mut().read_to_end(&mut body).await?;
490
491 let body_str = std::str::from_utf8(&body)?;
492
493 let models = serde_json::from_str::<ModelSchema>(body_str)?.data;
494
495 Ok(models)
496 } else {
497 Err(anyhow!("Failed to request models: {}", response.status()))
498 }
499}
500
501async fn request_api_token(oauth_token: &str, client: Arc<dyn HttpClient>) -> Result<ApiToken> {
502 let request_builder = HttpRequest::builder()
503 .method(Method::GET)
504 .uri(COPILOT_CHAT_AUTH_URL)
505 .header("Authorization", format!("token {}", oauth_token))
506 .header("Accept", "application/json");
507
508 let request = request_builder.body(AsyncBody::empty())?;
509
510 let mut response = client.send(request).await?;
511
512 if response.status().is_success() {
513 let mut body = Vec::new();
514 response.body_mut().read_to_end(&mut body).await?;
515
516 let body_str = std::str::from_utf8(&body)?;
517
518 let parsed: ApiTokenResponse = serde_json::from_str(body_str)?;
519 ApiToken::try_from(parsed)
520 } else {
521 let mut body = Vec::new();
522 response.body_mut().read_to_end(&mut body).await?;
523
524 let body_str = std::str::from_utf8(&body)?;
525
526 Err(anyhow!("Failed to request API token: {}", body_str))
527 }
528}
529
530fn extract_oauth_token(contents: String) -> Option<String> {
531 serde_json::from_str::<serde_json::Value>(&contents)
532 .map(|v| {
533 v.as_object().and_then(|obj| {
534 obj.iter().find_map(|(key, value)| {
535 if key.starts_with("github.com") {
536 value["oauth_token"].as_str().map(|v| v.to_string())
537 } else {
538 None
539 }
540 })
541 })
542 })
543 .ok()
544 .flatten()
545}
546
547async fn stream_completion(
548 client: Arc<dyn HttpClient>,
549 api_key: String,
550 request: Request,
551) -> Result<BoxStream<'static, Result<ResponseEvent>>> {
552 let request_builder = HttpRequest::builder()
553 .method(Method::POST)
554 .uri(COPILOT_CHAT_COMPLETION_URL)
555 .header(
556 "Editor-Version",
557 format!(
558 "Zed/{}",
559 option_env!("CARGO_PKG_VERSION").unwrap_or("unknown")
560 ),
561 )
562 .header("Authorization", format!("Bearer {}", api_key))
563 .header("Content-Type", "application/json")
564 .header("Copilot-Integration-Id", "vscode-chat")
565 .header("Copilot-Vision-Request", "true");
566
567 let is_streaming = request.stream;
568
569 let json = serde_json::to_string(&request)?;
570 let request = request_builder.body(AsyncBody::from(json))?;
571 let mut response = client.send(request).await?;
572
573 if !response.status().is_success() {
574 let mut body = Vec::new();
575 response.body_mut().read_to_end(&mut body).await?;
576 let body_str = std::str::from_utf8(&body)?;
577 return Err(anyhow!(
578 "Failed to connect to API: {} {}",
579 response.status(),
580 body_str
581 ));
582 }
583
584 if is_streaming {
585 let reader = BufReader::new(response.into_body());
586 Ok(reader
587 .lines()
588 .filter_map(|line| async move {
589 match line {
590 Ok(line) => {
591 let line = line.strip_prefix("data: ")?;
592 if line.starts_with("[DONE]") {
593 return None;
594 }
595
596 match serde_json::from_str::<ResponseEvent>(line) {
597 Ok(response) => {
598 if response.choices.is_empty() {
599 None
600 } else {
601 Some(Ok(response))
602 }
603 }
604 Err(error) => Some(Err(anyhow!(error))),
605 }
606 }
607 Err(error) => Some(Err(anyhow!(error))),
608 }
609 })
610 .boxed())
611 } else {
612 let mut body = Vec::new();
613 response.body_mut().read_to_end(&mut body).await?;
614 let body_str = std::str::from_utf8(&body)?;
615 let response: ResponseEvent = serde_json::from_str(body_str)?;
616
617 Ok(futures::stream::once(async move { Ok(response) }).boxed())
618 }
619}
620
621#[cfg(test)]
622mod tests {
623 use super::*;
624
625 #[test]
626 fn test_resilient_model_schema_deserialize() {
627 let json = r#"{
628 "data": [
629 {
630 "capabilities": {
631 "family": "gpt-4",
632 "limits": {
633 "max_context_window_tokens": 32768,
634 "max_output_tokens": 4096,
635 "max_prompt_tokens": 32768
636 },
637 "object": "model_capabilities",
638 "supports": { "streaming": true, "tool_calls": true },
639 "tokenizer": "cl100k_base",
640 "type": "chat"
641 },
642 "id": "gpt-4",
643 "model_picker_enabled": false,
644 "name": "GPT 4",
645 "object": "model",
646 "preview": false,
647 "vendor": "Azure OpenAI",
648 "version": "gpt-4-0613"
649 },
650 {
651 "some-unknown-field": 123
652 },
653 {
654 "capabilities": {
655 "family": "claude-3.7-sonnet",
656 "limits": {
657 "max_context_window_tokens": 200000,
658 "max_output_tokens": 16384,
659 "max_prompt_tokens": 90000,
660 "vision": {
661 "max_prompt_image_size": 3145728,
662 "max_prompt_images": 1,
663 "supported_media_types": ["image/jpeg", "image/png", "image/webp"]
664 }
665 },
666 "object": "model_capabilities",
667 "supports": {
668 "parallel_tool_calls": true,
669 "streaming": true,
670 "tool_calls": true,
671 "vision": true
672 },
673 "tokenizer": "o200k_base",
674 "type": "chat"
675 },
676 "id": "claude-3.7-sonnet",
677 "model_picker_enabled": true,
678 "name": "Claude 3.7 Sonnet",
679 "object": "model",
680 "policy": {
681 "state": "enabled",
682 "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)."
683 },
684 "preview": false,
685 "vendor": "Anthropic",
686 "version": "claude-3.7-sonnet"
687 }
688 ],
689 "object": "list"
690 }"#;
691
692 let schema: ModelSchema = serde_json::from_str(&json).unwrap();
693
694 assert_eq!(schema.data.len(), 2);
695 assert_eq!(schema.data[0].id, "gpt-4");
696 assert_eq!(schema.data[1].id, "claude-3.7-sonnet");
697 }
698}