ultron-ai 1.0.2

Provider layer of packages/ai: message model, streaming events, every API implementation (Anthropic, OpenAI completions/responses/codex, Azure, Google, Vertex, Mistral, Bedrock, pi-messages, Claude Code CLI, Claude subscription), provider auth and every OAuth flow, images API, model catalog, user models.json, env API keys
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
//! Interactive OAuth logins of `auth/oauth/*.ts`: Anthropic and OpenAI Codex browser logins (PKCE, a loopback callback
//! server raced against a pasted code), the OpenAI Codex and GitHub Copilot device-code logins, and Copilot model
//! enablement. Network goes through [`Transport`]; clock, sleeping and randomness go through [`LoginEnv`] so the
//! flows replay exactly against recorded TypeScript runs (`fixtures/generate-oauth-login.mjs`, `tests/oauth_login.rs`).
//! Prompts and events are the [`crate::auth`] interaction types every OAuth flow uses.

use crate::auth::{AuthEvent, AuthInteraction, AuthPrompt, SelectOption};
use crate::http::{AbortSignal, Transport};
use crate::oauth::{
    ANTHROPIC_CLIENT_ID, CODEX_CLIENT_ID, CODEX_TOKEN_URL, COPILOT_API_VERSION, COPILOT_HEADERS,
    COPILOT_INDIVIDUAL_URL, CopilotCatalog, OAuthCredential, OAuthProvider, Reply, SendError, anthropic_token_request,
    codex_credential, copilot_access_token, copilot_base_url, copilot_headers, fetch_copilot_json, normalize_domain,
    send,
};
use crate::oauth_flow::{
    CANCEL_MESSAGE as LOGIN_CANCELLED, LoginEnv, Poll, abortable_sleep, form_urlencode, generate_pkce_from,
    oauth_callback_host, oauth_error_html, oauth_success_html, poll_device_code_flow,
};
use serde_json::{Value, json};
use std::sync::{Arc, Mutex};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener;
use tokio::sync::oneshot;

const ANTHROPIC_AUTHORIZE_URL: &str = "https://claude.ai/oauth/authorize";
const ANTHROPIC_CALLBACK_PORT: u16 = 53692;
const ANTHROPIC_REDIRECT_URI: &str = "http://localhost:53692/callback";
const ANTHROPIC_SCOPES: &str =
    "org:create_api_key user:profile user:inference user:sessions:claude_code user:mcp_servers user:file_upload";
const CODEX_AUTHORIZE_URL: &str = "https://auth.openai.com/oauth/authorize";
const CODEX_CALLBACK_PORT: u16 = 1455;
const CODEX_REDIRECT_URI: &str = "http://localhost:1455/auth/callback";
const CODEX_SCOPE: &str = "openid profile email offline_access";
const CODEX_DEVICE_USER_CODE_URL: &str = "https://auth.openai.com/api/accounts/deviceauth/usercode";
const CODEX_DEVICE_TOKEN_URL: &str = "https://auth.openai.com/api/accounts/deviceauth/token";
const CODEX_DEVICE_VERIFICATION_URI: &str = "https://auth.openai.com/codex/device";
const CODEX_DEVICE_REDIRECT_URI: &str = "https://auth.openai.com/deviceauth/callback";
const CODEX_DEVICE_TIMEOUT_SECONDS: f64 = 15.0 * 60.0;
const COPILOT_CLIENT_ID: &str = "Iv1.b507a08c87ecfe98";
const MANUAL_INSTRUCTIONS: &str =
    "Complete login in your browser. If the browser is on another machine, paste the final redirect URL here.";
const MANUAL_MESSAGE: &str = "Complete login in your browser, or paste the authorization code / redirect URL here:";

/// Everything a login needs.
pub struct LoginContext<'a> {
    pub transport: &'a dyn Transport,
    pub interaction: &'a dyn AuthInteraction,
    pub env: &'a dyn LoginEnv,
    /// Aborts the whole login.
    pub signal: AbortSignal,
    /// Interface the callback server listens on ([`oauth_callback_host`] by default).
    pub callback_host: String,
    /// Listen on this port instead of the registered one (tests); the redirect URI keeps the registered port.
    pub callback_port: Option<u16>,
}

