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, 6, "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, 6, "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, 6, "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, 6, "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, 6, "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: Agent settings
197    terminal_ui::print_step_header(6, 6, "Settings");
198    configure_agent_settings(&home);
199
200    // Summary
201    println!();
202    println!(
203        "  \x1b[1;32m✓ Setup complete!\x1b[0m  \x1b[1m{}\x1b[0m configured, \x1b[2m{} already set, {} skipped\x1b[0m",
204        newly_configured.len(),
205        already_configured.len(),
206        not_installed.len()
207    );
208
209    if !errors.is_empty() {
210        println!(
211            "  \x1b[33m⚠ {} error{}: {}\x1b[0m",
212            errors.len(),
213            if errors.len() != 1 { "s" } else { "" },
214            errors.join(", ")
215        );
216    }
217
218    // Next steps
219    let shell = std::env::var("SHELL").unwrap_or_default();
220    let source_cmd = if shell.contains("zsh") {
221        "source ~/.zshrc"
222    } else if shell.contains("fish") {
223        "source ~/.config/fish/config.fish"
224    } else if shell.contains("bash") {
225        "source ~/.bashrc"
226    } else {
227        "Restart your shell"
228    };
229
230    let dim = "\x1b[2m";
231    let bold = "\x1b[1m";
232    let cyan = "\x1b[36m";
233    let yellow = "\x1b[33m";
234    let rst = "\x1b[0m";
235
236    println!();
237    println!("  {bold}Next steps:{rst}");
238    println!();
239    println!("  {cyan}1.{rst} Reload your shell:");
240    println!("     {bold}{source_cmd}{rst}");
241    println!();
242
243    let mut tools_to_restart: Vec<String> =
244        newly_configured.iter().map(|s| s.to_string()).collect();
245    for name in rules_result
246        .injected
247        .iter()
248        .chain(rules_result.updated.iter())
249    {
250        if !tools_to_restart.iter().any(|t| t == name) {
251            tools_to_restart.push(name.clone());
252        }
253    }
254
255    if !tools_to_restart.is_empty() {
256        println!("  {cyan}2.{rst} {yellow}{bold}Restart your IDE / AI tool:{rst}");
257        println!("     {bold}{}{rst}", tools_to_restart.join(", "));
258        println!(
259            "     {dim}The MCP connection must be re-established for changes to take effect.{rst}"
260        );
261        println!("     {dim}Close and re-open the application completely.{rst}");
262    } else if !already_configured.is_empty() {
263        println!(
264            "  {cyan}2.{rst} {dim}Your tools are already configured — no restart needed.{rst}"
265        );
266    }
267
268    println!();
269    println!(
270        "  {dim}After restart, nebu-ctx will automatically optimize every AI interaction.{rst}"
271    );
272    println!("  {dim}Verify with:{rst} {bold}nebu-ctx gain{rst}");
273
274    // Logo + commands
275    println!();
276    terminal_ui::print_nebu_splash();
277    terminal_ui::print_command_box();
278}
279
280#[derive(Debug, Clone, Copy, Default)]
281pub struct SetupOptions {
282    pub non_interactive: bool,
283    pub yes: bool,
284    pub fix: bool,
285    pub json: bool,
286}
287
288pub fn run_setup_with_options(opts: SetupOptions) -> Result<SetupReport, String> {
289    let _quiet_guard = opts.json.then(|| EnvVarGuard::set("NEBU_CTX_QUIET", "1"));
290    let started_at = Utc::now();
291    let home = dirs::home_dir().ok_or_else(|| "Cannot determine home directory".to_string())?;
292    let binary = resolve_portable_binary();
293    let home_str = home.to_string_lossy().to_string();
294
295    let mut steps: Vec<SetupStepReport> = Vec::new();
296
297    // Step: Shell Hook
298    let mut shell_step = SetupStepReport {
299        name: "shell_hook".to_string(),
300        ok: true,
301        items: Vec::new(),
302        warnings: Vec::new(),
303        errors: Vec::new(),
304    };
305    if !opts.non_interactive || opts.yes {
306        if opts.json {
307            crate::cli::cmd_init_quiet(&["--global".to_string()]);
308        } else {
309            crate::cli::cmd_init(&["--global".to_string()]);
310        }
311        crate::shell_hook::install_all(opts.json);
312        crate::cli::shell_init::write_env_sh_for_containers("");
313        shell_step.items.push(SetupItem {
314            name: "init --global".to_string(),
315            status: "ran".to_string(),
316            path: None,
317            note: None,
318        });
319        shell_step.items.push(SetupItem {
320            name: "universal_shell_hook".to_string(),
321            status: "installed".to_string(),
322            path: None,
323            note: Some("~/.zshenv, ~/.bashenv, agent aliases".to_string()),
324        });
325    } else {
326        shell_step
327            .warnings
328            .push("non_interactive_without_yes: shell hook not installed (use --yes)".to_string());
329        shell_step.ok = false;
330        shell_step.items.push(SetupItem {
331            name: "init --global".to_string(),
332            status: "skipped".to_string(),
333            path: None,
334            note: Some("requires --yes in --non-interactive mode".to_string()),
335        });
336    }
337    steps.push(shell_step);
338
339    // Step: Editor MCP config
340    let mut editor_step = SetupStepReport {
341        name: "editors".to_string(),
342        ok: true,
343        items: Vec::new(),
344        warnings: Vec::new(),
345        errors: Vec::new(),
346    };
347
348    let targets = crate::core::editor_registry::build_targets(&home);
349    for target in &targets {
350        let short_path = shorten_path(&target.config_path.to_string_lossy(), &home_str);
351        if !target.detect_path.exists() {
352            editor_step.items.push(SetupItem {
353                name: target.name.to_string(),
354                status: "not_detected".to_string(),
355                path: Some(short_path),
356                note: None,
357            });
358            continue;
359        }
360
361        let res = crate::core::editor_registry::write_config_with_options(
362            target,
363            &binary,
364            WriteOptions {
365                overwrite_invalid: opts.fix,
366            },
367        );
368        match res {
369            Ok(w) => {
370                editor_step.items.push(SetupItem {
371                    name: target.name.to_string(),
372                    status: match w.action {
373                        WriteAction::Created => "created".to_string(),
374                        WriteAction::Updated => "updated".to_string(),
375                        WriteAction::Already => "already".to_string(),
376                    },
377                    path: Some(short_path),
378                    note: w.note,
379                });
380            }
381            Err(e) => {
382                editor_step.ok = false;
383                editor_step.items.push(SetupItem {
384                    name: target.name.to_string(),
385                    status: "error".to_string(),
386                    path: Some(short_path),
387                    note: Some(e),
388                });
389            }
390        }
391    }
392    steps.push(editor_step);
393
394    // Step: Agent rules
395    let mut rules_step = SetupStepReport {
396        name: "agent_rules".to_string(),
397        ok: true,
398        items: Vec::new(),
399        warnings: Vec::new(),
400        errors: Vec::new(),
401    };
402    let rules_result = crate::rules_inject::inject_all_rules(&home);
403    for n in rules_result.injected {
404        rules_step.items.push(SetupItem {
405            name: n,
406            status: "injected".to_string(),
407            path: None,
408            note: None,
409        });
410    }
411    for n in rules_result.updated {
412        rules_step.items.push(SetupItem {
413            name: n,
414            status: "updated".to_string(),
415            path: None,
416            note: None,
417        });
418    }
419    for n in rules_result.already {
420        rules_step.items.push(SetupItem {
421            name: n,
422            status: "already".to_string(),
423            path: None,
424            note: None,
425        });
426    }
427    for e in rules_result.errors {
428        rules_step.ok = false;
429        rules_step.errors.push(e);
430    }
431    steps.push(rules_step);
432
433    // Step: Agent-specific hooks (Codex, Cursor)
434    let mut hooks_step = SetupStepReport {
435        name: "agent_hooks".to_string(),
436        ok: true,
437        items: Vec::new(),
438        warnings: Vec::new(),
439        errors: Vec::new(),
440    };
441    for target in &targets {
442        if !target.detect_path.exists() {
443            continue;
444        }
445        match target.agent_key.as_str() {
446            "codex" => {
447                crate::hooks::agents::install_codex_hook();
448                hooks_step.items.push(SetupItem {
449                    name: "Codex integration".to_string(),
450                    status: "installed".to_string(),
451                    path: Some("~/.codex/".to_string()),
452                    note: Some(
453                        "Installs AGENTS/MCP guidance and Codex-compatible SessionStart/PreToolUse hooks."
454                            .to_string(),
455                    ),
456                });
457            }
458            "cursor" => {
459                let hooks_path = home.join(".cursor/hooks.json");
460                if !hooks_path.exists() {
461                    crate::hooks::agents::install_cursor_hook(true);
462                    hooks_step.items.push(SetupItem {
463                        name: "Cursor hooks".to_string(),
464                        status: "installed".to_string(),
465                        path: Some("~/.cursor/hooks.json".to_string()),
466                        note: None,
467                    });
468                }
469            }
470            "claude" | "claude-code" => {
471                crate::hooks::install_agent_hook("claude", true);
472                hooks_step.items.push(SetupItem {
473                    name: "Claude Code hooks".to_string(),
474                    status: "updated".to_string(),
475                    path: Some("~/.claude/settings.json".to_string()),
476                    note: Some("PreToolUse + PostToolUse + Stop hooks".to_string()),
477                });
478            }
479            "copilot" => {
480                crate::hooks::install_agent_hook("copilot", true);
481                hooks_step.items.push(SetupItem {
482                    name: "Copilot hooks".to_string(),
483                    status: "updated".to_string(),
484                    path: Some("~/.github/hooks/hooks.json".to_string()),
485                    note: Some("preToolUse + postToolUse + postSession hooks".to_string()),
486                });
487            }
488            _ => {}
489        }
490    }
491    if !hooks_step.items.is_empty() {
492        steps.push(hooks_step);
493    }
494
495    // Step: Proxy env vars
496    let mut proxy_step = SetupStepReport {
497        name: "proxy_env".to_string(),
498        ok: true,
499        items: Vec::new(),
500        warnings: Vec::new(),
501        errors: Vec::new(),
502    };
503    crate::proxy_setup::install_proxy_env(&home, crate::proxy_setup::default_port(), opts.json);
504    proxy_step.items.push(SetupItem {
505        name: "proxy_env".to_string(),
506        status: "configured".to_string(),
507        path: None,
508        note: Some("ANTHROPIC_BASE_URL, OPENAI_BASE_URL, GEMINI_API_BASE_URL".to_string()),
509    });
510    steps.push(proxy_step);
511
512    // Step: Environment / doctor (compact)
513    let mut env_step = SetupStepReport {
514        name: "doctor_compact".to_string(),
515        ok: true,
516        items: Vec::new(),
517        warnings: Vec::new(),
518        errors: Vec::new(),
519    };
520    let (passed, total) = crate::doctor::compact_score();
521    env_step.items.push(SetupItem {
522        name: "doctor".to_string(),
523        status: format!("{passed}/{total}"),
524        path: None,
525        note: None,
526    });
527    if passed != total {
528        env_step.warnings.push(format!(
529            "doctor compact not fully passing: {passed}/{total}"
530        ));
531    }
532    steps.push(env_step);
533
534    let finished_at = Utc::now();
535    let success = steps.iter().all(|s| s.ok);
536    let report = SetupReport {
537        schema_version: 1,
538        started_at,
539        finished_at,
540        success,
541        platform: PlatformInfo {
542            os: std::env::consts::OS.to_string(),
543            arch: std::env::consts::ARCH.to_string(),
544        },
545        steps,
546        warnings: Vec::new(),
547        errors: Vec::new(),
548    };
549
550    let path = SetupReport::default_path()?;
551    let mut content =
552        serde_json::to_string_pretty(&report).map_err(|e| format!("serialize report: {e}"))?;
553    content.push('\n');
554    crate::config_io::write_atomic(&path, &content)?;
555
556    Ok(report)
557}
558
559pub fn configure_agent_mcp(agent: &str) -> Result<(), String> {
560    let home = dirs::home_dir().ok_or_else(|| "Cannot determine home directory".to_string())?;
561    let binary = resolve_portable_binary();
562
563    let mut targets = Vec::<EditorTarget>::new();
564
565    let push = |targets: &mut Vec<EditorTarget>,
566                name: &'static str,
567                config_path: PathBuf,
568                config_type: ConfigType| {
569        targets.push(EditorTarget {
570            name,
571            agent_key: agent.to_string(),
572            detect_path: PathBuf::from("/nonexistent"), // not used in direct agent config
573            config_path,
574            config_type,
575        });
576    };
577
578    match agent {
579        "cursor" => push(
580            &mut targets,
581            "Cursor",
582            home.join(".cursor/mcp.json"),
583            ConfigType::McpJson,
584        ),
585        "claude" | "claude-code" => push(
586            &mut targets,
587            "Claude Code",
588            crate::core::editor_registry::claude_mcp_json_path(&home),
589            ConfigType::McpJson,
590        ),
591        "windsurf" => push(
592            &mut targets,
593            "Windsurf",
594            home.join(".codeium/windsurf/mcp_config.json"),
595            ConfigType::McpJson,
596        ),
597        "codex" => push(
598            &mut targets,
599            "Codex CLI",
600            home.join(".codex/config.toml"),
601            ConfigType::Codex,
602        ),
603        "gemini" => {
604            push(
605                &mut targets,
606                "Gemini CLI",
607                home.join(".gemini/settings.json"),
608                ConfigType::GeminiSettings,
609            );
610            push(
611                &mut targets,
612                "Antigravity",
613                home.join(".gemini/antigravity/mcp_config.json"),
614                ConfigType::McpJson,
615            );
616        }
617        "antigravity" => push(
618            &mut targets,
619            "Antigravity",
620            home.join(".gemini/antigravity/mcp_config.json"),
621            ConfigType::McpJson,
622        ),
623        "copilot" => push(
624            &mut targets,
625            "VS Code / Copilot",
626            crate::core::editor_registry::vscode_mcp_path(),
627            ConfigType::VsCodeMcp,
628        ),
629        "crush" => push(
630            &mut targets,
631            "Crush",
632            home.join(".config/crush/crush.json"),
633            ConfigType::Crush,
634        ),
635        "pi" => push(
636            &mut targets,
637            "Pi Coding Agent",
638            home.join(".pi/agent/mcp.json"),
639            ConfigType::McpJson,
640        ),
641        "cline" => push(
642            &mut targets,
643            "Cline",
644            crate::core::editor_registry::cline_mcp_path(),
645            ConfigType::McpJson,
646        ),
647        "roo" => push(
648            &mut targets,
649            "Roo Code",
650            crate::core::editor_registry::roo_mcp_path(),
651            ConfigType::McpJson,
652        ),
653        "kiro" => push(
654            &mut targets,
655            "AWS Kiro",
656            home.join(".kiro/settings/mcp.json"),
657            ConfigType::McpJson,
658        ),
659        "verdent" => push(
660            &mut targets,
661            "Verdent",
662            home.join(".verdent/mcp.json"),
663            ConfigType::McpJson,
664        ),
665        "jetbrains" => {
666            // JetBrains uses servers[] array format, handled by install_jetbrains_hook
667        }
668        "qwen" => push(
669            &mut targets,
670            "Qwen Code",
671            home.join(".qwen/mcp.json"),
672            ConfigType::McpJson,
673        ),
674        "trae" => push(
675            &mut targets,
676            "Trae",
677            home.join(".trae/mcp.json"),
678            ConfigType::McpJson,
679        ),
680        "amazonq" => push(
681            &mut targets,
682            "Amazon Q Developer",
683            home.join(".aws/amazonq/mcp.json"),
684            ConfigType::McpJson,
685        ),
686        "opencode" => {
687            #[cfg(windows)]
688            let opencode_path = if let Ok(appdata) = std::env::var("APPDATA") {
689                std::path::PathBuf::from(appdata)
690                    .join("opencode")
691                    .join("opencode.json")
692            } else {
693                home.join(".config/opencode/opencode.json")
694            };
695            #[cfg(not(windows))]
696            let opencode_path = home.join(".config/opencode/opencode.json");
697            push(
698                &mut targets,
699                "OpenCode",
700                opencode_path,
701                ConfigType::OpenCode,
702            );
703        }
704        "aider" => push(
705            &mut targets,
706            "Aider",
707            home.join(".aider/mcp.json"),
708            ConfigType::McpJson,
709        ),
710        "amp" => {
711            // Amp uses amp.mcpServers in ~/.config/amp/settings.json, handled by install_amp_hook
712        }
713        "hermes" => push(
714            &mut targets,
715            "Hermes Agent",
716            home.join(".hermes/config.yaml"),
717            ConfigType::HermesYaml,
718        ),
719        _ => {
720            return Err(format!("Unknown agent '{agent}'"));
721        }
722    }
723
724    for t in &targets {
725        crate::core::editor_registry::write_config_with_options(
726            t,
727            &binary,
728            WriteOptions {
729                overwrite_invalid: true,
730            },
731        )?;
732    }
733
734    if agent == "kiro" {
735        install_kiro_steering(&home);
736    }
737
738    Ok(())
739}
740
741fn install_kiro_steering(home: &std::path::Path) {
742    let cwd = std::env::current_dir().unwrap_or_else(|_| home.to_path_buf());
743    let steering_dir = cwd.join(".kiro").join("steering");
744    let steering_file = steering_dir.join("nebu-ctx.md");
745
746    if steering_file.exists()
747        && std::fs::read_to_string(&steering_file)
748            .unwrap_or_default()
749            .contains("nebu-ctx")
750    {
751        println!("  Kiro steering file already exists at .kiro/steering/nebu-ctx.md");
752        return;
753    }
754
755    let _ = std::fs::create_dir_all(&steering_dir);
756    let _ = std::fs::write(&steering_file, crate::hooks::KIRO_STEERING_TEMPLATE);
757    println!("  \x1b[32m✓\x1b[0m Created .kiro/steering/nebu-ctx.md (Kiro will now prefer nebu-ctx tools)");
758}
759
760fn shorten_path(path: &str, home: &str) -> String {
761    if let Some(stripped) = path.strip_prefix(home) {
762        format!("~{stripped}")
763    } else {
764        path.to_string()
765    }
766}
767
768fn configure_agent_settings(home: &std::path::Path) {
769    use crate::terminal_ui;
770    use std::io::Write;
771
772    let config_dir = home.join(".nebu-ctx");
773    let _ = std::fs::create_dir_all(&config_dir);
774    let config_path = config_dir.join("config.toml");
775    let mut config_content = std::fs::read_to_string(&config_path).unwrap_or_default();
776
777    let dim = "\x1b[2m";
778    let bold = "\x1b[1m";
779    let rst = "\x1b[0m";
780
781    // Terse Agent Mode
782    println!(
783        "\n  {bold}Agent Output Optimization{rst} {dim}(reduces output tokens by 40-70%){rst}"
784    );
785    println!(
786        "  {dim}Levels: lite (concise), full (max density), ultra (expert pair-programmer){rst}"
787    );
788    print!("  Terse agent mode? {bold}[off/lite/full/ultra]{rst} {dim}(default: off){rst} ");
789    std::io::stdout().flush().ok();
790
791    let mut terse_input = String::new();
792    let terse_level = if std::io::stdin().read_line(&mut terse_input).is_ok() {
793        match terse_input.trim().to_lowercase().as_str() {
794            "lite" => "lite",
795            "full" => "full",
796            "ultra" => "ultra",
797            _ => "off",
798        }
799    } else {
800        "off"
801    };
802
803    if terse_level != "off" && !config_content.contains("terse_agent") {
804        if !config_content.is_empty() && !config_content.ends_with('\n') {
805            config_content.push('\n');
806        }
807        config_content.push_str(&format!("terse_agent = \"{terse_level}\"\n"));
808        terminal_ui::print_status_ok(&format!("Terse agent: {terse_level}"));
809    } else if terse_level == "off" {
810        terminal_ui::print_status_skip(
811            "Terse agent: off (change later with: nebu-ctx terse <level>)",
812        );
813    }
814
815    // Tool Result Archive
816    println!(
817        "\n  {bold}Tool Result Archive{rst} {dim}(zero-loss: large outputs archived, retrievable via ctx_expand){rst}"
818    );
819    print!("  Enable auto-archive? {bold}[Y/n]{rst} ");
820    std::io::stdout().flush().ok();
821
822    let mut archive_input = String::new();
823    let archive_on = if std::io::stdin().read_line(&mut archive_input).is_ok() {
824        let a = archive_input.trim().to_lowercase();
825        a.is_empty() || a == "y" || a == "yes"
826    } else {
827        true
828    };
829
830    if archive_on && !config_content.contains("[archive]") {
831        if !config_content.is_empty() && !config_content.ends_with('\n') {
832            config_content.push('\n');
833        }
834        config_content.push_str("\n[archive]\nenabled = true\n");
835        terminal_ui::print_status_ok("Tool Result Archive: enabled");
836    } else if !archive_on {
837        terminal_ui::print_status_skip("Archive: off (enable later in config.toml)");
838    }
839
840    // Output Density
841    println!(
842        "\n  {bold}Output Density{rst} {dim}(compresses tool output: normal, terse, ultra){rst}"
843    );
844    print!("  Output density? {bold}[normal/terse/ultra]{rst} {dim}(default: normal){rst} ");
845    std::io::stdout().flush().ok();
846
847    let mut density_input = String::new();
848    let density = if std::io::stdin().read_line(&mut density_input).is_ok() {
849        match density_input.trim().to_lowercase().as_str() {
850            "terse" => "terse",
851            "ultra" => "ultra",
852            _ => "normal",
853        }
854    } else {
855        "normal"
856    };
857
858    if density != "normal" && !config_content.contains("output_density") {
859        if !config_content.is_empty() && !config_content.ends_with('\n') {
860            config_content.push('\n');
861        }
862        config_content.push_str(&format!("output_density = \"{density}\"\n"));
863        terminal_ui::print_status_ok(&format!("Output density: {density}"));
864    } else if density == "normal" {
865        terminal_ui::print_status_skip("Output density: normal (change later in config.toml)");
866    }
867
868    let _ = std::fs::write(&config_path, config_content);
869}