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
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
//! The user's `models.json` providers and models over the built-in catalog (`coding-agent/src/core/provider-composer.ts`
//! as `model-runtime.ts` uses it): custom providers and models, built-in providers with a new base URL, headers,
//! `compat` or models, per-model `modelOverrides`, composition errors (`getError`), availability (`checkAuth`) and
//! request auth (`getAuth`: the configured API key as a literal, `$VAR` template or `!command`, provider and model
//! headers, `authHeader`), plus the config values of `resolve-config-value.ts`.
//!
//! Credential-blind: callers pass the environment lookup and the key the built-in provider would use.
//!
//! Evidence: `fixtures/generate-model-registry.mjs` runs the TypeScript model runtime over models.json files;
//! `tests/model_registry.rs` replays it.

use crate::auth::merge_headers;
use crate::model_config::ModelConfig;
use crate::options::{ProviderHeaders, SimpleStreamOptions};
use crate::types::Model;
use serde_json::{Map, Value, json};
use std::path::Path;
use std::sync::Arc;

/// An environment variable lookup.
pub type Lookup<'a> = &'a dyn Fn(&str) -> Option<String>;

/// An environment variable lookup a model keeps for its requests.
pub type SharedLookup = Arc<dyn Fn(&str) -> Option<String> + Send + Sync>;

/// What a provider's resolved auth adds to a request (`AuthResult` beyond its key): the base URL a stored OAuth
/// credential names (GitHub Copilot's token proxy endpoint), its headers (Kimi For Coding's `Authorization: Bearer
/// <access>`, `ANTHROPIC_AUTH_TOKEN`'s bearer, the AI Gateway's `cf-aig-authorization`), and the provider env it
/// resolves (a stored credential's `env`, Cloudflare's account and gateway ids), which the request's own env
/// overrides (`applyAuth`).
#[derive(Clone, Debug, Default, PartialEq)]
pub struct StoredRequestAuth {
    pub base_url: Option<String>,
    pub headers: Option<ProviderHeaders>,
    pub env: Option<crate::env::ProviderEnv>,
}

/// The [`StoredRequestAuth`] of the provider's stored OAuth credential, resolved (and refreshed) at request time.
pub type StoredAuth = Arc<dyn Fn() -> Option<StoredRequestAuth> + Send + Sync>;

/// What models.json adds to a model's requests, kept raw and resolved for each request as the TypeScript request
/// auth does (`getAuth` per request: `$VAR` templates read the environment then, `!command` values run then, with no
/// cache). A header that fails to resolve fails that request with its message.
#[derive(Clone, Default)]
pub struct RequestConfig {
    provider: String,
    model: String,
    /// `providers.<p>.headers`.
    provider_headers: Vec<(String, String)>,
    /// `modelOverrides.<id>.headers` under `models[].headers`.
    model_headers: Vec<(String, String)>,
    /// `authHeader`: send `Authorization: Bearer <key>`.
    pub auth_header: bool,
    lookup: Option<SharedLookup>,
    /// The provider's stored OAuth credential's request base URL and headers, when it has one.
    pub stored_auth: Option<StoredAuth>,
}

impl std::fmt::Debug for RequestConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RequestConfig")
            .field(
                "provider_headers",
                &self.provider_headers.iter().map(|(k, _)| k).collect::<Vec<_>>(),
            )
            .field(
                "model_headers",
                &self.model_headers.iter().map(|(k, _)| k).collect::<Vec<_>>(),
            )
            .field("auth_header", &self.auth_header)
            .field("stored_auth", &self.stored_auth.is_some())
            .finish()
    }
}

impl PartialEq for RequestConfig {
    fn eq(&self, other: &Self) -> bool {
        (
            &self.provider,
            &self.model,
            &self.provider_headers,
            &self.model_headers,
            self.auth_header,
        ) == (
            &other.provider,
            &other.model,
            &other.provider_headers,
            &other.model_headers,
            other.auth_header,
        )
    }
}

impl RequestConfig {
    /// The request headers for `key` (`withConfiguredAuth`, then the model's headers), resolved now: provider
    /// headers, the bearer header, then model headers (names compare case-insensitively).
    pub fn headers(&self, provider: &str, key: Option<&str>) -> Result<Option<ProviderHeaders>, String> {
        self.headers_in(provider, key, None, None)
    }

