rust_llm 2.1.1

One API for chat, tools, agents, structured output, embeddings, images, audio, batches, MCP, and judgments across LLM providers.
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
//! Port of `lib/ruby_llm/provider.rb` and `lib/ruby_llm/providers/*.rb`: where each service
//! lives, how it authenticates, and which wire protocol it speaks for a model.

use crate::config::Config;
use crate::error::{Error, Result};
use crate::model::Model;

/// The providers this port supports, by RubyLLM slug.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Provider {
    OpenAI,
    Anthropic,
    Gemini,
    DeepSeek,
    Mistral,
    OpenRouter,
    XAI,
    Perplexity,
    Ollama,
    OllamaCloud,
    GPUStack,
    Hetzner,
    /// TypeSafe's System One API (Jev judgment models).
    TypeSafe,
}

/// The wire formats under `RubyLLM::Protocols`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ProtocolName {
    ChatCompletions,
    Responses,
    Anthropic,
    Gemini,
    /// `Protocols::Interactions`: Gemini's Interactions API, opted into with `protocol: :interactions`.
    Interactions,
    /// `Providers::Mistral::Conversations`: Mistral's Conversations API (`protocol: :conversations`).
    Conversations,
    /// `Protocols::Perplexity::Router`: Perplexity Router (`protocol: :router_chat_completions`).
    RouterChatCompletions,
}

impl ProtocolName {
    pub fn parse(name: &str) -> Result<ProtocolName> {
        match name {
            "chat_completions" => Ok(ProtocolName::ChatCompletions),
            "responses" => Ok(ProtocolName::Responses),
            "anthropic" => Ok(ProtocolName::Anthropic),
            "gemini" => Ok(ProtocolName::Gemini),
            "interactions" => Ok(ProtocolName::Interactions),
            "conversations" => Ok(ProtocolName::Conversations),
            "router_chat_completions" => Ok(ProtocolName::RouterChatCompletions),
            other => Err(Error::Argument(format!("unknown protocol: {other}"))),
        }
    }

    /// The name a provider registers the protocol under (`protocol :chat_completions, ...`).
    pub fn name(&self) -> &'static str {
        match self {
            ProtocolName::ChatCompletions => "chat_completions",
            ProtocolName::Responses => "responses",
            ProtocolName::Anthropic => "anthropic",
            ProtocolName::Gemini => "gemini",
            ProtocolName::Interactions => "interactions",
            ProtocolName::Conversations => "conversations",
            ProtocolName::RouterChatCompletions => "router_chat_completions",
        }
    }
}

pub const ALL: &[Provider] = &[
    Provider::OpenAI,
    Provider::Anthropic,
    Provider::Gemini,
    Provider::DeepSeek,
    Provider::Mistral,
    Provider::OpenRouter,
    Provider::XAI,
    Provider::Perplexity,
    Provider::Ollama,
    Provider::OllamaCloud,
    Provider::GPUStack,
    Provider::Hetzner,
    Provider::TypeSafe,
];

/// `Providers::OpenAI::Capabilities::SEARCH_MODELS`: these only work over Chat Completions.
const OPENAI_SEARCH_MODELS: &[&str] = &[
    "gpt-4o-mini-search-preview",
    "gpt-4o-mini-search-preview-2025-03-11",
    "gpt-4o-search-preview",
    "gpt-4o-search-preview-2025-03-11",
    "gpt-5-search-api",
    "gpt-5-search-api-2025-10-14",
];

pub fn display_name(slug: &str) -> &str {
    Provider::resolve(slug).map(|p| p.display()).unwrap_or(slug)
}

impl Provider {
    pub fn resolve(slug: &str) -> Option<Provider> {
        ALL.iter().copied().find(|p| p.slug() == slug)
    }

    /// `Provider.resolve!`.
    pub fn resolve_or_err(slug: &str) -> Result<Provider> {
        Provider::resolve(slug).ok_or_else(|| {
            let known: Vec<&str> = ALL.iter().map(|p| p.slug()).collect();
            Error::Api(
                format!(
                    "Unknown provider: {slug:?}. Available providers: {}",
                    known.join(", ")
                ),
                None,
            )
        })
    }

