collet 0.1.1

Relentless agentic coding orchestrator with zero-drop agent loops
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
use super::agents::parse_agent_md;
use super::agents::update_agent_md_frontmatter_full;
use super::defaults::{write_config, write_config_commented};
use super::loader::load_config_file;
use super::paths::{collet_home, config_file_path, ensure_config_dir, logs_dir};
use super::secrets::{save_encrypted_key, save_web_credentials};
use super::types::{ConfigFile, ProviderEntry};
use super::wizard::presets::{
    CLI_PRESETS, cli_preset_to_provider, find_cli_preset, scan_cli_presets,
};
use super::wizard::{compute_optimal_assignments, wizard_style};
use crate::common::{AgentError, Result};
use std::path::PathBuf;

/// `collet secure [--web]` — encrypt and save credentials.
pub fn cmd_secure(args: &[String]) -> Result<()> {
    let is_web = args.iter().any(|a| a == "--web");

    if is_web {
        // Web credentials
        eprintln!("Web username (default: collet):");
        let mut username = String::new();
        std::io::stdin().read_line(&mut username)?;
        let username = username.trim();
        let username = if username.is_empty() {
            None
        } else {
            Some(username.to_string())
        };

        let password = rpassword::prompt_password("Web password: ")
            .map_err(|e| AgentError::Config(format!("Failed to read password: {e}")))?;
        let password = password.trim();
        if password.is_empty() {
            return Err(AgentError::Config("Empty password. Aborted.".to_string()));
        }

        save_web_credentials(username, password)?;
    } else {
        let key = rpassword::prompt_password("Paste your API key: ")
            .map_err(|e| AgentError::Config(format!("Failed to read key: {e}")))?;
        let key = key.trim();
        if key.is_empty() {
            return Err(AgentError::Config("Empty key. Aborted.".to_string()));
        }
        save_encrypted_key(key)?;
    }
    Ok(())
}

/// `collet unsecure [--web]` — remove encrypted credentials.
pub fn cmd_unsecure(args: &[String]) -> Result<()> {
    let is_web = args.iter().any(|a| a == "--web");

    let dir = ensure_config_dir()?;
    let path = dir.join("config.toml");
    if !path.exists() {
        eprintln!("ℹ️  No config file found.");
        return Ok(());
    }

    let content = std::fs::read_to_string(&path)?;
    let mut cf = toml::from_str::<ConfigFile>(&content).unwrap_or_default();

    if is_web {
        cf.web.username = None;
        cf.web.password_enc = None;
        eprintln!("✅ Web credentials removed.");
    } else {
        cf.api.api_key_enc = None;
        eprintln!("✅ API key removed.");
    }

    let toml_str = toml::to_string_pretty(&cf)
        .map_err(|e| AgentError::Config(format!("Failed to serialize config: {e}")))?;
    std::fs::write(&path, toml_str).map_err(|e| {
        AgentError::Config(format!("Failed to write config {}: {e}", path.display()))
    })?;

    Ok(())
}

