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
//! Claude models: the subscription first, Anthropic API credits second.
//!
//! Problem: a Max plan's five-hour or weekly window runs out mid-session, and every later `claude -p` call fails
//! until the reset. Max plans also carry monthly API credits on a linked Console organization; a key from that
//! organization bills those credits (and stops with a billing error once they are gone, never charging the plan).
//!
//! Every `claude-subscription` call goes through [`Router::stream`] (`dispatch`, the session lane and inference
//! frames all call it), so no path can skip it. `claude-code/<id>` requests with tools also run on the subscription
//! ([`Router::stream_claude_code`]) but never fall back: that provider never billed an API key. The model's tool list is untouched: this is host-side routing
//! of one Messages call, the transcript and its `[rlm]` tool list go to the API exactly as they went to the CLI.
//!
//! Trace: turn 1 runs `claude -p`; it fails with the plan's usage limit ([`super::limit`]) before any content
//! reached the sink, so the same model id and context stream from `anthropic/<id>` with the configured key, and
//! the message carries one [`ROUTE_DIAGNOSTIC`] ("Claude subscription limit reached; using API credits until
//! <reset>"). Turns 2..n go straight to the API until the reset (or [`DEFAULT_HOLD_MS`] when none was reported),
//! with no CLI spawned. The first turn after the reset tries the subscription again and, unless it is still out of
//! usage, carries a route diagnostic naming the switch back. A failed API call ends the hold too (a revoked key, the
//! credits gone, an outage): the next turn tries the subscription again rather than failing on the API until the
//! reset, and a plan still out of usage starts a new hold. A transient 429/529, an error after streamed content,
//! fallback turned off (`claude.apiFallback: false`) or no API key leave the subscription's error as it was (no
//! key adds [`KEY_HINT`]). The hold lives in this process only.

use super::{Config, ERROR_DIAGNOSTIC, catalog};
use crate::http::Transport;
use crate::options::SimpleStreamOptions;
use crate::types::{
    AssistantMessage, AssistantMessageDiagnostic, AssistantMessageEvent, EventSink, Model, StopReason,
    TranscriptContext,
};
use serde_json::{Map, Value};
use std::path::PathBuf;
use std::sync::{Arc, LazyLock, Mutex};

/// The diagnostic on the message where routing switched; `details.route` is `api` or `subscription`, and
/// `details.notice` is the line a UI shows.
pub const ROUTE_DIAGNOSTIC: &str = "claude_subscription_route";
/// How long calls stay on the API when the subscription named no reset time.
pub const DEFAULT_HOLD_MS: i64 = 60 * 60 * 1000;
/// Appended to an out-of-usage error when no API key is configured.
pub const KEY_HINT: &str = "To continue on API credits until the limit resets, set ANTHROPIC_API_KEY (or store an `anthropic` api_key credential in auth.json) to a key from the Console organization that receives your plan's monthly API credits; Claude Code itself never sees that key.";
/// Starts the error when the API credits are exhausted too.
pub const BOTH_EXHAUSTED: &str = "Claude subscription usage limit reached and the Anthropic API credits are exhausted";

pub type Clock = Arc<dyn Fn() -> i64 + Send + Sync>;
pub type Lookup = Arc<dyn Fn(&str) -> Option<String> + Send + Sync>;

/// Where the router reads time, keys and settings, and (tests) how it runs the subscription.
#[derive(Clone)]
pub struct RouterOptions {
    /// Epoch milliseconds.
    pub clock: Clock,
    /// The process environment (`ANTHROPIC_API_KEY`, `ULTRON_CODING_AGENT_DIR`, `HOME`).
    pub env: Lookup,
    /// The agent directory holding `auth.json` and `settings.json`; else resolved from `env`.
    pub agent_dir: Option<PathBuf>,
    /// The subscription's config; else [`super::config_for`] per call.
    pub subscription: Option<Config>,
    /// The API's base URL; else `providers.anthropic.baseUrl` of the agent directory's models.json; else the
    /// Anthropic model's.
    pub api_base_url: Option<String>,
}

