ironclaw 0.24.0

Secure personal AI assistant that protects your data and expands its capabilities on the fly
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
//! Declarative LLM provider registry.
//!
//! Providers are defined in JSON (compiled-in defaults + optional user file)
//! so adding a new OpenAI-compatible provider requires zero Rust code changes.
//!
//! ```text
//!   ┌─────────────────────┐    ┌──────────────────────────┐
//!   │  providers.json     │    │ ~/.ironclaw/providers.json│
//!   │  (built-in, embed)  │    │ (user overrides/extras)  │
//!   └────────┬────────────┘    └────────────┬─────────────┘
//!            │                              │
//!            └──────────┬───────────────────┘
//!//!              ┌──────────────────┐
//!              │ ProviderRegistry │
//!              │  .find("groq")   │──▶ ProviderDefinition
//!              │  .all()          │        ├ protocol
//!              │  .selectable()   │        ├ default_base_url
//!              └──────────────────┘        ├ api_key_env
//!                                          └ ...
//! ```

use std::collections::HashMap;

use serde::{Deserialize, Serialize};

/// API protocol a provider speaks.
///
/// Determines which rig-core client constructor to use.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ProviderProtocol {
    /// OpenAI Chat Completions API (`/v1/chat/completions`).
    /// Used by: OpenAI, Tinfoil, Groq, NVIDIA NIM, OpenRouter, etc.
    OpenAiCompletions,
    /// Anthropic Messages API.
    Anthropic,
    /// Ollama API (OpenAI-ish, no API key required).
    Ollama,
    /// GitHub Copilot API (OpenAI-compatible with token exchange).
    GithubCopilot,
}

/// How the setup wizard should collect credentials for this provider.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum SetupHint {
    /// Collect an API key and store it in the encrypted secrets store.
    ApiKey {
        /// Key name in the secrets store (e.g., "llm_groq_api_key").
        secret_name: String,
        /// URL where the user can generate an API key.
        #[serde(default)]
        key_url: Option<String>,
        /// Human-readable name for display in the wizard.
        display_name: String,
        /// Whether this provider supports `/v1/models` listing.
        #[serde(default)]
        can_list_models: bool,
        /// Optional filter for model listing (e.g., "chat").
        #[serde(default)]
        models_filter: Option<String>,
    },
    /// Ollama-style setup: just a base URL, no API key.
    Ollama {
        display_name: String,
        #[serde(default)]
        can_list_models: bool,
    },
    /// Generic OpenAI-compatible: ask for base URL + optional API key.
    OpenAiCompatible {
        secret_name: String,
        display_name: String,
        #[serde(default)]
        can_list_models: bool,
    },
}

impl SetupHint {
    pub fn display_name(&self) -> &str {
        match self {
            Self::ApiKey { display_name, .. } => display_name,
            Self::Ollama { display_name, .. } => display_name,
            Self::OpenAiCompatible { display_name, .. } => display_name,
        }
    }

    pub fn can_list_models(&self) -> bool {
        match self {
            Self::ApiKey {
                can_list_models, ..
            } => *can_list_models,
            Self::Ollama {
                can_list_models, ..
            } => *can_list_models,
            Self::OpenAiCompatible {
                can_list_models, ..
            } => *can_list_models,
        }
    }

    pub fn secret_name(&self) -> Option<&str> {
        match self {
            Self::ApiKey { secret_name, .. } => Some(secret_name),
            Self::OpenAiCompatible { secret_name, .. } => Some(secret_name),
            Self::Ollama { .. } => None,
        }
    }

    pub fn models_filter(&self) -> Option<&str> {
        match self {
            Self::ApiKey { models_filter, .. } => models_filter.as_deref(),
            _ => None,
        }
    }
}

/// Validates unsupported_params during deserialization.
///
/// Only allows: "temperature", "max_tokens", "stop_sequences".
/// Invalid parameter names cause a deserialization error.
mod unsupported_params_de {
    use serde::{Deserialize, Deserializer};