    /// [`RequestConfig::headers`] whose templates read `provider_env` (the stored credential's env) first for the
    /// provider headers, and `model_env` (it under the request's own env) first for the model headers, then the
    /// environment (`configContextEnv`, `resolveConfiguredModelHeaders`: an empty value reads as unset).
    pub fn headers_in(
        &self,
        provider: &str,
        key: Option<&str>,
        provider_env: Option<&crate::env::ProviderEnv>,
        model_env: Option<&crate::env::ProviderEnv>,
    ) -> Result<Option<ProviderHeaders>, String> {
        let base = |name: &str| self.lookup.as_ref().and_then(|lookup| lookup(name));
        let over = |env: Option<&crate::env::ProviderEnv>, name: &str| {
            env.and_then(|env| env.get(name))
                .filter(|v| !v.is_empty())
                .cloned()
                .or_else(|| base(name))
        };
        let provider_lookup = |name: &str| over(provider_env, name);
        let model_lookup = |name: &str| over(model_env, name);
        self.headers_with_env(provider, key, &provider_lookup, &model_lookup)
    }

    fn headers_with(
        &self,
        provider: &str,
        key: Option<&str>,
        lookup: Lookup<'_>,
    ) -> Result<Option<ProviderHeaders>, String> {
        self.headers_with_env(provider, key, lookup, lookup)
    }

    fn headers_with_env(
        &self,
        provider: &str,
        key: Option<&str>,
        lookup: Lookup<'_>,
        model_lookup: Lookup<'_>,
    ) -> Result<Option<ProviderHeaders>, String> {
        let mut headers = resolve_headers_or_err(
            &self.provider_headers,
            &format!("provider \"{}\"", self.provider),
            lookup,
        )
        .map_err(|e| auth_failed(provider, &e))?;
        if self.auth_header {
            let key = key
                .filter(|k| !k.is_empty())
                .ok_or_else(|| auth_failed(provider, "authHeader requires a resolved API key"))?;
            headers
                .get_or_insert_default()
                .insert("Authorization".into(), Some(format!("Bearer {key}")));
        }
        let model = resolve_headers_or_err(
            &self.model_headers,
            &format!("model \"{}/{}\"", self.provider, self.model),
            model_lookup,
        )?;
        Ok(merge_headers(headers.as_ref(), model.as_ref()))
    }
}

/// `ModelsError("auth", "API key auth failed for provider <p>", { cause })`'s message.
fn auth_failed(provider: &str, cause: &str) -> String {
    format!("API key auth failed for provider {provider}: {cause}")
}

fn stored_request_auth(model: &Model) -> Option<StoredRequestAuth> {
    (model.request.as_ref()?.stored_auth.as_ref()?)()
}

/// The options a request to `model` goes out with: the stored credential's headers, under the configured headers,
/// under the caller's own (`getAuth`'s `mergeHeaders(auth.headers, configuredHeaders)`, then `prepareRequest`'s
/// `mergeHeaders(auth.headers, options.headers)`), when the model carries a [`RequestConfig`]; `None` when nothing
/// changes.
fn options_with(
    model: &Model,
    options: &SimpleStreamOptions,
    stored: Option<&StoredRequestAuth>,
) -> Result<Option<SimpleStreamOptions>, String> {
    let Some(request) = &model.request else {
        return Ok(None);
    };
    let env = stored.and_then(|s| s.env.as_ref());
    // Model header templates see the stored env under the request's own (`{ ...resolution.env, ...overrides.env }`).
    let model_env = match (env, options.base.env.as_ref()) {
        (Some(stored), Some(own)) => {
            let mut merged = stored.clone();
            merged.extend(own.iter().map(|(k, v)| (k.clone(), v.clone())));
            Some(merged)
        }
        (stored, own) => stored.or(own).cloned(),
    };
    let configured = request.headers_in(
        &model.provider,
        options.base.api_key.as_deref(),
        env,
        model_env.as_ref(),
    )?;
    let auth = merge_headers(stored.and_then(|s| s.headers.as_ref()), configured.as_ref());
    if auth.is_none() && env.is_none() {
        return Ok(None);
    }
    let mut options = options.clone();
    options.base.headers = merge_headers(auth.as_ref(), options.base.headers.as_ref());
    // `{ ...(resolution.env ?? {}), ...(options?.env ?? {}) }`.
    if let Some(env) = env {
        let mut merged = env.clone();
        merged.extend(options.base.env.take().unwrap_or_default());
        options.base.env = Some(merged);
    }
    Ok(Some(options))
}

