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: lean-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, 6, "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, 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: lean-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, 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 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, 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[1mlean-ctx proxy start\x1b[0m");
182 println!(" \x1b[2mEnable autostart:\x1b[0m");
183 println!(" \x1b[1mlean-ctx proxy start --autostart\x1b[0m");
184
185 terminal_ui::print_step_header(5, 6, "Environment Check");
187 let lean_dir = home.join(".lean-ctx");
188 if !lean_dir.exists() {
189 let _ = std::fs::create_dir_all(&lean_dir);
190 terminal_ui::print_status_new("Created ~/.lean-ctx/");
191 } else {
192 terminal_ui::print_status_ok("~/.lean-ctx/ ready");
193 }
194 crate::doctor::run_compact();
195
196 terminal_ui::print_step_header(6, 6, "Help Improve lean-ctx");
198 println!(" Share anonymous compression stats to make lean-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(".lean-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: lean-ctx config");
228 }
229
230 println!();
232 println!(
233 " \x1b[1;32m✓ Setup complete!\x1b[0m \x1b[1m{}\x1b[0m configured, \x1b[2m{} already set, {} skipped\x1b[0m",
234 newly_configured.len(),
235 already_configured.len(),
236 not_installed.len()
237 );
238
239 if !errors.is_empty() {
240 println!(
241 " \x1b[33m⚠ {} error{}: {}\x1b[0m",
242 errors.len(),
243 if errors.len() != 1 { "s" } else { "" },
244 errors.join(", ")
245 );
246 }
247
248 let shell = std::env::var("SHELL").unwrap_or_default();
250 let source_cmd = if shell.contains("zsh") {
251 "source ~/.zshrc"
252 } else if shell.contains("fish") {
253 "source ~/.config/fish/config.fish"
254 } else if shell.contains("bash") {
255 "source ~/.bashrc"
256 } else {
257 "Restart your shell"
258 };
259
260 let dim = "\x1b[2m";
261 let bold = "\x1b[1m";
262 let cyan = "\x1b[36m";
263 let yellow = "\x1b[33m";
264 let rst = "\x1b[0m";
265
266 println!();
267 println!(" {bold}Next steps:{rst}");
268 println!();
269 println!(" {cyan}1.{rst} Reload your shell:");
270 println!(" {bold}{source_cmd}{rst}");
271 println!();
272
273 let mut tools_to_restart: Vec<String> =
274 newly_configured.iter().map(|s| s.to_string()).collect();
275 for name in rules_result
276 .injected
277 .iter()
278 .chain(rules_result.updated.iter())
279 {
280 if !tools_to_restart.iter().any(|t| t == name) {
281 tools_to_restart.push(name.clone());
282 }
283 }
284
285 if !tools_to_restart.is_empty() {
286 println!(" {cyan}2.{rst} {yellow}{bold}Restart your IDE / AI tool:{rst}");
287 println!(" {bold}{}{rst}", tools_to_restart.join(", "));
288 println!(
289 " {dim}The MCP connection must be re-established for changes to take effect.{rst}"
290 );
291 println!(" {dim}Close and re-open the application completely.{rst}");
292 } else if !already_configured.is_empty() {
293 println!(
294 " {cyan}2.{rst} {dim}Your tools are already configured — no restart needed.{rst}"
295 );
296 }
297
298 println!();
299 println!(
300 " {dim}After restart, lean-ctx will automatically optimize every AI interaction.{rst}"
301 );
302 println!(" {dim}Verify with:{rst} {bold}lean-ctx gain{rst}");
303
304 println!();
306 terminal_ui::print_logo_animated();
307 terminal_ui::print_command_box();
308}
309
310#[derive(Debug, Clone, Copy, Default)]
311pub struct SetupOptions {
312 pub non_interactive: bool,
313 pub yes: bool,
314 pub fix: bool,
315 pub json: bool,
316}
317
318pub fn run_setup_with_options(opts: SetupOptions) -> Result<SetupReport, String> {
319 let _quiet_guard = opts.json.then(|| EnvVarGuard::set("LEAN_CTX_QUIET", "1"));
320 let started_at = Utc::now();
321 let home = dirs::home_dir().ok_or_else(|| "Cannot determine home directory".to_string())?;
322 let binary = resolve_portable_binary();
323 let home_str = home.to_string_lossy().to_string();
324
325 let mut steps: Vec<SetupStepReport> = Vec::new();
326
327 let mut shell_step = SetupStepReport {
329 name: "shell_hook".to_string(),
330 ok: true,
331 items: Vec::new(),
332 warnings: Vec::new(),
333 errors: Vec::new(),
334 };
335 if !opts.non_interactive || opts.yes {
336 if opts.json {
337 crate::cli::cmd_init_quiet(&["--global".to_string()]);
338 } else {
339 crate::cli::cmd_init(&["--global".to_string()]);
340 }
341 crate::shell_hook::install_all(opts.json);
342 shell_step.items.push(SetupItem {
343 name: "init --global".to_string(),
344 status: "ran".to_string(),
345 path: None,
346 note: None,
347 });
348 shell_step.items.push(SetupItem {
349 name: "universal_shell_hook".to_string(),
350 status: "installed".to_string(),
351 path: None,
352 note: Some("~/.zshenv, ~/.bashenv, agent aliases".to_string()),
353 });
354 } else {
355 shell_step
356 .warnings
357 .push("non_interactive_without_yes: shell hook not installed (use --yes)".to_string());
358 shell_step.ok = false;
359 shell_step.items.push(SetupItem {
360 name: "init --global".to_string(),
361 status: "skipped".to_string(),
362 path: None,
363 note: Some("requires --yes in --non-interactive mode".to_string()),
364 });
365 }
366 steps.push(shell_step);
367
368 let mut editor_step = SetupStepReport {
370 name: "editors".to_string(),
371 ok: true,
372 items: Vec::new(),
373 warnings: Vec::new(),
374 errors: Vec::new(),
375 };
376
377 let targets = crate::core::editor_registry::build_targets(&home);
378 for target in &targets {
379 let short_path = shorten_path(&target.config_path.to_string_lossy(), &home_str);
380 if !target.detect_path.exists() {
381 editor_step.items.push(SetupItem {
382 name: target.name.to_string(),
383 status: "not_detected".to_string(),
384 path: Some(short_path),
385 note: None,
386 });
387 continue;
388 }
389
390 let res = crate::core::editor_registry::write_config_with_options(
391 target,
392 &binary,
393 WriteOptions {
394 overwrite_invalid: opts.fix,
395 },
396 );
397 match res {
398 Ok(w) => {
399 editor_step.items.push(SetupItem {
400 name: target.name.to_string(),
401 status: match w.action {
402 WriteAction::Created => "created".to_string(),
403 WriteAction::Updated => "updated".to_string(),
404 WriteAction::Already => "already".to_string(),
405 },
406 path: Some(short_path),
407 note: w.note,
408 });
409 }
410 Err(e) => {
411 editor_step.ok = false;
412 editor_step.items.push(SetupItem {
413 name: target.name.to_string(),
414 status: "error".to_string(),
415 path: Some(short_path),
416 note: Some(e),
417 });
418 }
419 }
420 }
421 steps.push(editor_step);
422
423 let mut rules_step = SetupStepReport {
425 name: "agent_rules".to_string(),
426 ok: true,
427 items: Vec::new(),
428 warnings: Vec::new(),
429 errors: Vec::new(),
430 };
431 let rules_result = crate::rules_inject::inject_all_rules(&home);
432 for n in rules_result.injected {
433 rules_step.items.push(SetupItem {
434 name: n,
435 status: "injected".to_string(),
436 path: None,
437 note: None,
438 });
439 }
440 for n in rules_result.updated {
441 rules_step.items.push(SetupItem {
442 name: n,
443 status: "updated".to_string(),
444 path: None,
445 note: None,
446 });
447 }
448 for n in rules_result.already {
449 rules_step.items.push(SetupItem {
450 name: n,
451 status: "already".to_string(),
452 path: None,
453 note: None,
454 });
455 }
456 for e in rules_result.errors {
457 rules_step.ok = false;
458 rules_step.errors.push(e);
459 }
460 steps.push(rules_step);
461
462 let mut hooks_step = SetupStepReport {
464 name: "agent_hooks".to_string(),
465 ok: true,
466 items: Vec::new(),
467 warnings: Vec::new(),
468 errors: Vec::new(),
469 };
470 for target in &targets {
471 if !target.detect_path.exists() {
472 continue;
473 }
474 match target.agent_key.as_str() {
475 "codex" => {
476 crate::hooks::agents::install_codex_hook();
477 hooks_step.items.push(SetupItem {
478 name: "Codex integration".to_string(),
479 status: "installed".to_string(),
480 path: Some("~/.codex/".to_string()),
481 note: Some(
482 "Installs AGENTS/MCP guidance and Codex-compatible SessionStart/PreToolUse hooks."
483 .to_string(),
484 ),
485 });
486 }
487 "cursor" => {
488 let hooks_path = home.join(".cursor/hooks.json");
489 if !hooks_path.exists() {
490 crate::hooks::agents::install_cursor_hook(true);
491 hooks_step.items.push(SetupItem {
492 name: "Cursor hooks".to_string(),
493 status: "installed".to_string(),
494 path: Some("~/.cursor/hooks.json".to_string()),
495 note: None,
496 });
497 }
498 }
499 _ => {}
500 }
501 }
502 if !hooks_step.items.is_empty() {
503 steps.push(hooks_step);
504 }
505
506 let mut proxy_step = SetupStepReport {
508 name: "proxy_env".to_string(),
509 ok: true,
510 items: Vec::new(),
511 warnings: Vec::new(),
512 errors: Vec::new(),
513 };
514 crate::proxy_setup::install_proxy_env(&home, crate::proxy_setup::default_port(), opts.json);
515 proxy_step.items.push(SetupItem {
516 name: "proxy_env".to_string(),
517 status: "configured".to_string(),
518 path: None,
519 note: Some("ANTHROPIC_BASE_URL, OPENAI_BASE_URL, GEMINI_API_BASE_URL".to_string()),
520 });
521 steps.push(proxy_step);
522
523 let mut env_step = SetupStepReport {
525 name: "doctor_compact".to_string(),
526 ok: true,
527 items: Vec::new(),
528 warnings: Vec::new(),
529 errors: Vec::new(),
530 };
531 let (passed, total) = crate::doctor::compact_score();
532 env_step.items.push(SetupItem {
533 name: "doctor".to_string(),
534 status: format!("{passed}/{total}"),
535 path: None,
536 note: None,
537 });
538 if passed != total {
539 env_step.warnings.push(format!(
540 "doctor compact not fully passing: {passed}/{total}"
541 ));
542 }
543 steps.push(env_step);
544
545 let finished_at = Utc::now();
546 let success = steps.iter().all(|s| s.ok);
547 let report = SetupReport {
548 schema_version: 1,
549 started_at,
550 finished_at,
551 success,
552 platform: PlatformInfo {
553 os: std::env::consts::OS.to_string(),
554 arch: std::env::consts::ARCH.to_string(),
555 },
556 steps,
557 warnings: Vec::new(),
558 errors: Vec::new(),
559 };
560
561 let path = SetupReport::default_path()?;
562 let mut content =
563 serde_json::to_string_pretty(&report).map_err(|e| format!("serialize report: {e}"))?;
564 content.push('\n');
565 crate::config_io::write_atomic(&path, &content)?;
566
567 Ok(report)
568}
569
570pub fn configure_agent_mcp(agent: &str) -> Result<(), String> {
571 let home = dirs::home_dir().ok_or_else(|| "Cannot determine home directory".to_string())?;
572 let binary = resolve_portable_binary();
573
574 let mut targets = Vec::<EditorTarget>::new();
575
576 let push = |targets: &mut Vec<EditorTarget>,
577 name: &'static str,
578 config_path: PathBuf,
579 config_type: ConfigType| {
580 targets.push(EditorTarget {
581 name,
582 agent_key: agent.to_string(),
583 detect_path: PathBuf::from("/nonexistent"), config_path,
585 config_type,
586 });
587 };
588
589 match agent {
590 "cursor" => push(
591 &mut targets,
592 "Cursor",
593 home.join(".cursor/mcp.json"),
594 ConfigType::McpJson,
595 ),
596 "claude" | "claude-code" => push(
597 &mut targets,
598 "Claude Code",
599 crate::core::editor_registry::claude_mcp_json_path(&home),
600 ConfigType::McpJson,
601 ),
602 "windsurf" => push(
603 &mut targets,
604 "Windsurf",
605 home.join(".codeium/windsurf/mcp_config.json"),
606 ConfigType::McpJson,
607 ),
608 "codex" => push(
609 &mut targets,
610 "Codex CLI",
611 home.join(".codex/config.toml"),
612 ConfigType::Codex,
613 ),
614 "gemini" => {
615 push(
616 &mut targets,
617 "Gemini CLI",
618 home.join(".gemini/settings.json"),
619 ConfigType::GeminiSettings,
620 );
621 push(
622 &mut targets,
623 "Antigravity",
624 home.join(".gemini/antigravity/mcp_config.json"),
625 ConfigType::McpJson,
626 );
627 }
628 "antigravity" => push(
629 &mut targets,
630 "Antigravity",
631 home.join(".gemini/antigravity/mcp_config.json"),
632 ConfigType::McpJson,
633 ),
634 "copilot" => push(
635 &mut targets,
636 "VS Code / Copilot",
637 crate::core::editor_registry::vscode_mcp_path(),
638 ConfigType::VsCodeMcp,
639 ),
640 "crush" => push(
641 &mut targets,
642 "Crush",
643 home.join(".config/crush/crush.json"),
644 ConfigType::Crush,
645 ),
646 "pi" => push(
647 &mut targets,
648 "Pi Coding Agent",
649 home.join(".pi/agent/mcp.json"),
650 ConfigType::McpJson,
651 ),
652 "cline" => push(
653 &mut targets,
654 "Cline",
655 crate::core::editor_registry::cline_mcp_path(),
656 ConfigType::McpJson,
657 ),
658 "roo" => push(
659 &mut targets,
660 "Roo Code",
661 crate::core::editor_registry::roo_mcp_path(),
662 ConfigType::McpJson,
663 ),
664 "kiro" => push(
665 &mut targets,
666 "AWS Kiro",
667 home.join(".kiro/settings/mcp.json"),
668 ConfigType::McpJson,
669 ),
670 "verdent" => push(
671 &mut targets,
672 "Verdent",
673 home.join(".verdent/mcp.json"),
674 ConfigType::McpJson,
675 ),
676 "jetbrains" => {
677 }
679 "qwen" => push(
680 &mut targets,
681 "Qwen Code",
682 home.join(".qwen/mcp.json"),
683 ConfigType::McpJson,
684 ),
685 "trae" => push(
686 &mut targets,
687 "Trae",
688 home.join(".trae/mcp.json"),
689 ConfigType::McpJson,
690 ),
691 "amazonq" => push(
692 &mut targets,
693 "Amazon Q Developer",
694 home.join(".aws/amazonq/mcp.json"),
695 ConfigType::McpJson,
696 ),
697 "opencode" => {
698 #[cfg(windows)]
699 let opencode_path = if let Ok(appdata) = std::env::var("APPDATA") {
700 std::path::PathBuf::from(appdata)
701 .join("opencode")
702 .join("opencode.json")
703 } else {
704 home.join(".config/opencode/opencode.json")
705 };
706 #[cfg(not(windows))]
707 let opencode_path = home.join(".config/opencode/opencode.json");
708 push(
709 &mut targets,
710 "OpenCode",
711 opencode_path,
712 ConfigType::OpenCode,
713 );
714 }
715 "aider" => push(
716 &mut targets,
717 "Aider",
718 home.join(".aider/mcp.json"),
719 ConfigType::McpJson,
720 ),
721 "amp" => {
722 }
724 "hermes" => push(
725 &mut targets,
726 "Hermes Agent",
727 home.join(".hermes/config.yaml"),
728 ConfigType::HermesYaml,
729 ),
730 _ => {
731 return Err(format!("Unknown agent '{agent}'"));
732 }
733 }
734
735 for t in &targets {
736 crate::core::editor_registry::write_config_with_options(
737 t,
738 &binary,
739 WriteOptions {
740 overwrite_invalid: true,
741 },
742 )?;
743 }
744
745 if agent == "kiro" {
746 install_kiro_steering(&home);
747 }
748
749 Ok(())
750}
751
752fn install_kiro_steering(home: &std::path::Path) {
753 let cwd = std::env::current_dir().unwrap_or_else(|_| home.to_path_buf());
754 let steering_dir = cwd.join(".kiro").join("steering");
755 let steering_file = steering_dir.join("lean-ctx.md");
756
757 if steering_file.exists()
758 && std::fs::read_to_string(&steering_file)
759 .unwrap_or_default()
760 .contains("lean-ctx")
761 {
762 println!(" Kiro steering file already exists at .kiro/steering/lean-ctx.md");
763 return;
764 }
765
766 let _ = std::fs::create_dir_all(&steering_dir);
767 let _ = std::fs::write(&steering_file, crate::hooks::KIRO_STEERING_TEMPLATE);
768 println!(" \x1b[32m✓\x1b[0m Created .kiro/steering/lean-ctx.md (Kiro will now prefer lean-ctx tools)");
769}
770
771fn shorten_path(path: &str, home: &str) -> String {
772 if let Some(stripped) = path.strip_prefix(home) {
773 format!("~{stripped}")
774 } else {
775 path.to_string()
776 }
777}