ai 0.5.1

Simple to use LLM library for Rust with streaming, tool calling, OAuth helpers, and a lightweight agent loop
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
use std::collections::HashMap;

use async_trait::async_trait;
use serde::Deserialize;
use serde_json::Value;
use tokio_util::sync::CancellationToken;

use crate::types::Model;
use crate::{Error, Result};

use super::*;

const GITHUB_COPILOT_CLIENT_ID: &str = "Iv1.b507a08c87ecfe98";
const GITHUB_COPILOT_USER_AGENT: &str = "GitHubCopilotChat/0.35.0";

const COPILOT_TOKEN_EXPIRY_SKEW_MS: u64 = 5 * 60 * 1000;

const DEFAULT_GITHUB_DOMAIN: &str = "github.com";

const DEFAULT_COPILOT_BASE_URL: &str = "https://api.individual.githubcopilot.com";

const GITHUB_COPILOT_REFRESH_TOKEN_KEY: &str = "githubRefreshToken";

const GITHUB_COPILOT_ACCESS_EXPIRES_KEY: &str = "githubAccessExpires";

const GITHUB_ACCESS_TOKEN_EXPIRY_SKEW_MS: u64 = 5 * 60 * 1000;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GitHubCopilotOAuthProvider;

impl GitHubCopilotOAuthProvider {
    pub const fn id(self) -> &'static str {
        "github-copilot"
    }

    pub const fn name(self) -> &'static str {
        "GitHub Copilot"
    }

    pub async fn login(self, callbacks: OAuthLoginCallbacks) -> Result<OAuthCredentials> {
        login_github_copilot(callbacks).await
    }

    pub async fn refresh_token(self, credentials: &OAuthCredentials) -> Result<OAuthCredentials> {
        refresh_github_copilot_credentials(credentials).await
    }

    pub fn get_api_key(self, credentials: &OAuthCredentials) -> String {
        credentials.access.clone()
    }

    pub fn modify_models(
        self,
        models: impl IntoIterator<Item = Model>,
        credentials: &OAuthCredentials,
    ) -> Vec<Model> {
        modify_github_copilot_models(models, credentials)
    }
}

pub fn github_copilot_oauth_provider() -> GitHubCopilotOAuthProvider {
    GitHubCopilotOAuthProvider
}

#[async_trait]
impl OAuthProviderInterface for GitHubCopilotOAuthProvider {
    fn id(&self) -> &'static str {
        "github-copilot"
    }

    fn name(&self) -> &'static str {
        "GitHub Copilot"
    }

    async fn login(&self, callbacks: OAuthLoginCallbacks) -> Result<OAuthCredentials> {
        login_github_copilot(callbacks).await
    }

    async fn refresh_token(&self, credentials: &OAuthCredentials) -> Result<OAuthCredentials> {
        refresh_github_copilot_credentials(credentials).await
    }

    fn get_api_key(&self, credentials: &OAuthCredentials) -> String {
        credentials.access.clone()
    }

    fn modify_models(&self, models: Vec<Model>, credentials: &OAuthCredentials) -> Vec<Model> {
        modify_github_copilot_models(models, credentials)
    }
}

const GITHUB_COPILOT_ENTERPRISE_URL_KEY: &str = "enterpriseUrl";

pub(crate) fn github_copilot_enterprise_domain(credentials: &OAuthCredentials) -> Option<&str> {
    credentials
        .extra
        .get(GITHUB_COPILOT_ENTERPRISE_URL_KEY)
        .and_then(Value::as_str)
}

pub(crate) fn get_github_copilot_base_url(
    token: Option<&str>,
    enterprise_domain: Option<&str>,
) -> String {
    if let Some(token) = token
        && let Some(base_url) = get_base_url_from_token(token)
    {
        return base_url;
    }
    if let Some(domain) = enterprise_domain.and_then(normalize_domain) {
        return format!("https://copilot-api.{domain}");
    }
    DEFAULT_COPILOT_BASE_URL.to_string()
}

/// A GitHub user access token minted via the device flow (or a refresh-token
/// grant), plus the refresh token and lifetime GitHub returns when the Copilot
/// app opts into expiring user tokens.
#[derive(Debug, Clone, PartialEq, Eq)]
struct GithubUserToken {
    access_token: String,
    refresh_token: Option<String>,
    expires_in: Option<u64>,
}

