merlion-agent 0.1.10

Merlion Agent CLI
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
//! Interactive first-run setup wizard.
//!
//! Walks the user through picking a provider, a default model, an API key,
//! and an optional system prompt — then writes `~/.merlion/config.yaml` and
//! (if a key was supplied) appends to `~/.merlion/.env`. Re-running is
//! safe: existing values are surfaced as defaults so the user can ack or
//! tweak them.
//!
//! Mirrors the UX of hermes's `hermes setup`.

use std::fs::OpenOptions;
use std::io::Write as _;

use anyhow::{Context, Result};
use dialoguer::console::style;
use dialoguer::{theme::ColorfulTheme, Input, Password, Select};
use merlion_config::{ensure_home, Config, ModelConfig};

/// A catalog entry for one provider preset. Drives the interactive
/// `merlion model` and `merlion setup` wizards: friendly label for the
/// provider picker, env-var name for the API-key prompt, and a curated
/// list of popular models for the model picker.
///
/// `prefix` must match a branch in
/// [`merlion_config::Config::resolve_provider`] — the
/// `catalog_prefixes_match_resolver` test enforces this.
pub struct ProviderEntry {
    pub prefix: &'static str,
    pub label: &'static str,
    /// Default API-key env var. Kept in the catalog for documentation +
    /// for `catalog_prefixes_match_resolver` to assert it matches what
    /// `resolve_provider` would compute; live wizard code reads the env
    /// var name via `cfg.resolve_provider()` so user overrides of
    /// `model.api_key_env` still take effect.
    #[allow(dead_code)]
    pub api_key_env: &'static str,
    pub models: &'static [&'static str],
}

impl ProviderEntry {
    pub fn default_model(&self) -> &'static str {
        self.models
            .first()
            .copied()
            .expect("each ProviderEntry must list at least one model")
    }
}

