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
//! OAuth refresh and request-auth derivation for the subscription providers of `auth/oauth/*.ts`: Anthropic (Claude
//! Pro/Max), OpenAI Codex (ChatGPT) and GitHub Copilot. Network goes through [`Transport`]; `now_ms` replaces
//! `Date.now()`. Credentials keep the stored `auth.json` shape (`{"type":"oauth", refresh, access, expires, ...}`).
//!
//! Evidence: `fixtures/generate-rest.mjs oauth` (its `subscription` cases) drives the TypeScript `OAuthAuth.refresh`/`toAuth` through a stubbed
//! `fetch`; `tests/oauth.rs` replays the recorded responses here and compares requests and results.

pub use crate::auth::{ModelAuth, OAuthCredential};
use crate::http::{AbortSignal, HeaderMap, HttpRequest, Transport};
use crate::oauth_flow::form_urlencode;
use jsrt::json_parse::parse_json;
use serde_json::{Map, Value, json};

pub(crate) const ANTHROPIC_CLIENT_ID: &str = "9d1c250a-e61b-44d9-88ed-5944d1962f5e";
pub(crate) const ANTHROPIC_TOKEN_URL: &str = "https://platform.claude.com/v1/oauth/token";
pub(crate) const CODEX_CLIENT_ID: &str = "app_EMoamEEZ73f0CkXaXp7hrann";
pub(crate) const CODEX_TOKEN_URL: &str = "https://auth.openai.com/oauth/token";
const CODEX_JWT_CLAIM_PATH: &str = "https://api.openai.com/auth";
pub(crate) const COPILOT_HEADERS: [(&str, &str); 4] = [
    ("user-agent", "GitHubCopilotChat/0.35.0"),
    ("editor-version", "vscode/1.107.0"),
    ("editor-plugin-version", "copilot-chat/0.35.0"),
    ("copilot-integration-id", "vscode-chat"),
];
pub(crate) const COPILOT_API_VERSION: &str = "2026-06-01";
pub(crate) const COPILOT_INDIVIDUAL_URL: &str = "https://api.individual.githubcopilot.com";
pub(crate) const FIVE_MINUTES_MS: f64 = 5.0 * 60.0 * 1000.0;

/// Which OAuth flow a credential belongs to.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum OAuthProvider {
    Anthropic,
    OpenAiCodex,
    GitHubCopilot,
}

impl OAuthProvider {
    pub fn from_id(id: &str) -> Option<Self> {
        match id {
            "anthropic" => Some(Self::Anthropic),
            "openai-codex" => Some(Self::OpenAiCodex),
            "github-copilot" => Some(Self::GitHubCopilot),
            _ => None,
        }
    }

    /// `OAuthAuth.refresh`: exchanges the refresh token for a new credential. `Err` is the upstream error message.
    pub async fn refresh(
        self,
        transport: &dyn Transport,
        credential: &OAuthCredential,
        now_ms: f64,
        signal: Option<&AbortSignal>,
    ) -> Result<OAuthCredential, String> {
        match self {
            Self::Anthropic => refresh_anthropic(transport, &credential.refresh, now_ms, signal).await,
            Self::OpenAiCodex => refresh_codex(transport, &credential.refresh, now_ms, signal).await,
            Self::GitHubCopilot => {
                let domain = copilot_enterprise_domain(credential);
                refresh_copilot(transport, &credential.refresh, domain.as_deref(), signal).await
            }
        }
    }

    /// `OAuthAuth.toAuth`.
    pub fn to_auth(self, credential: &OAuthCredential) -> ModelAuth {
        let base_url = (self == Self::GitHubCopilot).then(|| {
            copilot_base_url(
                Some(&credential.access),
                copilot_enterprise_domain(credential).as_deref(),
            )
        });
        ModelAuth {
            api_key: Some(credential.access.clone()),
            headers: None,
            base_url,
        }
    }
}

pub(crate) struct Reply {
    pub status: u16,
    pub headers: HeaderMap,
    pub text: String,
}

impl Reply {
    pub fn ok(&self) -> bool {
        (200..300).contains(&self.status)
    }

    pub fn status_text(&self) -> &'static str {
        status_text(self.status)
    }
}

/// Why a request produced no response: the signal aborted it (`AbortError`) or the transport failed (`TypeError`).
#[derive(Debug)]
pub(crate) enum SendError {
    Aborted,
    Network(String),
}

impl SendError {
    pub fn message(&self) -> String {
        match self {
            Self::Aborted => crate::provider::OPERATION_ABORTED.into(),
            Self::Network(message) => message.clone(),
        }
    }