    pub fn slug(&self) -> &'static str {
        match self {
            Provider::OpenAI => "openai",
            Provider::Anthropic => "anthropic",
            Provider::Gemini => "gemini",
            Provider::DeepSeek => "deepseek",
            Provider::Mistral => "mistral",
            Provider::OpenRouter => "openrouter",
            Provider::XAI => "xai",
            Provider::Perplexity => "perplexity",
            Provider::Ollama => "ollama",
            Provider::OllamaCloud => "ollama_cloud",
            Provider::GPUStack => "gpustack",
            Provider::Hetzner => "hetzner",
            Provider::TypeSafe => "typesafe",
        }
    }

    pub fn display(&self) -> &'static str {
        match self {
            Provider::OpenAI => "OpenAI",
            Provider::Anthropic => "Anthropic",
            Provider::Gemini => "Gemini",
            Provider::DeepSeek => "DeepSeek",
            Provider::Mistral => "Mistral",
            Provider::OpenRouter => "OpenRouter",
            Provider::XAI => "XAI",
            Provider::Perplexity => "Perplexity",
            Provider::Ollama => "Ollama",
            Provider::OllamaCloud => "OllamaCloud",
            Provider::GPUStack => "GPUStack",
            Provider::Hetzner => "Hetzner",
            Provider::TypeSafe => "TypeSafe",
        }
    }

    /// `Provider#original_image_detail?`: OpenAI sends `detail: "original"` for
    /// `resolution: :original`; the rest map it to high detail.
    pub fn is_original_image_detail(&self) -> bool {
        *self == Provider::OpenAI
    }

    /// `Provider#account_identity`: a SHA-256 hex digest naming the account that owns this
    /// provider's uploads (the endpoint and API key, plus OpenAI's organization and project), or
    /// `None` for providers whose uploads are kept within the process that made them.
    pub fn account_identity(&self, config: &Config) -> Option<String> {
        let key = |option: &str| config.get(option).unwrap_or_default().to_string();
        let base = self.api_base(config).unwrap_or_default();
        let parts = match self {
            Provider::OpenAI => vec![
                base,
                key("openai_api_key"),
                key("openai_organization_id"),
                key("openai_project_id"),
            ],
            Provider::Anthropic => vec![base, key("anthropic_api_key")],
            Provider::Gemini => vec![base, key("gemini_api_key")],
            Provider::OpenRouter => vec![base, key("openrouter_api_key")],
            Provider::XAI => vec![base, key("xai_api_key")],
            Provider::DeepSeek => vec![base, key("deepseek_api_key")],
            _ => return None,
        };
        // `account_digest(*parts)`: `Digest::SHA256.hexdigest(parts.join("\0"))`.
        use sha2::{Digest, Sha256};
        let digest = Sha256::digest(parts.join("\0").as_bytes());
        Some(digest.iter().map(|b| format!("{b:02x}")).collect())
    }

    /// `Provider.cache_write_input_multiplier`: the multiple of the input price a cache write
    /// with lifetime `ttl` costs, or `None` where the registry's cache-write price applies.
    /// models.dev prices Anthropic's five-minute write; a one-hour write is twice the input price.
    pub fn cache_write_input_multiplier(&self, _model_id: &str, ttl: &str) -> Option<f64> {
        match (self, ttl) {
            (Provider::Anthropic, "1h") => Some(2.0),
            _ => None,
        }
    }

    /// `Provider.local?`: local providers skip the registry (models are assumed to exist).
    pub fn is_local(&self) -> bool {
        matches!(self, Provider::Ollama | Provider::GPUStack)
    }

    /// `Provider.assume_models_exist?`.
    pub fn assume_models_exist(&self) -> bool {
        self.is_local() || matches!(self, Provider::OllamaCloud | Provider::Hetzner)
    }

    /// `Provider.configuration_requirements`.
    pub fn configuration_requirements(&self) -> &'static [&'static str] {
        match self {
            Provider::OpenAI => &["openai_api_key"],
            Provider::Anthropic => &["anthropic_api_key"],
            Provider::Gemini => &["gemini_api_key"],
            Provider::DeepSeek => &["deepseek_api_key"],
            Provider::Mistral => &["mistral_api_key"],
            Provider::OpenRouter => &["openrouter_api_key"],
            Provider::XAI => &["xai_api_key"],
            Provider::Perplexity => &["perplexity_api_key"],
            Provider::Ollama => &["ollama_api_base"],
            Provider::OllamaCloud => &["ollama_cloud_api_key"],
            Provider::GPUStack => &["gpustack_api_base"],
            Provider::Hetzner => &["hetzner_api_key"],
            Provider::TypeSafe => &["typesafe_api_key"],
        }
    }

    pub fn is_configured(&self, config: &Config) -> bool {
        self.configuration_requirements()
            .iter()
            .all(|r| config.get(r).is_some())
    }

    /// `Provider#ensure_configured!`.
    pub fn ensure_configured(&self, config: &Config) -> Result<()> {
        if self.is_configured(config) {
            return Ok(());
        }
        let lines: Vec<String> = self
            .configuration_requirements()
            .iter()
            .filter(|r| config.get(r).is_none())
            .map(|r| {
                format!(
                    "    config.set(\"{r}\", std::env::var(\"{}\")?);",
                    r.to_uppercase()
                )
            })
            .collect();
        Err(Error::Configuration(format!(
            "{} provider is not configured. Add this to your initialization:\n\nrust_llm::configure(|config| {{\n{}\n}});",
            self.display(),
            lines.join("\n")
        )))
    }

    pub fn api_base(&self, config: &Config) -> Result<String> {
        let (key, default) = match self {
            Provider::OpenAI => ("openai_api_base", Some("https://api.openai.com/v1")),
            Provider::Anthropic => ("anthropic_api_base", Some("https://api.anthropic.com")),
            Provider::Gemini => (
                "gemini_api_base",
                Some("https://generativelanguage.googleapis.com/v1beta"),
            ),
            Provider::DeepSeek => ("deepseek_api_base", Some("https://api.deepseek.com")),
            Provider::Mistral => ("mistral_api_base", Some("https://api.mistral.ai/v1")),
            Provider::OpenRouter => ("openrouter_api_base", Some("https://openrouter.ai/api/v1")),
            Provider::XAI => ("xai_api_base", Some("https://api.x.ai/v1")),
            Provider::Perplexity => ("perplexity_api_base", Some("https://api.perplexity.ai")),
            Provider::Ollama => ("ollama_api_base", None),
            Provider::OllamaCloud => ("ollama_cloud_api_base", Some("https://ollama.com/v1")),
            Provider::GPUStack => ("gpustack_api_base", None),
            Provider::Hetzner => (
                "hetzner_api_base",
                Some("https://inference.hetzner.com/api/v1"),
            ),
            Provider::TypeSafe => ("typesafe_api_base", Some("https://api.typesafe.ai")),
        };
        config
            .get(key)
            .or(default)
            .map(str::to_string)
            .ok_or_else(|| Error::Configuration(format!("{key} is not set")))
    }

    /// `Providers::Perplexity#agent_url`: the Agent endpoint under the configured base, which may
    /// be a gateway path with or without its own `/v1`.
    pub fn agent_url(&self, config: &Config) -> Result<String> {
        let base = self.api_base(config)?;
        let base = base.strip_suffix('/').unwrap_or(&base);
        Ok(format!(
            "{}/v1/agent",
            base.strip_suffix("/v1").unwrap_or(base)
        ))
    }

    /// `Provider#headers`.
    pub fn headers(&self, config: &Config) -> Vec<(String, String)> {
        let bearer = |key: &str| {
            config
                .get(key)
                .map(|v| vec![("Authorization".to_string(), format!("Bearer {v}"))])
                .unwrap_or_default()
        };
        match self {
            Provider::OpenAI => {
                let mut h = bearer("openai_api_key");
                if let Some(org) = config.get("openai_organization_id") {
                    h.push(("OpenAI-Organization".into(), org.into()));
                }
                if let Some(project) = config.get("openai_project_id") {
                    h.push(("OpenAI-Project".into(), project.into()));
                }
                h
            }
            Provider::Anthropic => vec![
                (
                    "x-api-key".into(),
                    config.get("anthropic_api_key").unwrap_or_default().into(),
                ),
                ("anthropic-version".into(), "2023-06-01".into()),
            ],
            Provider::Gemini => {
                vec![(
                    "x-goog-api-key".into(),
                    config.get("gemini_api_key").unwrap_or_default().into(),
                )]
            }
            Provider::DeepSeek => bearer("deepseek_api_key"),
            Provider::Mistral => bearer("mistral_api_key"),
            Provider::OpenRouter => {
                let mut h = bearer("openrouter_api_key");
                h.push((
                    "HTTP-Referer".into(),
                    config
                        .get("openrouter_app_url")
                        .unwrap_or("https://github.com/cole-robertson/rust_llm")
                        .into(),
                ));
                h.push((
                    "X-OpenRouter-Title".into(),
                    config
                        .get("openrouter_app_name")
                        .unwrap_or("RustLLM")
                        .into(),
                ));
                h
            }
            Provider::XAI => bearer("xai_api_key"),
            Provider::Perplexity => bearer("perplexity_api_key"),
            Provider::Ollama => bearer("ollama_api_key"),
            Provider::OllamaCloud => bearer("ollama_cloud_api_key"),
            Provider::GPUStack => bearer("gpustack_api_key"),
            Provider::Hetzner => bearer("hetzner_api_key"),
            Provider::TypeSafe => bearer("typesafe_api_key"),
        }
    }

    /// The first `protocol` a provider registers is its default.
    pub fn default_protocol(&self) -> ProtocolName {
        match self {
            Provider::OpenAI | Provider::XAI => ProtocolName::Responses,
            Provider::Anthropic => ProtocolName::Anthropic,
            Provider::Gemini => ProtocolName::Gemini,
            Provider::Perplexity => ProtocolName::Responses,
            _ => ProtocolName::ChatCompletions,
        }
    }

    pub fn supports_protocol(&self, protocol: ProtocolName) -> bool {
        match self {
            Provider::OpenAI
            | Provider::XAI
            | Provider::DeepSeek
            | Provider::OpenRouter
            | Provider::GPUStack => {
                matches!(
                    protocol,
                    ProtocolName::ChatCompletions | ProtocolName::Responses
                )
            }
            Provider::Anthropic => protocol == ProtocolName::Anthropic,
            Provider::Gemini => {
                matches!(protocol, ProtocolName::Gemini | ProtocolName::Interactions)
            }
            Provider::Mistral => matches!(
                protocol,
                ProtocolName::ChatCompletions | ProtocolName::Conversations
            ),
            Provider::Perplexity => {
                matches!(
                    protocol,
                    ProtocolName::ChatCompletions
                        | ProtocolName::Responses
                        | ProtocolName::RouterChatCompletions
                )
            }
            // System One answers judgments only (`rust_llm::judge`); it has no chat protocol.
            Provider::TypeSafe => false,
            _ => protocol == ProtocolName::ChatCompletions,
        }
    }

    /// `Provider#protocol_for` plus `resolve_protocol`: an explicit protocol wins, then the
    /// `<slug>_protocol` config option, then the provider's per-model rule.
    pub fn resolve_protocol(
        &self,
        explicit: Option<ProtocolName>,
        model: &Model,
        config: &Config,
    ) -> Result<ProtocolName> {
        let configured = match config.get(&format!("{}_protocol", self.slug())) {
            Some(name) => Some(ProtocolName::parse(name).map_err(|_| self.not_a_protocol(name))?),
            None => None,
        };
        let protocol = match explicit.or(configured) {
            Some(p) => p,
            None => match self {
                Provider::OpenAI
                    if OPENAI_SEARCH_MODELS.contains(&model.id.as_str())
                        || model.id.contains("audio")
                        || model.id.contains("realtime") =>
                {
                    ProtocolName::ChatCompletions
                }
                _ => self.default_protocol(),
            },
        };
        if *self == Provider::TypeSafe {
            return Err(Error::Api(
                format!("{} doesn't support chat", self.display()),
                None,
            ));
        }
        if !self.supports_protocol(protocol) {
            return Err(self.not_a_protocol(protocol.name()));
        }
        Ok(protocol)
    }

    /// `Provider.protocols.keys`: every protocol the Ruby provider registers, in order.
    pub fn protocol_names(&self) -> &'static [&'static str] {
        match self {
            Provider::OpenAI => &["responses", "chat_completions", "embeddings", "files"],
            Provider::Anthropic => &["anthropic", "files"],
            Provider::Gemini => &["gemini", "interactions", "live_transcription", "files"],
            Provider::DeepSeek => &["chat_completions", "responses", "files"],
            Provider::Mistral => &["chat_completions", "conversations", "files"],
            Provider::OpenRouter => &["chat_completions", "responses", "files"],
            Provider::XAI => &["responses", "chat_completions", "files"],
            Provider::Perplexity => &[
                "chat_completions",
                "router_chat_completions",
                "files",
                "agent_responses",
            ],
            Provider::GPUStack => &["chat_completions", "responses"],
            Provider::Ollama | Provider::OllamaCloud | Provider::Hetzner => &["chat_completions"],
            Provider::TypeSafe => &["system_one"],
        }
    }

    /// `Provider#fetch_protocol`'s error for a protocol the provider does not register.
    fn not_a_protocol(&self, name: &str) -> Error {
        Error::Api(
            format!(
                "{name} is not a protocol of {}. Available: {}",
                self.display(),
                self.protocol_names().join(", ")
            ),
            None,
        )
    }

    /// `Provider#configuration_options`: every option the provider reads.
    pub fn configuration_options(&self) -> &'static [&'static str] {
        match self {
            Provider::OpenAI => &[
                "openai_api_key",
                "openai_api_base",
                "openai_organization_id",
                "openai_project_id",
                "openai_use_system_role",
            ],
            Provider::Anthropic => &["anthropic_api_key", "anthropic_api_base"],
            Provider::Gemini => &["gemini_api_key", "gemini_api_base"],
            Provider::DeepSeek => &["deepseek_api_key", "deepseek_api_base"],
            Provider::Mistral => &["mistral_api_key", "mistral_api_base"],
            Provider::OpenRouter => &[
                "openrouter_api_key",
                "openrouter_api_base",
                "openrouter_app_url",
                "openrouter_app_name",
            ],
            Provider::XAI => &["xai_api_key", "xai_api_base"],
            Provider::Perplexity => &["perplexity_api_key", "perplexity_api_base"],
            Provider::Ollama => &["ollama_api_base", "ollama_api_key"],
            Provider::OllamaCloud => &["ollama_cloud_api_key", "ollama_cloud_api_base"],
            Provider::GPUStack => &["gpustack_api_base", "gpustack_api_key"],
            Provider::Hetzner => &["hetzner_api_key", "hetzner_api_base"],
            Provider::TypeSafe => &["typesafe_api_key", "typesafe_api_base"],
        }
    }

    /// `Provider.remote?`.
    pub fn is_remote(&self) -> bool {
        !self.is_local()
    }

    /// `Provider#retry_delay`: seconds a rate-limited response asked us to wait, read from
    /// provider-specific headers. Only OpenAI overrides the base `nil`: it reports each limit's
    /// reset as a duration like `"6m0s"`, `"7.66s"`, or `"76ms"` (`providers/openai.rb`), and the
    /// longer of the two waits wins.
    pub fn retry_delay(&self, headers: &[(String, String)]) -> Option<f64> {
        if *self != Provider::OpenAI {
            return None;
        }
        ["x-ratelimit-reset-requests", "x-ratelimit-reset-tokens"]
            .iter()
            .filter_map(|name| headers.iter().find(|(k, _)| k.eq_ignore_ascii_case(name)))
            .filter_map(|(_, v)| parse_reset_duration(v))
            .reduce(f64::max)
    }

    /// `Provider#parse_error`, with the overrides: Perplexity's HTML error pages and
    /// OpenRouter's `error.metadata.raw` upstream message. `None` for an empty body.
    pub fn parse_error(&self, body: &str) -> Option<String> {
        match self {
            Provider::Perplexity => self
                .strip_html_error(body)
                .or_else(|| crate::error::parse_error_message(body)),
            Provider::OpenRouter => openrouter_parse_error(body),
            _ => crate::error::parse_error_message(body),
        }
    }

    /// `Perplexity#router_url(operation)`: the Router lives under `/router/v1` of the API base,
    /// which may already end in it.
    pub(crate) fn router_url(&self, config: &Config, operation: &str) -> Result<String> {
        let base = self.api_base(config)?;
        let base = base.strip_suffix('/').unwrap_or(&base);
        let base = base.strip_suffix("/router/v1").unwrap_or(base);
        Ok(format!("{base}/router/v1/{operation}"))
    }

    /// `Perplexity#parse_error`: the `<title>` of the HTML page Perplexity returns for auth
    /// failures, minus the leading status code (`/<title>(.+?)<\/title>/`, `sub(/^\d+\s+/, '')`).
    pub(crate) fn strip_html_error(&self, body: &str) -> Option<String> {
        if *self != Provider::Perplexity || !body.contains("<html>") || !body.contains("<title>") {
            return None;
        }
        let start = body.find("<title>")? + 7;
        let end = body[start..].find("</title>")? + start;
        let title = &body[start..end];
        if title.is_empty() || title.contains('\n') {
            return None;
        }
        let digits = title.len() - title.trim_start_matches(|c: char| c.is_ascii_digit()).len();
        let rest = &title[digits..];
        let trimmed = rest.trim_start();
        Some(
            if digits > 0 && trimmed.len() < rest.len() {
                trimmed
            } else {
                title
            }
            .to_string(),
        )
    }
}

