@@ -45,11 +45,6 @@ export class ToolResultValidationError extends Error {
4545}
4646
4747export const EMPTY_CONVERSATION_LIST_ID = 'empty'
48- // Maximum number of characters to keep in request
49- // (200K tokens - 8K output tokens - 2k system prompt) * 3 = 570K characters, intentionally overestimating with 3:1 ratio
50- export const MaxOverallCharacters = 570_000
51- // Maximum number of history messages to include in each request to the LLM
52- const maxConversationHistoryMessages = 250
5348
5449/**
5550 * A singleton database class that manages chat history persistence using LokiJS.
@@ -708,15 +703,12 @@ export class ChatDatabase {
708703
709704 /**
710705 * Prepare the history messages for service request and fix the persisted history in DB to maintain the following invariants:
711- * 1. The history contains at most MaxConversationHistoryMessages messages. Oldest messages are dropped.
712- * 2. The first message is from the user and without any tool usage results, and the last message is from the assistant.
706+ * 1. The first message is from the user and without any tool usage results, and the last message is from the assistant.
713707 * The history contains alternating sequene of userMessage followed by assistantMessages
714- * 3. The toolUse and toolResult relationship is valid
715- * 4. The history character length is <= MaxConversationHistoryCharacters - newUserMessageCharacterCount. Oldest messages are dropped.
708+ * 2. The toolUse and toolResult relationship is valid
716709 */
717710 fixAndGetHistory (
718711 tabId : string ,
719- conversationId : string ,
720712 newUserMessage : ChatMessage ,
721713 pinnedContextMessages : ChatMessage [ ]
722714 ) : MessagesWithCharacterCount {
@@ -732,18 +724,19 @@ export class ChatDatabase {
732724
733725 this . #features. logging . info ( `Fixing history: tabId=${ tabId } ` )
734726
735- // 1. Make sure the length of the history messages don't exceed MaxConversationHistoryMessages
736- let allMessages = this . getMessages ( tabId , maxConversationHistoryMessages )
727+ let allMessages = this . getMessages ( tabId )
737728 if ( allMessages . length > 0 ) {
738- // 2 . Fix history: Ensure messages in history is valid for server side checks
729+ // 1 . Fix history: Ensure messages in history is valid for server side checks
739730 this . ensureValidMessageSequence ( tabId , allMessages )
740731
741- // 3 . Fix new user prompt: Ensure lastMessage in history toolUse and newMessage toolResult relationship is valid
732+ // 2 . Fix new user prompt: Ensure lastMessage in history toolUse and newMessage toolResult relationship is valid
742733 this . validateAndFixNewMessageToolResults ( allMessages , newUserMessage )
743734
744- // 4. NOTE: Keep this trimming logic at the end of the preprocess.
745- // Make sure max characters ≤ remaining Character Budget, must be put at the end of preprocessing
746- messagesWithCount = this . trimMessagesToMaxLength ( allMessages , newUserInputCount , tabId , conversationId )
735+ messagesWithCount = {
736+ history : allMessages ,
737+ historyCount : this . calculateMessagesCharacterCount ( allMessages ) ,
738+ currentCount : newUserInputCount ,
739+ }
747740
748741 // Edge case: If the history is empty and the next message contains tool results, then we have to just abandon them.
749742 if (
@@ -772,74 +765,11 @@ export class ChatDatabase {
772765 return messagesWithCount
773766 }
774767
775- /**
776- * Finds a suitable "break point" index in the message sequence.
777- *
778- * It ensures that the "break point" is at a clean conversation boundary where:
779- * 1. The message is from a user (type === 'prompt')
780- * 2. The message doesn't contain tool results that would break tool use/result pairs
781- * 3. The message has a non-empty body
782- *
783- * @param allMessages The array of conversation messages to search through
784- * @returns The index to trim from, or undefined if no suitable trimming point is found
785- */
786- private findIndexToTrim ( allMessages : Message [ ] ) : number | undefined {
787- for ( let i = 2 ; i < allMessages . length ; i ++ ) {
788- const message = allMessages [ i ]
789- if ( message . type === ( 'prompt' as ChatItemType ) && this . isValidUserMessageWithoutToolResults ( message ) ) {
790- return i
791- }
792- }
793- return undefined
794- }
795-
796768 private isValidUserMessageWithoutToolResults ( message : Message ) : boolean {
797769 const ctx = message . userInputMessageContext
798770 return ! ! ctx && ( ! ctx . toolResults || ctx . toolResults . length === 0 ) && message . body !== ''
799771 }
800772
801- private trimMessagesToMaxLength (
802- messages : Message [ ] ,
803- newUserInputCount : number ,
804- tabId : string ,
805- conversationId : string
806- ) : MessagesWithCharacterCount {
807- let historyCharacterCount = this . calculateMessagesCharacterCount ( messages )
808- const maxHistoryCharacterSize = Math . max ( 0 , MaxOverallCharacters - newUserInputCount )
809- let trimmedHistory = false
810- this . #features. logging . debug (
811- `Current history character count: ${ historyCharacterCount } , remaining history character budget: ${ maxHistoryCharacterSize } `
812- )
813- while ( historyCharacterCount > maxHistoryCharacterSize && messages . length > 2 ) {
814- trimmedHistory = true
815- // Find the next valid user message to start from
816- const indexToTrim = this . findIndexToTrim ( messages )
817- if ( indexToTrim !== undefined && indexToTrim > 0 ) {
818- this . #features. logging . debug (
819- `Removing the first ${ indexToTrim } elements in the history due to character count limit`
820- )
821- messages . splice ( 0 , indexToTrim )
822- } else {
823- this . #features. logging . debug (
824- 'Could not find a valid point to trim, reset history to reduce character count'
825- )
826- this . replaceHistory ( tabId , 'cwc' , conversationId , [ ] )
827- return { history : [ ] , historyCount : 0 , currentCount : newUserInputCount }
828- }
829- historyCharacterCount = this . calculateMessagesCharacterCount ( messages )
830- this . #features. logging . debug ( `History character count post trimming: ${ historyCharacterCount } ` )
831- }
832-
833- if ( trimmedHistory ) {
834- this . replaceHistory ( tabId , 'cwc' , conversationId , messages )
835- }
836- return {
837- history : messages ,
838- historyCount : historyCharacterCount ,
839- currentCount : newUserInputCount ,
840- }
841- }
842-
843773 private calculateToolSpecCharacterCount ( currentMessage : ChatMessage ) : number {
844774 let count = 0
845775 if ( currentMessage . userInputMessage ?. userInputMessageContext ?. tools ) {
0 commit comments