    const VALID_PARAMS: &[&str] = &["temperature", "max_tokens", "stop_sequences"];

    pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<String>, D::Error>
    where
        D: Deserializer<'de>,
    {
        let params: Vec<String> = Deserialize::deserialize(deserializer)?;
        for param in &params {
            if !VALID_PARAMS.contains(&param.as_str()) {
                return Err(serde::de::Error::custom(format!(
                    "unsupported parameter name '{}': must be one of: {}",
                    param,
                    VALID_PARAMS.join(", ")
                )));
            }
        }
        Ok(params)
    }
}

/// Declarative definition of an LLM provider.
///
/// One JSON object in `providers.json` maps to one `ProviderDefinition`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProviderDefinition {
    /// Unique identifier used in `LLM_BACKEND` (e.g., "groq", "tinfoil").
    pub id: String,
    /// Alternative names accepted in `LLM_BACKEND` (e.g., ["nvidia_nim", "nim"]).
    #[serde(default)]
    pub aliases: Vec<String>,
    /// Which API protocol to use.
    pub protocol: ProviderProtocol,
    /// Default base URL. `None` means use the rig-core default for the protocol.
    #[serde(default)]
    pub default_base_url: Option<String>,
    /// Env var for base URL override (e.g., "OPENAI_BASE_URL").
    #[serde(default)]
    pub base_url_env: Option<String>,
    /// Whether a base URL is required (for generic openai_compatible).
    #[serde(default)]
    pub base_url_required: bool,
    /// Env var for the API key (e.g., "GROQ_API_KEY").
    #[serde(default)]
    pub api_key_env: Option<String>,
    /// Whether an API key is required to use this provider.
    #[serde(default)]
    pub api_key_required: bool,
    /// Env var for the model name (e.g., "GROQ_MODEL").
    pub model_env: String,
    /// Default model if none specified.
    pub default_model: String,
    /// Human-readable one-line description.
    pub description: String,
    /// Env var for extra HTTP headers (format: `Key:Value,Key2:Value2`).
    #[serde(default)]
    pub extra_headers_env: Option<String>,
    /// Setup wizard hints.
    #[serde(default)]
    pub setup: Option<SetupHint>,
    /// Parameter names that this provider does not support (e.g., `["temperature"]`).
    /// Supported keys: `"temperature"`, `"max_tokens"`, `"stop_sequences"`.
    /// Listed parameters are stripped from requests before sending to avoid 400 errors.
    /// Invalid parameter names cause a deserialization error.
    #[serde(default, deserialize_with = "unsupported_params_de::deserialize")]
    pub unsupported_params: Vec<String>,
}

/// Registry of known LLM providers.
///
/// Built from compiled-in `providers.json` plus optional user overrides
/// from `~/.ironclaw/providers.json`.
pub struct ProviderRegistry {
    providers: Vec<ProviderDefinition>,
    /// Lowercase id/alias → index into `providers`.
    lookup: HashMap<String, usize>,
}

impl ProviderRegistry {
    /// Build a registry from a list of provider definitions.
    ///
    /// Later entries with duplicate IDs/aliases override earlier ones.
    pub fn new(providers: Vec<ProviderDefinition>) -> Self {
        let mut lookup = HashMap::new();
        for (idx, def) in providers.iter().enumerate() {
            lookup.insert(def.id.to_lowercase(), idx);
            for alias in &def.aliases {
                lookup.insert(alias.to_lowercase(), idx);
            }
        }
        Self { providers, lookup }
    }

