@@ -93,6 +93,29 @@ const requireDeviceAccess = McpInvocationContext.requireThreadMcpCapability("dev
9393 ) ,
9494) ;
9595
96+ export const resolveHostId = Effect . fn ( "handlers.resolveHostId" ) ( function * (
97+ hosts : ReadonlyArray < DeviceHostSummary > ,
98+ hostId : string | undefined ,
99+ ) : Effect . fn . Return < DeviceHostId | undefined , DeviceToolUnavailableError > {
100+ if ( ! hostId ) return undefined ;
101+ const exact = hosts . find ( ( host ) => host . id === hostId ) ;
102+ if ( exact ) return exact . id ;
103+ const labelMatches = hosts . filter ( ( host ) => host . label . toLowerCase ( ) === hostId . toLowerCase ( ) ) ;
104+ if ( labelMatches . length === 1 ) return labelMatches [ 0 ] ! . id ;
105+ const candidates = labelMatches . length > 1 ? labelMatches : hosts ;
106+ const listed = [
107+ ...candidates . slice ( 0 , 10 ) . map ( ( host ) => `${ host . label . slice ( 0 , 64 ) } (${ host . id } )` ) ,
108+ ...( candidates . length > 10 ? [ `${ candidates . length - 10 } more` ] : [ ] ) ,
109+ ] . join ( ", " ) ;
110+ const selector = hostId . slice ( 0 , 128 ) ;
111+ return yield * new DeviceToolUnavailableError ( {
112+ reason :
113+ labelMatches . length > 1
114+ ? `Several device hosts are named ${ selector } ; pass one id: ${ listed } .`
115+ : `No device host ${ selector } . Known hosts: ${ listed || "none" } .` ,
116+ } ) ;
117+ } ) ;
118+
96119const hostName = ( host : { readonly id : DeviceHostId ; readonly label ?: string | undefined } ) =>
97120 host . label === undefined ? `hostId ${ host . id } ` : `'${ host . label } ' (hostId ${ host . id } )` ;
98121
@@ -177,7 +200,7 @@ const handlers = {
177200 "Device support is off. Ask the user to enable it in the Device panel before installing or starting device tools." ,
178201 } ) ;
179202 }
180- const hostId = input ?. hostId ;
203+ const hostId = yield * resolveHostId ( state . hosts , input ?. hostId ) ;
181204 const open = state . sessions
182205 . filter ( ( session ) => session . threadId === scope . thread . threadId )
183206 . map ( ( session ) => ( { hostId : session . hostId , deviceId : session . deviceId } ) ) ;
@@ -204,7 +227,8 @@ const handlers = {
204227 "Device support is off. Ask the user to enable it in the Device panel before installing or starting device tools." ,
205228 } ) ;
206229 }
207- const target = yield * pickDevice ( state , input ) ;
230+ const hostId = yield * resolveHostId ( state . hosts , input . hostId ) ;
231+ const target = yield * pickDevice ( state , { ...input , hostId } ) ;
208232 // Resolve consent and agent connectivity before booting or registering a session.
209233 if ( ! ( yield * devices . agentReadinessIfSupported ( target . hostId ) ) ) {
210234 return yield * new DeviceToolUnavailableError ( {
@@ -273,14 +297,18 @@ const handlers = {
273297 const scope = yield * requireDeviceAccess ;
274298 const devices = yield * DeviceService . DeviceService ;
275299 const threadId = scope . thread . threadId ;
300+ const hostId =
301+ input . hostId === undefined
302+ ? undefined
303+ : yield * resolveHostId ( ( yield * devices . state ) . hosts , input . hostId ) ;
276304 const target =
277305 input . deviceId === undefined
278306 ? ( yield * devices . sessionsForThread ( threadId ) ) . findLast (
279- ( session ) => input . hostId === undefined || session . hostId === input . hostId ,
307+ ( session ) => hostId === undefined || session . hostId === hostId ,
280308 )
281309 : Option . getOrUndefined (
282310 yield * devices
283- . locateSession ( { threadId, deviceId : input . deviceId , hostId : input . hostId } )
311+ . locateSession ( { threadId, deviceId : input . deviceId , hostId } )
284312 . pipe ( mapLocateErrors ) ,
285313 ) ;
286314 if ( ! target ) {
@@ -307,13 +335,17 @@ const handlers = {
307335 const scope = yield * requireDeviceAccess ;
308336 const devices = yield * DeviceService . DeviceService ;
309337 const deviceId = input . deviceId ;
338+ const inputHostId =
339+ input . hostId === undefined
340+ ? undefined
341+ : yield * resolveHostId ( ( yield * devices . state ) . hosts , input . hostId ) ;
310342 const open =
311343 deviceId === undefined
312344 ? Option . none ( )
313345 : yield * devices
314- . locateSession ( { threadId : scope . thread . threadId , deviceId, hostId : input . hostId } )
346+ . locateSession ( { threadId : scope . thread . threadId , deviceId, hostId : inputHostId } )
315347 . pipe ( mapLocateErrors ) ;
316- const hostId = Option . isSome ( open ) ? open . value . hostId : input . hostId ;
348+ const hostId = Option . isSome ( open ) ? open . value . hostId : inputHostId ;
317349 yield * devices . close ( {
318350 threadId : scope . thread . threadId ,
319351 ...( hostId === undefined ? { } : { hostId } ) ,
0 commit comments