opencrabs 0.3.38

The autonomous, self-improving AI agent. Single Rust binary. Every channel. Install with: cargo install opencrabs
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
//! Shared provider + model selection state and logic.
//!
//! Used by both the `/models` dialog and the `/onboard` wizard to avoid
//! duplicate code that falls out of sync.

use crate::config::ProviderConfig;

/// Sentinel value stored in api_key_input when a key was loaded from config.
/// The actual key is never held in memory — this just signals "key exists".
pub const EXISTING_KEY_SENTINEL: &str = "__EXISTING_KEY__";

/// Provider definitions (index → info).
/// Last entry is always "Custom OpenAI-Compatible".
pub use crate::tui::onboarding::{PROVIDERS, ProviderInfo};

/// Index of the "Custom OpenAI-Compatible" sentinel (always last in PROVIDERS).
pub const CUSTOM_PROVIDER_IDX: usize = PROVIDERS.len() - 1;

/// First index used for existing custom provider instances (stored in config).
pub const CUSTOM_INSTANCES_START: usize = PROVIDERS.len();

/// Shared state for provider + model selection.
/// Both `/models` dialog and `/onboard` wizard embed this struct.
#[derive(Default)]
pub struct ProviderSelectorState {
    /// Currently selected provider index (0..CUSTOM_PROVIDER_IDX = static,
    /// CUSTOM_PROVIDER_IDX = new custom, CUSTOM_INSTANCES_START+ = existing customs)
    pub selected_provider: usize,
    /// Cached list of existing custom provider names
    pub custom_names: Vec<String>,
    /// Whether a key exists in config (boolean flag only — never load actual key into UI)
    pub has_existing_key: bool,
    /// User-typed API key, or EXISTING_KEY_SENTINEL when loaded from config
    pub api_key_input: String,
    /// Cursor position in api_key_input
    pub api_key_cursor: usize,
    /// Models fetched live from provider API
    pub models: Vec<String>,
    /// Models loaded from config.toml (fallback when API fetch not available)
    pub config_models: Vec<String>,
    /// Currently selected model index in filtered list
    pub selected_model: usize,
    /// Live search filter for models (case-insensitive substring match)
    pub model_filter: String,
    /// Whether an async model fetch is in progress
    pub models_fetching: bool,
    /// z.ai GLM endpoint type: 0=API, 1=Coding
    pub zhipu_endpoint_type: usize,
    /// Base URL for custom providers
    pub base_url: String,
    /// Model name for custom providers (free-text)
    pub custom_model: String,
    /// Identifier name for custom provider (e.g. "nvidia", "ollama")
    pub custom_name: String,
    /// Original name of the custom provider entry being edited, captured
    /// when the dialog opened. `Some(name)` = editing an existing entry;
    /// `None` = adding a new one. Save writes back to `editing_custom_key`
    /// (renaming the TOML table key if `custom_name` differs) instead of
    /// naively inserting at `custom_name` — prevents duplicate entries and
    /// api_key loss on rename.
    pub editing_custom_key: Option<String>,
    /// Context window size for custom providers (digits only)
    pub context_window: String,
    /// Which field is currently focused (numbering varies by provider type)
    pub focused_field: usize,
    /// Whether the provider list is expanded/visible
    pub showing_providers: bool,
    /// Codex OAuth device flow: user code to display
    pub codex_user_code: Option<String>,
    /// Codex OAuth device flow: current status
    pub codex_device_flow_status: crate::tui::onboarding::CodexDeviceFlowStatus,
}

impl ProviderSelectorState {
    /// Get provider info for the currently selected provider.
    pub fn current_provider(&self) -> &ProviderInfo {
        let idx = if self.selected_provider >= CUSTOM_PROVIDER_IDX {
            CUSTOM_PROVIDER_IDX
        } else {
            self.selected_provider
        };
        &PROVIDERS[idx]
    }

    pub fn is_custom(&self) -> bool {
        self.selected_provider >= CUSTOM_PROVIDER_IDX
    }

    pub fn is_cli(&self) -> bool {
        let id = self.provider_id();
        id == "claude-cli" || id == "opencode-cli" || id == "codex-cli"
    }

    pub fn is_oauth(&self) -> bool {
        let id = self.provider_id();
        id == "github" || id == "codex"
    }

