11import { randomUUID } from "node:crypto" ;
22import {
3+ CaptureScope ,
34 ClickButton ,
45 CuaDriver ,
56 DesktopScope ,
@@ -60,6 +61,9 @@ class DirectCuaDriverSession implements CuaDriverSession {
6061 readonly generation = randomUUID ( ) ;
6162 private readonly runtime : CuaDriverLike ;
6263 private readonly session : CuaDriverSessionLike ;
64+ private readonly publicSession = `openclaw-${ randomUUID ( ) } ` ;
65+ private startPromise : Promise < void > | undefined ;
66+ private started = false ;
6367 private disposed = false ;
6468
6569 constructor ( ) {
@@ -70,49 +74,99 @@ class DirectCuaDriverSession implements CuaDriverSession {
7074 authorization : { ...CUA_OPENCLAW_AUTHORIZATION } ,
7175 } ) ;
7276 this . session = createTrustedSession ( this . runtime , {
73- publicSession : `openclaw- ${ randomUUID ( ) } ` ,
77+ publicSession : this . publicSession ,
7478 mode : SessionPermissionMode . Unrestricted ,
7579 ttlSeconds : CUA_OPENCLAW_AUTHORIZATION . maxSessionTtlSeconds ,
7680 idleTtlSeconds : CUA_OPENCLAW_AUTHORIZATION . maxIdleTtlSeconds ,
7781 } ) ;
7882 }
7983
84+ private async ensureStarted ( signal ?: AbortSignal ) : Promise < void > {
85+ if ( this . disposed ) {
86+ throw new Error ( "COMPUTER_DRIVER_UNAVAILABLE: cua-computer is stopping" ) ;
87+ }
88+ if ( ! this . startPromise ) {
89+ const start = this . session
90+ . startSession (
91+ { session : this . publicSession , captureScope : CaptureScope . Desktop } ,
92+ asyncOptions ( signal ) ,
93+ )
94+ . then ( ( ) => {
95+ this . started = true ;
96+ } ) ;
97+ this . startPromise = start ;
98+ try {
99+ await start ;
100+ } catch ( error ) {
101+ if ( this . startPromise === start ) {
102+ this . startPromise = undefined ;
103+ }
104+ throw error ;
105+ }
106+ return ;
107+ }
108+ await this . startPromise ;
109+ }
110+
111+ private async invoke < T > (
112+ signal : AbortSignal | undefined ,
113+ operation : ( ) => Promise < T > ,
114+ ) : Promise < T > {
115+ await this . ensureStarted ( signal ) ;
116+ return await operation ( ) ;
117+ }
118+
80119 isAvailable ( ) : boolean {
81120 return ! this . disposed && this . runtime . isAvailable ( ) ;
82121 }
83122 resetAvailabilityCache ( ) : void { }
84- getDesktopState ( signal ?: AbortSignal ) {
85- return this . session . getDesktopState ( { } , asyncOptions ( signal ) ) ;
123+ async getDesktopState ( signal ?: AbortSignal ) {
124+ return await this . invoke ( signal , ( ) => this . session . getDesktopState ( { } , asyncOptions ( signal ) ) ) ;
86125 }
87- getScreenSize ( signal ?: AbortSignal ) {
88- return this . session . getScreenSize ( { } , asyncOptions ( signal ) ) ;
126+ async getScreenSize ( signal ?: AbortSignal ) {
127+ return await this . invoke ( signal , ( ) => this . session . getScreenSize ( { } , asyncOptions ( signal ) ) ) ;
89128 }
90- click ( input : { x : number ; y : number ; button : ClickButton ; count : number } , signal ?: AbortSignal ) {
91- return this . session . click ( { ...input , scope : DesktopScope . Desktop } , asyncOptions ( signal ) ) ;
129+ async click (
130+ input : { x : number ; y : number ; button : ClickButton ; count : number } ,
131+ signal ?: AbortSignal ,
132+ ) {
133+ return await this . invoke ( signal , ( ) =>
134+ this . session . click ( { ...input , scope : DesktopScope . Desktop } , asyncOptions ( signal ) ) ,
135+ ) ;
92136 }
93- drag (
137+ async drag (
94138 input : { fromX : number ; fromY : number ; toX : number ; toY : number ; durationMs ?: bigint } ,
95139 signal ?: AbortSignal ,
96140 ) {
97- return this . session . drag ( { ...input , scope : DesktopScope . Desktop } , asyncOptions ( signal ) ) ;
141+ return await this . invoke ( signal , ( ) =>
142+ this . session . drag ( { ...input , scope : DesktopScope . Desktop } , asyncOptions ( signal ) ) ,
143+ ) ;
98144 }
99- moveCursor ( input : { x : number ; y : number } , signal ?: AbortSignal ) {
100- return this . session . moveCursor ( { ...input , scope : DesktopScope . Desktop } , asyncOptions ( signal ) ) ;
145+ async moveCursor ( input : { x : number ; y : number } , signal ?: AbortSignal ) {
146+ return await this . invoke ( signal , ( ) =>
147+ this . session . moveCursor ( { ...input , scope : DesktopScope . Desktop } , asyncOptions ( signal ) ) ,
148+ ) ;
101149 }
102- scroll (
150+ async scroll (
103151 input : { x : number ; y : number ; direction : ScrollDirection ; amount : bigint } ,
104152 signal ?: AbortSignal ,
105153 ) {
106- return this . session . scroll (
107- { ...input , scope : DesktopScope . Desktop , by : ScrollBy . Line } ,
108- asyncOptions ( signal ) ,
154+ return await this . invoke ( signal , ( ) =>
155+ this . session . scroll (
156+ { ...input , scope : DesktopScope . Desktop , by : ScrollBy . Line } ,
157+ asyncOptions ( signal ) ,
158+ ) ,
109159 ) ;
110160 }
111- typeText ( text : string , signal ?: AbortSignal ) {
112- return this . session . typeText ( { text, scope : DesktopScope . Desktop } , asyncOptions ( signal ) ) ;
161+ async typeText ( text : string , signal ?: AbortSignal ) {
162+ return await this . invoke ( signal , ( ) =>
163+ this . session . typeText ( { text, scope : DesktopScope . Desktop } , asyncOptions ( signal ) ) ,
164+ ) ;
113165 }
114- pressKey ( input : { key : string ; modifiers : string [ ] } , signal ?: AbortSignal ) {
115- return this . session . pressKey ( { ...input , scope : DesktopScope . Desktop } , asyncOptions ( signal ) ) ;
166+ async pressKey ( input : { key : string ; modifiers : string [ ] } , signal ?: AbortSignal ) {
167+ return await this . invoke ( signal , ( ) =>
168+ this . session . pressKey ( { ...input , scope : DesktopScope . Desktop } , asyncOptions ( signal ) ) ,
169+ ) ;
116170 }
117171
118172 async dispose ( ) : Promise < void > {
@@ -121,6 +175,20 @@ class DirectCuaDriverSession implements CuaDriverSession {
121175 }
122176 this . disposed = true ;
123177 let failure : unknown ;
178+ try {
179+ await this . startPromise ;
180+ } catch ( error ) {
181+ failure = error ;
182+ }
183+ if ( this . started ) {
184+ try {
185+ // End the native desktop session before revoking its trusted handle.
186+ // Closing only the handle can leave the started session behind on stop.
187+ await this . session . endSession ( { session : this . publicSession } ) ;
188+ } catch ( error ) {
189+ failure ??= error ;
190+ }
191+ }
124192 try {
125193 this . session . close ( ) ;
126194 } catch ( error ) {
0 commit comments