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

Function compact_session

mini_claude_code/compact.py:328–378  ·  view source on GitHub ↗

源码: compact.rs:75-111 流程: 1. 检查是否需要压缩 (should_compact) 2. 分割: messages[:-N] 压缩, messages[-N:] 保留 3. 旧消息 → 结构化摘要 4. 新 session = [system 摘要] + [保留消息]

(
    messages: list[Message],
    config: CompactionConfig,
)

Source from the content-addressed store, hash-verified

326# ============================================================
327
328def compact_session(
329 messages: list[Message],
330 config: CompactionConfig,
331) -> CompactionResult:
332 """源码: compact.rs:75-111
333
334 流程:
335 1. 检查是否需要压缩 (should_compact)
336 2. 分割: messages[:-N] 压缩, messages[-N:] 保留
337 3. 旧消息 → 结构化摘要
338 4. 新 session = [system 摘要] + [保留消息]
339 """
340 if not should_compact(messages, config):
341 return CompactionResult(
342 compacted_messages=list(messages),
343 removed_count=0,
344 )
345
346 keep_from = max(0, len(messages) - config.preserve_recent_messages)
347 removed = messages[:keep_from]
348 preserved = messages[keep_from:]
349
350 summary = summarize_messages(removed)
351 formatted_summary = format_compact_summary(summary)
352
353 continuation_text = (
354 "This session is being continued from a previous conversation "
355 "that ran out of context. The summary below covers the earlier portion.\n\n"
356 f"{formatted_summary}"
357 )
358 if preserved:
359 continuation_text += "\n\nRecent messages are preserved verbatim."
360 continuation_text += (
361 "\nContinue the conversation from where it left off without "
362 "asking the user any further questions."
363 )
364
365 # 摘要作为 system 角色的消息 — 源码: compact.rs:95-99
366 system_msg = Message(
367 role="user", # 我们的 models.py 没有 system role,用 user 代替
368 content=[TextContentBlock(text=continuation_text)],
369 )
370
371 compacted = [system_msg] + list(preserved)
372
373 return CompactionResult(
374 summary=summary,
375 formatted_summary=formatted_summary,
376 compacted_messages=compacted,
377 removed_count=len(removed),
378 )

Callers 2

_maybe_auto_compactMethod · 0.90
run_replFunction · 0.90

Calls 6

MessageClass · 0.90
TextContentBlockClass · 0.90
should_compactFunction · 0.70
CompactionResultClass · 0.70
summarize_messagesFunction · 0.70
format_compact_summaryFunction · 0.70

Tested by

no test coverage detected