@@ -375,6 +375,7 @@ export class ChatController {
375
375
private async processStopResponseMessage ( message : StopResponseMessage ) {
376
376
const session = this . sessionStorage . getSession ( message . tabID )
377
377
session . tokenSource . cancel ( )
378
+ this . chatHistoryStorage . getTabHistory ( message . tabID ) . clearRecentHistory ( )
378
379
}
379
380
380
381
private async processTriggerTabIDReceived ( message : TriggerTabIDReceived ) {
@@ -721,6 +722,8 @@ export class ChatController {
721
722
const session = this . sessionStorage . getSession ( tabID )
722
723
const toolUse = session . toolUse
723
724
if ( ! toolUse || ! toolUse . input ) {
725
+ // Turn off AgentLoop flag if there's no tool use
726
+ this . sessionStorage . setAgentLoopInProgress ( tabID , false )
724
727
return
725
728
}
726
729
session . setToolUse ( undefined )
@@ -794,7 +797,6 @@ export class ChatController {
794
797
customization : getSelectedCustomization ( ) ,
795
798
toolResults : toolResults ,
796
799
origin : Origin . IDE ,
797
- chatHistory : this . chatHistoryStorage . getTabHistory ( tabID ) . getHistory ( ) ,
798
800
context : session . context ?? [ ] ,
799
801
relevantTextDocuments : [ ] ,
800
802
additionalContents : [ ] ,
@@ -985,10 +987,16 @@ export class ChatController {
985
987
errorMessage = e . message
986
988
}
987
989
990
+ // Turn off AgentLoop flag in case of exception
991
+ if ( tabID ) {
992
+ this . sessionStorage . setAgentLoopInProgress ( tabID , false )
993
+ }
994
+
988
995
this . messenger . sendErrorMessage ( errorMessage , tabID , requestID )
989
996
getLogger ( ) . error ( `error: ${ errorMessage } tabID: ${ tabID } requestID: ${ requestID } ` )
990
997
991
998
this . sessionStorage . deleteSession ( tabID )
999
+ this . chatHistoryStorage . getTabHistory ( tabID ) . clearRecentHistory ( )
992
1000
}
993
1001
994
1002
private async processContextMenuCommand ( command : EditorContextCommand ) {
@@ -1148,7 +1156,6 @@ export class ChatController {
1148
1156
codeQuery : lastTriggerEvent . context ?. focusAreaContext ?. names ,
1149
1157
userIntent : message . userIntent ,
1150
1158
customization : getSelectedCustomization ( ) ,
1151
- chatHistory : this . chatHistoryStorage . getTabHistory ( message . tabID ) . getHistory ( ) ,
1152
1159
contextLengths : {
1153
1160
...defaultContextLengths ,
1154
1161
} ,
@@ -1197,7 +1204,6 @@ export class ChatController {
1197
1204
codeQuery : context ?. focusAreaContext ?. names ,
1198
1205
userIntent : undefined ,
1199
1206
customization : getSelectedCustomization ( ) ,
1200
- chatHistory : this . chatHistoryStorage . getTabHistory ( message . tabID ) . getHistory ( ) ,
1201
1207
origin : Origin . IDE ,
1202
1208
context : message . context ?? [ ] ,
1203
1209
relevantTextDocuments : [ ] ,
@@ -1379,6 +1385,16 @@ export class ChatController {
1379
1385
}
1380
1386
1381
1387
const tabID = triggerEvent . tabID
1388
+ if ( this . sessionStorage . isAgentLoopInProgress ( tabID ) ) {
1389
+ // If a response is already in progress, stop it first
1390
+ const stopResponseMessage : StopResponseMessage = {
1391
+ tabID : tabID ,
1392
+ }
1393
+ await this . processStopResponseMessage ( stopResponseMessage )
1394
+ }
1395
+
1396
+ // Ensure AgentLoop flag is set to true during response generation
1397
+ this . sessionStorage . setAgentLoopInProgress ( tabID , true )
1382
1398
1383
1399
const credentialsState = await AuthUtil . instance . getChatAuthState ( )
1384
1400
@@ -1442,6 +1458,7 @@ export class ChatController {
1442
1458
if ( fixedHistoryMessage . userInputMessage ?. userInputMessageContext ) {
1443
1459
triggerPayload . toolResults = fixedHistoryMessage . userInputMessage . userInputMessageContext . toolResults
1444
1460
}
1461
+ triggerPayload . chatHistory = chatHistory . getHistory ( )
1445
1462
const request = triggerPayloadToChatRequest ( triggerPayload )
1446
1463
const conversationId = chatHistory . getConversationId ( ) || randomUUID ( )
1447
1464
chatHistory . setConversationId ( conversationId )
@@ -1496,16 +1513,23 @@ export class ChatController {
1496
1513
}
1497
1514
this . telemetryHelper . recordEnterFocusConversation ( triggerEvent . tabID )
1498
1515
this . telemetryHelper . recordStartConversation ( triggerEvent , triggerPayload )
1499
- chatHistory . appendUserMessage ( fixedHistoryMessage )
1516
+ if ( request . conversationState . currentMessage ) {
1517
+ chatHistory . appendUserMessage ( request . conversationState . currentMessage )
1518
+ }
1500
1519
1501
1520
getLogger ( ) . info (
1502
1521
`response to tab: ${ tabID } conversationID: ${ session . sessionIdentifier } requestID: ${
1503
1522
response . $metadata . requestId
1504
1523
} metadata: ${ inspect ( response . $metadata , { depth : 12 } ) } `
1505
1524
)
1506
1525
await this . messenger . sendAIResponse ( response , session , tabID , triggerID , triggerPayload , chatHistory )
1526
+
1527
+ // Turn off AgentLoop flag after sending the AI response
1528
+ this . sessionStorage . setAgentLoopInProgress ( tabID , false )
1507
1529
} catch ( e : any ) {
1508
1530
this . telemetryHelper . recordMessageResponseError ( triggerPayload , tabID , getHttpStatusCode ( e ) ?? 0 )
1531
+ // Turn off AgentLoop flag in case of exception
1532
+ this . sessionStorage . setAgentLoopInProgress ( tabID , false )
1509
1533
// clears session, record telemetry before this call
1510
1534
this . processException ( e , tabID )
1511
1535
}
0 commit comments