Skip to content

Commit 8a5322a

Browse files
authored
fix: include toolSpec count for history trimming (#1778)
1 parent fc8cc10 commit 8a5322a

File tree

1 file changed

+20
-2
lines changed
  • server/aws-lsp-codewhisperer/src/language-server/agenticChat/tools/chatDb

1 file changed

+20
-2
lines changed

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -617,8 +617,12 @@ export class ChatDatabase {
617617
const currentUserInputCharacterCount = this.calculateMessagesCharacterCount([
618618
chatMessageToMessage(newUserMessage),
619619
])
620-
this.#features.logging.debug(`Current user message characters: ${currentUserInputCharacterCount}`)
621-
const maxHistoryCharacterSize = Math.max(0, MaxOverallCharacters - currentUserInputCharacterCount)
620+
const currentInputToolSpecCount = this.calculateToolSpecCharacterCount(newUserMessage)
621+
const currentUserInputCount = currentUserInputCharacterCount + currentInputToolSpecCount
622+
this.#features.logging.debug(
623+
`Current user message characters input: ${currentUserInputCharacterCount} + toolSpec: ${currentInputToolSpecCount}`
624+
)
625+
const maxHistoryCharacterSize = Math.max(0, MaxOverallCharacters - currentUserInputCount)
622626
this.#features.logging.debug(`Current remaining character budget: ${maxHistoryCharacterSize}`)
623627
while (totalCharacters > maxHistoryCharacterSize && messages.length > 2) {
624628
// Find the next valid user message to start from
@@ -640,6 +644,20 @@ export class ChatDatabase {
640644
return messages
641645
}
642646

647+
private calculateToolSpecCharacterCount(currentMessage: ChatMessage): number {
648+
let count = 0
649+
if (currentMessage.userInputMessage?.userInputMessageContext?.tools) {
650+
try {
651+
for (const tool of currentMessage.userInputMessage?.userInputMessageContext?.tools) {
652+
count += JSON.stringify(tool).length
653+
}
654+
} catch (e) {
655+
this.#features.logging.error(`Error counting tools: ${String(e)}`)
656+
}
657+
}
658+
return count
659+
}
660+
643661
private calculateMessagesCharacterCount(allMessages: Message[]): number {
644662
let count = 0
645663
for (const message of allMessages) {

0 commit comments

Comments
 (0)