    /// `${error.name}: ${error.message}`.
    pub fn detail(&self) -> String {
        match self {
            Self::Aborted => format!("AbortError: {}", self.message()),
            Self::Network(message) => format!("TypeError: {message}"),
        }
    }
}

pub(crate) async fn send(
    transport: &dyn Transport,
    method: &str,
    url: &str,
    headers: &[(&str, &str)],
    body: String,
    signal: Option<&AbortSignal>,
) -> Result<Reply, SendError> {
    if signal.is_some_and(AbortSignal::aborted) {
        return Err(SendError::Aborted);
    }
    let headers: HeaderMap = headers
        .iter()
        .map(|(k, v)| (k.to_ascii_lowercase(), (*v).to_owned()))
        .collect();
    let request = HttpRequest {
        method: method.into(),
        url: url.into(),
        headers,
        body,
    };
    let mut response = transport.send(request).await.map_err(SendError::Network)?;
    let mut bytes = Vec::new();
    while let Some(chunk) = response.body.next_chunk().await.map_err(SendError::Network)? {
        bytes.extend(chunk);
    }
    Ok(Reply {
        status: response.status,
        headers: response.headers,
        text: String::from_utf8_lossy(&bytes).into_owned(),
    })
}

pub(crate) fn status_text(status: u16) -> &'static str {
    reqwest::StatusCode::from_u16(status)
        .ok()
        .and_then(|s| s.canonical_reason())
        .unwrap_or("")
}

async fn refresh_anthropic(
    transport: &dyn Transport,
    refresh: &str,
    now_ms: f64,
    signal: Option<&AbortSignal>,
) -> Result<OAuthCredential, String> {
    let body = json!({"grant_type": "refresh_token", "client_id": ANTHROPIC_CLIENT_ID, "refresh_token": refresh});
    anthropic_token_request(
        transport,
        &body,
        now_ms,
        signal,
        |details| format!("Anthropic token refresh request failed. url={ANTHROPIC_TOKEN_URL}; details={details}"),
        "Anthropic token refresh",
    )
    .await
}

/// `postJson` to the Anthropic token endpoint plus the credential built from its reply. `failed` wraps request
/// failures; `lead` starts the invalid-JSON and missing-field messages.
pub(crate) async fn anthropic_token_request(
    transport: &dyn Transport,
    body: &Value,
    now_ms: f64,
    signal: Option<&AbortSignal>,
    failed: impl Fn(String) -> String,
    lead: &str,
) -> Result<OAuthCredential, String> {
    let headers = [("Content-Type", "application/json"), ("Accept", "application/json")];
    let reply = send(
        transport,
        "POST",
        ANTHROPIC_TOKEN_URL,
        &headers,
        jsrt::json::stringify(body),
        signal,
    )
    .await
    .map_err(|e| failed(e.detail()))?;
    if !reply.ok() {
        return Err(failed(format!(
            "Error: HTTP request failed. status={}; url={ANTHROPIC_TOKEN_URL}; body={}",
            reply.status, reply.text
        )));
    }
    let data = parse_json(&reply.text).map_err(|e| {
        format!(
            "{lead} returned invalid JSON. url={ANTHROPIC_TOKEN_URL}; body={}; details=SyntaxError: {e}",
            reply.text
        )
    })?;
    // ponytail: upstream stores `undefined`/NaN for missing fields; here they are errors.
    let field = |name: &str| data.get(name).and_then(Value::as_str).map(str::to_owned);
    let (Some(access), Some(refresh), Some(expires_in)) = (
        field("access_token"),
        field("refresh_token"),
        data.get("expires_in").and_then(Value::as_f64),
    ) else {
        return Err(format!(
            "{lead} response missing fields: {}",
            jsrt::json::stringify(&data)
        ));
    };
    Ok(OAuthCredential {
        refresh,
        access,
        expires: now_ms + expires_in * 1000.0 - FIVE_MINUTES_MS,
        extra: Map::new(),
    })
}

async fn refresh_codex(
    transport: &dyn Transport,
    refresh: &str,
    now_ms: f64,
    signal: Option<&AbortSignal>,
) -> Result<OAuthCredential, String> {
    let body = form_urlencode(&[
        ("grant_type", "refresh_token"),
        ("refresh_token", refresh),
        ("client_id", CODEX_CLIENT_ID),
    ]);
    let headers = [("Content-Type", "application/x-www-form-urlencoded")];
    let reply = send(transport, "POST", CODEX_TOKEN_URL, &headers, body, signal)
        .await
        .map_err(|e| format!("OpenAI Codex token refresh error: {}", e.message()))?;
    codex_credential(&reply, "refresh", now_ms)
}

