检查是否需要自动压缩。如果需要,执行压缩。 对应源码: conversation.rs:310-333 (maybe_auto_compact)
(
session: Session,
cumulative_input_tokens: int,
threshold: int = DEFAULT_AUTO_COMPACTION_THRESHOLD,
)
| 351 | |
| 352 | |
| 353 | def maybe_auto_compact( |
| 354 | session: Session, |
| 355 | cumulative_input_tokens: int, |
| 356 | threshold: int = DEFAULT_AUTO_COMPACTION_THRESHOLD, |
| 357 | ) -> Optional[CompactionResult]: |
| 358 | """ |
| 359 | 检查是否需要自动压缩。如果需要,执行压缩。 |
| 360 | |
| 361 | 对应源码: conversation.rs:310-333 (maybe_auto_compact) |
| 362 | """ |
| 363 | if cumulative_input_tokens < threshold: |
| 364 | return None # 还没到限制,不需要压缩 |
| 365 | |
| 366 | result = compact_session(session, CompactionConfig( |
| 367 | preserve_recent_messages=4, |
| 368 | max_estimated_tokens=0, # 强制压缩(0 表示任何大小都要压缩) |
| 369 | )) |
| 370 | |
| 371 | if result.removed_message_count == 0: |
| 372 | return None # 没有可压缩的消息 |
| 373 | |
| 374 | return result |
| 375 | |
| 376 | |
| 377 | # ============================================================ |
no test coverage detected