impl Default for RouterOptions {
    fn default() -> Self {
        RouterOptions {
            clock: Arc::new(jsrt::date::now_ms),
            env: Arc::new(|name| std::env::var(name).ok().filter(|v| !v.is_empty())),
            agent_dir: None,
            subscription: None,
            api_base_url: None,
        }
    }
}

/// The routing state of one process: until when calls skip the subscription. It changes only under its lock, by
/// [`Router::start_hold`] and [`Router::end_hold`]; ending names the hold it ends, so a call that read an old hold
/// never clears a newer one another call started meanwhile.
pub struct Router {
    options: RouterOptions,
    hold: Mutex<Option<i64>>,
}

static GLOBAL: LazyLock<Arc<Router>> = LazyLock::new(|| Arc::new(Router::new(RouterOptions::default())));

/// The process's router.
pub fn global() -> Arc<Router> {
    GLOBAL.clone()
}

/// Holds `Start` until content arrives and the terminal event until the call is decided, so a call that failed
/// before any output can be replaced without the sink seeing it, and the terminal event carries the final message.
struct Gate<'s, 'e> {
    sink: &'s mut EventSink<'e>,
    start: Option<AssistantMessageEvent>,
    committed: bool,
}

impl<'s, 'e> Gate<'s, 'e> {
    fn new(sink: &'s mut EventSink<'e>) -> Self {
        Gate {
            sink,
            start: None,
            committed: false,
        }
    }

    fn event(&mut self, event: &AssistantMessageEvent) {
        match event {
            AssistantMessageEvent::Start { .. } if !self.committed => self.start = Some(event.clone()),
            AssistantMessageEvent::Done { .. } | AssistantMessageEvent::Error { .. } => {}
            _ => {
                self.commit();
                (self.sink)(event);
            }
        }
    }

    fn commit(&mut self) {
        self.committed = true;
        if let Some(start) = self.start.take() {
            (self.sink)(&start);
        }
    }

    /// Flush what was held and end the stream with `message`.
    fn finish(mut self, message: &AssistantMessage) {
        self.commit();
        let reason = message.stop_reason;
        let event = if matches!(reason, StopReason::Error | StopReason::Aborted) {
            AssistantMessageEvent::Error {
                reason,
                error: message.clone(),
            }
        } else {
            AssistantMessageEvent::Done {
                reason,
                message: message.clone(),
            }
        };
        (self.sink)(&event);
    }
}

/// `Some(reset)` when the subscription's message failed with the plan's usage limit ([`super::limit`]).
pub fn usage_limit_of(message: &AssistantMessage) -> Option<Option<i64>> {
    let details = message
        .diagnostics
        .iter()
        .flatten()
        .find(|d| d.kind == ERROR_DIAGNOSTIC)?
        .details
        .as_ref()?;
    (details.get("usage_limit") == Some(&Value::Bool(true))).then(|| details.get("resets_at").and_then(Value::as_i64))
}

/// Anthropic's answer once the credits are gone: `Your credit balance is too low ... Plans & Billing` (400) or a
/// `billing_error` (402); the recorded native text is `Credit balance is too low`.
fn billing_exhausted(text: &str) -> bool {
    let text = text.to_lowercase();
    text.contains("credit balance") || text.contains("billing")
}

/// `isAnthropicAdaptiveThinkingModel` of `packages/ai/scripts/generate-models.ts`: families that think adaptively.
fn adaptive_family(id: &str) -> bool {
    [
        "opus-4-6",
        "opus-4.6",
        "opus-4-7",
        "opus-4.7",
        "opus-4-8",
        "opus-4.8",
        "opus-5",
        "opus.5",
        "sonnet-4-6",
        "sonnet-4.6",
        "sonnet-5",
        "sonnet.5",
        "fable-5",
        "mythos-5",
        "haiku-5",
        "haiku.5",
    ]
    .iter()
    .any(|family| id.contains(family))
}

/// The newest older release of `id`'s family in the generated catalog: `claude-sonnet-5-5` -> `claude-sonnet-5`.
fn family_template(id: &str) -> Option<&'static Model> {
    let mut stem = id;
    while let Some((head, version)) = stem.rsplit_once('-') {
        if version.is_empty() || !version.chars().all(|c| c.is_ascii_digit()) {
            return None;
        }
        if let Some(model) = crate::models::builtin_model("anthropic", head) {
            return Some(model);
        }
        stem = head;
    }
    None
}

