From 62c373220047c802d9e12605d5563dd4e037abe8 Mon Sep 17 00:00:00 2001 From: Windpicker-owo <3431391539@qq.com> Date: Wed, 12 Nov 2025 16:23:39 +0800 Subject: [PATCH] =?UTF-8?q?refactor(action=5Fmanager):=20=E7=AE=80?= =?UTF-8?q?=E5=8C=96=E5=9B=9E=E5=A4=8D=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= =?UTF-8?q?=EF=BC=8C=E7=A7=BB=E9=99=A4=E5=A4=9A=E4=BD=99=E7=9A=84=E5=8F=82?= =?UTF-8?q?=E4=B8=8E=E8=80=85=E4=BF=A1=E6=81=AF=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/chat/planner_actions/action_manager.py | 27 +++++-------------- src/chat/replyer/default_generator.py | 30 ---------------------- src/memory_graph/tools/memory_tools.py | 14 +++++----- 3 files changed, 13 insertions(+), 58 deletions(-) diff --git a/src/chat/planner_actions/action_manager.py b/src/chat/planner_actions/action_manager.py index a6f114b2c..cb235c03e 100644 --- a/src/chat/planner_actions/action_manager.py +++ b/src/chat/planner_actions/action_manager.py @@ -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 diff --git a/src/chat/replyer/default_generator.py b/src/chat/replyer/default_generator.py index 2b8472148..c6a5bf0e3 100644 --- a/src/chat/replyer/default_generator.py +++ b/src/chat/replyer/default_generator.py @@ -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, } # 使用记忆管理器的智能检索(多查询策略) diff --git a/src/memory_graph/tools/memory_tools.py b/src/memory_graph/tools/memory_tools.py index 33618dd8c..20a9022f3 100644 --- a/src/memory_graph/tools/memory_tools.py +++ b/src/memory_graph/tools/memory_tools.py @@ -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 '无聊天历史'} ---