/// Provider + model catalog. Ordered with the entries most useful for a
/// Claude-Code-style coding workflow first (Anthropic, OpenAI, OpenRouter,
/// Gemini), then the rest grouped roughly by ecosystem.
///
/// Curated as of Jan 2026 — model lists will go stale; users can always
/// pick "Enter custom model name…" in the wizard, and `merlion model
/// <provider:model>` accepts any string.
pub const CATALOG: &[ProviderEntry] = &[
    ProviderEntry {
        prefix: "anthropic",
        label: "Anthropic (Claude — direct API)",
        api_key_env: "ANTHROPIC_API_KEY",
        // First entry is the wizard default — picked claude-sonnet-4 as a
        // balanced cost/quality starting point; users wanting Opus or
        // Haiku can pick from the list below.
        models: &[
            "claude-sonnet-4",
            "claude-opus-4-7",
            "claude-sonnet-4-6",
            "claude-haiku-4-5",
            "claude-opus-4",
        ],
    },
    ProviderEntry {
        prefix: "codex",
        label: "OpenAI Codex (ChatGPT subscription — `codex` CLI shell-out)",
        // Codex auth is held in ~/.codex/auth.json by `codex login`, not
        // in an env var. The catalog entry's api_key_env field is kept
        // for documentation only.
        api_key_env: "(codex login / ~/.codex/auth.json)",
        // Models available to ChatGPT subscription accounts via the
        // codex CLI. `gpt-5-codex` / `gpt-5` (no version suffix) are
        // gated to API-billed accounts; the codex-only IDs are listed
        // here. The "Enter custom model name…" escape hatch covers
        // anything new.
        models: &[
            "gpt-5.5",
            "gpt-5.4",
            "gpt-5.4-mini",
            "gpt-5.3-codex",
            "gpt-5.3-codex-spark",
            "gpt-5.2",
        ],
    },
    ProviderEntry {
        prefix: "openai",
        label: "OpenAI (gpt-5 family, gpt-4o, o1 reasoning)",
        api_key_env: "OPENAI_API_KEY",
        // First entry is the wizard default. gpt-5.5 is the current
        // OpenAI flagship as of 2026-05; gpt-4o-mini remains the
        // cheap-and-fast fallback at the bottom.
        models: &[
            "gpt-5.5",
            "gpt-5.4",
            "gpt-5.4-mini",
            "gpt-5.3-codex",
            "gpt-5.3-codex-spark",
            "gpt-5.2",
            "gpt-4o",
            "gpt-4o-mini",
            "o1-preview",
            "o1-mini",
        ],
    },
    ProviderEntry {
        prefix: "openrouter",
        label: "OpenRouter (100+ models, pay-per-use)",
        api_key_env: "OPENROUTER_API_KEY",
        models: &[
            "anthropic/claude-sonnet-4",
            "anthropic/claude-opus-4",
            "openai/gpt-5.5",
            "openai/gpt-5.4-mini",
            "openai/gpt-4o",
            "google/gemini-2.0-flash",
            "meta-llama/llama-3.3-70b-instruct",
        ],
    },
    ProviderEntry {
        prefix: "gemini",
        label: "Google AI Studio (Gemini — direct API)",
        api_key_env: "GEMINI_API_KEY",
        models: &[
            "gemini-2.0-flash",
            "gemini-1.5-pro",
            "gemini-1.5-flash",
            "gemini-2.0-flash-thinking-exp",
        ],
    },
    ProviderEntry {
        prefix: "groq",
        label: "Groq (LPU inference — fast Llama, Mixtral)",
        api_key_env: "GROQ_API_KEY",
        models: &[
            "llama-3.3-70b-versatile",
            "llama-3.1-8b-instant",
            "mixtral-8x7b-32768",
            "deepseek-r1-distill-llama-70b",
        ],
    },
    ProviderEntry {
        prefix: "deepseek",
        label: "DeepSeek (V3, R1, coder — direct API)",
        api_key_env: "DEEPSEEK_API_KEY",
        models: &["deepseek-chat", "deepseek-coder", "deepseek-reasoner"],
    },
    ProviderEntry {
        prefix: "moonshot",
        label: "Moonshot (Kimi K2 — global API)",
        api_key_env: "MOONSHOT_API_KEY",
        models: &[
            "kimi-k2-0905-preview",
            "moonshot-v1-128k",
            "moonshot-v1-32k",
            "moonshot-v1-8k",
        ],
    },
    ProviderEntry {
        prefix: "minimax",
        label: "MiniMax (M2 / Text-01 — global API)",
        api_key_env: "MINIMAX_API_KEY",
        models: &["MiniMax-M2", "MiniMax-Text-01"],
    },
    ProviderEntry {
        prefix: "zai",
        label: "Z.AI / GLM (Zhipu — direct API)",
        api_key_env: "ZAI_API_KEY",
        models: &["glm-4.6", "glm-4-air", "glm-4-flash"],
    },
    ProviderEntry {
        prefix: "nous",
        label: "Nous Research (Hermes models)",
        api_key_env: "NOUS_API_KEY",
        models: &["Hermes-4-405B", "Hermes-3-70B"],
    },
    ProviderEntry {
        prefix: "novita",
        label: "NovitaAI (open models, GPU cloud)",
        api_key_env: "NOVITA_API_KEY",
        models: &[
            "meta-llama/llama-3.3-70b-instruct",
            "meta-llama/llama-3.1-70b-instruct",
            "qwen/qwen-2.5-72b-instruct",
        ],
    },
    ProviderEntry {
        prefix: "bedrock",
        label: "AWS Bedrock (Claude on AWS, SigV4)",
        api_key_env: "AWS_ACCESS_KEY_ID",
        models: &[
            "anthropic.claude-3-5-sonnet-20241022-v2:0",
            "anthropic.claude-opus-4-20250514-v1:0",
            "anthropic.claude-3-5-haiku-20241022-v1:0",
        ],
    },
    ProviderEntry {
        prefix: "vertex",
        label: "Google Vertex AI (Gemini via gcloud)",
        api_key_env: "GOOGLE_CLOUD_PROJECT",
        models: &["gemini-2.0-flash", "gemini-1.5-pro", "gemini-1.5-flash"],
    },
];

/// Look up the catalog entry for a provider prefix (e.g. "anthropic").
/// Returns `None` for unknown prefixes — callers should fall back to
/// treating it as an unknown provider rather than panicking.
pub fn catalog_entry(prefix: &str) -> Option<&'static ProviderEntry> {
    CATALOG.iter().find(|p| p.prefix == prefix)
}

