11import {
22 type DeviceError ,
33 type DeviceHostId ,
4+ type DeviceHostSummary ,
45 type DeviceId ,
56 type DevicePlatform ,
67 type DeviceSummary ,
@@ -72,6 +73,26 @@ const requireDeviceAccess = McpInvocationContext.requireThreadMcpCapability("dev
7273 ) ,
7374) ;
7475
76+ export const resolveHostId = Effect . fn ( "handlers.resolveHostId" ) ( function * (
77+ hosts : ReadonlyArray < DeviceHostSummary > ,
78+ hostId : string | undefined ,
79+ ) : Effect . fn . Return < DeviceHostId | undefined , DeviceToolUnavailableError > {
80+ if ( ! hostId ) return undefined ;
81+ const exact = hosts . find ( ( host ) => host . id === hostId ) ;
82+ if ( exact ) return exact . id ;
83+ const labelMatches = hosts . filter ( ( host ) => host . label . toLowerCase ( ) === hostId . toLowerCase ( ) ) ;
84+ if ( labelMatches . length === 1 ) return labelMatches [ 0 ] ! . id ;
85+ const listed = ( labelMatches . length > 1 ? labelMatches : hosts )
86+ . map ( ( host ) => `${ host . label } (${ host . id } )` )
87+ . join ( ", " ) ;
88+ return yield * new DeviceToolUnavailableError ( {
89+ reason :
90+ labelMatches . length > 1
91+ ? `Several device hosts are named ${ hostId } ; pass one id: ${ listed } .`
92+ : `No device host ${ hostId } . Known hosts: ${ listed || "none" } .` ,
93+ } ) ;
94+ } ) ;
95+
7596const pickDevice = Effect . fn ( "handlers.pickDevice" ) ( function * (
7697 devices : ReadonlyArray < DeviceSummary > ,
7798 input : {
@@ -126,7 +147,7 @@ const handlers = {
126147 "Device support is off. Ask the user to enable it in the Device panel before installing or starting device tools." ,
127148 } ) ;
128149 }
129- const hostId = input ?. hostId ;
150+ const hostId = yield * resolveHostId ( state . hosts , input ?. hostId ) ;
130151 const open = state . sessions
131152 . filter ( ( session ) => session . threadId === scope . thread . threadId )
132153 . map ( ( session ) => ( { hostId : session . hostId , deviceId : session . deviceId } ) ) ;
@@ -153,7 +174,8 @@ const handlers = {
153174 "Device support is off. Ask the user to enable it in the Device panel before installing or starting device tools." ,
154175 } ) ;
155176 }
156- const target = yield * pickDevice ( state . devices , input ) ;
177+ const hostId = yield * resolveHostId ( state . hosts , input . hostId ) ;
178+ const target = yield * pickDevice ( state . devices , { ...input , hostId } ) ;
157179 // Resolve consent and agent connectivity before booting or registering a session.
158180 if ( ! ( yield * devices . agentReadinessIfSupported ( target . hostId ) ) ) {
159181 return yield * new DeviceToolUnavailableError ( {
@@ -221,8 +243,9 @@ const handlers = {
221243 const scope = yield * requireDeviceAccess ;
222244 const devices = yield * DeviceService . DeviceService ;
223245 const sessions = yield * devices . sessionsForThread ( scope . thread . threadId ) ;
246+ const inputHostId = yield * resolveHostId ( ( yield * devices . state ) . hosts , input . hostId ) ;
224247 const hostId =
225- input . deviceId === undefined ? input . hostId : ( input . hostId ?? LOCAL_DEVICE_HOST_ID ) ;
248+ input . deviceId === undefined ? inputHostId : ( inputHostId ?? LOCAL_DEVICE_HOST_ID ) ;
226249 const target = sessions . findLast (
227250 ( session ) =>
228251 ( hostId === undefined || session . hostId === hostId ) &&
@@ -251,9 +274,10 @@ const handlers = {
251274 Effect . gen ( function * ( ) {
252275 const scope = yield * requireDeviceAccess ;
253276 const devices = yield * DeviceService . DeviceService ;
277+ const hostId = yield * resolveHostId ( ( yield * devices . state ) . hosts , input . hostId ) ;
254278 yield * devices . close ( {
255279 threadId : scope . thread . threadId ,
256- ...( input . hostId === undefined ? { } : { hostId : input . hostId } ) ,
280+ ...( hostId === undefined ? { } : { hostId } ) ,
257281 ...( input . deviceId === undefined ? { } : { deviceId : input . deviceId } ) ,
258282 ...( input . shutdown === undefined ? { } : { shutdown : input . shutdown } ) ,
259283 } ) ;
0 commit comments