    /// Whether the current provider needs NO API key from the user: CLI
    /// subprocesses (claude-cli, …) AND key-less API providers whose onboarding
    /// entry has an empty `key_label` (e.g. Xiaomi, where the proxy supplies the
    /// key). The single source of truth for "skip the API-key field" — use this
    /// instead of re-deriving `is_cli() || key_label.is_empty()` at each site.
    pub fn is_keyless(&self) -> bool {
        self.is_cli() || (!self.is_custom() && self.current_provider().key_label.is_empty())
    }

    pub fn is_zhipu(&self) -> bool {
        self.provider_id() == "zhipu"
    }

    /// Get the canonical provider id for the current selection.
    pub fn provider_id(&self) -> &'static str {
        if self.selected_provider < CUSTOM_PROVIDER_IDX {
            PROVIDERS[self.selected_provider].id
        } else {
            "" // custom
        }
    }
}

/// Look up the index of a provider by its canonical id. Returns `None`
/// if the id isn't in `PROVIDERS`. Lets call-sites avoid hardcoding
/// positions so reordering the array doesn't cascade into the TUI.
pub fn index_of_provider(id: &str) -> Option<usize> {
    PROVIDERS.iter().position(|p| p.id == id)
}

impl ProviderSelectorState {
    /// Whether the current provider supports live model fetching from API.
    pub fn supports_model_fetch(&self) -> bool {
        // Custom providers: always try /v1/models if base_url is set
        if self.is_custom() {
            return !self.base_url.trim().is_empty();
        }
        matches!(
            self.provider_id(),
            "anthropic"
                | "openai"
                | "github"
                | "gemini"
                | "openrouter"
                | "zhipu"
                | "opencode-cli"
                | "codex-cli"
                | "codex"
                | "opencode"
                | "ollama"
        )
    }

    /// Maximum number of fields for the current provider type.
    pub fn max_field(&self) -> usize {
        if self.is_custom() {
            6 // provider(0), base_url(1), api_key(2), model(3), name(4), context_window(5)
        } else if self.is_zhipu() {
            4 // provider(0), endpoint_type(1), api_key(2), model(3)
        } else {
            3 // provider(0), api_key(1), model(2)
        }
    }

    /// Whether the current api_key_input holds a pre-existing key sentinel.
    pub fn has_existing_key_sentinel(&self) -> bool {
        self.api_key_input == EXISTING_KEY_SENTINEL
    }

    /// Visual display order: named providers sorted alphabetically,
    /// then existing custom instances, then "+ New Custom" last.
    pub fn provider_display_order(&self) -> Vec<usize> {
        let num_customs = self.custom_names.len();
        // Named providers: everything except the last "Custom" sentinel
        let mut static_indices: Vec<usize> = (0..CUSTOM_PROVIDER_IDX).collect();
        static_indices.sort_by_key(|&i| PROVIDERS[i].name.to_ascii_lowercase());
        static_indices
            .into_iter()
            .chain(CUSTOM_INSTANCES_START..CUSTOM_INSTANCES_START + num_customs)
            .chain(std::iter::once(CUSTOM_PROVIDER_IDX))
            .collect()
    }

