iphone-cli 0.1.5

Let AI agents read and operate any app on a real iPhone through the accessibility tree. No XCUITest, WebDriverAgent or Xcode signing.
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
//! Turn a raw dump into the element list `iph read` prints.

use std::collections::HashSet;

use serde::{Deserialize, Serialize};

use super::{
    archive::Ka,
    attrs::{self, traits},
    dump::{Dump, ObjKey, RawElement},
};

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Rect {
    pub x: f64,
    pub y: f64,
    pub w: f64,
    pub h: f64,
}

impl Rect {
    pub fn center(&self) -> (f64, f64) {
        (self.x + self.w / 2.0, self.y + self.h / 2.0)
    }

    pub fn contains(&self, x: f64, y: f64) -> bool {
        x >= self.x && x <= self.x + self.w && y >= self.y && y <= self.y + self.h
    }

    pub fn area(&self) -> f64 {
        self.w.max(0.0) * self.h.max(0.0)
    }

    pub fn intersection(&self, other: &Rect) -> Option<Rect> {
        let x1 = self.x.max(other.x);
        let y1 = self.y.max(other.y);
        let x2 = (self.x + self.w).min(other.x + other.w);
        let y2 = (self.y + self.h).min(other.y + other.h);
        (x2 > x1 && y2 > y1).then_some(Rect {
            x: x1,
            y: y1,
            w: x2 - x1,
            h: y2 - y1,
        })
    }

    /// Parse `{{x, y}, {w, h}}`.
    pub fn parse(s: &str) -> Option<Rect> {
        let nums: Vec<f64> = s
            .split(|c: char| c == '{' || c == '}' || c == ',' || c.is_whitespace())
            .filter(|p| !p.is_empty())
            .map(|p| p.parse::<f64>())
            .collect::<Result<_, _>>()
            .ok()?;
        if nums.len() != 4 || nums.iter().any(|n| !n.is_finite()) {
            return None;
        }
        Some(Rect {
            x: round1(nums[0]),
            y: round1(nums[1]),
            w: round1(nums[2]),
            h: round1(nums[3]),
        })
    }
}