    /// Load the default registry: built-in providers + user overrides.
    ///
    /// User providers from `~/.ironclaw/providers.json` are appended,
    /// with later entries overriding earlier ones by ID/alias.
    pub fn load() -> Self {
        let builtins: Vec<ProviderDefinition> =
            serde_json::from_str(include_str!("../../providers.json"))
                .expect("built-in providers.json must be valid JSON"); // safety: compile-time embedded file

        let mut all = builtins;

        if let Some(user_path) = user_providers_path()
            && user_path.exists()
        {
            match std::fs::read_to_string(&user_path) {
                Ok(contents) => match serde_json::from_str::<Vec<ProviderDefinition>>(&contents) {
                    Ok(user_defs) => {
                        tracing::info!(
                            count = user_defs.len(),
                            path = %user_path.display(),
                            "Loaded user provider definitions"
                        );
                        all.extend(user_defs);
                    }
                    Err(e) => {
                        tracing::warn!(
                            path = %user_path.display(),
                            error = %e,
                            "Failed to parse user providers.json, skipping"
                        );
                    }
                },
                Err(e) => {
                    tracing::warn!(
                        path = %user_path.display(),
                        error = %e,
                        "Failed to read user providers.json, skipping"
                    );
                }
            }
        }

        Self::new(all)
    }

    /// Look up a provider by ID or alias (case-insensitive).
    pub fn find(&self, id: &str) -> Option<&ProviderDefinition> {
        self.lookup
            .get(&id.to_lowercase())
            .map(|&idx| &self.providers[idx])
    }

    /// All registered providers (built-in + user).
    pub fn all(&self) -> &[ProviderDefinition] {
        &self.providers
    }

    /// Providers that should appear in the setup wizard's selection menu.
    ///
    /// Returns all providers that have a `setup` hint, in registry order.
    /// NearAI is not in the registry (handled specially) so it won't appear here.
    pub fn selectable(&self) -> Vec<&ProviderDefinition> {
        // Deduplicate: only keep the last definition for each ID
        let mut seen = HashMap::new();
        for def in &self.providers {
            seen.insert(def.id.as_str(), def);
        }
        // Preserve order of first appearance, but use the last (overridden)
        // definition for each ID. A user override that adds `setup` to a
        // provider that previously lacked it will be included correctly.
        let mut result = Vec::new();
        let mut emitted = std::collections::HashSet::new();
        for def in &self.providers {
            if emitted.insert(def.id.as_str()) {
                let final_def = seen[def.id.as_str()];
                if final_def.setup.is_some() {
                    result.push(final_def);
                }
            }
        }
        result
    }

    /// Check whether a backend string is a known provider (NearAI or registry).
    pub fn is_known(&self, backend: &str) -> bool {
        backend == "nearai"
            || backend == "near_ai"
            || backend == "near"
            || self.find(backend).is_some()
    }

    /// Get the model env var for a backend string.
    ///
    /// Returns the registry provider's `model_env` if found,
    /// or `"NEARAI_MODEL"` for the NearAI backend.
    pub fn model_env_var(&self, backend: &str) -> &str {
        if backend == "nearai" || backend == "near_ai" || backend == "near" {
            return "NEARAI_MODEL";
        }
        self.find(backend)
            .map(|def| def.model_env.as_str())
            .unwrap_or("LLM_MODEL")
    }
}

