feat:拆分HFC组件,为插件做准备
This commit is contained in:
165
src/chat/focus_chat/planners/action_factory.py
Normal file
165
src/chat/focus_chat/planners/action_factory.py
Normal file
@@ -0,0 +1,165 @@
|
||||
from typing import Dict, List, Optional, Callable, Coroutine, Type
|
||||
from src.chat.focus_chat.planners.actions.base_action import BaseAction
|
||||
from src.chat.focus_chat.planners.actions.reply_action import ReplyAction
|
||||
from src.chat.focus_chat.planners.actions.no_reply_action import NoReplyAction
|
||||
from src.chat.heart_flow.observation.observation import Observation
|
||||
from src.chat.focus_chat.expressors.default_expressor import DefaultExpressor
|
||||
from src.chat.message_receive.chat_stream import ChatStream
|
||||
from src.chat.focus_chat.heartFC_Cycleinfo import CycleDetail
|
||||
from src.common.logger_manager import get_logger
|
||||
|
||||
logger = get_logger("action_factory")
|
||||
|
||||
|
||||
class ActionFactory:
|
||||
"""
|
||||
动作工厂类,用于创建各种类型的动作处理器
|
||||
"""
|
||||
|
||||
# 注册的动作处理器类映射
|
||||
_action_handlers: Dict[str, Type[BaseAction]] = {
|
||||
"reply": ReplyAction,
|
||||
"no_reply": NoReplyAction,
|
||||
}
|
||||
|
||||
# 可用动作集定义(原ActionManager.DEFAULT_ACTIONS)
|
||||
DEFAULT_ACTIONS: Dict[str, str] = {
|
||||
"no_reply": "不操作,继续浏览",
|
||||
"reply": "表达想法,可以只包含文本、表情或两者都有",
|
||||
}
|
||||
_available_actions: Dict[str, str] = DEFAULT_ACTIONS.copy()
|
||||
_original_actions_backup: Optional[Dict[str, str]] = None
|
||||
|
||||
@classmethod
|
||||
def register_action_handler(cls, action_name: str, handler_class: Type[BaseAction]) -> None:
|
||||
"""
|
||||
注册新的动作处理器类
|
||||
|
||||
Args:
|
||||
action_name: 动作名称
|
||||
handler_class: 处理器类,必须是BaseAction的子类
|
||||
"""
|
||||
if not issubclass(handler_class, BaseAction):
|
||||
raise TypeError(f"{handler_class.__name__} 不是 BaseAction 的子类")
|
||||
|
||||
cls._action_handlers[action_name] = handler_class
|
||||
logger.info(f"已注册动作处理器: {action_name} -> {handler_class.__name__}")
|
||||
|
||||
@classmethod
|
||||
def create_action(
|
||||
cls,
|
||||
action_name: str,
|
||||
action_data: dict,
|
||||
reasoning: str,
|
||||
cycle_timers: dict,
|
||||
thinking_id: str,
|
||||
observations: List[Observation],
|
||||
expressor: DefaultExpressor,
|
||||
chat_stream: ChatStream,
|
||||
current_cycle: CycleDetail,
|
||||
log_prefix: str,
|
||||
on_consecutive_no_reply_callback: Callable[[], Coroutine[None, None, None]],
|
||||
total_no_reply_count: int = 0,
|
||||
total_waiting_time: float = 0.0,
|
||||
shutting_down: bool = False,
|
||||
) -> Optional[BaseAction]:
|
||||
"""
|
||||
创建动作处理器实例
|
||||
|
||||
Args:
|
||||
action_name: 动作名称
|
||||
action_data: 动作数据
|
||||
reasoning: 执行理由
|
||||
cycle_timers: 计时器字典
|
||||
thinking_id: 思考ID
|
||||
observations: 观察列表
|
||||
expressor: 表达器
|
||||
chat_stream: 聊天流
|
||||
current_cycle: 当前循环信息
|
||||
log_prefix: 日志前缀
|
||||
on_consecutive_no_reply_callback: 连续不回复回调
|
||||
total_no_reply_count: 连续不回复计数
|
||||
total_waiting_time: 累计等待时间
|
||||
shutting_down: 是否正在关闭
|
||||
|
||||
Returns:
|
||||
Optional[BaseAction]: 创建的动作处理器实例,如果动作名称未注册则返回None
|
||||
"""
|
||||
handler_class = cls._action_handlers.get(action_name)
|
||||
if not handler_class:
|
||||
logger.warning(f"未注册的动作类型: {action_name}")
|
||||
return None
|
||||
|
||||
try:
|
||||
if action_name == "reply":
|
||||
return handler_class(
|
||||
action_name=action_name,
|
||||
action_data=action_data,
|
||||
reasoning=reasoning,
|
||||
cycle_timers=cycle_timers,
|
||||
thinking_id=thinking_id,
|
||||
observations=observations,
|
||||
expressor=expressor,
|
||||
chat_stream=chat_stream,
|
||||
current_cycle=current_cycle,
|
||||
log_prefix=log_prefix,
|
||||
)
|
||||
elif action_name == "no_reply":
|
||||
return handler_class(
|
||||
action_name=action_name,
|
||||
action_data=action_data,
|
||||
reasoning=reasoning,
|
||||
cycle_timers=cycle_timers,
|
||||
thinking_id=thinking_id,
|
||||
observations=observations,
|
||||
on_consecutive_no_reply_callback=on_consecutive_no_reply_callback,
|
||||
current_cycle=current_cycle,
|
||||
log_prefix=log_prefix,
|
||||
total_no_reply_count=total_no_reply_count,
|
||||
total_waiting_time=total_waiting_time,
|
||||
shutting_down=shutting_down,
|
||||
)
|
||||
else:
|
||||
# 对于未来可能添加的其他动作类型,可以在这里扩展
|
||||
logger.warning(f"未实现的动作处理逻辑: {action_name}")
|
||||
return None
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"创建动作处理器实例失败: {e}")
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
def get_available_actions(cls) -> Dict[str, str]:
|
||||
"""获取当前可用的动作集"""
|
||||
return cls._available_actions.copy()
|
||||
|
||||
@classmethod
|
||||
def add_action(cls, action_name: str, description: str) -> bool:
|
||||
"""添加新的动作"""
|
||||
if action_name in cls._available_actions:
|
||||
return False
|
||||
cls._available_actions[action_name] = description
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
def remove_action(cls, action_name: str) -> bool:
|
||||
"""移除指定动作"""
|
||||
if action_name not in cls._available_actions:
|
||||
return False
|
||||
del cls._available_actions[action_name]
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
def temporarily_remove_actions(cls, actions_to_remove: List[str]) -> None:
|
||||
"""临时移除指定动作,备份原始动作集"""
|
||||
if cls._original_actions_backup is None:
|
||||
cls._original_actions_backup = cls._available_actions.copy()
|
||||
for name in actions_to_remove:
|
||||
cls._available_actions.pop(name, None)
|
||||
|
||||
@classmethod
|
||||
def restore_actions(cls) -> None:
|
||||
"""恢复之前备份的原始动作集"""
|
||||
if cls._original_actions_backup is not None:
|
||||
cls._available_actions = cls._original_actions_backup.copy()
|
||||
cls._original_actions_backup = None
|
||||
72
src/chat/focus_chat/planners/action_manager.py
Normal file
72
src/chat/focus_chat/planners/action_manager.py
Normal file
@@ -0,0 +1,72 @@
|
||||
from typing import List, Optional, Dict
|
||||
|
||||
# 默认动作定义
|
||||
DEFAULT_ACTIONS = {"no_reply": "不操作,继续浏览", "reply": "表达想法,可以只包含文本、表情或两者都有"}
|
||||
|
||||
|
||||
class ActionManager:
|
||||
"""动作管理器:控制每次决策可以使用的动作"""
|
||||
|
||||
def __init__(self):
|
||||
# 初始化为新的默认动作集
|
||||
self._available_actions: Dict[str, str] = DEFAULT_ACTIONS.copy()
|
||||
self._original_actions_backup: Optional[Dict[str, str]] = None
|
||||
|
||||
def get_available_actions(self) -> Dict[str, str]:
|
||||
"""获取当前可用的动作集"""
|
||||
return self._available_actions.copy() # 返回副本以防外部修改
|
||||
|
||||
def add_action(self, action_name: str, description: str) -> bool:
|
||||
"""
|
||||
添加新的动作
|
||||
|
||||
参数:
|
||||
action_name: 动作名称
|
||||
description: 动作描述
|
||||
|
||||
返回:
|
||||
bool: 是否添加成功
|
||||
"""
|
||||
if action_name in self._available_actions:
|
||||
return False
|
||||
self._available_actions[action_name] = description
|
||||
return True
|
||||
|
||||
def remove_action(self, action_name: str) -> bool:
|
||||
"""
|
||||
移除指定动作
|
||||
|
||||
参数:
|
||||
action_name: 动作名称
|
||||
|
||||
返回:
|
||||
bool: 是否移除成功
|
||||
"""
|
||||
if action_name not in self._available_actions:
|
||||
return False
|
||||
del self._available_actions[action_name]
|
||||
return True
|
||||
|
||||
def temporarily_remove_actions(self, actions_to_remove: List[str]):
|
||||
"""
|
||||
临时移除指定的动作,备份原始动作集。
|
||||
如果已经有备份,则不重复备份。
|
||||
"""
|
||||
if self._original_actions_backup is None:
|
||||
self._original_actions_backup = self._available_actions.copy()
|
||||
|
||||
actions_actually_removed = []
|
||||
for action_name in actions_to_remove:
|
||||
if action_name in self._available_actions:
|
||||
del self._available_actions[action_name]
|
||||
actions_actually_removed.append(action_name)
|
||||
# logger.debug(f"临时移除了动作: {actions_actually_removed}") # 可选日志
|
||||
|
||||
def restore_actions(self):
|
||||
"""
|
||||
恢复之前备份的原始动作集。
|
||||
"""
|
||||
if self._original_actions_backup is not None:
|
||||
self._available_actions = self._original_actions_backup.copy()
|
||||
self._original_actions_backup = None
|
||||
# logger.debug("恢复了原始动作集") # 可选日志
|
||||
37
src/chat/focus_chat/planners/actions/base_action.py
Normal file
37
src/chat/focus_chat/planners/actions/base_action.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Tuple
|
||||
from src.common.logger_manager import get_logger
|
||||
|
||||
logger = get_logger("base_action")
|
||||
|
||||
|
||||
class BaseAction(ABC):
|
||||
"""动作处理基类接口
|
||||
|
||||
所有具体的动作处理类都应该继承这个基类,并实现handle_action方法。
|
||||
"""
|
||||
|
||||
def __init__(self, action_name: str, action_data: dict, reasoning: str, cycle_timers: dict, thinking_id: str):
|
||||
"""初始化动作处理器
|
||||
|
||||
Args:
|
||||
action_name: 动作名称
|
||||
action_data: 动作数据
|
||||
reasoning: 执行该动作的理由
|
||||
cycle_timers: 计时器字典
|
||||
thinking_id: 思考ID
|
||||
"""
|
||||
self.action_name = action_name
|
||||
self.action_data = action_data
|
||||
self.reasoning = reasoning
|
||||
self.cycle_timers = cycle_timers
|
||||
self.thinking_id = thinking_id
|
||||
|
||||
@abstractmethod
|
||||
async def handle_action(self) -> Tuple[bool, str]:
|
||||
"""处理动作的抽象方法,需要被子类实现
|
||||
|
||||
Returns:
|
||||
Tuple[bool, str]: (是否执行成功, 回复文本)
|
||||
"""
|
||||
pass
|
||||
168
src/chat/focus_chat/planners/actions/no_reply_action.py
Normal file
168
src/chat/focus_chat/planners/actions/no_reply_action.py
Normal file
@@ -0,0 +1,168 @@
|
||||
import asyncio
|
||||
import traceback
|
||||
from src.common.logger_manager import get_logger
|
||||
from src.chat.utils.timer_calculator import Timer
|
||||
from src.chat.focus_chat.planners.actions.base_action import BaseAction
|
||||
from typing import Tuple, List, Callable, Coroutine
|
||||
from src.chat.heart_flow.observation.observation import Observation
|
||||
from src.chat.heart_flow.observation.chatting_observation import ChattingObservation
|
||||
from src.chat.focus_chat.heartFC_Cycleinfo import CycleDetail
|
||||
from src.chat.focus_chat.hfc_utils import parse_thinking_id_to_timestamp
|
||||
|
||||
logger = get_logger("action_taken")
|
||||
|
||||
# 常量定义
|
||||
WAITING_TIME_THRESHOLD = 300 # 等待新消息时间阈值,单位秒
|
||||
CONSECUTIVE_NO_REPLY_THRESHOLD = 3 # 连续不回复的阈值
|
||||
|
||||
|
||||
class NoReplyAction(BaseAction):
|
||||
"""不回复动作处理类
|
||||
|
||||
处理决定不回复的动作。
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
action_name: str,
|
||||
action_data: dict,
|
||||
reasoning: str,
|
||||
cycle_timers: dict,
|
||||
thinking_id: str,
|
||||
observations: List[Observation],
|
||||
on_consecutive_no_reply_callback: Callable[[], Coroutine[None, None, None]],
|
||||
current_cycle: CycleDetail,
|
||||
log_prefix: str,
|
||||
total_no_reply_count: int = 0,
|
||||
total_waiting_time: float = 0.0,
|
||||
shutting_down: bool = False,
|
||||
):
|
||||
"""初始化不回复动作处理器
|
||||
|
||||
Args:
|
||||
action_name: 动作名称
|
||||
action_data: 动作数据
|
||||
reasoning: 执行该动作的理由
|
||||
cycle_timers: 计时器字典
|
||||
thinking_id: 思考ID
|
||||
observations: 观察列表
|
||||
on_consecutive_no_reply_callback: 连续不回复达到阈值时调用的回调函数
|
||||
current_cycle: 当前循环信息
|
||||
log_prefix: 日志前缀
|
||||
total_no_reply_count: 连续不回复计数
|
||||
total_waiting_time: 累计等待时间
|
||||
shutting_down: 是否正在关闭
|
||||
"""
|
||||
super().__init__(action_name, action_data, reasoning, cycle_timers, thinking_id)
|
||||
self.observations = observations
|
||||
self.on_consecutive_no_reply_callback = on_consecutive_no_reply_callback
|
||||
self._current_cycle = current_cycle
|
||||
self.log_prefix = log_prefix
|
||||
self.total_no_reply_count = total_no_reply_count
|
||||
self.total_waiting_time = total_waiting_time
|
||||
self._shutting_down = shutting_down
|
||||
|
||||
async def handle_action(self) -> Tuple[bool, str]:
|
||||
"""
|
||||
处理不回复的情况
|
||||
|
||||
工作流程:
|
||||
1. 等待新消息、超时或关闭信号
|
||||
2. 根据等待结果更新连续不回复计数
|
||||
3. 如果达到阈值,触发回调
|
||||
|
||||
Returns:
|
||||
Tuple[bool, str]: (是否执行成功, 空字符串)
|
||||
"""
|
||||
logger.info(f"{self.log_prefix} 决定不回复: {self.reasoning}")
|
||||
|
||||
observation = self.observations[0] if self.observations else None
|
||||
|
||||
try:
|
||||
with Timer("等待新消息", self.cycle_timers):
|
||||
# 等待新消息、超时或关闭信号,并获取结果
|
||||
await self._wait_for_new_message(observation, self.thinking_id, self.log_prefix)
|
||||
# 从计时器获取实际等待时间
|
||||
current_waiting = self.cycle_timers.get("等待新消息", 0.0)
|
||||
|
||||
if not self._shutting_down:
|
||||
self.total_no_reply_count += 1
|
||||
self.total_waiting_time += current_waiting # 累加等待时间
|
||||
logger.debug(
|
||||
f"{self.log_prefix} 连续不回复计数增加: {self.total_no_reply_count}/{CONSECUTIVE_NO_REPLY_THRESHOLD}, "
|
||||
f"本次等待: {current_waiting:.2f}秒, 累计等待: {self.total_waiting_time:.2f}秒"
|
||||
)
|
||||
|
||||
# 检查是否同时达到次数和时间阈值
|
||||
time_threshold = 0.66 * WAITING_TIME_THRESHOLD * CONSECUTIVE_NO_REPLY_THRESHOLD
|
||||
if (
|
||||
self.total_no_reply_count >= CONSECUTIVE_NO_REPLY_THRESHOLD
|
||||
and self.total_waiting_time >= time_threshold
|
||||
):
|
||||
logger.info(
|
||||
f"{self.log_prefix} 连续不回复达到阈值 ({self.total_no_reply_count}次) "
|
||||
f"且累计等待时间达到 {self.total_waiting_time:.2f}秒 (阈值 {time_threshold}秒),"
|
||||
f"调用回调请求状态转换"
|
||||
)
|
||||
# 调用回调。注意:这里不重置计数器和时间,依赖回调函数成功改变状态来隐式重置上下文。
|
||||
await self.on_consecutive_no_reply_callback()
|
||||
elif self.total_no_reply_count >= CONSECUTIVE_NO_REPLY_THRESHOLD:
|
||||
# 仅次数达到阈值,但时间未达到
|
||||
logger.debug(
|
||||
f"{self.log_prefix} 连续不回复次数达到阈值 ({self.total_no_reply_count}次) "
|
||||
f"但累计等待时间 {self.total_waiting_time:.2f}秒 未达到时间阈值 ({time_threshold}秒),暂不调用回调"
|
||||
)
|
||||
# else: 次数和时间都未达到阈值,不做处理
|
||||
|
||||
return True, "" # 不回复动作没有回复文本
|
||||
|
||||
except asyncio.CancelledError:
|
||||
logger.info(f"{self.log_prefix} 处理 'no_reply' 时等待被中断 (CancelledError)")
|
||||
raise
|
||||
except Exception as e: # 捕获调用管理器或其他地方可能发生的错误
|
||||
logger.error(f"{self.log_prefix} 处理 'no_reply' 时发生错误: {e}")
|
||||
logger.error(traceback.format_exc())
|
||||
return False, ""
|
||||
|
||||
async def _wait_for_new_message(self, observation: ChattingObservation, thinking_id: str, log_prefix: str) -> bool:
|
||||
"""
|
||||
等待新消息 或 检测到关闭信号
|
||||
|
||||
参数:
|
||||
observation: 观察实例
|
||||
thinking_id: 思考ID
|
||||
log_prefix: 日志前缀
|
||||
|
||||
返回:
|
||||
bool: 是否检测到新消息 (如果因关闭信号退出则返回 False)
|
||||
"""
|
||||
wait_start_time = asyncio.get_event_loop().time()
|
||||
while True:
|
||||
# --- 在每次循环开始时检查关闭标志 ---
|
||||
if self._shutting_down:
|
||||
logger.info(f"{log_prefix} 等待新消息时检测到关闭信号,中断等待。")
|
||||
return False # 表示因为关闭而退出
|
||||
# -----------------------------------
|
||||
|
||||
thinking_id_timestamp = parse_thinking_id_to_timestamp(thinking_id)
|
||||
|
||||
# 检查新消息
|
||||
if await observation.has_new_messages_since(thinking_id_timestamp):
|
||||
logger.info(f"{log_prefix} 检测到新消息")
|
||||
return True
|
||||
|
||||
# 检查超时 (放在检查新消息和关闭之后)
|
||||
if asyncio.get_event_loop().time() - wait_start_time > WAITING_TIME_THRESHOLD:
|
||||
logger.warning(f"{log_prefix} 等待新消息超时({WAITING_TIME_THRESHOLD}秒)")
|
||||
return False
|
||||
|
||||
try:
|
||||
# 短暂休眠,让其他任务有机会运行,并能更快响应取消或关闭
|
||||
await asyncio.sleep(0.5) # 缩短休眠时间
|
||||
except asyncio.CancelledError:
|
||||
# 如果在休眠时被取消,再次检查关闭标志
|
||||
# 如果是正常关闭,则不需要警告
|
||||
if not self._shutting_down:
|
||||
logger.warning(f"{log_prefix} _wait_for_new_message 的休眠被意外取消")
|
||||
# 无论如何,重新抛出异常,让上层处理
|
||||
raise
|
||||
104
src/chat/focus_chat/planners/actions/reply_action.py
Normal file
104
src/chat/focus_chat/planners/actions/reply_action.py
Normal file
@@ -0,0 +1,104 @@
|
||||
from src.common.logger_manager import get_logger
|
||||
from src.chat.heart_flow.observation.chatting_observation import ChattingObservation
|
||||
from src.chat.focus_chat.hfc_utils import create_empty_anchor_message
|
||||
from src.chat.focus_chat.planners.actions.base_action import BaseAction
|
||||
from typing import Tuple, List
|
||||
from src.chat.focus_chat.heartFC_Cycleinfo import CycleDetail
|
||||
from src.chat.message_receive.chat_stream import ChatStream
|
||||
from src.chat.heart_flow.observation.observation import Observation
|
||||
from src.chat.focus_chat.expressors.default_expressor import DefaultExpressor
|
||||
|
||||
logger = get_logger("action_taken")
|
||||
|
||||
|
||||
class ReplyAction(BaseAction):
|
||||
"""回复动作处理类
|
||||
|
||||
处理发送回复消息的动作,包括文本和表情。
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
action_name: str,
|
||||
action_data: dict,
|
||||
reasoning: str,
|
||||
cycle_timers: dict,
|
||||
thinking_id: str,
|
||||
observations: List[Observation],
|
||||
expressor: DefaultExpressor,
|
||||
chat_stream: ChatStream,
|
||||
current_cycle: CycleDetail,
|
||||
log_prefix: str,
|
||||
):
|
||||
"""初始化回复动作处理器
|
||||
|
||||
Args:
|
||||
action_name: 动作名称
|
||||
action_data: 动作数据
|
||||
reasoning: 执行该动作的理由
|
||||
cycle_timers: 计时器字典
|
||||
thinking_id: 思考ID
|
||||
observations: 观察列表
|
||||
expressor: 表达器
|
||||
chat_stream: 聊天流
|
||||
current_cycle: 当前循环信息
|
||||
log_prefix: 日志前缀
|
||||
"""
|
||||
super().__init__(action_name, action_data, reasoning, cycle_timers, thinking_id)
|
||||
self.observations = observations
|
||||
self.expressor = expressor
|
||||
self.chat_stream = chat_stream
|
||||
self._current_cycle = current_cycle
|
||||
self.log_prefix = log_prefix
|
||||
self.total_no_reply_count = 0
|
||||
self.total_waiting_time = 0.0
|
||||
|
||||
async def handle_action(self) -> Tuple[bool, str]:
|
||||
"""
|
||||
处理统一的回复动作 - 可包含文本和表情,顺序任意
|
||||
|
||||
reply_data格式:
|
||||
{
|
||||
"text": "你好啊" # 文本内容列表(可选)
|
||||
"target": "锚定消息", # 锚定消息的文本内容
|
||||
"emojis": "微笑" # 表情关键词列表(可选)
|
||||
}
|
||||
|
||||
Returns:
|
||||
Tuple[bool, str]: (是否执行成功, 回复文本)
|
||||
"""
|
||||
# 重置连续不回复计数器
|
||||
self.total_no_reply_count = 0
|
||||
self.total_waiting_time = 0.0
|
||||
|
||||
# 从聊天观察获取锚定消息
|
||||
observations: ChattingObservation = self.observations[0]
|
||||
anchor_message = observations.serch_message_by_text(self.action_data["target"])
|
||||
|
||||
# 如果没有找到锚点消息,创建一个占位符
|
||||
if not anchor_message:
|
||||
logger.info(f"{self.log_prefix} 未找到锚点消息,创建占位符")
|
||||
anchor_message = await create_empty_anchor_message(
|
||||
self.chat_stream.platform, self.chat_stream.group_info, self.chat_stream
|
||||
)
|
||||
else:
|
||||
anchor_message.update_chat_stream(self.chat_stream)
|
||||
|
||||
success, reply_set = await self.expressor.deal_reply(
|
||||
cycle_timers=self.cycle_timers,
|
||||
action_data=self.action_data,
|
||||
anchor_message=anchor_message,
|
||||
reasoning=self.reasoning,
|
||||
thinking_id=self.thinking_id,
|
||||
)
|
||||
|
||||
reply_text = ""
|
||||
for reply in reply_set:
|
||||
type = reply[0]
|
||||
data = reply[1]
|
||||
if type == "text":
|
||||
reply_text += data
|
||||
elif type == "emoji":
|
||||
reply_text += data
|
||||
|
||||
return success, reply_text
|
||||
156
src/chat/focus_chat/planners/planner.py
Normal file
156
src/chat/focus_chat/planners/planner.py
Normal file
@@ -0,0 +1,156 @@
|
||||
import json # <--- 确保导入 json
|
||||
import traceback
|
||||
from typing import List, Dict, Any
|
||||
from rich.traceback import install
|
||||
from src.chat.models.utils_model import LLMRequest
|
||||
from src.config.config import global_config
|
||||
from src.chat.focus_chat.heartflow_prompt_builder import prompt_builder
|
||||
from src.chat.focus_chat.info.info_base import InfoBase
|
||||
from src.chat.focus_chat.info.obs_info import ObsInfo
|
||||
from src.chat.focus_chat.info.cycle_info import CycleInfo
|
||||
from src.chat.focus_chat.info.mind_info import MindInfo
|
||||
from src.chat.focus_chat.info.structured_info import StructuredInfo
|
||||
from src.chat.focus_chat.planners.action_factory import ActionFactory
|
||||
from src.common.logger_manager import get_logger
|
||||
|
||||
logger = get_logger("planner")
|
||||
|
||||
install(extra_lines=3)
|
||||
|
||||
|
||||
class ActionPlanner:
|
||||
def __init__(self, log_prefix: str):
|
||||
self.log_prefix = log_prefix
|
||||
# LLM规划器配置
|
||||
self.planner_llm = LLMRequest(
|
||||
model=global_config.llm_plan,
|
||||
max_tokens=1000,
|
||||
request_type="action_planning", # 用于动作规划
|
||||
)
|
||||
|
||||
async def plan(self, all_plan_info: List[InfoBase], cycle_timers: dict) -> Dict[str, Any]:
|
||||
"""
|
||||
规划器 (Planner): 使用LLM根据上下文决定做出什么动作。
|
||||
|
||||
参数:
|
||||
all_plan_info: 所有计划信息
|
||||
cycle_timers: 计时器字典
|
||||
"""
|
||||
|
||||
action = "no_reply" # 默认动作
|
||||
reasoning = "规划器初始化默认"
|
||||
|
||||
try:
|
||||
# 获取观察信息
|
||||
for info in all_plan_info:
|
||||
if isinstance(info, ObsInfo):
|
||||
logger.debug(f"{self.log_prefix} 观察信息: {info}")
|
||||
observed_messages = info.get_talking_message()
|
||||
observed_messages_str = info.get_talking_message_str_truncate()
|
||||
chat_type = info.get_chat_type()
|
||||
if chat_type == "group":
|
||||
is_group_chat = True
|
||||
else:
|
||||
is_group_chat = False
|
||||
elif isinstance(info, MindInfo):
|
||||
logger.debug(f"{self.log_prefix} 思维信息: {info}")
|
||||
current_mind = info.get_current_mind()
|
||||
elif isinstance(info, CycleInfo):
|
||||
logger.debug(f"{self.log_prefix} 循环信息: {info}")
|
||||
cycle_info = info.get_observe_info()
|
||||
elif isinstance(info, StructuredInfo):
|
||||
logger.debug(f"{self.log_prefix} 结构化信息: {info}")
|
||||
structured_info = info.get_data()
|
||||
|
||||
# 获取我们将传递给 prompt 构建器和用于验证的当前可用动作
|
||||
current_available_actions = ActionFactory.get_available_actions()
|
||||
|
||||
# --- 构建提示词 (调用修改后的 PromptBuilder 方法) ---
|
||||
prompt = await prompt_builder.build_planner_prompt(
|
||||
is_group_chat=is_group_chat, # <-- Pass HFC state
|
||||
chat_target_info=None,
|
||||
observed_messages_str=observed_messages_str, # <-- Pass local variable
|
||||
current_mind=current_mind, # <-- Pass argument
|
||||
structured_info=structured_info, # <-- Pass SubMind info
|
||||
current_available_actions=current_available_actions, # <-- Pass determined actions
|
||||
cycle_info=cycle_info, # <-- Pass cycle info
|
||||
)
|
||||
|
||||
# --- 调用 LLM (普通文本生成) ---
|
||||
llm_content = None
|
||||
try:
|
||||
llm_content, _, _ = await self.planner_llm.generate_response(prompt=prompt)
|
||||
logger.debug(f"{self.log_prefix}[Planner] LLM 原始 JSON 响应 (预期): {llm_content}")
|
||||
except Exception as req_e:
|
||||
logger.error(f"{self.log_prefix}[Planner] LLM 请求执行失败: {req_e}")
|
||||
reasoning = f"LLM 请求失败,你的模型出现问题: {req_e}"
|
||||
action = "no_reply"
|
||||
|
||||
if llm_content:
|
||||
try:
|
||||
# 尝试去除可能的 markdown 代码块标记
|
||||
cleaned_content = (
|
||||
llm_content.strip().removeprefix("```json").removeprefix("```").removesuffix("```").strip()
|
||||
)
|
||||
if not cleaned_content:
|
||||
raise json.JSONDecodeError("Cleaned content is empty", cleaned_content, 0)
|
||||
parsed_json = json.loads(cleaned_content)
|
||||
|
||||
# 提取决策,提供默认值
|
||||
extracted_action = parsed_json.get("action", "no_reply")
|
||||
extracted_reasoning = parsed_json.get("reasoning", "LLM未提供理由")
|
||||
|
||||
# 新的reply格式
|
||||
if extracted_action == "reply":
|
||||
action_data = {
|
||||
"text": parsed_json.get("text", []),
|
||||
"emojis": parsed_json.get("emojis", []),
|
||||
"target": parsed_json.get("target", ""),
|
||||
}
|
||||
else:
|
||||
action_data = {} # 其他动作可能不需要额外数据
|
||||
|
||||
if extracted_action not in current_available_actions:
|
||||
logger.warning(
|
||||
f"{self.log_prefix}LLM 返回了当前不可用或无效的动作: '{extracted_action}' (可用: {list(current_available_actions.keys())}),将强制使用 'no_reply'"
|
||||
)
|
||||
action = "no_reply"
|
||||
reasoning = f"LLM 返回了当前不可用的动作 '{extracted_action}' (可用: {list(current_available_actions.keys())})。原始理由: {extracted_reasoning}"
|
||||
else:
|
||||
# 动作有效且可用
|
||||
action = extracted_action
|
||||
reasoning = extracted_reasoning
|
||||
|
||||
except Exception as json_e:
|
||||
logger.warning(
|
||||
f"{self.log_prefix}解析LLM响应JSON失败,模型返回不标准: {json_e}. LLM原始输出: '{llm_content}'"
|
||||
)
|
||||
reasoning = f"解析LLM响应JSON失败: {json_e}. 将使用默认动作 'no_reply'."
|
||||
action = "no_reply"
|
||||
|
||||
except Exception as outer_e:
|
||||
logger.error(f"{self.log_prefix}Planner 处理过程中发生意外错误,规划失败,将执行 no_reply: {outer_e}")
|
||||
traceback.print_exc()
|
||||
action = "no_reply" # 发生未知错误,标记为 error 动作
|
||||
reasoning = f"Planner 内部处理错误: {outer_e}"
|
||||
|
||||
logger.debug(
|
||||
f"{self.log_prefix}规划器Prompt:\n{prompt}\n\n决策动作:{action},\n动作信息: '{action_data}'\n理由: {reasoning}"
|
||||
)
|
||||
|
||||
# 恢复原始动作集
|
||||
ActionFactory.restore_actions()
|
||||
logger.debug(
|
||||
f"{self.log_prefix}恢复了原始动作集, 当前可用: {list(ActionFactory.get_available_actions().keys())}"
|
||||
)
|
||||
|
||||
action_result = {"action_type": action, "action_data": action_data, "reasoning": reasoning}
|
||||
|
||||
plan_result = {
|
||||
"action_result": action_result,
|
||||
"current_mind": current_mind,
|
||||
"observed_messages": observed_messages,
|
||||
}
|
||||
|
||||
# 返回结果字典
|
||||
return plan_result
|
||||
Reference in New Issue
Block a user