feat:精简表达选择,优化replyer表现,优化缓存命中率

This commit is contained in:
SengokuCola
2026-04-24 21:27:45 +08:00
parent 3b6d30cd5e
commit 7719353984
11 changed files with 188 additions and 142 deletions

View File

@@ -90,7 +90,7 @@ def _append_reply_component(builder: MessageBuilder, component: ReplyComponent)
if not target_message_id:
return False
builder.add_text_content(f"[引用]quote_id={target_message_id}")
builder.add_text_content(f"[引用消息]{target_message_id}")
return True
@@ -167,7 +167,7 @@ def _render_component_for_prompt(component: StandardMessageComponents) -> str:
if target_content:
return f"[回复消息: {target_content}]"
target_message_id = component.target_message_id.strip()
return f"[引用]quote_id={target_message_id}" if target_message_id else "[回复消息]"
return f"[引用消息]{target_message_id}" if target_message_id else "[回复消息]"
if isinstance(component, ForwardNodeComponent):
return _build_forward_preview_block(component)

View File

@@ -2,10 +2,9 @@
from dataclasses import dataclass
from .context_messages import AssistantMessage, LLMContextMessage, ToolResultMessage
from .context_messages import AssistantMessage, LLMContextMessage
from .history_utils import drop_leading_orphan_tool_results, drop_orphan_tool_results, normalize_tool_result_order
TIMING_HISTORY_TOOL_NAMES = {"continue", "finish", "no_reply", "wait"}
EARLY_TRIM_RATIO = 0.2
@@ -27,7 +26,6 @@ def process_chat_history_after_cycle(
"""在每轮结束后统一执行历史裁切与清理。"""
processed_history = list(chat_history)
removed_timing_tool_count = _remove_early_timing_tool_records(processed_history)
removed_assistant_thought_count = _remove_early_assistant_thoughts(processed_history)
processed_history, orphan_removed_count = drop_orphan_tool_results(processed_history)
@@ -45,8 +43,7 @@ def process_chat_history_after_cycle(
removed_overflow_count += leading_orphan_removed_count
remaining_context_count = sum(1 for message in processed_history if message.count_in_context)
removed_count = (
removed_timing_tool_count
+ removed_assistant_thought_count
removed_assistant_thought_count
+ orphan_removed_count
+ removed_overflow_count
)
@@ -59,41 +56,6 @@ def process_chat_history_after_cycle(
)
def _remove_early_timing_tool_records(chat_history: list[LLMContextMessage]) -> int:
"""移除最早 20% 的门控/结束类工具链记录。"""
candidate_assistant_indexes = [
index
for index, message in enumerate(chat_history)
if _is_timing_tool_assistant_message(message)
]
remove_count = int(len(candidate_assistant_indexes) * EARLY_TRIM_RATIO)
if remove_count <= 0:
return 0
removed_indexes = set(candidate_assistant_indexes[:remove_count])
removed_tool_call_ids = {
tool_call.call_id
for index in removed_indexes
for tool_call in chat_history[index].tool_calls
if tool_call.call_id
}
filtered_history: list[LLMContextMessage] = []
removed_total = 0
for index, message in enumerate(chat_history):
if index in removed_indexes:
removed_total += 1
continue
if isinstance(message, ToolResultMessage) and message.tool_call_id in removed_tool_call_ids:
removed_total += 1
continue
filtered_history.append(message)
chat_history[:] = filtered_history
return removed_total
def _remove_early_assistant_thoughts(chat_history: list[LLMContextMessage]) -> int:
"""移除最早 20% 的非工具 assistant 思考内容。"""
@@ -122,8 +84,3 @@ def _remove_early_assistant_thoughts(chat_history: list[LLMContextMessage]) -> i
return removed_total
def _is_timing_tool_assistant_message(message: LLMContextMessage) -> bool:
if not isinstance(message, AssistantMessage) or not message.tool_calls:
return False
return all(tool_call.func_name in TIMING_HISTORY_TOOL_NAMES for tool_call in message.tool_calls)

View File

@@ -60,11 +60,23 @@ def build_visible_text_from_sequence(message_sequence: MessageSequence) -> str:
"""从消息片段序列提取可见文本。"""
parts: list[str] = []
pending_reply_body_prefix = False
def append_visible_part(text: str) -> None:
nonlocal pending_reply_body_prefix
if not text:
return
if pending_reply_body_prefix:
parts.append(f"\n[发言内容]{text}")
pending_reply_body_prefix = False
return
parts.append(text)
for component in message_sequence.components:
if isinstance(component, TextComponent):
match = SPEAKER_PREFIX_PATTERN.match(component.text or "")
if not match:
parts.append(component.text)
append_visible_part(component.text)
continue
normalized_parts: list[str] = []
@@ -75,24 +87,25 @@ def build_visible_text_from_sequence(message_sequence: MessageSequence) -> str:
normalized_parts.append(f"[msg_id:{message_id}]")
normalized_parts.append(f"[{match.group('speaker')}]")
normalized_parts.append(match.group("content"))
parts.append("".join(normalized_parts))
append_visible_part("".join(normalized_parts))
continue
if isinstance(component, EmojiComponent):
parts.append(component.content.strip() or "[表情包]")
append_visible_part(component.content.strip() or "[表情包]")
continue
if isinstance(component, ImageComponent):
parts.append(component.content.strip() or "[图片]")
append_visible_part(component.content.strip() or "[图片]")
continue
if isinstance(component, AtComponent):
parts.append(_render_at_component_text(component))
append_visible_part(_render_at_component_text(component))
continue
if isinstance(component, ReplyComponent):
target_message_id = component.target_message_id.strip()
if target_message_id:
parts.append(f"[引用]quote_id={target_message_id}")
parts.append(f"[引用消息]{target_message_id}")
pending_reply_body_prefix = True
return "".join(parts)

View File

@@ -273,10 +273,14 @@ async def _broadcast(event: str, data: Dict[str, Any]) -> None:
for connection in websocket_manager.connections.values()
if subscription_key in connection.subscriptions
)
logger.info(
f"[诊断] _broadcast: manager_id={id(websocket_manager)} "
f"总连接={total_connections} 订阅者={subscriber_count} event={event}"
)
# The above code is using the Python logging module to log a diagnostic message. It is logging
# information about the `_broadcast` function, including the `manager_id`, `total_connections`,
# `subscriber_count`, and `event` variables. The `logger.info()` function is used to log the message
# at the INFO level.
# logger.info(
# f"[诊断] _broadcast: manager_id={id(websocket_manager)} "
# f"总连接={total_connections} 订阅者={subscriber_count} event={event}"
# )
await websocket_manager.broadcast_to_topic(
domain=MONITOR_DOMAIN,
topic=MONITOR_TOPIC,

View File

@@ -478,11 +478,11 @@ class MaisakaReasoningEngine:
)
planner_duration_ms = (time.time() - planner_started_at) * 1000
cycle_detail.time_records["planner"] = planner_duration_ms / 1000
logger.info(
f"{self._runtime.log_prefix} 规划器执行完成: "
f"回合={round_index + 1} "
f"耗时={cycle_detail.time_records['planner']:.3f}"
)
# logger.info(
# f"{self._runtime.log_prefix} 规划器执行完成: "
# f"回合={round_index + 1} "
# f"耗时={cycle_detail.time_records['planner']:.3f} 秒"
# )
reasoning_content = response.content or ""
if self._should_replace_reasoning(reasoning_content):
response.content = "我应该根据我上面思考的内容进行反思,重新思考我下一步的行动,我需要分析当前场景,对话,以及我可以使用的工具,然后直接输出我的想法"
@@ -865,7 +865,7 @@ class MaisakaReasoningEngine:
return False
similarity = self._calculate_similarity(current_content, self._last_reasoning_content)
logger.info(f"{self._runtime.log_prefix} 思考内容相似度: {similarity:.2f}")
logger.debug(f"{self._runtime.log_prefix} 思考内容相似度: {similarity:.2f}")
return similarity > 0.9
@staticmethod