/// `anthropic/<id>` for `claude-subscription/<id>`: aliases and `[1m]` resolved to the canonical id.
///
/// The catalog is generated (models.dev through `packages/ai/scripts/generate-models.ts`, then
/// `data/generate-models.mjs`) and may lag a release the subscription already serves (`claude-sonnet-5-5`). Such
/// an id takes the family's newest catalog entry as its template (limits, cost, compat), with the flags the
/// generator gives the family: `forceAdaptiveThinking` for adaptive families, and no `off` thinking level where
/// the API rejects `thinking: disabled` ([`catalog::MANDATORY_THINKING`]).
pub fn api_model(model: &Model) -> Model {
    let id = catalog::canonical(&model.id);
    if let Some(known) = crate::models::builtin_model("anthropic", id) {
        return known.clone();
    }
    let mut api = family_template(id).cloned().unwrap_or_else(|| {
        let mut api = model.clone();
        api.api = "anthropic-messages".into();
        api.provider = "anthropic".into();
        api
    });
    api.id = id.to_string();
    api.name = model.name.clone();
    api.base_url = catalog::UPSTREAM.into();
    if adaptive_family(id) {
        api.reasoning = true;
        api.compat
            .get_or_insert_default()
            .insert("forceAdaptiveThinking".into(), Value::Bool(true));
    }
    if catalog::MANDATORY_THINKING.iter().any(|family| id.starts_with(family)) {
        let levels = api.thinking_level_map.get_or_insert_default();
        levels.insert("off".into(), None);
        levels.insert("minimal".into(), None);
    }
    api
}

fn route(route: &str, notice: String, extra: Map<String, Value>) -> AssistantMessageDiagnostic {
    let mut details = Map::new();
    details.insert("route".into(), route.into());
    details.insert("notice".into(), notice.into());
    details.extend(extra);
    AssistantMessageDiagnostic {
        kind: ROUTE_DIAGNOSTIC.into(),
        timestamp: jsrt::date::now_ms(),
        error: None,
        details: Some(details),
    }
}

/// Text `notice` of the switch to the API.
pub fn api_notice(until: i64) -> String {
    format!(
        "Claude subscription limit reached; using API credits until {}",
        jsrt::date::iso_string(until)
    )
}

pub const SUBSCRIPTION_NOTICE: &str = "Claude subscription available again; back on the subscription";

impl Router {
    pub fn new(options: RouterOptions) -> Self {
        Router {
            options,
            hold: Mutex::new(None),
        }
    }

    /// Until when calls go straight to the API (epoch ms), if they do.
    pub fn held_until(&self) -> Option<i64> {
        *jsrt::sync::lock(&self.hold)
    }

    /// The plan is out of usage until `until`: calls go to the API until then (or until a later hold's end).
    fn start_hold(&self, until: i64) {
        let mut hold = jsrt::sync::lock(&self.hold);
        *hold = Some(hold.map_or(until, |held| held.max(until)));
    }

    /// End the hold until `until`, unless another call replaced it since it was read.
    fn end_hold(&self, until: i64) {
        let mut hold = jsrt::sync::lock(&self.hold);
        if *hold == Some(until) {
            *hold = None;
        }
    }

    /// The call on API credits while held until `until`; a failed one ends the hold.
    #[allow(clippy::too_many_arguments)]
    async fn api_held(
        &self,
        until: i64,
        model: &Model,
        context: &TranscriptContext,
        options: &SimpleStreamOptions,
        transport: &dyn Transport,
        sink: &mut EventSink<'_>,
        key: &str,
        switched: Option<Switch>,
    ) -> AssistantMessage {
        let message = self.api(model, context, options, transport, sink, key, switched).await;
        if message.stop_reason == StopReason::Error {
            self.end_hold(until);
        }
        message
    }

    fn agent_dir(&self) -> PathBuf {
        self.options.agent_dir.clone().unwrap_or_else(|| {
            let env = &self.options.env;
            let home = env("HOME").map(PathBuf::from).unwrap_or_default();
            config::paths::agent_dir(|name| env(name), &home)
        })
    }