/// Flat prefix list — exposed for tests that want to iterate every known
/// provider without going through the catalog. Not used by the wizard
/// itself anymore (the catalog drives the order there).
#[cfg(test)]
const PROVIDERS: &[&str] = &[
    "anthropic",
    "openai",
    "openrouter",
    "gemini",
    "groq",
    "deepseek",
    "moonshot",
    "minimax",
    "zai",
    "nous",
    "novita",
    "bedrock",
    "vertex",
    "codex",
];

#[cfg(test)]
fn default_model_for(provider: &str) -> &'static str {
    catalog_entry(provider)
        .map(|p| p.default_model())
        .unwrap_or("gpt-5.5")
}

/// Which sections to run. Mirrors `hermes setup [model|gateway|agent]`:
/// each variant runs just that section's prompts. `Full` runs every
/// section, showing the welcome banner and Configuration Location panel
/// up front.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Section {
    Full,
    Model,
    Gateway,
    Agent,
}

/// Run the interactive setup wizard.
///
/// - `section` picks which section(s) to run (matches `merlion setup
///   model|gateway|agent`).
/// - `quick = true` mirrors `hermes setup --quick`: skip prompts whose
///   underlying value is already configured (config field or env var).
///
/// Writes `~/.merlion/config.yaml` and, for keys/tokens the user
/// enters, appends to `~/.merlion/.env`. Idempotent — re-running is
/// always safe.
pub async fn run(section: Section, quick: bool) -> Result<()> {
    use std::io::IsTerminal;
    if !std::io::stdin().is_terminal() {
        return Err(anyhow::anyhow!(
            "`merlion setup` is interactive and needs a real terminal.\n\
             From a non-TTY context, edit ~/.merlion/config.yaml directly,\n\
             or use the targeted shortcuts:\n  \
             merlion model <provider:model>\n  \
             merlion config edit"
        ));
    }
    let home = ensure_home()?;
    let config_path = home.join("config.yaml");
    let env_path = home.join(".env");

    let mut cfg = if config_path.exists() {
        let text = std::fs::read_to_string(&config_path)
            .with_context(|| format!("read {}", config_path.display()))?;
        serde_yaml::from_str::<Config>(&text).unwrap_or_default()
    } else {
        Config::default()
    };

    if section == Section::Full {
        print_banner();
        print_reconfigure_preamble(config_path.exists(), quick);
        print_config_location(&config_path, &env_path, &home);
    }

    let mut wrote_config = false;

    if matches!(section, Section::Full | Section::Model)
        && section_inference_provider(&mut cfg, &env_path, quick)?
    {
        wrote_config = true;
    }
    if matches!(section, Section::Full | Section::Gateway) {
        section_gateway(&env_path, quick)?;
    }
    if matches!(section, Section::Full | Section::Agent) && section_agent(&mut cfg, quick)? {
        wrote_config = true;
    }

    if wrote_config {
        let written = merlion_config::save(&cfg)?;
        println!();
        println!(
            "{} {}",
            style("").green().bold(),
            style(format!("Wrote {}", written.display())).bold()
        );
    }

    if section == Section::Full {
        print_next_steps();
    }

    Ok(())
}

// ─── Visual helpers ────────────────────────────────────────────────────

fn print_banner() {
    let title = "Merlion Agent Setup Wizard";
    let inner_width = title.chars().count() + 6;
    let horizontal: String = "".repeat(inner_width);
    println!();
    println!("  {}", style(format!("{horizontal}")).magenta());
    println!(
        "  {} {}   {}   {}",
        style("").magenta(),
        style("").magenta(),
        style(title).bold().magenta(),
        style("").magenta(),
    );
    println!("  {}", style(format!("{horizontal}")).magenta());
    println!();
    println!(
        "  {}",
        style("Let's configure your Merlion Agent installation.").dim()
    );
    println!("  {}", style("Press Ctrl+C at any time to exit.").dim());
    println!();
}

fn print_reconfigure_preamble(already_configured: bool, quick: bool) {
    section_header("Reconfigure");
    if already_configured {
        println!(
            "  {} {}",
            style("").green().bold(),
            style("You already have Merlion configured.").bold()
        );
        if quick {
            println!(
                "  {}",
                style("--quick mode — only prompting for missing values.").dim()
            );
        } else {
            println!(
                "  {}",
                style("Each prompt shows the current value. Press Enter to keep it,").dim()
            );
            println!("  {}", style("or type a new value to change it.").dim());
        }
    } else {
        println!(
            "  {}",
            style("First-time setup — let's walk through every section.").dim()
        );
    }
    println!();
    println!(
        "  {} {}",
        style("Tip:").dim(),
        style("jump to a section with 'merlion setup model|gateway|agent',").dim()
    );
    println!(
        "       {}",
        style("or fill only missing items with --quick.").dim()
    );
    println!();
}