impl<'a> LoginContext<'a> {
    pub fn new(transport: &'a dyn Transport, interaction: &'a dyn AuthInteraction, env: &'a dyn LoginEnv) -> Self {
        LoginContext {
            transport,
            interaction,
            env,
            signal: AbortSignal::new(),
            callback_host: oauth_callback_host(),
            callback_port: None,
        }
    }
}

impl OAuthProvider {
    /// Display name (`OAuthAuth.name`).
    pub fn name(self) -> &'static str {
        match self {
            Self::Anthropic => "Anthropic (Claude Pro/Max)",
            Self::OpenAiCodex => "OpenAI (ChatGPT Plus/Pro)",
            Self::GitHubCopilot => "GitHub Copilot",
        }
    }

    /// `OAuthAuth.login`: runs the interactive login and returns the credential to store.
    pub async fn login(self, ctx: &LoginContext<'_>) -> Result<OAuthCredential, String> {
        match self {
            Self::Anthropic => login_anthropic(ctx).await,
            Self::OpenAiCodex => login_codex(ctx).await,
            Self::GitHubCopilot => login_copilot(ctx).await,
        }
    }
}

/// What a pasted redirect URL, query string, `code#state` or bare code carries (`parseAuthorizationInput`).
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct AuthorizationInput {
    pub code: Option<String>,
    pub state: Option<String>,
}

pub fn parse_authorization_input(input: &str) -> AuthorizationInput {
    let value = input.trim();
    if value.is_empty() {
        return AuthorizationInput::default();
    }
    let get = |url: &reqwest::Url, key: &str| url.query_pairs().find(|(k, _)| k == key).map(|(_, v)| v.into_owned());
    if let Ok(url) = reqwest::Url::parse(value) {
        return AuthorizationInput {
            code: get(&url, "code"),
            state: get(&url, "state"),
        };
    }
    if value.contains('#') {
        // `value.split("#", 2)`: JS drops everything after the second piece.
        let mut parts = value.split('#');
        return AuthorizationInput {
            code: parts.next().map(str::to_owned),
            state: parts.next().map(str::to_owned),
        };
    }
    if value.contains("code=") {
        let query = value.strip_prefix('?').unwrap_or(value);
        let url = reqwest::Url::parse(&format!("http://x/?{query}")).expect("query URL");
        return AuthorizationInput {
            code: get(&url, "code"),
            state: get(&url, "state"),
        };
    }
    AuthorizationInput {
        code: Some(value.to_owned()),
        state: None,
    }
}

/// Which provider's callback route rules apply.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CallbackKind {
    Anthropic,
    OpenAiCodex,
}

/// A callback server's answer to one browser request; `code` is set when it completes the login.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CallbackReply {
    pub status: u16,
    pub content_type: &'static str,
    pub body: String,
    pub code: Option<(String, String)>,
}

const HTML: &str = "text/html; charset=utf-8";

/// The request handler of the Anthropic and Codex callback servers, for a request target such as `/callback?code=…`.
pub fn callback_reply(kind: CallbackKind, target: &str, expected_state: &str) -> CallbackReply {
    let reply = |status, body: String| CallbackReply {
        status,
        content_type: HTML,
        body,
        code: None,
    };
    let Ok(url) = reqwest::Url::parse("http://localhost").and_then(|base| base.join(target)) else {
        return match kind {
            CallbackKind::Anthropic => CallbackReply {
                status: 500,
                content_type: "text/plain; charset=utf-8",
                body: "Internal error".into(),
                code: None,
            },
            CallbackKind::OpenAiCodex => reply(
                500,
                oauth_error_html("Internal error while processing OAuth callback.", None),
            ),
        };
    };
    let param = |key: &str| url.query_pairs().find(|(k, _)| k == key).map(|(_, v)| v.into_owned());
    let (code, state) = (param("code"), param("state"));
    match kind {
        CallbackKind::Anthropic => {
            if url.path() != "/callback" {
                return reply(404, oauth_error_html("Callback route not found.", None));
            }
            if let Some(error) = param("error").filter(|e| !e.is_empty()) {
                return reply(
                    400,
                    oauth_error_html(
                        "Anthropic authentication did not complete.",
                        Some(&format!("Error: {error}")),
                    ),
                );
            }
            let (Some(code), Some(state)) = (code.filter(|c| !c.is_empty()), state.filter(|s| !s.is_empty())) else {
                return reply(400, oauth_error_html("Missing code or state parameter.", None));
            };
            if state != expected_state {
                return reply(400, oauth_error_html("State mismatch.", None));
            }
            CallbackReply {
                code: Some((code, state)),
                ..reply(
                    200,
                    oauth_success_html("Anthropic authentication completed. You can close this window."),
                )
            }
        }
        CallbackKind::OpenAiCodex => {
            if url.path() != "/auth/callback" {
                return reply(404, oauth_error_html("Callback route not found.", None));
            }
            if state.as_deref() != Some(expected_state) {
                return reply(400, oauth_error_html("State mismatch.", None));
            }
            let Some(code) = code.filter(|c| !c.is_empty()) else {
                return reply(400, oauth_error_html("Missing authorization code.", None));
            };
            CallbackReply {
                code: Some((code, expected_state.to_owned())),
                ..reply(
                    200,
                    oauth_success_html("OpenAI authentication completed. You can close this window."),
                )
            }
        }
    }
}

