Skip to content

Computer use CLI for AI agents

fix(windows): type CR LF as one line break (#368)

Railly Hugod77f910d61c9
diff --git a/crates/windows/src/input/keyboard/keyboard_text.rs b/crates/windows/src/input/keyboard/keyboard_text.rs
index 15d24ed80..69e24ee4c 100644
--- a/crates/windows/src/input/keyboard/keyboard_text.rs
+++ b/crates/windows/src/input/keyboard/keyboard_text.rs
@@ -6,8 +6,10 @@
//! deadline and gives per-chunk delivery accounting. Each chunk becomes one
//! `SendInput` batch of `KEYEVENTF_UNICODE` down/up pairs, one pair per code
//! unit (a surrogate pair is two pairs) - the A4-1 measured per-event
-//! contract. No arm of this module ever places the typed text itself into an
-//! error: only chunk counts survive into `details`.
+//! contract. A CR LF pair is posted as its CR alone: each Unicode CR or LF
+//! is a line break on its own, so posting both would type two. No arm of
+//! this module ever places the typed text itself into an error: only chunk
+//! counts survive into `details`.
use std::time::Duration;
@@ -125,7 +127,7 @@ fn text_chunks(
};
let mut chunks = Vec::new();
let mut current = Vec::with_capacity(TEXT_CHUNK_UTF16);
- for character in text.chars() {
+ for character in typed_chars(text) {
let mut encoded = [0_u16; 2];
let encoded = character.encode_utf16(&mut encoded);
if !current.is_empty() && current.len() + encoded.len() > TEXT_CHUNK_UTF16 {
@@ -143,7 +145,7 @@ fn planned_chunk_count(text: &str) -> Result<usize, AdapterError> {
let mut units = 0_usize;
let mut chunks = 0_usize;
let mut current = 0_usize;
- for character in text.chars() {
+ for character in typed_chars(text) {
let encoded = character.len_utf16();
units = units.checked_add(encoded).ok_or_else(text_too_large)?;
if units > MAX_TEXT_UTF16 {
@@ -158,6 +160,17 @@ fn planned_chunk_count(text: &str) -> Result<usize, AdapterError> {
Ok(chunks + usize::from(current > 0))
}
+/// The characters posted for `text`: every character except the LF of a
+/// CR LF pair.
+fn typed_chars(text: &str) -> impl Iterator<Item = char> + '_ {
+ let mut previous = None;
+ text.chars().filter(move |&character| {
+ let skip = character == '\n' && previous == Some('\r');
+ previous = Some(character);
+ !skip
+ })
+}
+
fn text_too_large() -> AdapterError {
AdapterError::new(
ErrorCode::InvalidArgs,
diff --git a/crates/windows/src/input/keyboard/keyboard_text_tests.rs b/crates/windows/src/input/keyboard/keyboard_text_tests.rs
index 92fc9aaf4..9f1677b26 100644
--- a/crates/windows/src/input/keyboard/keyboard_text_tests.rs
+++ b/crates/windows/src/input/keyboard/keyboard_text_tests.rs
@@ -111,6 +111,40 @@ fn a_payload_spanning_multiple_chunks_delivers_every_unit_in_order() {
assert_eq!(ups, text.encode_utf16().count());
}
+#[test]
+fn crlf_posts_one_line_break() {
+ sink::reset();
+
+ synthesize_text("a\r\nb", ample_deadline(), |_| Ok(())).expect("ample deadline succeeds");
+
+ let downs: Vec<u16> = sink::recorded()
+ .iter()
+ .filter(|event| event.flags == KEYEVENTF_UNICODE)
+ .map(|event| event.scan)
+ .collect();
+ assert_eq!(downs, "a\rb".encode_utf16().collect::<Vec<_>>());
+}
+
+#[test]
+fn lone_lf_and_lone_cr_are_posted_unchanged() {
+ let chunks = text_chunks("a\nb\rc\n\rd", None).unwrap();
+
+ assert_eq!(
+ String::from_utf16(&chunks.concat()).unwrap(),
+ "a\nb\rc\n\rd"
+ );
+}
+
+#[test]
+fn crlf_across_a_chunk_boundary_still_posts_one_line_break() {
+ let text = format!("{}\r\ntail", "a".repeat(TEXT_CHUNK_UTF16 - 1));
+ let chunks = text_chunks(&text, None).unwrap();
+
+ let expected = format!("{}\rtail", "a".repeat(TEXT_CHUNK_UTF16 - 1));
+ assert_eq!(String::from_utf16(&chunks.concat()).unwrap(), expected);
+ assert_eq!(planned_chunk_count(&text).unwrap(), chunks.len());
+}
+
#[test]
fn verify_failure_after_partial_delivery_enriches_chunk_counts() {
sink::reset();