fn print_config_location(
    config_path: &std::path::Path,
    env_path: &std::path::Path,
    home: &std::path::Path,
) {
    section_header("Configuration Location");
    println!(
        "  {} {}",
        style("Config file: ").cyan(),
        config_path.display()
    );
    println!("  {} {}", style("Secrets file:").cyan(), env_path.display());
    println!("  {} {}", style("Data folder: ").cyan(), home.display());
    println!();
    println!(
        "  {}",
        style("You can edit these files directly or use 'merlion config edit'.").dim()
    );
    println!();
}

fn section_header(title: &str) {
    println!(
        "{} {}",
        style("").cyan().bold(),
        style(title).cyan().bold()
    );
}

fn print_next_steps() {
    println!();
    section_header("Next steps");
    println!(
        "  {} verify config + credentials",
        style("merlion doctor").bold()
    );
    println!("  {}            start chatting", style("merlion").bold());
}

// ─── Inference Provider ────────────────────────────────────────────────

/// Returns `true` if `cfg.model` was changed and the config must be saved.
fn section_inference_provider(
    cfg: &mut Config,
    env_path: &std::path::Path,
    quick: bool,
) -> Result<bool> {
    println!();
    section_header("Inference Provider");

    let (current_provider, current_model) = match cfg.model.id.split_once(':') {
        Some((p, m)) => (p.to_string(), m.to_string()),
        None => (String::new(), cfg.model.id.clone()),
    };
    let is_codex = current_provider == "codex";

    let resolved_key_env = cfg
        .resolve_provider()
        .ok()
        .map(|p| p.api_key_env)
        .unwrap_or_else(|| "OPENAI_API_KEY".to_string());
    let creds_ok = if is_codex {
        codex_auth_exists()
    } else {
        std::env::var(&resolved_key_env)
            .ok()
            .filter(|v| !v.is_empty())
            .is_some()
    };

    let pretty_provider = catalog_entry(&current_provider)
        .map(|e| e.label.to_string())
        .unwrap_or_else(|| current_provider.clone());

    println!(
        "  {} {}",
        style("Current model:   ").dim(),
        style(&cfg.model.id).bold()
    );
    println!("  {} {}", style("Active provider: ").dim(), pretty_provider);
    if is_codex {
        let path_display = codex_auth_path()
            .map(|p| p.display().to_string())
            .unwrap_or_else(|| "~/.codex/auth.json".into());
        println!(
            "  {} {} {}",
            style("Codex auth:      ").dim(),
            if creds_ok {
                style("").green().bold().to_string()
            } else {
                style("missing").red().to_string()
            },
            if creds_ok {
                style(format!("({path_display})")).dim().to_string()
            } else {
                style("(run `codex login`)").dim().to_string()
            }
        );
    } else {
        println!(
            "  {} {} {}",
            style(format!("{resolved_key_env}:")).dim(),
            if creds_ok {
                style("").green().bold().to_string()
            } else {
                style("missing").red().to_string()
            },
            if creds_ok {
                style("(already set in env)").dim().to_string()
            } else {
                String::new()
            }
        );
    }
    println!();

    // --quick: if the model id is configured and creds are present, skip.
    if quick && creds_ok && !cfg.model.id.is_empty() {
        println!(
            "  {}",
            style("Skipped — model + credentials already set.").dim()
        );
        return Ok(false);
    }

    let theme = ColorfulTheme::default();

    // 3-way credential prompt when key is already set. For codex, drop
    // the "Re-enter API key" option (auth flows through `codex login`,
    // not an env var) — present a 2-choice picker instead.
    if creds_ok && !cfg.model.id.is_empty() {
        let choices: &[&str] = if is_codex {
            &[
                "Use existing model + credentials (skip)",
                "Change model / provider",
            ]
        } else {
            &[
                "Use existing model + credentials (skip)",
                "Change model / provider",
                "Re-enter API key",
            ]
        };
        let pick = Select::with_theme(&theme)
            .with_prompt("What would you like to do?")
            .items(choices)
            .default(0)
            .interact()?;
        match pick {
            0 => return Ok(false),
            1 => { /* fall through to provider+model pickers */ }
            2 => {
                prompt_and_save_key(env_path, &resolved_key_env)?;
                return Ok(false);
            }
            _ => unreachable!(),
        }
    }

    // Provider picker.
    let labels: Vec<String> = CATALOG
        .iter()
        .map(|e| {
            if e.prefix == current_provider {
                format!("{}  ← current", e.label)
            } else {
                e.label.to_string()
            }
        })
        .collect();
    let default_idx = CATALOG
        .iter()
        .position(|e| e.prefix == current_provider)
        .unwrap_or(0);
    let provider_idx = Select::with_theme(&theme)
        .with_prompt("Provider")
        .items(&labels)
        .default(default_idx)
        .interact()?;
    let entry = &CATALOG[provider_idx];

    // Model picker with "Enter custom" escape.
    const CUSTOM: &str = "Enter custom model name…";
    let mut model_items: Vec<String> = entry
        .models
        .iter()
        .map(|m| {
            if entry.prefix == current_provider && *m == current_model {
                format!("{m}  ← current")
            } else {
                (*m).to_string()
            }
        })
        .collect();
    model_items.push(CUSTOM.to_string());

    let model_default_idx = if entry.prefix == current_provider {
        entry
            .models
            .iter()
            .position(|m| *m == current_model)
            .unwrap_or(0)
    } else {
        0
    };
    let model_idx = Select::with_theme(&theme)
        .with_prompt("Model")
        .items(&model_items)
        .default(model_default_idx)
        .interact()?;
    let model = if model_idx == entry.models.len() {
        Input::with_theme(&theme)
            .with_prompt("Model name")
            .default(entry.default_model().to_string())
            .interact_text()?
    } else {
        entry.models[model_idx].to_string()
    };

    cfg.model = ModelConfig {
        id: format!("{}:{}", entry.prefix, model),
        base_url: cfg.model.base_url.clone(),
        api_key_env: cfg.model.api_key_env.clone(),
        temperature: cfg.model.temperature,
        max_tokens: cfg.model.max_tokens,
    };

    // Credentials for the (possibly new) provider.
    if entry.prefix == "codex" {
        if codex_auth_exists() {
            println!(
                "  {} {}",
                style("").green().bold(),
                style("Codex auth detected at ~/.codex/auth.json.").dim()
            );
        } else {
            println!(
                "  {} {}",
                style("·").dim(),
                style("Run `codex login` before using merlion with the codex provider.").dim()
            );
        }
        return Ok(true);
    }
    let new_key_env = cfg.resolve_provider()?.api_key_env;
    if std::env::var(&new_key_env)
        .ok()
        .filter(|v| !v.is_empty())
        .is_none()
    {
        prompt_and_save_key(env_path, &new_key_env)?;
    } else {
        println!(
            "  {} {} {}",
            style("").green().bold(),
            style(&new_key_env).bold(),
            style("already set in env.").dim()
        );
    }

    Ok(true)
}

