fix(computer-use): make modifier clicks interruption-safe (#11451)
* fix(computer-use): make modifier clicks interruption-safe * fix(computer-use): pace modified Windows multiclicks * fix(computer-use): address modifier safety review
This commit is contained in:
@@ -729,12 +729,13 @@ final class Provider {
|
||||
let snapshot = try currentSnapshot(params: params)
|
||||
let button = params["mouseButton"]?.string ?? "left"
|
||||
let count = try positiveInteger(params["clickCount"]?.number, defaultValue: 1, name: "clickCount")
|
||||
let modifiers = try KeyMap.parseModifiers(params["modifiers"]?.string)
|
||||
// Why: agents expect a click into a target app to make the next
|
||||
// keyboard action safe, even when the click uses an AX action path.
|
||||
recoverWindow(snapshot.app)
|
||||
if let elementIndex = try optionalInteger(params, "elementIndex") {
|
||||
let record = try element(snapshot, elementIndex)
|
||||
if count <= 1, let actionName = try performClickAction(record: record, mouseButton: button) {
|
||||
if modifiers.isEmpty, count <= 1, let actionName = try performClickAction(record: record, mouseButton: button) {
|
||||
return actionMetadata(path: "accessibility", actionName: actionName)
|
||||
}
|
||||
if let point = center(record.localFrame, in: snapshot.windowBounds) {
|
||||
@@ -742,7 +743,8 @@ final class Provider {
|
||||
pid: snapshot.app.pid,
|
||||
at: point,
|
||||
button: mouseButton(button),
|
||||
count: count
|
||||
count: count,
|
||||
modifiers: modifiers
|
||||
)
|
||||
return actionMetadata(path: "synthetic", fallbackReason: "actionUnsupported")
|
||||
}
|
||||
@@ -753,7 +755,8 @@ final class Provider {
|
||||
pid: snapshot.app.pid,
|
||||
at: point,
|
||||
button: mouseButton(button),
|
||||
count: count
|
||||
count: count,
|
||||
modifiers: modifiers
|
||||
)
|
||||
return actionMetadata(path: "synthetic")
|
||||
}
|
||||
@@ -2282,14 +2285,23 @@ private func resizePng(_ image: CGImage, scale: CGFloat) -> BoundedPNG? {
|
||||
}
|
||||
|
||||
private enum Input {
|
||||
static func click(pid: pid_t, at point: CGPoint, button: MouseButton, count: Int) throws {
|
||||
static func click(
|
||||
pid: pid_t,
|
||||
at point: CGPoint,
|
||||
button: MouseButton,
|
||||
count: Int,
|
||||
modifiers: [KeyModifier]
|
||||
) throws {
|
||||
guard let source = CGEventSource(stateID: .combinedSessionState) else {
|
||||
throw ProviderError.coded("accessibility_error", "failed to create event source")
|
||||
}
|
||||
let flags = modifiers.reduce(into: CGEventFlags()) { result, modifier in
|
||||
result.insert(modifier.flag)
|
||||
}
|
||||
for _ in 0..<max(count, 1) {
|
||||
try mouse(.mouseMoved, source: source, point: point, button: button.cgButton, pid: pid)
|
||||
try mouse(button.downEvent, source: source, point: point, button: button.cgButton, pid: pid)
|
||||
try mouse(button.upEvent, source: source, point: point, button: button.cgButton, pid: pid)
|
||||
try mouse(.mouseMoved, source: source, point: point, button: button.cgButton, flags: flags, pid: pid)
|
||||
try mouse(button.downEvent, source: source, point: point, button: button.cgButton, flags: flags, pid: pid)
|
||||
try mouse(button.upEvent, source: source, point: point, button: button.cgButton, flags: flags, pid: pid)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2343,16 +2355,20 @@ private enum Input {
|
||||
static func pressKey(_ key: String, pid: pid_t) throws {
|
||||
let parsed = try KeyMap.parse(key)
|
||||
var flags = CGEventFlags()
|
||||
var pressedModifiers: [KeyModifier] = []
|
||||
defer {
|
||||
for modifier in pressedModifiers.reversed() {
|
||||
flags.remove(modifier.flag)
|
||||
try? keyEvent(modifier.keyCode, down: false, flags: flags, pid: pid)
|
||||
}
|
||||
}
|
||||
for modifier in parsed.modifiers {
|
||||
flags.insert(modifier.flag)
|
||||
try keyEvent(modifier.keyCode, down: true, flags: flags, pid: pid)
|
||||
pressedModifiers.append(modifier)
|
||||
}
|
||||
try keyEvent(parsed.keyCode, down: true, flags: flags, pid: pid)
|
||||
try keyEvent(parsed.keyCode, down: false, flags: flags, pid: pid)
|
||||
for modifier in parsed.modifiers.reversed() {
|
||||
try keyEvent(modifier.keyCode, down: false, flags: flags, pid: pid)
|
||||
flags.remove(modifier.flag)
|
||||
}
|
||||
}
|
||||
|
||||
static func pasteText(_ text: String, pid: pid_t) throws {
|
||||
@@ -2377,10 +2393,18 @@ private enum Input {
|
||||
try pressKey("cmd+v", pid: pid)
|
||||
}
|
||||
|
||||
private static func mouse(_ type: CGEventType, source: CGEventSource, point: CGPoint, button: CGMouseButton, pid: pid_t) throws {
|
||||
private static func mouse(
|
||||
_ type: CGEventType,
|
||||
source: CGEventSource,
|
||||
point: CGPoint,
|
||||
button: CGMouseButton,
|
||||
flags: CGEventFlags = [],
|
||||
pid: pid_t
|
||||
) throws {
|
||||
guard let event = CGEvent(mouseEventSource: source, mouseType: type, mouseCursorPosition: point, mouseButton: button) else {
|
||||
throw ProviderError.coded("accessibility_error", "failed to create mouse event")
|
||||
}
|
||||
event.flags = flags
|
||||
event.postToPid(pid)
|
||||
}
|
||||
|
||||
@@ -2497,16 +2521,9 @@ private enum KeyMap {
|
||||
var modifiers: [KeyModifier] = []
|
||||
var keyName: String?
|
||||
for part in parts {
|
||||
switch part {
|
||||
case "cmd", "command", "meta", "super", "cmdorctrl", "commandorcontrol":
|
||||
modifiers.append(KeyModifier(keyCode: 55, flag: .maskCommand))
|
||||
case "ctrl", "control":
|
||||
modifiers.append(KeyModifier(keyCode: 59, flag: .maskControl))
|
||||
case "alt", "option":
|
||||
modifiers.append(KeyModifier(keyCode: 58, flag: .maskAlternate))
|
||||
case "shift":
|
||||
modifiers.append(KeyModifier(keyCode: 56, flag: .maskShift))
|
||||
default:
|
||||
if let modifier = modifier(part) {
|
||||
modifiers.append(modifier)
|
||||
} else {
|
||||
keyName = part
|
||||
}
|
||||
}
|
||||
@@ -2516,6 +2533,38 @@ private enum KeyMap {
|
||||
return ParsedKey(keyCode: keyCode, modifiers: modifiers)
|
||||
}
|
||||
|
||||
static func parseModifiers(_ spec: String?) throws -> [KeyModifier] {
|
||||
guard let spec else {
|
||||
return []
|
||||
}
|
||||
let parts = spec.split(separator: "+", omittingEmptySubsequences: false)
|
||||
.map { String($0).trimmingCharacters(in: .whitespacesAndNewlines).lowercased() }
|
||||
guard !parts.isEmpty, !parts.contains(where: \.isEmpty) else {
|
||||
throw ProviderError.coded("invalid_argument", "click modifiers require modifier keys only")
|
||||
}
|
||||
return try parts.map { part in
|
||||
guard let modifier = modifier(part) else {
|
||||
throw ProviderError.coded("invalid_argument", "unsupported click modifier '\(part)'")
|
||||
}
|
||||
return modifier
|
||||
}
|
||||
}
|
||||
|
||||
private static func modifier(_ part: String) -> KeyModifier? {
|
||||
switch part {
|
||||
case "cmd", "command", "meta", "super", "win", "cmdorctrl", "commandorcontrol":
|
||||
return KeyModifier(keyCode: 55, flag: .maskCommand)
|
||||
case "ctrl", "control":
|
||||
return KeyModifier(keyCode: 59, flag: .maskControl)
|
||||
case "alt", "option":
|
||||
return KeyModifier(keyCode: 58, flag: .maskAlternate)
|
||||
case "shift":
|
||||
return KeyModifier(keyCode: 56, flag: .maskShift)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
private static let codes: [String: CGKeyCode] = [
|
||||
"a": 0, "s": 1, "d": 2, "f": 3, "h": 4, "g": 5, "z": 6, "x": 7, "c": 8, "v": 9,
|
||||
"b": 11, "q": 12, "w": 13, "e": 14, "r": 15, "y": 16, "t": 17, "1": 18, "2": 19,
|
||||
|
||||
Reference in New Issue
Block a user