fn round1(v: f64) -> f64 {
    (v * 10.0).round() / 10.0
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Element {
    /// Short handle for this element, stable while the element lives.
    pub id: String,
    pub role: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub label: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub identifier: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub hint: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub frame: Option<Rect>,
    #[serde(default)]
    pub traits: Vec<String>,
    #[serde(default = "yes")]
    pub enabled: bool,
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub selected: bool,
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub focused: bool,
    /// `id` of the parent element, absent for an application.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parent: Option<String>,
    #[serde(default)]
    pub depth: u32,
    #[serde(default)]
    pub pid: i64,
    /// False when the element lies outside the screen.
    #[serde(default = "yes")]
    pub on_screen: bool,
    /// Every numbered attribute, only with `--raw`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub raw: Option<serde_json::Value>,
    /// The phone's name for the element, to ask it about one attribute.
    #[serde(skip)]
    pub key: Option<ObjKey>,
}

fn yes() -> bool {
    true
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct AppInfo {
    pub pid: i64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bundle_id: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Screen {
    /// Logical screen size in points.
    pub width: f64,
    pub height: f64,
    pub apps: Vec<AppInfo>,
    pub elements: Vec<Element>,
}

/// Role, label, value and rounded frame of one element.
pub type ElementSignature = (String, Option<String>, Option<String>, Option<(i64, i64, i64, i64)>);

#[derive(Debug, Clone, Copy, Default)]
pub struct BuildOptions {
    /// Keep elements that lie outside the screen and walk hidden children.
    pub include_offscreen: bool,
    /// Attach every numbered attribute to each element.
    pub raw: bool,
}

fn handle(key: &ObjKey) -> String {
    // Fold the 64-bit object id and the pid into 32 bits; unique enough within
    // one screen, and `Screen::build` de-duplicates the rare collision.
    let mut h = key.object_id ^ (key.object_id >> 32) ^ ((key.pid as u64) << 17);
    h = h.wrapping_mul(0x9E37_79B9_7F4A_7C15);
    format!("{:08x}", (h >> 32) as u32)
}

/// A readable value, including an empty or whitespace-only string. Missing
/// attributes and unsupported values are not evidence of an empty field.
pub(crate) fn value_text(v: Option<&Ka>) -> Option<String> {
    Some(match v? {
        Ka::Str(s) => s.clone(),
        other @ Ka::Obj { .. } => other.text()?,
        Ka::Int(i) => i.to_string(),
        Ka::UInt(u) => u.to_string(),
        Ka::Real(r) => r.to_string(),
        _ => return None,
    })
}

pub(crate) fn text(v: Option<&Ka>) -> Option<String> {
    let s = value_text(v)?;
    let t = s.trim();
    (!t.is_empty()).then(|| t.to_string())
}

fn frame_of(raw: &RawElement) -> Option<Rect> {
    raw.attrs
        .get(&attrs::FRAME)
        .and_then(|v| v.get("NS.rectval"))
        .and_then(|v| v.as_str())
        .and_then(Rect::parse)
}

pub(crate) fn children_of(raw: &RawElement, include_offscreen: bool) -> Vec<ObjKey> {
    let pick = |n: i64| -> Vec<ObjKey> {
        raw.attrs
            .get(&n)
            .and_then(|v| v.as_array())
            .map(|a| a.iter().filter_map(ObjKey::from_ka).collect())
            .unwrap_or_default()
    };
    let (first, second) = if include_offscreen {
        (attrs::CHILDREN, attrs::VISIBLE_CHILDREN)
    } else {
        (attrs::VISIBLE_CHILDREN, attrs::CHILDREN)
    };
    let list = pick(first);
    if list.is_empty() { pick(second) } else { list }
}

impl Screen {
    pub fn build(dump: &Dump, opts: BuildOptions) -> Screen {
        let mut screen = Screen {
            width: 0.0,
            height: 0.0,
            apps: Vec::new(),
            elements: Vec::new(),
        };
        // The screen is as large as the largest application frame.
        for key in &dump.applications {
            if let Some(f) = dump.elements.get(key).and_then(frame_of) {
                screen.width = screen.width.max(f.x + f.w);
                screen.height = screen.height.max(f.y + f.h);
            }
        }
        let bounds = Rect {
            x: 0.0,
            y: 0.0,
            w: screen.width,
            h: screen.height,
        };

        let mut seen: HashSet<ObjKey> = HashSet::new();
        let mut used_ids: HashSet<String> = HashSet::new();
        // Depth-first from each application, in the phone's own child order.
        let mut stack: Vec<(ObjKey, Option<String>, u32)> = dump
            .applications
            .iter()
            .rev()
            .map(|k| (*k, None, 0))
            .collect();
        // Elements the phone described but no application reaches (rare).
        let mut orphans: Vec<ObjKey> = Vec::new();

        loop {
            let Some((key, parent, depth)) = stack.pop() else {
                if orphans.is_empty() {
                    orphans = dump
                        .order
                        .iter()
                        .filter(|k| !seen.contains(k))
                        .copied()
                        .collect();
                    if orphans.is_empty() {
                        break;
                    }
                    for k in orphans.iter().rev() {
                        stack.push((*k, None, 1));
                    }
                    continue;
                }
                break;
            };
            if !seen.insert(key) {
                continue;
            }
            let Some(raw) = dump.elements.get(&key) else {
                continue;
            };
            let bits = raw
                .attrs
                .get(&attrs::TRAITS)
                .and_then(|v| v.as_u64())
                .unwrap_or(0);
            let role_num = raw.attrs.get(&attrs::ROLE).and_then(|v| v.as_i64());
            let frame = frame_of(raw);
            let on_screen = match (&frame, bounds.area() > 0.0) {
                (Some(f), true) => f.intersection(&bounds).is_some(),
                (Some(f), false) => f.area() > 0.0,
                (None, _) => raw.is_application,
            };

            let mut id = handle(&key);
            while !used_ids.insert(id.clone()) {
                id.push('x');
            }
            for child in children_of(raw, opts.include_offscreen).into_iter().rev() {
                stack.push((child, Some(id.clone()), depth + 1));
            }
            if raw.is_application {
                screen.apps.push(AppInfo {
                    pid: key.pid,
                    name: text(raw.attrs.get(&attrs::LABEL))
                        .or_else(|| text(raw.attrs.get(&attrs::LABEL_PLAIN))),
                    bundle_id: None,
                });
            }
            if !on_screen && !opts.include_offscreen && !raw.is_application {
                continue;
            }
            let label = text(raw.attrs.get(&attrs::LABEL))
                .or_else(|| text(raw.attrs.get(&attrs::LABEL_PLAIN)));
            let value = value_text(raw.attrs.get(&attrs::VALUE));
            let has_text = label.is_some() || value.as_ref().is_some_and(|v| !v.trim().is_empty());
            screen.elements.push(Element {
                id,
                role: attrs::role_name(role_num, bits, raw.is_application, has_text).to_string(),
                label,
                value,
                identifier: text(raw.attrs.get(&attrs::IDENTIFIER)),
                hint: text(raw.attrs.get(&attrs::HINT)),
                frame,
                traits: attrs::trait_names(bits)
                    .into_iter()
                    .map(String::from)
                    .collect(),
                enabled: bits & traits::NOT_ENABLED == 0,
                selected: bits & traits::SELECTED != 0,
                focused: bits & traits::IS_EDITING != 0,
                parent,
                depth,
                pid: key.pid,
                key: Some(key),
                on_screen,
                raw: opts.raw.then(|| {
                    let mut m = serde_json::Map::new();
                    for (n, v) in &raw.attrs {
                        m.insert(n.to_string(), v.to_json());
                    }
                    for (n, v) in &raw.singles {
                        m.insert(format!("single_{n}"), v.to_json());
                    }
                    serde_json::Value::Object(m)
                }),
            });
        }
        screen
    }

    /// What a comparison of two readings looks at: everything a user could
    /// see or an agent could act on, without handles.
    pub fn signature(&self) -> Vec<ElementSignature> {
        self.elements
            .iter()
            .map(|e| {
                (
                    e.role.clone(),
                    e.label.clone(),
                    e.value.clone(),
                    e.frame.map(|f| {
                        (
                            f.x.round() as i64,
                            f.y.round() as i64,
                            f.w.round() as i64,
                            f.h.round() as i64,
                        )
                    }),
                )
            })
            .collect()
    }

    pub fn by_id(&self, id: &str) -> Option<&Element> {
        self.elements.iter().find(|e| e.id == id)
    }

    /// The smallest on-screen element whose frame contains the point.
    pub fn element_at(&self, x: f64, y: f64) -> Option<&Element> {
        self.elements
            .iter()
            .filter(|e| e.role != "application")
            .filter_map(|e| e.frame.map(|f| (e, f)))
            .filter(|(_, f)| f.contains(x, y))
            .min_by(|a, b| a.1.area().total_cmp(&b.1.area()))
            .map(|(e, _)| e)
    }

    /// A short description of the screen for error reports: app names and the
    /// first few labels.
    pub fn summary(&self, max_labels: usize) -> serde_json::Value {
        let labels: Vec<&str> = self
            .elements
            .iter()
            .filter(|e| e.role != "application")
            .filter_map(|e| e.label.as_deref())
            .take(max_labels)
            .collect();
        serde_json::json!({
            "apps": self.apps.iter().filter_map(|a| a.name.clone()).collect::<Vec<_>>(),
            "element_count": self.elements.len(),
            "labels": labels,
        })
    }
}

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

    #[test]
    fn field_values_keep_empty_and_whitespace_distinct_from_missing() {
        for (raw, expected) in [
            (Ka::Str(String::new()), Some("")),
            (Ka::Str(" \n ".into()), Some(" \n ")),
            (Ka::Str("  hello  ".into()), Some("  hello  ")),
            (Ka::Int(0), Some("0")),
            (Ka::Null, None),
            (Ka::Bool(false), None),
            (
                Ka::Obj {
                    class: "Unknown".into(),
                    fields: vec![],
                },
                None,
            ),
        ] {
            assert_eq!(value_text(Some(&raw)).as_deref(), expected);
        }
        assert_eq!(value_text(None), None);
        // Labels keep their existing whitespace normalization.
        assert_eq!(
            text(Some(&Ka::Str("  hello  ".into()))).as_deref(),
            Some("hello")
        );
    }

    #[test]
    fn screen_build_preserves_an_empty_field_value() {
        use crate::ax::dump::DumpKind;
        let key = ObjKey {
            pid: 10,
            object_id: 1,
        };
        let mut dump = Dump {
            kind: DumpKind::Initial,
            applications: vec![key],
            elements: [(
                key,
                RawElement {
                    is_application: true,
                    ..Default::default()
                },
            )]
            .into(),
            order: vec![key],
        };
        let missing = Screen::build(&dump, BuildOptions::default());
        dump.elements
            .get_mut(&key)
            .unwrap()
            .attrs
            .insert(attrs::VALUE, Ka::Str(String::new()));
        let empty = Screen::build(&dump, BuildOptions::default());
        assert_eq!(missing.elements[0].value, None);
        assert_eq!(empty.elements[0].value.as_deref(), Some(""));
        assert_ne!(missing.signature(), empty.signature());
    }

    #[test]
    fn parses_rect_strings() {
        let r = Rect::parse("{{20, 59}, {44, 44}}").unwrap();
        assert_eq!((r.x, r.y, r.w, r.h), (20.0, 59.0, 44.0, 44.0));
        let r = Rect::parse("{{168.66666666666669, 70.666666666666657}, {92.3, 20.67}}").unwrap();
        assert_eq!(r.x, 168.7);
        assert_eq!(r.center().0, 168.7 + 92.3 / 2.0);
        assert!(Rect::parse("{{nan, 0}, {1, 1}}").is_none());
        assert!(Rect::parse("{1, 2}").is_none());
    }

    #[test]
    fn intersection_detects_offscreen() {
        let screen = Rect { x: 0.0, y: 0.0, w: 430.0, h: 932.0 };
        let below = Rect { x: 20.0, y: 1000.0, w: 390.0, h: 50.0 };
        let partly = Rect { x: 20.0, y: 900.0, w: 390.0, h: 50.0 };
        assert!(below.intersection(&screen).is_none());
        assert!(partly.intersection(&screen).is_some());
    }
}