Skip to content

Commit 3727b95

Browse files
Randall-Jiangwweitao
authored andcommitted
fix: add validation for empty chat history (aws#1403)
1 parent a8dc705 commit 3727b95

File tree

1 file changed

+12
-57
lines changed
  • server/aws-lsp-codewhisperer/src/language-server/agenticChat/tools/chatDb

1 file changed

+12
-57
lines changed

server/aws-lsp-codewhisperer/src/language-server/agenticChat/tools/chatDb/chatDb.ts

Lines changed: 12 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ export class ChatDatabase {
486486
}
487487

488488
private handleEmptyAssistantMessage(messages: Message[]): void {
489-
if (messages.length === 0) {
489+
if (messages.length < 2) {
490490
return
491491
}
492492

@@ -573,62 +573,6 @@ export class ChatDatabase {
573573
return count
574574
}
575575

576-
private calculateCurrentMessageCharacterCount(message: Message): number {
577-
let count = 0
578-
// Count characters of message text
579-
count += message.body.length
580-
581-
// Count characters in tool uses
582-
if (message.toolUses) {
583-
try {
584-
for (const toolUse of message.toolUses) {
585-
count += JSON.stringify(toolUse).length
586-
}
587-
} catch (e) {
588-
this.#features.logging.error(`Error counting toolUses: ${String(e)}`)
589-
}
590-
}
591-
// Count characters in tool results
592-
if (message.userInputMessageContext?.toolResults) {
593-
try {
594-
for (const toolResul of message.userInputMessageContext.toolResults) {
595-
count += JSON.stringify(toolResul).length
596-
}
597-
} catch (e) {
598-
this.#features.logging.error(`Error counting toolResults: ${String(e)}`)
599-
}
600-
}
601-
// Count characters in tool spec for the current user message
602-
if (message.userInputMessageContext?.tools) {
603-
try {
604-
for (const toolSpec of message.userInputMessageContext.tools) {
605-
count += JSON.stringify(toolSpec).length
606-
}
607-
} catch (e) {
608-
this.#features.logging.error(`Error counting tool spec length: ${String(e)}`)
609-
}
610-
}
611-
612-
if (message.userInputMessageContext?.additionalContext) {
613-
try {
614-
for (const addtionalContext of message.userInputMessageContext.additionalContext) {
615-
count += JSON.stringify(addtionalContext).length
616-
}
617-
} catch (e) {
618-
this.#features.logging.error(`Error counting addtionalContext length: ${String(e)}`)
619-
}
620-
}
621-
622-
if (message.userInputMessageContext?.editorState) {
623-
try {
624-
count += JSON.stringify(message.userInputMessageContext?.editorState).length
625-
} catch (e) {
626-
this.#features.logging.error(`Error counting editorState length: ${String(e)}`)
627-
}
628-
}
629-
return count
630-
}
631-
632576
ensureValidMessageSequence(messages: Message[], newUserMessage: ChatMessage): void {
633577
if (messages.length === 0) {
634578
return
@@ -646,6 +590,10 @@ export class ChatDatabase {
646590
this.#features.logging.debug('Dropped trailing user message')
647591
}
648592

593+
if (messages.length === 0) {
594+
return
595+
}
596+
649597
// Make sure there are alternating user and assistant messages
650598
const currentMessageType = chatMessageToMessage(newUserMessage).type
651599
const lastMessageType = messages[messages.length - 1].type
@@ -662,6 +610,13 @@ export class ChatDatabase {
662610
if (newUserMessage?.userInputMessage?.userInputMessageContext) {
663611
const newUserMessageContext = newUserMessage.userInputMessage.userInputMessageContext
664612
const toolResults = newUserMessageContext.toolResults || []
613+
if (messages.length === 0) {
614+
if (toolResults && toolResults.length > 0) {
615+
this.#features.logging.warn('New message has tool results but last message has no tool uses')
616+
return false
617+
}
618+
return true
619+
}
665620
const lastMsg = messages[messages.length - 1]
666621
const lastMsgToolUses = lastMsg?.toolUses || []
667622

0 commit comments

Comments
 (0)