(params: AutoCompactParams)
| 264 | * @returns Whether auto-compacting should be triggered |
| 265 | */ |
| 266 | export function shouldAutoCompact(params: AutoCompactParams): boolean { |
| 267 | const { chatHistory, model, systemMessage, tools } = params; |
| 268 | |
| 269 | const inputTokens = countTotalInputTokens({ |
| 270 | chatHistory, |
| 271 | systemMessage, |
| 272 | tools, |
| 273 | model, |
| 274 | }); |
| 275 | const contextLimit = getModelContextLimit(model); |
| 276 | const maxTokens = getModelMaxTokens(model); |
| 277 | |
| 278 | // Additional buffer matching the auto-compaction threshold formula |
| 279 | const ratioCompactionBuffer = Math.ceil( |
| 280 | (1 - AUTO_COMPACT_BUFFER_RATIO) * (contextLimit - maxTokens), |
| 281 | ); |
| 282 | const safeCompactionBuffer = Math.max(maxTokens, ratioCompactionBuffer); |
| 283 | const compactionBuffer = Math.min( |
| 284 | safeCompactionBuffer, |
| 285 | AUTO_COMPACT_BUFFER_CAP, |
| 286 | ); |
| 287 | |
| 288 | const compactionThreshold = contextLimit - maxTokens - compactionBuffer; |
| 289 | |
| 290 | // Ensure we have positive space available for input |
| 291 | if (compactionThreshold <= 0) { |
| 292 | throw new Error( |
| 293 | `max_tokens is larger than context_length, which should not be possible. Please check your configuration.`, |
| 294 | ); |
| 295 | } |
| 296 | |
| 297 | const toolTokens = tools ? countToolDefinitionTokens(tools) : 0; |
| 298 | const systemTokens = systemMessage ? encode(systemMessage).length : 0; |
| 299 | const shouldCompact = inputTokens >= compactionThreshold; |
| 300 | |
| 301 | logger.debug("Context usage check", { |
| 302 | inputTokens, |
| 303 | historyTokens: countChatHistoryTokens(chatHistory, model), |
| 304 | systemTokens, |
| 305 | toolTokens, |
| 306 | contextLimit, |
| 307 | maxTokens, |
| 308 | reservedForOutput: maxTokens, |
| 309 | compactionBuffer, |
| 310 | compactionThreshold, |
| 311 | shouldCompact, |
| 312 | }); |
| 313 | |
| 314 | return shouldCompact; |
| 315 | } |
no test coverage detected