/// The GitHub refresh token (`ghr_...`) persisted alongside the credentials, if
/// the account uses expiring user tokens.
fn github_copilot_refresh_token(credentials: &OAuthCredentials) -> Option<&str> {
    credentials
        .extra
        .get(GITHUB_COPILOT_REFRESH_TOKEN_KEY)
        .and_then(Value::as_str)
}

/// The absolute expiry (Unix epoch millis, skew-adjusted) of the stored GitHub
/// user access token, if known.
fn github_copilot_access_expires(credentials: &OAuthCredentials) -> Option<u64> {
    credentials
        .extra
        .get(GITHUB_COPILOT_ACCESS_EXPIRES_KEY)
        .and_then(Value::as_u64)
}

/// Converts an `expires_in` (seconds) into a skew-adjusted absolute expiry in
/// Unix epoch millis.
fn github_access_expiry_ms(expires_in_seconds: u64) -> u64 {
    crate::utils::time::now_millis()
        .saturating_add(expires_in_seconds.saturating_mul(1000))
        .saturating_sub(GITHUB_ACCESS_TOKEN_EXPIRY_SKEW_MS)
}

/// Whether an error is GitHub rejecting our credentials (401/403), i.e. a signal
/// to try renewing the GitHub user token before giving up.
fn is_github_auth_error(error: &Error) -> bool {
    matches!(error, Error::ApiStatus { status, .. } if matches!(status.as_u16(), 401 | 403))
}

/// POSTs `fields` as `application/x-www-form-urlencoded` to a GitHub OAuth
/// endpoint with the client headers Copilot expects, returning the checked
/// response. Shared by the device-code, token-poll, and refresh-grant calls.
async fn github_oauth_form_post(
    client: &reqwest::Client,
    url: &str,
    fields: &[(&str, &str)],
) -> Result<reqwest::Response> {
    let response = client
        .post(url)
        .header("Accept", "application/json")
        .header("User-Agent", GITHUB_COPILOT_USER_AGENT)
        .header("Content-Type", "application/x-www-form-urlencoded")
        .body(form_body(fields))
        .send()
        .await?;
    error_for_status(response).await
}

/// Mints a short-lived Copilot API token from a GitHub user access token.
async fn mint_copilot_token(
    github_access_token: &str,
    enterprise_domain: Option<&str>,
) -> Result<CopilotTokenResponse> {
    let domain = enterprise_domain
        .and_then(normalize_domain)
        .unwrap_or_else(|| DEFAULT_GITHUB_DOMAIN.to_string());
    let urls = GitHubCopilotUrls::new(&domain);
    let client = reqwest::Client::new();
    let response = client
        .get(urls.copilot_token_url)
        .headers(copilot_headers(Some(github_access_token))?)
        .send()
        .await?;
    let response = error_for_status(response).await?;

    parse_copilot_token_response(response.json::<Value>().await?)
}

/// Mints Copilot credentials from a GitHub user access token without attempting
/// to renew that token. Kept for the login path and backwards compatibility;
/// callers holding full credentials should prefer
/// [`refresh_github_copilot_credentials`], which can renew an expired GitHub
/// token before minting.
pub async fn refresh_github_copilot_token(
    refresh_token: &str,
    enterprise_domain: Option<&str>,
) -> Result<OAuthCredentials> {
    let token = mint_copilot_token(refresh_token, enterprise_domain).await?;
    Ok(copilot_credentials_from_token(
        refresh_token,
        None,
        None,
        enterprise_domain.and_then(normalize_domain),
        token,
    ))
}