/// `model` with the base URL its stored OAuth credential names for this request (`result.auth.baseUrl ? { ...model,
/// baseUrl } : model`); `None` when it names none.
fn model_with(model: &Model, stored: Option<&StoredRequestAuth>) -> Option<Model> {
    let base_url = stored?.base_url.clone()?;
    let mut model = model.clone();
    model.base_url = base_url;
    Some(model)
}

/// `prepareRequest`: the model a request goes to and the options it goes out with, over one resolution of the stored
/// credential (a refresh happens at most once per request).
pub fn prepare_request(
    model: &Model,
    options: &SimpleStreamOptions,
) -> Result<(Option<Model>, Option<SimpleStreamOptions>), String> {
    let stored = stored_request_auth(model);
    let routed = model_with(model, stored.as_ref());
    let prepared = options_with(model, options, stored.as_ref())?;
    Ok((routed, prepared))
}

/// Request auth for one model: the API key and the headers its requests carry.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct RequestAuth {
    pub api_key: Option<String>,
    pub headers: Option<ProviderHeaders>,
}

use config::radius::DEFAULT_RADIUS_GATEWAY;

/// models.json over the built-in catalog, with the Radius catalogs models-store.json keeps.
#[derive(Clone, Debug, Default)]
pub struct ModelRegistry {
    config: ModelConfig,
    /// models-store.json: provider id to its stored catalog (`refreshModels` restores it without the network).
    stored: Map<String, Value>,
}

impl ModelRegistry {
    pub fn new(config: ModelConfig) -> Self {
        ModelRegistry {
            config,
            stored: Map::new(),
        }
    }

    /// The registry of the models.json at `path` (`~` expands against `home`), with the models-store.json beside it;
    /// a missing file is an empty one.
    pub fn load(path: &Path, home: &Path) -> Self {
        let mut registry = Self::new(ModelConfig::load_with_home(path.to_str(), home));
        registry.stored = config::models_store::ModelsStore::beside(path).snapshot();
        registry
    }

    /// The Radius gateway behind `provider` and the name its login shows: the built-in `radius` provider, or a
    /// models.json provider with `"oauth": "radius"` and a `baseUrl` (`configureRadiusProviders`: the gateway is the
    /// base URL without its `/v1`).
    pub fn radius_gateway(&self, provider: &str) -> Option<(String, String)> {
        let config = self.provider_config(provider);
        let custom = config.filter(|c| c.get("oauth").and_then(Value::as_str) == Some("radius"));
        match custom.and_then(|c| Some((c, c.get("baseUrl")?.as_str()?))) {
            Some((config, base_url)) => {
                let trimmed = base_url
                    .strip_suffix("/v1/")
                    .or_else(|| base_url.strip_suffix("/v1"))
                    .unwrap_or(base_url);
                let name = config.get("name").and_then(Value::as_str).unwrap_or(provider);
                Some((name.to_string(), config::radius::normalize_radius_gateway_url(trimmed)))
            }
            None => (provider == "radius").then(|| ("Radius".to_string(), DEFAULT_RADIUS_GATEWAY.to_string())),
        }
    }

    /// The models a provider has before models.json applies: the built-in catalog with its stored pi.dev overlay
    /// when that is newer than the bundled data (`withRemoteCatalog`); for a Radius provider, the bundled Radius
    /// catalog (only on the default gateway, under the provider's id) with its stored catalog over it
    /// (`radiusProvider.getModels`).
    fn base_models(&self, provider: &str) -> Vec<Model> {
        let Some((_, gateway)) = self.radius_gateway(provider) else {
            let builtin: Vec<Model> = crate::models::builtin_models(provider).cloned().collect();
            let overlay = crate::model_refresh::remote_overlay(
                provider,
                self.stored.get(provider),
                crate::model_refresh::builtin_generated_at(),
            );
            return crate::model_refresh::merge_models(builtin, overlay);
        };
        let models: Vec<Model> = if gateway == DEFAULT_RADIUS_GATEWAY {
            crate::models::builtin_models("radius")
                .map(|m| Model {
                    provider: provider.to_string(),
                    ..m.clone()
                })
                .collect()
        } else {
            Vec::new()
        };
        let stored = crate::model_refresh::radius_stored(provider, self.stored.get(provider));
        crate::model_refresh::merge_models(models, stored)
    }

