Skip to main content

lean_ctx/
setup.rs

1use std::path::PathBuf;
2
3use crate::core::editor_registry::{ConfigType, EditorTarget, WriteAction, WriteOptions};
4use crate::core::portable_binary::resolve_portable_binary;
5use crate::core::setup_report::{PlatformInfo, SetupItem, SetupReport, SetupStepReport};
6use chrono::Utc;
7use std::ffi::OsString;
8
9pub fn claude_config_json_path(home: &std::path::Path) -> PathBuf {
10    crate::core::editor_registry::claude_mcp_json_path(home)
11}
12
13pub fn claude_config_dir(home: &std::path::Path) -> PathBuf {
14    crate::core::editor_registry::claude_state_dir(home)
15}
16
17pub(crate) struct EnvVarGuard {
18    key: &'static str,
19    previous: Option<OsString>,
20}
21
22impl EnvVarGuard {
23    pub(crate) fn set(key: &'static str, value: &str) -> Self {
24        let previous = std::env::var_os(key);
25        std::env::set_var(key, value);
26        Self { key, previous }
27    }
28}
29
30impl Drop for EnvVarGuard {
31    fn drop(&mut self) {
32        if let Some(previous) = &self.previous {
33            std::env::set_var(self.key, previous);
34        } else {
35            std::env::remove_var(self.key);
36        }
37    }
38}
39
40pub fn run_setup() {
41    use crate::terminal_ui;
42
43    if crate::shell::is_non_interactive() {
44        eprintln!("Non-interactive terminal detected (no TTY on stdin).");
45        eprintln!("Running in non-interactive mode (equivalent to: nebu-ctx setup --non-interactive --yes)");
46        eprintln!();
47        let opts = SetupOptions {
48            non_interactive: true,
49            yes: true,
50            fix: false,
51            json: false,
52        };
53        match run_setup_with_options(opts) {
54            Ok(report) => {
55                if !report.warnings.is_empty() {
56                    for w in &report.warnings {
57                        eprintln!("  warning: {w}");
58                    }
59                }
60            }
61            Err(e) => eprintln!("Setup error: {e}"),
62        }
63        return;
64    }
65
66    let home = match dirs::home_dir() {
67        Some(h) => h,
68        None => {
69            eprintln!("Cannot determine home directory");
70            std::process::exit(1);
71        }
72    };
73
74    let binary = resolve_portable_binary();
75
76    let home_str = home.to_string_lossy().to_string();
77
78    terminal_ui::print_setup_header();
79
80    // Step 1: Shell hook (legacy aliases + universal shell hook)
81    terminal_ui::print_step_header(1, 7, "Shell Hook");
82    crate::cli::cmd_init(&["--global".to_string()]);
83    crate::shell_hook::install_all(false);
84
85    // Step 2: Editor auto-detection + configuration
86    terminal_ui::print_step_header(2, 7, "AI Tool Detection");
87
88    let targets = crate::core::editor_registry::build_targets(&home);
89    let mut newly_configured: Vec<&str> = Vec::new();
90    let mut already_configured: Vec<&str> = Vec::new();
91    let mut not_installed: Vec<&str> = Vec::new();
92    let mut errors: Vec<&str> = Vec::new();
93
94    for target in &targets {
95        let short_path = shorten_path(&target.config_path.to_string_lossy(), &home_str);
96
97        if !target.detect_path.exists() {
98            not_installed.push(target.name);
99            continue;
100        }
101
102        match crate::core::editor_registry::write_config_with_options(
103            target,
104            &binary,
105            WriteOptions {
106                overwrite_invalid: false,
107            },
108        ) {
109            Ok(res) if res.action == WriteAction::Already => {
110                terminal_ui::print_status_ok(&format!(
111                    "{:<20} \x1b[2m{short_path}\x1b[0m",
112                    target.name
113                ));
114                already_configured.push(target.name);
115            }
116            Ok(_) => {
117                terminal_ui::print_status_new(&format!(
118                    "{:<20} \x1b[2m{short_path}\x1b[0m",
119                    target.name
120                ));
121                newly_configured.push(target.name);
122            }
123            Err(e) => {
124                terminal_ui::print_status_warn(&format!("{}: {e}", target.name));
125                errors.push(target.name);
126            }
127        }
128    }
129
130    let total_ok = newly_configured.len() + already_configured.len();
131    if total_ok == 0 && errors.is_empty() {
132        terminal_ui::print_status_warn(
133            "No AI tools detected. Install one and re-run: nebu-ctx setup",
134        );
135    }
136
137    if !not_installed.is_empty() {
138        println!(
139            "  \x1b[2m○ {} not detected: {}\x1b[0m",
140            not_installed.len(),
141            not_installed.join(", ")
142        );
143    }
144
145    // Step 3: Agent rules injection
146    terminal_ui::print_step_header(3, 7, "Agent Rules");
147    let rules_result = crate::rules_inject::inject_all_rules(&home);
148    for name in &rules_result.injected {
149        terminal_ui::print_status_new(&format!("{name:<20} \x1b[2mrules injected\x1b[0m"));
150    }
151    for name in &rules_result.updated {
152        terminal_ui::print_status_new(&format!("{name:<20} \x1b[2mrules updated\x1b[0m"));
153    }
154    for name in &rules_result.already {
155        terminal_ui::print_status_ok(&format!("{name:<20} \x1b[2mrules up-to-date\x1b[0m"));
156    }
157    for err in &rules_result.errors {
158        terminal_ui::print_status_warn(err);
159    }
160    if rules_result.injected.is_empty()
161        && rules_result.updated.is_empty()
162        && rules_result.already.is_empty()
163        && rules_result.errors.is_empty()
164    {
165        terminal_ui::print_status_skip("No agent rules needed");
166    }
167
168    // Legacy agent hooks
169    for target in &targets {
170        if !target.detect_path.exists() || target.agent_key.is_empty() {
171            continue;
172        }
173        crate::hooks::install_agent_hook(&target.agent_key, true);
174    }
175
176    // Step 4: API Proxy configuration
177    terminal_ui::print_step_header(4, 7, "API Proxy");
178    crate::proxy_setup::install_proxy_env(&home, crate::proxy_setup::default_port(), false);
179    println!();
180    println!("  \x1b[2mStart proxy for maximum token savings:\x1b[0m");
181    println!("    \x1b[1mnebu-ctx proxy start\x1b[0m");
182    println!("  \x1b[2mEnable autostart:\x1b[0m");
183    println!("    \x1b[1mnebu-ctx proxy start --autostart\x1b[0m");
184
185    // Step 5: Data directory + diagnostics
186    terminal_ui::print_step_header(5, 7, "Environment Check");
187    let lean_dir = home.join(".nebu-ctx");
188    if !lean_dir.exists() {
189        let _ = std::fs::create_dir_all(&lean_dir);
190        terminal_ui::print_status_new("Created ~/.nebu-ctx/");
191    } else {
192        terminal_ui::print_status_ok("~/.nebu-ctx/ ready");
193    }
194    crate::doctor::run_compact();
195
196    // Step 6: Data sharing
197    terminal_ui::print_step_header(6, 7, "Help Improve nebu-ctx");
198    println!("  Share anonymous compression stats to make nebu-ctx better.");
199    println!("  \x1b[1mNo code, no file names, no personal data — ever.\x1b[0m");
200    println!();
201    print!("  Enable anonymous data sharing? \x1b[1m[Y/n]\x1b[0m ");
202    use std::io::Write;
203    std::io::stdout().flush().ok();
204
205    let mut input = String::new();
206    let contribute = if std::io::stdin().read_line(&mut input).is_ok() {
207        let answer = input.trim().to_lowercase();
208        answer.is_empty() || answer == "y" || answer == "yes"
209    } else {
210        false
211    };
212
213    if contribute {
214        let config_dir = home.join(".nebu-ctx");
215        let _ = std::fs::create_dir_all(&config_dir);
216        let config_path = config_dir.join("config.toml");
217        let mut config_content = std::fs::read_to_string(&config_path).unwrap_or_default();
218        if !config_content.contains("[cloud]") {
219            if !config_content.is_empty() && !config_content.ends_with('\n') {
220                config_content.push('\n');
221            }
222            config_content.push_str("\n[cloud]\ncontribute_enabled = true\n");
223            let _ = std::fs::write(&config_path, config_content);
224        }
225        terminal_ui::print_status_ok("Enabled — thank you!");
226    } else {
227        terminal_ui::print_status_skip("Skipped — enable later with: nebu-ctx config");
228    }
229
230    // Step 7: Premium Features Configuration
231    terminal_ui::print_step_header(7, 7, "Premium Features");
232    configure_premium_features(&home);
233
234    // Summary
235    println!();
236    println!(
237        "  \x1b[1;32m✓ Setup complete!\x1b[0m  \x1b[1m{}\x1b[0m configured, \x1b[2m{} already set, {} skipped\x1b[0m",
238        newly_configured.len(),
239        already_configured.len(),
240        not_installed.len()
241    );
242
243    if !errors.is_empty() {
244        println!(
245            "  \x1b[33m⚠ {} error{}: {}\x1b[0m",
246            errors.len(),
247            if errors.len() != 1 { "s" } else { "" },
248            errors.join(", ")
249        );
250    }
251
252    // Next steps
253    let shell = std::env::var("SHELL").unwrap_or_default();
254    let source_cmd = if shell.contains("zsh") {
255        "source ~/.zshrc"
256    } else if shell.contains("fish") {
257        "source ~/.config/fish/config.fish"
258    } else if shell.contains("bash") {
259        "source ~/.bashrc"
260    } else {
261        "Restart your shell"
262    };
263
264    let dim = "\x1b[2m";
265    let bold = "\x1b[1m";
266    let cyan = "\x1b[36m";
267    let yellow = "\x1b[33m";
268    let rst = "\x1b[0m";
269
270    println!();
271    println!("  {bold}Next steps:{rst}");
272    println!();
273    println!("  {cyan}1.{rst} Reload your shell:");
274    println!("     {bold}{source_cmd}{rst}");
275    println!();
276
277    let mut tools_to_restart: Vec<String> =
278        newly_configured.iter().map(|s| s.to_string()).collect();
279    for name in rules_result
280        .injected
281        .iter()
282        .chain(rules_result.updated.iter())
283    {
284        if !tools_to_restart.iter().any(|t| t == name) {
285            tools_to_restart.push(name.clone());
286        }
287    }
288
289    if !tools_to_restart.is_empty() {
290        println!("  {cyan}2.{rst} {yellow}{bold}Restart your IDE / AI tool:{rst}");
291        println!("     {bold}{}{rst}", tools_to_restart.join(", "));
292        println!(
293            "     {dim}The MCP connection must be re-established for changes to take effect.{rst}"
294        );
295        println!("     {dim}Close and re-open the application completely.{rst}");
296    } else if !already_configured.is_empty() {
297        println!(
298            "  {cyan}2.{rst} {dim}Your tools are already configured — no restart needed.{rst}"
299        );
300    }
301
302    println!();
303    println!(
304        "  {dim}After restart, lean-ctx will automatically optimize every AI interaction.{rst}"
305    );
306    println!("  {dim}Verify with:{rst} {bold}nebu-ctx gain{rst}");
307
308    // Logo + commands
309    println!();
310    terminal_ui::print_logo_animated();
311    terminal_ui::print_command_box();
312}
313
314#[derive(Debug, Clone, Copy, Default)]
315pub struct SetupOptions {
316    pub non_interactive: bool,
317    pub yes: bool,
318    pub fix: bool,
319    pub json: bool,
320}
321
322pub fn run_setup_with_options(opts: SetupOptions) -> Result<SetupReport, String> {
323    let _quiet_guard = opts.json.then(|| EnvVarGuard::set("NEBU_CTX_QUIET", "1"));
324    let started_at = Utc::now();
325    let home = dirs::home_dir().ok_or_else(|| "Cannot determine home directory".to_string())?;
326    let binary = resolve_portable_binary();
327    let home_str = home.to_string_lossy().to_string();
328
329    let mut steps: Vec<SetupStepReport> = Vec::new();
330
331    // Step: Shell Hook
332    let mut shell_step = SetupStepReport {
333        name: "shell_hook".to_string(),
334        ok: true,
335        items: Vec::new(),
336        warnings: Vec::new(),
337        errors: Vec::new(),
338    };
339    if !opts.non_interactive || opts.yes {
340        if opts.json {
341            crate::cli::cmd_init_quiet(&["--global".to_string()]);
342        } else {
343            crate::cli::cmd_init(&["--global".to_string()]);
344        }
345        crate::shell_hook::install_all(opts.json);
346        crate::cli::shell_init::write_env_sh_for_containers("");
347        shell_step.items.push(SetupItem {
348            name: "init --global".to_string(),
349            status: "ran".to_string(),
350            path: None,
351            note: None,
352        });
353        shell_step.items.push(SetupItem {
354            name: "universal_shell_hook".to_string(),
355            status: "installed".to_string(),
356            path: None,
357            note: Some("~/.zshenv, ~/.bashenv, agent aliases".to_string()),
358        });
359    } else {
360        shell_step
361            .warnings
362            .push("non_interactive_without_yes: shell hook not installed (use --yes)".to_string());
363        shell_step.ok = false;
364        shell_step.items.push(SetupItem {
365            name: "init --global".to_string(),
366            status: "skipped".to_string(),
367            path: None,
368            note: Some("requires --yes in --non-interactive mode".to_string()),
369        });
370    }
371    steps.push(shell_step);
372
373    // Step: Editor MCP config
374    let mut editor_step = SetupStepReport {
375        name: "editors".to_string(),
376        ok: true,
377        items: Vec::new(),
378        warnings: Vec::new(),
379        errors: Vec::new(),
380    };
381
382    let targets = crate::core::editor_registry::build_targets(&home);
383    for target in &targets {
384        let short_path = shorten_path(&target.config_path.to_string_lossy(), &home_str);
385        if !target.detect_path.exists() {
386            editor_step.items.push(SetupItem {
387                name: target.name.to_string(),
388                status: "not_detected".to_string(),
389                path: Some(short_path),
390                note: None,
391            });
392            continue;
393        }
394
395        let res = crate::core::editor_registry::write_config_with_options(
396            target,
397            &binary,
398            WriteOptions {
399                overwrite_invalid: opts.fix,
400            },
401        );
402        match res {
403            Ok(w) => {
404                editor_step.items.push(SetupItem {
405                    name: target.name.to_string(),
406                    status: match w.action {
407                        WriteAction::Created => "created".to_string(),
408                        WriteAction::Updated => "updated".to_string(),
409                        WriteAction::Already => "already".to_string(),
410                    },
411                    path: Some(short_path),
412                    note: w.note,
413                });
414            }
415            Err(e) => {
416                editor_step.ok = false;
417                editor_step.items.push(SetupItem {
418                    name: target.name.to_string(),
419                    status: "error".to_string(),
420                    path: Some(short_path),
421                    note: Some(e),
422                });
423            }
424        }
425    }
426    steps.push(editor_step);
427
428    // Step: Agent rules
429    let mut rules_step = SetupStepReport {
430        name: "agent_rules".to_string(),
431        ok: true,
432        items: Vec::new(),
433        warnings: Vec::new(),
434        errors: Vec::new(),
435    };
436    let rules_result = crate::rules_inject::inject_all_rules(&home);
437    for n in rules_result.injected {
438        rules_step.items.push(SetupItem {
439            name: n,
440            status: "injected".to_string(),
441            path: None,
442            note: None,
443        });
444    }
445    for n in rules_result.updated {
446        rules_step.items.push(SetupItem {
447            name: n,
448            status: "updated".to_string(),
449            path: None,
450            note: None,
451        });
452    }
453    for n in rules_result.already {
454        rules_step.items.push(SetupItem {
455            name: n,
456            status: "already".to_string(),
457            path: None,
458            note: None,
459        });
460    }
461    for e in rules_result.errors {
462        rules_step.ok = false;
463        rules_step.errors.push(e);
464    }
465    steps.push(rules_step);
466
467    // Step: Agent-specific hooks (Codex, Cursor)
468    let mut hooks_step = SetupStepReport {
469        name: "agent_hooks".to_string(),
470        ok: true,
471        items: Vec::new(),
472        warnings: Vec::new(),
473        errors: Vec::new(),
474    };
475    for target in &targets {
476        if !target.detect_path.exists() {
477            continue;
478        }
479        match target.agent_key.as_str() {
480            "codex" => {
481                crate::hooks::agents::install_codex_hook();
482                hooks_step.items.push(SetupItem {
483                    name: "Codex integration".to_string(),
484                    status: "installed".to_string(),
485                    path: Some("~/.codex/".to_string()),
486                    note: Some(
487                        "Installs AGENTS/MCP guidance and Codex-compatible SessionStart/PreToolUse hooks."
488                            .to_string(),
489                    ),
490                });
491            }
492            "cursor" => {
493                let hooks_path = home.join(".cursor/hooks.json");
494                if !hooks_path.exists() {
495                    crate::hooks::agents::install_cursor_hook(true);
496                    hooks_step.items.push(SetupItem {
497                        name: "Cursor hooks".to_string(),
498                        status: "installed".to_string(),
499                        path: Some("~/.cursor/hooks.json".to_string()),
500                        note: None,
501                    });
502                }
503            }
504            _ => {}
505        }
506    }
507    if !hooks_step.items.is_empty() {
508        steps.push(hooks_step);
509    }
510
511    // Step: Proxy env vars
512    let mut proxy_step = SetupStepReport {
513        name: "proxy_env".to_string(),
514        ok: true,
515        items: Vec::new(),
516        warnings: Vec::new(),
517        errors: Vec::new(),
518    };
519    crate::proxy_setup::install_proxy_env(&home, crate::proxy_setup::default_port(), opts.json);
520    proxy_step.items.push(SetupItem {
521        name: "proxy_env".to_string(),
522        status: "configured".to_string(),
523        path: None,
524        note: Some("ANTHROPIC_BASE_URL, OPENAI_BASE_URL, GEMINI_API_BASE_URL".to_string()),
525    });
526    steps.push(proxy_step);
527
528    // Step: Environment / doctor (compact)
529    let mut env_step = SetupStepReport {
530        name: "doctor_compact".to_string(),
531        ok: true,
532        items: Vec::new(),
533        warnings: Vec::new(),
534        errors: Vec::new(),
535    };
536    let (passed, total) = crate::doctor::compact_score();
537    env_step.items.push(SetupItem {
538        name: "doctor".to_string(),
539        status: format!("{passed}/{total}"),
540        path: None,
541        note: None,
542    });
543    if passed != total {
544        env_step.warnings.push(format!(
545            "doctor compact not fully passing: {passed}/{total}"
546        ));
547    }
548    steps.push(env_step);
549
550    let finished_at = Utc::now();
551    let success = steps.iter().all(|s| s.ok);
552    let report = SetupReport {
553        schema_version: 1,
554        started_at,
555        finished_at,
556        success,
557        platform: PlatformInfo {
558            os: std::env::consts::OS.to_string(),
559            arch: std::env::consts::ARCH.to_string(),
560        },
561        steps,
562        warnings: Vec::new(),
563        errors: Vec::new(),
564    };
565
566    let path = SetupReport::default_path()?;
567    let mut content =
568        serde_json::to_string_pretty(&report).map_err(|e| format!("serialize report: {e}"))?;
569    content.push('\n');
570    crate::config_io::write_atomic(&path, &content)?;
571
572    Ok(report)
573}
574
575pub fn configure_agent_mcp(agent: &str) -> Result<(), String> {
576    let home = dirs::home_dir().ok_or_else(|| "Cannot determine home directory".to_string())?;
577    let binary = resolve_portable_binary();
578
579    let mut targets = Vec::<EditorTarget>::new();
580
581    let push = |targets: &mut Vec<EditorTarget>,
582                name: &'static str,
583                config_path: PathBuf,
584                config_type: ConfigType| {
585        targets.push(EditorTarget {
586            name,
587            agent_key: agent.to_string(),
588            detect_path: PathBuf::from("/nonexistent"), // not used in direct agent config
589            config_path,
590            config_type,
591        });
592    };
593
594    match agent {
595        "cursor" => push(
596            &mut targets,
597            "Cursor",
598            home.join(".cursor/mcp.json"),
599            ConfigType::McpJson,
600        ),
601        "claude" | "claude-code" => push(
602            &mut targets,
603            "Claude Code",
604            crate::core::editor_registry::claude_mcp_json_path(&home),
605            ConfigType::McpJson,
606        ),
607        "windsurf" => push(
608            &mut targets,
609            "Windsurf",
610            home.join(".codeium/windsurf/mcp_config.json"),
611            ConfigType::McpJson,
612        ),
613        "codex" => push(
614            &mut targets,
615            "Codex CLI",
616            home.join(".codex/config.toml"),
617            ConfigType::Codex,
618        ),
619        "gemini" => {
620            push(
621                &mut targets,
622                "Gemini CLI",
623                home.join(".gemini/settings.json"),
624                ConfigType::GeminiSettings,
625            );
626            push(
627                &mut targets,
628                "Antigravity",
629                home.join(".gemini/antigravity/mcp_config.json"),
630                ConfigType::McpJson,
631            );
632        }
633        "antigravity" => push(
634            &mut targets,
635            "Antigravity",
636            home.join(".gemini/antigravity/mcp_config.json"),
637            ConfigType::McpJson,
638        ),
639        "copilot" => push(
640            &mut targets,
641            "VS Code / Copilot",
642            crate::core::editor_registry::vscode_mcp_path(),
643            ConfigType::VsCodeMcp,
644        ),
645        "crush" => push(
646            &mut targets,
647            "Crush",
648            home.join(".config/crush/crush.json"),
649            ConfigType::Crush,
650        ),
651        "pi" => push(
652            &mut targets,
653            "Pi Coding Agent",
654            home.join(".pi/agent/mcp.json"),
655            ConfigType::McpJson,
656        ),
657        "cline" => push(
658            &mut targets,
659            "Cline",
660            crate::core::editor_registry::cline_mcp_path(),
661            ConfigType::McpJson,
662        ),
663        "roo" => push(
664            &mut targets,
665            "Roo Code",
666            crate::core::editor_registry::roo_mcp_path(),
667            ConfigType::McpJson,
668        ),
669        "kiro" => push(
670            &mut targets,
671            "AWS Kiro",
672            home.join(".kiro/settings/mcp.json"),
673            ConfigType::McpJson,
674        ),
675        "verdent" => push(
676            &mut targets,
677            "Verdent",
678            home.join(".verdent/mcp.json"),
679            ConfigType::McpJson,
680        ),
681        "jetbrains" => {
682            // JetBrains uses servers[] array format, handled by install_jetbrains_hook
683        }
684        "qwen" => push(
685            &mut targets,
686            "Qwen Code",
687            home.join(".qwen/mcp.json"),
688            ConfigType::McpJson,
689        ),
690        "trae" => push(
691            &mut targets,
692            "Trae",
693            home.join(".trae/mcp.json"),
694            ConfigType::McpJson,
695        ),
696        "amazonq" => push(
697            &mut targets,
698            "Amazon Q Developer",
699            home.join(".aws/amazonq/mcp.json"),
700            ConfigType::McpJson,
701        ),
702        "opencode" => {
703            #[cfg(windows)]
704            let opencode_path = if let Ok(appdata) = std::env::var("APPDATA") {
705                std::path::PathBuf::from(appdata)
706                    .join("opencode")
707                    .join("opencode.json")
708            } else {
709                home.join(".config/opencode/opencode.json")
710            };
711            #[cfg(not(windows))]
712            let opencode_path = home.join(".config/opencode/opencode.json");
713            push(
714                &mut targets,
715                "OpenCode",
716                opencode_path,
717                ConfigType::OpenCode,
718            );
719        }
720        "aider" => push(
721            &mut targets,
722            "Aider",
723            home.join(".aider/mcp.json"),
724            ConfigType::McpJson,
725        ),
726        "amp" => {
727            // Amp uses amp.mcpServers in ~/.config/amp/settings.json, handled by install_amp_hook
728        }
729        "hermes" => push(
730            &mut targets,
731            "Hermes Agent",
732            home.join(".hermes/config.yaml"),
733            ConfigType::HermesYaml,
734        ),
735        _ => {
736            return Err(format!("Unknown agent '{agent}'"));
737        }
738    }
739
740    for t in &targets {
741        crate::core::editor_registry::write_config_with_options(
742            t,
743            &binary,
744            WriteOptions {
745                overwrite_invalid: true,
746            },
747        )?;
748    }
749
750    if agent == "kiro" {
751        install_kiro_steering(&home);
752    }
753
754    Ok(())
755}
756
757fn install_kiro_steering(home: &std::path::Path) {
758    let cwd = std::env::current_dir().unwrap_or_else(|_| home.to_path_buf());
759    let steering_dir = cwd.join(".kiro").join("steering");
760    let steering_file = steering_dir.join("nebu-ctx.md");
761
762    if steering_file.exists()
763        && std::fs::read_to_string(&steering_file)
764            .unwrap_or_default()
765            .contains("nebu-ctx")
766    {
767        println!("  Kiro steering file already exists at .kiro/steering/nebu-ctx.md");
768        return;
769    }
770
771    let _ = std::fs::create_dir_all(&steering_dir);
772    let _ = std::fs::write(&steering_file, crate::hooks::KIRO_STEERING_TEMPLATE);
773    println!("  \x1b[32m✓\x1b[0m Created .kiro/steering/nebu-ctx.md (Kiro will now prefer nebu-ctx tools)");
774}
775
776fn shorten_path(path: &str, home: &str) -> String {
777    if let Some(stripped) = path.strip_prefix(home) {
778        format!("~{stripped}")
779    } else {
780        path.to_string()
781    }
782}
783
784fn configure_premium_features(home: &std::path::Path) {
785    use crate::terminal_ui;
786    use std::io::Write;
787
788    let config_dir = home.join(".nebu-ctx");
789    let _ = std::fs::create_dir_all(&config_dir);
790    let config_path = config_dir.join("config.toml");
791    let mut config_content = std::fs::read_to_string(&config_path).unwrap_or_default();
792
793    let dim = "\x1b[2m";
794    let bold = "\x1b[1m";
795    let rst = "\x1b[0m";
796
797    // Terse Agent Mode
798    println!(
799        "\n  {bold}Agent Output Optimization{rst} {dim}(reduces output tokens by 40-70%){rst}"
800    );
801    println!(
802        "  {dim}Levels: lite (concise), full (max density), ultra (expert pair-programmer){rst}"
803    );
804    print!("  Terse agent mode? {bold}[off/lite/full/ultra]{rst} {dim}(default: off){rst} ");
805    std::io::stdout().flush().ok();
806
807    let mut terse_input = String::new();
808    let terse_level = if std::io::stdin().read_line(&mut terse_input).is_ok() {
809        match terse_input.trim().to_lowercase().as_str() {
810            "lite" => "lite",
811            "full" => "full",
812            "ultra" => "ultra",
813            _ => "off",
814        }
815    } else {
816        "off"
817    };
818
819    if terse_level != "off" && !config_content.contains("terse_agent") {
820        if !config_content.is_empty() && !config_content.ends_with('\n') {
821            config_content.push('\n');
822        }
823        config_content.push_str(&format!("terse_agent = \"{terse_level}\"\n"));
824        terminal_ui::print_status_ok(&format!("Terse agent: {terse_level}"));
825    } else if terse_level == "off" {
826        terminal_ui::print_status_skip(
827            "Terse agent: off (change later with: nebu-ctx terse <level>)",
828        );
829    }
830
831    // Tool Result Archive
832    println!(
833        "\n  {bold}Tool Result Archive{rst} {dim}(zero-loss: large outputs archived, retrievable via ctx_expand){rst}"
834    );
835    print!("  Enable auto-archive? {bold}[Y/n]{rst} ");
836    std::io::stdout().flush().ok();
837
838    let mut archive_input = String::new();
839    let archive_on = if std::io::stdin().read_line(&mut archive_input).is_ok() {
840        let a = archive_input.trim().to_lowercase();
841        a.is_empty() || a == "y" || a == "yes"
842    } else {
843        true
844    };
845
846    if archive_on && !config_content.contains("[archive]") {
847        if !config_content.is_empty() && !config_content.ends_with('\n') {
848            config_content.push('\n');
849        }
850        config_content.push_str("\n[archive]\nenabled = true\n");
851        terminal_ui::print_status_ok("Tool Result Archive: enabled");
852    } else if !archive_on {
853        terminal_ui::print_status_skip("Archive: off (enable later in config.toml)");
854    }
855
856    // Output Density
857    println!(
858        "\n  {bold}Output Density{rst} {dim}(compresses tool output: normal, terse, ultra){rst}"
859    );
860    print!("  Output density? {bold}[normal/terse/ultra]{rst} {dim}(default: normal){rst} ");
861    std::io::stdout().flush().ok();
862
863    let mut density_input = String::new();
864    let density = if std::io::stdin().read_line(&mut density_input).is_ok() {
865        match density_input.trim().to_lowercase().as_str() {
866            "terse" => "terse",
867            "ultra" => "ultra",
868            _ => "normal",
869        }
870    } else {
871        "normal"
872    };
873
874    if density != "normal" && !config_content.contains("output_density") {
875        if !config_content.is_empty() && !config_content.ends_with('\n') {
876            config_content.push('\n');
877        }
878        config_content.push_str(&format!("output_density = \"{density}\"\n"));
879        terminal_ui::print_status_ok(&format!("Output density: {density}"));
880    } else if density == "normal" {
881        terminal_ui::print_status_skip("Output density: normal (change later in config.toml)");
882    }
883
884    let _ = std::fs::write(&config_path, config_content);
885}