/// Refreshes GitHub Copilot credentials, renewing the underlying GitHub user
/// access token when it has expired.
///
/// GitHub Apps that opt into expiring user tokens issue a `ghu_` access token
/// (valid ~8h) plus a `ghr_` refresh token (valid ~6 months). Previously only the
/// access token was kept, so once it lapsed every Copilot mint failed with
/// `401 Bad credentials` until the user re-ran the device-code login. When a
/// refresh token is present we now exchange it for a fresh GitHub token —
/// proactively when the stored token is known to have expired, and reactively if
/// a mint is rejected — so a long-running proxy self-heals without re-login. With
/// no refresh token (non-expiring tokens, or credentials from before this change)
/// the behaviour is unchanged and a hard failure still surfaces for re-login.
async fn refresh_github_copilot_credentials(
    credentials: &OAuthCredentials,
) -> Result<OAuthCredentials> {
    let enterprise_domain = github_copilot_enterprise_domain(credentials).map(str::to_string);
    let domain = enterprise_domain.as_deref();
    let mut github_access = credentials.refresh.clone();
    let mut github_refresh = github_copilot_refresh_token(credentials).map(str::to_string);
    let mut github_expires = github_copilot_access_expires(credentials);

    // Proactive: the stored GitHub token has expired and we hold a refresh token,
    // so renew before spending a request on a mint that would 401 anyway.
    if let Some(refresh) = github_refresh.clone()
        && github_expires.is_some_and(|expires| crate::utils::time::now_millis() >= expires)
    {
        let renewed = refresh_github_user_token(&refresh, domain).await?;
        github_access = renewed.access_token;
        github_refresh = renewed.refresh_token.or(Some(refresh));
        github_expires = renewed.expires_in.map(github_access_expiry_ms);
    }

    // Reactive: GitHub rejected the token (expiry unknown, or it lapsed just now).
    // Renew once and retry; if the renewal itself fails, surface it for re-login.
    let token = match mint_copilot_token(&github_access, domain).await {
        Ok(token) => token,
        Err(error) => {
            let Some(refresh) = github_refresh.clone() else {
                return Err(error);
            };
            if !is_github_auth_error(&error) {
                return Err(error);
            }
            let renewed = refresh_github_user_token(&refresh, domain).await?;
            github_access = renewed.access_token;
            github_refresh = renewed.refresh_token.or(Some(refresh));
            github_expires = renewed.expires_in.map(github_access_expiry_ms);
            mint_copilot_token(&github_access, domain).await?
        }
    };

    Ok(copilot_credentials_from_token(
        &github_access,
        github_refresh.as_deref(),
        github_expires,
        enterprise_domain,
        token,
    ))
}

/// Exchanges a GitHub refresh token (`ghr_`) for a fresh user access token using
/// the device-flow client id. Public clients refresh with just `client_id` (no
/// secret), the same way the token was originally minted.
async fn refresh_github_user_token(
    refresh_token: &str,
    enterprise_domain: Option<&str>,
) -> Result<GithubUserToken> {
    let domain = enterprise_domain
        .and_then(normalize_domain)
        .unwrap_or_else(|| DEFAULT_GITHUB_DOMAIN.to_string());
    let urls = GitHubCopilotUrls::new(&domain);
    let client = reqwest::Client::new();
    refresh_github_user_token_at(&client, &urls.access_token_url, refresh_token).await
}

async fn refresh_github_user_token_at(
    client: &reqwest::Client,
    access_token_url: &str,
    refresh_token: &str,
) -> Result<GithubUserToken> {
    let response = github_oauth_form_post(
        client,
        access_token_url,
        &[
            ("client_id", GITHUB_COPILOT_CLIENT_ID),
            ("grant_type", "refresh_token"),
            ("refresh_token", refresh_token),
        ],
    )
    .await?;

    match parse_device_token_response(response.json::<Value>().await?) {
        OAuthDeviceCodePollResult::Complete(token) => Ok(token),
        OAuthDeviceCodePollResult::Failed(message) => Err(Error::Provider(message)),
        OAuthDeviceCodePollResult::Pending | OAuthDeviceCodePollResult::SlowDown => {
            Err(Error::Provider(
                "Unexpected polling response while refreshing GitHub token".to_string(),
            ))
        }
    }
}

