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