feat:进一步优化缓存

This commit is contained in:
SengokuCola
2026-04-25 00:26:32 +08:00
parent 11f423b851
commit 705452793d
6 changed files with 69 additions and 88 deletions

View File

@@ -650,7 +650,6 @@ class MaisakaChatLoopService:
selected_indices.reverse()
selected_history = [filtered_history[index] for index in selected_indices]
selected_history, _ = MaisakaChatLoopService._hide_early_assistant_messages(selected_history)
selected_history, _ = drop_orphan_tool_results(selected_history)
selected_history, _ = normalize_tool_result_order(selected_history)
tool_message_count = sum(1 for message in selected_history if isinstance(message, ToolResultMessage))
@@ -709,38 +708,3 @@ class MaisakaChatLoopService:
return resolve_enable_visual_planner()
return True
@staticmethod
def _hide_early_assistant_messages(
selected_history: List[LLMContextMessage],
) -> tuple[List[LLMContextMessage], int]:
"""隐藏上下文中最早 50% 的 assistant 文本消息,但保留工具调用链路。"""
assistant_indices = [
index
for index, message in enumerate(selected_history)
if isinstance(message, AssistantMessage)
]
hidden_assistant_count = len(assistant_indices) // 2
if hidden_assistant_count <= 0:
return selected_history, 0
removed_assistant_indices = set(assistant_indices[:hidden_assistant_count])
filtered_history: List[LLMContextMessage] = []
for index, message in enumerate(selected_history):
if index in removed_assistant_indices:
if not message.tool_calls:
continue
filtered_history.append(
AssistantMessage(
content="",
timestamp=message.timestamp,
tool_calls=list(message.tool_calls),
source_kind=message.source_kind,
)
)
continue
filtered_history.append(message)
return filtered_history, hidden_assistant_count