    /// Radius providers without a stored catalog import the one their legacy OAuth credential carries
    /// (`gatewayConfig`, from the pre-models-store implementation) into `store`, as `radiusProvider.refreshModels`
    /// does on every start. `credentials` is auth.json's content.
    pub fn import_legacy_radius(&mut self, credentials: &Value, store: &config::models_store::ModelsStore, now: i64) {
        let providers: Vec<String> = self
            .provider_ids()
            .into_iter()
            .filter(|p| self.radius_gateway(p).is_some() && !self.stored.contains_key(p))
            .collect();
        for provider in providers {
            let Some(credential) = credentials.get(&provider).filter(|c| c["type"] == "oauth") else {
                continue;
            };
            let Some(legacy) = credential
                .get("gatewayConfig")
                .and_then(|config| crate::model_refresh::radius_models_from_config(&provider, config))
                .filter(|models| !models.is_empty())
            else {
                continue;
            };
            let entry = json!({"models": legacy, "checkedAt": now});
            if store.write(&provider, entry.clone()).is_ok() {
                self.stored.insert(provider, entry);
            }
        }
    }

    pub fn config(&self) -> &ModelConfig {
        &self.config
    }

    fn provider_config(&self, provider: &str) -> Option<&Map<String, Value>> {
        self.config.provider(provider).and_then(Value::as_object)
    }

    /// Built-in providers in catalog order, then models.json's own in file order.
    pub fn provider_ids(&self) -> Vec<String> {
        let mut ids: Vec<String> = crate::models::builtin_providers().map(str::to_string).collect();
        for id in self.config.provider_ids() {
            if !ids.iter().any(|known| known == id) {
                ids.push(id.to_string());
            }
        }
        ids
    }

    /// `composeModelProvider(...).getModels()` for a provider models.json names: its error when the configuration
    /// does not compose.
    fn compose(&self, provider: &str, config: &Map<String, Value>) -> Result<Vec<Model>, String> {
        compose_over(provider, self.base_models(provider).iter(), config)
    }

    /// A provider's models: composed with models.json, else (no entry, or one that does not compose) built-in.
    pub fn models(&self, provider: &str) -> Vec<Model> {
        match self.provider_config(provider) {
            Some(config) => self
                .compose(provider, config)
                .unwrap_or_else(|_| self.base_models(provider)),
            None => self.base_models(provider),
        }
    }

    /// Every provider's models, providers in [`ModelRegistry::provider_ids`] order.
    pub fn all_models(&self) -> Vec<Model> {
        self.provider_ids().iter().flat_map(|p| self.models(p)).collect()
    }

    /// `getModel(provider, id)`.
    pub fn find(&self, provider: &str, id: &str) -> Option<Model> {
        match self.provider_config(provider) {
            Some(config) => self.models(provider).into_iter().find(|m| m.id == id).or_else(|| {
                // A provider whose catalog takes any id (claude-subscription): models.json applies to that model.
                let dynamic = crate::models::builtin_model(provider, id)?;
                compose_over(provider, std::iter::once(dynamic), config)
                    .ok()?
                    .into_iter()
                    .find(|m| m.id == id)
            }),
            None => self
                .base_models(provider)
                .into_iter()
                .find(|m| m.id == id)
                .or_else(|| crate::models::builtin_model(provider, id).cloned()),
        }
    }

    /// `resolveCliModel` against every model (`--provider`, `--model` with fuzzy ids, `provider/pattern`,
    /// `<pattern>:<thinking>`, ambiguity across providers, the custom-id fallback); `has_auth` says which providers
    /// have configured credentials. The dynamic `claude-subscription` catalog (any valid id, nothing listed) resolves
    /// its references directly.
    pub fn resolve_cli(
        &self,
        cli_provider: Option<&str>,
        cli_model: &str,
        cli_thinking: Option<&str>,
        has_auth: &dyn Fn(&str) -> bool,
    ) -> crate::model_resolver::CliModel {
        if let Some(model) = self.resolve_dynamic(cli_provider, cli_model) {
            return crate::model_resolver::CliModel {
                model: Some(model),
                ..Default::default()
            };
        }
        crate::model_resolver::resolve_cli_model(
            cli_provider,
            Some(cli_model),
            cli_thinking,
            &self.all_models(),
            has_auth,
        )
    }

