@@ -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