pi_agent_rust 0.7.1

Native AI coding agent CLI - Rust port of Pi Agent
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
//! Frame-local DOM lookup and verified page-viewport input coordinates.
//!
//! The CDP connection stays attached to the page target. Same-process frame
//! nodes are resolved in their isolated world; input still uses native page
//! events. Never reuse frame-local getBoundingClientRect coordinates for them.

use super::{Cdp, document, element_call};
use crate::agent_cx::AgentCx;
use crate::error::{Error, Result};
use serde_json::{Value, json};

const MAX_QUADS: usize = 64;
const MAX_COORDINATE: f64 = 1_000_000.0;
const QUERY: &str =
    "function(selector) { return Document.prototype.querySelector.call(document, selector); }";
const CONTAINS_HIT: &str = r"function(hit) {
    if (!this.isConnected || this.ownerDocument !== document ||
        !hit || hit.ownerDocument !== document || !hit.isConnected) return false;
    for (let node = hit, depth = 0; node && depth < 256; ++depth) {
        if (node === this) return true;
        node = node.parentNode || (node.getRootNode && node.getRootNode().host);
    }
    return false;
}";

fn error(message: &'static str) -> Error {
    Error::tool("browser", message)
}

async fn world(owner: &AgentCx, cdp: &mut Cdp) -> Result<u64> {
    let doc = document(owner, cdp).await?;
    let response = cdp
        .command(
            owner,
            "Page.createIsolatedWorld",
            json!({"frameId": doc.frame, "worldName": "pi-browser-tools"}),
        )
        .await?;
    let context = response["executionContextId"]
        .as_u64()
        .filter(|id| *id > 0)
        .ok_or_else(|| error("selected frame has no isolated world"))?;
    if document(owner, cdp).await? != doc {
        return Err(error("selected frame navigated during DOM lookup"));
    }
    Ok(context)
}

fn remote_id(response: &Value) -> Result<&str> {
    if response.get("exceptionDetails").is_some() {
        return Err(error(
            "frame DOM lookup failed; check the selector and take a new snapshot",
        ));
    }
    response["result"]["objectId"]
        .as_str()
        .filter(|id| !id.is_empty())
        .ok_or_else(|| error("frame DOM lookup returned no object handle"))
}

async fn release(owner: &AgentCx, cdp: &mut Cdp, id: &str) {
    let _ = cdp
        .command(owner, "Runtime.releaseObject", json!({"objectId": id}))
        .await;
}

/// Selector text is a JSON argument, never interpolated into executable source.
/// No-match is distinct from malformed selectors, stale worlds or protocol errors.
pub(super) async fn resolve(owner: &AgentCx, cdp: &mut Cdp, selector: &str) -> Result<Option<u64>> {
    let context = world(owner, cdp).await?;
    let response = cdp
        .command(
            owner,
            "Runtime.callFunctionOn",
            json!({
                "executionContextId": context, "functionDeclaration": QUERY,
                "arguments": [{"value": selector}], "returnByValue": false,
            }),
        )
        .await?;
    if response.get("exceptionDetails").is_none()
        && response["result"]["type"] == "object"
        && response["result"]["subtype"] == "null"
    {
        document(owner, cdp).await?;
        return Ok(None);
    }
    let id = remote_id(&response)?;
    let described = cdp
        .command(
            owner,
            "DOM.describeNode",
            json!({"objectId": id, "depth": 0}),
        )
        .await;
    release(owner, cdp, id).await;
    let backend = described?["node"]["backendNodeId"]
        .as_u64()
        .filter(|id| *id > 0)
        .ok_or_else(|| error("frame selector returned no backend node ID"))?;
    document(owner, cdp).await?;
    Ok(Some(backend))
}

pub(super) async fn ensure_focus(owner: &AgentCx, cdp: &mut Cdp, id: u64) -> Result<()> {
    document(owner, cdp).await?;
    if element_call(owner, cdp, id, "focused", json!({})).await? != true {
        return Err(error(
            "selected frame control lost focus; no native input was sent",
        ));
    }
    document(owner, cdp).await?;
    Ok(())
}

#[derive(Clone, Copy, Debug)]
struct Point {
    x: f64,
    y: f64,
}

fn cross(a: Point, b: Point, c: Point) -> f64 {
    (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x)
}

fn area(points: &[Point]) -> f64 {
    if points.len() < 3 {
        return 0.0;
    }
    points
        .iter()
        .zip(points.iter().cycle().skip(1))
        .map(|(a, b)| a.x * b.y - a.y * b.x)
        .sum::<f64>()
        .abs()
        / 2.0
}

