@roam-agent/roam-computer-use 0.0.0-stage → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +80 -2
  3. package/assets/reference/img.jpg +0 -0
  4. package/docs/README.md +45 -0
  5. package/docs/content/docs/explanation/action-pipeline.mdx +36 -0
  6. package/docs/content/docs/explanation/architecture.mdx +161 -0
  7. package/docs/content/docs/explanation/index.mdx +19 -0
  8. package/docs/content/docs/explanation/meta.json +5 -0
  9. package/docs/content/docs/explanation/observation-pipeline.mdx +37 -0
  10. package/docs/content/docs/explanation/platform-boundary.mdx +37 -0
  11. package/docs/content/docs/explanation/state-and-scheduling.mdx +35 -0
  12. package/docs/content/docs/how-to-guides/develop-and-validate.mdx +112 -0
  13. package/docs/content/docs/how-to-guides/index.mdx +12 -0
  14. package/docs/content/docs/how-to-guides/linux.mdx +68 -0
  15. package/docs/content/docs/how-to-guides/meta.json +1 -0
  16. package/docs/content/docs/how-to-guides/troubleshooting.mdx +126 -0
  17. package/docs/content/docs/index.mdx +67 -0
  18. package/docs/content/docs/meta.json +5 -0
  19. package/docs/content/docs/reference/agent/contract.mdx +162 -0
  20. package/docs/content/docs/reference/agent/index.mdx +8 -0
  21. package/docs/content/docs/reference/agent/meta.json +8 -0
  22. package/docs/content/docs/reference/agent/tools.mdx +221 -0
  23. package/docs/content/docs/reference/codebase.mdx +139 -0
  24. package/docs/content/docs/reference/configuration.mdx +113 -0
  25. package/docs/content/docs/reference/index.mdx +28 -0
  26. package/docs/content/docs/reference/meta.json +10 -0
  27. package/docs/content/docs/reference/platforms/index.mdx +18 -0
  28. package/docs/content/docs/reference/platforms/macos/accessibility.mdx +61 -0
  29. package/docs/content/docs/reference/platforms/macos/actions.mdx +63 -0
  30. package/docs/content/docs/reference/platforms/macos/capture.mdx +58 -0
  31. package/docs/content/docs/reference/platforms/macos/implementation.mdx +98 -0
  32. package/docs/content/docs/reference/platforms/macos/index.mdx +16 -0
  33. package/docs/content/docs/reference/platforms/macos/meta.json +10 -0
  34. package/docs/content/docs/reference/platforms/macos/root-targeting.mdx +47 -0
  35. package/docs/content/docs/reference/platforms/meta.json +8 -0
  36. package/docs/content/docs/reference/platforms/windows.mdx +75 -0
  37. package/docs/content/docs/tutorials/getting-started.mdx +46 -0
  38. package/docs/content/docs/tutorials/index.mdx +10 -0
  39. package/docs/content/docs/tutorials/meta.json +1 -0
  40. package/extensions/computer-use.ts +235 -0
  41. package/native/linux/bridge-rs/Cargo.lock +1204 -0
  42. package/native/linux/bridge-rs/Cargo.toml +21 -0
  43. package/native/linux/bridge-rs/src/atspi.rs +670 -0
  44. package/native/linux/bridge-rs/src/error.rs +65 -0
  45. package/native/linux/bridge-rs/src/lib.rs +11 -0
  46. package/native/linux/bridge-rs/src/main.rs +1110 -0
  47. package/native/linux/bridge-rs/src/protocol.rs +49 -0
  48. package/native/linux/bridge-rs/src/state.rs +293 -0
  49. package/native/linux/bridge-rs/src/wayland.rs +89 -0
  50. package/native/linux/bridge-rs/src/x11.rs +909 -0
  51. package/native/linux/bridge-rs/tests/protocol_tests.rs +73 -0
  52. package/native/macos/activity_glow.swift +169 -0
  53. package/native/macos/bridge.swift +3968 -0
  54. package/native/windows/bridge-rs/Cargo.lock +396 -0
  55. package/native/windows/bridge-rs/Cargo.toml +30 -0
  56. package/native/windows/bridge-rs/src/capture.rs +561 -0
  57. package/native/windows/bridge-rs/src/error.rs +81 -0
  58. package/native/windows/bridge-rs/src/input.rs +501 -0
  59. package/native/windows/bridge-rs/src/lib.rs +85 -0
  60. package/native/windows/bridge-rs/src/main.rs +1683 -0
  61. package/native/windows/bridge-rs/src/protocol.rs +53 -0
  62. package/native/windows/bridge-rs/src/refs.rs +237 -0
  63. package/native/windows/bridge-rs/src/state.rs +55 -0
  64. package/native/windows/bridge-rs/src/uia.rs +1252 -0
  65. package/native/windows/bridge-rs/src/window.rs +718 -0
  66. package/native/windows/bridge-rs/tests/protocol_tests.rs +252 -0
  67. package/native/windows/bridge-rs/tests/refs_tests.rs +86 -0
  68. package/native/windows/bridge-rs/tests/state_tests.rs +33 -0
  69. package/package.json +105 -5
  70. package/prebuilt/macos/arm64/bridge +0 -0
  71. package/prebuilt/macos/x64/bridge +0 -0
  72. package/roam/README.md +52 -0
  73. package/roam/bridge/macos/arm64/bridge +0 -0
  74. package/roam/bridge/macos/x64/bridge +0 -0
  75. package/roam/bridge/manifest.json +20 -0
  76. package/roam/helper-path.d.mts +9 -0
  77. package/roam/helper-path.mjs +36 -0
  78. package/roam/identity.d.mts +16 -0
  79. package/roam/identity.mjs +64 -0
  80. package/roam/index.ts +51 -0
  81. package/roam/redirect.ts +51 -0
  82. package/roam/repair.ts +120 -0
  83. package/scripts/build-native.mjs +286 -0
  84. package/scripts/lib/roam-bridge.mjs +98 -0
  85. package/scripts/setup-helper.mjs +635 -0
  86. package/src/actions.ts +130 -0
  87. package/src/bridge.ts +2594 -0
  88. package/src/cdp.ts +658 -0
  89. package/src/config.ts +226 -0
  90. package/src/contract.ts +107 -0
  91. package/src/extension/index.ts +9 -0
  92. package/src/find-params.ts +21 -0
  93. package/src/note.ts +195 -0
  94. package/src/outline.ts +702 -0
  95. package/src/output.ts +134 -0
  96. package/src/permissions.ts +111 -0
  97. package/src/platform/architecture.ts +23 -0
  98. package/src/platform/coerce.ts +16 -0
  99. package/src/platform/index.ts +61 -0
  100. package/src/platform/linux/backend.ts +186 -0
  101. package/src/platform/linux/helper.ts +238 -0
  102. package/src/platform/macos/backend.ts +147 -0
  103. package/src/platform/macos/browser.ts +110 -0
  104. package/src/platform/macos/helper.ts +369 -0
  105. package/src/platform/macos/permissions.ts +146 -0
  106. package/src/platform/types.ts +229 -0
  107. package/src/platform/windows/backend.ts +142 -0
  108. package/src/platform/windows/helper.ts +140 -0
  109. package/src/root-selection.ts +25 -0
  110. package/src/runtime.ts +129 -0
  111. package/src/state.ts +146 -0
  112. package/src/view.ts +147 -0