/// `collet status` — show resolved config.
pub fn cmd_status() -> Result<()> {
    use super::types::Config;
    let path = config_file_path();
    let file_exists = path.exists();

    eprintln!("collet status");
    eprintln!("─────────────────────────────");
    eprintln!(
        "config: {} {}",
        path.display(),
        if file_exists {
            ""
        } else {
            "❌ (run: collet setup)"
        }
    );

    match Config::load() {
        Ok(c) => {
            let masked_key = if c.api_key.len() > 8 {
                format!(
                    "{}...{}",
                    &c.api_key[..4],
                    &c.api_key[c.api_key.len() - 4..]
                )
            } else {
                "***".into()
            };
            eprintln!();
            eprintln!("[api]");
            eprintln!("  key:        {masked_key}");
            eprintln!("  base_url:   {}", c.base_url);
            eprintln!("  model:      {}", c.model);
            eprintln!("  max_tokens: {}", c.max_tokens);
            eprintln!();
            eprintln!("[agent]");
            eprintln!("  timeout:    {}s", c.tool_timeout_secs);
            eprintln!("  task_timeout: {}s", c.task_timeout_secs);
            eprintln!("  max_iter:   {}", c.max_iterations);
            eprintln!("  max_cont:   {}", c.max_continuations);
            eprintln!("  breaker:    {}", c.circuit_breaker_threshold);
            eprintln!();
            eprintln!("[context]");
            eprintln!("  max_tokens: {}", c.context_max_tokens);
            eprintln!("  compact_at: {:.0}%", c.compaction_threshold * 100.0);
            eprintln!();
            eprintln!("[hooks]");
            eprintln!("  commit:     {}", c.auto_commit);
            eprintln!("  lint:       {}", c.lint_cmd.as_deref().unwrap_or("-"));
            eprintln!("  test:       {}", c.test_cmd.as_deref().unwrap_or("-"));
            eprintln!();
            eprintln!("[ui]");
            eprintln!("  theme:      {}", c.theme);
            eprintln!();
            eprintln!("[collaboration]");
            eprintln!("  mode:       {}", c.collaboration.mode);
            eprintln!("  max_agents: {}", c.collaboration.max_agents);
            eprintln!("  strategy:   {:?}", c.collaboration.strategy);
            eprintln!("  consensus:  {}", c.collaboration.require_consensus);
            eprintln!("  conflicts:  {:?}", c.collaboration.conflict_resolution);
            eprintln!();
            eprintln!("[web]");
            eprintln!("  host:       {}", c.web.host);
            eprintln!("  port:       {}", c.web.port);
            eprintln!("  username:   {}", c.web.username);
            eprintln!(
                "  password:   {}",
                if c.web.password.is_some() {
                    "***"
                } else {
                    "(none)"
                }
            );
            eprintln!();
            eprintln!("[paths]");
            eprintln!("  home:       {}", collet_home(None).display());
            eprintln!("  logs:       {}", logs_dir().display());
        }
        Err(e) => {
            eprintln!();
            eprintln!("{e}");
            eprintln!();
            eprintln!("Fix: collet setup  (or: collet secure)");
        }
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Subcommand: provider add / remove / list / use
// ---------------------------------------------------------------------------

// ── collet clis ──────────────────────────────────────────────────────────────

/// `collet provider <sub>` — manage provider credentials.
pub fn cmd_clis(args: &[String]) -> Result<()> {
    match args.first().map(|s| s.as_str()) {
        Some("help") | Some("--help") | Some("-h") => {
            print_clis_usage();
            Ok(())
        }
        Some("add") => cmd_clis_add(&args[1..]),
        Some("remove") => cmd_clis_remove(&args[1..]),
        Some("auto") => cmd_clis_auto(),
        Some("list") | None => cmd_clis_list(),
        _ => {
            print_clis_usage();
            Ok(())
        }
    }
}

fn print_clis_usage() {
    eprintln!("collet clis — Manage CLI coding agent providers");
    eprintln!();
    eprintln!("USAGE:");
    eprintln!("  collet clis [COMMAND]");
    eprintln!();
    eprintln!("COMMANDS:");
    eprintln!("  list          Show registered CLI providers and detected binaries (default)");
    eprintln!("  add [name]    Register a CLI coding agent as a provider");
    eprintln!("  remove [name] Remove a registered CLI provider");
    eprintln!("  auto          Auto-detect and register/unregister CLI agents");
    eprintln!();
    eprintln!("DESCRIPTION:");
    eprintln!("  Manages external CLI coding agents (claude, codex, gemini, aider, etc.)");
    eprintln!("  as first-class providers. Detected agents can be used as fallback providers");
    eprintln!("  in agent definitions.");
    eprintln!();
    eprintln!("EXAMPLES:");
    eprintln!("  collet clis              List all CLI agents");
    eprintln!("  collet clis auto         Auto-detect and sync");
    eprintln!("  collet clis add claude   Register Claude Code CLI");
    eprintln!("  collet clis remove aider Remove Aider");
}

fn cmd_clis_list() -> Result<()> {
    let (_path, config) = load_config_for_edit()?;
    let detected = scan_cli_presets();

    eprintln!("\x1b[1mRegistered CLI providers:\x1b[0m");
    let cli_providers: Vec<_> = config
        .providers
        .iter()
        .filter(|p| p.cli.is_some())
        .collect();
    if cli_providers.is_empty() {
        eprintln!("  (none)");
    } else {
        for p in &cli_providers {
            let bin = p.cli.as_deref().unwrap_or("?");
            let status = if p.cli_available() {
                "\x1b[32m●\x1b[0m"
            } else {
                "\x1b[31m●\x1b[0m"
            };
            let models = if p.models.is_empty() {
                String::new()
            } else {
                format!(" ({})", p.models.join(", "))
            };
            eprintln!("  {status} {name:<20} cli={bin}{models}", name = p.name);
        }
    }

    eprintln!();
    eprintln!("\x1b[1mDetected on PATH (not yet registered):\x1b[0m");
    let registered_bins: Vec<_> = cli_providers
        .iter()
        .filter_map(|p| p.cli.as_deref())
        .collect();
    let unregistered: Vec<_> = detected
        .iter()
        .filter(|d| !registered_bins.contains(&d.binary))
        .collect();
    if unregistered.is_empty() {
        eprintln!("  (none)");
    } else {
        for d in &unregistered {
            eprintln!(
                "  \x1b[33m○\x1b[0m {name:<20} binary={binary}",
                name = d.name,
                binary = d.binary
            );
        }
    }
    eprintln!();
    eprintln!("Run \x1b[1mcollet clis auto\x1b[0m to register all detected CLIs.");
    Ok(())
}

fn cmd_clis_add(args: &[String]) -> Result<()> {
    let query = match args.first() {
        Some(q) => q.clone(),
        None => {
            eprintln!("Available CLI presets:");
            for (i, p) in CLI_PRESETS.iter().enumerate() {
                let available = std::process::Command::new("which")
                    .arg(p.binary)
                    .stdout(std::process::Stdio::null())
                    .stderr(std::process::Stdio::null())
                    .status()
                    .map(|s| s.success())
                    .unwrap_or(false);
                let mark = if available {
                    "\x1b[32m●\x1b[0m"
                } else {
                    "\x1b[31m●\x1b[0m"
                };
                eprintln!(
                    "  {mark} [{i:>2}] {label:<20} ({binary})",
                    label = p.label,
                    binary = p.binary
                );
            }
            eprint!("\nSelect preset (name or number): ");
            let mut input = String::new();
            std::io::stdin().read_line(&mut input)?;
            input.trim().to_string()
        }
    };

    // Try as number first
    let preset = if let Ok(idx) = query.parse::<usize>() {
        CLI_PRESETS.get(idx)
    } else {
        find_cli_preset(&query)
    };

    let preset = match preset {
        Some(p) => p,
        None => {
            eprintln!("Unknown CLI preset: {query}");
            return Ok(());
        }
    };

    let (path, mut config) = load_config_for_edit()?;
    if config.providers.iter().any(|p| p.name == preset.name) {
        eprintln!("Provider '{}' already registered.", preset.name);
        return Ok(());
    }

    let entry = cli_preset_to_provider(preset);
    eprintln!(
        "Adding CLI provider: {} (binary: {})",
        entry.name, preset.binary
    );
    config.providers.push(entry);
    write_config(&path, &config)?;
    eprintln!(
        "\x1b[32m✓\x1b[0m Registered. Use in agents: provider = \"{}\"",
        preset.name
    );
    Ok(())
}

fn cmd_clis_remove(args: &[String]) -> Result<()> {
    let (path, mut config) = load_config_for_edit()?;
    let cli_names: Vec<String> = config
        .providers
        .iter()
        .filter(|p| p.cli.is_some())
        .map(|p| p.name.clone())
        .collect();

    if cli_names.is_empty() {
        eprintln!("No CLI providers registered.");
        return Ok(());
    }

    let name = match args.first() {
        Some(n) => n.clone(),
        None => {
            eprintln!("Registered CLI providers:");
            for (i, name) in cli_names.iter().enumerate() {
                eprintln!("  [{i}] {name}");
            }
            eprint!("\nSelect to remove (name or number): ");
            let mut input = String::new();
            std::io::stdin().read_line(&mut input)?;
            let input = input.trim().to_string();
            if let Ok(idx) = input.parse::<usize>() {
                cli_names.get(idx).cloned().unwrap_or(input)
            } else {
                input
            }
        }
    };

    let before = config.providers.len();
    config.providers.retain(|p| p.name != name);
    if config.providers.len() < before {
        write_config(&path, &config)?;
        eprintln!("\x1b[32m✓\x1b[0m Removed CLI provider: {name}");
    } else {
        eprintln!("Provider '{name}' not found.");
    }
    Ok(())
}

fn cmd_clis_auto() -> Result<()> {
    let (path, mut config) = load_config_for_edit()?;
    let detected = scan_cli_presets();
    let mut added = 0;
    let mut removed = 0;

    // Add detected but not yet registered
    for preset in &detected {
        if !config.providers.iter().any(|p| p.name == preset.name) {
            eprintln!(
                "  \x1b[32m+\x1b[0m Adding {name} ({binary})",
                name = preset.name,
                binary = preset.binary
            );
            config.providers.push(cli_preset_to_provider(preset));
            added += 1;
        }
    }

    // Remove registered CLI providers whose binary is no longer on PATH
    let detected_names: Vec<&str> = detected.iter().map(|d| d.name).collect();
    let to_remove: Vec<String> = config
        .providers
        .iter()
        .filter(|p: &&ProviderEntry| p.is_cli() && !detected_names.contains(&p.name.as_str()))
        .map(|p| p.name.clone())
        .collect();
    for name in &to_remove {
        eprintln!("  \x1b[31m-\x1b[0m Removing {name} (binary not found)");
        config.providers.retain(|p| p.name != *name);
        removed += 1;
    }

    if added > 0 || removed > 0 {
        write_config(&path, &config)?;
    }
    eprintln!("\x1b[32m✓\x1b[0m {added} added, {removed} removed.");
    Ok(())
}

// ── collet provider ─────────────────────────────────────────────────────────

pub fn cmd_provider(args: &[String]) -> Result<()> {
    let sub = args.first().map(|s| s.as_str());
    match sub {
        Some("help") | Some("--help") | Some("-h") => {
            print_provider_usage();
            Ok(())
        }
        Some("add") => cmd_provider_add(&args[1..]),
        Some("remove") => cmd_provider_remove(),
        Some("list") => cmd_provider_list(),
        Some("use") => cmd_provider_use(&args[1..]),
        Some("models") => cmd_provider_models(&args[1..]),
        _ => {
            print_provider_usage();
            Ok(())
        }
    }
}

fn print_provider_usage() {
    eprintln!("collet provider — Manage LLM API providers");
    eprintln!();
    eprintln!("USAGE:");
    eprintln!("  collet provider <COMMAND>");
    eprintln!();
    eprintln!("COMMANDS:");
    eprintln!("  add [name]    Register a provider (or add model to existing)");
    eprintln!("  remove        Remove a registered provider");
    eprintln!("  list          Show registered providers and models");
    eprintln!("  use [name]    Switch the active provider");
    eprintln!("  models        Add/remove models for a provider");
    eprintln!();
    eprintln!("DESCRIPTION:");
    eprintln!("  Manages LLM API provider connections. Providers define the base URL,");
    eprintln!("  API key, and available models for each backend (OpenAI, Anthropic,");
    eprintln!("  Google, local endpoints, etc.).");
    eprintln!();
    eprintln!("EXAMPLES:");
    eprintln!("  collet provider add anthropic");
    eprintln!("  collet provider list");
    eprintln!("  collet provider use openai");
    eprintln!("  collet provider models openai");
}

/// Find a ProviderPreset by case-insensitive partial match on label.
fn find_preset(query: &str) -> Option<&'static super::wizard::presets::ProviderPreset> {
    super::wizard::presets::find_provider_preset(query)
}

/// Load config file for modification (returns default if missing).
pub(super) fn load_config_for_edit() -> Result<(PathBuf, ConfigFile)> {
    let dir = ensure_config_dir()?;
    let path = dir.join("config.toml");
    let cf = if path.exists() {
        let content = std::fs::read_to_string(&path)?;
        toml::from_str::<ConfigFile>(&content).unwrap_or_default()
    } else {
        ConfigFile::default()
    };
    Ok((path, cf))
}

/// Inputs collected from the provider-add wizard.
///
/// Module-private; lives only inside this file. Replaces a 6-tuple return
/// type whose positional API was both clippy::type_complexity-noisy and
/// hiding two dead values: a plain `api_key` (only the encrypted form is
/// ever consumed downstream) and a duplicate `model` field equal to
/// `models[0]`. Both have been removed.
struct ProviderInput {
    /// Display label of the selected preset (used as the provider entry name).
    preset_label: String,
    /// Base URL the user picked or accepted from the preset.
    base_url: String,
    /// All models the user selected. Always non-empty: the wizard inserts
    /// the preset's `default_model` as a fallback when nothing is chosen.
    /// The first element is treated as the active model.
    models: Vec<String>,
    /// Encrypted API key, or `None` for keyless local providers
    /// (Ollama, LM Studio).
    encrypted_api_key: Option<String>,
}

/// Gather provider inputs: preset selection, API key, base URL, models, and encryption.
fn gather_provider_inputs(args: &[String]) -> Result<ProviderInput> {
    use super::secrets::{decrypt_key, encrypt_key};
    use super::wizard::presets::PROVIDERS;
    use super::wizard::ui::{
        prompt_api_key, prompt_with_default, wizard_confirm, wizard_multi_select, wizard_select,
    };
    use wizard_style as s;

    let preset = if let Some(name) = args.first() {
        find_preset(name).ok_or_else(|| {
            AgentError::Config(format!(
                "Unknown provider '{name}'. Run `collet provider add` to see available providers."
            ))
        })?
    } else {
        let labels: Vec<&str> = PROVIDERS.iter().map(|p| p.label).collect();
        let idx = wizard_select("Select provider", &labels, 0)?;
        &PROVIDERS[idx]
    };

    eprintln!(
        "\n  {}{}Adding provider: {}{}\n",
        s::BOLD,
        s::CYAN,
        preset.label,
        s::RESET
    );

    // API key: skip for local providers (Ollama, LM Studio)
    let api_key = if !preset.api_key_required {
        eprintln!(
            "  {}No API key required (local server).{}",
            s::DIM,
            s::RESET
        );
        String::new()
    } else {
        let env_key = if !preset.env_key_hint.is_empty() {
            std::env::var(preset.env_key_hint)
                .ok()
                .filter(|v| !v.is_empty())
        } else {
            None
        };

        if let Some(ref env_val) = env_key {
            let masked = if env_val.len() > 8 {
                let start: String = env_val.chars().take(4).collect();
                let end: String = env_val
                    .chars()
                    .rev()
                    .take(4)
                    .collect::<Vec<_>>()
                    .into_iter()
                    .rev()
                    .collect();
                format!("{start}...{end}")
            } else {
                "***".to_string()
            };
            eprintln!(
                "  {}Found {} in environment: {}{}",
                s::GREEN,
                preset.env_key_hint,
                masked,
                s::RESET
            );
            if wizard_confirm(
                &format!("Use {} from environment?", preset.env_key_hint),
                true,
            )? {
                env_val.clone()
            } else {
                prompt_api_key()?
            }
        } else {
            if !preset.env_key_hint.is_empty() {
                eprintln!(
                    "  {}Hint: set {} env var for auto-detection{}",
                    s::DIM,
                    preset.env_key_hint,
                    s::RESET
                );
            }
            prompt_api_key()?
        }
    };

    let base_url = if preset.base_url.is_empty() {
        prompt_with_default(
            "Base URL",
            "https://api.example.com/v1",
            &mut std::io::stdin().lock(),
        )?
    } else {
        prompt_with_default("Base URL", preset.base_url, &mut std::io::stdin().lock())?
    };

    // Model — multi-select from recommended list + custom comma-separated input
    let selected_models: Vec<String> = if !preset.recommended_models.is_empty() {
        let mut choices: Vec<&str> = preset.recommended_models.to_vec();
        choices.push("(custom)");
        let configured: Vec<bool> = choices.iter().map(|&m| m == preset.default_model).collect();
        let indices = wizard_multi_select("Models (Space to toggle)", &choices, &configured)?;
        let mut models = Vec::new();
        let custom_idx = choices.len() - 1;
        for idx in &indices {
            if *idx == custom_idx {
                // Custom: comma-separated input
                let input = prompt_with_default(
                    "Custom models (comma-separated)",
                    "",
                    &mut std::io::stdin().lock(),
                )?;
                for m in input.split(',') {
                    let m = m.trim();
                    if !m.is_empty() {
                        models.push(m.to_string());
                    }
                }
            } else {
                models.push(choices[*idx].to_string());
            }
        }
        if models.is_empty() {
            // Fallback: use default model
            models.push(preset.default_model.to_string());
        }
        models
    } else {
        let default_model = if preset.default_model.is_empty() {
            "gpt-4o"
        } else {
            preset.default_model
        };
        let input = prompt_with_default(
            "Models (comma-separated)",
            default_model,
            &mut std::io::stdin().lock(),
        )?;
        input
            .split(',')
            .map(|m| m.trim().to_string())
            .filter(|m| !m.is_empty())
            .collect()
    };
    // Encrypt API key (skip for local/keyless providers).
    // The plain `api_key` lives only inside this function — used for
    // encryption + round-trip verification, then dropped.
    let encrypted_api_key = if api_key.is_empty() {
        None
    } else {
        let enc = encrypt_key(&api_key)?;
        let decrypted = decrypt_key(&enc)?;
        if decrypted != api_key {
            return Err(AgentError::Config(
                "Encryption verification failed.".to_string(),
            ));
        }
        Some(enc)
    };

    Ok(ProviderInput {
        preset_label: preset.label.to_string(),
        base_url,
        models: selected_models,
        encrypted_api_key,
    })
}

fn cmd_provider_add(args: &[String]) -> Result<()> {
    use wizard_style as s;

    let input = gather_provider_inputs(args)?;

    let (path, mut cf) = load_config_for_edit()?;

    let entry_name = input.preset_label;

    // Check if provider already exists — merge new models into it
    if let Some(existing) = cf
        .providers
        .iter_mut()
        .find(|p| p.name.eq_ignore_ascii_case(&entry_name))
    {
        let mut added = Vec::new();
        // Merge all selected models (default + extras)
        for m in input.models {
            if !existing
                .all_models()
                .iter()
                .any(|em| em.eq_ignore_ascii_case(&m))
            {
                existing.models.push(m.clone());
                added.push(m);
            }
        }
        if input.encrypted_api_key.is_some() {
            existing.api_key_enc = input.encrypted_api_key;
        }
        if added.is_empty() {
            eprintln!(
                "\n  {}{}All selected models already registered for '{}'.{}",
                s::BOLD,
                s::YELLOW,
                entry_name,
                s::RESET
            );
        } else {
            write_config_commented(&path, &cf)?;
            eprintln!(
                "\n  {}{}✅ Models added to '{}': {}{}",
                s::BOLD,
                s::GREEN,
                entry_name,
                added.join(", "),
                s::RESET
            );
        }
        return Ok(());
    }

    // Active model is the first selected (wizard guarantees ≥1).
    // Capture before moving `input.models` into the new ProviderEntry.
    let active_model = input.models.first().cloned();

    // Insert at position 0 (same behavior as setup command).
    cf.providers.insert(
        0,
        ProviderEntry {
            name: entry_name.clone(),
            base_url: input.base_url,
            api_key_enc: input.encrypted_api_key,
            models: input.models,
            cli: None,
            cli_args: Vec::new(),
            cli_yolo_args: Vec::new(),
            cli_model_env: None,
            cli_skip_model: false,
            cli_yolo_env: Vec::new(),
            cli_max_turns_flag: None,
        },
    );

    // If first provider or no active, set as active
    if cf.api.provider.is_none() || cf.providers.is_empty() {
        cf.api.provider = Some(entry_name.clone());
        cf.api.model = active_model;
        cf.api.api_key_enc = cf.providers.first().and_then(|p| p.api_key_enc.clone());
        cf.api.api_key = None;
    }

    write_config_commented(&path, &cf)?;

    eprintln!(
        "\n  {}{}✅ Provider '{}' registered.{}",
        s::BOLD,
        s::GREEN,
        entry_name,
        s::RESET
    );

    offer_agent_remap(&path, &mut cf)?;

    Ok(())
}

/// Offer cross-provider optimal agent model assignments.
///
/// When a new provider is added, display the optimal model assignments for
/// architect/code/ask agents across all available providers. Optionally
/// update agent .md files with these models.
fn offer_agent_remap(path: &std::path::Path, cf: &mut ConfigFile) -> Result<()> {
    use super::wizard::ui::wizard_confirm;
    use wizard_style as s;

    let agents_dir = collet_home(None).join("agents");
    if !agents_dir.exists() || compute_optimal_assignments(&cf.providers).is_none() {
        return Ok(());
    }

    let optimal = compute_optimal_assignments(&cf.providers).unwrap();

    eprintln!();
    eprintln!(
        "  {}Optimal agent assignments across all providers:{}",
        s::DIM,
        s::RESET
    );
    eprintln!(
        "  {}  architect → {}/{}{}",
        s::DIM,
        optimal.architect.0,
        optimal.architect.1,
        s::RESET
    );
    eprintln!(
        "  {}  code      → {}/{}{}",
        s::DIM,
        optimal.code.0,
        optimal.code.1,
        s::RESET
    );
    eprintln!(
        "  {}  ask       → {}/{}{}",
        s::DIM,
        optimal.ask.0,
        optimal.ask.1,
        s::RESET
    );
    eprintln!();

    // Seed coordinator/worker tier models only if not already set by the user.
    let coord_spec = format!("{}/{}", optimal.architect.0, optimal.architect.1);
    let worker_spec = format!("{}/{}", optimal.code.0, optimal.code.1);
    let mut seeded = false;
    if cf.collaboration.coordinator_model.is_none() {
        cf.collaboration.coordinator_model = Some(coord_spec.clone());
        seeded = true;
    }
    if cf.collaboration.worker_model.is_none() {
        cf.collaboration.worker_model = Some(worker_spec.clone());
        seeded = true;
    }
    if seeded {
        write_config_commented(path, cf)?;
    }
    if cf.collaboration.coordinator_model.as_deref() == Some(&coord_spec) {
        eprintln!(
            "  {}coordinator_model = {:?}{}",
            s::DIM,
            coord_spec,
            s::RESET
        );
    }
    if cf.collaboration.worker_model.as_deref() == Some(&worker_spec) {
        eprintln!(
            "  {}worker_model      = {:?}{}",
            s::DIM,
            worker_spec,
            s::RESET
        );
    }
    eprintln!();

    if !wizard_confirm("Update agent models now?", false)? {
        eprintln!(
            "  {}To change later: edit .collet/agents/{{code,architect,ask}}.md{}",
            s::DIM,
            s::RESET
        );
        return Ok(());
    }

    let mut updated = 0usize;
    if let Ok(entries) = std::fs::read_dir(&agents_dir) {
        let mut files: Vec<_> = entries
            .filter_map(|e| e.ok())
            .filter(|e| e.path().extension().is_some_and(|x| x == "md"))
            .collect();
        files.sort_by_key(|e| e.file_name());
        for entry in files {
            let path = entry.path();
            let agent = parse_agent_md(&path);
            let tier = agent
                .as_ref()
                .and_then(|a| a.tier.as_deref())
                .unwrap_or("medium");
            let agent_name = agent
                .as_ref()
                .map(|a| a.name.as_str())
                .or_else(|| path.file_stem().and_then(|s| s.to_str()))
                .unwrap_or("unknown");
            let (provider, mdl) = match tier {
                "heavy" => &optimal.architect,
                "light" => &optimal.ask,
                _ => &optimal.code,
            };
            update_agent_md_frontmatter_full(&path, mdl, provider, None);
            eprintln!(
                "  {}{} {} [{}] → {}/{}",
                s::GREEN,
                s::RESET,
                agent_name,
                tier,
                provider,
                mdl
            );
            updated += 1;
        }
    }
    if updated == 0 {
        eprintln!("  {}(no agent files found){}", s::DIM, s::RESET);
    }
    eprintln!();

    Ok(())
}

fn cmd_provider_remove() -> Result<()> {
    use super::wizard::ui::wizard_select;
    use wizard_style as s;

    let (path, mut cf) = load_config_for_edit()?;

    if cf.providers.is_empty() {
        eprintln!("No providers registered.");
        return Ok(());
    }

    let names: Vec<String> = cf.providers.iter().map(|p| p.name.clone()).collect();
    let labels: Vec<&str> = names.iter().map(|s| s.as_str()).collect();
    let idx = wizard_select("Remove provider", &labels, 0)?;
    let removed_name = names[idx].clone();

    cf.providers.remove(idx);

    // If removed was active, clear or reset to next available provider
    if cf.api.provider.as_deref() == Some(&removed_name) {
        if let Some(first) = cf.providers.first() {
            cf.api.provider = Some(first.name.clone());
            cf.api.model = first.models.first().cloned();
            cf.api.api_key_enc = first.api_key_enc.clone();
        } else {
            cf.api.provider = None;
            cf.api.model = None;
            cf.api.api_key_enc = None;
        }
        cf.api.api_key = None;
    }

    write_config(&path, &cf)?;

    eprintln!(
        "  {}{}✅ Provider '{}' removed.{}",
        s::BOLD,
        s::GREEN,
        removed_name,
        s::RESET
    );
    Ok(())
}

fn cmd_provider_list() -> Result<()> {
    use wizard_style as s;

    let cf = load_config_file()?;

    if cf.providers.is_empty() {
        eprintln!("No providers registered. Run: collet provider add");
        return Ok(());
    }

    let active = cf.api.provider.as_deref().unwrap_or("");

    eprintln!();
    eprintln!("  {}{}Registered providers:{}", s::BOLD, s::CYAN, s::RESET);
    eprintln!();

    for p in &cf.providers {
        let is_active = p.name.eq_ignore_ascii_case(active);
        let marker = if is_active {
            format!("{}{}", s::GREEN, s::RESET)
        } else {
            format!("{}{}", s::DIM, s::RESET)
        };
        let url_display = if p.base_url.len() > 40 {
            format!("{}", &p.base_url[..39])
        } else {
            p.base_url.clone()
        };
        eprintln!(
            "  {} {}{}{} {}({}){}",
            marker,
            s::WHITE,
            p.name,
            s::RESET,
            s::DIM,
            url_display,
            s::RESET
        );
        let all = p.all_models();
        if !all.is_empty() {
            eprintln!("      models: {}{}{}", s::DIM, all.join(", "), s::RESET);
        }
    }
    eprintln!();
    Ok(())
}

fn cmd_provider_use(args: &[String]) -> Result<()> {
    use super::wizard::ui::wizard_select;
    use wizard_style as s;

    let (path, mut cf) = load_config_for_edit()?;

    if cf.providers.is_empty() {
        eprintln!("No providers registered. Run: collet provider add");
        return Ok(());
    }

    let entry = if let Some(name) = args.first() {
        let q = name.to_lowercase();
        cf.providers
            .iter()
            .find(|p| p.name.to_lowercase() == q || p.name.to_lowercase().contains(&q))
            .cloned()
            .ok_or_else(|| {
                AgentError::Config(format!(
                    "No registered provider matches '{name}'. Run: collet provider list"
                ))
            })?
    } else {
        let names: Vec<String> = cf.providers.iter().map(|p| p.name.clone()).collect();
        let labels: Vec<&str> = names.iter().map(|s| s.as_str()).collect();
        let idx = wizard_select("Switch active provider", &labels, 0)?;
        cf.providers[idx].clone()
    };

    cf.api.provider = Some(entry.name.clone());
    let default_model = entry.all_models().into_iter().next().map(|s| s.to_string());
    cf.api.model = default_model.clone();
    cf.api.api_key_enc = entry.api_key_enc.clone();
    cf.api.api_key = None;

    write_config(&path, &cf)?;

    let model_display = default_model.as_deref().unwrap_or("(none)");
    eprintln!(
        "  {}{}✅ Active provider: {} (model: {}){}",
        s::BOLD,
        s::GREEN,
        entry.name,
        model_display,
        s::RESET
    );
    Ok(())
}

fn cmd_provider_models(args: &[String]) -> Result<()> {
    use super::wizard::ui::{prompt_with_default, wizard_select};
    use wizard_style as s;

    let (path, mut cf) = load_config_for_edit()?;

    if cf.providers.is_empty() {
        eprintln!("No providers registered. Run: collet provider add");
        return Ok(());
    }

    // Select provider
    let names: Vec<String> = cf.providers.iter().map(|p| p.name.clone()).collect();
    let labels: Vec<&str> = names.iter().map(|s| s.as_str()).collect();
    let prov_idx = if let Some(name) = args.first() {
        let q = name.to_lowercase();
        names
            .iter()
            .position(|n| n.to_lowercase() == q || n.to_lowercase().contains(&q))
            .ok_or_else(|| AgentError::Config(format!("No provider matches '{name}'")))?
    } else {
        wizard_select("Select provider", &labels, 0)?
    };

    let provider = &cf.providers[prov_idx];
    let all = provider.all_models();
    eprintln!(
        "\n  {}{}Provider: {}{}",
        s::BOLD,
        s::CYAN,
        provider.name,
        s::RESET
    );
    eprintln!(
        "  Current models: {}{}{}\n",
        s::DIM,
        all.join(", "),
        s::RESET
    );

    let action_idx = wizard_select("Action", &["Add model", "Remove model"], 0)?;

    if action_idx == 0 {
        // Add model
        let model = prompt_with_default("Model name", "", &mut std::io::stdin().lock())?;
        if model.is_empty() {
            eprintln!("  {}No model name provided.{}", s::DIM, s::RESET);
            return Ok(());
        }
        let provider = &mut cf.providers[prov_idx];
        if provider
            .all_models()
            .iter()
            .any(|m| m.eq_ignore_ascii_case(&model))
        {
            eprintln!(
                "  {}{}Model '{}' already exists.{}",
                s::BOLD,
                s::YELLOW,
                model,
                s::RESET
            );
            return Ok(());
        }
        provider.models.push(model.clone());
        write_config(&path, &cf)?;
        eprintln!(
            "  {}{}✅ Model '{}' added.{}",
            s::BOLD,
            s::GREEN,
            model,
            s::RESET
        );
    } else {
        // Remove model
        let provider = &cf.providers[prov_idx];
        let all: Vec<String> = provider
            .all_models()
            .iter()
            .map(|m| m.to_string())
            .collect();
        if all.len() <= 1 {
            eprintln!(
                "  {}Cannot remove the only model. Use 'provider remove' instead.{}",
                s::DIM,
                s::RESET
            );
            return Ok(());
        }
        let model_labels: Vec<&str> = all.iter().map(|s| s.as_str()).collect();
        let rm_idx = wizard_select("Remove model", &model_labels, 0)?;
        let removed = all[rm_idx].clone();

        let provider = &mut cf.providers[prov_idx];
        provider
            .models
            .retain(|m| !m.eq_ignore_ascii_case(&removed));
        write_config(&path, &cf)?;
        eprintln!(
            "  {}{}✅ Model '{}' removed.{}",
            s::BOLD,
            s::GREEN,
            removed,
            s::RESET
        );
    }

    Ok(())
}