    /// A `claude-subscription/<id>` reference (or `--provider claude-subscription --model <id>`).
    fn resolve_dynamic(&self, cli_provider: Option<&str>, cli_model: &str) -> Option<Model> {
        let dynamic = crate::claude_subscription::PROVIDER;
        fn prefixed<'t>(text: &'t str, dynamic: &str) -> Option<&'t str> {
            let head = text.get(..dynamic.len() + 1)?;
            head.eq_ignore_ascii_case(&format!("{dynamic}/"))
                .then(|| &text[dynamic.len() + 1..])
        }
        let id = match cli_provider {
            Some(p) if p.eq_ignore_ascii_case(dynamic) => prefixed(cli_model, dynamic).unwrap_or(cli_model),
            Some(_) => return None,
            None => prefixed(cli_model, dynamic)?,
        };
        if self.models(dynamic).iter().any(|m| m.id.eq_ignore_ascii_case(id)) {
            return None;
        }
        self.find(dynamic, id)
    }

    /// [`ModelRegistry::resolve_cli`] without credentials or a thinking flag: the model, or the error message.
    pub fn resolve(&self, cli_provider: Option<&str>, cli_model: &str) -> Result<Model, String> {
        let resolved = self.resolve_cli(cli_provider, cli_model, None, &|_| false);
        match (resolved.model, resolved.error) {
            (Some(model), _) => Ok(model),
            (None, error) => Err(error.unwrap_or_else(|| format!("Model \"{cli_model}\" not found"))),
        }
    }

    /// `getError` without availability: the models.json load error, then each provider's composition error.
    pub fn error(&self) -> Option<String> {
        let mut errors: Vec<String> = self.config.error().map(str::to_string).into_iter().collect();
        for provider in self.provider_ids() {
            let composed = match self.provider_config(&provider) {
                Some(config) => self.compose(&provider, config).map(|_| ()),
                None => Ok(()),
            };
            if let Err(error) = composed {
                errors.push(format!("Provider \"{provider}\": {error}"));
            }
        }
        (!errors.is_empty()).then(|| errors.join("\n\n"))
    }

    /// The provider's models.json `apiKey` when its configuration composes.
    fn raw_key(&self, provider: &str) -> Option<&str> {
        let config = self.provider_config(provider)?;
        self.compose(provider, config).ok()?;
        config.get("apiKey").and_then(Value::as_str)
    }

    /// `checkAuth` without stored credentials: a configured key counts when it is a command or every variable it
    /// names is set; otherwise `inherited` (the built-in provider's own check). The `claude-code` provider's own
    /// check is whether its CLI is present (`claudeCodeCliAuth().check`), whatever `inherited` says: its requests
    /// resolve with or without the CLI.
    pub fn auth_configured(&self, provider: &str, lookup: Lookup<'_>, inherited: bool) -> bool {
        match self.raw_key(provider) {
            Some(raw) if config::auth::is_command(raw) => true,
            Some(raw) => config::auth::env_names(raw)
                .iter()
                .all(|name| lookup(name).is_some_and(|v| !v.trim().is_empty())),
            None if provider == crate::claude_code::PROVIDER => {
                crate::claude_code::configured(&crate::env::EnvSource {
                    lookup,
                    exists: &|path: &Path| path.exists(),
                    home: lookup("HOME").map(std::path::PathBuf::from),
                })
            }
            None => inherited,
        }
    }

    /// The key requests of `provider` use: `override_key` (`--api-key`), else the models.json `apiKey` resolved
    /// (an error when it does not resolve; no fallback to the environment), else `inherited` (the built-in key).
    pub fn api_key(
        &self,
        provider: &str,
        override_key: Option<&str>,
        lookup: Lookup<'_>,
        inherited: Option<String>,
    ) -> Result<Option<String>, String> {
        if let Some(key) = override_key {
            return Ok(Some(key.to_string()));
        }
        match self.raw_key(provider) {
            Some(raw) => {
                config::auth::resolve_or_err(raw, &format!("API key for provider \"{provider}\""), lookup, &[])
                    .map(Some)
                    .map_err(|e| auth_failed(provider, &e))
            }
            None => Ok(inherited),
        }
    }

    /// The [`RequestConfig`] of `model` (None when models.json has nothing for its provider): the raw headers and
    /// `authHeader`, resolved with `lookup` at each request.
    pub fn request_config(&self, model: &Model, lookup: Option<SharedLookup>) -> Option<RequestConfig> {
        let config = self.provider_config(&model.provider)?;
        let strings = |value: Option<&Value>| -> Vec<(String, String)> {
            value
                .and_then(Value::as_object)
                .map(|map| {
                    map.iter()
                        .filter_map(|(k, v)| Some((k.clone(), v.as_str()?.to_string())))
                        .collect()
                })
                .unwrap_or_default()
        };
        // `rawModelHeaders`: the override's headers under the definition's (object spread).
        let mut model_headers: Vec<(String, String)> = strings(
            config
                .get("modelOverrides")
                .and_then(|o| o.get(&model.id))
                .and_then(|o| o.get("headers")),
        );
        let definition = config
            .get("models")
            .and_then(Value::as_array)
            .and_then(|models| models.iter().find(|m| m["id"].as_str() == Some(model.id.as_str())));
        for (key, value) in strings(definition.and_then(|d| d.get("headers"))) {
            match model_headers.iter_mut().find(|(k, _)| *k == key) {
                Some(entry) => entry.1 = value,
                None => model_headers.push((key, value)),
            }
        }
        Some(RequestConfig {
            provider: model.provider.clone(),
            model: model.id.clone(),
            provider_headers: strings(config.get("headers")),
            model_headers,
            auth_header: config.get("authHeader").and_then(Value::as_bool).unwrap_or(false),
            lookup,
            stored_auth: None,
        })
    }

    /// `model` ready for requests: its [`RequestConfig`] attached, resolving with `lookup` per request.
    pub fn for_requests(&self, mut model: Model, lookup: SharedLookup) -> Model {
        model.request = self.request_config(&model, Some(lookup));
        model
    }

    /// `getAuth(model, {apiKey})`: the key and headers a request to `model` carries; `None` when the provider has no
    /// key at all (not configured).
    pub fn request_auth(
        &self,
        model: &Model,
        override_key: Option<&str>,
        lookup: Lookup<'_>,
        inherited: Option<String>,
    ) -> Result<Option<RequestAuth>, String> {
        let Some(api_key) = self.api_key(&model.provider, override_key, lookup, inherited)? else {
            return Ok(None);
        };
        let headers = match self.request_config(model, None) {
            Some(request) => request.headers_with(&model.provider, Some(&api_key), lookup)?,
            None => None,
        };
        Ok(Some(RequestAuth {
            api_key: Some(api_key),
            headers,
        }))
    }
}