    /// Check if a provider at the given index has credentials configured.
    /// Used by renderers to show a green indicator in the provider list.
    /// Does NOT mutate state — pure read from config.
    pub fn provider_has_credentials(&self, idx: usize) -> bool {
        let config = match crate::config::Config::load() {
            Ok(c) => c,
            Err(_) => return false,
        };

        if idx < CUSTOM_PROVIDER_IDX {
            let id = PROVIDERS[idx].id;
            match id {
                // CLI providers — always "configured" if binary exists
                "claude-cli" | "opencode-cli" | "codex-cli" => {
                    let bin = match id {
                        "claude-cli" => "claude",
                        "opencode-cli" => "opencode",
                        _ => "codex",
                    };
                    which::which(bin).is_ok()
                }
                // OAuth providers — check for token/accounts
                "github" => config
                    .providers
                    .github
                    .as_ref()
                    .and_then(|p| p.api_key.as_ref())
                    .is_some_and(|k| !k.is_empty()),
                "codex" => {
                    // Check for OAuth tokens at ~/.opencrabs/auth/codex.json
                    let token_path = crate::config::opencrabs_home()
                        .join("auth")
                        .join("codex.json");
                    token_path.exists()
                }
                "qwen" => config
                    .providers
                    .qwen
                    .as_ref()
                    .and_then(|p| p.api_key.as_ref())
                    .is_some_and(|k| !k.is_empty()),
                // Keyless API providers (empty key_label, e.g. Xiaomi): the
                // proxy supplies the key, so they're always configured/ready.
                _ if PROVIDERS[idx].key_label.is_empty() => true,
                // Standard API key providers
                _ => crate::utils::providers::config_for(&config.providers, id)
                    .and_then(|p| p.api_key.as_ref())
                    .is_some_and(|k| !k.is_empty()),
            }
        } else if idx == CUSTOM_PROVIDER_IDX {
            false // "+ New Custom" — never configured
        } else {
            // Existing custom provider
            let custom_idx = idx - CUSTOM_INSTANCES_START;
            self.custom_names
                .get(custom_idx)
                .and_then(|name| config.providers.custom_by_name(name))
                .and_then(|p| p.api_key.as_ref())
                .is_some_and(|k| !k.is_empty())
        }
    }

    /// Detect if an API key exists in config for the current provider.
    /// Sets `has_existing_key` flag and `api_key_input` sentinel. Never loads actual key.
    pub fn detect_existing_key(&mut self) {
        fn has_nonempty_key(p: Option<&ProviderConfig>) -> bool {
            p.and_then(|p| p.api_key.as_ref())
                .is_some_and(|k| !k.is_empty())
        }

        self.api_key_input.clear();
        self.has_existing_key = false;

        if let Ok(config) = crate::config::Config::load() {
            let has_key = if self.selected_provider < CUSTOM_PROVIDER_IDX {
                let id = PROVIDERS[self.selected_provider].id;
                if self.is_cli() {
                    false // CLI providers — no API key
                } else if self.is_oauth() {
                    // OAuth providers — check for token file, not API key
                    let id = PROVIDERS[self.selected_provider].id;
                    if id == "codex" {
                        let token_path = crate::config::opencrabs_home()
                            .join("auth")
                            .join("codex.json");
                        token_path.exists()
                    } else if id == "github" {
                        config
                            .providers
                            .github
                            .as_ref()
                            .and_then(|p| p.api_key.as_ref())
                            .is_some_and(|k| !k.is_empty())
                    } else {
                        false
                    }
                } else {
                    has_nonempty_key(crate::utils::providers::config_for(&config.providers, id))
                }
            } else if self.selected_provider == CUSTOM_PROVIDER_IDX {
                // New custom — start with blank fields
                self.custom_name.clear();
                self.base_url.clear();
                self.custom_model.clear();
                self.context_window.clear();
                false
            } else {
                // Existing custom provider
                let custom_idx = self.selected_provider - CUSTOM_INSTANCES_START;
                if let Some(cname) = self.custom_names.get(custom_idx).cloned() {
                    if let Some(c) = config.providers.custom_by_name(&cname) {
                        self.custom_name = cname;
                        self.base_url = c.base_url.clone().unwrap_or_default();
                        self.custom_model = c.default_model.clone().unwrap_or_default();
                        self.context_window = c
                            .context_window
                            .map(|cw| cw.to_string())
                            .unwrap_or_default();
                        c.api_key.as_ref().is_some_and(|k| !k.is_empty())
                    } else {
                        false
                    }
                } else {
                    false
                }
            };

            self.has_existing_key = has_key;
            if has_key {
                self.api_key_input = EXISTING_KEY_SENTINEL.to_string();
                self.api_key_cursor = 0;
            }
        }

        // Clear model selection when provider changes
        self.selected_model = 0;
        self.model_filter.clear();
    }

