refactor(action_manager): 简化回复处理逻辑,移除多余的参与者信息获取

This commit is contained in:
Windpicker-owo
2025-11-12 16:23:39 +08:00
parent 80736a0deb
commit 62c3732200
3 changed files with 13 additions and 58 deletions

View File

@@ -301,7 +301,7 @@ class ChatterActionManager:
async def _after_reply():
# 发送并存储回复
loop_info, reply_text, cycle_timers_reply = await self._send_and_store_reply(
reply_text, cycle_timers_reply = await self._send_and_store_reply(
chat_stream,
response_set,
asyncio.get_event_loop().time(),
@@ -313,14 +313,14 @@ class ChatterActionManager:
)
# 记录回复动作到目标消息
asyncio.create_task(self._record_action_to_message(chat_stream, action_name, target_message, action_data))
await self._record_action_to_message(chat_stream, action_name, target_message, action_data)
# 回复成功,重置打断计数
await self._reset_interruption_count_after_action(chat_stream.stream_id)
return loop_info, reply_text, cycle_timers_reply
loop_info, reply_text, _ = await _after_reply()
return {"action_type": action_name, "success": True, "reply_text": reply_text, "loop_info": loop_info}
return reply_text
asyncio.create_task(_after_reply())
return {"action_type": action_name, "success": True}
except Exception as e:
logger.error(f"{log_prefix} 执行动作时出错: {e}")
@@ -477,7 +477,7 @@ class ChatterActionManager:
thinking_id,
actions,
should_quote_reply: bool | None = None,
) -> tuple[dict[str, Any], str, dict[str, float]]:
) -> tuple[str, dict[str, float]]:
"""
发送并存储回复信息
@@ -540,20 +540,7 @@ class ChatterActionManager:
action_name="reply",
)
# 构建循环信息
loop_info: dict[str, Any] = {
"loop_plan_info": {
"action_result": actions,
},
"loop_action_info": {
"action_taken": True,
"reply_text": reply_text,
"command": "",
"taken_time": time.time(),
},
}
return loop_info, reply_text, cycle_timers
return reply_text, cycle_timers
async def send_response(
self, chat_stream, reply_set, thinking_start_time, message_data, should_quote_reply: bool | None = None

View File

@@ -592,35 +592,6 @@ class DefaultReplyer:
if user_info_obj:
sender_name = getattr(user_info_obj, "user_nickname", "") or getattr(user_info_obj, "user_cardname", "")
# 获取参与者信息
participants = []
try:
# 尝试从聊天流中获取参与者信息
if hasattr(stream, "context_manager"):
history_manager = stream.context_manager
# 获取最近的参与者列表
recent_records = history_manager.get_memory_chat_history(
user_id=getattr(stream.user_info, "user_id", ""),
count=10,
memory_types=["chat_message", "system_message"]
)
# 提取唯一的参与者名称
for record in recent_records[:5]: # 最近5条记录
content = record.get("content", {})
participant = content.get("participant_name")
if participant and participant not in participants:
participants.append(participant)
# 如果消息包含发送者信息,也添加到参与者列表
if content.get("sender_name") and content.get("sender_name") not in participants:
participants.append(content.get("sender_name"))
except Exception as e:
logger.debug(f"获取参与者信息失败: {e}")
# 如果发送者不在参与者列表中,添加进去
if sender_name and sender_name not in participants:
participants.insert(0, sender_name)
# 格式化聊天历史为更友好的格式
formatted_history = ""
if chat_history:
@@ -632,7 +603,6 @@ class DefaultReplyer:
query_context = {
"chat_history": formatted_history,
"sender": sender_name,
"participants": participants,
}
# 使用记忆管理器的智能检索(多查询策略)

View File

@@ -917,7 +917,6 @@ class MemoryTools:
)
# 获取上下文信息
participants = context.get("participants", []) if context else []
chat_history = context.get("chat_history", "") if context else ""
sender = context.get("sender", "") if context else ""
@@ -925,18 +924,17 @@ class MemoryTools:
recent_chat = ""
if chat_history:
lines = chat_history.strip().split("\n")
# 取最近5条消息
recent_lines = lines[-5:] if len(lines) > 5 else lines
# 取最近10条消息
recent_lines = lines[-10:] if len(lines) > 10 else lines
recent_chat = "\n".join(recent_lines)
import datetime
current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
prompt = f"""基于聊天上下文为查询生成3-5个不同角度的搜索语句并识别查询意图对应的记忆类型JSON格式
**当前查询** {query}
**发送者:** {sender if sender else '未知'}
**参与者:** {', '.join(participants) if participants else ''}
**当前时间:** {__import__('datetime').datetime.now().__str__()}
**当前时间** {current_time}
**最近聊天记录(最近5条):**
**最近聊天记录(最近10条):**
{recent_chat if recent_chat else '无聊天历史'}
---