/// `composeModelProvider(...).getModels()` over `base` (the provider's built-in models) for a provider models.json
/// names: its error when the configuration does not compose.
fn compose_over<'a>(
    provider: &str,
    base: impl Iterator<Item = &'a Model>,
    config: &Map<String, Value>,
) -> Result<Vec<Model>, String> {
    let base: Vec<Value> = base.map(|m| serde_json::to_value(m).unwrap_or(Value::Null)).collect();

    let models = apply_models_json(provider, base, config)?;
    let overrides = config.get("modelOverrides").and_then(Value::as_object);
    models
        .into_iter()
        .map(|model| {
            let id = model["id"].as_str().unwrap_or_default().to_string();
            let model = match overrides.and_then(|o| o.get(&id)).and_then(Value::as_object) {
                Some(ov) => apply_model_override(model, ov),
                None => model,
            };
            // ponytail: the Rust model keeps integral token counts; a fractional contextWindow is refused here.
            serde_json::from_value(model).map_err(|e| format!("Provider {provider}, model {id}: {e}"))
        })
        .collect()
}

/// A present, non-null property (`??` in TypeScript).
fn get<'a>(map: &'a Map<String, Value>, key: &str) -> Option<&'a Value> {
    map.get(key).filter(|v| !v.is_null())
}

fn set(map: &mut Map<String, Value>, key: &str, value: Option<Value>) {
    match value {
        Some(value) => {
            map.insert(key.to_string(), value);
        }
        None => {
            map.remove(key);
        }
    }
}

/// `{...a, ...b}` for objects (anything else spreads to nothing).
fn spread(a: Option<&Value>, b: Option<&Value>) -> Value {
    let mut out = Map::new();
    for value in [a, b].into_iter().flatten() {
        if let Some(map) = value.as_object() {
            for (k, v) in map {
                out.insert(k.clone(), v.clone());
            }
        }
    }
    Value::Object(out)
}

/// `mergeCompat`: the override's keys over the base's; routing and chat-template objects merge one level deeper.
fn merge_compat(base: Option<&Value>, override_: Option<&Value>) -> Option<Value> {
    let Some(override_) = override_ else {
        return base.cloned();
    };
    let mut merged = spread(base, Some(override_));
    let merged_map = merged.as_object_mut()?;
    for key in [
        "openRouterRouting",
        "vercelGatewayRouting",
        "chatTemplateKwargs",
        "chatTemplateArgs",
    ] {
        let base_value = base.and_then(|b| b.get(key));
        let override_value = override_.get(key);
        if base_value.is_some_and(Value::is_object) || override_value.is_some_and(Value::is_object) {
            merged_map.insert(key.to_string(), spread(base_value, override_value));
        }
    }
    Some(merged)
}

