shift-preflight-cli 0.9.4

CLI for SHIFT — transform images in AI model payloads to meet provider constraints
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//! Interactive setup for multi-agent SHIFT integration.
//!
//! Detects which AI coding agents are installed and configures each one
//! to route API traffic through the SHIFT proxy. Also optionally installs
//! a macOS LaunchAgent for auto-start on login.
//!
//! Supported agents and their configuration mechanisms:
//!   - OpenCode:    writes opencode.json (plugin + provider.baseURL)
//!   - Claude Code: writes ~/.claude/settings.json (env.ANTHROPIC_BASE_URL)
//!   - Codex CLI:   writes ~/.codex/config.toml (openai_base_url)
//!   - Cursor:      prints manual instructions (UI-only setting)

use anyhow::{Context, Result};
use std::fs;
use std::path::PathBuf;
use std::process::Command;

const DEFAULT_PORT: u16 = 8787;
#[cfg(target_os = "macos")]
const LAUNCH_AGENT_LABEL: &str = "com.shift-ai.proxy";

/// Detected agent information.
struct DetectedAgent {
    name: &'static str,
    key: &'static str,
    detected: bool,
    configured: bool,
}

/// Check if a command exists on PATH.
fn command_exists(cmd: &str) -> bool {
    Command::new("which")
        .arg(cmd)
        .output()
        .map(|o| o.status.success())
        .unwrap_or(false)
}

/// Check if a directory exists.
fn dir_exists(path: &str) -> bool {
    let expanded = shellexpand(path);
    std::path::Path::new(&expanded).exists()
}

/// Simple ~ expansion.
fn shellexpand(path: &str) -> String {
    if let Some(rest) = path.strip_prefix("~/") {
        if let Ok(home) = std::env::var("HOME") {
            return format!("{}/{}", home, rest);
        }
    }
    path.to_string()
}

/// Detect installed agents.
fn detect_agents() -> Vec<DetectedAgent> {
    vec![
        DetectedAgent {
            name: "OpenCode",
            key: "opencode",
            detected: command_exists("opencode") || dir_exists("~/.config/opencode"),
            configured: false,
        },
        DetectedAgent {
            name: "Claude Code",
            key: "claude-code",
            detected: command_exists("claude") || dir_exists("~/.claude"),
            configured: false,
        },
        DetectedAgent {
            name: "Codex CLI",
            key: "codex",
            detected: command_exists("codex") || dir_exists("~/.codex"),
            configured: false,
        },
        DetectedAgent {
            name: "Cursor",
            key: "cursor",
            detected: command_exists("cursor") || dir_exists("~/.cursor"),
            configured: false,
        },
    ]
}

/// Check prerequisites.
fn check_prerequisites() -> Result<(bool, bool)> {
    let shift_ok = command_exists("shift-ai");
    let npx_ok = command_exists("npx");
    Ok((shift_ok, npx_ok))
}

// ── macOS LaunchAgent ────────────────────────────────────────────────

/// Generate the macOS LaunchAgent plist content.
#[cfg(target_os = "macos")]
fn launchagent_plist(shift_ai_path: &str, port: u16) -> String {
    format!(
        r#"<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>{label}</string>
    <key>ProgramArguments</key>
    <array>
        <string>{binary}</string>
        <string>proxy</string>
        <string>start</string>
        <string>--port</string>
        <string>{port}</string>
        <string>--foreground</string>
    </array>
    <key>KeepAlive</key>
    <true/>
    <key>RunAtLoad</key>
    <true/>
    <key>StandardOutPath</key>
    <string>{home}/.shift/proxy.log</string>
    <key>StandardErrorPath</key>
    <string>{home}/.shift/proxy.log</string>
</dict>
</plist>"#,
        label = LAUNCH_AGENT_LABEL,
        binary = shift_ai_path,
        port = port,
        home = std::env::var("HOME").unwrap_or_else(|_| "/tmp".into()),
    )
}

/// Install the macOS LaunchAgent.
#[cfg(target_os = "macos")]
fn install_launchagent(port: u16) -> Result<()> {
    let home = std::env::var("HOME").context("HOME not set")?;
    let agents_dir = PathBuf::from(&home).join("Library/LaunchAgents");
    fs::create_dir_all(&agents_dir).context("failed to create LaunchAgents directory")?;

    let plist_path = agents_dir.join(format!("{}.plist", LAUNCH_AGENT_LABEL));

    // Find shift-ai binary path
    let shift_ai_path = Command::new("which")
        .arg("shift-ai")
        .output()
        .context("failed to find shift-ai")?;
    let binary = String::from_utf8_lossy(&shift_ai_path.stdout)
        .trim()
        .to_string();
    if binary.is_empty() {
        anyhow::bail!("shift-ai not found on PATH");
    }

    let content = launchagent_plist(&binary, port);
    fs::write(&plist_path, &content).context("failed to write LaunchAgent plist")?;

    // Unload if already loaded (ignore errors)
    let _ = Command::new("launchctl")
        .args(["unload", &plist_path.to_string_lossy()])
        .output();

    // Load
    Command::new("launchctl")
        .args(["load", &plist_path.to_string_lossy()])
        .status()
        .context("failed to load LaunchAgent")?;

    Ok(())
}

