Skip to content

Commit 1aeabe5

Browse files
committed
fix(cua): handle lowercase modifier keys and unify right-click in Tzafon templates
1 parent 00b5338 commit 1aeabe5

File tree

2 files changed

+53
-25
lines changed

2 files changed

+53
-25
lines changed

pkg/templates/python/tzafon-computer-use/tools/computer.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,19 @@
1515

1616
from .base import ToolError
1717

18-
MODIFIER_MAP = {"Control": "Ctrl", "Enter": "Return"}
18+
MODIFIER_MAP = {
19+
"Control": "Ctrl",
20+
"control": "Ctrl",
21+
"ctrl": "Ctrl",
22+
"Enter": "Return",
23+
"enter": "Return",
24+
"Escape": "Escape",
25+
"esc": "Escape",
26+
"Shift": "Shift",
27+
"shift": "Shift",
28+
"Alt": "Alt",
29+
"alt": "Alt",
30+
}
1931
MODIFIER_NAMES = {"Ctrl", "Shift", "Alt", "Meta", "Super"}
2032

2133

@@ -65,7 +77,11 @@ async def execute(self, action: Any) -> None:
6577
"""Map a Tzafon model action to Kernel Computer Controls."""
6678
t = action.type
6779

68-
if t == "click":
80+
if t == "click" and getattr(action, "button", "left") == "right":
81+
self.kernel.browsers.computer.click_mouse(
82+
self.session_id, x=self._x(action), y=self._y(action), button="right",
83+
)
84+
elif t == "click":
6985
self.kernel.browsers.computer.click_mouse(
7086
self.session_id, x=self._x(action), y=self._y(action),
7187
)
@@ -77,10 +93,6 @@ async def execute(self, action: Any) -> None:
7793
self.kernel.browsers.computer.click_mouse(
7894
self.session_id, x=self._x(action), y=self._y(action), num_clicks=3,
7995
)
80-
elif t == "right_click":
81-
self.kernel.browsers.computer.click_mouse(
82-
self.session_id, x=self._x(action), y=self._y(action), button="right",
83-
)
8496
elif t == "type":
8597
self.kernel.browsers.computer.type_text(self.session_id, text=action.text)
8698
elif t in ("key", "keypress"):
@@ -115,16 +127,19 @@ async def execute(self, action: Any) -> None:
115127
elif t == "drag":
116128
x1 = getattr(action, "x", None)
117129
if x1 is None:
118-
x1 = getattr(action, "x1", 0)
130+
x1 = getattr(action, "x1", None)
119131
y1 = getattr(action, "y", None)
120132
if y1 is None:
121-
y1 = getattr(action, "y1", 0)
133+
y1 = getattr(action, "y1", None)
122134
x2 = getattr(action, "end_x", None)
123135
if x2 is None:
124-
x2 = getattr(action, "x2", 0)
136+
x2 = getattr(action, "x2", None)
125137
y2 = getattr(action, "end_y", None)
126138
if y2 is None:
127-
y2 = getattr(action, "y2", 0)
139+
y2 = getattr(action, "y2", None)
140+
if any(v is None for v in (x1, y1, x2, y2)):
141+
print(f"drag action missing coordinates, skipping: {action}")
142+
return
128143
self.kernel.browsers.computer.drag_mouse(
129144
self.session_id,
130145
path=[

pkg/templates/typescript/tzafon-computer-use/tools/computer.ts

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,16 @@ export class ToolError extends Error {
1717

1818
const MODIFIER_MAP: Record<string, string> = {
1919
Control: 'Ctrl',
20+
control: 'Ctrl',
21+
ctrl: 'Ctrl',
2022
Enter: 'Return',
23+
enter: 'Return',
24+
Escape: 'Escape',
25+
esc: 'Escape',
26+
Shift: 'Shift',
27+
shift: 'Shift',
28+
Alt: 'Alt',
29+
alt: 'Alt',
2130
};
2231

2332
const MODIFIER_NAMES = new Set(['Ctrl', 'Shift', 'Alt', 'Meta', 'Super']);
@@ -75,6 +84,13 @@ export class ComputerTool {
7584
}
7685

7786
async execute(action: any): Promise<void> {
87+
if (action.type === 'click' && action.button === 'right') {
88+
await this.kernel.browsers.computer.clickMouse(this.sessionId, {
89+
x: this.x(action), y: this.y(action), button: 'right',
90+
});
91+
return;
92+
}
93+
7894
switch (action.type) {
7995
case 'click':
8096
await this.kernel.browsers.computer.clickMouse(this.sessionId, {
@@ -94,12 +110,6 @@ export class ComputerTool {
94110
});
95111
break;
96112

97-
case 'right_click':
98-
await this.kernel.browsers.computer.clickMouse(this.sessionId, {
99-
x: this.x(action), y: this.y(action), button: 'right',
100-
});
101-
break;
102-
103113
case 'type':
104114
await this.kernel.browsers.computer.typeText(this.sessionId, {
105115
text: action.text,
@@ -146,20 +156,23 @@ export class ComputerTool {
146156
});
147157
break;
148158

149-
case 'drag':
159+
case 'drag': {
160+
const x1 = action.x ?? action.x1;
161+
const y1 = action.y ?? action.y1;
162+
const x2 = action.end_x ?? action.x2;
163+
const y2 = action.end_y ?? action.y2;
164+
if (x1 == null || y1 == null || x2 == null || y2 == null) {
165+
console.warn('drag action missing coordinates, skipping', action);
166+
break;
167+
}
150168
await this.kernel.browsers.computer.dragMouse(this.sessionId, {
151169
path: [
152-
[
153-
clamp(action.x ?? action.x1, this.width),
154-
clamp(action.y ?? action.y1, this.height),
155-
],
156-
[
157-
clamp(action.end_x ?? action.x2, this.width),
158-
clamp(action.end_y ?? action.y2, this.height),
159-
],
170+
[clamp(x1, this.width), clamp(y1, this.height)],
171+
[clamp(x2, this.width), clamp(y2, this.height)],
160172
],
161173
});
162174
break;
175+
}
163176

164177
case 'wait':
165178
await new Promise((r) => setTimeout(r, 2000));

0 commit comments

Comments
 (0)