    /// `providers.anthropic.baseUrl` of the agent directory's models.json: the fallback goes where every other
    /// `anthropic` call goes (a proxy, a gateway, a test's stub), never past that override to the public API.
    fn configured_base_url(&self) -> Option<String> {
        let path = config::paths::AgentPaths::new(self.agent_dir()).models();
        crate::model_config::ModelConfig::load(Some(&path.to_string_lossy()))
            .provider("anthropic")?
            .get("baseUrl")?
            .as_str()
            .filter(|url| !url.trim().is_empty())
            .map(str::to_string)
    }

    /// `claude.apiFallback` of the global settings (default on).
    fn enabled(&self, agent_dir: &std::path::Path) -> bool {
        config::settings::SettingsManager::create(agent_dir, agent_dir, false).claude_api_fallback()
    }

    /// The fallback's API key: `ANTHROPIC_API_KEY` (the request's env, then the process's), else the auth store's
    /// `anthropic` api_key credential. Never an OAuth token: that is the subscription again.
    fn api_key(&self, options: &SimpleStreamOptions, agent_dir: &std::path::Path) -> Option<String> {
        let usable = |key: &String| !key.trim().is_empty() && !crate::anthropic::is_oauth_token(key);
        let name = crate::env::ANTHROPIC_API_KEY_ENV;
        let request = options.base.env.as_ref().and_then(|env| env.get(name)).cloned();
        if let Some(key) = request.or_else(|| (self.options.env)(name)).filter(usable) {
            return Some(key);
        }
        let path = config::paths::AgentPaths::new(agent_dir).auth();
        let stored = config::auth::read_stored_credential(&path, "anthropic")?;
        if stored.get("type").and_then(Value::as_str) != Some("api_key") {
            return None;
        }
        // The store resolves `$VAR` templates the way every other reader does.
        let resolved = config::auth::AuthStorage::create(path).read("anthropic").ok()??;
        resolved
            .get("key")
            .and_then(Value::as_str)
            .map(str::to_string)
            .filter(usable)
    }

    async fn subscription(
        &self,
        model: &Model,
        context: &TranscriptContext,
        options: &SimpleStreamOptions,
        sink: &mut EventSink<'_>,
    ) -> AssistantMessage {
        match &self.options.subscription {
            Some(config) => super::stream_with(model, context, options, config.clone(), sink).await,
            None => super::stream_simple(model, context, options, sink).await,
        }
    }

    /// A `claude-code/<id>` request with tools, on the subscription only (never API credits: `claude-code` never
    /// used a key), or refused while a backend override is set ([`super::claude_code_lane`]); `None` for a request
    /// that stays on `claude -p`.
    pub async fn stream_claude_code(
        &self,
        model: &Model,
        context: &TranscriptContext,
        options: &SimpleStreamOptions,
        sink: &mut EventSink<'_>,
    ) -> Option<AssistantMessage> {
        use super::claude_code_lane::{Route, refuse, route};
        let env = || {
            let config = self.options.subscription.clone().unwrap_or_default();
            super::process::base_env(&config, options.base.env.as_ref())
        };
        let subscription = match route(model, context, env)? {
            Route::Refused(error) => return Some(refuse(model, error, sink)),
            Route::Subscription(subscription) => subscription,
        };
        if self.options.subscription.is_some() {
            return Some(self.subscription(&subscription, context, options, sink).await);
        }
        // The `claude` the claude-code provider runs (ULTRON_CLAUDE_CODE_BIN, else PATH), never one found elsewhere.
        let config = super::config_for(&subscription, options).and_then(|(mut config, _)| {
            let cli_env = crate::claude_code_cli::child_env(&options.base);
            let bin = crate::claude_code_cli::resolve_claude_cli(&cli_env).map_err(super::ClaudeError::Missing)?;
            config.command = Some(vec![bin]);
            Ok(config)
        });
        Some(match config {
            Ok(config) => super::stream_with(&subscription, context, options, config, sink).await,
            Err(error) => refuse(model, error.to_string(), sink),
        })
    }