fn merge_input_limits(base: Option<&Value>, override_: Option<&Value>) -> Option<Value> {
    let Some(override_) = override_ else {
        return base.cloned();
    };
    let mut merged = spread(base, Some(override_));
    let base_images = base.and_then(|b| b.get("images")).filter(|v| !v.is_null());
    let images = match override_.get("images").filter(|v| !v.is_null()) {
        Some(images) => {
            let mut out = spread(base_images, Some(images));
            let base_resize = base_images.and_then(|i| i.get("resize")).filter(|v| !v.is_null());
            let resize = match images.get("resize").filter(|v| !v.is_null()) {
                Some(resize) => Some(spread(base_resize, Some(resize))),
                None => base_resize.cloned(),
            };
            if let Some(map) = out.as_object_mut() {
                set(map, "resize", resize);
            }
            Some(out)
        }
        None => base_images.cloned(),
    };
    if let Some(map) = merged.as_object_mut() {
        set(map, "images", images);
    }
    Some(merged)
}

fn apply_model_override(model: Value, ov: &Map<String, Value>) -> Value {
    let Value::Object(mut model) = model else {
        return model;
    };
    let keep = |model: &Map<String, Value>, key: &str| get(ov, key).or_else(|| get(model, key)).cloned();
    let name = keep(&model, "name");
    set(&mut model, "name", name);
    let reasoning = keep(&model, "reasoning");
    set(&mut model, "reasoning", reasoning);
    if let Some(map) = get(ov, "thinkingLevelMap") {
        let merged = spread(get(&model, "thinkingLevelMap"), Some(map));
        model.insert("thinkingLevelMap".into(), merged);
    }
    let input = keep(&model, "input");
    set(&mut model, "input", input);
    let limits = merge_input_limits(get(&model, "inputLimits"), get(ov, "inputLimits"));
    set(&mut model, "inputLimits", limits);
    if let Some(cost) = get(ov, "cost").and_then(Value::as_object) {
        let base = get(&model, "cost")
            .and_then(Value::as_object)
            .cloned()
            .unwrap_or_default();
        let mut merged = Map::new();
        for key in ["input", "output", "cacheRead", "cacheWrite", "tiers"] {
            if let Some(value) = get(cost, key).or_else(|| get(&base, key)) {
                merged.insert(key.into(), value.clone());
            }
        }
        model.insert("cost".into(), Value::Object(merged));
    }
    if let Some(cache) = get(ov, "promptCache") {
        let merged = spread(get(&model, "promptCache"), Some(cache));
        model.insert("promptCache".into(), merged);
    }
    for key in ["contextWindow", "maxTokens"] {
        let value = keep(&model, key);
        set(&mut model, key, value);
    }
    if let Some(params) = get(ov, "samplingParams") {
        let merged = spread(get(&model, "samplingParams"), Some(params));
        model.insert("samplingParams".into(), merged);
    }
    let compat = merge_compat(get(&model, "compat"), get(ov, "compat"));
    set(&mut model, "compat", compat);
    Value::Object(model)
}