fn parse_quad(value: &Value) -> Result<Vec<Point>> {
    let coordinates = value
        .as_array()
        .filter(|items| items.len() == 8)
        .ok_or_else(|| error("browser returned an invalid content quad"))?;
    let number = |value: &Value| {
        value
            .as_f64()
            .filter(|value| value.is_finite() && value.abs() <= MAX_COORDINATE)
            .ok_or_else(|| error("browser returned an invalid content-quad coordinate"))
    };
    let mut points = Vec::with_capacity(4);
    for [x, y] in coordinates.as_chunks::<2>().0 {
        points.push(Point {
            x: number(x)?,
            y: number(y)?,
        });
    }
    let turns: Vec<_> = (0..4)
        .map(|i| cross(points[i], points[(i + 1) % 4], points[(i + 2) % 4]))
        .collect();
    if turns.iter().any(|value| *value > 0.0) && turns.iter().any(|value| *value < 0.0) {
        return Err(error("browser returned a folded content quad"));
    }
    Ok(points)
}

/// Sutherland-Hodgman clipping preserves rotated quads. Clamping individual
/// vertices would invent a polygon that does not describe the visible element.
fn clip(points: Vec<Point>, axis_x: bool, boundary: f64, keep_greater: bool) -> Vec<Point> {
    let mut output = Vec::new();
    let coordinate = |point: Point| if axis_x { point.x } else { point.y };
    let inside = |point: Point| {
        if keep_greater {
            coordinate(point) >= boundary
        } else {
            coordinate(point) <= boundary
        }
    };
    let Some(mut previous) = points.last().copied() else {
        return output;
    };
    for current in points {
        if inside(previous) != inside(current) {
            let ratio =
                (boundary - coordinate(previous)) / (coordinate(current) - coordinate(previous));
            output.push(Point {
                x: previous.x + ratio * (current.x - previous.x),
                y: previous.y + ratio * (current.y - previous.y),
            });
        }
        if inside(current) {
            output.push(current);
        }
        previous = current;
    }
    output
}

fn contains(points: &[Point], point: Point) -> bool {
    let turns: Vec<_> = points
        .iter()
        .zip(points.iter().cycle().skip(1))
        .map(|(a, b)| cross(*a, *b, point))
        .collect();
    !(turns.iter().any(|value| *value > 0.0) && turns.iter().any(|value| *value < 0.0))
}

fn candidate(value: &Value, width: f64, height: f64) -> Result<Option<(i32, i32)>> {
    let mut points = parse_quad(value)?;
    for (axis, boundary, greater) in [
        (true, 0.0, true),
        (true, width, false),
        (false, 0.0, true),
        (false, height, false),
    ] {
        points = clip(points, axis, boundary, greater);
    }
    if area(&points) <= 1.0 {
        return Ok(None);
    }
    let count = u32::try_from(points.len()).map_err(|_| error("invalid clipped quad"))?;
    let point = Point {
        x: (points.iter().map(|p| p.x).sum::<f64>() / f64::from(count)).round(),
        y: (points.iter().map(|p| p.y).sum::<f64>() / f64::from(count)).round(),
    };
    if point.x < 0.0
        || point.y < 0.0
        || point.x >= width
        || point.y >= height
        || !contains(&points, point)
    {
        return Ok(None);
    }
    // Inputs are finite and bounded to +/-1,000,000, far inside i32's range.
    #[allow(clippy::cast_possible_truncation)]
    let pixel = (point.x as i32, point.y as i32);
    Ok(Some(pixel))
}

async fn contains_hit(owner: &AgentCx, cdp: &mut Cdp, target: u64, hit: u64) -> Result<bool> {
    let context = world(owner, cdp).await?;
    let target = cdp
        .command(
            owner,
            "DOM.resolveNode",
            json!({"backendNodeId":target,"executionContextId":context}),
        )
        .await?;
    let target_id = target["object"]["objectId"]
        .as_str()
        .ok_or_else(|| error("click target has no DOM handle"))?;
    let result = async {
        let hit = cdp
            .command(
                owner,
                "DOM.resolveNode",
                json!({"backendNodeId":hit,"executionContextId":context}),
            )
            .await?;
        let hit_id = hit["object"]["objectId"]
            .as_str()
            .ok_or_else(|| error("hit-test node has no DOM handle"))?;
        let response = cdp
            .command(
                owner,
                "Runtime.callFunctionOn",
                json!({
                    "objectId":target_id,"functionDeclaration":CONTAINS_HIT,
                    "arguments":[{"objectId":hit_id}],"returnByValue":true,
                }),
            )
            .await;
        release(owner, cdp, hit_id).await;
        let response = response?;
        if response.get("exceptionDetails").is_some() {
            return Err(error("frame click hit-test failed"));
        }
        Ok(response["result"]["value"] == true)
    }
    .await;
    release(owner, cdp, target_id).await;
    result
}

