computer-protocol 0.3.1

Types shared by the host MCP server and the in-container daemon.
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
//! The `computer_act` batch: what an agent sends, and the typed actions `computerd` runs.

use std::time::Duration;

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::Observation;

/// Most actions one batch may hold, counting each double click as two clicks.
pub const MAX_ACTIONS: usize = 24;

/// Wait length when an action gives none, in milliseconds.
pub const DEFAULT_WAIT_MS: u16 = 350;

/// Longest single wait, and longest settle time, in milliseconds.
pub const MAX_WAIT_MS: u16 = 5000;

/// Settle time before the closing screenshot when the batch gives none, in milliseconds.
pub const DEFAULT_SETTLE_MS: u16 = 300;

/// Wheel steps per scroll action when it gives none.
pub const DEFAULT_SCROLL_AMOUNT: u8 = 3;

/// Most wheel steps one scroll action may take.
pub const MAX_SCROLL_AMOUNT: u8 = 20;

/// Most characters all `type` actions of one batch may hold together.
pub const MAX_TYPE_CHARS: usize = 1000;

/// Time `computerd` may spend on top of the waits and typing of a batch before giving up.
const WORK_ALLOWANCE: Duration = Duration::from_secs(30);

/// Time a `focus` action may take when it has to start an application.
const LAUNCH_ALLOWANCE: Duration = Duration::from_secs(40);

/// Time applications get to reread the keyboard after a temporary key binding changes.
pub const KEYMAP_SETTLE: Duration = Duration::from_millis(20);

/// Allowance for the X round trips behind one typed character.
const ROUND_TRIP_ALLOWANCE: Duration = Duration::from_millis(10);

/// Time one typed character may take: it can need its own binding, which costs two settle pauses.
const TYPE_CHAR_ALLOWANCE: Duration = KEYMAP_SETTLE
    .saturating_mul(2)
    .saturating_add(ROUND_TRIP_ALLOWANCE);

/// Mouse button.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "lowercase")]
pub enum Button {
    Left,
    Right,
    Middle,
}

/// Scroll direction.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "lowercase")]
pub enum Direction {
    Up,
    Down,
    Left,
    Right,
}

/// A position on the screen, in pixels from the top left corner.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct Point {
    pub x: u16,
    pub y: u16,
}

/// One validated action. Double clicks are already split into two clicks.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "lowercase")]
pub enum Action {
    Click {
        at: Point,
        button: Button,
    },
    Move {
        at: Point,
    },
    Down {
        at: Option<Point>,
        button: Button,
    },
    Up {
        at: Option<Point>,
        button: Button,
    },
    Type {
        text: String,
    },
    Key {
        key: String,
        modifiers: Vec<String>,
    },
    Scroll {
        at: Option<Point>,
        direction: Direction,
        amount: u8,
    },
    Wait {
        ms: u16,
    },
    Focus {
        application: String,
        uri: Option<String>,
    },
}

/// Body of `POST /sessions/{id}/act`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ActRequest {
    pub actions: Vec<Action>,
    /// Whether to end with a screenshot.
    pub observe: bool,
    /// Pause before the closing screenshot, in milliseconds.
    pub settle_ms: u16,
}

impl ActRequest {
    /// Longest the batch can take: its waits, the settle time, typing, launches, and a fixed allowance for the rest.
    #[must_use]
    pub fn time_budget(&self) -> Duration {
        let mut budget = WORK_ALLOWANCE + Duration::from_millis(u64::from(self.settle_ms));
        for action in &self.actions {
            match action {
                Action::Wait { ms } => budget += Duration::from_millis(u64::from(*ms)),
                Action::Type { text } => {
                    budget += TYPE_CHAR_ALLOWANCE
                        * u32::try_from(text.chars().count()).unwrap_or(u32::MAX);
                }
                Action::Focus { .. } => budget += LAUNCH_ALLOWANCE,
                _ => {}
            }
        }
        budget
    }
}