    /// One `claude-subscription` call: the subscription, or the API while the plan is out of usage.
    pub async fn stream(
        &self,
        model: &Model,
        context: &TranscriptContext,
        options: &SimpleStreamOptions,
        transport: &dyn Transport,
        sink: &mut EventSink<'_>,
    ) -> AssistantMessage {
        let agent_dir = self.agent_dir();
        if !self.enabled(&agent_dir) {
            *jsrt::sync::lock(&self.hold) = None;
            return self.subscription(model, context, options, sink).await;
        }
        let now = (self.options.clock)();
        // Back on the subscription because the hold ran out: said on this call's message.
        let mut expired = false;
        if let Some(until) = self.held_until() {
            if now < until {
                if let Some(key) = self.api_key(options, &agent_dir) {
                    return self
                        .api_held(until, model, context, options, transport, sink, &key, None)
                        .await;
                }
            } else {
                expired = true;
            }
            self.end_hold(until);
        }
        let mut gate = Gate::new(sink);
        let mut message = {
            let mut forward = |event: &AssistantMessageEvent| gate.event(event);
            self.subscription(model, context, options, &mut forward).await
        };
        let limit = usage_limit_of(&message);
        match limit {
            Some(resets_at) if !gate.committed => match self.api_key(options, &agent_dir) {
                Some(key) => {
                    let until = resets_at.filter(|r| *r > now).unwrap_or(now + DEFAULT_HOLD_MS);
                    self.start_hold(until);
                    let switched = Switch {
                        until,
                        reason: message.error_message.clone().unwrap_or_default(),
                    };
                    return self
                        .api_held(
                            until,
                            model,
                            context,
                            options,
                            transport,
                            gate.sink,
                            &key,
                            Some(switched),
                        )
                        .await;
                }
                None => {
                    let error = message.error_message.get_or_insert_default();
                    error.push_str(&format!(" ({KEY_HINT})"));
                }
            },
            _ if expired => {
                let mut extra = Map::new();
                extra.insert("provider".into(), super::PROVIDER.into());
                message
                    .diagnostics
                    .get_or_insert_default()
                    .insert(0, route("subscription", SUBSCRIPTION_NOTICE.into(), extra));
            }
            _ => {}
        }
        gate.finish(&message);
        message
    }

    /// The call on API credits: same model id, same context, the fallback key.
    #[allow(clippy::too_many_arguments)]
    async fn api(
        &self,
        model: &Model,
        context: &TranscriptContext,
        options: &SimpleStreamOptions,
        transport: &dyn Transport,
        sink: &mut EventSink<'_>,
        key: &str,
        switched: Option<Switch>,
    ) -> AssistantMessage {
        let mut api_model = api_model(model);
        if let Some(base) = self.options.api_base_url.clone().or_else(|| self.configured_base_url()) {
            api_model.base_url = base;
        }
        let mut request = options.clone();
        request.base.api_key = Some(key.to_string());
        let mut gate = Gate::new(sink);
        let result = {
            let mut forward = |event: &AssistantMessageEvent| gate.event(event);
            crate::anthropic::stream_simple(&api_model, context, &request, transport, &mut forward).await
        };
        let mut message = result.unwrap_or_else(|error| {
            let mut message = AssistantMessage::pending(&api_model, jsrt::date::now_ms());
            message.stop_reason = StopReason::Error;
            message.error_message = Some(error);
            message
        });
        if message.stop_reason == StopReason::Error
            && let Some(error) = message.error_message.clone().filter(|e| billing_exhausted(e))
        {
            let until = self
                .held_until()
                .map(|u| format!(" until {}", jsrt::date::iso_string(u)))
                .unwrap_or_default();
            message.error_message = Some(format!(
                "{BOTH_EXHAUSTED}: the subscription is out of usage{until}, and the API answered: {error}"
            ));
        }
        if let Some(switched) = switched {
            let mut extra = Map::new();
            extra.insert("provider".into(), "anthropic".into());
            extra.insert("model".into(), api_model.id.clone().into());
            extra.insert("until".into(), switched.until.into());
            extra.insert("reason".into(), switched.reason.into());
            message
                .diagnostics
                .get_or_insert_default()
                .insert(0, route("api", api_notice(switched.until), extra));
        }
        gate.finish(&message);
        message
    }
}

struct Switch {
    until: i64,
    reason: String,
}