// ─── Gateway ───────────────────────────────────────────────────────────

fn section_gateway(env_path: &std::path::Path, quick: bool) -> Result<()> {
    println!();
    section_header("Messaging Gateway (optional)");
    println!(
        "  {}",
        style("Talk to merlion from Telegram / Discord / Slack. Skip if you don't").dim()
    );
    println!(
        "  {}",
        style("need it — you can always configure later with 'merlion setup gateway'.").dim()
    );
    println!();

    let theme = ColorfulTheme::default();

    let platforms = [
        (
            "Telegram",
            "TELEGRAM_BOT_TOKEN",
            "MERLION_GATEWAY_ALLOW_TELEGRAM",
            "BotFather token (e.g. 1234567890:AAH...)",
            "your Telegram numeric user id (comma-separated for multiple)",
        ),
        (
            "Discord",
            "DISCORD_BOT_TOKEN",
            "MERLION_GATEWAY_ALLOW_DISCORD",
            "Discord bot token from developer portal",
            "your Discord user id",
        ),
        (
            "Slack",
            "SLACK_APP_TOKEN",
            "MERLION_GATEWAY_ALLOW_SLACK",
            "Slack app-level token (xapp-...)",
            "your Slack user id (Uxxx)",
        ),
    ];

    for (name, token_var, allow_var, token_hint, allow_hint) in platforms {
        let token_set = std::env::var(token_var)
            .ok()
            .filter(|v| !v.is_empty())
            .is_some();
        let allow_set = std::env::var(allow_var)
            .ok()
            .filter(|v| !v.is_empty())
            .is_some();

        // Slack also needs SLACK_BOT_TOKEN; surface it briefly without
        // making the loop hairy.
        let slack_bot_ok = name != "Slack"
            || std::env::var("SLACK_BOT_TOKEN")
                .ok()
                .filter(|v| !v.is_empty())
                .is_some();

        let status_mark = if token_set && allow_set && slack_bot_ok {
            style("").green().bold()
        } else if token_set || allow_set {
            style("").yellow().bold()
        } else {
            style("·").dim().bold()
        };
        println!("  {} {}", status_mark, style(name).bold());
        println!(
            "      {} {}",
            style(format!("{token_var}:")).dim(),
            if token_set { "set" } else { "missing" }
        );
        if name == "Slack" {
            println!(
                "      {} {}",
                style("SLACK_BOT_TOKEN:").dim(),
                if slack_bot_ok { "set" } else { "missing" }
            );
        }
        println!(
            "      {} {}",
            style(format!("{allow_var}:")).dim(),
            if allow_set { "set" } else { "missing" }
        );

        if quick && token_set && allow_set && slack_bot_ok {
            continue;
        }

        let configure = dialoguer::Confirm::with_theme(&theme)
            .with_prompt(format!("  Configure {name}?"))
            .default(false)
            .interact()?;
        if !configure {
            continue;
        }

        if !token_set {
            let token: String = Password::with_theme(&theme)
                .with_prompt(format!("  {token_var}{token_hint}"))
                .allow_empty_password(true)
                .interact()?;
            if !token.trim().is_empty() {
                append_env_line(env_path, token_var, token.trim())?;
                println!(
                    "    {} {}",
                    style("").green(),
                    style(format!("Saved {token_var}")).dim()
                );
            }
        }

        if name == "Slack" && !slack_bot_ok {
            let bot: String = Password::with_theme(&theme)
                .with_prompt("  SLACK_BOT_TOKEN — Slack bot OAuth token (xoxb-...)")
                .allow_empty_password(true)
                .interact()?;
            if !bot.trim().is_empty() {
                append_env_line(env_path, "SLACK_BOT_TOKEN", bot.trim())?;
                println!(
                    "    {} {}",
                    style("").green(),
                    style("Saved SLACK_BOT_TOKEN").dim()
                );
            }
        }

        if !allow_set {
            let allow: String = Input::with_theme(&theme)
                .with_prompt(format!("  {allow_var}{allow_hint}"))
                .allow_empty(true)
                .interact_text()?;
            if !allow.trim().is_empty() {
                append_env_line(env_path, allow_var, allow.trim())?;
                println!(
                    "    {} {}",
                    style("").green(),
                    style(format!("Saved {allow_var}")).dim()
                );
            }
        }
    }

    Ok(())
}