/// A loopback HTTP server that answers browser redirects with [`callback_reply`] until one delivers a code. It stops
/// accepting when dropped.
pub struct CallbackServer {
    code: oneshot::Receiver<(String, String)>,
    task: tokio::task::JoinHandle<()>,
}

impl Drop for CallbackServer {
    fn drop(&mut self) {
        self.task.abort();
    }
}

impl CallbackServer {
    pub async fn start(host: &str, port: u16, kind: CallbackKind, expected_state: &str) -> std::io::Result<Self> {
        let listener = TcpListener::bind((host, port)).await?;
        let (sender, code) = oneshot::channel();
        let sender = Arc::new(Mutex::new(Some(sender)));
        let expected_state = expected_state.to_owned();
        let task = tokio::spawn(async move {
            while let Ok((mut stream, _)) = listener.accept().await {
                let sender = Arc::clone(&sender);
                let expected_state = expected_state.clone();
                tokio::spawn(async move {
                    let Some(target) = read_request_target(&mut stream).await else {
                        return;
                    };
                    let reply = callback_reply(kind, &target, &expected_state);
                    let head = format!(
                        "HTTP/1.1 {} {}\r\nContent-Type: {}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
                        reply.status,
                        crate::oauth::status_text(reply.status),
                        reply.content_type,
                        reply.body.len()
                    );
                    let _ = stream.write_all(head.as_bytes()).await;
                    let _ = stream.write_all(reply.body.as_bytes()).await;
                    let _ = stream.shutdown().await;
                    if let Some(code) = reply.code
                        && let Some(sender) = sender.lock().expect("callback sender").take()
                    {
                        let _ = sender.send(code);
                    }
                });
            }
        });
        Ok(CallbackServer { code, task })
    }

    /// Resolves with the first `(code, state)` a browser delivered.
    pub async fn wait_for_code(&mut self) -> Option<(String, String)> {
        (&mut self.code).await.ok()
    }
}

async fn read_request_target(stream: &mut tokio::net::TcpStream) -> Option<String> {
    let mut buf = Vec::new();
    let mut chunk = [0u8; 2048];
    while !buf.windows(4).any(|w| w == b"\r\n\r\n") && buf.len() < 16 * 1024 {
        let n = stream.read(&mut chunk).await.ok()?;
        if n == 0 {
            break;
        }
        buf.extend_from_slice(&chunk[..n]);
    }
    let head = String::from_utf8_lossy(&buf);
    let line = head.lines().next()?;
    line.split(' ').nth(1).map(str::to_owned)
}

