fastshell 0.3.3

Lightweight Bash + Python runtime SDK 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
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
508
// Copyright (c) 2025 xiefujin <490021684@qq.com>
// Licensed under Apache-2.0, see LICENSE file for full license terms.

use crate::sdk::plugin::DevicePlugin;
use crate::shell::{CommandOutput, Shell};

pub(crate) fn plugin<T>(
    shell: &Shell,
    f: impl FnOnce(&Box<dyn DevicePlugin>) -> Result<T, String>,
) -> Result<T, CommandOutput> {
    let guard = match shell.plugin.lock() {
        Ok(g) => g,
        Err(poisoned) => poisoned.into_inner(),
    };
    match guard.as_ref() {
        Some(p) => f(p).map_err(|e| CommandOutput::error(e, 1)),
        None => Err(CommandOutput::not_supported("device plugin")),
    }
}

impl Shell {
    /// Resolve a device-command path (VFS semantics: `/x` = sandbox root,
    /// relative = cwd) into a **host absolute path** before handing it to the
    /// device plugin.
    ///
    /// Rationale: agent tasks run in their own sandbox (the project dir), not
    /// the app-global one. The Kotlin side used to join every path onto the
    /// global sandbox root, so `camera /photo.jpg` from an agent task saved
    /// the photo where the agent could never see it. Resolving here — against
    /// THIS instance's VFS — puts device files exactly where the caller's
    /// `ls` will find them, and keeps the path-escape protection.
    pub(crate) fn device_host_path(&self, vpath: &str) -> Result<String, CommandOutput> {
        match self.vfs.resolve(vpath, &self.cwd) {
            Ok(p) => Ok(p.to_string_lossy().to_string()),
            Err(e) => Err(CommandOutput::error(format!("{vpath}: {e}\n"), 1)),
        }
    }
}

impl Shell {
    // ── camera ──
    pub fn cmd_camera(&self, args: &[&str]) -> CommandOutput {
        let path = args
            .first()
            .map(|s| s.to_string())
            .unwrap_or_else(|| "/photo.jpg".to_string());
        if let Some(perm) = self.check_device_permission("camera", "photo") {
            return perm;
        }
        let host = match self.device_host_path(&path) { Ok(h) => h, Err(e) => return e };
        match plugin(self, |p| p.take_photo(&host)) {
            Ok(()) => CommandOutput::success(format!("Photo saved to {}\n", path)),
            Err(e) => e,
        }
    }

    // ── screencapture ──
    pub fn cmd_screencapture(&self, args: &[&str]) -> CommandOutput {
        let path = args
            .first()
            .map(|s| s.to_string())
            .unwrap_or_else(|| "/screenshot.png".to_string());
        if let Some(perm) = self.check_device_permission("screen", "capture") {
            return perm;
        }
        let host = match self.device_host_path(&path) { Ok(h) => h, Err(e) => return e };
        match plugin(self, |p| p.take_screenshot(&host)) {
            Ok(()) => CommandOutput::success(format!("Screenshot saved to {}\n", path)),
            Err(e) => e,
        }
    }

    // ── photolib ──
    pub fn cmd_photolib(&self, args: &[&str]) -> CommandOutput {
        let mut media_type = "image";
        let mut count = 1u32;
        let mut output_dir = "/".to_string();
        let mut i = 0;
        while i < args.len() {
            match args[i] {
                "--video" => media_type = "video",
                "-n" | "--count" => {
                    if i + 1 < args.len() {
                        count = args[i + 1].parse::<u32>().unwrap_or(1).max(1);
                        i += 1;
                    }
                }
                "-o" | "--output" => {
                    if i + 1 < args.len() {
                        output_dir = args[i + 1].to_string();
                        i += 1;
                    }
                }
                arg if !arg.starts_with('-') => {
                    output_dir = arg.to_string();
                }
                _ => {}
            }
            i += 1;
        }
        if let Some(perm) = self.check_device_permission("photolib", "read") {
            return perm;
        }
        let output_dir = match self.device_host_path(&output_dir) { Ok(h) => h, Err(e) => return e };
        match plugin(self, |p| {
            if media_type == "video" {
                p.pick_video(&output_dir).map(|p| {
                    serde_json::to_string_pretty(&serde_json::json!({"video": p})).unwrap_or(p)
                })
            } else {
                let files = p.pick_photo(&output_dir, count)?;
                let json = serde_json::json!({"photos": files});
                serde_json::to_string_pretty(&json).map_err(|e| e.to_string())
            }
        }) {
            Ok(out) => CommandOutput::success(format!("{}\n", out)),
            Err(e) => e,
        }
    }

