feat(middleware/context-compression): Add history tracking and resolution - #6270
feat(middleware/context-compression): Add history tracking and resolution#6270ssbushi wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a non-destructive session history feature to the context compression middleware, preserving original uncompressed messages in the request and response while storing the compressed history in message metadata. It also adds a helper function resolveCompressedHistory to extract active messages, along with corresponding tests and documentation updates. Feedback is provided regarding a potential edge case in Array.prototype.slice where a negative index could be passed if compressedMessages.length is less than tailCount.
| const compressedPrefix = | ||
| tailCount > 0 | ||
| ? compressedMessages.slice( | ||
| 0, | ||
| compressedMessages.length - tailCount | ||
| ) | ||
| : compressedMessages; |
There was a problem hiding this comment.
If compressedMessages.length is less than tailCount (which can occur in edge cases such as summarization failures or custom middleware modifications), compressedMessages.length - tailCount will be negative. In JavaScript, passing a negative index as the second argument to Array.prototype.slice slices from the end of the array rather than returning an empty array, which would result in an incorrect compressedPrefix. Using Math.max(0, compressedMessages.length - tailCount) ensures it safely defaults to 0 and returns an empty array.
| const compressedPrefix = | |
| tailCount > 0 | |
| ? compressedMessages.slice( | |
| 0, | |
| compressedMessages.length - tailCount | |
| ) | |
| : compressedMessages; | |
| const compressedPrefix = | |
| tailCount > 0 | |
| ? compressedMessages.slice( | |
| 0, | |
| Math.max(0, compressedMessages.length - tailCount) | |
| ) | |
| : compressedMessages; |
No description provided.