/// Waits for whichever comes first: the callback server's code, the manual prompt, or the login being aborted. Then
/// mirrors upstream: a pasted answer is used when the browser delivered nothing; with no code yet, the manual prompt
/// is awaited after all. Returns `(code, state)` as given (`state` from the pasted input, if any).
async fn race_callback_and_prompt(
    ctx: &LoginContext<'_>,
    server: Option<&mut CallbackServer>,
    placeholder: &str,
    check_state: impl Fn(&AuthorizationInput) -> Result<(), String>,
) -> Result<(Option<String>, Option<String>), String> {
    let manual_signal = AbortSignal::new();
    let mut prompt = ctx.interaction.prompt(
        AuthPrompt::ManualCode {
            message: MANUAL_MESSAGE.into(),
            placeholder: Some(placeholder.into()),
        },
        Some(manual_signal.clone()),
    );
    let result = async {
        let mut manual: Option<Result<String, String>> = None;
        let from_server = match server {
            Some(server) => tokio::select! {
                code = server.wait_for_code() => code,
                answer = &mut prompt => { manual = Some(answer); None }
                () = ctx.signal.cancelled() => None,
            },
            None => {
                manual = Some((&mut prompt).await);
                None
            }
        };
        if let Some(Err(error)) = &manual {
            return Err(error.clone());
        }
        if let Some((code, state)) = from_server {
            return Ok((Some(code), Some(state)));
        }
        let parse = |input: &str| -> Result<(Option<String>, Option<String>), String> {
            let parsed = parse_authorization_input(input);
            check_state(&parsed)?;
            Ok((parsed.code, parsed.state))
        };
        let mut found = (None, None);
        if let Some(Ok(input)) = &manual
            && !input.is_empty()
        {
            found = parse(input)?;
        }
        if found.0.as_deref().is_none_or(str::is_empty) {
            let answer = match manual {
                Some(answer) => answer,
                None => (&mut prompt).await,
            };
            let input = answer?;
            if !input.is_empty() {
                found = parse(&input)?;
            }
        }
        Ok(found)
    }
    .await;
    manual_signal.abort();
    result
}

async fn login_anthropic(ctx: &LoginContext<'_>) -> Result<OAuthCredential, String> {
    let (verifier, challenge) = generate_pkce_from(ctx.env);
    let port = ctx.callback_port.unwrap_or(ANTHROPIC_CALLBACK_PORT);
    let mut server = CallbackServer::start(&ctx.callback_host, port, CallbackKind::Anthropic, &verifier)
        .await
        .map_err(|e| format!("listen {}:{port}: {e}", ctx.callback_host))?;
    let params = form_urlencode(&[
        ("code", "true"),
        ("client_id", ANTHROPIC_CLIENT_ID),
        ("response_type", "code"),
        ("redirect_uri", ANTHROPIC_REDIRECT_URI),
        ("scope", ANTHROPIC_SCOPES),
        ("code_challenge", &challenge),
        ("code_challenge_method", "S256"),
        ("state", &verifier),
    ]);
    ctx.interaction.notify(AuthEvent::AuthUrl {
        url: format!("{ANTHROPIC_AUTHORIZE_URL}?{params}"),
        instructions: Some(MANUAL_INSTRUCTIONS.into()),
    });
    let (code, state) = race_callback_and_prompt(ctx, Some(&mut server), ANTHROPIC_REDIRECT_URI, |parsed| match parsed
        .state
        .as_deref()
    {
        Some(state) if !state.is_empty() && state != verifier => Err("OAuth state mismatch".into()),
        _ => Ok(()),
    })
    .await?;
    drop(server);
    let code = code.filter(|c| !c.is_empty()).ok_or("Missing authorization code")?;
    let state = state.unwrap_or_else(|| verifier.clone());
    if state.is_empty() {
        return Err("Missing OAuth state".into());
    }
    ctx.interaction.notify(AuthEvent::Progress {
        message: "Exchanging authorization code for tokens...".into(),
    });
    let body = json!({
        "grant_type": "authorization_code",
        "client_id": ANTHROPIC_CLIENT_ID,
        "code": code,
        "state": state,
        "redirect_uri": ANTHROPIC_REDIRECT_URI,
        "code_verifier": verifier,
    });
    anthropic_token_request(
        ctx.transport,
        &body,
        ctx.env.now_ms(),
        Some(&ctx.signal),
        |details| {
            format!(
                "Token exchange request failed. url={}; redirect_uri={ANTHROPIC_REDIRECT_URI}; response_type=authorization_code; details={details}",
                crate::oauth::ANTHROPIC_TOKEN_URL
            )
        },
        "Token exchange",
    )
    .await
}

