66import type { Attachment , SessionOptions , SweCustomAgent } from '@github/copilot/sdk' ;
77import * as l10n from '@vscode/l10n' ;
88import * as vscode from 'vscode' ;
9- import { ChatExtendedRequestHandler , ChatSessionProviderOptionItem , Uri } from 'vscode' ;
9+ import { ChatExtendedRequestHandler , ChatRequestTurn2 , ChatSessionProviderOptionItem , Uri } from 'vscode' ;
1010import { IRunCommandExecutionService } from '../../../platform/commands/common/runCommandExecutionService' ;
1111import { ConfigKey , IConfigurationService } from '../../../platform/configuration/common/configurationService' ;
1212import { INativeEnvService } from '../../../platform/env/common/envService' ;
@@ -33,6 +33,7 @@ import { StopWatch } from '../../../util/vs/base/common/stopwatch';
3333import { URI } from '../../../util/vs/base/common/uri' ;
3434import { EXTENSION_ID } from '../../common/constants' ;
3535import { ChatVariablesCollection , extractDebugTargetSessionIds , isPromptFile } from '../../prompt/common/chatVariablesCollection' ;
36+ import { GitBranchNameGenerator } from '../../prompt/node/gitBranch' ;
3637import { IToolsService } from '../../tools/common/toolsService' ;
3738import { IChatSessionMetadataStore , RepositoryProperties , StoredModeInstructions } from '../common/chatSessionMetadataStore' ;
3839import { IChatSessionWorkspaceFolderService } from '../common/chatSessionWorkspaceFolderService' ;
@@ -1097,6 +1098,7 @@ export class CopilotCLIChatSessionParticipant extends Disposable {
10971098 private readonly promptResolver : CopilotCLIPromptResolver ,
10981099 private readonly sessionItemProvider : CopilotCLIChatSessionItemProvider ,
10991100 private readonly cloudSessionProvider : CopilotCloudSessionsProvider | undefined ,
1101+ private readonly branchNameGenerator : GitBranchNameGenerator | undefined ,
11001102 @IGitService private readonly gitService : IGitService ,
11011103 @ICopilotCLIModels private readonly copilotCLIModels : ICopilotCLIModels ,
11021104 @ICopilotCLIAgents private readonly copilotCLIAgents : ICopilotCLIAgents ,
@@ -1311,7 +1313,14 @@ export class CopilotCLIChatSessionParticipant extends Disposable {
13111313 this . getAgent ( id , request , token ) ,
13121314 ] ) ;
13131315
1314- const sessionResult = await this . getOrCreateSession ( request , chatSessionContext , stream , { model, agent } , disposables , token ) ;
1316+ const requestTurn = new ChatRequestTurn2 ( request . prompt ?? '' , request . command , [ ] , '' , [ ] , [ ] , undefined , undefined , undefined ) ;
1317+ const fakeContext : vscode . ChatContext = {
1318+ history : [ requestTurn ] ,
1319+ yieldRequested : false ,
1320+ } ;
1321+ const newBranch = ( isUntitled && request . prompt && this . branchNameGenerator ) ? this . branchNameGenerator . generateBranchName ( fakeContext , token ) : undefined ;
1322+
1323+ const sessionResult = await this . getOrCreateSession ( request , chatSessionContext , stream , { model, agent, newBranch } , disposables , token ) ;
13151324 const session = sessionResult . session ;
13161325 if ( session ) {
13171326 disposables . add ( session ) ;
@@ -1675,13 +1684,13 @@ export class CopilotCLIChatSessionParticipant extends Disposable {
16751684 }
16761685 }
16771686
1678- private async getOrCreateSession ( request : vscode . ChatRequest , chatSessionContext : vscode . ChatSessionContext , stream : vscode . ChatResponseStream , options : { model : { model : string ; reasoningEffort ?: string } | undefined ; agent : SweCustomAgent | undefined } , disposables : DisposableStore , token : vscode . CancellationToken ) : Promise < { session : IReference < ICopilotCLISession > | undefined ; trusted : boolean } > {
1687+ private async getOrCreateSession ( request : vscode . ChatRequest , chatSessionContext : vscode . ChatSessionContext , stream : vscode . ChatResponseStream , options : { model : { model : string ; reasoningEffort ?: string } | undefined ; agent : SweCustomAgent | undefined ; newBranch ?: Promise < string | undefined > } , disposables : DisposableStore , token : vscode . CancellationToken ) : Promise < { session : IReference < ICopilotCLISession > | undefined ; trusted : boolean } > {
16791688 const { resource } = chatSessionContext . chatSessionItem ;
16801689 const existingSessionId = this . sessionItemProvider . untitledSessionIdMapping . get ( SessionIdForCLI . parse ( resource ) ) ;
16811690 const id = existingSessionId ?? SessionIdForCLI . parse ( resource ) ;
16821691 const isNewSession = chatSessionContext . isUntitled && ! existingSessionId ;
16831692
1684- const { workspaceInfo, cancelled, trusted } = await this . getOrInitializeWorkingDirectory ( chatSessionContext , stream , request . toolInvocationToken , token ) ;
1693+ const { workspaceInfo, cancelled, trusted } = await this . getOrInitializeWorkingDirectory ( chatSessionContext , stream , request . toolInvocationToken , token , options . newBranch ) ;
16851694 const workingDirectory = getWorkingDirectory ( workspaceInfo ) ;
16861695 const worktreeProperties = workspaceInfo . worktreeProperties ;
16871696 if ( cancelled || token . isCancellationRequested ) {
@@ -1772,7 +1781,8 @@ export class CopilotCLIChatSessionParticipant extends Disposable {
17721781 chatSessionContext : vscode . ChatSessionContext | undefined ,
17731782 stream : vscode . ChatResponseStream ,
17741783 toolInvocationToken : vscode . ChatParticipantToolToken ,
1775- token : vscode . CancellationToken
1784+ token : vscode . CancellationToken ,
1785+ newBranch ?: Promise < string | undefined >
17761786 ) : Promise < {
17771787 workspaceInfo : IWorkspaceInfo ;
17781788 cancelled : boolean ;
@@ -1788,7 +1798,7 @@ export class CopilotCLIChatSessionParticipant extends Disposable {
17881798 // Use FolderRepositoryManager to initialize folder/repository with worktree creation
17891799 const branch = _sessionBranch . get ( id ) ;
17901800 const isolation = _sessionIsolation . get ( id ) ?? undefined ;
1791- folderInfo = await this . folderRepositoryManager . initializeFolderRepository ( id , { stream, toolInvocationToken, branch : branch ?? undefined , isolation, folder : undefined } , token ) ;
1801+ folderInfo = await this . folderRepositoryManager . initializeFolderRepository ( id , { stream, toolInvocationToken, branch : branch ?? undefined , isolation, folder : undefined , newBranch } , token ) ;
17921802 } else {
17931803 // Existing session - use getFolderRepository for resolution with trust check
17941804 folderInfo = await this . folderRepositoryManager . getFolderRepository ( id , { promptForTrust : true , stream } , token ) ;
0 commit comments