Skip to content

Commit 63da6b2

Browse files
committed
fix(amazonq): Don't delete history on exception; Don't include history for requests to Mynah
1 parent 5825d42 commit 63da6b2

File tree

3 files changed

+16
-35
lines changed

3 files changed

+16
-35
lines changed

packages/core/src/codewhispererChat/controllers/chat/controller.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,7 @@ export class ChatController {
744744
const toolResults: ToolResult[] = []
745745

746746
let response = ''
747+
let shouldSkipHistory = false
747748
if (toolUseError) {
748749
toolResults.push({
749750
content: [{ text: toolUseError.message }],
@@ -753,6 +754,7 @@ export class ChatController {
753754
if (toolUseError instanceof SyntaxError) {
754755
response =
755756
"Your toolUse input isn't valid. Please check the syntax and make sure the input is complete. If the input is large, break it down into multiple tool uses with smaller input."
757+
shouldSkipHistory = true
756758
}
757759
} else {
758760
const result = ToolUtils.tryFromToolUse(toolUse)
@@ -844,6 +846,7 @@ export class ChatController {
844846
contextLengths: {
845847
...defaultContextLengths,
846848
},
849+
skipHistoryRecord: shouldSkipHistory,
847850
},
848851
triggerID
849852
)
@@ -1120,9 +1123,6 @@ export class ChatController {
11201123

11211124
this.messenger.sendErrorMessage(errorMessage, tabID, requestID, statusCode)
11221125
getLogger().error(`error: ${errorMessage} tabID: ${tabID} requestID: ${requestID}`)
1123-
1124-
this.sessionStorage.deleteSession(tabID)
1125-
this.chatHistoryDb.clearTab(tabID)
11261126
}
11271127

11281128
private async processContextMenuCommand(command: EditorContextCommand) {
@@ -1299,6 +1299,7 @@ export class ChatController {
12991299
this.processException(e, message.tabID)
13001300
}
13011301
}
1302+
13021303
private sessionCleanUp(session: ChatSession) {
13031304
// Create a fresh token for this new conversation
13041305
session.createNewTokenSource()
@@ -1581,9 +1582,10 @@ export class ChatController {
15811582
if (currentMessage) {
15821583
this.chatHistoryDb.fixHistory(tabID, currentMessage)
15831584
}
1584-
request.conversationState.history = this.chatHistoryDb
1585-
.getMessages(tabID)
1586-
.map((chat) => messageToChatMessage(chat))
1585+
// Do not include chatHistory for requests going to Mynah
1586+
request.conversationState.history = request.conversationState.currentMessage?.userInputMessage?.userIntent
1587+
? []
1588+
: this.chatHistoryDb.getMessages(tabID).map((chat) => messageToChatMessage(chat))
15871589
request.conversationState.conversationId = session.sessionIdentifier
15881590

15891591
triggerPayload.documentReferences = this.mergeRelevantTextDocuments(triggerPayload.relevantTextDocuments)
@@ -1646,7 +1648,12 @@ export class ChatController {
16461648
this.telemetryHelper.recordEnterFocusConversation(triggerEvent.tabID)
16471649
this.telemetryHelper.recordStartConversation(triggerEvent, triggerPayload)
16481650

1649-
if (currentMessage && session.sessionIdentifier && !this.isTriggerCancelled(triggerID)) {
1651+
if (
1652+
currentMessage &&
1653+
session.sessionIdentifier &&
1654+
!this.isTriggerCancelled(triggerID) &&
1655+
!triggerPayload.skipHistoryRecord
1656+
) {
16501657
this.chatHistoryDb.addMessage(tabID, 'cwc', session.sessionIdentifier, {
16511658
body: triggerPayload.message,
16521659
type: 'prompt' as any,

packages/core/src/codewhispererChat/controllers/chat/model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ export interface TriggerPayload {
238238
origin?: Origin
239239
pairProgrammingModeOn?: boolean
240240
history?: Message[]
241+
skipHistoryRecord?: boolean
241242
}
242243

243244
export type ContextLengths = {

packages/core/src/shared/db/chatDb/chatDb.ts

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -170,34 +170,6 @@ export class Database {
170170
}
171171
}
172172

173-
// Removes the most recent message(s) from the chat history for a given tab
174-
clearRecentHistory(tabId: string): void {
175-
if (this.initialized) {
176-
const historyId = this.historyIdMapping.get(tabId)
177-
this.logger.info(`Clearing recent history: tabId=${tabId}, historyId=${historyId || 'undefined'}`)
178-
if (historyId) {
179-
const tabCollection = this.db.getCollection<Tab>(TabCollection)
180-
const tabData = tabCollection.findOne({ historyId })
181-
if (tabData) {
182-
const activeConversation = tabData.conversations[0]
183-
const allMessages = this.getMessages(tabId)
184-
const lastMessage = allMessages[allMessages.length - 1]
185-
this.logger.debug(`Last message type: ${lastMessage.type}`)
186-
187-
if (lastMessage.type === ('prompt' as ChatItemType)) {
188-
allMessages.pop()
189-
this.logger.info(`Removed last user message`)
190-
} else {
191-
allMessages.splice(-2)
192-
this.logger.info(`Removed last assistant message and user message`)
193-
}
194-
activeConversation.messages = allMessages
195-
tabCollection.update(tabData)
196-
}
197-
}
198-
}
199-
}
200-
201173
updateTabOpenState(tabId: string, isOpen: boolean) {
202174
if (this.initialized) {
203175
const tabCollection = this.db.getCollection<Tab>(TabCollection)
@@ -388,6 +360,7 @@ export class Database {
388360
this.handleToolUses(allMessages, newUserMessage)
389361

390362
activeConversation.messages = allMessages
363+
tabData.updatedAt = new Date()
391364
tabCollection.update(tabData)
392365
this.logger.info(`Updated tab data in collection`)
393366
}

0 commit comments

Comments
 (0)