/// Reply to `POST /sessions/{id}/act`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ActReply {
    pub actions_run: usize,
    /// The closing screenshot, when the batch asked for one.
    pub observation: Option<Observation>,
    /// Screen number this batch opened for the session, when it was the session's first call.
    #[serde(default)]
    pub opened_screen: Option<u8>,
}

/// Kind of a [`RawAction`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "lowercase")]
pub enum Kind {
    Click,
    Move,
    Down,
    Up,
    Type,
    Key,
    Scroll,
    Wait,
    Focus,
}

impl Kind {
    fn name(self) -> &'static str {
        match self {
            Self::Click => "click",
            Self::Move => "move",
            Self::Down => "down",
            Self::Up => "up",
            Self::Type => "type",
            Self::Key => "key",
            Self::Scroll => "scroll",
            Self::Wait => "wait",
            Self::Focus => "focus",
        }
    }
}

/// An action as the agent writes it. Which fields apply depends on `kind`.
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
pub struct RawAction {
    /// What to do.
    #[serde(default)]
    pub kind: Option<Kind>,
    /// Pixel column on the screen. Needed by click and move, optional for down, up, and scroll.
    pub x: Option<f64>,
    /// Pixel row on the screen. Needed by click and move, optional for down, up, and scroll.
    pub y: Option<f64>,
    /// Mouse button for click, down, and up. Default left.
    pub button: Option<Button>,
    /// For click: click twice.
    pub double: Option<bool>,
    /// For type: the text to type. Any Unicode works. A newline presses Enter.
    pub text: Option<String>,
    /// For key: a key name such as enter, esc, tab, backspace, delete, space, left, right, up, down, home, end, pageup, pagedown, f1 to f12, or a single character.
    pub key: Option<String>,
    /// For key: modifiers held while the key is pressed, from ctrl, alt, shift, and super (also cmd, meta, win, option).
    pub modifiers: Option<Vec<String>>,
    /// For scroll: the direction.
    pub direction: Option<Direction>,
    /// For scroll: wheel steps, 1 to 20. Default 3.
    pub amount: Option<f64>,
    /// For wait: milliseconds, up to 5000. Default 350.
    pub ms: Option<f64>,
    /// For focus: name or title of an open window to raise, matched without regard to case. Also names an application to start when no window matches.
    pub application: Option<String>,
    /// For focus: a page or file to open in the application. The application is started or reused with it.
    pub uri: Option<String>,
}

/// Why a batch was refused. The text tells the agent what to fix.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("{0}")]
pub struct ActionError(String);

impl ActionError {
    /// A refusal with the given message.
    #[must_use]
    pub fn new(message: impl Into<String>) -> Self {
        Self(message.into())
    }
}

fn to_u16(value: f64) -> Option<u16> {
    let rounded = value.round();
    if (0.0..=f64::from(u16::MAX)).contains(&rounded) {
        #[expect(
            clippy::cast_possible_truncation,
            clippy::cast_sign_loss,
            reason = "the range check above keeps the value inside u16"
        )]
        Some(rounded as u16)
    } else {
        None
    }
}

fn coordinate(number: usize, kind: Kind, axis: &str, value: f64) -> Result<u16, ActionError> {
    to_u16(value).ok_or_else(|| {
        ActionError::new(format!(
            "action {number} ({}): {axis} must be a screen pixel from 0 to {}, got {value}",
            kind.name(),
            u16::MAX
        ))
    })
}

fn point(number: usize, kind: Kind, raw: &RawAction) -> Result<Option<Point>, ActionError> {
    match (raw.x, raw.y) {
        (Some(x), Some(y)) => Ok(Some(Point {
            x: coordinate(number, kind, "x", x)?,
            y: coordinate(number, kind, "y", y)?,
        })),
        (None, None) => Ok(None),
        _ => Err(ActionError::new(format!(
            "action {number} ({}): give both x and y, or neither",
            kind.name()
        ))),
    }
}

