@@ -368,6 +368,7 @@ export class ChatController {
368368 private async processStopResponseMessage ( message : StopResponseMessage ) {
369369 const session = this . sessionStorage . getSession ( message . tabID )
370370 session . tokenSource . cancel ( )
371+ this . chatHistoryStorage . getTabHistory ( message . tabID ) . clearRecentHistory ( )
371372 }
372373
373374 private async processTriggerTabIDReceived ( message : TriggerTabIDReceived ) {
@@ -650,6 +651,8 @@ export class ChatController {
650651 const session = this . sessionStorage . getSession ( tabID )
651652 const toolUse = session . toolUse
652653 if ( ! toolUse || ! toolUse . input ) {
654+ // Turn off AgentLoop flag if there's no tool use
655+ this . sessionStorage . setAgentLoopInProgress ( tabID , false )
653656 return
654657 }
655658 session . setToolUse ( undefined )
@@ -723,7 +726,6 @@ export class ChatController {
723726 customization : getSelectedCustomization ( ) ,
724727 toolResults : toolResults ,
725728 origin : Origin . IDE ,
726- chatHistory : this . chatHistoryStorage . getTabHistory ( tabID ) . getHistory ( ) ,
727729 context : session . context ?? [ ] ,
728730 relevantTextDocuments : [ ] ,
729731 additionalContents : [ ] ,
@@ -899,10 +901,16 @@ export class ChatController {
899901 errorMessage = e . message
900902 }
901903
904+ // Turn off AgentLoop flag in case of exception
905+ if ( tabID ) {
906+ this . sessionStorage . setAgentLoopInProgress ( tabID , false )
907+ }
908+
902909 this . messenger . sendErrorMessage ( errorMessage , tabID , requestID )
903910 getLogger ( ) . error ( `error: ${ errorMessage } tabID: ${ tabID } requestID: ${ requestID } ` )
904911
905912 this . sessionStorage . deleteSession ( tabID )
913+ this . chatHistoryStorage . getTabHistory ( tabID ) . clearRecentHistory ( )
906914 }
907915
908916 private async processContextMenuCommand ( command : EditorContextCommand ) {
@@ -1062,7 +1070,6 @@ export class ChatController {
10621070 codeQuery : lastTriggerEvent . context ?. focusAreaContext ?. names ,
10631071 userIntent : message . userIntent ,
10641072 customization : getSelectedCustomization ( ) ,
1065- chatHistory : this . chatHistoryStorage . getTabHistory ( message . tabID ) . getHistory ( ) ,
10661073 contextLengths : {
10671074 ...defaultContextLengths ,
10681075 } ,
@@ -1111,7 +1118,6 @@ export class ChatController {
11111118 codeQuery : context ?. focusAreaContext ?. names ,
11121119 userIntent : undefined ,
11131120 customization : getSelectedCustomization ( ) ,
1114- chatHistory : this . chatHistoryStorage . getTabHistory ( message . tabID ) . getHistory ( ) ,
11151121 origin : Origin . IDE ,
11161122 context : message . context ?? [ ] ,
11171123 relevantTextDocuments : [ ] ,
@@ -1293,6 +1299,16 @@ export class ChatController {
12931299 }
12941300
12951301 const tabID = triggerEvent . tabID
1302+ if ( this . sessionStorage . isAgentLoopInProgress ( tabID ) ) {
1303+ // If a response is already in progress, stop it first
1304+ const stopResponseMessage : StopResponseMessage = {
1305+ tabID : tabID ,
1306+ }
1307+ await this . processStopResponseMessage ( stopResponseMessage )
1308+ }
1309+
1310+ // Ensure AgentLoop flag is set to true during response generation
1311+ this . sessionStorage . setAgentLoopInProgress ( tabID , true )
12961312
12971313 const credentialsState = await AuthUtil . instance . getChatAuthState ( )
12981314
@@ -1355,6 +1371,7 @@ export class ChatController {
13551371 if ( fixedHistoryMessage . userInputMessage ?. userInputMessageContext ) {
13561372 triggerPayload . toolResults = fixedHistoryMessage . userInputMessage . userInputMessageContext . toolResults
13571373 }
1374+ triggerPayload . chatHistory = chatHistory . getHistory ( )
13581375 const request = triggerPayloadToChatRequest ( triggerPayload )
13591376 const conversationId = chatHistory . getConversationId ( ) || randomUUID ( )
13601377 chatHistory . setConversationId ( conversationId )
@@ -1409,16 +1426,23 @@ export class ChatController {
14091426 }
14101427 this . telemetryHelper . recordEnterFocusConversation ( triggerEvent . tabID )
14111428 this . telemetryHelper . recordStartConversation ( triggerEvent , triggerPayload )
1412- chatHistory . appendUserMessage ( fixedHistoryMessage )
1429+ if ( request . conversationState . currentMessage ) {
1430+ chatHistory . appendUserMessage ( request . conversationState . currentMessage )
1431+ }
14131432
14141433 getLogger ( ) . info (
14151434 `response to tab: ${ tabID } conversationID: ${ session . sessionIdentifier } requestID: ${
14161435 response . $metadata . requestId
14171436 } metadata: ${ inspect ( response . $metadata , { depth : 12 } ) } `
14181437 )
14191438 await this . messenger . sendAIResponse ( response , session , tabID , triggerID , triggerPayload , chatHistory )
1439+
1440+ // Turn off AgentLoop flag after sending the AI response
1441+ this . sessionStorage . setAgentLoopInProgress ( tabID , false )
14201442 } catch ( e : any ) {
14211443 this . telemetryHelper . recordMessageResponseError ( triggerPayload , tabID , getHttpStatusCode ( e ) ?? 0 )
1444+ // Turn off AgentLoop flag in case of exception
1445+ this . sessionStorage . setAgentLoopInProgress ( tabID , false )
14221446 // clears session, record telemetry before this call
14231447 this . processException ( e , tabID )
14241448 }
0 commit comments