fn user_providers_path() -> Option<std::path::PathBuf> {
    Some(crate::bootstrap::ironclaw_base_dir().join("providers.json"))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_builtin_registry_loads() {
        let registry = ProviderRegistry::new(
            serde_json::from_str(include_str!("../../providers.json")).unwrap(),
        );
        assert!(
            registry.all().len() >= 5,
            "should have at least 5 built-in providers"
        );
    }

    #[test]
    fn test_find_by_id() {
        let registry = ProviderRegistry::new(
            serde_json::from_str(include_str!("../../providers.json")).unwrap(),
        );
        let openai = registry.find("openai").expect("openai should exist");
        assert_eq!(openai.id, "openai");
        assert_eq!(openai.protocol, ProviderProtocol::OpenAiCompletions);
    }

    #[test]
    fn test_find_by_alias() {
        let registry = ProviderRegistry::new(
            serde_json::from_str(include_str!("../../providers.json")).unwrap(),
        );
        let openai = registry
            .find("open_ai")
            .expect("alias open_ai should resolve");
        assert_eq!(openai.id, "openai");
    }

    #[test]
    fn test_find_case_insensitive() {
        let registry = ProviderRegistry::new(
            serde_json::from_str(include_str!("../../providers.json")).unwrap(),
        );
        assert!(registry.find("OpenAI").is_some());
        assert!(registry.find("GROQ").is_some());
        assert!(registry.find("Tinfoil").is_some());
    }

    #[test]
    fn test_find_unknown_returns_none() {
        let registry = ProviderRegistry::new(
            serde_json::from_str(include_str!("../../providers.json")).unwrap(),
        );
        assert!(registry.find("nonexistent_provider").is_none());
    }

    #[test]
    fn test_selectable_has_setup_hints() {
        let registry = ProviderRegistry::new(
            serde_json::from_str(include_str!("../../providers.json")).unwrap(),
        );
        let selectable = registry.selectable();
        assert!(!selectable.is_empty());
        for def in &selectable {
            assert!(
                def.setup.is_some(),
                "selectable provider {} must have setup hint",
                def.id
            );
        }
    }

    #[test]
    fn test_user_override_wins() {
        let builtins: Vec<ProviderDefinition> =
            serde_json::from_str(include_str!("../../providers.json")).unwrap();
        let mut all = builtins;
        // Simulate user overriding tinfoil with a different default model
        all.push(ProviderDefinition {
            id: "tinfoil".to_string(),
            aliases: vec![],
            protocol: ProviderProtocol::OpenAiCompletions,
            default_base_url: Some("https://custom.tinfoil.example/v1".to_string()),
            base_url_env: None,
            base_url_required: false,
            api_key_env: Some("TINFOIL_API_KEY".to_string()),
            api_key_required: true,
            model_env: "TINFOIL_MODEL".to_string(),
            default_model: "custom-model".to_string(),
            description: "Custom tinfoil".to_string(),
            extra_headers_env: None,
            setup: None,
            unsupported_params: vec![],
        });
        let registry = ProviderRegistry::new(all);
        let tf = registry.find("tinfoil").expect("tinfoil should exist");
        assert_eq!(tf.default_model, "custom-model", "user override should win");
    }

    #[test]
    fn test_model_env_var_nearai() {
        let registry = ProviderRegistry::new(
            serde_json::from_str(include_str!("../../providers.json")).unwrap(),
        );
        assert_eq!(registry.model_env_var("nearai"), "NEARAI_MODEL");
        assert_eq!(registry.model_env_var("near_ai"), "NEARAI_MODEL");
    }

    #[test]
    fn test_model_env_var_registry_provider() {
        let registry = ProviderRegistry::new(
            serde_json::from_str(include_str!("../../providers.json")).unwrap(),
        );
        assert_eq!(registry.model_env_var("groq"), "GROQ_MODEL");
        assert_eq!(registry.model_env_var("tinfoil"), "TINFOIL_MODEL");
        assert_eq!(registry.model_env_var("openai"), "OPENAI_MODEL");
    }

    #[test]
    fn test_model_env_var_unknown_fallback() {
        let registry = ProviderRegistry::new(
            serde_json::from_str(include_str!("../../providers.json")).unwrap(),
        );
        assert_eq!(registry.model_env_var("nonexistent"), "LLM_MODEL");
    }

    #[test]
    fn test_is_known() {
        let registry = ProviderRegistry::new(
            serde_json::from_str(include_str!("../../providers.json")).unwrap(),
        );
        assert!(registry.is_known("nearai"));
        assert!(registry.is_known("openai"));
        assert!(registry.is_known("groq"));
        assert!(!registry.is_known("nonexistent"));
    }

    #[test]
    fn test_all_providers_have_required_fields() {
        let providers: Vec<ProviderDefinition> =
            serde_json::from_str(include_str!("../../providers.json")).unwrap();
        for def in &providers {
            assert!(!def.id.is_empty(), "provider must have an id");
            assert!(!def.model_env.is_empty(), "{}: model_env required", def.id);
            assert!(
                !def.default_model.is_empty(),
                "{}: default_model required",
                def.id
            );
            assert!(
                !def.description.is_empty(),
                "{}: description required",
                def.id
            );
        }
    }

    #[test]
    fn test_openai_compatible_providers_have_base_url() {
        let providers: Vec<ProviderDefinition> =
            serde_json::from_str(include_str!("../../providers.json")).unwrap();
        for def in &providers {
            if def.protocol == ProviderProtocol::OpenAiCompletions
                && def.id != "openai"
                && def.id != "openai_compatible"
                && def.id != "bedrock"
                && def.id != "cloudflare"
            {
                assert!(
                    def.default_base_url.is_some(),
                    "{}: OpenAI-completions provider should have a default_base_url",
                    def.id
                );
            }
        }
    }

    #[test]
    fn test_models_filter_accessor() {
        let registry = ProviderRegistry::new(
            serde_json::from_str(include_str!("../../providers.json")).unwrap(),
        );
        // Groq has models_filter: "chat"
        let groq = registry.find("groq").expect("groq should exist");
        let filter = groq
            .setup
            .as_ref()
            .and_then(|s| s.models_filter())
            .expect("groq should have models_filter");
        assert_eq!(filter, "chat");

        // OpenAI has no models_filter
        let openai = registry.find("openai").expect("openai should exist");
        assert!(
            openai
                .setup
                .as_ref()
                .and_then(|s| s.models_filter())
                .is_none(),
            "openai should not have models_filter"
        );

        // Ollama setup hint variant should return None
        let ollama = registry.find("ollama").expect("ollama should exist");
        assert!(
            ollama
                .setup
                .as_ref()
                .and_then(|s| s.models_filter())
                .is_none(),
            "ollama should not have models_filter"
        );
    }

    #[test]
    fn test_selectable_user_override_adds_setup() {
        // A built-in provider without setup hint should NOT appear in selectable().
        // But if a user override adds a setup hint, it SHOULD appear.
        let mut providers: Vec<ProviderDefinition> = vec![ProviderDefinition {
            id: "custom".to_string(),
            aliases: vec![],
            protocol: ProviderProtocol::OpenAiCompletions,
            default_base_url: Some("http://localhost/v1".to_string()),
            base_url_env: None,
            base_url_required: false,
            api_key_env: None,
            api_key_required: false,
            model_env: "CUSTOM_MODEL".to_string(),
            default_model: "m1".to_string(),
            description: "No setup".to_string(),
            extra_headers_env: None,
            setup: None, // no setup hint
            unsupported_params: vec![],
        }];

        let registry = ProviderRegistry::new(providers.clone());
        assert!(
            registry.selectable().is_empty(),
            "provider without setup should not be selectable"
        );

        // User override adds a setup hint
        providers.push(ProviderDefinition {
            id: "custom".to_string(),
            aliases: vec![],
            protocol: ProviderProtocol::OpenAiCompletions,
            default_base_url: Some("http://localhost/v1".to_string()),
            base_url_env: None,
            base_url_required: false,
            api_key_env: Some("CUSTOM_API_KEY".to_string()),
            api_key_required: true,
            model_env: "CUSTOM_MODEL".to_string(),
            default_model: "m1".to_string(),
            description: "Now with setup".to_string(),
            extra_headers_env: None,
            setup: Some(SetupHint::ApiKey {
                secret_name: "llm_custom_api_key".to_string(),
                key_url: None,
                display_name: "Custom".to_string(),
                can_list_models: false,
                models_filter: None,
            }),
            unsupported_params: vec![],
        });

        let registry = ProviderRegistry::new(providers);
        let selectable = registry.selectable();
        assert_eq!(
            selectable.len(),
            1,
            "user override with setup should appear"
        );
        assert_eq!(selectable[0].id, "custom");
        assert_eq!(
            selectable[0].description, "Now with setup",
            "should use the overridden definition"
        );
    }

    #[test]
    fn test_selectable_user_override_removes_setup() {
        // If a built-in has setup but user override removes it, it should
        // NOT appear in selectable().
        let providers = vec![
            ProviderDefinition {
                id: "provider_a".to_string(),
                aliases: vec![],
                protocol: ProviderProtocol::OpenAiCompletions,
                default_base_url: Some("http://a/v1".to_string()),
                base_url_env: None,
                base_url_required: false,
                api_key_env: Some("A_KEY".to_string()),
                api_key_required: true,
                model_env: "A_MODEL".to_string(),
                default_model: "m1".to_string(),
                description: "Has setup".to_string(),
                extra_headers_env: None,
                setup: Some(SetupHint::ApiKey {
                    secret_name: "a".to_string(),
                    key_url: None,
                    display_name: "A".to_string(),
                    can_list_models: false,
                    models_filter: None,
                }),
                unsupported_params: vec![],
            },
            // User override removes setup
            ProviderDefinition {
                id: "provider_a".to_string(),
                aliases: vec![],
                protocol: ProviderProtocol::OpenAiCompletions,
                default_base_url: Some("http://a/v1".to_string()),
                base_url_env: None,
                base_url_required: false,
                api_key_env: Some("A_KEY".to_string()),
                api_key_required: false,
                model_env: "A_MODEL".to_string(),
                default_model: "m1".to_string(),
                description: "No setup now".to_string(),
                extra_headers_env: None,
                setup: None,
                unsupported_params: vec![],
            },
        ];

        let registry = ProviderRegistry::new(providers);
        assert!(
            registry.selectable().is_empty(),
            "user override removing setup should exclude from selectable"
        );
        // But find() should still work (uses the override)
        let def = registry
            .find("provider_a")
            .expect("should still be findable");
        assert_eq!(def.description, "No setup now");
    }

    #[test]
    fn test_selectable_preserves_order_with_dedup() {
        // If providers A, B, C are defined, and a user override for B comes
        // later, selectable() should return A, B, C (not A, C, B).
        let providers = vec![
            ProviderDefinition {
                id: "aaa".to_string(),
                aliases: vec![],
                protocol: ProviderProtocol::OpenAiCompletions,
                default_base_url: Some("http://a/v1".to_string()),
                base_url_env: None,
                base_url_required: false,
                api_key_env: None,
                api_key_required: false,
                model_env: "A".to_string(),
                default_model: "m".to_string(),
                description: "A".to_string(),
                extra_headers_env: None,
                setup: Some(SetupHint::Ollama {
                    display_name: "A".to_string(),
                    can_list_models: false,
                }),
                unsupported_params: vec![],
            },
            ProviderDefinition {
                id: "bbb".to_string(),
                aliases: vec![],
                protocol: ProviderProtocol::OpenAiCompletions,
                default_base_url: Some("http://b/v1".to_string()),
                base_url_env: None,
                base_url_required: false,
                api_key_env: None,
                api_key_required: false,
                model_env: "B".to_string(),
                default_model: "m".to_string(),
                description: "B-original".to_string(),
                extra_headers_env: None,
                setup: Some(SetupHint::Ollama {
                    display_name: "B".to_string(),
                    can_list_models: false,
                }),
                unsupported_params: vec![],
            },
            ProviderDefinition {
                id: "ccc".to_string(),
                aliases: vec![],
                protocol: ProviderProtocol::OpenAiCompletions,
                default_base_url: Some("http://c/v1".to_string()),
                base_url_env: None,
                base_url_required: false,
                api_key_env: None,
                api_key_required: false,
                model_env: "C".to_string(),
                default_model: "m".to_string(),
                description: "C".to_string(),
                extra_headers_env: None,
                setup: Some(SetupHint::Ollama {
                    display_name: "C".to_string(),
                    can_list_models: false,
                }),
                unsupported_params: vec![],
            },
            // User override for B
            ProviderDefinition {
                id: "bbb".to_string(),
                aliases: vec![],
                protocol: ProviderProtocol::OpenAiCompletions,
                default_base_url: Some("http://b-new/v1".to_string()),
                base_url_env: None,
                base_url_required: false,
                api_key_env: None,
                api_key_required: false,
                model_env: "B".to_string(),
                default_model: "m".to_string(),
                description: "B-override".to_string(),
                extra_headers_env: None,
                setup: Some(SetupHint::Ollama {
                    display_name: "B".to_string(),
                    can_list_models: false,
                }),
                unsupported_params: vec![],
            },
        ];

        let registry = ProviderRegistry::new(providers);
        let selectable = registry.selectable();
        let ids: Vec<&str> = selectable.iter().map(|d| d.id.as_str()).collect();
        assert_eq!(ids, vec!["aaa", "bbb", "ccc"], "order should be preserved");
        assert_eq!(
            selectable[1].description, "B-override",
            "should use the overridden definition"
        );
    }

    #[test]
    fn test_unsupported_params_deserialized() {
        let providers: Vec<ProviderDefinition> =
            serde_json::from_str(include_str!("../../providers.json")).unwrap();

        // Tinfoil should have temperature in unsupported_params
        let tinfoil = providers.iter().find(|p| p.id == "tinfoil").unwrap();
        assert!(
            tinfoil
                .unsupported_params
                .contains(&"temperature".to_string()),
            "tinfoil should have 'temperature' in unsupported_params"
        );

        // OpenAI should also have temperature in unsupported_params
        let openai = providers.iter().find(|p| p.id == "openai").unwrap();
        assert!(
            openai
                .unsupported_params
                .contains(&"temperature".to_string()),
            "openai should have 'temperature' in unsupported_params"
        );

        // Providers without the field in JSON should deserialize to empty vec
        let groq = providers.iter().find(|p| p.id == "groq").unwrap();
        assert!(
            groq.unsupported_params.is_empty(),
            "groq should have empty unsupported_params (field absent in JSON)"
        );

        // All entries should only contain valid param names
        // (Invalid names should be rejected at deserialization time)
        for def in &providers {
            for param in &def.unsupported_params {
                assert!(
                    !param.is_empty(),
                    "{}: unsupported_params contains empty string",
                    def.id
                );
                assert!(
                    matches!(
                        param.as_str(),
                        "temperature" | "max_tokens" | "stop_sequences"
                    ),
                    "{}: unsupported_params contains invalid parameter '{}'",
                    def.id,
                    param
                );
            }
        }
    }

    #[test]
    fn test_unsupported_params_validation_rejects_invalid() {
        // Invalid parameter names should cause deserialization error
        let invalid_json = r#"[{
            "id": "test",
            "protocol": "open_ai_completions",
            "model_env": "TEST_MODEL",
            "default_model": "test-model",
            "description": "Test provider",
            "unsupported_params": ["temperrature"]
        }]"#;

        let result: Result<Vec<ProviderDefinition>, _> = serde_json::from_str(invalid_json);
        assert!(
            result.is_err(),
            "should reject invalid parameter name 'temperrature'"
        );
        assert!(
            result.err().unwrap().to_string().contains("temperrature"),
            "error message should mention the invalid parameter"
        );
    }

    #[test]
    fn test_all_builtin_api_key_providers_have_api_key_env() {
        // Every built-in provider with SetupHint::ApiKey must have api_key_env
        // set, otherwise inject_llm_keys_from_secrets can't map the secret.
        let providers: Vec<ProviderDefinition> =
            serde_json::from_str(include_str!("../../providers.json")).unwrap();
        for def in &providers {
            if let Some(SetupHint::ApiKey { .. }) = &def.setup {
                assert!(
                    def.api_key_env.is_some(),
                    "{}: ApiKey setup hint requires api_key_env to be set",
                    def.id
                );
            }
        }
    }
}