refactor:重构聊天状态切换模式,移除限额,精简切换逻辑
This commit is contained in:
@@ -137,11 +137,7 @@ class ActionManager:
|
||||
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]:
|
||||
"""
|
||||
@@ -156,11 +152,7 @@ class ActionManager:
|
||||
observations: 观察列表
|
||||
expressor: 表达器
|
||||
chat_stream: 聊天流
|
||||
current_cycle: 当前循环信息
|
||||
log_prefix: 日志前缀
|
||||
on_consecutive_no_reply_callback: 连续不回复回调
|
||||
total_no_reply_count: 连续不回复计数
|
||||
total_waiting_time: 累计等待时间
|
||||
shutting_down: 是否正在关闭
|
||||
|
||||
Returns:
|
||||
@@ -179,7 +171,6 @@ class ActionManager:
|
||||
try:
|
||||
# 创建动作实例
|
||||
instance = handler_class(
|
||||
action_name=action_name,
|
||||
action_data=action_data,
|
||||
reasoning=reasoning,
|
||||
cycle_timers=cycle_timers,
|
||||
@@ -187,11 +178,7 @@ class ActionManager:
|
||||
observations=observations,
|
||||
expressor=expressor,
|
||||
chat_stream=chat_stream,
|
||||
current_cycle=current_cycle,
|
||||
log_prefix=log_prefix,
|
||||
on_consecutive_no_reply_callback=on_consecutive_no_reply_callback,
|
||||
# total_no_reply_count=total_no_reply_count,
|
||||
# total_waiting_time=total_waiting_time,
|
||||
shutting_down=shutting_down,
|
||||
)
|
||||
|
||||
|
||||
108
src/chat/focus_chat/planners/actions/exit_focus_chat_action.py
Normal file
108
src/chat/focus_chat/planners/actions/exit_focus_chat_action.py
Normal file
@@ -0,0 +1,108 @@
|
||||
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, register_action
|
||||
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.heart_flow.sub_heartflow import SubHeartFlow
|
||||
from src.chat.message_receive.chat_stream import ChatStream
|
||||
from src.chat.heart_flow.heartflow import heartflow
|
||||
from src.chat.heart_flow.sub_heartflow import ChatState
|
||||
|
||||
logger = get_logger("action_taken")
|
||||
|
||||
|
||||
@register_action
|
||||
class ExitFocusChatAction(BaseAction):
|
||||
"""退出专注聊天动作处理类
|
||||
|
||||
处理决定退出专注聊天的动作。
|
||||
执行后会将所属的sub heartflow转变为normal_chat状态。
|
||||
"""
|
||||
|
||||
action_name = "exit_focus_chat"
|
||||
action_description = "退出专注聊天,转为普通聊天模式"
|
||||
action_parameters = {}
|
||||
action_require = [
|
||||
"很长时间没有回复,你决定退出专注聊天",
|
||||
"当前内容不需要持续专注关注,你决定退出专注聊天",
|
||||
"聊天内容已经完成,你决定退出专注聊天",
|
||||
]
|
||||
default = True
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
action_data: dict,
|
||||
reasoning: str,
|
||||
cycle_timers: dict,
|
||||
thinking_id: str,
|
||||
observations: List[Observation],
|
||||
log_prefix: str,
|
||||
chat_stream: ChatStream,
|
||||
shutting_down: bool = False,
|
||||
**kwargs,
|
||||
):
|
||||
"""初始化退出专注聊天动作处理器
|
||||
|
||||
Args:
|
||||
action_data: 动作数据
|
||||
reasoning: 执行该动作的理由
|
||||
cycle_timers: 计时器字典
|
||||
thinking_id: 思考ID
|
||||
observations: 观察列表
|
||||
log_prefix: 日志前缀
|
||||
shutting_down: 是否正在关闭
|
||||
"""
|
||||
super().__init__(action_data, reasoning, cycle_timers, thinking_id)
|
||||
self.observations = observations
|
||||
self.log_prefix = log_prefix
|
||||
self._shutting_down = shutting_down
|
||||
self.chat_id = chat_stream.stream_id
|
||||
|
||||
|
||||
|
||||
async def handle_action(self) -> Tuple[bool, str]:
|
||||
"""
|
||||
处理退出专注聊天的情况
|
||||
|
||||
工作流程:
|
||||
1. 将sub heartflow转换为normal_chat状态
|
||||
2. 等待新消息、超时或关闭信号
|
||||
3. 根据等待结果更新连续不回复计数
|
||||
4. 如果达到阈值,触发回调
|
||||
|
||||
Returns:
|
||||
Tuple[bool, str]: (是否执行成功, 状态转换消息)
|
||||
"""
|
||||
try:
|
||||
# 转换状态
|
||||
status_message = ""
|
||||
self.sub_heartflow = await heartflow.get_or_create_subheartflow(self.chat_id)
|
||||
if self.sub_heartflow:
|
||||
try:
|
||||
# 转换为normal_chat状态
|
||||
await self.sub_heartflow.change_chat_state(ChatState.NORMAL_CHAT)
|
||||
status_message = "已成功切换到普通聊天模式"
|
||||
logger.info(f"{self.log_prefix} {status_message}")
|
||||
except Exception as e:
|
||||
error_msg = f"切换到普通聊天模式失败: {str(e)}"
|
||||
logger.error(f"{self.log_prefix} {error_msg}")
|
||||
return False, error_msg
|
||||
else:
|
||||
warning_msg = "未找到有效的sub heartflow实例,无法切换状态"
|
||||
logger.warning(f"{self.log_prefix} {warning_msg}")
|
||||
return False, warning_msg
|
||||
|
||||
|
||||
return True, status_message
|
||||
|
||||
except asyncio.CancelledError:
|
||||
logger.info(f"{self.log_prefix} 处理 'exit_focus_chat' 时等待被中断 (CancelledError)")
|
||||
raise
|
||||
except Exception as e:
|
||||
error_msg = f"处理 'exit_focus_chat' 时发生错误: {str(e)}"
|
||||
logger.error(f"{self.log_prefix} {error_msg}")
|
||||
logger.error(traceback.format_exc())
|
||||
return False, error_msg
|
||||
@@ -6,14 +6,12 @@ from src.chat.focus_chat.planners.actions.base_action import BaseAction, registe
|
||||
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 # 连续不回复的阈值
|
||||
|
||||
|
||||
@register_action
|
||||
@@ -40,11 +38,7 @@ class NoReplyAction(BaseAction):
|
||||
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,
|
||||
**kwargs,
|
||||
):
|
||||
@@ -57,20 +51,12 @@ class NoReplyAction(BaseAction):
|
||||
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_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]:
|
||||
@@ -93,8 +79,6 @@ class NoReplyAction(BaseAction):
|
||||
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)
|
||||
|
||||
return True, "" # 不回复动作没有回复文本
|
||||
|
||||
|
||||
@@ -30,8 +30,6 @@ class PluginAction(BaseAction):
|
||||
self._services["expressor"] = kwargs["expressor"]
|
||||
if "chat_stream" in kwargs:
|
||||
self._services["chat_stream"] = kwargs["chat_stream"]
|
||||
if "current_cycle" in kwargs:
|
||||
self._services["current_cycle"] = kwargs["current_cycle"]
|
||||
|
||||
self.log_prefix = kwargs.get("log_prefix", "")
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@ from typing import Tuple, List
|
||||
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.chat.heart_flow.observation.chatting_observation import ChattingObservation
|
||||
from src.chat.focus_chat.hfc_utils import create_empty_anchor_message
|
||||
|
||||
@@ -41,7 +40,6 @@ class ReplyAction(BaseAction):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
action_name: str,
|
||||
action_data: dict,
|
||||
reasoning: str,
|
||||
cycle_timers: dict,
|
||||
@@ -49,7 +47,6 @@ class ReplyAction(BaseAction):
|
||||
observations: List[Observation],
|
||||
expressor: DefaultExpressor,
|
||||
chat_stream: ChatStream,
|
||||
current_cycle: CycleDetail,
|
||||
log_prefix: str,
|
||||
**kwargs,
|
||||
):
|
||||
@@ -64,14 +61,12 @@ class ReplyAction(BaseAction):
|
||||
observations: 观察列表
|
||||
expressor: 表达器
|
||||
chat_stream: 聊天流
|
||||
current_cycle: 当前循环信息
|
||||
log_prefix: 日志前缀
|
||||
"""
|
||||
super().__init__(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
|
||||
|
||||
async def handle_action(self) -> Tuple[bool, str]:
|
||||
|
||||
@@ -8,12 +8,12 @@ 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.action_info import ActionInfo
|
||||
from src.chat.focus_chat.info.structured_info import StructuredInfo
|
||||
from src.common.logger_manager import get_logger
|
||||
from src.chat.utils.prompt_builder import Prompt, global_prompt_manager
|
||||
from src.individuality.individuality import Individuality
|
||||
from src.chat.focus_chat.planners.action_manager import ActionManager
|
||||
from src.chat.focus_chat.planners.action_manager import ActionInfo
|
||||
|
||||
logger = get_logger("planner")
|
||||
|
||||
@@ -87,34 +87,68 @@ class ActionPlanner:
|
||||
|
||||
action = "no_reply" # 默认动作
|
||||
reasoning = "规划器初始化默认"
|
||||
action_data = {}
|
||||
|
||||
try:
|
||||
# 获取观察信息
|
||||
extra_info: list[str] = []
|
||||
|
||||
# 首先处理动作变更
|
||||
for info in all_plan_info:
|
||||
if isinstance(info, ActionInfo) and info.has_changes():
|
||||
add_actions = info.get_add_actions()
|
||||
remove_actions = info.get_remove_actions()
|
||||
reason = info.get_reason()
|
||||
|
||||
# 处理动作的增加
|
||||
for action_name in add_actions:
|
||||
if action_name in self.action_manager.get_registered_actions():
|
||||
self.action_manager.add_action_to_using(action_name)
|
||||
logger.debug(f"{self.log_prefix}添加动作: {action_name}, 原因: {reason}")
|
||||
|
||||
# 处理动作的移除
|
||||
for action_name in remove_actions:
|
||||
self.action_manager.remove_action_from_using(action_name)
|
||||
logger.debug(f"{self.log_prefix}移除动作: {action_name}, 原因: {reason}")
|
||||
|
||||
# 如果当前选择的动作被移除了,更新为no_reply
|
||||
if action in remove_actions:
|
||||
action = "no_reply"
|
||||
reasoning = f"之前选择的动作{action}已被移除,原因: {reason}"
|
||||
|
||||
# 继续处理其他信息
|
||||
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
|
||||
is_group_chat = (chat_type == "group")
|
||||
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()
|
||||
else:
|
||||
logger.debug(f"{self.log_prefix} 其他信息: {info}")
|
||||
elif not isinstance(info, ActionInfo): # 跳过已处理的ActionInfo
|
||||
extra_info.append(info.get_processed_info())
|
||||
|
||||
# 获取当前可用的动作
|
||||
current_available_actions = self.action_manager.get_using_actions()
|
||||
|
||||
# 如果没有可用动作,直接返回no_reply
|
||||
if not current_available_actions:
|
||||
logger.warning(f"{self.log_prefix}没有可用的动作,将使用no_reply")
|
||||
action = "no_reply"
|
||||
reasoning = "没有可用的动作"
|
||||
return {
|
||||
"action_result": {
|
||||
"action_type": action,
|
||||
"action_data": action_data,
|
||||
"reasoning": reasoning
|
||||
},
|
||||
"current_mind": current_mind,
|
||||
"observed_messages": observed_messages
|
||||
}
|
||||
|
||||
# --- 构建提示词 (调用修改后的 PromptBuilder 方法) ---
|
||||
prompt = await self.build_planner_prompt(
|
||||
@@ -181,7 +215,7 @@ class ActionPlanner:
|
||||
except Exception as outer_e:
|
||||
logger.error(f"{self.log_prefix}Planner 处理过程中发生意外错误,规划失败,将执行 no_reply: {outer_e}")
|
||||
traceback.print_exc()
|
||||
action = "no_reply" # 发生未知错误,标记为 error 动作
|
||||
action = "no_reply"
|
||||
reasoning = f"Planner 内部处理错误: {outer_e}"
|
||||
|
||||
logger.debug(
|
||||
@@ -202,7 +236,6 @@ class ActionPlanner:
|
||||
"observed_messages": observed_messages,
|
||||
}
|
||||
|
||||
# 返回结果字典
|
||||
return plan_result
|
||||
|
||||
async def build_planner_prompt(
|
||||
|
||||
Reference in New Issue
Block a user