@@ -26,6 +26,7 @@ import {
2626import {
2727 parseRecorderArgs ,
2828 readRecorderSession ,
29+ type ActionsOptions ,
2930 type ArtifactsOptions ,
3031 type RecoverOptions ,
3132 recorderUsageText ,
@@ -174,14 +175,13 @@ export DISPLAY=:99
174175win="$(wmctrl -lx | awk 'tolower($0) ~ /telegramdesktop/ {print $1; exit}')"
175176test -n "$win"
176177eval "$(xdotool getwindowgeometry --shell "$win")"
177- printf '%s %s %s %s\n' "$X" "$Y" "$WIDTH" "$HEIGHT"` ;
178+ printf '%s %s %s %s %s \n' "$win" "$X" "$Y" "$WIDTH" "$HEIGHT"` ;
178179}
179180
180- function renderHideTelegramWindow ( ) : string {
181+ function renderHideTelegramWindow ( windowId : string ) : string {
181182 return `set -euo pipefail
182183export DISPLAY=:99
183- win="$(wmctrl -lx | awk 'tolower($0) ~ /telegramdesktop/ {print $1; exit}')"
184- test -n "$win"
184+ win=${ shellQuote ( windowId ) }
185185xdotool windowminimize "$win"
186186sleep 0.2` ;
187187}
@@ -236,19 +236,26 @@ exit 1`;
236236
237237export function parseWindowGeometry ( raw : string ) : {
238238 height : number ;
239+ id : string ;
239240 width : number ;
240241 x : number ;
241242 y : number ;
242243} {
243- const parts = raw . trim ( ) . split ( / \s + / u) . map ( Number ) ;
244- if ( parts . length !== 4 || parts . some ( ( value ) => ! Number . isFinite ( value ) || value < 0 ) ) {
244+ const [ id , ...rawGeometry ] = raw . trim ( ) . split ( / \s + / u) ;
245+ const parts = rawGeometry . map ( Number ) ;
246+ if (
247+ ! id ||
248+ ! / ^ 0 x [ 0 - 9 a - f ] + $ / iu. test ( id ) ||
249+ parts . length !== 4 ||
250+ parts . some ( ( value ) => ! Number . isFinite ( value ) || value < 0 )
251+ ) {
245252 throw new Error ( `Telegram Desktop window geometry was not readable: ${ raw . trim ( ) } ` ) ;
246253 }
247254 const [ x , y , width , height ] = parts as [ number , number , number , number ] ;
248255 if ( width < 200 || height < 200 ) {
249256 throw new Error ( `Telegram Desktop window is too small to crop: ${ width } x${ height } ` ) ;
250257 }
251- return { height, width, x, y } ;
258+ return { height, id , width, x, y } ;
252259}
253260
254261function driverCommand ( userDriver : string [ ] , args : string [ ] ) {
@@ -611,7 +618,7 @@ async function startRecorderAttempt(
611618 // The lane clears prior history before recorder start. Keep the empty chat hidden until
612619 // the first session-owned send is ready, so setup frames reveal neither account UI nor chat.
613620 await operations . sshRun ( {
614- command : renderHideTelegramWindow ( ) ,
621+ command : renderHideTelegramWindow ( windowGeometry . id ) ,
615622 cwd,
616623 inspect,
617624 run : operations . runCommand ,
@@ -815,6 +822,93 @@ export async function viewRecorder(
815822 } ) ;
816823}
817824
825+ const desktopActionsSchema = z
826+ . array (
827+ z . discriminatedUnion ( "command" , [
828+ z . object ( {
829+ button : z . number ( ) . int ( ) . min ( 1 ) . max ( 5 ) . default ( 1 ) ,
830+ command : z . literal ( "click" ) ,
831+ x : z . number ( ) . int ( ) . nonnegative ( ) ,
832+ y : z . number ( ) . int ( ) . nonnegative ( ) ,
833+ } ) ,
834+ z . object ( {
835+ command : z . literal ( "key" ) ,
836+ keys : z
837+ . array ( z . string ( ) . regex ( / ^ [ A - Z a - z 0 - 9 _ + : - ] + $ / u) )
838+ . min ( 1 )
839+ . max ( 20 ) ,
840+ } ) ,
841+ z . object ( { command : z . literal ( "sleep" ) , milliseconds : z . number ( ) . int ( ) . min ( 1 ) . max ( 30_000 ) } ) ,
842+ z . object ( {
843+ command : z . literal ( "type" ) ,
844+ delayMs : z . number ( ) . int ( ) . min ( 0 ) . max ( 1_000 ) . default ( 5 ) ,
845+ text : z . string ( ) . min ( 1 ) . max ( 10_000 ) ,
846+ } ) ,
847+ ] ) ,
848+ )
849+ . min ( 1 )
850+ . max ( 100 ) ;
851+
852+ export async function runRecorderActions (
853+ cwd : string ,
854+ opts : ActionsOptions ,
855+ operations : RecorderOperations = defaultOperations ,
856+ ) : Promise < { results : Array < { command : string ; stderr : string ; stdout : string } > } > {
857+ const sessionPath = resolveRecorderPath ( cwd , opts . sessionPath , "--session" ) ;
858+ const actionsPath = resolveRecorderPath ( cwd , opts . actionsFile , "--actions-file" ) ;
859+ const actionsStat = fs . lstatSync ( actionsPath ) ;
860+ if ( ! actionsStat . isFile ( ) || actionsStat . isSymbolicLink ( ) || actionsStat . size > 64 * 1024 ) {
861+ throw new Error ( "--actions-file must be a regular file no larger than 64 KiB." ) ;
862+ }
863+ const actions = desktopActionsSchema . parse ( JSON . parse ( fs . readFileSync ( actionsPath , "utf8" ) ) ) ;
864+ const session = readRecorderSession ( sessionPath ) ;
865+ for ( const action of actions ) {
866+ if (
867+ action . command === "click" &&
868+ ( action . x >= session . window . width || action . y >= session . window . height )
869+ ) {
870+ throw new Error ( "click coordinates must stay inside the Telegram window." ) ;
871+ }
872+ }
873+ const crabboxBin = process . env . OPENCLAW_TELEGRAM_USER_CRABBOX_BIN ?. trim ( ) || "crabbox" ;
874+ const inspect = await sessionInspect ( { crabboxBin, cwd, operations, session } ) ;
875+ const results : Array < { command : string ; stderr : string ; stdout : string } > = [ ] ;
876+ for ( const action of actions ) {
877+ if ( action . command === "sleep" ) {
878+ await sleep ( action . milliseconds ) ;
879+ results . push ( { command : "sleep" , stderr : "" , stdout : "" } ) ;
880+ continue ;
881+ }
882+ const telegramWindow = `win=${ shellQuote ( session . window . id ) }
883+ if ! wmctrl -lx | awk -v win="$win" 'tolower($1) == tolower(win) && tolower($0) ~ /telegramdesktop/ {found=1} END {exit !found}'; then
884+ echo "Recorded Telegram window $win no longer exists." >&2
885+ exit 1
886+ fi
887+ eval "$(xdotool getwindowgeometry --shell "$win")"
888+ if [ "$X" -ne ${ session . window . x } ] || [ "$Y" -ne ${ session . window . y } ] || [ "$WIDTH" -ne ${ session . window . width } ] || [ "$HEIGHT" -ne ${ session . window . height } ]; then
889+ echo "Recorded Telegram window $win moved or resized." >&2
890+ exit 1
891+ fi
892+ ` ;
893+ const actionCommand =
894+ action . command === "click"
895+ ? `xdotool windowactivate --sync "$win" mousemove --window "$win" ${ action . x } ${ action . y } click ${ action . button } `
896+ : action . command === "key"
897+ ? `xdotool key --window "$win" ${ action . keys . map ( shellQuote ) . join ( " " ) } `
898+ : `xdotool type --window "$win" --delay ${ action . delayMs } -- ${ shellQuote ( action . text ) } ` ;
899+ const result = await operations . sshRun ( {
900+ command : `export DISPLAY=:99\n${ telegramWindow } ${ actionCommand } ` ,
901+ cwd,
902+ inspect,
903+ run : operations . runCommand ,
904+ stdio : "pipe" ,
905+ timeoutMs : opts . timeoutSeconds * 1000 ,
906+ } ) ;
907+ results . push ( { command : action . command , ...result } ) ;
908+ }
909+ return { results } ;
910+ }
911+
818912async function captureScreenshot ( params : {
819913 crop : ReturnType < typeof proofViewport > ;
820914 cwd : string ;
@@ -1067,6 +1161,10 @@ async function main(): Promise<void> {
10671161 console . log ( `Telegram Desktop opened message ${ opts . messageId } .` ) ;
10681162 return ;
10691163 }
1164+ if ( opts . command === "actions" ) {
1165+ console . log ( JSON . stringify ( await runRecorderActions ( cwd , opts ) , null , 2 ) ) ;
1166+ return ;
1167+ }
10701168 if ( opts . command === "screenshot" ) {
10711169 console . log ( await screenshotRecorder ( cwd , opts ) ) ;
10721170 return ;
0 commit comments