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
18#[cfg(test)]
19#[path = "tests_codex_rail.rs"]
20mod tests_codex_rail;
21
22#[cfg(test)]
23#[path = "tests_codex_provider.rs"]
24mod tests_codex_provider;
25
26use std::path::Path;
27
28use crate::marked_block;
29
30use claude::{install_claude_env, install_claude_env_inner, uninstall_claude_env};
31use codex::{codex_config_has_local_proxy_entry, strip_codex_proxy_entries, uninstall_codex_env};
32use commandcode::{install_commandcode_env, uninstall_commandcode_env};
33use grok::{
34 grok_config_has_local_proxy_entry, install_grok_env, strip_grok_proxy_entries,
35 uninstall_grok_env,
36};
37use pi::{install_pi_env, uninstall_pi_env};
38use shell::install_shell_exports;
39use util::{PROXY_ENV_END, PROXY_ENV_START};
40
41pub use claude::anthropic_api_key_available;
42pub(crate) use claude::repair_claude_subscription_env;
43#[cfg(feature = "http-server")]
44pub(crate) use codex::CodexEnvOutcome;
45pub(crate) use codex::install_codex_env;
46pub(crate) use commandcode::COMMANDCODE_MCP_INSTRUCTIONS;
47pub use grok::{grok_session_auth_available, xai_api_key_available};
48pub(crate) use util::is_proxy_reachable;
49pub use util::{
50 configured_proxy_live, default_port, is_local_lean_ctx_url, lean_ctx_proxy_responding,
51 proxy_timeout,
52};
53
54pub fn install_proxy_env(home: &Path, port: u16, quiet: bool) {
55 let cfg = crate::core::config::Config::load();
56 if cfg.proxy_enabled != Some(true) {
57 if !quiet {
58 println!(" Proxy env skipped (not enabled in config)");
59 }
60 return;
61 }
62 install_shell_exports(home, port, quiet, false);
63 install_claude_env(home, port, quiet);
64 install_codex_env(home, port, quiet);
65 install_pi_env(home, port, quiet, false);
66 install_grok_env(home, port, quiet, false);
67 install_commandcode_env(home, port, quiet, false);
68}
69
70pub fn install_proxy_env_unchecked(home: &Path, port: u16, quiet: bool, force_endpoint: bool) {
73 install_shell_exports(home, port, quiet, force_endpoint);
76 if force_endpoint {
77 install_claude_env_inner(home, port, quiet, true);
78 } else {
79 install_claude_env(home, port, quiet);
80 }
81 install_codex_env(home, port, quiet);
82 install_pi_env(home, port, quiet, force_endpoint);
83 install_grok_env(home, port, quiet, force_endpoint);
84 install_commandcode_env(home, port, quiet, force_endpoint);
85}
86
87pub fn preview_proxy_cleanup(home: &Path) {
88 let settings_dir = crate::core::editor_registry::claude_state_dir(home);
89 let settings_path = settings_dir.join("settings.json");
90 if let Ok(content) = std::fs::read_to_string(&settings_path)
91 && let Ok(doc) = crate::core::jsonc::parse_jsonc(&content)
92 && doc
93 .get("env")
94 .and_then(|e| e.get("ANTHROPIC_BASE_URL"))
95 .and_then(|v| v.as_str())
96 .is_some_and(is_local_lean_ctx_url)
97 {
98 let cfg = crate::core::config::Config::load();
99 if let Some(ref upstream) = cfg.proxy.anthropic_upstream {
100 println!(" Would restore ANTHROPIC_BASE_URL → {upstream} in Claude Code settings");
101 } else {
102 println!(" Would remove ANTHROPIC_BASE_URL from Claude Code settings");
103 }
104 }
105
106 let codex_path = crate::core::home::resolve_codex_config_path()
107 .unwrap_or_else(|| home.join(".codex/config.toml"));
108 if let Ok(content) = std::fs::read_to_string(codex_path)
109 && codex_config_has_local_proxy_entry(&content)
110 {
111 println!(" Would remove Codex proxy URL from config.toml");
112 }
113
114 let grok_path = home.join(".grok/config.toml");
115 if let Ok(content) = std::fs::read_to_string(grok_path)
116 && grok_config_has_local_proxy_entry(&content)
117 {
118 println!(" Would remove Grok proxy models_base_url from config.toml");
119 }
120
121 let cc_mcp = home.join(".commandcode/mcp.json");
122 if let Ok(content) = std::fs::read_to_string(cc_mcp)
123 && content.contains("lean-ctx")
124 {
125 println!(" Would remove lean-ctx from Command Code MCP (~/.commandcode/mcp.json)");
126 }
127}
128
129#[must_use]
133pub fn egress_routed_hosts(home: &Path) -> Vec<&'static str> {
134 if crate::core::config::Config::load().proxy_enabled != Some(true) {
135 return Vec::new();
136 }
137 let mut routed = Vec::new();
138 let settings_path = crate::core::editor_registry::claude_state_dir(home).join("settings.json");
139 if std::fs::read_to_string(&settings_path)
140 .ok()
141 .and_then(|content| crate::core::jsonc::parse_jsonc(&content).ok())
142 .and_then(|doc| {
143 doc.get("env")
144 .and_then(|e| e.get("ANTHROPIC_BASE_URL"))
145 .and_then(|v| v.as_str())
146 .map(is_local_lean_ctx_url)
147 })
148 .unwrap_or(false)
149 {
150 routed.push("claude");
151 }
152 let codex_path = crate::core::home::resolve_codex_config_path()
153 .unwrap_or_else(|| home.join(".codex/config.toml"));
154 if std::fs::read_to_string(codex_path)
155 .is_ok_and(|content| codex_config_has_local_proxy_entry(&content))
156 {
157 routed.push("codex");
158 }
159 if std::fs::read_to_string(home.join(".grok/config.toml"))
160 .is_ok_and(|content| grok_config_has_local_proxy_entry(&content))
161 {
162 routed.push("grok");
163 }
164 routed
165}
166
167pub fn cleanup_stale_proxy_env(home: &Path) -> usize {
170 let cfg = crate::core::config::Config::load();
171 if cfg.proxy_enabled == Some(true) {
172 return 0;
173 }
174
175 let mut cleaned = 0;
176
177 let settings_dir = crate::core::editor_registry::claude_state_dir(home);
178 let settings_path = settings_dir.join("settings.json");
179 if let Ok(content) = std::fs::read_to_string(&settings_path)
180 && let Ok(mut doc) = crate::core::jsonc::parse_jsonc(&content)
181 && let Some(base_url) = doc
182 .get("env")
183 .and_then(|e| e.get("ANTHROPIC_BASE_URL"))
184 .and_then(|v| v.as_str())
185 .map(String::from)
186 && is_local_lean_ctx_url(&base_url)
187 && let Some(env_obj) = doc.get_mut("env").and_then(|e| e.as_object_mut())
188 {
189 if let Some(ref upstream) = cfg.proxy.anthropic_upstream {
190 env_obj.insert(
191 "ANTHROPIC_BASE_URL".to_string(),
192 serde_json::Value::String(upstream.clone()),
193 );
194 println!(" ✓ Restored ANTHROPIC_BASE_URL → {upstream} in Claude Code settings");
195 } else {
196 env_obj.remove("ANTHROPIC_BASE_URL");
197 if env_obj.is_empty() {
198 doc.as_object_mut().map(|o| o.remove("env"));
199 }
200 println!(" ✓ Removed stale ANTHROPIC_BASE_URL from Claude Code settings");
201 }
202 let out = serde_json::to_string_pretty(&doc).unwrap_or_default();
203 let _ = std::fs::write(&settings_path, out + "\n");
204 cleaned += 1;
205 }
206
207 let codex_path = crate::core::home::resolve_codex_config_path()
208 .unwrap_or_else(|| home.join(".codex/config.toml"));
209 if let Ok(content) = std::fs::read_to_string(&codex_path)
210 && codex_config_has_local_proxy_entry(&content)
211 {
212 let filtered = strip_codex_proxy_entries(&content);
213 let _ = std::fs::write(&codex_path, &filtered);
214 println!(" ✓ Removed stale Codex proxy URL from config.toml");
215 cleaned += 1;
216 }
217
218 let grok_path = home.join(".grok/config.toml");
219 if let Ok(content) = std::fs::read_to_string(&grok_path)
220 && grok_config_has_local_proxy_entry(&content)
221 {
222 let cleaned_toml = strip_grok_proxy_entries(&content);
223 let _ = std::fs::write(&grok_path, &cleaned_toml);
224 println!(" ✓ Removed stale Grok proxy models_base_url from config.toml");
225 cleaned += 1;
226 }
227
228 cleaned
229}
230
231pub fn has_stale_proxy_url(home: &Path) -> bool {
232 let cfg = crate::core::config::Config::load();
233 if cfg.proxy_enabled == Some(true) {
234 return false;
235 }
236
237 let settings_dir = crate::core::editor_registry::claude_state_dir(home);
238 let settings_path = settings_dir.join("settings.json");
239 let Ok(content) = std::fs::read_to_string(&settings_path) else {
240 return false;
241 };
242 let Ok(doc) = crate::core::jsonc::parse_jsonc(&content) else {
243 return false;
244 };
245
246 let base_url = doc
247 .get("env")
248 .and_then(|e| e.get("ANTHROPIC_BASE_URL"))
249 .and_then(|v| v.as_str())
250 .unwrap_or("");
251
252 is_local_lean_ctx_url(base_url)
253}
254
255pub fn uninstall_proxy_env(home: &Path, quiet: bool) {
256 for rc in &[home.join(".zshrc"), home.join(".bashrc")] {
257 let label = format!(
258 "proxy env from ~/{}",
259 rc.file_name().unwrap_or_default().to_string_lossy()
260 );
261 marked_block::remove_from_file(rc, PROXY_ENV_START, PROXY_ENV_END, quiet, &label);
262 }
263
264 let fish_config = home.join(".config/fish/config.fish");
265 if fish_config.exists() {
266 marked_block::remove_from_file(
267 &fish_config,
268 PROXY_ENV_START,
269 PROXY_ENV_END,
270 quiet,
271 "proxy env from ~/.config/fish/config.fish",
272 );
273 }
274
275 let ps_profile =
276 dirs::home_dir().map(|h| crate::shell::platform::resolve_powershell_profile_path(&h));
277 if let Some(ref ps) = ps_profile
278 && ps.exists()
279 {
280 marked_block::remove_from_file(
281 ps,
282 PROXY_ENV_START,
283 PROXY_ENV_END,
284 quiet,
285 "proxy env from PowerShell profile",
286 );
287 }
288
289 uninstall_claude_env(home, quiet);
290 uninstall_codex_env(home, quiet);
291 uninstall_pi_env(home, quiet);
292 uninstall_grok_env(home, quiet);
293 uninstall_commandcode_env(home, quiet);
294}