| 254 | |
| 255 | // Helper to calculate threshold using the new formula |
| 256 | const calculateThreshold = ( |
| 257 | contextLength: number, |
| 258 | maxTokens: number, |
| 259 | ): number => { |
| 260 | const ratioCompactionBuffer = Math.ceil( |
| 261 | (1 - AUTO_COMPACT_BUFFER_RATIO) * (contextLength - maxTokens), |
| 262 | ); |
| 263 | const safeCompactionBuffer = Math.max(maxTokens, ratioCompactionBuffer); |
| 264 | const compactionBuffer = Math.min( |
| 265 | safeCompactionBuffer, |
| 266 | AUTO_COMPACT_BUFFER_CAP, |
| 267 | ); |
| 268 | return contextLength - maxTokens - compactionBuffer; |
| 269 | }; |
| 270 | |
| 271 | it("should compact when input tokens exceed threshold with maxTokens", () => { |
| 272 | const model = createModel(1000, 200); // 1000 context, 200 max output |