/// `readTokenResponse` + `credentialsFromToken`; `operation` is "exchange" or "refresh".
pub(crate) fn codex_credential(reply: &Reply, operation: &str, now_ms: f64) -> Result<OAuthCredential, String> {
    if !reply.ok() {
        let text = if reply.text.is_empty() {
            reply.status_text()
        } else {
            &reply.text
        };
        return Err(format!(
            "OpenAI Codex token {operation} failed ({}): {text}",
            reply.status
        ));
    }
    let data = parse_json(&reply.text).map_err(|e| format!("SyntaxError: {e}"))?;
    let non_empty = |name: &str| {
        data.get(name)
            .and_then(Value::as_str)
            .filter(|s| !s.is_empty())
            .map(str::to_owned)
    };
    let (Some(access), Some(refresh), Some(expires_in)) = (
        non_empty("access_token"),
        non_empty("refresh_token"),
        data.get("expires_in").and_then(Value::as_f64),
    ) else {
        return Err(format!(
            "OpenAI Codex token {operation} response missing fields: {}",
            jsrt::json::stringify(&data)
        ));
    };
    let account_id = codex_account_id(&access).ok_or("Failed to extract accountId from token")?;
    let mut extra = Map::new();
    extra.insert("accountId".into(), json!(account_id));
    Ok(OAuthCredential {
        refresh,
        access,
        expires: now_ms + expires_in * 1000.0,
        extra,
    })
}

/// The ChatGPT account id in a Codex access token's JWT payload (`getAccountId`).
pub fn codex_account_id(access_token: &str) -> Option<String> {
    let parts: Vec<&str> = access_token.split('.').collect();
    if parts.len() != 3 {
        return None;
    }
    // `atob` yields one char per byte; JSON.parse reads that Latin-1 string.
    let decoded = crate::jsutil::latin1(&jsrt::base64::atob(parts[1])?);
    let payload = parse_json(&decoded).ok()?;
    let id = payload.get(CODEX_JWT_CLAIM_PATH)?.get("chatgpt_account_id")?.as_str()?;
    (!id.is_empty()).then(|| id.to_owned())
}

/// `normalizeDomain`: the lower-cased host of a GitHub Enterprise URL or bare domain.
pub fn normalize_domain(input: &str) -> Option<String> {
    let trimmed = input.trim();
    if trimmed.is_empty() {
        return None;
    }
    let url = if trimmed.contains("://") {
        trimmed.to_owned()
    } else {
        format!("https://{trimmed}")
    };
    reqwest::Url::parse(&url).ok()?.host_str().map(str::to_owned)
}

pub(crate) fn copilot_enterprise_domain(credential: &OAuthCredential) -> Option<String> {
    let url = credential
        .extra
        .get("enterpriseUrl")?
        .as_str()
        .filter(|s| !s.is_empty())?;
    normalize_domain(url)
}

/// `getGitHubCopilotBaseUrl`: the token's `proxy-ep` host (as `api.`), else the enterprise or individual endpoint.
pub fn copilot_base_url(token: Option<&str>, enterprise_domain: Option<&str>) -> String {
    let from_token = token.filter(|t| !t.is_empty()).and_then(|t| {
        let host = t
            .match_indices("proxy-ep=")
            .find_map(|(i, m)| t[i + m.len()..].split(';').next().filter(|h| !h.is_empty()))?;
        Some(format!(
            "https://{}",
            host.strip_prefix("proxy.")
                .map_or(host.to_owned(), |rest| format!("api.{rest}"))
        ))
    });
    from_token.unwrap_or_else(|| match enterprise_domain.filter(|d| !d.is_empty()) {
        Some(domain) => format!("https://copilot-api.{domain}"),
        None => COPILOT_INDIVIDUAL_URL.to_owned(),
    })
}

/// `fetchJson`: GET/POST, `${status} ${statusText}: ${text}` on a non-2xx reply, then the parsed body.
pub(crate) async fn fetch_copilot_json(
    transport: &dyn Transport,
    method: &str,
    url: &str,
    headers: &[(&str, &str)],
    body: String,
    signal: Option<&AbortSignal>,
) -> Result<Value, String> {
    let reply = send(transport, method, url, headers, body, signal)
        .await
        .map_err(|e| e.message())?;
    if !reply.ok() {
        return Err(format!("{} {}: {}", reply.status, reply.status_text(), reply.text));
    }
    parse_json(&reply.text).map_err(|e| format!("SyntaxError: {e}"))
}

pub(crate) fn copilot_headers(authorization: &str) -> Vec<(&str, &str)> {
    let mut headers = vec![("Accept", "application/json"), ("Authorization", authorization)];
    headers.extend(COPILOT_HEADERS);
    headers
}

