MCPcopy Create free account
hub / github.com/Louisym/MiniCC / maybe_auto_compact

Function maybe_auto_compact

tutorials/07_auto_compaction.py:353–374  ·  view source on GitHub ↗

检查是否需要自动压缩。如果需要,执行压缩。 对应源码: conversation.rs:310-333 (maybe_auto_compact)

(
    session: Session,
    cumulative_input_tokens: int,
    threshold: int = DEFAULT_AUTO_COMPACTION_THRESHOLD,
)

Source from the content-addressed store, hash-verified

351
352
353def 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# ============================================================

Callers 1

mainFunction · 0.70

Calls 2

compact_sessionFunction · 0.70
CompactionConfigClass · 0.70

Tested by

no test coverage detected