@@ -87,11 +87,11 @@ import {
8787 defaultContextLengths ,
8888} from '../../constants'
8989import { ChatSession } from '../../clients/chat/v0/chat'
90- import { ChatHistoryManager } from '../../storages/chatHistory'
9190import { amazonQTabSuffix } from '../../../shared/constants'
9291import { OutputKind } from '../../tools/toolShared'
9392import { ToolUtils , Tool , ToolType } from '../../tools/toolUtils'
9493import { ChatStream } from '../../tools/chatStream'
94+ import { ChatHistoryStorage } from '../../storages/chatHistoryStorage'
9595import { FsWrite , FsWriteParams } from '../../tools/fsWrite'
9696import { tempDirPath } from '../../../shared/filesystemUtilities'
9797
@@ -155,7 +155,7 @@ export class ChatController {
155155 private readonly userIntentRecognizer : UserIntentRecognizer
156156 private readonly telemetryHelper : CWCTelemetryHelper
157157 private userPromptsWatcher : vscode . FileSystemWatcher | undefined
158- private readonly chatHistoryManager : ChatHistoryManager
158+ private readonly chatHistoryStorage : ChatHistoryStorage
159159
160160 public constructor (
161161 private readonly chatControllerMessageListeners : ChatControllerMessageListeners ,
@@ -173,7 +173,7 @@ export class ChatController {
173173 this . editorContentController = new EditorContentController ( )
174174 this . promptGenerator = new PromptsGenerator ( )
175175 this . userIntentRecognizer = new UserIntentRecognizer ( )
176- this . chatHistoryManager = new ChatHistoryManager ( )
176+ this . chatHistoryStorage = new ChatHistoryStorage ( )
177177
178178 onDidChangeAmazonQVisibility ( ( visible ) => {
179179 if ( visible ) {
@@ -424,7 +424,7 @@ export class ChatController {
424424
425425 private async processTabCloseMessage ( message : TabClosedMessage ) {
426426 this . sessionStorage . deleteSession ( message . tabID )
427- this . chatHistoryManager . clear ( )
427+ this . chatHistoryStorage . deleteHistory ( message . tabID )
428428 this . triggerEventsStorage . removeTabEvents ( message . tabID )
429429 // this.telemetryHelper.recordCloseChat(message.tabID)
430430 }
@@ -710,7 +710,7 @@ export class ChatController {
710710 customization : getSelectedCustomization ( ) ,
711711 toolResults : toolResults ,
712712 origin : Origin . IDE ,
713- chatHistory : this . chatHistoryManager . getHistory ( ) ,
713+ chatHistory : this . chatHistoryStorage . getHistory ( tabID ) . getHistory ( ) ,
714714 context : session . context ?? [ ] ,
715715 relevantTextDocuments : [ ] ,
716716 additionalContents : [ ] ,
@@ -890,7 +890,6 @@ export class ChatController {
890890 getLogger ( ) . error ( `error: ${ errorMessage } tabID: ${ tabID } requestID: ${ requestID } ` )
891891
892892 this . sessionStorage . deleteSession ( tabID )
893- this . chatHistoryManager . clear ( )
894893 }
895894
896895 private async processContextMenuCommand ( command : EditorContextCommand ) {
@@ -964,7 +963,6 @@ export class ChatController {
964963 codeQuery : context ?. focusAreaContext ?. names ,
965964 userIntent : this . userIntentRecognizer . getFromContextMenuCommand ( command ) ,
966965 customization : getSelectedCustomization ( ) ,
967- chatHistory : this . chatHistoryManager . getHistory ( ) ,
968966 additionalContents : [ ] ,
969967 relevantTextDocuments : [ ] ,
970968 documentReferences : [ ] ,
@@ -1012,7 +1010,7 @@ export class ChatController {
10121010 switch ( message . command ) {
10131011 case 'clear' :
10141012 this . sessionStorage . deleteSession ( message . tabID )
1015- this . chatHistoryManager . clear ( )
1013+ this . chatHistoryStorage . getHistory ( message . tabID ) . clear ( )
10161014 this . triggerEventsStorage . removeTabEvents ( message . tabID )
10171015 recordTelemetryChatRunCommand ( 'clear' )
10181016 return
@@ -1051,7 +1049,7 @@ export class ChatController {
10511049 codeQuery : lastTriggerEvent . context ?. focusAreaContext ?. names ,
10521050 userIntent : message . userIntent ,
10531051 customization : getSelectedCustomization ( ) ,
1054- chatHistory : this . chatHistoryManager . getHistory ( ) ,
1052+ chatHistory : this . chatHistoryStorage . getHistory ( message . tabID ) . getHistory ( ) ,
10551053 contextLengths : {
10561054 ...defaultContextLengths ,
10571055 } ,
@@ -1100,7 +1098,7 @@ export class ChatController {
11001098 codeQuery : context ?. focusAreaContext ?. names ,
11011099 userIntent : this . userIntentRecognizer . getFromPromptChatMessage ( message ) ,
11021100 customization : getSelectedCustomization ( ) ,
1103- chatHistory : this . chatHistoryManager . getHistory ( ) ,
1101+ chatHistory : this . chatHistoryStorage . getHistory ( message . tabID ) . getHistory ( ) ,
11041102 origin : Origin . IDE ,
11051103 context : message . context ?? [ ] ,
11061104 relevantTextDocuments : [ ] ,
@@ -1327,16 +1325,28 @@ export class ChatController {
13271325
13281326 triggerPayload . contextLengths . userInputContextLength = triggerPayload . message . length
13291327 triggerPayload . contextLengths . focusFileContextLength = triggerPayload . fileText . length
1330- const request = triggerPayloadToChatRequest ( triggerPayload )
1331- if (
1332- this . chatHistoryManager . getConversationId ( ) !== undefined &&
1333- this . chatHistoryManager . getConversationId ( ) !== ''
1334- ) {
1335- request . conversationState . conversationId = this . chatHistoryManager . getConversationId ( )
1336- } else {
1337- this . chatHistoryManager . setConversationId ( randomUUID ( ) )
1338- request . conversationState . conversationId = this . chatHistoryManager . getConversationId ( )
1328+
1329+ const chatHistory = this . chatHistoryStorage . getHistory ( tabID )
1330+ const newUserMessage = {
1331+ userInputMessage : {
1332+ content : triggerPayload . message ,
1333+ userIntent : triggerPayload . userIntent ,
1334+ ...( triggerPayload . origin && { origin : triggerPayload . origin } ) ,
1335+ userInputMessageContext : {
1336+ tools : tools ,
1337+ ...( triggerPayload . toolResults && { toolResults : triggerPayload . toolResults } ) ,
1338+ } ,
1339+ } ,
1340+ }
1341+ const fixedHistoryMessage = chatHistory . fixHistory ( newUserMessage )
1342+ if ( fixedHistoryMessage . userInputMessage ?. userInputMessageContext ) {
1343+ triggerPayload . toolResults = fixedHistoryMessage . userInputMessage . userInputMessageContext . toolResults
13391344 }
1345+ const request = triggerPayloadToChatRequest ( triggerPayload )
1346+ const conversationId = chatHistory . getConversationId ( ) || randomUUID ( )
1347+ chatHistory . setConversationId ( conversationId )
1348+ request . conversationState . conversationId = conversationId
1349+
13401350 triggerPayload . documentReferences = this . mergeRelevantTextDocuments ( triggerPayload . relevantTextDocuments )
13411351
13421352 // Update context transparency after it's truncated dynamically to show users only the context sent.
@@ -1386,32 +1396,14 @@ export class ChatController {
13861396 }
13871397 this . telemetryHelper . recordEnterFocusConversation ( triggerEvent . tabID )
13881398 this . telemetryHelper . recordStartConversation ( triggerEvent , triggerPayload )
1389-
1390- this . chatHistoryManager . appendUserMessage ( {
1391- userInputMessage : {
1392- content : triggerPayload . message ,
1393- userIntent : triggerPayload . userIntent ,
1394- ...( triggerPayload . origin && { origin : triggerPayload . origin } ) ,
1395- userInputMessageContext : {
1396- tools : tools ,
1397- ...( triggerPayload . toolResults && { toolResults : triggerPayload . toolResults } ) ,
1398- } ,
1399- } ,
1400- } )
1399+ chatHistory . appendUserMessage ( fixedHistoryMessage )
14011400
14021401 getLogger ( ) . info (
14031402 `response to tab: ${ tabID } conversationID: ${ session . sessionIdentifier } requestID: ${
14041403 response . $metadata . requestId
14051404 } metadata: ${ inspect ( response . $metadata , { depth : 12 } ) } `
14061405 )
1407- await this . messenger . sendAIResponse (
1408- response ,
1409- session ,
1410- tabID ,
1411- triggerID ,
1412- triggerPayload ,
1413- this . chatHistoryManager
1414- )
1406+ await this . messenger . sendAIResponse ( response , session , tabID , triggerID , triggerPayload , chatHistory )
14151407 } catch ( e : any ) {
14161408 this . telemetryHelper . recordMessageResponseError ( triggerPayload , tabID , getHttpStatusCode ( e ) ?? 0 )
14171409 // clears session, record telemetry before this call
0 commit comments