async fn login_codex(ctx: &LoginContext<'_>) -> Result<OAuthCredential, String> {
    let option = |id: &str, label: &str| SelectOption {
        id: id.into(),
        label: label.into(),
        description: None,
    };
    let method = ctx
        .interaction
        .prompt(
            AuthPrompt::Select {
                message: "Select OpenAI Codex login method:".into(),
                options: vec![
                    option("browser", "Browser login (default)"),
                    option("device_code", "Device code login (headless)"),
                ],
            },
            None,
        )
        .await?;
    match method.as_str() {
        "device_code" => login_codex_device(ctx).await,
        "browser" => login_codex_browser(ctx).await,
        _ => Err(format!("Unknown OpenAI Codex login method: {method}")),
    }
}

/// `fetchWithLoginCancellation`: a transport failure after the login was aborted reads "Login cancelled".
async fn codex_send(
    ctx: &LoginContext<'_>,
    url: &str,
    headers: &[(&str, &str)],
    body: String,
) -> Result<Reply, String> {
    send(ctx.transport, "POST", url, headers, body, Some(&ctx.signal))
        .await
        .map_err(|e| match e {
            _ if ctx.signal.aborted() => LOGIN_CANCELLED.to_owned(),
            SendError::Aborted | SendError::Network(_) => e.message(),
        })
}

async fn codex_exchange(
    ctx: &LoginContext<'_>,
    code: &str,
    verifier: &str,
    redirect_uri: &str,
) -> Result<OAuthCredential, String> {
    let body = form_urlencode(&[
        ("grant_type", "authorization_code"),
        ("client_id", CODEX_CLIENT_ID),
        ("code", code),
        ("code_verifier", verifier),
        ("redirect_uri", redirect_uri),
    ]);
    let headers = [("Content-Type", "application/x-www-form-urlencoded")];
    let reply = codex_send(ctx, CODEX_TOKEN_URL, &headers, body).await?;
    codex_credential(&reply, "exchange", ctx.env.now_ms())
}

async fn login_codex_browser(ctx: &LoginContext<'_>) -> Result<OAuthCredential, String> {
    let (verifier, challenge) = generate_pkce_from(ctx.env);
    let mut state_bytes = [0u8; 16];
    ctx.env.fill_random(&mut state_bytes);
    let state: String = state_bytes.iter().map(|b| format!("{b:02x}")).collect();
    let params = form_urlencode(&[
        ("response_type", "code"),
        ("client_id", CODEX_CLIENT_ID),
        ("redirect_uri", CODEX_REDIRECT_URI),
        ("scope", CODEX_SCOPE),
        ("code_challenge", &challenge),
        ("code_challenge_method", "S256"),
        ("state", &state),
        ("id_token_add_organizations", "true"),
        ("codex_cli_simplified_flow", "true"),
        ("originator", "pi"),
    ]);
    let port = ctx.callback_port.unwrap_or(CODEX_CALLBACK_PORT);
    // Upstream falls back to the pasted code alone when the port is taken.
    let mut server = CallbackServer::start(&ctx.callback_host, port, CallbackKind::OpenAiCodex, &state)
        .await
        .ok();
    ctx.interaction.notify(AuthEvent::AuthUrl {
        url: format!("{CODEX_AUTHORIZE_URL}?{params}"),
        instructions: Some(MANUAL_INSTRUCTIONS.into()),
    });
    let (code, _) = race_callback_and_prompt(ctx, server.as_mut(), CODEX_REDIRECT_URI, |parsed| {
        match parsed.state.as_deref() {
            Some(s) if !s.is_empty() && s != state => Err("State mismatch".into()),
            _ => Ok(()),
        }
    })
    .await?;
    drop(server);
    let code = code.filter(|c| !c.is_empty()).ok_or("Missing authorization code")?;
    codex_exchange(ctx, &code, &verifier, CODEX_REDIRECT_URI).await
}