@@ -0,0 +1,501 @@
1
+ use serde_json::{json, Value};
2
+
3
+ use crate::error::{ErrorCode, ProtocolError};
4
+
5
+ #[derive(Debug, Clone, PartialEq)]
6
+ pub enum ActTarget {
7
+ Ref(String),
8
+ Point { x: f64, y: f64 },
9
+ }
10
+
11
+ #[derive(Debug, Clone, PartialEq)]
12
+ pub struct ParsedActRequest {
13
+ pub look_id: String,
14
+ pub pid: Option<u64>,
15
+ pub action: String,
16
+ pub policy: String,
17
+ pub target: ActTarget,
18
+ pub params: Value,
19
+ }
20
+
21
+ pub fn parse_act_request(args: &Value) -> Result<ParsedActRequest, ProtocolError> {
22
+ let look_id = args
23
+ .get("lookId")
24
+ .and_then(Value::as_str)
25
+ .filter(|value| !value.is_empty())
26
+ .ok_or_else(|| invalid("act requires lookId"))?
27
+ .to_owned();
28
+ let action = args
29
+ .get("action")
30
+ .and_then(Value::as_str)
31
+ .filter(|value| !value.is_empty())
32
+ .ok_or_else(|| invalid("act requires action"))?
33
+ .to_owned();
34
+ let policy = args
35
+ .get("policy")
36
+ .and_then(Value::as_str)
37
+ .unwrap_or("default")
38
+ .to_owned();
39
+ let target_value = args
40
+ .get("target")
41
+ .ok_or_else(|| invalid("act requires target"))?;
42
+ let target = if let Some(reference) = target_value.get("ref").and_then(Value::as_str) {
43
+ ActTarget::Ref(reference.to_owned())
44
+ } else if let (Some(x), Some(y)) = (
45
+ target_value.get("x").and_then(Value::as_f64),
46
+ target_value.get("y").and_then(Value::as_f64),
47
+ ) {
48
+ ActTarget::Point { x, y }
49
+ } else {
50
+ return Err(invalid("act target must include ref or x/y"));
51
+ };
52
+ Ok(ParsedActRequest {
53
+ look_id,
54
+ pid: args.get("pid").and_then(Value::as_u64),
55
+ action,
56
+ policy,
57
+ target,
58
+ params: args.get("params").cloned().unwrap_or_else(|| json!({})),
59
+ })
60
+ }
61
+
62
+ pub fn policy_allows_raw_input(request: &ParsedActRequest) -> Result<(), ProtocolError> {
63
+ if request.policy != "foreground" {
64
+ Err(ProtocolError::new(
65
+ "Background policy blocks global Windows coordinate/raw input grounding",
66
+ if request.policy == "ax_only" {
67
+ ErrorCode::CoordinateBlocked
68
+ } else {
69
+ ErrorCode::ForegroundRequired
70
+ },
71
+ ))
72
+ } else {
73
+ Ok(())
74
+ }
75
+ }
76
+
77
+ pub fn response_with_delta(mut response: Value, source: &str, delta: Vec<Value>) -> Value {
78
+ if !response
79
+ .get("performed")
80
+ .map(Value::is_object)
81
+ .unwrap_or(false)
82
+ {
83
+ response["performed"] = json!({});
84
+ }
85
+ response["performed"]["deltaSource"] = json!(source);
86
+ if !delta.is_empty() {
87
+ response["rootDelta"] = Value::Array(delta);
88
+ }
89
+ response
90
+ }
91
+
92
+ pub fn open_browser_location(url: &str) -> Result<(), ProtocolError> {
93
+ #[cfg(not(windows))]
94
+ {
95
+ let _ = url;
96
+ Ok(())
97
+ }
98
+ #[cfg(windows)]
99
+ {
100
+ native::open_browser_location(url)
101
+ }
102
+ }
103
+
104
+ pub fn act(args: &Value) -> Result<Value, ProtocolError> {
105
+ let request = parse_act_request(args)?;
106
+ let grounding = args
107
+ .get("resolvedPoint")
108
+ .map(|_| "coordinates")
109
+ .unwrap_or("description");
110
+
111
+ if matches!(
112
+ request.action.as_str(),
113
+ "typeText" | "keypress" | "drag" | "moveMouse"
114
+ ) {
115
+ policy_allows_raw_input(&request)?;
116
+ }
117
+ if grounding == "coordinates" {
118
+ policy_allows_raw_input(&request)?;
119
+ }
120
+
121
+ #[cfg(not(windows))]
122
+ {
123
+ Ok(json!({
124
+ "outcome": "unknown",
125
+ "performed": { "grounding": grounding, "delivery": if grounding == "description" { "ax" } else { "hid" } },
126
+ "evidence": { "platform": "non-windows-test-stub" }
127
+ }))
128
+ }
129
+
130
+ #[cfg(windows)]
131
+ {
132
+ native::act(&request, args, grounding)
133
+ }
134
+ }
135
+
136
+ fn invalid(message: impl Into<String>) -> ProtocolError {
137
+ ProtocolError::new(message.into(), ErrorCode::InvalidRequest)
138
+ }
139
+
140
+ #[cfg(windows)]
141
+ mod native {
142
+ use super::*;
143
+ use windows::Win32::UI::Input::KeyboardAndMouse::*;
144
+ use windows::Win32::UI::WindowsAndMessaging::SetCursorPos;
145
+
146
+ pub fn open_browser_location(url: &str) -> Result<(), ProtocolError> {
147
+ hotkey(&[VK_CONTROL], VK_L)?;
148
+ send_text(url)?;
149
+ send(&[key(VK_RETURN, false), key(VK_RETURN, true)])
150
+ }
151
+
152
+ pub fn act(
153
+ request: &ParsedActRequest,
154
+ args: &Value,
155
+ grounding: &str,
156
+ ) -> Result<Value, ProtocolError> {
157
+ let point = args.get("resolvedPoint").and_then(point_value);
158
+ match request.action.as_str() {
159
+ "press" | "click" => {
160
+ let (x, y) = point.ok_or_else(|| invalid("click requires resolvedPoint"))?;
161
+ click(
162
+ x,
163
+ y,
164
+ request
165
+ .params
166
+ .get("button")
167
+ .and_then(Value::as_str)
168
+ .unwrap_or("left"),
169
+ )?;
170
+ ok("unknown", grounding, "hid")
171
+ }
172
+ "moveMouse" => {
173
+ let (x, y) = point.ok_or_else(|| invalid("moveMouse requires resolvedPoint"))?;
174
+ unsafe { SetCursorPos(x, y) }.map_err(input_failed)?;
175
+ ok("unknown", "coordinates", "hid")
176
+ }
177
+ "drag" => {
178
+ let path = args
179
+ .get("resolvedPath")
180
+ .and_then(Value::as_array)
181
+ .ok_or_else(|| invalid("drag requires resolvedPath"))?;
182
+ let points = path
183
+ .iter()
184
+ .map(point_value_required)
185
+ .collect::<Result<Vec<_>, _>>()?;
186
+ drag(&points)?;
187
+ ok("unknown", "coordinates", "hid")
188
+ }
189
+ "scroll" => {
190
+ let (x, y) = point.ok_or_else(|| invalid("scroll requires resolvedPoint"))?;
191
+ let dy = request
192
+ .params
193
+ .get("scrollY")
194
+ .and_then(Value::as_f64)
195
+ .unwrap_or(0.0);
196
+ let dx = request
197
+ .params
198
+ .get("scrollX")
199
+ .and_then(Value::as_f64)
200
+ .unwrap_or(0.0);
201
+ scroll(x, y, dx, dy)?;
202
+ ok("unknown", grounding, "hid")
203
+ }
204
+ "typeText" => {
205
+ send_text(
206
+ request
207
+ .params
208
+ .get("text")
209
+ .and_then(Value::as_str)
210
+ .unwrap_or(""),
211
+ )?;
212
+ ok("unknown", "coordinates", "hid")
213
+ }
214
+ "setText" => {
215
+ if let Some((x, y)) = point {
216
+ click(x, y, "left")?;
217
+ }
218
+ hotkey(&[VK_CONTROL], VK_A)?;
219
+ let text = request
220
+ .params
221
+ .get("text")
222
+ .and_then(Value::as_str)
223
+ .unwrap_or("");
224
+ send_text(text)?;
225
+ Ok(
226
+ json!({ "outcome": "unknown", "performed": { "grounding": grounding, "delivery": "hid" }, "evidence": { "value": text } }),
227
+ )
228
+ }
229
+ "keypress" => {
230
+ let keys = request
231
+ .params
232
+ .get("keys")
233
+ .and_then(Value::as_array)
234
+ .ok_or_else(|| invalid("keypress requires keys"))?;
235
+ send_keys(keys)?;
236
+ ok("unknown", "coordinates", "hid")
237
+ }
238
+ other => Err(ProtocolError::new(
239
+ format!("Unsupported Windows action '{other}'"),
240
+ ErrorCode::UnsupportedCommand,
241
+ )),
242
+ }
243
+ }
244
+
245
+ fn ok(outcome: &str, grounding: &str, delivery: &str) -> Result<Value, ProtocolError> {
246
+ Ok(
247
+ json!({ "outcome": outcome, "performed": { "grounding": grounding, "delivery": delivery } }),
248
+ )
249
+ }
250
+
251
+ fn input_failed(error: impl std::fmt::Display) -> ProtocolError {
252
+ ProtocolError::new(format!("Input failed: {error}"), ErrorCode::CaptureFailed)
253
+ }
254
+
255
+ fn point_value(value: &Value) -> Option<(i32, i32)> {
256
+ Some((
257
+ value.get("x")?.as_f64()?.round() as i32,
258
+ value.get("y")?.as_f64()?.round() as i32,
259
+ ))
260
+ }
261
+
262
+ fn point_value_required(value: &Value) -> Result<(i32, i32), ProtocolError> {
263
+ point_value(value).ok_or_else(|| invalid("point requires x and y"))
264
+ }
265
+
266
+ fn send(inputs: &[INPUT]) -> Result<(), ProtocolError> {
267
+ let sent = unsafe { SendInput(inputs, std::mem::size_of::<INPUT>() as i32) };
268
+ if sent == inputs.len() as u32 {
269
+ Ok(())
270
+ } else {
271
+ Err(input_failed(format!(
272
+ "SendInput inserted {sent}/{} events",
273
+ inputs.len()
274
+ )))
275
+ }
276
+ }
277
+
278
+ fn mouse(flags: MOUSE_EVENT_FLAGS, data: u32) -> INPUT {
279
+ INPUT {
280
+ r#type: INPUT_MOUSE,
281
+ Anonymous: INPUT_0 {
282
+ mi: MOUSEINPUT {
283
+ dwFlags: flags,
284
+ mouseData: data,
285
+ ..Default::default()
286
+ },
287
+ },
288
+ }
289
+ }
290
+
291
+ fn key(vk: VIRTUAL_KEY, up: bool) -> INPUT {
292
+ INPUT {
293
+ r#type: INPUT_KEYBOARD,
294
+ Anonymous: INPUT_0 {
295
+ ki: KEYBDINPUT {
296
+ wVk: vk,
297
+ dwFlags: if up {
298
+ KEYEVENTF_KEYUP
299
+ } else {
300
+ KEYBD_EVENT_FLAGS(0)
301
+ },
302
+ ..Default::default()
303
+ },
304
+ },
305
+ }
306
+ }
307
+
308
+ fn unicode(ch: u16, up: bool) -> INPUT {
309
+ INPUT {
310
+ r#type: INPUT_KEYBOARD,
311
+ Anonymous: INPUT_0 {
312
+ ki: KEYBDINPUT {
313
+ wScan: ch,
314
+ dwFlags: KEYEVENTF_UNICODE
315
+ | if up {
316
+ KEYEVENTF_KEYUP
317
+ } else {
318
+ KEYBD_EVENT_FLAGS(0)
319
+ },
320
+ ..Default::default()
321
+ },
322
+ },
323
+ }
324
+ }
325
+
326
+ fn click(x: i32, y: i32, button: &str) -> Result<(), ProtocolError> {
327
+ unsafe { SetCursorPos(x, y) }.map_err(input_failed)?;
328
+ let (down, up) = match button {
329
+ "right" => (MOUSEEVENTF_RIGHTDOWN, MOUSEEVENTF_RIGHTUP),
330
+ "middle" => (MOUSEEVENTF_MIDDLEDOWN, MOUSEEVENTF_MIDDLEUP),
331
+ _ => (MOUSEEVENTF_LEFTDOWN, MOUSEEVENTF_LEFTUP),
332
+ };
333
+ send(&[mouse(down, 0), mouse(up, 0)])
334
+ }
335
+
336
+ fn drag(points: &[(i32, i32)]) -> Result<(), ProtocolError> {
337
+ if points.len() < 2 {
338
+ return Err(invalid("drag requires at least two points"));
339
+ }
340
+ unsafe { SetCursorPos(points[0].0, points[0].1) }.map_err(input_failed)?;
341
+ send(&[mouse(MOUSEEVENTF_LEFTDOWN, 0)])?;
342
+ for &(x, y) in &points[1..] {
343
+ unsafe { SetCursorPos(x, y) }.map_err(input_failed)?;
344
+ }
345
+ send(&[mouse(MOUSEEVENTF_LEFTUP, 0)])
346
+ }
347
+
348
+ fn scroll(x: i32, y: i32, dx: f64, dy: f64) -> Result<(), ProtocolError> {
349
+ unsafe { SetCursorPos(x, y) }.map_err(input_failed)?;
350
+ let mut inputs = Vec::new();
351
+ if dy != 0.0 {
352
+ inputs.push(mouse(
353
+ MOUSEEVENTF_WHEEL,
354
+ (-dy * 120.0).round() as i32 as u32,
355
+ ));
356
+ }
357
+ if dx != 0.0 {
358
+ inputs.push(mouse(
359
+ MOUSEEVENTF_HWHEEL,
360
+ (dx * 120.0).round() as i32 as u32,
361
+ ));
362
+ }
363
+ if inputs.is_empty() {
364
+ return Ok(());
365
+ }
366
+ send(&inputs)
367
+ }
368
+
369
+ fn send_text(text: &str) -> Result<(), ProtocolError> {
370
+ let mut inputs = Vec::new();
371
+ for unit in text.encode_utf16() {
372
+ inputs.push(unicode(unit, false));
373
+ inputs.push(unicode(unit, true));
374
+ }
375
+ send(&inputs)
376
+ }
377
+
378
+ fn send_keys(keys: &[Value]) -> Result<(), ProtocolError> {
379
+ let names = keys.iter().filter_map(Value::as_str).collect::<Vec<_>>();
380
+ if names.is_empty() {
381
+ return Err(invalid("keypress requires at least one key"));
382
+ }
383
+ let vks = names
384
+ .iter()
385
+ .map(|name| vk_for(name).ok_or_else(|| invalid(format!("Unsupported key '{name}'"))))
386
+ .collect::<Result<Vec<_>, _>>()?;
387
+ for vk in &vks[..vks.len().saturating_sub(1)] {
388
+ send(&[key(*vk, false)])?;
389
+ }
390
+ if let Some(last) = vks.last() {
391
+ send(&[key(*last, false), key(*last, true)])?;
392
+ }
393
+ for vk in vks[..vks.len().saturating_sub(1)].iter().rev() {
394
+ send(&[key(*vk, true)])?;
395
+ }
396
+ Ok(())
397
+ }
398
+
399
+ fn hotkey(mods: &[VIRTUAL_KEY], key_vk: VIRTUAL_KEY) -> Result<(), ProtocolError> {
400
+ for vk in mods {
401
+ send(&[key(*vk, false)])?;
402
+ }
403
+ send(&[key(key_vk, false), key(key_vk, true)])?;
404
+ for vk in mods.iter().rev() {
405
+ send(&[key(*vk, true)])?;
406
+ }
407
+ Ok(())
408
+ }
409
+
410
+ fn vk_for(name: &str) -> Option<VIRTUAL_KEY> {
411
+ match name.to_ascii_lowercase().as_str() {
412
+ "enter" | "return" => Some(VK_RETURN),
413
+ "escape" | "esc" => Some(VK_ESCAPE),
414
+ "tab" => Some(VK_TAB),
415
+ "backspace" => Some(VK_BACK),
416
+ "delete" => Some(VK_DELETE),
417
+ "space" => Some(VK_SPACE),
418
+ "left" | "arrowleft" => Some(VK_LEFT),
419
+ "right" | "arrowright" => Some(VK_RIGHT),
420
+ "up" | "arrowup" => Some(VK_UP),
421
+ "down" | "arrowdown" => Some(VK_DOWN),
422
+ "home" => Some(VK_HOME),
423
+ "end" => Some(VK_END),
424
+ "pageup" => Some(VK_PRIOR),
425
+ "pagedown" => Some(VK_NEXT),
426
+ "ctrl" | "control" => Some(VK_CONTROL),
427
+ "shift" => Some(VK_SHIFT),
428
+ "alt" | "option" => Some(VK_MENU),
429
+ "cmd" | "win" | "meta" => Some(VK_LWIN),
430
+ key if key.len() == 1 => {
431
+ Some(VIRTUAL_KEY(key.as_bytes()[0].to_ascii_uppercase() as u16))
432
+ }
433
+ key if key.starts_with('f') => key[1..]
434
+ .parse::<u16>()
435
+ .ok()
436
+ .filter(|n| (1..=24).contains(n))
437
+ .map(|n| VIRTUAL_KEY(VK_F1.0 + n - 1)),
438
+ _ => None,
439
+ }
440
+ }
441
+ }
442
+
443
+ #[cfg(test)]
444
+ mod tests {
445
+ use super::*;
446
+
447
+ #[test]
448
+ fn parses_discriminated_click_request() {
449
+ let parsed = parse_act_request(&json!({"lookId":"look_1","action":"click","policy":"default","target":{"ref":"@e1"},"params":{"button":"left"}})).unwrap();
450
+ assert_eq!(parsed.action, "click");
451
+ assert_eq!(parsed.target, ActTarget::Ref("@e1".to_owned()));
452
+ }
453
+
454
+ #[test]
455
+ fn non_foreground_policies_block_raw_input() {
456
+ let parsed = parse_act_request(&json!({"lookId":"look_1","action":"typeText","policy":"ax_only","target":{"ref":"@e1"},"params":{"text":"x"}})).unwrap();
457
+ let err = policy_allows_raw_input(&parsed).unwrap_err();
458
+ assert_eq!(err.code, ErrorCode::CoordinateBlocked);
459
+ let background = parse_act_request(&json!({"lookId":"look_1","action":"click","policy":"background","target":{"x":1,"y":1},"params":{}})).unwrap();
460
+ assert_eq!(
461
+ policy_allows_raw_input(&background).unwrap_err().code,
462
+ ErrorCode::ForegroundRequired
463
+ );
464
+ let default_policy = parse_act_request(&json!({"lookId":"look_1","action":"click","policy":"default","target":{"x":1,"y":1},"params":{}})).unwrap();
465
+ assert_eq!(
466
+ policy_allows_raw_input(&default_policy).unwrap_err().code,
467
+ ErrorCode::ForegroundRequired
468
+ );
469
+ let foreground = parse_act_request(&json!({"lookId":"look_1","action":"click","policy":"foreground","target":{"x":1,"y":1},"params":{}})).unwrap();
470
+ assert!(policy_allows_raw_input(&foreground).is_ok());
471
+ }
472
+
473
+ #[test]
474
+ fn attaches_delta_source_and_delta() {
475
+ let out = response_with_delta(
476
+ json!({"outcome":"worked","performed":{}}),
477
+ "snapshot",
478
+ vec![json!({"change":"focused","kind":"window","pid":1})],
479
+ );
480
+ assert_eq!(out["performed"]["deltaSource"], "snapshot");
481
+ assert_eq!(out["rootDelta"].as_array().unwrap().len(), 1);
482
+ }
483
+
484
+ #[test]
485
+ fn raw_coordinate_actions_do_not_claim_worked() {
486
+ let actions = ["click", "moveMouse", "scroll", "typeText", "keypress"];
487
+ for action in actions {
488
+ let params = match action {
489
+ "typeText" => json!({"text":"abc"}),
490
+ "keypress" => json!({"keys":["enter"]}),
491
+ "scroll" => json!({"scrollY":1,"scrollX":0}),
492
+ _ => json!({}),
493
+ };
494
+ let result = act(&json!({"lookId":"look_1","action":action,"policy":"foreground","target":{"x":1,"y":1},"params":params,"resolvedPoint":{"x":1,"y":1}})).unwrap();
495
+ assert_ne!(
496
+ result["outcome"], "worked",
497
+ "{action} must not report worked without verification"
498
+ );
499
+ }
500
+ }
501
+ }
@@ -0,0 +1,85 @@
1
+ //! Protocol types for the pi-computer-use Windows helper.
2
+ //!
3
+ //! This crate provides the JSON-lines protocol types used to communicate
4
+ //! between the TypeScript host and the Rust helper binary.
5
+
6
+ pub mod capture;
7
+ pub mod error;
8
+ pub mod input;
9
+ pub mod protocol;
10
+ pub mod refs;
11
+ pub mod state;
12
+ pub mod uia;
13
+ pub mod window;
14
+
15
+ pub use error::{ErrorCode, ProtocolError};
16
+ pub use protocol::{Request, Response};
17
+
18
+ /*
19
+ PI_DOCS_REFERENCE_BEGIN reference/platforms/windows
20
+
21
+ `windows-bridge.exe` is the native helper for the root-forest Windows backend. It is spawned by the TypeScript helper client and speaks stdin/stdout JSON-lines protocol version **4**.
22
+
23
+ ## Backend contract
24
+
25
+ The TypeScript Windows backend is intentionally stateless. It forwards root-forest seam calls to the Rust helper:
26
+
27
+ - `listRoots({ pid? })`: cheap HWND metadata only. Roots do not fabricate pairing or sheet counts; HWND/class/style diagnostics live under `metadata` when useful.
28
+ - `look`: atomic observe result with `lookId`, outline, window payload (`rootRef`, `kind`, frame, scale), and optional image. Menu/outline-only roots omit `image`; coordinate acts against those roots fail with `coordinate_unavailable_for_root`.
29
+ - `act`: forwards the typed `PlatformActRequest` whole. The helper validates `lookId`, resolves refs from helper-owned look state, applies policy, executes, and returns honest outcome/evidence plus `performed.deltaSource` and shallow `rootDelta`.
30
+ - `uiaReadText` / `uiaWaitFor`: helper-side live UIA reads/polls. No TypeScript screenshot polling or module-level element state is used.
31
+
32
+ ## Root metadata
33
+
34
+ Windows roots are top-level HWNDs:
35
+
36
+ - `#32768` class => `kind: "menu"`
37
+ - `#32770` class => `kind: "dialog"`
38
+ - owned popups => `kind: "popover"`
39
+ - other HWNDs => `kind: "window"`
40
+
41
+ The helper declares per-monitor-v2 DPI awareness at startup and reports `scaleFactor = GetDpiForWindow(hwnd) / 96`.
42
+
43
+ ## Refs, actions, and deltas
44
+
45
+ Observed `@e` refs store UIA RuntimeId and AutomationId metadata. Ref-targeted actions resolve a live `IUIAutomationElement` first:
46
+
47
+ - `press`/`click`: `InvokePattern`, then `TogglePattern`, then `SelectionItemPattern`, then `ExpandCollapsePattern`, then LegacyIAccessible default action; successful pattern grounding reports `grounding: "description"`, `delivery: "ax"`. Coordinate fallback is blocked by `ax_only` and preflights occlusion with `ElementFromPoint`.
48
+ - `setText`: `ValuePattern.SetValue`, then `CurrentValue` read-back. `evidence.value` is the value actually read.
49
+ - `scroll`: `ScrollPattern` where exposed; wheel fallback is raw input and policy-gated.
50
+ - `typeText`, `keypress`, `drag`, `moveMouse`, and coordinate targets remain raw input and report `unknown` unless verified.
51
+
52
+ `readText` resolves the live element and reads TextPattern → ValuePattern → CurrentName. `waitFor` polls the live UIA subtree at about 150ms intervals.
53
+
54
+ Root deltas are baselined at act time. The helper polls a cheap top-level HWND signature for early settle (`deltaSource: "win-poll"`), then takes short catch-up snapshots before returning the final full before/after diff. If no change is seen before the cap, it returns the full snapshot timeout path (`"snapshot"`).
55
+
56
+ ## Protocol
57
+
58
+ Request envelope:
59
+
60
+ ```json
61
+ { "protocolVersion": 4, "id": "req_1", "cmd": "listRoots", "args": {} }
62
+ ```
63
+
64
+ Response envelope:
65
+
66
+ ```json
67
+ { "protocolVersion": 4, "id": "req_1", "ok": true, "result": { } }
68
+ ```
69
+
70
+ Diagnostics (`cmd: "diagnostics"`) returns the protocol version and helper process metadata. The TypeScript backend rejects a mismatched version with a “Restart Pi …” error.
71
+
72
+ ## Remote acceptance notes
73
+
74
+ - **Item 5 / menus:** click Notepad's File menu, read `rootDelta`, then observe the `appeared` root's `ref`. Menus are separate roots; observing the original Notepad window will not show the menu contents.
75
+ - **Item 7 / occlusion:** record element coordinates from a fresh look, cover the window, then issue a coordinate act (`target: { "x": ..., "y": ... }`, `policy: "default"`). Expect `occluded_target`. Ref-targeted acts may re-resolve and do not exercise this coordinate preflight path.
76
+ - **Item 8 / ref re-resolve:** fresh look, capture any element ref, move the window, then act on the same ref with the same `lookId`. RuntimeId/AutomationId re-resolution should succeed with refound semantics, not `stale_ref`.
77
+ - **Pressability smoke:** a fresh look at Win11 Notepad must show `canPress: true` on menu bar items and buttons. If not, attach the raw outline dump so the capability matrix can be inspected.
78
+
79
+ ## Local constraints
80
+
81
+ - Local child process only; no service, socket, or network listener.
82
+ - Helper path: `%USERPROFILE%\.pi\agent\helpers\pi-computer-use\windows-bridge.exe`.
83
+ - UIAccess/elevated-window limitations are reported as errors; there is no interactive permission grant loop.
84
+ PI_DOCS_REFERENCE_END
85
+ */