@@ -23,7 +23,9 @@ import { desktopAppIcon, desktopAppLabel } from "./desktop-app-presentation.ts";
2323import { DesktopClient , type DesktopConnectionHandle } from "./desktop-client.ts" ;
2424import { desktopCredentialRequirement } from "./desktop-panel-credentials.ts" ;
2525import { desktopPanelLauncherStyles } from "./desktop-panel-launcher-styles.ts" ;
26+ import { type DesktopPanelState , renderDesktopPanelRecovery } from "./desktop-panel-state.ts" ;
2627import { desktopPanelStyles } from "./desktop-panel-styles.ts" ;
28+ import { desktopSourceForEnvironment } from "./desktop-source.ts" ;
2729
2830const CLOSE_GLYPH = svg `< svg viewBox ="0 0 16 16 " width ="12 " height ="12 " fill ="none " stroke ="currentColor " stroke-width ="1.5 " stroke-linecap ="round "> < path d ="M4 4l8 8M12 4l-8 8 " /> </ svg > ` ;
2931const DOCK_BOTTOM_GLYPH = svg `< svg viewBox ="0 0 16 16 " width ="13 " height ="13 " fill ="none " stroke ="currentColor " stroke-width ="1.3 "> < rect x ="2 " y ="2.5 " width ="12 " height ="11 " rx ="1.5 " /> < path d ="M2 10h12 " /> </ svg > ` ;
@@ -38,7 +40,6 @@ const panelLayout = createDockPanelLayout({
3840 defaultHeight : 420 ,
3941 defaultWidth : 560 ,
4042} ) ;
41- type DesktopPanelState = "picker" | "credentials" | "connecting" | "connected" | "disconnected" ;
4243type DesktopAppId = WorkerDesktopAppId ;
4344type DesktopCredentials = { username ?: string ; password ?: string } ;
4445type PendingDesktopConnection = {
@@ -49,16 +50,6 @@ type PendingDesktopConnection = {
4950} ;
5051type ObservedDesktopConnection = PendingDesktopConnection & { observed : DesktopObserveResult } ;
5152
52- function desktopSourceForEnvironment ( environment : Pick < EnvironmentSummary , "id" > ) : DesktopSource {
53- if ( environment . id === "gateway" ) {
54- return { kind : "host" } ;
55- }
56- if ( environment . id . startsWith ( "node:" ) && environment . id . length > "node:" . length ) {
57- return { kind : "node" , nodeId : environment . id . slice ( "node:" . length ) } ;
58- }
59- return { kind : "environment" , environmentId : environment . id } ;
60- }
61-
6253/** `<openclaw-desktop-panel>` — dockable RFB access to Gateway desktop sources. */
6354class OpenClawDesktopPanel extends OpenClawLitElement {
6455 @property ( { attribute : false } ) client : GatewayBrowserClient | null = null ;
@@ -151,7 +142,7 @@ class OpenClawDesktopPanel extends OpenClawLitElement {
151142 const wasOpen = this . dockLayout . open ;
152143 this . dockLayout . setOpen ( true ) ;
153144 if ( detail ?. environmentId ) {
154- void this . connectEnvironment ( detail . environmentId , false ) ;
145+ void this . connectRequestedEnvironment ( detail . environmentId ) ;
155146 } else if ( ! wasOpen ) {
156147 void this . refreshEnvironments ( ) ;
157148 } else if ( detail ?. open !== true ) {
@@ -191,31 +182,49 @@ class OpenClawDesktopPanel extends OpenClawLitElement {
191182 this . launchErrorText = null ;
192183 }
193184
194- private async refreshEnvironments ( ) : Promise < void > {
185+ private async refreshEnvironments ( expectedOperationId ?: number ) : Promise < boolean > {
195186 const client = this . client ;
196187 if ( ! client || ! this . available ) {
197- return ;
188+ return false ;
198189 }
199- const operationId = ++ this . operationId ;
190+ const operationId = expectedOperationId ?? ++ this . operationId ;
200191 this . loading = true ;
201192 this . errorText = null ;
202193 try {
203194 const result = await client . request < EnvironmentsListResult > ( "environments.list" , { } ) ;
204195 if ( operationId !== this . operationId ) {
205- return ;
196+ return false ;
206197 }
207198 this . environments = result . environments . filter ( ( environment ) => environment . desktop === true ) ;
199+ return true ;
208200 } catch ( error ) {
209201 if ( operationId === this . operationId ) {
210202 this . errorText = t ( "desktop.errors.listFailed" , { error : formatUiError ( error ) } ) ;
211203 }
204+ return false ;
212205 } finally {
213206 if ( operationId === this . operationId ) {
214207 this . loading = false ;
215208 }
216209 }
217210 }
218211
212+ private async connectRequestedEnvironment ( environmentId : string ) : Promise < void > {
213+ this . returnToPicker ( ) ;
214+ this . environmentId = environmentId ;
215+ this . state = "connecting" ;
216+ const operationId = this . operationId ;
217+ const inventoryLoaded = await this . refreshEnvironments ( operationId ) ;
218+ if ( operationId !== this . operationId ) {
219+ return ;
220+ }
221+ if ( ! inventoryLoaded ) {
222+ this . state = "inventory-error" ;
223+ return ;
224+ }
225+ void this . connectEnvironment ( environmentId , false ) ;
226+ }
227+
219228 private async connectEnvironment (
220229 environmentId : string ,
221230 control : boolean ,
@@ -229,11 +238,11 @@ class OpenClawDesktopPanel extends OpenClawLitElement {
229238 this . clearLaunchState ( ) ;
230239 this . credentials = undefined ;
231240 this . credentialAuth = undefined ;
232- this . desktopApps = [
233- ...( this . environments . find ( ( environment ) => environment . id === environmentId ) ?. worker
234- ?. desktopApps ?? [ ] ) ,
235- ] ;
236241 }
242+ this . desktopApps = [
243+ ...( this . environments . find ( ( environment ) => environment . id === environmentId ) ?. worker
244+ ?. desktopApps ?? [ ] ) ,
245+ ] ;
237246 this . disconnectConnection ( ) ;
238247 const operationId = this . operationId ;
239248 const environment = this . environments . find ( ( candidate ) => candidate . id === environmentId ) ?? {
@@ -628,27 +637,6 @@ class OpenClawDesktopPanel extends OpenClawLitElement {
628637 ` ;
629638 }
630639
631- private renderDisconnected ( ) {
632- return html `
633- < div class ="desktop-status ">
634- < div >
635- ${ t ( "desktop.disconnected" , {
636- reason : this . disconnectedReason ?? t ( "desktop.unknownReason" ) ,
637- } ) }
638- </ div >
639- < button
640- class ="desktop-button desktop-button--primary "
641- type ="button "
642- @click =${ ( ) =>
643- this . environmentId &&
644- void this . connectEnvironment ( this . environmentId , this . controlling ) }
645- >
646- ${ t ( "desktop.reconnect" ) }
647- </ button >
648- </ div >
649- ` ;
650- }
651-
652640 private renderCredentials ( ) {
653641 const ardAccount = this . credentialAuth === "ard-account" ;
654642 return html `
@@ -710,10 +698,18 @@ class OpenClawDesktopPanel extends OpenClawLitElement {
710698 : nothing }
711699 ${ this . state === "picker"
712700 ? this . renderPicker ( )
713- : this . state === "credentials"
714- ? this . renderCredentials ( )
715- : this . state === "disconnected"
716- ? this . renderDisconnected ( )
701+ : this . state === "inventory-error" || this . state === "disconnected"
702+ ? renderDesktopPanelRecovery ( {
703+ inventoryError : this . state === "inventory-error" ,
704+ reason : this . disconnectedReason ,
705+ onRetry : ( ) =>
706+ this . environmentId &&
707+ void ( this . state === "inventory-error"
708+ ? this . connectRequestedEnvironment ( this . environmentId )
709+ : this . connectEnvironment ( this . environmentId , this . controlling ) ) ,
710+ } )
711+ : this . state === "credentials"
712+ ? this . renderCredentials ( )
717713 : this . renderConnection ( ) }
718714 </ div >
719715 </ section >
0 commit comments