async fn login_codex_device(ctx: &LoginContext<'_>) -> Result<OAuthCredential, String> {
    let headers = [("Content-Type", "application/json")];
    let body = jsrt::json::stringify(&json!({"client_id": CODEX_CLIENT_ID}));
    let reply = codex_send(ctx, CODEX_DEVICE_USER_CODE_URL, &headers, body).await?;
    if !reply.ok() {
        if reply.status == 404 {
            return Err("OpenAI Codex device code login is not enabled for this server. Use browser login or verify the server URL.".into());
        }
        let suffix = if reply.text.is_empty() {
            String::new()
        } else {
            format!(": {}", reply.text)
        };
        return Err(format!(
            "OpenAI Codex device code request failed with status {}{suffix}",
            reply.status
        ));
    }
    let data = jsrt::json_parse::parse_json(&reply.text).map_err(|e| format!("SyntaxError: {e}"))?;
    let interval = match data.get("interval") {
        Some(Value::String(s)) => Some(jsrt::number::parse(s)),
        Some(Value::Number(n)) => n.as_f64(),
        _ => None,
    };
    let invalid = || {
        format!(
            "Invalid OpenAI Codex device code response: {}",
            jsrt::json::stringify(&data)
        )
    };
    // ponytail: upstream also accepts non-string truthy ids/codes and passes them through; here they are invalid.
    let (Some(device_auth_id), Some(user_code), Some(interval)) = (
        data.get("device_auth_id")
            .and_then(Value::as_str)
            .filter(|_| jsrt::value::truthy(data.get("device_auth_id"))),
        data.get("user_code")
            .and_then(Value::as_str)
            .filter(|_| jsrt::value::truthy(data.get("user_code"))),
        interval.filter(|i| i.is_finite() && *i >= 0.0),
    ) else {
        return Err(invalid());
    };
    ctx.interaction.notify(AuthEvent::DeviceCode {
        user_code: user_code.into(),
        verification_uri: CODEX_DEVICE_VERIFICATION_URI.into(),
        interval_seconds: Some(interval),
        expires_in_seconds: Some(CODEX_DEVICE_TIMEOUT_SECONDS),
    });
    let poll_body = jsrt::json::stringify(&json!({"device_auth_id": device_auth_id, "user_code": user_code}));
    let (code, verifier) = poll_device_code_flow(
        ctx.env,
        Some(&ctx.signal),
        Some(interval),
        Some(CODEX_DEVICE_TIMEOUT_SECONDS),
        false,
        || async {
            let reply = codex_send(ctx, CODEX_DEVICE_TOKEN_URL, &headers, poll_body.clone()).await?;
            if reply.ok() {
                let data = jsrt::json_parse::parse_json(&reply.text).map_err(|e| format!("SyntaxError: {e}"))?;
                let field = |key| data.get(key).and_then(Value::as_str).filter(|s| !s.is_empty());
                return Ok(match (field("authorization_code"), field("code_verifier")) {
                    (Some(code), Some(verifier)) => Poll::Complete((code.to_owned(), verifier.to_owned())),
                    _ => Poll::Failed(format!(
                        "Invalid OpenAI Codex device auth token response: {}",
                        jsrt::json::stringify(&data)
                    )),
                });
            }
            if matches!(reply.status, 403 | 404) {
                return Ok(Poll::Pending);
            }
            let error_code = jsrt::json_parse::parse_json(&reply.text)
                .ok()
                .and_then(|json| match json.get("error") {
                    Some(Value::Object(error)) => error.get("code").and_then(Value::as_str).map(str::to_owned),
                    Some(Value::String(error)) => Some(error.clone()),
                    _ => None,
                });
            Ok(match error_code.as_deref() {
                Some("deviceauth_authorization_pending") => Poll::Pending,
                Some("slow_down") => Poll::SlowDown(None),
                _ => {
                    let suffix = if reply.text.is_empty() {
                        String::new()
                    } else {
                        format!(": {}", reply.text)
                    };
                    Poll::Failed(format!(
                        "OpenAI Codex device auth failed with status {}{suffix}",
                        reply.status
                    ))
                }
            })
        },
    )
    .await?;
    codex_exchange(ctx, &code, &verifier, CODEX_DEVICE_REDIRECT_URI).await
}