fn required_point(number: usize, kind: Kind, raw: &RawAction) -> Result<Point, ActionError> {
    point(number, kind, raw)?.ok_or_else(|| {
        ActionError::new(format!(
            "action {number} ({}) needs x and y, the pixel position on the screen",
            kind.name()
        ))
    })
}

/// Rounds `value` into `min..=max`. `None` when it is not a number.
fn clamped(value: f64, min: u16, max: u16) -> Option<u16> {
    if value.is_nan() {
        return None;
    }
    to_u16(value.clamp(f64::from(min), f64::from(max)))
}

fn parse_one(number: usize, raw: &RawAction, out: &mut Vec<Action>) -> Result<(), ActionError> {
    let Some(kind) = raw.kind else {
        return Err(ActionError::new(format!(
            "action {number} has no kind, use one of click, move, down, up, type, key, scroll, wait, focus"
        )));
    };
    let button = raw.button.unwrap_or(Button::Left);
    match kind {
        Kind::Click => {
            let at = required_point(number, kind, raw)?;
            let click = Action::Click { at, button };
            out.push(click.clone());
            if raw.double == Some(true) {
                out.push(click);
            }
        }
        Kind::Move => out.push(Action::Move {
            at: required_point(number, kind, raw)?,
        }),
        Kind::Down => out.push(Action::Down {
            at: point(number, kind, raw)?,
            button,
        }),
        Kind::Up => out.push(Action::Up {
            at: point(number, kind, raw)?,
            button,
        }),
        Kind::Type => {
            let text = raw.text.clone().ok_or_else(|| {
                ActionError::new(format!("action {number} (type) needs text to type"))
            })?;
            out.push(Action::Type { text });
        }
        Kind::Key => {
            let key = raw
                .key
                .as_deref()
                .map(str::trim)
                .filter(|key| !key.is_empty())
                .ok_or_else(|| {
                    ActionError::new(format!(
                        "action {number} (key) needs a key such as enter, esc, tab, or a letter"
                    ))
                })?;
            out.push(Action::Key {
                key: key.to_owned(),
                modifiers: raw.modifiers.clone().unwrap_or_default(),
            });
        }
        Kind::Scroll => {
            let direction = raw.direction.ok_or_else(|| {
                ActionError::new(format!(
                    "action {number} (scroll) needs a direction: up, down, left, or right"
                ))
            })?;
            let amount = match raw.amount {
                None => DEFAULT_SCROLL_AMOUNT,
                Some(value) => clamped(value, 1, u16::from(MAX_SCROLL_AMOUNT))
                    .and_then(|amount| u8::try_from(amount).ok())
                    .ok_or_else(|| {
                        ActionError::new(format!(
                            "action {number} (scroll): amount is not a number"
                        ))
                    })?,
            };
            out.push(Action::Scroll {
                at: point(number, kind, raw)?,
                direction,
                amount,
            });
        }
        Kind::Wait => {
            let ms = match raw.ms {
                None => DEFAULT_WAIT_MS,
                Some(value) => clamped(value, 0, MAX_WAIT_MS).ok_or_else(|| {
                    ActionError::new(format!("action {number} (wait): ms is not a number"))
                })?,
            };
            out.push(Action::Wait { ms });
        }
        Kind::Focus => {
            let application = raw
                .application
                .as_deref()
                .map(str::trim)
                .filter(|name| !name.is_empty())
                .ok_or_else(|| {
                    ActionError::new(format!(
                        "action {number} (focus) needs the application name or window title to raise"
                    ))
                })?;
            out.push(Action::Focus {
                application: application.to_owned(),
                uri: raw.uri.clone(),
            });
        }
    }
    Ok(())
}

