async-llm 0.10.0

Async Rust client for LLM APIs - Anthropic Messages API today; fork of async-anthropic with thinking-block and prompt-cache support.
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
//! ChatGPT device-login and refreshable credential support.

use crate::responses::ResponsesError;
use async_trait::async_trait;
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine as _};
use serde::{Deserialize, Serialize};
use std::{
    sync::{Arc, RwLock},
    time::{SystemTime, UNIX_EPOCH},
};

pub const DEFAULT_ISSUER: &str = "https://auth.openai.com";
const EXPIRY_SKEW_SECONDS: u64 = 60;
/// OpenAI's documented default when a token response omits `expires_in`.
/// Treating an absent value as "never expires" would strand the credential:
/// nothing would ever refresh it, and every call after the hour would 401.
const DEFAULT_EXPIRES_IN_SECONDS: u64 = 3600;
const DEFAULT_POLL_INTERVAL_SECONDS: u64 = 5;

/// Identifies the calling client to OpenAI's ChatGPT auth and Codex endpoints.
///
/// The device-auth endpoints are not the public OAuth ones: they key their
/// behaviour off `client_id` and off an `originator` header naming the tool
/// making the request.
#[derive(Clone, Debug)]
pub struct ChatGptAuth {
    issuer: String,
    client_id: String,
    originator: Option<String>,
}

impl ChatGptAuth {
    #[must_use]
    pub fn new(client_id: impl Into<String>) -> Self {
        Self {
            issuer: DEFAULT_ISSUER.into(),
            client_id: client_id.into(),
            originator: None,
        }
    }

    /// Point the flow at a different issuer. Tests only — there is one OpenAI.
    #[must_use]
    pub fn with_issuer(mut self, issuer: impl Into<String>) -> Self {
        self.issuer = issuer.into().trim_end_matches('/').into();
        self
    }

    #[must_use]
    pub fn with_originator(mut self, originator: impl Into<String>) -> Self {
        self.originator = Some(originator.into());
        self
    }

    #[must_use]
    pub fn issuer(&self) -> &str {
        &self.issuer
    }

    #[must_use]
    pub fn client_id(&self) -> &str {
        &self.client_id
    }

    #[must_use]
    pub fn originator(&self) -> Option<&str> {
        self.originator.as_deref()
    }

    /// Where the person approves a device login. OpenAI's user-code response
    /// does not carry it, so it is derived rather than read.
    #[must_use]
    pub fn verification_url(&self) -> String {
        format!("{}/codex/device", self.issuer)
    }

    fn identify(&self, builder: reqwest::RequestBuilder) -> reqwest::RequestBuilder {
        match &self.originator {
            Some(originator) => builder.header("originator", originator),
            None => builder,
        }
    }
}

/// Persisted ChatGPT OAuth tokens and their selected account identifier.
#[derive(Clone, Default, Deserialize, Serialize, PartialEq, Eq)]
pub struct StoredTokens {
    pub access_token: String,
    pub refresh_token: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub id_token: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub account_id: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub expires_at: Option<u64>,
}

impl std::fmt::Debug for StoredTokens {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("StoredTokens")
            .field("access_token", &"<redacted>")
            .field("refresh_token", &"<redacted>")
            .field("id_token", &self.id_token.as_ref().map(|_| "<redacted>"))
            .field("account_id", &self.account_id)
            .field("expires_at", &self.expires_at)
            .finish()
    }
}

impl StoredTokens {
    pub fn new(
        access_token: impl Into<String>,
        refresh_token: impl Into<String>,
        id_token: impl Into<String>,
    ) -> Result<Self, ResponsesError> {
        let id_token = id_token.into();
        let account_id = account_id_from_id_token(&id_token)?;
        Ok(Self {
            access_token: access_token.into(),
            refresh_token: refresh_token.into(),
            id_token: Some(id_token),
            account_id,
            expires_at: None,
        })
    }