/// `refreshGitHubCopilotAccessToken`: trades a GitHub token for a Copilot token (no model list).
pub(crate) async fn copilot_access_token(
    transport: &dyn Transport,
    github_token: &str,
    enterprise_domain: Option<&str>,
    signal: Option<&AbortSignal>,
) -> Result<OAuthCredential, String> {
    let domain = enterprise_domain.unwrap_or("github.com");
    let bearer = format!("Bearer {github_token}");
    let url = format!("https://api.{domain}/copilot_internal/v2/token");
    let raw = fetch_copilot_json(transport, "GET", &url, &copilot_headers(&bearer), String::new(), signal).await?;
    if !raw.is_object() {
        return Err("Invalid Copilot token response".into());
    }
    let (Some(token), Some(expires_at)) = (
        raw.get("token").and_then(Value::as_str),
        raw.get("expires_at").and_then(Value::as_f64),
    ) else {
        return Err("Invalid Copilot token response fields".into());
    };
    let mut extra = Map::new();
    if let Some(domain) = enterprise_domain {
        extra.insert("enterpriseUrl".into(), json!(domain));
    }
    Ok(OAuthCredential {
        refresh: github_token.to_owned(),
        access: token.to_owned(),
        expires: expires_at * 1000.0 - FIVE_MINUTES_MS,
        extra,
    })
}

async fn refresh_copilot(
    transport: &dyn Transport,
    refresh: &str,
    enterprise_domain: Option<&str>,
    signal: Option<&AbortSignal>,
) -> Result<OAuthCredential, String> {
    let mut credential = copilot_access_token(transport, refresh, enterprise_domain, signal).await?;
    // fetchGitHubCopilotModels with maxRetries 0: one request, no 429 retry.
    let base_url = copilot_base_url(Some(&credential.access), enterprise_domain);
    let bearer = format!("Bearer {}", credential.access);
    let mut headers = copilot_headers(&bearer);
    headers.push(("X-GitHub-Api-Version", COPILOT_API_VERSION));
    let models = fetch_copilot_json(
        transport,
        "GET",
        &format!("{base_url}/models"),
        &headers,
        String::new(),
        signal,
    )
    .await?;
    let catalog = CopilotCatalog::parse(&models, base_url == COPILOT_INDIVIDUAL_URL)?;
    credential
        .extra
        .insert("availableModelIds".into(), json!(catalog.available));
    Ok(credential)
}

/// `parseGitHubCopilotModelCatalog`.
pub(crate) struct CopilotCatalog {
    /// Picker-enabled, tool-capable models not disabled by policy; on the individual endpoint, policy-enabled models
    /// when no picker flag is set.
    pub available: Vec<String>,
    /// Built-in Copilot models whose policy is still unconfigured, to enable at login.
    pub policy: Vec<String>,
}

impl CopilotCatalog {
    pub fn parse(raw: &Value, allow_policy_fallback: bool) -> Result<Self, String> {
        let data = raw
            .get("data")
            .and_then(Value::as_array)
            .ok_or("Invalid Copilot models response")?;
        let models: Vec<(&str, bool, Option<&Value>)> = data
            .iter()
            .filter_map(|item| {
                let id = item.as_object()?.get("id")?.as_str()?;
                let tool_calls = item
                    .get("capabilities")
                    .and_then(|c| c.get("supports"))
                    .and_then(|s| s.get("tool_calls"));
                if tool_calls == Some(&Value::Bool(false)) {
                    return None;
                }
                let picker = item.get("model_picker_enabled") == Some(&Value::Bool(true));
                Some((id, picker, item.get("policy").and_then(|p| p.get("state"))))
            })
            .collect();
        let state_is = |state: Option<&Value>, want: &str| state.and_then(Value::as_str) == Some(want);
        let picker: Vec<String> = models
            .iter()
            .filter(|(_, picker, state)| *picker && !state_is(*state, "disabled"))
            .map(|(id, ..)| (*id).to_owned())
            .collect();
        let use_policy_fallback = allow_policy_fallback && picker.is_empty();
        let policy = models
            .iter()
            .filter(|(id, picker, state)| {
                state_is(*state, "unconfigured")
                    && crate::models::builtin_model("github-copilot", id).is_some()
                    && (*picker || use_policy_fallback)
            })
            .map(|(id, ..)| (*id).to_owned())
            .collect();
        let available = if !use_policy_fallback {
            picker
        } else {
            models
                .iter()
                .filter(|(.., state)| state_is(*state, "enabled"))
                .map(|(id, ..)| (*id).to_owned())
                .collect()
        };
        Ok(CopilotCatalog { available, policy })
    }
}