refactor:重构聊天状态切换模式,移除限额,精简切换逻辑
This commit is contained in:
@@ -68,7 +68,6 @@ class HeartFChatting:
|
||||
self,
|
||||
chat_id: str,
|
||||
observations: list[Observation],
|
||||
on_consecutive_no_reply_callback: Callable[[], Coroutine[None, None, None]],
|
||||
):
|
||||
"""
|
||||
HeartFChatting 初始化函数
|
||||
@@ -76,12 +75,10 @@ class HeartFChatting:
|
||||
参数:
|
||||
chat_id: 聊天流唯一标识符(如stream_id)
|
||||
observations: 关联的观察列表
|
||||
on_consecutive_no_reply_callback: 连续不回复达到阈值时调用的异步回调函数
|
||||
"""
|
||||
# 基础属性
|
||||
self.stream_id: str = chat_id # 聊天流ID
|
||||
self.chat_stream: Optional[ChatStream] = None # 关联的聊天流
|
||||
self.on_consecutive_no_reply_callback = on_consecutive_no_reply_callback
|
||||
self.log_prefix: str = str(chat_id) # Initial default, will be updated
|
||||
self.hfcloop_observation = HFCloopObservation(observe_id=self.stream_id)
|
||||
self.chatting_observation = observations[0]
|
||||
@@ -165,7 +162,7 @@ class HeartFChatting:
|
||||
启动 HeartFChatting 的主循环。
|
||||
注意:调用此方法前必须确保已经成功初始化。
|
||||
"""
|
||||
logger.info(f"{self.log_prefix} 开始认真水群(HFC)...")
|
||||
logger.info(f"{self.log_prefix} 开始认真聊天(HFC)...")
|
||||
await self._start_loop_if_needed()
|
||||
|
||||
async def _start_loop_if_needed(self):
|
||||
@@ -463,11 +460,7 @@ class HeartFChatting:
|
||||
observations=self.all_observations,
|
||||
expressor=self.expressor,
|
||||
chat_stream=self.chat_stream,
|
||||
current_cycle=self._current_cycle,
|
||||
log_prefix=self.log_prefix,
|
||||
on_consecutive_no_reply_callback=self.on_consecutive_no_reply_callback,
|
||||
# total_no_reply_count=self.total_no_reply_count,
|
||||
# total_waiting_time=self.total_waiting_time,
|
||||
shutting_down=self._shutting_down,
|
||||
)
|
||||
|
||||
|
||||
@@ -234,7 +234,8 @@ class PromptBuilder:
|
||||
reply_style2=reply_style2_chosen,
|
||||
keywords_reaction_prompt=keywords_reaction_prompt,
|
||||
prompt_ger=prompt_ger,
|
||||
moderation_prompt=await global_prompt_manager.get_prompt_async("moderation_prompt"),
|
||||
# moderation_prompt=await global_prompt_manager.get_prompt_async("moderation_prompt"),
|
||||
moderation_prompt="",
|
||||
)
|
||||
else:
|
||||
template_name = "reasoning_prompt_private_main"
|
||||
@@ -256,7 +257,8 @@ class PromptBuilder:
|
||||
reply_style2=reply_style2_chosen,
|
||||
keywords_reaction_prompt=keywords_reaction_prompt,
|
||||
prompt_ger=prompt_ger,
|
||||
moderation_prompt=await global_prompt_manager.get_prompt_async("moderation_prompt"),
|
||||
# moderation_prompt=await global_prompt_manager.get_prompt_async("moderation_prompt"),
|
||||
moderation_prompt="",
|
||||
)
|
||||
# --- End choosing template ---
|
||||
|
||||
|
||||
83
src/chat/focus_chat/info/action_info.py
Normal file
83
src/chat/focus_chat/info/action_info.py
Normal file
@@ -0,0 +1,83 @@
|
||||
from typing import Dict, Optional, Any, List
|
||||
from dataclasses import dataclass
|
||||
from .info_base import InfoBase
|
||||
|
||||
|
||||
@dataclass
|
||||
class ActionInfo(InfoBase):
|
||||
"""动作信息类
|
||||
|
||||
用于管理和记录动作的变更信息,包括需要添加或移除的动作。
|
||||
继承自 InfoBase 类,使用字典存储具体数据。
|
||||
|
||||
Attributes:
|
||||
type (str): 信息类型标识符,固定为 "action"
|
||||
|
||||
Data Fields:
|
||||
add_actions (List[str]): 需要添加的动作列表
|
||||
remove_actions (List[str]): 需要移除的动作列表
|
||||
reason (str): 变更原因说明
|
||||
"""
|
||||
|
||||
type: str = "action"
|
||||
|
||||
def get_type(self) -> str:
|
||||
"""获取信息类型"""
|
||||
return self.type
|
||||
|
||||
def get_data(self) -> Dict[str, Any]:
|
||||
"""获取信息数据"""
|
||||
return self.data
|
||||
|
||||
def set_action_changes(self, action_changes: Dict[str, List[str]]) -> None:
|
||||
"""设置动作变更信息
|
||||
|
||||
Args:
|
||||
action_changes (Dict[str, List[str]]): 包含要增加和删除的动作列表
|
||||
{
|
||||
"add": ["action1", "action2"],
|
||||
"remove": ["action3"]
|
||||
}
|
||||
"""
|
||||
self.data["add_actions"] = action_changes.get("add", [])
|
||||
self.data["remove_actions"] = action_changes.get("remove", [])
|
||||
|
||||
def set_reason(self, reason: str) -> None:
|
||||
"""设置变更原因
|
||||
|
||||
Args:
|
||||
reason (str): 动作变更的原因说明
|
||||
"""
|
||||
self.data["reason"] = reason
|
||||
|
||||
def get_add_actions(self) -> List[str]:
|
||||
"""获取需要添加的动作列表
|
||||
|
||||
Returns:
|
||||
List[str]: 需要添加的动作列表
|
||||
"""
|
||||
return self.data.get("add_actions", [])
|
||||
|
||||
def get_remove_actions(self) -> List[str]:
|
||||
"""获取需要移除的动作列表
|
||||
|
||||
Returns:
|
||||
List[str]: 需要移除的动作列表
|
||||
"""
|
||||
return self.data.get("remove_actions", [])
|
||||
|
||||
def get_reason(self) -> Optional[str]:
|
||||
"""获取变更原因
|
||||
|
||||
Returns:
|
||||
Optional[str]: 动作变更的原因说明,如果未设置则返回 None
|
||||
"""
|
||||
return self.data.get("reason")
|
||||
|
||||
def has_changes(self) -> bool:
|
||||
"""检查是否有动作变更
|
||||
|
||||
Returns:
|
||||
bool: 如果有任何动作需要添加或移除则返回True
|
||||
"""
|
||||
return bool(self.get_add_actions() or self.get_remove_actions())
|
||||
126
src/chat/focus_chat/info_processors/action_processor.py
Normal file
126
src/chat/focus_chat/info_processors/action_processor.py
Normal file
@@ -0,0 +1,126 @@
|
||||
from typing import List, Optional, Any
|
||||
from src.chat.focus_chat.info.obs_info import ObsInfo
|
||||
from src.chat.heart_flow.observation.observation import Observation
|
||||
from src.chat.focus_chat.info.info_base import InfoBase
|
||||
from src.chat.focus_chat.info.action_info import ActionInfo
|
||||
from .base_processor import BaseProcessor
|
||||
from src.common.logger_manager import get_logger
|
||||
from src.chat.heart_flow.observation.chatting_observation import ChattingObservation
|
||||
from src.chat.heart_flow.observation.hfcloop_observation import HFCloopObservation
|
||||
from src.chat.focus_chat.info.cycle_info import CycleInfo
|
||||
from datetime import datetime
|
||||
from typing import Dict
|
||||
from src.chat.models.utils_model import LLMRequest
|
||||
from src.config.config import global_config
|
||||
import random
|
||||
|
||||
logger = get_logger("processor")
|
||||
|
||||
|
||||
class ActionProcessor(BaseProcessor):
|
||||
"""动作处理器
|
||||
|
||||
用于处理Observation对象,将其转换为ObsInfo对象。
|
||||
"""
|
||||
|
||||
log_prefix = "聊天信息处理"
|
||||
|
||||
def __init__(self):
|
||||
"""初始化观察处理器"""
|
||||
super().__init__()
|
||||
# TODO: API-Adapter修改标记
|
||||
self.model_summary = LLMRequest(
|
||||
model=global_config.model.observation, temperature=0.7, max_tokens=300, request_type="chat_observation"
|
||||
)
|
||||
|
||||
async def process_info(
|
||||
self,
|
||||
observations: Optional[List[Observation]] = None,
|
||||
running_memorys: Optional[List[Dict]] = None,
|
||||
**kwargs: Any,
|
||||
) -> List[InfoBase]:
|
||||
"""处理Observation对象
|
||||
|
||||
Args:
|
||||
infos: InfoBase对象列表
|
||||
observations: 可选的Observation对象列表
|
||||
**kwargs: 其他可选参数
|
||||
|
||||
Returns:
|
||||
List[InfoBase]: 处理后的ObsInfo实例列表
|
||||
"""
|
||||
# print(f"observations: {observations}")
|
||||
processed_infos = []
|
||||
|
||||
# 处理Observation对象
|
||||
if observations:
|
||||
for obs in observations:
|
||||
|
||||
if isinstance(obs, HFCloopObservation):
|
||||
|
||||
|
||||
# 创建动作信息
|
||||
action_info = ActionInfo()
|
||||
action_changes = await self.analyze_loop_actions(obs)
|
||||
if action_changes["add"] or action_changes["remove"]:
|
||||
action_info.set_action_changes(action_changes)
|
||||
# 设置变更原因
|
||||
reasons = []
|
||||
if action_changes["add"]:
|
||||
reasons.append(f"添加动作{action_changes['add']}因为检测到大量无回复")
|
||||
if action_changes["remove"]:
|
||||
reasons.append(f"移除动作{action_changes['remove']}因为检测到连续回复")
|
||||
action_info.set_reason(" | ".join(reasons))
|
||||
processed_infos.append(action_info)
|
||||
|
||||
return processed_infos
|
||||
|
||||
|
||||
async def analyze_loop_actions(self, obs: HFCloopObservation) -> Dict[str, List[str]]:
|
||||
"""分析最近的循环内容并决定动作的增减
|
||||
|
||||
Returns:
|
||||
Dict[str, List[str]]: 包含要增加和删除的动作
|
||||
{
|
||||
"add": ["action1", "action2"],
|
||||
"remove": ["action3"]
|
||||
}
|
||||
"""
|
||||
result = {"add": [], "remove": []}
|
||||
|
||||
# 获取最近10次循环
|
||||
recent_cycles = obs.history_loop[-10:] if len(obs.history_loop) > 10 else obs.history_loop
|
||||
if not recent_cycles:
|
||||
return result
|
||||
|
||||
# 统计no_reply的数量
|
||||
no_reply_count = 0
|
||||
reply_sequence = [] # 记录最近的动作序列
|
||||
|
||||
for cycle in recent_cycles:
|
||||
action_type = cycle.loop_plan_info["action_result"]["action_type"]
|
||||
if action_type == "no_reply":
|
||||
no_reply_count += 1
|
||||
reply_sequence.append(action_type == "reply")
|
||||
|
||||
# 检查no_reply比例
|
||||
if len(recent_cycles) >= 5 and (no_reply_count / len(recent_cycles)) >= 0.8:
|
||||
result["add"].append("exit_focus_chat")
|
||||
|
||||
# 获取最近三次的reply状态
|
||||
last_three = reply_sequence[-3:] if len(reply_sequence) >= 3 else reply_sequence
|
||||
|
||||
# 根据最近的reply情况决定是否移除reply动作
|
||||
if len(last_three) >= 3 and all(last_three):
|
||||
# 如果最近三次都是reply,直接移除
|
||||
result["remove"].append("reply")
|
||||
elif len(last_three) >= 2 and all(last_three[-2:]):
|
||||
# 如果最近两次都是reply,40%概率移除
|
||||
if random.random() < 0.4:
|
||||
result["remove"].append("reply")
|
||||
elif last_three and last_three[-1]:
|
||||
# 如果最近一次是reply,20%概率移除
|
||||
if random.random() < 0.2:
|
||||
result["remove"].append("reply")
|
||||
|
||||
return result
|
||||
@@ -22,7 +22,7 @@ logger = get_logger("processor")
|
||||
def init_prompt():
|
||||
indentify_prompt = """
|
||||
你的名字是{bot_name},你是一个AI,但是你伪装成了一个人类,你的人格是,{prompt_personality}。
|
||||
你对外的形象是一只橙色的鱼,头上有绿色的树叶,你用的头像也是这个。
|
||||
你的头像形象是一只橙色的鱼,头上有绿色的树叶。
|
||||
|
||||
{relation_prompt}
|
||||
{memory_str}
|
||||
@@ -36,6 +36,9 @@ def init_prompt():
|
||||
3. 你的自我认同是否有助于你的回答,如果你需要自我相关的信息来帮你参与聊天,请输出,否则请输出十个字以内的简短自我认同
|
||||
4. 一般情况下不用输出自我认同,只需要输出十几个字的简短自我认同就好,除非有明显需要自我认同的场景
|
||||
|
||||
请回复的平淡一些,简短一些,说中文,不要浮夸,平淡一些。
|
||||
请注意不要输出多余内容(包括前后缀,冒号和引号,括号(),表情包,at或 @等 )。只输出内容。
|
||||
|
||||
"""
|
||||
Prompt(indentify_prompt, "indentify_prompt")
|
||||
|
||||
|
||||
@@ -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