    fn expires_soon(&self) -> bool {
        self.expires_at
            .is_some_and(|expires_at| expires_at <= unix_time() + EXPIRY_SKEW_SECONDS)
    }
}

/// Storage used to load and persist refreshable ChatGPT credentials.
#[async_trait]
pub trait TokenStore: Send + Sync {
    async fn load(&self) -> Result<Option<StoredTokens>, ResponsesError>;
    async fn save(&self, tokens: StoredTokens) -> Result<(), ResponsesError>;
}

/// Refreshable ChatGPT OAuth tokens.
pub struct ChatGptTokens {
    tokens: RwLock<StoredTokens>,
    store: Arc<dyn TokenStore>,
    auth: ChatGptAuth,
}

impl std::fmt::Debug for ChatGptTokens {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("ChatGptTokens")
            .field("auth", &self.auth)
            .finish_non_exhaustive()
    }
}

impl ChatGptTokens {
    #[must_use]
    pub fn new(tokens: StoredTokens, store: Arc<dyn TokenStore>, auth: ChatGptAuth) -> Self {
        Self {
            tokens: RwLock::new(tokens),
            store,
            auth,
        }
    }

    #[must_use]
    pub fn auth(&self) -> &ChatGptAuth {
        &self.auth
    }

    pub async fn access_token(&self) -> Result<String, ResponsesError> {
        let expires_soon = self
            .tokens
            .read()
            .map_err(|_| ResponsesError::Authentication("token lock poisoned".into()))?
            .expires_soon();
        if expires_soon {
            self.refresh().await?;
        }
        self.tokens
            .read()
            .map_err(|_| ResponsesError::Authentication("token lock poisoned".into()))
            .map(|tokens| tokens.access_token.clone())
    }

    pub async fn account_id(&self) -> Result<Option<String>, ResponsesError> {
        self.tokens
            .read()
            .map_err(|_| ResponsesError::Authentication("token lock poisoned".into()))
            .map(|tokens| tokens.account_id.clone())
    }

    /// Exchanges the stored refresh token and persists the replacement tokens.
    pub async fn refresh(&self) -> Result<(), ResponsesError> {
        let existing = self
            .tokens
            .read()
            .map_err(|_| ResponsesError::Authentication("token lock poisoned".into()))?
            .clone();
        let response = self
            .auth
            .identify(
                reqwest::Client::new()
                    .post(format!("{}/oauth/token", self.auth.issuer))
                    .form(&[
                        ("grant_type", "refresh_token"),
                        ("refresh_token", existing.refresh_token.as_str()),
                        ("client_id", self.auth.client_id.as_str()),
                    ]),
            )
            .send()
            .await?;
        let tokens = parse_token_response(response, Some(&existing)).await?;
        self.store.save(tokens.clone()).await?;
        *self
            .tokens
            .write()
            .map_err(|_| ResponsesError::Authentication("token lock poisoned".into()))? = tokens;
        Ok(())
    }
}

/// The device code displayed to a user during ChatGPT login.
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
pub struct DeviceLogin {
    pub device_auth_id: String,
    pub user_code: String,
    /// Where the person approves this login. OpenAI's user-code response omits
    /// it, so `start_device_login` derives it from the issuer.
    #[serde(default)]
    pub verification_uri: String,
    /// Seconds to wait between polls. OpenAI sends this as a JSON *string*, so
    /// it is read leniently; under a second would rate-limit the very login it
    /// is trying to complete.
    #[serde(
        default = "default_poll_interval",
        deserialize_with = "deserialize_poll_interval"
    )]
    pub interval: u64,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub code_verifier: Option<String>,
}

fn default_poll_interval() -> u64 {
    DEFAULT_POLL_INTERVAL_SECONDS
}