    // ── record ──
    pub fn cmd_record(&self, args: &[&str]) -> CommandOutput {
        let mut path = "/recording.wav".to_string();
        let mut duration: u32 = 10;
        let mut i = 0;
        while i < args.len() {
            match args[i] {
                "-d" | "--duration" => {
                    if i + 1 < args.len() {
                        duration = args[i + 1].parse().unwrap_or(10);
                        i += 1;
                    }
                }
                "-o" | "--output" => {
                    if i + 1 < args.len() {
                        path = args[i + 1].to_string();
                        i += 1;
                    }
                }
                arg if !arg.starts_with('-') => {
                    path = arg.to_string();
                }
                _ => {}
            }
            i += 1;
        }
        if let Some(perm) = self.check_device_permission("microphone", "record") {
            return perm;
        }
        let duration = duration.max(1);
        let host = match self.device_host_path(&path) { Ok(h) => h, Err(e) => return e };
        match plugin(self, |p| p.record_audio(&host, duration)) {
            Ok(()) => CommandOutput::success(format!("Recorded {}s to {}\n", duration, path)),
            Err(e) => e,
        }
    }

    // ── play ──
    pub fn cmd_play(&self, args: &[&str]) -> CommandOutput {
        let path = match args.first() {
            Some(p) => p.to_string(),
            None => return CommandOutput::error("play: missing file path\n".to_string(), 1),
        };
        let host = match self.device_host_path(&path) { Ok(h) => h, Err(e) => return e };
        match plugin(self, |p| p.play_audio(&host)) {
            Ok(()) => CommandOutput::success(String::new()),
            Err(e) => e,
        }
    }

    // ── say ──
    pub fn cmd_say(&self, args: &[&str]) -> CommandOutput {
        let text = args.join(" ");
        if text.is_empty() {
            return CommandOutput::error("say: missing text\n".to_string(), 1);
        }
        match plugin(self, |p| p.text_to_speech(&text)) {
            Ok(()) => CommandOutput::success(String::new()),
            Err(e) => e,
        }
    }

    // ── speech ──
    pub fn cmd_speech(&self, args: &[&str]) -> CommandOutput {
        let path = match args.first() {
            Some(p) => p.to_string(),
            None => {
                return CommandOutput::error("speech: missing audio file path\n".to_string(), 1)
            }
        };
        if let Some(perm) = self.check_device_permission("microphone", "speech") {
            return perm;
        }
        let host = match self.device_host_path(&path) { Ok(h) => h, Err(e) => return e };
        match plugin(self, |p| p.speech_to_text(&host)) {
            Ok(text) => CommandOutput::success(format!("{}\n", text)),
            Err(e) => e,
        }
    }

    // ── contacts ──
    pub fn cmd_contacts(&self, args: &[&str]) -> CommandOutput {
        let mut query = String::new();
        let mut limit = 50u32;
        let mut contact_id: Option<String> = None;
        let mut i = 0;
        while i < args.len() {
            match args[i] {
                "get" => {
                    if i + 1 < args.len() {
                        contact_id = Some(args[i + 1].to_string());
                        i += 1;
                    }
                }
                "search" => {
                    if i + 1 < args.len() {
                        query = args[i + 1].to_string();
                        i += 1;
                    }
                }
                "-n" | "--limit" => {
                    if i + 1 < args.len() {
                        limit = args[i + 1].parse().unwrap_or(50);
                        i += 1;
                    }
                }
                _ => {}
            }
            i += 1;
        }
        if let Some(perm) = self.check_device_permission("contacts", "read") {
            return perm;
        }
        match plugin(self, |p| {
            if let Some(cid) = contact_id {
                let c = p.get_contact(&cid)?;
                serde_json::to_string_pretty(&c).map_err(|e| e.to_string())
            } else {
                let list = p.list_contacts(&query, limit)?;
                serde_json::to_string_pretty(&list).map_err(|e| e.to_string())
            }
        }) {
            Ok(json) => CommandOutput::success(format!("{}\n", json)),
            Err(e) => e,
        }
    }

