feat:压缩早期assitant信息
This commit is contained in:
@@ -880,10 +880,17 @@ class MaisakaChatLoopService:
|
|||||||
|
|
||||||
selected_indices.reverse()
|
selected_indices.reverse()
|
||||||
selected_history = [chat_history[index] for index in selected_indices]
|
selected_history = [chat_history[index] for index in selected_indices]
|
||||||
|
selected_history, hidden_assistant_count = MaisakaChatLoopService._hide_early_assistant_messages(selected_history)
|
||||||
selected_history = MaisakaChatLoopService._drop_leading_orphan_tool_results(selected_history)
|
selected_history = MaisakaChatLoopService._drop_leading_orphan_tool_results(selected_history)
|
||||||
|
selection_reason = (
|
||||||
|
f"上下文裁剪:最近 {effective_context_size} 条 user/assistant 消息,"
|
||||||
|
f"实际发送 {len(selected_history)} 条"
|
||||||
|
)
|
||||||
|
if hidden_assistant_count > 0:
|
||||||
|
selection_reason += f",已隐藏最早 {hidden_assistant_count} 条 assistant 消息"
|
||||||
return (
|
return (
|
||||||
selected_history,
|
selected_history,
|
||||||
f"???????? {effective_context_size} ? user/assistant??????????? {len(selected_history)} ?",
|
selection_reason,
|
||||||
)
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@@ -917,6 +924,7 @@ class MaisakaChatLoopService:
|
|||||||
|
|
||||||
selected_indices.reverse()
|
selected_indices.reverse()
|
||||||
selected_history = [chat_history[index] for index in selected_indices]
|
selected_history = [chat_history[index] for index in selected_indices]
|
||||||
|
selected_history, hidden_assistant_count = MaisakaChatLoopService._hide_early_assistant_messages(selected_history)
|
||||||
selected_history = MaisakaChatLoopService._drop_leading_orphan_tool_results(selected_history)
|
selected_history = MaisakaChatLoopService._drop_leading_orphan_tool_results(selected_history)
|
||||||
return (
|
return (
|
||||||
selected_history,
|
selected_history,
|
||||||
@@ -926,6 +934,41 @@ class MaisakaChatLoopService:
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@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
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _drop_leading_orphan_tool_results(
|
def _drop_leading_orphan_tool_results(
|
||||||
selected_history: List[LLMContextMessage],
|
selected_history: List[LLMContextMessage],
|
||||||
|
|||||||
@@ -170,8 +170,11 @@ class PromptCLIVisualizer:
|
|||||||
path_result = cls._build_image_file_link(image_format, image_base64)
|
path_result = cls._build_image_file_link(image_format, image_base64)
|
||||||
if path_result is not None:
|
if path_result is not None:
|
||||||
file_uri, file_path = path_result
|
file_uri, file_path = path_result
|
||||||
preview_parts.append(Text.from_markup(f"\n[link={file_uri}]点击打开图片[/link]", style="cyan"))
|
preview_parts: List[RenderableType] = [
|
||||||
preview_parts.append(Text(f"\n{file_path}", style="dim"))
|
Text(f"图片格式 image/{normalized_format} {size_text} 路径:{file_path}", style="magenta")
|
||||||
|
]
|
||||||
|
|
||||||
|
preview_parts.append(Text.from_markup(f"[link={file_uri}]点击打开图片[/link]", style="cyan"))
|
||||||
|
|
||||||
return Panel(
|
return Panel(
|
||||||
Group(*preview_parts),
|
Group(*preview_parts),
|
||||||
|
|||||||
Reference in New Issue
Block a user