// ── Per-agent configuration ──────────────────────────────────────────

/// Configure Claude Code by writing/updating ~/.claude/settings.json.
fn configure_claude_code(port: u16) -> Result<bool> {
    let home = std::env::var("HOME").context("HOME not set")?;
    let settings_dir = PathBuf::from(&home).join(".claude");
    fs::create_dir_all(&settings_dir)?;

    let settings_path = settings_dir.join("settings.json");

    let mut settings: serde_json::Value = if settings_path.exists() {
        let content = fs::read_to_string(&settings_path)?;
        serde_json::from_str(&content).unwrap_or_else(|_| serde_json::json!({}))
    } else {
        serde_json::json!({})
    };

    // Add env.ANTHROPIC_BASE_URL
    let env = settings
        .as_object_mut()
        .context("settings is not an object")?
        .entry("env")
        .or_insert_with(|| serde_json::json!({}));

    if let Some(env_obj) = env.as_object_mut() {
        let url = format!("http://localhost:{}", port);
        env_obj.insert(
            "ANTHROPIC_BASE_URL".to_string(),
            serde_json::Value::String(url),
        );
    }

    let content = serde_json::to_string_pretty(&settings)?;
    fs::write(&settings_path, content)?;
    Ok(true)
}

/// Configure OpenCode by updating opencode.json.
fn configure_opencode(port: u16) -> Result<bool> {
    let home = std::env::var("HOME").context("HOME not set")?;
    let config_path = PathBuf::from(&home).join(".config/opencode/opencode.json");

    if !config_path.exists() {
        return Ok(false);
    }

    let content = fs::read_to_string(&config_path)?;
    let mut config: serde_json::Value =
        serde_json::from_str(&content).unwrap_or_else(|_| serde_json::json!({}));

    // Set provider.anthropic.options.baseURL
    let provider = config
        .as_object_mut()
        .context("config is not an object")?
        .entry("provider")
        .or_insert_with(|| serde_json::json!({}));
    let anthropic = provider
        .as_object_mut()
        .context("provider is not an object")?
        .entry("anthropic")
        .or_insert_with(|| serde_json::json!({}));
    let options = anthropic
        .as_object_mut()
        .context("anthropic is not an object")?
        .entry("options")
        .or_insert_with(|| serde_json::json!({}));

    if let Some(opts) = options.as_object_mut() {
        let url = format!("http://localhost:{}/v1", port);
        opts.insert("baseURL".to_string(), serde_json::Value::String(url));
    }

    // Add plugin if not present
    let plugins = config
        .as_object_mut()
        .unwrap()
        .entry("plugin")
        .or_insert_with(|| serde_json::json!([]));
    if let Some(arr) = plugins.as_array_mut() {
        let plugin_name = "@shift-preflight/opencode-plugin";
        if !arr.iter().any(|v| v.as_str() == Some(plugin_name)) {
            arr.push(serde_json::Value::String(plugin_name.to_string()));
        }
    }

    let output = serde_json::to_string_pretty(&config)?;
    fs::write(&config_path, output)?;
    Ok(true)
}

/// Configure Codex CLI by writing/updating ~/.codex/config.toml.
fn configure_codex(port: u16) -> Result<bool> {
    let home = std::env::var("HOME").context("HOME not set")?;
    let config_dir = PathBuf::from(&home).join(".codex");
    fs::create_dir_all(&config_dir)?;

    let config_path = config_dir.join("config.toml");
    let key = "openai_base_url";
    let value = format!("http://localhost:{}", port);

    if config_path.exists() {
        let content = fs::read_to_string(&config_path)?;
        // Check if already set
        if content.contains(key) {
            // Replace existing line
            let mut new_lines = Vec::new();
            for line in content.lines() {
                if line.trim_start().starts_with(key) {
                    new_lines.push(format!("{} = \"{}\"", key, value));
                } else {
                    new_lines.push(line.to_string());
                }
            }
            fs::write(&config_path, new_lines.join("\n") + "\n")?;
        } else {
            // Append
            let mut content = content;
            if !content.ends_with('\n') {
                content.push('\n');
            }
            content.push_str(&format!("{} = \"{}\"\n", key, value));
            fs::write(&config_path, content)?;
        }
    } else {
        // Create new config
        fs::write(&config_path, format!("{} = \"{}\"\n", key, value))?;
    }

    Ok(true)
}