    // ── location ──
    pub fn cmd_location(&self, _args: &[&str]) -> CommandOutput {
        if let Some(perm) = self.check_device_permission("location", "gps") {
            return perm;
        }
        match plugin(self, |p| {
            let loc = p.get_location()?;
            serde_json::to_string_pretty(&loc).map_err(|e| e.to_string())
        }) {
            Ok(json) => CommandOutput::success(format!("{}\n", json)),
            Err(e) => e,
        }
    }

    // ── clipboard (read) ──
    pub fn cmd_clipboard(&self, args: &[&str]) -> CommandOutput {
        if args.first() == Some(&"set") {
            let text = args[1..].join(" ");
            if text.is_empty() {
                return CommandOutput::error("clipboard set: missing text\n".to_string(), 1);
            }
            return match plugin(self, |p| p.set_clipboard(&text)) {
                Ok(()) => CommandOutput::success(String::new()),
                Err(e) => e,
            };
        }
        match plugin(self, |p| p.get_clipboard()) {
            Ok(text) => CommandOutput::success(format!("{}\n", text)),
            Err(e) => e,
        }
    }

    // ── pbcopy (write clipboard) ──
    pub fn cmd_pbcopy(&self, args: &[&str]) -> CommandOutput {
        let text = args.join(" ");
        if text.is_empty() {
            return CommandOutput::error("pbcopy: missing text\n".to_string(), 1);
        }
        match plugin(self, |p| p.set_clipboard(&text)) {
            Ok(()) => CommandOutput::success(String::new()),
            Err(e) => e,
        }
    }

    // ── sensor ──
    pub fn cmd_sensor(&self, args: &[&str]) -> CommandOutput {
        let sub = args.first().map(|s| *s).unwrap_or("list");
        match sub {
            "orientation" => match plugin(self, |p| {
                let o = p.get_orientation()?;
                serde_json::to_string_pretty(&o).map_err(|e| e.to_string())
            }) {
                Ok(json) => CommandOutput::success(format!("{}\n", json)),
                Err(e) => e,
            },
            "motion" => match plugin(self, |p| {
                let m = p.get_motion()?;
                serde_json::to_string_pretty(&m).map_err(|e| e.to_string())
            }) {
                Ok(json) => CommandOutput::success(format!("{}\n", json)),
                Err(e) => e,
            },
            "light" => match plugin(self, |p| p.get_ambient_light()) {
                Ok(v) => CommandOutput::success(format!("{} lux\n", v)),
                Err(e) => e,
            },
            "proximity" => match plugin(self, |p| p.get_proximity()) {
                Ok(v) => CommandOutput::success(format!("{}\n", if v { "near" } else { "far" })),
                Err(e) => e,
            },
            "list" | _ => match plugin(self, |p| {
                let sensors = p.list_sensors()?;
                serde_json::to_string_pretty(&sensors).map_err(|e| e.to_string())
            }) {
                Ok(json) => CommandOutput::success(format!("{}\n", json)),
                Err(e) => e,
            },
        }
    }

    // ── notify ──
    pub fn cmd_notify(&self, args: &[&str]) -> CommandOutput {
        let (title, body) = if let Some(pos) = args.iter().position(|&a| a == "--") {
            let title = args[..pos].join(" ");
            let body = args[pos + 1..].join(" ");
            (title, body)
        } else if args.len() >= 2 {
            (args[0].to_string(), args[1..].join(" "))
        } else if args.len() == 1 {
            ("fastshell".to_string(), args[0].to_string())
        } else {
            return CommandOutput::error("notify-send: missing message\n".to_string(), 1);
        };
        let sound = args.contains(&"--sound");
        match plugin(self, |p| p.send_notification(&title, &body, sound)) {
            Ok(()) => CommandOutput::success(String::new()),
            Err(e) => e,
        }
    }

    // ── share ──
    pub fn cmd_share(&self, args: &[&str]) -> CommandOutput {
        let mut path: Option<String> = None;
        let mut text: Option<String> = None;
        let mut mime = "application/octet-stream".to_string();
        let mut i = 0;
        while i < args.len() {
            match args[i] {
                "--text" => {
                    if i + 1 < args.len() {
                        text = Some(args[i + 1].to_string());
                        i += 1;
                    }
                }
                "--mime" => {
                    if i + 1 < args.len() {
                        mime = args[i + 1].to_string();
                        i += 1;
                    }
                }
                arg if !arg.starts_with('-') => {
                    path = Some(arg.to_string());
                }
                _ => {}
            }
            i += 1;
        }
        match (path, text) {
            (Some(p), _) => {
                let host = match self.device_host_path(&p) { Ok(h) => h, Err(e) => return e };
                match plugin(self, |p2| p2.share_file(&host, &mime)) {
                    Ok(()) => CommandOutput::success(String::new()),
                    Err(e) => e,
                }
            }
            (_, Some(t)) => match plugin(self, |p2| p2.share_text(&t)) {
                Ok(()) => CommandOutput::success(String::new()),
                Err(e) => e,
            },
            _ => CommandOutput::error("share: missing file or --text\n".to_string(), 1),
        }
    }