pub async fn login_github_copilot(callbacks: OAuthLoginCallbacks) -> Result<OAuthCredentials> {
    let input = (callbacks.on_prompt)(OAuthPrompt {
        message: "GitHub Enterprise URL/domain (blank for github.com)".to_string(),
        placeholder: Some("company.ghe.com".to_string()),
        allow_empty: true,
    })
    .await?;

    if callbacks
        .cancellation_token
        .as_ref()
        .is_some_and(CancellationToken::is_cancelled)
    {
        return Err(Error::Provider(CANCEL_MESSAGE.to_string()));
    }

    let trimmed = input.trim();
    let enterprise_domain = normalize_domain(&input);
    if !trimmed.is_empty() && enterprise_domain.is_none() {
        return Err(Error::Provider(
            "Invalid GitHub Enterprise URL/domain".to_string(),
        ));
    }
    let domain = enterprise_domain
        .clone()
        .unwrap_or_else(|| DEFAULT_GITHUB_DOMAIN.to_string());

    let device = start_github_device_flow(&domain).await?;
    (callbacks.on_device_code)(OAuthDeviceCodeInfo {
        user_code: device.user_code.clone(),
        verification_uri: device.verification_uri.clone(),
        interval_seconds: device.interval,
        expires_in_seconds: Some(device.expires_in),
    });

    let github =
        poll_for_github_access_token(&domain, device, callbacks.cancellation_token.clone()).await?;
    let github_expires = github.expires_in.map(github_access_expiry_ms);
    let token = mint_copilot_token(&github.access_token, enterprise_domain.as_deref()).await?;

    Ok(copilot_credentials_from_token(
        &github.access_token,
        github.refresh_token.as_deref(),
        github_expires,
        enterprise_domain,
        token,
    ))
}

pub fn modify_github_copilot_models(
    models: impl IntoIterator<Item = Model>,
    credentials: &OAuthCredentials,
) -> Vec<Model> {
    let domain = github_copilot_enterprise_domain(credentials).and_then(normalize_domain);
    let base_url = get_github_copilot_base_url(Some(&credentials.access), domain.as_deref());
    models
        .into_iter()
        .map(|mut model| {
            if model.provider == "github-copilot" {
                model.base_url = base_url.clone();
            }
            model
        })
        .collect()
}

async fn start_github_device_flow(domain: &str) -> Result<DeviceCodeResponse> {
    let urls = GitHubCopilotUrls::new(domain);
    let client = reqwest::Client::new();
    let response = github_oauth_form_post(
        &client,
        &urls.device_code_url,
        &[
            ("client_id", GITHUB_COPILOT_CLIENT_ID),
            ("scope", "read:user"),
        ],
    )
    .await?;
    let device = response.json::<DeviceCodeResponse>().await?;
    if device.device_code.is_empty()
        || device.user_code.is_empty()
        || device.verification_uri.is_empty()
        || device.expires_in == 0
    {
        return Err(Error::Provider(
            "Invalid device code response fields".to_string(),
        ));
    }
    Ok(device)
}

async fn poll_for_github_access_token(
    domain: &str,
    device: DeviceCodeResponse,
    cancellation_token: Option<CancellationToken>,
) -> Result<GithubUserToken> {
    let urls = GitHubCopilotUrls::new(domain);
    let client = reqwest::Client::new();
    poll_oauth_device_code_flow(
        device.interval,
        Some(device.expires_in),
        cancellation_token,
        move || {
            let client = client.clone();
            let access_token_url = urls.access_token_url.clone();
            let device_code = device.device_code.clone();
            async move {
                let response = github_oauth_form_post(
                    &client,
                    &access_token_url,
                    &[
                        ("client_id", GITHUB_COPILOT_CLIENT_ID),
                        ("device_code", device_code.as_str()),
                        ("grant_type", "urn:ietf:params:oauth:grant-type:device_code"),
                    ],
                )
                .await?;
                Ok(parse_device_token_response(response.json::<Value>().await?))
            }
        },
    )
    .await
}

fn get_base_url_from_token(token: &str) -> Option<String> {
    let proxy_host = token
        .split(';')
        .find_map(|part| part.strip_prefix("proxy-ep="))?;
    let api_host = proxy_host
        .strip_prefix("proxy.")
        .map(|host| format!("api.{host}"))
        .unwrap_or_else(|| proxy_host.to_string());
    Some(format!("https://{api_host}"))
}

fn parse_device_token_response(raw: Value) -> OAuthDeviceCodePollResult<GithubUserToken> {
    let Some(object) = raw.as_object() else {
        return OAuthDeviceCodePollResult::Failed("Invalid device token response".to_string());
    };

    if let Some(access_token) = object.get("access_token").and_then(Value::as_str) {
        return OAuthDeviceCodePollResult::Complete(GithubUserToken {
            access_token: access_token.to_string(),
            refresh_token: object
                .get("refresh_token")
                .and_then(Value::as_str)
                .map(str::to_string),
            expires_in: object.get("expires_in").and_then(Value::as_u64),
        });
    }

    let Some(error) = object.get("error").and_then(Value::as_str) else {
        return OAuthDeviceCodePollResult::Failed("Invalid device token response".to_string());
    };

    match error {
        "authorization_pending" => OAuthDeviceCodePollResult::Pending,
        "slow_down" => OAuthDeviceCodePollResult::SlowDown,
        error => {
            let suffix = object
                .get("error_description")
                .and_then(Value::as_str)
                .map(|description| format!(": {description}"))
                .unwrap_or_default();
            OAuthDeviceCodePollResult::Failed(format!("Device flow failed: {error}{suffix}"))
        }
    }
}