// ─── Agent ─────────────────────────────────────────────────────────────

/// Returns `true` if the system prompt was edited.
fn section_agent(cfg: &mut Config, quick: bool) -> Result<bool> {
    println!();
    section_header("Agent");

    let sys_current = cfg.system_prompt.clone().unwrap_or_default();
    println!(
        "  {} {}",
        style("System prompt:  ").dim(),
        if sys_current.is_empty() {
            style("(none)").dim().to_string()
        } else {
            let oneline = sys_current.replace('\n', " ");
            let preview = if oneline.chars().count() > 70 {
                let truncated: String = oneline.chars().take(67).collect();
                format!("{truncated}...")
            } else {
                oneline
            };
            preview
        }
    );
    println!(
        "  {} {}",
        style("Max iterations: ").dim(),
        cfg.max_iterations
    );

    if quick && !sys_current.is_empty() {
        println!();
        println!("  {}", style("Skipped — system prompt already set.").dim());
        return Ok(false);
    }
    println!();

    let theme = ColorfulTheme::default();
    let sys: String = Input::with_theme(&theme)
        .with_prompt("System prompt (optional, press Enter to keep)")
        .default(sys_current.clone())
        .allow_empty(true)
        .interact_text()?;
    let new_sys = if sys.trim().is_empty() {
        None
    } else {
        Some(sys)
    };
    if new_sys == cfg.system_prompt {
        return Ok(false);
    }
    cfg.system_prompt = new_sys;
    Ok(true)
}