async fn login_copilot(ctx: &LoginContext<'_>) -> Result<OAuthCredential, String> {
    let input = ctx
        .interaction
        .prompt(
            AuthPrompt::Text {
                message: "GitHub Enterprise URL/domain (blank for github.com)".into(),
                placeholder: Some("company.ghe.com".into()),
            },
            None,
        )
        .await?;
    if ctx.signal.aborted() {
        return Err(LOGIN_CANCELLED.into());
    }
    let enterprise = normalize_domain(&input);
    if !input.trim().is_empty() && enterprise.is_none() {
        return Err("Invalid GitHub Enterprise URL/domain".into());
    }
    let domain = enterprise.clone().unwrap_or_else(|| "github.com".into());
    let signal = Some(&ctx.signal);
    let form_headers = [
        ("Accept", "application/json"),
        ("Content-Type", "application/x-www-form-urlencoded"),
        ("User-Agent", "GitHubCopilotChat/0.35.0"),
    ];

    // startDeviceFlow
    let raw = fetch_copilot_json(
        ctx.transport,
        "POST",
        &format!("https://{domain}/login/device/code"),
        &form_headers,
        form_urlencode(&[("client_id", COPILOT_CLIENT_ID), ("scope", "read:user")]),
        signal,
    )
    .await?;
    if !raw.is_object() && !raw.is_array() {
        return Err("Invalid device code response".into());
    }
    let interval = match raw.get("interval") {
        None => Ok(None),
        Some(Value::Number(n)) => Ok(n.as_f64()),
        Some(_) => Err(()),
    };
    let (Some(device_code), Some(user_code), Some(verification_uri), Ok(interval), Some(expires_in)) = (
        raw.get("device_code").and_then(Value::as_str),
        raw.get("user_code").and_then(Value::as_str),
        raw.get("verification_uri").and_then(Value::as_str),
        interval,
        raw.get("expires_in").and_then(Value::as_f64),
    ) else {
        return Err("Invalid device code response fields".into());
    };
    // The URI is opened in a browser, so only http(s) URLs are trusted.
    let verification_uri = reqwest::Url::parse(verification_uri)
        .ok()
        .filter(|url| matches!(url.scheme(), "https" | "http"))
        .ok_or("Untrusted verification_uri in device code response")?
        .to_string();
    ctx.interaction.notify(AuthEvent::DeviceCode {
        user_code: user_code.into(),
        verification_uri,
        interval_seconds: interval,
        expires_in_seconds: Some(expires_in),
    });

    // pollForGitHubAccessToken
    let poll_body = form_urlencode(&[
        ("client_id", COPILOT_CLIENT_ID),
        ("device_code", device_code),
        ("grant_type", "urn:ietf:params:oauth:grant-type:device_code"),
    ]);
    let token_url = format!("https://{domain}/login/oauth/access_token");
    let github_token = poll_device_code_flow(ctx.env, signal, interval, Some(expires_in), true, || async {
        let raw = fetch_copilot_json(
            ctx.transport,
            "POST",
            &token_url,
            &form_headers,
            poll_body.clone(),
            signal,
        )
        .await?;
        if let Some(token) = raw.get("access_token").and_then(Value::as_str) {
            return Ok(Poll::Complete(token.to_owned()));
        }
        let Some(error) = raw.get("error").and_then(Value::as_str) else {
            return Ok(Poll::Failed("Invalid device token response".into()));
        };
        Ok(match error {
            "authorization_pending" => Poll::Pending,
            "slow_down" => Poll::SlowDown(raw.get("interval").and_then(Value::as_f64)),
            _ => {
                let description = raw
                    .get("error_description")
                    .and_then(Value::as_str)
                    .filter(|d| !d.is_empty())
                    .map_or(String::new(), |d| format!(": {d}"));
                Poll::Failed(format!("Device flow failed: {error}{description}"))
            }
        })
    })
    .await?;

    let mut credential = copilot_access_token(ctx.transport, &github_token, enterprise.as_deref(), signal).await?;
    let base_url = copilot_base_url(Some(&credential.access), enterprise.as_deref());
    let catalog = fetch_copilot_models(ctx, &credential.access, &base_url).await?;
    let mut available = catalog.available;
    if !catalog.policy.is_empty() {
        ctx.interaction.notify(AuthEvent::Progress {
            message: "Enabling models...".into(),
        });
        for id in enable_copilot_models(ctx, &credential.access, &base_url, &catalog.policy).await? {
            if !available.contains(&id) {
                available.push(id);
            }
        }
    }
    credential.extra.insert("availableModelIds".into(), json!(available));
    Ok(credential)
}