    /// Load custom provider fields when navigating to an existing custom (10+),
    /// clear fields for new custom (9), load zhipu endpoint type for index 6.
    pub fn load_custom_fields(&mut self) {
        if self.is_zhipu()
            && let Ok(config) = crate::config::Config::load()
            && let Some(zhipu) = &config.providers.zhipu
        {
            self.zhipu_endpoint_type = match zhipu.endpoint_type.as_deref() {
                Some("coding") => 1,
                _ => 0,
            };
        }
        if self.selected_provider == CUSTOM_PROVIDER_IDX {
            self.custom_name.clear();
            self.base_url.clear();
            self.custom_model.clear();
            self.context_window.clear();
        } else if self.selected_provider >= CUSTOM_INSTANCES_START {
            let custom_idx = self.selected_provider - CUSTOM_INSTANCES_START;
            if let Some(cname) = self.custom_names.get(custom_idx).cloned()
                && let Ok(config) = crate::config::Config::load()
                && let Some(c) = config.providers.custom_by_name(&cname)
            {
                self.custom_name = cname;
                self.base_url = c.base_url.clone().unwrap_or_default();
                self.custom_model = c.default_model.clone().unwrap_or_default();
                self.context_window = c
                    .context_window
                    .map(|cw| cw.to_string())
                    .unwrap_or_default();
                if c.api_key.as_ref().is_some_and(|k| !k.is_empty()) {
                    self.api_key_input = EXISTING_KEY_SENTINEL.to_string();
                }
            }
        }
    }

    /// Load the actual API key value from config for the current provider.
    /// Used when making API calls (fetch models, save config). Returns None if no key.
    pub fn load_api_key_from_config(&self) -> Option<String> {
        let config = crate::config::Config::load().ok()?;
        if self.selected_provider < CUSTOM_PROVIDER_IDX {
            crate::utils::providers::config_for(
                &config.providers,
                PROVIDERS[self.selected_provider].id,
            )
            .and_then(|p| p.api_key.clone())
        } else if self.selected_provider >= CUSTOM_INSTANCES_START {
            let custom_idx = self.selected_provider - CUSTOM_INSTANCES_START;
            self.custom_names.get(custom_idx).and_then(|name| {
                config
                    .providers
                    .custom_by_name(name)
                    .and_then(|p| p.api_key.clone())
            })
        } else {
            None
        }
        .filter(|k| !k.is_empty())
    }

    /// Resolve the effective API key: user-typed key if present, else config key.
    pub fn resolve_api_key(&self) -> Option<String> {
        if !self.api_key_input.is_empty() && self.api_key_input != EXISTING_KEY_SENTINEL {
            Some(self.api_key_input.clone())
        } else {
            self.load_api_key_from_config()
        }
    }

    /// Zhipu endpoint type as string for API calls.
    pub fn zhipu_endpoint_str(&self) -> Option<String> {
        if self.is_zhipu() {
            Some(
                if self.zhipu_endpoint_type == 1 {
                    "coding"
                } else {
                    "api"
                }
                .to_string(),
            )
        } else {
            None
        }
    }

    // ── Model list management ───────────────────────────────────────

    /// Merge any config-persisted models into the live-fetched list
    /// so user-pasted models that the provider's `/v1/models` doesn't
    /// list survive the fetch. Reloads `config_models` first so the
    /// merge sees the latest disk state. Fetched names keep their
    /// order at the top; config-only names get appended at the end.
    pub fn merge_config_models_into_fetched(&mut self) {
        self.reload_config_models();
        let extras: Vec<String> = self
            .config_models
            .iter()
            .filter(|m| !self.models.iter().any(|x| x == *m))
            .cloned()
            .collect();
        self.models.extend(extras);
    }

    /// Reload config_models for the currently selected provider.
    pub fn reload_config_models(&mut self) {
        self.config_models.clear();
        if let Ok(config) = crate::config::Config::load() {
            if self.is_cli() {
                return; // CLI — static or fetched, no config models
            }
            if self.selected_provider < CUSTOM_PROVIDER_IDX {
                let id = PROVIDERS[self.selected_provider].id;
                if let Some(p) = crate::utils::providers::config_for(&config.providers, id)
                    && !p.models.is_empty()
                {
                    self.config_models = p.models.clone();
                    return;
                }
            } else if self.selected_provider >= CUSTOM_PROVIDER_IDX
                && let Some((_name, p)) = config.providers.active_custom()
                && !p.models.is_empty()
            {
                self.config_models = p.models.clone();
                return;
            }
        }
        self.config_models = load_default_models(self.provider_id());
    }