/// Prompt for an API key (hidden) and append it to `.env` if entered.
/// Path to `~/.codex/auth.json`. Returns None when no home directory.
fn codex_auth_path() -> Option<std::path::PathBuf> {
    dirs::home_dir().map(|h| h.join(".codex/auth.json"))
}

/// Codex stores its OAuth token at `~/.codex/auth.json` after a successful
/// `codex login`. The setup wizard uses the file's existence as a proxy
/// for "credentials present" — codex itself will validate the token on
/// the first `codex exec` call.
fn codex_auth_exists() -> bool {
    codex_auth_path().map(|p| p.exists()).unwrap_or(false)
}

fn prompt_and_save_key(env_path: &std::path::Path, key_env: &str) -> Result<()> {
    let theme = ColorfulTheme::default();
    let prompt = format!("{key_env} (press Enter to skip and add it manually later)");
    let key: String = Password::with_theme(&theme)
        .with_prompt(prompt)
        .allow_empty_password(true)
        .interact()?;
    let trimmed = key.trim();
    if trimmed.is_empty() {
        println!(
            "  {} Add `{key_env}=...` to {} before running merlion.",
            style("·").dim(),
            env_path.display()
        );
    } else {
        append_env_line(env_path, key_env, trimmed)?;
        println!(
            "  {} {}",
            style("").green().bold(),
            style(format!("Saved {key_env} to {}", env_path.display())).dim()
        );
    }
    Ok(())
}

/// Append `KEY=VALUE` to `path`, creating the file if it doesn't exist.
/// We don't try to dedupe — `dotenvy` reads top-to-bottom and later values
/// win, so re-running the wizard correctly overrides an earlier key.
pub fn append_env_line(path: &std::path::Path, key: &str, value: &str) -> Result<()> {
    let mut f = OpenOptions::new()
        .create(true)
        .append(true)
        .open(path)
        .with_context(|| format!("open {} for append", path.display()))?;
    // Ensure the new entry starts on its own line even if the existing file
    // was missing a trailing newline.
    let needs_leading_newline = path.metadata().map(|m| m.len() > 0).unwrap_or(false)
        && !file_ends_with_newline(path).unwrap_or(true);
    if needs_leading_newline {
        writeln!(f).ok();
    }
    writeln!(f, "{key}={value}").with_context(|| format!("write {}", path.display()))?;
    Ok(())
}

