lingshu-tools 0.10.0

Tool registry, ToolHandler trait, and 50+ tool implementations
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
//! cua-driver install / upgrade (Hermes-aligned upstream install.sh).

use std::process::Command;

pub const CUA_DRIVER_INSTALL_SCRIPT_URL: &str =
    "https://raw.githubusercontent.com/trycua/cua/main/libs/cua-driver/scripts/install.sh";

pub const CUA_DRIVER_INSTALL_SHELL: &str = "/bin/bash -c \"$(curl -fsSL \
    https://raw.githubusercontent.com/trycua/cua/main/libs/cua-driver/scripts/install.sh)\"";

/// cua-driver release Lingshu targets — mirrors Hermes'
/// `PINNED_CUA_DRIVER_VERSION` (default `0.5.0`).
///
/// Note: Hermes leaves this as a decorative constant — its installer runs the
/// bare upstream `install.sh` (baked default) and exports `HERMES_CUA_DRIVER_VERSION`,
/// which `install.sh` never reads (the script reads `CUA_DRIVER_VERSION`).
/// Lingshu instead wires the pin into `install.sh` via `CUA_DRIVER_VERSION`
/// (see [`run_installer`]) with a retry-to-latest fallback so a pin that is not
/// yet published upstream cannot break installs.
pub const PINNED_CUA_DRIVER_VERSION: &str = "0.5.0";

/// Pure resolver for the cua-driver version pin given the two override env values.
///
/// Precedence: `EDGECRAB_CUA_DRIVER_VERSION` → `HERMES_CUA_DRIVER_VERSION`
/// (honored for hermes-agent migrants) → [`PINNED_CUA_DRIVER_VERSION`].
/// Blank/whitespace overrides are ignored.
fn resolve_pinned_version(lingshu: Option<&str>, hermes: Option<&str>) -> String {
    lingshu
        .map(str::trim)
        .filter(|v| !v.is_empty())
        .or_else(|| hermes.map(str::trim).filter(|v| !v.is_empty()))
        .map(str::to_string)
        .unwrap_or_else(|| PINNED_CUA_DRIVER_VERSION.to_string())
}

/// True when the resolved pin came from an explicit env override (vs. the
/// built-in default). Explicit overrides are always passed to `install.sh`;
/// the default is attempted first but falls back to latest if unpublished.
fn has_explicit_version_override(lingshu: Option<&str>, hermes: Option<&str>) -> bool {
    [lingshu, hermes]
        .into_iter()
        .flatten()
        .any(|v| !v.trim().is_empty())
}

fn env_opt(key: &str) -> Option<String> {
    std::env::var(key).ok()
}

/// The cua-driver version Lingshu targets, honoring env overrides.
pub fn pinned_cua_driver_version() -> String {
    resolve_pinned_version(
        env_opt("EDGECRAB_CUA_DRIVER_VERSION").as_deref(),
        env_opt("HERMES_CUA_DRIVER_VERSION").as_deref(),
    )
}

