@@ -87,11 +87,11 @@ import {
87
87
defaultContextLengths ,
88
88
} from '../../constants'
89
89
import { ChatSession } from '../../clients/chat/v0/chat'
90
- import { ChatHistoryManager } from '../../storages/chatHistory'
91
90
import { amazonQTabSuffix } from '../../../shared/constants'
92
91
import { OutputKind } from '../../tools/toolShared'
93
92
import { ToolUtils , Tool , ToolType } from '../../tools/toolUtils'
94
93
import { ChatStream } from '../../tools/chatStream'
94
+ import { ChatHistoryStorage } from '../../storages/chatHistoryStorage'
95
95
import { FsWrite , FsWriteParams } from '../../tools/fsWrite'
96
96
import { tempDirPath } from '../../../shared/filesystemUtilities'
97
97
@@ -155,7 +155,7 @@ export class ChatController {
155
155
private readonly userIntentRecognizer : UserIntentRecognizer
156
156
private readonly telemetryHelper : CWCTelemetryHelper
157
157
private userPromptsWatcher : vscode . FileSystemWatcher | undefined
158
- private readonly chatHistoryManager : ChatHistoryManager
158
+ private readonly chatHistoryStorage : ChatHistoryStorage
159
159
160
160
public constructor (
161
161
private readonly chatControllerMessageListeners : ChatControllerMessageListeners ,
@@ -173,7 +173,7 @@ export class ChatController {
173
173
this . editorContentController = new EditorContentController ( )
174
174
this . promptGenerator = new PromptsGenerator ( )
175
175
this . userIntentRecognizer = new UserIntentRecognizer ( )
176
- this . chatHistoryManager = new ChatHistoryManager ( )
176
+ this . chatHistoryStorage = new ChatHistoryStorage ( )
177
177
178
178
onDidChangeAmazonQVisibility ( ( visible ) => {
179
179
if ( visible ) {
@@ -424,7 +424,7 @@ export class ChatController {
424
424
425
425
private async processTabCloseMessage ( message : TabClosedMessage ) {
426
426
this . sessionStorage . deleteSession ( message . tabID )
427
- this . chatHistoryManager . clear ( )
427
+ this . chatHistoryStorage . deleteHistory ( message . tabID )
428
428
this . triggerEventsStorage . removeTabEvents ( message . tabID )
429
429
// this.telemetryHelper.recordCloseChat(message.tabID)
430
430
}
@@ -711,7 +711,7 @@ export class ChatController {
711
711
customization : getSelectedCustomization ( ) ,
712
712
toolResults : toolResults ,
713
713
origin : Origin . IDE ,
714
- chatHistory : this . chatHistoryManager . getHistory ( ) ,
714
+ chatHistory : this . chatHistoryStorage . getTabHistory ( tabID ) . getHistory ( ) ,
715
715
context : session . context ?? [ ] ,
716
716
relevantTextDocuments : [ ] ,
717
717
additionalContents : [ ] ,
@@ -891,7 +891,6 @@ export class ChatController {
891
891
getLogger ( ) . error ( `error: ${ errorMessage } tabID: ${ tabID } requestID: ${ requestID } ` )
892
892
893
893
this . sessionStorage . deleteSession ( tabID )
894
- this . chatHistoryManager . clear ( )
895
894
}
896
895
897
896
private async processContextMenuCommand ( command : EditorContextCommand ) {
@@ -965,7 +964,6 @@ export class ChatController {
965
964
codeQuery : context ?. focusAreaContext ?. names ,
966
965
userIntent : this . userIntentRecognizer . getFromContextMenuCommand ( command ) ,
967
966
customization : getSelectedCustomization ( ) ,
968
- chatHistory : this . chatHistoryManager . getHistory ( ) ,
969
967
additionalContents : [ ] ,
970
968
relevantTextDocuments : [ ] ,
971
969
documentReferences : [ ] ,
@@ -1013,7 +1011,7 @@ export class ChatController {
1013
1011
switch ( message . command ) {
1014
1012
case 'clear' :
1015
1013
this . sessionStorage . deleteSession ( message . tabID )
1016
- this . chatHistoryManager . clear ( )
1014
+ this . chatHistoryStorage . getTabHistory ( message . tabID ) . clear ( )
1017
1015
this . triggerEventsStorage . removeTabEvents ( message . tabID )
1018
1016
recordTelemetryChatRunCommand ( 'clear' )
1019
1017
return
@@ -1052,7 +1050,7 @@ export class ChatController {
1052
1050
codeQuery : lastTriggerEvent . context ?. focusAreaContext ?. names ,
1053
1051
userIntent : message . userIntent ,
1054
1052
customization : getSelectedCustomization ( ) ,
1055
- chatHistory : this . chatHistoryManager . getHistory ( ) ,
1053
+ chatHistory : this . chatHistoryStorage . getTabHistory ( message . tabID ) . getHistory ( ) ,
1056
1054
contextLengths : {
1057
1055
...defaultContextLengths ,
1058
1056
} ,
@@ -1101,7 +1099,7 @@ export class ChatController {
1101
1099
codeQuery : context ?. focusAreaContext ?. names ,
1102
1100
userIntent : this . userIntentRecognizer . getFromPromptChatMessage ( message ) ,
1103
1101
customization : getSelectedCustomization ( ) ,
1104
- chatHistory : this . chatHistoryManager . getHistory ( ) ,
1102
+ chatHistory : this . chatHistoryStorage . getTabHistory ( message . tabID ) . getHistory ( ) ,
1105
1103
origin : Origin . IDE ,
1106
1104
context : message . context ?? [ ] ,
1107
1105
relevantTextDocuments : [ ] ,
@@ -1328,16 +1326,28 @@ export class ChatController {
1328
1326
1329
1327
triggerPayload . contextLengths . userInputContextLength = triggerPayload . message . length
1330
1328
triggerPayload . contextLengths . focusFileContextLength = triggerPayload . fileText . length
1331
- const request = triggerPayloadToChatRequest ( triggerPayload )
1332
- if (
1333
- this . chatHistoryManager . getConversationId ( ) !== undefined &&
1334
- this . chatHistoryManager . getConversationId ( ) !== ''
1335
- ) {
1336
- request . conversationState . conversationId = this . chatHistoryManager . getConversationId ( )
1337
- } else {
1338
- this . chatHistoryManager . setConversationId ( randomUUID ( ) )
1339
- request . conversationState . conversationId = this . chatHistoryManager . getConversationId ( )
1329
+
1330
+ const chatHistory = this . chatHistoryStorage . getTabHistory ( tabID )
1331
+ const newUserMessage = {
1332
+ userInputMessage : {
1333
+ content : triggerPayload . message ,
1334
+ userIntent : triggerPayload . userIntent ,
1335
+ ...( triggerPayload . origin && { origin : triggerPayload . origin } ) ,
1336
+ userInputMessageContext : {
1337
+ tools : tools ,
1338
+ ...( triggerPayload . toolResults && { toolResults : triggerPayload . toolResults } ) ,
1339
+ } ,
1340
+ } ,
1341
+ }
1342
+ const fixedHistoryMessage = chatHistory . fixHistory ( newUserMessage )
1343
+ if ( fixedHistoryMessage . userInputMessage ?. userInputMessageContext ) {
1344
+ triggerPayload . toolResults = fixedHistoryMessage . userInputMessage . userInputMessageContext . toolResults
1340
1345
}
1346
+ const request = triggerPayloadToChatRequest ( triggerPayload )
1347
+ const conversationId = chatHistory . getConversationId ( ) || randomUUID ( )
1348
+ chatHistory . setConversationId ( conversationId )
1349
+ request . conversationState . conversationId = conversationId
1350
+
1341
1351
triggerPayload . documentReferences = this . mergeRelevantTextDocuments ( triggerPayload . relevantTextDocuments )
1342
1352
1343
1353
// Update context transparency after it's truncated dynamically to show users only the context sent.
@@ -1387,32 +1397,14 @@ export class ChatController {
1387
1397
}
1388
1398
this . telemetryHelper . recordEnterFocusConversation ( triggerEvent . tabID )
1389
1399
this . telemetryHelper . recordStartConversation ( triggerEvent , triggerPayload )
1390
-
1391
- this . chatHistoryManager . appendUserMessage ( {
1392
- userInputMessage : {
1393
- content : triggerPayload . message ,
1394
- userIntent : triggerPayload . userIntent ,
1395
- ...( triggerPayload . origin && { origin : triggerPayload . origin } ) ,
1396
- userInputMessageContext : {
1397
- tools : tools ,
1398
- ...( triggerPayload . toolResults && { toolResults : triggerPayload . toolResults } ) ,
1399
- } ,
1400
- } ,
1401
- } )
1400
+ chatHistory . appendUserMessage ( fixedHistoryMessage )
1402
1401
1403
1402
getLogger ( ) . info (
1404
1403
`response to tab: ${ tabID } conversationID: ${ session . sessionIdentifier } requestID: ${
1405
1404
response . $metadata . requestId
1406
1405
} metadata: ${ inspect ( response . $metadata , { depth : 12 } ) } `
1407
1406
)
1408
- await this . messenger . sendAIResponse (
1409
- response ,
1410
- session ,
1411
- tabID ,
1412
- triggerID ,
1413
- triggerPayload ,
1414
- this . chatHistoryManager
1415
- )
1407
+ await this . messenger . sendAIResponse ( response , session , tabID , triggerID , triggerPayload , chatHistory )
1416
1408
} catch ( e : any ) {
1417
1409
this . telemetryHelper . recordMessageResponseError ( triggerPayload , tabID , getHttpStatusCode ( e ) ?? 0 )
1418
1410
// clears session, record telemetry before this call
0 commit comments