in server/aws-lsp-codewhisperer/src/language-server/agenticChat/tools/chatDb/chatDb.ts [543:597]
private calculateCurrentMessageCharacterCount(message: Message): number {
let count = 0
// Count characters of message text
count += message.body.length
// Count characters in tool uses
if (message.toolUses) {
try {
for (const toolUse of message.toolUses) {
count += JSON.stringify(toolUse).length
}
} catch (e) {
this.#features.logging.error(`Error counting toolUses: ${String(e)}`)
}
}
// Count characters in tool results
if (message.userInputMessageContext?.toolResults) {
try {
for (const toolResul of message.userInputMessageContext.toolResults) {
count += JSON.stringify(toolResul).length
}
} catch (e) {
this.#features.logging.error(`Error counting toolResults: ${String(e)}`)
}
}
// Count characters in tool spec for the current user message
if (message.userInputMessageContext?.tools) {
try {
for (const toolSpec of message.userInputMessageContext.tools) {
count += JSON.stringify(toolSpec).length
}
} catch (e) {
this.#features.logging.error(`Error counting tool spec length: ${String(e)}`)
}
}
if (message.userInputMessageContext?.additionalContext) {
try {
for (const addtionalContext of message.userInputMessageContext.additionalContext) {
count += JSON.stringify(addtionalContext).length
}
} catch (e) {
this.#features.logging.error(`Error counting addtionalContext length: ${String(e)}`)
}
}
if (message.userInputMessageContext?.editorState) {
try {
count += JSON.stringify(message.userInputMessageContext?.editorState).length
} catch (e) {
this.#features.logging.error(`Error counting editorState length: ${String(e)}`)
}
}
return count
}