impl ActRequest {
    /// Turns the agent's batch into typed actions and applies every limit.
    ///
    /// `observe` defaults to true and `settle_ms` to [`DEFAULT_SETTLE_MS`]. Waits and
    /// the settle time are capped at [`MAX_WAIT_MS`].
    ///
    /// # Errors
    ///
    /// Fails when the batch is empty or too long, an action lacks a field it needs,
    /// or a number is not usable. The message names the action.
    pub fn parse(
        raw: &[RawAction],
        observe: Option<bool>,
        settle_ms: Option<f64>,
    ) -> Result<Self, ActionError> {
        if raw.is_empty() {
            return Err(ActionError::new("actions must hold at least one action"));
        }
        if raw.len() > MAX_ACTIONS {
            return Err(ActionError::new(format!(
                "{} actions given, the limit is {MAX_ACTIONS}. Split the batch",
                raw.len()
            )));
        }
        let mut actions = Vec::with_capacity(raw.len());
        for (index, action) in raw.iter().enumerate() {
            parse_one(index + 1, action, &mut actions)?;
        }
        if actions.len() > MAX_ACTIONS {
            return Err(ActionError::new(format!(
                "the batch runs {} actions because each double click counts as two, the limit is {MAX_ACTIONS}. Split the batch",
                actions.len()
            )));
        }
        let typed: usize = actions
            .iter()
            .map(|action| match action {
                Action::Type { text } => text.chars().count(),
                _ => 0,
            })
            .sum();
        if typed > MAX_TYPE_CHARS {
            return Err(ActionError::new(format!(
                "the batch types {typed} characters, the limit is {MAX_TYPE_CHARS}. Split the text across batches"
            )));
        }
        let settle_ms = match settle_ms {
            None => DEFAULT_SETTLE_MS,
            Some(value) => clamped(value, 0, MAX_WAIT_MS)
                .ok_or_else(|| ActionError::new("settle_ms is not a number"))?,
        };
        Ok(Self {
            actions,
            observe: observe.unwrap_or(true),
            settle_ms,
        })
    }
}

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

    fn raw(json: serde_json::Value) -> Vec<RawAction> {
        serde_json::from_value(json).unwrap()
    }

    fn parse(json: serde_json::Value) -> Result<ActRequest, ActionError> {
        ActRequest::parse(&raw(json), None, None)
    }

    fn repeated(action: serde_json::Value, times: usize) -> serde_json::Value {
        serde_json::Value::Array(vec![action; times])
    }

    #[test]
    fn double_click_becomes_two_clicks_and_counts_toward_the_limit() {
        let request = parse(serde_json::json!([
            { "kind": "click", "x": 10.4, "y": 20.6, "button": "right", "double": true }
        ]))
        .unwrap();
        let click = Action::Click {
            at: Point { x: 10, y: 21 },
            button: Button::Right,
        };
        assert_eq!(request.actions, vec![click.clone(), click]);

        let double = serde_json::json!({ "kind": "click", "x": 1, "y": 1, "double": true });
        assert_eq!(
            parse(repeated(double.clone(), 12)).unwrap().actions.len(),
            MAX_ACTIONS
        );
        let error = parse(repeated(double, 13)).unwrap_err();
        assert!(error.to_string().contains("runs 26 actions"), "{error}");

        let wait = serde_json::json!({ "kind": "wait" });
        assert_eq!(parse(repeated(wait.clone(), 24)).unwrap().actions.len(), 24);
        assert!(parse(repeated(wait, 25)).is_err());
    }

    #[test]
    fn waits_scrolls_and_settle_are_clamped_and_defaulted() {
        let request = ActRequest::parse(
            &raw(serde_json::json!([
                { "kind": "wait" },
                { "kind": "wait", "ms": 99999 },
                { "kind": "wait", "ms": -5 },
                { "kind": "scroll", "direction": "down" },
                { "kind": "scroll", "direction": "left", "amount": 500 },
                { "kind": "scroll", "direction": "up", "amount": 0, "x": 5, "y": 6 },
            ])),
            Some(false),
            Some(60000.0),
        )
        .unwrap();
        let scroll = |direction, amount, at| Action::Scroll {
            at,
            direction,
            amount,
        };
        assert_eq!(
            request.actions,
            vec![
                Action::Wait { ms: 350 },
                Action::Wait { ms: 5000 },
                Action::Wait { ms: 0 },
                scroll(Direction::Down, 3, None),
                scroll(Direction::Left, 20, None),
                scroll(Direction::Up, 1, Some(Point { x: 5, y: 6 })),
            ]
        );
        assert!(!request.observe);
        assert_eq!(request.settle_ms, 5000);

        let defaults =
            ActRequest::parse(&raw(serde_json::json!([{ "kind": "wait" }])), None, None).unwrap();
        assert!(defaults.observe);
        assert_eq!(defaults.settle_ms, DEFAULT_SETTLE_MS);
    }

    #[test]
    fn missing_fields_and_bad_numbers_name_the_action() {
        for (action, expected) in [
            (serde_json::json!({}), "action 2 has no kind"),
            (
                serde_json::json!({ "kind": "click" }),
                "action 2 (click) needs x and y",
            ),
            (
                serde_json::json!({ "kind": "click", "x": 1 }),
                "action 2 (click): give both x and y",
            ),
            (
                serde_json::json!({ "kind": "down", "x": 1 }),
                "action 2 (down): give both x and y",
            ),
            (
                serde_json::json!({ "kind": "move", "x": -1, "y": 2 }),
                "action 2 (move): x must be",
            ),
            (
                serde_json::json!({ "kind": "type" }),
                "action 2 (type) needs text",
            ),
            (
                serde_json::json!({ "kind": "key", "key": " " }),
                "action 2 (key) needs a key",
            ),
            (
                serde_json::json!({ "kind": "scroll" }),
                "action 2 (scroll) needs a direction",
            ),
            (
                serde_json::json!({ "kind": "focus", "application": " " }),
                "action 2 (focus) needs",
            ),
        ] {
            let error = parse(serde_json::json!([{ "kind": "wait" }, action])).unwrap_err();
            assert!(error.to_string().starts_with(expected), "{error}");
        }
        assert!(parse(serde_json::json!([])).is_err());
    }

    #[test]
    fn unknown_kinds_do_not_deserialize() {
        let error = serde_json::from_value::<RawAction>(serde_json::json!({ "kind": "tap" }))
            .unwrap_err()
            .to_string();
        assert!(error.contains("expected one of"), "{error}");
    }

    #[test]
    fn typed_text_is_capped_across_the_batch() {
        let half = "é".repeat(MAX_TYPE_CHARS / 2 + 1);
        let error = parse(serde_json::json!([
            { "kind": "type", "text": half },
            { "kind": "type", "text": half },
        ]))
        .unwrap_err();
        assert!(error.to_string().contains("types"), "{error}");
    }

    #[test]
    fn time_budget_covers_waits_settle_and_typing() {
        let request = ActRequest::parse(
            &raw(serde_json::json!([
                { "kind": "wait", "ms": 1000 },
                { "kind": "type", "text": "abcde" },
            ])),
            None,
            Some(500.0),
        )
        .unwrap();
        assert_eq!(
            request.time_budget(),
            Duration::from_millis(30_000 + 500 + 1000 + 5 * 50)
        );
    }

    #[test]
    fn actions_survive_the_wire_round_trip() {
        let request = parse(serde_json::json!([
            { "kind": "down", "button": "middle" },
            { "kind": "key", "key": "c", "modifiers": ["ctrl"] },
            { "kind": "focus", "application": "xterm" },
        ]))
        .unwrap();
        let json = serde_json::to_string(&request).unwrap();
        assert_eq!(serde_json::from_str::<ActRequest>(&json).unwrap(), request);
    }

    #[test]
    fn typing_budget_covers_a_binding_pause_for_every_character() {
        let chars = MAX_TYPE_CHARS;
        let request = ActRequest {
            actions: vec![Action::Type {
                text: "あ".repeat(chars),
            }],
            observe: false,
            settle_ms: 0,
        };
        let per_char = KEYMAP_SETTLE * 2 + Duration::from_millis(10);
        assert!(request.time_budget() >= WORK_ALLOWANCE + per_char * u32::try_from(chars).unwrap());
    }
}