const LOGIN_RETRY_MAX: u32 = 2;
const LOGIN_RETRY_BUDGET_MS: f64 = 5000.0;

/// `fetchWithRateLimitRetry` with the login policy (two retries within five seconds): a 429 waits `Retry-After`
/// (seconds or an HTTP date), else 500 ms doubling, and is returned as is once the budget would be exceeded.
async fn send_with_rate_limit_retry(
    ctx: &LoginContext<'_>,
    method: &str,
    url: &str,
    headers: &[(&str, &str)],
    body: &str,
) -> Result<Reply, SendError> {
    let env = ctx.env;
    let deadline = env.now_ms() + LOGIN_RETRY_BUDGET_MS;
    let mut retry = 0;
    loop {
        let reply = send(ctx.transport, method, url, headers, body.to_owned(), Some(&ctx.signal)).await?;
        if reply.status != 429 || retry == LOGIN_RETRY_MAX {
            return Ok(reply);
        }
        let mut delay = 500.0 * f64::from(2u32.pow(retry));
        if let Some(retry_after) = reply.headers.get("retry-after").filter(|v| !v.is_empty()) {
            let seconds = jsrt::number::parse_float(retry_after);
            delay = if seconds.is_nan() {
                crate::http::parse_http_date(retry_after).map_or(f64::NAN, |at| at as f64 - env.now_ms())
            } else {
                seconds * 1000.0
            };
            if !delay.is_finite() {
                return Ok(reply);
            }
        }
        let delay = delay.max(0.0);
        if delay >= deadline - env.now_ms() {
            return Ok(reply);
        }
        abortable_sleep(env, delay, Some(&ctx.signal), crate::provider::OPERATION_ABORTED)
            .await
            .map_err(|_| SendError::Aborted)?;
        retry += 1;
    }
}

async fn fetch_copilot_models(ctx: &LoginContext<'_>, token: &str, base_url: &str) -> Result<CopilotCatalog, String> {
    let bearer = format!("Bearer {token}");
    let mut headers = copilot_headers(&bearer);
    headers.push(("X-GitHub-Api-Version", COPILOT_API_VERSION));
    let reply = send_with_rate_limit_retry(ctx, "GET", &format!("{base_url}/models"), &headers, "")
        .await
        .map_err(|e| e.message())?;
    if !reply.ok() {
        return Err(format!("{} {}: {}", reply.status, reply.status_text(), reply.text));
    }
    let raw = jsrt::json_parse::parse_json(&reply.text).map_err(|e| format!("SyntaxError: {e}"))?;
    CopilotCatalog::parse(&raw, base_url == COPILOT_INDIVIDUAL_URL)
}

/// `enableGitHubCopilotModels`: best effort per model; a rate limit that outlasts the retries stops the batch.
async fn enable_copilot_models(
    ctx: &LoginContext<'_>,
    token: &str,
    base_url: &str,
    ids: &[String],
) -> Result<Vec<String>, String> {
    let bearer = format!("Bearer {token}");
    let mut headers = vec![("Content-Type", "application/json"), ("Authorization", bearer.as_str())];
    headers.extend(COPILOT_HEADERS);
    headers.push(("openai-intent", "chat-policy"));
    headers.push(("x-interaction-type", "chat-policy"));
    let mut enabled = Vec::new();
    for id in ids {
        let url = format!("{base_url}/models/{id}/policy");
        match send_with_rate_limit_retry(ctx, "POST", &url, &headers, r#"{"state":"enabled"}"#).await {
            Ok(reply) if reply.status == 429 => {
                if ctx.signal.aborted() {
                    return Err(format!("{} {}: {}", reply.status, reply.status_text(), reply.text));
                }
                break;
            }
            Ok(reply) if reply.ok() => enabled.push(id.clone()),
            Ok(_) => {}
            Err(error) if ctx.signal.aborted() => return Err(error.message()),
            Err(_) => {}
        }
    }
    Ok(enabled)
}