fn parse_copilot_token_response(raw: Value) -> Result<CopilotTokenResponse> {
    let Some(object) = raw.as_object() else {
        return Err(Error::Provider(
            "Invalid Copilot token response".to_string(),
        ));
    };

    let Some(token) = object.get("token").and_then(Value::as_str) else {
        return Err(Error::Provider(
            "Invalid Copilot token response fields".to_string(),
        ));
    };
    let Some(expires_at) = object.get("expires_at").and_then(Value::as_u64) else {
        return Err(Error::Provider(
            "Invalid Copilot token response fields".to_string(),
        ));
    };

    Ok(CopilotTokenResponse {
        token: token.to_string(),
        expires_at,
    })
}

fn copilot_credentials_from_token(
    github_access_token: &str,
    github_refresh_token: Option<&str>,
    github_access_expires_at_ms: Option<u64>,
    enterprise_domain: Option<String>,
    token: CopilotTokenResponse,
) -> OAuthCredentials {
    let mut extra = HashMap::new();
    if let Some(enterprise_domain) = enterprise_domain {
        extra.insert(
            GITHUB_COPILOT_ENTERPRISE_URL_KEY.to_string(),
            Value::String(enterprise_domain),
        );
    }
    if let Some(refresh_token) = github_refresh_token {
        extra.insert(
            GITHUB_COPILOT_REFRESH_TOKEN_KEY.to_string(),
            Value::String(refresh_token.to_string()),
        );
    }
    if let Some(expires_at) = github_access_expires_at_ms {
        extra.insert(
            GITHUB_COPILOT_ACCESS_EXPIRES_KEY.to_string(),
            Value::from(expires_at),
        );
    }

    OAuthCredentials {
        refresh: github_access_token.to_string(),
        access: token.token,
        expires: token
            .expires_at
            .saturating_mul(1000)
            .saturating_sub(COPILOT_TOKEN_EXPIRY_SKEW_MS),
        extra,
    }
}

fn copilot_headers(refresh_token: Option<&str>) -> Result<reqwest::header::HeaderMap> {
    let mut headers = reqwest::header::HeaderMap::new();
    headers.insert(
        "Accept",
        reqwest::header::HeaderValue::from_static("application/json"),
    );
    headers.insert(
        "User-Agent",
        reqwest::header::HeaderValue::from_static(GITHUB_COPILOT_USER_AGENT),
    );
    headers.insert(
        "Editor-Version",
        reqwest::header::HeaderValue::from_static("vscode/1.107.0"),
    );
    headers.insert(
        "Editor-Plugin-Version",
        reqwest::header::HeaderValue::from_static("copilot-chat/0.35.0"),
    );
    headers.insert(
        "Copilot-Integration-Id",
        reqwest::header::HeaderValue::from_static("vscode-chat"),
    );
    if let Some(token) = refresh_token {
        headers.insert(
            reqwest::header::AUTHORIZATION,
            reqwest::header::HeaderValue::from_str(&format!("Bearer {token}"))
                .map_err(|e| Error::InvalidHeaderValue("authorization".to_string(), e))?,
        );
    }
    Ok(headers)
}

#[derive(Debug)]
struct GitHubCopilotUrls {
    device_code_url: String,
    access_token_url: String,
    copilot_token_url: String,
}

impl GitHubCopilotUrls {
    fn new(domain: &str) -> Self {
        Self {
            device_code_url: format!("https://{domain}/login/device/code"),
            access_token_url: format!("https://{domain}/login/oauth/access_token"),
            copilot_token_url: format!("https://api.{domain}/copilot_internal/v2/token"),
        }
    }
}

