aacode-rs 1.8.15

Rust port of the AACode ReAct coding agent, built on fastshell for mobile AI agents
Documentation
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
// Copyright (c) 2026 xiefujin <490021684@qq.com>
// Licensed under GPL-3.0, see LICENSE file for full license terms.

//! Read-only Mobile Use tools (`ui_*`).
//!
//! These are the observation half of Mobile Use: the model can inspect the
//! device UI but **cannot** click/type/gesture. Mutating `ui_act` /
//! `ui_gesture` / `ui_set_progress` stay host-only until a per-call approval
//! flow exists.
//!
//! The host (Android/iOS) implements `DevicePlugin::ui_*`; fastshell forwards
//! through `Fastshell::ui_call`, and `FastshellBackend` exposes it here.

use super::backend::ShellBackend;
use super::registry::Tool;
use super::schema::{ParamType, ToolParameter, ToolSchema};
use crate::error::Result;
use serde_json::{json, Value};
use std::sync::atomic::AtomicBool;
use std::sync::Arc;

fn call_ui(backend: &Arc<dyn ShellBackend>, method: &str, args: Value) -> Result<String> {
    match backend.ui_call(method, &args) {
        None => Ok(json!({
            "success": false,
            "error": "mobile use unavailable: no host backend registered"
        })
        .to_string()),
        Some(Ok(v)) => Ok(v.to_string()),
        Some(Err(e)) => Ok(json!({"success": false, "error": e}).to_string()),
    }
}

fn target_arg(args: &Value) -> Value {
    args.get("target").cloned().unwrap_or_else(|| json!({}))
}

pub struct UiBackendsTool {
    pub backend: Arc<dyn ShellBackend>,
}

#[async_trait::async_trait]
impl Tool for UiBackendsTool {
    fn schema(&self) -> ToolSchema {
        ToolSchema::new(
            "ui_backends",
            "List the ready Mobile Use backends (e.g. accessibility / shizuku / root). \
             Read-only. The actual backend is chosen by the host's user settings.",
            vec![],
        )
    }
    async fn call(&self, _args: &Value, _c: &AtomicBool) -> Result<String> {
        call_ui(&self.backend, "ui.backends", json!({}))
    }
}

pub struct UiListWindowsTool {
    pub backend: Arc<dyn ShellBackend>,
}

#[async_trait::async_trait]
impl Tool for UiListWindowsTool {
    fn schema(&self) -> ToolSchema {
        ToolSchema::new(
            "ui_list_windows",
            "List visible device windows with package/window id/bounds/focus. Read-only.",
            vec![],
        )
    }
    async fn call(&self, _args: &Value, _c: &AtomicBool) -> Result<String> {
        call_ui(&self.backend, "ui.list_windows", json!({}))
    }
}

pub struct UiSnapshotTool {
    pub backend: Arc<dyn ShellBackend>,
}

#[async_trait::async_trait]
impl Tool for UiSnapshotTool {
    fn schema(&self) -> ToolSchema {
        ToolSchema::new(
            "ui_snapshot",
            "Read the device UI tree (role/text/view id/bounds/states) with a stable \
             node_ref and fingerprint per node. Read-only. Password fields are redacted. \
             Bounded by max_depth/max_nodes/max_text_len; `truncated` is explicit.",
            vec![
                ToolParameter::new(
                    "target",
                    ParamType::Object,
                    false,
                    "Optional {package, window_id}.",
                    &[],
                ),
                ToolParameter::new(
                    "max_depth",
                    ParamType::Integer,
                    false,
                    "Max depth (default 64).",
                    &[],
                ),
                ToolParameter::new(
                    "max_nodes",
                    ParamType::Integer,
                    false,
                    "Max nodes (default 300).",
                    &[],
                ),
                ToolParameter::new(
                    "max_text_len",
                    ParamType::Integer,
                    false,
                    "Max chars per text/description (0 = unlimited).",
                    &[],
                ),
            ],
        )
    }
    async fn call(&self, args: &Value, _c: &AtomicBool) -> Result<String> {
        let limits = json!({
            "max_depth": args.get("max_depth").and_then(|v| v.as_u64()).unwrap_or(64),
            "max_nodes": args.get("max_nodes").and_then(|v| v.as_u64()).unwrap_or(300),
            "max_text_len": args.get("max_text_len").and_then(|v| v.as_u64()).unwrap_or(0),
        });
        call_ui(
            &self.backend,
            "ui.snapshot",
            json!({"target": target_arg(args), "limits": limits}),
        )
    }
}

