Optimize: force aggressive tag compression before history truncation to save context

This commit is contained in:
Liang Jiaqing
2026-04-06 21:00:39 +08:00
parent 53a859058a
commit b4741a9a39

View File

@@ -14,9 +14,10 @@ mykeys = _load_mykeys()
proxy = mykeys.get("proxy", 'http://127.0.0.1:2082') proxy = mykeys.get("proxy", 'http://127.0.0.1:2082')
proxies = {"http": proxy, "https": proxy} if proxy else None proxies = {"http": proxy, "https": proxy} if proxy else None
def compress_history_tags(messages, keep_recent=10, max_len=800): def compress_history_tags(messages, keep_recent=10, max_len=800, force=False):
"""Compress <thinking>/<tool_use>/<tool_result> tags in older messages to save tokens.""" """Compress <thinking>/<tool_use>/<tool_result> tags in older messages to save tokens."""
compress_history_tags._cd = getattr(compress_history_tags, '_cd', 0) + 1 compress_history_tags._cd = getattr(compress_history_tags, '_cd', 0) + 1
if force: compress_history_tags._cd = 0
if compress_history_tags._cd % 5 != 0: return messages if compress_history_tags._cd % 5 != 0: return messages
_before = sum(len(json.dumps(m, ensure_ascii=False)) for m in messages) _before = sum(len(json.dumps(m, ensure_ascii=False)) for m in messages)
_pats = {tag: re.compile(rf'(<{tag}>)([\s\S]*?)(</{tag}>)') for tag in ('thinking', 'think', 'tool_use', 'tool_result')} _pats = {tag: re.compile(rf'(<{tag}>)([\s\S]*?)(</{tag}>)') for tag in ('thinking', 'think', 'tool_use', 'tool_result')}
@@ -60,6 +61,7 @@ def trim_messages_history(history, context_win):
cost = sum(len(json.dumps(m, ensure_ascii=False)) for m in history) cost = sum(len(json.dumps(m, ensure_ascii=False)) for m in history)
print(f'[Debug] Current context: {cost} chars, {len(history)} messages.') print(f'[Debug] Current context: {cost} chars, {len(history)} messages.')
if cost > context_win * 3: if cost > context_win * 3:
compress_history_tags(history, keep_recent=4, force=True) # trim breaks cache, so compress more btw
target = context_win * 3 * 0.6 target = context_win * 3 * 0.6
while len(history) > 5 and cost > target: while len(history) > 5 and cost > target:
history.pop(0) history.pop(0)