fn deserialize_poll_interval<'de, D>(deserializer: D) -> Result<u64, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let value = serde_json::Value::deserialize(deserializer)?;
    let seconds = match &value {
        serde_json::Value::String(text) => text.parse::<u64>().ok(),
        other => other.as_u64(),
    };
    Ok(seconds.unwrap_or(DEFAULT_POLL_INTERVAL_SECONDS).max(1))
}

impl DeviceLogin {
    #[must_use]
    pub fn new(
        device_auth_id: impl Into<String>,
        user_code: impl Into<String>,
        verification_uri: impl Into<String>,
        interval: u64,
    ) -> Self {
        Self {
            device_auth_id: device_auth_id.into(),
            user_code: user_code.into(),
            verification_uri: verification_uri.into(),
            interval,
            code_verifier: None,
        }
    }

    #[must_use]
    pub fn authorization_request(&self, client_id: impl Into<String>) -> serde_json::Value {
        serde_json::json!({"client_id": client_id.into()})
    }

    #[must_use]
    pub fn poll_request(&self) -> serde_json::Value {
        serde_json::json!({
            "device_auth_id": self.device_auth_id,
            "user_code": self.user_code,
        })
    }
}

/// The current state of a device login poll.
#[derive(Debug)]
pub enum DeviceLoginPoll {
    Pending,
    Approved(Arc<ChatGptTokens>),
}

/// Starts the ChatGPT device authorization flow.
pub async fn start_device_login(
    http_client: &reqwest::Client,
    auth: &ChatGptAuth,
) -> Result<DeviceLogin, ResponsesError> {
    let response = auth
        .identify(
            http_client
                .post(format!(
                    "{}/api/accounts/deviceauth/usercode",
                    auth.issuer()
                ))
                .json(&serde_json::json!({"client_id": auth.client_id()})),
        )
        .send()
        .await?;
    let mut login: DeviceLogin = parse_json_response(response).await?;
    if login.verification_uri.is_empty() {
        login.verification_uri = auth.verification_url();
    }
    Ok(login)
}

/// Polls a device authorization flow. Pending authorization is not an error.
///
/// The device-auth endpoint answers 4xx for the whole window in which the
/// person has not yet approved the code, so an unsuccessful poll is reported as
/// [`DeviceLoginPoll::Pending`] rather than as a failure — a login that is
/// merely unfinished must not look like a broken one.
pub async fn poll_device_login(
    http_client: &reqwest::Client,
    auth: &ChatGptAuth,
    login: &DeviceLogin,
    store: Arc<dyn TokenStore>,
) -> Result<DeviceLoginPoll, ResponsesError> {
    let response = auth
        .identify(
            http_client
                .post(format!("{}/api/accounts/deviceauth/token", auth.issuer()))
                .json(&login.poll_request()),
        )
        .send()
        .await?;
    let status = response.status();
    let body = response.text().await?;
    if !status.is_success() {
        return Ok(DeviceLoginPoll::Pending);
    }
    // An approval that carries neither code nor verifier is the same
    // "not finished yet" state expressed as a 200.
    let Ok(approval) = serde_json::from_str::<DeviceApproval>(&body) else {
        return Ok(DeviceLoginPoll::Pending);
    };
    let Some(code_verifier) = approval
        .code_verifier
        .or_else(|| login.code_verifier.clone())
    else {
        return Ok(DeviceLoginPoll::Pending);
    };
    // OpenAI's own device callback. Nothing ever navigates to it — declaring it
    // is the protocol formality that lets a server with no reachable redirect
    // complete the exchange at all.
    let redirect_uri = format!("{}/deviceauth/callback", auth.issuer());
    let response = auth
        .identify(
            http_client
                .post(format!("{}/oauth/token", auth.issuer()))
                .form(&[
                    ("grant_type", "authorization_code"),
                    ("client_id", auth.client_id()),
                    ("code", approval.authorization_code.as_str()),
                    ("redirect_uri", redirect_uri.as_str()),
                    ("code_verifier", code_verifier.as_str()),
                ]),
        )
        .send()
        .await?;
    let tokens = parse_token_response(response, None).await?;
    store.save(tokens.clone()).await?;
    Ok(DeviceLoginPoll::Approved(Arc::new(ChatGptTokens::new(
        tokens,
        store,
        auth.clone(),
    ))))
}