/// Select a page-viewport point and verify Chromium's hit test there belongs
/// to this frame and this element (or its composed descendant). Ancestor
/// overlays, sibling frames and browser coordinate inconsistencies fail closed.
pub(super) async fn click_point(owner: &AgentCx, cdp: &mut Cdp, id: u64) -> Result<(i32, i32)> {
    let doc = document(owner, cdp).await?;
    // Retain native element visibility, enabled/inert and local occlusion checks.
    element_call(owner, cdp, id, "point", json!({})).await?;
    let response = cdp
        .command(owner, "DOM.getContentQuads", json!({"backendNodeId":id}))
        .await?;
    let quads = response["quads"]
        .as_array()
        .filter(|quads| quads.len() <= MAX_QUADS)
        .ok_or_else(|| error("browser returned too many or invalid content quads"))?;
    let metrics = cdp
        .command(owner, "Page.getLayoutMetrics", json!({}))
        .await?;
    let viewport = metrics
        .get("cssLayoutViewport")
        .or_else(|| metrics.get("layoutViewport"))
        .ok_or_else(|| error("browser returned no layout viewport"))?;
    let dimension = |key: &str| {
        viewport[key]
            .as_f64()
            .filter(|n| n.is_finite() && *n > 0.0 && *n <= MAX_COORDINATE)
            .ok_or_else(|| error("browser returned an invalid layout viewport"))
    };
    let width = dimension("clientWidth")?;
    let height = dimension("clientHeight")?;
    for quad in quads {
        let Some((x, y)) = candidate(quad, width, height)? else {
            continue;
        };
        let hit = cdp
            .command(
                owner,
                "DOM.getNodeForLocation",
                json!({
                    "x":x,"y":y,"includeUserAgentShadowDOM":false,"ignorePointerEventsNone":false,
                }),
            )
            .await?;
        if hit["frameId"].as_str() != Some(doc.frame.as_str()) {
            continue;
        }
        let hit_id = hit["backendNodeId"]
            .as_u64()
            .filter(|id| *id > 0)
            .ok_or_else(|| error("browser hit-test returned no node"))?;
        if contains_hit(owner, cdp, id, hit_id).await? {
            if document(owner, cdp).await? != doc {
                return Err(error("selected frame navigated before input dispatch"));
            }
            return Ok((x, y));
        }
    }
    Err(error(
        "selected frame element has no unobstructed page-viewport input point",
    ))
}

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

    #[test]
    fn iframe_offset_coordinates_are_not_replaced_with_local_rect_coordinates() {
        assert_eq!(
            candidate(
                &json!([300, 200, 400, 200, 400, 240, 300, 240]),
                1000.0,
                800.0
            )
            .unwrap(),
            Some((350, 220))
        );
    }

    #[test]
    fn partially_visible_and_rotated_quads_are_clipped_before_choosing_a_point() {
        assert_eq!(
            candidate(&json!([-100, 10, 100, 10, 100, 50, -100, 50]), 800.0, 600.0).unwrap(),
            Some((50, 30))
        );
        let quad = json!([0, -100, 100, 0, 0, 100, -100, 0]);
        let (x, y) = candidate(&quad, 800.0, 600.0).unwrap().unwrap();
        assert!(x > 0 && y > 0 && x + y <= 100);
    }

    #[test]
    fn invisible_degenerate_and_subpixel_quads_do_not_generate_input() {
        for quad in [
            json!([900, 0, 950, 0, 950, 30, 900, 30]),
            json!([1, 1, 1, 1, 1, 1, 1, 1]),
            json!([0, 0, 0.5, 0, 0.5, 0.5, 0, 0.5]),
        ] {
            assert!(candidate(&quad, 800.0, 600.0).unwrap().is_none());
        }
    }

    #[test]
    fn malformed_and_folded_geometry_is_an_error_not_a_guess() {
        for quad in [
            json!([]),
            json!([0, 0, 1, 0, 1, 1, 0, "x"]),
            json!([0, 0, 1, 0, 1, 1, 0, 1e100]),
            json!([0, 0, 100, 100, 0, 100, 100, 0]),
        ] {
            assert!(candidate(&quad, 800.0, 600.0).is_err());
        }
    }

    #[test]
    fn remote_handle_failures_do_not_echo_page_exception_values() {
        let response =
            json!({"exceptionDetails":{"text":"PRIVATE-CANARY"},"result":{"objectId":"object"}});
        let error = remote_id(&response).unwrap_err().to_string();
        assert!(!error.contains("PRIVATE-CANARY"));
        assert!(remote_id(&json!({"result":{"type":"undefined"}})).is_err());
        assert_eq!(
            remote_id(&json!({"result":{"objectId":"valid"}})).unwrap(),
            "valid"
        );
    }
}