    /// All model names for the current provider, with the live fetch
    /// merged on top of the config-persisted list. Any model the user
    /// has previously pasted in (and saved) survives even when the
    /// provider's `/v1/models` endpoint omits it on the next call.
    /// Fetched names win for ordering; config-only names are appended
    /// at the end in their original order. Falls back to the static
    /// provider catalogue when nothing's been fetched or saved yet.
    pub fn all_model_names(&self) -> Vec<&str> {
        if self.models.is_empty() && self.config_models.is_empty() {
            return self.current_provider().models.to_vec();
        }
        let mut out: Vec<&str> = Vec::with_capacity(self.models.len() + self.config_models.len());
        for m in &self.models {
            out.push(m.as_str());
        }
        for m in &self.config_models {
            if !out.contains(&m.as_str()) {
                out.push(m.as_str());
            }
        }
        out
    }

    /// Model names filtered by `model_filter` (case-insensitive substring match).
    pub fn filtered_model_names(&self) -> Vec<&str> {
        let all = self.all_model_names();
        if self.model_filter.is_empty() {
            all
        } else {
            let q = self.model_filter.to_lowercase();
            all.into_iter()
                .filter(|m| m.to_lowercase().contains(&q))
                .collect()
        }
    }

    /// Number of models available after applying the current filter.
    pub fn model_count(&self) -> usize {
        self.filtered_model_names().len()
    }

    /// Get the selected model name (resolves through filter).
    ///
    /// Three branches, in order:
    ///   1. Filter matches something → pick `filtered[selected_model]`.
    ///   2. Filter matches nothing AND filter is non-empty → use the
    ///      typed text itself as the model name. This is the escape
    ///      hatch for new models that aren't in the hardcoded list yet
    ///      (e.g. user types `MiniMax-M3` on a build where the suggestion
    ///      list still only shows M2.7 / M2.5 / M2.1). The wizard render
    ///      should surface this so the user can see what will commit.
    ///   3. Filter is empty AND nothing matches → fall back to the first
    ///      entry in the full list (default behaviour for a fresh
    ///      provider with no typed input).
    pub fn selected_model_name(&self) -> &str {
        let filtered = self.filtered_model_names();
        if let Some(name) = filtered.get(self.selected_model) {
            name
        } else if !self.model_filter.trim().is_empty() {
            // Typed text becomes the model name when there's no list
            // match. Without this branch the wizard silently fell back
            // to "first item in the list", losing the user's input —
            // a user typing `MiniMax-M3` on a build before this fix
            // would end up configured for `MiniMax-M2.7`.
            self.model_filter.trim()
        } else {
            self.all_model_names().first().copied().unwrap_or("")
        }
    }

    /// Resolve `selected_model` index from `custom_model` string.
    pub fn resolve_selected_model_index(&mut self) {
        if self.custom_model.is_empty() {
            return;
        }
        let all = self.all_model_names();
        if let Some(idx) = all.iter().position(|m| *m == self.custom_model) {
            self.selected_model = idx;
        }
    }

    /// Cache existing custom provider names from config.
    pub fn load_custom_names(&mut self) {
        self.custom_names = crate::config::Config::load()
            .ok()
            .and_then(|c| c.providers.custom.map(|m| m.keys().cloned().collect()))
            .unwrap_or_default();
    }
}

/// Map an API model id to a human-readable display label.
/// Returns the id itself when no special label is defined.
/// Used by /models, onboarding, and the footer to show friendly names
/// for models whose API id is an opaque alias (e.g. qwen-oauth's `coder-model`).
pub fn model_display_label(model_id: &str) -> &str {
    match model_id {
        "qwen-3.7-max" | "qwen3.7-max" | "qwen-latest-series" | "qwen-latest-series-invite" => {
            "Qwen 3.7 Max"
        }
        "qwen-3.7-plus" | "qwen3.7-plus" | "qwen-3.7-plus-preview" => "Qwen 3.7 Plus",
        "qwen-3.6-max-preview" | "qwen3.6-max-preview" => "Qwen 3.6 Max Preview",
        "coder-model" | "qwen-3.6-plus" | "qwen3.6-plus" => "Qwen 3.6 Plus",
        "qwen-3.5-plus" | "qwen3.5-plus" => "Qwen 3.5 Plus",
        "minimax-m2.5" => "Minimax M2.5",
        "minimax-m2.7" => "Minimax M2.7",
        "minimax-m3" => "Minimax M3",
        "mimo-v2-omni" | "mimo-v2-omni-free" => "Mimo V2 Omni",
        "mimo-v2-pro" | "mimo-v2-pro-free" => "Mimo V2 Pro",
        "kimi-k2.6" => "Kimi K2.6",
        "kimi-k2.5" | "kimi-k2-5" => "Kimi K2.5",
        "glm-5.1" => "GLM 5.1",
        "glm-5-turbo" => "GLM 5 Turbo",
        "fable" | "fable-5" => "Fable 5",
        "opus-4-8" => "Opus 4.8",
        "opus-4-7" => "Opus 4.7",
        "opus-4-6" => "Opus 4.6",
        "sonnet-4-6" => "Sonnet 4.6",
        "haiku-4-5" => "Haiku 4.5",
        other => prettify_claude_cli_model(other).unwrap_or(other),
    }
}