// ── Interactive setup ────────────────────────────────────────────────

/// Run the interactive setup.
pub fn run_setup() -> Result<()> {
    use colored::Colorize;
    use std::io::{self, IsTerminal, Write};

    let use_color = io::stdout().is_terminal();
    let port = DEFAULT_PORT;

    // Header
    if use_color {
        println!("{}", "SHIFT Setup".bold().green());
        println!("{}", "".repeat(50).green());
    } else {
        println!("=== SHIFT Setup ===");
    }
    println!();

    // Prerequisites
    println!("Checking prerequisites...");
    let (shift_ok, npx_ok) = check_prerequisites()?;
    if shift_ok {
        println!(
            "  {} shift-ai v{} installed",
            "".green(),
            env!("CARGO_PKG_VERSION")
        );
    } else {
        println!("  {} shift-ai not found", "".red());
        println!();
        println!("Install shift-ai first:");
        println!("  brew install alohaninja/shift/shift-ai");
        println!("  # or: cargo install shift-preflight-cli");
        return Ok(());
    }
    if npx_ok {
        println!("  {} npx available", "".green());
    } else {
        println!("  {} npx not found (required for proxy)", "".red());
        println!();
        println!("Install Node.js: brew install node");
        return Ok(());
    }
    println!();

    // Detect agents
    println!("Detecting AI agents...");
    let mut agents = detect_agents();
    let any_detected = agents.iter().any(|a| a.detected);

    for agent in &agents {
        if agent.detected {
            println!("  {} {} — found", "".green(), agent.name);
        } else {
            println!("  {} {} — not found", "".red().dimmed(), agent.name);
        }
    }
    println!();

    if !any_detected {
        println!("No AI agents detected. You can still start the proxy manually:");
        println!("  shift-ai proxy start");
        println!();
        println!("Then configure your agent:");
        println!("  shift-ai env --list");
        return Ok(());
    }

    // Ask for confirmation
    print!("Configure detected agents and install LaunchAgent? [Y/n] ");
    io::stdout().flush()?;
    let mut input = String::new();
    io::stdin().read_line(&mut input)?;
    let input = input.trim().to_lowercase();
    if input == "n" || input == "no" {
        println!("Setup cancelled.");
        return Ok(());
    }
    println!();

    // Configure each detected agent
    println!("Installing...");

    for agent in agents.iter_mut() {
        if !agent.detected {
            continue;
        }

        let result = match agent.key {
            "opencode" => configure_opencode(port),
            "claude-code" => configure_claude_code(port),
            "codex" => configure_codex(port),
            "cursor" => Ok(false), // UI-only, handled below
            _ => Ok(false),
        };

        match result {
            Ok(true) => {
                agent.configured = true;
                println!("  {} {} configured", "".green(), agent.name);
            }
            Ok(false) if agent.key == "cursor" => {
                println!(
                    "  {} {} — open Settings > Models > Override OpenAI Base URL:",
                    "".yellow(),
                    agent.name,
                );
                println!("         http://localhost:{}/v1", port);
            }
            Ok(false) => {
                println!(
                    "  {} {} — run: shift-ai env {}",
                    "".yellow(),
                    agent.name,
                    agent.key
                );
            }
            Err(e) => {
                println!("  {} {} — failed: {}", "".red(), agent.name, e);
            }
        }
    }

    // Install LaunchAgent (macOS only)
    #[cfg(target_os = "macos")]
    {
        match install_launchagent(port) {
            Ok(()) => {
                println!(
                    "  {} LaunchAgent installed (auto-start on login)",
                    "".green()
                );
            }
            Err(e) => {
                println!("  {} LaunchAgent — failed: {}", "".red(), e);
            }
        }
    }

    // Start proxy now
    match crate::proxy::ensure(Some(port), None, false) {
        Ok(()) => {
            println!("  {} Proxy healthy on port {}", "".green(), port);
        }
        Err(e) => {
            println!("  {} Proxy — failed to start: {}", "".red(), e);
        }
    }

    println!();
    if use_color {
        println!("{}", "Done!".bold().green());
    } else {
        println!("Done!");
    }
    println!("All API traffic now flows through SHIFT.");
    println!("Run `shift-ai gain` to see cumulative token savings.");

    Ok(())
}