diff --git a/src/utils/messageUtils.ts b/src/utils/messageUtils.ts index 2d7fc62..e4aacde 100644 --- a/src/utils/messageUtils.ts +++ b/src/utils/messageUtils.ts @@ -51,13 +51,39 @@ export const limitMessageTokens = ( const limitedMessages: MessageInterface[] = []; let tokenCount = 0; - for (let i = messages.length - 1; i >= 0; i--) { + const isSystemFirstMessage = messages[0]?.role === 'system'; + let retainSystemMessage = false; + + // Check if the first message is a system message and if it fits within the token limit + if (isSystemFirstMessage) { + const systemTokenCount = countTokens([messages[0]], model); + if (systemTokenCount < limit) { + tokenCount += systemTokenCount; + retainSystemMessage = true; + } + } + + // Iterate through messages in reverse order, adding them to the limitedMessages array + // until the token limit is reached (excludes first message) + for (let i = messages.length - 1; i >= 1; i--) { const count = countTokens([messages[i]], model); if (count + tokenCount > limit) break; tokenCount += count; limitedMessages.unshift({ ...messages[i] }); } + // Process first message + if (retainSystemMessage) { + // Insert the system message in the third position from the end + limitedMessages.splice(-3, 0, { ...messages[0] }); + } else if (!isSystemFirstMessage) { + // Check if the first message (non-system) can fit within the limit + const firstMessageTokenCount = countTokens([messages[0]], model); + if (firstMessageTokenCount + tokenCount < limit) { + limitedMessages.unshift({ ...messages[0] }); + } + } + return limitedMessages; };