lean_ctx/proxy_setup/
mod.rs1mod claude;
7mod codex;
8mod commandcode;
9mod grok;
10mod pi;
11mod shell;
12mod util;
13
14#[cfg(test)]
15#[path = "tests.rs"]
16mod tests;
17
18use std::path::Path;
19
20use crate::marked_block;
21
22use claude::{install_claude_env, install_claude_env_inner, uninstall_claude_env};
23use codex::{codex_config_has_local_proxy_entry, strip_codex_proxy_entries, uninstall_codex_env};
24use commandcode::{install_commandcode_env, uninstall_commandcode_env};
25use grok::{
26 grok_config_has_local_proxy_entry, install_grok_env, strip_grok_proxy_entries,
27 uninstall_grok_env,
28};
29use pi::{install_pi_env, uninstall_pi_env};
30use shell::install_shell_exports;
31use util::{PROXY_ENV_END, PROXY_ENV_START};
32
33pub use claude::anthropic_api_key_available;
34pub(crate) use claude::repair_claude_subscription_env;
35pub(crate) use codex::{CodexEnvOutcome, install_codex_env};
36pub(crate) use commandcode::COMMANDCODE_MCP_INSTRUCTIONS;
37pub use grok::{grok_session_auth_available, xai_api_key_available};
38pub(crate) use util::is_proxy_reachable;
39pub use util::{default_port, is_local_lean_ctx_url, proxy_timeout};
40
41pub fn install_proxy_env(home: &Path, port: u16, quiet: bool) {
42 let cfg = crate::core::config::Config::load();
43 if cfg.proxy_enabled != Some(true) {
44 if !quiet {
45 println!(" Proxy env skipped (not enabled in config)");
46 }
47 return;
48 }
49 install_shell_exports(home, port, quiet, false);
50 install_claude_env(home, port, quiet);
51 install_codex_env(home, port, quiet);
52 install_pi_env(home, port, quiet, false);
53 install_grok_env(home, port, quiet, false);
54 install_commandcode_env(home, port, quiet, false);
55}
56
57pub fn install_proxy_env_unchecked(home: &Path, port: u16, quiet: bool, force_endpoint: bool) {
60 install_shell_exports(home, port, quiet, force_endpoint);
63 if force_endpoint {
64 install_claude_env_inner(home, port, quiet, true);
65 } else {
66 install_claude_env(home, port, quiet);
67 }
68 install_codex_env(home, port, quiet);
69 install_pi_env(home, port, quiet, force_endpoint);
70 install_grok_env(home, port, quiet, force_endpoint);
71 install_commandcode_env(home, port, quiet, force_endpoint);
72}
73
74pub fn preview_proxy_cleanup(home: &Path) {
75 let settings_dir = crate::core::editor_registry::claude_state_dir(home);
76 let settings_path = settings_dir.join("settings.json");
77 if let Ok(content) = std::fs::read_to_string(&settings_path)
78 && content.contains("ANTHROPIC_BASE_URL")
79 {
80 let cfg = crate::core::config::Config::load();
81 if let Some(ref upstream) = cfg.proxy.anthropic_upstream {
82 println!(" Would restore ANTHROPIC_BASE_URL → {upstream} in Claude Code settings");
83 } else {
84 println!(" Would remove ANTHROPIC_BASE_URL from Claude Code settings");
85 }
86 }
87
88 let codex_path = crate::core::home::resolve_codex_config_path()
89 .unwrap_or_else(|| home.join(".codex/config.toml"));
90 if let Ok(content) = std::fs::read_to_string(codex_path)
91 && codex_config_has_local_proxy_entry(&content)
92 {
93 println!(" Would remove Codex proxy URL from config.toml");
94 }
95
96 let grok_path = home.join(".grok/config.toml");
97 if let Ok(content) = std::fs::read_to_string(grok_path)
98 && grok_config_has_local_proxy_entry(&content)
99 {
100 println!(" Would remove Grok proxy models_base_url from config.toml");
101 }
102
103 let cc_mcp = home.join(".commandcode/mcp.json");
104 if let Ok(content) = std::fs::read_to_string(cc_mcp)
105 && content.contains("lean-ctx")
106 {
107 println!(" Would remove lean-ctx from Command Code MCP (~/.commandcode/mcp.json)");
108 }
109}
110
111pub fn cleanup_stale_proxy_env(home: &Path) -> usize {
114 let cfg = crate::core::config::Config::load();
115 if cfg.proxy_enabled == Some(true) {
116 return 0;
117 }
118
119 let mut cleaned = 0;
120
121 let settings_dir = crate::core::editor_registry::claude_state_dir(home);
122 let settings_path = settings_dir.join("settings.json");
123 if let Ok(content) = std::fs::read_to_string(&settings_path)
124 && let Ok(mut doc) = crate::core::jsonc::parse_jsonc(&content)
125 && let Some(base_url) = doc
126 .get("env")
127 .and_then(|e| e.get("ANTHROPIC_BASE_URL"))
128 .and_then(|v| v.as_str())
129 .map(String::from)
130 && is_local_lean_ctx_url(&base_url)
131 && let Some(env_obj) = doc.get_mut("env").and_then(|e| e.as_object_mut())
132 {
133 if let Some(ref upstream) = cfg.proxy.anthropic_upstream {
134 env_obj.insert(
135 "ANTHROPIC_BASE_URL".to_string(),
136 serde_json::Value::String(upstream.clone()),
137 );
138 println!(" ✓ Restored ANTHROPIC_BASE_URL → {upstream} in Claude Code settings");
139 } else {
140 env_obj.remove("ANTHROPIC_BASE_URL");
141 if env_obj.is_empty() {
142 doc.as_object_mut().map(|o| o.remove("env"));
143 }
144 println!(" ✓ Removed stale ANTHROPIC_BASE_URL from Claude Code settings");
145 }
146 let out = serde_json::to_string_pretty(&doc).unwrap_or_default();
147 let _ = std::fs::write(&settings_path, out + "\n");
148 cleaned += 1;
149 }
150
151 let codex_path = crate::core::home::resolve_codex_config_path()
152 .unwrap_or_else(|| home.join(".codex/config.toml"));
153 if let Ok(content) = std::fs::read_to_string(&codex_path)
154 && codex_config_has_local_proxy_entry(&content)
155 {
156 let filtered = strip_codex_proxy_entries(&content);
157 let _ = std::fs::write(&codex_path, &filtered);
158 println!(" ✓ Removed stale Codex proxy URL from config.toml");
159 cleaned += 1;
160 }
161
162 let grok_path = home.join(".grok/config.toml");
163 if let Ok(content) = std::fs::read_to_string(&grok_path)
164 && grok_config_has_local_proxy_entry(&content)
165 {
166 let cleaned_toml = strip_grok_proxy_entries(&content);
167 let _ = std::fs::write(&grok_path, &cleaned_toml);
168 println!(" ✓ Removed stale Grok proxy models_base_url from config.toml");
169 cleaned += 1;
170 }
171
172 cleaned
173}
174
175pub fn has_stale_proxy_url(home: &Path) -> bool {
176 let cfg = crate::core::config::Config::load();
177 if cfg.proxy_enabled == Some(true) {
178 return false;
179 }
180
181 let settings_dir = crate::core::editor_registry::claude_state_dir(home);
182 let settings_path = settings_dir.join("settings.json");
183 let Ok(content) = std::fs::read_to_string(&settings_path) else {
184 return false;
185 };
186 let Ok(doc) = crate::core::jsonc::parse_jsonc(&content) else {
187 return false;
188 };
189
190 let base_url = doc
191 .get("env")
192 .and_then(|e| e.get("ANTHROPIC_BASE_URL"))
193 .and_then(|v| v.as_str())
194 .unwrap_or("");
195
196 is_local_lean_ctx_url(base_url)
197}
198
199pub fn uninstall_proxy_env(home: &Path, quiet: bool) {
200 for rc in &[home.join(".zshrc"), home.join(".bashrc")] {
201 let label = format!(
202 "proxy env from ~/{}",
203 rc.file_name().unwrap_or_default().to_string_lossy()
204 );
205 marked_block::remove_from_file(rc, PROXY_ENV_START, PROXY_ENV_END, quiet, &label);
206 }
207
208 let fish_config = home.join(".config/fish/config.fish");
209 if fish_config.exists() {
210 marked_block::remove_from_file(
211 &fish_config,
212 PROXY_ENV_START,
213 PROXY_ENV_END,
214 quiet,
215 "proxy env from ~/.config/fish/config.fish",
216 );
217 }
218
219 let ps_profile =
220 dirs::home_dir().map(|h| crate::shell::platform::resolve_powershell_profile_path(&h));
221 if let Some(ref ps) = ps_profile
222 && ps.exists()
223 {
224 marked_block::remove_from_file(
225 ps,
226 PROXY_ENV_START,
227 PROXY_ENV_END,
228 quiet,
229 "proxy env from PowerShell profile",
230 );
231 }
232
233 uninstall_claude_env(home, quiet);
234 uninstall_codex_env(home, quiet);
235 uninstall_pi_env(home, quiet);
236 uninstall_grok_env(home, quiet);
237 uninstall_commandcode_env(home, quiet);
238}