Make compression algo slightly more aggressive (#10024)

This commit is contained in:
Tommaso Sciortino
2025-09-27 05:15:53 -07:00
committed by GitHub
parent db51e3f4cd
commit 93694c6a65
2 changed files with 3 additions and 3 deletions

View File

@@ -131,7 +131,7 @@ async function fromAsync<T>(promise: AsyncGenerator<T>): Promise<readonly T[]> {
return results;
}
describe('findIndexAfterFraction', () => {
describe('findCompressSplitPoint', () => {
it('should throw an error for non-positive numbers', () => {
expect(() => findCompressSplitPoint([], 0)).toThrow(
'Fraction must be between 0 and 1',
@@ -156,7 +156,7 @@ describe('findIndexAfterFraction', () => {
{ role: 'model', parts: [{ text: 'This is the fourth message.' }] }, // JSON length: 68 (80%)
{ role: 'user', parts: [{ text: 'This is the fifth message.' }] }, // JSON length: 65 (100%)
];
expect(findCompressSplitPoint(history, 0.5)).toBe(2);
expect(findCompressSplitPoint(history, 0.5)).toBe(4);
});
it('should handle a fraction of last index', () => {

View File

@@ -83,7 +83,6 @@ export function findCompressSplitPoint(
let lastSplitPoint = 0; // 0 is always valid (compress nothing)
let cumulativeCharCount = 0;
for (let i = 0; i < contents.length; i++) {
cumulativeCharCount += charCounts[i];
const content = contents[i];
if (
content.role === 'user' &&
@@ -94,6 +93,7 @@ export function findCompressSplitPoint(
}
lastSplitPoint = i;
}
cumulativeCharCount += charCounts[i];
}
// We found no split points after targetCharCount.