pub struct UiFindTool {
    pub backend: Arc<dyn ShellBackend>,
}

#[async_trait::async_trait]
impl Tool for UiFindTool {
    fn schema(&self) -> ToolSchema {
        ToolSchema::new(
            "ui_find",
            "Find UI nodes by selector: text= / role= / role=button[name=\"X\"] / \
             id= (view id). Returns bounded matches with node_ref + fingerprint. Read-only.",
            vec![
                ToolParameter::new(
                    "selector",
                    ParamType::String,
                    true,
                    "text= / role= / id= (view id) selector.",
                    &["query"],
                ),
                ToolParameter::new(
                    "limit",
                    ParamType::Integer,
                    false,
                    "Max matches (default 20).",
                    &[],
                ),
                ToolParameter::new(
                    "target",
                    ParamType::Object,
                    false,
                    "Optional {package, window_id}.",
                    &[],
                ),
            ],
        )
    }
    async fn call(&self, args: &Value, _c: &AtomicBool) -> Result<String> {
        let selector = args.get("selector").and_then(|v| v.as_str()).unwrap_or("");
        let limit = args.get("limit").and_then(|v| v.as_u64()).unwrap_or(20);
        call_ui(
            &self.backend,
            "ui.find",
            json!({"target": target_arg(args), "selector": selector, "limit": limit}),
        )
    }
}

pub struct UiWaitTool {
    pub backend: Arc<dyn ShellBackend>,
}

#[async_trait::async_trait]
impl Tool for UiWaitTool {
    fn schema(&self) -> ToolSchema {
        ToolSchema::new(
            "ui_wait",
            "Wait (bounded, ≤10s) until a selector is present or gone. Read-only: it \
             only observes, never acts.",
            vec![
                ToolParameter::new(
                    "selector",
                    ParamType::String,
                    true,
                    "text= / role= / id= selector.",
                    &["query"],
                ),
                ToolParameter::new(
                    "present",
                    ParamType::Boolean,
                    false,
                    "true = wait until present (default), false = until gone.",
                    &[],
                ),
                ToolParameter::new(
                    "timeout_ms",
                    ParamType::Integer,
                    false,
                    "Timeout (default 5000, max 10000).",
                    &[],
                ),
                ToolParameter::new(
                    "target",
                    ParamType::Object,
                    false,
                    "Optional {package, window_id}.",
                    &[],
                ),
            ],
        )
    }
    async fn call(&self, args: &Value, _c: &AtomicBool) -> Result<String> {
        let selector = args.get("selector").and_then(|v| v.as_str()).unwrap_or("");
        let present = args
            .get("present")
            .and_then(|v| v.as_bool())
            .unwrap_or(true);
        let timeout = args
            .get("timeout_ms")
            .and_then(|v| v.as_u64())
            .unwrap_or(5000)
            .min(10_000);
        call_ui(
            &self.backend,
            "ui.wait",
            json!({
                "target": target_arg(args),
                "selector": selector,
                "present": present,
                "timeout_ms": timeout,
            }),
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tools::backend::CmdOutput;

    struct MockUiBackend;
    impl ShellBackend for MockUiBackend {
        fn run(
            &self,
            _command: &str,
            _stdin: Option<&str>,
            _timeout: u64,
            _idle: u64,
            _cwd: &std::path::Path,
        ) -> CmdOutput {
            CmdOutput {
                stdout: String::new(),
                stderr: String::new(),
                exit_code: 0,
            }
        }
        fn kind(&self) -> &'static str {
            "mock"
        }
        fn ui_call(
            &self,
            method: &str,
            _args: &Value,
        ) -> Option<std::result::Result<Value, String>> {
            Some(Ok(match method {
                "ui.backends" => json!(["accessibility"]),
                "ui.list_windows" => json!({"windows": [{"package": "com.x", "window_id": 1}]}),
                "ui.snapshot" => {
                    json!({"backend": "accessibility", "nodes": [{"node_ref": "n1", "text": "Go"}]})
                }
                "ui.find" => json!({"nodes": [{"node_ref": "n1", "text": "Go"}]}),
                "ui.wait" => json!({"present": true}),
                _ => json!({"ok": false}),
            }))
        }
    }

    fn backend() -> Arc<dyn ShellBackend> {
        Arc::new(MockUiBackend)
    }

    fn parse(s: String) -> Value {
        serde_json::from_str(&s).unwrap()
    }

    #[tokio::test]
    async fn read_only_tools_roundtrip() {
        let b = backend();
        let cancel = AtomicBool::new(false);

        let v = parse(
            UiBackendsTool { backend: b.clone() }
                .call(&json!({}), &cancel)
                .await
                .unwrap(),
        );
        assert_eq!(v[0], "accessibility");

        let v = parse(
            UiListWindowsTool { backend: b.clone() }
                .call(&json!({}), &cancel)
                .await
                .unwrap(),
        );
        assert_eq!(v["windows"][0]["package"], "com.x");

        let v = parse(
            UiSnapshotTool { backend: b.clone() }
                .call(&json!({"max_nodes": 10}), &cancel)
                .await
                .unwrap(),
        );
        assert_eq!(v["nodes"][0]["text"], "Go");

        let v = parse(
            UiFindTool { backend: b.clone() }
                .call(&json!({"selector": "text=Go"}), &cancel)
                .await
                .unwrap(),
        );
        assert_eq!(v["nodes"][0]["node_ref"], "n1");

        let v = parse(
            UiWaitTool { backend: b }
                .call(&json!({"selector": "text=Go", "present": true}), &cancel)
                .await
                .unwrap(),
        );
        assert_eq!(v["present"], true);
    }

    struct CapturingBackend {
        seen: std::sync::Mutex<Vec<(String, Value)>>,
    }

    impl ShellBackend for CapturingBackend {
        fn run(
            &self,
            _command: &str,
            _stdin: Option<&str>,
            _timeout: u64,
            _idle: u64,
            _cwd: &std::path::Path,
        ) -> CmdOutput {
            CmdOutput {
                stdout: String::new(),
                stderr: String::new(),
                exit_code: 0,
            }
        }
        fn kind(&self) -> &'static str {
            "capture"
        }
        fn ui_call(
            &self,
            method: &str,
            args: &Value,
        ) -> Option<std::result::Result<Value, String>> {
            self.seen
                .lock()
                .unwrap()
                .push((method.to_string(), args.clone()));
            Some(Ok(json!({"ok": true})))
        }
    }

