@@ -44,11 +44,12 @@ import { buildChatHistoryFromEvents, stripReminders } from '../common/copilotCLI
4444import { ICustomSessionTitleService } from '../common/customSessionTitleService' ;
4545import { IChatDelegationSummaryService } from '../common/delegationSummaryService' ;
4646import { getCopilotCLISessionDir , getCopilotCLISessionEventsFile , getCopilotCLIWorkspaceFile } from './cliHelpers' ;
47- import { CopilotCLISessionOptions , ICopilotCLIAgents , ICopilotCLISDK } from './copilotCli' ;
47+ import { ICopilotCLIAgents , ICopilotCLISDK } from './copilotCli' ;
4848import { CopilotCliBridgeSpanProcessor } from './copilotCliBridgeSpanProcessor' ;
4949import { CopilotCLISession , ICopilotCLISession } from './copilotcliSession' ;
5050import { ICopilotCLISkills } from './copilotCLISkills' ;
51- import { ICopilotCLIMCPHandler , McpServerMappings } from './mcpHandler' ;
51+ import { ICopilotCLIMCPHandler , McpServerMappings , remapCustomAgentTools } from './mcpHandler' ;
52+
5253
5354const COPILOT_CLI_WORKSPACE_JSON_FILE_KEY = 'github.copilot.cli.workspaceSessionFile' ;
5455
@@ -525,9 +526,9 @@ export class CopilotCLISessionService extends Disposable implements ICopilotCLIS
525526 const { mcpConfig : mcpServers , disposable : mcpGateway } = await this . mcpHandler . loadMcpConfig ( ) ;
526527 try {
527528 const copilotUrl = this . configurationService . getConfig ( ConfigKey . Shared . DebugOverrideProxyUrl ) || undefined ;
528- const options = await this . createSessionsOptions ( { model, workspaceInfo, mcpServers, agent, copilotUrl, sessionId, debugTargetSessionIds } ) ;
529+ const { agentName , sessionOptions } = await this . createSessionsOptions ( { model, workspaceInfo, mcpServers, agent, copilotUrl, sessionId, debugTargetSessionIds, mcpServerMappings } ) ;
529530 const sessionManager = await raceCancellationError ( this . getSessionManager ( ) , token ) ;
530- const sdkSession = await sessionManager . createSession ( { ...options . toSessionOptions ( mcpServerMappings ) , sessionId } ) ;
531+ const sdkSession = await sessionManager . createSession ( { ...sessionOptions , sessionId } ) ;
531532 this . _newSessionIds . delete ( sdkSession . sessionId ) ;
532533 // After the first session creation, the SDK's OTel TracerProvider is
533534 // initialized. Install the bridge processor so SDK-native spans flow
@@ -548,7 +549,7 @@ export class CopilotCLISessionService extends Disposable implements ICopilotCLIS
548549 }
549550 this . logService . trace ( `[CopilotCLISession] Created new CopilotCLI session ${ sdkSession . sessionId } .` ) ;
550551
551- const session = await this . createCopilotSession ( sdkSession , options , sessionManager ) ;
552+ const session = await this . createCopilotSession ( sdkSession , workspaceInfo , agentName , sessionManager ) ;
552553 session . object . add ( mcpGateway ) ;
553554 return session ;
554555 }
@@ -638,14 +639,59 @@ export class CopilotCLISessionService extends Disposable implements ICopilotCLIS
638639 return false ;
639640 }
640641
641- protected async createSessionsOptions ( options : { model ?: string ; workspaceInfo : IWorkspaceInfo ; mcpServers ?: SessionOptions [ 'mcpServers' ] ; agent : SweCustomAgent | undefined ; copilotUrl ?: string ; sessionId ?: string ; debugTargetSessionIds ?: readonly string [ ] } , readonly ?: boolean ) : Promise < CopilotCLISessionOptions > {
642+ protected async createSessionsOptions ( options : { model ?: string ; workspaceInfo : IWorkspaceInfo ; mcpServers ?: SessionOptions [ 'mcpServers' ] ; agent : SweCustomAgent | undefined ; copilotUrl ?: string ; sessionId ?: string ; debugTargetSessionIds ?: readonly string [ ] ; mcpServerMappings ?: McpServerMappings } , readonly ?: boolean ) : Promise < { readonly sessionOptions : Readonly < SessionOptions > ; readonly agentName : string | undefined } > {
642643 const [ customAgents , skillLocations ] = await Promise . all ( [
643644 this . agents . getAgents ( ) ,
644- readonly ? Promise . resolve ( [ ] ) : this . copilotCLISkills . getSkillsLocations ( ) ,
645+ readonly ? Promise . resolve < Uri [ ] > ( [ ] ) : this . copilotCLISkills . getSkillsLocations ( ) ,
645646 ] ) ;
646647 const variablesContext = this . _promptVariablesService . buildTemplateVariablesContext ( options . sessionId , options . debugTargetSessionIds ) ;
647648 const systemMessage = variablesContext ? { mode : 'append' as const , content : variablesContext } : undefined ;
648- return new CopilotCLISessionOptions ( { ...options , customAgents, skillLocations, systemMessage } , this . logService ) ;
649+
650+ const allOptions : SessionOptions = {
651+ clientName : 'vscode' ,
652+ } ;
653+
654+ const workingDirectory = getWorkingDirectory ( options . workspaceInfo ) ;
655+ if ( workingDirectory ) {
656+ allOptions . workingDirectory = workingDirectory . fsPath ;
657+ }
658+ if ( options . model ) {
659+ allOptions . model = options . model as unknown as SessionOptions [ 'model' ] ;
660+ }
661+ if ( options . mcpServers && Object . keys ( options . mcpServers ) . length > 0 ) {
662+ allOptions . mcpServers = options . mcpServers ;
663+ this . logService . info ( `[CopilotCLISession] Passing ${ Object . keys ( options . mcpServers ) . length } MCP server(s) to SDK: [${ Object . keys ( options . mcpServers ) . join ( ', ' ) } ]` ) ;
664+ for ( const [ id , cfg ] of Object . entries ( options . mcpServers ) ) {
665+ this . logService . info ( `[CopilotCLISession] ${ id } : type=${ cfg . type } ` ) ;
666+ }
667+ } else {
668+ this . logService . info ( '[CopilotCLISession] No MCP servers to pass to SDK' ) ;
669+ }
670+ if ( skillLocations . length > 0 ) {
671+ allOptions . skillDirectories = skillLocations . map ( uri => uri . fsPath ) ;
672+ }
673+ if ( options . mcpServerMappings ?. size && customAgents && options . mcpServers ) {
674+ remapCustomAgentTools ( customAgents , options . mcpServerMappings , options . mcpServers , options . agent ) ;
675+ }
676+ if ( options . agent ) {
677+ allOptions . selectedCustomAgent = options . agent ;
678+ }
679+ if ( customAgents . length > 0 ) {
680+ allOptions . customAgents = customAgents ;
681+ }
682+ allOptions . enableStreaming = true ;
683+ if ( options . copilotUrl ) {
684+ allOptions . copilotUrl = options . copilotUrl ;
685+ }
686+ if ( systemMessage ) {
687+ allOptions . systemMessage = systemMessage ;
688+ }
689+ allOptions . sessionCapabilities = new Set ( [ 'plan-mode' , 'memory' , 'cli-documentation' , 'ask-user' , 'interactive-mode' , 'system-notifications' ] ) ;
690+
691+ return {
692+ sessionOptions : allOptions as Readonly < SessionOptions > ,
693+ agentName : options . agent ?. name ,
694+ } ;
649695 }
650696
651697 public async getSession ( { sessionId, model, workspaceInfo, readonly, agent, debugTargetSessionIds, mcpServerMappings } : { sessionId : string ; model ?: string ; workspaceInfo : IWorkspaceInfo ; readonly : boolean ; agent ?: SweCustomAgent ; debugTargetSessionIds ?: readonly string [ ] ; mcpServerMappings ?: McpServerMappings } , token : CancellationToken ) : Promise < RefCountedSession | undefined > {
@@ -677,15 +723,15 @@ export class CopilotCLISessionService extends Disposable implements ICopilotCLIS
677723 ] ) ;
678724 try {
679725 const copilotUrl = this . configurationService . getConfig ( ConfigKey . Shared . DebugOverrideProxyUrl ) || undefined ;
680- const options = await this . createSessionsOptions ( { model, agent, workspaceInfo, mcpServers, copilotUrl, sessionId, debugTargetSessionIds } , readonly ) ;
726+ const { agentName , sessionOptions } = await this . createSessionsOptions ( { model, agent, workspaceInfo, mcpServers, copilotUrl, sessionId, debugTargetSessionIds, mcpServerMappings } , readonly ) ;
681727
682- const sdkSession = await sessionManager . getSession ( { ...options . toSessionOptions ( mcpServerMappings ) , sessionId } , ! readonly ) ;
728+ const sdkSession = await sessionManager . getSession ( { ...sessionOptions , sessionId } , ! readonly ) ;
683729 if ( ! sdkSession ) {
684730 this . logService . error ( `[CopilotCLISession] CopilotCLI failed to get session ${ sessionId } .` ) ;
685731 return undefined ;
686732 }
687733
688- const session = await this . createCopilotSession ( sdkSession , options , sessionManager , readonly ) ;
734+ const session = await this . createCopilotSession ( sdkSession , workspaceInfo , agentName , sessionManager , readonly ) ;
689735 session . object . add ( mcpGateway ) ;
690736 return session ;
691737 }
@@ -722,9 +768,9 @@ export class CopilotCLISessionService extends Disposable implements ICopilotCLIS
722768 ] ) ;
723769
724770 const copilotUrl = this . configurationService . getConfig ( ConfigKey . Shared . DebugOverrideProxyUrl ) || undefined ;
725- const options = await this . createSessionsOptions ( { workspaceInfo, mcpServers : undefined , copilotUrl, agent : undefined , sessionId : newSessionId } , false ) ;
771+ const { agentName , sessionOptions } = await this . createSessionsOptions ( { workspaceInfo, mcpServers : undefined , copilotUrl, agent : undefined , sessionId : newSessionId } , false ) ;
726772
727- const sdkSession = await sessionManager . getSession ( { ...options . toSessionOptions ( ) , sessionId : newSessionId } , false ) ;
773+ const sdkSession = await sessionManager . getSession ( { ...sessionOptions , sessionId : newSessionId } , false ) ;
728774 if ( ! sdkSession ) {
729775 this . logService . error ( `[CopilotCLISession] CopilotCLI failed to open forked session ${ newSessionId } .` ) ;
730776 throw new Error ( `Failed to fork session ${ sessionId } ` ) ;
@@ -736,7 +782,7 @@ export class CopilotCLISessionService extends Disposable implements ICopilotCLIS
736782 let events : ReturnType < typeof sdkSession . getEvents > = [ ] ;
737783 // Only if we have a request to truncate should we open and trucate.
738784 if ( requestId ) {
739- const session = this . createCopilotSession ( sdkSession , options , sessionManager , false , true ) ;
785+ const session = this . createCopilotSession ( sdkSession , workspaceInfo , agentName , sessionManager , false , true ) ;
740786 disposables . add ( session ) ;
741787 const history = await session . object . getChatHistory ( ) ;
742788 const requestToTruncateTo = history . find ( event => event instanceof ChatRequestTurn2 && event . id === requestId ) ;
@@ -856,8 +902,8 @@ export class CopilotCLISessionService extends Disposable implements ICopilotCLIS
856902 return firstUserMessage ;
857903 }
858904
859- private createCopilotSession ( sdkSession : Session , options : CopilotCLISessionOptions , sessionManager : internal . LocalSessionManager , readonly = false , nowait = false ) : RefCountedSession {
860- const session = this . instantiationService . createInstance ( CopilotCLISession , options , sdkSession ) ;
905+ private createCopilotSession ( sdkSession : Session , workspaceInfo : IWorkspaceInfo , agentName : string | undefined , sessionManager : internal . LocalSessionManager , readonly = false , nowait = false ) : RefCountedSession {
906+ const session = this . instantiationService . createInstance ( CopilotCLISession , workspaceInfo , agentName , sdkSession ) ;
861907 this . _debugFileLogger . startSession ( session . sessionId ) . catch ( err => {
862908 this . logService . error ( '[CopilotCLISession] Failed to start debug log session' , err ) ;
863909 } ) ;
0 commit comments