@@ -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 } from '../../tools/toolUtils'
9493import { ChatStream } from '../../tools/chatStream'
94+ import { ChatHistoryStorage } from '../../storages/chatHistoryStorage'
9595
9696export interface ChatControllerMessagePublishers {
9797 readonly processPromptChatMessage : MessagePublisher < PromptMessage >
@@ -153,7 +153,7 @@ export class ChatController {
153153 private readonly userIntentRecognizer : UserIntentRecognizer
154154 private readonly telemetryHelper : CWCTelemetryHelper
155155 private userPromptsWatcher : vscode . FileSystemWatcher | undefined
156- private readonly chatHistoryManager : ChatHistoryManager
156+ private readonly chatHistoryStorage : ChatHistoryStorage
157157
158158 public constructor (
159159 private readonly chatControllerMessageListeners : ChatControllerMessageListeners ,
@@ -171,7 +171,7 @@ export class ChatController {
171171 this . editorContentController = new EditorContentController ( )
172172 this . promptGenerator = new PromptsGenerator ( )
173173 this . userIntentRecognizer = new UserIntentRecognizer ( )
174- this . chatHistoryManager = new ChatHistoryManager ( )
174+ this . chatHistoryStorage = new ChatHistoryStorage ( )
175175
176176 onDidChangeAmazonQVisibility ( ( visible ) => {
177177 if ( visible ) {
@@ -434,7 +434,7 @@ export class ChatController {
434434
435435 private async processTabCloseMessage ( message : TabClosedMessage ) {
436436 this . sessionStorage . deleteSession ( message . tabID )
437- this . chatHistoryManager . clear ( )
437+ this . chatHistoryStorage . deleteHistory ( message . tabID )
438438 this . triggerEventsStorage . removeTabEvents ( message . tabID )
439439 // this.telemetryHelper.recordCloseChat(message.tabID)
440440 }
@@ -747,7 +747,6 @@ export class ChatController {
747747 getLogger ( ) . error ( `error: ${ errorMessage } tabID: ${ tabID } requestID: ${ requestID } ` )
748748
749749 this . sessionStorage . deleteSession ( tabID )
750- this . chatHistoryManager . clear ( )
751750 }
752751
753752 private async processContextMenuCommand ( command : EditorContextCommand ) {
@@ -821,7 +820,6 @@ export class ChatController {
821820 codeQuery : context ?. focusAreaContext ?. names ,
822821 userIntent : this . userIntentRecognizer . getFromContextMenuCommand ( command ) ,
823822 customization : getSelectedCustomization ( ) ,
824- chatHistory : this . chatHistoryManager . getHistory ( ) ,
825823 additionalContents : [ ] ,
826824 relevantTextDocuments : [ ] ,
827825 documentReferences : [ ] ,
@@ -869,7 +867,7 @@ export class ChatController {
869867 switch ( message . command ) {
870868 case 'clear' :
871869 this . sessionStorage . deleteSession ( message . tabID )
872- this . chatHistoryManager . clear ( )
870+ this . chatHistoryStorage . getHistory ( message . tabID ) . clear ( )
873871 this . triggerEventsStorage . removeTabEvents ( message . tabID )
874872 recordTelemetryChatRunCommand ( 'clear' )
875873 return
@@ -908,7 +906,7 @@ export class ChatController {
908906 codeQuery : lastTriggerEvent . context ?. focusAreaContext ?. names ,
909907 userIntent : message . userIntent ,
910908 customization : getSelectedCustomization ( ) ,
911- chatHistory : this . chatHistoryManager . getHistory ( ) ,
909+ chatHistory : this . chatHistoryStorage . getHistory ( message . tabID ) . getHistory ( ) ,
912910 contextLengths : {
913911 ...defaultContextLengths ,
914912 } ,
@@ -996,7 +994,7 @@ export class ChatController {
996994 customization : getSelectedCustomization ( ) ,
997995 toolResults : toolResults ,
998996 origin : Origin . IDE ,
999- chatHistory : this . chatHistoryManager . getHistory ( ) ,
997+ chatHistory : this . chatHistoryStorage . getHistory ( tabID ) . getHistory ( ) ,
1000998 context : [ ] ,
1001999 relevantTextDocuments : [ ] ,
10021000 additionalContents : [ ] ,
@@ -1042,7 +1040,7 @@ export class ChatController {
10421040 codeQuery : context ?. focusAreaContext ?. names ,
10431041 userIntent : this . userIntentRecognizer . getFromPromptChatMessage ( message ) ,
10441042 customization : getSelectedCustomization ( ) ,
1045- chatHistory : this . chatHistoryManager . getHistory ( ) ,
1043+ chatHistory : this . chatHistoryStorage . getHistory ( message . tabID ) . getHistory ( ) ,
10461044 origin : Origin . IDE ,
10471045 context : message . context ?? [ ] ,
10481046 relevantTextDocuments : [ ] ,
@@ -1269,16 +1267,28 @@ export class ChatController {
12691267
12701268 triggerPayload . contextLengths . userInputContextLength = triggerPayload . message . length
12711269 triggerPayload . contextLengths . focusFileContextLength = triggerPayload . fileText . length
1272- const request = triggerPayloadToChatRequest ( triggerPayload )
1273- if (
1274- this . chatHistoryManager . getConversationId ( ) !== undefined &&
1275- this . chatHistoryManager . getConversationId ( ) !== ''
1276- ) {
1277- request . conversationState . conversationId = this . chatHistoryManager . getConversationId ( )
1278- } else {
1279- this . chatHistoryManager . setConversationId ( randomUUID ( ) )
1280- request . conversationState . conversationId = this . chatHistoryManager . getConversationId ( )
1270+
1271+ const chatHistory = this . chatHistoryStorage . getHistory ( tabID )
1272+ const newUserMessage = {
1273+ userInputMessage : {
1274+ content : triggerPayload . message ,
1275+ userIntent : triggerPayload . userIntent ,
1276+ ...( triggerPayload . origin && { origin : triggerPayload . origin } ) ,
1277+ userInputMessageContext : {
1278+ tools : tools ,
1279+ ...( triggerPayload . toolResults && { toolResults : triggerPayload . toolResults } ) ,
1280+ } ,
1281+ } ,
1282+ }
1283+ const fixedHistoryMessage = chatHistory . fixHistory ( newUserMessage )
1284+ if ( fixedHistoryMessage . userInputMessage ?. userInputMessageContext ) {
1285+ triggerPayload . toolResults = fixedHistoryMessage . userInputMessage . userInputMessageContext . toolResults
12811286 }
1287+ const request = triggerPayloadToChatRequest ( triggerPayload )
1288+ const conversationId = chatHistory . getConversationId ( ) || randomUUID ( )
1289+ chatHistory . setConversationId ( conversationId )
1290+ request . conversationState . conversationId = conversationId
1291+
12821292 triggerPayload . documentReferences = this . mergeRelevantTextDocuments ( triggerPayload . relevantTextDocuments )
12831293
12841294 // Update context transparency after it's truncated dynamically to show users only the context sent.
@@ -1328,32 +1338,14 @@ export class ChatController {
13281338 }
13291339 this . telemetryHelper . recordEnterFocusConversation ( triggerEvent . tabID )
13301340 this . telemetryHelper . recordStartConversation ( triggerEvent , triggerPayload )
1331-
1332- this . chatHistoryManager . appendUserMessage ( {
1333- userInputMessage : {
1334- content : triggerPayload . message ,
1335- userIntent : triggerPayload . userIntent ,
1336- ...( triggerPayload . origin && { origin : triggerPayload . origin } ) ,
1337- userInputMessageContext : {
1338- tools : tools ,
1339- ...( triggerPayload . toolResults && { toolResults : triggerPayload . toolResults } ) ,
1340- } ,
1341- } ,
1342- } )
1341+ chatHistory . appendUserMessage ( fixedHistoryMessage )
13431342
13441343 getLogger ( ) . info (
13451344 `response to tab: ${ tabID } conversationID: ${ session . sessionIdentifier } requestID: ${
13461345 response . $metadata . requestId
13471346 } metadata: ${ inspect ( response . $metadata , { depth : 12 } ) } `
13481347 )
1349- await this . messenger . sendAIResponse (
1350- response ,
1351- session ,
1352- tabID ,
1353- triggerID ,
1354- triggerPayload ,
1355- this . chatHistoryManager
1356- )
1348+ await this . messenger . sendAIResponse ( response , session , tabID , triggerID , triggerPayload , chatHistory )
13571349 } catch ( e : any ) {
13581350 this . telemetryHelper . recordMessageResponseError ( triggerPayload , tabID , getHttpStatusCode ( e ) ?? 0 )
13591351 // clears session, record telemetry before this call
0 commit comments