Skip to content

Commit d19c755

Browse files
authored
feat(computer-use): computer.act v2 action contract with capability-filtered tool (#123544)
* feat(computer-use): computer.act v2 action contract with capability-filtered tool * fix(computer-use): break contract import cycle, bound model-visible elements, regen swift protocol * test(computer-use): satisfy curly rule in schema-cap helpers * fix(computer-use): satisfy type-aware lint on contract and tool * test(gateway-protocol): keep connect-params suite under the line cap
1 parent 3a49aa1 commit d19c755

33 files changed

Lines changed: 1375 additions & 233 deletions

‎apps/shared/OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,7 @@ public struct ConnectParams: Codable, Sendable {
11551155
public let client: [String: AnyCodable]
11561156
public let caps: [String]?
11571157
public let commands: [String]?
1158+
public let computeruse: AnyCodable?
11581159
public let workerruns: WorkerAdmissionHandshake?
11591160
public let permissions: [String: AnyCodable]?
11601161
public let pathenv: String?
@@ -1171,6 +1172,7 @@ public struct ConnectParams: Codable, Sendable {
11711172
client: [String: AnyCodable],
11721173
caps: [String]? = nil,
11731174
commands: [String]? = nil,
1175+
computeruse: AnyCodable? = nil,
11741176
workerruns: WorkerAdmissionHandshake? = nil,
11751177
permissions: [String: AnyCodable]? = nil,
11761178
pathenv: String? = nil,
@@ -1186,6 +1188,7 @@ public struct ConnectParams: Codable, Sendable {
11861188
self.client = client
11871189
self.caps = caps
11881190
self.commands = commands
1191+
self.computeruse = computeruse
11891192
self.workerruns = workerruns
11901193
self.permissions = permissions
11911194
self.pathenv = pathenv
@@ -1203,6 +1206,7 @@ public struct ConnectParams: Codable, Sendable {
12031206
case client
12041207
case caps
12051208
case commands
1209+
case computeruse = "computerUse"
12061210
case workerruns = "workerRuns"
12071211
case permissions
12081212
case pathenv = "pathEnv"

‎extensions/cua-computer/index.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,12 @@ export default definePluginEntry({
2424
);
2525
}
2626
registerComputerUseProvider(api, createCuaComputerProvider());
27+
// Dangerous plugin command: excluded from default allowlists, and the
28+
// Gateway fails closed when this policy registration is missing.
29+
api.registerNodeInvokePolicy({
30+
commands: ["computer.act"],
31+
dangerous: true,
32+
handle: async (context) => await context.invokeNode(),
33+
});
2734
},
2835
});

‎extensions/cua-computer/src/commands.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,43 @@ async function execution(session: CuaDriverSession) {
7979
}
8080

8181
describe("cua-computer provider", () => {
82+
it("advertises only its current foreground coordinate capability", () => {
83+
const { session } = driver();
84+
const descriptor = createCuaComputerProvider({
85+
platform: "linux",
86+
driver: session,
87+
}).capabilities();
88+
expect(descriptor).toEqual({
89+
contractVersion: 2,
90+
provider: {
91+
id: "cua-computer",
92+
label: "CUA Computer",
93+
generation: "cua-computer-coordinate-v1",
94+
},
95+
actions: [
96+
"screenshot",
97+
"left_click",
98+
"right_click",
99+
"middle_click",
100+
"double_click",
101+
"triple_click",
102+
"mouse_move",
103+
"left_click_drag",
104+
"left_mouse_down",
105+
"left_mouse_up",
106+
"scroll",
107+
"type",
108+
"key",
109+
"hold_key",
110+
"wait",
111+
],
112+
targets: ["screen"],
113+
deliveryModes: ["foreground"],
114+
observations: ["image"],
115+
features: { recording: false, agentCursor: false, multiDisplay: false },
116+
});
117+
});
118+
82119
it("uses one typed session for snapshot and frame-authorized click", async () => {
83120
const { session, getDesktopState, getScreenSize, click } = driver();
84121
const computer = await execution(session);

‎extensions/cua-computer/src/commands.ts‎

Lines changed: 66 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs from "node:fs";
22
import path from "node:path";
33
import {
4+
COMPUTER_USE_V2_ACTION_NAMES,
45
parseComputerActParamsJSON,
56
parseScreenSnapshotParamsJSON,
67
type ComputerActParams,
@@ -29,6 +30,24 @@ import {
2930
} from "./frame.js";
3031

3132
const AVAILABILITY_POLL_MS = 5_000;
33+
const CUA_COORDINATE_ACTION_NAMES = COMPUTER_USE_V2_ACTION_NAMES.slice(0, 15);
34+
const CUA_WIRE_ACTION_NAMES = COMPUTER_USE_V2_ACTION_NAMES.slice(1, 14);
35+
type CuaComputerActParams = {
36+
action: ComputerActParams["action"];
37+
displayFrameId?: string;
38+
x?: number;
39+
y?: number;
40+
fromX?: number;
41+
fromY?: number;
42+
text?: string;
43+
keys?: string;
44+
modifiers?: string;
45+
scrollDirection?: "up" | "down" | "left" | "right";
46+
scrollAmount?: number;
47+
durationMs?: number;
48+
screenIndex?: number;
49+
refWidth?: number;
50+
};
3251
// Rastermill enforces inputPixels before resizing, so this must clear the native
3352
// capture, not the delivered frame. 8K (7680x4320 = ~33.2M) is a valid primary
3453
// display; budget above it so full-resolution snapshots reach the downscaler.
@@ -191,7 +210,7 @@ function createImageProcessor(env: NodeJS.ProcessEnv): ImageProcessor {
191210

192211
function clickArgs(
193212
frame: CuaLastFrame,
194-
params: ComputerActParams,
213+
params: CuaComputerActParams,
195214
button: ClickButton,
196215
count: 1 | 2 | 3,
197216
) {
@@ -212,7 +231,7 @@ function clickArgs(
212231
async function currentFrame(
213232
driver: CuaDriverSession,
214233
frameState: CuaFrameState,
215-
params: ComputerActParams,
234+
params: CuaComputerActParams,
216235
signal?: AbortSignal,
217236
): Promise<CuaLastFrame> {
218237
const current = screenSize(await driver.getScreenSize(signal));
@@ -231,36 +250,40 @@ async function handleAct(
231250
params: ComputerActParams,
232251
signal?: AbortSignal,
233252
): Promise<string> {
234-
assertPrimaryDisplay(params.screenIndex);
253+
if (!(CUA_WIRE_ACTION_NAMES as readonly string[]).includes(params.action)) {
254+
throw new Error(`COMPUTER_UNSUPPORTED_ACTION: ${params.action}`);
255+
}
256+
const v1Params = params as CuaComputerActParams;
257+
assertPrimaryDisplay(v1Params.screenIndex);
235258
// `wait` never reaches the wire: core sleeps locally and the Swift wire enum
236259
// has no wait case, so accepting it here would fork the computer.act contract.
237260
if (
238-
params.action === "hold_key" ||
239-
params.action === "left_mouse_down" ||
240-
params.action === "left_mouse_up"
261+
v1Params.action === "hold_key" ||
262+
v1Params.action === "left_mouse_down" ||
263+
v1Params.action === "left_mouse_up"
241264
) {
242265
// Upstream has no desktop keyboard-down API, and its Linux mouse hold tools
243266
// are window-only, so these actions cannot preserve desktop-scope semantics.
244-
throw new Error(`COMPUTER_UNSUPPORTED_ACTION: ${params.action}`);
267+
throw new Error(`COMPUTER_UNSUPPORTED_ACTION: ${v1Params.action}`);
245268
}
246269

247270
// Every action uses scope:"desktop", a global SendInput/XTest/wayland_desktop
248271
// injection that is inherently foreground and ignores delivery_mode (that
249272
// background-vs-foreground contract is window-targeted only). We deliberately
250273
// never send delivery_mode.
251-
switch (params.action) {
274+
switch (v1Params.action) {
252275
case "type": {
253-
if (!params.text) {
276+
if (!v1Params.text) {
254277
throw new Error("COMPUTER_INVALID_REQUEST: text is required for type");
255278
}
256-
assertToolSuccess(await driver.typeText(params.text, signal), "type_text");
279+
assertToolSuccess(await driver.typeText(v1Params.text, signal), "type_text");
257280
break;
258281
}
259282
case "key": {
260283
// press_key applies the modifier array on every backend: X11 via XTest,
261284
// and native Wayland by internally promoting a modifier chord to
262285
// hotkey_focused. No separate hotkey call is needed for chords.
263-
const chord = parseKeyChord(params.keys);
286+
const chord = parseKeyChord(v1Params.keys);
264287
assertToolSuccess(
265288
await driver.pressKey(
266289
{
@@ -274,10 +297,10 @@ async function handleAct(
274297
break;
275298
}
276299
case "scroll": {
277-
if (!params.scrollDirection) {
300+
if (!v1Params.scrollDirection) {
278301
throw new Error("COMPUTER_INVALID_REQUEST: scrollDirection is required for scroll");
279302
}
280-
if (normalizeModifiers(params.modifiers).length > 0) {
303+
if (normalizeModifiers(v1Params.modifiers).length > 0) {
281304
throw new Error(
282305
"COMPUTER_UNSUPPORTED_ACTION: modifier-held scroll is unsupported by cua-driver",
283306
);
@@ -286,20 +309,20 @@ async function handleAct(
286309
// frame-authorized like clicks. We deliberately do not synthesize a point
287310
// from get_cursor_position: that mixes cursor and capture coordinate
288311
// spaces across X11/Wayland/Windows and would scroll an unverified target.
289-
const frame = await currentFrame(driver, frameState, params, signal);
290-
const point = scalePoint(frame, params.x, params.y, params.action);
312+
const frame = await currentFrame(driver, frameState, v1Params, signal);
313+
const point = scalePoint(frame, v1Params.x, v1Params.y, v1Params.action);
291314
const direction = {
292315
up: ScrollDirection.Up,
293316
down: ScrollDirection.Down,
294317
left: ScrollDirection.Left,
295318
right: ScrollDirection.Right,
296-
}[params.scrollDirection];
319+
}[v1Params.scrollDirection];
297320
assertToolSuccess(
298321
await driver.scroll(
299322
{
300323
direction,
301324
// Schema guarantees a positive amount; cap at the driver's max of 50.
302-
amount: BigInt(Math.min(50, params.scrollAmount ?? 3)),
325+
amount: BigInt(Math.min(50, v1Params.scrollAmount ?? 3)),
303326
...point,
304327
},
305328
signal,
@@ -309,49 +332,49 @@ async function handleAct(
309332
break;
310333
}
311334
default: {
312-
const frame = await currentFrame(driver, frameState, params, signal);
313-
switch (params.action) {
335+
const frame = await currentFrame(driver, frameState, v1Params, signal);
336+
switch (v1Params.action) {
314337
case "left_click":
315338
assertToolSuccess(
316-
await driver.click(clickArgs(frame, params, ClickButton.Left, 1), signal),
339+
await driver.click(clickArgs(frame, v1Params, ClickButton.Left, 1), signal),
317340
"click",
318341
);
319342
break;
320343
case "right_click":
321344
assertToolSuccess(
322-
await driver.click(clickArgs(frame, params, ClickButton.Right, 1), signal),
345+
await driver.click(clickArgs(frame, v1Params, ClickButton.Right, 1), signal),
323346
"click",
324347
);
325348
break;
326349
case "middle_click":
327350
assertToolSuccess(
328-
await driver.click(clickArgs(frame, params, ClickButton.Middle, 1), signal),
351+
await driver.click(clickArgs(frame, v1Params, ClickButton.Middle, 1), signal),
329352
"click",
330353
);
331354
break;
332355
case "double_click":
333356
assertToolSuccess(
334-
await driver.click(clickArgs(frame, params, ClickButton.Left, 2), signal),
357+
await driver.click(clickArgs(frame, v1Params, ClickButton.Left, 2), signal),
335358
"click",
336359
);
337360
break;
338361
case "triple_click":
339362
assertToolSuccess(
340-
await driver.click(clickArgs(frame, params, ClickButton.Left, 3), signal),
363+
await driver.click(clickArgs(frame, v1Params, ClickButton.Left, 3), signal),
341364
"click",
342365
);
343366
break;
344367
case "mouse_move": {
345-
const point = scalePoint(frame, params.x, params.y, params.action);
368+
const point = scalePoint(frame, v1Params.x, v1Params.y, v1Params.action);
346369
assertToolSuccess(await driver.moveCursor(point, signal), "move_cursor");
347370
break;
348371
}
349372
case "left_click_drag": {
350-
const from = scalePoint(frame, params.fromX, params.fromY, "drag start");
351-
const to = scalePoint(frame, params.x, params.y, "drag end");
373+
const from = scalePoint(frame, v1Params.fromX, v1Params.fromY, "drag start");
374+
const to = scalePoint(frame, v1Params.x, v1Params.y, "drag end");
352375
// The typed desktop drag API has no modifier field. Refuse instead of
353376
// silently widening a model request into an unmodified drag.
354-
if (normalizeModifiers(params.modifiers).length > 0) {
377+
if (normalizeModifiers(v1Params.modifiers).length > 0) {
355378
throw new Error(
356379
"COMPUTER_UNSUPPORTED_ACTION: modifier-held drag is unsupported by cua-driver",
357380
);
@@ -365,9 +388,9 @@ async function handleAct(
365388
toY: to.y,
366389
// CUA caps desktop drag duration at 10 seconds; clamp rather than
367390
// rejecting a valid computer.act request at the SDK boundary.
368-
...(params.durationMs === undefined
391+
...(v1Params.durationMs === undefined
369392
? {}
370-
: { durationMs: BigInt(Math.min(10_000, params.durationMs)) }),
393+
: { durationMs: BigInt(Math.min(10_000, v1Params.durationMs)) }),
371394
},
372395
signal,
373396
),
@@ -413,6 +436,19 @@ export function createCuaComputerProvider(
413436
return {
414437
id: "cua-computer",
415438
label: "CUA Computer",
439+
capabilities: () => ({
440+
contractVersion: 2,
441+
provider: {
442+
id: "cua-computer",
443+
label: "CUA Computer",
444+
generation: "cua-computer-coordinate-v1",
445+
},
446+
actions: CUA_COORDINATE_ACTION_NAMES,
447+
targets: ["screen"],
448+
deliveryModes: ["foreground"],
449+
observations: ["image"],
450+
features: { recording: false, agentCursor: false, multiDisplay: false },
451+
}),
416452
isAvailable,
417453
watchAvailability: (_context, onChange) => {
418454
let knownAvailable = isAvailable();

‎extensions/cua-computer/src/computer-use-peekaboo-parity.test-fixtures.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
* documented 25.
1111
*/
1212

13+
import type { ComputerUseV2ActionName } from "openclaw/plugin-sdk/computer-use";
1314
import type {
1415
ComputerUseDeliveryMode,
1516
ComputerUseProviderId,
16-
ComputerUseV2ActionName,
1717
CuaMcpToolName,
1818
} from "./computer-use-provider-parity.test-fixtures.js";
1919

‎extensions/cua-computer/src/computer-use-provider-parity.test-fixtures.ts‎

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,51 +9,7 @@
99
* source commit below; that catalog contains 26 tools, not the previously
1010
* documented 25.
1111
*/
12-
13-
export const COMPUTER_USE_V2_ACTION_NAMES = [
14-
"screenshot",
15-
"left_click",
16-
"right_click",
17-
"middle_click",
18-
"double_click",
19-
"triple_click",
20-
"mouse_move",
21-
"left_click_drag",
22-
"left_mouse_down",
23-
"left_mouse_up",
24-
"scroll",
25-
"type",
26-
"key",
27-
"hold_key",
28-
"wait",
29-
"list_apps",
30-
"list_windows",
31-
"get_accessibility_tree",
32-
"get_cursor_position",
33-
"get_window_state",
34-
"launch_app",
35-
"kill_app",
36-
"bring_to_front",
37-
"set_value",
38-
"zoom",
39-
"get_browser_state",
40-
"browser_prepare",
41-
"browser_navigate",
42-
"browser_click",
43-
"browser_type",
44-
"browser_dialog",
45-
"browser_set_input_files",
46-
"browser_download",
47-
"browser_pointer",
48-
"escalate_scope",
49-
"get_recording_state",
50-
"start_recording",
51-
"stop_recording",
52-
"replay_trajectory",
53-
"invoke_menu",
54-
] as const;
55-
56-
export type ComputerUseV2ActionName = (typeof COMPUTER_USE_V2_ACTION_NAMES)[number];
12+
import type { ComputerUseV2ActionName } from "openclaw/plugin-sdk/computer-use";
5713
export type ComputerUseProviderId = "cua" | "peekaboo";
5814
export type ComputerUseDeliveryMode = "background" | "foreground";
5915

‎extensions/cua-computer/src/computer-use-provider-parity.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { COMPUTER_USE_V2_ACTION_NAMES } from "openclaw/plugin-sdk/computer-use";
12
import { describe, expect, it } from "vitest";
23
import {
34
COMPUTER_USE_V2_PROVIDER_ACTION_SUPPORT,
@@ -7,7 +8,6 @@ import {
78
PEEKABOO_PROVIDER_PARITY_SOURCE,
89
} from "./computer-use-peekaboo-parity.test-fixtures.js";
910
import {
10-
COMPUTER_USE_V2_ACTION_NAMES,
1111
CUA_MCP_TOOL_NAMES,
1212
CUA_MCP_TOOL_PARITY,
1313
CUA_PROVIDER_PARITY_SOURCE,

0 commit comments

Comments
 (0)