#[derive(Deserialize)]
struct DeviceApproval {
    authorization_code: String,
    #[serde(default)]
    code_verifier: Option<String>,
}

#[derive(Deserialize)]
struct TokenResponse {
    access_token: String,
    #[serde(default)]
    refresh_token: Option<String>,
    #[serde(default)]
    id_token: Option<String>,
    #[serde(default)]
    expires_in: Option<u64>,
}

async fn parse_token_response(
    response: reqwest::Response,
    existing: Option<&StoredTokens>,
) -> Result<StoredTokens, ResponsesError> {
    let response = parse_json_response::<TokenResponse>(response).await?;
    let has_id_token = response.id_token.is_some();
    let id_token = response
        .id_token
        .or_else(|| existing.and_then(|tokens| tokens.id_token.clone()));
    let account_id = if has_id_token {
        id_token
            .as_deref()
            .map(account_id_from_id_token)
            .transpose()?
            .flatten()
    } else {
        existing.and_then(|tokens| tokens.account_id.clone())
    };
    Ok(StoredTokens {
        access_token: response.access_token,
        refresh_token: response
            .refresh_token
            .or_else(|| existing.map(|tokens| tokens.refresh_token.clone()))
            .ok_or_else(|| {
                ResponsesError::Authentication("token response omitted refresh_token".into())
            })?,
        id_token,
        account_id,
        expires_at: Some(
            unix_time().saturating_add(response.expires_in.unwrap_or(DEFAULT_EXPIRES_IN_SECONDS)),
        ),
    })
}

async fn parse_json_response<T: serde::de::DeserializeOwned>(
    response: reqwest::Response,
) -> Result<T, ResponsesError> {
    let status = response.status();
    let body = response.text().await?;
    if !status.is_success() {
        // A 5xx from the auth endpoint is the endpoint being unwell, not the
        // credential being rejected. Keeping the two apart is what lets a
        // caller retry instead of discarding a refresh token that still works.
        return Err(if status.as_u16() >= 500 {
            ResponsesError::Overloaded {
                status: status.as_u16(),
                body,
            }
        } else {
            ResponsesError::Api {
                status: status.as_u16(),
                body,
            }
        });
    }
    serde_json::from_str(&body)
        .map_err(|error| ResponsesError::Authentication(format!("invalid auth response: {error}")))
}

fn account_id_from_id_token(id_token: &str) -> Result<Option<String>, ResponsesError> {
    let payload = id_token
        .split('.')
        .nth(1)
        .ok_or_else(|| ResponsesError::Authentication("invalid ID token".into()))?;
    let bytes = URL_SAFE_NO_PAD.decode(payload).map_err(|error| {
        ResponsesError::Authentication(format!("invalid ID token payload: {error}"))
    })?;
    let claims: serde_json::Value = serde_json::from_slice(&bytes).map_err(|error| {
        ResponsesError::Authentication(format!("invalid ID token claims: {error}"))
    })?;

    Ok(claims
        .get("chatgpt_account_id")
        .and_then(serde_json::Value::as_str)
        .or_else(|| {
            claims
                .get("https://api.openai.com/auth")
                .and_then(|auth| auth.get("chatgpt_account_id"))
                .and_then(serde_json::Value::as_str)
        })
        .or_else(|| {
            claims
                .get("organizations")
                .and_then(serde_json::Value::as_array)
                .and_then(|organizations| organizations.first())
                .and_then(|organization| organization.get("id"))
                .and_then(serde_json::Value::as_str)
        })
        .map(ToOwned::to_owned))
}

fn unix_time() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs()
}