#[derive(Debug, Deserialize)]
struct DeviceCodeResponse {
    device_code: String,
    user_code: String,
    verification_uri: String,
    interval: Option<u64>,
    expires_in: u64,
}

#[derive(Debug)]
struct CopilotTokenResponse {
    token: String,
    expires_at: u64,
}

#[cfg(test)]
mod tests {
    use std::sync::{Arc, Mutex};

    use super::*;
    use crate::oauth::test_support::read_http_request;
    use crate::types::ModelCost;
    use tokio::io::AsyncWriteExt;
    use tokio::net::TcpListener;

    #[test]
    fn resolves_copilot_base_url_from_token_or_enterprise_domain() {
        assert_eq!(
            get_github_copilot_base_url(
                Some("tid=test;proxy-ep=proxy.individual.githubcopilot.com;exp=1"),
                None,
            ),
            DEFAULT_COPILOT_BASE_URL
        );
        assert_eq!(
            get_github_copilot_base_url(None, Some("https://company.ghe.com/path")),
            "https://copilot-api.company.ghe.com"
        );
        assert_eq!(
            get_github_copilot_base_url(None, None),
            DEFAULT_COPILOT_BASE_URL
        );
    }

    #[test]
    fn copilot_credentials_apply_expiry_skew() {
        let credentials = copilot_credentials_from_token(
            "refresh-token",
            None,
            None,
            Some("company.ghe.com".to_string()),
            CopilotTokenResponse {
                token: "access-token".to_string(),
                expires_at: 1_000,
            },
        );

        assert_eq!(credentials.refresh, "refresh-token");
        assert_eq!(credentials.access, "access-token");
        assert_eq!(credentials.expires, 700_000);
        assert_eq!(
            github_copilot_enterprise_domain(&credentials),
            Some("company.ghe.com")
        );
    }

    #[test]
    fn oauth_credentials_flatten_provider_specific_extra_fields() {
        let credentials = copilot_credentials_from_token(
            "refresh-token",
            None,
            None,
            Some("company.ghe.com".to_string()),
            CopilotTokenResponse {
                token: "access-token".to_string(),
                expires_at: 1_000,
            },
        );

        let json = serde_json::to_value(&credentials).expect("credentials json");
        assert_eq!(json["enterpriseUrl"], "company.ghe.com");
        assert!(json.get("extra").is_none());

        let round_tripped: OAuthCredentials =
            serde_json::from_value(json).expect("round-tripped credentials");
        assert_eq!(
            github_copilot_enterprise_domain(&round_tripped),
            Some("company.ghe.com")
        );
    }

    #[test]
    fn provider_updates_only_github_copilot_model_base_urls() {
        let credentials = OAuthCredentials {
            refresh: "refresh".to_string(),
            access: "tid=test;proxy-ep=proxy.enterprise.example.com;exp=1".to_string(),
            expires: 1,
            extra: HashMap::new(),
        };
        let models = vec![
            Model {
                id: "gpt".to_string(),
                name: "gpt".to_string(),
                api: "openai-completions".to_string(),
                provider: "github-copilot".to_string(),
                base_url: "https://old.example.com".to_string(),
                cost: ModelCost::default(),
                ..Default::default()
            },
            Model {
                id: "claude".to_string(),
                name: "claude".to_string(),
                api: "anthropic-messages".to_string(),
                provider: "anthropic".to_string(),
                base_url: "https://api.anthropic.com".to_string(),
                cost: ModelCost::default(),
                ..Default::default()
            },
        ];

        let updated = github_copilot_oauth_provider().modify_models(models, &credentials);

        assert_eq!(updated[0].base_url, "https://api.enterprise.example.com");
        assert_eq!(updated[1].base_url, "https://api.anthropic.com");
    }