    #[tokio::test]
    async fn read_only_tools_forward_bounded_args() {
        let b = Arc::new(CapturingBackend {
            seen: std::sync::Mutex::new(Vec::new()),
        });
        let cancel = AtomicBool::new(false);

        UiSnapshotTool { backend: b.clone() }
            .call(&json!({"max_nodes": 7, "max_depth": 3}), &cancel)
            .await
            .unwrap();
        UiWaitTool { backend: b.clone() }
            .call(
                &json!({"selector": "text=Go", "timeout_ms": 999_999}),
                &cancel,
            )
            .await
            .unwrap();
        UiFindTool { backend: b.clone() }
            .call(&json!({"selector": "id=x", "limit": 500}), &cancel)
            .await
            .unwrap();

        let seen = b.seen.lock().unwrap();
        assert_eq!(seen[0].0, "ui.snapshot");
        assert_eq!(seen[0].1["limits"]["max_nodes"], 7);
        assert_eq!(seen[0].1["limits"]["max_depth"], 3);
        assert_eq!(seen[1].0, "ui.wait");
        assert_eq!(seen[1].1["timeout_ms"], 10_000);
        assert_eq!(seen[2].0, "ui.find");
        assert_eq!(seen[2].1["selector"], "id=x");
    }

    #[test]
    fn default_registry_exposes_read_only_ui_tools() {
        use crate::config::AgentConfig;
        use crate::tools::backend::NativeShell;
        use crate::tools::registry::is_read_only;

        let cfg = AgentConfig::default();
        let reg = crate::tools::build_default_registry(
            Arc::new(NativeShell::new()),
            std::env::temp_dir(),
            &cfg,
        );
        for name in [
            "ui_backends",
            "ui_list_windows",
            "ui_snapshot",
            "ui_find",
            "ui_wait",
        ] {
            assert!(reg.contains(name), "{name} missing from registry");
            assert!(is_read_only(name), "{name} must be read-only");
        }
        for name in ["ui_act", "ui_gesture", "ui_set_progress"] {
            assert!(
                !reg.contains(name),
                "{name} must not be exposed to the model"
            );
        }
    }

    #[tokio::test]
    async fn no_host_backend_is_reported() {
        // NativeShell has no plugin → ui_call returns None.
        let b: Arc<dyn ShellBackend> = Arc::new(crate::tools::backend::NativeShell::new());
        let cancel = AtomicBool::new(false);
        let v = parse(
            UiSnapshotTool { backend: b }
                .call(&json!({}), &cancel)
                .await
                .unwrap(),
        );
        assert_eq!(v["success"], false);
        assert!(v["error"].as_str().unwrap().contains("unavailable"));
    }
}