/// Fallback prettifier for Claude CLI shorthand models we haven't
/// hardcoded yet. Matches `opus-X-Y` / `sonnet-X-Y` / `haiku-X-Y` and
/// returns "Opus X.Y", "Sonnet X.Y", "Haiku X.Y". The `&'static str`
/// return matches the main match arm; per-id strings are leaked into
/// a process-wide cache (one slot per distinct model id ever observed)
/// so the same id never re-leaks.
fn prettify_claude_cli_model(model: &str) -> Option<&'static str> {
    use std::collections::HashMap;
    use std::sync::{LazyLock, Mutex};
    static PRETTIFIED: LazyLock<Mutex<HashMap<String, &'static str>>> =
        LazyLock::new(|| Mutex::new(HashMap::new()));

    let (family, rest) = if let Some(r) = model.strip_prefix("opus-") {
        ("Opus", r)
    } else if let Some(r) = model.strip_prefix("sonnet-") {
        ("Sonnet", r)
    } else if let Some(r) = model.strip_prefix("haiku-") {
        ("Haiku", r)
    } else {
        return None;
    };
    let (major, minor) = rest.split_once('-')?;
    if major.is_empty()
        || minor.is_empty()
        || !major.chars().all(|c| c.is_ascii_digit())
        || !minor.chars().all(|c| c.is_ascii_digit())
    {
        return None;
    }

    let mut cache = PRETTIFIED.lock().ok()?;
    if let Some(existing) = cache.get(model) {
        return Some(existing);
    }
    let pretty: &'static str =
        Box::leak(format!("{} {}.{}", family, major, minor).into_boxed_str());
    cache.insert(model.to_string(), pretty);
    Some(pretty)
}

/// Load default models from embedded config.toml.example for a provider.
pub fn load_default_models(provider_id: &str) -> Vec<String> {
    let config_content = include_str!("../../config.toml.example");
    let mut models = Vec::new();

    if let Ok(config) = config_content.parse::<toml::Value>()
        && let Some(providers) = config.get("providers")
    {
        // Map provider id to config.toml.example section key
        // (config uses underscore: "claude_cli", but provider id uses hyphen: "claude-cli")
        let section_key = match provider_id {
            "claude-cli" => "claude_cli",
            "opencode-cli" => "opencode_cli",
            "codex-cli" => "codex_cli",
            "codex" => "codex", // Codex OAuth
            "" => "custom",     // empty id = custom providers
            other => other,
        };

        if section_key == "custom" {
            // Custom providers: merge models from all custom sections
            if let Some(custom) = providers.get("custom")
                && let Some(custom_table) = custom.as_table()
            {
                for (_name, entry) in custom_table {
                    if let Some(models_arr) = entry.get("models").and_then(|m| m.as_array()) {
                        for model in models_arr {
                            if let Some(model_str) = model.as_str()
                                && !models.contains(&model_str.to_string())
                            {
                                models.push(model_str.to_string());
                            }
                        }
                    }
                }
            }
        } else if let Some(section) = providers.get(section_key)
            && let Some(models_arr) = section.get("models").and_then(|m| m.as_array())
        {
            for model in models_arr {
                if let Some(model_str) = model.as_str() {
                    models.push(model_str.to_string());
                }
            }
        }
    }

    tracing::debug!(
        "Loaded {} default models from config.toml.example for provider '{}'",
        models.len(),
        provider_id
    );
    models
}