Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/core/src/amazonq/webview/ui/apps/cwChatConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export interface ConnectorProps extends BaseConnectorProps {
inProgress: boolean,
message: string,
messageId: string | undefined,
enableStopAction: boolean
enableStopAction: boolean,
isPromptInputDisabled: boolean
) => void
}

Expand Down Expand Up @@ -259,7 +260,8 @@ export class Connector extends BaseConnector {
messageData.inProgress,
messageData.message ?? undefined,
messageData.messageId ?? undefined,
enableStopAction
enableStopAction,
false
)
return
}
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/amazonq/webview/ui/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,13 @@ export const createMynahUI = (
inProgress: boolean,
message: string | undefined,
messageId: string | undefined = undefined,
enableStopAction: boolean = false
enableStopAction: boolean = false,
isPromptInputDisabled: boolean = true
) => {
if (inProgress) {
mynahUI.updateStore(tabID, {
loadingChat: true,
promptInputDisabledState: true,
promptInputDisabledState: isPromptInputDisabled,
cancelButtonWhenLoading: enableStopAction,
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ export class ChatController {
private async processStopResponseMessage(message: StopResponseMessage) {
const session = this.sessionStorage.getSession(message.tabID)
session.tokenSource.cancel()
this.messenger.sendEmptyMessage(message.tabID, '', undefined)
this.chatHistoryStorage.getTabHistory(message.tabID).clearRecentHistory()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -735,4 +735,28 @@ export class Messenger {
public sendAsyncEventProgress(tabID: string, inProgress: boolean, message: string | undefined) {
this.dispatcher.sendAsyncEventProgress(new AsyncEventProgressMessage(tabID, 'CWChat', inProgress, message))
}

public sendEmptyMessage(
tabID: string,
triggerId: string,
mergedRelevantDocuments: DocumentReference[] | undefined
) {
this.dispatcher.sendChatMessage(
new ChatMessage(
{
message: '',
messageType: 'answer',
followUps: undefined,
followUpsHeader: undefined,
relatedSuggestions: undefined,
triggerID: triggerId,
messageID: '',
userIntent: undefined,
codeBlockLanguage: undefined,
contextList: undefined,
},
tabID
)
)
}
}
21 changes: 19 additions & 2 deletions packages/core/src/codewhispererChat/storages/chatHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,13 @@ export class ChatHistoryManager {
* Push an assistant message to the history
*/
public pushAssistantMessage(newMessage: ChatMessage): void {
if (newMessage !== undefined && this.lastUserMessage !== undefined) {
this.logger.warn('last Message should not be defined when pushing an assistant message')
if (newMessage !== undefined && this.lastUserMessage === undefined) {
this.logger.warn('first assistant response should always come after user input message')
return
}
// check if last message in histroy is assistant message and now replace it in that case
if (this.history.length > 0 && this.history.at(-1)?.assistantResponseMessage) {
this.history.pop()
}
this.history.push(newMessage)
}
Expand All @@ -101,6 +106,18 @@ export class ChatHistoryManager {
}

private trimConversationHistory(): void {
// make sure the UseInputMessage is the first stored message
if (this.history.length === 1 && this.history[0].assistantResponseMessage) {
this.history = []
}

if (
this.history.at(-1)?.assistantResponseMessage?.content === '' &&
this.history.at(-1)?.assistantResponseMessage?.toolUses === undefined
) {
Comment on lines +114 to +117
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please explain what this do?

Copy link
Contributor Author

@ashishrp-aws ashishrp-aws Apr 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In some scenarios we are breaking the breaking the assistant response and that empty message is being pushed to chat history as assistant response. so in that specific case, we are correcting the history by clearing up the recent messages. Also making sure that to not toolUse messages with empty content

this.clearRecentHistory()
}

if (this.history.length <= MaxConversationHistoryLength) {
return
}
Expand Down
Loading