fn file_ends_with_newline(path: &std::path::Path) -> Result<bool> {
    let text = std::fs::read_to_string(path)?;
    Ok(text.ends_with('\n'))
}

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

    #[test]
    fn default_model_covers_all_providers() {
        // Every provider known to `resolve_provider` should have a default
        // suggestion — otherwise the wizard would offer the unrelated
        // `gpt-4o-mini` fallback and confuse the user.
        for p in PROVIDERS {
            let m = default_model_for(p);
            assert!(!m.is_empty(), "no default model for `{p}`");
        }
    }

    #[test]
    fn catalog_prefixes_match_resolver() {
        // Every catalog prefix must round-trip through `resolve_provider`
        // with its own first model — otherwise the wizard would happily
        // write a `provider:model` id that the runtime can't construct.
        for entry in CATALOG {
            let mut cfg = Config::default();
            cfg.model.id = format!("{}:{}", entry.prefix, entry.default_model());
            cfg.model.base_url = None;
            cfg.model.api_key_env = None;
            let resolved = cfg
                .resolve_provider()
                .unwrap_or_else(|e| panic!("catalog entry `{}` failed resolve: {e}", entry.prefix));
            assert_eq!(
                resolved.api_key_env, entry.api_key_env,
                "catalog `api_key_env` for `{}` disagrees with resolver",
                entry.prefix
            );
        }
    }

    #[test]
    fn catalog_models_nonempty() {
        for entry in CATALOG {
            assert!(
                !entry.models.is_empty(),
                "catalog entry `{}` has no models",
                entry.prefix
            );
        }
    }

    #[test]
    fn catalog_entry_lookup() {
        assert!(catalog_entry("anthropic").is_some());
        assert!(catalog_entry("openai").is_some());
        assert!(catalog_entry("bogus").is_none());
    }

    #[test]
    fn default_model_specific_providers() {
        assert_eq!(default_model_for("openai"), "gpt-5.5");
        assert_eq!(default_model_for("anthropic"), "claude-sonnet-4");
        assert_eq!(default_model_for("gemini"), "gemini-2.0-flash");
        assert_eq!(default_model_for("deepseek"), "deepseek-chat");
        assert_eq!(default_model_for("groq"), "llama-3.3-70b-versatile");
        assert_eq!(default_model_for("zai"), "glm-4.6");
    }

    #[test]
    fn default_model_unknown_falls_back() {
        // Unknown provider falls back to the OpenAI default — keeps the
        // wizard's "Model" placeholder useful even for a typo'd prefix.
        assert_eq!(default_model_for("not-a-real-provider"), "gpt-5.5");
    }

    #[test]
    fn provider_list_round_trips_through_resolve() {
        // Each provider prefix in PROVIDERS should resolve cleanly through
        // `Config::resolve_provider` so the wizard never produces a config
        // the loader can't parse.
        for p in PROVIDERS {
            let cfg = Config {
                model: ModelConfig {
                    id: format!("{p}:{}", default_model_for(p)),
                    base_url: None,
                    api_key_env: None,
                    temperature: None,
                    max_tokens: None,
                },
                system_prompt: None,
                max_iterations: 32,
            };
            let resolved = cfg
                .resolve_provider()
                .unwrap_or_else(|e| panic!("provider `{p}` failed to resolve: {e}"));
            assert!(
                !resolved.api_key_env.is_empty(),
                "empty api_key_env for {p}"
            );
        }
    }

    #[test]
    fn append_env_line_creates_file_and_appends() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join(".env");

        append_env_line(&path, "FOO_API_KEY", "abc123").unwrap();
        let text = std::fs::read_to_string(&path).unwrap();
        assert_eq!(text, "FOO_API_KEY=abc123\n");

        // Re-running appends; later values win under dotenvy.
        append_env_line(&path, "FOO_API_KEY", "xyz789").unwrap();
        let text = std::fs::read_to_string(&path).unwrap();
        assert_eq!(text, "FOO_API_KEY=abc123\nFOO_API_KEY=xyz789\n");
    }

    #[test]
    fn append_env_line_adds_leading_newline_when_missing() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join(".env");
        std::fs::write(&path, "EXISTING=1").unwrap(); // no trailing newline

        append_env_line(&path, "NEW", "2").unwrap();
        let text = std::fs::read_to_string(&path).unwrap();
        assert_eq!(text, "EXISTING=1\nNEW=2\n");
    }
}

// ────────────────────────────────────────────────────────────────────────────
// WIRING SPEC — main.rs changes required to expose this wizard
// ────────────────────────────────────────────────────────────────────────────
//
// This module is intentionally not wired into `main.rs` by this patch. To
// activate it, apply the following four edits to
// `crates/merlion-cli/src/main.rs`:
//
// 1) Declare the module alongside `mod approver; mod tui;`:
//
//        mod setup;
//
// 2) Add a `Setup` variant to the `Command` enum (no fields, no args):
//
//        /// Interactive first-run setup wizard. Writes ~/.merlion/config.yaml
//        /// and ~/.merlion/.env after asking for provider, model, API key,
//        /// and an optional system prompt.
//        Setup,
//
// 3) Add a dispatch arm inside the `match cli.command.unwrap_or(...)` block,
//    next to `Command::Doctor => doctor(cfg),`:
//
//        Command::Setup => setup::run().await,
//
// 4) No new `use` lines are required at the top of `main.rs` — `setup::run`
//    is referenced through the `setup` module path and returns
//    `anyhow::Result<()>`, matching the other async dispatch arms.
//
// After those edits, `merlion setup` runs the wizard. The wizard reads any
// existing `~/.merlion/config.yaml` as defaults, so it is safe to re-run.