/// `modelFromJson`: a models.json model definition, with `defaults` (a model of the same provider) filling `api`
/// and `baseUrl` the definition and provider leave out.
fn model_from_json(
    provider: &str,
    definition: &Map<String, Value>,
    config: &Map<String, Value>,
    defaults: Option<&Map<String, Value>>,
) -> Result<Value, String> {
    let id = definition.get("id").and_then(Value::as_str).unwrap_or_default();
    let pick = |key: &str| {
        get(definition, key)
            .or_else(|| get(config, key))
            .or_else(|| defaults.and_then(|d| get(d, key)))
            .cloned()
    };
    let api = pick("api").ok_or_else(|| {
        format!("Provider {provider}, model {id}: no \"api\" specified. Set at provider or model level.")
    })?;
    let base_url = pick("baseUrl")
        .ok_or_else(|| format!("Provider {provider}: \"baseUrl\" is required when defining custom models."))?;
    for (key, name) in [("contextWindow", "contextWindow"), ("maxTokens", "maxTokens")] {
        if get(definition, key).and_then(Value::as_f64).is_some_and(|n| n <= 0.0) {
            return Err(format!("Provider {provider}, model {id}: invalid {name}"));
        }
    }
    let mut model = Map::new();
    let mut put = |key: &str, value: Option<Value>| {
        if let Some(value) = value {
            model.insert(key.to_string(), value);
        }
    };
    put("id", Some(json!(id)));
    put(
        "name",
        Some(get(definition, "name").cloned().unwrap_or_else(|| json!(id))),
    );
    put("api", Some(api));
    put("provider", Some(json!(provider)));
    put("baseUrl", Some(base_url));
    put(
        "reasoning",
        Some(get(definition, "reasoning").cloned().unwrap_or(json!(false))),
    );
    put("thinkingLevelMap", get(definition, "thinkingLevelMap").cloned());
    put(
        "input",
        Some(get(definition, "input").cloned().unwrap_or(json!(["text"]))),
    );
    put("inputLimits", get(definition, "inputLimits").cloned());
    put(
        "cost",
        Some(
            get(definition, "cost")
                .cloned()
                .unwrap_or(json!({"input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0})),
        ),
    );
    put("promptCache", get(definition, "promptCache").cloned());
    put(
        "contextWindow",
        Some(get(definition, "contextWindow").cloned().unwrap_or(json!(128000))),
    );
    put(
        "maxTokens",
        Some(get(definition, "maxTokens").cloned().unwrap_or(json!(16384))),
    );
    put("samplingParams", get(definition, "samplingParams").cloned());
    put("compat", merge_compat(get(config, "compat"), get(definition, "compat")));
    Ok(Value::Object(model))
}

/// `findModelDefaults`: the model with this id, else one on `api`, else an `openai-completions` one, else the first.
fn find_defaults<'a>(models: &'a [Value], id: &str, api: Option<&str>) -> Option<&'a Map<String, Value>> {
    let find = |key: &str, value: &str| models.iter().find(|m| m[key].as_str() == Some(value));
    find("id", id)
        .or_else(|| api.and_then(|api| find("api", api)))
        .or_else(|| find("api", "openai-completions"))
        .or_else(|| models.first())
        .and_then(Value::as_object)
}

/// `applyModelsJson`: base URL and compat applied to the provider's models, then models.json's models upserted.
fn apply_models_json(provider: &str, base: Vec<Value>, config: &Map<String, Value>) -> Result<Vec<Value>, String> {
    let oauth = get(config, "oauth");
    let base_url = get(config, "baseUrl");
    if oauth.is_some() && base_url.is_none() {
        return Err(format!(
            "Provider {provider}: \"baseUrl\" is required when \"oauth\" is set."
        ));
    }
    let definitions: &[Value] = get(config, "models")
        .and_then(Value::as_array)
        .map_or(&[], Vec::as_slice);
    let has_overrides = get(config, "modelOverrides")
        .and_then(Value::as_object)
        .is_some_and(|o| !o.is_empty());
    if definitions.is_empty()
        && base_url.is_none()
        && get(config, "headers").is_none()
        && get(config, "compat").is_none()
        && !has_overrides
        && get(config, "apiKey").is_none()
        && oauth.is_none()
        && !config.contains_key("authHeader")
    {
        return Err(format!(
            "Provider {provider}: must specify \"baseUrl\", \"headers\", \"compat\", \"modelOverrides\", or \"models\"."
        ));
    }
    let radius = oauth.and_then(Value::as_str) == Some("radius");
    let mut models: Vec<Value> = base
        .into_iter()
        .map(|model| {
            let Value::Object(mut model) = model else {
                return model;
            };
            if !radius && let Some(url) = base_url {
                model.insert("baseUrl".into(), url.clone());
            }
            let compat = merge_compat(get(&model, "compat"), get(config, "compat"));
            set(&mut model, "compat", compat);
            Value::Object(model)
        })
        .collect();
    for definition in definitions {
        let Some(definition) = definition.as_object() else {
            continue;
        };
        let id = definition.get("id").and_then(Value::as_str).unwrap_or_default();
        let api = get(definition, "api")
            .or_else(|| get(config, "api"))
            .and_then(Value::as_str);
        let defaults = find_defaults(&models, id, api).cloned();
        let model = model_from_json(provider, definition, config, defaults.as_ref())?;
        match models.iter().position(|m| m["id"].as_str() == Some(id)) {
            Some(index) => models[index] = model,
            None => models.push(model),
        }
    }
    Ok(models)
}

/// `resolveHeadersOrThrow` as request headers.
fn resolve_headers_or_err(
    headers: &[(String, String)],
    description: &str,
    lookup: Lookup<'_>,
) -> Result<Option<ProviderHeaders>, String> {
    let resolved = config::auth::resolve_headers_or_err(headers, description, lookup, &[])?;
    Ok(resolved.map(|pairs| pairs.into_iter().map(|(k, v)| (k, Some(v))).collect()))
}