fn pinned_version_is_explicit() -> bool {
    has_explicit_version_override(
        env_opt("EDGECRAB_CUA_DRIVER_VERSION").as_deref(),
        env_opt("HERMES_CUA_DRIVER_VERSION").as_deref(),
    )
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InstallOutcome {
    Installed,
    AlreadyInstalled,
    Upgraded,
    Failed,
    SkippedNonMacos,
}

#[derive(Debug, Clone)]
pub struct CuaDriverInstallResult {
    pub outcome: InstallOutcome,
    pub messages: Vec<String>,
    pub path_before: Option<String>,
    pub path_after: Option<String>,
    pub version_before: Option<String>,
    pub version_after: Option<String>,
}

impl CuaDriverInstallResult {
    pub fn ok(&self) -> bool {
        matches!(
            self.outcome,
            InstallOutcome::Installed | InstallOutcome::AlreadyInstalled | InstallOutcome::Upgraded
        )
    }
}

pub fn resolve_driver_path(cmd: &str) -> Option<String> {
    which::which(cmd).ok().map(|p| p.display().to_string())
}

pub fn driver_version(cmd: &str) -> Option<String> {
    Command::new(cmd)
        .arg("--version")
        .output()
        .ok()
        .filter(|o| o.status.success())
        .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
        .filter(|s| !s.is_empty())
}

/// Parse `/computer install`, `/computer install upgrade`, `/computer upgrade`.
pub fn parse_install_args(args: &str) -> (bool, bool) {
    let lower = args.trim().to_ascii_lowercase();
    let tokens: Vec<&str> = lower.split_whitespace().collect();
    let wants_install = tokens
        .first()
        .is_some_and(|t| *t == "install" || *t == "upgrade");
    let upgrade = tokens.first().is_some_and(|t| *t == "upgrade")
        || tokens.iter().any(|t| *t == "upgrade" || *t == "--upgrade");
    (wants_install, upgrade)
}

pub fn install_cua_driver(cmd: &str, upgrade: bool) -> CuaDriverInstallResult {
    let mut messages = Vec::new();

    if !super::permissions::is_macos() {
        messages.push("Computer Use (cua-driver) is macOS-only.".into());
        return CuaDriverInstallResult {
            outcome: InstallOutcome::SkippedNonMacos,
            messages,
            path_before: None,
            path_after: None,
            version_before: None,
            version_after: None,
        };
    }

    let path_before = resolve_driver_path(cmd);
    let version_before = path_before.as_ref().and_then(|_| driver_version(cmd));

    if path_before.is_some() && !upgrade {
        let version = version_before
            .clone()
            .unwrap_or_else(|| "unknown version".into());
        messages.push(format!("{cmd} already installed: {version}"));
        if let Some(ref path) = path_before {
            messages.push(format!("  path: {path}"));
        }
        messages.push("Grant macOS permissions if not done yet:".into());
        messages.push("  System Settings → Privacy & Security → Accessibility".into());
        messages.push("  System Settings → Privacy & Security → Screen Recording".into());
        messages.push("Run `/computer open` to jump there, then `/computer status`.".into());
        messages.push("Refresh to latest: `/computer install upgrade`".into());
        return CuaDriverInstallResult {
            outcome: InstallOutcome::AlreadyInstalled,
            messages,
            path_before: path_before.clone(),
            path_after: path_before,
            version_before: version_before.clone(),
            version_after: version_before,
        };
    }

    if which::which("curl").is_err() {
        messages.push("curl not found — install curl or run manually:".into());
        messages.push(format!("  {CUA_DRIVER_INSTALL_SHELL}"));
        messages.push("Docs: https://github.com/trycua/cua/tree/main/libs/cua-driver".into());
        return CuaDriverInstallResult {
            outcome: InstallOutcome::Failed,
            messages,
            path_before,
            path_after: None,
            version_before,
            version_after: None,
        };
    }

    if !check_cua_driver_asset_for_arch(&mut messages) {
        return CuaDriverInstallResult {
            outcome: InstallOutcome::Failed,
            messages,
            path_before: path_before.clone(),
            path_after: path_before,
            version_before,
            version_after: None,
        };
    }

    let label = if upgrade { "Refreshing" } else { "Installing" };
    messages.push(format!(
        "{label} cua-driver (macOS background computer-use)…"
    ));
    messages.push(format!(
        "Target version: {} (override via EDGECRAB_CUA_DRIVER_VERSION).",
        pinned_cua_driver_version()
    ));
    messages.push("This downloads the matching release from GitHub (may take a minute).".into());

    match run_installer() {
        Ok(()) => {
            let path_after = resolve_driver_path(cmd);
            let version_after = path_after.as_ref().and_then(|_| driver_version(cmd));
            if path_after.is_some() {
                let outcome = if upgrade && path_before.is_some() {
                    InstallOutcome::Upgraded
                } else {
                    InstallOutcome::Installed
                };
                messages.push(format!("{cmd} installed successfully."));
                if let Some(ref path) = path_after {
                    messages.push(format!("  path: {path}"));
                }
                if let (Some(before), Some(after)) = (&version_before, &version_after) {
                    if before != after {
                        messages.push(format!("  version: {before}{after}"));
                    } else if !after.is_empty() {
                        messages.push(format!("  version: {after} (up to date)"));
                    }
                } else if let Some(ref ver) = version_after {
                    messages.push(format!("  version: {ver}"));
                }
                messages.push(String::new());
                messages.push("IMPORTANT — grant macOS permissions now:".into());
                messages.push("  1. Run `/computer open`".into());
                messages.push("  2. Enable Accessibility + Screen Recording for:".into());
                messages.push("     • your terminal (Terminal.app / iTerm / Cursor)".into());
                messages.push("     • Lingshu (this app)".into());
                messages.push("  3. Run `/computer enable` then `/computer status`".into());
                CuaDriverInstallResult {
                    outcome,
                    messages,
                    path_before,
                    path_after,
                    version_before,
                    version_after,
                }
            } else {
                messages.push(format!(
                    "Installer finished but `{cmd}` is still not on PATH."
                ));
                messages.push(
                    "Restart your shell or add ~/.local/bin to PATH, then `/computer status`."
                        .into(),
                );
                messages.push(format!("Manual install: {CUA_DRIVER_INSTALL_SHELL}"));
                CuaDriverInstallResult {
                    outcome: InstallOutcome::Failed,
                    messages,
                    path_before,
                    path_after: None,
                    version_before,
                    version_after: None,
                }
            }
        }
        Err(err) => {
            messages.push(format!("Install failed: {err}"));
            messages.push(format!("Re-run manually: {CUA_DRIVER_INSTALL_SHELL}"));
            CuaDriverInstallResult {
                outcome: InstallOutcome::Failed,
                messages,
                path_before: path_before.clone(),
                path_after: path_before,
                version_before,
                version_after: None,
            }
        }
    }
}

fn run_installer() -> Result<(), String> {
    let pinned = pinned_cua_driver_version();
    let explicit = pinned_version_is_explicit();

    // Attempt the pinned version first (same release Hermes targets). The
    // upstream install.sh reads CUA_DRIVER_VERSION and resolves the matching
    // `cua-driver-v<ver>` release tag.
    match run_install_script(Some(&pinned)) {
        Ok(()) => Ok(()),
        Err(pinned_err) => {
            if explicit {
                // The user asked for this exact version — don't silently swap it.
                Err(pinned_err)
            } else {
                // Default pin may not be published upstream yet — fall back to
                // the installer's baked latest so installs never hard-fail.
                tracing::warn!(
                    "cua-driver pin {pinned} install failed ({pinned_err}); retrying latest"
                );
                run_install_script(None)
            }
        }
    }
}

/// Run the upstream `install.sh`. When `version` is `Some`, the matching release
/// tag is pinned via `CUA_DRIVER_VERSION`; otherwise the script's baked latest
/// is installed.
fn run_install_script(version: Option<&str>) -> Result<(), String> {
    let mut cmd = Command::new("/bin/bash");
    cmd.arg("-c").arg(format!(
        "curl -fsSL {CUA_DRIVER_INSTALL_SCRIPT_URL} | /bin/bash"
    ));
    if let Some(version) = version {
        cmd.env("CUA_DRIVER_VERSION", version);
    }

    let status = cmd.status().map_err(|e| e.to_string())?;

    if status.success() {
        Ok(())
    } else {
        Err(format!("installer exited with {status}"))
    }
}

/// Intel Macs may lack a published asset — warn before attempting install.
fn check_cua_driver_asset_for_arch(messages: &mut Vec<String>) -> bool {
    if cfg!(target_arch = "aarch64") {
        return true;
    }

    let output = Command::new("curl")
        .args([
            "-fsSL",
            "-H",
            "Accept: application/vnd.github+json",
            "https://api.github.com/repos/trycua/cua/releases/latest",
        ])
        .output();

    let Ok(output) = output else {
        return true;
    };
    if !output.status.success() {
        return true;
    }

    let body = String::from_utf8_lossy(&output.stdout);
    let tag = extract_json_string(&body, "tag_name").unwrap_or_default();
    let has_intel = body.contains("x86_64") || body.contains("amd64");
    if !has_intel && !tag.is_empty() {
        messages.push(format!(
            "Latest CUA release ({tag}) has no Intel (x86_64) asset."
        ));
        messages.push("cua-driver currently ships Apple Silicon builds only.".into());
        messages.push("See: https://github.com/trycua/cua/issues/1493".into());
        return false;
    }
    true
}

fn extract_json_string(json: &str, key: &str) -> Option<String> {
    let needle = format!("\"{key}\"");
    let start = json.find(&needle)?;
    let after_key = &json[start + needle.len()..];
    let colon = after_key.find(':')?;
    let rest = after_key[colon + 1..].trim_start();
    if !rest.starts_with('"') {
        return None;
    }
    let inner = &rest[1..];
    let end = inner.find('"')?;
    Some(inner[..end].to_string())
}

pub fn render_install_report(result: &CuaDriverInstallResult) -> String {
    let mut lines = vec![
        "Computer Use — cua-driver install".into(),
        String::new(),
        match result.outcome {
            InstallOutcome::Installed => "Result: installed".into(),
            InstallOutcome::AlreadyInstalled => "Result: already installed".into(),
            InstallOutcome::Upgraded => "Result: upgraded".into(),
            InstallOutcome::Failed => "Result: failed".into(),
            InstallOutcome::SkippedNonMacos => "Result: skipped (macOS only)".into(),
        },
        String::new(),
    ];
    for msg in &result.messages {
        if msg.is_empty() {
            lines.push(String::new());
        } else {
            lines.push(format!("  {msg}"));
        }
    }
    lines.join("\n")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_install_upgrade_variants() {
        assert_eq!(parse_install_args("install"), (true, false));
        assert_eq!(parse_install_args("install upgrade"), (true, true));
        assert_eq!(parse_install_args("install --upgrade"), (true, true));
        assert_eq!(parse_install_args("upgrade"), (true, true));
        assert_eq!(parse_install_args("status"), (false, false));
    }

    #[test]
    fn install_report_renders_outcome() {
        let report = render_install_report(&CuaDriverInstallResult {
            outcome: InstallOutcome::AlreadyInstalled,
            messages: vec!["cua-driver already installed".into()],
            path_before: Some("/usr/local/bin/cua-driver".into()),
            path_after: Some("/usr/local/bin/cua-driver".into()),
            version_before: Some("1.0.0".into()),
            version_after: Some("1.0.0".into()),
        });
        assert!(report.contains("already installed"));
        assert!(report.contains("cua-driver already installed"));
    }

    #[test]
    fn pin_defaults_to_hermes_target() {
        assert_eq!(
            resolve_pinned_version(None, None),
            PINNED_CUA_DRIVER_VERSION
        );
        assert_eq!(
            resolve_pinned_version(Some("  "), Some("")),
            PINNED_CUA_DRIVER_VERSION
        );
    }

    #[test]
    fn pin_honors_lingshu_override_first() {
        assert_eq!(
            resolve_pinned_version(Some("0.3.1"), Some("0.5.0")),
            "0.3.1"
        );
    }

    #[test]
    fn pin_falls_back_to_hermes_env() {
        assert_eq!(resolve_pinned_version(None, Some("0.4.2")), "0.4.2");
        assert_eq!(resolve_pinned_version(Some("   "), Some("0.4.2")), "0.4.2");
    }

    #[test]
    fn explicit_override_detection() {
        assert!(!has_explicit_version_override(None, None));
        assert!(!has_explicit_version_override(Some("  "), Some("")));
        assert!(has_explicit_version_override(Some("0.2.0"), None));
        assert!(has_explicit_version_override(None, Some("0.5.0")));
    }
}