Skip to content

Commit 38b4fe1

Browse files
lzwjavaclaude
andcommitted
feat: add /compact command to compress conversation history
Add /compact command that uses LLM to intelligently summarize and compress conversation history while preserving key context like decisions, code changes, and technical details. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 5f3b2cc commit 38b4fe1

3 files changed

Lines changed: 56 additions & 0 deletions

File tree

iclaw/commands/compact.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""Handle /compact command to compress conversation history using LLM."""
2+
3+
4+
def handle_compact_command(messages, chat_fn, copilot_token, current_model):
5+
"""Compress conversation history using LLM to summarize context."""
6+
if not messages:
7+
print("No conversation history to compact.")
8+
return messages
9+
10+
prompt = """Summarize the conversation history below into a concise context summary that preserves:
11+
1. Key decisions and outcomes
12+
2. Important code changes or file references
13+
3. Current task or goal
14+
4. Critical technical details
15+
16+
Be extremely concise. Output only the summary, no preamble.
17+
18+
Conversation history:
19+
"""
20+
21+
history_text = ""
22+
for msg in messages:
23+
role = msg.get("role", "")
24+
content = msg.get("content", "")
25+
if content:
26+
history_text += f"{role}: {content}\n\n"
27+
28+
compact_messages = [{"role": "user", "content": prompt + history_text}]
29+
30+
try:
31+
response = chat_fn(compact_messages, copilot_token, current_model, tools=None)
32+
summary = response.get("content", "")
33+
34+
if summary:
35+
new_messages = [
36+
{
37+
"role": "system",
38+
"content": f"Previous conversation summary:\n{summary}",
39+
}
40+
]
41+
print(f"Compacted {len(messages)} messages into summary.")
42+
return new_messages
43+
else:
44+
print("Failed to compact: no summary generated.")
45+
return messages
46+
except Exception as e:
47+
print(f"Error compacting history: {e}")
48+
return messages

iclaw/completer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"/status",
1616
"/help",
1717
"/clear",
18+
"/compact",
1819
".exit",
1920
]
2021

iclaw/main.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from iclaw import http
1010
from iclaw import log
1111
from iclaw.at_mention import resolve_at_mentions
12+
from iclaw.commands.compact import handle_compact_command
1213
from iclaw.commands.log import handle_log_command
1314
from iclaw.commands.model import handle_model_command, handle_model_provider_command
1415
from iclaw.commands.proxy import handle_ca_bundle_command, handle_proxy_command
@@ -38,6 +39,7 @@
3839
("/log", "Set log verbosity (usage: /log [verbose|info])"),
3940
("/copy", "Copy last Copilot response to clipboard"),
4041
("/clear", "Clear conversation history"),
42+
("/compact", "Compact conversation history using LLM"),
4143
("/status", "Show current settings"),
4244
("/help", "Show available commands"),
4345
(".exit", "Quit"),
@@ -222,6 +224,11 @@ def main():
222224
last_reply = None
223225
print("Conversation history cleared.")
224226
continue
227+
if user_input == "/compact":
228+
messages = handle_compact_command(
229+
messages, chat, copilot_token, current_model
230+
)
231+
continue
225232

226233
if not copilot_token:
227234
print("Not authenticated. Type /provider_model first.", file=sys.stderr)

0 commit comments

Comments
 (0)