11import fs from "node:fs" ;
22import path from "node:path" ;
33import {
4+ COMPUTER_USE_V2_ACTION_NAMES ,
45 parseComputerActParamsJSON ,
56 parseScreenSnapshotParamsJSON ,
67 type ComputerActParams ,
@@ -29,6 +30,24 @@ import {
2930} from "./frame.js" ;
3031
3132const 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
192211function 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(
212231async 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 ( ) ;
0 commit comments