    // ── open ──
    pub fn cmd_open_url(&self, args: &[&str]) -> CommandOutput {
        let url = match args.first() {
            Some(u) => u.to_string(),
            None => return CommandOutput::error("open: missing URL\n".to_string(), 1),
        };
        match plugin(self, |p| p.open_url(&url)) {
            Ok(()) => CommandOutput::success(String::new()),
            Err(e) => e,
        }
    }

    // ── auth ──
    pub fn cmd_auth(&self, args: &[&str]) -> CommandOutput {
        let reason = if args.first() == Some(&"bio") {
            args.get(1)
                .map(|s| s.to_string())
                .unwrap_or_else(|| "Authenticate".to_string())
        } else {
            args.join(" ")
        };
        if reason.is_empty() {
            return CommandOutput::error("auth: missing reason\n".to_string(), 1);
        }
        match plugin(self, |p| p.authenticate_biometric(&reason)) {
            Ok(true) => CommandOutput::success("authenticated\n".to_string()),
            Ok(false) => CommandOutput::error("authentication failed\n".to_string(), 1),
            Err(e) => e,
        }
    }

    // ── battery ──
    pub fn cmd_battery(&self, _args: &[&str]) -> CommandOutput {
        match plugin(self, |p| {
            let b = p.get_battery()?;
            serde_json::to_string_pretty(&b).map_err(|e| e.to_string())
        }) {
            Ok(json) => CommandOutput::success(format!("{}\n", json)),
            Err(e) => e,
        }
    }

    // ── vibrate ──
    pub fn cmd_vibrate(&self, args: &[&str]) -> CommandOutput {
        let ms: u32 = args.first().and_then(|s| s.parse().ok()).unwrap_or(200);
        match plugin(self, |p| p.vibrate(ms)) {
            Ok(()) => CommandOutput::success(String::new()),
            Err(e) => e,
        }
    }

    // ── screen ──
    pub fn cmd_screen(&self, args: &[&str]) -> CommandOutput {
        let mut i = 0;
        while i < args.len() {
            match args[i] {
                "brightness" => {
                    if i + 1 < args.len() {
                        if let Ok(level) = args[i + 1].parse::<f64>() {
                            let level = level.clamp(0.0, 1.0);
                            return match plugin(self, |p| p.set_brightness(level)) {
                                Ok(()) => CommandOutput::success(String::new()),
                                Err(e) => e,
                            };
                        }
                    }
                    return CommandOutput::error(
                        "screen brightness: missing or invalid level (0.0-1.0)\n".to_string(),
                        1,
                    );
                }
                "on" => {
                    return match plugin(self, |p| p.keep_screen_on(true)) {
                        Ok(()) => CommandOutput::success(String::new()),
                        Err(e) => e,
                    };
                }
                "off" => {
                    return match plugin(self, |p| p.keep_screen_on(false)) {
                        Ok(()) => CommandOutput::success(String::new()),
                        Err(e) => e,
                    };
                }
                _ => {}
            }
            i += 1;
        }
        CommandOutput::error(
            "screen: usage: screen brightness <0.0-1.0> | on | off\n".to_string(),
            1,
        )
    }

    // ── device ──
    pub fn cmd_device(&self, args: &[&str]) -> CommandOutput {
        let sub = args.first().map(|s| *s);
        match sub {
            Some("info") => match plugin(self, |p| {
                let d = p.get_device_info()?;
                serde_json::to_string_pretty(&d).map_err(|e| e.to_string())
            }) {
                Ok(json) => CommandOutput::success(format!("{}\n", json)),
                Err(e) => e,
            },
            Some("network") => match plugin(self, |p| {
                let n = p.get_network_type()?;
                serde_json::to_string_pretty(&n).map_err(|e| e.to_string())
            }) {
                Ok(json) => CommandOutput::success(format!("{}\n", json)),
                Err(e) => e,
            },
            _ => CommandOutput::error(
                "device: usage: device info | device network\n".to_string(),
                1,
            ),
        }
    }
}