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 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 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 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 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 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 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 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 terminal_ui::print_step_header(7, 7, "Premium Features");
232 configure_premium_features(&home);
233
234 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 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 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 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 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 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 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 "claude" | "claude-code" => {
505 crate::hooks::install_agent_hook("claude", true);
506 hooks_step.items.push(SetupItem {
507 name: "Claude Code hooks".to_string(),
508 status: "updated".to_string(),
509 path: Some("~/.claude/settings.json".to_string()),
510 note: Some("PreToolUse + PostToolUse + Stop hooks".to_string()),
511 });
512 }
513 "copilot" => {
514 crate::hooks::install_agent_hook("copilot", true);
515 hooks_step.items.push(SetupItem {
516 name: "Copilot hooks".to_string(),
517 status: "updated".to_string(),
518 path: Some("~/.github/hooks/hooks.json".to_string()),
519 note: Some("preToolUse + postToolUse + postSession hooks".to_string()),
520 });
521 }
522 _ => {}
523 }
524 }
525 if !hooks_step.items.is_empty() {
526 steps.push(hooks_step);
527 }
528
529 let mut proxy_step = SetupStepReport {
531 name: "proxy_env".to_string(),
532 ok: true,
533 items: Vec::new(),
534 warnings: Vec::new(),
535 errors: Vec::new(),
536 };
537 crate::proxy_setup::install_proxy_env(&home, crate::proxy_setup::default_port(), opts.json);
538 proxy_step.items.push(SetupItem {
539 name: "proxy_env".to_string(),
540 status: "configured".to_string(),
541 path: None,
542 note: Some("ANTHROPIC_BASE_URL, OPENAI_BASE_URL, GEMINI_API_BASE_URL".to_string()),
543 });
544 steps.push(proxy_step);
545
546 let mut env_step = SetupStepReport {
548 name: "doctor_compact".to_string(),
549 ok: true,
550 items: Vec::new(),
551 warnings: Vec::new(),
552 errors: Vec::new(),
553 };
554 let (passed, total) = crate::doctor::compact_score();
555 env_step.items.push(SetupItem {
556 name: "doctor".to_string(),
557 status: format!("{passed}/{total}"),
558 path: None,
559 note: None,
560 });
561 if passed != total {
562 env_step.warnings.push(format!(
563 "doctor compact not fully passing: {passed}/{total}"
564 ));
565 }
566 steps.push(env_step);
567
568 let finished_at = Utc::now();
569 let success = steps.iter().all(|s| s.ok);
570 let report = SetupReport {
571 schema_version: 1,
572 started_at,
573 finished_at,
574 success,
575 platform: PlatformInfo {
576 os: std::env::consts::OS.to_string(),
577 arch: std::env::consts::ARCH.to_string(),
578 },
579 steps,
580 warnings: Vec::new(),
581 errors: Vec::new(),
582 };
583
584 let path = SetupReport::default_path()?;
585 let mut content =
586 serde_json::to_string_pretty(&report).map_err(|e| format!("serialize report: {e}"))?;
587 content.push('\n');
588 crate::config_io::write_atomic(&path, &content)?;
589
590 Ok(report)
591}
592
593pub fn configure_agent_mcp(agent: &str) -> Result<(), String> {
594 let home = dirs::home_dir().ok_or_else(|| "Cannot determine home directory".to_string())?;
595 let binary = resolve_portable_binary();
596
597 let mut targets = Vec::<EditorTarget>::new();
598
599 let push = |targets: &mut Vec<EditorTarget>,
600 name: &'static str,
601 config_path: PathBuf,
602 config_type: ConfigType| {
603 targets.push(EditorTarget {
604 name,
605 agent_key: agent.to_string(),
606 detect_path: PathBuf::from("/nonexistent"), config_path,
608 config_type,
609 });
610 };
611
612 match agent {
613 "cursor" => push(
614 &mut targets,
615 "Cursor",
616 home.join(".cursor/mcp.json"),
617 ConfigType::McpJson,
618 ),
619 "claude" | "claude-code" => push(
620 &mut targets,
621 "Claude Code",
622 crate::core::editor_registry::claude_mcp_json_path(&home),
623 ConfigType::McpJson,
624 ),
625 "windsurf" => push(
626 &mut targets,
627 "Windsurf",
628 home.join(".codeium/windsurf/mcp_config.json"),
629 ConfigType::McpJson,
630 ),
631 "codex" => push(
632 &mut targets,
633 "Codex CLI",
634 home.join(".codex/config.toml"),
635 ConfigType::Codex,
636 ),
637 "gemini" => {
638 push(
639 &mut targets,
640 "Gemini CLI",
641 home.join(".gemini/settings.json"),
642 ConfigType::GeminiSettings,
643 );
644 push(
645 &mut targets,
646 "Antigravity",
647 home.join(".gemini/antigravity/mcp_config.json"),
648 ConfigType::McpJson,
649 );
650 }
651 "antigravity" => push(
652 &mut targets,
653 "Antigravity",
654 home.join(".gemini/antigravity/mcp_config.json"),
655 ConfigType::McpJson,
656 ),
657 "copilot" => push(
658 &mut targets,
659 "VS Code / Copilot",
660 crate::core::editor_registry::vscode_mcp_path(),
661 ConfigType::VsCodeMcp,
662 ),
663 "crush" => push(
664 &mut targets,
665 "Crush",
666 home.join(".config/crush/crush.json"),
667 ConfigType::Crush,
668 ),
669 "pi" => push(
670 &mut targets,
671 "Pi Coding Agent",
672 home.join(".pi/agent/mcp.json"),
673 ConfigType::McpJson,
674 ),
675 "cline" => push(
676 &mut targets,
677 "Cline",
678 crate::core::editor_registry::cline_mcp_path(),
679 ConfigType::McpJson,
680 ),
681 "roo" => push(
682 &mut targets,
683 "Roo Code",
684 crate::core::editor_registry::roo_mcp_path(),
685 ConfigType::McpJson,
686 ),
687 "kiro" => push(
688 &mut targets,
689 "AWS Kiro",
690 home.join(".kiro/settings/mcp.json"),
691 ConfigType::McpJson,
692 ),
693 "verdent" => push(
694 &mut targets,
695 "Verdent",
696 home.join(".verdent/mcp.json"),
697 ConfigType::McpJson,
698 ),
699 "jetbrains" => {
700 }
702 "qwen" => push(
703 &mut targets,
704 "Qwen Code",
705 home.join(".qwen/mcp.json"),
706 ConfigType::McpJson,
707 ),
708 "trae" => push(
709 &mut targets,
710 "Trae",
711 home.join(".trae/mcp.json"),
712 ConfigType::McpJson,
713 ),
714 "amazonq" => push(
715 &mut targets,
716 "Amazon Q Developer",
717 home.join(".aws/amazonq/mcp.json"),
718 ConfigType::McpJson,
719 ),
720 "opencode" => {
721 #[cfg(windows)]
722 let opencode_path = if let Ok(appdata) = std::env::var("APPDATA") {
723 std::path::PathBuf::from(appdata)
724 .join("opencode")
725 .join("opencode.json")
726 } else {
727 home.join(".config/opencode/opencode.json")
728 };
729 #[cfg(not(windows))]
730 let opencode_path = home.join(".config/opencode/opencode.json");
731 push(
732 &mut targets,
733 "OpenCode",
734 opencode_path,
735 ConfigType::OpenCode,
736 );
737 }
738 "aider" => push(
739 &mut targets,
740 "Aider",
741 home.join(".aider/mcp.json"),
742 ConfigType::McpJson,
743 ),
744 "amp" => {
745 }
747 "hermes" => push(
748 &mut targets,
749 "Hermes Agent",
750 home.join(".hermes/config.yaml"),
751 ConfigType::HermesYaml,
752 ),
753 _ => {
754 return Err(format!("Unknown agent '{agent}'"));
755 }
756 }
757
758 for t in &targets {
759 crate::core::editor_registry::write_config_with_options(
760 t,
761 &binary,
762 WriteOptions {
763 overwrite_invalid: true,
764 },
765 )?;
766 }
767
768 if agent == "kiro" {
769 install_kiro_steering(&home);
770 }
771
772 Ok(())
773}
774
775fn install_kiro_steering(home: &std::path::Path) {
776 let cwd = std::env::current_dir().unwrap_or_else(|_| home.to_path_buf());
777 let steering_dir = cwd.join(".kiro").join("steering");
778 let steering_file = steering_dir.join("nebu-ctx.md");
779
780 if steering_file.exists()
781 && std::fs::read_to_string(&steering_file)
782 .unwrap_or_default()
783 .contains("nebu-ctx")
784 {
785 println!(" Kiro steering file already exists at .kiro/steering/nebu-ctx.md");
786 return;
787 }
788
789 let _ = std::fs::create_dir_all(&steering_dir);
790 let _ = std::fs::write(&steering_file, crate::hooks::KIRO_STEERING_TEMPLATE);
791 println!(" \x1b[32m✓\x1b[0m Created .kiro/steering/nebu-ctx.md (Kiro will now prefer nebu-ctx tools)");
792}
793
794fn shorten_path(path: &str, home: &str) -> String {
795 if let Some(stripped) = path.strip_prefix(home) {
796 format!("~{stripped}")
797 } else {
798 path.to_string()
799 }
800}
801
802fn configure_premium_features(home: &std::path::Path) {
803 use crate::terminal_ui;
804 use std::io::Write;
805
806 let config_dir = home.join(".nebu-ctx");
807 let _ = std::fs::create_dir_all(&config_dir);
808 let config_path = config_dir.join("config.toml");
809 let mut config_content = std::fs::read_to_string(&config_path).unwrap_or_default();
810
811 let dim = "\x1b[2m";
812 let bold = "\x1b[1m";
813 let rst = "\x1b[0m";
814
815 println!(
817 "\n {bold}Agent Output Optimization{rst} {dim}(reduces output tokens by 40-70%){rst}"
818 );
819 println!(
820 " {dim}Levels: lite (concise), full (max density), ultra (expert pair-programmer){rst}"
821 );
822 print!(" Terse agent mode? {bold}[off/lite/full/ultra]{rst} {dim}(default: off){rst} ");
823 std::io::stdout().flush().ok();
824
825 let mut terse_input = String::new();
826 let terse_level = if std::io::stdin().read_line(&mut terse_input).is_ok() {
827 match terse_input.trim().to_lowercase().as_str() {
828 "lite" => "lite",
829 "full" => "full",
830 "ultra" => "ultra",
831 _ => "off",
832 }
833 } else {
834 "off"
835 };
836
837 if terse_level != "off" && !config_content.contains("terse_agent") {
838 if !config_content.is_empty() && !config_content.ends_with('\n') {
839 config_content.push('\n');
840 }
841 config_content.push_str(&format!("terse_agent = \"{terse_level}\"\n"));
842 terminal_ui::print_status_ok(&format!("Terse agent: {terse_level}"));
843 } else if terse_level == "off" {
844 terminal_ui::print_status_skip(
845 "Terse agent: off (change later with: nebu-ctx terse <level>)",
846 );
847 }
848
849 println!(
851 "\n {bold}Tool Result Archive{rst} {dim}(zero-loss: large outputs archived, retrievable via ctx_expand){rst}"
852 );
853 print!(" Enable auto-archive? {bold}[Y/n]{rst} ");
854 std::io::stdout().flush().ok();
855
856 let mut archive_input = String::new();
857 let archive_on = if std::io::stdin().read_line(&mut archive_input).is_ok() {
858 let a = archive_input.trim().to_lowercase();
859 a.is_empty() || a == "y" || a == "yes"
860 } else {
861 true
862 };
863
864 if archive_on && !config_content.contains("[archive]") {
865 if !config_content.is_empty() && !config_content.ends_with('\n') {
866 config_content.push('\n');
867 }
868 config_content.push_str("\n[archive]\nenabled = true\n");
869 terminal_ui::print_status_ok("Tool Result Archive: enabled");
870 } else if !archive_on {
871 terminal_ui::print_status_skip("Archive: off (enable later in config.toml)");
872 }
873
874 println!(
876 "\n {bold}Output Density{rst} {dim}(compresses tool output: normal, terse, ultra){rst}"
877 );
878 print!(" Output density? {bold}[normal/terse/ultra]{rst} {dim}(default: normal){rst} ");
879 std::io::stdout().flush().ok();
880
881 let mut density_input = String::new();
882 let density = if std::io::stdin().read_line(&mut density_input).is_ok() {
883 match density_input.trim().to_lowercase().as_str() {
884 "terse" => "terse",
885 "ultra" => "ultra",
886 _ => "normal",
887 }
888 } else {
889 "normal"
890 };
891
892 if density != "normal" && !config_content.contains("output_density") {
893 if !config_content.is_empty() && !config_content.ends_with('\n') {
894 config_content.push('\n');
895 }
896 config_content.push_str(&format!("output_density = \"{density}\"\n"));
897 terminal_ui::print_status_ok(&format!("Output density: {density}"));
898 } else if density == "normal" {
899 terminal_ui::print_status_skip("Output density: normal (change later in config.toml)");
900 }
901
902 let _ = std::fs::write(&config_path, config_content);
903}