    #[test]
    fn parses_device_token_response() {
        assert_eq!(
            parse_device_token_response(serde_json::json!({ "access_token": "ghu_refresh" })),
            OAuthDeviceCodePollResult::Complete(GithubUserToken {
                access_token: "ghu_refresh".to_string(),
                refresh_token: None,
                expires_in: None,
            })
        );
        assert_eq!(
            parse_device_token_response(serde_json::json!({
                "error": "authorization_pending",
                "error_description": "pending"
            })),
            OAuthDeviceCodePollResult::Pending
        );
        assert_eq!(
            parse_device_token_response(serde_json::json!({
                "error": "slow_down",
                "error_description": "slow down"
            })),
            OAuthDeviceCodePollResult::SlowDown
        );
        assert_eq!(
            parse_device_token_response(serde_json::json!({
                "error": "access_denied",
                "error_description": "denied"
            })),
            OAuthDeviceCodePollResult::Failed(
                "Device flow failed: access_denied: denied".to_string()
            )
        );
        assert_eq!(
            parse_device_token_response(serde_json::json!({ "access_token": 1 })),
            OAuthDeviceCodePollResult::Failed("Invalid device token response".to_string())
        );
        assert_eq!(
            parse_device_token_response(serde_json::Value::Null),
            OAuthDeviceCodePollResult::Failed("Invalid device token response".to_string())
        );
    }

    #[test]
    fn parses_copilot_token_response() {
        let token = parse_copilot_token_response(serde_json::json!({
            "token": "tid=test;exp=9999999999",
            "expires_at": 9999999999_u64
        }))
        .unwrap();

        assert_eq!(token.token, "tid=test;exp=9999999999");
        assert_eq!(token.expires_at, 9999999999);
        assert_eq!(
            parse_copilot_token_response(serde_json::Value::Null)
                .unwrap_err()
                .to_string(),
            "provider error: Invalid Copilot token response"
        );
        assert_eq!(
            parse_copilot_token_response(serde_json::json!({ "token": "tid=test" }))
                .unwrap_err()
                .to_string(),
            "provider error: Invalid Copilot token response fields"
        );
        assert_eq!(
            parse_copilot_token_response(serde_json::json!({
                "token": "tid=test",
                "expires_at": "later"
            }))
            .unwrap_err()
            .to_string(),
            "provider error: Invalid Copilot token response fields"
        );
    }

    #[test]
    fn provider_metadata_matches_expected_shape() {
        let provider = github_copilot_oauth_provider();
        assert_eq!(provider.id(), "github-copilot");
        assert_eq!(provider.name(), "GitHub Copilot");
    }

    #[test]
    fn copilot_headers_include_static_client_metadata_and_bearer() {
        let headers = copilot_headers(Some("refresh-token")).unwrap();

        assert_eq!(
            headers
                .get("user-agent")
                .and_then(|value| value.to_str().ok()),
            Some("GitHubCopilotChat/0.35.0")
        );
        assert_eq!(
            headers
                .get("editor-version")
                .and_then(|value| value.to_str().ok()),
            Some("vscode/1.107.0")
        );
        assert_eq!(
            headers
                .get("authorization")
                .and_then(|value| value.to_str().ok()),
            Some("Bearer refresh-token")
        );
    }

    #[test]
    fn form_body_matches_url_search_params_encoding() {
        assert_eq!(
            form_body(&[
                ("client_id", GITHUB_COPILOT_CLIENT_ID),
                ("scope", "read:user"),
                ("device_code", "abc def/ghi")
            ]),
            "client_id=Iv1.b507a08c87ecfe98&scope=read%3Auser&device_code=abc+def%2Fghi"
        );
    }

    #[test]
    fn device_token_response_captures_refresh_token_and_expiry() {
        match parse_device_token_response(serde_json::json!({
            "access_token": "ghu_new",
            "refresh_token": "ghr_new",
            "expires_in": 28_800,
            "token_type": "bearer"
        })) {
            OAuthDeviceCodePollResult::Complete(token) => {
                assert_eq!(token.access_token, "ghu_new");
                assert_eq!(token.refresh_token.as_deref(), Some("ghr_new"));
                assert_eq!(token.expires_in, Some(28_800));
            }
            other => panic!("expected Complete, got {other:?}"),
        }
    }

    #[test]
    fn device_token_response_without_refresh_token_is_non_expiring() {
        match parse_device_token_response(
            serde_json::json!({ "access_token": "gho_classic", "token_type": "bearer" }),
        ) {
            OAuthDeviceCodePollResult::Complete(token) => {
                assert_eq!(token.access_token, "gho_classic");
                assert!(token.refresh_token.is_none());
                assert!(token.expires_in.is_none());
            }
            other => panic!("expected Complete, got {other:?}"),
        }
    }