/// `Providers::OpenAI#parse_reset_duration`: `"1h2m3s"` style durations; `None` for anything
/// that is not entirely made of `<number><unit>` parts.
fn parse_reset_duration(value: &str) -> Option<f64> {
    static PART: std::sync::LazyLock<regex::Regex> =
        std::sync::LazyLock::new(|| regex::Regex::new(r"(\d+(?:\.\d+)?)(ms|h|m|s)").unwrap()); // constant pattern
    let parts: Vec<(&str, &str)> = PART
        .captures_iter(value)
        .map(|c| {
            let (_, [amount, unit]) = c.extract();
            (amount, unit)
        })
        .collect();
    if parts.is_empty()
        || parts
            .iter()
            .map(|(a, u)| format!("{a}{u}"))
            .collect::<String>()
            != value
    {
        return None;
    }
    let seconds = |unit: &str| match unit {
        "h" => 3600.0,
        "m" => 60.0,
        "s" => 1.0,
        _ => 0.001,
    };
    Some(
        parts
            .iter()
            .map(|(a, u)| a.parse::<f64>().unwrap_or(0.0) * seconds(u))
            .sum(),
    )
}

/// `Providers::OpenRouter#parse_error`: the shared body shapes, plus the upstream provider's own
/// message from `error.metadata.raw` appended as `"<message> - <raw message>"`.
fn openrouter_parse_error(body: &str) -> Option<String> {
    use serde_json::Value;
    if body.is_empty() {
        return None;
    }
    let try_parse =
        |s: &str| serde_json::from_str::<Value>(s).unwrap_or_else(|_| Value::String(s.to_string()));
    // `error_message`: a hash's `message`, a list's messages joined, a scalar as text.
    fn error_message(value: Option<&Value>) -> Option<String> {
        match value? {
            Value::Object(o) => o.get("message").and_then(Value::as_str).map(str::to_string),
            Value::Array(parts) => {
                let messages: Vec<String> = parts
                    .iter()
                    .filter_map(|p| error_message(Some(p)))
                    .collect();
                (!messages.is_empty()).then(|| messages.join(". "))
            }
            Value::Null => None,
            Value::String(s) => Some(s.clone()),
            other => Some(other.to_string()),
        }
    }
    let part_message = |part: &Value| -> Option<String> {
        let Value::Object(part) = part else {
            return error_message(Some(part));
        };
        let error = part.get("error");
        let message = error_message(error);
        let Some(Value::Object(metadata)) = error.and_then(|e| e.get("metadata")) else {
            return message;
        };
        let raw = match metadata.get("raw") {
            Some(Value::String(s)) => try_parse(s),
            Some(other) => other.clone(),
            None => Value::Null,
        };
        let Value::Object(raw) = raw else {
            return message;
        };
        match error_message(raw.get("error")) {
            Some(raw_message) => Some(
                message
                    .into_iter()
                    .chain([raw_message])
                    .collect::<Vec<_>>()
                    .join(" - "),
            ),
            None => message,
        }
    };
    match try_parse(body) {
        Value::Object(o) => part_message(&Value::Object(o)),
        Value::Array(parts) => {
            let messages: Vec<String> = parts
                .iter()
                .filter_map(part_message)
                .filter(|m| !m.is_empty())
                .collect();
            (!messages.is_empty()).then(|| messages.join(". "))
        }
        Value::String(s) => Some(s),
        other => Some(other.to_string()),
    }
}

/// `Provider.local_providers`.
pub fn local_providers() -> Vec<Provider> {
    ALL.iter().copied().filter(Provider::is_local).collect()
}

/// `Provider.remote_providers`.
pub fn remote_providers() -> Vec<Provider> {
    ALL.iter().copied().filter(Provider::is_remote).collect()
}

/// `Provider.configured_providers(config)`: the providers whose requirements `config` meets.
pub fn configured_providers(config: &Config) -> Vec<Provider> {
    ALL.iter()
        .copied()
        .filter(|p| p.is_configured(config))
        .collect()
}

/// `Provider.configured_remote_providers(config)`.
pub fn configured_remote_providers(config: &Config) -> Vec<Provider> {
    ALL.iter()
        .copied()
        .filter(|p| p.is_remote() && p.is_configured(config))
        .collect()
}