@@ -417,6 +417,21 @@ export class CopilotSession {
417417 args : string ;
418418 } ;
419419 void this . _executeCommandAndRespond ( requestId , commandName , command , args ) ;
420+ } else if ( ( event as { type : string } ) . type === "elicitation.requested" ) {
421+ // TODO: Remove type casts above once session-events codegen includes these event types
422+ if ( this . elicitationHandler ) {
423+ const data = ( event as { data : Record < string , unknown > } ) . data ;
424+ void this . _handleElicitationRequest (
425+ {
426+ message : data . message as string ,
427+ requestedSchema :
428+ data . requestedSchema as ElicitationRequest [ "requestedSchema" ] ,
429+ mode : data . mode as ElicitationRequest [ "mode" ] ,
430+ elicitationSource : data . elicitationSource as string | undefined ,
431+ } ,
432+ data . requestId as string
433+ ) ;
434+ }
420435 } else if ( ( event as { type : string } ) . type === "capabilities.changed" ) {
421436 const data = ( event as { data : Partial < SessionCapabilities > } ) . data ;
422437 this . _capabilities = { ...this . _capabilities , ...data } ;
@@ -598,17 +613,33 @@ export class CopilotSession {
598613 }
599614
600615 /**
601- * Handles an elicitation.request RPC callback from the server.
616+ * Handles an elicitation.requested broadcast event.
617+ * Invokes the registered handler and responds via handlePendingElicitation RPC.
602618 * @internal
603619 */
604- async _handleElicitationRequest (
605- request : ElicitationRequest ,
606- sessionId : string
607- ) : Promise < ElicitationResult > {
620+ async _handleElicitationRequest ( request : ElicitationRequest , requestId : string ) : Promise < void > {
608621 if ( ! this . elicitationHandler ) {
609- throw new Error ( "Elicitation requested but no handler registered" ) ;
622+ return ;
623+ }
624+ try {
625+ const result = await this . elicitationHandler ( request , { sessionId : this . sessionId } ) ;
626+ await this . connection . sendRequest ( "session.ui.handlePendingElicitation" , {
627+ sessionId : this . sessionId ,
628+ requestId,
629+ result,
630+ } ) ;
631+ } catch {
632+ // Handler failed — attempt to cancel so the request doesn't hang
633+ try {
634+ await this . connection . sendRequest ( "session.ui.handlePendingElicitation" , {
635+ sessionId : this . sessionId ,
636+ requestId,
637+ result : { action : "cancel" } ,
638+ } ) ;
639+ } catch {
640+ // Best effort — another client may have already responded
641+ }
610642 }
611- return await this . elicitationHandler ( request , { sessionId } ) ;
612643 }
613644
614645 /**
0 commit comments