    #[test]
    fn github_auth_errors_signal_token_renewal() {
        assert!(is_github_auth_error(&Error::ApiStatus {
            status: reqwest::StatusCode::UNAUTHORIZED,
            body: String::new(),
        }));
        assert!(is_github_auth_error(&Error::ApiStatus {
            status: reqwest::StatusCode::FORBIDDEN,
            body: String::new(),
        }));
        assert!(!is_github_auth_error(&Error::ApiStatus {
            status: reqwest::StatusCode::INTERNAL_SERVER_ERROR,
            body: String::new(),
        }));
        assert!(!is_github_auth_error(&Error::Provider("nope".to_string())));
    }

    #[test]
    fn copilot_credentials_persist_refresh_token_and_expiry() {
        let credentials = copilot_credentials_from_token(
            "ghu_access",
            Some("ghr_refresh"),
            Some(1_700_000_000_000),
            None,
            CopilotTokenResponse {
                token: "copilot-token".to_string(),
                expires_at: 2_000,
            },
        );

        assert_eq!(credentials.refresh, "ghu_access");
        assert_eq!(
            github_copilot_refresh_token(&credentials),
            Some("ghr_refresh")
        );
        assert_eq!(
            github_copilot_access_expires(&credentials),
            Some(1_700_000_000_000)
        );

        let json = serde_json::to_value(&credentials).expect("credentials json");
        assert_eq!(json["githubRefreshToken"], "ghr_refresh");
        assert_eq!(
            json["githubAccessExpires"].as_u64(),
            Some(1_700_000_000_000)
        );
        let round_tripped: OAuthCredentials =
            serde_json::from_value(json).expect("round-tripped credentials");
        assert_eq!(
            github_copilot_refresh_token(&round_tripped),
            Some("ghr_refresh")
        );
    }

    #[test]
    fn copilot_credentials_omit_refresh_metadata_when_absent() {
        let credentials = copilot_credentials_from_token(
            "ghu_access",
            None,
            None,
            None,
            CopilotTokenResponse {
                token: "copilot-token".to_string(),
                expires_at: 2_000,
            },
        );

        assert!(github_copilot_refresh_token(&credentials).is_none());
        assert!(github_copilot_access_expires(&credentials).is_none());
        let json = serde_json::to_value(&credentials).expect("credentials json");
        assert!(json.get("githubRefreshToken").is_none());
        assert!(json.get("githubAccessExpires").is_none());
    }

    #[tokio::test]
    async fn github_user_token_refresh_uses_refresh_token_grant() {
        let captured_body = Arc::new(Mutex::new(None));
        let token_url = spawn_github_refresh_server(Arc::clone(&captured_body)).await;
        let client = reqwest::Client::new();

        let token = refresh_github_user_token_at(&client, &token_url, "ghr_old")
            .await
            .unwrap();

        assert_eq!(token.access_token, "ghu_refreshed");
        assert_eq!(token.refresh_token.as_deref(), Some("ghr_rotated"));
        assert_eq!(token.expires_in, Some(28_800));

        let body = captured_body
            .lock()
            .expect("captured body lock poisoned")
            .clone()
            .expect("captured request body");
        assert!(body.contains("grant_type=refresh_token"), "body: {body}");
        assert!(body.contains("refresh_token=ghr_old"), "body: {body}");
        assert!(
            body.contains(&format!("client_id={GITHUB_COPILOT_CLIENT_ID}")),
            "body: {body}"
        );
    }

    async fn spawn_github_refresh_server(captured_body: Arc<Mutex<Option<String>>>) -> String {
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        tokio::spawn(async move {
            let (mut socket, _) = listener.accept().await.unwrap();
            let request = read_http_request(&mut socket).await;
            let body = request
                .split_once("\r\n\r\n")
                .map(|(_, body)| body.to_string())
                .unwrap_or_default();
            *captured_body.lock().expect("captured body lock poisoned") = Some(body);
            let response_body = serde_json::json!({
                "access_token": "ghu_refreshed",
                "refresh_token": "ghr_rotated",
                "expires_in": 28_800,
                "token_type": "bearer",
                "scope": ""
            })
            .to_string();
            let response = format!(
                "HTTP/1.1 200 OK\r\ncontent-type: application/json\r\ncontent-length: {}\r\n\r\n{}",
                response_body.len(),
                response_body
            );
            socket.write_all(response.as_bytes()).await.unwrap();
        });
        format!("http://{addr}/login/oauth/access_token")
    }
}