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

Function demo_auto_compaction

tutorials/16_prompt_building_and_compaction.py:948–1007  ·  view source on GitHub ↗

演示自动压缩

()

Source from the content-addressed store, hash-verified

946
947
948def demo_auto_compaction():
949 """演示自动压缩"""
950 print("\n" + "=" * 60)
951 print("自动压缩演示")
952 print("=" * 60)
953
954 # 构建一个较长的会话
955 messages = [
956 ConversationMessage.user_text("请帮我修改 rust/crates/runtime/src/main.rs 中的 bug"),
957 ConversationMessage(
958 role=MessageRole.ASSISTANT,
959 blocks=[
960 ContentBlock(block_type="text", text="让我先读取这个文件"),
961 ContentBlock(
962 block_type="tool_use", tool_name="Read",
963 tool_id="1", input_str='{"path":"rust/crates/runtime/src/main.rs"}'
964 ),
965 ],
966 ),
967 ConversationMessage(
968 role=MessageRole.TOOL,
969 blocks=[ContentBlock(
970 block_type="tool_result", tool_name="Read",
971 output="fn main() { println!(\"hello\"); }" * 50, # 模拟大文件
972 )],
973 ),
974 ConversationMessage(
975 role=MessageRole.ASSISTANT,
976 blocks=[ContentBlock(
977 block_type="text",
978 text="我发现了 bug。Next: 需要修改第 42 行的类型错误。"
979 )],
980 ),
981 ConversationMessage.user_text("好的请修改"),
982 ConversationMessage(
983 role=MessageRole.ASSISTANT,
984 blocks=[ContentBlock(
985 block_type="text",
986 text="已修改完成。todo: 还需要更新测试文件 tests/test_main.rs"
987 )],
988 ),
989 ]
990
991 print(f"\n压缩前:")
992 print(f" 消息数: {len(messages)}")
993 print(f" 估算 tokens: {estimate_session_tokens(messages)}")
994
995 result = compact_session(
996 messages,
997 CompactionConfig(preserve_recent_messages=2, max_estimated_tokens=100),
998 )
999
1000 print(f"\n压缩后:")
1001 print(f" 消息数: {len(result.compacted_messages)}")
1002 print(f" 删除了: {result.removed_count} 条消息")
1003 print(f" 估算 tokens: {estimate_session_tokens(result.compacted_messages)}")
1004
1005 print(f"\n摘要内容:")

Calls 6

ConversationMessageClass · 0.70
ContentBlockClass · 0.70
estimate_session_tokensFunction · 0.70
compact_sessionFunction · 0.70
CompactionConfigClass · 0.70
user_textMethod · 0.45

Tested by

no test coverage detected